forked from Versent/saml2aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathawsconfig.go
157 lines (126 loc) · 3.28 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
package saml2aws
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
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")
)
// 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,
}
}
// Save persist the credentials
func (p *CredentialsProvider) Save(id, secret, token string) error {
filename, err := p.filename()
if err != nil {
return err
}
err = p.ensureConfigExists()
if err != nil {
return err
}
return saveProfile(filename, p.Profile, id, secret, token)
}
// Load load the aws credentials file
func (p *CredentialsProvider) Load() (string, string, string, error) {
filename, err := p.filename()
if err != nil {
return "", "", "", err
}
config, err := ini.Load(filename)
if err != nil {
return "", "", "", err
}
iniProfile, err := config.GetSection(p.Profile)
if err != nil {
return "", "", "", ErrCredentialsNotFound
}
idKey, err := iniProfile.GetKey("aws_access_key_id")
if err != nil {
return "", "", "", ErrCredentialsNotFound
}
secretKey, err := iniProfile.GetKey("aws_secret_access_key")
if err != nil {
return "", "", "", ErrCredentialsNotFound
}
tokenKey, err := iniProfile.GetKey("aws_session_token")
if err != nil {
return "", "", "", ErrCredentialsNotFound
}
return idKey.String(), secretKey.String(), tokenKey.String(), nil
}
// ensureConfigExists verify that the config file exists
func (p *CredentialsProvider) ensureConfigExists() error {
filename, err := p.filename()
if err != nil {
return err
}
if _, err := os.Stat(filename); err != nil {
if os.IsNotExist(err) {
// create an base config file
err = ioutil.WriteFile(filename, []byte("["+p.Profile+"]"), 0600)
if err != nil {
return err
}
}
return err
}
return nil
}
func (p *CredentialsProvider) filename() (string, error) {
if p.Filename == "" {
if p.Filename = os.Getenv("AWS_SHARED_CREDENTIALS_FILE"); p.Filename != "" {
return p.Filename, nil
}
homeDir := os.Getenv("HOME") // *nix
if homeDir == "" { // Windows
homeDir = os.Getenv("USERPROFILE")
}
if homeDir == "" {
return "", ErrCredentialsHomeNotFound
}
p.Filename = filepath.Join(homeDir, ".aws", "credentials")
}
return p.Filename, nil
}
func saveProfile(filename, profile, id, secret, token string) error {
config, err := ini.Load(filename)
if err != nil {
return err
}
iniProfile, err := config.NewSection(profile)
if err != nil {
return err
}
_, err = iniProfile.NewKey("aws_access_key_id", id)
if err != nil {
return err
}
_, err = iniProfile.NewKey("aws_secret_access_key", secret)
if err != nil {
return err
}
_, err = iniProfile.NewKey("aws_session_token", token)
if err != nil {
return err
}
_, err = iniProfile.NewKey("aws_security_token", token)
if err != nil {
return err
}
return config.SaveTo(filename)
}