-
Notifications
You must be signed in to change notification settings - Fork 23
/
config.go
224 lines (185 loc) · 5.81 KB
/
config.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"errors"
"fmt"
"log"
"strings"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/Cox-Automotive/alks-go"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
)
// Version number, to be injected at link time
// to set, add `-ldflags "-X main.versionNumber=1.2.3"` to the go build command
var versionNumber string
var errNoValidCredentialSources = errors.New(`No valid credential sources found for ALKS Provider.
Please see https://github.com/Cox-Automotive/terraform-provider-alks#authentication for more information on
providing credentials for the ALKS Provider`)
// Config stores ALKS configuration and credentials
type Config struct {
URL string
AccessKey string
SecretKey string
Token string
CredsFilename string
Profile string
AssumeRole assumeRoleDetails
Account string
Role string
}
type assumeRoleDetails struct {
RoleARN string
SessionName string
ExternalID string
Policy string
}
func getCredentials(c *Config) *credentials.Credentials {
// Follow the same priority as the AWS Terraform Provider
// https://www.terraform.io/docs/providers/aws/#authentication
providers := []credentials.Provider{
&credentials.StaticProvider{Value: credentials.Value{
AccessKeyID: c.AccessKey,
SecretAccessKey: c.SecretKey,
SessionToken: c.Token,
}},
&credentials.EnvProvider{},
&credentials.SharedCredentialsProvider{
Filename: c.CredsFilename,
Profile: c.Profile,
},
}
return credentials.NewChainCredentials(providers)
}
func getCredentialsFromSession(c *Config) (*credentials.Credentials, error) {
var sess *session.Session
var err error
options := &session.Options{
Config: aws.Config{
MaxRetries: aws.Int(0),
Region: aws.String("us-east-1"),
},
}
options.Profile = c.Profile
options.SharedConfigState = session.SharedConfigEnable
sess, err = session.NewSessionWithOptions(*options)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NoCredentialProviders" {
return nil, errNoValidCredentialSources
}
return nil, fmt.Errorf("Error creating AWS session: %s", err)
}
creds := sess.Config.Credentials
cp, err := sess.Config.Credentials.Get()
if err != nil {
return nil, errNoValidCredentialSources
}
log.Printf("[DEBUG] Got session credentials from provider: %s\n", cp.ProviderName)
return creds, nil
}
// Client returns a properly configured ALKS client or an appropriate error if initialization fails
func (c *Config) Client() (*alks.Client, error) {
log.Println("[DEBUG] Validating STS credentials")
// lookup credentials
creds := getCredentials(c)
cp, cpErr := creds.Get()
if cpErr == nil {
log.Printf("[DEBUG] Got credentials from provider: %s\n", cp.ProviderName)
}
// validate we have credentials
if cpErr != nil {
if awsErr, ok := cpErr.(awserr.Error); ok && awsErr.Code() == "NoCredentialProviders" {
var err error
creds, err = getCredentialsFromSession(c)
if err != nil {
return nil, err
}
cp, cpErr = creds.Get()
}
}
if cpErr != nil {
return nil, errNoValidCredentialSources
}
// create a new session to test credentails
sess, err := session.NewSession(&aws.Config{
Region: aws.String("us-east-1"),
Credentials: creds,
})
// validate session
if err != nil {
return nil, fmt.Errorf("Error creating session from STS. (%v)", err)
}
var stsconn *sts.STS
// we need to assume another role before creating an ALKS client
if c.AssumeRole.RoleARN != "" {
arCreds := stscreds.NewCredentials(sess, c.AssumeRole.RoleARN, func(p *stscreds.AssumeRoleProvider) {
if c.AssumeRole.SessionName != "" {
p.RoleSessionName = c.AssumeRole.SessionName
}
if c.AssumeRole.ExternalID != "" {
p.ExternalID = &c.AssumeRole.ExternalID
}
if c.AssumeRole.Policy != "" {
p.Policy = &c.AssumeRole.Policy
}
})
cp, cpErr = arCreds.Get()
if cpErr != nil {
return nil, fmt.Errorf("The role %q cannot be assumed. Please verify the role ARN, role policies and your base AWS credentials", c.AssumeRole.RoleARN)
}
stsconn = sts.New(sess, &aws.Config{
Region: aws.String("us-east-1"),
Credentials: arCreds,
})
} else {
stsconn = sts.New(sess)
}
// make a basic api call to test creds are valid
_, serr := stsconn.GetCallerIdentity(&sts.GetCallerIdentityInput{})
// check for valid creds
if serr != nil {
return nil, serr
}
// got good creds, create alks sts client
client, err := alks.NewSTSClient(c.URL, cp.AccessKeyID, cp.SecretAccessKey, cp.SessionToken)
if err != nil {
return nil, err
}
// 1. Check if calling for a specific account
if len(c.Account) > 0 && len(c.Role) > 0 {
// 2. Generate client specified
client, err = generateNewClient(c, client)
if err != nil {
return nil, err
}
}
client.SetUserAgent(fmt.Sprintf("alks-terraform-provider-%s", getPluginVersion()))
log.Println("[INFO] ALKS Client configured")
return client, nil
}
func getPluginVersion() string {
if versionNumber != "" {
return versionNumber
}
return "unknown"
}
func generateNewClient(c *Config, client *alks.Client) (*alks.Client, error) {
// 3. Create account string
newAccDetail := c.Account + "/ALKS" + c.Role
// Calling for the same account; exit early
if strings.Contains(newAccDetail, client.AccountDetails.Account) {
return client, nil
}
// 4. Alright, new credentials needed - swap em out.
client.AccountDetails.Account = newAccDetail
client.AccountDetails.Role = c.Role
newCreds, err := client.CreateIamSession()
if err != nil {
return nil, err
}
newClient, _ := alks.NewSTSClient(c.URL, newCreds.AccessKey, newCreds.SecretKey, newCreds.SessionToken)
// 5. Return this new client for provider
return newClient, nil
}