Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-4.4] Bug 1880202: destroy/gcp: correct extract service account email from IAM policy binding member #4197

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions pkg/destroy/gcp/policybinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func clearIAMPolicyBindings(policy *resourcemanager.Policy, emails sets.String,
for _, binding := range policy.Bindings {
members := []string{}
for _, member := range binding.Members {
email := strings.TrimPrefix(strings.TrimPrefix(member, "deleted:"), "serviceAccount:")
email := policyMemberToEmail(member)
if emails.Has(email) {
logger.Debugf("IAM: removing %s from role %s", member, binding.Role)
removedBindings = true
Expand All @@ -65,7 +65,7 @@ func (o *ClusterUninstaller) destroyIAMPolicyBindings() error {
for _, item := range sas {
emails.Insert(item.url)
}

o.Logger.Debugf("candidate members to be removed: %s", emails.List())
if !clearIAMPolicyBindings(policy, emails, o.Logger) {
pendingPolicy := o.getPendingItems("iampolicy")
if len(pendingPolicy) > 0 {
Expand All @@ -78,3 +78,14 @@ func (o *ClusterUninstaller) destroyIAMPolicyBindings() error {
err = o.setProjectIAMPolicy(policy)
return aggregateError([]error{err}, 1)
}

// policyMemberToEmail takes member of IAM policy binding and converts it to service account email.
// https://cloud.google.com/iam/docs/reference/rest/v1/Policy#Binding
// see members[]
func policyMemberToEmail(member string) string {
email := strings.TrimPrefix(strings.TrimPrefix(member, "deleted:"), "serviceAccount:")
if idx := strings.Index(email, "?uid"); idx != -1 {
email = email[:idx]
}
return email
}
38 changes: 38 additions & 0 deletions pkg/destroy/gcp/policybinding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package gcp

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_policyMemberToEmail(t *testing.T) {
cases := []struct {
member string
email string
}{{
member: "serviceAccount:operator@project",
email: "operator@project",
}, {
member: "deleted:serviceAccount:operator@project",
email: "operator@project",
}, {
member: "deleted:serviceAccount:operator@project?uid=1231243234",
email: "operator@project",
}, {
member: "user:user@project",
email: "user:user@project",
}, {
member: "deleted:user:user@project",
email: "user:user@project",
}, {
member: "deleted:user:user@project?uid=1232131243",
email: "user:user@project",
}}
for _, test := range cases {
t.Run("", func(t *testing.T) {
email := policyMemberToEmail(test.member)
assert.Equal(t, email, test.email)
})
}
}