Giter VIP home page Giter VIP logo

aws-sso-credentials's Introduction

aws-sso-credentials

About

aws-sso-credentials - A simple Python tool to simplify getting short-term credential tokens for CLI/Boto3 operations when using AWS SSO. Uses standard AWS CLI configuration files and allows easy swapping between roles/accounts.

Motivation

In my organisation we use various CLI/Boto3 based tools with AWS. We have several accounts/roles and need a way to handle MFA, switch between accounts/roles, grab temporary session credentials and make sure they're up to date. To this end our go-to tool of choice was Limes.

We switched to using AWS SSO linked to our Azure AD to centralise user management. This works great for Single-Sign-On but and the new AWS CLI v2 supports AWS SSO natively. However, getting temporary credentials for use with Boto3 based apps, especially one that doesn't support profiles was a pain involving copying credentials from a web portal, exporting environment variables and a lot of error prone manual steps.

This script is a quick work around to give us something functional that fits with our way of working until something better comes along. Maybe it works for you too.

How it works

This script piggy-backs on the new AWS CLI tool to read the SSO credentials cache and then makes Boto3 calls to retrieve the temporary credentials for the relevant account/role you want.

It uses the standard AWS CLI configuration files, can trigger a SSO login session if needed and gives you an interactive command line interface to switch between the role and account you want. It will can also copy your chosen profile/credentials into the default profile for times where don't want/can't tell your application to use a specific profile.

Prerequisites

The script is written in Python 3 and requires a working installation of AWS CLI v2.

The scripts dependencies are defined in the requirements.txt file. You can install these with:

  pip install -r requirements.txt

Setting up

  1. Install the AWS CLI v2 and configure your profiles as per the documentation. For example:
[profile dev-env]
region = eu-west-1
sso_start_url = https://yoursso.awsapps.com/start
sso_region = eu-west-1
sso_account_id = 123456654321
sso_role_name = DevOps

[profile prod-env]
region = eu-west-1
sso_start_url = https://yoursso.awsapps.com/start
sso_region = eu-west-1
sso_account_id = 543210012345
sso_role_name = DevOps
  1. Run the AWS CLI tool at least once using one of the profiles you created so that the SSO cache is created.
  aws sso login --profile dev-env
  1. Copy the awssso script to somewhere you can run it. Usually somewhere on your %PATH% or make a symlink to it from somewhere like /usr/local/bin. Make sure to make it executable, i.e. chmod ug+x awssso.

That's it. You should be good to go.

Usage

You can run awssso passing it the name of the profile you want credentials for.

  $ awssso dev-env

If you don't pass a profile name it will allow you to select from a list:

   $ awssso
   
   [?] Please select an AWS config profile: dev-env
   default
 > dev-env
   prod-env

Once the profile is selected, the script will check if you're current SSO credentials are valid and warn you if they will expire soon. It will then use these credentials to get the short term-credentials and copy them to your .aws/credentials file.

You can then use these credentials with the tool of your choice either by passing the profile name, or setting the profile in your environment:

  export AWS_PROFILE=dev-env

If you want to avoid having to set a profile, use the -d option detailed below.

Options

  • -h, --help - Show help and a list of command line options.
  • -v, --verbose Verbose mode. Tells you what the script is doing and dumps information about when your SSO credentials and temporary credentials expire.
  • --login Invokes the AWS CLI to perform a SSO login and refresh SSO credentials.
  • --docker Use the Docker version of the AWS CLI
  • -d, --use-default Copies the chosen profile and credentials to the default profile. This removes the need to pass a profile name or export the AWS_PROFILE environment variable.

Example

Here is a simple example that I use in my own day-to-day routine.

  $ awsso --login -v -d dev-env
  
  Attempting to automatically open the SSO authorization page in your default browser.
  If the browser does not open or you wish to use a different device to authorize this request,
  open the following URL:

  https://device.sso.eu-west-1.amazonaws.com/

  Then enter the code:

  ABCD-WXYZ
  Successully logged into Start URL: https://yoursso.awsapps.com/start

  Reading profile: [profile dev-env]

  Checking for SSO credentials...
  Found credentials. Valid until 2020-05-01 22:32:11 UTC

  Fetching short-term CLI/Boto3 session token...
  Got session token. Valid until 2020-05-01 18:32:11 UTC

  Adding to credential files under [default]
  Copying profile [profile dev-env] to [default]

aws-sso-credentials's People

Contributors

listik avatar lizacodes avatar neiljed avatar zscholl 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

aws-sso-credentials's Issues

Added new features

Hi,

I added a couple of new features to the script which will be useful for the wider audience.

  1. Capability to differentiate SSO and non-SSO profiles
  2. Capability to get SSO credentials for all SSO profiles

When I am trying to push the branch for PR and merge, I am getting blocked.

Please let me know how can I push my changes?
I have attached the updated file.

Kind regards,
Syed

Deprecation warning.

There's a deprecation warning showing up now in the awssso.py, posting this for awareness.

awssso.py:228: DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC).
expires = datetime.utcfromtimestamp(response['roleCredentials']['expiration'] / 1000.0).astimezone(UTC)

awssso.py:249: DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC).
session_expiration_human = datetime.utcfromtimestamp(session_expiration_unix).strftime('%Y-%m-%dT%H:%M:%S%z')

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.