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.
99 lines
3.5 KiB
Go
99 lines
3.5 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.DenialStore = (*Client)(nil)
|
|
_ access.PrincipalSource = (*Client)(nil)
|
|
)
|
|
|
|
func (c *Client) PrincipalFor(ctx context.Context, id identity.IdentityID) (access.Principal, error) {
|
|
r, err := c.access.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.access.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.access.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.access.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.access.DeletePolicy(ctx, &pb.DeletePolicyRequest{Policy: accessPolicyProto(p)})
|
|
return unmapError(err)
|
|
}
|
|
|
|
func (c *Client) EnsureShippedOrgPolicies(ctx context.Context, orgID org.OrgID) error {
|
|
_, err := c.access.EnsureShippedOrgPolicies(ctx, &pb.EnsureShippedOrgPoliciesRequest{OrgId: string(orgID)})
|
|
return unmapError(err)
|
|
}
|
|
|
|
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
|
|
}
|