Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates the Developer Connect service account identifier and introduces a specialized AddServiceAccountRoleBinding method to the IAM client for managing service-account-level permissions. The Cloud Build logic was updated to utilize this new method for service account impersonation. Feedback was provided to correct the indentation from spaces to tabs in the new implementation and to add a missing trailing newline to the file.
Comment on lines
+153
to
+194
| // AddServiceAccountRoleBinding adds a member to a role on a specific SA and returns the updated Policy. | ||
| func (c *IAMClientImpl) AddServiceAccountRoleBinding(ctx context.Context, resourceID, role, member string) (*iamv1.Policy, error) { | ||
| // 1. Get the current policy (includes the ETag) | ||
| policy, err := c.iamService.Projects.ServiceAccounts.GetIamPolicy(resourceID).Context(ctx).Do() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get iam policy: %w", err) | ||
| } | ||
|
|
||
| // 2. Modify the policy | ||
| updated := false | ||
| for _, binding := range policy.Bindings { | ||
| if binding.Role == role { | ||
| for _, m := range binding.Members { | ||
| if m == member { | ||
| return policy, nil // Already exists, exit early to save an API call | ||
| } | ||
| } | ||
| binding.Members = append(binding.Members, member) | ||
| updated = true | ||
| break | ||
| } | ||
| } | ||
|
|
||
| if !updated { | ||
| policy.Bindings = append(policy.Bindings, &iamv1.Binding{ | ||
| Role: role, | ||
| Members: []string{member}, | ||
| }) | ||
| } | ||
|
|
||
| // 3. Set the policy (The ETag in 'policy' ensures we don't overwrite concurrent changes) | ||
| request := &iamv1.SetIamPolicyRequest{ | ||
| Policy: policy, | ||
| } | ||
|
|
||
| updatedPolicy, err := c.iamService.Projects.ServiceAccounts.SetIamPolicy(resourceID, request).Context(ctx).Do() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to set iam policy: %w", err) | ||
| } | ||
|
|
||
| return updatedPolicy, nil | ||
| } No newline at end of file |
Contributor
There was a problem hiding this comment.
The implementation of AddServiceAccountRoleBinding uses spaces for indentation instead of tabs. In Go, tabs are the standard for indentation. Additionally, the file is missing a newline at the end.
// AddServiceAccountRoleBinding adds a member to a role on a specific SA and returns the updated Policy.
func (c *IAMClientImpl) AddServiceAccountRoleBinding(ctx context.Context, resourceID, role, member string) (*iamv1.Policy, error) {
// 1. Get the current policy (includes the ETag)
policy, err := c.iamService.Projects.ServiceAccounts.GetIamPolicy(resourceID).Context(ctx).Do()
if err != nil {
return nil, fmt.Errorf("failed to get iam policy: %w", err)
}
// 2. Modify the policy
updated := false
for _, binding := range policy.Bindings {
if binding.Role == role {
for _, m := range binding.Members {
if m == member {
return policy, nil // Already exists, exit early to save an API call
}
}
binding.Members = append(binding.Members, member)
updated = true
break
}
}
if !updated {
policy.Bindings = append(policy.Bindings, &iamv1.Binding{
Role: role,
Members: []string{member},
})
}
// 3. Set the policy (The ETag in 'policy' ensures we don't overwrite concurrent changes)
request := &iamv1.SetIamPolicyRequest{
Policy: policy,
}
updatedPolicy, err := c.iamService.Projects.ServiceAccounts.SetIamPolicy(resourceID, request).Context(ctx).Do()
if err != nil {
return nil, fmt.Errorf("failed to set iam policy: %w", err)
}
return updatedPolicy, nil
}
haroonc
approved these changes
Apr 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…missions
Fixes #<issue_number_goes_here>