Files
archie/internal/infrastructure/staterpc/access_client.go
T
sam cc7bc90ff3 fix(access): check the policy version before each decision
archied, archie-ui and the tool gateway re-read the stored policies on a 30s
ticker, so a forbid written seconds earlier did not stop a run. One
instance-wide revision now moves inside the transaction of every policy write,
and a decision reads it first and rebuilds the engine only when it moved.

A version read that fails, or a policy list that fails after a bump, refuses
with ErrChainUnavailable instead of standing on the last chain, so three call
sites now treat unavailable as availability rather than denial: dispatch leaves
the capture for the next cycle, a task start parks transient, and the tool
gateway reports the tool as unavailable.
2026-10-25 16:21:01 +11:00

85 lines
2.9 KiB
Go

// Access: the client half of the policy-chain and denial-record RPCs.
// The reset surface is deliberately absent:
// `archie-state-store access reset` runs on the State Store host, never over the wire.
package staterpc
import (
"context"
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/org"
)
// The Client satisfies the access contracts the two callers depend on.
var (
_ access.PolicyStore = (*Client)(nil)
_ access.DenialRecorder = (*Client)(nil)
_ access.PrincipalSource = (*Client)(nil)
)
func (c *Client) PrincipalFor(ctx context.Context, id identity.IdentityID) (access.Principal, error) {
r, err := c.client.GetPrincipal(ctx, &pb.GetPrincipalRequest{IdentityId: string(id)})
if err != nil {
return access.Principal{}, unmapError(err)
}
value := r.GetPrincipal()
memberships := mapValues(value.GetMemberships(), func(m *pb.PrincipalMembership) org.Membership {
return org.Membership{
IdentityID: id,
OrgID: org.OrgID(m.GetOrgId()),
WorkspaceID: org.WorkspaceID(m.GetWorkspaceId()),
Role: org.Role(m.GetRole()),
}
})
orgID := org.OrgID(value.GetOrgId())
return access.Principal{
IdentityID: identity.IdentityID(value.GetIdentityId()),
Org: orgID,
Memberships: memberships,
Workspaces: mapValues(value.GetWorkspaces(), func(w *pb.PrincipalWorkspace) org.Workspace {
return org.Workspace{ID: org.WorkspaceID(w.GetWorkspaceId()), OrgID: orgID, Environment: w.GetEnvironment()}
}),
}, nil
}
func (c *Client) ListPolicies(ctx context.Context) ([]access.Policy, error) {
r, err := c.client.ListPolicies(ctx, &pb.ListPoliciesRequest{})
if err != nil {
return nil, unmapError(err)
}
return mapValues(r.Policies, accessPolicyValue), nil
}
func (c *Client) PoliciesVersion(ctx context.Context) (int64, error) {
r, err := c.client.GetPoliciesVersion(ctx, &pb.GetPoliciesVersionRequest{})
if err != nil {
return 0, unmapError(err)
}
return r.GetVersion(), nil
}
func (c *Client) PutPolicy(ctx context.Context, p access.Policy) (int64, error) {
r, err := c.client.PutPolicy(ctx, &pb.PutPolicyRequest{Policy: accessPolicyProto(p)})
if err != nil {
return 0, unmapError(err)
}
return r.Version, nil
}
func (c *Client) DeletePolicy(ctx context.Context, p access.Policy) error {
_, err := c.client.DeletePolicy(ctx, &pb.DeletePolicyRequest{Policy: accessPolicyProto(p)})
return unmapError(err)
}
func (c *Client) EnsureShippedOrgPolicies(ctx context.Context, orgID org.OrgID) error {
_, err := c.client.EnsureShippedOrgPolicies(ctx, &pb.EnsureShippedOrgPoliciesRequest{OrgId: string(orgID)})
return unmapError(err)
}
func (c *Client) RecordDenial(ctx context.Context, d access.Denial) error {
_, err := c.client.RecordDenial(ctx, &pb.RecordDenialRequest{Denial: accessDenialProto(d)})
return unmapError(err)
}