-
Notifications
You must be signed in to change notification settings - Fork 46
Added AWS kms support #105
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
382ba9b
Refactor
avara1986 ad18008
Refactor 2
avara1986 d7f0f12
Fix pylint
avara1986 2613091
Fix pylint
avara1986 6459ddf
Updated conf with encryption
avara1986 9fd8c29
Fix singleton service initialization
avara1986 444f767
Added AWS KMS support
avara1986 487d4ca
Updated tests
avara1986 388c8e7
mock Init boto
avara1986 7666788
Dynamic import not working with moto
avara1986 458f26f
Removed base64
avara1986 13f5752
Updated docs
avara1986 9dc3d39
Updated tests
avara1986 b97b301
Updated tests
avara1986 263a459
Added encrypt method
avara1986 1126037
Removed optional 64
avara1986 7a295db
Updated docs
avara1986 63b5248
Updated docs and examples
avara1986 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 hidden or 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
Empty file.
This file contains hidden or 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,12 @@ | ||
pyms: | ||
crypt: | ||
method: "aws_kms" | ||
key_id: "alias/prueba-avara" | ||
config: | ||
DEBUG: true | ||
TESTING: false | ||
SWAGGER: true | ||
APP_NAME: business-glossary | ||
APPLICATION_ROOT : "" | ||
SECRET_KEY: "gjr39dkjn344_!67#" | ||
enc_encrypted_key: "AQICAHiALhLQv4eW8jqUccFSnkyDkBAWLAm97Lr2qmdItkUCIAEVoPzSHLW+If9sxSRJ420jAAAAoDCBnQYJKoZIhvcNAQcGoIGPMIGMAgEAMIGGBgkqhkiG9w0BBwEwHgYJYIZIAWUDBAEuMBEEDHoNko2L0A0m/r/h9QIBEIBZPsxFUeHFQzEacdLde5eeJRTHw8e0eSwG7UkJzc+ZdBp1xS9DyqBsHQw4Xnx58iQxCgH6ivRKOraZGKX5ebIZUrw/d+XD8YmbdCosx/TwnHVLneehSbWjF1c=" |
This file contains hidden or 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,17 @@ | ||
from base64 import b64decode | ||
|
||
from flask import jsonify | ||
|
||
from pyms.flask.app import Microservice | ||
|
||
ms = Microservice() | ||
app = ms.create_app() | ||
|
||
|
||
@app.route("/") | ||
def example(): | ||
return jsonify({"main": app.ms.config.encrypted_key}) | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run() |
This file contains hidden or 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 hidden or 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,44 @@ | ||
import base64 | ||
|
||
from pyms.crypt.driver import CryptAbstract | ||
from pyms.utils import check_package_exists, import_package | ||
|
||
|
||
class Crypt(CryptAbstract): | ||
encryption_algorithm = "SYMMETRIC_DEFAULT" # 'SYMMETRIC_DEFAULT' | 'RSAES_OAEP_SHA_1' | 'RSAES_OAEP_SHA_256' | ||
key_id = "" | ||
|
||
def __init__(self, *args, **kwargs): | ||
self._init_boto() | ||
super().__init__(*args, **kwargs) | ||
|
||
def encrypt(self, message): # pragma: no cover | ||
ciphertext = self.client.encrypt( | ||
KeyId=self.config.key_id, | ||
Plaintext=bytes(message, encoding="UTF-8"), | ||
) | ||
return str(base64.b64encode(ciphertext["CiphertextBlob"]), encoding="UTF-8") | ||
|
||
def _init_boto(self): # pragma: no cover | ||
check_package_exists("boto3") | ||
boto3 = import_package("boto3") | ||
boto3.set_stream_logger(name='botocore') | ||
self.client = boto3.client('kms') | ||
|
||
def _aws_decrypt(self, blob_text): # pragma: no cover | ||
response = self.client.decrypt( | ||
CiphertextBlob=blob_text, | ||
KeyId=self.config.key_id, | ||
EncryptionAlgorithm=self.encryption_algorithm | ||
) | ||
return str(response['Plaintext'], encoding="UTF-8") | ||
|
||
def _parse_encrypted(self, encrypted): | ||
blob_text = base64.b64decode(encrypted) | ||
return blob_text | ||
|
||
def decrypt(self, encrypted): | ||
blob_text = self._parse_encrypted(encrypted) | ||
decrypted = self._aws_decrypt(blob_text) | ||
|
||
return decrypted |
This file contains hidden or 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 hidden or 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 hidden or 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,9 @@ | ||
from pyms.config import get_conf | ||
|
||
|
||
class ConfigResource: | ||
|
||
config_resource = None | ||
|
||
def __init__(self, *args, **kwargs): | ||
self.config = get_conf(service=self.config_resource, empty_init=True, uppercase=False, *args, **kwargs) |
This file contains hidden or 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
Empty file.
This file contains hidden or 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,53 @@ | ||
import logging | ||
from abc import ABC, abstractmethod | ||
|
||
from pyms.config.resource import ConfigResource | ||
from pyms.constants import CRYPT_BASE, LOGGER_NAME | ||
from pyms.utils import import_from | ||
|
||
logger = logging.getLogger(LOGGER_NAME) | ||
|
||
CRYPT_RESOURCES_CLASS = "Crypt" | ||
|
||
|
||
class CryptAbstract(ABC): | ||
|
||
def __init__(self, *args, **kwargs): | ||
self.config = kwargs.get("config") | ||
|
||
@abstractmethod | ||
def encrypt(self, message): | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def decrypt(self, encrypted): | ||
raise NotImplementedError | ||
|
||
|
||
class CryptNone(CryptAbstract): | ||
|
||
def encrypt(self, message): | ||
return message | ||
|
||
def decrypt(self, encrypted): | ||
return encrypted | ||
|
||
|
||
class CryptResource(ConfigResource): | ||
"""This class works between `pyms.flask.create_app.Microservice` and `pyms.flask.services.[THESERVICE]`. Search | ||
for a file with the name you want to load, set the configuration and return a instance of the class you want | ||
""" | ||
config_resource = CRYPT_BASE | ||
|
||
def get_crypt(self, *args, **kwargs) -> CryptAbstract: | ||
if self.config.method == "fernet": | ||
crypt_object = import_from("pyms.crypt.fernet", CRYPT_RESOURCES_CLASS) | ||
elif self.config.method == "aws_kms": | ||
crypt_object = import_from("pyms.cloud.aws.kms", CRYPT_RESOURCES_CLASS) | ||
else: | ||
crypt_object = CryptNone | ||
logger.debug("Init crypt {}".format(crypt_object)) | ||
return crypt_object(config=self.config, *args, **kwargs) | ||
|
||
def __call__(self, *args, **kwargs): | ||
return self.get_crypt(*args, **kwargs) |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.