@@ -28,7 +28,8 @@ func TestAuthorize(t *testing.T) {
28
28
ResourceRequest : true ,
29
29
Namespace : "ns" ,
30
30
},
31
- expectedErr : `user missing from context` ,
31
+ expectedAllowed : kauthorizer .DecisionNoOpinion ,
32
+ expectedErr : `user missing from context` ,
32
33
},
33
34
{
34
35
name : "no extra" ,
@@ -37,7 +38,8 @@ func TestAuthorize(t *testing.T) {
37
38
ResourceRequest : true ,
38
39
Namespace : "ns" ,
39
40
},
40
- expectedCalled : true ,
41
+ expectedCalled : true ,
42
+ expectedAllowed : kauthorizer .DecisionNoOpinion ,
41
43
},
42
44
{
43
45
name : "empty extra" ,
@@ -46,7 +48,8 @@ func TestAuthorize(t *testing.T) {
46
48
ResourceRequest : true ,
47
49
Namespace : "ns" ,
48
50
},
49
- expectedCalled : true ,
51
+ expectedCalled : true ,
52
+ expectedAllowed : kauthorizer .DecisionNoOpinion ,
50
53
},
51
54
{
52
55
name : "empty scopes" ,
@@ -55,7 +58,8 @@ func TestAuthorize(t *testing.T) {
55
58
ResourceRequest : true ,
56
59
Namespace : "ns" ,
57
60
},
58
- expectedCalled : true ,
61
+ expectedCalled : true ,
62
+ expectedAllowed : kauthorizer .DecisionNoOpinion ,
59
63
},
60
64
{
61
65
name : "bad scope" ,
@@ -64,8 +68,9 @@ func TestAuthorize(t *testing.T) {
64
68
ResourceRequest : true ,
65
69
Namespace : "ns" ,
66
70
},
67
- expectedMsg : `scopes [does-not-exist] prevent this action; User "" cannot "" "" with name "" in project "ns"` ,
68
- expectedErr : `no scope evaluator found for "does-not-exist"` ,
71
+ expectedAllowed : kauthorizer .DecisionDeny ,
72
+ expectedMsg : `scopes [does-not-exist] prevent this action; User "" cannot "" "" with name "" in project "ns"` ,
73
+ expectedErr : `no scope evaluator found for "does-not-exist"` ,
69
74
},
70
75
{
71
76
name : "bad scope 2" ,
@@ -74,8 +79,9 @@ func TestAuthorize(t *testing.T) {
74
79
ResourceRequest : true ,
75
80
Namespace : "ns" ,
76
81
},
77
- expectedMsg : `scopes [user:dne] prevent this action; User "" cannot "" "" with name "" in project "ns"` ,
78
- expectedErr : `unrecognized scope: user:dne` ,
82
+ expectedAllowed : kauthorizer .DecisionDeny ,
83
+ expectedMsg : `scopes [user:dne] prevent this action; User "" cannot "" "" with name "" in project "ns"` ,
84
+ expectedErr : `unrecognized scope: user:dne` ,
79
85
},
80
86
{
81
87
name : "scope doesn't cover" ,
@@ -84,7 +90,8 @@ func TestAuthorize(t *testing.T) {
84
90
ResourceRequest : true ,
85
91
Namespace : "ns" ,
86
92
Verb : "get" , Resource : "users" , Name : "harold" },
87
- expectedMsg : `scopes [user:info] prevent this action; User "" cannot get users in project "ns"` ,
93
+ expectedAllowed : kauthorizer .DecisionDeny ,
94
+ expectedMsg : `scopes [user:info] prevent this action; User "" cannot get users in project "ns"` ,
88
95
},
89
96
{
90
97
name : "scope covers" ,
@@ -93,7 +100,8 @@ func TestAuthorize(t *testing.T) {
93
100
ResourceRequest : true ,
94
101
Namespace : "ns" ,
95
102
Verb : "get" , Resource : "users" , Name : "~" },
96
- expectedCalled : true ,
103
+ expectedCalled : true ,
104
+ expectedAllowed : kauthorizer .DecisionNoOpinion ,
97
105
},
98
106
{
99
107
name : "scope covers for discovery" ,
@@ -102,7 +110,8 @@ func TestAuthorize(t *testing.T) {
102
110
ResourceRequest : false ,
103
111
Namespace : "ns" ,
104
112
Verb : "get" , Path : "/api" },
105
- expectedCalled : true ,
113
+ expectedCalled : true ,
114
+ expectedAllowed : kauthorizer .DecisionNoOpinion ,
106
115
},
107
116
{
108
117
name : "user:full covers any resource" ,
@@ -111,7 +120,8 @@ func TestAuthorize(t *testing.T) {
111
120
ResourceRequest : true ,
112
121
Namespace : "ns" ,
113
122
Verb : "update" , Resource : "users" , Name : "harold" },
114
- expectedCalled : true ,
123
+ expectedCalled : true ,
124
+ expectedAllowed : kauthorizer .DecisionNoOpinion ,
115
125
},
116
126
{
117
127
name : "user:full covers any non-resource" ,
@@ -120,35 +130,38 @@ func TestAuthorize(t *testing.T) {
120
130
ResourceRequest : false ,
121
131
Namespace : "ns" ,
122
132
Verb : "post" , Path : "/foo/bar/baz" },
123
- expectedCalled : true ,
133
+ expectedCalled : true ,
134
+ expectedAllowed : kauthorizer .DecisionNoOpinion ,
124
135
},
125
136
}
126
137
127
138
for _ , tc := range testCases {
128
- delegate := & fakeAuthorizer {allowed : tc .delegateAuthAllowed }
129
- authorizer := NewAuthorizer (delegate , nil , defaultauthorizer .NewForbiddenMessageResolver ("" ))
139
+ t .Run (tc .name , func (t * testing.T ) {
140
+ delegate := & fakeAuthorizer {allowed : tc .delegateAuthAllowed }
141
+ authorizer := NewAuthorizer (delegate , nil , defaultauthorizer .NewForbiddenMessageResolver ("" ))
130
142
131
- actualAllowed , actualMsg , actualErr := authorizer .Authorize (tc .attributes )
132
- switch {
133
- case len (tc .expectedErr ) == 0 && actualErr == nil :
134
- case len (tc .expectedErr ) == 0 && actualErr != nil :
135
- t .Errorf ("%s: unexpected error: %v" , tc .name , actualErr )
136
- case len (tc .expectedErr ) != 0 && actualErr == nil :
137
- t .Errorf ("%s: missing error: %v" , tc .name , tc .expectedErr )
138
- case len (tc .expectedErr ) != 0 && actualErr != nil :
139
- if ! strings .Contains (actualErr .Error (), tc .expectedErr ) {
140
- t .Errorf ("%s: expected %v, got %v" , tc .name , tc .expectedErr , actualErr )
143
+ actualAllowed , actualMsg , actualErr := authorizer .Authorize (tc .attributes )
144
+ switch {
145
+ case len (tc .expectedErr ) == 0 && actualErr == nil :
146
+ case len (tc .expectedErr ) == 0 && actualErr != nil :
147
+ t .Errorf ("%s: unexpected error: %v" , tc .name , actualErr )
148
+ case len (tc .expectedErr ) != 0 && actualErr == nil :
149
+ t .Errorf ("%s: missing error: %v" , tc .name , tc .expectedErr )
150
+ case len (tc .expectedErr ) != 0 && actualErr != nil :
151
+ if ! strings .Contains (actualErr .Error (), tc .expectedErr ) {
152
+ t .Errorf ("expected %v, got %v" , tc .expectedErr , actualErr )
153
+ }
141
154
}
142
- }
143
- if tc .expectedMsg != actualMsg {
144
- t . Errorf ( "%s: expected %v, got %v" , tc . name , tc . expectedMsg , actualMsg )
145
- }
146
- if tc .expectedAllowed != actualAllowed {
147
- t . Errorf ( "%s: expected %v, got %v" , tc . name , tc . expectedAllowed , actualAllowed )
148
- }
149
- if tc .expectedCalled != delegate .called {
150
- t . Errorf ( "%s: expected %v, got %v" , tc . name , tc . expectedCalled , delegate . called )
151
- }
155
+ if tc . expectedMsg != actualMsg {
156
+ t . Errorf ( "expected %v, got %v" , tc .expectedMsg , actualMsg )
157
+ }
158
+ if tc . expectedAllowed != actualAllowed {
159
+ t . Errorf ( "expected %v, got %v" , tc .expectedAllowed , actualAllowed )
160
+ }
161
+ if tc . expectedCalled != delegate . called {
162
+ t . Errorf ( "expected %v, got %v" , tc .expectedCalled , delegate .called )
163
+ }
164
+ })
152
165
}
153
166
}
154
167
@@ -162,7 +175,7 @@ func (a *fakeAuthorizer) Authorize(passedAttributes kauthorizer.Attributes) (kau
162
175
if a .allowed {
163
176
return kauthorizer .DecisionAllow , "" , nil
164
177
}
165
- return kauthorizer .DecisionDeny , "" , nil
178
+ return kauthorizer .DecisionNoOpinion , "" , nil
166
179
}
167
180
168
181
func (a * fakeAuthorizer ) GetAllowedSubjects (attributes kauthorizer.Attributes ) (sets.String , sets.String , error ) {
0 commit comments