SPB Git

Repository Public

authz_test.go 4.8 KB 111 lines go

1package authz23import "testing"45func TestRoleDefaults(t *testing.T) {6	cases := []struct {7		role Role8		perm Permission9		want bool10	}{11		{Viewer, NodesRead, true}, {Viewer, NodesPrivateRead, false}, {Viewer, LogsRead, false}, {Viewer, AuditRead, false},12		{Viewer, ActionsRunLow, false}, {Operator, ActionsRunMedium, true}, {Operator, ActionsRunHigh, false},13		{Operator, UsersManage, false}, {Admin, ActionsRunHigh, true}, {Admin, ActionsRunCritical, false},14		{Admin, UsersManage, true}, {Owner, ActionsRunCritical, true}, {Role("root"), NodesRead, false},15	}16	for _, c := range cases {17		if got := Decide(Subject{UserID: "u", Role: c.role}, c.perm, Resource{}, nil); got != c.want {18			t.Errorf("%s %s = %v, want %v", c.role, c.perm, got, c.want)19		}20	}21}2223func TestDenyWinsAndScopes(t *testing.T) {24	s := Subject{UserID: "u1", Role: Operator}25	grants := []Grant{26		{SubjectKind: "user", Subject: "u1", Permission: ActionsRunMedium, ResourceKind: "node", ResourceID: "n1", Effect: Deny},27		{SubjectKind: "user", Subject: "u1", Permission: ActionsRunHigh, ResourceKind: "node", ResourceID: "n2", Effect: Allow},28		{SubjectKind: "role", Subject: "operator", Permission: AuditRead, Effect: Allow},29		{SubjectKind: "user", Subject: "other", Permission: NodesRead, Effect: Deny},30	}31	if Decide(s, ActionsRunMedium, Resource{"node", "n1"}, grants) {32		t.Error("scoped deny must win over role default")33	}34	if !Decide(s, ActionsRunMedium, Resource{"node", "n2"}, grants) {35		t.Error("deny on n1 must not affect n2")36	}37	if !Decide(s, ActionsRunHigh, Resource{"node", "n2"}, grants) || Decide(s, ActionsRunHigh, Resource{"node", "n1"}, grants) {38		t.Error("scoped allow must apply to its resource only")39	}40	if Decide(s, ActionsRunHigh, Resource{}, grants) {41		t.Error("scoped allow must not apply to an unscoped check")42	}43	if !Decide(s, AuditRead, Resource{}, grants) || !Decide(s, NodesRead, Resource{}, grants) {44		t.Error("role grant applies; other user's deny does not")45	}46}4748func TestGrantsCannotEscalate(t *testing.T) {49	g := func(p Permission) []Grant {50		return []Grant{{SubjectKind: "user", Subject: "u", Permission: p, Effect: Allow}}51	}52	if Decide(Subject{"u", Admin}, ActionsRunCritical, Resource{}, g(ActionsRunCritical)) {53		t.Error("critical actions are owner-only, even with a grant")54	}55	if Decide(Subject{"u", Operator}, UsersManage, Resource{}, g(UsersManage)) {56		t.Error("user management cannot be granted below admin")57	}58	if Decide(Subject{"u", Viewer}, Permission("made.up"), Resource{}, g("made.up")) {59		t.Error("unknown permission must be denied")60	}61}6263func TestCanAssignRole(t *testing.T) {64	if CanAssignRole(Admin, Viewer, Owner) || CanAssignRole(Admin, Owner, Viewer) || CanAssignRole(Operator, Viewer, Operator) {65		t.Error("only owners touch owners; operators assign nothing")66	}67	if !CanAssignRole(Admin, Viewer, Admin) || !CanAssignRole(Owner, Admin, Owner) {68		t.Error("admin may promote to admin; owner may promote to owner")69	}70}7172func TestDecideFacets(t *testing.T) {73	s := Subject{UserID: "u1", Role: Viewer}74	node := []Resource{{"node", "n1"}, {"site", "lab"}, {"tag", "gpu"}}75	grants := []Grant{76		{SubjectKind: "user", Subject: "u1", Permission: NodesPrivateRead, ResourceKind: "site", ResourceID: "lab", Effect: Allow},77		{SubjectKind: "role", Subject: "viewer", Permission: NodesRead, ResourceKind: "tag", ResourceID: "secret", Effect: Deny},78	}79	if !DecideFacets(s, NodesPrivateRead, node, grants) {80		t.Error("an allow on one facet (site) must grant access")81	}82	if DecideFacets(s, NodesPrivateRead, []Resource{{"node", "n2"}, {"site", "other"}}, grants) {83		t.Error("an allow on another site must not apply")84	}85	if !DecideFacets(s, NodesRead, node, grants) {86		t.Error("role default applies when no deny matches a facet")87	}88	if DecideFacets(s, NodesRead, append(node, Resource{"tag", "secret"}), grants) {89		t.Error("a deny on any facet must win")90	}91	if DecideFacets(s, NodesRead, nil, grants) != Decide(s, NodesRead, Resource{}, grants) {92		t.Error("without facets DecideFacets must equal Decide on the zero resource")93	}94	both := append(grants, Grant{SubjectKind: "user", Subject: "u1", Permission: NodesPrivateRead, ResourceKind: "node", ResourceID: "n1", Effect: Deny})95	if DecideFacets(s, NodesPrivateRead, node, both) {96		t.Error("deny on the node must win over an allow on its site")97	}98}99100func TestGrantable(t *testing.T) {101	if Grantable(Admin, ActionsRunCritical) || !Grantable(Owner, ActionsRunCritical) {102		t.Error("critical actions are grantable to owners only")103	}104	if Grantable(Operator, UsersManage) || !Grantable(Admin, UsersManage) {105		t.Error("administration permissions are not grantable below admin")106	}107	if !Grantable(Viewer, LogsRead) || Grantable(Viewer, Permission("made.up")) || Grantable(Role("nobody"), LogsRead) {108		t.Error("ordinary permissions are grantable; unknown permissions and roles are not")109	}110}111
Files main
Menu

Theme