Skip to content

Fix -key suffix stripping in account_keys.py and add dry-run input to PolicyEnforcer - #41591

Merged
derrickaw merged 3 commits into
masterfrom
codescan/py-account-keys-logging
Nov 25, 2026
Merged

derrickaw merged 3 commits into
masterfrom
codescan/py-account-keys-logging

Conversation

@derrickaw

@derrickaw derrickaw commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Note: this PR was auto-generated by tooling. Before moving it out of draft, the author has done a full review of the PR, specifically evaluating it on the following criteria:

  • The PR remediates confirmed, live Code Scanning finding(s) (CodeQL / zizmor) on master without altering intended public API or workflow semantics
  • The fix is minimal, idiomatic, and avoids suppressing alerts or masking underlying defects
  • Relevant unit tests, linters, and CI checks have been verified

If you have concerns with the PR or with the underlying methodology used to generate this, please comment below - they will be addressed by the author not the tooling :)

Summary

  • Dismissed false-positive CodeQL py/clear-text-logging-sensitive-data alerts (#1835–#1840): CodeQL's SensitiveDataHeuristics flagged secret_client.list_secrets() resource names because the variable/method names contain "secret", even though GCP Secret Manager resource IDs (<service-account>-key) are public metadata used in Beam's audit drift reports rather than secret key payloads. Restored all original per-secret logging in infra/enforcement/account_keys.py.
  • Fix -key suffix stripping bug in infra/enforcement/account_keys.py: Replace character-set secret.strip("-key") with suffix-accurate secret.removesuffix("-key") in generate_compliance() so service account IDs starting or ending with k, e, or y are not corrupted.
  • Add dry-run workflow_dispatch action input & scope permissions in .github/workflows/beam_Infrastructure_PolicyEnforcer.yml: Allow manual workflow_dispatch runs to default to action=print (exercising live GCP IAM and Secret Manager checks and printing the audit report to workflow logs without updating GitHub issues or emailing dev@beam.apache.org, while keeping announce on the weekly Monday schedule), and move issues: write and id-token: write to job level (zizmor excessive-permissions).

Verification

  • Live Infrastructure Policy Enforcer (action=print) dry-run on branch codescan/py-account-keys-logging: https://github.com/apache/beam/actions/runs/45252411747 (success across both Run IAM Policy Enforcement and Run Account Keys Policy Enforcement with zero issue/email side effects)
  • zizmor --config .github/zizmor.yml --persona=pedantic .github/workflows/beam_Infrastructure_PolicyEnforcer.yml (0 findings)
  • ./gradlew :beam-test-gha:preCommit and python3 -m py_compile infra/enforcement/account_keys.py

@derrickaw

Copy link
Copy Markdown
Collaborator Author

/gemini review

@github-actions github-actions Bot added the size/s S: 10-49 lines changed label Nov 23, 2026
Comment thread infra/enforcement/account_keys.py Fixed
@derrickaw

Copy link
Copy Markdown
Collaborator Author

/gemini review

@github-actions github-actions Bot 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.

📋 Review Summary

This PR resolves six CodeQL py/clear-text-logging-sensitive-data alerts in infra/enforcement/account_keys.py by replacing logged secret identifiers and full IAM policy objects with non-sensitive service account identifiers and aggregate counts. It also fixes a subtle string-stripping bug by replacing secret.strip("-key") with secret.removesuffix("-key").

🔍 General Feedback

  • The refactoring cleanly eliminates CodeQL taint flows and variable-name heuristic triggers while preserving all compliance checking and generation behavior.
  • Reordering the permission check loop before appending undeclared accounts in generate_compliance() avoids unnecessary skipped iterations on newly added accounts with empty authorized_users.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

@derrickaw
derrickaw marked this pull request as ready for review November 23, 2026 18:19
@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @damccorm added as fallback since no labels match configuration
Backup reviewer: tvalentyn (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@damccorm

Copy link
Copy Markdown
Contributor

Could you please run https://github.com/apache/beam/actions/workflows/beam_Infrastructure_PolicyEnforcer.yml and share the results? FWIW, this is a cleaner way to make these PRs verifiable than a python script to run

verified_keys.append(key_id)
return verified_keys
except Exception as e:
self.logger.error(f"Failed to retrieve verified keys from Secret Manager for secret '{secret_name}': {e}")

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.

Why does logging the secret name matter? I don't think this is protected in Beam

@damccorm

Copy link
Copy Markdown
Contributor

High level feedback on a few of these PRs: I'm supportive of driving down these warnings, but it is unclear at this point what the work to value ratio is - along those lines, I have a few questions:

  1. Approximately how many PRs will it take to resolve all warnings?
  2. Are there broad things we can do to scope the warnings down (e.g. exclude all test files)? I think this should be doable via config
  3. What value do we expect to get out of these? (e.g. has it found instances of real impact)

I've started including a doc with more details on approach/high level impact in similar large scale changes (e.g. see the description in #41272), and would recommend doing something similar. You could also inline it if the answers are simple.

@github-actions github-actions Bot 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.

📋 Review Summary

This PR updates infra/enforcement/account_keys.py to remediate CodeQL py/clear-text-logging-sensitive-data alerts by replacing secret names and full IAM policies in log messages with service account identifiers and summary counts, and fixes a suffix-stripping bug (strip("-key") -> removesuffix("-key")).

🔍 General Feedback

  • Replacing individual undeclared secret warnings in check_compliance() with a single aggregate count (undeclared_count) removes actionable diagnostic context from compliance reports and announcements. Since secret resource names here follow the <account_id>-key convention (which is public metadata in the repo's keys YAML file rather than secret payload material), consider whether suppressing the false-positive CodeQL heuristic or logging the derived account_id is preferable to losing the identifier in drift alerts.
  • Good catch replacing secret.strip("-key") with secret.removesuffix("-key") when deriving account_id.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

Comment thread infra/enforcement/account_keys.py Resolved
@damccorm

damccorm commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Practically, why this matters to me as a reviewer:

If the value is high and we only need a small number of PRs to realize the value, then I won't care about things like #41591 (comment) (we can just make the switch to make the scanner happy).

If the value is low and we need many PRs, then the math is different and I'd push back/look for more systemic changes we can make at a tooling level.

I'm still calibrating how to handle all of this (so treat my PRs (and feedback) as an experimental approach to learn from, not my recommendation on how to handle everything moving forward 😅 )

@derrickaw derrickaw changed the title Avoid logging secret identifiers and IAM policies in account_keys.py Fix -key suffix stripping in account_keys.py and add dry-run input to PolicyEnforcer Nov 23, 2026
@derrickaw

Copy link
Copy Markdown
Collaborator Author

High level feedback on a few of these PRs: I'm supportive of driving down these warnings, but it is unclear at this point what the work to value ratio is - along those lines, I have a few questions:

  1. Approximately how many PRs will it take to resolve all warnings?

Approximately ~40 more merged PRs needed to cleanup everything to this point.

  1. Are there broad things we can do to scope the warnings down (e.g. exclude all test files)? I think this should be doable via config

I opened another PR for zizmor to get rid of the Note warnings (~1200) since they are not applicable to this repo - #41637

  1. What value do we expect to get out of these? (e.g. has it found instances of real impact)

I've started including a doc with more details on approach/high level impact in similar large scale changes (e.g. see the description in #41272), and would recommend doing something similar. You could also inline it if the answers are simple.

Some current or near future impact with this cleanup:

  1. Archive path-traversal (ZipSlip) in Java ZipFiles.unzipFile (Enforce canonical path prefix check in ZipFiles.unzipFile #41497) and Go xlang expansionx (Enforce IsLocal and prefix check in expansionx validatePath #41589).
  2. Shell / template injection (High) in shared CI actions (build-push-docker-action Pass inputs via env in build-push-docker-action #41515, test-arguments-action Pass inputs and outputs via env in test-arguments-action #41592, setup-k8s-access Pass inputs and outputs via env in setup-k8s-access #41605) and pr-bot exec (Pass git argument arrays to @actions/exec in pr-bot updateReviewers #41524).
  3. Least-privilege GITHUB_TOKEN scoping on CI workflows (Harden permissions and setup-node caching in typescript_tests.yml #41499, Scope write permissions to job in update_python_dependencies.yml #41516, Scope write permissions to jobs in cut_release_branch.yml #41522, Add read-only permissions and self-repo actions in python_tests.yml #41526; Add read-only permissions and self-repo actions in java_tests.yml #41626, Add read-only permissions and self-repo actions in local_env_tests.yml #41631).
  4. Dead-code & bug cleanups: Deleted unused 27-alert dind-up-action (Remove unused dind-up-action composite action #41585)

@damccorm damccorm 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.

Thanks!

@damccorm

Copy link
Copy Markdown
Contributor

High level feedback on a few of these PRs: I'm supportive of driving down these warnings, but it is unclear at this point what the work to value ratio is - along those lines, I have a few questions:

  1. Approximately how many PRs will it take to resolve all warnings?

Approximately ~40 more merged PRs needed to cleanup everything to this point.

  1. Are there broad things we can do to scope the warnings down (e.g. exclude all test files)? I think this should be doable via config

I opened another PR for zizmor to get rid of the Note warnings (~1200) since they are not applicable to this repo - #41637

  1. What value do we expect to get out of these? (e.g. has it found instances of real impact)

I've started including a doc with more details on approach/high level impact in similar large scale changes (e.g. see the description in #41272), and would recommend doing something similar. You could also inline it if the answers are simple.

Some current or near future impact with this cleanup:

  1. Archive path-traversal (ZipSlip) in Java ZipFiles.unzipFile (Enforce canonical path prefix check in ZipFiles.unzipFile #41497) and Go xlang expansionx (Enforce IsLocal and prefix check in expansionx validatePath #41589).
  2. Shell / template injection (High) in shared CI actions (build-push-docker-action Pass inputs via env in build-push-docker-action #41515, test-arguments-action Pass inputs and outputs via env in test-arguments-action #41592, setup-k8s-access Pass inputs and outputs via env in setup-k8s-access #41605) and pr-bot exec (Pass git argument arrays to @actions/exec in pr-bot updateReviewers #41524).
  3. Least-privilege GITHUB_TOKEN scoping on CI workflows (Harden permissions and setup-node caching in typescript_tests.yml #41499, Scope write permissions to job in update_python_dependencies.yml #41516, Scope write permissions to jobs in cut_release_branch.yml #41522, Add read-only permissions and self-repo actions in python_tests.yml #41526; Add read-only permissions and self-repo actions in java_tests.yml #41626, Add read-only permissions and self-repo actions in local_env_tests.yml #41631).
  4. Dead-code & bug cleanups: Deleted unused 27-alert dind-up-action (Remove unused dind-up-action composite action #41585)

Thanks, this is helpful! If you're autogenerating descriptions, I'd recommend including this kind of context

@derrickaw
derrickaw merged commit cd22ba1 into master Nov 25, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants