Git Product home page Git Product logo

aws-rotate-key's Introduction

aws-rotate-key

As a security best practice, AWS recommends that users periodically regenerate their API access keys. This tool simplifies the rotation of access keys defined in your credentials file.

When run, the program will list the current access keys associated with your IAM user, and print the steps it has to perform to rotate them. It will then wait for your confirmation before continuing.

Usage

$ aws-rotate-key --help
Usage of aws-rotate-key:
  -auth-profile string
    	Use a different profile when calling AWS.
  -d	Delete the old key instead of deactivating it.
  -mfa
    	Use MFA.
  -mfa-serial-number string
    	Specify the MFA device to use. (optional)
  -profile string
    	The profile to use. (default "default")
  -version
    	Print version number
  -y	Automatic "yes" to prompts.

Example

$ aws-rotate-key --profile work
Using access key AKIAJMIGD6UPCXCFWVOA from profile "work".
Your user ARN is: arn:aws:iam::123456789012:user/your_username

Your user has 2 access keys:
- AKIAI3KI7UC6BPI4O57A (Inactive, created 2018-11-22 21:47:46 +0000 UTC, last used 2018-11-30 20:35:41 +0000 UTC for service s3 in us-west-2)
- AKIAJMIGD6UPCXCFWVOA (Active, created 2018-11-30 21:55:57 +0000 UTC, last used 2018-12-20 12:14:10 +0000 UTC for service s3 in us-west-2)

You have two access keys, which is the maximum number of access keys allowed.
Do you want to delete AKIAI3KI7UC6BPI4O57A and create a new key? [yN] y
Deleted access key: AKIAI3KI7UC6BPI4O57A
Created access key: AKIAIX46CKYT7E5I3KVQ
Wrote new key pair to /Users/your_username/.aws/credentials
Deactivated old access key: AKIAJMIGD6UPCXCFWVOA
Please make sure this key is not used elsewhere.
Please note that it may take a minute for your new access key to propagate in the AWS control plane.

Install

You can download binaries from the releases section.

You can use Homebrew to install on macOS:

brew install aws-rotate-key

You can install using a PPA on Ubuntu Linux:

sudo add-apt-repository ppa:stefansundin/aws-rotate-key
sudo apt install aws-rotate-key

If you have Go installed then you can download and build the program using:

go install github.com/stefansundin/aws-rotate-key@latest

Setup

Make sure your users have permissions to update their own access keys. The following AWS documentation page explains the required permissions: https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_delegate-permissions_examples.html.

The following IAM policy is enough for aws-rotate-key:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "iam:ListAccessKeys",
                "iam:GetAccessKeyLastUsed",
                "iam:DeleteAccessKey",
                "iam:CreateAccessKey",
                "iam:UpdateAccessKey"
            ],
            "Resource": [
                "arn:aws:iam::AWS_ACCOUNT_ID:user/${aws:username}"
            ]
        }
    ]
}

Warning

Replace AWS_ACCOUNT_ID with your AWS account id.

Require MFA

You can require MFA by adding a Condition clause. Please note that you have to use the -mfa option when running the program.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "iam:ListMFADevices"
            ],
            "Resource": [
                "arn:aws:iam::AWS_ACCOUNT_ID:user/${aws:username}"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "iam:ListAccessKeys",
                "iam:GetAccessKeyLastUsed",
                "iam:DeleteAccessKey",
                "iam:CreateAccessKey",
                "iam:UpdateAccessKey"
            ],
            "Resource": [
                "arn:aws:iam::AWS_ACCOUNT_ID:user/${aws:username}"
            ],
            "Condition": {
                "Bool": {
                    "aws:MultiFactorAuthPresent": true
                }
            }
        }
    ]
}

Note that this makes it harder to rotate access keys using aws-cli commands, as it only supports MFA when assuming roles. You will still be able to use the AWS management console.

Contribute

To download and hack on the source code, run:

git clone https://github.com/stefansundin/aws-rotate-key.git
cd aws-rotate-key
go build

aws-rotate-key's People

Contributors

beckje01 avatar boroivanov avatar conorgil avatar dstokes avatar lukateras avatar marier-nico avatar michaelpotter avatar stefansundin avatar tuck182 avatar ymatsiuk avatar

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  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

aws-rotate-key's Issues

Rotate keys for another user

Great tool, thank you for it!

I wonder if you have plans to make it possible to rotate someone else keys instead of the user itself?

I have limited users who do not have iam:* at all and I want to use admin's profile to rotate keys for them from the command line.

Automation

That's not really an issue and feel free to close it, but I was thinking (and that's only a suggestion) about fully automating aws key rotation: what if a program can perform AWS API call to import all AWS IAM users, and create new keys per each user, store it in .csv file and then use AWS SNS service to notify each user about new keys? That would be cool to work on and that's what has been on my mind for the last few weeks and just today I came across your repo

Module name changed to github.com/Fullscreen/aws-rotate-key causing issues with `go get`

When you try to go get this project it now errors with the following:

go: downloading github.com/stefansundin/aws-rotate-key v1.0.7
go get: github.com/stefansundin/[email protected]: parsing go.mod:
        module declares its path as: github.com/Fullscreen/aws-rotate-key
                but was required as: github.com/stefansundin/aws-rotate-key

Even though master is correct the last tagged version has this incorrect module name.

Submit to official Homebrew repository

Hi everyone,

If someone would like to submit aws-rotate-key to the official homebrew repository, that would be great. I was researching it, but apparently it is frowned upon to submit your own work. Yeah, that is a funny rule, but it's a rule.

I know that my Homebrew tap should work well for almost everyone, but it also makes some other formulas available that are not related to this project.

If you'd like to help me submit this to Homebrew/homebrew-core, then please feel free to attempt to submit it, and report back here.

Here are some links to get started:

Thanks!

Support for multiple MFA devices

I pushed some commits that add support for multiple MFA devices which was released recently [1]. Unfortunately my personal AWS account doesn't have access to this new feature yet (some technical reason [2] is preventing AWS from rolling this out quickly, if you have a recently created AWS account then you probably already have access).

I'm fairly confident that the new code works but I want to test this thoroughly before I release the new version, so I am waiting until I have access. In the mean time if you want to try out the new version you can install it by running:

go install github.com/stefansundin/aws-rotate-key@7ab41aeea7aac0f224c3a86c8627a3c453915f47

This new version also fixes a problem with hardware MFA tokens (they use serial numbers instead of ARNs). I don't have a hardware token so if anyone could test this that would be great!

Let me know if you find any bugs!

  1. https://aws.amazon.com/blogs/security/you-can-now-assign-multiple-mfa-devices-in-iam/
  2. https://www.reddit.com/r/aws/comments/yx9yr5/comment/iz8e4bd/

Issue with user name starting with `_`

Thanks for making this tool, it's quite useful. I recommended its use to other. And received a weird issue -

~ aws-rotate-key -auth-profile 'nbsa-org-5317/_dev' -profile 'nbsa-org-5317/_dev'
Using access key AKIAWI2WUXBKWKCGKWVU from profile "nbsa-org-5317/_dev".
Your user ARN is: arn:aws:iam::431289055317:user/_nbsa.dev

AccessDenied: User: arn:aws:iam::431289055317:user/_nbsa.dev is not authorized to perform: iam:ListAccessKeys on resource: user null_nbsa.dev with an explicit deny in an identity-based policy
        status code: 403, request id: ffd4e26a-94de-4de9-9a2d-f912b549e750

Looks the resource: user is identified as null_nbsa.dev

Repository move

Hi there,

You may have noticed that this repository has been moved from Fullscreen/aws-rotate-key to stefansundin/aws-rotate-key.

This program was developed by me a few years ago when I was employed at Fullscreen. We decided to open source it so that it could be to the benefit of everyone who uses AWS. Since that time, I left Fullscreen but I have continued to help maintain this project. We decided that there is no reason to spend precious company time from Fullscreen for the long-term maintenance of this program, so we transferred the repository to my GitHub user.

There are no big changes coming. I plan to introduce a PPA to make the program easier to install on Debian/Ubuntu (hopefully later this week).

I will update the instructions to my own Homebrew tap, but the one that Fullscreen hosts will continue to work for the foreseeable future. There may be a slight lag to get it updated when new versions are released. Update: the official Homebrew repository now includes aws-rotate-key.

I created this issue to explain why the repository was transferred. Please comment if you have any further questions.

I want to thank Fullscreen for allowing us to create cool tools like this one, and I hope lots of companies benefit from it being open source.

Ubuntu 18.04 runtime error

Greetings,

When running the script manually it runs smooothly, but running it under cron i'm receiving the following error:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x673d99]

goroutine 1 [running]:
main.main()
	/Users/bivanov/code/fs/aws-rotate-key/main.go:43 +0x2129

Any idea what that could be?

Rotation fails on Ubuntu 18.04.1 LTS (Bionic Beaver)

aws-rotate-keys fails on Ubuntu 18.04.1 LTS (Bionic Beaver) with the following error message:

Using access key <AWS_OLD_ACCESS_KEY> from profile "<AWS_PROFILE>".
Your user arn is: <ARN>

You have 1 access key associated with your user:
- <AWS_OLD_ACCESS_KEY> (Active, created <CREATED_TIMESTAMP>, last used <LAST_USED_TIMESTAMP> for service <SERVICE> in <AWS_REGION>)

Do you want to create a new key and deactivate <AWS_OLD_ACCESS_KEY>? [yN] y
Created access key <AWS_NEW_ACCESS_KEY>.
Failed to replace old access key. Aborting.
Please verify that the file <HOME_DIR>/.aws/credentials is formatted correctly.
Deleted access key <AWS_NEW_ACCESS_KEY>.

According to colleagues, this issue also appeared on MacOS

Rotate without deactivating/deleting

Would be nice to be able to rotate the "other" access key without disabling the current access key.

With longer-running batch processes that only read their credentials once, deactivating the "current" key after rotation may mean disabling a key that's still in use/expected to work.

This would mean a user always has two access keys active, and e.g. every 24h rotates "the older one".

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.