Merge branch 'worktree-agent-a82ec64b6cc1a2809'
# Conflicts: # internal/app/archie/operations.go # modules/events/events.go # modules/governance/accounts_api.go # modules/governance/governance.go
This commit is contained in:
44 files changed
+1462
-266
No files matched your search
@@ -0,0 +1,113 @@
|
||||
package archie
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"regexp"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/platform/httpapi"
|
||||
"github.com/samcharles93/archie-core/modules/governance"
|
||||
)
|
||||
|
||||
// operator authenticates every request as an instance administrator.
|
||||
type operator struct{}
|
||||
|
||||
func (operator) Authenticate(ctx context.Context, _ *http.Request) (context.Context, bool, error) {
|
||||
return ctx, true, nil
|
||||
}
|
||||
|
||||
// refuses answers every Allow with err and records what it was asked.
|
||||
type refuses struct {
|
||||
err error
|
||||
asked []string
|
||||
}
|
||||
|
||||
func (r *refuses) Allow(_ context.Context, action, kind string) error {
|
||||
r.asked = append(r.asked, action+" "+kind)
|
||||
return r.err
|
||||
}
|
||||
|
||||
// publicOperations are the only operations a caller reaches without a
|
||||
// credential: sign-in, setup and the auth status the sign-in page reads.
|
||||
var publicOperations = []string{
|
||||
"begin-oidc-sign-in", "begin-passkey-sign-in", "finish-oidc-sign-in", "finish-passkey-sign-in",
|
||||
"get-auth-status", "recover-account", "set-up-account", "sign-in", "sign-out",
|
||||
}
|
||||
|
||||
var pathParam = regexp.MustCompile(`\{[^}]+\}`)
|
||||
|
||||
// TestEveryOperationIsAuthorized guards deny-by-default across every module's
|
||||
// routes. Each registered operation is either one of the named public
|
||||
// operations or declares a valid action and resource kind, and a request that
|
||||
// fails the grant, or that cannot be decided, is refused before its handler
|
||||
// runs. The handlers here have nothing behind them, so one that ran would
|
||||
// panic.
|
||||
func TestEveryOperationIsAuthorized(t *testing.T) {
|
||||
probe := &refuses{err: httpapi.ErrDenied}
|
||||
mux, api := emptyAPI(operator{}, probe)
|
||||
routes := httpapi.Routes(api)
|
||||
if len(routes) < 100 {
|
||||
t.Fatalf("only %d operations registered", len(routes))
|
||||
}
|
||||
|
||||
var public []string
|
||||
for _, route := range routes {
|
||||
if route.Access.IsPublic() {
|
||||
public = append(public, route.ID)
|
||||
continue
|
||||
}
|
||||
action, kind, ok := route.Access.Declared()
|
||||
if !ok {
|
||||
t.Errorf("%s declares no access", route.ID)
|
||||
continue
|
||||
}
|
||||
if err := governance.Action(action).Validate(); err != nil || kind == "" {
|
||||
t.Errorf("%s declares %q on %q", route.ID, action, kind)
|
||||
}
|
||||
}
|
||||
slices.Sort(public)
|
||||
if !slices.Equal(public, publicOperations) {
|
||||
t.Errorf("public operations = %v, want %v", public, publicOperations)
|
||||
}
|
||||
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
err error
|
||||
want int
|
||||
}{
|
||||
{"a failing grant", httpapi.ErrDenied, http.StatusForbidden},
|
||||
{"an unavailable policy chain", httpapi.ErrUnavailable, http.StatusServiceUnavailable},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
for _, route := range routes {
|
||||
action, kind, ok := route.Access.Declared()
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
probe.err, probe.asked = tt.err, nil
|
||||
req := httptest.NewRequestWithContext(t.Context(), route.Method, pathParam.ReplaceAllString(route.Path, "x"), nil)
|
||||
rec := httptest.NewRecorder()
|
||||
mux.ServeHTTP(rec, req)
|
||||
if rec.Code != tt.want {
|
||||
t.Errorf("%s: status = %d, want %d", route.ID, rec.Code, tt.want)
|
||||
}
|
||||
if want := []string{action + " " + kind}; !slices.Equal(probe.asked, want) {
|
||||
t.Errorf("%s: decided on %v, want %v", route.ID, probe.asked, want)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestUnwiredAuthorizerRefuses guards the boot order: until the policy chain
|
||||
// is wired, an authorized operation is refused rather than let through.
|
||||
func TestUnwiredAuthorizerRefuses(t *testing.T) {
|
||||
settings := &governance.Module{}
|
||||
if err := settings.Allow(governance.WithPrincipal(t.Context(), governance.SharedTokenOwner()), "read", "run"); !errors.Is(err, httpapi.ErrUnavailable) {
|
||||
t.Fatalf("Allow with no chain = %v, want %v", err, httpapi.ErrUnavailable)
|
||||
}
|
||||
}
|
||||
@@ -68,7 +68,15 @@ func (m apiModules) register(api huma.API) {
|
||||
// needs no database.
|
||||
func Document() ([]byte, error) {
|
||||
settings := &governance.Module{}
|
||||
api := httpapi.New(http.NewServeMux(), buildinfo.Version, settings.Authenticator(""))
|
||||
_, api := emptyAPI(settings.Authenticator(""), settings)
|
||||
return api.OpenAPI().YAML()
|
||||
}
|
||||
|
||||
// emptyAPI registers every route on modules with nothing behind them.
|
||||
func emptyAPI(auth httpapi.Authenticator, authz httpapi.Authorizer) (*http.ServeMux, huma.API) {
|
||||
mux := http.NewServeMux()
|
||||
settings := &governance.Module{}
|
||||
api := httpapi.New(mux, buildinfo.Version, auth, authz)
|
||||
apiModules{
|
||||
settings: settings, conns: &connectors.Module{}, system: &system.Module{},
|
||||
tell: &deliveries.Module{}, allow: &approvals.Module{}, classes: &judgement.Classes{},
|
||||
@@ -76,5 +84,5 @@ func Document() ([]byte, error) {
|
||||
engine: &runs.Engine{}, chat: &chat.Module{}, drafts: &drafts.Module{},
|
||||
notes: &memory.Module{}, live: hub.New(),
|
||||
}.register(api)
|
||||
return api.OpenAPI().YAML()
|
||||
return mux, api
|
||||
}
|
||||
@@ -55,11 +55,15 @@ func (o *operations) Start(ctx context.Context) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Every entry point asks the same chain, and each refusal is stored as a
|
||||
// denial.
|
||||
checked := governance.Recording(access, o.settings, o.log.With("module", "governance"))
|
||||
// A workflow takes its context from the run handle, as a handler takes it
|
||||
// from the request.
|
||||
o.engine.Use(runs.Deps{
|
||||
Settings: o.settings, Toolbox: o.conns, Judge: o.judge, Approver: o.allow,
|
||||
Notifier: o.tell, Messenger: o.tell, Memory: o.remember, Capabilities: o.conns,
|
||||
Access: checked,
|
||||
})
|
||||
if err := installStarter(ctx, o.engine); err != nil {
|
||||
return err
|
||||
@@ -76,10 +80,13 @@ func (o *operations) Start(ctx context.Context) error {
|
||||
|
||||
// Nothing below can fail, so a retry cannot launch a connector or a loop twice.
|
||||
o.checks.Register("connectors", o.conns.Ready)
|
||||
// Administrative writes are checked against the same live policy chain as
|
||||
// every other request.
|
||||
o.settings.UseAuthorizer(access)
|
||||
o.allow.UseAuthorizer(access)
|
||||
// Administrative writes, and every API request, are checked against the same
|
||||
// live policy chain as chat and background work. The webhook intake applies
|
||||
// each source's delivery policy from it.
|
||||
o.settings.UseAuthorizer(checked)
|
||||
o.allow.UseAuthorizer(checked)
|
||||
o.tell.UseAccess(checked)
|
||||
o.hear.UseDeliveryPolicy(access)
|
||||
o.talk.Use(chat.Deps{
|
||||
People: o.settings, Access: access, Settings: o.settings, Toolbox: o.conns, Judge: o.judge,
|
||||
Approver: o.allow, Memory: o.remember, Runs: o.engine, Notifier: o.tell,
|
||||
|
||||
@@ -151,7 +151,7 @@ func wire(log *slog.Logger, configPath string, cfg Config, db *sqlx.DB) (*binary
|
||||
runtime.RegisterBuiltinClasses()
|
||||
settings := governance.NewModule(db, log.With("module", "governance"), orgID)
|
||||
mux := http.NewServeMux()
|
||||
api := httpapi.New(mux, buildinfo.Version, settings.Authenticator(cfg.AdminTokenSHA256))
|
||||
api := httpapi.New(mux, buildinfo.Version, settings.Authenticator(cfg.AdminTokenSHA256), settings)
|
||||
|
||||
conns, err := connectors.NewModule(db, log.With("module", "connectors"), orgID, connectors.Sealing{
|
||||
Identity: cfg.AgeIdentity, Backups: cfg.AgeBackupRecipients,
|
||||
|
||||
Reference in new issue
Block a user