|
|
|
@@ -125,7 +125,7 @@ func TestPendingChainRefusesUntilLoaded(t *testing.T) {
|
|
|
|
|
if !loaded {
|
|
|
|
|
return nil, errors.New("store down")
|
|
|
|
|
}
|
|
|
|
|
return nil, nil
|
|
|
|
|
return ShippedOrgPolicies(DefaultOrgID), nil
|
|
|
|
|
}, slog.New(slog.DiscardHandler))
|
|
|
|
|
|
|
|
|
|
request := func() Decision {
|
|
|
|
@@ -199,17 +199,20 @@ func TestLiveAppliesAPolicyEditBeforeTheNextDecision(t *testing.T) {
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "an unchanged version keeps the chain",
|
|
|
|
|
write: func(store *fakePolicyStore) { store.policies = []Policy{forbidAll} },
|
|
|
|
|
write: func(store *fakePolicyStore) { store.policies = append(ShippedOrgPolicies(DefaultOrgID), forbidAll) },
|
|
|
|
|
wantAllowed: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "a bumped version applies the forbid",
|
|
|
|
|
write: func(store *fakePolicyStore) { store.policies = []Policy{forbidAll}; store.version++ },
|
|
|
|
|
name: "a bumped version applies the forbid",
|
|
|
|
|
write: func(store *fakePolicyStore) {
|
|
|
|
|
store.policies = append(ShippedOrgPolicies(DefaultOrgID), forbidAll)
|
|
|
|
|
store.version++
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "a failed version read refuses",
|
|
|
|
|
write: func(store *fakePolicyStore) {
|
|
|
|
|
store.policies = []Policy{forbidAll}
|
|
|
|
|
store.policies = append(ShippedOrgPolicies(DefaultOrgID), forbidAll)
|
|
|
|
|
store.version++
|
|
|
|
|
store.versionErr = storeDown
|
|
|
|
|
},
|
|
|
|
@@ -223,7 +226,7 @@ func TestLiveAppliesAPolicyEditBeforeTheNextDecision(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
store := &fakePolicyStore{version: 1}
|
|
|
|
|
store := &fakePolicyStore{version: 1, policies: ShippedOrgPolicies(DefaultOrgID)}
|
|
|
|
|
live := NewPending(store.PoliciesVersion, store.ListPolicies, slog.New(slog.DiscardHandler))
|
|
|
|
|
if err := live.Reload(t.Context()); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
@@ -496,3 +499,35 @@ func TestSeedShippedPolicies(t *testing.T) {
|
|
|
|
|
t.Fatal("a failed seed returned no error")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TestAuthorizeWithoutOrgPolicies guards the deny-by-default rule: an org
|
|
|
|
|
// with no stored org policies, or only narrowing ones, must refuse every
|
|
|
|
|
// request instead of falling through to allow.
|
|
|
|
|
func TestAuthorizeWithoutOrgPolicies(t *testing.T) {
|
|
|
|
|
const org = OrgID("acme")
|
|
|
|
|
owner := Principal{IdentityID: "o", Org: org, Memberships: []Membership{{IdentityID: "o", OrgID: org, Role: RoleOwner}}}
|
|
|
|
|
permitAllInstance := Policy{ID: "all", Level: LevelInstance, Text: `permit(principal, action, resource);`}
|
|
|
|
|
spaceOnly := Policy{
|
|
|
|
|
ID: "space", Level: LevelWorkspace, OrgID: org, WorkspaceID: "prod",
|
|
|
|
|
Text: `permit(principal, action, resource);`,
|
|
|
|
|
}
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
policies []Policy
|
|
|
|
|
allowed bool
|
|
|
|
|
}{
|
|
|
|
|
{name: "no policies at all"},
|
|
|
|
|
{name: "another org is seeded", policies: ShippedOrgPolicies(DefaultOrgID)},
|
|
|
|
|
{name: "instance policy only", policies: []Policy{permitAllInstance}},
|
|
|
|
|
{name: "workspace policy only", policies: []Policy{spaceOnly}},
|
|
|
|
|
{name: "shipped org policies", policies: ShippedOrgPolicies(org), allowed: true},
|
|
|
|
|
}
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
got := NewEngine(tt.policies).Authorize(t.Context(), owner, ActionRead, Resource{Kind: KindDashboard, Org: org}, AccessContext{})
|
|
|
|
|
if got.Allowed != tt.allowed {
|
|
|
|
|
t.Fatalf("allowed = %v, want %v (%+v)", got.Allowed, tt.allowed, got)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|