bugfix trusted policy

This commit is contained in:
kkb0318
2024-05-26 16:17:34 +09:00
parent f85bef7995
commit ddd3cff935
3 changed files with 14 additions and 12 deletions
+6 -5
View File
@@ -12,6 +12,7 @@ import (
"github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/iam" "github.com/aws/aws-sdk-go-v2/service/iam"
"github.com/aws/smithy-go" "github.com/aws/smithy-go"
irsav1alpha1 "github.com/kkb0318/irsa-manager/api/v1alpha1"
"github.com/kkb0318/irsa-manager/internal/issuer" "github.com/kkb0318/irsa-manager/internal/issuer"
) )
@@ -19,8 +20,8 @@ import (
type RoleManager struct { type RoleManager struct {
// RoleName represents the name of the IAM role // RoleName represents the name of the IAM role
RoleName string RoleName string
// Namespaces represents the list of namespaces associated with the role // ServiceAccount represents the ServiceAccount Name and namespaces associated with the role
Namespaces []string ServiceAccount irsav1alpha1.IRSAServiceAccount
// Policies represents the list of policies to be attached to the role // Policies represents the list of policies to be attached to the role
Policies []string Policies []string
@@ -64,8 +65,8 @@ func (a *AwsIamClient) DeleteIRSARole(ctx context.Context, r RoleManager) error
// CreateIRSARole creates an IAM role with the specified trust policy and attaches specified policies to it // CreateIRSARole creates an IAM role with the specified trust policy and attaches specified policies to it
func (a *AwsIamClient) CreateIRSARole(ctx context.Context, issuerMeta issuer.OIDCIssuerMeta, r RoleManager) error { func (a *AwsIamClient) CreateIRSARole(ctx context.Context, issuerMeta issuer.OIDCIssuerMeta, r RoleManager) error {
providerArn := fmt.Sprintf("arn:aws:iam::%s:oidc-provider/%s", r.AccountId, issuerMeta.IssuerHostPath()) providerArn := fmt.Sprintf("arn:aws:iam::%s:oidc-provider/%s", r.AccountId, issuerMeta.IssuerHostPath())
statement := make([]map[string]interface{}, len(r.Namespaces)) statement := make([]map[string]interface{}, len(r.ServiceAccount.Namespaces))
for i, ns := range r.Namespaces { for i, ns := range r.ServiceAccount.Namespaces {
statement[i] = map[string]interface{}{ statement[i] = map[string]interface{}{
"Effect": "Allow", "Effect": "Allow",
"Principal": map[string]interface{}{ "Principal": map[string]interface{}{
@@ -74,7 +75,7 @@ func (a *AwsIamClient) CreateIRSARole(ctx context.Context, issuerMeta issuer.OID
"Action": "sts:AssumeRoleWithWebIdentity", "Action": "sts:AssumeRoleWithWebIdentity",
"Condition": map[string]interface{}{ "Condition": map[string]interface{}{
"StringEquals": map[string]interface{}{ "StringEquals": map[string]interface{}{
fmt.Sprintf("%s:sub", issuerMeta.IssuerHostPath()): fmt.Sprintf("system:serviceaccount:%s:%s", ns, r.RoleName), fmt.Sprintf("%s:sub", issuerMeta.IssuerHostPath()): fmt.Sprintf("system:serviceaccount:%s:%s", ns, r.ServiceAccount.Name),
}, },
}, },
} }
+6 -5
View File
@@ -155,16 +155,17 @@ func (r *IRSAReconciler) reconcile(ctx context.Context, obj *irsav1alpha1.IRSA,
return err return err
} }
roleManager := awsclient.RoleManager{ roleManager := awsclient.RoleManager{
RoleName: obj.Spec.IamRole.Name, RoleName: obj.Spec.IamRole.Name,
Namespaces: serviceAccount.Namespaces, ServiceAccount: serviceAccount,
Policies: obj.Spec.IamPolicies, Policies: obj.Spec.IamPolicies,
AccountId: accountId, AccountId: accountId,
} }
issuerMeta, err := issuer.NewS3IssuerMeta(&irsaSetup.Spec.Discovery.S3) issuerMeta, err := issuer.NewS3IssuerMeta(&irsaSetup.Spec.Discovery.S3)
if err != nil { if err != nil {
return err return err
} }
err = r.AwsClient.IamClient().CreateIRSARole(ctx, err = r.AwsClient.IamClient().CreateIRSARole(
ctx,
issuerMeta, issuerMeta,
roleManager, roleManager,
) )
+2 -2
View File
@@ -183,9 +183,9 @@ var _ = Describe("IRSA Controller", func() {
} }
By("creating the mock ISASetup") By("creating the mock ISASetup")
if tt.irsaSetupObj != nil { if tt.irsaSetupObj != nil {
err := k8sClient.Get(ctx, client.ObjectKeyFromObject(tt.irsaSetupObj), tt.irsaSetupObj) err := k8sClient.Get(ctx, client.ObjectKeyFromObject(tt.irsaSetupObj), &irsav1alpha1.IRSASetup{})
if err != nil && errors.IsNotFound(err) { if err != nil && errors.IsNotFound(err) {
Expect(k8sClient.Create(ctx, tt.obj)).To(Succeed()) Expect(k8sClient.Create(ctx, tt.irsaSetupObj)).To(Succeed())
} }
} }