forked from Versent/saml2aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
156 lines (125 loc) · 3.09 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
package saml2aws
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
ini "gopkg.in/ini.v1"
)
var (
// ErrConfigHomeNotFound returned when a user home directory can't be located.
ErrConfigHomeNotFound = errors.New("user home directory not found")
// ErrConfigFileNotFound returned when the required aws credentials file doesn't exist.
ErrConfigFileNotFound = errors.New("aws credentials file not found")
)
// ConfigLoader loads config options
type ConfigLoader struct {
Filename string
Profile string
}
// NewConfigLoader helper to create the config
func NewConfigLoader(profile string) *ConfigLoader {
return &ConfigLoader{
Profile: profile,
}
}
// ensureConfigExists verify that the config file exists
func (p *ConfigLoader) 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
}
// SaveUsername persist the username
func (p *ConfigLoader) SaveUsername(username string) error {
filename, err := p.filename()
if err != nil {
return err
}
return saveConfig(filename, p.Profile, "username", username)
}
// LoadUsername load the username
func (p *ConfigLoader) LoadUsername() (string, error) {
filename, err := p.filename()
if err != nil {
return "", err
}
err = p.ensureConfigExists()
if err != nil {
return "", err
}
return loadConfig(filename, p.Profile, "username")
}
// SaveHostname persist the hostname
func (p *ConfigLoader) SaveHostname(hostname string) error {
filename, err := p.filename()
if err != nil {
return err
}
return saveConfig(filename, p.Profile, "hostname", hostname)
}
// LoadHostname load the hostname
func (p *ConfigLoader) LoadHostname() (string, error) {
filename, err := p.filename()
if err != nil {
return "", err
}
err = p.ensureConfigExists()
if err != nil {
return "", err
}
return loadConfig(filename, p.Profile, "hostname")
}
func (p *ConfigLoader) filename() (string, error) {
if p.Filename == "" {
if p.Filename = os.Getenv("AWS2SAML_CONFIG_FILE"); p.Filename != "" {
return p.Filename, nil
}
homeDir := os.Getenv("HOME") // *nix
if homeDir == "" { // Windows
homeDir = os.Getenv("USERPROFILE")
}
if homeDir == "" {
return "", ErrConfigHomeNotFound
}
p.Filename = filepath.Join(homeDir, ".aws2saml.config")
}
return p.Filename, nil
}
func loadConfig(filename, profile, field string) (string, error) {
config, err := ini.Load(filename)
if err != nil {
return "", err
}
iniProfile, err := config.GetSection(profile)
if err != nil {
return "", err
}
return iniProfile.Key(field).String(), nil
}
func saveConfig(filename, profile, field, value 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(field, value)
if err != nil {
return err
}
return config.SaveTo(filename)
}