| 1 |
/* |
| 2 |
Copyright 2016 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package authorizerfactory |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"k8s.io/apiserver/pkg/authentication/user" |
| 24 |
"k8s.io/apiserver/pkg/authorization/authorizer" |
| 25 |
) |
| 26 |
|
| 27 |
func TestNewAlwaysAllowAuthorizer(t *testing.T) { |
| 28 |
aaa := NewAlwaysAllowAuthorizer() |
| 29 |
if decision, _, _ := aaa.Authorize(context.Background(), nil); decision != authorizer.DecisionAllow { |
| 30 |
t.Errorf("AlwaysAllowAuthorizer.Authorize did not authorize successfully.") |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
func TestNewAlwaysDenyAuthorizer(t *testing.T) { |
| 35 |
ada := NewAlwaysDenyAuthorizer() |
| 36 |
if decision, _, _ := ada.Authorize(context.Background(), nil); decision == authorizer.DecisionAllow { |
| 37 |
t.Errorf("AlwaysDenyAuthorizer.Authorize returned nil instead of error.") |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
func TestPrivilegedGroupAuthorizer(t *testing.T) { |
| 42 |
auth := NewPrivilegedGroups("allow-01", "allow-01") |
| 43 |
|
| 44 |
yes := authorizer.AttributesRecord{User: &user.DefaultInfo{Groups: []string{"no", "allow-01"}}} |
| 45 |
no := authorizer.AttributesRecord{User: &user.DefaultInfo{Groups: []string{"no", "deny-01"}}} |
| 46 |
|
| 47 |
if authorized, _, _ := auth.Authorize(context.Background(), yes); authorized != authorizer.DecisionAllow { |
| 48 |
t.Errorf("failed") |
| 49 |
} |
| 50 |
if authorized, _, _ := auth.Authorize(context.Background(), no); authorized == authorizer.DecisionAllow { |
| 51 |
t.Errorf("failed") |
| 52 |
} |
| 53 |
} |