2020-05-05 07:53:42 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-03-05 16:21:11 +00:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2021-04-21 07:55:29 +00:00
|
|
|
"net/url"
|
2021-03-05 16:21:11 +00:00
|
|
|
"regexp"
|
2020-05-05 07:53:42 +00:00
|
|
|
"testing"
|
2021-03-05 16:21:11 +00:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
2020-05-05 07:53:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseAuthConfigFailure(t *testing.T) {
|
|
|
|
f := func(s string) {
|
|
|
|
t.Helper()
|
2023-04-20 17:08:27 +00:00
|
|
|
ac, err := parseAuthConfig([]byte(s))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = parseAuthConfigUsers(ac)
|
2020-05-05 07:53:42 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expecting non-nil error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Empty config
|
|
|
|
f(``)
|
|
|
|
|
|
|
|
// Invalid entry
|
|
|
|
f(`foobar`)
|
|
|
|
f(`foobar: baz`)
|
|
|
|
|
|
|
|
// Empty users
|
|
|
|
f(`users: []`)
|
|
|
|
|
|
|
|
// Missing url_prefix
|
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- username: foo
|
|
|
|
`)
|
|
|
|
|
|
|
|
// Invalid url_prefix
|
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- username: foo
|
|
|
|
url_prefix: bar
|
|
|
|
`)
|
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- username: foo
|
|
|
|
url_prefix: ftp://bar
|
|
|
|
`)
|
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- username: foo
|
|
|
|
url_prefix: //bar
|
|
|
|
`)
|
2021-02-11 10:40:59 +00:00
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- username: foo
|
|
|
|
url_prefix: http:///bar
|
|
|
|
`)
|
2021-04-21 07:55:29 +00:00
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- username: foo
|
2021-05-28 22:00:23 +00:00
|
|
|
url_prefix:
|
|
|
|
bar: baz
|
|
|
|
`)
|
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- username: foo
|
|
|
|
url_prefix:
|
|
|
|
- [foo]
|
|
|
|
`)
|
|
|
|
|
2021-10-22 16:08:06 +00:00
|
|
|
// Invalid headers
|
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- username: foo
|
|
|
|
url_prefix: http://foo.bar
|
|
|
|
headers: foobar
|
|
|
|
`)
|
|
|
|
|
2021-05-28 22:00:23 +00:00
|
|
|
// empty url_prefix
|
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- username: foo
|
|
|
|
url_prefix: []
|
2021-04-21 07:55:29 +00:00
|
|
|
`)
|
2020-05-05 07:53:42 +00:00
|
|
|
|
2021-04-02 19:14:53 +00:00
|
|
|
// Username and bearer_token in a single config
|
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- username: foo
|
|
|
|
bearer_token: bbb
|
|
|
|
url_prefix: http://foo.bar
|
|
|
|
`)
|
|
|
|
|
|
|
|
// Bearer_token and password in a single config
|
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- password: foo
|
|
|
|
bearer_token: bbb
|
|
|
|
url_prefix: http://foo.bar
|
|
|
|
`)
|
|
|
|
|
2020-05-05 07:53:42 +00:00
|
|
|
// Duplicate users
|
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- username: foo
|
|
|
|
url_prefix: http://foo.bar
|
|
|
|
- username: bar
|
|
|
|
url_prefix: http://xxx.yyy
|
|
|
|
- username: foo
|
|
|
|
url_prefix: https://sss.sss
|
|
|
|
`)
|
2022-07-19 16:33:17 +00:00
|
|
|
// Duplicate users
|
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- username: foo
|
|
|
|
password: bar
|
|
|
|
url_prefix: http://foo.bar
|
|
|
|
- username: bar
|
|
|
|
url_prefix: http://xxx.yyy
|
|
|
|
- username: foo
|
|
|
|
password: bar
|
|
|
|
url_prefix: https://sss.sss
|
|
|
|
`)
|
2021-02-11 10:40:59 +00:00
|
|
|
|
2021-04-02 19:14:53 +00:00
|
|
|
// Duplicate bearer_tokens
|
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- bearer_token: foo
|
|
|
|
url_prefix: http://foo.bar
|
|
|
|
- username: bar
|
|
|
|
url_prefix: http://xxx.yyy
|
|
|
|
- bearer_token: foo
|
|
|
|
url_prefix: https://sss.sss
|
|
|
|
`)
|
|
|
|
|
2021-02-11 10:40:59 +00:00
|
|
|
// Missing url_prefix in url_map
|
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- username: a
|
|
|
|
url_map:
|
|
|
|
- src_paths: ["/foo/bar"]
|
|
|
|
`)
|
2023-12-13 22:46:36 +00:00
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- username: a
|
|
|
|
url_map:
|
|
|
|
- src_hosts: ["foobar"]
|
|
|
|
`)
|
2021-02-11 10:40:59 +00:00
|
|
|
|
2021-04-21 07:55:29 +00:00
|
|
|
// Invalid url_prefix in url_map
|
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- username: a
|
|
|
|
url_map:
|
|
|
|
- src_paths: ["/foo/bar"]
|
|
|
|
url_prefix: foo.bar
|
|
|
|
`)
|
2023-12-13 22:46:36 +00:00
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- username: a
|
|
|
|
url_map:
|
|
|
|
- src_hosts: ["foobar"]
|
|
|
|
url_prefix: foo.bar
|
|
|
|
`)
|
2021-04-21 07:55:29 +00:00
|
|
|
|
2021-05-28 22:00:23 +00:00
|
|
|
// empty url_prefix in url_map
|
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- username: a
|
|
|
|
url_map:
|
|
|
|
- src_paths: ['/foo/bar']
|
|
|
|
url_prefix: []
|
|
|
|
`)
|
2023-12-13 22:46:36 +00:00
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- username: a
|
|
|
|
url_map:
|
|
|
|
- src_phosts: ['foobar']
|
|
|
|
url_prefix: []
|
|
|
|
`)
|
2021-05-28 22:00:23 +00:00
|
|
|
|
2023-12-13 22:46:36 +00:00
|
|
|
// Missing src_paths and src_hosts in url_map
|
2021-02-11 10:40:59 +00:00
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- username: a
|
|
|
|
url_map:
|
|
|
|
- url_prefix: http://foobar
|
|
|
|
`)
|
|
|
|
|
2021-03-05 16:21:11 +00:00
|
|
|
// Invalid regexp in src_path.
|
2021-02-11 10:40:59 +00:00
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- username: a
|
|
|
|
url_map:
|
2021-03-05 16:21:11 +00:00
|
|
|
- src_paths: ['fo[obar']
|
2021-02-11 10:40:59 +00:00
|
|
|
url_prefix: http://foobar
|
|
|
|
`)
|
2021-10-22 16:08:06 +00:00
|
|
|
|
2023-12-13 22:46:36 +00:00
|
|
|
// Invalid regexp in src_hosts
|
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- username: a
|
|
|
|
url_map:
|
|
|
|
- src_hosts: ['fo[obar']
|
|
|
|
url_prefix: http://foobar
|
|
|
|
`)
|
|
|
|
|
2021-10-22 16:08:06 +00:00
|
|
|
// Invalid headers in url_map (missing ':')
|
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- username: a
|
|
|
|
url_map:
|
|
|
|
- src_paths: ['/foobar']
|
|
|
|
url_prefix: http://foobar
|
|
|
|
headers:
|
|
|
|
- foobar
|
|
|
|
`)
|
|
|
|
// Invalid headers in url_map (dictionary instead of array)
|
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- username: a
|
|
|
|
url_map:
|
|
|
|
- src_paths: ['/foobar']
|
|
|
|
url_prefix: http://foobar
|
|
|
|
headers:
|
|
|
|
aaa: bbb
|
|
|
|
`)
|
2020-05-05 07:53:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAuthConfigSuccess(t *testing.T) {
|
|
|
|
f := func(s string, expectedAuthConfig map[string]*UserInfo) {
|
|
|
|
t.Helper()
|
2023-04-20 17:08:27 +00:00
|
|
|
ac, err := parseAuthConfig([]byte(s))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
m, err := parseAuthConfigUsers(ac)
|
2020-05-05 07:53:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
removeMetrics(m)
|
2021-03-05 16:21:11 +00:00
|
|
|
if err := areEqualConfigs(m, expectedAuthConfig); err != nil {
|
|
|
|
t.Fatal(err)
|
2020-05-05 07:53:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Single user
|
2023-11-13 07:23:35 +00:00
|
|
|
insecureSkipVerifyTrue := true
|
2020-05-05 07:53:42 +00:00
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- username: foo
|
|
|
|
password: bar
|
|
|
|
url_prefix: http://aaa:343/bbb
|
2023-02-10 04:03:01 +00:00
|
|
|
max_concurrent_requests: 5
|
2023-11-03 11:04:17 +00:00
|
|
|
tls_insecure_skip_verify: true
|
2020-05-05 07:53:42 +00:00
|
|
|
`, map[string]*UserInfo{
|
2021-04-02 19:14:53 +00:00
|
|
|
getAuthToken("", "foo", "bar"): {
|
2023-02-10 04:03:01 +00:00
|
|
|
Username: "foo",
|
|
|
|
Password: "bar",
|
|
|
|
URLPrefix: mustParseURL("http://aaa:343/bbb"),
|
|
|
|
MaxConcurrentRequests: 5,
|
2023-11-13 07:23:35 +00:00
|
|
|
TLSInsecureSkipVerify: &insecureSkipVerifyTrue,
|
2020-05-05 07:53:42 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2021-05-28 22:00:23 +00:00
|
|
|
// Multiple url_prefix entries
|
2023-11-13 07:23:35 +00:00
|
|
|
insecureSkipVerifyFalse := false
|
2021-05-28 22:00:23 +00:00
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- username: foo
|
|
|
|
password: bar
|
|
|
|
url_prefix:
|
|
|
|
- http://node1:343/bbb
|
|
|
|
- http://node2:343/bbb
|
2023-11-13 07:23:35 +00:00
|
|
|
tls_insecure_skip_verify: false
|
2023-11-13 21:30:39 +00:00
|
|
|
retry_status_codes: [500, 501]
|
2023-12-08 21:27:53 +00:00
|
|
|
load_balancing_policy: first_available
|
2023-11-13 21:30:39 +00:00
|
|
|
drop_src_path_prefix_parts: 1
|
2021-05-28 22:00:23 +00:00
|
|
|
`, map[string]*UserInfo{
|
|
|
|
getAuthToken("", "foo", "bar"): {
|
|
|
|
Username: "foo",
|
|
|
|
Password: "bar",
|
|
|
|
URLPrefix: mustParseURLs([]string{
|
|
|
|
"http://node1:343/bbb",
|
|
|
|
"http://node2:343/bbb",
|
|
|
|
}),
|
2023-11-13 21:30:39 +00:00
|
|
|
TLSInsecureSkipVerify: &insecureSkipVerifyFalse,
|
|
|
|
RetryStatusCodes: []int{500, 501},
|
2023-12-08 21:27:53 +00:00
|
|
|
LoadBalancingPolicy: "first_available",
|
2023-11-13 21:30:39 +00:00
|
|
|
DropSrcPathPrefixParts: 1,
|
2021-05-28 22:00:23 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2020-05-05 07:53:42 +00:00
|
|
|
// Multiple users
|
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- username: foo
|
|
|
|
url_prefix: http://foo
|
|
|
|
- username: bar
|
|
|
|
url_prefix: https://bar/x///
|
|
|
|
`, map[string]*UserInfo{
|
2021-04-02 19:14:53 +00:00
|
|
|
getAuthToken("", "foo", ""): {
|
2020-05-05 07:53:42 +00:00
|
|
|
Username: "foo",
|
2021-04-21 07:55:29 +00:00
|
|
|
URLPrefix: mustParseURL("http://foo"),
|
2020-05-05 07:53:42 +00:00
|
|
|
},
|
2021-04-02 19:14:53 +00:00
|
|
|
getAuthToken("", "bar", ""): {
|
2020-05-05 07:53:42 +00:00
|
|
|
Username: "bar",
|
2021-04-21 07:55:29 +00:00
|
|
|
URLPrefix: mustParseURL("https://bar/x"),
|
2020-05-05 07:53:42 +00:00
|
|
|
},
|
|
|
|
})
|
2021-02-11 10:40:59 +00:00
|
|
|
|
|
|
|
// non-empty URLMap
|
|
|
|
f(`
|
|
|
|
users:
|
2021-04-02 19:14:53 +00:00
|
|
|
- bearer_token: foo
|
2021-02-11 10:40:59 +00:00
|
|
|
url_map:
|
2021-03-05 16:21:11 +00:00
|
|
|
- src_paths: ["/api/v1/query","/api/v1/query_range","/api/v1/label/[^./]+/.+"]
|
2021-02-11 10:40:59 +00:00
|
|
|
url_prefix: http://vmselect/select/0/prometheus
|
|
|
|
- src_paths: ["/api/v1/write"]
|
2023-12-13 22:46:36 +00:00
|
|
|
src_hosts: ["foo\\.bar", "baz:1234"]
|
2021-05-28 22:00:23 +00:00
|
|
|
url_prefix: ["http://vminsert1/insert/0/prometheus","http://vminsert2/insert/0/prometheus"]
|
2021-10-22 16:08:06 +00:00
|
|
|
headers:
|
|
|
|
- "foo: bar"
|
|
|
|
- "xxx: y"
|
2021-02-11 10:40:59 +00:00
|
|
|
`, map[string]*UserInfo{
|
2021-04-02 19:14:53 +00:00
|
|
|
getAuthToken("foo", "", ""): {
|
|
|
|
BearerToken: "foo",
|
2023-01-27 08:18:58 +00:00
|
|
|
URLMaps: []URLMap{
|
2021-02-11 10:40:59 +00:00
|
|
|
{
|
2023-12-13 22:46:36 +00:00
|
|
|
SrcPaths: getRegexs([]string{"/api/v1/query", "/api/v1/query_range", "/api/v1/label/[^./]+/.+"}),
|
2021-04-21 07:55:29 +00:00
|
|
|
URLPrefix: mustParseURL("http://vmselect/select/0/prometheus"),
|
2021-02-11 10:40:59 +00:00
|
|
|
},
|
|
|
|
{
|
2023-12-13 22:46:36 +00:00
|
|
|
SrcHosts: getRegexs([]string{"foo\\.bar", "baz:1234"}),
|
|
|
|
SrcPaths: getRegexs([]string{"/api/v1/write"}),
|
2021-05-28 22:00:23 +00:00
|
|
|
URLPrefix: mustParseURLs([]string{
|
|
|
|
"http://vminsert1/insert/0/prometheus",
|
|
|
|
"http://vminsert2/insert/0/prometheus",
|
|
|
|
}),
|
2023-08-31 12:26:51 +00:00
|
|
|
HeadersConf: HeadersConf{
|
|
|
|
RequestHeaders: []Header{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Value: "bar",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "xxx",
|
|
|
|
Value: "y",
|
|
|
|
},
|
2021-10-22 16:08:06 +00:00
|
|
|
},
|
|
|
|
},
|
2021-02-11 10:40:59 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-11-17 11:31:16 +00:00
|
|
|
getAuthToken("", "foo", ""): {
|
|
|
|
BearerToken: "foo",
|
2023-01-27 08:18:58 +00:00
|
|
|
URLMaps: []URLMap{
|
2021-11-17 11:31:16 +00:00
|
|
|
{
|
2023-12-13 22:46:36 +00:00
|
|
|
SrcPaths: getRegexs([]string{"/api/v1/query", "/api/v1/query_range", "/api/v1/label/[^./]+/.+"}),
|
2021-11-17 11:31:16 +00:00
|
|
|
URLPrefix: mustParseURL("http://vmselect/select/0/prometheus"),
|
|
|
|
},
|
|
|
|
{
|
2023-12-13 22:46:36 +00:00
|
|
|
SrcHosts: getRegexs([]string{"foo\\.bar", "baz:1234"}),
|
|
|
|
SrcPaths: getRegexs([]string{"/api/v1/write"}),
|
2021-11-17 11:31:16 +00:00
|
|
|
URLPrefix: mustParseURLs([]string{
|
|
|
|
"http://vminsert1/insert/0/prometheus",
|
|
|
|
"http://vminsert2/insert/0/prometheus",
|
|
|
|
}),
|
2023-08-31 12:26:51 +00:00
|
|
|
HeadersConf: HeadersConf{
|
|
|
|
RequestHeaders: []Header{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Value: "bar",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "xxx",
|
|
|
|
Value: "y",
|
|
|
|
},
|
2021-11-17 11:31:16 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-02-11 10:40:59 +00:00
|
|
|
})
|
2022-07-19 16:33:17 +00:00
|
|
|
// Multiple users with the same name
|
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- username: foo-same
|
|
|
|
password: baz
|
|
|
|
url_prefix: http://foo
|
|
|
|
- username: foo-same
|
|
|
|
password: bar
|
|
|
|
url_prefix: https://bar/x///
|
|
|
|
`, map[string]*UserInfo{
|
|
|
|
getAuthToken("", "foo-same", "baz"): {
|
|
|
|
Username: "foo-same",
|
|
|
|
Password: "baz",
|
|
|
|
URLPrefix: mustParseURL("http://foo"),
|
|
|
|
},
|
|
|
|
getAuthToken("", "foo-same", "bar"): {
|
|
|
|
Username: "foo-same",
|
|
|
|
Password: "bar",
|
|
|
|
URLPrefix: mustParseURL("https://bar/x"),
|
|
|
|
},
|
|
|
|
})
|
2023-04-26 09:04:35 +00:00
|
|
|
// with default url
|
|
|
|
f(`
|
|
|
|
users:
|
|
|
|
- bearer_token: foo
|
|
|
|
url_map:
|
|
|
|
- src_paths: ["/api/v1/query","/api/v1/query_range","/api/v1/label/[^./]+/.+"]
|
|
|
|
url_prefix: http://vmselect/select/0/prometheus
|
|
|
|
- src_paths: ["/api/v1/write"]
|
|
|
|
url_prefix: ["http://vminsert1/insert/0/prometheus","http://vminsert2/insert/0/prometheus"]
|
|
|
|
headers:
|
|
|
|
- "foo: bar"
|
|
|
|
- "xxx: y"
|
2023-05-08 22:12:19 +00:00
|
|
|
default_url:
|
2023-04-26 09:04:35 +00:00
|
|
|
- http://default1/select/0/prometheus
|
|
|
|
- http://default2/select/0/prometheus
|
|
|
|
`, map[string]*UserInfo{
|
|
|
|
getAuthToken("foo", "", ""): {
|
|
|
|
BearerToken: "foo",
|
|
|
|
URLMaps: []URLMap{
|
|
|
|
{
|
2023-12-13 22:46:36 +00:00
|
|
|
SrcPaths: getRegexs([]string{"/api/v1/query", "/api/v1/query_range", "/api/v1/label/[^./]+/.+"}),
|
2023-04-26 09:04:35 +00:00
|
|
|
URLPrefix: mustParseURL("http://vmselect/select/0/prometheus"),
|
|
|
|
},
|
|
|
|
{
|
2023-12-13 22:46:36 +00:00
|
|
|
SrcPaths: getRegexs([]string{"/api/v1/write"}),
|
2023-04-26 09:04:35 +00:00
|
|
|
URLPrefix: mustParseURLs([]string{
|
|
|
|
"http://vminsert1/insert/0/prometheus",
|
|
|
|
"http://vminsert2/insert/0/prometheus",
|
|
|
|
}),
|
2023-08-31 12:26:51 +00:00
|
|
|
HeadersConf: HeadersConf{
|
|
|
|
RequestHeaders: []Header{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Value: "bar",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "xxx",
|
|
|
|
Value: "y",
|
|
|
|
},
|
2023-04-26 09:04:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DefaultURL: mustParseURLs([]string{
|
|
|
|
"http://default1/select/0/prometheus",
|
|
|
|
"http://default2/select/0/prometheus",
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
getAuthToken("", "foo", ""): {
|
|
|
|
BearerToken: "foo",
|
|
|
|
URLMaps: []URLMap{
|
|
|
|
{
|
2023-12-13 22:46:36 +00:00
|
|
|
SrcPaths: getRegexs([]string{"/api/v1/query", "/api/v1/query_range", "/api/v1/label/[^./]+/.+"}),
|
2023-04-26 09:04:35 +00:00
|
|
|
URLPrefix: mustParseURL("http://vmselect/select/0/prometheus"),
|
|
|
|
},
|
|
|
|
{
|
2023-12-13 22:46:36 +00:00
|
|
|
SrcPaths: getRegexs([]string{"/api/v1/write"}),
|
2023-04-26 09:04:35 +00:00
|
|
|
URLPrefix: mustParseURLs([]string{
|
|
|
|
"http://vminsert1/insert/0/prometheus",
|
|
|
|
"http://vminsert2/insert/0/prometheus",
|
|
|
|
}),
|
2023-08-31 12:26:51 +00:00
|
|
|
HeadersConf: HeadersConf{
|
|
|
|
RequestHeaders: []Header{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Value: "bar",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "xxx",
|
|
|
|
Value: "y",
|
|
|
|
},
|
2023-04-26 09:04:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
DefaultURL: mustParseURLs([]string{
|
|
|
|
"http://default1/select/0/prometheus",
|
|
|
|
"http://default2/select/0/prometheus",
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
})
|
2022-07-19 16:33:17 +00:00
|
|
|
|
2020-05-05 07:53:42 +00:00
|
|
|
}
|
|
|
|
|
2023-11-03 11:04:17 +00:00
|
|
|
func TestParseAuthConfigPassesTLSVerificationConfig(t *testing.T) {
|
|
|
|
c := `
|
|
|
|
users:
|
|
|
|
- username: foo
|
|
|
|
password: bar
|
|
|
|
url_prefix: https://aaa/bbb
|
|
|
|
max_concurrent_requests: 5
|
|
|
|
tls_insecure_skip_verify: true
|
|
|
|
|
|
|
|
unauthorized_user:
|
|
|
|
url_prefix: http://aaa:343/bbb
|
|
|
|
max_concurrent_requests: 5
|
|
|
|
tls_insecure_skip_verify: false
|
|
|
|
`
|
|
|
|
|
|
|
|
ac, err := parseAuthConfig([]byte(c))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
m, err := parseAuthConfigUsers(ac)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := m[getAuthToken("", "foo", "bar")]
|
2023-11-13 07:23:35 +00:00
|
|
|
if !isSetBool(ui.TLSInsecureSkipVerify, true) || !ui.httpTransport.TLSClientConfig.InsecureSkipVerify {
|
2023-11-03 11:04:17 +00:00
|
|
|
t.Fatalf("unexpected TLSInsecureSkipVerify value for user foo")
|
|
|
|
}
|
|
|
|
|
2023-11-13 07:23:35 +00:00
|
|
|
if !isSetBool(ac.UnauthorizedUser.TLSInsecureSkipVerify, false) || ac.UnauthorizedUser.httpTransport.TLSClientConfig.InsecureSkipVerify {
|
2023-11-03 11:04:17 +00:00
|
|
|
t.Fatalf("unexpected TLSInsecureSkipVerify value for unauthorized_user")
|
|
|
|
}
|
2023-11-13 07:23:35 +00:00
|
|
|
}
|
2023-11-03 11:04:17 +00:00
|
|
|
|
2023-11-13 07:23:35 +00:00
|
|
|
func isSetBool(boolP *bool, expectedValue bool) bool {
|
|
|
|
if boolP == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return *boolP == expectedValue
|
2023-11-03 11:04:17 +00:00
|
|
|
}
|
|
|
|
|
2023-12-13 22:46:36 +00:00
|
|
|
func getRegexs(paths []string) []*Regex {
|
|
|
|
var sps []*Regex
|
2021-03-05 16:21:11 +00:00
|
|
|
for _, path := range paths {
|
2023-12-13 22:46:36 +00:00
|
|
|
sps = append(sps, &Regex{
|
2021-03-05 16:21:11 +00:00
|
|
|
sOriginal: path,
|
|
|
|
re: regexp.MustCompile("^(?:" + path + ")$"),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return sps
|
|
|
|
}
|
|
|
|
|
2020-05-05 07:53:42 +00:00
|
|
|
func removeMetrics(m map[string]*UserInfo) {
|
|
|
|
for _, info := range m {
|
|
|
|
info.requests = nil
|
|
|
|
}
|
|
|
|
}
|
2021-03-05 16:21:11 +00:00
|
|
|
|
|
|
|
func areEqualConfigs(a, b map[string]*UserInfo) error {
|
|
|
|
aData, err := yaml.Marshal(a)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot marshal a: %w", err)
|
|
|
|
}
|
|
|
|
bData, err := yaml.Marshal(b)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot marshal b: %w", err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(aData, bData) {
|
|
|
|
return fmt.Errorf("unexpected configs;\ngot\n%s\nwant\n%s", aData, bData)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-04-21 07:55:29 +00:00
|
|
|
|
2021-05-28 22:00:23 +00:00
|
|
|
func mustParseURL(u string) *URLPrefix {
|
|
|
|
return mustParseURLs([]string{u})
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustParseURLs(us []string) *URLPrefix {
|
2023-02-11 08:27:40 +00:00
|
|
|
bus := make([]*backendURL, len(us))
|
2021-05-28 22:00:23 +00:00
|
|
|
for i, u := range us {
|
|
|
|
pu, err := url.Parse(u)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("BUG: cannot parse %q: %w", u, err))
|
|
|
|
}
|
2023-02-11 08:27:40 +00:00
|
|
|
bus[i] = &backendURL{
|
|
|
|
url: pu,
|
|
|
|
}
|
2021-04-21 07:55:29 +00:00
|
|
|
}
|
2021-05-28 22:00:23 +00:00
|
|
|
return &URLPrefix{
|
2023-02-11 08:27:40 +00:00
|
|
|
bus: bus,
|
2021-04-21 07:55:29 +00:00
|
|
|
}
|
|
|
|
}
|