- redact single-use link codes from stored account.link events after handling, with a migration to scrub existing rows - refuse deletion of shipped org policies and hide the delete action in the UI - validate agent step models against the workflow model policy at save time, including the default model
145 lines
5.7 KiB
Go
145 lines
5.7 KiB
Go
package governance
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"net/http"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// fakePolicies applies the same validation and lockout rule as the module's
|
|
// store-backed methods, against policies held in memory.
|
|
type fakePolicies struct {
|
|
stored []Policy
|
|
puts []Policy
|
|
deletes []Policy
|
|
org []Principal
|
|
}
|
|
|
|
func (f *fakePolicies) ListPolicies(context.Context) ([]Policy, error) { return f.stored, nil }
|
|
|
|
func (f *fakePolicies) PutPolicy(ctx context.Context, p Policy) (int64, error) {
|
|
err := CheckPolicyChange(ctx, f.stored, func(cur []Policy) []Policy {
|
|
return append(slices.DeleteFunc(cur, func(c Policy) bool { return samePolicy(c, p) }), p)
|
|
}, []Org{{ID: DefaultOrgID}}, func(context.Context, OrgID) ([]Principal, error) { return f.org, nil })
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
f.puts = append(f.puts, p)
|
|
return 1, nil
|
|
}
|
|
|
|
func (f *fakePolicies) DeletePolicy(_ context.Context, p Policy) error {
|
|
f.deletes = append(f.deletes, p)
|
|
return nil
|
|
}
|
|
|
|
func policyRoutes(f *fakePolicies) *accessAPI {
|
|
return &accessAPI{policies: f, log: slog.New(slog.DiscardHandler)}
|
|
}
|
|
|
|
// TestPolicyEdits guards what a policy edit may do: an invalid policy is
|
|
// refused with the Cedar error and never stored, a change that leaves no owner
|
|
// able to manage policies is refused, and a policy lands in the caller's own
|
|
// organisation, with instance policies reserved for the instance owner.
|
|
func TestPolicyEdits(t *testing.T) {
|
|
owner := actor("root", RoleOwner)
|
|
otherOrgOwner := Principal{
|
|
IdentityID: "pat", Kind: KindUser, Org: "acme",
|
|
Memberships: []Membership{{IdentityID: "pat", OrgID: "acme", Role: RoleOwner}},
|
|
}
|
|
withOwner := func() *fakePolicies {
|
|
return &fakePolicies{stored: ShippedOrgPolicies(DefaultOrgID), org: []Principal{owner}}
|
|
}
|
|
policy := func(level Level, id, text string) Policy {
|
|
return Policy{ID: id, Level: level, OrgID: "elsewhere", Text: text}
|
|
}
|
|
const harmless = `permit(principal, action == Archie::Action::"read", resource);`
|
|
|
|
tests := []struct {
|
|
name string
|
|
by Principal
|
|
policy Policy
|
|
want int
|
|
wantError string
|
|
stored bool
|
|
wantOrg OrgID
|
|
}{
|
|
{name: "a valid policy", by: owner, policy: policy(LevelOrg, "p", harmless), want: http.StatusOK, stored: true, wantOrg: DefaultOrgID},
|
|
{name: "text that does not compile", by: owner, policy: policy(LevelOrg, "p", `permit(principal, action == Archie::Action::"nope", resource);`), want: http.StatusBadRequest, wantError: "nope"},
|
|
{name: "no policy text", by: owner, policy: policy(LevelOrg, "p", " "), want: http.StatusBadRequest, wantError: "no text"},
|
|
{name: "a forbid that locks owners out", by: owner, policy: policy(LevelOrg, "lock", `forbid(principal, action == Archie::Action::"manage_policies", resource);`), want: http.StatusConflict},
|
|
{name: "an instance policy by an instance owner", by: owner, policy: policy(LevelInstance, "i", `permit(principal, action, resource);`), want: http.StatusOK, stored: true},
|
|
{name: "an instance policy by another organisation's owner", by: otherOrgOwner, policy: policy(LevelInstance, "i", harmless), want: http.StatusForbidden},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
f := withOwner()
|
|
_, err := policyRoutes(f).putPolicy(WithPrincipal(t.Context(), tt.by), &policyInput{Body: tt.policy})
|
|
if got := status(t, err); got != tt.want {
|
|
t.Fatalf("status = %d (%v), want %d", got, err, tt.want)
|
|
}
|
|
if tt.wantError != "" && !strings.Contains(err.Error(), tt.wantError) {
|
|
t.Fatalf("error %q does not carry the Cedar error %q", err, tt.wantError)
|
|
}
|
|
if stored := len(f.puts) > 0; stored != tt.stored {
|
|
t.Fatalf("stored = %v, want %v", stored, tt.stored)
|
|
}
|
|
if tt.wantOrg != "" && f.puts[0].OrgID != tt.wantOrg {
|
|
t.Fatalf("policy stored for org %q, want %q", f.puts[0].OrgID, tt.wantOrg)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestShippedPolicyDeletion guards that a shipped org policy is edited and
|
|
// never deleted, because an org without an active org policy denies every
|
|
// request. A policy an operator created is deleted as usual.
|
|
func TestShippedPolicyDeletion(t *testing.T) {
|
|
owner := actor("root", RoleOwner)
|
|
const harmless = `permit(principal, action == Archie::Action::"read", resource);`
|
|
shipped := ShippedRolePolicies(DefaultOrgID)[0]
|
|
custom := Policy{ID: "custom", Level: LevelOrg, OrgID: DefaultOrgID, Text: harmless}
|
|
sameIDInstance := Policy{ID: shipped.ID, Level: LevelInstance, Text: harmless}
|
|
edited := shipped
|
|
edited.Text = harmless
|
|
|
|
tests := []struct {
|
|
name string
|
|
delete *Policy
|
|
put *Policy
|
|
want int
|
|
wantDeleted bool
|
|
wantPut bool
|
|
}{
|
|
{name: "delete a shipped policy", delete: &shipped, want: http.StatusConflict},
|
|
{name: "delete an operator policy", delete: &custom, want: http.StatusOK, wantDeleted: true},
|
|
{name: "delete an instance policy that reuses a shipped ID", delete: &sameIDInstance, want: http.StatusOK, wantDeleted: true},
|
|
{name: "edit a shipped policy", put: &edited, want: http.StatusOK, wantPut: true},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
f := &fakePolicies{stored: ShippedOrgPolicies(DefaultOrgID), org: []Principal{owner}}
|
|
routes := policyRoutes(f)
|
|
ctx := WithPrincipal(t.Context(), owner)
|
|
var err error
|
|
if tt.delete != nil {
|
|
_, err = routes.deletePolicy(ctx, &policyInput{Body: *tt.delete})
|
|
} else {
|
|
_, err = routes.putPolicy(ctx, &policyInput{Body: *tt.put})
|
|
}
|
|
if got := status(t, err); got != tt.want {
|
|
t.Fatalf("status = %d (%v), want %d", got, err, tt.want)
|
|
}
|
|
if deleted := len(f.deletes) > 0; deleted != tt.wantDeleted {
|
|
t.Fatalf("deleted = %v, want %v", deleted, tt.wantDeleted)
|
|
}
|
|
if put := len(f.puts) > 0; put != tt.wantPut {
|
|
t.Fatalf("put = %v, want %v", put, tt.wantPut)
|
|
}
|
|
})
|
|
}
|
|
}
|