Skip to content

Commit

Permalink
Fix duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
christophetd committed Nov 28, 2023
1 parent 4287697 commit 58b25d5
Showing 1 changed file with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,29 @@ func (m *EKSCluster) AnalyzeRoleRelationshipsForPodIdentity() error {
if err != nil {
return fmt.Errorf("unable to describe pod identity association %s: %v", podAssociation.ID, err)
}
assumableIamRole := AssumableIAMRole{
IAMRole: &IAMRole{Arn: *podAssociationDetails.Association.RoleArn},
Reason: AssumeIAMRoleReasonPodIdentity,
}

pods, ok := m.PodsByNamespace[podAssociationNamespace]
if !ok {
// no pods in podAssociationNamespace, go to the next one
continue
}

// cache to avoid counting multiple IAM roles for a given SA
serviceAccountsHandledForPodAssociation := map[string]bool{}

// All pods in this podAssociationNamespace with this service account can assume the role
for i, _ := range pods {
if pods[i].ServiceAccount.Name == podAssociation.ServiceAccountName {
pods[i].ServiceAccount.AssumableRoles = append(pods[i].ServiceAccount.AssumableRoles, &assumableIamRole)
for _, pod := range pods {
if pod.ServiceAccount.Name == podAssociation.ServiceAccountName {
assumableIamRole := AssumableIAMRole{
IAMRole: &IAMRole{Arn: *podAssociationDetails.Association.RoleArn},
Reason: AssumeIAMRoleReasonPodIdentity,
}

// Did we already find this role for this SA? (case where multiple pods have the same SA)
if _, ok := serviceAccountsHandledForPodAssociation[pod.ServiceAccount.Name]; !ok {
log.Println("Adding assumable role " + assumableIamRole.IAMRole.Arn + " to pod " + pod.Name + " in namespace " + pod.Namespace)
pod.ServiceAccount.AssumableRoles = append(pod.ServiceAccount.AssumableRoles, &assumableIamRole)
serviceAccountsHandledForPodAssociation[pod.ServiceAccount.Name] = true
}
}
}
}
Expand Down

0 comments on commit 58b25d5

Please sign in to comment.