Skip to content

Avoid exposing server token in process cmdline in deployment scripts - #1062

Merged
mlim19 merged 2 commits into
masterfrom
fix_token_exposure_in_cmdline
Oct 12, 2026
Merged

mlim19 merged 2 commits into
masterfrom
fix_token_exposure_in_cmdline

Conversation

@mlim19

@mlim19 mlim19 commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Description

The deployment scripts launched gProfiler with --token='<token>' on the command line (via sudo sh -c / setsid / systemd ExecStart). The token then lives in the process argv for the entire run and is readable by any user with /proc access (/proc/<pid>/cmdline is world-readable by default on Linux).

Fix

Pass credentials via environment variables instead of command-line arguments. gProfiler already supports this: its configargparse parser is configured with auto_env_var_prefix="gprofiler_", which derives the env var name from the long option name: --token reads GPROFILER_TOKEN and --service-name reads GPROFILER_SERVICE_NAME (verified against the pinned ConfigArgParse 1.7). Environment lives in /proc/<pid>/environ, which is readable only by the process owner.

  • systemd:
    • create_systemd_service.sh writes the credentials to granulate-gprofiler.env next to the unit, created with mode 0600 (via umask 077), and points the unit's EnvironmentFile= at it. Plain Environment= was avoided because systemctl show displays its values to any local user.
    • Credential flags removed from ExecStart.
    • The ExecStartPre checks use $${VAR} so systemd doesn't substitute the token into bash -c's argv; bash reads it from its environment instead.
    • The user-facing GPROFILER_TOKEN / GPROFILER_SERVICE inputs are unchanged. Values containing a single quote or newline are rejected.
  • Dataproc: export GPROFILER_TOKEN / GPROFILER_SERVICE_NAME (read from instance metadata as before) and launch via sudo --preserve-env=GPROFILER_TOKEN,GPROFILER_SERVICE_NAME, dropping the --token / --service-name flags.
  • EMR: bootstrap actions can only receive arguments, so the script now moves --token / --service-name (both --opt=value and --opt value forms) from its arguments into gProfiler's environment and passes the remaining arguments through. Existing bootstrap Args keep working unchanged. The README notes that bootstrap arguments remain visible to anyone who can view the cluster's EMR configuration (e.g. aws emr list-bootstrap-actions).
  • README: documents the systemd env file and the EMR note above.

How Has This Been Tested?

  • ConfigArgParse 1.7: with GPROFILER_SERVER_TOKEN, GPROFILER_TOKEN and GPROFILER_SERVICE_NAME all set, --token resolved from GPROFILER_TOKEN.
  • systemd:
    • Generated the unit with a fake token and ran it as a user-level systemd service with a stub ExecStart. The stub received the token and service name via its environment, its cmdline was only -cu, systemctl show reported an empty Environment=, and the token didn't appear in the journal.
    • With an empty token in the env file, the ExecStartPre check failed with missing GPROFILER_TOKEN! and the main process didn't start.
    • Confirmed that without $$ escaping, systemd substitutes the token into bash -c's argv.
  • Dataproc: ran the script with stubbed metadata, a sudo stub emulating env_reset plus --preserve-env, and a stub gProfiler. The credentials arrived via the environment, and the cmdline contained only -cu --collect-spark-metrics.
  • EMR: ran the script with stubbed wget / gProfiler for --token=X --service-name=Y ..., --token X --service-name Y ..., and no credentials. Credentials moved to the environment, other arguments passed through, and the no-credentials case behaves as before.
  • shellcheck v0.8.0 (the version used by shell_lint.sh): no warnings on the changed scripts.
  • Not tested on real EMR / Dataproc clusters or with a root systemd install.

Out of scope (follow-ups)

  • gProfiler logs arguments=args.__dict__ (including server_token) at startup to /var/log/gprofiler/gprofiler.log (mode 0644) and includes it in uploaded run_arguments metadata.
  • The Dataproc script's set -x traces the token assignment into the init-action log.
  • The k8s/Helm manifests (--token=$(GPROFILER_TOKEN)), the Ansible playbook, and docker-compose still pass --token on the command line.

🤖 Generated with Claude Code

The deployment scripts passed --token on the command line, making it
visible in /proc/<pid>/cmdline which is world-readable on Linux.

Fix: Use environment variables instead of command-line arguments.
gprofiler's configargparse with auto_env_var_prefix="gprofiler_"
automatically reads GPROFILER_SERVER_TOKEN and GPROFILER_SERVICE_NAME.

Changes:
- dataproc: Export env vars and use sudo --preserve-env
- systemd: Map user vars to internal names, remove args from ExecStart
- emr: Add security note about using env vars

No breaking changes - user-facing variable names are preserved.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 5, 2026 18:06

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.

Copilot review overview

🟡 Changes recommended

Credential configuration errors prevent startup, and command-line token exposure remains in supported deployment paths.

Review effort: Balanced
Findings: 3 High severity · 1 Medium severity

Open (4)
What changed in this PR

Moves deployment credentials toward environment-based configuration to reduce token exposure in process command lines.

Changes:

  • Switches Dataproc launches to exported credentials preserved through sudo.
  • Updates systemd configuration while retaining existing installer inputs.
  • Adds EMR security guidance without changing launch behavior.
File Description
deploy/​systemd/​granulate-gprofiler.service.template Replaces credential arguments with environment settings.
deploy/​systemd/​create_systemd_service.sh Maps existing inputs into the updated template.
deploy/​emr/​gprofiler_action.sh Adds guidance against command-line tokens.
deploy/​dataproc/​gprofiler_initialization_action.sh Exports credentials and preserves them through sudo.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread deploy/emr/gprofiler_action.sh Resolved
Comment thread deploy/emr/gprofiler_action.sh Resolved
- Use GPROFILER_TOKEN: configargparse derives env var names from the long
  option (--token), not from dest, so GPROFILER_SERVER_TOKEN was ignored
  and systemd/Dataproc launches started without a token.
- systemd: load credentials from a mode-0600 EnvironmentFile written by
  the generator instead of Environment= (visible to any user via
  `systemctl show`), and escape the ExecStartPre checks with $$ so systemd
  doesn't substitute the token into bash's argv.
- EMR: bootstrap actions only receive arguments, so move --token and
  --service-name from the arguments into gProfiler's environment before
  launching it. Existing bootstrap Args keep working.
- README: document the systemd env file and the remaining EMR exposure
  via the cluster's bootstrap action configuration.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 9, 2026 04:40

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.

Comment thread deploy/emr/gprofiler_action.sh Resolved
@mlim19
mlim19 merged commit 5337059 into master Oct 12, 2026
76 of 81 checks passed
@mlim19
mlim19 deleted the fix_token_exposure_in_cmdline branch October 12, 2026 19:00
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.

3 participants