Skip to content

fix: security and reliability fixes for kubectl, credentials, account cache and CLI - #42

Open
avroguha wants to merge 22 commits into
fidelity:masterfrom
avroguha:improvements
Open

avroguha wants to merge 22 commits into
fidelity:masterfrom
avroguha:improvements

Conversation

@avroguha

Copy link
Copy Markdown

Summary

A set of security and reliability fixes found by reading the code and then confirmed against a real AWS account and a real EKS cluster. Every fix comes with tests that fail on master and pass here. The commits are small and independent, one topic each, so they can be reviewed (or cherry-picked) one at a time. I am happy to split this into several pull requests if that is easier to review.

No new dependencies. No change to the public library API. The behaviour changes that a user could notice are listed in their own section below.

Security

Commit Problem on master Fix
fix(kubectl): give each kubectl its own KUBECONFIG and a valid kubeconfig KUBECONFIG was set in the process-wide os.environ from worker threads, so with several accounts one account's kubectl could run against another account's cluster. A test with 8 threads reproduces it. The variable is passed in a per-call copy of the environment.
fix(acctload): cache accounts per source in a private directory Every loader cached to one file, <tmp>/awsrun.dat. On a multi-user host another user could create that file (and an .expiry file beside it, which was honoured even with max_age: 0) and so choose the accounts awsrun acts on, or read the cached list. The file was also shared by every URL, parser and by awsrun/azurerun, so one source's accounts could be returned for another. The temporary file used while saving had a fixed name. The default cache file is named after the source and kept in $XDG_CACHE_HOME/awsrun or ~/.cache/awsrun (mode 0700), written through a private temporary file. An explicit cache_path is still honoured.
fix(creds): do not show the configured password in --help The password from the config file or PASSWORD was the argparse default of --saml-password, --oauth2-password and --ad-password, so awsrun --help printed it as (default: ...). The fallback is resolved when the plug-in is instantiated. Precedence is unchanged: flag, config, environment, prompt.
fix(plugmgr): mask passwords, secrets and tokens in the INFO log The parsed arguments, including password flags, were logged verbatim with --log-level INFO. Values of sensitive keys and flags are masked.
fix(console): never send long-term credentials to the federation endpoint IAM user keys have no session token, which was not detected. The access key and secret key were sent to the sign-in endpoint in a query string and then echoed in the error message. Observed on a real account. The session token is checked first and nothing is sent.
fix(acctload) / fix(session): add a timeout ... HTTP requests to account-list servers and identity providers had no timeout, so an unresponsive server hung the CLI indefinitely. A connect/read timeout of 10/60 seconds.

Bugs

Commit Problem on master
fix(kubectl): give each kubectl ... With long-term credentials the generated kubeconfig contained AWS_SESSION_TOKEN: None, and kubectl answered "You must be logged in to the server". Confirmed on a real EKS cluster with IAM user keys. The kubeconfig is now built with yaml.safe_dump, which also quotes values correctly.
fix(kubectl): make --awsrun-output-dir work The directory is a string from the CLI or config and was joined with /: unsupported operand type(s) for /: 'str' and 'str'. Found while testing on EKS.
fix(kubectl): read --output default from its own config key --output took its default from the namespace config key, so a namespace list in the config raised a TypeError and an output key was ignored. The error for a prohibited flag also printed a literal --{arg}.
fix(cmdmgr): list commands even when an optional one exits on import dx_status, dx_maint and last call sys.exit() when their extras are missing. SystemExit is not an Exception, so with a default awsrun[aws] install the command list was never displayed and an unknown command reported another command's install hint. Running such a command by name still prints its hint.
fix(accts): accept the documented 'ntlm' auth type in URLLoader The config type check listed ntml, so auth: ntlm was rejected and the misspelling passed validation only to fail with a KeyError. --loader-auth and --loader-parser values are now validated too.
fix(aws,az): keep dotted account names in --awsrun-output-dir file names Path.with_suffix() replaced everything after the last dot, so accounts such as team.prod and team.dev wrote to the same file.
fix(plugmgr): pass default by keyword when instantiate parses implicitly instantiate() without a prior parse_args() failed with "cannot import 'None'". Affects library users.
fix(plugmgr): report a missing dependency instead of 'does not contain' Without the aws extra the error was "module 'awsrun.plugins.creds' does not contain 'aws.Default'" rather than a missing boto3.
fix(acctload): run the resolved Azure CLI path in the AzureCLI loader On Windows az is az.cmd, which subprocess cannot start by its bare name. The az command already used the resolved path.
fix(cli): allow running as 'python -m awsrun.cli' Failed with "unknown variant: cli.py".

Command line behaviour

  • fix(cli): explain --force when stdin is closed and exit cleanly on Ctrl-C
    • With no terminal, the confirmation prompt failed with "EOF when reading a line". It now says to use --force.
    • Ctrl-C printed a stack trace. It now prints "Interrupted" and exits with status 130 (AWSRUN_TRACE still shows the trace).
    • An interrupted run kept processing every account still waiting for a worker before it could exit. Those are now cancelled.
  • feat(console): make the sign-in URL issuer configurable adds --issuer and the issuer config key. The default is unchanged.

Documentation, build and tests

  • docs: correct examples and references that do not match the code (import path of CredsViaProfile, LoadBalancerArn, a copied config example, __all__, the formatter named in CONTRIBUTING).
  • build: correct the license file name and declare beautifulsoup4 (pyproject.toml pointed at LICENSE while the file is LICENSE.md; bs4 is a placeholder for beautifulsoup4; classifiers for the Python versions CI already tests).
  • test(cache): make the mtime in test_caching independent of the time zone: this existing test failed on any machine whose time zone is ahead of UTC.
  • test(acctload): follow the cache to its per-source location: updates the integration tests for the new cache location, and builds file URLs with Path.as_uri() so they also pass on Windows.

Behaviour changes to be aware of

  1. Account cache location. The default moves from <tmp>/awsrun.dat to a per-source file under ~/.cache/awsrun. An existing cache is simply not reused once. cache_path is unaffected.
  2. kubeconfig format. The file written to ~/.kube/awsrun-* has the same content but is produced by a YAML serializer, and omits AWS_SESSION_TOKEN when there is no token.
  3. Ctrl-C exits with 130 instead of a stack trace.
  4. --help shows (default: None) for the password flags.
  5. Output file names only differ for account names that contain a dot, where they were previously wrong.
  6. Debug logging masks sensitive values.

Testing

  • Unit and integration tests: 346 before, 504 now. Each new test was run against master first to confirm that it fails there.
  • The CI recipe (pip install .[aws,azure,dev] ruff, ruff check --extend-select I ., ruff format --check ., pytest) was run on Linux for Python 3.9, 3.10, 3.11, 3.12 and 3.13: all 504 tests pass on each. The unit tests were also repeated 25 times on Windows and 10 times on Linux to check for flaky tests.
  • Windows (Python 3.12): the suite passes; three POSIX-only tests are skipped there.
  • Real AWS account with a read-only IAM user: every bundled command was run with both master and this branch. Output and exit codes match, apart from the console fix above.
  • Real EKS cluster (Kubernetes 1.37) on Windows and Linux: kubectl list clusters, get namespaces and pods, several namespaces, JSON/YAML/text annotation, output directory, a refused write and prohibited flags.

Not covered: SAML and OAuth2 sign-in against a real identity provider, cross-account roles, several accounts in parallel (the KUBECONFIG race is covered by a threaded unit test only), and the interactive last -i screen.

Not included

Two things I noticed and left alone because they change behaviour people may rely on. I can open issues for them if useful:

  • awsrun exits 0 even when every account fails.
  • Core flags such as --include, --exclude and --force are consumed anywhere on the line, so they cannot be passed through to a wrapped tool (aws s3 sync --exclude ...).

🤖 Generated with Claude Code

avroguha and others added 22 commits November 22, 2026 11:15
The --output flag took its default from the 'namespace' config key, so a
namespace list in the user config raised a TypeError and an 'output' key
was never honoured. Also add the missing f-string prefix so the error for
a prohibited kubectl flag names the flag.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Avro Guha <avro.guha@gmail.com>
PluginManager.instantiate() called parse_args(*keys, default), which made
the default plug-in path part of the config key path. Calling instantiate()
without a prior parse_args() therefore failed with "cannot import 'None'".

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Avro Guha <avro.guha@gmail.com>
time.mktime() treats the frozen UTC time tuple as local time, so the test
failed on any machine whose time zone is ahead of UTC.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Avro Guha <avro.guha@gmail.com>
On Windows 'az' is 'az.cmd', which subprocess cannot start by its bare
name. Use the path returned by shutil.which(), as the az command does.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Avro Guha <avro.guha@gmail.com>
load_dotted_object() swallowed every ModuleNotFoundError, so running
without the 'aws' extra reported that awsrun.plugins.creds did not contain
'aws.Default' rather than that boto3 was missing.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Avro Guha <avro.guha@gmail.com>
The config type check listed 'ntml', so 'auth: ntlm' was rejected and the
misspelling passed validation only to fail with a KeyError. Also validate
--loader-auth and --loader-parser values on the command line.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Avro Guha <avro.guha@gmail.com>
Add --issuer and the 'issuer' config key. The default is unchanged. Also
correct the documented type of the 'region' option.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Avro Guha <avro.guha@gmail.com>
The variant was derived from the raw program name, so running the module
directly failed with 'unknown variant: cli.py'.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Avro Guha <avro.guha@gmail.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Avro Guha <avro.guha@gmail.com>
…nfig

Commands run concurrently in worker threads, but KUBECONFIG was set in the
process-wide os.environ before each subprocess started, so one account's
kubectl could run against another account's cluster. Pass the variable in
a per-call copy of the environment instead.

The kubeconfig is now generated with yaml.safe_dump: long-term credentials
no longer get AWS_SESSION_TOKEN set to the string 'None', values are always
quoted correctly, and slashes in the account name are escaped in the file
name. Output file names no longer lose everything after a dot in the
account name.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Avro Guha <avro.guha@gmail.com>
Path.with_suffix() replaced everything after the last dot, so accounts
such as 'team.prod' and 'team.dev' wrote to the same file.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Avro Guha <avro.guha@gmail.com>
The parsed arguments, including --saml-password and friends, were logged
verbatim when running with --log-level INFO or DEBUG.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Avro Guha <avro.guha@gmail.com>
The password from the config file or the PASSWORD environment variable was
used as the argparse default of --saml-password, --oauth2-password and
--ad-password, so 'awsrun --help' printed it as '(default: ...)'. The
fallback is now resolved when the plug-in is instantiated; precedence is
unchanged (flag, config, environment, prompt).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Avro Guha <avro.guha@gmail.com>
Every loader cached its accounts in one file, <tmp>/awsrun.dat:

- On a multi-user system any other user could create that file (and an
  .expiry file next to it, honoured even with caching disabled) to choose
  the accounts awsrun selects, or read the cached account list.
- The file was shared by every URL, parser and by awsrun and azurerun, so
  within max_age one source's accounts were returned for another.
- The temporary file used while saving had a fixed, predictable name.

The default cache file is now named after the source and kept in the
user's cache directory ($XDG_CACHE_HOME or ~/.cache, under awsrun/, mode
0700), and is written through a private temporary file. An explicit
cache_path is still honoured. HTTP requests made by the loaders now have a
timeout so an unresponsive server cannot hang the CLI.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Avro Guha <avro.guha@gmail.com>
An identity provider that stopped responding would hang the CLI forever.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Avro Guha <avro.guha@gmail.com>
The bundled dx_status, dx_maint and last commands call sys.exit() when
their optional dependencies are missing. SystemExit is not an Exception,
so with a default install the list of commands was never displayed and an
unknown command reported another command's install hint. Running such a
command by name still prints its hint.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Avro Guha <avro.guha@gmail.com>
…rl-C

The confirmation prompt failed with 'EOF when reading a line' when there
was nobody to answer, and Ctrl-C printed a stack trace. An interrupted run
also kept processing every account still waiting for a worker before it
could exit; those are now cancelled.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Avro Guha <avro.guha@gmail.com>
…oint

IAM user keys have no session token, which was not detected, so the access
key and secret key were sent to the sign-in service before it rejected
them.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Avro Guha <avro.guha@gmail.com>
pyproject.toml referred to LICENSE while the file is LICENSE.md, and
depended on the bs4 placeholder rather than beautifulsoup4 itself. Also
list the Python versions that CI already tests.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Avro Guha <avro.guha@gmail.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Avro Guha <avro.guha@gmail.com>
The directory arrives as a string from the command line or the config
file, but was joined to the file name with the '/' operator, so saving the
output failed with "unsupported operand type(s) for /: 'str' and 'str'".
It is converted to a path, and created if it does not exist, as the aws
command does.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Avro Guha <avro.guha@gmail.com>
The integration tests expected the cache to be <tmp>/awsrun.dat. They now
look for the file of the loader and URL under test, in a cache directory
that belongs to the test. The file URLs are built with Path.as_uri() so
these tests also pass on Windows.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Avro Guha <avro.guha@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant