physera / onelogin-aws-cli

Assume an AWS Role and cache credentials using Onelogin
MIT License
67 stars 32 forks source link
aws-cli onelogin saml

onelogin-aws-cli

A CLI utility that helps with using AWS CLI when using AWS Roles and OneLogin authentication.

Build Status codecov

This package provides a CLI utility program that:

In order to be able to use this program, you must first Configure SAML for AWS in OneLogin.

Note that while the repo and the pip package are called onelogin-aws-cli, the installed program is called onelogin-aws-login.

Installation

To install, use pip:

$ pip install onelogin-aws-cli

Note that onelogin-aws-cli requires Python 3.

Note that it is not recommended to install Python packages globally on your system. Pyenv is a great tool for managing your Python environments.

Another possibility is to install from source using pip:

$ cd onelogin-aws-cli
$ pip3 install .

Yet another is to install using pipx:

$ cd onelogin-aws-cli
$ pipx install --verbose --spec . onelogin-aws-cli

Usage

Running onelogin-aws-login will perform the authentication against OneLogin, and cache the credentials in the AWS CLI Shared Credentials File.

For every required piece of information, the program will present interactive inputs, unless that value has already been provided through either command line parameters, environment variables, or configuration file directives.

$ onelogin-aws-login
Onelogin Username: myuser@mycompany.com
Onelogin Password:
Google Authenticator Token: 579114
Pick a role:
[1]: arn:aws:iam::166878887401:role/onelogin-test-ec2
[2]: arn:aws:iam::166878887401:role/onelogin-test-s3
[3]: arn:aws:iam::772123451421:role/onelogin-test-s3
? 3
Credentials cached in '/Users/myuser/.aws/credentials'
Expires at 2018-05-24 15:15:41+00:00
Use aws cli with --profile 772123451421:role/onelogin-test-s3/myuser@mycompany.com

Interactive Configuration

Passing the -c or --configure command line parameter will start an interactive configuration, that presents a series of interactive inputs to gather the required pieces of information, and save them to the configuration file automatically.

$ onelogin-aws-login -c

This is a special mode of operation for this program, and it is typically only used once, after installing the program.

However, note that it only supports a basic use case. More advanced use cases will require manual editing of the configuration file.

Command Line Parameters

Environment Variables

Configuration File

The configuration file is located at ~/.onelogin-aws.config.

It is an .ini file where each section defines a config name, which can be provided using either the command line parameter --config-name or the environment variable ONELOGIN_AWS_CLI_CONFIG_NAME.

If no config name is provided, the [defaults] section is used automatically.

All other sections automatically inherit from the [defaults] section, and can define any additional directives as desired.

Directives

Example

[defaults]
base_uri = https://api.us.onelogin.com/
subdomain = mycompany
username = john@mycompany.com
client_id = f99ee51f00400649280db1028ffa3ca9b21b680f2189b238d342cc8158c401c7
client_secret = a85234b6db01a29a493e2422d7930dffe6f4d3a826270a18838574f6b8ef7c3e
save_password = yes
profile = mycompany-onelogin
duration_seconds = 3600
auto_determine_ip_address = yes

[testing]
aws_app_id = 555029

[staging]
aws_app_id = 555045

[live]
aws_app_id = 555070

[testing-admin]
aws_app_id = 555029
role_arn = arn:aws:iam::123456789123:role/Admin

[staging-admin]
aws_app_id = 555045
role_arn = arn:aws:iam::123456789123:role/Admin

[live-admin]
aws_app_id = 555070
role_arn = arn:aws:iam::123456789123:role/Admin

This example will let you select from 6 config names, that are variations of the same base values specified in [defaults].

The first three, testing, staging, and live, all have different OneLogin application IDs.

The latter three, testing-admin, staging-admin, and live-admin, also have role_arn specified, so they will automatically assume the role with that ARN.

For example, to use the staging config, you could run:

$ onelogin-aws-login -C staging

And to use the live-admin config, you could run:

$ onelogin-aws-login -C live-admin

Developing onelogin-aws-cli

Run tests

$ python3 -m venv env
$ source env/bin/activate
(env)$ pip install -r requirements.txt
(env)$ python setup.py nosetests
(env)$ deactivate