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
Show file tree
Hide file tree
Changes from 2 commits
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
47 changes: 47 additions & 0 deletions AUTHENTICATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [Using kube2iam](#using-kube2iam)
- [Steps](#steps-1)
- [Using `assumeRole`](#using-assumerole)
- [Using `assumeRoleWithWebIdentity`](#using-assumerolewithwebidentity)

## Overview

Expand Down Expand Up @@ -343,3 +344,49 @@ spec:
source: InjectedIdentity
EOF
```

## Using `assumeRoleWithWebIdentity`

`provider-aws` will be configured to connect to the aws account in `RoleARN` and request
a session for `RoleARN` using it's `InjectedIdentity`

This is most useful when "sts chaining" (see [Using `assumeRole`](#using-assumerole))
jessesanford marked this conversation as resolved.
Show resolved Hide resolved
is not allowed between accounts or when cross account IRSA is more suitable.

IRSA will need to be configured between the account hosting the `RoleARN` and the
K8s cluster hosting the provider pod. See [Using IAM Roles for `ServiceAccounts`](#using-iam-roles-for-serviceaccounts)
for more info on how to setup IRSA.

Next, the `provider-aws` must be configured to use `assumeRoleWithWebIdentity`.
The code snippet below shows how to configure `provider-aws` to do so.

```console
$ cat <<EOF | kubectl apply -f -
apiVersion: aws.crossplane.io/v1beta1
kind: ProviderConfig
metadata:
name: account-b
spec:
assumeRoleWithWebIdentity:
roleARN: "arn:aws:iam::999999999999:role/account_b"
credentials:
source: InjectedIdentity
EOF
```

Role session name is supported (see <https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRoleWithWebIdentity.html#API_AssumeRoleWithWebIdentity_RequestParameters>).

```console
$ cat <<EOF | kubectl apply -f -
apiVersion: aws.crossplane.io/v1beta1
kind: ProviderConfig
metadata:
name: account-b
spec:
assumeRoleWithWebIdentity:
roleARN: "arn:aws:iam::999999999999:role/account_b"
roleSessionName: "my-optional-session-name"
credentials:
source: InjectedIdentity
EOF
```
14 changes: 14 additions & 0 deletions apis/v1beta1/providerconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type ProviderConfigSpec struct {
// AssumeRole defines the options for assuming an IAM role
AssumeRole *AssumeRoleOptions `json:"assumeRole,omitempty"`

// AssumeRoleWithWebIdentity defines the options for assuming an IAM role with a Web Identity
AssumeRoleWithWebIdentity *AssumeRoleWithWebIdentityOptions `json:"assumeRoleWithWebIdentity,omitempty"`

// AssumeRoleARN to assume with provider credentials
// This setting will be deprecated. Use the roleARN field under assumeRole instead.
// +optional
Expand Down Expand Up @@ -91,6 +94,17 @@ type AssumeRoleOptions struct {
TransitiveTagKeys []string `json:"transitiveTagKeys,omitempty"`
}

// AssumeRoleWithWebIdentityOptions define the options for assuming an IAM Role
// Fields are similar to the STS WebIdentityRoleOptions in the AWS SDK
type AssumeRoleWithWebIdentityOptions struct {
// AssumeRoleARN to assume with provider credentials
RoleARN *string `json:"roleARN,omitempty"`

// RoleSessionName is the session name, if you wish to uniquely identify this session.
// +optional
RoleSessionName string `json:"roleSessionName,omitempty"`
}

// EndpointConfig is used to configure the AWS client for a custom endpoint.
type EndpointConfig struct {
// URL lets you configure the endpoint URL to be used in SDK calls.
Expand Down
25 changes: 25 additions & 0 deletions apis/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions package/crds/aws.crossplane.io_providerconfigs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ spec:
setting will be deprecated. Use the roleARN field under assumeRole
instead.
type: string
assumeRoleWithWebIdentity:
description: AssumeRoleWithWebIdentity defines the options for assuming
an IAM role with a Web Identity
properties:
roleARN:
description: AssumeRoleARN to assume with provider credentials
type: string
roleSessionName:
description: RoleSessionName is the session name, if you wish
to uniquely identify this session.
type: string
type: object
credentials:
description: Credentials required to authenticate to this provider.
properties:
Expand Down
118 changes: 118 additions & 0 deletions pkg/clients/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ func UseProviderConfig(ctx context.Context, c client.Client, mg resource.Managed
}
return SetResolver(pc, cfg), nil
}
if pc.Spec.AssumeRoleWithWebIdentity != nil && pc.Spec.AssumeRoleWithWebIdentity.RoleARN != nil {
cfg, err := UsePodServiceAccountAssumeRoleWithWebIdentity(ctx, []byte{}, DefaultSection, region, pc)
if err != nil {
return nil, err
}
return SetResolver(pc, cfg), nil
}
cfg, err := UsePodServiceAccount(ctx, []byte{}, DefaultSection, region)
if err != nil {
return nil, err
Expand Down Expand Up @@ -379,6 +386,42 @@ func UsePodServiceAccountAssumeRole(ctx context.Context, _ []byte, _, region str
return &cnf, err
}

// UsePodServiceAccountAssumeRoleWithWebIdentity assumes an IAM role
// configured via a ServiceAccount assume Cross account IAM roles
// https://aws.amazon.com/blogs/containers/cross-account-iam-roles-for-kubernetes-service-accounts/
func UsePodServiceAccountAssumeRoleWithWebIdentity(ctx context.Context, _ []byte, _, region string, pc *v1beta1.ProviderConfig) (*aws.Config, error) {
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, err
}

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")
}
return &cnf, err
}

// UsePodServiceAccount assumes an IAM role configured via a ServiceAccount.
// https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html
func UsePodServiceAccount(ctx context.Context, _ []byte, _, region string) (*aws.Config, error) {
Expand Down Expand Up @@ -420,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 @@ -576,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 Expand Up @@ -664,6 +758,17 @@ func GetAssumeRoleARN(pcs *v1beta1.ProviderConfigSpec) (*string, error) {
return nil, errors.New("a RoleARN must be set to assume an IAM Role")
}

// GetAssumeRoleWithWebIdentityARN gets the RoleArn from a ProviderConfigSpec
func GetAssumeRoleWithWebIdentityARN(pcs *v1beta1.ProviderConfigSpec) (*string, error) {
if pcs.AssumeRoleWithWebIdentity != nil {
if pcs.AssumeRoleWithWebIdentity.RoleARN != nil && StringValue(pcs.AssumeRoleWithWebIdentity.RoleARN) != "" {
return pcs.AssumeRoleWithWebIdentity.RoleARN, nil
}
}

return nil, errors.New("a RoleARN must be set to assume with web identity")
}

// SetAssumeRoleOptions sets options when Assuming an IAM Role
func SetAssumeRoleOptions(pc *v1beta1.ProviderConfig) func(*stscreds.AssumeRoleOptions) {
if pc.Spec.AssumeRole != nil {
Expand Down Expand Up @@ -694,6 +799,19 @@ func SetAssumeRoleOptions(pc *v1beta1.ProviderConfig) func(*stscreds.AssumeRoleO
return func(opt *stscreds.AssumeRoleOptions) {}
}

// SetWebIdentityRoleOptions sets options when exchanging a WebIdentity Token for a Role
func SetWebIdentityRoleOptions(pc *v1beta1.ProviderConfig) func(*stscreds.WebIdentityRoleOptions) {
if pc.Spec.AssumeRoleWithWebIdentity != nil {
return func(opt *stscreds.WebIdentityRoleOptions) {
if pc.Spec.AssumeRoleWithWebIdentity.RoleSessionName != "" {
opt.RoleSessionName = pc.Spec.AssumeRoleWithWebIdentity.RoleSessionName
}
}
}

return func(opt *stscreds.WebIdentityRoleOptions) {}
}

// TODO(muvaf): All the types that use CreateJSONPatch are known during
// development time. In order to avoid unnecessary panic checks, we can generate
// the code that creates a patch between two objects that share the same type.
Expand Down
Loading