Disabling is the right answer for somebody who has left: it keeps the record of who registered, who decided and when, which is the point of having the trail. It is the wrong answer for an account opened to check that a page works. Ten of those had accumulated on this estate, each one disabled and each one still in the list. Two refusals, both in the same transaction as the delete so two administrators racing cannot step through the gap. The last active administrator, because nothing else in the system can grant admin and removing the last one locks everybody out of the accounts page for good. An account whose tenant still holds a cluster. The cluster would go on running, owned by a name nobody can sign in as and reachable by administrators alone, and the person it belongs to could not release it. The message says how many and to release them first. The page keeps the rule the other actions keep - you cannot decide on your own account - and says in one line when to disable and when to delete, because the difference is not obvious from two buttons side by side. The command line takes -delete, and deliberately does not ask for -by: there is nothing left to attribute the decision to. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
252 lines
7.6 KiB
Go
252 lines
7.6 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"errors"
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
"unicode"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"github.com/ilkermanap/hpcod/internal/store"
|
|
)
|
|
|
|
// Signing up, and deciding who gets in.
|
|
//
|
|
// The public page used to ask people to send an email, which is not a
|
|
// process - it is a hope that someone reads their inbox. Anyone can now
|
|
// register, nobody is let in by registering, and an administrator decides.
|
|
//
|
|
// The safety of that rests on one thing being true in the database rather
|
|
// than in this file: a new row is 'pending' and not an administrator,
|
|
// because those come from column defaults that the insert never overrides.
|
|
// A mistake in the handlers below cannot produce a live account.
|
|
|
|
const (
|
|
minPasswordLen = 12
|
|
maxFieldLen = 200
|
|
// One registration at a time from an address, roughly. Enough to stop a
|
|
// script filling the table without making a real person wait.
|
|
signupInterval = 20 * time.Second
|
|
)
|
|
|
|
// signupLimiter is a deliberately small thing: last attempt per address.
|
|
// It is not a defence against a distributed flood, and is not meant to be;
|
|
// it stops the obvious loop.
|
|
//
|
|
// Its zero value works. A limiter that had to be constructed would be a
|
|
// limiter someone could forget to construct, and the failure mode of that
|
|
// is no rate limit at all - which is exactly the wrong way round.
|
|
type signupLimiter struct {
|
|
mu sync.Mutex
|
|
last map[string]time.Time
|
|
}
|
|
|
|
func (l *signupLimiter) allow(ip string) bool {
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
if l.last == nil {
|
|
l.last = map[string]time.Time{}
|
|
}
|
|
now := time.Now()
|
|
if t, ok := l.last[ip]; ok && now.Sub(t) < signupInterval {
|
|
return false
|
|
}
|
|
// Trim while we are here, so the map cannot grow without bound.
|
|
for k, t := range l.last {
|
|
if now.Sub(t) > time.Hour {
|
|
delete(l.last, k)
|
|
}
|
|
}
|
|
l.last[ip] = now
|
|
return true
|
|
}
|
|
|
|
func (s *Server) pageSignup(w http.ResponseWriter, r *http.Request) {
|
|
d := pageData{Page: "signup"}
|
|
if s.store == nil {
|
|
d.Err = "Registration is not available: this portal has no account database."
|
|
s.render(w, "signup.html", d)
|
|
return
|
|
}
|
|
if r.Method != http.MethodPost {
|
|
s.render(w, "signup.html", d)
|
|
return
|
|
}
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
d.Err = "Malformed request."
|
|
s.render(w, "signup.html", d)
|
|
return
|
|
}
|
|
username := strings.TrimSpace(r.PostFormValue("username"))
|
|
email := strings.TrimSpace(r.PostFormValue("email"))
|
|
reason := strings.TrimSpace(r.PostFormValue("reason"))
|
|
pass := r.PostFormValue("password")
|
|
d.Form = map[string]string{"username": username, "email": email, "reason": reason}
|
|
|
|
if err := validSignup(username, pass, email, reason); err != nil {
|
|
d.Err = err.Error()
|
|
s.render(w, "signup.html", d)
|
|
return
|
|
}
|
|
|
|
ip := clientIP(r, s.opt.TrustedProxy)
|
|
if !s.signups.allow(ip) {
|
|
d.Err = "That was quick. Wait a moment and try again."
|
|
s.render(w, "signup.html", d)
|
|
return
|
|
}
|
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(pass), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
d.Err = "Could not store that password."
|
|
s.render(w, "signup.html", d)
|
|
return
|
|
}
|
|
|
|
acct, err := s.store.CreateAccount(r.Context(), username, string(hash), email, reason)
|
|
switch {
|
|
case errors.Is(err, store.ErrAccountExists):
|
|
d.Err = "That username is taken."
|
|
s.render(w, "signup.html", d)
|
|
return
|
|
case err != nil:
|
|
log.Printf("signup: %v", err)
|
|
d.Err = "Could not register that account."
|
|
s.render(w, "signup.html", d)
|
|
return
|
|
}
|
|
|
|
// Belt and braces. The database defaults are what make this true; this
|
|
// is here so that if they are ever changed, it is noticed here and not
|
|
// by whoever gets an account they should not have.
|
|
if acct.State != "pending" || acct.IsAdmin {
|
|
log.Printf("SECURITY: signup produced state=%q admin=%v for %q; disabling it",
|
|
acct.State, acct.IsAdmin, username)
|
|
if _, err := s.store.DecideAccount(r.Context(), username, "disabled", "signup guard"); err != nil {
|
|
log.Printf("signup guard: could not disable %q: %v", username, err)
|
|
}
|
|
d.Err = "Registration failed. An administrator has been notified."
|
|
s.render(w, "signup.html", d)
|
|
return
|
|
}
|
|
|
|
log.Printf("signup: %q registered from %s, awaiting approval", username, ip)
|
|
d.Done = true
|
|
s.render(w, "signup.html", d)
|
|
}
|
|
|
|
func validSignup(username, pass, email, reason string) error {
|
|
switch {
|
|
case len(username) < 3 || len(username) > 32:
|
|
return errors.New("A username is 3 to 32 characters.")
|
|
case !validUsername(username):
|
|
return errors.New("A username may contain letters, digits, dot, dash and underscore.")
|
|
case len([]rune(pass)) < minPasswordLen:
|
|
return errors.New("A password is at least 12 characters. Length is what makes one hard to guess.")
|
|
case len(email) > maxFieldLen || len(reason) > maxFieldLen:
|
|
return errors.New("Keep the email and the reason under 200 characters.")
|
|
case email != "" && !strings.Contains(email, "@"):
|
|
return errors.New("That does not look like an email address.")
|
|
case reason == "":
|
|
return errors.New("Say what you need a cluster for: it is the only thing the approver has to go on.")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func validUsername(s string) bool {
|
|
for _, r := range s {
|
|
if !unicode.IsLetter(r) && !unicode.IsDigit(r) &&
|
|
r != '.' && r != '-' && r != '_' {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// ---------- approval ----------
|
|
|
|
func (s *Server) pageAccounts(w http.ResponseWriter, r *http.Request) {
|
|
// Checked from the session, not from anything the client sends.
|
|
if !isAdmin(r.Context()) {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
if s.store == nil {
|
|
s.render(w, "accounts.html", pageData{Page: "accounts",
|
|
Err: "No account database is configured."})
|
|
return
|
|
}
|
|
if r.Method == http.MethodPost {
|
|
s.accountAction(w, r)
|
|
return
|
|
}
|
|
|
|
d := s.base("accounts", r)
|
|
accounts, err := s.store.Accounts(r.Context())
|
|
if err != nil {
|
|
d.Err = err.Error()
|
|
}
|
|
d.Accounts = accounts
|
|
s.render(w, "accounts.html", d)
|
|
}
|
|
|
|
func (s *Server) accountAction(w http.ResponseWriter, r *http.Request) {
|
|
if err := r.ParseForm(); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
who := userFromContext(r.Context())
|
|
if who == "" {
|
|
// Should be unreachable: isAdmin is only true for a session.
|
|
http.Error(w, "an unattributed decision is not a decision", http.StatusForbidden)
|
|
return
|
|
}
|
|
username := strings.TrimSpace(r.PostFormValue("username"))
|
|
if username == "" {
|
|
http.Error(w, "which account?", http.StatusBadRequest)
|
|
return
|
|
}
|
|
if username == who {
|
|
// Approving or disabling yourself is either pointless or a way to
|
|
// lock the last administrator out.
|
|
http.Error(w, "you cannot decide on your own account", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
var err error
|
|
switch r.PostFormValue("do") {
|
|
case "approve":
|
|
_, err = s.store.DecideAccount(r.Context(), username, "active", who)
|
|
case "disable":
|
|
_, err = s.store.DecideAccount(r.Context(), username, "disabled", who)
|
|
case "grant-admin":
|
|
_, err = s.store.SetAdmin(r.Context(), username, true)
|
|
case "revoke-admin":
|
|
_, err = s.store.SetAdmin(r.Context(), username, false)
|
|
case "delete":
|
|
// Disabling keeps the trail - who registered, who decided, when -
|
|
// and is the right answer for a person who has left. Deleting is
|
|
// for the accounts that should not be in the trail at all: the
|
|
// ones opened to verify something and never used by anybody.
|
|
//
|
|
// The store refuses the last active administrator and any account
|
|
// whose tenant still holds a cluster, in the same transaction as
|
|
// the delete.
|
|
err = s.store.DeleteAccount(r.Context(), username)
|
|
default:
|
|
http.Error(w, "unknown action", http.StatusBadRequest)
|
|
return
|
|
}
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
log.Printf("account %q: %s by %s", username, r.PostFormValue("do"), who)
|
|
http.Redirect(w, r, "/admin/accounts", http.StatusSeeOther)
|
|
}
|