-
Notifications
You must be signed in to change notification settings - Fork 792
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
Mongo key encryption #1506
Merged
Merged
Mongo key encryption #1506
Changes from 7 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
191fbea
Refactor password based encryptor into PasswordBasedStringEncryptor a…
VakarisZ fd1cb9d
Add a secret to datastore encryptor
VakarisZ 4f17693
Split up the initialization of mongo_key into 2 parts: directory of m…
VakarisZ f97ec4e
Implement data store encryptor key removal on registration and unit t…
VakarisZ e280c4f
Move data store encryptor secret generation into the data store encry…
VakarisZ 4cbed6d
Fix typos and rename files/classes related to data store encryptor. C…
VakarisZ ddae092
Refactor test_data_store_encryptor.py to use (path / to / file).isfil…
VakarisZ b2bbb62
Add CHANGELOG.md entry for #1463 (Encrypt the database key with user'…
VakarisZ da169dd
Refactor DataStoreEncryptor by splitting up initialization related me…
VakarisZ 26ba02a
Refactor get_credentials_from_request to get_username_password_from_r…
VakarisZ 9d6dc3b
Move all encryptor building related code to encryptor_factory.py from…
VakarisZ 34d065c
Move encryptors into a separate folder
VakarisZ 3ec26bc
Refactor data store encryptor to IEncryptor interface, move data stor…
VakarisZ ea6fe37
Fix scoutsuite unit test to use updated datastore encryptor interface
VakarisZ a2b09a9
Fix unit tests for data store encryptor
VakarisZ 3b5dd6a
Remove database initialization during island startup
VakarisZ ddff2f0
Refactor a couple of imports into a shorter import statement
VakarisZ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 9 additions & 6 deletions
15
monkey/monkey_island/cc/server_utils/encryption/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
monkey/monkey_island/cc/server_utils/encryption/encryptor_factory.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
from ctypes import Union | ||
|
||
_factory: Union[None, EncryptorFactory] = None | ||
|
||
|
||
class EncryptorFactory: | ||
|
||
_KEY_FILENAME = "mongo_key.bin" | ||
|
||
def __init__(self, key_file_dir: str): | ||
self.key_file_path = os.path.join(key_file_dir, self._KEY_FILENAME) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer pathlib |
||
|
||
|
||
class FactoryNotInitializedError(Exception): | ||
pass | ||
|
||
|
||
def get_secret_from_credentials(username: str, password: str) -> str: | ||
return f"{username}:{password}" | ||
|
||
|
||
def remove_old_datastore_key(): | ||
if _factory is None: | ||
raise FactoryNotInitializedError | ||
if os.path.isfile(_factory.key_file_path): | ||
os.remove(_factory.key_file_path) | ||
|
||
|
||
def initialize_encryptor_factory(key_file_dir: str): | ||
global _factory | ||
_factory = EncryptorFactory(key_file_dir) | ||
|
||
|
||
def get_encryptor_factory(): | ||
return _factory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Factories are responsible for constructing objects. We should have
factory.set_key_file_path()
andfactory.set_secret()
and then callfactory.construct_data_store_encryptor()
or similar.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is not a factory, it's a class that handles initialization parameters for data store encryptor. I'm not sure what would be a better name, maybe
DataStoreEncryptorInitializer
? I don't see any benefits in making this an actual factory, since the output object will not be different depending on key file path and secret. What is more, the usage of such factory is less intuitive. The registration resource would have tofactory.set_secret()
thenbuilt_encryptor = factory.construct_data_store_encryptor()
and finallyset_encryptor(built_encryptor)
. Now it's only one call -initialize_datastore_encryptor(username, password)
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can simplify this way down. I'm going to dig into it a bit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll try to refactor and see how it looks