|
| 1 | +/* |
| 2 | +Copyright 2021 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// Package identity provides the AWSPrincipalTypeProvider interface and its implementations. |
| 18 | +package identityv2 |
| 19 | + |
| 20 | +import ( |
| 21 | + "bytes" |
| 22 | + "context" |
| 23 | + "crypto/sha256" |
| 24 | + "encoding/gob" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 28 | + "github.com/aws/aws-sdk-go-v2/config" |
| 29 | + "github.com/aws/aws-sdk-go-v2/credentials" |
| 30 | + "github.com/aws/aws-sdk-go-v2/credentials/stscreds" |
| 31 | + "github.com/aws/aws-sdk-go-v2/service/sts" |
| 32 | + corev1 "k8s.io/api/core/v1" |
| 33 | + |
| 34 | + infrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2" |
| 35 | + "sigs.k8s.io/cluster-api-provider-aws/v2/pkg/logger" |
| 36 | +) |
| 37 | + |
| 38 | +// AWSPrincipalTypeProvider defines the interface for AWS Principal Type Provider. |
| 39 | +type AWSPrincipalTypeProvider interface { |
| 40 | + aws.CredentialsProvider |
| 41 | + // Hash returns a unique hash of the data forming the V2 credentials |
| 42 | + // for this Principal |
| 43 | + Hash() (string, error) |
| 44 | + Name() string |
| 45 | +} |
| 46 | + |
| 47 | +// NewAWSStaticPrincipalTypeProvider will create a new AWSStaticPrincipalTypeProvider from a given AWSClusterStaticIdentity. |
| 48 | +func NewAWSStaticPrincipalTypeProvider(identity *infrav1.AWSClusterStaticIdentity, secret *corev1.Secret) *AWSStaticPrincipalTypeProvider { |
| 49 | + accessKeyID := string(secret.Data["AccessKeyID"]) |
| 50 | + secretAccessKey := string(secret.Data["SecretAccessKey"]) |
| 51 | + sessionToken := string(secret.Data["SessionToken"]) |
| 52 | + |
| 53 | + credProvider := credentials.NewStaticCredentialsProvider(accessKeyID, secretAccessKey, sessionToken) |
| 54 | + credCache := aws.NewCredentialsCache(credProvider) |
| 55 | + return &AWSStaticPrincipalTypeProvider{ |
| 56 | + Principal: identity, |
| 57 | + credentials: credCache, |
| 58 | + AccessKeyID: accessKeyID, |
| 59 | + SecretAccessKey: secretAccessKey, |
| 60 | + SessionToken: sessionToken, |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// GetAssumeRoleCredentialsCache will return the CredentialsCache of a given AWSRolePrincipalTypeProvider. |
| 65 | +func GetAssumeRoleCredentialsCache(roleIdentityProvider *AWSRolePrincipalTypeProvider, optFns []func(*config.LoadOptions) error) (*aws.CredentialsCache, error) { |
| 66 | + cfg, err := config.LoadDefaultConfig(context.TODO(), optFns...) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + |
| 71 | + stsClient := sts.NewFromConfig(cfg) |
| 72 | + credsProvider := stscreds.NewAssumeRoleProvider(stsClient, roleIdentityProvider.Principal.Spec.RoleArn, func(o *stscreds.AssumeRoleOptions) { |
| 73 | + if roleIdentityProvider.Principal.Spec.ExternalID != "" { |
| 74 | + o.ExternalID = aws.String(roleIdentityProvider.Principal.Spec.ExternalID) |
| 75 | + } |
| 76 | + o.RoleSessionName = roleIdentityProvider.Principal.Spec.SessionName |
| 77 | + if roleIdentityProvider.Principal.Spec.InlinePolicy != "" { |
| 78 | + o.Policy = aws.String(roleIdentityProvider.Principal.Spec.InlinePolicy) |
| 79 | + } |
| 80 | + o.Duration = time.Duration(roleIdentityProvider.Principal.Spec.DurationSeconds) * time.Second |
| 81 | + // For testing |
| 82 | + if roleIdentityProvider.stsClient != nil { |
| 83 | + o.Client = roleIdentityProvider.stsClient |
| 84 | + } |
| 85 | + }) |
| 86 | + |
| 87 | + return aws.NewCredentialsCache(credsProvider), nil |
| 88 | +} |
| 89 | + |
| 90 | +// NewAWSRolePrincipalTypeProvider will create a new AWSRolePrincipalTypeProvider from an AWSClusterRoleIdentity. |
| 91 | +func NewAWSRolePrincipalTypeProvider(identity *infrav1.AWSClusterRoleIdentity, sourceProvider AWSPrincipalTypeProvider, region string, log logger.Wrapper) *AWSRolePrincipalTypeProvider { |
| 92 | + return &AWSRolePrincipalTypeProvider{ |
| 93 | + credentials: nil, |
| 94 | + stsClient: nil, |
| 95 | + region: region, |
| 96 | + Principal: identity, |
| 97 | + sourceProvider: sourceProvider, |
| 98 | + log: log.WithName("AWSRolePrincipalTypeProvider"), |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// AWSStaticPrincipalTypeProvider defines the specs for a static AWSPrincipalTypeProvider. |
| 103 | +type AWSStaticPrincipalTypeProvider struct { |
| 104 | + Principal *infrav1.AWSClusterStaticIdentity |
| 105 | + credentials *aws.CredentialsCache |
| 106 | + // these are for tests :/ |
| 107 | + AccessKeyID string |
| 108 | + SecretAccessKey string |
| 109 | + SessionToken string |
| 110 | +} |
| 111 | + |
| 112 | +// Hash returns the byte encoded AWSStaticPrincipalTypeProvider. |
| 113 | +func (p *AWSStaticPrincipalTypeProvider) Hash() (string, error) { |
| 114 | + var roleIdentityValue bytes.Buffer |
| 115 | + err := gob.NewEncoder(&roleIdentityValue).Encode(p) |
| 116 | + if err != nil { |
| 117 | + return "", err |
| 118 | + } |
| 119 | + hash := sha256.New() |
| 120 | + return string(hash.Sum(roleIdentityValue.Bytes())), nil |
| 121 | +} |
| 122 | + |
| 123 | +// Retrieve returns the credential values for the AWSStaticPrincipalTypeProvider. |
| 124 | +func (p *AWSStaticPrincipalTypeProvider) Retrieve(ctx context.Context) (aws.Credentials, error) { |
| 125 | + return p.credentials.Retrieve(ctx) |
| 126 | +} |
| 127 | + |
| 128 | +// Name returns the name of the AWSStaticPrincipalTypeProvider. |
| 129 | +func (p *AWSStaticPrincipalTypeProvider) Name() string { |
| 130 | + return p.Principal.Name |
| 131 | +} |
| 132 | + |
| 133 | +// AWSRolePrincipalTypeProvider defines the specs for a AWSPrincipalTypeProvider with a role. |
| 134 | +type AWSRolePrincipalTypeProvider struct { |
| 135 | + Principal *infrav1.AWSClusterRoleIdentity |
| 136 | + credentials *aws.CredentialsCache |
| 137 | + region string |
| 138 | + sourceProvider AWSPrincipalTypeProvider |
| 139 | + log logger.Wrapper |
| 140 | + stsClient stscreds.AssumeRoleAPIClient |
| 141 | +} |
| 142 | + |
| 143 | +// Hash returns the byte encoded AWSRolePrincipalTypeProvider. |
| 144 | +func (p *AWSRolePrincipalTypeProvider) Hash() (string, error) { |
| 145 | + var roleIdentityValue bytes.Buffer |
| 146 | + err := gob.NewEncoder(&roleIdentityValue).Encode(p) |
| 147 | + if err != nil { |
| 148 | + return "", err |
| 149 | + } |
| 150 | + hash := sha256.New() |
| 151 | + return string(hash.Sum(roleIdentityValue.Bytes())), nil |
| 152 | +} |
| 153 | + |
| 154 | +// Name returns the name of the AWSRolePrincipalTypeProvider. |
| 155 | +func (p *AWSRolePrincipalTypeProvider) Name() string { |
| 156 | + return p.Principal.Name |
| 157 | +} |
| 158 | + |
| 159 | +// Retrieve returns the credential values for the AWSRolePrincipalTypeProvider. |
| 160 | +func (p *AWSRolePrincipalTypeProvider) Retrieve(ctx context.Context) (aws.Credentials, error) { |
| 161 | + |
| 162 | + if p.credentials == nil { |
| 163 | + optFns := []func(*config.LoadOptions) error{config.WithRegion(p.region)} |
| 164 | + if p.sourceProvider != nil { |
| 165 | + sourceCreds, err := p.sourceProvider.Retrieve(ctx) |
| 166 | + if err != nil { |
| 167 | + return aws.Credentials{}, err |
| 168 | + } |
| 169 | + optFns = append(optFns, config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(sourceCreds.AccessKeyID, sourceCreds.SecretAccessKey, sourceCreds.SessionToken))) |
| 170 | + } |
| 171 | + |
| 172 | + creds, err := GetAssumeRoleCredentialsCache(p, optFns) |
| 173 | + if err != nil { |
| 174 | + return aws.Credentials{}, err |
| 175 | + } |
| 176 | + // Update credentials |
| 177 | + p.credentials = creds |
| 178 | + } |
| 179 | + return p.credentials.Retrieve(ctx) |
| 180 | +} |
0 commit comments