feat: HA-Cluster v1.2.x — Split-Brain, TOTP, Enterprise-FW, Drift-Fix, VIP-Recovery
- keepalived: pg_role='standby' hat Vorrang vor role für BACKUP-Bestimmung - keepalived-master.sh: gecrasht Dienste beim MASTER-Übergang starten (nicht nur reload) - confighash: ip_addresses per Interface-Name hashen statt per FK (Cross-Node-Drift-Fix) - TOTP/2FA: RFC 6238 — Setup-Flow, QR-Code, Admin-Disable; two-step Login - Firewall-UI: Enterprise-Design — auto-Beschreibung, icon-only Actions, zero-hit Indikator - fe80-Filter: Link-local IPv6 aus NTP/DNS Listen-Dropdowns entfernen - VIP-Dashboard, Dual-Path VRRP, GW-Tracking (Migrations 0033/0034) - Forward Proxy + DNS erweiterte Einstellungen (Migrations 0031/0032) - unbound-control: edgeguard in unbound-Gruppe via postinst Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -60,15 +60,22 @@ func (h *AuthHandler) WithClusterTLS(store *clustertls.Store) *AuthHandler {
|
||||
return h
|
||||
}
|
||||
|
||||
const totpPendingCookie = "edgeguard_totp_pending"
|
||||
|
||||
// Register mounts /auth/login + /logout (public) and /auth/me
|
||||
// (gated by requireAuth, passed in as a per-route middleware).
|
||||
func (h *AuthHandler) Register(rg *gin.RouterGroup, requireAuth gin.HandlerFunc) {
|
||||
g := rg.Group("/auth")
|
||||
g.POST("/login", h.Login)
|
||||
g.POST("/logout", h.Logout)
|
||||
g.POST("/totp-verify", h.TOTPVerify)
|
||||
g.GET("/me", requireAuth, h.Me)
|
||||
g.POST("/reset-password", h.ResetPassword)
|
||||
g.POST("/change-password", requireAuth, h.ChangePassword)
|
||||
// TOTP self-service (authenticated user manages own 2FA)
|
||||
g.POST("/totp/setup", requireAuth, h.TOTPSetup)
|
||||
g.POST("/totp/confirm", requireAuth, h.TOTPConfirm)
|
||||
g.DELETE("/totp", requireAuth, h.TOTPDisable)
|
||||
}
|
||||
|
||||
type loginRequest struct {
|
||||
@@ -77,9 +84,10 @@ type loginRequest struct {
|
||||
}
|
||||
|
||||
type loginResponse struct {
|
||||
Actor string `json:"actor"`
|
||||
Role string `json:"role"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
Actor string `json:"actor"`
|
||||
Role string `json:"role"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
TOTPRequired bool `json:"totp_required,omitempty"`
|
||||
}
|
||||
|
||||
func (h *AuthHandler) Login(c *gin.Context) {
|
||||
@@ -101,12 +109,13 @@ func (h *AuthHandler) Login(c *gin.Context) {
|
||||
email := strings.TrimSpace(req.Email)
|
||||
actor, role := "", "admin"
|
||||
remote := c.ClientIP()
|
||||
var totpEnabled bool
|
||||
|
||||
// 1. Try DB users table first.
|
||||
if h.Users != nil {
|
||||
u, hash, dbErr := h.Users.FindByEmail(c.Request.Context(), email)
|
||||
ai, dbErr := h.Users.FindForAuth(c.Request.Context(), email)
|
||||
if dbErr == nil {
|
||||
if !u.Active {
|
||||
if !ai.Active {
|
||||
if h.Audit != nil {
|
||||
_ = h.Audit.Log(c.Request.Context(), email, "auth.login.failed",
|
||||
email, gin.H{"reason": "account_disabled", "remote": remote}, h.NodeID)
|
||||
@@ -114,7 +123,7 @@ func (h *AuthHandler) Login(c *gin.Context) {
|
||||
response.Unauthorized(c, errors.New("account_disabled"))
|
||||
return
|
||||
}
|
||||
if !usersvc.VerifyPassword(hash, req.Password) {
|
||||
if !usersvc.VerifyPassword(ai.PasswordHash, req.Password) {
|
||||
if h.Audit != nil {
|
||||
_ = h.Audit.Log(c.Request.Context(), email, "auth.login.failed",
|
||||
email, gin.H{"reason": "invalid_credentials", "remote": remote}, h.NodeID)
|
||||
@@ -122,9 +131,10 @@ func (h *AuthHandler) Login(c *gin.Context) {
|
||||
response.Unauthorized(c, errors.New("invalid_credentials"))
|
||||
return
|
||||
}
|
||||
actor = u.Email
|
||||
role = u.Role
|
||||
h.Users.RecordLogin(c.Request.Context(), u.ID)
|
||||
actor = ai.Email
|
||||
role = ai.Role
|
||||
totpEnabled = ai.TOTPEnabled
|
||||
h.Users.RecordLogin(c.Request.Context(), ai.ID)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,16 +143,13 @@ func (h *AuthHandler) Login(c *gin.Context) {
|
||||
if strings.EqualFold(st.AdminEmail, email) && st.VerifyAdminPassword(req.Password) {
|
||||
actor = st.AdminEmail
|
||||
role = "admin"
|
||||
// Auto-migrate: insert the setup-store admin into the DB so it
|
||||
// shows up in user management from this point on.
|
||||
if h.Users != nil {
|
||||
_, _ = h.Users.Upsert(c.Request.Context(), st.AdminEmail, req.Password, "admin", true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Auth federation: cluster nodes forward failed auth to the primary
|
||||
// via mTLS so users can log in with their primary credentials on any node.
|
||||
// 3. Auth federation: cluster nodes forward failed auth to the primary.
|
||||
if actor == "" && st.IsClusterNode && st.PrimaryFQDN != "" && h.ClusterTLS != nil {
|
||||
if a, r, err := h.checkWithPrimary(c.Request.Context(), st.PrimaryFQDN, email, req.Password); err == nil {
|
||||
actor = a
|
||||
@@ -161,6 +168,21 @@ func (h *AuthHandler) Login(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// TOTP gate: password OK but 2FA required → issue a short-lived pending
|
||||
// cookie and tell the UI to show the TOTP input.
|
||||
if totpEnabled {
|
||||
pending, ptok, err := h.Signer.IssueWithRoleTTL(actor, "totp_pending", 2*time.Minute)
|
||||
if err != nil {
|
||||
response.Internal(c, err)
|
||||
return
|
||||
}
|
||||
c.SetSameSite(http.SameSiteStrictMode)
|
||||
c.SetCookie(totpPendingCookie, pending, int(2*time.Minute/time.Second), "/", "", true, true)
|
||||
_ = ptok
|
||||
response.OK(c, loginResponse{TOTPRequired: true})
|
||||
return
|
||||
}
|
||||
|
||||
raw, tok, err := h.Signer.IssueWithRole(actor, role)
|
||||
if err != nil {
|
||||
response.Internal(c, err)
|
||||
@@ -179,6 +201,146 @@ func (h *AuthHandler) Login(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
type totpVerifyRequest struct {
|
||||
Code string `json:"code" binding:"required"`
|
||||
}
|
||||
|
||||
// TOTPVerify completes the two-step login: verifies the TOTP code from the
|
||||
// pending cookie and, on success, issues a full session JWT.
|
||||
func (h *AuthHandler) TOTPVerify(c *gin.Context) {
|
||||
var req totpVerifyRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, err)
|
||||
return
|
||||
}
|
||||
pendingRaw, err := c.Cookie(totpPendingCookie)
|
||||
if err != nil || pendingRaw == "" {
|
||||
response.Unauthorized(c, errors.New("no_pending_totp"))
|
||||
return
|
||||
}
|
||||
ptok, err := h.Signer.Verify(pendingRaw)
|
||||
if err != nil || ptok.Role != "totp_pending" {
|
||||
response.Unauthorized(c, errors.New("invalid_pending_token"))
|
||||
return
|
||||
}
|
||||
|
||||
if h.Users == nil {
|
||||
response.Internal(c, errors.New("users repo unavailable"))
|
||||
return
|
||||
}
|
||||
ai, err := h.Users.FindForAuth(c.Request.Context(), ptok.Actor)
|
||||
if err != nil || !ai.TOTPEnabled || ai.TOTPSecret == nil {
|
||||
response.Unauthorized(c, errors.New("totp_not_configured"))
|
||||
return
|
||||
}
|
||||
if !usersvc.VerifyTOTP(*ai.TOTPSecret, req.Code) {
|
||||
if h.Audit != nil {
|
||||
_ = h.Audit.Log(c.Request.Context(), ptok.Actor, "auth.totp.failed",
|
||||
ptok.Actor, gin.H{"remote": c.ClientIP()}, h.NodeID)
|
||||
}
|
||||
response.Unauthorized(c, errors.New("invalid_totp_code"))
|
||||
return
|
||||
}
|
||||
|
||||
// Clear pending cookie, issue full session.
|
||||
c.SetSameSite(http.SameSiteStrictMode)
|
||||
c.SetCookie(totpPendingCookie, "", -1, "/", "", true, true)
|
||||
|
||||
raw, tok, err := h.Signer.IssueWithRole(ptok.Actor, ai.Role)
|
||||
if err != nil {
|
||||
response.Internal(c, err)
|
||||
return
|
||||
}
|
||||
setSessionCookie(c, raw, tok.Exp)
|
||||
|
||||
if h.Audit != nil {
|
||||
_ = h.Audit.Log(c.Request.Context(), ptok.Actor, "auth.login.success",
|
||||
ptok.Actor, gin.H{"role": ai.Role, "remote": c.ClientIP(), "totp": true}, h.NodeID)
|
||||
}
|
||||
response.OK(c, loginResponse{
|
||||
Actor: tok.Actor,
|
||||
Role: tok.Role,
|
||||
ExpiresAt: time.Unix(tok.Exp, 0).UTC(),
|
||||
})
|
||||
}
|
||||
|
||||
// TOTPSetup generates a new TOTP secret for the authenticated user and returns
|
||||
// the provisioning URI (renders as QR code in the UI). Secret is not saved yet.
|
||||
func (h *AuthHandler) TOTPSetup(c *gin.Context) {
|
||||
tok := CurrentToken(c)
|
||||
if tok == nil {
|
||||
response.Unauthorized(c, nil)
|
||||
return
|
||||
}
|
||||
secret, uri, err := usersvc.GenerateTOTPSecret(tok.Actor)
|
||||
if err != nil {
|
||||
response.Internal(c, err)
|
||||
return
|
||||
}
|
||||
response.OK(c, gin.H{"secret": secret, "uri": uri})
|
||||
}
|
||||
|
||||
type totpConfirmRequest struct {
|
||||
Secret string `json:"secret" binding:"required"`
|
||||
Code string `json:"code" binding:"required"`
|
||||
}
|
||||
|
||||
// TOTPConfirm verifies the code against the provisioned secret and, on success,
|
||||
// enables TOTP for the user.
|
||||
func (h *AuthHandler) TOTPConfirm(c *gin.Context) {
|
||||
var req totpConfirmRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, err)
|
||||
return
|
||||
}
|
||||
tok := CurrentToken(c)
|
||||
if tok == nil || h.Users == nil {
|
||||
response.Unauthorized(c, nil)
|
||||
return
|
||||
}
|
||||
u, _, err := h.Users.FindByEmail(c.Request.Context(), tok.Actor)
|
||||
if err != nil {
|
||||
response.Internal(c, err)
|
||||
return
|
||||
}
|
||||
if err := h.Users.ConfirmTOTP(c.Request.Context(), u.ID, req.Secret, req.Code); err != nil {
|
||||
if err.Error() == "invalid_totp_code" {
|
||||
response.Err(c, http.StatusUnprocessableEntity, err)
|
||||
return
|
||||
}
|
||||
response.Internal(c, err)
|
||||
return
|
||||
}
|
||||
if h.Audit != nil {
|
||||
_ = h.Audit.Log(c.Request.Context(), tok.Actor, "auth.totp.enabled",
|
||||
tok.Actor, nil, h.NodeID)
|
||||
}
|
||||
response.OK(c, gin.H{"ok": true})
|
||||
}
|
||||
|
||||
// TOTPDisable disables TOTP for the authenticated user.
|
||||
func (h *AuthHandler) TOTPDisable(c *gin.Context) {
|
||||
tok := CurrentToken(c)
|
||||
if tok == nil || h.Users == nil {
|
||||
response.Unauthorized(c, nil)
|
||||
return
|
||||
}
|
||||
u, _, err := h.Users.FindByEmail(c.Request.Context(), tok.Actor)
|
||||
if err != nil {
|
||||
response.Internal(c, err)
|
||||
return
|
||||
}
|
||||
if err := h.Users.DisableTOTP(c.Request.Context(), u.ID); err != nil {
|
||||
response.Internal(c, err)
|
||||
return
|
||||
}
|
||||
if h.Audit != nil {
|
||||
_ = h.Audit.Log(c.Request.Context(), tok.Actor, "auth.totp.disabled",
|
||||
tok.Actor, nil, h.NodeID)
|
||||
}
|
||||
response.OK(c, gin.H{"ok": true})
|
||||
}
|
||||
|
||||
func (h *AuthHandler) Logout(c *gin.Context) {
|
||||
clearSessionCookie(c)
|
||||
response.OK(c, gin.H{"logged_out": true})
|
||||
|
||||
Reference in New Issue
Block a user