feat(access): denial retention knob, State Store sweep and dashboard denials page (#1842)
Add [access].denial_retention (default 90 days, matching the audit-retention seed) wired through config defaults and validation; the State Store sweeps denial records older than the configured age hourly via the new access.DenialPruner / PruneDenials store query. The dashboard gains GET /api/access/denials (read_audit-gated, so org admins/owners see their org's records; the store scopes by the credential's org) and an /org/denials page backed by a Pinia store.
This commit is contained in:
1 parent
369e741df1
commit
da01f2cbe1
28 files changed
+625
-116
No files matched your search
@@ -42,6 +42,11 @@ const (
|
||||
defaultCaptureRatePerSecond = 1.0
|
||||
defaultCaptureRateBurst = 5
|
||||
|
||||
// defaultDenialRetention keeps denial records for 90 days, the same
|
||||
// horizon the audit-retention seed gives the audit trail
|
||||
// (docs/prds/orgs-and-access.md, "Audit retention").
|
||||
defaultDenialRetention = 90 * 24 * time.Hour
|
||||
|
||||
// defaultHealthListen is the daemon's own liveness address.
|
||||
defaultHealthListen = "127.0.0.1:8485"
|
||||
|
||||
@@ -70,6 +75,7 @@ func (l *Loader) applyDefaults(cfg *config.Config) {
|
||||
applyContainerDefaults(cfg)
|
||||
applyToolPolicyDefaults(cfg)
|
||||
applyCaptureDefaults(cfg)
|
||||
applyAccessDefaults(cfg)
|
||||
applyNATSDefaults(cfg)
|
||||
applyMemoryDefaults(cfg)
|
||||
applyCuratorDefaults(cfg)
|
||||
@@ -249,6 +255,14 @@ func applyForgeDefaults(cfg *config.Config) {
|
||||
}
|
||||
}
|
||||
|
||||
// applyAccessDefaults fills the access surfaces' settings. See
|
||||
// [config.AccessConfig].
|
||||
func applyAccessDefaults(cfg *config.Config) {
|
||||
if cfg.Access.DenialRetention == 0 {
|
||||
cfg.Access.DenialRetention = config.Duration(defaultDenialRetention)
|
||||
}
|
||||
}
|
||||
|
||||
// applyNATSDefaults resolves the nats mode from URL when the operator left it
|
||||
// unset. URL set implies external; URL empty implies embedded.
|
||||
func applyNATSDefaults(cfg *config.Config) {
|
||||
|
||||
@@ -17,6 +17,7 @@ var DeniedKeys = map[string]string{
|
||||
"org": "Pins the deployment's org at startup.",
|
||||
"memory.engine": "Constructs the memory engine at startup.",
|
||||
"capture.*": "Constructs the dashboard capture receiver at startup.",
|
||||
"access.*": "Read by the State Store's denial sweep at startup; a change needs a restart.",
|
||||
"log.level": "Constructs each service's logging handlers at startup.",
|
||||
"log.*": "Constructs each service's logging handlers at startup.",
|
||||
"tools.mcp_servers[].headers": "Process credentials supplied at startup; values are never published.",
|
||||
|
||||
@@ -75,7 +75,10 @@ func validateBootstrap(cfg *config.Config) error {
|
||||
if err := validateCurators(cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
return validateCapture(cfg)
|
||||
if err := validateCapture(cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
return validateAccess(cfg)
|
||||
}
|
||||
|
||||
// validateDatabaseOwned checks the settings the control plane owns:
|
||||
@@ -96,6 +99,14 @@ func validateDatabaseOwned(cfg *config.Config) error {
|
||||
return validateRepositoryContents(cfg)
|
||||
}
|
||||
|
||||
// validateAccess rejects a negative denial retention.
|
||||
func validateAccess(cfg *config.Config) error {
|
||||
if cfg.Access.DenialRetention < 0 {
|
||||
return fmt.Errorf("%w: access.denial_retention must not be negative", ErrInvalidInput)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateCurators rejects a curator definition with no interval. The
|
||||
// interval is a live-path value: a definition missing it is refused with a
|
||||
// clear error rather than defaulted at registration or pass time, so a
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
@@ -22,9 +23,10 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
_ access.PolicyStore = (*Store)(nil)
|
||||
_ access.Resetter = (*Store)(nil)
|
||||
_ access.DenialStore = (*Store)(nil)
|
||||
_ access.PolicyStore = (*Store)(nil)
|
||||
_ access.Resetter = (*Store)(nil)
|
||||
_ access.DenialStore = (*Store)(nil)
|
||||
_ access.DenialPruner = (*Store)(nil)
|
||||
)
|
||||
|
||||
// shippedOrgPolicyIDs lists an org's shipped role policy IDs: the seed's
|
||||
@@ -403,3 +405,26 @@ func (s *Store) ListDenials(ctx context.Context, orgID org.OrgID, limit int) ([]
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// denialPruneBatch is one delete's bound, so a single sweep does not own the
|
||||
// table for an unbounded time.
|
||||
const denialPruneBatch = 10_000
|
||||
|
||||
// PruneDenials deletes the denial records created before cutoff in bounded
|
||||
// batches and returns how many rows it removed. The State Store's retention
|
||||
// sweep is the only caller: the configured retention sets the cutoff.
|
||||
func (s *Store) PruneDenials(ctx context.Context, cutoff time.Time) (int64, error) {
|
||||
var total int64
|
||||
for {
|
||||
removed, err := s.queries().PruneDenials(ctx, postgresdb.PruneDenialsParams{
|
||||
Cutoff: cutoff.UTC(), Batch: denialPruneBatch,
|
||||
})
|
||||
if err != nil {
|
||||
return total, err
|
||||
}
|
||||
total += removed
|
||||
if removed < denialPruneBatch {
|
||||
return total, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ package postgresdb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
@@ -329,6 +330,32 @@ func (q *Queries) LockPolicyWrite(ctx context.Context, key string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
const pruneDenials = `-- name: PruneDenials :execrows
|
||||
DELETE FROM access_denials
|
||||
WHERE id IN (
|
||||
SELECT id FROM access_denials
|
||||
WHERE created_at < $1::timestamptz
|
||||
ORDER BY created_at
|
||||
LIMIT $2::bigint
|
||||
)
|
||||
`
|
||||
|
||||
type PruneDenialsParams struct {
|
||||
Cutoff time.Time
|
||||
Batch int64
|
||||
}
|
||||
|
||||
// The retention sweep's delete: every denial record created before the
|
||||
// cutoff, oldest first in bounded batches so one sweep does not own the
|
||||
// table for an unbounded time.
|
||||
func (q *Queries) PruneDenials(ctx context.Context, arg PruneDenialsParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, pruneDenials, arg.Cutoff, arg.Batch)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const recordDenial = `-- name: RecordDenial :exec
|
||||
INSERT INTO access_denials (org_id, principal, action, resource_kind, resource_id, level, policies)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
|
||||
@@ -74,6 +74,18 @@ DO UPDATE SET count = access_denials.count + 1, updated_at = now();
|
||||
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;
|
||||
|
||||
-- name: PruneDenials :execrows
|
||||
-- The retention sweep's delete: every denial record created before the
|
||||
-- cutoff, oldest first in bounded batches so one sweep does not own the
|
||||
-- table for an unbounded time.
|
||||
DELETE FROM access_denials
|
||||
WHERE id IN (
|
||||
SELECT id FROM access_denials
|
||||
WHERE created_at < sqlc.arg(cutoff)::timestamptz
|
||||
ORDER BY created_at
|
||||
LIMIT sqlc.arg(batch)::bigint
|
||||
);
|
||||
|
||||
-- name: InsertAccessResetAudit :exec
|
||||
INSERT INTO sys_audit (at, table_name, record_key, field, record_version, actor, source, request_id, org_id)
|
||||
VALUES (now(), 'access_policies', $1, 'reset', 1, 'archie-state-store access reset', 'archie-state-store', '', $2);
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
// The Client satisfies the access contracts the two callers depend on.
|
||||
var (
|
||||
_ access.PolicyStore = (*Client)(nil)
|
||||
_ access.DenialRecorder = (*Client)(nil)
|
||||
_ access.DenialStore = (*Client)(nil)
|
||||
_ access.PrincipalSource = (*Client)(nil)
|
||||
)
|
||||
|
||||
@@ -82,3 +82,17 @@ func (c *Client) RecordDenial(ctx context.Context, d access.Denial) error {
|
||||
_, err := c.access.RecordDenial(ctx, &pb.RecordDenialRequest{Denial: accessDenialProto(d)})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
// ListDenials returns the denials the credential's org holds, newest first.
|
||||
// The org is the one the State Store derives from the caller's credential, so
|
||||
// the contract's orgID is not sent: no request field sets scope.
|
||||
func (c *Client) ListDenials(ctx context.Context, _ org.OrgID, limit int) ([]access.Denial, error) {
|
||||
if limit <= 0 || limit > 1000 {
|
||||
limit = 100
|
||||
}
|
||||
r, err := c.access.ListDenials(ctx, &pb.ListDenialsRequest{Limit: int32(limit)})
|
||||
if err != nil {
|
||||
return nil, unmapError(err)
|
||||
}
|
||||
return mapValues(r.GetDenials(), accessDenialValue), nil
|
||||
}
|
||||
Reference in new issue
Block a user