Skip to content

Commit f387595

Browse files
authored
Merge pull request #1495 from guardicore/delay-mongo-init
Delay mongo init to after registration
2 parents 51f179c + 7939ed4 commit f387595

File tree

8 files changed

+40
-8
lines changed

8 files changed

+40
-8
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Changelog](https://keepachangelog.com/en/1.0.0/).
1414
### Changed
1515
- The name of the "Communicate as new user" post-breach action to "Communicate
1616
as backdoor user". #1410
17+
- Resetting login credentials also cleans the contents of the database. #1495
1718
- ATT&CK report messages (more accurate now). #1483
1819

1920
### Removed

docs/content/FAQ/_index.md

+6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ When you first access the Monkey Island server, you'll be prompted to create an
5656
To reset the credentials, edit the `server_config.json` file manually
5757
(located in the [data directory](/reference/data_directory)).
5858

59+
{{% notice warning %}}
60+
If you reset the credentials, the database will be cleared. Any findings of the Infection Monkey from previous runs will be lost. <br/><br/>
61+
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.
62+
{{% /notice %}}
63+
64+
5965
In order to reset the credentials, the following edits need to be made:
6066
1. Delete the `user` field. It will look like this:
6167
```json

monkey/monkey_island/cc/resources/auth/registration.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import logging
23

34
import flask_restful
45
from flask import make_response, request
@@ -7,17 +8,22 @@
78
import monkey_island.cc.resources.auth.password_utils as password_utils
89
from common.utils.exceptions import InvalidRegistrationCredentialsError, RegistrationNotNeededError
910
from monkey_island.cc.environment.user_creds import UserCreds
11+
from monkey_island.cc.setup.mongo.database_initializer import reset_database
12+
13+
logger = logging.getLogger(__name__)
1014

1115

1216
class Registration(flask_restful.Resource):
1317
def get(self):
14-
return {"needs_registration": env_singleton.env.needs_registration()}
18+
is_registration_needed = env_singleton.env.needs_registration()
19+
return {"needs_registration": is_registration_needed}
1520

1621
def post(self):
1722
credentials = _get_user_credentials_from_request(request)
1823

1924
try:
2025
env_singleton.env.try_add_user(credentials)
26+
reset_database()
2127
return make_response({"error": ""}, 200)
2228
except (InvalidRegistrationCredentialsError, RegistrationNotNeededError) as e:
2329
return make_response({"error": str(e)}, 400)

monkey/monkey_island/cc/server_setup.py

-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
from monkey_island.cc.setup.gevent_hub_error_handler import GeventHubErrorHandler # noqa: E402
3737
from monkey_island.cc.setup.island_config_options import IslandConfigOptions # noqa: E402
3838
from monkey_island.cc.setup.mongo import mongo_setup # noqa: E402
39-
from monkey_island.cc.setup.mongo.database_initializer import init_collections # noqa: E402
4039
from monkey_island.cc.setup.mongo.mongo_db_process import MongoDbProcess # noqa: E402
4140

4241
logger = logging.getLogger(__name__)
@@ -131,8 +130,6 @@ def _start_island_server(should_setup_only, config_options: IslandConfigOptions)
131130
populate_exporter_list()
132131
app = init_app(mongo_setup.MONGO_URL)
133132

134-
init_collections()
135-
136133
if should_setup_only:
137134
logger.warning("Setup only flag passed. Exiting.")
138135
return

monkey/monkey_island/cc/services/database.py

+4
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ def drop_collection(collection_name: str):
3737
def init_db():
3838
if not mongo.db.collection_names():
3939
Database.reset_db()
40+
41+
@staticmethod
42+
def is_mitigations_missing() -> bool:
43+
return bool(AttackMitigations.COLLECTION_NAME not in mongo.db.list_collection_names())

monkey/monkey_island/cc/setup/mongo/database_initializer.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
from monkey_island.cc.database import mongo
66
from monkey_island.cc.models.attack.attack_mitigations import AttackMitigations
77
from monkey_island.cc.services.attack.mitre_api_interface import MitreApiInterface
8+
from monkey_island.cc.services.database import Database
89

910
logger = logging.getLogger(__name__)
1011

1112

12-
def init_collections():
13-
logger.info("Setting up the Monkey Island, this might take a while...")
14-
_try_store_mitigations_on_mongo()
13+
def reset_database():
14+
Database.reset_db()
15+
if Database.is_mitigations_missing():
16+
logger.info("Populating Monkey Island with ATT&CK mitigations.")
17+
_try_store_mitigations_on_mongo()
1518

1619

1720
def _try_store_mitigations_on_mongo():

monkey/monkey_island/cc/ui/src/components/pages/RegisterPage.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import {Row, Col, Container, Form, Button} from 'react-bootstrap';
44
import AuthService from '../../services/AuthService';
55
import monkeyDetective from '../../images/detective-monkey.svg';
66
import ParticleBackground from '../ui-components/ParticleBackground';
7+
import LoadingIcon from '../ui-components/LoadingIcon';
78

89
class RegisterPageComponent extends React.Component {
910

1011
register = (event) => {
1112
event.preventDefault();
13+
this.setState({loading: true})
1214
this.auth.register(this.username, this.password).then(res => {
1315
this.setState({failed: false, error: ''});
1416
if (res['result']) {
@@ -68,7 +70,12 @@ class RegisterPageComponent extends React.Component {
6870
<Form.Control onChange={evt => this.updateUsername(evt)} type='text' placeholder='Username'/>
6971
<Form.Control onChange={evt => this.updatePassword(evt)} type='password' placeholder='Password'/>
7072
<Button className={'monkey-submit-button'} type={'submit'} >
71-
Let's go!
73+
{
74+
this.state.loading ?
75+
<LoadingIcon/>
76+
:
77+
'Let\'s go!'
78+
}
7279
</Button>
7380
<Row>
7481
<Col>

monkey/monkey_island/cc/ui/src/styles/pages/AuthPage.scss

+8
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,11 @@
3535
margin-bottom: 20px;
3636
text-align: center;
3737
}
38+
39+
.auth-container .monkey-submit-button:hover .loading-icon {
40+
color: $monkey-black;
41+
}
42+
43+
.auth-container .monkey-submit-button:focus .loading-icon {
44+
color: $monkey-black;
45+
}

0 commit comments

Comments
 (0)