| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- package policy_test
- import (
- "testing"
- "github.com/porter-dev/porter/api/server/authz/policy"
- "github.com/porter-dev/porter/api/types"
- "github.com/stretchr/testify/assert"
- )
- type testHasScopeAccess struct {
- description string
- policy []*types.PolicyDocument
- reqScopes map[types.PermissionScope]*types.RequestAction
- expRes bool
- }
- var hasScopeAccessTests = []testHasScopeAccess{
- {
- description: "admin access to project",
- policy: types.AdminPolicy,
- reqScopes: map[types.PermissionScope]*types.RequestAction{
- types.ProjectScope: {
- Verb: types.APIVerbGet,
- Resource: types.NameOrUInt{
- UInt: 1,
- },
- },
- },
- expRes: true,
- },
- {
- description: "viewer access cannot perform write operation",
- policy: types.ViewerPolicy,
- reqScopes: map[types.PermissionScope]*types.RequestAction{
- types.ClusterScope: {
- Verb: types.APIVerbCreate,
- Resource: types.NameOrUInt{
- UInt: 1,
- },
- },
- },
- expRes: false,
- },
- {
- description: "developer access cannot write settings",
- policy: types.DeveloperPolicy,
- reqScopes: map[types.PermissionScope]*types.RequestAction{
- types.SettingsScope: {
- Verb: types.APIVerbUpdate,
- Resource: types.NameOrUInt{
- UInt: 1,
- },
- },
- },
- expRes: false,
- },
- {
- description: "custom policy for cluster 1 can write cluster 1",
- policy: testPolicySpecificClusters,
- reqScopes: map[types.PermissionScope]*types.RequestAction{
- types.ClusterScope: {
- Verb: types.APIVerbUpdate,
- Resource: types.NameOrUInt{
- UInt: 1,
- },
- },
- },
- expRes: true,
- },
- {
- description: "custom policy for cluster 1 cannot write cluster 2",
- policy: testPolicySpecificClusters,
- reqScopes: map[types.PermissionScope]*types.RequestAction{
- types.ClusterScope: {
- Verb: types.APIVerbUpdate,
- Resource: types.NameOrUInt{
- UInt: 2,
- },
- },
- },
- expRes: false,
- },
- {
- description: "cannot access wrong namespace + cluster combination",
- policy: testPolicyNamespaceSpecific,
- reqScopes: map[types.PermissionScope]*types.RequestAction{
- types.ClusterScope: {
- Verb: types.APIVerbGet,
- Resource: types.NameOrUInt{
- UInt: 500,
- },
- },
- types.NamespaceScope: {
- Verb: types.APIVerbGet,
- Resource: types.NameOrUInt{
- Name: "default",
- },
- },
- },
- expRes: false,
- },
- {
- description: "can access set namespace + cluster combination",
- policy: testPolicyNamespaceSpecific,
- reqScopes: map[types.PermissionScope]*types.RequestAction{
- types.ClusterScope: {
- Verb: types.APIVerbGet,
- Resource: types.NameOrUInt{
- UInt: 500,
- },
- },
- types.NamespaceScope: {
- Verb: types.APIVerbGet,
- Resource: types.NameOrUInt{
- Name: "abelanger",
- },
- },
- },
- expRes: true,
- },
- {
- description: "cannot write the set namespace + cluster combination",
- policy: testPolicyNamespaceSpecific,
- reqScopes: map[types.PermissionScope]*types.RequestAction{
- types.ClusterScope: {
- Verb: types.APIVerbGet,
- Resource: types.NameOrUInt{
- UInt: 500,
- },
- },
- types.NamespaceScope: {
- Verb: types.APIVerbDelete,
- Resource: types.NameOrUInt{
- Name: "abelanger",
- },
- },
- },
- expRes: false,
- },
- {
- description: "test invalid policy document",
- policy: testInvalidPolicyDocument,
- reqScopes: map[types.PermissionScope]*types.RequestAction{
- types.ProjectScope: {
- Verb: types.APIVerbGet,
- Resource: types.NameOrUInt{
- UInt: 1,
- },
- },
- },
- expRes: false,
- },
- {
- description: "test invalid policy document nested",
- policy: testInvalidPolicyDocumentNested,
- reqScopes: map[types.PermissionScope]*types.RequestAction{
- types.ProjectScope: {
- Verb: types.APIVerbGet,
- Resource: types.NameOrUInt{
- UInt: 1,
- },
- },
- },
- expRes: false,
- },
- }
- func TestHasScopeAccess(t *testing.T) {
- assert := assert.New(t)
- for _, test := range hasScopeAccessTests {
- res := policy.HasScopeAccess(
- test.policy,
- test.reqScopes,
- )
- assert.Equal(test.expRes, res, test.description)
- }
- }
- func BenchmarkSimpleHasScopeAccess(b *testing.B) {
- for i := 0; i < b.N; i++ {
- res := policy.HasScopeAccess(
- testPolicySpecificClusters,
- map[types.PermissionScope]*types.RequestAction{
- types.ClusterScope: {
- Verb: types.APIVerbCreate,
- Resource: types.NameOrUInt{
- UInt: 1,
- },
- },
- },
- )
- // we expect all results to be true, so fatal if not
- if !res {
- b.Fatalf("benchmark failed correctness: expected true")
- }
- }
- }
- var testPolicySpecificClusters = []*types.PolicyDocument{
- {
- Scope: types.ProjectScope,
- Verbs: types.ReadWriteVerbGroup(),
- Children: map[types.PermissionScope]*types.PolicyDocument{
- types.ClusterScope: {
- Scope: types.ClusterScope,
- Verbs: types.ReadWriteVerbGroup(),
- Resources: []types.NameOrUInt{
- {
- UInt: 1,
- },
- },
- },
- },
- },
- }
- var testPolicyNamespaceSpecific = []*types.PolicyDocument{
- // This document allows a user to view the namespace "abelanger" in the cluster
- // with id 500.
- {
- Scope: types.ProjectScope,
- Verbs: types.ReadWriteVerbGroup(),
- Children: map[types.PermissionScope]*types.PolicyDocument{
- types.ClusterScope: {
- Scope: types.ClusterScope,
- Verbs: types.ReadVerbGroup(),
- Resources: []types.NameOrUInt{
- {
- UInt: 500,
- },
- },
- Children: map[types.PermissionScope]*types.PolicyDocument{
- types.NamespaceScope: {
- Scope: types.NamespaceScope,
- Verbs: types.ReadVerbGroup(),
- Resources: []types.NameOrUInt{
- {
- Name: "abelanger",
- },
- },
- },
- },
- },
- },
- },
- // This document allows a user to view the namespace "default" in the cluster
- // with id 501.
- {
- Scope: types.ProjectScope,
- Verbs: types.ReadWriteVerbGroup(),
- Children: map[types.PermissionScope]*types.PolicyDocument{
- types.ClusterScope: {
- Scope: types.ClusterScope,
- Verbs: types.ReadVerbGroup(),
- Resources: []types.NameOrUInt{
- {
- UInt: 501,
- },
- },
- Children: map[types.PermissionScope]*types.PolicyDocument{
- types.NamespaceScope: {
- Scope: types.NamespaceScope,
- Verbs: types.ReadVerbGroup(),
- Resources: []types.NameOrUInt{
- {
- Name: "default",
- },
- },
- },
- },
- },
- },
- },
- }
- // NOTE: these are invalid policy documents that don't follow the accepted heirarchy
- // for scopes. Don't use this as a model for a valid doc.
- var testInvalidPolicyDocument = []*types.PolicyDocument{
- {
- // invalid because cluster above project
- Scope: types.ClusterScope,
- Verbs: types.ReadWriteVerbGroup(),
- Children: map[types.PermissionScope]*types.PolicyDocument{
- types.ProjectScope: {
- Scope: types.ProjectScope,
- Verbs: types.ReadWriteVerbGroup(),
- Resources: []types.NameOrUInt{
- {
- UInt: 1,
- },
- },
- },
- },
- },
- }
- var testInvalidPolicyDocumentNested = []*types.PolicyDocument{
- {
- // invalid because release is a child of cluster, not namespace scope
- Scope: types.ProjectScope,
- Verbs: types.ReadWriteVerbGroup(),
- Children: map[types.PermissionScope]*types.PolicyDocument{
- types.ClusterScope: {
- Scope: types.ClusterScope,
- Verbs: types.ReadWriteVerbGroup(),
- Resources: []types.NameOrUInt{
- {
- UInt: 1,
- },
- },
- Children: map[types.PermissionScope]*types.PolicyDocument{
- types.ReleaseScope: {
- Scope: types.ReleaseScope,
- Verbs: types.ReadWriteVerbGroup(),
- Resources: []types.NameOrUInt{
- {
- UInt: 1,
- },
- },
- },
- },
- },
- },
- },
- }
|