Skip to content

Commit 6c1d47a

Browse files
Egor Novikovparagor
authored andcommitted
Add assume role for Issuer
Signed-off-by: Egor Novikov <[email protected]>
1 parent d1d373b commit 6c1d47a

File tree

7 files changed

+43
-14
lines changed

7 files changed

+43
-14
lines changed

charts/aws-pca-issuer/crds/awspca.cert-manager.io_awspcaclusterissuers.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ spec:
4646
region:
4747
description: Should contain the AWS region if it cannot be inferred
4848
type: string
49+
role:
50+
description: Specifies the role to assume when issuing certificates.
51+
type: string
4952
secretRef:
5053
description: Needs to be specified if you want to authorize with AWS
5154
using an access and secret key

charts/aws-pca-issuer/crds/awspca.cert-manager.io_awspcaissuers.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ spec:
4545
region:
4646
description: Should contain the AWS region if it cannot be inferred
4747
type: string
48+
role:
49+
description: Specifies the role to assume when issuing certificates.
50+
type: string
4851
secretRef:
4952
description: Needs to be specified if you want to authorize with AWS
5053
using an access and secret key

config/crd/bases/awspca.cert-manager.io_awspcaclusterissuers.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ spec:
4646
region:
4747
description: Should contain the AWS region if it cannot be inferred
4848
type: string
49+
role:
50+
description: Specifies the role to assume when issuing certificates.
51+
type: string
4952
secretRef:
5053
description: Needs to be specified if you want to authorize with AWS
5154
using an access and secret key

config/crd/bases/awspca.cert-manager.io_awspcaissuers.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ spec:
4545
region:
4646
description: Should contain the AWS region if it cannot be inferred
4747
type: string
48+
role:
49+
description: Specifies the role to assume when issuing certificates.
50+
type: string
4851
secretRef:
4952
description: Needs to be specified if you want to authorize with AWS
5053
using an access and secret key
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: awspca.cert-manager.io/v1beta1
2+
kind: AWSPCAClusterIssuer
3+
metadata:
4+
name: example
5+
spec:
6+
arn: <some-pca-arn>
7+
role: <some-role-arn>

pkg/api/v1beta1/awspcaissuer_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ type AWSPCAIssuerSpec struct {
3737
// Needs to be specified if you want to authorize with AWS using an access and secret key
3838
// +optional
3939
SecretRef AWSCredentialsSecretReference `json:"secretRef,omitempty"`
40+
// Specifies the role to assume when issuing certificates.
41+
// +optional
42+
Role string `json:"role,omitempty"`
4043
}
4144

4245
// AWSCredentialsSecretReference defines the secret used by the issuer

pkg/aws/pca.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ import (
3232
"github.com/aws/aws-sdk-go-v2/aws/retry"
3333
"github.com/aws/aws-sdk-go-v2/config"
3434
"github.com/aws/aws-sdk-go-v2/credentials"
35+
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
3536
"github.com/aws/aws-sdk-go-v2/service/acmpca"
3637
acmpcatypes "github.com/aws/aws-sdk-go-v2/service/acmpca/types"
38+
"github.com/aws/aws-sdk-go-v2/service/sts"
3739
injections "github.com/cert-manager/aws-privateca-issuer/pkg/api/injections"
3840
api "github.com/cert-manager/aws-privateca-issuer/pkg/api/v1beta1"
3941
cmapi "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
@@ -89,6 +91,11 @@ func GetConfig(ctx context.Context, client client.Client, spec *api.AWSPCAIssuer
8991
}
9092

9193
func LoadConfig(ctx context.Context, client client.Client, spec *api.AWSPCAIssuerSpec) (aws.Config, error) {
94+
var configOptions []func(*config.LoadOptions) error
95+
if spec.Region != "" {
96+
configOptions = append(configOptions, config.WithRegion(spec.Region))
97+
}
98+
9299
if spec.SecretRef.Name != "" {
93100
secretNamespaceName := types.NamespacedName{
94101
Namespace: spec.SecretRef.Namespace,
@@ -118,23 +125,23 @@ func LoadConfig(ctx context.Context, client client.Client, spec *api.AWSPCAIssue
118125
return aws.Config{}, ErrNoSecretAccessKey
119126
}
120127

121-
if spec.Region != "" {
122-
return config.LoadDefaultConfig(ctx,
123-
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(string(accessKey), string(secretKey), "")),
124-
config.WithRegion(spec.Region),
125-
)
126-
}
127-
128-
return config.LoadDefaultConfig(ctx,
129-
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(string(accessKey), string(secretKey), "")),
130-
)
131-
} else if spec.Region != "" {
132-
return config.LoadDefaultConfig(ctx,
133-
config.WithRegion(spec.Region),
128+
configOptions = append(configOptions, config.WithCredentialsProvider(
129+
credentials.NewStaticCredentialsProvider(string(accessKey), string(secretKey), "")),
134130
)
135131
}
136132

137-
return config.LoadDefaultConfig(ctx)
133+
cfg, err := config.LoadDefaultConfig(ctx, configOptions...)
134+
if err != nil {
135+
return aws.Config{}, err
136+
}
137+
138+
if spec.Role != "" {
139+
stsService := sts.NewFromConfig(cfg)
140+
creds := stscreds.NewAssumeRoleProvider(stsService, spec.Role)
141+
cfg.Credentials = aws.NewCredentialsCache(creds)
142+
}
143+
144+
return cfg, nil
138145
}
139146

140147
func ClearProvisioners() {

0 commit comments

Comments
 (0)