This repository has been archived by the owner on Apr 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
config-loader.go
183 lines (148 loc) · 3.56 KB
/
config-loader.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
package main
import "github.com/go-ini/ini"
import "github.com/tj/go-debug"
import "os"
import "fmt"
import "errors"
var badCfgErr = errors.New("Could not find a suitable oktad config file!")
var awsProfileNotFound = errors.New("AWS profile not found!")
var debugCfg = debug.Debug("oktad:config")
type OktaConfig struct {
BaseURL string
AppURL string
}
// this is what we care about
// in your aws config
type AwsConfig struct {
// destination ARN
DestArn string
Region string
}
// loads configuration data from the file specified
func parseConfig(fname string) (OktaConfig, error) {
var cfg OktaConfig
f, err := loadConfig(fname)
if err != nil {
return cfg, err
}
osec := f.Section("okta")
if osec == nil {
return cfg, badCfgErr
}
if !osec.HasKey("baseUrl") || !osec.HasKey("appUrl") {
return cfg, badCfgErr
}
bu, err := osec.GetKey("baseUrl")
if err != nil {
return cfg, err
}
au, err := osec.GetKey("appUrl")
if err != nil {
return cfg, err
}
cfg.BaseURL = bu.String()
cfg.AppURL = au.String()
return cfg, nil
}
//figures out which config to load
func loadConfig(fname string) (*ini.File, error) {
cwd, _ := os.Getwd()
cwdPath := fmt.Sprintf(
"%s/%s",
cwd,
".okta",
)
hdirPath := fmt.Sprintf(
"%s/%s",
os.Getenv("HOME"),
".okta-aws/config",
)
debugCfg("trying to load from config param file")
if _, err := os.Stat(fname); err == nil {
debugCfg("loading %s", fname)
f, err := ini.Load(fname)
if err == nil {
return f, nil
}
debugCfg("error loading %s: %s", fname, err)
}
debugCfg("trying to load from CWD")
if _, err := os.Stat(cwdPath); err == nil {
debugCfg("loading %s", cwdPath)
f, err := ini.Load(cwdPath)
if err == nil {
return f, nil
}
debugCfg("error loading %s: %s", cwdPath, err)
}
debugCfg("trying to load from home dir")
if _, err := os.Stat(hdirPath); err == nil {
debugCfg("loading %s", hdirPath)
f, err := ini.Load(hdirPath)
if err == nil {
return f, nil
}
debugCfg("error loading %s: %s", hdirPath, err)
}
return nil, badCfgErr
}
// loads the aws profile file, which we need
// to look up info to assume roles
func loadAwsCfg() (*ini.File, error) {
return ini.Load(
fmt.Sprintf(
"%s/%s",
os.Getenv("HOME"),
".aws/config",
),
)
}
// reads your AWS config file to load the role ARN
// for a specific profile; returns the ARN, whether we found your profile,
// and an error if any
func readAwsProfile(name string) (AwsConfig, error) {
var cfg AwsConfig
asec, err := loadAwsCfg()
if err != nil {
debugCfg("aws profile load err, %s", err)
return cfg, err
}
s, err := asec.GetSection(name)
if err != nil {
debugCfg("aws profile read err, %s", err)
return cfg, awsProfileNotFound
}
if !s.HasKey("role_arn") {
debugCfg("aws profile %s missing role_arn key", name)
return cfg, err
}
arnKey, _ := s.GetKey("role_arn")
cfg.DestArn = arnKey.String()
// try to figure out a region...
// try to look for a region key in current section
// if fail: try to look for source_profile
// if THAT fails, try to load default
var loadSection string
if s.HasKey("region") {
k, _ := s.GetKey("region")
cfg.Region = k.String()
} else if s.HasKey("source_profile") {
k, _ := s.GetKey("source_profile")
loadSection = k.String()
} else {
loadSection = "default"
}
if loadSection != "" {
sec, err := asec.GetSection(loadSection)
if err == nil {
if k, err := sec.GetKey("region"); err == nil {
cfg.Region = k.String()
}
}
}
// finally, if cfg.region is empty, just use us-east-1
if cfg.Region == "" {
cfg.Region = "us-east-1"
}
return cfg, nil
}