-
Notifications
You must be signed in to change notification settings - Fork 50
/
credentials.go
49 lines (37 loc) · 1.32 KB
/
credentials.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
package aws
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
awsCfg "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/justtrackio/gosoline/pkg/cfg"
)
func GetCredentialsProvider(ctx context.Context, config cfg.Config, settings ClientSettings) (aws.CredentialsProvider, error) {
if len(settings.AssumeRole) > 0 {
return GetAssumeRoleCredentialsProvider(ctx, settings.AssumeRole)
}
if creds := UnmarshalCredentials(config); creds != nil {
return credentials.NewStaticCredentialsProvider(creds.AccessKeyID, creds.SecretAccessKey, creds.SessionToken), nil
}
return nil, nil
}
func GetAssumeRoleCredentialsProvider(ctx context.Context, roleArn string) (aws.CredentialsProvider, error) {
var err error
var cfg aws.Config
if cfg, err = awsCfg.LoadDefaultConfig(ctx); err != nil {
return nil, fmt.Errorf("can not load default aws config: %w", err)
}
stsClient := sts.NewFromConfig(cfg)
return stscreds.NewAssumeRoleProvider(stsClient, roleArn), nil
}
func UnmarshalCredentials(config cfg.Config) *Credentials {
if !config.HasPrefix("cloud.aws.credentials") {
return nil
}
creds := &Credentials{}
config.UnmarshalKey("cloud.aws.credentials", creds)
return creds
}