forked from Versent/saml2aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
awsconfig.go
251 lines (197 loc) · 5.39 KB
/
awsconfig.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
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
package awsconfig
import (
"io/ioutil"
"os"
"path"
"path/filepath"
"runtime"
"time"
homedir "github.com/mitchellh/go-homedir"
"github.com/sirupsen/logrus"
"github.com/pkg/errors"
ini "gopkg.in/ini.v1"
)
var (
// ErrCredentialsHomeNotFound returned when a user home directory can't be located.
ErrCredentialsHomeNotFound = errors.New("user home directory not found")
// ErrCredentialsNotFound returned when the required aws credentials don't exist.
ErrCredentialsNotFound = errors.New("aws credentials not found")
logger = logrus.WithField("pkg", "awsconfig")
)
// AWSCredentials represents the set of attributes used to authenticate to AWS with a short lived session
type AWSCredentials struct {
AWSAccessKey string `ini:"aws_access_key_id"`
AWSSecretKey string `ini:"aws_secret_access_key"`
AWSSessionToken string `ini:"aws_session_token"`
AWSSecurityToken string `ini:"aws_security_token"`
PrincipalARN string `ini:"x_principal_arn"`
Expires time.Time `ini:"x_security_token_expires"`
}
// CredentialsProvider loads aws credentials file
type CredentialsProvider struct {
Filename string
Profile string
}
// NewSharedCredentials helper to create the credentials provider
func NewSharedCredentials(profile string) *CredentialsProvider {
return &CredentialsProvider{
Profile: profile,
}
}
// CredsExists verify that the credentials exist
func (p *CredentialsProvider) CredsExists() (bool, error) {
filename, err := p.resolveFilename()
if err != nil {
return false, err
}
err = p.ensureConfigExists()
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, errors.Wrapf(err, "unable to load file %s", filename)
}
return true, nil
}
// Save persist the credentials
func (p *CredentialsProvider) Save(awsCreds *AWSCredentials) error {
filename, err := p.resolveFilename()
if err != nil {
return err
}
err = p.ensureConfigExists()
if err != nil {
if os.IsNotExist(err) {
return createAndSaveProfile(filename, p.Profile, awsCreds)
}
return errors.Wrap(err, "unable to load file")
}
return saveProfile(filename, p.Profile, awsCreds)
}
// Load load the aws credentials file
func (p *CredentialsProvider) Load() (*AWSCredentials, error) {
filename, err := p.resolveFilename()
if err != nil {
return nil, err
}
config, err := ini.Load(filename)
if err != nil {
return nil, err
}
iniProfile, err := config.GetSection(p.Profile)
if err != nil {
return nil, ErrCredentialsNotFound
}
awsCreds := new(AWSCredentials)
err = iniProfile.MapTo(awsCreds)
if err != nil {
return nil, ErrCredentialsNotFound
}
return awsCreds, nil
}
// Expired checks if the current credentials are expired
func (p *CredentialsProvider) Expired() bool {
creds, err := p.Load()
if err != nil {
return true
}
return time.Now().After(creds.Expires)
}
// ensureConfigExists verify that the config file exists
func (p *CredentialsProvider) ensureConfigExists() error {
filename, err := p.resolveFilename()
if err != nil {
return err
}
logger.WithField("filename", filename).Debug("ensureConfigExists")
if _, err := os.Stat(filename); err != nil {
if os.IsNotExist(err) {
dir := filepath.Dir(filename)
err = os.MkdirAll(dir, os.ModePerm)
if err != nil {
return err
}
logger.WithField("dir", dir).Debug("Dir created")
// create an base config file
err = ioutil.WriteFile(filename, []byte("["+p.Profile+"]"), 0600)
if err != nil {
return err
}
logger.WithField("filename", filename).Debug("File created")
}
return err
}
return nil
}
func (p *CredentialsProvider) resolveFilename() (string, error) {
if p.Filename == "" {
filename, err := locateConfigFile()
if err != nil {
return "", err
}
p.Filename = filename
}
return p.Filename, nil
}
func locateConfigFile() (string, error) {
filename := os.Getenv("AWS_SHARED_CREDENTIALS_FILE")
if filename != "" {
return filename, nil
}
var name string
var err error
if runtime.GOOS == "windows" {
name = path.Join(os.Getenv("USERPROFILE"), ".aws", "credentials")
} else {
name, err = homedir.Expand("~/.aws/credentials")
if err != nil {
return "", ErrCredentialsHomeNotFound
}
}
logger.WithField("name", name).Debug("Expand")
// is the filename a symlink?
name, err = resolveSymlink(name)
if err != nil {
return "", errors.Wrap(err, "unable to resolve symlink")
}
logger.WithField("name", name).Debug("resolveSymlink")
return name, nil
}
func resolveSymlink(filename string) (string, error) {
sympath, err := filepath.EvalSymlinks(filename)
// return the un modified filename
if os.IsNotExist(err) {
return filename, nil
}
if err != nil {
return "", err
}
return sympath, nil
}
func createAndSaveProfile(filename, profile string, awsCreds *AWSCredentials) error {
dirPath := filepath.Dir(filename)
err := os.Mkdir(dirPath, 0700)
if err != nil {
return errors.Wrapf(err, "unable to create %s directory", dirPath)
}
_, err = os.Create(filename)
if err != nil {
return errors.Wrapf(err, "unable to create configuration")
}
return saveProfile(filename, profile, awsCreds)
}
func saveProfile(filename, profile string, awsCreds *AWSCredentials) error {
config, err := ini.Load(filename)
if err != nil {
return err
}
iniProfile, err := config.NewSection(profile)
if err != nil {
return err
}
err = iniProfile.ReflectFrom(awsCreds)
if err != nil {
return err
}
return config.SaveTo(filename)
}