The Ultimate Guide to Automating AWS Access Keys Updates with GitLab CI Script
Image by Iona - hkhazo.biz.id

The Ultimate Guide to Automating AWS Access Keys Updates with GitLab CI Script

Posted on

Are you tired of manually updating or adding AWS access keys in AWS Secrets Manager? Do you find it frustrating to keep track of multiple keys and rotate them regularly? Well, worry no more! In this article, we’ll show you how to automate the process using a GitLab CI script, making your life as a DevOps engineer much easier.

What is AWS Secrets Manager?

AWS Secrets Manager is a service offered by Amazon Web Services that enables you to securely store, manage, and retrieve sensitive data such as database credentials, API keys, and encryption keys. It provides a secure way to store and rotate your access keys, making it an essential tool for any AWS-based application.

Why Automate Updating or Adding AWS Access Keys?

Manually updating or adding AWS access keys in AWS Secrets Manager can be a tedious and error-prone process. With multiple keys to manage, it’s easy to forget to update or rotate them regularly, leaving your application vulnerable to security risks. Automating this process ensures that your access keys are always up-to-date and secure, reducing the risk of unauthorized access to your AWS resources.

Prerequisites

Before we dive into the GitLab CI script, make sure you have the following prerequisites:

  • A GitLab account with a repository containing your AWS access keys.
  • An AWS account with AWS Secrets Manager enabled.
  • AWS CLI installed and configured on your machine.
  • A basic understanding of GitLab CI/CD pipelines and YAML syntax.

Step 1: Create a GitLab CI Script

Create a new file in your GitLab repository’s root directory, named `.gitlab-ci.yml`. This file will contain the script that automates the updating or adding of AWS access keys in AWS Secrets Manager.


image: amazon/aws-cli

stages:
  - update-access-keys

variables:
  AWS_DEFAULT_REGION: 'us-west-2'
  AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
  AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
  SECRETS_MANAGER_REGION: 'us-west-2'
  SECRETS_MANAGER_ID: 'your-secrets-manager-id'

update-access-keys:
  stage: update-access-keys
  script:
    - aws secretsmanager get-secret-value --secret-id $SECRETS_MANAGER_ID --region $SECRETS_MANAGER_REGION --query SecretString --output text > access_keys.json
    - python update_access_keys.py
  only:
    - main

In this script, we’re using the Amazon AWS CLI image to run our pipeline. We define three stages: `update-access-keys`, `variables`, and `script`. The `variables` section defines the environment variables we’ll use in our script, including the AWS access key ID, secret access key, Secrets Manager region, and Secrets Manager ID.

Step 2: Create a Python Script to Update Access Keys

Create a new file in the same directory as your `.gitlab-ci.yml` file, named `update_access_keys.py`. This script will read the current access keys from AWS Secrets Manager, update or add new keys, and store the updated keys back in AWS Secrets Manager.


import json
import boto3

secrets_manager = boto3.client('secretsmanager', region_name='us-west-2')

def get_current_access_keys():
    response = secrets_manager.get_secret_value(SecretId='your-secrets-manager-id')
    access_keys = json.loads(response['SecretString'])
    return access_keys

def update_access_keys(access_keys):
    access_keys['AWS_ACCESS_KEY_ID'] = 'new-access-key-id'
    access_keys['AWS_SECRET_ACCESS_KEY'] = 'new-secret-access-key'
    return access_keys

def store_access_keys(access_keys):
    secrets_manager.update_secret(SecretId='your-secrets-manager-id', SecretString=json.dumps(access_keys))

def main():
    current_access_keys = get_current_access_keys()
    updated_access_keys = update_access_keys(current_access_keys)
    store_access_keys(updated_access_keys)

if __name__ == '__main__':
    main()

In this script, we’re using the Boto3 library to interact with AWS Secrets Manager. We define three functions: `get_current_access_keys` to read the current access keys, `update_access_keys` to update or add new keys, and `store_access_keys` to store the updated keys back in AWS Secrets Manager.

Step 3: Configure AWS CLI and Secrets Manager

Make sure you have the AWS CLI installed and configured on your machine. You’ll need to set up your AWS access key ID and secret access key as environment variables, and configure the AWS CLI to use the correct region and Secrets Manager ID.


export AWS_ACCESS_KEY_ID=your-access-key-id
export AWS_SECRET_ACCESS_KEY=your-secret-access-key
export AWS_DEFAULT_REGION=us-west-2
export SECRETS_MANAGER_REGION=us-west-2
export SECRETS_MANAGER_ID=your-secrets-manager-id

Step 4: Run the GitLab CI Pipeline

Commit your changes to your GitLab repository, and push the updates to trigger the pipeline. You can view the pipeline’s progress in the GitLab CI/CD dashboard.

Stage Status
update-access-keys Passed

If everything is set up correctly, the pipeline should run successfully, updating or adding the AWS access keys in AWS Secrets Manager.

Conclusion

And that’s it! With this GitLab CI script, you’ve automated the process of updating or adding AWS access keys in AWS Secrets Manager. This ensures that your access keys are always up-to-date and secure, reducing the risk of unauthorized access to your AWS resources.

Remember to rotate your access keys regularly to maintain the highest level of security. You can schedule the pipeline to run on a regular basis, such as daily or weekly, to automate the rotation process.

FAQs

Q: What is the purpose of the `update_access_keys.py` script?

A: The `update_access_keys.py` script reads the current access keys from AWS Secrets Manager, updates or adds new keys, and stores the updated keys back in AWS Secrets Manager.

Q: How do I set up the AWS CLI and Secrets Manager?

A: Make sure you have the AWS CLI installed and configured on your machine. Set up your AWS access key ID and secret access key as environment variables, and configure the AWS CLI to use the correct region and Secrets Manager ID.

Q: How often should I rotate my access keys?

A: It’s recommended to rotate your access keys regularly, such as every 90 days, to maintain the highest level of security. You can schedule the pipeline to run on a regular basis to automate the rotation process.

With this comprehensive guide, you’ve learned how to automate the process of updating or adding AWS access keys in AWS Secrets Manager using a GitLab CI script. Start automating your access key management today and take the first step towards a more secure AWS environment!

Frequently Asked Question

Get the scoop on automating AWS access key updates in AWS Secrets Manager using GitLab CI scripts!

Can I update AWS access keys in AWS Secrets Manager using GitLab CI scripts?

Yes, you can! GitLab CI scripts can be used to automate updating or adding AWS access keys in AWS Secrets Manager. You’ll need to create a `.gitlab-ci.yml` file with a script that uses the AWS CLI to update the access keys. You’ll also need to store your AWS credentials securely using GitLab CI variables or a secrets manager like Hashicorp’s Vault.

What’s the simplest way to update AWS access keys using GitLab CI?

One simple approach is to create a GitLab CI job that runs an AWS CLI command to update the access key. You can use the `aws secretsmanager update-secret` command, specifying the secret ID, access key ID, and new access key value. For example: `aws secretsmanager update-secret –secret-id –access-key-id –access-key-value `.

How do I securely store my AWS credentials in GitLab CI?

To store your AWS credentials securely, you can use GitLab CI variables. Create a CI variable for your AWS access key ID and secret access key, and mark them as “masked” to hide their values in the GitLab CI logs. You can also use a secrets manager like Hashicorp’s Vault to store your AWS credentials, and then retrieve them in your GitLab CI script using the Vault API.

Can I automate rotating AWS access keys using GitLab CI?

Yes, you can! By using GitLab CI scripts, you can automate rotating your AWS access keys on a schedule. Create a CI job that runs periodically (e.g., daily or weekly), and have it generate a new access key pair using the AWS CLI `create-access-key` command. Then, update the secret in AWS Secrets Manager with the new access key pair, and delete the old access key.

Are there any Gotchas I should watch out for when automating AWS access key updates?

Yes, be aware of a few potential gotchas! Make sure to handle errors and exceptions properly in your GitLab CI script, as a failed update can leave your AWS access keys in an inconsistent state. Also, ensure that your script has the necessary permissions to update the secret in AWS Secrets Manager. Finally, consider implementing a rollback mechanism in case something goes wrong during the update process.

Leave a Reply

Your email address will not be published. Required fields are marked *