|
| 1 | +# aws-sso-refresh |
| 2 | + |
| 3 | +Automatically refresh AWS SSO sessions before they expire, so you never hit "token expired" errors again. |
| 4 | + |
| 5 | +## The Problem |
| 6 | + |
| 7 | +When using AWS SSO (Identity Center), your session tokens expire after a few hours. This means you constantly have to run `aws sso login --profile xyz` when your tokens expire mid-task. |
| 8 | + |
| 9 | +## The Solution |
| 10 | + |
| 11 | +`aws-sso-refresh` runs as a background daemon and proactively refreshes your SSO sessions before they expire. It opens a browser window for re-authentication (which auto-approves if you're already logged in), so your tokens stay fresh. |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +### Homebrew (recommended) |
| 16 | + |
| 17 | +```bash |
| 18 | +brew install jhubbardsf/aws-sso-refresh/aws-sso-refresh |
| 19 | +``` |
| 20 | + |
| 21 | +### Manual / curl |
| 22 | + |
| 23 | +```bash |
| 24 | +curl -fsSL https://raw.githubusercontent.com/jhubbardsf/aws-sso-refresh/main/install.sh | bash |
| 25 | +``` |
| 26 | + |
| 27 | +## Requirements |
| 28 | + |
| 29 | +- **macOS** (uses launchd for background scheduling) |
| 30 | +- **bash 4.0+** (macOS ships with 3.2 - install with `brew install bash`) |
| 31 | +- **jq** (`brew install jq`) |
| 32 | +- **AWS CLI v2** (`brew install awscli`) |
| 33 | + |
| 34 | +## Usage |
| 35 | + |
| 36 | +```bash |
| 37 | +# Check your SSO session status |
| 38 | +aws-sso-refresh status |
| 39 | + |
| 40 | +# Run a refresh check manually |
| 41 | +aws-sso-refresh |
| 42 | + |
| 43 | +# Install the background daemon (runs every 10 minutes) |
| 44 | +aws-sso-refresh install |
| 45 | + |
| 46 | +# View the refresh log |
| 47 | +aws-sso-refresh logs |
| 48 | + |
| 49 | +# Remove the background daemon |
| 50 | +aws-sso-refresh uninstall |
| 51 | + |
| 52 | +# Show help |
| 53 | +aws-sso-refresh help |
| 54 | +``` |
| 55 | + |
| 56 | +## How It Works |
| 57 | + |
| 58 | +1. **Parses** your `~/.aws/config` to find all `[sso-session]` blocks |
| 59 | +2. **Checks** the token cache at `~/.aws/sso/cache/` for expiration times |
| 60 | +3. **Refreshes** sessions within 30 minutes of expiring via `aws sso login --sso-session <name>` |
| 61 | +4. **Opens a browser** for re-authentication (auto-approves if already logged in) |
| 62 | + |
| 63 | +### Example Status Output |
| 64 | + |
| 65 | +``` |
| 66 | +AWS SSO Sessions: |
| 67 | +
|
| 68 | + ✓ my-company-sso 5h 23m remaining |
| 69 | + ✓ my-personal-sso 2h 10m remaining |
| 70 | +
|
| 71 | +Daemon: running (PID 1234) |
| 72 | +Interval: every 10 minutes |
| 73 | +Threshold: refresh when < 30m remaining |
| 74 | +``` |
| 75 | + |
| 76 | +## Configuration |
| 77 | + |
| 78 | +### Refresh Threshold |
| 79 | + |
| 80 | +By default, sessions are refreshed when they have less than 30 minutes remaining. Customize this with: |
| 81 | + |
| 82 | +```bash |
| 83 | +export AWS_SSO_REFRESH_THRESHOLD=60 # Refresh when < 60 minutes remaining |
| 84 | +``` |
| 85 | + |
| 86 | +Add this to your `~/.zshrc` or `~/.bashrc` to persist. |
| 87 | + |
| 88 | +### AWS Config |
| 89 | + |
| 90 | +Your `~/.aws/config` should use the modern `sso-session` format: |
| 91 | + |
| 92 | +```ini |
| 93 | +[sso-session my-sso] |
| 94 | +sso_start_url = https://my-company.awsapps.com/start |
| 95 | +sso_region = us-east-1 |
| 96 | +sso_registration_scopes = sso:account:access |
| 97 | + |
| 98 | +[profile dev] |
| 99 | +sso_session = my-sso |
| 100 | +sso_account_id = 123456789012 |
| 101 | +sso_role_name = DeveloperAccess |
| 102 | +region = us-east-1 |
| 103 | + |
| 104 | +[profile prod] |
| 105 | +sso_session = my-sso |
| 106 | +sso_account_id = 123456789012 |
| 107 | +sso_role_name = ReadOnlyAccess |
| 108 | +region = us-east-1 |
| 109 | +``` |
| 110 | + |
| 111 | +With this setup, you only need to authenticate once per `sso-session`, not per profile! |
| 112 | + |
| 113 | +## Files |
| 114 | + |
| 115 | +| Path | Purpose | |
| 116 | +|------|---------| |
| 117 | +| `~/.aws/config` | Your AWS configuration with SSO sessions | |
| 118 | +| `~/.aws/sso/cache/` | AWS SSO token cache | |
| 119 | +| `~/.local/share/aws-sso-refresh/refresh.log` | Daemon log file | |
| 120 | +| `~/Library/LaunchAgents/com.aws.sso-refresh.plist` | macOS LaunchAgent | |
| 121 | + |
| 122 | +## Troubleshooting |
| 123 | + |
| 124 | +### "This script requires bash 4.0 or later" |
| 125 | + |
| 126 | +macOS ships with bash 3.2 (from 2007!) due to licensing. Install modern bash: |
| 127 | + |
| 128 | +```bash |
| 129 | +brew install bash |
| 130 | +``` |
| 131 | + |
| 132 | +### Sessions not refreshing |
| 133 | + |
| 134 | +1. Check the daemon is running: `aws-sso-refresh status` |
| 135 | +2. Check the logs: `aws-sso-refresh logs` |
| 136 | +3. Run manually to test: `aws-sso-refresh` |
| 137 | + |
| 138 | +### Browser doesn't auto-approve |
| 139 | + |
| 140 | +Your Identity Center session may have expired. You'll need to manually approve in the browser once, then subsequent refreshes should be automatic. |
| 141 | + |
| 142 | +## License |
| 143 | + |
| 144 | +MIT - See [LICENSE](LICENSE) for details. |
| 145 | + |
| 146 | +## Contributing |
| 147 | + |
| 148 | +Contributions welcome! Please open an issue or PR on GitHub. |
0 commit comments