2020-05-05 07:53:42 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-04-02 19:14:53 +00:00
|
|
|
"encoding/base64"
|
2020-05-05 07:53:42 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/url"
|
2021-05-21 13:34:03 +00:00
|
|
|
"os"
|
2021-03-05 16:21:11 +00:00
|
|
|
"regexp"
|
2021-05-28 22:00:23 +00:00
|
|
|
"strconv"
|
2020-05-05 07:53:42 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2020-08-13 13:43:55 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/envtemplate"
|
2020-05-05 07:53:42 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/procutil"
|
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-04-20 17:16:17 +00:00
|
|
|
authConfigPath = flag.String("auth.config", "", "Path to auth config. See https://docs.victoriametrics.com/vmauth.html "+
|
2020-05-05 07:53:42 +00:00
|
|
|
"for details on the format of this auth config")
|
|
|
|
)
|
|
|
|
|
|
|
|
// AuthConfig represents auth config.
|
|
|
|
type AuthConfig struct {
|
2021-10-22 16:08:06 +00:00
|
|
|
Users []UserInfo `yaml:"users,omitempty"`
|
2020-05-05 07:53:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UserInfo is user information read from authConfigPath
|
|
|
|
type UserInfo struct {
|
2021-11-16 22:47:31 +00:00
|
|
|
Name string `yaml:"name,omitempty"`
|
2021-10-22 16:08:06 +00:00
|
|
|
BearerToken string `yaml:"bearer_token,omitempty"`
|
|
|
|
Username string `yaml:"username,omitempty"`
|
|
|
|
Password string `yaml:"password,omitempty"`
|
|
|
|
URLPrefix *URLPrefix `yaml:"url_prefix,omitempty"`
|
|
|
|
URLMap []URLMap `yaml:"url_map,omitempty"`
|
|
|
|
Headers []Header `yaml:"headers,omitempty"`
|
2020-05-05 07:53:42 +00:00
|
|
|
|
|
|
|
requests *metrics.Counter
|
|
|
|
}
|
|
|
|
|
2021-10-22 16:08:06 +00:00
|
|
|
// Header is `Name: Value` http header, which must be added to the proxied request.
|
|
|
|
type Header struct {
|
|
|
|
Name string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML unmarshals h from f.
|
|
|
|
func (h *Header) UnmarshalYAML(f func(interface{}) error) error {
|
|
|
|
var s string
|
|
|
|
if err := f(&s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n := strings.IndexByte(s, ':')
|
|
|
|
if n < 0 {
|
|
|
|
return fmt.Errorf("missing speparator char ':' between Name and Value in the header %q; expected format - 'Name: Value'", s)
|
|
|
|
}
|
|
|
|
h.Name = strings.TrimSpace(s[:n])
|
|
|
|
h.Value = strings.TrimSpace(s[n+1:])
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalYAML marshals h to yaml.
|
|
|
|
func (h *Header) MarshalYAML() (interface{}, error) {
|
|
|
|
s := fmt.Sprintf("%s: %s", h.Name, h.Value)
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2021-02-11 10:40:59 +00:00
|
|
|
// URLMap is a mapping from source paths to target urls.
|
|
|
|
type URLMap struct {
|
2021-10-22 16:08:06 +00:00
|
|
|
SrcPaths []*SrcPath `yaml:"src_paths,omitempty"`
|
|
|
|
URLPrefix *URLPrefix `yaml:"url_prefix,omitempty"`
|
|
|
|
Headers []Header `yaml:"headers,omitempty"`
|
2021-03-05 16:21:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SrcPath represents an src path
|
|
|
|
type SrcPath struct {
|
|
|
|
sOriginal string
|
|
|
|
re *regexp.Regexp
|
|
|
|
}
|
|
|
|
|
2021-05-28 22:00:23 +00:00
|
|
|
// URLPrefix represents pased `url_prefix`
|
|
|
|
type URLPrefix struct {
|
|
|
|
n uint32
|
|
|
|
urls []*url.URL
|
2021-04-21 07:55:29 +00:00
|
|
|
}
|
|
|
|
|
2021-05-28 22:00:23 +00:00
|
|
|
func (up *URLPrefix) getNextURL() *url.URL {
|
|
|
|
n := atomic.AddUint32(&up.n, 1)
|
|
|
|
idx := n % uint32(len(up.urls))
|
|
|
|
return up.urls[idx]
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML unmarshals up from yaml.
|
|
|
|
func (up *URLPrefix) UnmarshalYAML(f func(interface{}) error) error {
|
|
|
|
var v interface{}
|
|
|
|
if err := f(&v); err != nil {
|
2021-04-21 07:55:29 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-05-28 22:00:23 +00:00
|
|
|
var urls []string
|
|
|
|
switch x := v.(type) {
|
|
|
|
case string:
|
|
|
|
urls = []string{x}
|
|
|
|
case []interface{}:
|
|
|
|
if len(x) == 0 {
|
|
|
|
return fmt.Errorf("`url_prefix` must contain at least a single url")
|
|
|
|
}
|
|
|
|
us := make([]string, len(x))
|
|
|
|
for i, xx := range x {
|
|
|
|
s, ok := xx.(string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("`url_prefix` must contain array of strings; got %T", xx)
|
|
|
|
}
|
|
|
|
us[i] = s
|
|
|
|
}
|
|
|
|
urls = us
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unexpected type for `url_prefix`: %T; want string or []string", v)
|
|
|
|
}
|
|
|
|
pus := make([]*url.URL, len(urls))
|
|
|
|
for i, u := range urls {
|
|
|
|
pu, err := url.Parse(u)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot unmarshal %q into url: %w", u, err)
|
|
|
|
}
|
|
|
|
pus[i] = pu
|
2021-04-21 07:55:29 +00:00
|
|
|
}
|
2021-05-28 22:00:23 +00:00
|
|
|
up.urls = pus
|
2021-04-21 07:55:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-28 22:00:23 +00:00
|
|
|
// MarshalYAML marshals up to yaml.
|
|
|
|
func (up *URLPrefix) MarshalYAML() (interface{}, error) {
|
|
|
|
var b []byte
|
|
|
|
if len(up.urls) == 1 {
|
|
|
|
u := up.urls[0].String()
|
|
|
|
b = strconv.AppendQuote(b, u)
|
|
|
|
return string(b), nil
|
|
|
|
}
|
|
|
|
b = append(b, '[')
|
|
|
|
for i, pu := range up.urls {
|
|
|
|
u := pu.String()
|
|
|
|
b = strconv.AppendQuote(b, u)
|
|
|
|
if i+1 < len(up.urls) {
|
|
|
|
b = append(b, ',')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b = append(b, ']')
|
|
|
|
return string(b), nil
|
2021-04-21 07:55:29 +00:00
|
|
|
}
|
|
|
|
|
2021-03-05 16:21:11 +00:00
|
|
|
func (sp *SrcPath) match(s string) bool {
|
|
|
|
prefix, ok := sp.re.LiteralPrefix()
|
|
|
|
if ok {
|
|
|
|
// Fast path - literal match
|
|
|
|
return s == prefix
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(s, prefix) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return sp.re.MatchString(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML implements yaml.Unmarshaler
|
|
|
|
func (sp *SrcPath) UnmarshalYAML(f func(interface{}) error) error {
|
|
|
|
var s string
|
|
|
|
if err := f(&s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sAnchored := "^(?:" + s + ")$"
|
|
|
|
re, err := regexp.Compile(sAnchored)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot build regexp from %q: %w", s, err)
|
|
|
|
}
|
|
|
|
sp.sOriginal = s
|
|
|
|
sp.re = re
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalYAML implements yaml.Marshaler.
|
|
|
|
func (sp *SrcPath) MarshalYAML() (interface{}, error) {
|
|
|
|
return sp.sOriginal, nil
|
2021-02-11 10:40:59 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 07:53:42 +00:00
|
|
|
func initAuthConfig() {
|
|
|
|
if len(*authConfigPath) == 0 {
|
2020-06-05 17:13:03 +00:00
|
|
|
logger.Fatalf("missing required `-auth.config` command-line flag")
|
2020-05-05 07:53:42 +00:00
|
|
|
}
|
2021-05-21 13:34:03 +00:00
|
|
|
|
|
|
|
// Register SIGHUP handler for config re-read just before readAuthConfig call.
|
|
|
|
// This guarantees that the config will be re-read if the signal arrives during readAuthConfig call.
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1240
|
|
|
|
sighupCh := procutil.NewSighupChan()
|
|
|
|
|
2020-05-05 07:53:42 +00:00
|
|
|
m, err := readAuthConfig(*authConfigPath)
|
|
|
|
if err != nil {
|
2020-06-05 17:13:03 +00:00
|
|
|
logger.Fatalf("cannot load auth config from `-auth.config=%s`: %s", *authConfigPath, err)
|
2020-05-05 07:53:42 +00:00
|
|
|
}
|
|
|
|
authConfig.Store(m)
|
|
|
|
stopCh = make(chan struct{})
|
|
|
|
authConfigWG.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer authConfigWG.Done()
|
2021-05-21 13:34:03 +00:00
|
|
|
authConfigReloader(sighupCh)
|
2020-05-05 07:53:42 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func stopAuthConfig() {
|
|
|
|
close(stopCh)
|
|
|
|
authConfigWG.Wait()
|
|
|
|
}
|
|
|
|
|
2021-05-21 13:34:03 +00:00
|
|
|
func authConfigReloader(sighupCh <-chan os.Signal) {
|
2020-05-05 07:53:42 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stopCh:
|
|
|
|
return
|
|
|
|
case <-sighupCh:
|
2020-06-03 20:22:09 +00:00
|
|
|
logger.Infof("SIGHUP received; loading -auth.config=%q", *authConfigPath)
|
2020-05-05 07:53:42 +00:00
|
|
|
m, err := readAuthConfig(*authConfigPath)
|
|
|
|
if err != nil {
|
2020-06-03 20:22:09 +00:00
|
|
|
logger.Errorf("failed to load -auth.config=%q; using the last successfully loaded config; error: %s", *authConfigPath, err)
|
2020-05-05 07:53:42 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
authConfig.Store(m)
|
2020-06-03 20:22:09 +00:00
|
|
|
logger.Infof("Successfully reloaded -auth.config=%q", *authConfigPath)
|
2020-05-05 07:53:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var authConfig atomic.Value
|
|
|
|
var authConfigWG sync.WaitGroup
|
|
|
|
var stopCh chan struct{}
|
|
|
|
|
|
|
|
func readAuthConfig(path string) (map[string]*UserInfo, error) {
|
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot read %q: %w", path, err)
|
2020-05-05 07:53:42 +00:00
|
|
|
}
|
|
|
|
m, err := parseAuthConfig(data)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot parse %q: %w", path, err)
|
2020-05-05 07:53:42 +00:00
|
|
|
}
|
|
|
|
logger.Infof("Loaded information about %d users from %q", len(m), path)
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseAuthConfig(data []byte) (map[string]*UserInfo, error) {
|
2020-08-13 13:43:55 +00:00
|
|
|
data = envtemplate.Replace(data)
|
2020-05-05 07:53:42 +00:00
|
|
|
var ac AuthConfig
|
|
|
|
if err := yaml.UnmarshalStrict(data, &ac); err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot unmarshal AuthConfig data: %w", err)
|
2020-05-05 07:53:42 +00:00
|
|
|
}
|
|
|
|
uis := ac.Users
|
|
|
|
if len(uis) == 0 {
|
|
|
|
return nil, fmt.Errorf("`users` section cannot be empty in AuthConfig")
|
|
|
|
}
|
2021-04-02 19:14:53 +00:00
|
|
|
byAuthToken := make(map[string]*UserInfo, len(uis))
|
|
|
|
byUsername := make(map[string]bool, len(uis))
|
|
|
|
byBearerToken := make(map[string]bool, len(uis))
|
2020-05-05 07:53:42 +00:00
|
|
|
for i := range uis {
|
|
|
|
ui := &uis[i]
|
2021-04-02 19:14:53 +00:00
|
|
|
if ui.BearerToken == "" && ui.Username == "" {
|
|
|
|
return nil, fmt.Errorf("either bearer_token or username must be set")
|
|
|
|
}
|
|
|
|
if ui.BearerToken != "" && ui.Username != "" {
|
|
|
|
return nil, fmt.Errorf("bearer_token=%q and username=%q cannot be set simultaneously", ui.BearerToken, ui.Username)
|
|
|
|
}
|
|
|
|
if byBearerToken[ui.BearerToken] {
|
|
|
|
return nil, fmt.Errorf("duplicate bearer_token found; bearer_token: %q", ui.BearerToken)
|
|
|
|
}
|
|
|
|
if byUsername[ui.Username] {
|
2020-05-05 07:53:42 +00:00
|
|
|
return nil, fmt.Errorf("duplicate username found; username: %q", ui.Username)
|
|
|
|
}
|
2021-04-02 19:14:53 +00:00
|
|
|
authToken := getAuthToken(ui.BearerToken, ui.Username, ui.Password)
|
|
|
|
if byAuthToken[authToken] != nil {
|
|
|
|
return nil, fmt.Errorf("duplicate auth token found for bearer_token=%q, username=%q: %q", authToken, ui.BearerToken, ui.Username)
|
|
|
|
}
|
2021-04-21 07:55:29 +00:00
|
|
|
if ui.URLPrefix != nil {
|
2021-05-28 22:00:23 +00:00
|
|
|
if err := ui.URLPrefix.sanitize(); err != nil {
|
2021-02-11 10:40:59 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-05 07:53:42 +00:00
|
|
|
}
|
2021-02-11 10:40:59 +00:00
|
|
|
for _, e := range ui.URLMap {
|
|
|
|
if len(e.SrcPaths) == 0 {
|
2021-04-21 07:55:29 +00:00
|
|
|
return nil, fmt.Errorf("missing `src_paths` in `url_map`")
|
|
|
|
}
|
|
|
|
if e.URLPrefix == nil {
|
|
|
|
return nil, fmt.Errorf("missing `url_prefix` in `url_map`")
|
2021-02-11 10:40:59 +00:00
|
|
|
}
|
2021-05-28 22:00:23 +00:00
|
|
|
if err := e.URLPrefix.sanitize(); err != nil {
|
2021-02-11 10:40:59 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-05 07:53:42 +00:00
|
|
|
}
|
2021-04-21 07:55:29 +00:00
|
|
|
if len(ui.URLMap) == 0 && ui.URLPrefix == nil {
|
2021-02-11 10:40:59 +00:00
|
|
|
return nil, fmt.Errorf("missing `url_prefix`")
|
2020-05-05 07:53:42 +00:00
|
|
|
}
|
2021-04-02 19:14:53 +00:00
|
|
|
if ui.BearerToken != "" {
|
2021-11-16 22:47:31 +00:00
|
|
|
name := "bearer_token"
|
|
|
|
if ui.Name != "" {
|
|
|
|
name = ui.Name
|
|
|
|
}
|
2021-04-02 19:14:53 +00:00
|
|
|
if ui.Password != "" {
|
|
|
|
return nil, fmt.Errorf("password shouldn't be set for bearer_token %q", ui.BearerToken)
|
|
|
|
}
|
2021-11-16 22:47:31 +00:00
|
|
|
ui.requests = metrics.GetOrCreateCounter(fmt.Sprintf(`vmauth_user_requests_total{username=%q}`, name))
|
2021-04-02 19:14:53 +00:00
|
|
|
byBearerToken[ui.BearerToken] = true
|
|
|
|
}
|
|
|
|
if ui.Username != "" {
|
2021-11-16 22:47:31 +00:00
|
|
|
name := ui.Username
|
|
|
|
if ui.Name != "" {
|
|
|
|
name = ui.Name
|
|
|
|
}
|
|
|
|
ui.requests = metrics.GetOrCreateCounter(fmt.Sprintf(`vmauth_user_requests_total{username=%q}`, name))
|
2021-04-02 19:14:53 +00:00
|
|
|
byUsername[ui.Username] = true
|
|
|
|
}
|
|
|
|
byAuthToken[authToken] = ui
|
2020-05-05 07:53:42 +00:00
|
|
|
}
|
2021-04-02 19:14:53 +00:00
|
|
|
return byAuthToken, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAuthToken(bearerToken, username, password string) string {
|
|
|
|
if bearerToken != "" {
|
|
|
|
return "Bearer " + bearerToken
|
|
|
|
}
|
|
|
|
token := username + ":" + password
|
|
|
|
token64 := base64.StdEncoding.EncodeToString([]byte(token))
|
|
|
|
return "Basic " + token64
|
2020-05-05 07:53:42 +00:00
|
|
|
}
|
2021-02-11 10:40:59 +00:00
|
|
|
|
2021-05-28 22:00:23 +00:00
|
|
|
func (up *URLPrefix) sanitize() error {
|
|
|
|
for i, pu := range up.urls {
|
|
|
|
puNew, err := sanitizeURLPrefix(pu)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
up.urls[i] = puNew
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-21 07:55:29 +00:00
|
|
|
func sanitizeURLPrefix(urlPrefix *url.URL) (*url.URL, error) {
|
2021-02-11 10:40:59 +00:00
|
|
|
// Remove trailing '/' from urlPrefix
|
2021-04-21 07:55:29 +00:00
|
|
|
for strings.HasSuffix(urlPrefix.Path, "/") {
|
|
|
|
urlPrefix.Path = urlPrefix.Path[:len(urlPrefix.Path)-1]
|
2021-02-11 10:40:59 +00:00
|
|
|
}
|
|
|
|
// Validate urlPrefix
|
2021-04-21 07:55:29 +00:00
|
|
|
if urlPrefix.Scheme != "http" && urlPrefix.Scheme != "https" {
|
|
|
|
return nil, fmt.Errorf("unsupported scheme for `url_prefix: %q`: %q; must be `http` or `https`", urlPrefix, urlPrefix.Scheme)
|
2021-02-11 10:40:59 +00:00
|
|
|
}
|
2021-04-21 07:55:29 +00:00
|
|
|
if urlPrefix.Host == "" {
|
|
|
|
return nil, fmt.Errorf("missing hostname in `url_prefix %q`", urlPrefix.Host)
|
2021-02-11 10:40:59 +00:00
|
|
|
}
|
|
|
|
return urlPrefix, nil
|
|
|
|
}
|