Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delay mongo init to after registration #1495

Merged
merged 11 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Changelog](https://keepachangelog.com/en/1.0.0/).
### Changed
- The name of the "Communicate as new user" post-breach action to "Communicate
as backdoor user". #1410
- Resetting login credentials also cleans the contents of the database. #1495
- ATT&CK report messages (more accurate now). #1483

### Removed
Expand Down
6 changes: 6 additions & 0 deletions docs/content/FAQ/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ When you first access the Monkey Island server, you'll be prompted to create an
To reset the credentials, edit the `server_config.json` file manually
(located in the [data directory](/reference/data_directory)).

{{% notice warning %}}
If you reset the credentials, the database will be cleared. Any findings of the Infection Monkey from previous runs will be lost. <br/><br/>
However, you can save the Monkey's existing configuration by logging in with your current credentials and clicking on the **Export config** button on the configuration page.
{{% /notice %}}


In order to reset the credentials, the following edits need to be made:
1. Delete the `user` field. It will look like this:
```json
Expand Down
8 changes: 7 additions & 1 deletion monkey/monkey_island/cc/resources/auth/registration.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import logging

import flask_restful
from flask import make_response, request
Expand All @@ -7,17 +8,22 @@
import monkey_island.cc.resources.auth.password_utils as password_utils
from common.utils.exceptions import InvalidRegistrationCredentialsError, RegistrationNotNeededError
from monkey_island.cc.environment.user_creds import UserCreds
from monkey_island.cc.setup.mongo.database_initializer import reset_database

logger = logging.getLogger(__name__)


class Registration(flask_restful.Resource):
def get(self):
return {"needs_registration": env_singleton.env.needs_registration()}
is_registration_needed = env_singleton.env.needs_registration()
return {"needs_registration": is_registration_needed}

def post(self):
credentials = _get_user_credentials_from_request(request)

try:
env_singleton.env.try_add_user(credentials)
reset_database()
return make_response({"error": ""}, 200)
except (InvalidRegistrationCredentialsError, RegistrationNotNeededError) as e:
return make_response({"error": str(e)}, 400)
Expand Down
3 changes: 0 additions & 3 deletions monkey/monkey_island/cc/server_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
from monkey_island.cc.setup.gevent_hub_error_handler import GeventHubErrorHandler # noqa: E402
from monkey_island.cc.setup.island_config_options import IslandConfigOptions # noqa: E402
from monkey_island.cc.setup.mongo import mongo_setup # noqa: E402
from monkey_island.cc.setup.mongo.database_initializer import init_collections # noqa: E402
from monkey_island.cc.setup.mongo.mongo_db_process import MongoDbProcess # noqa: E402

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -131,8 +130,6 @@ def _start_island_server(should_setup_only, config_options: IslandConfigOptions)
populate_exporter_list()
app = init_app(mongo_setup.MONGO_URL)

init_collections()

if should_setup_only:
logger.warning("Setup only flag passed. Exiting.")
return
Expand Down
4 changes: 4 additions & 0 deletions monkey/monkey_island/cc/services/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ def drop_collection(collection_name: str):
def init_db():
if not mongo.db.collection_names():
Database.reset_db()

@staticmethod
def is_mitigations_missing() -> bool:
return bool(AttackMitigations.COLLECTION_NAME not in mongo.db.list_collection_names())
9 changes: 6 additions & 3 deletions monkey/monkey_island/cc/setup/mongo/database_initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
from monkey_island.cc.database import mongo
from monkey_island.cc.models.attack.attack_mitigations import AttackMitigations
from monkey_island.cc.services.attack.mitre_api_interface import MitreApiInterface
from monkey_island.cc.services.database import Database

logger = logging.getLogger(__name__)


def init_collections():
logger.info("Setting up the Monkey Island, this might take a while...")
_try_store_mitigations_on_mongo()
def reset_database():
Database.reset_db()
if Database.is_mitigations_missing():
logger.info("Populating Monkey Island with ATT&CK mitigations.")
_try_store_mitigations_on_mongo()


def _try_store_mitigations_on_mongo():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import {Row, Col, Container, Form, Button} from 'react-bootstrap';
import AuthService from '../../services/AuthService';
import monkeyDetective from '../../images/detective-monkey.svg';
import ParticleBackground from '../ui-components/ParticleBackground';
import LoadingIcon from '../ui-components/LoadingIcon';

class RegisterPageComponent extends React.Component {

register = (event) => {
event.preventDefault();
this.setState({loading: true})
this.auth.register(this.username, this.password).then(res => {
this.setState({failed: false, error: ''});
if (res['result']) {
Expand Down Expand Up @@ -68,7 +70,12 @@ class RegisterPageComponent extends React.Component {
<Form.Control onChange={evt => this.updateUsername(evt)} type='text' placeholder='Username'/>
<Form.Control onChange={evt => this.updatePassword(evt)} type='password' placeholder='Password'/>
<Button className={'monkey-submit-button'} type={'submit'} >
Let's go!
{
this.state.loading ?
<LoadingIcon/>
:
'Let\'s go!'
}
</Button>
<Row>
<Col>
Expand Down
8 changes: 8 additions & 0 deletions monkey/monkey_island/cc/ui/src/styles/pages/AuthPage.scss
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,11 @@
margin-bottom: 20px;
text-align: center;
}

.auth-container .monkey-submit-button:hover .loading-icon {
color: $monkey-black;
}

.auth-container .monkey-submit-button:focus .loading-icon {
color: $monkey-black;
}