Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions collector/baidu/collector/iam/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
package iam

import (
"github.com/core-sdk/constant"
"github.com/core-sdk/log"
"github.com/core-sdk/schema"
"context"

"github.com/baidubce/bce-sdk-go/services/iam"
"github.com/baidubce/bce-sdk-go/services/iam/api"
"github.com/cloudrec/baidu/collector"
"github.com/core-sdk/constant"
"github.com/core-sdk/log"
"github.com/core-sdk/schema"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -57,6 +58,16 @@ func GetInstanceDetail(ctx context.Context, service schema.ServiceInterface, res
return err
}

// add the root user detail first
rootDetail := Detail{
User: getUserDetail(ctx, client, "root"),
LoginProfile: getUserLoginProfile(ctx, client, "root"),
AccessKeys: getUserAccessKey(ctx, client, "root"),
Policies: getUserPolicies(ctx, client, "root"),
Groups: getUserGroups(ctx, client, "root"),
}
res <- rootDetail
Comment on lines +61 to +69
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This block of code for fetching root user details is nearly identical to the code within the for loop on lines 71-80 that fetches details for other IAM users. This code duplication can lead to maintenance issues. For instance, if a new field is added to the Detail struct, it would need to be updated in two separate places.

To improve maintainability, consider refactoring this to eliminate the duplication. One approach is to prepend the 'root' user to the list of users and then process them all within a single loop.

For example, you could replace lines 61-80 with something like this:

// Prepend root user to the list of users to be processed.
allUsers := append([]api.User{{Name: "root"}}, users.Users...)

for _, user := range allUsers {
    detail := Detail{
        User:         getUserDetail(ctx, client, user.Name),
        LoginProfile: getUserLoginProfile(ctx, client, user.Name),
        AccessKeys:   getUserAccessKey(ctx, client, user.Name),
        Policies:     getUserPolicies(ctx, client, user.Name),
        Groups:       getUserGroups(ctx, client, user.Name),
    }
    res <- detail
}


for _, user := range users.Users {
detail := Detail{
User: getUserDetail(ctx, client, user.Name),
Expand Down
Loading