Webhook intake answers 503 and counts no refusal while the chain loads. The readiness probe reads the chain on every check. The UI never exits on a first-load failure. An invalid instance policy closes the instance but lets an instance admin repair policies. Outage pages are 503, and the health detail names the unloaded chain.
54 lines
2.8 KiB
Go
54 lines
2.8 KiB
Go
package access
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/access"
|
|
"github.com/samcharles93/archie-core/internal/domain/org"
|
|
)
|
|
|
|
func TestAuthorizeDelivery(t *testing.T) {
|
|
permitAll := access.Policy{ID: "all", Level: access.LevelInstance, Text: `permit(principal, action, resource);`}
|
|
lanOnly := access.Policy{ID: "lan-only", Level: access.LevelInstance, Text: `forbid(principal, action == Archie::Action::"deliver", resource) unless { context.addr.isInRange(ip("10.0.0.0/8")) };`}
|
|
sourceOnly := access.Policy{
|
|
ID: "github-only", Level: access.LevelObject, OrgID: org.DefaultOrgID, ObjectKind: access.KindSource, ObjectID: "github",
|
|
Text: `permit(principal, action == Archie::Action::"deliver", resource) when { context.addr.isInRange(ip("140.82.112.0/20")) };`,
|
|
}
|
|
acmeOnly := access.Policy{
|
|
ID: "acme-only", Level: access.LevelObject, OrgID: "acme", ObjectKind: access.KindSource, ObjectID: "hook",
|
|
Text: `permit(principal, action == Archie::Action::"deliver", resource) when { context.addr.isInRange(ip("10.0.0.0/8")) };`,
|
|
}
|
|
shipped := access.ShippedOrgPolicies(org.DefaultOrgID)
|
|
|
|
tests := []struct {
|
|
name string
|
|
org org.OrgID
|
|
policies []access.Policy
|
|
source string
|
|
addr string
|
|
allowed bool
|
|
}{
|
|
{name: "no rules admits every sender", policies: shipped, source: "github", addr: "203.0.113.9", allowed: true},
|
|
{name: "network rule admits its range", policies: append(shipped, permitAll, lanOnly), source: "ci", addr: "10.1.2.3", allowed: true},
|
|
{name: "network rule refuses outside its range", policies: append(shipped, permitAll, lanOnly), source: "ci", addr: "203.0.113.9"},
|
|
{name: "source policy refuses another sender", policies: append(shipped, sourceOnly), source: "github", addr: "203.0.113.9"},
|
|
{name: "source policy admits its sender", policies: append(shipped, sourceOnly), source: "github", addr: "140.82.112.5", allowed: true},
|
|
{name: "source policy leaves other sources alone", policies: append(shipped, sourceOnly), source: "ci", addr: "203.0.113.9", allowed: true},
|
|
{name: "other org's source policy refuses an out-of-range sender", org: "acme", policies: append(shipped, acmeOnly), source: "hook", addr: "203.0.113.9"},
|
|
{name: "other org's source policy admits its sender", org: "acme", policies: append(shipped, acmeOnly), source: "hook", addr: "10.1.2.3", allowed: true},
|
|
{name: "other org's source policy leaves the default org alone", policies: append(shipped, acmeOnly), source: "hook", addr: "203.0.113.9", allowed: true},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
engine := New(tt.policies)
|
|
acting := tt.org
|
|
if acting == "" {
|
|
acting = org.DefaultOrgID
|
|
}
|
|
if got := engine.AuthorizeDelivery(acting, tt.source, tt.addr); got.Allowed != tt.allowed {
|
|
t.Fatalf("allowed = %v, want %v (%+v)", got.Allowed, tt.allowed, got)
|
|
}
|
|
})
|
|
}
|
|
}
|