How to use describe_secret method in localstack

Best Python code snippet using localstack_python

test_rotation.py

Source:test_rotation.py Github

copy

Full Screen

1""" Tests for the rotation lambda. """2import json3from unittest.mock import patch, MagicMock as Mock, ANY4import pytest5import botocore6from src import rotation7ARN = 'arn:aws:secretsmanager:::secrets/fakesecret'8URL = 'unit-test.url.com'9@patch('src.rotation.client')10@patch('src.rotation.config')11def test_handler_no_rotation(mock_config, mock_secrets):12 """Test the handler when no rotation needs to occur"""13 mock_secrets.describe_secret.return_value = {'RotationEnabled': False}14 mock_secrets.get_secret_value.return_value = {'SecretString': json.dumps(15 {'tenant': 'mmm-dev'}16 )}17 token = 'token'18 provider = 'auth0'19 mock_config.get_provider.return_value = provider20 with pytest.raises(ValueError):21 rotation.lambda_handler(22 {23 'SecretId': ARN,24 'ClientRequestToken': token,25 'Step': 'createSecret'26 },27 {}28 )29@patch('src.rotation.rollback_secret')30@patch('src.rotation.client')31@patch('src.rotation.config')32def test_handler_rollback(mock_config, mock_secrets, rollback_secret):33 """Test the handler when secret is rotated but secrets manager can't update"""34 mock_secrets.describe_secret.return_value = {35 'RotationEnabled': True,36 'VersionIdsToStages': {37 'token': ['AWSPENDING']38 }39 }40 mock_secrets.get_secret_value.return_value = {'SecretString': json.dumps(41 {'tenant': 'mmm-dev', 'client_secret': 'foo', 'client_id': 'bar'}42 )}43 token = 'token'44 mock_config.get_provider.return_value.rotate_client_secret.return_value = 'baz'45 mock_secrets.put_secret_value.side_effect = botocore.exceptions.ClientError(46 {'Error': {'Code':'ResourceExistsException'},},47 'ResourceExistsException'48 )49 rotation.lambda_handler(50 {51 'SecretId': ARN,52 'ClientRequestToken': token,53 'Step': 'createSecret'54 },55 {}56 )57 mock_secrets.put_secret_value.assert_called_with(58 SecretId='arn:aws:secretsmanager:::secrets/fakesecret',59 ClientRequestToken='token',60 SecretString=json.dumps({'tenant': 'mmm-dev', 'client_secret': 'baz', 'client_id': 'bar'}),61 VersionStages=['AWSPENDING'],62 )63 rollback_secret.assert_called_with(64 ANY,65 {'tenant': 'mmm-dev', 'client_secret': 'foo', 'client_id': 'bar'},66 )67@patch('src.rotation.client')68@patch('src.rotation.config')69def test_handler_no_token(mock_config, mock_secrets):70 """Test the handler when there is no token"""71 mock_secrets.describe_secret.return_value = {72 'RotationEnabled': True,73 'VersionIdsToStages': {}74 }75 mock_secrets.get_secret_value.return_value = {'SecretString': json.dumps(76 {'tenant': 'mmm-dev'}77 )}78 token = 'token'79 provider = 'auth0'80 mock_config.get_provider.return_value = provider81 with pytest.raises(ValueError):82 rotation.lambda_handler(83 {84 'SecretId': ARN,85 'ClientRequestToken': token,86 'Step': 'createSecret'87 },88 {}89 )90@patch('src.rotation.client')91@patch('src.rotation.config')92def test_handler_is_current(mock_config, mock_secrets):93 """When the token passed is already the current"""94 mock_secrets.describe_secret.return_value = {95 'RotationEnabled': True,96 'VersionIdsToStages': {97 'token': ['AWSCURRENT']98 }99 }100 mock_secrets.get_secret_value.return_value = {'SecretString': json.dumps(101 {'tenant': 'mmm-dev'}102 )}103 token = 'token'104 provider = 'auth0'105 mock_config.get_provider.return_value = provider106 rotation.lambda_handler(107 {108 'SecretId': ARN,109 'ClientRequestToken': token,110 'Step': 'createSecret'111 },112 {}113 )114@patch('src.rotation.client')115@patch('src.rotation.config')116def test_handler_no_pending(mock_config, mock_secrets):117 """When there is no pending version to change"""118 mock_secrets.describe_secret.return_value = {119 'RotationEnabled': True,120 'VersionIdsToStages': {121 'token': []122 }123 }124 mock_secrets.get_secret_value.return_value = {'SecretString': json.dumps(125 {'tenant': 'mmm-dev'}126 )}127 token = 'token'128 provider = 'auth0'129 mock_config.get_provider.return_value = provider130 with pytest.raises(ValueError):131 rotation.lambda_handler(132 {133 'SecretId': ARN,134 'ClientRequestToken': token,135 'Step': 'createSecret'136 },137 {}138 )139@patch('src.rotation.client')140@patch('src.rotation.create_secret')141@patch('src.rotation.config')142def test_handler_create(mock_config, mock_create, mock_secrets):143 """Test the handler with a create event"""144 mock_secrets.describe_secret.return_value = {145 'RotationEnabled': True,146 'VersionIdsToStages': {147 'token': ['AWSPENDING']148 }149 }150 mock_secrets.get_secret_value.return_value = {'SecretString': json.dumps(151 {'tenant': 'mmm-dev'}152 )}153 token = 'token'154 provider = 'auth0'155 mock_config.get_provider.return_value = provider156 rotation.lambda_handler(157 {158 'SecretId': ARN,159 'ClientRequestToken': token,160 'Step': 'createSecret'161 },162 {}163 )164 mock_create.assert_called_with(mock_secrets, provider, ARN, token)165@patch('src.rotation.client')166@patch('src.rotation.set_secret')167@patch('src.rotation.config')168def test_handler_set(mock_config, mock_set, mock_secrets):169 """Test the handler for setting a secret"""170 mock_secrets.describe_secret.return_value = {171 'RotationEnabled': True,172 'VersionIdsToStages': {173 'token': ['AWSPENDING']174 }175 }176 mock_secrets.get_secret_value.return_value = {'SecretString': json.dumps(177 {'tenant': 'mmm-dev'}178 )}179 token = 'token'180 provider = 'auth0'181 mock_config.get_provider.return_value = provider182 rotation.lambda_handler(183 {184 'SecretId': ARN,185 'ClientRequestToken': token,186 'Step': 'setSecret'187 },188 {}189 )190 mock_set.assert_called_with()191@patch('src.rotation.client')192@patch('src.rotation.test_secret')193@patch('src.rotation.config')194def test_handler_test(mock_config, mock_test, mock_secrets):195 """Test the handler with a test event"""196 mock_secrets.describe_secret.return_value = {197 'RotationEnabled': True,198 'VersionIdsToStages': {199 'token': ['AWSPENDING']200 }201 }202 mock_secrets.get_secret_value.return_value = {'SecretString': json.dumps(203 {'tenant': 'mmm-dev'}204 )}205 token = 'token'206 provider = 'auth0'207 mock_config.get_provider.return_value = provider208 rotation.lambda_handler(209 {210 'SecretId': ARN,211 'ClientRequestToken': token,212 'Step': 'testSecret'213 },214 {}215 )216 mock_test.assert_called_with(mock_secrets, provider, ARN)217@patch('src.rotation.client')218@patch('src.rotation.finish_secret')219@patch('src.rotation.config')220def test_handler_finish(mock_config, mock_finish, mock_secrets):221 """Test the handler with a finish event"""222 mock_secrets.describe_secret.return_value = {223 'RotationEnabled': True,224 'VersionIdsToStages': {225 'token': ['AWSPENDING']226 }227 }228 mock_secrets.get_secret_value.return_value = {'SecretString': json.dumps(229 {'tenant': 'mmm-dev'}230 )}231 token = 'token'232 provider = 'auth0'233 mock_config.get_provider.return_value = provider234 rotation.lambda_handler(235 {236 'SecretId': ARN,237 'ClientRequestToken': token,238 'Step': 'finishSecret'239 },240 {}241 )242 mock_finish.assert_called_with(mock_secrets, ARN, token)243@patch('src.rotation.client')244@patch('src.rotation.config')245def test_handler_invalid_step(mock_config, mock_secrets):246 """Test the handler with an invalid step"""247 mock_secrets.describe_secret.return_value = {248 'RotationEnabled': True,249 'VersionIdsToStages': {250 'token': ['AWSPENDING']251 }252 }253 mock_secrets.get_secret_value.return_value = {'SecretString': json.dumps(254 {'tenant': 'mmm-dev'}255 )}256 token = 'token'257 provider = 'auth0'258 mock_config.get_provider.return_value = provider259 with pytest.raises(ValueError):260 rotation.lambda_handler(261 {262 'SecretId': ARN,263 'ClientRequestToken': token,264 'Step': 'invalidStep'265 },266 {}267 )268@patch('src.rotation.secret.get_secret')269def test_create_secret_exists(mock_get_secret):270 """Test the create method"""271 client_id = 'client_id'272 new_secret = 'new_secret'273 mock_get_secret.return_value = json.dumps({274 'client_id': client_id,275 'client_secret': 'old_secret'276 })277 provider = Mock()278 provider.rotate_client_secret.return_value = new_secret279 client = Mock()280 token = 'token'281 rotation.create_secret(client, provider, ARN, token)282 mock_get_secret.assert_called_with(283 client, ARN, rotation.logger)284 provider.rotate_client_secret.assert_called_with(client_id=client_id)285 client.put_secret_value.assert_called_with(286 SecretId=ARN,287 ClientRequestToken=token,288 SecretString=json.dumps(289 {'client_id': client_id, 'client_secret': new_secret}),290 VersionStages=['AWSPENDING']291 )292def test_set_secret():293 """Test the set secret method"""294 # The method does nothing at the moment295 rotation.set_secret()296@patch('src.rotation.secret.get_secret')297def test_test_secret(mock_get_secret):298 """Test the test_secret method"""299 tenant = 'my-tenant.auth0.com'300 client_id = 'client_id'301 client_secret = 'client_secret'302 mock_get_secret.return_value = json.dumps({303 'tenant': tenant,304 'client_id': client_id,305 'client_secret': client_secret306 })307 provider = Mock()308 provider.get_application.return_value={'client_secret':client_secret}309 client = Mock()310 assert rotation.test_secret(client, provider, ARN)311 mock_get_secret.assert_called_with(312 client=client, secret_id=ARN, stage='AWSPENDING')313@patch('src.rotation.secret.get_secret')314def test_test_secret_fail(mock_get_secret):315 """Test the test_secret method when the test fails"""316 tenant = 'my-tenant.auth0.com'317 client_id = 'client_id'318 client_secret = 'client_secret'319 mock_get_secret.return_value = json.dumps({320 'tenant': tenant,321 'client_id': client_id,322 'client_secret': client_secret323 })324 provider = Mock()325 client = Mock()326 provider.get_application.return_value={'client_secret':'foobarbazquxquux'}327 with pytest.raises(ValueError):328 rotation.test_secret(client, provider, ARN)329 mock_get_secret.assert_called_with(330 client=client, secret_id=ARN, stage='AWSPENDING')331def test_finish_secret():332 """Test the finish_secret method"""333 mock_secrets = Mock()334 token = 'ver2'335 mock_secrets.describe_secret.return_value = {336 'VersionIdsToStages': {337 'ver1': ['AWSCURRENT'],338 'ver2': ['AWSPENDING']339 }340 }341 rotation.finish_secret(mock_secrets, ARN, token)342 mock_secrets.describe_secret.assert_called_with(SecretId=ARN)343 mock_secrets.update_secret_version_stage.assert_called_with(344 SecretId=ARN,345 VersionStage='AWSCURRENT',346 MoveToVersionId=token,347 RemoveFromVersionId='ver1'348 )349def test_finish_secret_finished():350 """ Test that it doesn't update the secret if it's already AWSCURRENT"""351 mock_secrets = Mock()352 token = 'ver1'353 mock_secrets.describe_secret.return_value = {354 'VersionIdsToStages': {355 'ver1': ['AWSCURRENT'],356 'ver2': ['AWSPENDING']357 }358 }359 rotation.finish_secret(mock_secrets, ARN, token)360 mock_secrets.describe_secret.assert_called_with(SecretId=ARN)...

Full Screen

Full Screen

aws_secretsmanager_info.py

Source:aws_secretsmanager_info.py Github

copy

Full Screen

...65 return paginator.paginate(66 SecretId=module.params['id']67 ), True68 else:69 return client.describe_secret(70 SecretId=module.params['id']71 ), False72 elif module.params['list_secrets']:73 if client.can_paginate('list_secrets'):74 paginator = client.get_paginator('list_secrets')75 return paginator.paginate(), True76 else:77 return client.list_secrets(), False78 else:79 return None, False80 except (BotoCoreError, ClientError) as e:81 module.fail_json_aws(e, msg='Failed to fetch AWS Secrets Manager details')82def main():83 argument_spec = dict(...

Full Screen

Full Screen

vault.py

Source:vault.py Github

copy

Full Screen

...53 if 'SecretString' in get_secret_value_response:54 secrets = get_secret_value_response['SecretString']55 secobj = json.loads(secrets)56 if seckey in secobj:57 describe_secret = client.describe_secret(SecretId=secret_name)58 if 'CreatedDate' in describe_secret:59 crdate = describe_secret['CreatedDate']60 response = { "Vault": secret_name,"Key":seckey,"Value": secobj[seckey], 'CreatedDate':str(crdate)}61 else:62 response = base64.b64decode(get_secret_value_response['SecretBinary'])63 responseList.append(response)64 # if response if null, implying key not found.65 if ( response == "" ):66 responseList.clear()67 responseList.append("Invalid Key")68 return {69 'statusCode': 200,70 'body': json.dumps(responseList)71 }

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful