feat(access): archied access reset --org/--instance on the State Store host (archie-core-qc8p)

This commit is contained in:
sam committed 2026-09-26 15:23:32 +10:00
1 parent 020c6fb9d9
commit 2cb5ae3418
6 files changed
+196 -2

No files matched your search

+6
View File
@@ -2,6 +2,7 @@
package main
import (
"context"
"os"
"github.com/samcharles93/archie-core/internal/app/archied"
@@ -15,5 +16,10 @@ func main() {
if archied.IsSetupArgs(args) {
os.Exit(archied.RunSetup(args[1:], os.Stdin, os.Stdout, os.Stderr))
}
// The access recovery command runs before the daemon's flags are parsed:
// it is a State Store host operation, not a daemon run.
if archied.IsAccessResetArgs(args) {
os.Exit(archied.RunAccessReset(context.Background(), args[2:], os.Stderr))
}
os.Exit(archied.Run())
}
+110
View File
@@ -0,0 +1,110 @@
// The `archied access reset` command: the locked-out-org and broken-instance
// recovery surface (docs/prds/orgs-and-access.md, "Recovering from a
// locked-out org"). It restores the shipped role policies for one org and
// removes its other org-level policies, or removes the stored instance
// policies; the cross-org forbid is engine-enforced and survives any reset.
//
// It runs only on the State Store host, never over the network, and is
// recorded as an audit event. It claims the same store ownership the State
// Store holds, so a running store refuses it: stop the store, reset, start
// the store.
package archied
import (
"context"
"flag"
"fmt"
"io"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
)
// accessResetCommand is the argv selector for the recovery command.
const accessResetCommand = "access"
// IsAccessResetArgs reports whether args selects the access recovery command.
func IsAccessResetArgs(args []string) bool {
return len(args) > 2 && args[0] == accessResetCommand && args[1] == resetCommand
}
const resetCommand = "reset"
// RunAccessReset performs the reset and returns the process exit code:
// 0 reset, 1 failure, 2 usage.
func RunAccessReset(ctx context.Context, args []string, stderr io.Writer) int {
fs := flag.NewFlagSet(resetCommand, flag.ContinueOnError)
fs.SetOutput(stderr)
cfgPath := fs.String("config", configuration.DefaultConfigPath(), "path to a TOML/YAML config file or configuration directory")
overlayPath := fs.String("config-overlay", "", "path to a TOML/YAML overlay file or configuration directory applied on top of -config")
orgFlag := fs.String("org", "", "org whose access to reset; conflicts with --instance")
instance := fs.Bool("instance", false, "reset the stored instance policies")
if err := fs.Parse(args); err != nil {
return 2
}
if fs.NArg() > 0 {
fmt.Fprintf(stderr, "reset: unexpected argument %q\n", fs.Arg(0))
return 2
}
if (*orgFlag != "" && *instance) || (*orgFlag == "" && !*instance) {
fmt.Fprintln(stderr, "reset: give exactly one of --org <org> or --instance")
return 2
}
b := newBootstrap()
defer b.cleanup()
if err := b.loadConfig(ctx, *cfgPath, *overlayPath); err != nil {
return 1
}
if err := b.openStateStorePool(ctx); err != nil {
// A running State Store holds the ownership claim; the reset must not
// race it. Stop the store, reset, start the store.
fmt.Fprintln(stderr, "reset: open the State Store database (stop archie-state-store first)")
return 1
}
resetter, ok := b.st.(access.Resetter)
if !ok {
fmt.Fprintln(stderr, "reset: the State Store does not support the access reset surface")
return 1
}
var err error
switch {
case *instance:
err = resetter.ResetInstancePolicies(ctx)
default:
err = resetter.ResetOrgPolicies(ctx, org.OrgID(*orgFlag))
}
if err != nil {
fmt.Fprintf(stderr, "reset: %v\n", err)
return 1
}
if err := recordResetAudit(ctx, b.pg, *orgFlag, *instance); err != nil {
fmt.Fprintf(stderr, "reset: audit: %v\n", err)
return 1
}
fmt.Fprintf(stderr, "reset: access policies for %s restored to the shipped set\n", resetTarget(*orgFlag, *instance))
return 0
}
// resetTarget names what the reset covered, for the operator's output.
func resetTarget(orgID string, instance bool) string {
if instance {
return "the instance"
}
return fmt.Sprintf("org %q", orgID)
}
// recordResetAudit writes the one audit row the reset is recorded as. The
// policy changes themselves carry their own sys_audit rows; this names the
// recovery.
func recordResetAudit(ctx context.Context, pool *pgxpool.Pool, orgID string, instance bool) error {
key := "reset/instance"
if !instance {
key = fmt.Sprintf("reset/org/%s", orgID)
}
return postgres.New(pool).RecordResetAudit(ctx, key)
}
+54
View File
@@ -0,0 +1,54 @@
package archied
import (
"strings"
"testing"
)
func TestIsAccessResetArgs(t *testing.T) {
tests := []struct {
name string
args []string
want bool
}{
{name: "org reset", args: []string{"access", "reset", "--org", "acme"}, want: true},
{name: "instance reset", args: []string{"access", "reset", "--instance"}, want: true},
{name: "config first", args: []string{"access", "reset", "-config", "/tmp/config.toml", "--org", "acme"}, want: true},
{name: "setup is not a reset", args: []string{"setup"}, want: false},
{name: "bare access", args: []string{"access"}, want: false},
{name: "other subcommand", args: []string{"access", "grant"}, want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsAccessResetArgs(tt.args); got != tt.want {
t.Fatalf("IsAccessResetArgs(%v) = %v, want %v", tt.args, got, tt.want)
}
})
}
}
func TestRunAccessResetUsage(t *testing.T) {
tests := []struct {
name string
args []string
wantCode int
wantText string
}{
{name: "no target", args: nil, wantCode: 2, wantText: "exactly one of --org"},
{name: "both targets", args: []string{"--org", "acme", "--instance"}, wantCode: 2, wantText: "exactly one of --org"},
{name: "unexpected argument", args: []string{"--instance", "extra"}, wantCode: 2, wantText: "unexpected argument"},
{name: "unknown flag", args: []string{"--workspace", "net"}, wantCode: 2},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var stderr strings.Builder
code := RunAccessReset(t.Context(), tt.args, &stderr)
if code != tt.wantCode {
t.Fatalf("RunAccessReset(%v) = %d, want %d (stderr: %s)", tt.args, code, tt.wantCode, stderr.String())
}
if tt.wantText != "" && !strings.Contains(stderr.String(), tt.wantText) {
t.Fatalf("stderr = %q, want it to name %q", stderr.String(), tt.wantText)
}
})
}
}
+6 -1
View File
@@ -194,7 +194,12 @@ func (s *Store) ResetInstancePolicies(ctx context.Context) error {
return s.queries().ResetInstancePolicies(ctx)
}
// RecordDenial records one refusal. The store coalesces identical denials
// RecordResetAudit records the one audit row a reset is written as.
func (s *Store) RecordResetAudit(ctx context.Context, key string) error {
return s.queries().InsertAccessResetAudit(ctx, key)
}
// RecordDenial records one refusal The store coalesces identical denials
// within a minute into one row with a count, so a retry storm does not
// flood the surface.
func (s *Store) RecordDenial(ctx context.Context, d access.Denial) error {
@@ -154,6 +154,19 @@ func (q *Queries) InsertAccessPolicyAudit(ctx context.Context, arg InsertAccessP
return err
}
const insertAccessResetAudit = `-- name: InsertAccessResetAudit :exec
INSERT INTO sys_audit (at, table_name, record_key, field, record_version, actor, source, request_id)
VALUES (now(), 'access_policies', $1, 'reset', 1, 'archied access reset', 'archied', '')
`
// The one audit row a reset is recorded as; the reset's policy changes
// carry their own rows (docs/prds/orgs-and-access.md, "Recovering from a
// locked-out org").
func (q *Queries) InsertAccessResetAudit(ctx context.Context, recordKey string) error {
_, err := q.db.Exec(ctx, insertAccessResetAudit, recordKey)
return err
}
const listAccessPolicies = `-- name: ListAccessPolicies :many
SELECT level, org_id, workspace_id, object_kind, object_id, policy_id, text, updated_at
@@ -68,4 +68,10 @@ DO UPDATE SET count = access_denials.count + 1, updated_at = now();
-- name: ListDenials :many
SELECT id, org_id, principal, action, resource_kind, resource_id, level, policies, minute_at, count, created_at, updated_at
FROM access_denials WHERE org_id = $1 ORDER BY id DESC LIMIT $2;
FROM access_denials WHERE org_id = $1 ORDER BY id DESC LIMIT $2;
-- name: InsertAccessResetAudit :exec
-- The one audit row a reset is recorded as; the reset's policy changes
-- carry their own rows (docs/prds/orgs-and-access.md, "Recovering from a
-- locked-out org").
INSERT INTO sys_audit (at, table_name, record_key, field, record_version, actor, source, request_id)
VALUES (now(), 'access_policies', $1, 'reset', 1, 'archied access reset', 'archied', '');