From 8efe12d66eb181cb1dba47d35829be696d1aa90c Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Wed, 6 Mar 2024 21:19:43 +0200 Subject: [PATCH] app/vmauth: simplify configuration for `src_query_args` Use the shorter form: src_query_args: - arg1=value1 - arg2=value2 instead of src_query_args: - name: arg1 value: value2 - name: arg2 value: value2 Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/5878 --- app/vmauth/auth_config.go | 25 ++++++++++++++++++++++++- app/vmauth/auth_config_test.go | 12 +----------- docs/vmauth.md | 6 ++---- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/app/vmauth/auth_config.go b/app/vmauth/auth_config.go index 7d1e1db8aa..cbe024dd1b 100644 --- a/app/vmauth/auth_config.go +++ b/app/vmauth/auth_config.go @@ -163,13 +163,36 @@ type URLMap struct { // Regex represents a regex type Regex struct { + re *regexp.Regexp + sOriginal string - re *regexp.Regexp } +// QueryArg represents HTTP query arg type QueryArg struct { Name string `yaml:"name"` Value string `yaml:"value,omitempty"` + + sOriginal string +} + +// UnmarshalYAML unmarshals up from yaml. +func (qa *QueryArg) UnmarshalYAML(f func(interface{}) error) error { + if err := f(&qa.sOriginal); err != nil { + return err + } + + n := strings.IndexByte(qa.sOriginal, '=') + if n >= 0 { + qa.Name = qa.sOriginal[:n] + qa.Value = qa.sOriginal[n+1:] + } + return nil +} + +// MarshalYAML marshals up to yaml. +func (qa *QueryArg) MarshalYAML() (interface{}, error) { + return qa.sOriginal, nil } // URLPrefix represents passed `url_prefix` diff --git a/app/vmauth/auth_config_test.go b/app/vmauth/auth_config_test.go index 7586b63159..6e22e6ed96 100644 --- a/app/vmauth/auth_config_test.go +++ b/app/vmauth/auth_config_test.go @@ -218,15 +218,6 @@ users: - src_query_args: abc url_prefix: http://foobar `) - f(` -users: -- username: a - url_map: - - src_query_args: - - name: foo - incorrect_value: bar - url_prefix: http://foobar -`) // Invalid headers in url_map (missing ':') f(` @@ -350,8 +341,7 @@ users: - src_paths: ["/api/v1/write"] src_hosts: ["foo\\.bar", "baz:1234"] src_query_args: - - name: foo - value: bar + - 'foo=bar' url_prefix: ["http://vminsert1/insert/0/prometheus","http://vminsert2/insert/0/prometheus"] headers: - "foo: bar" diff --git a/docs/vmauth.md b/docs/vmauth.md index f1b0a3992b..43a62e94f3 100644 --- a/docs/vmauth.md +++ b/docs/vmauth.md @@ -125,12 +125,10 @@ while routing requests with `db=bar` query arg to `http://app2-backend`: unauthorized_user: url_map: - src_query_args: - - name: db - value: foo + - "db=foo" url_prefix: "http://app1-backend/" - src_query_args: - - name: db - value: bar + - "db=bar" url_prefix: "http://app2-backend/" ```