1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-22 06:49:40 +02:00

fix(uac): ignore duplicates, spaces and casing in portainer labels (#4823)

* fix: ignore duplicates, spaces and casing in portainer labels

* cleanup

* fix: rebase error
This commit is contained in:
Dmitry Salakhov 2021-03-03 09:38:59 +00:00 committed by GitHub
parent 6c8276c65c
commit f03cf2a6e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 90 additions and 7 deletions

View file

@ -31,6 +31,23 @@ type (
}
)
func getUniqueElements(items string) []string {
result := []string{}
seen := make(map[string]struct{})
for _, item := range strings.Split(items, ",") {
v := strings.TrimSpace(item)
if v == "" {
continue
}
if _, ok := seen[v]; !ok {
result = append(result, v)
seen[v] = struct{}{}
}
}
return result
}
func (transport *Transport) newResourceControlFromPortainerLabels(labelsObject map[string]interface{}, resourceID string, resourceType portainer.ResourceControlType) (*portainer.ResourceControl, error) {
if labelsObject[resourceLabelForPortainerPublicResourceControl] != nil {
resourceControl := authorization.NewPublicResourceControl(resourceID, resourceType)
@ -47,12 +64,12 @@ func (transport *Transport) newResourceControlFromPortainerLabels(labelsObject m
userNames := make([]string, 0)
if labelsObject[resourceLabelForPortainerTeamResourceControl] != nil {
concatenatedTeamNames := labelsObject[resourceLabelForPortainerTeamResourceControl].(string)
teamNames = strings.Split(concatenatedTeamNames, ",")
teamNames = getUniqueElements(concatenatedTeamNames)
}
if labelsObject[resourceLabelForPortainerUserResourceControl] != nil {
concatenatedUserNames := labelsObject[resourceLabelForPortainerUserResourceControl].(string)
userNames = strings.Split(concatenatedUserNames, ",")
userNames = getUniqueElements(concatenatedUserNames)
}
if len(teamNames) > 0 || len(userNames) > 0 {

View file

@ -0,0 +1,63 @@
package docker
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_getUniqueElements(t *testing.T) {
cases := []struct {
description string
input string
expected []string
}{
{
description: "no items padded",
input: " ",
expected: []string{},
},
{
description: "single item",
input: "a",
expected: []string{"a"},
},
{
description: "single item padded",
input: " a ",
expected: []string{"a"},
},
{
description: "multiple items",
input: "a,b",
expected: []string{"a", "b"},
},
{
description: "multiple items padded",
input: " a , b ",
expected: []string{"a", "b"},
},
{
description: "multiple items with empty values",
input: " a , ,b ",
expected: []string{"a", "b"},
},
{
description: "duplicates",
input: " a , a ",
expected: []string{"a"},
},
{
description: "mix with duplicates",
input: " a ,b, a ",
expected: []string{"a", "b"},
},
}
for _, tt := range cases {
t.Run(tt.description, func(t *testing.T) {
result := getUniqueElements(tt.input)
assert.ElementsMatch(t, result, tt.expected)
})
}
}