moto/tests
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
..
test_acm Make it possible to customize the ACM cert validation wait time. (#3843) 2021-04-10 08:13:20 +01:00
test_apigateway Fix : Remove Invalid request error when response-template is not specified (#3885) 2021-05-01 15:45:06 +01:00
test_applicationautoscaling Remove redundant test code (#3640) 2021-01-31 12:21:24 +00:00
test_athena Linting 2020-11-11 15:55:37 +00:00
test_autoscaling Fix/autoscaling elb interaction (#3757) 2021-04-30 09:29:20 +01:00
test_awslambda Assume synchronous Lambda invocation by default (#3615) 2021-01-26 13:28:01 +00:00
test_batch Fix Race Condition in batch:SubmitJob (#3480) 2020-11-18 10:49:25 +00:00
test_cloudformation Py3: use unittest.mock instead of mock (#3481) 2021-04-06 10:22:42 +01:00
test_cloudwatch Tech debt (#3653) 2021-02-02 16:31:26 +00:00
test_codecommit Finish porting from nose to pytest. 2020-11-10 08:25:05 +01:00
test_codepipeline Linting 2020-11-11 15:55:37 +00:00
test_cognitoidentity Fix ResourceNotFoundError exception message (#3582) 2021-01-10 13:26:40 +00:00
test_cognitoidp Implement User Pool MFA Actions (#3903) 2021-05-06 17:59:04 +01:00
test_config Linting 2020-11-11 15:55:37 +00:00
test_core Py3: use unittest.mock instead of mock (#3481) 2021-04-06 10:22:42 +01:00
test_datapipeline Port test suite from nose to pytest. 2020-11-10 08:23:44 +01:00
test_datasync Finish porting from nose to pytest. 2020-11-10 08:25:05 +01:00
test_dms DMS - basic task replication methods implementation (#3900) 2021-05-06 18:33:48 +01:00
test_dynamodb Port test suite from nose to pytest. 2020-11-10 08:23:44 +01:00
test_dynamodb2 Enforce dynamodb key size limit [solves #3866] (#3888) 2021-04-30 13:47:47 +01:00
test_dynamodbstreams Py3: use unittest.mock instead of mock (#3481) 2021-04-06 10:22:42 +01:00
test_ec2 Set multiple Security Groups when calling modify_network_interface_attribute (#3911) 2021-05-07 13:50:26 +01:00
test_ec2instanceconnect Improve implementation coverage (and layout) 2020-05-12 14:58:35 +01:00
test_ecr Remove redundant test code (#3640) 2021-01-31 12:21:24 +00:00
test_ecs Add the abilitiy to set ECS task definition task role and execution role arns (#3869) 2021-04-21 18:56:09 +01:00
test_elasticbeanstalk Refactor ARNs to remove hardcoded account id (#3701) 2021-02-17 09:06:22 +00:00
test_elb Address pytest warnings (#3629) 2021-01-29 11:31:56 +00:00
test_elbv2 (fix) Fixes #3648 (#3649) 2021-02-02 15:21:16 +00:00
test_emr EMR and SWF - add arn to response (#3873) 2021-04-23 15:20:36 +01:00
test_events Enhancement/3837 (#3847) 2021-04-10 14:27:38 +01:00
test_forecast Linting 2020-11-11 15:55:37 +00:00
test_glacier Port test suite from nose to pytest. 2020-11-10 08:23:44 +01:00
test_glue Implement full Database object for Glue get_database() - fix for #3571. (#3572) 2021-01-11 13:10:18 +00:00
test_iam IAM - get_user() #3828 (#3829) 2021-04-03 10:38:18 +01:00
test_iot fix #3867 IoT list_principal_things (#3868) 2021-04-19 13:35:09 +01:00
test_iotdata Fix: IoT does not work in server mode (#3644) 2021-02-01 13:15:57 +00:00
test_kinesis Fix: ApproximateArrivalTimestamp should be epoch with millisecond precision (#3764) 2021-03-11 08:54:21 +00:00
test_kinesisvideo Port test suite from nose to pytest. 2020-11-10 08:23:44 +01:00
test_kinesisvideoarchivedmedia Linting 2020-11-11 15:55:37 +00:00
test_kms Remove redundant test code (#3640) 2021-01-31 12:21:24 +00:00
test_logs Fix separator in filter_log_events nextToken value. (#3914) 2021-05-07 18:49:36 +01:00
test_managedblockchain Linting 2020-11-11 15:55:37 +00:00
test_mediaconnect Implementation of core AWS Mediastore endpoints (#3825) 2021-04-08 16:51:50 +01:00
test_medialive Implementation of core AWS Mediapackage endpoints (#3762) 2021-03-12 14:56:25 +00:00
test_mediapackage Implementation of core AWS Mediastore endpoints (#3825) 2021-04-08 16:51:50 +01:00
test_mediastore Implementation of core AWS Mediastore endpoints (#3825) 2021-04-08 16:51:50 +01:00
test_opsworks Address pytest warnings (#3629) 2021-01-29 11:31:56 +00:00
test_organizations adding list_create_account_status to organization #3691 (#3692) 2021-02-15 11:39:23 +00:00
test_packages Py3: use unittest.mock instead of mock (#3481) 2021-04-06 10:22:42 +01:00
test_polly Port test suite from nose to pytest. 2020-11-10 08:23:44 +01:00
test_ram Finish porting from nose to pytest. 2020-11-10 08:25:05 +01:00
test_rds Port test suite from nose to pytest. 2020-11-10 08:23:44 +01:00
test_rds2 Py3: use unittest.mock instead of mock (#3481) 2021-04-06 10:22:42 +01:00
test_redshift Fix:Add functionality authorize-cluster-security-group-ingress (#3742) 2021-03-10 08:46:13 +00:00
test_resourcegroups Run black on moto & test directories. 2019-10-31 10:36:05 -07:00
test_resourcegroupstaggingapi Add ResourceGroupsTaggingAPI Support for RDS Resources (#3674) 2021-02-11 15:15:24 +00:00
test_route53 sort Route53 resource record sets (#3864) 2021-04-20 12:50:05 +01:00
test_s3 S3 - Fix exception for missing versionID (#3887) 2021-04-30 12:36:08 +01:00
test_s3bucket_path Port test suite from nose to pytest. 2020-11-10 08:23:44 +01:00
test_sagemaker Add CloudFormation support for SageMaker Endpoint Configs and Endpoints (#3863) 2021-04-17 13:49:46 +01:00
test_secretsmanager Support rotating secrets using Lambda [#3905] (#3912) 2021-05-11 12:08:01 +01:00
test_ses Raise TemplateDoesNotExist if template does not exist (#3784) 2021-03-19 15:36:53 +00:00
test_sns Update SNS get_endpoint_attributes response for Not Found endpoints (#3637) 2021-01-31 12:29:10 +00:00
test_sqs Fix:SQS Added support for system attributes in sqs (#3878) 2021-05-01 07:48:39 +01:00
test_ssm Modify SSM put_parameter() to raise ValidationException if value is empty string (#3806) 2021-03-28 14:45:57 +01:00
test_stepfunctions Update integration test to use Pytest (#3703) 2021-02-17 15:06:31 +00:00
test_sts Enable AWSLambda and STS mocking for AWS China regions/endpoints (#3574) 2021-01-08 14:22:12 +00:00
test_support Support - Allow describe_cases without any params (#3818) 2021-03-31 13:18:46 +01:00
test_swf EMR and SWF - add arn to response (#3873) 2021-04-23 15:20:36 +01:00
test_transcribe Linting 2020-11-11 15:55:37 +00:00
test_utilities Support Podman for mocking Lambda (#3702) 2021-02-18 08:58:20 +00:00
test_xray Port test suite from nose to pytest. 2020-11-10 08:23:44 +01:00
__init__.py Address pytest warnings (#3629) 2021-01-29 11:31:56 +00:00
compat.py Check S3_IGNORE_SUBDOMAIN_BUCKETNAME environment variable (#3796) 2021-03-26 16:51:19 +00:00
helpers.py Port test suite from nose to pytest. 2020-11-10 08:23:44 +01:00