refactor(archie,system): remove runtime config writes and finish track 1 (#2153)
This commit is contained in:
8 files changed
+45
-200
No files matched your search
@@ -1,13 +1,9 @@
|
||||
// Package system serves the bootstrap values the binary reads before the
|
||||
// database is reachable, read-only, and the one change that must survive a
|
||||
// restart: the admin token.
|
||||
// database is reachable, read-only, and instance health.
|
||||
package system
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"sync"
|
||||
@@ -15,8 +11,6 @@ import (
|
||||
"github.com/danielgtaylor/huma/v2"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/platform/httpapi"
|
||||
"github.com/samcharles93/archie-core/internal/platform/httpjson"
|
||||
"github.com/samcharles93/archie-core/modules/audit"
|
||||
"github.com/samcharles93/archie-core/modules/governance"
|
||||
)
|
||||
|
||||
@@ -31,25 +25,19 @@ type Bootstrap struct {
|
||||
Sealing bool `json:"sealing_identity_set"`
|
||||
}
|
||||
|
||||
// Module serves the bootstrap view and rotates the admin token.
|
||||
// Module serves the bootstrap view and instance health.
|
||||
type Module struct {
|
||||
mu sync.RWMutex
|
||||
bootstrap Bootstrap
|
||||
rotate func(hash string) error
|
||||
sources Sources
|
||||
audit audit.Recorder
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
// New returns the system module. rotate writes a new admin token hash to the
|
||||
// config the binary loaded.
|
||||
func New(b Bootstrap, rotate func(hash string) error, log *slog.Logger) *Module {
|
||||
return &Module{bootstrap: b, rotate: rotate, log: log}
|
||||
// New returns the system module.
|
||||
func New(b Bootstrap, log *slog.Logger) *Module {
|
||||
return &Module{bootstrap: b, log: log}
|
||||
}
|
||||
|
||||
// UseAudit configures the audit recorder notified on administrative actions.
|
||||
func (m *Module) UseAudit(r audit.Recorder) { m.audit = r }
|
||||
|
||||
// Register adds the system API.
|
||||
func (m *Module) Register(api huma.API) {
|
||||
tags := []string{"System"}
|
||||
@@ -57,57 +45,11 @@ func (m *Module) Register(api huma.API) {
|
||||
OperationID: "get-bootstrap", Method: http.MethodGet, Path: "/api/system/bootstrap",
|
||||
Summary: "Get the bootstrap values, which only the config file sets", Tags: tags,
|
||||
}, m.getBootstrap)
|
||||
httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionUpdate, governance.KindInstance), huma.Operation{
|
||||
OperationID: "rotate-admin-token", Method: http.MethodPost, Path: "/api/admin/token",
|
||||
Summary: "Rotate the admin token; the new one is shown once", Tags: tags,
|
||||
}, m.rotateAdminToken)
|
||||
m.registerHealth(api)
|
||||
}
|
||||
|
||||
// adminToken is a new admin token, returned once.
|
||||
type adminToken struct {
|
||||
Token string `json:"token" doc:"Shown once; only its hash is written to the config"`
|
||||
}
|
||||
|
||||
func (m *Module) getBootstrap(_ context.Context, _ *struct{}) (*httpapi.Reply[Bootstrap], error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
return httpapi.OK(m.bootstrap), nil
|
||||
}
|
||||
|
||||
// rotateAdminToken generates a new token, writes only its hash to the config,
|
||||
// and returns the token once. The old token and the old hash are never read
|
||||
// back or logged.
|
||||
func (m *Module) rotateAdminToken(ctx context.Context, _ *struct{}) (*httpapi.Reply[adminToken], error) {
|
||||
token, err := newToken()
|
||||
if err != nil {
|
||||
m.log.ErrorContext(ctx, "generate admin token", "error", err)
|
||||
return nil, huma.NewError(http.StatusInternalServerError, "couldn't generate a token")
|
||||
}
|
||||
hash := httpjson.TokenHash(token)
|
||||
if err := m.rotate(hash); err != nil {
|
||||
m.log.ErrorContext(ctx, "write admin token hash", "error", err)
|
||||
return nil, huma.NewError(http.StatusInternalServerError, "couldn't write the new token hash to the config")
|
||||
}
|
||||
m.mu.Lock()
|
||||
m.bootstrap.AdminPrefix, m.bootstrap.AdminSet = hash[:8], true
|
||||
m.mu.Unlock()
|
||||
m.log.InfoContext(ctx, "admin token rotated", "audit", true, "action", "admin.token.rotate")
|
||||
audit.Emit(ctx, m.audit, m.log, audit.Event{
|
||||
Category: "auth",
|
||||
Action: "admin.token.rotate",
|
||||
ActorID: governance.CallerID(ctx),
|
||||
ResourceKind: "instance",
|
||||
ResourceID: "admin_token",
|
||||
Outcome: "success",
|
||||
})
|
||||
return httpapi.OK(adminToken{Token: token}), nil
|
||||
}
|
||||
|
||||
func newToken() (string, error) {
|
||||
b := make([]byte, 32)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return "", fmt.Errorf("generate token: %w", err)
|
||||
}
|
||||
return hex.EncodeToString(b), nil
|
||||
}
|
||||
Reference in new issue
Block a user