forked from openshift/installer
-
Notifications
You must be signed in to change notification settings - Fork 6
/
aws.go
182 lines (163 loc) · 4.98 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
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
// Package aws collects AWS-specific configuration.
package aws
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/defaults"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/openshift/installer/pkg/types/aws"
"github.com/openshift/installer/pkg/types/aws/validation"
"github.com/openshift/installer/pkg/version"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
survey "gopkg.in/AlecAivazis/survey.v1"
ini "gopkg.in/ini.v1"
)
// Platform collects AWS-specific configuration.
func Platform() (*aws.Platform, error) {
longRegions := make([]string, 0, len(validation.Regions))
shortRegions := make([]string, 0, len(validation.Regions))
for id, location := range validation.Regions {
longRegions = append(longRegions, fmt.Sprintf("%s (%s)", id, location))
shortRegions = append(shortRegions, id)
}
regionTransform := survey.TransformString(func(s string) string {
return strings.SplitN(s, " ", 2)[0]
})
defaultRegion := "us-east-1"
_, ok := validation.Regions[defaultRegion]
if !ok {
panic(fmt.Sprintf("installer bug: invalid default AWS region %q", defaultRegion))
}
ssn, err := GetSession()
if err != nil {
return nil, err
}
defaultRegionPointer := ssn.Config.Region
if defaultRegionPointer != nil && *defaultRegionPointer != "" {
_, ok := validation.Regions[*defaultRegionPointer]
if ok {
defaultRegion = *defaultRegionPointer
} else {
logrus.Warnf("Unrecognized AWS region %q, defaulting to %s", *defaultRegionPointer, defaultRegion)
}
}
sort.Strings(longRegions)
sort.Strings(shortRegions)
var region string
err = survey.Ask([]*survey.Question{
{
Prompt: &survey.Select{
Message: "Region",
Help: "The AWS region to be used for installation.",
Default: fmt.Sprintf("%s (%s)", defaultRegion, validation.Regions[defaultRegion]),
Options: longRegions,
},
Validate: survey.ComposeValidators(survey.Required, func(ans interface{}) error {
choice := regionTransform(ans).(string)
i := sort.SearchStrings(shortRegions, choice)
if i == len(shortRegions) || shortRegions[i] != choice {
return errors.Errorf("invalid region %q", choice)
}
return nil
}),
Transform: regionTransform,
},
}, ®ion)
if err != nil {
return nil, err
}
return &aws.Platform{
Region: region,
}, nil
}
// GetSession returns an AWS session by checking credentials
// and, if no creds are found, asks for them and stores them on disk in a config file
func GetSession() (*session.Session, error) {
ssn := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
ssn.Config.Credentials = credentials.NewChainCredentials([]credentials.Provider{
&credentials.EnvProvider{},
&credentials.SharedCredentialsProvider{},
})
_, err := ssn.Config.Credentials.Get()
if err == credentials.ErrNoValidProvidersFoundInChain {
err = getCredentials()
if err != nil {
return nil, err
}
}
ssn.Handlers.Build.PushBackNamed(request.NamedHandler{
Name: "openshiftInstaller.OpenshiftInstallerUserAgentHandler",
Fn: request.MakeAddToUserAgentHandler("OpenShift/4.x Installer", version.Raw),
})
return ssn, nil
}
func getCredentials() error {
var keyID string
err := survey.Ask([]*survey.Question{
{
Prompt: &survey.Input{
Message: "AWS Access Key ID",
Help: "The AWS access key ID to use for installation (this is not your username).\nhttps://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html",
},
},
}, &keyID)
if err != nil {
return err
}
var secretKey string
err = survey.Ask([]*survey.Question{
{
Prompt: &survey.Password{
Message: "AWS Secret Access Key",
Help: "The AWS secret access key corresponding to your access key ID (this is not your password).",
},
},
}, &secretKey)
if err != nil {
return err
}
path := defaults.SharedCredentialsFilename()
logrus.Infof("Writing AWS credentials to %q (https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html)", path)
err = os.MkdirAll(filepath.Dir(path), 0700)
if err != nil {
return err
}
creds, err := ini.Load(path)
if err != nil {
if os.IsNotExist(err) {
creds = ini.Empty()
creds.Section("").Comment = "https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html"
} else {
return err
}
}
profile := os.Getenv("AWS_PROFILE")
if profile == "" {
profile = "default"
}
creds.Section(profile).Key("aws_access_key_id").SetValue(keyID)
creds.Section(profile).Key("aws_secret_access_key").SetValue(secretKey)
tempPath := path + ".tmp"
file, err := os.OpenFile(tempPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
if err != nil {
return err
}
defer file.Close()
_, err = creds.WriteTo(file)
if err != nil {
err2 := os.Remove(tempPath)
if err2 != nil {
logrus.Error(errors.Wrap(err2, "failed to remove partially-written credentials file"))
}
return err
}
return os.Rename(tempPath, path)
}