11/*
22 * Package responsible for returning an AWS SDK config with credentials
33 * given an AWS region, K8s namespace, and K8s service account.
4- *
5- * This package requries that the K8s service account be associated with an IAM
6- * role via IAM Roles for Service Accounts (IRSA).
74 */
85package auth
96
@@ -13,11 +10,9 @@ import (
1310
1411 "github.com/aws/aws-sdk-go-v2/aws"
1512 "github.com/aws/aws-sdk-go-v2/config"
16- "github.com/aws/aws-sdk-go-v2/credentials/stscreds"
1713 "github.com/aws/aws-sdk-go-v2/service/sts"
1814 "github.com/aws/secrets-store-csi-driver-provider-aws/credential_provider"
1915
20- k8sv1 "k8s.io/client-go/kubernetes/typed/core/v1"
2116 "k8s.io/klog/v2"
2217)
2318
@@ -28,30 +23,27 @@ const (
2823// ProviderVersion is injected at build time from the Makefile
2924var ProviderVersion = "unknown"
3025
31- // Auth is the main entry point to retrieve an AWS config. The caller
32- // initializes a new Auth object with NewAuth passing the region, namespace, pod name,
33- // K8s service account and usePodIdentity flag (and request context). The caller can then obtain AWS
34- // config by calling GetAWSConfig. podIdentityHttpTimeout is used to specify the HTTP timeout used for
35- // Pod Identity auth
26+ // Auth is the main entry point to retrieve an AWS config.
3627type Auth struct {
37- region , nameSpace , svcAcc , podName , preferredAddressType , eksAddonVersion string
38- usePodIdentity bool
39- podIdentityHttpTimeout * time.Duration
40- k8sClient k8sv1.CoreV1Interface
41- stsClient stscreds.AssumeRoleWithWebIdentityAPIClient
28+ region , nameSpace , svcAcc , preferredAddressType , eksAddonVersion string
29+ roleArn string
30+ usePodIdentity bool
31+ podIdentityHttpTimeout * time.Duration
32+ serviceAccountTokens string
33+ stsClient * sts.Client
4234}
4335
4436// NewAuth creates an Auth object for an incoming mount request.
4537func NewAuth (
46- region , nameSpace , svcAcc , podName , preferredAddressType , eksAddonVersion string ,
38+ region , nameSpace , svcAcc , preferredAddressType , eksAddonVersion string ,
39+ roleArn string ,
4740 usePodIdentity bool ,
4841 podIdentityHttpTimeout * time.Duration ,
49- k8sClient k8sv1. CoreV1Interface ,
42+ serviceAccountTokens string ,
5043) (auth * Auth , e error ) {
5144 var stsClient * sts.Client
5245
5346 if ! usePodIdentity {
54- // Get an initial config to use for STS calls when using IRSA
5547 cfg , err := config .LoadDefaultConfig (context .Background (),
5648 config .WithRegion (region ),
5749 config .WithDefaultsMode (aws .DefaultsModeStandard ),
@@ -66,15 +58,14 @@ func NewAuth(
6658 region : region ,
6759 nameSpace : nameSpace ,
6860 svcAcc : svcAcc ,
69- podName : podName ,
7061 preferredAddressType : preferredAddressType ,
7162 eksAddonVersion : eksAddonVersion ,
63+ roleArn : roleArn ,
7264 usePodIdentity : usePodIdentity ,
7365 podIdentityHttpTimeout : podIdentityHttpTimeout ,
74- k8sClient : k8sClient ,
66+ serviceAccountTokens : serviceAccountTokens ,
7567 stsClient : stsClient ,
7668 }, nil
77-
7869}
7970
8071// getAppID returns the AppID string for User-Agent
@@ -86,11 +77,10 @@ func (p Auth) getAppID() string {
8677 return ProviderName + "-" + version
8778}
8879
89- // Get the AWS config associated with a given pod's service account.
90- // The returned config is capable of automatically refreshing creds as needed
91- // by using a private TokenFetcher helper.
80+ // GetAWSConfig returns the AWS config for the pod's service account.
9281func (p Auth ) GetAWSConfig (ctx context.Context ) (aws.Config , error ) {
9382 var credProvider credential_provider.ConfigProvider
83+ var err error
9484
9585 appID := p .getAppID ()
9686
@@ -99,14 +89,18 @@ func (p Auth) GetAWSConfig(ctx context.Context) (aws.Config, error) {
9989 if p .podIdentityHttpTimeout != nil {
10090 klog .Infof ("Using custom Pod Identity timeout: %v" , * p .podIdentityHttpTimeout )
10191 }
102- var err error
103- credProvider , err = credential_provider .NewPodIdentityCredentialProvider (p .region , p .nameSpace , p .svcAcc , p .podName , p .preferredAddressType , p .podIdentityHttpTimeout , appID , p .k8sClient )
104- if err != nil {
105- return aws.Config {}, err
106- }
92+ credProvider , err = credential_provider .NewPodIdentityCredentialProvider (
93+ p .region , p .preferredAddressType , p .podIdentityHttpTimeout , appID , p .serviceAccountTokens ,
94+ )
10795 } else {
10896 klog .Infof ("Using IAM Roles for Service Accounts for authentication in namespace: %s, service account: %s" , p .nameSpace , p .svcAcc )
109- credProvider = credential_provider .NewIRSACredentialProvider (p .stsClient , p .region , p .nameSpace , p .svcAcc , appID , p .k8sClient )
97+ credProvider , err = credential_provider .NewIRSACredentialProvider (
98+ p .stsClient , p .region , p .roleArn , appID , p .serviceAccountTokens ,
99+ )
100+ }
101+
102+ if err != nil {
103+ return aws.Config {}, err
110104 }
111105
112106 return credProvider .GetAWSConfig (ctx )
0 commit comments