|
| 1 | +# aws_login_headless.sh |
| 2 | + |
| 3 | +Fully automated AWS SSO login using headless browser automation. No manual browser interaction required. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +1. AWS CLI v2: |
| 8 | + ```bash |
| 9 | + aws --version # Should be 2.x |
| 10 | + ``` |
| 11 | + |
| 12 | +2. uv (Python package manager - handles Python and Playwright automatically): |
| 13 | + ```bash |
| 14 | + # macOS/Linux |
| 15 | + curl -LsSf https://astral.sh/uv/install.sh | sh |
| 16 | + |
| 17 | + # Or via Homebrew |
| 18 | + brew install uv |
| 19 | + |
| 20 | + # Verify installation |
| 21 | + uv --version |
| 22 | + ``` |
| 23 | + |
| 24 | +3. Optional - 1Password CLI for automatic password retrieval: |
| 25 | + ```bash |
| 26 | + op --version |
| 27 | + ``` |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +### Interactive Mode |
| 32 | + |
| 33 | +Prompts for password (uses `default` profile): |
| 34 | + |
| 35 | +```bash |
| 36 | +./aws_login_headless.sh |
| 37 | +``` |
| 38 | + |
| 39 | +Specify a different AWS profile: |
| 40 | + |
| 41 | +```bash |
| 42 | +./aws_login_headless.sh --profile production |
| 43 | +``` |
| 44 | + |
| 45 | +### 1Password Integration |
| 46 | + |
| 47 | +Retrieve password from 1Password using item name: |
| 48 | + |
| 49 | +```bash |
| 50 | +./aws_login_headless.sh --op-item TACO42BURRITO88SALSA99 |
| 51 | +``` |
| 52 | + |
| 53 | +Or using full secret reference: |
| 54 | + |
| 55 | +```bash |
| 56 | +./aws_login_headless.sh --op-item "op://Employee/AWS/password" |
| 57 | +``` |
| 58 | + |
| 59 | +Use specific 1Password account: |
| 60 | + |
| 61 | +```bash |
| 62 | +./aws_login_headless.sh --op-item TACO42BURRITO88SALSA99 --op-account work |
| 63 | +``` |
| 64 | + |
| 65 | +Combine with custom AWS profile: |
| 66 | + |
| 67 | +```bash |
| 68 | +./aws_login_headless.sh --profile staging --op-item "AWS" --op-account my.1password.com |
| 69 | +``` |
| 70 | + |
| 71 | +### Additional Options |
| 72 | + |
| 73 | +Pre-fill username (if required by your IdP): |
| 74 | + |
| 75 | +```bash |
| 76 | +./aws_login_headless.sh --username joe@example.com --op-item xyz123 |
| 77 | +``` |
| 78 | + |
| 79 | +Debug with visible browser: |
| 80 | + |
| 81 | +```bash |
| 82 | +./aws_login_headless.sh --no-headless |
| 83 | +``` |
| 84 | + |
| 85 | +## Example Output |
| 86 | + |
| 87 | +``` |
| 88 | +% ./aws_login_headless.sh --op-item NACHOS77CHIPS42QUESO88 |
| 89 | +Retrieving SSO password from 1Password... |
| 90 | +Initiating AWS SSO login for profile: default |
| 91 | +Verification URL: https://device.sso.us-east-1.amazonaws.com/?user_code=WXYZ-ABCD |
| 92 | +Launching headless browser automation... |
| 93 | +Navigating to: https://device.sso.us-east-1.amazonaws.com/?user_code=WXYZ-ABCD |
| 94 | +Filling password |
| 95 | +Submitting login form |
| 96 | +Waiting for authentication to complete... |
| 97 | +✓ Authentication successful! |
| 98 | +
|
| 99 | +✓ Successfully authenticated to AWS SSO |
| 100 | +
|
| 101 | +AWS profile 'default' is now logged in. |
| 102 | +``` |
| 103 | + |
| 104 | +## How It Works |
| 105 | + |
| 106 | +1. Script starts `aws sso login --no-browser` and captures the verification URL |
| 107 | +2. Optionally retrieves SSO password from 1Password |
| 108 | +3. Uses `uvx` to run Python script with Playwright (automatically manages dependencies) |
| 109 | +4. Launches headless Chrome browser using Playwright |
| 110 | +5. Automates form filling and submission |
| 111 | +6. Waits for authentication confirmation |
| 112 | +7. Exits when login completes |
| 113 | + |
| 114 | +## Troubleshooting |
| 115 | + |
| 116 | +### uv Not Found |
| 117 | + |
| 118 | +``` |
| 119 | +Error: Missing required dependencies: uv |
| 120 | +``` |
| 121 | + |
| 122 | +**Solution**: Install uv using the instructions in Prerequisites section |
| 123 | + |
| 124 | +### Browser Installation Failed |
| 125 | + |
| 126 | +If first-time browser installation fails: |
| 127 | + |
| 128 | +```bash |
| 129 | +uvx --from playwright playwright install chromium |
| 130 | +``` |
| 131 | + |
| 132 | +### Browser State Issues |
| 133 | + |
| 134 | +If authentication fails repeatedly, clear browser state: |
| 135 | + |
| 136 | +```bash |
| 137 | +rm -rf ~/.aws_login_browser_data |
| 138 | +``` |
| 139 | + |
| 140 | +### Debugging Login Flow |
| 141 | + |
| 142 | +Run with visible browser to see what's happening: |
| 143 | + |
| 144 | +```bash |
| 145 | +./aws_login_headless.sh --no-headless |
| 146 | +``` |
| 147 | + |
| 148 | +### Already Logged In |
| 149 | + |
| 150 | +If you're already authenticated: |
| 151 | + |
| 152 | +``` |
| 153 | +✓ Already logged in to AWS SSO |
| 154 | +``` |
| 155 | + |
| 156 | +Script exits early - no action needed. |
| 157 | + |
| 158 | +## Security Notes |
| 159 | + |
| 160 | +- Passwords are never stored or logged |
| 161 | +- Browser session data stored in `~/.aws_login_browser_data` for persistent login sessions |
| 162 | +- When using 1Password, credentials are retrieved securely via `op` CLI |
| 163 | +- Headless browser runs in isolated user data directory |
| 164 | +- Python dependencies managed by uv in isolated environments |
| 165 | + |
| 166 | +## Notes |
| 167 | + |
| 168 | +- Supports any AWS profile via `--profile` flag (defaults to `default`) |
| 169 | +- Requires AWS CLI v2 configured with SSO |
| 170 | +- Uses uv to automatically manage Python and Playwright dependencies |
| 171 | +- Browser automation selectors work with standard AWS SSO login pages |
| 172 | +- Optimized for Okta SSO with fast polling and short timeouts |
| 173 | +- Custom IdP login pages may require selector adjustments in `aws_login_headless_playwright.py` |
| 174 | +- First run automatically installs Chromium browser (one-time setup) |
0 commit comments