Giter Club home page Giter Club logo

aws-secrets-manager-ssh-key-rotation's Introduction

AWS Secrets Manager - Lambda rotation function for SSH keys

Please see a walk-through of using this function in How to use AWS Secrets Manager to securely store and rotate SSH key pairs.

The above post shows you how to deploy the rotation Lambda function and resources to test the rotation in the us-east-1 AWS region. See below if you want to deploy the function to a different region, or want to re-build the Lambda function ZIP.

License Summary

This sample code is made available under a modified MIT license. See the LICENSE file.

Deploying to an AWS region other than us-east-1

To deploy the rotation Lambda function to an AWS Region other than us-east-1:

  1. Clone this repository to your desktop using git.
    git clone https://github.com/aws-samples/aws-secrets-manager-ssh-key-rotation.git
    
  2. Create a new S3 bucket or reuse an existing S3 bucket in your chosen AWS region where you want to deploy the Lambda function. This S3 bucket will store the Lambda function ZIP file.
  3. Upload the packaged Lambda function ZIP file dist/rotate_ssh_python3.9.zip to your S3 bucket. Note the S3 URL to the uploaded ZIP file (e.g. s3://bucketname/path/to/rotate_ssh_python3.9.zip)
  4. Edit the packaged CloudFormation template secretsmanager_rotate_ssh_keys_packaged.yaml and change the CodeUri to point to your S3 URL. I.e. change this line:
          CodeUri: s3://awsiammedia/public/sample/SecretsManagerStoreRotateSSHKeyPairs/rotate_ssh.zip
    
  5. Now you can create a new Stack in CloudFormation in your chosen AWS region using the modified packaged template.

Deploying the Lambda function ZIP file after making code changes

The Lambda rotation function uses the Python paramiko package that requires natively compiled cryptography libraries. For this reason, the Lambda function must be packaged on the same environment and architecture as the Lambda run-time for Python 3.9 - Amazon Linux 2 on x86_64.

The Lambda function is deployed using an AWS Serverless Application Model (SAM) template secretsmanager_rotate_ssh_keys.template that must be packaged before it can be deployed.

To deploy the Lambda function ZIP file after making code changes, or to re-package the CloudFormation template:

  1. Create a new S3 bucket or reuse an existing S3 bucket in your chosen AWS region where you want to deploy the Lambda function. This S3 bucket will store the Lambda function ZIP file and packaged CloudFormation template. Note the S3 bucket name.
  2. Launch an Amazon Linux 2 x86 EC2 instance in your chosen AWS region, ensuring that you can log into the instance using SSH or Systems Manager Session Manager. Ensure the EC2 Instance Profile for this instance has permissions to upload objects to your S3 bucket, invoke CloudFormation APIs, create IAM Roles, and create Lambda functions.
  3. SSH or start a Session Manager session to log into the EC2 instance.
  4. Install the development tools group:
    yum groupinstall development
    
  5. Install Python 3.9 by building from the source. See this post for detailed instructions.
  6. Clone this repository to the EC2 instance:
    git clone https://github.com/aws-samples/aws-secrets-manager-ssh-key-rotation.git
    
  7. Edit the shell script deployer.sh and replace values for these variables to match the S3 bucket you identified above and your chosen AWS region:
    S3Bucket=BUCKET_NAME
    REGION=us-east-1
    
  8. Run the shell script to package the Lambda ZIP file, package the CloudFormation SAM template, and deploy the template to your chosen AWS region:
    sh deployer.sh
    
  9. The packaged ZIP file and the packaged CloudFormation template are uploaded to your S3 bucket.
  10. Navigate to the CloudFormation console in your chosen AWS region to view the Stack named RotateSSH and see the resources created, including the rotation Lambda function.

aws-secrets-manager-ssh-key-rotation's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

aws-secrets-manager-ssh-key-rotation's Issues

Python 3.9 is not supporting cryptography package

When migrating to python runtime 3.9 in lambda function and using the packages from rotate_ssh_python3.9.zip, function throws the below error when invoked.

[ERROR] Runtime.ImportModuleError: Unable to import module 'rotate': No module named 'cryptography'

Ineffective security measure in copy_file.py

This line of code is intended to purge the secret from memory:

private_key = '##############################################'

Unfortunately, it does not: python strings are immutable, and reassignment of a string variable in python allocates a new buffer, leaving the old buffer in memory as garbage. Thus the best practices being espoused here are not actually being adhered to.

I verified this using a similar approach to this nice blog post:

$ echo 'import os; x="verysecret"; x="###############"; del x; os.abort()' > test.py
$ ulimit -c unlimited
$ python3 test.py
Aborted (core dumped)
$ grep verysecret core
Binary file core matches

Thus this string would remain in RAM until the same address happens to be used again later.

The AWS blog post accompanying this repository wrote:

details about keeping secret data in volatile memory will follow later in this post.

...but no such details seemed to be forthcoming.

Unable to import module 'rotate': /var/task/bcrypt/_bcrypt.so: invalid ELF header

Hi there,

Recently we have SSH key rotation requirement to meet and we found Secrets Manager and we found this repo. First of all thanks for all the great work!

However, after I deployed the cloudformation stack and try out how things work, I see this error in Secrets Manager:
Fail to rotate the secret "test-ssh-key" A previous rotation isn’t complete. That rotation will be reattempted.

Then I went to take a look at the lambda log and found this:

Unable to import module 'rotate': /var/task/bcrypt/_bcrypt.so: invalid ELF header

My environment:

OS: Mac OS high sierra 10.13.6
Python2:
Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 29 2018, 20:59:26)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin

Python3:
Python 3.6.8 (v3.6.8:3c6b436a57, Dec 24 2018, 02:04:31)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin

Deployment set up:

Since I skipped deploying the EC2 cf template, I modified the secretsmanager cf template at a minimal level:

... Same as the sample seceretsmanager_rotate_ssg_keys.template

VpcConfig:
SubnetIds:
- subnet-1234567
- subnet-2345678
SecurityGroupIds:
- sg-123 # default
- sg-345 # SSH

... same as the sample

I also did another experiment, if I change to use pip3 in deployer.sh, the error message thrown by lambda will change to:

Unable to import module 'rotate': /var/task/bcrypt/_bcrypt.abi3.so: invalid ELF header

And another modification I made is just changing the region to us-east-1, which I believe is irrelevant to this issue too.

How can I get around this? I think the lambda is not correctly built?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.