Add authentication: admin/agent bearer tokens for the API, login for the dashboard
control-api: every route now carries a mandatory access level (admin / agent / open) in a route table. All /api/v1/admin/* require the admin token; the write calls of validator-agent and prober (self-check, events, results, complete) require a separate static agent token; register, heartbeat and fetching the assignment stay open. Tokens come from env vars, are compared in constant time and never logged. An empty token leaves that level open with a startup warning (backward compatible). validator-agent / prober: apiclient sends the agent token only to control-api. admin-dashboard: login/password (from env) with a stateless HMAC session cookie, Origin-based CSRF check, per-IP brute-force throttle, HX-Redirect for htmx polls, logout in the sidebar; the dashboard calls control-api with the admin token. Login page layout fixed after review. Also: env plumbing in docker-compose/rxprod-compose/systemd/config examples, e2e script with token assertions, tests, docs (API, SETUP, USAGE, DASHBOARD, README), plan and review under docs/changes/, bin/ rebuilt with new SHA256SUMS. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
972ad47d0c
commit
debf2afed2
67 files changed
+2050
-105
No files matched your search
@@ -0,0 +1,392 @@
|
||||
package dashboard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/hmac"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"crypto/subtle"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
sessionCookieName = "session"
|
||||
defaultSessionTTL = 8 * time.Hour
|
||||
|
||||
// Brute-force throttle: maxLoginFailures failures from one client IP
|
||||
// within loginFailureWindow lock that IP out until the oldest failure
|
||||
// leaves the window.
|
||||
maxLoginFailures = 5
|
||||
loginFailureWindow = 10 * time.Minute
|
||||
)
|
||||
|
||||
// authState holds everything the login/session machinery needs. A nil-safe
|
||||
// zero value is never used: Server.auth is always set by New, with
|
||||
// enabled=false when no credentials were configured.
|
||||
type authState struct {
|
||||
enabled bool
|
||||
user [sha256.Size]byte // sha256(username)
|
||||
pass [sha256.Size]byte // sha256(password)
|
||||
key []byte // HMAC key for session cookies
|
||||
ttl time.Duration
|
||||
now func() time.Time
|
||||
throttle *loginThrottle
|
||||
}
|
||||
|
||||
func newAuthState(cfg Config, log *slog.Logger) *authState {
|
||||
a := &authState{now: time.Now, throttle: newLoginThrottle(), ttl: cfg.SessionTTL}
|
||||
if a.ttl <= 0 {
|
||||
a.ttl = defaultSessionTTL
|
||||
}
|
||||
if cfg.Username == "" || cfg.Password == "" {
|
||||
log.Warn("dashboard login is disabled: username/password are not set, anyone who can reach this port has full access")
|
||||
return a
|
||||
}
|
||||
a.enabled = true
|
||||
a.user = sha256.Sum256([]byte(cfg.Username))
|
||||
a.pass = sha256.Sum256([]byte(cfg.Password))
|
||||
if cfg.SessionSecret != "" {
|
||||
a.key = []byte(cfg.SessionSecret)
|
||||
} else {
|
||||
a.key = make([]byte, 32)
|
||||
if _, err := rand.Read(a.key); err != nil {
|
||||
panic("dashboard: crypto/rand failed: " + err.Error())
|
||||
}
|
||||
log.Warn("dashboard session secret is not set: using a random one, sessions are reset on every restart")
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// checkCredentials compares both fields in constant time and always
|
||||
// evaluates both, so neither the length nor which field was wrong leaks.
|
||||
func (a *authState) checkCredentials(user, pass string) bool {
|
||||
u := sha256.Sum256([]byte(user))
|
||||
p := sha256.Sum256([]byte(pass))
|
||||
uOK := subtle.ConstantTimeCompare(u[:], a.user[:])
|
||||
pOK := subtle.ConstantTimeCompare(p[:], a.pass[:])
|
||||
return uOK&pOK == 1
|
||||
}
|
||||
|
||||
// ---- session cookie ----
|
||||
|
||||
type sessionPayload struct {
|
||||
User string `json:"u"`
|
||||
Exp int64 `json:"exp"`
|
||||
}
|
||||
|
||||
func (a *authState) sign(msg string) []byte {
|
||||
m := hmac.New(sha256.New, a.key)
|
||||
m.Write([]byte(msg))
|
||||
return m.Sum(nil)
|
||||
}
|
||||
|
||||
func (a *authState) issue(user string) (value string, exp time.Time) {
|
||||
exp = a.now().Add(a.ttl)
|
||||
raw, _ := json.Marshal(sessionPayload{User: user, Exp: exp.Unix()})
|
||||
p := base64.RawURLEncoding.EncodeToString(raw)
|
||||
return p + "." + base64.RawURLEncoding.EncodeToString(a.sign(p)), exp
|
||||
}
|
||||
|
||||
// verify returns the session's user if value is an untampered, unexpired
|
||||
// cookie issued with this server's key.
|
||||
func (a *authState) verify(value string) (string, bool) {
|
||||
p, sig, ok := strings.Cut(value, ".")
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
got, err := base64.RawURLEncoding.DecodeString(sig)
|
||||
if err != nil || !hmac.Equal(got, a.sign(p)) {
|
||||
return "", false
|
||||
}
|
||||
raw, err := base64.RawURLEncoding.DecodeString(p)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
var sp sessionPayload
|
||||
if json.Unmarshal(raw, &sp) != nil || sp.User == "" || a.now().Unix() >= sp.Exp {
|
||||
return "", false
|
||||
}
|
||||
return sp.User, true
|
||||
}
|
||||
|
||||
func isHTTPS(r *http.Request) bool {
|
||||
return r.TLS != nil || strings.EqualFold(r.Header.Get("X-Forwarded-Proto"), "https")
|
||||
}
|
||||
|
||||
func (a *authState) setCookie(w http.ResponseWriter, r *http.Request, user string) {
|
||||
value, exp := a.issue(user)
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: sessionCookieName, Value: value, Path: "/", Expires: exp,
|
||||
MaxAge: int(a.ttl.Seconds()), HttpOnly: true, Secure: isHTTPS(r), SameSite: http.SameSiteStrictMode,
|
||||
})
|
||||
}
|
||||
|
||||
func clearCookie(w http.ResponseWriter, r *http.Request) {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: sessionCookieName, Value: "", Path: "/", MaxAge: -1,
|
||||
HttpOnly: true, Secure: isHTTPS(r), SameSite: http.SameSiteStrictMode,
|
||||
})
|
||||
}
|
||||
|
||||
// ---- request context ----
|
||||
|
||||
type ctxKey struct{}
|
||||
|
||||
func userFromRequest(r *http.Request) string {
|
||||
u, _ := r.Context().Value(ctxKey{}).(string)
|
||||
return u
|
||||
}
|
||||
|
||||
// ---- middleware ----
|
||||
|
||||
// authMiddleware gates every route except the login page, logout and static
|
||||
// assets. With login disabled it is a pass-through (no CSRF check either).
|
||||
func (s *Server) authMiddleware(next http.Handler) http.Handler {
|
||||
a := s.auth
|
||||
if !a.enabled {
|
||||
return next
|
||||
}
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if !safeMethod(r.Method) && !sameOrigin(r) {
|
||||
s.Log.Warn("rejected cross-origin request", "method", r.Method, "path", r.URL.Path)
|
||||
http.Error(w, "forbidden: cross-origin request", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
if isOpenPath(r) {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
if c, err := r.Cookie(sessionCookieName); err == nil {
|
||||
if user, ok := a.verify(c.Value); ok {
|
||||
next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), ctxKey{}, user)))
|
||||
return
|
||||
}
|
||||
}
|
||||
// htmx polls fragments in the background: answer with an
|
||||
// HX-Redirect instead of a redirect whose login page would be
|
||||
// swapped into the fragment.
|
||||
if r.Header.Get("HX-Request") == "true" {
|
||||
w.Header().Set("HX-Redirect", "/login")
|
||||
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
target := "/login"
|
||||
if r.Method == http.MethodGet {
|
||||
if n := r.URL.RequestURI(); n != "/" {
|
||||
target += "?next=" + url.QueryEscape(n)
|
||||
}
|
||||
}
|
||||
http.Redirect(w, r, target, http.StatusSeeOther)
|
||||
})
|
||||
}
|
||||
|
||||
func isOpenPath(r *http.Request) bool {
|
||||
switch r.URL.Path {
|
||||
case "/login":
|
||||
return r.Method == http.MethodGet || r.Method == http.MethodPost
|
||||
case "/logout":
|
||||
return r.Method == http.MethodPost
|
||||
}
|
||||
return strings.HasPrefix(r.URL.Path, "/static/") && (r.Method == http.MethodGet || r.Method == http.MethodHead)
|
||||
}
|
||||
|
||||
func safeMethod(m string) bool {
|
||||
return m == http.MethodGet || m == http.MethodHead || m == http.MethodOptions
|
||||
}
|
||||
|
||||
// sameOrigin implements the CSRF check: Origin (or, when absent, Referer)
|
||||
// must name this very host. Browsers always send Origin on cross-site
|
||||
// POSTs; a request with neither header is refused.
|
||||
func sameOrigin(r *http.Request) bool {
|
||||
h := r.Header.Get("Origin")
|
||||
if h == "" {
|
||||
h = r.Header.Get("Referer")
|
||||
}
|
||||
if h == "" || h == "null" {
|
||||
return false
|
||||
}
|
||||
u, err := url.Parse(h)
|
||||
if err != nil || u.Host == "" {
|
||||
return false
|
||||
}
|
||||
return strings.EqualFold(u.Host, r.Host)
|
||||
}
|
||||
|
||||
// safeNext returns next only if it is a same-origin relative path, so the
|
||||
// post-login redirect can never leave the site.
|
||||
func safeNext(next string) string {
|
||||
if next == "" || next[0] != '/' || strings.HasPrefix(next, "//") || strings.HasPrefix(next, "/\\") {
|
||||
return "/"
|
||||
}
|
||||
for _, c := range next {
|
||||
if c < 0x20 || c == 0x7f || c == '\\' {
|
||||
return "/"
|
||||
}
|
||||
}
|
||||
u, err := url.Parse(next)
|
||||
if err != nil || u.Scheme != "" || u.Host != "" {
|
||||
return "/"
|
||||
}
|
||||
return next
|
||||
}
|
||||
|
||||
// ---- brute-force throttle ----
|
||||
|
||||
type loginThrottle struct {
|
||||
mu sync.Mutex
|
||||
failures map[string][]time.Time
|
||||
}
|
||||
|
||||
func newLoginThrottle() *loginThrottle {
|
||||
return &loginThrottle{failures: map[string][]time.Time{}}
|
||||
}
|
||||
|
||||
// prune drops expired failures (all clients) — caller holds mu. The map is
|
||||
// bounded by the number of distinct IPs that failed in the last window.
|
||||
func (t *loginThrottle) prune(now time.Time) {
|
||||
for ip, fs := range t.failures {
|
||||
i := 0
|
||||
for i < len(fs) && now.Sub(fs[i]) >= loginFailureWindow {
|
||||
i++
|
||||
}
|
||||
if i == len(fs) {
|
||||
delete(t.failures, ip)
|
||||
} else if i > 0 {
|
||||
t.failures[ip] = fs[i:]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// blocked reports whether ip is locked out, and for how long.
|
||||
func (t *loginThrottle) blocked(ip string, now time.Time) (bool, time.Duration) {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
t.prune(now)
|
||||
fs := t.failures[ip]
|
||||
if len(fs) < maxLoginFailures {
|
||||
return false, 0
|
||||
}
|
||||
return true, fs[0].Add(loginFailureWindow).Sub(now)
|
||||
}
|
||||
|
||||
func (t *loginThrottle) fail(ip string, now time.Time) {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
t.prune(now)
|
||||
t.failures[ip] = append(t.failures[ip], now)
|
||||
}
|
||||
|
||||
func (t *loginThrottle) clear(ip string) {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
delete(t.failures, ip)
|
||||
}
|
||||
|
||||
func clientIP(r *http.Request) string {
|
||||
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
||||
if err != nil {
|
||||
return r.RemoteAddr
|
||||
}
|
||||
return host
|
||||
}
|
||||
|
||||
// ---- handlers ----
|
||||
|
||||
type loginPageData struct {
|
||||
Error string
|
||||
Next string
|
||||
}
|
||||
|
||||
func (s *Server) renderLogin(w http.ResponseWriter, status int, data loginPageData) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
w.WriteHeader(status)
|
||||
if err := s.tmpl.ExecuteTemplate(w, "login_page", data); err != nil {
|
||||
s.Log.Error("render login", "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) handleLoginPage(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.auth.enabled {
|
||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
if c, err := r.Cookie(sessionCookieName); err == nil {
|
||||
if _, ok := s.auth.verify(c.Value); ok {
|
||||
http.Redirect(w, r, safeNext(r.URL.Query().Get("next")), http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
}
|
||||
s.renderLogin(w, http.StatusOK, loginPageData{Next: safeNext(r.URL.Query().Get("next"))})
|
||||
}
|
||||
|
||||
func (s *Server) handleLoginSubmit(w http.ResponseWriter, r *http.Request) {
|
||||
a := s.auth
|
||||
if !a.enabled {
|
||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 4096)
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, "bad request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
next := safeNext(r.PostForm.Get("next"))
|
||||
ip := clientIP(r)
|
||||
now := a.now()
|
||||
|
||||
if blocked, wait := a.throttle.blocked(ip, now); blocked {
|
||||
secs := int(wait.Seconds()) + 1
|
||||
w.Header().Set("Retry-After", strconv.Itoa(secs))
|
||||
s.Log.Warn("login throttled", "remote", ip)
|
||||
s.renderLogin(w, http.StatusTooManyRequests, loginPageData{Next: next, Error: "Слишком много попыток входа. Повторите позже."})
|
||||
return
|
||||
}
|
||||
user := r.PostForm.Get("username")
|
||||
if !a.checkCredentials(user, r.PostForm.Get("password")) {
|
||||
a.throttle.fail(ip, now)
|
||||
s.Log.Warn("login failed", "remote", ip)
|
||||
s.renderLogin(w, http.StatusOK, loginPageData{Next: next, Error: "Неверный логин или пароль"})
|
||||
return
|
||||
}
|
||||
a.throttle.clear(ip)
|
||||
a.setCookie(w, r, user)
|
||||
http.Redirect(w, r, next, http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func (s *Server) handleLogout(w http.ResponseWriter, r *http.Request) {
|
||||
clearCookie(w, r)
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// withAuthInfo returns a copy of the page-data struct data with its embedded
|
||||
// PageData's AuthEnabled/User filled in, so the sidebar can show the logout
|
||||
// control. Data without an embedded PageData is returned unchanged.
|
||||
func (s *Server) withAuthInfo(r *http.Request, data interface{}) interface{} {
|
||||
if !s.auth.enabled || data == nil {
|
||||
return data
|
||||
}
|
||||
v := reflect.ValueOf(data)
|
||||
if v.Kind() != reflect.Struct {
|
||||
return data
|
||||
}
|
||||
cp := reflect.New(v.Type()).Elem()
|
||||
cp.Set(v)
|
||||
pd := cp.FieldByName("PageData")
|
||||
if !pd.IsValid() || pd.Type() != reflect.TypeOf(PageData{}) {
|
||||
return data
|
||||
}
|
||||
pd.FieldByName("AuthEnabled").SetBool(true)
|
||||
pd.FieldByName("User").SetString(userFromRequest(r))
|
||||
return cp.Interface()
|
||||
}
|
||||
@@ -0,0 +1,422 @@
|
||||
package dashboard
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
testUser = "admin"
|
||||
testPass = "correct horse battery staple"
|
||||
testSecret = "test-session-secret"
|
||||
)
|
||||
|
||||
func newAuthTestServer(t *testing.T, caURL string, mutate func(*Config)) (*Server, *httptest.Server) {
|
||||
t.Helper()
|
||||
cfg := Config{
|
||||
ControlAPIBaseURL: caURL,
|
||||
ControlAPITimeout: 5 * time.Second,
|
||||
LastCompletedCount: 20,
|
||||
OverviewPollIntervalS: 5,
|
||||
ControlAPIToken: "ca-admin-token",
|
||||
Username: testUser,
|
||||
Password: testPass,
|
||||
SessionSecret: testSecret,
|
||||
SessionTTL: time.Hour,
|
||||
}
|
||||
if mutate != nil {
|
||||
mutate(&cfg)
|
||||
}
|
||||
log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError}))
|
||||
srv, err := New(cfg, log)
|
||||
if err != nil {
|
||||
t.Fatalf("new dashboard server: %v", err)
|
||||
}
|
||||
ts := httptest.NewServer(srv.Handler())
|
||||
t.Cleanup(ts.Close)
|
||||
return srv, ts
|
||||
}
|
||||
|
||||
// noFollow is a client that returns redirects as-is.
|
||||
func noFollow(ts *httptest.Server) *http.Client {
|
||||
c := *ts.Client()
|
||||
c.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }
|
||||
return &c
|
||||
}
|
||||
|
||||
type reqOpts struct {
|
||||
method string
|
||||
path string
|
||||
form url.Values
|
||||
cookie string
|
||||
headers map[string]string
|
||||
}
|
||||
|
||||
func doReq(t *testing.T, ts *httptest.Server, o reqOpts) (*http.Response, string) {
|
||||
t.Helper()
|
||||
if o.method == "" {
|
||||
o.method = http.MethodGet
|
||||
}
|
||||
var body io.Reader
|
||||
if o.form != nil {
|
||||
body = strings.NewReader(o.form.Encode())
|
||||
}
|
||||
req, err := http.NewRequest(o.method, ts.URL+o.path, body)
|
||||
if err != nil {
|
||||
t.Fatalf("new request: %v", err)
|
||||
}
|
||||
if o.form != nil {
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
}
|
||||
if o.cookie != "" {
|
||||
req.AddCookie(&http.Cookie{Name: sessionCookieName, Value: o.cookie})
|
||||
}
|
||||
for k, v := range o.headers {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
resp, err := noFollow(ts).Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("%s %s: %v", o.method, o.path, err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
b, _ := io.ReadAll(resp.Body)
|
||||
return resp, string(b)
|
||||
}
|
||||
|
||||
func sessionCookie(resp *http.Response) *http.Cookie {
|
||||
for _, c := range resp.Cookies() {
|
||||
if c.Name == sessionCookieName {
|
||||
return c
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func loginForm(user, pass string) url.Values {
|
||||
return url.Values{"username": {user}, "password": {pass}}
|
||||
}
|
||||
|
||||
// login performs a successful login and returns the session cookie value.
|
||||
func login(t *testing.T, ts *httptest.Server) string {
|
||||
t.Helper()
|
||||
resp, _ := doReq(t, ts, reqOpts{method: http.MethodPost, path: "/login", form: loginForm(testUser, testPass),
|
||||
headers: map[string]string{"Origin": ts.URL}})
|
||||
c := sessionCookie(resp)
|
||||
if resp.StatusCode != http.StatusSeeOther || c == nil {
|
||||
t.Fatalf("login: status=%d cookie=%v, want 303 + session cookie", resp.StatusCode, c)
|
||||
}
|
||||
return c.Value
|
||||
}
|
||||
|
||||
func TestUnauthenticatedRedirectsToLogin(t *testing.T) {
|
||||
_, caURL := newFakeControlAPI(t)
|
||||
_, ts := newAuthTestServer(t, caURL, nil)
|
||||
|
||||
resp, _ := doReq(t, ts, reqOpts{path: "/ips"})
|
||||
if resp.StatusCode != http.StatusSeeOther {
|
||||
t.Fatalf("status=%d, want 303", resp.StatusCode)
|
||||
}
|
||||
if loc := resp.Header.Get("Location"); loc != "/login?next=%2Fips" {
|
||||
t.Fatalf("Location=%q, want /login?next=%%2Fips", loc)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnauthenticatedHTMXGets401WithHXRedirect(t *testing.T) {
|
||||
_, caURL := newFakeControlAPI(t)
|
||||
_, ts := newAuthTestServer(t, caURL, nil)
|
||||
|
||||
resp, _ := doReq(t, ts, reqOpts{path: "/overview/fragment", headers: map[string]string{"HX-Request": "true"}})
|
||||
if resp.StatusCode != http.StatusUnauthorized || resp.Header.Get("HX-Redirect") != "/login" {
|
||||
t.Fatalf("status=%d HX-Redirect=%q, want 401 + /login", resp.StatusCode, resp.Header.Get("HX-Redirect"))
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginSuccessSetsCookieAndGrantsAccess(t *testing.T) {
|
||||
_, caURL := newFakeControlAPI(t)
|
||||
_, ts := newAuthTestServer(t, caURL, nil)
|
||||
|
||||
resp, _ := doReq(t, ts, reqOpts{method: http.MethodPost, path: "/login",
|
||||
form: url.Values{"username": {testUser}, "password": {testPass}, "next": {"/ips"}},
|
||||
headers: map[string]string{"Origin": ts.URL}})
|
||||
if resp.StatusCode != http.StatusSeeOther || resp.Header.Get("Location") != "/ips" {
|
||||
t.Fatalf("status=%d Location=%q, want 303 /ips", resp.StatusCode, resp.Header.Get("Location"))
|
||||
}
|
||||
c := sessionCookie(resp)
|
||||
if c == nil {
|
||||
t.Fatal("no session cookie")
|
||||
}
|
||||
if !c.HttpOnly || c.SameSite != http.SameSiteStrictMode || c.Path != "/" || c.MaxAge <= 0 {
|
||||
t.Fatalf("cookie flags: httponly=%v samesite=%v path=%q maxage=%d", c.HttpOnly, c.SameSite, c.Path, c.MaxAge)
|
||||
}
|
||||
if c.Secure {
|
||||
t.Fatal("cookie must not be Secure over plain HTTP")
|
||||
}
|
||||
|
||||
resp, body := doReq(t, ts, reqOpts{path: "/overview", cookie: c.Value})
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("authenticated GET /overview: status=%d", resp.StatusCode)
|
||||
}
|
||||
if !strings.Contains(body, "Выйти") || !strings.Contains(body, testUser) {
|
||||
t.Fatal("sidebar must show the user and the logout button when auth is enabled")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCookieSecureBehindHTTPSProxy(t *testing.T) {
|
||||
_, caURL := newFakeControlAPI(t)
|
||||
_, ts := newAuthTestServer(t, caURL, nil)
|
||||
resp, _ := doReq(t, ts, reqOpts{method: http.MethodPost, path: "/login", form: loginForm(testUser, testPass),
|
||||
headers: map[string]string{"Origin": ts.URL, "X-Forwarded-Proto": "https"}})
|
||||
if c := sessionCookie(resp); c == nil || !c.Secure {
|
||||
t.Fatalf("cookie=%v, want Secure with X-Forwarded-Proto=https", c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginWrongPasswordShowsErrorWithoutCookie(t *testing.T) {
|
||||
_, caURL := newFakeControlAPI(t)
|
||||
_, ts := newAuthTestServer(t, caURL, nil)
|
||||
|
||||
for _, f := range []url.Values{loginForm(testUser, "nope"), loginForm("nobody", testPass)} {
|
||||
resp, body := doReq(t, ts, reqOpts{method: http.MethodPost, path: "/login", form: f,
|
||||
headers: map[string]string{"Origin": ts.URL}})
|
||||
if resp.StatusCode != http.StatusOK || sessionCookie(resp) != nil {
|
||||
t.Fatalf("status=%d cookie=%v, want 200 and no cookie", resp.StatusCode, sessionCookie(resp))
|
||||
}
|
||||
if !strings.Contains(body, "Неверный логин или пароль") {
|
||||
t.Fatal("login page must show the error")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTamperedAndExpiredCookiesRejected(t *testing.T) {
|
||||
_, caURL := newFakeControlAPI(t)
|
||||
srv, ts := newAuthTestServer(t, caURL, nil)
|
||||
good := login(t, ts)
|
||||
|
||||
payload, sig, _ := strings.Cut(good, ".")
|
||||
for name, v := range map[string]string{
|
||||
"tampered payload": "AAAA" + payload[4:] + "." + sig,
|
||||
"tampered signature": payload + "." + sig[:len(sig)-2] + "AA",
|
||||
"no signature": payload,
|
||||
"garbage": "x.y",
|
||||
"signed with new key": otherKeyCookie(t),
|
||||
} {
|
||||
resp, _ := doReq(t, ts, reqOpts{path: "/overview", cookie: v})
|
||||
if resp.StatusCode != http.StatusSeeOther {
|
||||
t.Fatalf("%s: status=%d, want 303 to /login", name, resp.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
// Expiry: advance the server's clock beyond the TTL.
|
||||
srv.auth.now = func() time.Time { return time.Now().Add(2 * time.Hour) }
|
||||
resp, _ := doReq(t, ts, reqOpts{path: "/overview", cookie: good})
|
||||
if resp.StatusCode != http.StatusSeeOther {
|
||||
t.Fatalf("expired cookie: status=%d, want 303", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
func otherKeyCookie(t *testing.T) string {
|
||||
t.Helper()
|
||||
a := newAuthState(Config{Username: "u", Password: "p", SessionSecret: "another-secret"},
|
||||
slog.New(slog.NewTextHandler(io.Discard, nil)))
|
||||
v, _ := a.issue(testUser)
|
||||
return v
|
||||
}
|
||||
|
||||
func TestLogoutClearsCookie(t *testing.T) {
|
||||
_, caURL := newFakeControlAPI(t)
|
||||
_, ts := newAuthTestServer(t, caURL, nil)
|
||||
good := login(t, ts)
|
||||
|
||||
resp, _ := doReq(t, ts, reqOpts{method: http.MethodPost, path: "/logout", cookie: good,
|
||||
headers: map[string]string{"Origin": ts.URL}})
|
||||
c := sessionCookie(resp)
|
||||
if resp.StatusCode != http.StatusSeeOther || resp.Header.Get("Location") != "/login" || c == nil || c.MaxAge >= 0 || c.Value != "" {
|
||||
t.Fatalf("logout: status=%d loc=%q cookie=%+v", resp.StatusCode, resp.Header.Get("Location"), c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCSRFOriginCheck(t *testing.T) {
|
||||
_, caURL := newFakeControlAPI(t)
|
||||
_, ts := newAuthTestServer(t, caURL, nil)
|
||||
good := login(t, ts)
|
||||
|
||||
cases := map[string]map[string]string{
|
||||
"foreign origin": {"Origin": "http://evil.example"},
|
||||
"null origin": {"Origin": "null"},
|
||||
"no origin/referer": {},
|
||||
"foreign referer": {"Referer": "http://evil.example/page"},
|
||||
}
|
||||
for name, h := range cases {
|
||||
resp, _ := doReq(t, ts, reqOpts{method: http.MethodPost, path: "/ips/clear", cookie: good, headers: h})
|
||||
if resp.StatusCode != http.StatusForbidden {
|
||||
t.Fatalf("%s: status=%d, want 403", name, resp.StatusCode)
|
||||
}
|
||||
}
|
||||
// Login itself is covered too.
|
||||
resp, _ := doReq(t, ts, reqOpts{method: http.MethodPost, path: "/login", form: loginForm(testUser, testPass),
|
||||
headers: map[string]string{"Origin": "http://evil.example"}})
|
||||
if resp.StatusCode != http.StatusForbidden || sessionCookie(resp) != nil {
|
||||
t.Fatalf("cross-origin login: status=%d, want 403 and no cookie", resp.StatusCode)
|
||||
}
|
||||
// Same-origin Origin, and Referer fallback, pass.
|
||||
for name, h := range map[string]map[string]string{
|
||||
"origin": {"Origin": ts.URL},
|
||||
"referer": {"Referer": ts.URL + "/ips"},
|
||||
} {
|
||||
resp, _ := doReq(t, ts, reqOpts{method: http.MethodPost, path: "/ips/clear", cookie: good, headers: h})
|
||||
if resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusSeeOther {
|
||||
t.Fatalf("same-origin %s: status=%d, want request to be served", name, resp.StatusCode)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginThrottle(t *testing.T) {
|
||||
_, caURL := newFakeControlAPI(t)
|
||||
_, ts := newAuthTestServer(t, caURL, nil)
|
||||
|
||||
for i := 0; i < maxLoginFailures; i++ {
|
||||
resp, _ := doReq(t, ts, reqOpts{method: http.MethodPost, path: "/login", form: loginForm(testUser, "bad"),
|
||||
headers: map[string]string{"Origin": ts.URL}})
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("attempt %d: status=%d, want 200", i+1, resp.StatusCode)
|
||||
}
|
||||
}
|
||||
// Now locked out, even with the right password.
|
||||
resp, _ := doReq(t, ts, reqOpts{method: http.MethodPost, path: "/login", form: loginForm(testUser, testPass),
|
||||
headers: map[string]string{"Origin": ts.URL}})
|
||||
if resp.StatusCode != http.StatusTooManyRequests || resp.Header.Get("Retry-After") == "" || sessionCookie(resp) != nil {
|
||||
t.Fatalf("status=%d Retry-After=%q, want 429 with Retry-After and no cookie", resp.StatusCode, resp.Header.Get("Retry-After"))
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginThrottleClearedBySuccessAndExpires(t *testing.T) {
|
||||
th := newLoginThrottle()
|
||||
now := time.Now()
|
||||
for i := 0; i < maxLoginFailures-1; i++ {
|
||||
th.fail("1.2.3.4", now)
|
||||
}
|
||||
if b, _ := th.blocked("1.2.3.4", now); b {
|
||||
t.Fatal("blocked before reaching the limit")
|
||||
}
|
||||
th.fail("1.2.3.4", now)
|
||||
if b, _ := th.blocked("1.2.3.4", now); !b {
|
||||
t.Fatal("not blocked at the limit")
|
||||
}
|
||||
if b, _ := th.blocked("1.2.3.4", now.Add(loginFailureWindow+time.Second)); b {
|
||||
t.Fatal("still blocked after the window")
|
||||
}
|
||||
if len(th.failures) != 0 {
|
||||
t.Fatal("expired entries must be pruned")
|
||||
}
|
||||
th.fail("5.6.7.8", now)
|
||||
th.clear("5.6.7.8")
|
||||
if len(th.failures) != 0 {
|
||||
t.Fatal("clear must drop the entry")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStaticAndLoginPageOpen(t *testing.T) {
|
||||
_, caURL := newFakeControlAPI(t)
|
||||
_, ts := newAuthTestServer(t, caURL, nil)
|
||||
|
||||
if resp, _ := doReq(t, ts, reqOpts{path: "/static/dashboard.css"}); resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("/static/dashboard.css: status=%d, want 200", resp.StatusCode)
|
||||
}
|
||||
resp, body := doReq(t, ts, reqOpts{path: "/login"})
|
||||
if resp.StatusCode != http.StatusOK || !strings.Contains(body, `name="password"`) {
|
||||
t.Fatalf("/login: status=%d", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginNextOpenRedirectRejected(t *testing.T) {
|
||||
for next, want := range map[string]string{
|
||||
"/ips": "/ips",
|
||||
"/ips?q=1": "/ips?q=1",
|
||||
"//evil.example": "/",
|
||||
"/\\evil.example": "/",
|
||||
"http://evil.example": "/",
|
||||
"https://evil.example/x": "/",
|
||||
"javascript:alert(1)": "/",
|
||||
"": "/",
|
||||
"/a\r\nSet-Cookie: x": "/",
|
||||
} {
|
||||
if got := safeNext(next); got != want {
|
||||
t.Fatalf("safeNext(%q)=%q, want %q", next, got, want)
|
||||
}
|
||||
}
|
||||
|
||||
_, caURL := newFakeControlAPI(t)
|
||||
_, ts := newAuthTestServer(t, caURL, nil)
|
||||
resp, _ := doReq(t, ts, reqOpts{method: http.MethodPost, path: "/login",
|
||||
form: url.Values{"username": {testUser}, "password": {testPass}, "next": {"//evil.example"}},
|
||||
headers: map[string]string{"Origin": ts.URL}})
|
||||
if resp.Header.Get("Location") != "/" {
|
||||
t.Fatalf("Location=%q, want /", resp.Header.Get("Location"))
|
||||
}
|
||||
}
|
||||
|
||||
func TestControlAPITokenForwarded(t *testing.T) {
|
||||
fake, caURL := newFakeControlAPI(t)
|
||||
_, ts := newAuthTestServer(t, caURL, nil)
|
||||
good := login(t, ts)
|
||||
|
||||
if resp, _ := doReq(t, ts, reqOpts{path: "/overview", cookie: good}); resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("GET /overview: status=%d", resp.StatusCode)
|
||||
}
|
||||
fake.authMu.Lock()
|
||||
defer fake.authMu.Unlock()
|
||||
if len(fake.authHeaders) == 0 {
|
||||
t.Fatal("control-api was never called")
|
||||
}
|
||||
for _, h := range fake.authHeaders {
|
||||
if h != "Bearer ca-admin-token" {
|
||||
t.Fatalf("control-api Authorization=%q, want Bearer ca-admin-token", h)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthDisabledWithZeroConfig(t *testing.T) {
|
||||
fake, caURL := newFakeControlAPI(t)
|
||||
ts := newTestServer(t, caURL) // zero auth fields
|
||||
resp, body := doReq(t, ts, reqOpts{path: "/overview"})
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("status=%d, want 200 without login", resp.StatusCode)
|
||||
}
|
||||
if strings.Contains(body, "/logout") {
|
||||
t.Fatal("logout control must be hidden when auth is disabled")
|
||||
}
|
||||
if resp, _ := doReq(t, ts, reqOpts{path: "/login"}); resp.StatusCode != http.StatusSeeOther || resp.Header.Get("Location") != "/" {
|
||||
t.Fatalf("/login with auth disabled: status=%d loc=%q, want redirect to /", resp.StatusCode, resp.Header.Get("Location"))
|
||||
}
|
||||
fake.authMu.Lock()
|
||||
defer fake.authMu.Unlock()
|
||||
for _, h := range fake.authHeaders {
|
||||
if h != "" {
|
||||
t.Fatalf("no token configured but Authorization=%q was sent", h)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthDisabledWhenOnlyUsernameSet(t *testing.T) {
|
||||
_, caURL := newFakeControlAPI(t)
|
||||
_, ts := newAuthTestServer(t, caURL, func(c *Config) { c.Password = "" })
|
||||
if resp, _ := doReq(t, ts, reqOpts{path: "/overview"}); resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("status=%d, want 200 (auth disabled without password)", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptySessionSecretUsesRandomKey(t *testing.T) {
|
||||
_, caURL := newFakeControlAPI(t)
|
||||
_, ts := newAuthTestServer(t, caURL, func(c *Config) { c.SessionSecret = "" })
|
||||
good := login(t, ts)
|
||||
if resp, _ := doReq(t, ts, reqOpts{path: "/overview", cookie: good}); resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("status=%d, want 200 with random-key session", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,8 @@ func (e *apiErr) Error() string {
|
||||
type client struct {
|
||||
baseURL string
|
||||
http *http.Client
|
||||
// token, when non-empty, is sent to control-api as a Bearer credential.
|
||||
token string
|
||||
}
|
||||
|
||||
func newClient(baseURL string, timeout time.Duration) *client {
|
||||
@@ -61,6 +63,9 @@ func (c *client) do(ctx context.Context, method, path string, body, out interfac
|
||||
if body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
if c.token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+c.token)
|
||||
}
|
||||
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
|
||||
@@ -46,6 +46,10 @@ type fakeControlAPI struct {
|
||||
// autoCycleDown makes all four endpoints answer 500 (unavailable API).
|
||||
autoCycle autoCycleDTO
|
||||
autoCycleDown bool
|
||||
|
||||
// authHeaders records the Authorization header of every request served.
|
||||
authMu sync.Mutex
|
||||
authHeaders []string
|
||||
}
|
||||
|
||||
func newFakeControlAPI(t *testing.T) (*fakeControlAPI, string) {
|
||||
@@ -532,7 +536,12 @@ func (f *fakeControlAPI) handler() http.Handler {
|
||||
writeJSON(w, http.StatusOK, map[string]bool{"ok": true})
|
||||
})
|
||||
|
||||
return mux
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
f.authMu.Lock()
|
||||
f.authHeaders = append(f.authHeaders, r.Header.Get("Authorization"))
|
||||
f.authMu.Unlock()
|
||||
mux.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func (f *fakeControlAPI) findIP(addr string) int {
|
||||
|
||||
@@ -31,7 +31,7 @@ func (s *Server) handleCheckTypesPage(w http.ResponseWriter, r *http.Request) {
|
||||
data, err := s.loadCheckTypesPage(r)
|
||||
data.ActiveNav = "check-types"
|
||||
data.Banner = bannerFor(err)
|
||||
s.renderPage(w, "checktypes_page", data)
|
||||
s.renderPage(w, r, "checktypes_page", data)
|
||||
}
|
||||
|
||||
func (s *Server) renderCheckTypesTable(w http.ResponseWriter, r *http.Request, actionErr error) {
|
||||
|
||||
@@ -25,7 +25,7 @@ func (s *Server) handleIPsPage(w http.ResponseWriter, r *http.Request) {
|
||||
data := ipsPageData{Items: items, FIPSettleSeconds: settings.FIPSettleSeconds}
|
||||
data.ActiveNav = "ips"
|
||||
data.Banner = bannerFor(err)
|
||||
s.renderPage(w, "ips_page", data)
|
||||
s.renderPage(w, r, "ips_page", data)
|
||||
}
|
||||
|
||||
func (s *Server) handleIPDetail(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -34,7 +34,7 @@ func (s *Server) handleIPDetail(w http.ResponseWriter, r *http.Request) {
|
||||
data := ipDetailData{Detail: detail}
|
||||
data.ActiveNav = "ips"
|
||||
data.Banner = bannerFor(err)
|
||||
s.renderPage(w, "ip_detail_page", data)
|
||||
s.renderPage(w, r, "ip_detail_page", data)
|
||||
}
|
||||
|
||||
// renderIPsTable re-fetches the current queue and renders the ips_table
|
||||
|
||||
@@ -57,7 +57,7 @@ func (s *Server) handleOverview(w http.ResponseWriter, r *http.Request) {
|
||||
data, err := s.loadOverview(r)
|
||||
data.ActiveNav = "overview"
|
||||
data.Banner = bannerFor(err)
|
||||
s.renderPage(w, "overview_page", data)
|
||||
s.renderPage(w, r, "overview_page", data)
|
||||
}
|
||||
|
||||
// handleOverviewFragment serves both the recurring poll and every
|
||||
|
||||
@@ -34,7 +34,7 @@ func (s *Server) handleRegistryPage(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
data.ActiveNav = "registry"
|
||||
data.Banner = bannerFor(err)
|
||||
s.renderPage(w, "registry_page", data)
|
||||
s.renderPage(w, r, "registry_page", data)
|
||||
}
|
||||
|
||||
// filterRegistryItems narrows items to those whose address contains q
|
||||
@@ -68,5 +68,5 @@ func (s *Server) handleRegistryDetail(w http.ResponseWriter, r *http.Request) {
|
||||
data := registryDetailData{History: history}
|
||||
data.ActiveNav = "registry"
|
||||
data.Banner = bannerFor(err)
|
||||
s.renderPage(w, "registry_detail_page", data)
|
||||
s.renderPage(w, r, "registry_detail_page", data)
|
||||
}
|
||||
@@ -29,7 +29,7 @@ func (s *Server) handleSettingsPage(w http.ResponseWriter, r *http.Request) {
|
||||
data := settingsPageData{Settings: settings, Inbound: inbound, AutoCycle: autoCycle}
|
||||
data.ActiveNav = "settings"
|
||||
data.Banner = bannerFor(err)
|
||||
s.renderPage(w, "settings_page", data)
|
||||
s.renderPage(w, r, "settings_page", data)
|
||||
}
|
||||
|
||||
// renderSettingsForm re-fetches the current settings and renders the
|
||||
|
||||
@@ -16,7 +16,7 @@ func (s *Server) handleSitesPage(w http.ResponseWriter, r *http.Request) {
|
||||
data := sitesPageData{Items: items}
|
||||
data.ActiveNav = "sites"
|
||||
data.Banner = bannerFor(err)
|
||||
s.renderPage(w, "sites_page", data)
|
||||
s.renderPage(w, r, "sites_page", data)
|
||||
}
|
||||
|
||||
func (s *Server) renderSitesTable(w http.ResponseWriter, r *http.Request, actionErr error) {
|
||||
|
||||
@@ -83,7 +83,7 @@ func (s *Server) handleTargetsPage(w http.ResponseWriter, r *http.Request) {
|
||||
data, err := s.loadTargetsPage(r)
|
||||
data.ActiveNav = "targets"
|
||||
data.Banner = bannerFor(err)
|
||||
s.renderPage(w, "targets_page", data)
|
||||
s.renderPage(w, r, "targets_page", data)
|
||||
}
|
||||
|
||||
// renderTargetsTable renders the #targets-table-wrap swap target plus,
|
||||
|
||||
@@ -15,7 +15,7 @@ func (s *Server) handleValidatorsPage(w http.ResponseWriter, r *http.Request) {
|
||||
data := validatorsPageData{Items: items}
|
||||
data.ActiveNav = "validators"
|
||||
data.Banner = bannerFor(err)
|
||||
s.renderPage(w, "validators_page", data)
|
||||
s.renderPage(w, r, "validators_page", data)
|
||||
}
|
||||
|
||||
func (s *Server) renderValidatorsTable(w http.ResponseWriter, r *http.Request, actionErr error) {
|
||||
|
||||
@@ -155,6 +155,10 @@ type bannerData struct {
|
||||
type PageData struct {
|
||||
Banner bannerData
|
||||
ActiveNav string
|
||||
// AuthEnabled/User are filled in by renderPage (see withAuthInfo) so the
|
||||
// sidebar can show the signed-in user and the logout button.
|
||||
AuthEnabled bool
|
||||
User string
|
||||
}
|
||||
|
||||
func bannerFor(err error) bannerData {
|
||||
@@ -172,8 +176,9 @@ func bannerFor(err error) bannerData {
|
||||
// navigation. actionErr (if any — e.g. the primary control-api call for
|
||||
// this page failed) is surfaced via the embedded PageData.Banner, which
|
||||
// the caller must have already set via bannerFor.
|
||||
func (s *Server) renderPage(w http.ResponseWriter, name string, data interface{}) {
|
||||
func (s *Server) renderPage(w http.ResponseWriter, r *http.Request, name string, data interface{}) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
data = s.withAuthInfo(r, data)
|
||||
if err := s.tmpl.ExecuteTemplate(w, name, data); err != nil {
|
||||
s.Log.Error("render page", "template", name, "err", err)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,12 @@ package dashboard
|
||||
import "net/http"
|
||||
|
||||
func (s *Server) routes(mux *http.ServeMux) {
|
||||
// /login, /logout and /static/ are the only routes reachable without a
|
||||
// session — see authMiddleware in auth.go.
|
||||
mux.HandleFunc("GET /login", s.handleLoginPage)
|
||||
mux.HandleFunc("POST /login", s.handleLoginSubmit)
|
||||
mux.HandleFunc("POST /logout", s.handleLogout)
|
||||
|
||||
mux.HandleFunc("GET /{$}", s.handleIndex)
|
||||
|
||||
mux.HandleFunc("GET /overview", s.handleOverview)
|
||||
|
||||
@@ -18,6 +18,19 @@ type Config struct {
|
||||
ControlAPITimeout time.Duration
|
||||
LastCompletedCount int
|
||||
OverviewPollIntervalS int
|
||||
|
||||
// ControlAPIToken is the admin bearer token sent to control-api. Empty =
|
||||
// no Authorization header.
|
||||
ControlAPIToken string
|
||||
// Username/Password are the single dashboard administrator's login. If
|
||||
// either is empty, login is DISABLED (every page is open).
|
||||
Username string
|
||||
Password string
|
||||
// SessionSecret is the HMAC key for session cookies; empty = random per
|
||||
// process start (sessions are lost on restart).
|
||||
SessionSecret string
|
||||
// SessionTTL is the session lifetime (default 8h).
|
||||
SessionTTL time.Duration
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
@@ -25,6 +38,7 @@ type Server struct {
|
||||
Cfg Config
|
||||
tmpl *template.Template
|
||||
Log *slog.Logger
|
||||
auth *authState
|
||||
}
|
||||
|
||||
func New(cfg Config, log *slog.Logger) (*Server, error) {
|
||||
@@ -32,18 +46,22 @@ func New(cfg Config, log *slog.Logger) (*Server, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ca := newClient(cfg.ControlAPIBaseURL, cfg.ControlAPITimeout)
|
||||
ca.token = cfg.ControlAPIToken
|
||||
return &Server{
|
||||
CA: newClient(cfg.ControlAPIBaseURL, cfg.ControlAPITimeout),
|
||||
CA: ca,
|
||||
Cfg: cfg,
|
||||
tmpl: tmpl,
|
||||
Log: log,
|
||||
auth: newAuthState(cfg, log),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Server) Handler() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
s.routes(mux)
|
||||
return loggingMiddleware(s.Log, mux)
|
||||
// Never log cookies, Authorization or form bodies here.
|
||||
return loggingMiddleware(s.Log, s.authMiddleware(mux))
|
||||
}
|
||||
|
||||
func loggingMiddleware(log *slog.Logger, next http.Handler) http.Handler {
|
||||
|
||||
@@ -337,7 +337,7 @@ nav.nav-groups { display: flex; flex-direction: column; gap: 1px; }
|
||||
}
|
||||
.form-check { display: flex; align-items: center; gap: 6px; font-size: 13px; }
|
||||
|
||||
input[type="text"], input[type="number"], input[type="search"], textarea, select {
|
||||
input[type="text"], input[type="password"], input[type="number"], input[type="search"], textarea, select {
|
||||
font-family: var(--font-ui);
|
||||
font-size: 13.5px;
|
||||
padding: 7px 10px;
|
||||
@@ -544,3 +544,18 @@ tr.edit-row textarea { flex: 1 1 auto; }
|
||||
Height is set by JS (see templates/targets.html), computed from the
|
||||
field's own line-height, so it stays correct if font-size ever changes. */
|
||||
textarea.autosize { overflow-y: hidden; resize: none; }
|
||||
|
||||
/* ---------- login page & sidebar user ---------- */
|
||||
.login-shell { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px 16px; position: relative; }
|
||||
.login-card { width: 100%; max-width: 380px; }
|
||||
.login-card h1 { font-size: 18px; margin: 0 0 4px; }
|
||||
.login-card .panel-body { display: flex; flex-direction: column; gap: 14px; }
|
||||
/* .field is flex: 1 1 220px (for rows); in this column the basis would become a 220px height. */
|
||||
.login-card .field { flex: 0 0 auto; }
|
||||
.sidebar-user {
|
||||
margin-top: auto; margin-bottom: 8px;
|
||||
display: flex; align-items: center; justify-content: space-between; gap: 8px;
|
||||
font-family: var(--font-display); font-size: 11px; color: var(--text-muted);
|
||||
}
|
||||
.sidebar-user-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||
.sidebar-user + .sidebar-foot { margin-top: 0; }
|
||||
@@ -78,6 +78,13 @@
|
||||
</a>
|
||||
</nav>
|
||||
|
||||
{{if .AuthEnabled}}
|
||||
<form class="sidebar-user" method="post" action="/logout">
|
||||
<span class="sidebar-user-name" title="{{.User}}">{{.User}}</span>
|
||||
<button type="submit" class="btn btn-ghost btn-sm">Выйти</button>
|
||||
</form>
|
||||
{{end}}
|
||||
|
||||
<div class="sidebar-foot">
|
||||
<span class="sidebar-foot-status"><span class="pulse-dot"></span>control-api</span>
|
||||
<button type="button" class="theme-toggle" id="themeToggle" aria-label="Переключить тему" title="Переключить тему">
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
{{define "login_page"}}
|
||||
<!doctype html>
|
||||
<html lang="ru">
|
||||
<head>{{template "html_head" .}}</head>
|
||||
<body>
|
||||
<div class="bg-grid"></div>
|
||||
<main class="login-shell">
|
||||
<div class="login-card">
|
||||
<form class="panel" method="post" action="/login" autocomplete="on">
|
||||
<div class="panel-head"><h2>Cloud IP Validator — вход</h2></div>
|
||||
<div class="panel-body">
|
||||
{{if .Error}}<div class="alert alert-warning" role="alert">{{.Error}}</div>{{end}}
|
||||
<input type="hidden" name="next" value="{{.Next}}">
|
||||
<div class="field">
|
||||
<label for="login-username">Логин</label>
|
||||
<input type="text" id="login-username" name="username" autocomplete="username" autofocus required>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label for="login-password">Пароль</label>
|
||||
<input type="password" id="login-password" name="password" autocomplete="current-password" required>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary btn-block">Войти</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
{{end}}
|
||||
Reference in new issue
Block a user