-
Notifications
You must be signed in to change notification settings - Fork 170
/
shared.go
72 lines (62 loc) · 1.77 KB
/
shared.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
package store
import (
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/session"
)
const (
RegionEnvVar = "CHAMBER_AWS_REGION"
CustomSSMEndpointEnvVar = "CHAMBER_AWS_SSM_ENDPOINT"
)
func getSession(numRetries int) (*session.Session, *string, error) {
var region *string
endpointResolver := func(service, region string, optFns ...func(*endpoints.Options)) (endpoints.ResolvedEndpoint, error) {
customSsmEndpoint, ok := os.LookupEnv(CustomSSMEndpointEnvVar)
if ok {
return endpoints.ResolvedEndpoint{
URL: customSsmEndpoint,
}, nil
}
return endpoints.DefaultResolver().EndpointFor(service, region, optFns...)
}
if regionOverride, ok := os.LookupEnv(RegionEnvVar); ok {
region = aws.String(regionOverride)
}
retSession, err := session.NewSessionWithOptions(
session.Options{
Config: aws.Config{
Region: region,
MaxRetries: aws.Int(numRetries),
EndpointResolver: endpoints.ResolverFunc(endpointResolver),
},
SharedConfigState: session.SharedConfigEnable,
},
)
if err != nil {
return nil, nil, err
}
// If region is still not set, attempt to determine it via ec2 metadata API
if aws.StringValue(retSession.Config.Region) == "" {
session := session.New()
ec2metadataSvc := ec2metadata.New(session)
if regionOverride, err := ec2metadataSvc.Region(); err == nil {
region = aws.String(regionOverride)
}
}
return retSession, region, nil
}
func uniqueStringSlice(slice []string) []string {
unique := make(map[string]struct{}, len(slice))
j := 0
for _, value := range slice {
if _, ok := unique[value]; ok {
continue
}
unique[value] = struct{}{}
slice[j] = value
j++
}
return slice[:j]
}