-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathaws.go
93 lines (80 loc) · 2.03 KB
/
aws.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package clients
import (
"context"
"net/url"
"os"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
)
func NewAwsConfigWithUri(uri string) (aws.Config, error) {
ctx := context.Background()
cfg := aws.Config{}
// Parse URI.
u, err := url.Parse(uri)
if err != nil {
return cfg, err
}
profile := os.Getenv("AWS_PROFILE")
if str := u.Query().Get("profile"); str != "" {
profile = str
}
if profile != "" {
cfg, err = config.LoadDefaultConfig(ctx, config.WithSharedConfigProfile(profile))
if err != nil {
return cfg, err
}
} else {
cfg, err = config.LoadDefaultConfig(ctx)
if err != nil {
return cfg, err
}
}
useCredentials := false
accessKeyId := os.Getenv("AWS_ACCESS_KEY_ID")
if str := u.Query().Get("access_key_id"); str != "" {
accessKeyId = str
}
if accessKeyId != "" {
useCredentials = true
}
secretAccessKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
if str := u.Query().Get("secret_access_key"); str != "" {
secretAccessKey = str
}
if secretAccessKey != "" {
useCredentials = true
}
sessionToken := os.Getenv("AWS_SESSION_TOKEN")
if str := u.Query().Get("session_token"); str != "" {
sessionToken = str
}
if sessionToken != "" {
useCredentials = true
}
if useCredentials {
cfg.Credentials = aws.NewCredentialsCache(credentials.NewStaticCredentialsProvider(accessKeyId, secretAccessKey, sessionToken))
}
region := os.Getenv("AWS_DEFAULT_REGION")
if str := u.Query().Get("region"); str != "" {
region = str
}
if region != "" {
cfg.Region = region
}
endpoint := os.Getenv("AWS_ENDPOINT_URL")
if str := u.Query().Get("endpoint_url"); str != "" {
endpoint = str
}
if endpoint != "" {
customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{
PartitionID: "aws",
URL: endpoint,
SigningRegion: cfg.Region,
}, nil
})
cfg.EndpointResolverWithOptions = customResolver
}
return cfg, nil
}