forked from thorbjornsen/adsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
335 lines (282 loc) · 8.02 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package adsync
import (
"os"
"strconv"
"strings"
)
type AzureConfig struct {
TenantId string
ClientId string
ClientSecret string
AuthRetries int;
GroupFilter []string
}
type RangerConfig struct {
Host string
User string
Pass string
CreateUserUri string
UpdateUserUri string
DeleteUserUri string
UserInfoUri string
GroupsUri string
GroupUsersUri string
GroupInfoUri string
GroupUserUri string
GroupDeleteUri string
GroupInfoLimit int
Headers map[string]string
}
type GeneralConfig struct {
Threads int
}
type TlsConfig struct {
InsecureSkipVerify bool
AdditionalCertificatesPemFilename string
}
type GroupFileConfig struct {
CreateGroupFile bool
GroupFilePath string
GroupFileName string
}
type Config struct {
Azure AzureConfig
Ranger RangerConfig
General GeneralConfig
Tls TlsConfig
GroupFile GroupFileConfig
}
func NewConfig() Config {
ok := true
//
// Required Azure
//
tenantId, present := os.LookupEnv("AZURE_TENANT_ID")
if ! present {
logger.Error("AZURE_TENANT_ID is not set")
ok = false
}
clientId, present := os.LookupEnv("AZURE_CLIENT_ID")
if ! present {
logger.Error("AZURE_CLIENT_ID is not set")
ok = false
}
clientSecret, present := os.LookupEnv("AZURE_CLIENT_SECRET")
if ! present {
logger.Error("AZURE_CLIENT_SECRET is not set")
ok = false
}
//
// Optional Azure
//
authRetries := 1
retries, present := os.LookupEnv("AZURE_AUTH_RETRIES")
if present {
if i, e := strconv.Atoi(retries); e != nil {
logger.Error("AZURE_AUTH_RETRIES is not a valid number")
ok = false
} else {
authRetries = i
}
}
groupFilter := []string{}
if filter, present := os.LookupEnv("AZURE_GROUP_FILTER"); present {
if len(filter) > 0 {
// Semicolon separated list
groupFilter = strings.Split(filter, ";")
}
}
//
// Required Ranger
//
rangerHost, present := os.LookupEnv("RANGER_HOST")
if ! present {
logger.Error("RANGER_HOST is not set")
ok = false
}
rangerUser, present := os.LookupEnv("RANGER_USER")
if ! present {
logger.Error("RANGER_USER is not set")
ok = false
}
rangerPass, present := os.LookupEnv("RANGER_PASS")
if ! present {
logger.Error("RANGER_PASS is not set")
ok = false
}
//
// Optional Ranger
//
createUserUri, present := os.LookupEnv("RANGER_CREATE_USER_URI")
if ! present {
createUserUri = "/service/users/default"
}
updateUserUri, present := os.LookupEnv("RANGER_UPDATE_USER_URI")
if ! present {
updateUserUri = "/service/users"
}
deleteUserUri, present := os.LookupEnv("RANGER_UPDATE_USER_URI")
if ! present {
deleteUserUri = "/service/xusers/users/"
} else if ! strings.HasSuffix(deleteUserUri, "/") {
deleteUserUri += "/"
}
userInfoUri, present := os.LookupEnv("RANGER_USER_INFO_URI")
if ! present {
userInfoUri = "/service/xusers/users/userinfo"
}
groupsUri, present := os.LookupEnv("RANGER_GROUPS_URI")
if ! present {
groupsUri = "/service/xusers/groups/"
} else if ! strings.HasSuffix(groupsUri, "/") {
groupsUri += "/"
}
groupUsersUri, present := os.LookupEnv("RANGER_GROUP_USERS_URI")
if ! present {
groupUsersUri = "/service/xusers/groupusers/groupName/"
} else if ! strings.HasSuffix(groupUsersUri, "/") {
groupUsersUri += "/"
}
groupInfoUri, present := os.LookupEnv("RANGER_GROUP_INFO_URI")
if ! present {
groupInfoUri = "/service/xusers/groups/groupinfo"
}
groupUserUri, present := os.LookupEnv("RANGER_GROUP_USER_URI")
if ! present {
groupUserUri = "/service/xusers/group/"
} else if ! strings.HasSuffix(groupUserUri, "/") {
groupUserUri += "/"
}
groupDeleteUri, present := os.LookupEnv("RANGER_GROUP_DELETE_URI")
if ! present {
groupDeleteUri = "/service/xusers/secure/groups/id/"
} else if ! strings.HasSuffix(groupsUri, "/") {
groupDeleteUri += "/"
}
groupInfoLimit := 0
limit, present := os.LookupEnv("RANGER_GROUP_INFO_LIMIT")
if present {
if i, e := strconv.Atoi(limit); e != nil {
logger.Error("RANGER_GROUP_INFO_LIMIT is not a valid number")
ok = false
} else {
groupInfoLimit = i
}
}
//
// General config
//
threads := 1
t, present := os.LookupEnv("ADSYNC_THREADS")
if present {
if i, e := strconv.Atoi(t); e != nil {
logger.Error("ADSYNC_THREADS is not a valid number")
ok = false
} else {
threads = i
}
}
if level, present := os.LookupEnv("ADSYNC_LOG_LEVEL"); present {
switch strings.ToUpper(level) {
case "DEBUG":
logger.Level = DEBUG
case "INFO":
logger.Level = INFO
case "WARN":
logger.Level = WARN
case "ERROR":
logger.Level = ERROR
case "FATAL":
logger.Level = FATAL
default:
logger.Level = INFO
}
}
// TLS Config
insecureSkipVerify := false
s, present := os.LookupEnv("TLS_INSECURE_SKIP_VERIFY")
if present {
if i, e := strconv.ParseBool(s); e != nil {
logger.Error("TLS_INSECURE_SKIP_VERIFY is not a valid bool")
ok = false
} else {
insecureSkipVerify = i
}
}
var certFilename string
c, present := os.LookupEnv("TLS_ADDITIONAL_CERTIFICATES_PEM_FILENAME")
if present {
certFilename = c
}
//File Group Provider Config
createGroupFile := false
cg, present := os.LookupEnv("CREATE_GROUP_FILE")
if present {
if i, e := strconv.ParseBool(cg); e != nil {
logger.Error("CREATE_GROUP_FILE is not a valid bool")
ok = false
} else {
createGroupFile = i
}
}
var groupFilePath string
gf, present := os.LookupEnv("GROUP_FILE_PATH")
if present {
groupFilePath = gf
}
var groupFileName string
gn, present := os.LookupEnv("GROUP_FILE_NAME")
if present {
groupFileName = gn
}
//
// Sanity check
//
if ! ok {
logger.Fatal("Required configuration parameters were not found")
}
return Config {
Azure: AzureConfig {
TenantId: tenantId,
ClientId: clientId,
ClientSecret: clientSecret,
AuthRetries: authRetries,
GroupFilter: groupFilter,
},
Ranger: RangerConfig {
Host: rangerHost,
User: rangerUser,
Pass: rangerPass,
CreateUserUri: createUserUri,
UpdateUserUri: updateUserUri,
DeleteUserUri: deleteUserUri,
UserInfoUri: userInfoUri,
GroupsUri: groupsUri,
GroupUsersUri: groupUsersUri,
GroupInfoUri: groupInfoUri,
GroupUserUri: groupUserUri,
GroupDeleteUri: groupDeleteUri,
GroupInfoLimit: groupInfoLimit,
},
General: GeneralConfig {
Threads: threads,
},
Tls: TlsConfig{
InsecureSkipVerify: insecureSkipVerify,
AdditionalCertificatesPemFilename: certFilename,
},
GroupFile: GroupFileConfig {
CreateGroupFile: createGroupFile,
GroupFilePath: groupFilePath,
GroupFileName: groupFileName,
},
}
}
var config = NewConfig()
func RangerHeaders( headers map[string]string ) {
//
// Basically going to assume the caller sent in valid data
//
// Save the headers for use later on
config.Ranger.Headers = headers
}