Giter Site home page Giter Site logo

hwatts / awssecretsmanagerrotatemssql Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 10 KB

A Lambda handler for the AWS Secrets Manager single user rotation strategy

License: MIT License

C# 100.00%
netcore aws-lambda aws-secrets-manager csharp sql-server

awssecretsmanagerrotatemssql's Introduction

SQL Server Single User Lambda handler for AWS Secrets Manager

Using the strategy described here: https://docs.aws.amazon.com/secretsmanager/latest/userguide/rotating-secrets-one-user-one-password.html

Build

install .net core SDK for your platform: https://www.microsoft.com/net/download

install a recent version of the awscli that includes support for sam-template packaging: https://aws.amazon.com/cli/

Restore dependencies

    dotnet restore

Package function into zip

    dotnet lambda package --FunctionName RotateMssql

Deploy

Package sam template and copy zipped deployable to an S3 bucket:

    aws cloudformation package --template-file sam-template.yaml \
    --output-template-file output-sam-template.yaml --s3-bucket <yourS3Bucket>

Deploy sam template (creates a Cloudformation stack):

    aws cloudformation deploy --template-file output-sam-template.yaml \
    --stack-name RotateMSSQL --capabilities CAPABILITY_IAM \
    --parameter-overrides Subnets=<subnet-AZ1>,<subnet-AZ2>,<subnet-AZ3> SecurityGroupId=<security-group-id>

Notes:

The password rotation itself is implemented with SMO, specifically the Login.ChangePassword(oldPassword,newPassword) Method. This potentially has compatability issues with older versions of SQL Server, but the rationale for using it is that MS SQL doesn't allow the use of parameters on DDL statements, leaving significant risk of SQL Injection attacks when using ALTER LOGIN syntax directly.

Currently, this only works with TCP connections to SQL Server over a known port, which works for RDS, but wouldn't work with named instances etc. Since this uses SMO, it's easy to adapt for wider compatibility - pull requests welcome.

The AWS Secret that invokes this must include the following initial values:

Key Description
username The username to enable rotation for
password The current valid password - note that this will be used for initial sign-on, then immediately changed with a random string
host The network name of the host to connect to (should work with IP or any network name that can be resolved in the VPC DNS)
port The TCP port to connect to the instance on (1433 is the default for SQL Server)
dbname The database name to connect to
engine Must be "sqlserver"

By default, SQL Logins have permission to change their own password, so this should work in most environments.

Although AWS Secrets Manager goes to some lengths to ensure that password changes are tested before they're commited and that the random password is stored in Secrets Manager before changing in the database, there is potential for a password to be rotated, but not promoted to AWSCURRENT in certain failure scenarios (mostly failure of the AWS Secrets Manager API itself). For this reason, I recommend not using this script to rotate the master password, or if you do, have a method of resetting the password outside of Secrets Manager in case of failure.

This code should be considered sample code to modify and test in your environment. It has not been heavily tested for use in a production environment, so doing so is at your own risk.

awssecretsmanagerrotatemssql's People

Contributors

hwatts avatar

Stargazers

Ruskin Dantra avatar

Watchers

James Cloos avatar  avatar

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.