Skip to content

Conversation

@Lyc-heng
Copy link
Contributor

@Lyc-heng Lyc-heng commented Nov 11, 2025

Thank you for your contribution to CloudRec!

What About:

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

Description:

hws Collector:Add AK details to check the last usage time of the AK.

hws rule:Add a new rule to check whether the AK last used time exceeds 365 days.

Summary by Sourcery

Add permanent access key details to Huawei Cloud collector and introduce a policy to flag keys unused for over one year

New Features:

  • Add OPA rule to detect Huawei Cloud IAM access keys that have not been used for more than 365 days

Enhancements:

  • Extend HWS collector to fetch and include detailed last-use information for each access key

@sourcery-ai
Copy link

sourcery-ai bot commented Nov 11, 2025

Reviewer's Guide

Extends the HWS IAM collector to fetch each access key’s last-used details by refactoring credential retrieval and updates the user detail struct, and introduces an OPA policy that flags active access keys unused for over 365 days.

Sequence diagram for fetching user and AK last-used details

sequenceDiagram
    participant Collector
    participant IamClient
    participant UserDetail
    Collector->>IamClient: ListPermanentAccessKeys(userId)
    IamClient-->>Collector: Credentials list
    loop For each credential
        Collector->>IamClient: ShowPermanentAccessKey(AccessKey)
        IamClient-->>Collector: ShowCredential (last_use_time, status)
    end
    Collector->>UserDetail: Populate Credentials and CredentialsDetail
Loading

Class diagram for updated UserDetail and credential retrieval

classDiagram
    class UserDetail {
        +KeystoneListUsersResult User
        +ShowUserResult UserAttribute
        +[]Credentials Credentials
        +[]ShowCredential CredentialsDetail
        +[]UserGroup UserGroups
        +LoginProtectResult LoginProtects
        +PasswordPolicyResult DomainPasswordPolicy
    }
    UserDetail --> "*" Credentials
    UserDetail --> "*" ShowCredential
    UserDetail --> "*" UserGroup
    UserDetail --> LoginProtectResult
    UserDetail --> PasswordPolicyResult

    class IamClient
    class model {
        ListPermanentAccessKeysRequest
        ShowPermanentAccessKeyRequest
        Credentials
        ShowCredential
    }
    IamClient --> model.ListPermanentAccessKeysRequest
    IamClient --> model.ShowPermanentAccessKeyRequest
    IamClient --> model.Credentials
    IamClient --> model.ShowCredential

    UserDetail "1" -- "*" Credentials
    UserDetail "1" -- "*" ShowCredential

    class Functions {
        +GetUserDetail(ctx, service, res)
        +listPermanentAccessKeys(ctx, cli, id)
        +showPermanentAccessKey(ctx, cli, AccessKey)
    }
    Functions ..> IamClient
    Functions ..> UserDetail
    Functions ..> model.Credentials
    Functions ..> model.ShowCredential
Loading

File-Level Changes

Change Details Files
Include detailed credential usage in user detail
  • Added CredentialsDetail field to UserDetail struct
  • Unpacked returned credentials and details in GetUserDetail
collector/hws/collector/iam/user.go
Refactor access key listing to fetch per-key usage metadata
  • Changed listPermanentAccessKeys signature to return both credentials and detail slice
  • Looped over each credential to call showPermanentAccessKey
  • Introduced showPermanentAccessKey helper and error handling
collector/hws/collector/iam/user.go
Add new OPA rule for AK inactivity over one year
  • Created policy.rego rule to detect active keys unused for >365 days
  • Supplied sample input, metadata, and relation JSONs for testing
rules/HUAWEI_CLOUD/HUAWEI_CLOUD_IAM User_202511112036_1023138/policy.rego
rules/HUAWEI_CLOUD/HUAWEI_CLOUD_IAM User_202511112036_1023138/input.json
rules/HUAWEI_CLOUD/HUAWEI_CLOUD_IAM User_202511112036_1023138/metadata.json
rules/HUAWEI_CLOUD/HUAWEI_CLOUD_IAM User_202511112036_1023138/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 @Lyc-heng, 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 focuses on bolstering the security posture related to Huawei Cloud IAM Access Keys. It achieves this by first enhancing the Go-based collector to gather comprehensive details, including the last usage timestamp for each Access Key. Subsequently, a new OPA rule leverages this enriched data to automatically detect and report any Access Keys that have remained inactive for more than a year, promoting proactive security measures against dormant credentials.

Highlights

  • Enhanced IAM Collector: The Huawei Cloud (HWS) IAM collector has been updated to retrieve more detailed information about Access Keys (AKs), specifically their last usage time, which is crucial for security auditing.
  • New OPA Security Rule: A new Open Policy Agent (OPA) rule has been introduced to identify IAM user Access Keys that have not been used for over 365 days, flagging them as potential security risks.
  • Improved Credential Management: This change helps enforce better credential lifecycle management by identifying and highlighting stale or unused Access Keys, thereby reducing the attack surface.
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:

Blocking issues:

  • Detected a Generic API Key, potentially exposing access to various services and sensitive operations. (link)
  • Detected a Generic API Key, potentially exposing access to various services and sensitive operations. (link)
  • Detected a Generic API Key, potentially exposing access to various services and sensitive operations. (link)
  • Detected a Generic API Key, potentially exposing access to various services and sensitive operations. (link)
  • Detected a Generic API Key, potentially exposing access to various services and sensitive operations. (link)

General comments:

  • Consider returning a slice of model.Credentials (instead of a *[]model.Credentials) to simplify the API and avoid unnecessary pointer indirection.
  • Rename the local variable 'CredentialsDetail' to follow Go naming conventions (e.g., 'credentialsDetail') to avoid confusion with the exported struct field.
  • In showPermanentAccessKey, skip appending nil entries on error so that failed credential fetches don’t propagate invalid values into the rule evaluation.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider returning a slice of model.Credentials (instead of a *[]model.Credentials) to simplify the API and avoid unnecessary pointer indirection.
- Rename the local variable 'CredentialsDetail' to follow Go naming conventions (e.g., 'credentialsDetail') to avoid confusion with the exported struct field.
- In showPermanentAccessKey, skip appending nil entries on error so that failed credential fetches don’t propagate invalid values into the rule evaluation.

## Individual Comments

### Comment 1
<location> `rules/HUAWEI_CLOUD/HUAWEI_CLOUD_IAM User_202511112036_1023138/input.json:1` </location>
<code_context>
MvwxL6lnop8WR6WtsVZL
</code_context>

<issue_to_address>
**security (generic-api-key):** Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

*Source: gitleaks*
</issue_to_address>

### Comment 2
<location> `rules/HUAWEI_CLOUD/HUAWEI_CLOUD_IAM User_202511112036_1023138/input.json:1` </location>
<code_context>
dCGceTyJTRnTMekZ8Fpo
</code_context>

<issue_to_address>
**security (generic-api-key):** Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

*Source: gitleaks*
</issue_to_address>

### Comment 3
<location> `rules/HUAWEI_CLOUD/HUAWEI_CLOUD_IAM User_202511112036_1023138/input.json:1` </location>
<code_context>
ml64zq0ePvNjzGepQ3UZBaop8FoMSh2QkBWPOWggRFDxaG3pwy8NOtHug4XoUZohb6uCLSArHJdC9CqTJTZgrSBfwtSKNLF0dVhYFNugJWGJs7iAKhJCY6LBgFrG
</code_context>

<issue_to_address>
**security (generic-api-key):** Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

*Source: gitleaks*
</issue_to_address>

### Comment 4
<location> `rules/HUAWEI_CLOUD/HUAWEI_CLOUD_IAM User_202511112036_1023138/input.json:1` </location>
<code_context>
gV2kBLXcYCmavOfr0Z1g
</code_context>

<issue_to_address>
**security (generic-api-key):** Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

*Source: gitleaks*
</issue_to_address>

### Comment 5
<location> `rules/HUAWEI_CLOUD/HUAWEI_CLOUD_IAM User_202511112036_1023138/input.json:1` </location>
<code_context>
yG0nDtyNpaOcxP1v8ftA
</code_context>

<issue_to_address>
**security (generic-api-key):** Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

*Source: gitleaks*
</issue_to_address>

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 feature to collect detailed access key information for Huawei Cloud IAM users and a corresponding OPA policy to identify access keys that have not been used for over a year. The changes in user.go correctly extend the data collection to include CredentialsDetail. However, there are a few high-severity issues related to efficiency in the Go collector and correctness in the OPA policy and its input data that need to be addressed.

Comment on lines +95 to +97
for _, credential := range *response.Credentials{
credentialDetail := showPermanentAccessKey(ctx, cli, credential.Access)
credentialsDetail = append(credentialsDetail, credentialDetail)
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 listPermanentAccessKeys function currently makes a separate API call to showPermanentAccessKey for each credential within the loop. This pattern can lead to an N+1 query problem, which might significantly impact performance, especially for users with a large number of access keys. Consider exploring if the Huawei Cloud IAM API offers a bulk retrieval method for access key details or if there's a more efficient way to gather this information.

}

user_name := input.UserAttribute.name
user_id := input.UserAttribute.domain_id
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 user_id variable is assigned input.UserAttribute.domain_id. Typically, user_id refers to the unique identifier of the user, not the domain. It is likely that input.UserAttribute.id or input.User.id should be used here to correctly identify the user. Please verify the intended source for the user's ID and correct this assignment.

user_id := input.UserAttribute.id

Copy link
Collaborator

@j3ttt j3ttt left a comment

Choose a reason for hiding this comment

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

lgtm

@j3ttt j3ttt merged commit ec1ac7e into antgroup:main Nov 12, 2025
1 of 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.

2 participants