forked from observatorium/api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes authorization flow for /api/logs/v1/{tenant}/rules (observatori…
- Loading branch information
1 parent
5951750
commit 08a17ed
Showing
4 changed files
with
85 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package authorization | ||
|
||
import ( | ||
"net/url" | ||
) | ||
|
||
func parseLogRulesSelectors(selectorNames map[string]bool, queryParameter url.Values) map[string][]string { | ||
selectors := make(map[string][]string) | ||
appendSelector := func(selector, value string) { | ||
values, ok := selectors[selector] | ||
if !ok { | ||
values = make([]string, 0) | ||
} | ||
|
||
values = append(values, value) | ||
selectors[selector] = values | ||
} | ||
|
||
for selector := range selectorNames { | ||
values := queryParameter[selector] | ||
for _, value := range values { | ||
appendSelector(selector, value) | ||
} | ||
} | ||
|
||
return selectors | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package authorization | ||
|
||
import ( | ||
"net/url" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/efficientgo/core/testutil" | ||
) | ||
|
||
func Test_parseQueryParametersSelectors(t *testing.T) { | ||
testSelectorLabels := map[string]bool{ | ||
"namespace": true, | ||
"other_namespace_label": true, | ||
} | ||
tests := []struct { | ||
queryParameters string | ||
wantSelectors map[string][]string | ||
}{ | ||
{ | ||
queryParameters: `namespace=test`, | ||
wantSelectors: map[string][]string{ | ||
"namespace": {"test"}, | ||
}, | ||
}, | ||
{ | ||
queryParameters: `namespace=test&other_namespace_label=test2`, | ||
wantSelectors: map[string][]string{ | ||
"namespace": {"test"}, | ||
"other_namespace_label": {"test2"}, | ||
}, | ||
}, | ||
{ | ||
queryParameters: `namespace=test&namespace=test2`, | ||
wantSelectors: map[string][]string{ | ||
"namespace": {"test", "test2"}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.queryParameters, func(t *testing.T) { | ||
queryValues, err := url.ParseQuery(tt.queryParameters) | ||
testutil.Ok(t, err) | ||
|
||
gotNamespaces := parseLogRulesSelectors(testSelectorLabels, queryValues) | ||
if !reflect.DeepEqual(gotNamespaces, tt.wantSelectors) { | ||
t.Errorf("parseLogStreamSelectors() got = %v, want %v", gotNamespaces, tt.wantSelectors) | ||
} | ||
}) | ||
} | ||
} |