moto/moto
Daniel Samuels a4b1498665
Support rotating secrets using Lambda [#3905] (#3912)
* Support rotating secrets using Lambda

The Secrets manager rotation process uses an AWS Lambda function
to perform the rotation of a secret. [1]

In fact, it's not possible to trigger rotation of a Secret
without specifying a Lambda function at some point in the life
of the secret:

```
$ aws secretsmanager rotate-secret --secret-id /rotationTest

An error occurred (InvalidRequestException) when calling the RotateSecret operation: No Lambda rotation function ARN is associated with this secret.
```

`moto` can be a little more lenient in this regard and allow
`rotate_secret` to be called without a Lambda function being
present, if only to allow simulation of the `AWSCURRENT` and
`AWSPREVIOUS` labels moving across versions.

However, if a lambda function _has_ been specified when calling
`rotate_secret`, it should be invoked therefore providing the
developer with the full multi-stage process [3] which can be
used to test the Lambda function itself and ensuring that full
end-to-end testing is performed. Without this there's no easy
way to configure the Secret in the state needed to provide the
Lambda function with the data in the format it needs to be in
at each step of the invocation process.

[1]: https://docs.aws.amazon.com/secretsmanager/latest/userguide/rotating-secrets-lambda-function-overview.html
[2]: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/secretsmanager.html#SecretsManager.Client.rotate_secret
[3]: https://docs.aws.amazon.com/secretsmanager/latest/userguide/rotating-secrets-lambda-function-overview.html#rotation-explanation-of-steps

* Run `black` over `secretsmanager/models.py`

* Make `lambda_backends` import local to the condition

* Implement `update_secret_version_stage`

Allow a staging label to be moved across versions.

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/secretsmanager.html#SecretsManager.Client.update_secret_version_stage

* Add an integration test for Secrets Manager & Lambda

* Support passing `ClientRequestToken` to `put_secret_value`

By passing `ClientRequestToken` to `put_secret_value` within
the lambda function  invoked by calling `rotate_secret`, one
can update the value associated with the existing (pending)
version, without causing a new secret version to be created.

* Add application logic for `AWSPENDING`

The rotation function must end with the versions of the secret
in one of two states:

 - The `AWSPENDING` and `AWSCURRENT` staging labels are
   attached to the same version of the secret, or
 - The `AWSPENDING` staging label is not attached to any
   version of the secret.

If the `AWSPENDING` staging label is present but not attached
to the same version as `AWSCURRENT` then any later invocation
of RotateSecret assumes that a previous rotation request is
still in progress and returns an error.

* Update `default_version_id` after Lambda rotation concludes

Call `set_default_version_id` directly, rather than going 
through `reset_default_version` as the Lambda function is 
responsible for moving the version labels around, not `rotate_secret`.

* Run `black` over changed files

* Fix Python 2.7 compatibility

* Add additional test coverage for Secrets Manager

* Fix bug found by tests

AWSPENDING + AWSCURRENT check wasn't using `version_stages`.
Also tidy up the AWSCURRENT moving in `update_secret_version_stage`
to remove AWSPREVIOUS it from the new stage.

* Run `black` over changed files

* Add additional `rotate_secret` tests

* Skip `test_rotate_secret_lambda_invocations` in test server mode

* Add test for invalid Lambda ARN
2021-05-11 12:08:01 +01:00
..
acm Make it possible to customize the ACM cert validation wait time. (#3843) 2021-04-10 08:13:20 +01:00
apigateway Fix : Remove Invalid request error when response-template is not specified (#3885) 2021-05-01 15:45:06 +01:00
applicationautoscaling Add failures output to ecs.describe_services (#3641) 2021-02-01 12:19:46 +00:00
athena Implemented Athena create_named_query, get_named_query (#1524) (#3065) 2020-06-11 17:27:29 +01:00
autoscaling Fix/autoscaling elb interaction (#3757) 2021-04-30 09:29:20 +01:00
awslambda Support Podman for mocking Lambda (#3702) 2021-02-18 08:58:20 +00:00
batch added feature update_usage_plan and fixed some lint errors (#3727) 2021-02-25 07:46:11 +00:00
cloudformation Add CloudFormation support for SageMaker Notebook Instances (#3845) 2021-04-09 18:54:00 +01:00
cloudwatch Fix response parsing error on Java SDK v2 for CW GetMetricData (#3645) (#3675) 2021-02-11 15:04:59 +00:00
codecommit List dependencies for services - add integration test to verify 2020-09-13 16:08:23 +01:00
codepipeline List dependencies for services - add integration test to verify 2020-09-13 16:08:23 +01:00
cognitoidentity Fix ResourceNotFoundError exception message (#3582) 2021-01-10 13:26:40 +00:00
cognitoidp Implement User Pool MFA Actions (#3903) 2021-05-06 17:59:04 +01:00
config Linting 2020-11-11 15:55:37 +00:00
core Enforce dynamodb key size limit [solves #3866] (#3888) 2021-04-30 13:47:47 +01:00
datapipeline Iam cloudformation update, singificant cloudformation refactoring (#3218) 2020-08-27 10:11:47 +01:00
datasync Add missing regions to all services 2019-12-26 17:12:22 +01:00
dms DMS - basic task replication methods implementation (#3900) 2021-05-06 18:33:48 +01:00
dynamodb Decentralize cloudformation naming responsibilities (#3201) 2020-08-01 15:23:36 +01:00
dynamodb2 Enforce dynamodb key size limit [solves #3866] (#3888) 2021-04-30 13:47:47 +01:00
dynamodbstreams Prevent JSON dumps error when dealing with complex types 2020-04-06 17:21:26 +10:00
ec2 Set multiple Security Groups when calling modify_network_interface_attribute (#3911) 2021-05-07 13:50:26 +01:00
ec2instanceconnect Remove boto package dependency 2020-11-26 23:59:15 -08:00
ecr ecr: Fix "imageDigest" value in ecr.list_images() response (#3436) 2020-11-05 14:10:23 +00:00
ecs Add the abilitiy to set ECS task definition task role and execution role arns (#3869) 2021-04-21 18:56:09 +01:00
elasticbeanstalk Refactor ARNs to remove hardcoded account id (#3701) 2021-02-17 09:06:22 +00:00
elb Fix/autoscaling elb interaction (#3757) 2021-04-30 09:29:20 +01:00
elbv2 (fix) Fixes #3648 (#3649) 2021-02-02 15:21:16 +00:00
emr EMR and SWF - add arn to response (#3873) 2021-04-23 15:20:36 +01:00
events Enhancement/3837 (#3847) 2021-04-10 14:27:38 +01:00
forecast moved iteritems backwards compatibility to six (#3672) 2021-02-11 14:44:37 +00:00
glacier Fixed linter errors 2019-12-26 21:03:49 +01:00
glue Implement full Database object for Glue get_database() - fix for #3571. (#3572) 2021-01-11 13:10:18 +00:00
iam IAM - get_user() #3828 (#3829) 2021-04-03 10:38:18 +01:00
instance_metadata Run black on moto & test directories. 2019-10-31 10:36:05 -07:00
iot fix #3867 IoT list_principal_things (#3868) 2021-04-19 13:35:09 +01:00
iotdata Fix: IoT does not work in server mode (#3644) 2021-02-01 13:15:57 +00:00
kinesis Fix: ApproximateArrivalTimestamp should be epoch with millisecond precision (#3764) 2021-03-11 08:54:21 +00:00
kinesisvideo Add kinesisvideo archived media (#3280) 2020-09-04 12:14:48 +01:00
kinesisvideoarchivedmedia Add kinesisvideo archived media (#3280) 2020-09-04 12:14:48 +01:00
kms List dependencies for services - add integration test to verify 2020-09-13 16:08:23 +01:00
logs Fix separator in filter_log_events nextToken value. (#3914) 2021-05-07 18:49:36 +01:00
managedblockchain Fix Blockchain to work with new API (#3575) 2021-01-07 11:18:50 +00:00
mediaconnect Implementation of core AWS Mediastore endpoints (#3825) 2021-04-08 16:51:50 +01:00
medialive Implementation of core AWS Media Live endpoins (#3428) 2021-01-19 16:11:39 +00:00
mediapackage Implementation of core AWS Mediapackage endpoints (#3762) 2021-03-12 14:56:25 +00:00
mediastore Implementation of core AWS Mediastore endpoints (#3825) 2021-04-08 16:51:50 +01:00
opsworks added feature update_usage_plan and fixed some lint errors (#3727) 2021-02-25 07:46:11 +00:00
organizations adding list_create_account_status to organization #3691 (#3692) 2021-02-15 11:39:23 +00:00
packages Remove boto package dependency 2020-11-26 23:59:15 -08:00
polly Fixed linter errors 2019-12-26 21:03:49 +01:00
ram RAM - implement CRUD endpoints (#3158) 2020-07-21 14:15:13 +01:00
rds Remove boto package dependency 2020-11-26 23:59:15 -08:00
rds2 Minor RDS Clean up (#3682) 2021-02-13 11:12:02 +00:00
redshift Fix:Add functionality authorize-cluster-security-group-ingress (#3742) 2021-03-10 08:46:13 +00:00
resourcegroups Fix resource groups tests (#3204) 2020-07-31 07:18:52 +01:00
resourcegroupstaggingapi Add ResourceGroupsTaggingAPI Support for RDS Resources (#3674) 2021-02-11 15:15:24 +00:00
route53 sort Route53 resource record sets (#3864) 2021-04-20 12:50:05 +01:00
s3 S3 - Fix exception for missing versionID (#3887) 2021-04-30 12:36:08 +01:00
s3bucket_path Run black on moto & test directories. 2019-10-31 10:36:05 -07:00
sagemaker Add CloudFormation support for SageMaker Endpoint Configs and Endpoints (#3863) 2021-04-17 13:49:46 +01:00
secretsmanager Support rotating secrets using Lambda [#3905] (#3912) 2021-05-11 12:08:01 +01:00
ses Raise TemplateDoesNotExist if template does not exist (#3784) 2021-03-19 15:36:53 +00:00
sns Remove duplicate template of SNS error response (#3647) 2021-02-01 14:58:48 +00:00
sqs Fix:SQS Added support for system attributes in sqs (#3878) 2021-05-01 07:48:39 +01:00
ssm Modify SSM put_parameter() to raise ValidationException if value is empty string (#3806) 2021-03-28 14:45:57 +01:00
stepfunctions Add basic get_execution_history implementation for Step Functions (#3507) 2020-12-03 18:32:06 +00:00
sts #3599 - Update Implementation Coverage script (#3621) 2021-01-27 18:54:21 +00:00
support Support - Allow describe_cases without any params (#3818) 2021-03-31 13:18:46 +01:00
swf EMR and SWF - add arn to response (#3873) 2021-04-23 15:20:36 +01:00
templates Add about page. 2017-03-12 19:58:40 -04:00
transcribe Transcribe Medical Support (#3299) 2020-09-30 13:18:26 +01:00
utilities Add encoding-param to open(), in case the underlying OS has a different default (#3827) 2021-04-01 16:51:10 +01:00
xray Tech Debt - Remove duplicate AWSError classes 2020-11-05 11:20:18 +00:00
__init__.py DMS - basic task replication methods implementation (#3900) 2021-05-06 18:33:48 +01:00
backends.py DMS - basic task replication methods implementation (#3900) 2021-05-06 18:33:48 +01:00
compat.py Py3: use unittest.mock instead of mock (#3481) 2021-04-06 10:22:42 +01:00
server.py allow specifying the service as env var (#3899) 2021-05-06 14:47:08 +01:00
settings.py Make it possible to customize the ACM cert validation wait time. (#3843) 2021-04-10 08:13:20 +01:00