forked from aws/amazon-ecs-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_v1.go
More file actions
264 lines (230 loc) · 9.64 KB
/
config_v1.go
File metadata and controls
264 lines (230 loc) · 9.64 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright 2015-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package config
import (
"fmt"
"os"
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/commands/flags"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/urfave/cli"
)
const (
configVersion = "v1"
composeProjectNamePrefixKey = "compose-project-name-prefix"
composeServiceNamePrefixKey = "compose-service-name-prefix"
cfnStackNamePrefixKey = "cfn-stack-name-prefix"
awsAccessKey = "aws_access_key_id"
awsSecretKey = "aws_secret_access_key"
awsSessionToken = "aws_session_token"
clusterKey = "cluster"
clustersKey = "clusters"
regionKey = "region"
iniConfigVersion = 0
yamlConfigVersion = 1
)
// LocalConfig is the top level struct representing the local ECS configuration
type LocalConfig struct {
Version int // which format version was the config file that was read. 1 == yaml, 0 == old ini
Cluster string
AWSProfile string
Region string
AWSAccessKey string
AWSSecretKey string
AWSSessionToken string
ComposeServiceNamePrefix string
ComposeProjectNamePrefix string // Deprecated; remains for backwards compatibility
CFNStackName string
CFNStackNamePrefix string // Deprecated; remains for backwards compatibility
DefaultLaunchType string
}
// Profile is a simple struct for storing a single AWS profile config
type Profile struct {
AWSAccessKey string `yaml:"aws_access_key_id"`
AWSSecretKey string `yaml:"aws_secret_access_key"`
AWSSessionToken string `yaml:"aws_session_token,omitempty"`
}
// Cluster is a simple struct for storing a single ECS cluster config
type Cluster struct {
Cluster string `yaml:"cluster"`
Region string `yaml:"region"`
ComposeServiceNamePrefix string `yaml:"compose-service-name-prefix,omitempty"`
CFNStackName string `yaml:"cfn-stack-name,omitempty"`
DefaultLaunchType string `yaml:"default_launch_type"`
}
// ClusterConfig is the top level struct representing the cluster config file
type ClusterConfig struct {
Version string
Default string `yaml:"default"`
Clusters map[string]Cluster `yaml:"clusters"`
}
// ProfileConfig is the top level struct representing the Credentials file
type ProfileConfig struct {
Version string
Default string `yaml:"default"`
Profiles map[string]Profile `yaml:"ecs_profiles"`
}
// NewLocalConfig creates a new instance of CliConfig from the cluster name.
func NewLocalConfig(cluster string) *LocalConfig {
return &LocalConfig{Cluster: cluster}
}
// ToAWSSession creates a new Session object from the CliConfig object.
// Region: Order of resolution
// 1) ECS CLI Flags
// a) Region Flag --region
// b) Cluster Config Flag (--cluster-config)
// 2) ECS Config - attempts to fetch the region from the default ECS Profile
// 3) Environment Variable - attempts to fetch the region from environment variables:
// a) AWS_REGION (OR)
// b) AWS_DEFAULT_REGION
// 4) AWS Profile - attempts to use region from AWS profile name
// a) --aws-profile flag
// b) AWS_PROFILE environment variable
// c) AWS_DEFAULT_PROFILE environment variable (defaults to 'default')
//
// Credentials: Order of resolution
// 1) ECS CLI Profile Flags
// a) ECS Profile (--ecs-profile)
// b) AWS Profile (--aws-profile)
// 2) Environment Variables - attempts to fetch the credentials from environment variables:
// a) ECS_PROFILE
// b) AWS_PROFILE
// c) AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, Optional: AWS_SESSION_TOKEN
// 3) ECS Config - attempts to fetch the credentials from the default ECS Profile
// 4) Default AWS Profile - attempts to use credentials (aws_access_key_id, aws_secret_access_key) or assume_role (role_arn, source_profile) from AWS profile name
// a) AWS_DEFAULT_PROFILE environment variable (defaults to 'default')
// 5) EC2 Instance role
func (cfg *LocalConfig) ToAWSSession(context *cli.Context) (*session.Session, error) {
svcConfig := aws.Config{}
if ecsEndpoint := RecursiveFlagSearch(context, flags.EndpointFlag); ecsEndpoint != "" {
defaultResolver := endpoints.DefaultResolver()
ecsCustomResolverFn := func(service, region string, optFns ...func(*endpoints.Options)) (endpoints.ResolvedEndpoint, error) {
if service == "ecs" {
return endpoints.ResolvedEndpoint{
URL: ecsEndpoint,
}, nil
}
return defaultResolver.EndpointFor(service, region, optFns...)
}
svcConfig.EndpointResolver = endpoints.ResolverFunc(ecsCustomResolverFn)
}
return cfg.toAWSSessionWithConfig(context, &svcConfig)
}
// ToAWSSessionWithConfig processes credential order of precedence
// The argument svcConfig is needed to allow important unit tests to work
// (for example: assume role)
func (cfg *LocalConfig) toAWSSessionWithConfig(context *cli.Context, svcConfig *aws.Config) (*session.Session, error) {
region, err := cfg.getRegion()
if err != nil || region == "" {
return nil, fmt.Errorf("Set a region using ecs-cli configure command with the --%s flag or %s environment variable or --%s flag", flags.RegionFlag, flags.AwsRegionEnvVar, flags.ProfileFlag)
}
if hasProfileFlags(context) {
// The AWS SDK Go lets Env Vars override sourcing a profile from the shared credential file
// This means that if the Env Vars are present, it will ignore the profile.
// So we unset them and then reset them, to allow our order of precedence to be correct.
keyID, secretKey := unsetEnvVars()
defer resetEnvVars(keyID, secretKey)
return sessionFromECSConfig(cfg, region, svcConfig)
} else if hasEnvVars(context) {
return sessionFromProfile("", region, svcConfig)
} else if isDefaultECSProfileCase(cfg) {
return sessionFromECSConfig(cfg, region, svcConfig)
}
return sessionFromProfile("", region, svcConfig)
}
func hasProfileFlags(context *cli.Context) bool {
return (RecursiveFlagSearch(context, flags.ECSProfileFlag) != "" || RecursiveFlagSearch(context, flags.AWSProfileFlag) != "")
}
func hasEnvVars(context *cli.Context) bool {
return (os.Getenv(flags.AWSSecretKeyEnvVar) != "" && os.Getenv(flags.AWSAccessKeyEnvVar) != "")
}
func isDefaultECSProfileCase(cfg *LocalConfig) bool {
return (cfg.AWSAccessKey != "" || cfg.AWSSecretKey != "" || cfg.AWSProfile != "")
}
func sessionFromECSConfig(cfg *LocalConfig, region string, svcConfig *aws.Config) (*session.Session, error) {
if cfg.AWSSecretKey != "" {
return sessionFromKeys(region, cfg.AWSAccessKey, cfg.AWSSecretKey, cfg.AWSSessionToken, svcConfig)
}
return sessionFromProfile(cfg.AWSProfile, region, svcConfig)
}
func unsetEnvVars() (keyID string, secretKey string) {
keyID = os.Getenv(flags.AWSAccessKeyEnvVar)
secretKey = os.Getenv(flags.AWSSecretKeyEnvVar)
os.Unsetenv(flags.AWSAccessKeyEnvVar)
os.Unsetenv(flags.AWSSecretKeyEnvVar)
return keyID, secretKey
}
func resetEnvVars(keyID string, secretKey string) {
os.Setenv(flags.AWSAccessKeyEnvVar, keyID)
os.Setenv(flags.AWSSecretKeyEnvVar, secretKey)
}
func sessionFromProfile(profile string, region string, svcConfig *aws.Config) (*session.Session, error) {
svcConfig.Region = aws.String(region)
return session.NewSessionWithOptions(session.Options{
Config: *svcConfig,
Profile: profile,
SharedConfigState: session.SharedConfigEnable,
})
}
func sessionFromKeys(region string, awsAccess string, awsSecret string, sessionToken string, svcConfig *aws.Config) (*session.Session, error) {
svcConfig.Region = aws.String(region)
svcConfig.Credentials = credentials.NewStaticCredentials(awsAccess, awsSecret, sessionToken)
return session.NewSession(svcConfig)
}
// Region: Order of resolution
// 1) ECS CLI Flags
// a) Region Flag --region
// b) Cluster Config Flag (--cluster-config)
// 2) ECS Config - attempts to fetch the region from the default ECS Profile
// 3) Environment Variable - attempts to fetch the region from environment variables:
// a) AWS_REGION (OR)
// b) AWS_DEFAULT_REGION
// 4) AWS Profile - attempts to use region from AWS profile name
// a) --aws-profile flag
// b) AWS_PROFILE environment variable
// c) AWS_DEFAULT_PROFILE environment variable (defaults to 'default')
func (cfg *LocalConfig) getRegion() (string, error) {
region := cfg.Region
if region == "" {
// Search the chain of environment variables for region.
for _, envVar := range []string{flags.AwsRegionEnvVar, flags.AwsDefaultRegionEnvVar} {
region = os.Getenv(envVar)
if region != "" {
break
}
}
}
var err error
if region == "" {
region, err = cfg.getRegionFromAWSProfile()
}
return region, err
}
func (cfg *LocalConfig) getRegionFromAWSProfile() (string, error) {
awsProfile := ""
if cfg.AWSProfile != "" {
awsProfile = cfg.AWSProfile
} else {
awsProfile = os.Getenv(flags.AwsDefaultProfileEnvVar)
}
s, err := session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
Profile: awsProfile,
})
if err != nil {
return "", err
}
return aws.StringValue(s.Config.Region), nil
}