Metadata-Version: 2.4
Name: ot-aws-sso-login
Version: 1.2.2
Summary: Configure and authenticate an AWS IAM Identity Center (SSO) profile in one command.
Classifier: Programming Language :: Python :: 3
Classifier: Environment :: Console
Classifier: Operating System :: OS Independent
Requires-Python: >=3.9
Description-Content-Type: text/markdown

# ot-aws-sso-login

**Turn an assigned AWS account and SSO role into a configured, authenticated, verified CLI profile.**

`ot-aws-sso-login` is a lightweight Python command-line tool that configures an
AWS IAM Identity Center profile, reuses a working session or starts browser
sign-in, and verifies the resulting AWS account and role.

It brings a repetitive developer setup workflow into one command while keeping
authentication with the official AWS CLI and the organization's identity provider.

```bash
ot-aws-sso-login \
  --sso-start-url https://example.awsapps.com/start \
  --account 123456789012 \
  --role DevOps \
  --profile-name aws_gcc
```

## Why I built this

AWS SSO authentication is more than opening a login page. A developer also needs
the correct Identity Center settings, account ID, role, local profile, and AWS
region. These details can become repetitive setup work across terminals and
development machines.

I built this package to address that workflow:

| Problem | Approach |
| --- | --- |
| Repeating the same organization-level SSO settings | Reusable environment settings, with command-line overrides |
| Different developers have different account and role assignments | Keep the assigned account and role configurable per user |
| Role-derived names do not always match a developer's workflow | Support custom local profile names such as `aws_gcc` |
| An existing session might already work | Check it before starting another sign-in |
| A successful command alone does not confirm the intended identity | Verify the returned AWS account and SSO role with STS |
| Existing AWS environment variables can confuse authentication | Remove selected credential/profile overrides from child processes |
| Updating a shared AWS config file needs care | Back up an existing config and replace the updated file atomically |
| A local script is awkward to distribute and update | Package it as an installable CLI with no Python runtime dependencies |

The guiding principle is to **automate the setup around authentication, rather
than reimplement authentication itself**.

## What it does

- Checks for AWS CLI v2, version 2.9 or newer.
- Creates or updates an `sso-session` section and a named profile.
- Supports custom profile names through a flag or environment variable.
- Checks whether the configured profile can call AWS STS.
- Runs `aws sso login` when that check fails.
- Verifies the authenticated account and expected SSO role.
- Supports browser, no-browser, and device-code login options.
- Provides verbose logging and a quiet mode.
- Runs as either `ot-aws-sso-login` or `python -m ot_aws_sso_login`.

### What it does not do

- It does not create AWS accounts, assign roles, or grant permissions.
- It does not automatically discover or offer a selection of assigned roles.
  Supply an account and role already assigned to you.
- It does not read `.env` files automatically.
- It does not change `AWS_PROFILE` in your parent shell.
- It does not replace the AWS CLI, your identity provider, or your organization's
  MFA requirements.
- It is an interactive developer login helper, not an unattended CI authentication
  mechanism.

## Requirements

- Python 3.9 or newer.
- AWS CLI **v2**, version **2.9 or newer**. For device-code mode, use a current
  AWS CLI v2 release that supports `--use-device-code`.
- An AWS IAM Identity Center account and role assignment.
- Your organization's IAM Identity Center start URL; it is not embedded in the package.
- Network access to the identity provider and AWS services.

Check your local tools:

```bash
python --version
aws --version
```

Install AWS CLI using the [official installation guide](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html).
The Python package does not install AWS CLI for you.

## Installation

From PyPI:

```bash
python -m pip install --upgrade ot-aws-sso-login
```

Alternatively, install the CLI in an isolated environment with
[pipx](https://pipx.pypa.io/):

```bash
pipx install ot-aws-sso-login
```

For an existing pipx installation:

```bash
pipx upgrade ot-aws-sso-login
```

Verify installation:

```bash
ot-aws-sso-login --version
ot-aws-sso-login --help
```

## Quick start

### 1. Configure and sign in

Replace the example account and role with your actual assignments. The profile
name is a local alias you choose; it does not need to match your role name.
Replace the example start URL with the one supplied by your organization.

```bash
ot-aws-sso-login \
  --sso-start-url https://example.awsapps.com/start \
  --account 123456789012 \
  --role DevOps \
  --profile-name aws_gcc
```

Complete company sign-in in the browser if prompted. If the existing session is
working, the tool skips the login step.

### 2. Use the profile

Pass the profile explicitly:

```bash
aws sts get-caller-identity --profile aws_gcc
aws s3 ls --profile aws_gcc
```

Or select it for subsequent commands in your current Bash/Zsh/Git Bash terminal:

```bash
export AWS_PROFILE=aws_gcc
aws sts get-caller-identity
```

Individual AWS operations still depend on the permissions assigned to your role.

### 3. Run again when needed

Repeat the same command to check the session and sign in if necessary. The
profile configuration persists in your AWS config file.

## Configure once with environment variables

For Bash, Zsh, or Git Bash:

```bash
export AWS_SSO_ACCOUNT=123456789012
export AWS_SSO_ROLE=DevOps
export AWS_SSO_PROFILE_NAME=aws_gcc
export AWS_SSO_START_URL="https://example.awsapps.com/start"

ot-aws-sso-login
```

Put these exports in your shell startup file if you want them available in new
terminals. Each developer should use their own assigned account and role.

### Using a `.env` file

Create a local `.env`:

```dotenv
AWS_SSO_ACCOUNT=123456789012
AWS_SSO_ROLE=DevOps
AWS_SSO_PROFILE_NAME=aws_gcc
AWS_SSO_START_URL=https://example.awsapps.com/start
```

The package **does not load this file automatically**. From the directory
containing it, export its values into Bash, Zsh, or Git Bash:

```bash
set -a
source .env
set +a

ot-aws-sso-login
```

Only source a file you trust: `source` executes shell code, not just assignments.
These examples assume shell-compatible `KEY=value` syntax.

No AWS secret access key, company password, or PyPI token is needed in this file.
If you use it in another repository, follow that repository's policy for keeping
local configuration out of version control.

### PowerShell

Set environment variables directly:

```powershell
$env:AWS_SSO_ACCOUNT = "123456789012"
$env:AWS_SSO_ROLE = "DevOps"
$env:AWS_SSO_PROFILE_NAME = "aws_gcc"
$env:AWS_SSO_START_URL = "https://example.awsapps.com/start"

ot-aws-sso-login

$env:AWS_PROFILE = "aws_gcc"
aws sts get-caller-identity
```

## Configuration reference

For settings with a matching flag, precedence is:

**Command-line flag > environment variable > built-in default.**

An account, role, and SSO start URL are required, either as flags or environment
variables. There is no built-in organization URL.

| Flag | Environment variable | Default / purpose |
| --- | --- | --- |
| `--account` | `AWS_SSO_ACCOUNT` | Required: 12-digit assigned AWS account ID |
| `--role` | `AWS_SSO_ROLE` | Required: assigned SSO role name |
| `--profile-name` | `AWS_SSO_PROFILE_NAME` | `<role>-<account>`; custom local profile alias |
| `--region` | `AWS_SSO_DEFAULT_REGION` | `us-east-1`; region used by the created profile |
| `--sso-start-url` | `AWS_SSO_START_URL` | Required: your organization's HTTPS Identity Center start URL |
| `--sso-region` | `AWS_SSO_REGION` | `us-east-1`; region hosting IAM Identity Center |
| `--sso-session` | `AWS_SSO_SESSION` | `opentext-sso`; shared SSO session name |
| No flag | `AWS_CONFIG_FILE` | `~/.aws/config`; standard AWS config path override |

Set your Identity Center instance details (replace the example URL):

```bash
export AWS_SSO_START_URL="https://example.awsapps.com/start"
export AWS_SSO_REGION="eu-west-1"
export AWS_SSO_SESSION="other-org-sso"
```

Choose a different session name for a different Identity Center instance.
Updating a shared session's URL or region also affects other profiles referencing
that session.

### Additional options

| Option | Purpose |
| --- | --- |
| `--no-browser` | Ask AWS CLI not to open a browser automatically |
| `--use-device-code` | Request device-code authentication; requires a supporting AWS CLI version |
| `-v`, `--verbose` | Include debug logging |
| `-q`, `--quiet` | Suppress informational package logs; AWS CLI interaction may still appear |
| `--version` | Print package version |
| `-h`, `--help` | Show available arguments |

For a remote terminal where you will sign in using a browser on another device:
With `AWS_SSO_START_URL` already exported:

```bash
ot-aws-sso-login \
  --account 123456789012 \
  --role DevOps \
  --profile-name aws_gcc \
  --no-browser \
  --use-device-code
```

Follow the URL and code instructions printed by AWS CLI.

## Custom profiles and multiple assignments

A profile is a local name for an account/role configuration. Renaming a profile
does not change your permissions.

You can keep separate profiles for separate assignments:
These commands assume `AWS_SSO_START_URL` is already exported:

```bash
ot-aws-sso-login \
  --account 123456789012 --role DevOps --profile-name gcc-dev

ot-aws-sso-login \
  --account 210987654321 --role ReadOnly --profile-name gcc-readonly

aws sts get-caller-identity --profile gcc-dev
aws sts get-caller-identity --profile gcc-readonly
```

Reusing a profile name updates that profile's SSO settings. Use distinct names
when you want to retain multiple account/role configurations.

## How it works

```text
Read flags and environment variables
                |
Validate arguments and check AWS CLI
                |
Read existing AWS config and create a one-time backup
                |
Update the SSO session and profile; write atomically
                |
Check profile with STS get-caller-identity
                |
       +--------+--------+
       |                 |
   Check works       Check fails
       |                 |
       |          Run aws sso login
       |                 |
       +--------+--------+
                |
Verify expected account and SSO role with STS
                |
Report the authenticated identity and profile
```

The profile is written before login, so it remains configured even if sign-in is
cancelled or fails.

### Example generated configuration

For the `aws_gcc` profile:

```ini
[sso-session opentext-sso]
sso_start_url = https://example.awsapps.com/start
sso_region = us-east-1
sso_registration_scopes = sso:account:access

[profile aws_gcc]
sso_session = opentext-sso
sso_account_id = 123456789012
sso_role_name = DevOps
region = us-east-1
output = json
```

Other sections and keys are retained when the configuration is rewritten, but
comments and formatting are not preserved. Existing keys not managed by the
tool are not removed from an updated profile.

## Design decisions and trade-offs

### Delegate authentication to AWS CLI

The package orchestrates the official CLI instead of implementing its own OAuth
flow, MFA handling, or credential cache. Authentication behavior and token
storage remain the responsibility of AWS CLI and the identity provider.

### Separate organization settings from user assignments

Reusable environment settings reduce repeated setup, while account, role, and
custom profile settings let each user work with their own assignments. The SSO
start URL is supplied by the user, never embedded in the package. Overrides
allow the same tool to target another Identity Center instance.

### Verify identity, not only command success

After session checking or login, the tool calls STS and checks the account and
the `AWSReservedSSO_<role>_...` assumed-role ARN. A successful login is not treated
as sufficient evidence of the intended identity.

### Limit inherited credential confusion

For login and STS child processes, the tool removes these inherited variables:

```text
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_SESSION_TOKEN
AWS_PROFILE
AWS_DEFAULT_PROFILE
```

It logs the variable names, not their values. This does not unset anything in
your shell or eliminate every possible source of AWS configuration overrides.
For later commands, stale credential variables in your shell may still need to
be cleared.

### Protect the config from partial writes

The tool reads an existing config before modifying it and makes a one-time
backup named `<config-filename>.bak-sso-script`. It writes the updated config
to a temporary file in the same directory, flushes it, and replaces the target
file atomically.

The new config is written with owner-only permissions where supported. Windows
access control follows the host filesystem's permission behavior.

Atomic replacement protects against a partially written config; it is **not**
a lock against concurrent writers. Avoid running multiple config-editing
processes at the same time. The backup is the first snapshot, not a backup of
every update.

### Keep the package lightweight

The implementation uses Python's standard library and declares no Python
runtime dependencies. AWS CLI is a separate prerequisite, keeping the package
focused on configuration and orchestration.

## Troubleshooting

### Required account, role, or start URL arguments are missing

The values are not available in the process environment. A `.env` file alone
does not export them.

Load your trusted file:

```bash
set -a
source .env
set +a
ot-aws-sso-login
```

Or pass `--account`, `--role`, and `--sso-start-url` directly.

Starting with version 1.2.2, users upgrading from older versions must supply
`AWS_SSO_START_URL` or `--sso-start-url`; the built-in organization URL has been
removed.

### `ot-aws-sso-login: command not found`

Activate the virtual environment where you installed the package, or ensure
your Python/pipx executable directory is on `PATH`.

You can also use the Python interpreter where it is installed:

```bash
python -m ot_aws_sso_login --help
```

### AWS CLI is missing or too old

Run `aws --version`. Install or update to AWS CLI v2 using the official AWS
installer. If `--use-device-code` is unrecognized, update AWS CLI or omit that
option when device-code authentication is not needed.

### Browser sign-in succeeds, but account or role verification fails

Confirm the account and role are assigned to your user, check for conflicting
configuration on a reused profile, and try a fresh custom profile name. The
tool intentionally reports a failure when STS does not match the requested
identity.

### It starts login even though I recently signed in

Any unsuccessful initial STS check triggers login. An expired session is one
possible cause, but network, endpoint, or configuration errors can also make
that check fail. Check connectivity and try:

```bash
aws sts get-caller-identity --profile aws_gcc
```

### AWS config cannot be read

Check the config path, file permissions, and INI syntax. The tool reports
unreadable or malformed existing config files rather than deliberately
replacing them with a new configuration.

### Later AWS commands use the wrong identity

Pass `--profile aws_gcc` explicitly and inspect credential variables already
exported in your shell. The package's cleaned child-process environment does
not modify your parent terminal.

### Exit status

- `0`: authentication and identity verification completed successfully.
- `1`: handled configuration, command, or identity verification failure.
- `2`: argument validation failure.
- `130`: interrupted with Ctrl+C during the main workflow.

Some filesystem failures can still raise a Python traceback; these are not
currently all translated into the package's user-facing error format.

## Local development and packaging

The project uses a `src` layout:

```text
pyproject.toml                  Package metadata and CLI entry point
src/ot_aws_sso_login/
    __init__.py                 Package version
    __main__.py                 python -m entry point
    cli.py                      Configuration and authentication workflow
```

For local development:

```bash
python -m venv .venv
source .venv/bin/activate
python -m pip install -e .
```

On PowerShell, activate with `.venv\Scripts\Activate.ps1` instead.

To build and validate distributions:

```bash
python -m pip install build twine
python -m build
python -m twine check dist/ot_aws_sso_login-1.2.2*
```

When releasing, update the version in both `pyproject.toml` and
`src/ot_aws_sso_login/__init__.py`, then use that version in the distribution
commands. Build and metadata checks do not replace functional testing of login
with an assigned account and role.

## Scope and current limitations

- There is no automated regression test suite in the current project.
- Real authentication depends on the local AWS CLI, network, and organization
  configuration.
- Config comments and formatting are lost during rewriting.
- Concurrent config edits are not coordinated.
- The package does not automatically clean conflicting legacy keys from an
  existing profile.
- `get_token.py` is a separate browser-token helper, not part of this package's
  installed SSO command or documented authentication workflow.

These boundaries keep the current tool focused: simplify developer SSO setup,
retain the official authentication flow, and verify the resulting identity.
