Skip to content

Commit cb2d3e3

Browse files
committed
RHINENG-18385: allow 'equal' opration in resourceDefinitions
1 parent 4261be9 commit cb2d3e3

2 files changed

Lines changed: 103 additions & 3 deletions

File tree

manager/middlewares/rbac.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,7 @@ func findInventoryGroups(access *rbac.AccessPagination) (map[string]string, erro
161161
continue
162162
}
163163

164-
// https://github.com/RedHatInsights/insights-host-inventory/
165-
// blob/a7c8a7c980012c89e18ec0f7074609e216b37a8d/lib/middleware.py#L124
166-
if rd.AttributeFilter.Operation != "in" {
164+
if rd.AttributeFilter.Operation != "in" && rd.AttributeFilter.Operation != "equal" {
167165
err := fmt.Errorf(
168166
"invalid value '%s' for attributeFilter.Operation",
169167
rd.AttributeFilter.Operation,

manager/middlewares/rbac_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package middlewares
33
import (
44
"app/base/rbac"
55
"app/base/utils"
6+
"encoding/json"
67
"net/http"
78
"net/http/httptest"
89
"testing"
@@ -412,3 +413,104 @@ func TestMultiplePermissions(t *testing.T) {
412413
assert.True(t, checkPermissions(&access, handler, "GET"))
413414
assert.False(t, checkPermissions(&access, handler, "DELETE"))
414415
}
416+
417+
func TestPermissionsAllowedOperations(t *testing.T) {
418+
handler := "SystemsListHandler"
419+
access := rbac.AccessPagination{}
420+
data := `{"data": [
421+
{
422+
"resourceDefinitions": [],
423+
"permission": "patch:*:read"
424+
},
425+
{
426+
"resourceDefinitions": [
427+
{
428+
"attributeFilter": {
429+
"key": "group.id",
430+
"value": "00000000-f688-49d4-a8e2-87394f1ac1b1",
431+
"operation": "equal"
432+
}
433+
}
434+
],
435+
"permission": "inventory:hosts:read"
436+
},
437+
{
438+
"resourceDefinitions": [
439+
{
440+
"attributeFilter": {
441+
"key": "group.id",
442+
"value": [ "00000000-f7a6-45a1-b5a8-410f20052fb1", "00000000-78e0-4cad-bf01-63cf1e4b1dca" ],
443+
"operation": "in"
444+
}
445+
}
446+
],
447+
"permission": "inventory:hosts:read"
448+
},
449+
{
450+
"resourceDefinitions": [
451+
{
452+
"attributeFilter": {
453+
"key": "group.id",
454+
"value": [ "00000000-f688-49d4-a8e2-ee394f1ac1b1" ],
455+
"operation": "in"
456+
}
457+
}
458+
],
459+
"permission": "inventory:hosts:read"
460+
},
461+
{
462+
"resourceDefinitions": [
463+
{
464+
"attributeFilter": {
465+
"key": "group.id",
466+
"value": null,
467+
"operation": "equal"
468+
}
469+
}
470+
],
471+
"permission": "inventory:hosts:read"
472+
}
473+
]
474+
}
475+
`
476+
err := json.Unmarshal([]byte(data), &access)
477+
assert.NoError(t, err)
478+
assert.True(t, checkPermissions(&access, handler, "GET"))
479+
groups, err := findInventoryGroups(&access)
480+
assert.NoError(t, err)
481+
assert.Equal(t, "[]", groups["ungrouped"])
482+
assert.Equal(t, `{"[{\"id\":\"00000000-f688-49d4-a8e2-87394f1ac1b1\"}]",`+
483+
`"[{\"id\":\"00000000-f7a6-45a1-b5a8-410f20052fb1\"}]",`+
484+
`"[{\"id\":\"00000000-78e0-4cad-bf01-63cf1e4b1dca\"}]",`+
485+
`"[{\"id\":\"00000000-f688-49d4-a8e2-ee394f1ac1b1\"}]"}`, groups["grouped"])
486+
}
487+
488+
func TestPermissionsUnknownOperation(t *testing.T) {
489+
handler := "SystemsListHandler"
490+
access := rbac.AccessPagination{}
491+
data := `{"data": [
492+
{
493+
"resourceDefinitions": [],
494+
"permission": "patch:*:read"
495+
},
496+
{
497+
"resourceDefinitions": [
498+
{
499+
"attributeFilter": {
500+
"key": "group.id",
501+
"value": "00000000-f688-49d4-a8e2-87394f1ac1b1",
502+
"operation": "not_in"
503+
}
504+
}
505+
],
506+
"permission": "inventory:hosts:read"
507+
}
508+
]
509+
}
510+
`
511+
err := json.Unmarshal([]byte(data), &access)
512+
assert.NoError(t, err)
513+
assert.True(t, checkPermissions(&access, handler, "GET"))
514+
_, err = findInventoryGroups(&access)
515+
assert.EqualError(t, err, "invalid value 'not_in' for attributeFilter.Operation")
516+
}

0 commit comments

Comments
 (0)