-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprovider.go
57 lines (45 loc) · 1.56 KB
/
provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package awscompat
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
creds "github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/sts"
"golang.org/x/oauth2"
)
const (
GCPProviderName = "GCPProvider"
)
type GCPProvider struct {
identityInput sts.AssumeRoleWithWebIdentityInput
tokenSource oauth2.TokenSource
region string
}
func NewGCPAWSCredentials(ts oauth2.TokenSource, region string, cfg *sts.AssumeRoleWithWebIdentityInput) (creds.CredentialsProvider, error) {
// todo, validate input parameters...or just fail on retrieve()
return &GCPProvider{identityInput: *cfg, region: region, tokenSource: ts}, nil
}
func (s *GCPProvider) Retrieve(context.Context) (creds.Credentials, error) {
tok, err := s.tokenSource.Token()
if err != nil {
return creds.Credentials{}, err
}
s.identityInput.WebIdentityToken = &tok.AccessToken
c, err := config.LoadDefaultConfig(context.Background(), config.WithRegion(s.region))
if err != nil {
return creds.Credentials{}, err
}
client := sts.NewFromConfig(c)
stsOutput, err := client.AssumeRoleWithWebIdentity(context.Background(), &s.identityInput)
if err != nil {
return creds.Credentials{}, err
}
return creds.Credentials{
AccessKeyID: aws.ToString(stsOutput.Credentials.AccessKeyId),
SecretAccessKey: aws.ToString(stsOutput.Credentials.SecretAccessKey),
SessionToken: aws.ToString(stsOutput.Credentials.SessionToken),
Source: GCPProviderName,
CanExpire: true,
Expires: *stsOutput.Credentials.Expiration,
}, nil
}