Skip to content

[GSoC-273] Fixing the github action Unmanaged Service Account Keys - #39177

Open
HansMarcus01 wants to merge 13 commits into
apache:masterfrom
HansMarcus01:Fix-AuditUnmanagedAccountKeys
Open

[GSoC-273] Fixing the github action Unmanaged Service Account Keys#39177
HansMarcus01 wants to merge 13 commits into
apache:masterfrom
HansMarcus01:Fix-AuditUnmanagedAccountKeys

Conversation

@HansMarcus01

@HansMarcus01 HansMarcus01 commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

This pull request fixes a runtime error in the GitHub action "Unmanaged Service Account Keys." It adds a new report for unauthorized service accounts and prompts users to comply with compliance policies. Additionally, it enables the action that reports permission issues for users working on the Apache Beam project. Handling the hashicorp/terraform procedure is included to address the permissions issue it had.

Currently, the keys.yaml file is empty, indicating that the service accounts were created manually, thus preventing secure management.

Automation and Workflow Integration:

  • Unmanaged Keys Report Degeneration Fix: (account_keys.py) This file corrects the generation of the report to include the case where both the service accounts and their keys are created from the project policies.
  • Hashicorp/setup-terraform permissions fixed: (.github/workflows/beam_Infrastructure_UsersPermissions.yml) the current version used was updated to avoid the permissions problem with the Terraform procedure
  • Enabling the feature to announce the reporting of user policy violations: (.github/workflows/beam_Infrastructure_PolicyEnforcer.yml, iam.py) The generation of the report for user policy violations was updated to include a history and allow generating the issue that notifies such problems.
    Output expected:
image

Expected output from iam.py

Title: [SECURITY] Action Required: Unauthorized IAM Users Detected

Body:

Compliance Audit Report (2026-08-18 02:33:07 UTC)

Critical security violations detected in IAM policies for project apache-beam-testing:

History

Click to expand

[... Previous reports would be collapsed here ...]

Expected output form account_keys.py

Title: [SECURITY] Action Required: Unmanaged Service Account Keys Detected

Body:

Unmanaged Keys Audit Report (2026-08-18 02:35:41 UTC)

The following unauthorized or unmanaged keys were detected in apache-beam-testing:

Please investigate and revoke these keys if they are not part of the official rotation system.

History

Click to expand

[... Previous reports would be collapsed here ...]

…rt for service accounts that are not yet found in keys.yaml
@github-actions github-actions Bot added the build label Jun 30, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the 'Unmanaged Service Account Keys' GitHub action by improving how compliance issues are reported and tracked. It introduces a more robust system for logging unauthorized service accounts and compliance violations, ensuring that security alerts are distinct from general issues and that historical audit data is preserved within GitHub issues instead of being lost during updates.

Highlights

  • Enhanced Compliance Reporting: Implemented a structured audit report format for GitHub issues, including timestamped entries and a collapsible history section for better tracking of compliance violations.
  • Security Alert Differentiation: Separated unmanaged service account keys from general compliance issues, ensuring security-critical alerts are prioritized and clearly identified.
  • Improved Issue Management: Updated the logic for creating and updating GitHub issues to append new reports to existing threads rather than overwriting them, preserving audit trails.
New Features

🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Ignored Files
  • Ignored by pattern: .github/workflows/** (1)
    • .github/workflows/beam_Infrastructure_AuditUnmanagedKeys.yml
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request refactors the compliance enforcement logic for service account keys. It separates general compliance issues from unmanaged key security alerts, and updates the announcement system to append new reports to existing GitHub issues while archiving older reports into a collapsed history section. The review feedback highlights a potential runtime NameError in account_keys.py due to a missing datetime import, points out robustness issues in sending.py when parsing and replacing HTML tags in the issue body, and recommends replacing non-idiomatic inline import statements with standard imports at the top of the file.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread infra/enforcement/account_keys.py Outdated
self.sending_client.print_announcement(title, body, recipient, announcement)
if unmanaged_keys_issues:
self.logger.info("Printing security dashboard update for unmanaged keys...")
timestamp = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")

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.

high

Potential Runtime Error: Ensure that datetime is imported at the top of the file. If it is not imported, this line will raise a NameError at runtime.

If datetime is not imported, please add from datetime import datetime, timezone at the top of the file and update this line to use them directly.

Suggested change
timestamp = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")

Comment thread infra/enforcement/sending.py Outdated
Comment on lines +265 to +268
headed = old_body.split(history_marker)
last_report = headed[0].strip()
old_history = headed[1].replace("</details>", "").strip()
combined_history = f"{last_report}\n\n---\n\n{old_history}"

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.

high

Correctness/Robustness: Using .replace("</details>", "") is risky because it will remove all occurrences of </details> within the history, which will corrupt the Markdown rendering if there are other collapsed sections or nested HTML details blocks. Additionally, split(history_marker) should limit the split to 1 to prevent issues if the marker appears elsewhere.

Instead, split with maxsplit=1 and safely strip only the trailing </details> tag.

Suggested change
headed = old_body.split(history_marker)
last_report = headed[0].strip()
old_history = headed[1].replace("</details>", "").strip()
combined_history = f"{last_report}\n\n---\n\n{old_history}"
headed = old_body.split(history_marker, 1)
last_report = headed[0].strip()
old_history = headed[1].rstrip()
if old_history.endswith("</details>"):
old_history = old_history[:-10].rstrip()
combined_history = f"{last_report}\n\n---\n\n{old_history}"

Comment thread infra/enforcement/sending.py Outdated
open_issues = self._get_open_issues(title)
open_issues.sort(key=lambda x: x.updated_at, reverse=True)

timestamp = __import__("datetime").datetime.now(__import__("datetime").timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")

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.

medium

Style/Maintainability: Using inline __import__("datetime") is non-idiomatic and violates PEP 8 guidelines regarding imports being at the top of the file.

Please import datetime and timezone at the top of the file:

from datetime import datetime, timezone

And simplify this line.

Suggested change
timestamp = __import__("datetime").datetime.now(__import__("datetime").timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
References
  1. Imports should be placed at the top of the file, as per PEP 8 guidelines. (link)

Comment thread infra/enforcement/sending.py Outdated
print("\nSimulating GitHub issue creation...")
print(f"Title: {title}")
print(f"Body: {body}")
timestamp = __import__("datetime").datetime.now(__import__("datetime").timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")

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.

medium

Style/Maintainability: Using inline __import__("datetime") is non-idiomatic and violates PEP 8 guidelines.

Please import datetime and timezone at the top of the file and simplify this line.

Suggested change
timestamp = __import__("datetime").datetime.now(__import__("datetime").timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
References
  1. Imports should be placed at the top of the file, as per PEP 8 guidelines. (link)

Comment thread infra/enforcement/account_keys.py Dismissed
@github-actions

Copy link
Copy Markdown
Contributor

Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment assign set of reviewers

…their roles within the GCP environment, eliminates redundant code, and updates the documentation.
…sting the correct version of hashicorp/terraform and avoiding the permissions error
Comment thread infra/enforcement/sending.py Dismissed
@HansMarcus01

Copy link
Copy Markdown
Contributor Author

@Amar3tto PTAL

@HansMarcus01

Copy link
Copy Markdown
Contributor Author

Note: The "Clear-text logging of sensitive information" warning flagged on this file is a false positive.CodeQL's heuristics flagged the variables secret, secret_name, and managed_secrets. However, these variables exclusively store the GCP Secret Manager resource identifiers (names/IDs), not the actual cryptographic payloads or sensitive data. The actual secret payloads are securely handled in a separate method (_get_verified_keys_from_secret_manager) and are never passed to the logger. Logging resource names is standard practice and strictly necessary for infrastructure auditing and debugging. Since no sensitive data is being exposed in plain text, I suggest we dismiss or ignore this specific alert.

Comment thread .github/workflows/beam_Infrastructure_PolicyEnforcer.yml
Comment thread .github/workflows/beam_Infrastructure_UsersPermissions.yml Outdated
Comment thread .github/workflows/beam_Infrastructure_AuditUnmanagedKeys.yml
@@ -82,4 +83,4 @@ jobs:
EMAIL_ADDRESS: ${{ secrets.ISSUE_REPORT_SENDER_EMAIL_ADDRESS }}
EMAIL_PASSWORD: ${{ secrets.ISSUE_REPORT_SENDER_EMAIL_PASSWORD }}
EMAIL_RECIPIENT: "dev@beam.apache.org"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

if we do not need the email recipient, please create an issue to track removing this (no need to fix in this pull request)

Comment thread infra/enforcement/account_keys.py
@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @Abacn for label build.

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).

…suring that unmanaged keys are reported for both authorized and unauthorized service accounts.
Comment thread infra/enforcement/iam.py Outdated
Comment thread infra/enforcement/account_keys.py Outdated
Comment thread infra/enforcement/iam.py Outdated
…AM_USER]' and '[IAC_DRIFT_SA_KEY]' tags in the enforcement scripts.

This change allows for better filtering of notifications, reducing alert fatigue, and clearly distinguishing between routine infrastructure drift and actual critical security vulnerabilities.
@HansMarcus01 HansMarcus01 changed the title [GSoC 2026] Fixing the github action Unmanaged Service Account Keys [GSoC-273] Fixing the github action Unmanaged Service Account Keys Aug 18, 2026
Comment thread infra/enforcement/account_keys.py Outdated
self.logger.info(f"Found {len(general_issues)} general compliance issues. Triggering announcement...")
title = f"Account Keys Compliance Issue Detected"
body = f"Account keys for project {self.project_id} are not compliant with the defined policies on {self.service_account_keys_file}\n\n"
title = f"[SECURITY] Action Required: Unauthorized Service Accounts Detected"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

change the title too to include IAC_DRIFT_SA_KEY - also, create a constant at the top of the file for IAC_DRIFT_SA_KEY. Even better if we have a central place for all the constants.

Comment thread infra/enforcement/iam.py

if current_user and not existing_user:
differences.append(f"User {email} not found in existing policy.")
differences.append(f"IAC_DRIFT_IAM_USER: Unauthorized user '{email}' detected in GCP but not found in existing policy.")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

create a constant for IAC_DRIFT_IAM_USER

Comment thread infra/enforcement/iam.py Outdated
for issue in diff:
body += f"- {issue}\n"
iam_drift_issues = [issue for issue in diff if "IAC_DRIFT_IAM_USER" in issue]
general_issues = [issue for issue in diff if "IAC_DRIFT_IAM_USER" not in issue]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

is it okay if general_issues only filters out IAC_DRIFT_IAM_USER but not IAC_DRIFT_SA_KEY

Comment thread infra/enforcement/iam.py Outdated
for issue in diff:
body += f"- {issue}\n"
iam_drift_issues = [issue for issue in diff if "IAC_DRIFT_IAM_USER" in issue]
general_issues = [issue for issue in diff if "IAC_DRIFT_IAM_USER" not in issue]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

use a coinstant here please and make sure filtering is correct (do we only filter out IAC_DRIFT_IAM_USER ? or should we filter out other IAC_DRIFT constants?

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.

4 participants