| 1 |
/* |
| 2 |
Copyright 2017 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 admission |
| 18 |
|
| 19 |
import ( |
| 20 |
"io" |
| 21 |
"os" |
| 22 |
"reflect" |
| 23 |
"testing" |
| 24 |
|
| 25 |
"github.com/stretchr/testify/assert" |
| 26 |
"github.com/stretchr/testify/require" |
| 27 |
|
| 28 |
"k8s.io/apimachinery/pkg/runtime" |
| 29 |
"k8s.io/apimachinery/pkg/util/json" |
| 30 |
"k8s.io/apiserver/pkg/apis/apiserver" |
| 31 |
apiserverapiv1 "k8s.io/apiserver/pkg/apis/apiserver/v1" |
| 32 |
apiserverapiv1alpha1 "k8s.io/apiserver/pkg/apis/apiserver/v1alpha1" |
| 33 |
) |
| 34 |
|
| 35 |
func TestReadAdmissionConfiguration(t *testing.T) { |
| 36 |
// create a place holder file to hold per test config |
| 37 |
configFile, err := os.CreateTemp("", "admission-plugin-config") |
| 38 |
if err != nil { |
| 39 |
t.Fatalf("unexpected err: %v", err) |
| 40 |
} |
| 41 |
defer os.Remove(configFile.Name()) |
| 42 |
if err = configFile.Close(); err != nil { |
| 43 |
t.Fatalf("unexpected err: %v", err) |
| 44 |
} |
| 45 |
configFileName := configFile.Name() |
| 46 |
// the location that will be fixed up to be relative to the test config file. |
| 47 |
imagePolicyWebhookFile, err := makeAbs("image-policy-webhook.json", os.TempDir()) |
| 48 |
if err != nil { |
| 49 |
t.Fatalf("unexpected err: %v", err) |
| 50 |
} |
| 51 |
// individual test scenarios |
| 52 |
testCases := map[string]struct { |
| 53 |
ConfigBody string |
| 54 |
ExpectedAdmissionConfig *apiserver.AdmissionConfiguration |
| 55 |
PluginNames []string |
| 56 |
ExpectedError string |
| 57 |
}{ |
| 58 |
"duplicate field configuration error": { |
| 59 |
ConfigBody: `{ |
| 60 |
"apiVersion": "apiserver.k8s.io/v1alpha1", |
| 61 |
"kind": "AdmissionConfiguration", |
| 62 |
"plugins": [ |
| 63 |
{"name": "ImagePolicyWebhook-duplicate", "name": "ImagePolicyWebhook", "path": "image-policy-webhook.json"}, |
| 64 |
{"name": "ResourceQuota"} |
| 65 |
]}`, |
| 66 |
ExpectedError: "strict decoding error: duplicate field", |
| 67 |
}, |
| 68 |
"unknown field configuration error": { |
| 69 |
ConfigBody: `{ |
| 70 |
"apiVersion": "apiserver.k8s.io/v1alpha1", |
| 71 |
"kind": "AdmissionConfiguration", |
| 72 |
"plugins": [ |
| 73 |
{"foo": "bar", "name": "ImagePolicyWebhook", "path": "image-policy-webhook.json"}, |
| 74 |
{"name": "ResourceQuota"} |
| 75 |
]}`, |
| 76 |
ExpectedError: "strict decoding error: unknown field", |
| 77 |
}, |
| 78 |
"v1alpha1 configuration - path fixup": { |
| 79 |
ConfigBody: `{ |
| 80 |
"apiVersion": "apiserver.k8s.io/v1alpha1", |
| 81 |
"kind": "AdmissionConfiguration", |
| 82 |
"plugins": [ |
| 83 |
{"name": "ImagePolicyWebhook", "path": "image-policy-webhook.json"}, |
| 84 |
{"name": "ResourceQuota"} |
| 85 |
]}`, |
| 86 |
ExpectedAdmissionConfig: &apiserver.AdmissionConfiguration{ |
| 87 |
Plugins: []apiserver.AdmissionPluginConfiguration{ |
| 88 |
{ |
| 89 |
Name: "ImagePolicyWebhook", |
| 90 |
Path: imagePolicyWebhookFile, |
| 91 |
}, |
| 92 |
{ |
| 93 |
Name: "ResourceQuota", |
| 94 |
}, |
| 95 |
}, |
| 96 |
}, |
| 97 |
PluginNames: []string{}, |
| 98 |
}, |
| 99 |
"v1alpha1 configuration - abspath": { |
| 100 |
ConfigBody: `{ |
| 101 |
"apiVersion": "apiserver.k8s.io/v1alpha1", |
| 102 |
"kind": "AdmissionConfiguration", |
| 103 |
"plugins": [ |
| 104 |
{"name": "ImagePolicyWebhook", "path": "/tmp/image-policy-webhook.json"}, |
| 105 |
{"name": "ResourceQuota"} |
| 106 |
]}`, |
| 107 |
ExpectedAdmissionConfig: &apiserver.AdmissionConfiguration{ |
| 108 |
Plugins: []apiserver.AdmissionPluginConfiguration{ |
| 109 |
{ |
| 110 |
Name: "ImagePolicyWebhook", |
| 111 |
Path: "/tmp/image-policy-webhook.json", |
| 112 |
}, |
| 113 |
{ |
| 114 |
Name: "ResourceQuota", |
| 115 |
}, |
| 116 |
}, |
| 117 |
}, |
| 118 |
PluginNames: []string{}, |
| 119 |
}, |
| 120 |
"v1 configuration - path fixup": { |
| 121 |
ConfigBody: `{ |
| 122 |
"apiVersion": "apiserver.config.k8s.io/v1", |
| 123 |
"kind": "AdmissionConfiguration", |
| 124 |
"plugins": [ |
| 125 |
{"name": "ImagePolicyWebhook", "path": "image-policy-webhook.json"}, |
| 126 |
{"name": "ResourceQuota"} |
| 127 |
]}`, |
| 128 |
ExpectedAdmissionConfig: &apiserver.AdmissionConfiguration{ |
| 129 |
Plugins: []apiserver.AdmissionPluginConfiguration{ |
| 130 |
{ |
| 131 |
Name: "ImagePolicyWebhook", |
| 132 |
Path: imagePolicyWebhookFile, |
| 133 |
}, |
| 134 |
{ |
| 135 |
Name: "ResourceQuota", |
| 136 |
}, |
| 137 |
}, |
| 138 |
}, |
| 139 |
PluginNames: []string{}, |
| 140 |
}, |
| 141 |
"v1 configuration - abspath": { |
| 142 |
ConfigBody: `{ |
| 143 |
"apiVersion": "apiserver.config.k8s.io/v1", |
| 144 |
"kind": "AdmissionConfiguration", |
| 145 |
"plugins": [ |
| 146 |
{"name": "ImagePolicyWebhook", "path": "/tmp/image-policy-webhook.json"}, |
| 147 |
{"name": "ResourceQuota"} |
| 148 |
]}`, |
| 149 |
ExpectedAdmissionConfig: &apiserver.AdmissionConfiguration{ |
| 150 |
Plugins: []apiserver.AdmissionPluginConfiguration{ |
| 151 |
{ |
| 152 |
Name: "ImagePolicyWebhook", |
| 153 |
Path: "/tmp/image-policy-webhook.json", |
| 154 |
}, |
| 155 |
{ |
| 156 |
Name: "ResourceQuota", |
| 157 |
}, |
| 158 |
}, |
| 159 |
}, |
| 160 |
PluginNames: []string{}, |
| 161 |
}, |
| 162 |
"legacy configuration with using legacy plugins": { |
| 163 |
ConfigBody: `{ |
| 164 |
"imagePolicy": { |
| 165 |
"kubeConfigFile": "/home/user/.kube/config", |
| 166 |
"allowTTL": 30, |
| 167 |
"denyTTL": 30, |
| 168 |
"retryBackoff": 500, |
| 169 |
"defaultAllow": true |
| 170 |
}, |
| 171 |
"podNodeSelectorPluginConfig": { |
| 172 |
"clusterDefaultNodeSelector": "" |
| 173 |
} |
| 174 |
}`, |
| 175 |
ExpectedAdmissionConfig: &apiserver.AdmissionConfiguration{ |
| 176 |
Plugins: []apiserver.AdmissionPluginConfiguration{ |
| 177 |
{ |
| 178 |
Name: "ImagePolicyWebhook", |
| 179 |
Path: configFileName, |
| 180 |
}, |
| 181 |
{ |
| 182 |
Name: "PodNodeSelector", |
| 183 |
Path: configFileName, |
| 184 |
}, |
| 185 |
}, |
| 186 |
}, |
| 187 |
PluginNames: []string{"ImagePolicyWebhook", "PodNodeSelector"}, |
| 188 |
}, |
| 189 |
"legacy configuration not using legacy plugins": { |
| 190 |
ConfigBody: `{ |
| 191 |
"imagePolicy": { |
| 192 |
"kubeConfigFile": "/home/user/.kube/config", |
| 193 |
"allowTTL": 30, |
| 194 |
"denyTTL": 30, |
| 195 |
"retryBackoff": 500, |
| 196 |
"defaultAllow": true |
| 197 |
}, |
| 198 |
"podNodeSelectorPluginConfig": { |
| 199 |
"clusterDefaultNodeSelector": "" |
| 200 |
} |
| 201 |
}`, |
| 202 |
ExpectedAdmissionConfig: &apiserver.AdmissionConfiguration{}, |
| 203 |
PluginNames: []string{"NamespaceLifecycle", "InitialResources"}, |
| 204 |
}, |
| 205 |
} |
| 206 |
|
| 207 |
scheme := runtime.NewScheme() |
| 208 |
require.NoError(t, apiserver.AddToScheme(scheme)) |
| 209 |
require.NoError(t, apiserverapiv1alpha1.AddToScheme(scheme)) |
| 210 |
require.NoError(t, apiserverapiv1.AddToScheme(scheme)) |
| 211 |
|
| 212 |
for testName, testCase := range testCases { |
| 213 |
if err = os.WriteFile(configFileName, []byte(testCase.ConfigBody), 0644); err != nil { |
| 214 |
t.Fatalf("unexpected err writing temp file: %v", err) |
| 215 |
} |
| 216 |
config, err := ReadAdmissionConfiguration(testCase.PluginNames, configFileName, scheme) |
| 217 |
if testCase.ExpectedError != "" { |
| 218 |
if err != nil { |
| 219 |
assert.Contains(t, err.Error(), testCase.ExpectedError) |
| 220 |
} else { |
| 221 |
t.Fatalf("expected error %q but received none", testCase.ExpectedError) |
| 222 |
} |
| 223 |
} else if err != nil { |
| 224 |
t.Fatalf("unexpected err: %v", err) |
| 225 |
} else if !reflect.DeepEqual(config.(configProvider).config, testCase.ExpectedAdmissionConfig) { |
| 226 |
t.Fatalf("%s: Expected:\n\t%#v\nGot:\n\t%#v", testName, testCase.ExpectedAdmissionConfig, config.(configProvider).config) |
| 227 |
} |
| 228 |
|
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
func TestEmbeddedConfiguration(t *testing.T) { |
| 233 |
// create a place holder file to hold per test config |
| 234 |
configFile, err := os.CreateTemp("", "admission-plugin-config") |
| 235 |
if err != nil { |
| 236 |
t.Fatalf("unexpected err: %v", err) |
| 237 |
} |
| 238 |
defer os.Remove(configFile.Name()) |
| 239 |
if err = configFile.Close(); err != nil { |
| 240 |
t.Fatalf("unexpected err: %v", err) |
| 241 |
} |
| 242 |
configFileName := configFile.Name() |
| 243 |
|
| 244 |
testCases := map[string]struct { |
| 245 |
ConfigBody string |
| 246 |
ExpectedConfig string |
| 247 |
}{ |
| 248 |
"v1alpha1 versioned configuration": { |
| 249 |
ConfigBody: `{ |
| 250 |
"apiVersion": "apiserver.k8s.io/v1alpha1", |
| 251 |
"kind": "AdmissionConfiguration", |
| 252 |
"plugins": [ |
| 253 |
{ |
| 254 |
"name": "Foo", |
| 255 |
"configuration": { |
| 256 |
"apiVersion": "foo.admission.k8s.io/v1alpha1", |
| 257 |
"kind": "Configuration", |
| 258 |
"foo": "bar" |
| 259 |
} |
| 260 |
} |
| 261 |
]}`, |
| 262 |
ExpectedConfig: `{ |
| 263 |
"apiVersion": "foo.admission.k8s.io/v1alpha1", |
| 264 |
"kind": "Configuration", |
| 265 |
"foo": "bar" |
| 266 |
}`, |
| 267 |
}, |
| 268 |
"v1alpha1 legacy configuration": { |
| 269 |
ConfigBody: `{ |
| 270 |
"apiVersion": "apiserver.k8s.io/v1alpha1", |
| 271 |
"kind": "AdmissionConfiguration", |
| 272 |
"plugins": [ |
| 273 |
{ |
| 274 |
"name": "Foo", |
| 275 |
"configuration": { |
| 276 |
"foo": "bar" |
| 277 |
} |
| 278 |
} |
| 279 |
]}`, |
| 280 |
ExpectedConfig: `{ |
| 281 |
"foo": "bar" |
| 282 |
}`, |
| 283 |
}, |
| 284 |
"v1 versioned configuration": { |
| 285 |
ConfigBody: `{ |
| 286 |
"apiVersion": "apiserver.config.k8s.io/v1", |
| 287 |
"kind": "AdmissionConfiguration", |
| 288 |
"plugins": [ |
| 289 |
{ |
| 290 |
"name": "Foo", |
| 291 |
"configuration": { |
| 292 |
"apiVersion": "foo.admission.k8s.io/v1alpha1", |
| 293 |
"kind": "Configuration", |
| 294 |
"foo": "bar" |
| 295 |
} |
| 296 |
} |
| 297 |
]}`, |
| 298 |
ExpectedConfig: `{ |
| 299 |
"apiVersion": "foo.admission.k8s.io/v1alpha1", |
| 300 |
"kind": "Configuration", |
| 301 |
"foo": "bar" |
| 302 |
}`, |
| 303 |
}, |
| 304 |
"v1 legacy configuration": { |
| 305 |
ConfigBody: `{ |
| 306 |
"apiVersion": "apiserver.config.k8s.io/v1", |
| 307 |
"kind": "AdmissionConfiguration", |
| 308 |
"plugins": [ |
| 309 |
{ |
| 310 |
"name": "Foo", |
| 311 |
"configuration": { |
| 312 |
"foo": "bar" |
| 313 |
} |
| 314 |
} |
| 315 |
]}`, |
| 316 |
ExpectedConfig: `{ |
| 317 |
"foo": "bar" |
| 318 |
}`, |
| 319 |
}, |
| 320 |
} |
| 321 |
|
| 322 |
for desc, test := range testCases { |
| 323 |
scheme := runtime.NewScheme() |
| 324 |
require.NoError(t, apiserver.AddToScheme(scheme)) |
| 325 |
require.NoError(t, apiserverapiv1alpha1.AddToScheme(scheme)) |
| 326 |
require.NoError(t, apiserverapiv1.AddToScheme(scheme)) |
| 327 |
|
| 328 |
if err = os.WriteFile(configFileName, []byte(test.ConfigBody), 0644); err != nil { |
| 329 |
t.Errorf("[%s] unexpected err writing temp file: %v", desc, err) |
| 330 |
continue |
| 331 |
} |
| 332 |
config, err := ReadAdmissionConfiguration([]string{"Foo"}, configFileName, scheme) |
| 333 |
if err != nil { |
| 334 |
t.Errorf("[%s] unexpected err: %v", desc, err) |
| 335 |
continue |
| 336 |
} |
| 337 |
r, err := config.ConfigFor("Foo") |
| 338 |
if err != nil { |
| 339 |
t.Errorf("[%s] Failed to get Foo config: %v", desc, err) |
| 340 |
continue |
| 341 |
} |
| 342 |
bs, err := io.ReadAll(r) |
| 343 |
if err != nil { |
| 344 |
t.Errorf("[%s] Failed to read Foo config data: %v", desc, err) |
| 345 |
continue |
| 346 |
} |
| 347 |
|
| 348 |
if !equalJSON(test.ExpectedConfig, string(bs)) { |
| 349 |
t.Errorf("Unexpected config: expected=%q got=%q", test.ExpectedConfig, string(bs)) |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
func equalJSON(a, b string) bool { |
| 355 |
var x, y interface{} |
| 356 |
if err := json.Unmarshal([]byte(a), &x); err != nil { |
| 357 |
return false |
| 358 |
} |
| 359 |
if err := json.Unmarshal([]byte(b), &y); err != nil { |
| 360 |
return false |
| 361 |
} |
| 362 |
return reflect.DeepEqual(x, y) |
| 363 |
} |