-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauthorization_test.go
179 lines (163 loc) · 6.21 KB
/
authorization_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package options
import (
"fmt"
"reflect"
"strings"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/spf13/pflag"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/kubernetes/pkg/kubeapiserver/authorizer/modes"
)
func TestAuthzValidate(t *testing.T) {
examplePolicyFile := "../../auth/authorizer/abac/example_policy_file.jsonl"
testCases := []struct {
name string
modes []string
policyFile string
webhookConfigFile string
webhookRetryBackoff *wait.Backoff
expectErr bool
expectErrorSubString string
}{
{
name: "Unknown modes should return errors",
modes: []string{"DoesNotExist"},
expectErr: true,
expectErrorSubString: "is not a valid mode",
},
{
name: "At least one authorizationMode is necessary",
modes: []string{},
expectErr: true,
expectErrorSubString: "at least one authorization-mode must be passed",
},
{
name: "ModeAlwaysAllow specified more than once",
modes: []string{modes.ModeAlwaysAllow, modes.ModeAlwaysAllow},
expectErr: true,
expectErrorSubString: "has mode specified more than once",
},
{
name: "ModeAlwaysAllow and ModeAlwaysDeny should return without authorizationPolicyFile",
modes: []string{modes.ModeAlwaysAllow, modes.ModeAlwaysDeny},
expectErr: false,
},
{
name: "ModeABAC requires a policy file",
modes: []string{modes.ModeAlwaysAllow, modes.ModeAlwaysDeny, modes.ModeABAC},
expectErr: true,
expectErrorSubString: "authorization-mode ABAC's authorization policy file not passed",
},
{
name: "Authorization Policy file cannot be used without ModeABAC",
modes: []string{modes.ModeAlwaysAllow, modes.ModeAlwaysDeny},
policyFile: examplePolicyFile,
webhookConfigFile: "",
expectErr: true,
expectErrorSubString: "cannot specify --authorization-policy-file without mode ABAC",
},
{
name: "ModeABAC should not error if a valid policy path is provided",
modes: []string{modes.ModeAlwaysAllow, modes.ModeAlwaysDeny, modes.ModeABAC},
policyFile: examplePolicyFile,
webhookConfigFile: "",
expectErr: false,
},
{
name: "ModeWebhook requires a config file",
modes: []string{modes.ModeWebhook},
expectErr: true,
expectErrorSubString: "authorization-mode Webhook's authorization config file not passed",
},
{
name: "Cannot provide webhook config file without ModeWebhook",
modes: []string{modes.ModeAlwaysAllow},
webhookConfigFile: "authz_webhook_config.yaml",
expectErr: true,
expectErrorSubString: "cannot specify --authorization-webhook-config-file without mode Webhook",
},
{
name: "ModeWebhook should not error if a valid config file is provided",
modes: []string{modes.ModeWebhook},
webhookConfigFile: "authz_webhook_config.yaml",
expectErr: false,
},
{
name: "ModeWebhook should error if an invalid number of webhook retry attempts is provided",
modes: []string{modes.ModeWebhook},
webhookConfigFile: "authz_webhook_config.yaml",
webhookRetryBackoff: &wait.Backoff{Steps: 0},
expectErr: true,
expectErrorSubString: "number of webhook retry attempts must be greater than 0",
},
}
for _, testcase := range testCases {
t.Run(testcase.name, func(t *testing.T) {
options := NewBuiltInAuthorizationOptions()
options.Modes = testcase.modes
options.WebhookConfigFile = testcase.webhookConfigFile
options.WebhookRetryBackoff = testcase.webhookRetryBackoff
options.PolicyFile = testcase.policyFile
errs := options.Validate()
if len(errs) > 0 && !testcase.expectErr {
t.Errorf("got unexpected err %v", errs)
}
if testcase.expectErr && len(errs) == 0 {
t.Errorf("should return an error")
}
if len(errs) > 0 && testcase.expectErr {
if !strings.Contains(utilerrors.NewAggregate(errs).Error(), testcase.expectErrorSubString) {
t.Errorf("exepected to found error: %s, but no error found", testcase.expectErrorSubString)
}
}
})
}
}
func TestBuiltInAuthorizationOptionsAddFlags(t *testing.T) {
var args = []string{
fmt.Sprintf("--authorization-mode=%s,%s,%s,%s", modes.ModeAlwaysAllow, modes.ModeAlwaysDeny, modes.ModeABAC, modes.ModeWebhook),
"--authorization-policy-file=policy_file.json",
"--authorization-webhook-config-file=webhook_config_file.yaml",
"--authorization-webhook-version=v1",
"--authorization-webhook-cache-authorized-ttl=60s",
"--authorization-webhook-cache-unauthorized-ttl=30s",
}
expected := &BuiltInAuthorizationOptions{
Modes: []string{modes.ModeAlwaysAllow, modes.ModeAlwaysDeny, modes.ModeABAC, modes.ModeWebhook},
PolicyFile: "policy_file.json",
WebhookConfigFile: "webhook_config_file.yaml",
WebhookVersion: "v1",
WebhookCacheAuthorizedTTL: 60 * time.Second,
WebhookCacheUnauthorizedTTL: 30 * time.Second,
WebhookRetryBackoff: &wait.Backoff{
Duration: 500 * time.Millisecond,
Factor: 1.5,
Jitter: 0.2,
Steps: 5,
},
}
opts := NewBuiltInAuthorizationOptions()
pf := pflag.NewFlagSet("test-builtin-authorization-opts", pflag.ContinueOnError)
opts.AddFlags(pf)
if err := pf.Parse(args); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(opts, expected) {
t.Error(cmp.Diff(opts, expected))
}
}