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

feat(assumeWebIdentityRole): support AssumeRoleWithWebIdentity arn swap #1258

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions pkg/clients/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,13 @@ func GetConfigV1(ctx context.Context, c client.Client, mg resource.Managed, regi
}
return GetSessionV1(cfg)
}
if pc.Spec.AssumeRoleWithWebIdentity != nil && pc.Spec.AssumeRoleWithWebIdentity.RoleARN != nil {
cfg, err := UsePodServiceAccountV1AssumeRoleWithWebIdentity(ctx, []byte{}, pc, DefaultSection, region)
if err != nil {
return nil, err
}
return SetResolver(pc, cfg), nil
}
cfg, err := UsePodServiceAccountV1(ctx, []byte{}, pc, DefaultSection, region)
if err != nil {
return nil, errors.Wrap(err, "cannot use pod service account")
Expand Down Expand Up @@ -619,6 +626,50 @@ func UsePodServiceAccountV1AssumeRole(ctx context.Context, _ []byte, pc *v1beta1
return SetResolverV1(pc, awsv1.NewConfig().WithCredentials(v1creds).WithRegion(region)), nil
}

// UsePodServiceAccountV1AssumeRoleWithWebIdentity assumes an IAM role configured via a ServiceAccount and
// assume Cross account IAM role
// https://aws.amazon.com/blogs/containers/cross-account-iam-roles-for-kubernetes-service-accounts/
func UsePodServiceAccountV1AssumeRoleWithWebIdentity(ctx context.Context, _ []byte, pc *v1beta1.ProviderConfig, _, region string) (*awsv1.Config, error) {
jessesanford marked this conversation as resolved.
Show resolved Hide resolved
cfg, err := config.LoadDefaultConfig(ctx, userAgentV2)
if err != nil {
return nil, errors.Wrap(err, "failed to load default AWS config")
}

roleArn, err := GetAssumeRoleWithWebIdentityARN(pc.Spec.DeepCopy())
if err != nil {
return nil, errors.Wrap(err, "failed to get role arn for assume role with web identity")
}

stsclient := sts.NewFromConfig(cfg)
webIdentityRoleOptions := SetWebIdentityRoleOptions(pc)

cnf, err := config.LoadDefaultConfig(
ctx,
userAgentV2,
config.WithRegion(region),
config.WithCredentialsProvider(aws.NewCredentialsCache(
stscreds.NewWebIdentityRoleProvider(
stsclient,
StringValue(roleArn),
stscreds.IdentityTokenFile("/var/run/secrets/eks.amazonaws.com/serviceaccount/token"),
webIdentityRoleOptions,
)),
),
)
if err != nil {
return nil, errors.Wrap(err, "failed to load assumed role AWS config")
}
v2creds, err := cnf.Credentials.Retrieve(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to retrieve credentials")
}
v1creds := credentialsv1.NewStaticCredentials(
v2creds.AccessKeyID,
v2creds.SecretAccessKey,
v2creds.SessionToken)
return SetResolverV1(pc, awsv1.NewConfig().WithCredentials(v1creds).WithRegion(region)), nil
}

// UsePodServiceAccountV1 assumes an IAM role configured via a ServiceAccount.
// https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html
func UsePodServiceAccountV1(ctx context.Context, _ []byte, pc *v1beta1.ProviderConfig, _, region string) (*awsv1.Config, error) {
Expand Down