Skip to content

Commit b9d1f9d

Browse files
feat: CSI Token Requests
1 parent 072ee2d commit b9d1f9d

File tree

16 files changed

+665
-939
lines changed

16 files changed

+665
-939
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,29 @@ helm install -n kube-system secrets-provider-aws aws-secrets-manager/secrets-sto
3535
kubectl apply -f https://raw.githubusercontent.com/aws/secrets-store-csi-driver-provider-aws/main/deployment/aws-provider-installer.yaml
3636
```
3737

38+
### Separate CSI Driver Installation
39+
40+
If you install the secrets-store-csi-driver separately (not via this Helm chart), you must configure `tokenRequests` in the CSI driver for the AWS provider to authenticate with AWS services:
41+
42+
```shell
43+
helm upgrade csi-secrets-store secrets-store-csi-driver/secrets-store-csi-driver \
44+
--set tokenRequests[0].audience="sts.amazonaws.com" \
45+
--set tokenRequests[1].audience="pods.eks.amazonaws.com"
46+
```
47+
48+
Or if using kubectl, add the following to your CSIDriver manifest:
49+
50+
```yaml
51+
apiVersion: storage.k8s.io/v1
52+
kind: CSIDriver
53+
metadata:
54+
name: secrets-store.csi.k8s.io
55+
spec:
56+
tokenRequests:
57+
- audience: "sts.amazonaws.com"
58+
- audience: "pods.eks.amazonaws.com"
59+
```
60+
3861
## Usage
3962

4063
Set the region name and name of your cluster to use in the bash commands that follow:

auth/auth.go

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
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
*/
85
package 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
2924
var 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.
3627
type 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.
4537
func 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.
9281
func (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

Comments
 (0)