Operations register through httpapi.Register with Public, Authorized or AdminAuthorized, and the guard asks the policy chain for the declared action and resource kind before the handler runs. An undeclared operation and an undecidable chain are refused. The workload principal, its shipped policy, a self-service policy and Recording for denials are added in governance.
99 lines
3.5 KiB
Go
99 lines
3.5 KiB
Go
// 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.
|
|
package system
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"sync"
|
|
|
|
"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/governance"
|
|
)
|
|
|
|
// Bootstrap is what the binary loaded from config.toml, reduced to values safe
|
|
// to show: no connection URL and no secret.
|
|
type Bootstrap struct {
|
|
Listen string `json:"listen"`
|
|
Database string `json:"database_host"`
|
|
Schemas int `json:"schemas"`
|
|
AdminPrefix string `json:"admin_token_hash_prefix"`
|
|
AdminSet bool `json:"admin_token_set"`
|
|
Sealing bool `json:"sealing_identity_set"`
|
|
}
|
|
|
|
// Module serves the bootstrap view and rotates the admin token.
|
|
type Module struct {
|
|
mu sync.RWMutex
|
|
bootstrap Bootstrap
|
|
rotate func(hash string) error
|
|
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}
|
|
}
|
|
|
|
// Register adds the system API.
|
|
func (m *Module) Register(api huma.API) {
|
|
tags := []string{"System"}
|
|
httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionRead, governance.KindInstance), huma.Operation{
|
|
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)
|
|
}
|
|
|
|
// 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")
|
|
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
|
|
}
|