Skip to content

Conversation

@wzx-ob
Copy link
Contributor

@wzx-ob wzx-ob commented Dec 5, 2025

Thank you for your contribution to CloudRec!

What About:

  • Server (java)
  • Collector (go)
  • Rule (opa)

Description:

Add a new rule for Tencent Cloud CAM

Summary by Sourcery

New Features:

  • Introduce an OPA policy rule that flags active Tencent Cloud CAM access keys with administrator-level permissions as risky.

@sourcery-ai
Copy link

sourcery-ai bot commented Dec 5, 2025

Reviewer's Guide

Adds a new OPA rule package for Tencent Cloud CAM users that flags active access keys with administrator-level permissions, along with associated rule metadata/fixture files.

Flow diagram for Tencent Cloud CAM admin access key risk rule

flowchart TD
    A[input AccessKeys and AttachedUserPolicyDetail] --> B{AccessKeys is not null}
    B -->|no| Z[Set risk to false]
    B -->|yes| C[Iterate over each access key p in AccessKeys]
    C --> D{p.Status == Active}
    D -->|no| C
    D -->|yes| E{AttachedUserPolicyDetail is not null}
    E -->|no| C
    E -->|yes| F[Iterate over each policy p2 in AttachedUserPolicyDetail]

    %% Branch 1: Direct AdministratorAccess policy name
    F --> G{p2.AttachedUserPolicy.PolicyName == AdministratorAccess}
    G -->|yes| H[Add p to root_ak]
    G -->|no| I[Check policy document]

    %% Branch 2: Policy document with wildcard admin
    I --> J[json.unmarshal p2.PolicyDocument into parsed_policy]
    J --> K{parsed_policy.statement not null}
    K -->|no| C
    K -->|yes| L[Iterate over each statement in parsed_policy.statement]
    L --> M{statement.action == *}
    M -->|no| L
    M -->|yes| N{statement.effect == allow}
    N -->|no| L
    N -->|yes| O{no statement.condition}
    O -->|no| L
    O -->|yes| P[Add p to root_ak]

    H --> Q{more access keys?}
    P --> Q
    Q -->|yes| C
    Q -->|no| R{count root_ak > 0}

    R -->|yes| S[Set risk to true]
    R -->|no| Z[Set risk to false]
Loading

File-Level Changes

Change Details Files
Introduce an OPA policy that marks Tencent Cloud CAM users as risky when they have active access keys combined with administrator-equivalent permissions.
  • Define a new Rego package with a default false risk flag that becomes true when any qualifying access key is detected.
  • Implement a helper rule that collects active access keys on users directly attached to the built-in AdministratorAccess policy.
  • Implement an additional helper rule that parses inline policy documents and flags users whose policies grant wildcard actions with allow effect and no conditions.
  • Add accompanying input, metadata, and relation JSON stubs/files for the new rule directory.
rules/TENCENT_CLOUD/TENCENT_CLOUD_CAM User_202512041838_656258/policy.rego
rules/TENCENT_CLOUD/TENCENT_CLOUD_CAM User_202512041838_656258/input.json
rules/TENCENT_CLOUD/TENCENT_CLOUD_CAM User_202512041838_656258/metadata.json
rules/TENCENT_CLOUD/TENCENT_CLOUD_CAM User_202512041838_656258/relation.json

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @wzx-ob, 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 adds a new security rule for Tencent Cloud CAM users. The rule, implemented using OPA, aims to detect and flag CAM users who have active Access Keys associated with highly privileged policies, thereby enhancing cloud security posture by identifying potential over-permissioning risks.

Highlights

  • New Tencent Cloud CAM Rule: Introduces a new OPA rule designed to identify Tencent Cloud CAM users with active Access Keys that possess overly broad permissions, specifically targeting AdministratorAccess policies or policies allowing all (*) actions.
  • Policy Implementation: The rule is implemented in Rego, defining risk if an active Access Key is found with either AdministratorAccess or a policy statement granting * action with an "allow" effect and no conditions.
  • Rule Metadata and Example: Includes metadata.json with details like advice ("该账号AK权限过大,根据业务场景配置权限最小化,以及删除不使用的AK。"), description, and a sample input.json for testing the rule.
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 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 counter productive. 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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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.

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes - here's some feedback:

  • The two root_ak rules share a lot of duplicated logic (filters on input.AccessKeys and AttachedUserPolicyDetail); consider factoring the common predicates into a helper rule or comprehension to make the policy easier to maintain.
  • The AccessKeyId := p.AccessKeyId assignment is never used in either root_ak rule; if it’s not needed for downstream processing, remove it to avoid confusion.
  • The package name and directory (TENCENT_CLOUD_CAM User_...) include a space and a numeric suffix; if there is a project convention for stable, space-free identifiers, aligning with it will make this rule easier to reference and reuse.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The two `root_ak` rules share a lot of duplicated logic (filters on `input.AccessKeys` and `AttachedUserPolicyDetail`); consider factoring the common predicates into a helper rule or comprehension to make the policy easier to maintain.
- The `AccessKeyId := p.AccessKeyId` assignment is never used in either `root_ak` rule; if it’s not needed for downstream processing, remove it to avoid confusion.
- The package name and directory (`TENCENT_CLOUD_CAM User_...`) include a space and a numeric suffix; if there is a project convention for stable, space-free identifiers, aligning with it will make this rule easier to reference and reuse.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

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 introduces a new OPA rule to identify Tencent Cloud CAM users with active access keys and administrator-level permissions. The overall goal is clear and valuable for security auditing. However, the implementation of the Rego policy can be significantly improved for better readability, maintainability, and efficiency. My review includes a detailed comment with a suggested refactoring of the policy to align with Rego best practices, making it cleaner and more performant.

Comment on lines +7 to +32
risk if {
count(root_ak) > 0
}
root_ak contains p if {
input.AccessKeys != null
some p in input.AccessKeys
AccessKeyId :=p.AccessKeyId
p.Status == "Active"
input.AttachedUserPolicyDetail != null
some p2 in input.AttachedUserPolicyDetail
p2.AttachedUserPolicy.PolicyName == "AdministratorAccess"
}
root_ak contains p if {
input.AccessKeys != null
some p in input.AccessKeys
AccessKeyId :=p.AccessKeyId
p.Status == "Active"
input.AttachedUserPolicyDetail != null
some p2 in input.AttachedUserPolicyDetail
json.unmarshal(p2.PolicyDocument, parsed_policy)
parsed_policy.statement != null
some statement in parsed_policy.statement
statement.action == "*"
statement.effect == "allow"
not statement.condition
}
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The current implementation of the policy can be significantly improved in terms of clarity, efficiency, and maintainability.

  • Code Duplication: The logic to find an active access key is repeated in both root_ak rules.
  • Inefficiency: The policy builds a set (root_ak) of all active access keys, only to check if this set is non-empty. It's more efficient to use boolean helper rules to directly determine if a risk exists, avoiding the creation of a potentially large set.
  • Unused Variables: The AccessKeyId variable is assigned on lines 13 and 22 but never used, adding noise to the code.
  • Readability: The logic is complex and nested. Breaking it down into smaller, single-purpose helper rules would make the policy much easier to understand and maintain.

I suggest refactoring the policy to use helper rules, which is a common and recommended practice in Rego. This will make the code cleaner, more efficient, and more readable.

risk if {
    has_active_ak
    has_admin_policy
}

has_active_ak {
    input.AccessKeys != null
    some p in input.AccessKeys
    p.Status == "Active"
}

has_admin_policy {
    input.AttachedUserPolicyDetail != null
    some p2 in input.AttachedUserPolicyDetail
    is_administrator_policy(p2)
}

is_administrator_policy(policy) {
    policy.AttachedUserPolicy.PolicyName == "AdministratorAccess"
}

is_administrator_policy(policy) {
    json.unmarshal(policy.PolicyDocument, parsed_policy)
    parsed_policy.statement != null
    some statement in parsed_policy.statement
    statement.action == "*"
    statement.effect == "allow"
    not statement.condition
}

@j3ttt j3ttt merged commit 8052127 into antgroup:main Dec 8, 2025
2 checks passed
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