2020-05-05 07:53:42 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
2020-05-16 08:59:30 +00:00
|
|
|
"os"
|
2020-05-05 07:53:42 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/buildinfo"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/envflag"
|
2020-12-03 19:40:30 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil"
|
2020-05-05 07:53:42 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/procutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-05-14 15:10:19 +00:00
|
|
|
httpListenAddr = flag.String("httpListenAddr", ":8427", "TCP address to listen for http connections")
|
|
|
|
maxIdleConnsPerBackend = flag.Int("maxIdleConnsPerBackend", 100, "The maximum number of idle connections vmauth can open per each backend host")
|
2020-05-05 07:53:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2020-05-16 08:59:30 +00:00
|
|
|
// Write flags and help message to stdout, since it is easier to grep or pipe.
|
|
|
|
flag.CommandLine.SetOutput(os.Stdout)
|
2020-06-05 07:39:46 +00:00
|
|
|
flag.Usage = usage
|
2020-05-05 07:53:42 +00:00
|
|
|
envflag.Parse()
|
|
|
|
buildinfo.Init()
|
|
|
|
logger.Init()
|
|
|
|
logger.Infof("starting vmauth at %q...", *httpListenAddr)
|
|
|
|
startTime := time.Now()
|
|
|
|
initAuthConfig()
|
|
|
|
go httpserver.Serve(*httpListenAddr, requestHandler)
|
|
|
|
logger.Infof("started vmauth in %.3f seconds", time.Since(startTime).Seconds())
|
|
|
|
|
|
|
|
sig := procutil.WaitForSigterm()
|
|
|
|
logger.Infof("received signal %s", sig)
|
|
|
|
|
|
|
|
startTime = time.Now()
|
|
|
|
logger.Infof("gracefully shutting down webservice at %q", *httpListenAddr)
|
|
|
|
if err := httpserver.Stop(*httpListenAddr); err != nil {
|
|
|
|
logger.Fatalf("cannot stop the webservice: %s", err)
|
|
|
|
}
|
|
|
|
logger.Infof("successfully shut down the webservice in %.3f seconds", time.Since(startTime).Seconds())
|
|
|
|
stopAuthConfig()
|
|
|
|
logger.Infof("successfully stopped vmauth in %.3f seconds", time.Since(startTime).Seconds())
|
|
|
|
}
|
|
|
|
|
|
|
|
func requestHandler(w http.ResponseWriter, r *http.Request) bool {
|
2021-04-02 19:14:53 +00:00
|
|
|
authToken := r.Header.Get("Authorization")
|
|
|
|
if authToken == "" {
|
2020-08-09 06:38:41 +00:00
|
|
|
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
|
2021-04-02 19:14:53 +00:00
|
|
|
http.Error(w, "missing `Authorization` request header", http.StatusUnauthorized)
|
2020-05-05 07:53:42 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
ac := authConfig.Load().(map[string]*UserInfo)
|
2021-04-02 19:14:53 +00:00
|
|
|
ui := ac[authToken]
|
|
|
|
if ui == nil {
|
|
|
|
httpserver.Errorf(w, r, "cannot find the provided auth token %q in config", authToken)
|
2020-05-05 07:53:42 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-02-11 10:40:59 +00:00
|
|
|
ui.requests.Inc()
|
|
|
|
targetURL, err := createTargetURL(ui, r.URL)
|
|
|
|
if err != nil {
|
|
|
|
httpserver.Errorf(w, r, "cannot determine targetURL: %s", err)
|
|
|
|
return true
|
|
|
|
}
|
2021-04-21 07:55:29 +00:00
|
|
|
r.Header.Set("vm-target-url", targetURL.String())
|
2020-05-05 07:53:42 +00:00
|
|
|
reverseProxy.ServeHTTP(w, r)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
var reverseProxy = &httputil.ReverseProxy{
|
|
|
|
Director: func(r *http.Request) {
|
|
|
|
targetURL := r.Header.Get("vm-target-url")
|
|
|
|
target, err := url.Parse(targetURL)
|
|
|
|
if err != nil {
|
|
|
|
logger.Panicf("BUG: unexpected error when parsing targetURL=%q: %s", targetURL, err)
|
|
|
|
}
|
|
|
|
r.URL = target
|
|
|
|
},
|
2020-06-05 17:13:39 +00:00
|
|
|
Transport: func() *http.Transport {
|
|
|
|
tr := http.DefaultTransport.(*http.Transport).Clone()
|
|
|
|
// Automatic compression must be disabled in order to fix https://github.com/VictoriaMetrics/VictoriaMetrics/issues/535
|
|
|
|
tr.DisableCompression = true
|
|
|
|
// Disable HTTP/2.0, since VictoriaMetrics components don't support HTTP/2.0 (because there is no sense in this).
|
|
|
|
tr.ForceAttemptHTTP2 = false
|
2021-05-14 15:10:19 +00:00
|
|
|
tr.MaxIdleConnsPerHost = *maxIdleConnsPerBackend
|
2020-06-05 17:13:39 +00:00
|
|
|
return tr
|
|
|
|
}(),
|
2020-05-05 07:53:42 +00:00
|
|
|
FlushInterval: time.Second,
|
|
|
|
ErrorLog: logger.StdErrorLogger(),
|
|
|
|
}
|
2020-06-05 07:39:46 +00:00
|
|
|
|
|
|
|
func usage() {
|
|
|
|
const s = `
|
|
|
|
vmauth authenticates and authorizes incoming requests and proxies them to VictoriaMetrics.
|
|
|
|
|
2021-04-20 17:16:17 +00:00
|
|
|
See the docs at https://docs.victoriametrics.com/vmauth.html .
|
2020-06-05 07:39:46 +00:00
|
|
|
`
|
2020-12-03 19:40:30 +00:00
|
|
|
flagutil.Usage(s)
|
2020-06-05 07:39:46 +00:00
|
|
|
}
|