From a601cbf809e6dc3b77bc43d44dec809f130ad291 Mon Sep 17 00:00:00 2001 From: zane Date: Tue, 5 Jun 2018 22:11:00 -0700 Subject: [PATCH 1/9] Initial commit --- README.md | 2 ++ moto/__init__.py | 1 + moto/backends.py | 2 ++ moto/secretsmanager/__init__.py | 6 ++++++ 4 files changed, 11 insertions(+) create mode 100644 moto/secretsmanager/__init__.py diff --git a/README.md b/README.md index 3fbee44f8..8d70f2e08 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,8 @@ It gets even better! Moto isn't just for Python code and it isn't just for S3. L |------------------------------------------------------------------------------| | S3 | @mock_s3 | core endpoints done | |------------------------------------------------------------------------------| +| Secrets Manager | @mock_secretsmanager | basic endpoints done +|------------------------------------------------------------------------------| | SES | @mock_ses | all endpoints done | |------------------------------------------------------------------------------| | SNS | @mock_sns | all endpoints done | diff --git a/moto/__init__.py b/moto/__init__.py index 5e6f71b7a..0ce5e54d1 100644 --- a/moto/__init__.py +++ b/moto/__init__.py @@ -34,6 +34,7 @@ from .rds2 import mock_rds2, mock_rds2_deprecated # flake8: noqa from .redshift import mock_redshift, mock_redshift_deprecated # flake8: noqa from .s3 import mock_s3, mock_s3_deprecated # flake8: noqa from .ses import mock_ses, mock_ses_deprecated # flake8: noqa +from .secretsmanager import mock_secretsmanager # flake8: noqa from .sns import mock_sns, mock_sns_deprecated # flake8: noqa from .sqs import mock_sqs, mock_sqs_deprecated # flake8: noqa from .sts import mock_sts, mock_sts_deprecated # flake8: noqa diff --git a/moto/backends.py b/moto/backends.py index 496af13e1..cd8fe174f 100644 --- a/moto/backends.py +++ b/moto/backends.py @@ -32,6 +32,7 @@ from moto.redshift import redshift_backends from moto.route53 import route53_backends from moto.s3 import s3_backends from moto.ses import ses_backends +from moto.secretsmanager import secretsmanager_backends from moto.sns import sns_backends from moto.sqs import sqs_backends from moto.ssm import ssm_backends @@ -77,6 +78,7 @@ BACKENDS = { 's3': s3_backends, 's3bucket_path': s3_backends, 'ses': ses_backends, + 'secretsmanager': secretsmanager_backends, 'sns': sns_backends, 'sqs': sqs_backends, 'ssm': ssm_backends, diff --git a/moto/secretsmanager/__init__.py b/moto/secretsmanager/__init__.py new file mode 100644 index 000000000..bfc31930d --- /dev/null +++ b/moto/secretsmanager/__init__.py @@ -0,0 +1,6 @@ +from __future__ import unicode_literals +from .models import secretsmanager_backends +from ..core.models import base_decorator, deprecated_base_decorator + +secretsmanager_backend = secretsmanager_backends['us-east-1'] +mock_secretsmanager = base_decorator(secretsmanager_backends) From 1f5d51d62c5d219d78643b5c8ae7eb4dde02c993 Mon Sep 17 00:00:00 2001 From: zane Date: Tue, 5 Jun 2018 22:15:24 -0700 Subject: [PATCH 2/9] SecretsManager one word --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8d70f2e08..a6926a58f 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,7 @@ It gets even better! Moto isn't just for Python code and it isn't just for S3. L |------------------------------------------------------------------------------| | S3 | @mock_s3 | core endpoints done | |------------------------------------------------------------------------------| -| Secrets Manager | @mock_secretsmanager | basic endpoints done +| SecretsManager | @mock_secretsmanager | basic endpoints done |------------------------------------------------------------------------------| | SES | @mock_ses | all endpoints done | |------------------------------------------------------------------------------| From 99bbe6b86a5b5ec24acaf2b29762c975d23347b9 Mon Sep 17 00:00:00 2001 From: zane Date: Thu, 7 Jun 2018 22:07:20 -0700 Subject: [PATCH 3/9] Adding models with get_secret_vaule function --- moto/secretsmanager/models.py | 49 +++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 moto/secretsmanager/models.py diff --git a/moto/secretsmanager/models.py b/moto/secretsmanager/models.py new file mode 100644 index 000000000..d5ff69248 --- /dev/null +++ b/moto/secretsmanager/models.py @@ -0,0 +1,49 @@ +from __future__ import unicode_literals + +import datetime +import json + +import boto.secretsmanager + +from moto.core import BaseBackend, BaseModel + +class SecretsManager(BaseModel): + + def __init__(self, region, **kwargs): + self.secret_id = kwargs.get('secret_id', '') + self.version_id = kwargs.get('version_id', '') + self.string_id = kwargs.get('string_id', '') + +class SecretsManagerBackend(BaseBackend): + + def __init__(self, region): + super(SecretsManagerBackend, self).__init__() + self.region = region + + def get_secret_value(self, secret_id, version_id, string_id): + + secret_response = SecretsManager(self.region, secret_id=secret_id, + version_id=version_id, + string_id=string_id) + + response = json.dumps({ + "ARN": secret_arn, + "Name": self.secret_id, + "VersionId": "A435958A-D821-4193-B719-B7769357AER4", + "SecretBinary": b"testbytes", + "SecretString": "mysecretstring", + "VersionStages": [ + "AWSCURRENT", + ], + "CreatedDate": datetime.datetime.utcnow() + }) + + return response + + def secret_arn(self): + return "arn:aws:secretsmanager:{0}:1234567890:secret:{1}-rIjad".format( + self.region, self.secret_id) + +secretsmanager_backends = {} +for regin in boto.secretsmanager.regions(): + secretsmanager_backends[region.name] = SecretsManagerBackend(region.name) From 6e893ff055eb9ebf41e9a1ea8347acafa7e7a344 Mon Sep 17 00:00:00 2001 From: zane Date: Thu, 7 Jun 2018 22:31:44 -0700 Subject: [PATCH 4/9] Adding response, urls and test --- moto/secretsmanager/responses.py | 17 +++++++++++++++++ moto/secretsmanager/urls.py | 10 ++++++++++ .../test_secretsmanager/test_secretsmanager.py | 13 +++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 moto/secretsmanager/responses.py create mode 100644 moto/secretsmanager/urls.py create mode 100644 tests/test_secretsmanager/test_secretsmanager.py diff --git a/moto/secretsmanager/responses.py b/moto/secretsmanager/responses.py new file mode 100644 index 000000000..10d67ae6e --- /dev/null +++ b/moto/secretsmanager/responses.py @@ -0,0 +1,17 @@ +from __future__ import unicode_literals + +from moto.core.responses import BaseResponse + +from .models import secretsmanager_backends + + +class SecretsManagerResponse(BaseResponse): + + def get_secret_value(self): + secret_id = self.get_param('SecretId') + version_id = self.get_param('VersionId') + version_stage = self.get_param('VersionStage') + return secretsmanager_backends[self.region].get_secret_value( + secret_id=secret_id, + version_id=version_id, + version_stage=version_stage) diff --git a/moto/secretsmanager/urls.py b/moto/secretsmanager/urls.py new file mode 100644 index 000000000..9e39e7263 --- /dev/null +++ b/moto/secretsmanager/urls.py @@ -0,0 +1,10 @@ +from __future__ import unicode_literals +from .responses import SecretsManagerResponse + +url_bases = [ + "https?://secretsmanager.(.+).amazonaws.com", +] + +url_paths = { + '{0}/$': SecretsManagerResponse.dispatch, +} diff --git a/tests/test_secretsmanager/test_secretsmanager.py b/tests/test_secretsmanager/test_secretsmanager.py new file mode 100644 index 000000000..b80e13503 --- /dev/null +++ b/tests/test_secretsmanager/test_secretsmanager.py @@ -0,0 +1,13 @@ +from __future__ import unicode_literals + +import boto3 + +from moto import mock_secretsmanager +import sure # noqa + +@mock_secretsmanager +def test_get_secret_value(): + conn = boto3.client('secretsmanager', 'us-west-2') + + result = conn.get_secret_value(SecretId='java-util-test-password') + assert result['SecretString'] == 'mysecretstring' From c4abd98ed3545e9bddc9a488df4f8a8ae5823fb1 Mon Sep 17 00:00:00 2001 From: zane Date: Thu, 7 Jun 2018 22:53:34 -0700 Subject: [PATCH 5/9] Adding server test for get_secret_value --- tests/test_secretsmanager/test_server.py | 27 ++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 tests/test_secretsmanager/test_server.py diff --git a/tests/test_secretsmanager/test_server.py b/tests/test_secretsmanager/test_server.py new file mode 100644 index 000000000..9089c9407 --- /dev/null +++ b/tests/test_secretsmanager/test_server.py @@ -0,0 +1,27 @@ +from __future__ import unicode_literals + +import json +import sure # noqa + +import moto.server as server +from moto import mock_secretsmanager + +''' +Test the different server responses +''' + + +@mock_secretsmanager +def test_get_secret_value(): + + backend = server.create_backend_app("secretsmanager-get-secret-value") + test_client = backend.test_client() + + res = test_client.post('/', + data={"SecretId": "test", "VersionStage": "AWSCURRENT"}, + headers={ + "X-Amz-Target": "secretsmanager.GetSecretValue"}, + ) + + json_data = json.loads(res.data.decode("utf-8")) + assert json_data['SecretId'] == "test" From 8ee7a9495df56373c71f0c8767e16988c9cfa714 Mon Sep 17 00:00:00 2001 From: zane Date: Sat, 9 Jun 2018 22:08:10 -0700 Subject: [PATCH 6/9] UPdate --- Makefile | 6 ++--- moto/secretsmanager/__init__.py | 2 +- moto/secretsmanager/models.py | 22 +++++++++---------- .../test_secretsmanager.py | 2 +- 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index f224d7091..e7aafba70 100644 --- a/Makefile +++ b/Makefile @@ -9,8 +9,8 @@ else endif init: - @python setup.py develop - @pip install -r requirements.txt + @python3 setup.py develop + @pip3 install -r requirements.txt lint: flake8 moto @@ -18,7 +18,7 @@ lint: test: lint rm -f .coverage rm -rf cover - @nosetests -sv --with-coverage --cover-html ./tests/ $(TEST_EXCLUDE) + nosetests -sv --with-coverage --cover-html ./tests/ $(TEST_EXCLUDE) test_server: @TEST_SERVER_MODE=true nosetests -sv --with-coverage --cover-html ./tests/ diff --git a/moto/secretsmanager/__init__.py b/moto/secretsmanager/__init__.py index bfc31930d..c7fbb2869 100644 --- a/moto/secretsmanager/__init__.py +++ b/moto/secretsmanager/__init__.py @@ -1,6 +1,6 @@ from __future__ import unicode_literals from .models import secretsmanager_backends -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator secretsmanager_backend = secretsmanager_backends['us-east-1'] mock_secretsmanager = base_decorator(secretsmanager_backends) diff --git a/moto/secretsmanager/models.py b/moto/secretsmanager/models.py index d5ff69248..4d95be48b 100644 --- a/moto/secretsmanager/models.py +++ b/moto/secretsmanager/models.py @@ -3,31 +3,28 @@ from __future__ import unicode_literals import datetime import json -import boto.secretsmanager +import boto3 from moto.core import BaseBackend, BaseModel + class SecretsManager(BaseModel): - def __init__(self, region, **kwargs): + def __init__(self, region_name, **kwargs): self.secret_id = kwargs.get('secret_id', '') self.version_id = kwargs.get('version_id', '') self.string_id = kwargs.get('string_id', '') class SecretsManagerBackend(BaseBackend): - def __init__(self, region): + def __init__(self, region_name=None): super(SecretsManagerBackend, self).__init__() - self.region = region + self.region = region_name def get_secret_value(self, secret_id, version_id, string_id): - secret_response = SecretsManager(self.region, secret_id=secret_id, - version_id=version_id, - string_id=string_id) - response = json.dumps({ - "ARN": secret_arn, + "ARN": self.secret_arn(), "Name": self.secret_id, "VersionId": "A435958A-D821-4193-B719-B7769357AER4", "SecretBinary": b"testbytes", @@ -44,6 +41,7 @@ class SecretsManagerBackend(BaseBackend): return "arn:aws:secretsmanager:{0}:1234567890:secret:{1}-rIjad".format( self.region, self.secret_id) -secretsmanager_backends = {} -for regin in boto.secretsmanager.regions(): - secretsmanager_backends[region.name] = SecretsManagerBackend(region.name) + +available_regions = boto3.session.Session().get_available_regions("secretsmanager") +print(available_regions) +secretsmanager_backends = {region: SecretsManagerBackend(region_name=region) for region in available_regions} diff --git a/tests/test_secretsmanager/test_secretsmanager.py b/tests/test_secretsmanager/test_secretsmanager.py index b80e13503..df4f0f69e 100644 --- a/tests/test_secretsmanager/test_secretsmanager.py +++ b/tests/test_secretsmanager/test_secretsmanager.py @@ -7,7 +7,7 @@ import sure # noqa @mock_secretsmanager def test_get_secret_value(): - conn = boto3.client('secretsmanager', 'us-west-2') + conn = boto3.client('secretsmanager', region_name='us-west-2') result = conn.get_secret_value(SecretId='java-util-test-password') assert result['SecretString'] == 'mysecretstring' From b1a8ae829961e105fc1d96adb5355cd696db4232 Mon Sep 17 00:00:00 2001 From: zane Date: Sat, 9 Jun 2018 23:42:33 -0700 Subject: [PATCH 7/9] Updating tests --- moto/secretsmanager/models.py | 13 +++++++------ moto/secretsmanager/responses.py | 6 +++--- tests/test_secretsmanager/test_server.py | 4 ++-- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/moto/secretsmanager/models.py b/moto/secretsmanager/models.py index 4d95be48b..85f9c8a51 100644 --- a/moto/secretsmanager/models.py +++ b/moto/secretsmanager/models.py @@ -1,6 +1,6 @@ from __future__ import unicode_literals -import datetime +import time import json import boto3 @@ -13,26 +13,27 @@ class SecretsManager(BaseModel): def __init__(self, region_name, **kwargs): self.secret_id = kwargs.get('secret_id', '') self.version_id = kwargs.get('version_id', '') - self.string_id = kwargs.get('string_id', '') + self.version_stage = kwargs.get('version_stage', '') class SecretsManagerBackend(BaseBackend): - def __init__(self, region_name=None): + def __init__(self, region_name=None, **kwargs): super(SecretsManagerBackend, self).__init__() self.region = region_name + self.secret_id = kwargs.get('secret_id', '') + self.createdate = int(time.time()) - def get_secret_value(self, secret_id, version_id, string_id): + def get_secret_value(self, secret_id, version_id, version_stage): response = json.dumps({ "ARN": self.secret_arn(), "Name": self.secret_id, "VersionId": "A435958A-D821-4193-B719-B7769357AER4", - "SecretBinary": b"testbytes", "SecretString": "mysecretstring", "VersionStages": [ "AWSCURRENT", ], - "CreatedDate": datetime.datetime.utcnow() + "CreatedDate": "2018-05-23 13:16:57.198000" }) return response diff --git a/moto/secretsmanager/responses.py b/moto/secretsmanager/responses.py index 10d67ae6e..144a254ec 100644 --- a/moto/secretsmanager/responses.py +++ b/moto/secretsmanager/responses.py @@ -8,9 +8,9 @@ from .models import secretsmanager_backends class SecretsManagerResponse(BaseResponse): def get_secret_value(self): - secret_id = self.get_param('SecretId') - version_id = self.get_param('VersionId') - version_stage = self.get_param('VersionStage') + secret_id = self._get_param('SecretId') + version_id = self._get_param('VersionId') + version_stage = self._get_param('VersionStage') return secretsmanager_backends[self.region].get_secret_value( secret_id=secret_id, version_id=version_id, diff --git a/tests/test_secretsmanager/test_server.py b/tests/test_secretsmanager/test_server.py index 9089c9407..142e9fe7d 100644 --- a/tests/test_secretsmanager/test_server.py +++ b/tests/test_secretsmanager/test_server.py @@ -14,7 +14,7 @@ Test the different server responses @mock_secretsmanager def test_get_secret_value(): - backend = server.create_backend_app("secretsmanager-get-secret-value") + backend = server.create_backend_app("secretsmanager") test_client = backend.test_client() res = test_client.post('/', @@ -24,4 +24,4 @@ def test_get_secret_value(): ) json_data = json.loads(res.data.decode("utf-8")) - assert json_data['SecretId'] == "test" + assert json_data['SecretString'] == "mysecretstring" From 35366699a255f2711833577702c396eda09378c5 Mon Sep 17 00:00:00 2001 From: zane Date: Sat, 9 Jun 2018 23:45:55 -0700 Subject: [PATCH 8/9] update makefile back" --- Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index e7aafba70..f224d7091 100644 --- a/Makefile +++ b/Makefile @@ -9,8 +9,8 @@ else endif init: - @python3 setup.py develop - @pip3 install -r requirements.txt + @python setup.py develop + @pip install -r requirements.txt lint: flake8 moto @@ -18,7 +18,7 @@ lint: test: lint rm -f .coverage rm -rf cover - nosetests -sv --with-coverage --cover-html ./tests/ $(TEST_EXCLUDE) + @nosetests -sv --with-coverage --cover-html ./tests/ $(TEST_EXCLUDE) test_server: @TEST_SERVER_MODE=true nosetests -sv --with-coverage --cover-html ./tests/ From c6387504bcd6a67f6a035aeacedd71fa7e48b748 Mon Sep 17 00:00:00 2001 From: zane Date: Sun, 10 Jun 2018 00:23:10 -0700 Subject: [PATCH 9/9] Update flake --- moto/secretsmanager/models.py | 1 + 1 file changed, 1 insertion(+) diff --git a/moto/secretsmanager/models.py b/moto/secretsmanager/models.py index 85f9c8a51..fb09d20e4 100644 --- a/moto/secretsmanager/models.py +++ b/moto/secretsmanager/models.py @@ -15,6 +15,7 @@ class SecretsManager(BaseModel): self.version_id = kwargs.get('version_id', '') self.version_stage = kwargs.get('version_stage', '') + class SecretsManagerBackend(BaseBackend): def __init__(self, region_name=None, **kwargs):