Skip to content

Commit 67c1569

Browse files
committed
remove group permission checking
1 parent c5ba79b commit 67c1569

File tree

1 file changed

+28
-49
lines changed

1 file changed

+28
-49
lines changed

internal/pkg/rpa/main.go

+28-49
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/tls"
66
"encoding/base64"
77
"encoding/json"
8+
"errors"
89
"fmt"
910
"io"
1011
"io/ioutil"
@@ -57,35 +58,6 @@ func (a *App) apiRequest(method, url string, data io.Reader) ([]byte, int) {
5758
return body, resp.StatusCode
5859
}
5960

60-
// getUserGroups retrieves the groups of which the current user has rights to administer
61-
func (a *App) getUserGroups() []GroupUID {
62-
endpoint := a.Config.RPAURL + "/fapi/rest/5_1/users/settings/"
63-
body, _ := a.apiRequest("GET", endpoint, nil)
64-
var usr UsersSettingsResponse
65-
json.Unmarshal(body, &usr)
66-
67-
var allowedGroups []GroupUID
68-
for _, u := range usr.Users {
69-
if u.Name == a.Config.Username {
70-
allowedGroups = u.Groups
71-
}
72-
}
73-
return allowedGroups
74-
}
75-
76-
// groupInGroups returns true if a group UID exists in a slice of GroupUID
77-
func (a *App) groupInGroups(groupID int, usersGroups []GroupUID) bool {
78-
if usersGroups == nil {
79-
usersGroups = a.getUserGroups()
80-
}
81-
for _, g := range usersGroups {
82-
if g.ID == groupID {
83-
return true
84-
}
85-
}
86-
return false
87-
}
88-
8961
func (a *App) getAllGroups() []GroupUID {
9062
endpoint := a.Config.RPAURL + "/fapi/rest/5_1/groups/"
9163
body, _ := a.apiRequest("GET", endpoint, nil)
@@ -249,7 +221,7 @@ func (a *App) startTransfer(t Task) {
249221
fmt.Printf("Starting Transfer for Group %s Copy %s\n", t.GroupName, t.CopyName)
250222
}
251223

252-
func (a *App) imageAccess(t Task) {
224+
func (a *App) imageAccess(t Task) error {
253225
operationName := "Disabling"
254226
operation := "disable_image_access"
255227
if t.Enable == true {
@@ -270,13 +242,14 @@ func (a *App) imageAccess(t Task) {
270242
}
271243

272244
if !a.Config.CheckMode {
273-
_, statusCode := a.apiRequest("PUT", endpoint, bytes.NewBuffer(json))
245+
body, statusCode := a.apiRequest("PUT", endpoint, bytes.NewBuffer(json))
274246
if statusCode != 204 {
275-
log.Errorf("Expected status code '204' and received: %d\n", statusCode)
276-
log.Fatalf("Error %s Latest Image for Group %s Copy %s\n", operationName, t.GroupName, t.CopyName)
247+
log.Debugf("Expected status code '204' and received: %d\n", statusCode)
248+
return errors.New(string(body))
277249
}
278250
}
279251
fmt.Printf("%s Latest Image for Group %s Copy %s\n", operationName, t.GroupName, t.CopyName)
252+
return nil
280253
}
281254

282255
func (a *App) pollImageAccessEnabled(groupID int, stateDesired bool) {
@@ -320,7 +293,7 @@ func (a *App) directAccess(t Task) {
320293

321294
// EnableAll wraper for enabling Direct Image Access for all CG
322295
func (a *App) EnableAll() {
323-
groups := a.getUserGroups() // only groups user has permission to admin
296+
groups := a.getAllGroups() // only groups user has permission to admin
324297
for _, g := range groups {
325298
var t Task
326299
GroupName := a.getGroupName(g.ID)
@@ -338,7 +311,11 @@ func (a *App) EnableAll() {
338311
t.CopyUID = copySettings.CopyUID.GlobalCopyUID.CopyUID
339312
t.Enable = true // whether to enable or disable the following tasks
340313
if !a.Config.CheckMode {
341-
a.imageAccess(t)
314+
err := a.imageAccess(t)
315+
if err != nil {
316+
log.Warnf("%s %s\n", GroupName, err)
317+
continue
318+
}
342319
a.pollImageAccessEnabled(g.ID, true)
343320
a.directAccess(t)
344321
}
@@ -349,11 +326,6 @@ func (a *App) EnableAll() {
349326
// EnableOne wraper for enabling Direct Image Access for a single CG
350327
func (a *App) EnableOne() {
351328
groupID := a.getGroupIDByName(a.Group)
352-
usersGroups := a.getUserGroups()
353-
if a.groupInGroups(groupID, usersGroups) == false {
354-
log.Error("User does not have sufficient access to administer ", a.Group)
355-
return
356-
}
357329
var t Task
358330
groupCopiesSettings := a.getGroupCopiesSettings(groupID)
359331
copySettings := a.getRequestedCopy(groupCopiesSettings)
@@ -369,15 +341,19 @@ func (a *App) EnableOne() {
369341
t.CopyUID = copySettings.CopyUID.GlobalCopyUID.CopyUID
370342
t.Enable = true // whether to enable or disable the following tasks
371343
if !a.Config.CheckMode {
372-
a.imageAccess(t)
344+
err := a.imageAccess(t)
345+
if err != nil {
346+
log.Warnf("%s %s\n", a.Group, err)
347+
return
348+
}
373349
a.pollImageAccessEnabled(groupID, true)
374350
a.directAccess(t)
375351
}
376352
}
377353

378354
// FinishAll wraper for finishing Direct Image Access for all CG
379355
func (a *App) FinishAll() {
380-
groups := a.getUserGroups() // only groups user has permission to admin
356+
groups := a.getAllGroups() // only groups user has permission to admin
381357
for _, g := range groups {
382358
var t Task
383359
GroupName := a.getGroupName(g.ID)
@@ -390,7 +366,11 @@ func (a *App) FinishAll() {
390366
t.CopyUID = copySettings.CopyUID.GlobalCopyUID.CopyUID
391367
t.Enable = false // whether to enable or disable the following tasks
392368
if !a.Config.CheckMode {
393-
a.imageAccess(t)
369+
err := a.imageAccess(t)
370+
if err != nil {
371+
log.Warnf("%s %s\n", GroupName, err)
372+
continue
373+
}
394374
a.pollImageAccessEnabled(g.ID, false)
395375
a.startTransfer(t)
396376
}
@@ -401,11 +381,6 @@ func (a *App) FinishAll() {
401381
// FinishOne wraper for finishing Direct Image Access for a single CG
402382
func (a *App) FinishOne() {
403383
groupID := a.getGroupIDByName(a.Group)
404-
usersGroups := a.getUserGroups()
405-
if a.groupInGroups(groupID, usersGroups) == false {
406-
log.Error("User does not have sufficient access to administer ", a.Group)
407-
return
408-
}
409384
var t Task
410385
groupCopiesSettings := a.getGroupCopiesSettings(groupID)
411386
copySettings := a.getRequestedCopy(groupCopiesSettings)
@@ -416,7 +391,11 @@ func (a *App) FinishOne() {
416391
t.CopyUID = copySettings.CopyUID.GlobalCopyUID.CopyUID
417392
t.Enable = false // whether to enable or disable the following tasks
418393
if !a.Config.CheckMode {
419-
a.imageAccess(t)
394+
err := a.imageAccess(t)
395+
if err != nil {
396+
log.Warnf("%s %s\n", a.Group, err)
397+
return
398+
}
420399
a.pollImageAccessEnabled(groupID, false)
421400
a.startTransfer(t)
422401
}

0 commit comments

Comments
 (0)