Skip to content

Commit

Permalink
Trong: THIS SHOULD WORK
Browse files Browse the repository at this point in the history
Co-Authored-By: Trogn Huu Nguyen <[email protected]>
  • Loading branch information
Reasonable-Solutions and tronghn committed Feb 14, 2024
1 parent 2e85482 commit b7d16e9
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions internal/reconcilers/google/cdn/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ func (r *cdnReconciler) Reconcile(ctx context.Context, client *apiclient.APIClie
"tenant": r.tenantName,
managedByLabelName: managedByLabelValue,
}
email := *naisTeam.GoogleGroupEmail
teamEmail := *naisTeam.GoogleGroupEmail

urlMapName := "nais-cdn-urlmap"
cacheInvalidatorRole := "roles/cdnCacheInvalidator"
cacheInvalidatorRole := "projects/nais-management-7178/roles/cdnCacheInvalidator"

// bucket name needs to be globally unique
tenantTeamName := fmt.Sprintf("%s-%s", strings.ReplaceAll(r.tenantName, ".", "-"), naisTeam.Slug)
Expand All @@ -113,7 +113,7 @@ func (r *cdnReconciler) Reconcile(ctx context.Context, client *apiclient.APIClie
}

// set up iam policy for the bucket
err = r.setBucketPolicy(ctx, bucketName, email, googleServiceAccount)
err = r.setBucketPolicy(ctx, bucketName, teamEmail, googleServiceAccount)
if err != nil {
return fmt.Errorf("set bucket policy: %w", err)
}
Expand All @@ -123,7 +123,7 @@ func (r *cdnReconciler) Reconcile(ctx context.Context, client *apiclient.APIClie
return fmt.Errorf("get or create backend bucket: %w", err)
}

err = r.setCacheInvalidationIamPolicy(ctx, email, googleServiceAccount, cacheInvalidatorRole, log)
err = r.setCacheInvalidationIamPolicy(ctx, teamEmail, googleServiceAccount, cacheInvalidatorRole, log)
if err != nil {
return fmt.Errorf("create team access for cache invalidation: %w", err)
}
Expand Down Expand Up @@ -199,7 +199,7 @@ func (r *cdnReconciler) Delete(ctx context.Context, client *apiclient.APIClient,
return nil
}

func (r *cdnReconciler) setCacheInvalidationIamPolicy(ctx context.Context, email string, googleServiceAccount *iam.ServiceAccount, cacheInvalidatorRole string, log logrus.FieldLogger) error {
func (r *cdnReconciler) setCacheInvalidationIamPolicy(ctx context.Context, teamEmail string, googleServiceAccount *iam.ServiceAccount, cacheInvalidatorRole string, log logrus.FieldLogger) error {
// grant teams access to cache invalidation
managementProjectName := "projects/" + r.googleManagementProjectID
projectPolicy, err := r.services.cloudResourceManagerProjects.GetIamPolicy(managementProjectName, &cloudresourcemanager.GetIamPolicyRequest{}).Context(ctx).Do()
Expand All @@ -209,15 +209,11 @@ func (r *cdnReconciler) setCacheInvalidationIamPolicy(ctx context.Context, email

newBindings, updated := gcpReconciler.CalculateRoleBindings(projectPolicy.Bindings, map[string][]string{
cacheInvalidatorRole: {
fmt.Sprintf("group:%s", email),
fmt.Sprintf("group:%s", teamEmail),
fmt.Sprintf("serviceAccount:%s", googleServiceAccount.Email),
},
})

for _, binding := range newBindings {
log.Infof("setting IAM binding for role %q: members %v", binding.Role, binding.Members)
}

if updated {
projectPolicy.Bindings = newBindings
_, err = r.services.cloudResourceManagerProjects.SetIamPolicy(managementProjectName, &cloudresourcemanager.SetIamPolicyRequest{
Expand Down

0 comments on commit b7d16e9

Please sign in to comment.