feat(auth): add argon2id passwords, sessions, rate limiter, authorization and audit builders
- feat(auth): add argon2id passwords, sessions, rate limiter, authorization and audit builders - feat(cli): add check and audit commands - feat(api): add api framework, login, page routes and account, user, delegation and audit handlers - test(auth): cover passwords, sessions, rate limiter, authorization and audit builders - test(api): cover the api framework, sessions, client origin, headers and cross-origin refusal - test(api): cover account, user, delegation and audit handlers and the authorization matrix - test(ui): cover login, logout, page routes and the page authorization matrix
This commit is contained in:
1 parent
23bec143e3
commit
99fa5de747
28 files changed
+6591
-243
No files matched your search
@@ -0,0 +1,340 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
// Copyright (C) 2026 Bryan Joshua Pedini
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type auditEntry struct {
|
||||
ID int64
|
||||
Time time.Time
|
||||
UserID sql.NullInt64
|
||||
Actor string
|
||||
Origin string
|
||||
Operation string
|
||||
Status sql.NullInt64
|
||||
Error sql.NullString
|
||||
Zone sql.NullString
|
||||
RecordID sql.NullInt64
|
||||
TargetUserID sql.NullInt64
|
||||
Before []byte
|
||||
After []byte
|
||||
}
|
||||
|
||||
const (
|
||||
opLogin = "login"
|
||||
opLogout = "logout"
|
||||
opAccountRead = "account_read"
|
||||
opPasswordChange = "password_change"
|
||||
opZoneList = "zone_list"
|
||||
opZoneRead = "zone_read"
|
||||
opZoneCreate = "zone_create"
|
||||
opZoneDelete = "zone_delete"
|
||||
opRecordList = "record_list"
|
||||
opRecordCreate = "record_create"
|
||||
opRecordUpdate = "record_update"
|
||||
opRecordDelete = "record_delete"
|
||||
opRecordBulkDelete = "record_bulk_delete"
|
||||
opSOAUpdate = "soa_update"
|
||||
opZoneCheck = "zone_check"
|
||||
opCheckAll = "check_all"
|
||||
opZoneExport = "zone_export"
|
||||
opZoneImportPreview = "zone_import_preview"
|
||||
opZoneImport = "zone_import"
|
||||
opDelegationList = "delegation_list"
|
||||
opDelegationGrant = "delegation_grant"
|
||||
opDelegationRevoke = "delegation_revoke"
|
||||
opUserList = "user_list"
|
||||
opUserRead = "user_read"
|
||||
opUserCreate = "user_create"
|
||||
opUserRole = "user_role"
|
||||
opUserPassword = "user_password"
|
||||
opUserDelete = "user_delete"
|
||||
opAuditRead = "audit_read"
|
||||
)
|
||||
|
||||
const (
|
||||
cliActor = reservedUsername
|
||||
cliOrigin = "local"
|
||||
auditTimeLayout = "2006-01-02T15:04:05.000000Z"
|
||||
defaultBodyLimit = 64 << 10
|
||||
importBodyLimit = 2 << 20
|
||||
apiPrefix = "/api/v1"
|
||||
auditDefaultLimit = 50
|
||||
auditMaxLimit = 200
|
||||
)
|
||||
|
||||
type auditOperation struct {
|
||||
Name string
|
||||
Kind string
|
||||
}
|
||||
|
||||
var auditOperations = []auditOperation{
|
||||
{opAccountRead, auditKindRead},
|
||||
{opLogin, auditKindChange},
|
||||
{opLogout, auditKindChange},
|
||||
{opPasswordChange, auditKindChange},
|
||||
{opZoneList, auditKindRead},
|
||||
{opZoneRead, auditKindRead},
|
||||
{opZoneCreate, auditKindChange},
|
||||
{opZoneDelete, auditKindChange},
|
||||
{opRecordList, auditKindRead},
|
||||
{opRecordCreate, auditKindChange},
|
||||
{opRecordUpdate, auditKindChange},
|
||||
{opRecordDelete, auditKindChange},
|
||||
{opRecordBulkDelete, auditKindChange},
|
||||
{opSOAUpdate, auditKindChange},
|
||||
{opZoneCheck, auditKindRead},
|
||||
{opCheckAll, auditKindRead},
|
||||
{opZoneExport, auditKindRead},
|
||||
{opZoneImportPreview, auditKindRead},
|
||||
{opZoneImport, auditKindChange},
|
||||
{opDelegationList, auditKindRead},
|
||||
{opDelegationGrant, auditKindChange},
|
||||
{opDelegationRevoke, auditKindChange},
|
||||
{opUserList, auditKindRead},
|
||||
{opUserRead, auditKindRead},
|
||||
{opUserCreate, auditKindChange},
|
||||
{opUserRole, auditKindChange},
|
||||
{opUserPassword, auditKindChange},
|
||||
{opUserDelete, auditKindChange},
|
||||
{opAuditRead, auditKindRead},
|
||||
}
|
||||
|
||||
var auditChangeOperations = operationsOfKind(auditKindChange)
|
||||
|
||||
var zoneOperations = []string{
|
||||
opZoneList, opZoneRead, opZoneCreate, opZoneDelete,
|
||||
opRecordList, opRecordCreate, opRecordUpdate, opRecordDelete, opRecordBulkDelete, opSOAUpdate,
|
||||
opZoneCheck, opCheckAll, opZoneExport, opZoneImportPreview, opZoneImport,
|
||||
}
|
||||
|
||||
func operationsOfKind(kind string) []string {
|
||||
var out []string
|
||||
for _, o := range auditOperations {
|
||||
if o.Kind == kind {
|
||||
out = append(out, o.Name)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func knownOperation(name string) bool {
|
||||
return slices.ContainsFunc(auditOperations, func(o auditOperation) bool { return o.Name == name })
|
||||
}
|
||||
|
||||
type apiRoute struct {
|
||||
Method string
|
||||
Path string
|
||||
Operation string
|
||||
Access string
|
||||
BodyLimit int64
|
||||
}
|
||||
|
||||
var apiRoutes = []apiRoute{
|
||||
{"GET", "/account", opAccountRead, accessSession, defaultBodyLimit},
|
||||
{"PUT", "/account/password", opPasswordChange, accessSession, defaultBodyLimit},
|
||||
{"GET", "/zones", opZoneList, accessSession, defaultBodyLimit},
|
||||
{"POST", "/zones", opZoneCreate, accessAdmin, defaultBodyLimit},
|
||||
{"GET", "/zones/{zone}", opZoneRead, accessZone, defaultBodyLimit},
|
||||
{"DELETE", "/zones/{zone}", opZoneDelete, accessAdmin, defaultBodyLimit},
|
||||
{"GET", "/zones/{zone}/records", opRecordList, accessZone, defaultBodyLimit},
|
||||
{"POST", "/zones/{zone}/records", opRecordCreate, accessZone, defaultBodyLimit},
|
||||
{"PUT", "/zones/{zone}/records/{id}", opRecordUpdate, accessZone, defaultBodyLimit},
|
||||
{"DELETE", "/zones/{zone}/records/{id}", opRecordDelete, accessZone, defaultBodyLimit},
|
||||
{"POST", "/zones/{zone}/records/delete", opRecordBulkDelete, accessZone, defaultBodyLimit},
|
||||
{"PUT", "/zones/{zone}/soa", opSOAUpdate, accessZone, defaultBodyLimit},
|
||||
{"GET", "/zones/{zone}/check", opZoneCheck, accessZone, defaultBodyLimit},
|
||||
{"GET", "/check", opCheckAll, accessSession, defaultBodyLimit},
|
||||
{"GET", "/zones/{zone}/export", opZoneExport, accessZone, defaultBodyLimit},
|
||||
{"POST", "/zones/{zone}/import/preview", opZoneImportPreview, accessZone, importBodyLimit},
|
||||
{"POST", "/zones/{zone}/import/apply", opZoneImport, accessZone, importBodyLimit},
|
||||
{"GET", "/zones/{zone}/users", opDelegationList, accessAdmin, defaultBodyLimit},
|
||||
{"PUT", "/zones/{zone}/users/{username}", opDelegationGrant, accessAdmin, defaultBodyLimit},
|
||||
{"DELETE", "/zones/{zone}/users/{username}", opDelegationRevoke, accessAdmin, defaultBodyLimit},
|
||||
{"GET", "/users", opUserList, accessAdmin, defaultBodyLimit},
|
||||
{"POST", "/users", opUserCreate, accessAdmin, defaultBodyLimit},
|
||||
{"GET", "/users/{username}", opUserRead, accessAdmin, defaultBodyLimit},
|
||||
{"PUT", "/users/{username}/role", opUserRole, accessAdmin, defaultBodyLimit},
|
||||
{"PUT", "/users/{username}/password", opUserPassword, accessAdmin, defaultBodyLimit},
|
||||
{"DELETE", "/users/{username}", opUserDelete, accessAdmin, defaultBodyLimit},
|
||||
{"GET", "/audit", opAuditRead, accessSession, defaultBodyLimit},
|
||||
}
|
||||
|
||||
type pageRoute struct {
|
||||
Method string
|
||||
Path string
|
||||
Access string
|
||||
Page string
|
||||
}
|
||||
|
||||
var pageRoutes = []pageRoute{
|
||||
{"GET", "/login", accessNone, "login"},
|
||||
{"POST", "/login", accessNone, ""},
|
||||
{"POST", "/logout", accessSession, ""},
|
||||
{"GET", "/", accessSession, "zones"},
|
||||
{"GET", "/zones/{zone}", accessZone, "zone"},
|
||||
{"GET", "/zones/{zone}/import", accessZone, "import"},
|
||||
{"GET", "/audit", accessSession, "audit"},
|
||||
{"GET", "/account", accessSession, "account"},
|
||||
{"GET", "/users", accessAdmin, "users"},
|
||||
{"GET", "/users/{username}", accessAdmin, "user"},
|
||||
{"GET", "/version", accessNone, ""},
|
||||
{"GET", "/health", accessNone, ""},
|
||||
{"GET", "/static/{hash}/...", accessNone, ""},
|
||||
}
|
||||
|
||||
func routeHasZone(path string) bool {
|
||||
return strings.Contains(path, "{zone}")
|
||||
}
|
||||
|
||||
func apiAuditEntry(now time.Time, p principal, origin, operation, zone string) auditEntry {
|
||||
e := auditEntry{
|
||||
Time: now,
|
||||
UserID: sql.NullInt64{Int64: p.User.ID, Valid: true},
|
||||
Actor: p.User.Username,
|
||||
Origin: origin,
|
||||
Operation: operation,
|
||||
}
|
||||
if zone != "" {
|
||||
e.Zone = sql.NullString{String: zone, Valid: true}
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
func loginActor(submitted string) string {
|
||||
return printableText(submitted, auditMaxActorRunes)
|
||||
}
|
||||
|
||||
func loginAuditEntry(now time.Time, operation, submitted string, user *userRow, origin string) auditEntry {
|
||||
e := auditEntry{Time: now, Actor: loginActor(submitted), Origin: origin, Operation: operation}
|
||||
if user != nil {
|
||||
e.UserID = sql.NullInt64{Int64: user.ID, Valid: true}
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
func finishAuditEntry(e *auditEntry, status int, message string) {
|
||||
e.Status = sql.NullInt64{Int64: int64(status), Valid: true}
|
||||
e.Error = sql.NullString{}
|
||||
if status >= 400 {
|
||||
e.Error = sql.NullString{String: message, Valid: true}
|
||||
}
|
||||
}
|
||||
|
||||
func cliAuditEntry(now time.Time, operation string, target userRow, before, after *userRow) auditEntry {
|
||||
e := auditEntry{
|
||||
Time: now,
|
||||
Actor: cliActor,
|
||||
Origin: cliOrigin,
|
||||
Operation: operation,
|
||||
TargetUserID: sql.NullInt64{Int64: target.ID, Valid: true},
|
||||
}
|
||||
if before != nil {
|
||||
e.Before = auditUserJSON(*before)
|
||||
}
|
||||
if after != nil {
|
||||
e.After = auditUserJSON(*after)
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
func formatAuditTime(t time.Time) string {
|
||||
return t.UTC().Format(auditTimeLayout)
|
||||
}
|
||||
|
||||
func formatAuditNullTime(t sql.NullTime) *string {
|
||||
if !t.Valid {
|
||||
return nil
|
||||
}
|
||||
s := formatAuditTime(t.Time)
|
||||
return &s
|
||||
}
|
||||
|
||||
type auditUserValue struct {
|
||||
ID int64 `json:"id"`
|
||||
Username string `json:"username"`
|
||||
Role string `json:"role"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
DeletedAt *string `json:"deleted_at"`
|
||||
}
|
||||
|
||||
type auditRecordValue struct {
|
||||
ID int64 `json:"id"`
|
||||
Zone string `json:"zone"`
|
||||
Name string `json:"name"`
|
||||
TTL *int64 `json:"ttl"`
|
||||
RecordType string `json:"record_type"`
|
||||
Content *string `json:"content"`
|
||||
}
|
||||
|
||||
type auditDelegationValue struct {
|
||||
Zone string `json:"zone"`
|
||||
Username string `json:"username"`
|
||||
}
|
||||
|
||||
func mustAuditJSON(v any) []byte {
|
||||
b, _ := json.Marshal(v)
|
||||
return b
|
||||
}
|
||||
|
||||
func auditUserValueOf(u userRow) auditUserValue {
|
||||
return auditUserValue{ID: u.ID, Username: u.Username, Role: u.Role, CreatedAt: formatAuditTime(u.CreatedAt), DeletedAt: formatAuditNullTime(u.DeletedAt)}
|
||||
}
|
||||
|
||||
func auditUserJSON(u userRow) []byte {
|
||||
return mustAuditJSON(auditUserValueOf(u))
|
||||
}
|
||||
|
||||
func auditRecordValueOf(r recordRow) auditRecordValue {
|
||||
v := auditRecordValue{ID: r.ID, Zone: r.Zone, Name: r.Name, RecordType: r.RecordType}
|
||||
if r.TTL.Valid {
|
||||
ttl := r.TTL.Int64
|
||||
v.TTL = &ttl
|
||||
}
|
||||
if r.Content.Valid {
|
||||
content := r.Content.String
|
||||
v.Content = &content
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func auditRecordJSON(r recordRow) []byte {
|
||||
return mustAuditJSON(auditRecordValueOf(r))
|
||||
}
|
||||
|
||||
func auditRecordsJSON(rows []recordRow) []byte {
|
||||
values := make([]auditRecordValue, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
values = append(values, auditRecordValueOf(r))
|
||||
}
|
||||
return mustAuditJSON(values)
|
||||
}
|
||||
|
||||
func auditDelegationJSON(zone, username string) []byte {
|
||||
return mustAuditJSON(auditDelegationValue{Zone: zone, Username: username})
|
||||
}
|
||||
|
||||
func auditDelegationsJSON(rows []delegationRow) []byte {
|
||||
values := make([]auditDelegationValue, 0, len(rows))
|
||||
for _, d := range rows {
|
||||
values = append(values, auditDelegationValue{Zone: d.Zone, Username: d.Username})
|
||||
}
|
||||
return mustAuditJSON(values)
|
||||
}
|
||||
|
||||
func auditVisibleTo(p principal, e auditEntry, delegated map[string]bool) bool {
|
||||
if p.isAdmin() {
|
||||
return true
|
||||
}
|
||||
if e.UserID.Valid && e.UserID.Int64 == p.User.ID {
|
||||
return true
|
||||
}
|
||||
return e.Zone.Valid && delegated[e.Zone.String]
|
||||
}
|
||||
Reference in new issue
Block a user