|
| 1 | + |
| 2 | +# NPM user credentials rotation infrastructure |
| 3 | + |
| 4 | +This CDK app helps to automate the credentials rotation for a NPM user. |
| 5 | +The credentials that are configured for rotation include login password and access keys. |
| 6 | + |
| 7 | +#### Configuring the app |
| 8 | +The `lambda_functions/secrets.json` configuration file should be populated with the |
| 9 | +information about **accessing** secret values from AWS Secrets Manager. **It must not be |
| 10 | +used to store the secret values themselves.** |
| 11 | + |
| 12 | +``` |
| 13 | +{ |
| 14 | + "npm_login_username_secret": { |
| 15 | + "arn": "<npm_login_username_secret_arn>", |
| 16 | + "secret_key": "npm_login_username" |
| 17 | + }, |
| 18 | + "npm_login_password_secret": { |
| 19 | + "arn": "<npm_login_password_secret_arn>", |
| 20 | + "secret_key": "npm_login_password", |
| 21 | + "alarm_subscriptions": ["[email protected]"] |
| 22 | + }, |
| 23 | + "npm_otp_seed_secret": { |
| 24 | + "arn": "<npm_otp_seed_secret_arn>", |
| 25 | + "secret_key": "npm_otp_seed" |
| 26 | + }, |
| 27 | + "npm_access_token_secrets": { |
| 28 | + "secrets": [ |
| 29 | + { |
| 30 | + "arn": "<npm_access_token_codegen_arn>", |
| 31 | + "secret_key": "npm_access_token_codegen" |
| 32 | + }, |
| 33 | + { |
| 34 | + "arn": "<npm_access_token_js_arn>", |
| 35 | + "secret_key": "npm_access_token_js" |
| 36 | + } |
| 37 | + ], |
| 38 | + "alarm_subscriptions": ["[email protected]"] |
| 39 | + } |
| 40 | +} |
| 41 | +``` |
| 42 | +Since, CDK does not support creating/populating the secrets, recommended best practice is |
| 43 | +to use the AWS CLI or Console to create the above secrets. |
| 44 | +For example, to create a secret to hold the npm username using CLI: |
| 45 | +``` |
| 46 | +aws secretsmanager create-secret --name npm-username-secret --secret-string "{ \"npm_login_username\": \"my-npm-username\" }" |
| 47 | +``` |
| 48 | +Paste the `ARN` returned from above operation under `npm_login_username_secret`. |
| 49 | +The `secret_key` (`npm_login_username` in this case) can also be customized. |
| 50 | + |
| 51 | +Similarly, create the other secrets required in the configuration file: |
| 52 | +* `npm_login_username_secret`: stores the login username for the npm user. This secret is static and is not rotated. |
| 53 | +* `npm_login_password_secret`: stores the login password for the npm user. This secret is configured for rotation and accepts |
| 54 | +a list of emails to alert in case the rotation fails. |
| 55 | +* `npm_otp_seed_secret`: stores the OTP seed for the npm user. This is created when the NPM user enables 2-factor Authentication. |
| 56 | +This secret is static and is not rotated. |
| 57 | +* `npm_access_token_secrets`: stores the list of secrets that hold the access keys created by the npm user. |
| 58 | +This secret is configured for rotation and accepts a list of emails to alert in case the rotation fails. |
| 59 | + |
| 60 | +#### Deploying the infrastructure |
| 61 | +The AWS credentials have to be set using following environment variables: |
| 62 | + 1. `AWS_ACCESS_KEY_ID` |
| 63 | + 2. `AWS_SECRET_ACCESS_KEY` |
| 64 | + 3. `AWS_SESSION_TOKEN` |
| 65 | + 4. `AWS_DEFAULT_REGION`: The region of deployment should be same as the region where the above secrets are created. |
| 66 | + |
| 67 | +`python 3` and `pip3` should be installed. |
| 68 | +Run `pip3 install -r requirements.txt --upgrade` from the app root to fetch necessary modules for the app. |
| 69 | + |
| 70 | +At this point you can now deploy the infrastructure using: |
| 71 | +``` |
| 72 | +$ cdk deploy --all |
| 73 | +``` |
| 74 | +or run any of the cdk commands listed below. |
| 75 | + |
| 76 | + |
| 77 | +#### Useful commands |
| 78 | + |
| 79 | + * `cdk ls` list all stacks in the app |
| 80 | + * `cdk synth` emits the synthesized CloudFormation template |
| 81 | + * `cdk deploy` deploy this stack to your default AWS account/region |
| 82 | + * `cdk diff` compare deployed stack with current state |
| 83 | + * `cdk docs` open CDK documentation |
| 84 | + |
| 85 | + |
| 86 | +#### Stacks included in the app |
| 87 | +* `UserLoginPasswordRotatorStack`: Contains resources like a rotator lambda to rotate the `npm_login_password_secret` |
| 88 | +along with necessary permissions, alarms to monitor it. |
| 89 | +* `UserAccessTokensRotatorStack`: Contains resources like a rotator lambda to rotate the access keys specified under `npm_access_token_secrets` |
| 90 | +along with necessary permissions, alarms to monitor it. |
| 91 | + |
| 92 | +To deploy a single stack, use `cdk deploy UserLoginPasswordRotatorStack` |
| 93 | + |
| 94 | +------------------ |
| 95 | + |
| 96 | +[amplify.aws](https://amplify.aws) |
0 commit comments