This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
/
aws_session.go
111 lines (91 loc) · 2.81 KB
/
aws_session.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* (c) 2016-2018 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package aws_session
import (
"os"
"sync"
"time"
"github.com/adobe-platform/porter/constants"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
)
var (
regionToSession map[string]*session.Session
regionToSessionLock sync.RWMutex
)
func STS(region, roleARN string, duration time.Duration) *session.Session {
// clamp duration to sts:AssumeRole session length bounds
if duration < 900*time.Second {
duration = 900 * time.Second
}
if duration > 1*time.Hour {
duration = 1 * time.Hour
}
stsClient := sts.New(Get(region))
tokenCredentials := stscreds.NewCredentialsWithClient(stsClient, roleARN, func(provider *stscreds.AssumeRoleProvider) {
provider.Duration = duration
})
config := aws.NewConfig()
config.WithRegion(region)
config.WithCredentials(tokenCredentials)
if os.Getenv(constants.EnvDebugAws) != "" {
config.WithLogLevel(aws.LogDebug)
}
roleSession := session.New(config)
return roleSession
}
func Get(region string) (regionSession *session.Session) {
regionToSessionLock.RLock()
if !regionInMap(region) {
regionToSessionLock.RUnlock()
regionToSessionLock.Lock()
defer regionToSessionLock.Unlock()
// Multiple readers could be at regionToSessionLock.Lock()
// Check condition again
if !regionInMap(region) {
regionSession = addSession(region)
} else {
// readers 2-N that were stopped at regionToSessionLock.Lock()
// need the session
regionSession = regionToSession[region]
}
} else {
regionSession = regionToSession[region]
regionToSessionLock.RUnlock()
}
return
}
func regionInMap(region string) bool {
if regionToSession == nil {
return false
}
if _, exists := regionToSession[region]; !exists {
return false
}
return true
}
func addSession(region string) *session.Session {
if regionToSession == nil {
regionToSession = make(map[string]*session.Session)
}
// must alter sts config as well
config := aws.NewConfig()
config.WithRegion(region)
if os.Getenv(constants.EnvDebugAws) != "" {
config.WithLogLevel(aws.LogDebug)
}
regionSession := session.New(config)
regionToSession[region] = regionSession
return regionSession
}