/
ucpAuth.go
468 lines (391 loc) · 10.8 KB
/
ucpAuth.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
package ucp
import (
"encoding/csv"
"encoding/json"
"fmt"
"os"
"strconv"
log "github.com/sirupsen/logrus"
"github.com/joeabbey/diver/pkg/ucp/types"
)
// AuthStatus - will return the current logged in user
func (c *Client) AuthStatus() (*ucptypes.Account, error) {
log.Debugln("Retrieving the current authorisation status")
url := fmt.Sprintf("%s/id/", c.UCPURL)
response, err := c.getRequest(url, nil)
if err != nil {
return nil, fmt.Errorf("Unable to authenticate with existing token")
}
var a ucptypes.Account
err = json.Unmarshal(response, &a)
if err != nil {
return nil, err
}
return &a, nil
}
// NewAccount - Creates a new account within UCP
func NewAccount(fullname, username, password string, isActive, isOrg, isAdmin, searchLDAP bool) *ucptypes.Account {
return &ucptypes.Account{
FullName: fullname,
IsActive: isActive,
IsAdmin: isAdmin,
IsOrg: isOrg,
Name: username,
Password: password,
SearchLDAP: searchLDAP,
}
}
// NewUser - Creates a new user accound
func NewUser(fullname, username, password string, isActive, isAdmin, searchLDAP bool) *ucptypes.Account {
return &ucptypes.Account{
FullName: fullname,
IsActive: isActive,
IsAdmin: isAdmin,
IsOrg: false,
Name: username,
Password: password,
SearchLDAP: searchLDAP,
}
}
// NewOrg - Creates a new organisation
func NewOrg(fullname, username, password string, isActive, isAdmin, searchLDAP bool) *ucptypes.Account {
return &ucptypes.Account{
FullName: fullname,
IsActive: isActive,
IsAdmin: isAdmin,
IsOrg: true,
Name: username,
Password: password,
SearchLDAP: searchLDAP,
}
}
//AddAccount - adds a new account to UCP
func (c *Client) AddAccount(account *ucptypes.Account) error {
if len(account.Password) < 8 && !account.IsOrg {
// UCP appears to have a minimal password lenght (not sure about complexity)
return fmt.Errorf("Password doesn't meet complexity levels or length")
}
if account.IsOrg {
log.Infof("Creating account for Organisation [%s]", account.Name)
} else {
if account.FullName != "" {
log.Infof("Creating account for user [%s]", account.FullName)
} else {
log.Infof("Creating account for user [%s]", account.Name)
}
}
url := fmt.Sprintf("%s/accounts", c.UCPURL)
b, err := json.Marshal(account)
log.Debugf("%s", string(b))
if err != nil {
return err
}
response, err := c.postRequest(url, b)
if err != nil {
log.Debugf("response: %v", err)
parseerr := ParseUCPError([]byte(response))
if parseerr != nil {
log.Debugf("%s", response)
log.Errorf("Error parsing UCP error: %v", err)
}
return err
}
log.Debugf("%v", string(response))
return nil
}
//DeleteAccount - deletes an account in UCP
func (c *Client) DeleteAccount(account string) error {
log.Infof("Deleting account [%s]", account)
url := fmt.Sprintf("%s/accounts/%s", c.UCPURL, account)
response, err := c.delRequest(url, nil)
if err != nil {
parseerr := ParseUCPError(response)
if parseerr != nil {
log.Errorf("Error parsing UCP error: %v", err)
}
return err
}
return nil
}
//AddTeamToOrganisation - adds a team to an existing organisation
func (c *Client) AddTeamToOrganisation(team *ucptypes.Team, org string) error {
log.Infof("Creating team [%s]", team.Name)
url := fmt.Sprintf("%s/accounts/%s/teams", c.UCPURL, org)
b, err := json.Marshal(team)
log.Debugf("%s", string(b))
if err != nil {
return err
}
response, err := c.postRequest(url, b)
if err != nil {
parseerr := ParseUCPError(response)
if err != nil {
log.Errorf("Error parsing UCP error: %v", parseerr)
}
return err
}
log.Debugf("%v", string(response))
return nil
}
//DeleteTeamFromOrganisation - deletes an account in UCP
func (c *Client) DeleteTeamFromOrganisation(team, org string) error {
log.Infof("Deleting team [%s] from org [%s]", team, org)
url := fmt.Sprintf("%s/accounts/%s/teams/%s", c.UCPURL, org, team)
response, err := c.delRequest(url, nil)
if err != nil {
parseerr := ParseUCPError(response)
if parseerr != nil {
log.Errorf("Error parsing UCP error: %v", parseerr)
}
return err
}
return nil
}
//AddUserToTeam - adds a team to an existing organisation
func (c *Client) AddUserToTeam(user, org, team string) error {
log.Infof("Adding user [%s] to team [%s] in organisation [%s]", user, team, org)
url := fmt.Sprintf("%s/accounts/%s/teams/%s/members/%s", c.UCPURL, org, team, user)
// TODO - user can be an admin, but does he need to be a team admin (not sure what it does)
response, err := c.putRequest(url, []byte(`{}`))
if err != nil {
log.Debugf("%v", string(response))
parserr := ParseUCPError(response)
if parserr != nil {
log.Errorf("Error parsing UCP error: %v", err)
}
return err
}
log.Debugf("%v", string(response))
return nil
}
//DelUserFromTeam - deletes a user from a team in an existing organisation
func (c *Client) DelUserFromTeam(user, org, team string) error {
log.Infof("Deleting user [%s] from team [%s] in organisation [%s]", user, team, org)
url := fmt.Sprintf("%s/accounts/%s/teams/%s/members/%s", c.UCPURL, org, team, user)
// TODO - user can be an admin, but does he need to be a team admin (not sure what it does)
response, err := c.delRequest(url, nil)
if err != nil {
if len(response) > 0 {
log.Debugf("%v", string(response))
} else {
return err
}
parseerr := ParseUCPError(response)
if parseerr != nil {
log.Errorf("Error parsing UCP error: %v", parseerr)
}
return err
}
if len(response) > 0 {
log.Debugf("%v", string(response))
}
return nil
}
// AddUserToOrganisation - Adds a user to an existing organisation TODO
func (c *Client) AddUserToOrganisation(user, org string) error {
return nil
}
//ImportAccountsFromCSV - This will import accounts from a CSV file and create/delete them as described in the CSV
func (c *Client) ImportAccountsFromCSV(path string) error {
csvFile, err := os.Open(path)
if err != nil {
return err
}
defer csvFile.Close()
csvLines, err := csv.NewReader(csvFile).ReadAll()
if err != nil {
return err
}
var accounts []ucptypes.Account
var action []int8 // 0 = create, 1 = delete, 2 = update, 3 = ignore
// Parsing from a CSV to a GO struct is a bit messy, using the ParseBool method
log.Infof("Parsing CSV file [%s]", path)
for _, line := range csvLines {
// Check for Header line
if line[1] == "Full Name" && line[2] == "Active account" {
log.Debugf("Ignoring Header")
continue
}
// Parse the action
switch line[0] {
case "create":
log.Debug("Creating a new account")
action = append(action, 0)
case "delete":
log.Debug("Deleting an existing account")
action = append(action, 1)
case "update":
log.Debug("Updating an existing account")
action = append(action, 2)
case "ignore":
log.Debugf("Ignoring the account [%s]", line[5])
action = append(action, 3)
default:
return fmt.Errorf("Unknown action [%s] on account", line[0])
}
var acct ucptypes.Account
acct.FullName = line[1]
// Is Active
var b bool
b, err := strconv.ParseBool(line[2])
if err != nil {
return err
}
acct.IsActive = b
// Is Admin
b, err = strconv.ParseBool(line[3])
if err != nil {
return err
}
acct.IsAdmin = b
// Is Org
b, err = strconv.ParseBool(line[4])
if err != nil {
return err
}
acct.IsOrg = b
// Name string `json:"name"`
acct.Name = line[5]
// Password string `json:"password"`
acct.Password = line[6]
// SearchLDAP bool `json:"searchLDAP"`
b, err = strconv.ParseBool(line[7])
if err != nil {
return err
}
acct.SearchLDAP = b
accounts = append(accounts, acct)
}
log.Debugf("About to add %d accounts", len(accounts))
//TODO - loop through accounts array and add accounts
if len(accounts) != len(action) {
return fmt.Errorf("Actions doesn't match the number of accounts")
}
for i := range accounts {
switch action[i] {
case 0:
c.AddAccount(&accounts[i])
case 1:
c.DeleteAccount(accounts[i].Name)
case 2:
log.Warnf("Not implemented yet") //TODO
case 3:
// Account is ignored
default:
return fmt.Errorf("Unknown action being performed on user [%s]", accounts[i].FullName)
}
}
return nil
}
//ExportAccountsToCSV -
func (c *Client) ExportAccountsToCSV(path string) error {
// Default Query
var accountQuery ucptypes.Account
a, err := c.GetAccounts(accountQuery, -1)
if err != nil {
return err
}
csvFile, err := os.Create(path)
if err != nil {
return err
}
defer csvFile.Close()
writer := csv.NewWriter(csvFile)
defer writer.Flush()
// Write a header to the top of the CSV making it easier to understand
var csvHeader = []string{
"Action",
"Full Name",
"Active account",
"Administrative User",
"Is Organisation",
"ID",
"Password",
"LDAP User"}
writer.Write(csvHeader)
for _, acct := range a.Accounts {
var csvRow = []string{
"ignore", // this will step over the account when parsing the CSV
acct.FullName,
strconv.FormatBool(acct.IsActive),
strconv.FormatBool(acct.IsAdmin),
strconv.FormatBool(acct.IsOrg),
acct.Name,
acct.Password,
strconv.FormatBool(acct.SearchLDAP)}
writer.Write(csvRow)
}
return nil
}
//CreateExampleAccountCSV -
func CreateExampleAccountCSV() error {
path := "example_accounts.csv"
csvFile, err := os.Create(path)
if err != nil {
return err
}
defer csvFile.Close()
writer := csv.NewWriter(csvFile)
defer writer.Flush()
action := "create"
acct := ucptypes.Account{
FullName: "John Doe",
IsActive: true,
IsAdmin: true,
IsOrg: false,
Name: "jdoe",
Password: "lockedDown",
SearchLDAP: false,
}
for i := 1; i <= 100; i++ {
var csvString = []string{
action,
fmt.Sprintf("%s%d", acct.FullName, i),
strconv.FormatBool(acct.IsActive),
strconv.FormatBool(acct.IsAdmin),
strconv.FormatBool(acct.IsOrg),
fmt.Sprintf("%s%d", acct.Name, i),
acct.Password,
strconv.FormatBool(acct.SearchLDAP),
}
err = writer.Write(csvString)
if err != nil {
return err
}
}
return nil
}
// GetAccounts - This will get all accounts
func (c *Client) GetAccounts(query ucptypes.Account, count int) (*ucptypes.AccountList, error) {
// Build the URL (TODO set limit)
var url string
if count > 0 {
url = fmt.Sprintf("%s/accounts/?limit=%d", c.UCPURL, count)
} else {
// DEFAULT to 1000 records
url = fmt.Sprintf("%s/accounts/?limit=1000", c.UCPURL)
}
if query.IsOrg == true {
log.Debugln("Retrieving Organisations")
url = url + "&filter=orgs"
}
if query.IsAdmin == true {
log.Debugln("Retrieving Admins")
url = url + "&filter=admins"
}
if query.IsActive == true {
log.Debugln("Retrieving InActive users")
url = url + "&filter=inactive"
}
log.Debugf("Built URL [%s]", url)
response, err := c.getRequest(url, nil)
if err != nil {
return nil, fmt.Errorf("%s", string(response))
}
var a ucptypes.AccountList
err = json.Unmarshal(response, &a)
if err != nil {
return nil, err
}
return &a, nil
}