fix(statestore): an invalid stored policy raises an alert and resolves when it validates

Issue: archie-core-kgxw.16
This commit is contained in:
sam committed 2026-10-13 16:14:16 +11:00
1 parent 2090c41960
commit acacdfdab5
3 files changed
+63 -4

No files matched your search

+58
View File
@@ -6,10 +6,12 @@ import (
"slices"
"time"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/alert"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/presence"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
infraaccess "github.com/samcharles93/archie-core/internal/infrastructure/access"
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
)
@@ -96,6 +98,7 @@ func (b *server) sweepAlerts(ctx context.Context, store alert.Store) {
if records != nil {
b.checkServices(ctx, reporter, records, &watch)
}
b.checkPolicies(ctx, reporter, store)
if pruner != nil {
if _, err := pruner.PruneAlerts(ctx, time.Now().Add(-alertRetention)); err != nil && ctx.Err() == nil {
b.log.Warn("prune alerts", "err", err)
@@ -135,3 +138,58 @@ func (b *server) checkServices(ctx context.Context, reporter alert.Reporter, rec
}
}
}
// checkPolicies raises an alert for every stored policy that does not compile
// and clears it once the policy validates. An invalid policy keeps denying its
// level, so the dashboard stays up to repair it: an instance policy asks for
// action in the system org, any other level warns in its own org. The text is
// fixed; the compile error is logged once at boot.
func (b *server) checkPolicies(ctx context.Context, reporter alert.Reporter, store alert.Store) {
policies, ok := b.st.(access.PolicyStore)
orgs, hasOrgs := b.st.(org.Repository)
if !ok || !hasOrgs {
return
}
stored, err := policies.ListPolicies(ctx)
if err != nil {
if ctx.Err() == nil {
b.log.Warn("policies not checked", "err", err)
}
return
}
invalid := map[alert.Key]bool{}
for _, problem := range infraaccess.New(stored).Problems() {
in := policyAlert(problem.Policy)
invalid[in.Key] = true
reporter.Raise(ctx, in)
}
list, err := orgs.ListOrgs(ctx)
if err != nil {
return
}
for _, o := range list {
open, err := store.ListAlerts(ctx, o.ID, alert.Filter{})
if err != nil {
continue
}
for _, a := range open {
if a.Kind == alert.KindPolicyInvalid && !invalid[a.Key] {
reporter.Resolve(ctx, a.Key)
}
}
}
}
func policyAlert(p access.Policy) alert.Input {
orgID, severity := p.OrgID, alert.SeverityWarning
if p.Level == access.LevelInstance {
orgID, severity = org.DefaultOrgID, alert.SeverityAction
}
return alert.Input{
Org: orgID, Kind: alert.KindPolicyInvalid, Subject: "policy:" + p.ID,
Severity: severity, Condition: true,
Title: fmt.Sprintf("Access policy %s is invalid", p.ID),
Body: fmt.Sprintf("The %s-level policy %s does not compile, so it denies everything at its level until it is fixed.", p.Level, p.ID),
Link: "/org/access",
}
}
+4 -4
View File
@@ -151,10 +151,10 @@ func Run(ctx context.Context, options Options) error { //nolint:cyclop // the co
// seedAndValidatePolicies seeds the shipped role policies for every org that
// lacks them, grants the agent identities their org's shipped developer role,
// and re-validates every stored policy this process serves. An invalid org,
// workspace or object policy is logged here and named as a problem by the
// engines the consumers build; an invalid instance policy fails this boot --
// the store stops serving until it is fixed. A store without the access
// and re-validates every stored policy this process serves. An invalid
// policy at any level keeps denying its level and is named as a problem
// by the engines the consumers build; the alert sweep raises an alert for it,
// so boot goes on and the dashboard can repair it. A store without the access
// surfaces degrades: there is no chain to seed.
func (b *server) seedAndValidatePolicies(ctx context.Context) error {
policies, ok := b.st.(access.PolicyStore)
+1
View File
@@ -70,6 +70,7 @@ const (
KindCredentialInvalid = "credential.rejected"
KindServiceUnhealthy = "service.unhealthy"
KindReleaseAvailable = "release.available"
KindPolicyInvalid = "policy.invalid"
)
// Limits on what a raiser may store.