Config: Support JSON list syntax (#61288)

* Config: Separate lists either by spaces or by commas.

* Simplify space separation

* use separate function for the config strings

* Change behavior only if string contains quotes

* add test for invalid string

* Use JSON list syntax

* ignore leading spaces when process list

* Add notes about using JSON lists into the docs

* Fix typo

* Apply suggestions from code review

Co-authored-by: Christopher Moyer <35463610+chri2547@users.noreply.github.com>

Co-authored-by: Christopher Moyer <35463610+chri2547@users.noreply.github.com>
This commit is contained in:
Alexander Zobnin 2023-01-16 14:03:20 +01:00 committed by GitHub
parent 045a12047f
commit 997105c20d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 6 deletions

View File

@ -110,6 +110,12 @@ the group `foo`, set
allowed_groups = example, foo/bar
```
To put values containing spaces in the list, use the following JSON syntax:
```ini
allowed_groups = ["Admins", "Software Engineers"]
```
Note that in GitLab, the group or subgroup name doesn't always match its
display name, especially if the display name contains spaces or special
characters. Make sure you always use the group or subgroup name as it appears

View File

@ -81,6 +81,12 @@ The `allowed_domains` option limits access to the users belonging to the specifi
allowed_domains = mycompany.com mycompany.org
```
To put values containing spaces in the list, use the following JSON syntax:
```ini
allowed_groups = ["Admins", "Software Engineers"]
```
### Map roles
Grafana can attempt to do role mapping through Okta OAuth. In order to achieve this, Grafana checks for the presence of a role using the [JMESPath](http://jmespath.org/examples.html) specified via the `role_attribute_path` configuration option.

View File

@ -390,6 +390,12 @@ You can use `*` as the Grafana organization in the mapping if you want all users
With the [`allowed_organizations`]({{< relref "../../../configure-grafana/enterprise-configuration/#allowed-organizations" >}}) option you can specify a list of organizations where the user must be a member of at least one of them to be able to log in to Grafana.
To put values containing spaces in the list, use the following JSON syntax:
```ini
allowed_organizations = ["org 1", "second org"]
```
### Example SAML configuration
```bash

View File

@ -1,6 +1,7 @@
package util
import (
"encoding/json"
"fmt"
"math"
"strings"
@ -33,6 +34,16 @@ func SplitString(str string) []string {
return []string{}
}
// JSON list syntax support
if strings.Index(strings.TrimSpace(str), "[") == 0 {
var res []string
err := json.Unmarshal([]byte(str), &res)
if err != nil {
return []string{}
}
return res
}
return strings.Fields(strings.ReplaceAll(str, ",", " "))
}

View File

@ -46,12 +46,17 @@ func TestStringsFallback3(t *testing.T) {
func TestSplitString(t *testing.T) {
tests := map[string][]string{
"": {},
"test": {"test"},
"test1 test2 test3": {"test1", "test2", "test3"},
"test1,test2,test3": {"test1", "test2", "test3"},
"test1, test2, test3": {"test1", "test2", "test3"},
"test1 , test2 test3": {"test1", "test2", "test3"},
"": {},
"test": {"test"},
" test1 test2 test3": {"test1", "test2", "test3"},
"test1,test2,test3": {"test1", "test2", "test3"},
"test1, test2, test3": {"test1", "test2", "test3"},
"test1 , test2 test3": {"test1", "test2", "test3"},
"foo, bar baz": {"foo", "bar", "baz"},
`["foo", "bar baz"]`: {"foo", "bar baz"},
`["foo", "bar \"baz\""]`: {"foo", "bar \"baz\""},
` ["foo", "bar baz"]`: {"foo", "bar baz"},
`[]`: {},
}
for input, expected := range tests {
assert.EqualValues(t, expected, SplitString(input))