Merge pull request #1677 from sepulworld/adding_secretmanager_support

[WIP] Adding secretmanager support
This commit is contained in:
Steve Pulec 2018-06-10 20:57:49 -04:00 committed by GitHub
commit 38a4558925
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 127 additions and 0 deletions

View File

@ -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 |
|------------------------------------------------------------------------------|
| SecretsManager | @mock_secretsmanager | basic endpoints done
|------------------------------------------------------------------------------|
| SES | @mock_ses | all endpoints done |
|------------------------------------------------------------------------------|
| SNS | @mock_sns | all endpoints done |

View File

@ -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

View File

@ -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,

View File

@ -0,0 +1,6 @@
from __future__ import unicode_literals
from .models import secretsmanager_backends
from ..core.models import base_decorator
secretsmanager_backend = secretsmanager_backends['us-east-1']
mock_secretsmanager = base_decorator(secretsmanager_backends)

View File

@ -0,0 +1,49 @@
from __future__ import unicode_literals
import time
import json
import boto3
from moto.core import BaseBackend, BaseModel
class SecretsManager(BaseModel):
def __init__(self, region_name, **kwargs):
self.secret_id = kwargs.get('secret_id', '')
self.version_id = kwargs.get('version_id', '')
self.version_stage = kwargs.get('version_stage', '')
class SecretsManagerBackend(BaseBackend):
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, version_stage):
response = json.dumps({
"ARN": self.secret_arn(),
"Name": self.secret_id,
"VersionId": "A435958A-D821-4193-B719-B7769357AER4",
"SecretString": "mysecretstring",
"VersionStages": [
"AWSCURRENT",
],
"CreatedDate": "2018-05-23 13:16:57.198000"
})
return response
def secret_arn(self):
return "arn:aws:secretsmanager:{0}:1234567890:secret:{1}-rIjad".format(
self.region, self.secret_id)
available_regions = boto3.session.Session().get_available_regions("secretsmanager")
print(available_regions)
secretsmanager_backends = {region: SecretsManagerBackend(region_name=region) for region in available_regions}

View File

@ -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)

View File

@ -0,0 +1,10 @@
from __future__ import unicode_literals
from .responses import SecretsManagerResponse
url_bases = [
"https?://secretsmanager.(.+).amazonaws.com",
]
url_paths = {
'{0}/$': SecretsManagerResponse.dispatch,
}

View File

@ -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', region_name='us-west-2')
result = conn.get_secret_value(SecretId='java-util-test-password')
assert result['SecretString'] == 'mysecretstring'

View File

@ -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")
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['SecretString'] == "mysecretstring"