From 2e6ef8f0bb597e1ee3b7d608b502e9d53a2964c0 Mon Sep 17 00:00:00 2001 From: Patrik Segedy Date: Thu, 11 Jul 2024 09:20:29 +0200 Subject: [PATCH] RHINENG-11257: fix attributeFilter parsing for string value --- base/rbac/rbac.go | 44 ++++++++++++++++++++++++++-- base/rbac/rbac_test.go | 65 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 base/rbac/rbac_test.go diff --git a/base/rbac/rbac.go b/base/rbac/rbac.go index 86ccea1a8..2f571fb38 100644 --- a/base/rbac/rbac.go +++ b/base/rbac/rbac.go @@ -1,5 +1,10 @@ package rbac +import ( + "encoding/json" + "strings" +) + type AccessPagination struct { Data []Access `json:"data"` } @@ -13,9 +18,11 @@ type ResourceDefinition struct { AttributeFilter AttributeFilter `json:"attributeFilter,omitempty"` } +type AttributeFilterValue []*string + type AttributeFilter struct { - Key string `json:"key"` - Value []*string `json:"value"` + Key string `json:"key"` + Value AttributeFilterValue `json:"value"` } type inventoryGroup struct { @@ -24,3 +31,36 @@ type inventoryGroup struct { } type InventoryGroup []inventoryGroup + +func (a *AttributeFilterValue) UnmarshalJSON(data []byte) error { + var ( + array []*string + value *string + err error + ) + + if err = json.Unmarshal(data, &array); err != nil { + // parsing of AttributeFilter Value into []*string failed + // try to parse it as *string + if err = json.Unmarshal(data, &value); err != nil { + // fail, the value is neither []*string nor *string + return err + } + if value != nil { + for _, v := range strings.Split(*value, ",") { + // split values by `,` in case it is comma separated string + // and not just a single value in string + v := v + array = append(array, &v) + } + } + } + if array == nil && value == nil { + // in this case we got `"value": null` + // we should apply the permission to systems with no inventory groups + array = append(array, value) + } + + *a = array + return nil +} diff --git a/base/rbac/rbac_test.go b/base/rbac/rbac_test.go new file mode 100644 index 000000000..6c839b9aa --- /dev/null +++ b/base/rbac/rbac_test.go @@ -0,0 +1,65 @@ +package rbac + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestParsing(t *testing.T) { + data := []byte(` + { + "resourceDefinitions": [ + {"attributeFilter": { + "key": "single_string", + "value": "string" + }}, + {"attributeFilter": { + "key": "comma_separated", + "value": "comma,separated" + }}, + {"attributeFilter": { + "key": "null", + "value": null + }}, + {"attributeFilter": { + "key": "string_array", + "value": ["string", "array"] + }}, + {"attributeFilter": { + "key": "string_array_with_null", + "value": ["string", "array", null] + }}, + {"attributeFilter": { + "key": "null_array", + "value": [null] + }}, + {"attributeFilter": { + "key": "empty_array", + "value": [] + }} + ] + } + `) + stringS := "string" + commaS := "comma" + separatedS := "separated" + arrayS := "array" + + expected := []ResourceDefinition{ + {AttributeFilter: AttributeFilter{Key: "single_string", Value: []*string{&stringS}}}, + {AttributeFilter: AttributeFilter{Key: "comma_separated", Value: []*string{&commaS, &separatedS}}}, + {AttributeFilter: AttributeFilter{Key: "null", Value: []*string{nil}}}, + {AttributeFilter: AttributeFilter{Key: "string_array", Value: []*string{&stringS, &arrayS}}}, + {AttributeFilter: AttributeFilter{Key: "string_array_with_null", Value: []*string{&stringS, &arrayS, nil}}}, + {AttributeFilter: AttributeFilter{Key: "null_array", Value: []*string{nil}}}, + {AttributeFilter: AttributeFilter{Key: "empty_array", Value: []*string{}}}, + } + + var v Access + err := json.Unmarshal(data, &v) + if assert.NoError(t, err) { + assert.Equal(t, expected, v.ResourceDefinitions) + } +}