abhilash1in / aws-secrets-manager-action

Use secrets from AWS Secrets Manager as environment variables in your GitHub Actions workflow
MIT License
68 stars 43 forks source link
aws-secrets-manager github-actions

AWS Secrets Manager GitHub Action

Tests GitHub license

GitHub Action to fetch secrets from AWS Secrets Manager and inject them as environment variables into your GitHub Actions workflow.

The injected environment variable names will only contain upper case letters, digits and underscores. It will not begin with a digit.

If your secret name contains any characters other than upper case letters, digits and underscores, it will not be used directly as the environment variable name. Rather, it will be transformed into a string that only contains upper case letters, digits and underscores.

For example:

Usage

Refer Configure AWS Credentials for AWS recommended best practices on how to configure AWS credentials for use with GitHub Actions.

steps:
- name: Configure AWS Credentials
  uses: aws-actions/configure-aws-credentials@v1
  with:
    aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
    aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
    aws-region: ${{ secrets.AWS_REGION }}

- name: Read secrets from AWS Secrets Manager into environment variables
  uses: abhilash1in/aws-secrets-manager-action@v2.1.0
  with:
    secrets: |
      my_secret_1
      app1/dev/*
    parse-json: true

- name: Check if env variable is set after fetching secrets
  run: if [ -z ${MY_SECRET_1+x} ]; then echo "MY_SECRET_1 is unset"; else echo "MY_SECRET_1 is set to '$MY_SECRET_1'"; fi
parse-json AWS Secrets Manager Secret
(name = value)
Injected Environment Variable
(name = value)
Explanation
true foo = { "bar": "baz" } FOO_BAR = baz Values that can be parsed into a JSON will be parsed and flattened
true 1/dev/foo = { "bar" = "baz" } _1_DEV_FOO = { "bar" = "baz" } Values that cannot be parsed into a JSON will NOT be parsed
true foo = { "bar": "baz" }
ham = eggs
FOO_BAR = baz AND
ham = eggs
If multiple secrets, values that can be parsed into a JSON will be parsed and flattened
false dev_foo = { "bar": "baz" } DEV_FOO = { "bar": "baz" } Not parsed

Note:

Features

IAM Policy

The aws-access-key-id and aws-secret-access-key provided by you should belong to an IAM user with the following minimum permissions:

Example 1 (Simple):

If your secrets are encrypted using the default AWS managed encryption key, then the IAM user needs to have a policy attached similar to:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "secretsmanager:GetSecretValue"
            ],
            "Effect": "Allow",
            "Resource": "*"
        }
    ]
}

Example 2 (Advanced):

If your secrets are encrypted using a customer managed AWS Key Management Service (KMS) key, then the IAM user needs a policy similar to the one below. We can restrict access to specific secrets (resources) in a specific region or we can use * for 'Any'.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "secretsmanager:GetSecretValue",
                "kms:Decrypt"
            ],
            "Effect": "Allow",
            "Resource": [
              "arn:aws:secretsmanager:*:000000000000:secret:*",
              "arn:aws:secretsmanager:us-east-1:000000000000:secret:mySecretID"
            ]
        }
    ]
}

Here 000000000000 is your AWS account ID, us-east-1 is the AWS region code which has the secret(s) and mySecretID is the ID of your secret (usually different from a secret name). Please refer your AWS Secrets Manager console for the exact resource ARN.

Contributing

We would love for you to contribute to @abhilash1in/aws-secrets-manager-action. Issues and Pull Requests are welcome!

License

The scripts and documentation in this project are released under the MIT License.