Skip to content

Delay mongo init to after registration#1495

Merged
VakarisZ merged 11 commits intodevelopfrom
delay-mongo-init
Sep 29, 2021
Merged

Delay mongo init to after registration#1495
VakarisZ merged 11 commits intodevelopfrom
delay-mongo-init

Conversation

@shreyamalviya
Copy link
Contributor

What does this PR do?

#1463

PR Checklist

  • Have you added an explanation of what your changes do and why you'd like to include them?
  • Is the TravisCI build passing?
  • Was the CHANGELOG.md updated to reflect the changes?
  • Was the documentation framework updated to reflect the changes?

Testing Checklist

  • Added relevant unit tests?
  • Have you successfully tested your changes locally? Elaborate:

    Tested by running the Island (deleted data dir, deleted DB, deleted server config, deleted creds)
    Tested by running Hugo.

  • If applicable, add screenshots or log transcripts of the feature working

image

@codecov
Copy link

codecov bot commented Sep 28, 2021

Codecov Report

Merging #1495 (349b381) into develop (b791ee1) will decrease coverage by 0.52%.
The diff coverage is 42.85%.

❗ Current head 349b381 differs from pull request most recent head 579ebf4. Consider uploading reports for the commit 579ebf4 to get more accurate results
Impacted file tree graph

@@ Coverage Diff @@ ## develop #1495 +/- ## =========================================== - Coverage 42.61% 42.09% -0.53%  =========================================== Files 476 468 -8 Lines 14193 14069 -124 =========================================== - Hits 6049 5922 -127  - Misses 8144 8147 +3 
Impacted Files Coverage Δ
monkey/monkey_island/cc/server_setup.py 0.00% <ø> (ø)
...ey/monkey_island/cc/resources/auth/registration.py 44.44% <35.71%> (-3.39%) ⬇️
monkey/monkey_island/cc/resources/auth/auth.py 55.76% <57.14%> (+0.21%) ⬆️
...d/cc/services/attack/technique_reports/__init__.py 45.07% <0.00%> (-21.26%) ⬇️
...land/cc/services/attack/technique_reports/T1197.py 61.53% <0.00%> (-2.75%) ⬇️
...land/cc/services/attack/technique_reports/T1035.py 70.00% <0.00%> (-2.73%) ⬇️
...land/cc/services/attack/technique_reports/T1106.py 70.00% <0.00%> (-2.73%) ⬇️
...land/cc/services/attack/technique_reports/T1090.py 50.00% <0.00%> (-2.64%) ⬇️
...land/cc/services/attack/technique_reports/T1041.py 45.00% <0.00%> (-2.62%) ⬇️
...land/cc/services/attack/technique_reports/T1064.py 66.66% <0.00%> (-2.57%) ⬇️
... and 55 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b791ee1...579ebf4. Read the comment docs.


if _credentials_match_registered_user(username, password):
access_token = _create_access_token(username)
_check_attack_mitigations_in_mongo()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This checks for attack mitigations during login? Why? Shouldn't it happen during registration?

Comment on lines +81 to +83
def _check_attack_mitigations_in_mongo():
if AttackMitigations.COLLECTION_NAME not in mongo.db.list_collection_names():
init_collections()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this function doesn't belong here since it has nothing to do with authentication's resource

Comment on lines +19 to +22
is_registration_needed = env_singleton.env.needs_registration()
if is_registration_needed:
# if registration is required, drop previous user's data (for credentials reset case)
_drop_mongo_db()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All this logic should be moved into init_collections method which gets called during registration. This way interaction with mongodb during registration is separated from the logic of resources. In general resources shouldn't interact with database directly.

Comment on lines +34 to +38
except Exception as ex:
logger.error(
"Exception raised during registration; most likely an issue with the "
f"mongo collection's initialisation. Exception: {str(ex)}."
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the error has to do with interaction with mongo we should catch it when interacting with mongo.

"Exception raised during registration; most likely an issue with the "
f"mongo collection's initialisation. Exception: {str(ex)}."
)
return make_response({"error": str(ex)}, 400)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the poin of this response? user can already see the error in logs and we don't handle 400 in any special way on the front end.



def _drop_mongo_db():
mongo.db.command("dropDatabase")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not mongo.drop_database('db')? Why are we even dropping the whole database if we're going to import the exact same mitigations?

Copy link
Contributor

@VakarisZ VakarisZ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly nit-picking on the readability, but some major questions as well.

CHANGELOG.md Outdated
Comment on lines +17 to +18
- Initialise MongoDB collection of attack mitigations after registration or login (if required)
instead of on Island startup. #1495
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is too low-level for the changelog.

Maybe a better entry is "Resetting credentials clears the database".

VakarisZ added a commit that referenced this pull request Sep 29, 2021
…esetting login credentials also cleans the contents of the database. #1495"
shreyamalviya and others added 10 commits September 29, 2021 16:44
of when registration request is sent The issue with this whole change is that there's a long gap where nothing happens after you click on the log in or register button on the UI. But we don't need to worry about this because we plan on shipping Island's mongodb with attack mitigations already present.
…esetting login credentials also cleans the contents of the database. #1495"
def reset_database():
Database.reset_db()
if Database.is_mitigations_missing():
logger.info("Populating Monkey Island with ATT&CK mitigations, this might take a while...")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This happens very quickly now (< 5ms). I think we can remove "this might take a while..."

Copy link
Contributor

@ilija-lazoroski ilija-lazoroski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job!

@VakarisZ VakarisZ merged commit f387595 into develop Sep 29, 2021
@VakarisZ VakarisZ deleted the delay-mongo-init branch September 29, 2021 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants