Skip to content

scripts: reject long-lived AKIA keys in env (credential guard) - #574

Merged
neuromechanist merged 2 commits into
devfrom
feature/scripts-credential-guard
May 22, 2026
Merged

neuromechanist merged 2 commits into
devfrom
feature/scripts-credential-guard

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Summary

Implements principles 5 and 6 from PR #571 (`docs/operations/access-policies.md`) at the script layer. Admin scripts in `scripts/` now refuse to run when a long-lived AKIA* access key is set in the process environment — a real risk on shared infrastructure (e.g., the hallu clone of nemar-cli lives under `/data/qumulo/`, a multi-tenant SDSC mount).

What changed

New: `scripts/lib/aws-creds-guard.sh` — small shared library exporting `nemar_guard_aws_credentials`:

  • Rejects `AKIA*` keys in env (long-lived; must live in `~/.aws/credentials` mode 0600 instead).
  • Warns on `ASIA*` keys in env (short-lived STS sessions; acceptable but `~/.aws/credentials` is preferred).
  • Calls `aws sts get-caller-identity` to verify the resolved identity, fails fast if no credentials are configured.
  • Logs the authenticated ARN to stderr for audit.

Updated scripts (sourced the guard, removed legacy env-var-required checks, updated docstrings to point at the new credential flow):

  • `scripts/nemar-restore-dataset.sh`
  • `scripts/migrate-s3-keys.sh`
  • `scripts/migrate-s3-remote.sh`
  • `scripts/migrate-s3-structure.sh`
  • `scripts/cleanup-test-datasets.sh`

Why this matters

Even though none of these scripts currently leak credentials directly, the previous pattern (`export AWS_ACCESS_KEY_ID=...`) means:

  • The key shows up in `/proc//environ` and `ps eww` for any child process running on the same machine.
  • It persists in shell history (`/.bash_history`, `/.zsh_history`).
  • It propagates to debug logs that capture environment.
  • The hallu clone of nemar-cli sits on `/data/qumulo/openneuro/nemar-cli/` — a Qumulo-mounted directory readable from other SDSC infrastructure. A future careless edit that ever wrote a key into a script there would broadcast that key across the entire mount.

By rejecting AKIA in env at the start of every admin script, we close the most common foot-gun: an admin sourcing a `credentials.sh` file or running `export` and then forgetting to clean up.

Test plan

  • `bash -n` syntax check on all 6 files
  • Guard with AKIA env var: rejected with full error message
  • Guard with ASIA env var: warns but proceeds; logged identity matches the federated user
  • No backward-incompatible interface change — the scripts still need credentials; they just can't take them via long-lived env vars

Companion to

  • PR docs: access policies + 4 scoped IAM users #571 — `docs/operations/access-policies.md` (principles 5 + 6 and the IAM user catalog)
  • The `nemar-hallu-readonly` deployment to `/home/yahya/.aws/credentials` on hallu (out of git scope; done via scp 2026-05-22)

Follow-ups

  • Pull this PR on hallu (`cd /data/qumulo/openneuro/nemar-cli && git pull`) so the cron picks up the guard.
  • Consider moving the hallu nemar-cli clone out of `/data/qumulo/` to `/home/yahya/.local/share/nemar-cli/` for stronger script-tampering protection. Separate PR.

@github-actions

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR574

Version: 0.8.27-PR574.42a5ba2
Commit: 42a5ba2


🤖 Updated on each push.

@neuromechanist

Copy link
Copy Markdown
Contributor Author

Review (pr-review-toolkit:code-reviewer)

Critical: none.

Important — both fixed in 42a5ba2:

  1. Dry-run path in `migrate-s3-structure.sh` + `cleanup-test-datasets.sh` was blocked by the live `aws sts` probe; previewing on a fresh laptop would hard-fail. Now wrapped in `if ! $DRY_RUN; then ... fi`.
  2. Unknown-prefix branch in `aws-creds-guard.sh` printed "unrecognized prefix" with no remediation. Now prints the same actionable AKIA*/ASIA*/SSO guidance as the AKIA branch.

Strengths confirmed:

  • BASH_SOURCE[0] resolves correctly inside the `validate_environment()` function in `nemar-restore-dataset.sh`
  • bash 3.2 (macOS) + bash 5 (Linux hallu) portability OK
  • shellcheck clean apart from one pre-existing SC2155 unrelated to this PR
  • Exit code 2 propagates correctly via `|| exit $?` and explicit `return 2`

@neuromechanist
neuromechanist merged commit e039039 into dev May 22, 2026
9 checks passed
@neuromechanist
neuromechanist deleted the feature/scripts-credential-guard branch May 22, 2026 19:03

This branch was previously deployed

1 inactive deployment
npm — 42a5ba22 Deployed May 22, 2026 by neuromechanist via publish #1054
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