Skip to content

fix(aks-diagnostic): move SAS token off argv to an environment variable - #9883

Draft
Calvin S. (calvin197) wants to merge 2 commits into
mainfrom
security/aks-diagnostic-sas-token-env
Draft

Calvin S. (calvin197) wants to merge 2 commits into
mainfrom
security/aks-diagnostic-sas-token-env

Conversation

@calvin197

Copy link
Copy Markdown
Contributor

What this PR does / why we need it:

The SAS token used to authenticate blob uploads in aks-diagnostic.py was a required positional CLI argument, exposing it to any local process able to read this process's command line (e.g. via /proc/<pid>/cmdline), rather than only to whoever invoked the script (ADO finding 37518923).

Add support for passing it via the AKS_DIAGNOSTIC_SAS_TOKEN environment variable (preferred, takes precedence when both are set), which does not appear in argv. The positional CLI argument is kept as a deprecated fallback for backward compatibility with any existing external callers — this script is baked onto the VHD standalone (no in-repo caller invokes it; vhdbuilder/packer/packer_source.sh only copies the file), so there is no way to enumerate or update every caller in this change. Using it now prints a warning to stderr pointing at the env var instead.

To make the new optional sas_token positional unambiguous against the pre-existing optional file_path positional, file_path is now the --file-path flag rather than a second positional argument (two optional trailing positionals cannot otherwise be told apart by argparse).

Also removed the unused handleArgs() function: dead code (zero call sites, confirmed via repo-wide search) that carried the exact same argv-based SAS token exposure as a legacy positional-args fallback.

No change to the actual upload behavior/HTTP logic. Validated: python3 -m py_compile and pyflakes both pass with no warnings/errors. Manually exercised all argument-resolution paths — missing token (both env var and positional absent) now errors with a clear message; env-var-only invocation succeeds and the token does not appear in the process's argv (checked via ps/cmdline); positional-only invocation still works and emits the deprecation warning; when both are set, the env var wins and no warning is printed. --help output reviewed for correctness.

Which issue(s) this PR fixes:
Fixes #

ADO work items (msazure/CloudNativeCompute): 37518923 (parent: 37518878)

The SAS token used to authenticate blob uploads was a required positional
CLI argument, exposing it to any local process able to read this
process's command line (e.g. via /proc/<pid>/cmdline), rather than only
to whoever invoked the script.

Add support for passing it via the AKS_DIAGNOSTIC_SAS_TOKEN environment
variable (preferred, takes precedence when both are set), which does not
appear in argv. The positional CLI argument is kept as a deprecated
fallback for backward compatibility with any existing external callers -
this script is baked onto the VHD standalone (no in-repo caller invokes
it; vhdbuilder/packer/packer_source.sh only copies the file), so there is
no way to enumerate or update every caller in this change. Using it now
prints a warning to stderr pointing at the env var instead.

To make the new optional sas_token positional unambiguous against the
pre-existing optional file_path positional, file_path is now the
--file-path flag rather than a second positional argument (two optional
trailing positionals cannot otherwise be told apart by argparse).

Also removed the unused handleArgs() function: dead code (zero call
sites, confirmed via repo-wide search) that carried the exact same
argv-based SAS token exposure as a legacy positional-args fallback.

No change to the actual upload behavior/HTTP logic.

Verified locally:
  - python3 -m py_compile and pyflakes both pass with no warnings/errors.
  - Manually exercised all argument-resolution paths: missing token (both
    env var and positional absent) now errors with a clear message;
    env-var-only invocation succeeds and the token does not appear in the
    process's argv (checked via ps/cmdline); positional-only invocation
    still works and emits the deprecation warning; when both are set, the
    env var wins and no warning is printed. --help output reviewed for
    correctness.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 350c2577-7031-4d0d-adc1-e99d053fc981
@calvin197
Calvin S. (calvin197) requested review from a team as code owners October 23, 2026 07:50
Copilot AI balanced review requested due to automatic review settings October 23, 2026 07:50
@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Windows Unit Test Results

  3 files   18 suites   1m 16s ⏱️
563 tests 563 ✅ 0 💤 0 ❌
585 runs  585 ✅ 0 💤 0 ❌

Results for commit e8a35c0.

♻️ This comment has been updated with latest results.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Previously supported invocations with a positional custom file path now fail argument parsing, breaking compatibility for external callers.

1 open issue
What changed in this PR

This PR adds an environment-variable option for diagnostic upload credentials, reducing command-line exposure of SAS tokens in the standalone VHD script.

Changes:

  • Prefer AKS_DIAGNOSTIC_SAS_TOKEN, retaining a deprecated positional token fallback.
  • Replace the positional custom file path with --file-path and update usage documentation.
  • Remove the unused handleArgs() function without changing upload HTTP logic.
File Description
parts/​linux/​cloud-init/​artifacts/​aks-diagnostic.py Updates credential resolution, argument parsing, warnings, and usage examples.

Review effort: Balanced

Comment thread parts/linux/cloud-init/artifacts/aks-diagnostic.py Outdated
The previous commit converted file_path from a positional argument to
a --file-path flag to resolve an argparse ambiguity with the new
optional sas_token positional. That broke the pre-existing 4-argument
invocation style (container url sas_token file_path), which is a real
regression for any external caller still using it.

Fix properly: capture the trailing sas_token/file_path positionals
together via nargs='*' and resolve them by count after parsing, so
every pre-existing invocation shape - (), (token,), (token, file_path)
- behaves exactly as before. --file-path remains available as the
preferred way to set a custom path when the token itself comes from
the AKS_DIAGNOSTIC_SAS_TOKEN env var (no token positional at all).

Verified locally: re-ran all argument-resolution paths, specifically
including the previously-broken old 4-positional-argument form
(container url token file_path) with a real file on disk, confirming
it resolves file_path correctly and reaches the upload call. Also
re-verified the new env-var + --file-path combination, the "too many
arguments" error path, and that the SAS token still does not appear in
the process's argv when sourced from the environment variable.
python3 -m py_compile and pyflakes both pass with no warnings.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 350c2577-7031-4d0d-adc1-e99d053fc981
Copilot AI balanced review requested due to automatic review settings October 23, 2026 08:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The scoped changes preserve legacy invocation forms, and focused verification found no blocking issues with token precedence or argument handling.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@github-actions

Copy link
Copy Markdown
Contributor

Code Coverage

Package Line Rate Health
test 0% ❌
windows 27% ❌
staging_cse_windows Package 1 65% ➖
debug 0% ❌
provisioningscripts 2% ❌
parts_windows Package 1 73% ➖
Summary 38% (1652 / 6834) ❌

@calvin197
Calvin S. (calvin197) marked this pull request as draft October 23, 2026 08:04
@github-actions

Copy link
Copy Markdown
Contributor

Code Coverage

Package Line Rate Health
shellspec spec 28% ❌
Summary 28% (4591 / 16082) ❌

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.

2 participants