| 1 |
/* |
| 2 |
Copyright 2022 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 validating |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"errors" |
| 22 |
"fmt" |
| 23 |
"strings" |
| 24 |
"testing" |
| 25 |
"time" |
| 26 |
|
| 27 |
celtypes "github.com/google/cel-go/common/types" |
| 28 |
"github.com/stretchr/testify/require" |
| 29 |
|
| 30 |
admissionv1 "k8s.io/api/admission/v1" |
| 31 |
v1 "k8s.io/api/admissionregistration/v1" |
| 32 |
corev1 "k8s.io/api/core/v1" |
| 33 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 34 |
"k8s.io/apimachinery/pkg/runtime" |
| 35 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 36 |
"k8s.io/apiserver/pkg/admission" |
| 37 |
"k8s.io/apiserver/pkg/admission/plugin/cel" |
| 38 |
"k8s.io/apiserver/pkg/admission/plugin/webhook/matchconditions" |
| 39 |
celconfig "k8s.io/apiserver/pkg/apis/cel" |
| 40 |
"k8s.io/apiserver/pkg/authorization/authorizer" |
| 41 |
apiservercel "k8s.io/apiserver/pkg/cel" |
| 42 |
"k8s.io/apiserver/pkg/cel/environment" |
| 43 |
) |
| 44 |
|
| 45 |
var _ cel.ConditionEvaluator = &fakeCelFilter{} |
| 46 |
|
| 47 |
type fakeCelFilter struct { |
| 48 |
evaluations []cel.EvaluationResult |
| 49 |
throwError bool |
| 50 |
} |
| 51 |
|
| 52 |
func (f *fakeCelFilter) ForInput(ctx context.Context, versionedAttr *admission.VersionedAttributes, request *admissionv1.AdmissionRequest, optionalVars cel.OptionalVariableBindings, namespace *corev1.Namespace, costBudget int64) ([]cel.EvaluationResult, int64, error) { |
| 53 |
if costBudget <= 0 { // this filter will cost 1, so cost = 0 means fail. |
| 54 |
return nil, -1, &apiservercel.Error{ |
| 55 |
Type: apiservercel.ErrorTypeInvalid, |
| 56 |
Detail: fmt.Sprintf("validation failed due to running out of cost budget, no further validation rules will be run"), |
| 57 |
Cause: apiservercel.ErrOutOfBudget, |
| 58 |
} |
| 59 |
} |
| 60 |
if f.throwError { |
| 61 |
return nil, -1, errors.New("test error") |
| 62 |
} |
| 63 |
return f.evaluations, costBudget - 1, nil |
| 64 |
} |
| 65 |
|
| 66 |
func (f *fakeCelFilter) CompilationErrors() []error { |
| 67 |
return []error{} |
| 68 |
} |
| 69 |
|
| 70 |
var _ matchconditions.Matcher = &fakeCELMatcher{} |
| 71 |
|
| 72 |
type fakeCELMatcher struct { |
| 73 |
error error |
| 74 |
matches bool |
| 75 |
} |
| 76 |
|
| 77 |
func (f *fakeCELMatcher) Match(ctx context.Context, versionedAttr *admission.VersionedAttributes, versionedParams runtime.Object, authz authorizer.Authorizer) matchconditions.MatchResult { |
| 78 |
return matchconditions.MatchResult{Matches: f.matches, FailedConditionName: "placeholder", Error: f.error} |
| 79 |
} |
| 80 |
|
| 81 |
func TestValidate(t *testing.T) { |
| 82 |
ignore := v1.Ignore |
| 83 |
fail := v1.Fail |
| 84 |
|
| 85 |
forbiddenReason := metav1.StatusReasonForbidden |
| 86 |
unauthorizedReason := metav1.StatusReasonUnauthorized |
| 87 |
|
| 88 |
fakeAttr := admission.NewAttributesRecord(nil, nil, schema.GroupVersionKind{}, "default", "foo", schema.GroupVersionResource{}, "", admission.Create, nil, false, nil) |
| 89 |
fakeVersionedAttr, _ := admission.NewVersionedAttributes(fakeAttr, schema.GroupVersionKind{}, nil) |
| 90 |
|
| 91 |
cases := []struct { |
| 92 |
name string |
| 93 |
failPolicy *v1.FailurePolicyType |
| 94 |
matcher matchconditions.Matcher |
| 95 |
evaluations []cel.EvaluationResult |
| 96 |
messageEvaluations []cel.EvaluationResult |
| 97 |
auditEvaluations []cel.EvaluationResult |
| 98 |
policyDecision []PolicyDecision |
| 99 |
auditAnnotations []PolicyAuditAnnotation |
| 100 |
throwError bool |
| 101 |
costBudget int64 // leave zero to use default |
| 102 |
}{ |
| 103 |
{ |
| 104 |
name: "test pass", |
| 105 |
evaluations: []cel.EvaluationResult{ |
| 106 |
{ |
| 107 |
EvalResult: celtypes.True, |
| 108 |
ExpressionAccessor: &ValidationCondition{}, |
| 109 |
Elapsed: time.Millisecond, |
| 110 |
}, |
| 111 |
}, |
| 112 |
policyDecision: []PolicyDecision{ |
| 113 |
{ |
| 114 |
Action: ActionAdmit, |
| 115 |
Elapsed: time.Millisecond, |
| 116 |
}, |
| 117 |
}, |
| 118 |
}, |
| 119 |
{ |
| 120 |
name: "test multiple pass", |
| 121 |
evaluations: []cel.EvaluationResult{ |
| 122 |
{ |
| 123 |
EvalResult: celtypes.True, |
| 124 |
ExpressionAccessor: &ValidationCondition{}, |
| 125 |
Elapsed: time.Millisecond, |
| 126 |
}, |
| 127 |
{ |
| 128 |
EvalResult: celtypes.True, |
| 129 |
ExpressionAccessor: &ValidationCondition{}, |
| 130 |
Elapsed: time.Millisecond, |
| 131 |
}, |
| 132 |
}, |
| 133 |
policyDecision: []PolicyDecision{ |
| 134 |
{ |
| 135 |
Action: ActionAdmit, |
| 136 |
Elapsed: time.Millisecond, |
| 137 |
}, |
| 138 |
{ |
| 139 |
Action: ActionAdmit, |
| 140 |
Elapsed: time.Millisecond, |
| 141 |
}, |
| 142 |
}, |
| 143 |
}, |
| 144 |
{ |
| 145 |
name: "test error with failurepolicy ignore", |
| 146 |
evaluations: []cel.EvaluationResult{ |
| 147 |
{ |
| 148 |
Error: errors.New(""), |
| 149 |
ExpressionAccessor: &ValidationCondition{}, |
| 150 |
Elapsed: time.Millisecond, |
| 151 |
}, |
| 152 |
}, |
| 153 |
policyDecision: []PolicyDecision{ |
| 154 |
{ |
| 155 |
Action: ActionAdmit, |
| 156 |
Elapsed: time.Millisecond, |
| 157 |
}, |
| 158 |
}, |
| 159 |
failPolicy: &ignore, |
| 160 |
}, |
| 161 |
{ |
| 162 |
name: "test error with failurepolicy nil", |
| 163 |
evaluations: []cel.EvaluationResult{ |
| 164 |
{ |
| 165 |
Error: errors.New(""), |
| 166 |
ExpressionAccessor: &ValidationCondition{}, |
| 167 |
Elapsed: time.Millisecond, |
| 168 |
}, |
| 169 |
}, |
| 170 |
policyDecision: []PolicyDecision{ |
| 171 |
{ |
| 172 |
Action: ActionDeny, |
| 173 |
Elapsed: time.Millisecond, |
| 174 |
}, |
| 175 |
}, |
| 176 |
}, |
| 177 |
{ |
| 178 |
name: "test fail with failurepolicy fail", |
| 179 |
evaluations: []cel.EvaluationResult{ |
| 180 |
{ |
| 181 |
Error: errors.New(""), |
| 182 |
ExpressionAccessor: &ValidationCondition{}, |
| 183 |
Elapsed: time.Millisecond, |
| 184 |
}, |
| 185 |
}, |
| 186 |
policyDecision: []PolicyDecision{ |
| 187 |
{ |
| 188 |
Action: ActionDeny, |
| 189 |
Elapsed: time.Millisecond, |
| 190 |
}, |
| 191 |
}, |
| 192 |
failPolicy: &fail, |
| 193 |
}, |
| 194 |
{ |
| 195 |
name: "test fail with failurepolicy ignore with multiple validations", |
| 196 |
evaluations: []cel.EvaluationResult{ |
| 197 |
{ |
| 198 |
EvalResult: celtypes.True, |
| 199 |
ExpressionAccessor: &ValidationCondition{}, |
| 200 |
Elapsed: time.Millisecond, |
| 201 |
}, |
| 202 |
{ |
| 203 |
Error: errors.New(""), |
| 204 |
ExpressionAccessor: &ValidationCondition{}, |
| 205 |
Elapsed: time.Millisecond, |
| 206 |
}, |
| 207 |
}, |
| 208 |
policyDecision: []PolicyDecision{ |
| 209 |
{ |
| 210 |
Action: ActionAdmit, |
| 211 |
Elapsed: time.Millisecond, |
| 212 |
}, |
| 213 |
{ |
| 214 |
Action: ActionAdmit, |
| 215 |
Elapsed: time.Millisecond, |
| 216 |
}, |
| 217 |
}, |
| 218 |
failPolicy: &ignore, |
| 219 |
}, |
| 220 |
{ |
| 221 |
name: "test fail with failurepolicy nil with multiple validations", |
| 222 |
evaluations: []cel.EvaluationResult{ |
| 223 |
{ |
| 224 |
EvalResult: celtypes.True, |
| 225 |
ExpressionAccessor: &ValidationCondition{}, |
| 226 |
Elapsed: time.Millisecond, |
| 227 |
}, |
| 228 |
{ |
| 229 |
Error: errors.New(""), |
| 230 |
ExpressionAccessor: &ValidationCondition{}, |
| 231 |
Elapsed: time.Millisecond, |
| 232 |
}, |
| 233 |
}, |
| 234 |
policyDecision: []PolicyDecision{ |
| 235 |
{ |
| 236 |
Action: ActionAdmit, |
| 237 |
Elapsed: time.Millisecond, |
| 238 |
}, |
| 239 |
{ |
| 240 |
Action: ActionDeny, |
| 241 |
Elapsed: time.Millisecond, |
| 242 |
}, |
| 243 |
}, |
| 244 |
}, |
| 245 |
{ |
| 246 |
name: "test fail with failurepolicy fail with multiple validations", |
| 247 |
evaluations: []cel.EvaluationResult{ |
| 248 |
{ |
| 249 |
EvalResult: celtypes.True, |
| 250 |
ExpressionAccessor: &ValidationCondition{}, |
| 251 |
Elapsed: time.Millisecond, |
| 252 |
}, |
| 253 |
{ |
| 254 |
Error: errors.New(""), |
| 255 |
ExpressionAccessor: &ValidationCondition{}, |
| 256 |
Elapsed: time.Millisecond, |
| 257 |
}, |
| 258 |
}, |
| 259 |
policyDecision: []PolicyDecision{ |
| 260 |
{ |
| 261 |
Action: ActionAdmit, |
| 262 |
Elapsed: time.Millisecond, |
| 263 |
}, |
| 264 |
{ |
| 265 |
Action: ActionDeny, |
| 266 |
Elapsed: time.Millisecond, |
| 267 |
}, |
| 268 |
}, |
| 269 |
failPolicy: &fail, |
| 270 |
}, |
| 271 |
{ |
| 272 |
name: "test fail with failurepolicy ignore with multiple failed validations", |
| 273 |
evaluations: []cel.EvaluationResult{ |
| 274 |
{ |
| 275 |
Error: errors.New(""), |
| 276 |
ExpressionAccessor: &ValidationCondition{}, |
| 277 |
Elapsed: time.Millisecond, |
| 278 |
}, |
| 279 |
{ |
| 280 |
Error: errors.New(""), |
| 281 |
ExpressionAccessor: &ValidationCondition{}, |
| 282 |
Elapsed: time.Millisecond, |
| 283 |
}, |
| 284 |
}, |
| 285 |
policyDecision: []PolicyDecision{ |
| 286 |
{ |
| 287 |
Action: ActionAdmit, |
| 288 |
Elapsed: time.Millisecond, |
| 289 |
}, |
| 290 |
{ |
| 291 |
Action: ActionAdmit, |
| 292 |
Elapsed: time.Millisecond, |
| 293 |
}, |
| 294 |
}, |
| 295 |
failPolicy: &ignore, |
| 296 |
}, |
| 297 |
{ |
| 298 |
name: "test fail with failurepolicy nil with multiple failed validations", |
| 299 |
evaluations: []cel.EvaluationResult{ |
| 300 |
{ |
| 301 |
Error: errors.New(""), |
| 302 |
ExpressionAccessor: &ValidationCondition{}, |
| 303 |
Elapsed: time.Millisecond, |
| 304 |
}, |
| 305 |
{ |
| 306 |
Error: errors.New(""), |
| 307 |
ExpressionAccessor: &ValidationCondition{}, |
| 308 |
Elapsed: time.Millisecond, |
| 309 |
}, |
| 310 |
}, |
| 311 |
policyDecision: []PolicyDecision{ |
| 312 |
{ |
| 313 |
Action: ActionDeny, |
| 314 |
Elapsed: time.Millisecond, |
| 315 |
}, |
| 316 |
{ |
| 317 |
Action: ActionDeny, |
| 318 |
Elapsed: time.Millisecond, |
| 319 |
}, |
| 320 |
}, |
| 321 |
}, |
| 322 |
{ |
| 323 |
name: "test fail with failurepolicy fail with multiple failed validations", |
| 324 |
evaluations: []cel.EvaluationResult{ |
| 325 |
{ |
| 326 |
Error: errors.New(""), |
| 327 |
ExpressionAccessor: &ValidationCondition{}, |
| 328 |
Elapsed: time.Millisecond, |
| 329 |
}, |
| 330 |
{ |
| 331 |
Error: errors.New(""), |
| 332 |
ExpressionAccessor: &ValidationCondition{}, |
| 333 |
Elapsed: time.Millisecond, |
| 334 |
}, |
| 335 |
}, |
| 336 |
policyDecision: []PolicyDecision{ |
| 337 |
{ |
| 338 |
Action: ActionDeny, |
| 339 |
Elapsed: time.Millisecond, |
| 340 |
}, |
| 341 |
{ |
| 342 |
Action: ActionDeny, |
| 343 |
Elapsed: time.Millisecond, |
| 344 |
}, |
| 345 |
}, |
| 346 |
failPolicy: &fail, |
| 347 |
}, |
| 348 |
{ |
| 349 |
name: "test reason for fail no reason set", |
| 350 |
evaluations: []cel.EvaluationResult{ |
| 351 |
{ |
| 352 |
EvalResult: celtypes.False, |
| 353 |
ExpressionAccessor: &ValidationCondition{ |
| 354 |
Expression: "this.expression == unit.test", |
| 355 |
}, |
| 356 |
Elapsed: time.Millisecond, |
| 357 |
}, |
| 358 |
}, |
| 359 |
policyDecision: []PolicyDecision{ |
| 360 |
{ |
| 361 |
Action: ActionDeny, |
| 362 |
Reason: metav1.StatusReasonInvalid, |
| 363 |
Message: "failed expression: this.expression == unit.test", |
| 364 |
Elapsed: time.Millisecond, |
| 365 |
}, |
| 366 |
}, |
| 367 |
failPolicy: &fail, |
| 368 |
}, |
| 369 |
{ |
| 370 |
name: "test reason for fail reason set", |
| 371 |
evaluations: []cel.EvaluationResult{ |
| 372 |
{ |
| 373 |
EvalResult: celtypes.False, |
| 374 |
ExpressionAccessor: &ValidationCondition{ |
| 375 |
Reason: &forbiddenReason, |
| 376 |
Expression: "this.expression == unit.test", |
| 377 |
}, |
| 378 |
Elapsed: time.Millisecond, |
| 379 |
}, |
| 380 |
}, |
| 381 |
policyDecision: []PolicyDecision{ |
| 382 |
{ |
| 383 |
Action: ActionDeny, |
| 384 |
Reason: metav1.StatusReasonForbidden, |
| 385 |
Message: "failed expression: this.expression == unit.test", |
| 386 |
Elapsed: time.Millisecond, |
| 387 |
}, |
| 388 |
}, |
| 389 |
failPolicy: &fail, |
| 390 |
}, |
| 391 |
{ |
| 392 |
name: "test reason for failed validations multiple validations", |
| 393 |
evaluations: []cel.EvaluationResult{ |
| 394 |
{ |
| 395 |
EvalResult: celtypes.False, |
| 396 |
ExpressionAccessor: &ValidationCondition{ |
| 397 |
Reason: &forbiddenReason, |
| 398 |
Expression: "this.expression == unit.test", |
| 399 |
}, |
| 400 |
Elapsed: time.Millisecond, |
| 401 |
}, |
| 402 |
{ |
| 403 |
EvalResult: celtypes.False, |
| 404 |
ExpressionAccessor: &ValidationCondition{ |
| 405 |
Reason: &unauthorizedReason, |
| 406 |
Expression: "this.expression.2 == unit.test.2", |
| 407 |
}, |
| 408 |
Elapsed: time.Millisecond, |
| 409 |
}, |
| 410 |
}, |
| 411 |
policyDecision: []PolicyDecision{ |
| 412 |
{ |
| 413 |
Action: ActionDeny, |
| 414 |
Reason: metav1.StatusReasonForbidden, |
| 415 |
Message: "failed expression: this.expression == unit.test", |
| 416 |
Elapsed: time.Millisecond, |
| 417 |
}, |
| 418 |
{ |
| 419 |
Action: ActionDeny, |
| 420 |
Reason: metav1.StatusReasonUnauthorized, |
| 421 |
Message: "failed expression: this.expression.2 == unit.test.2", |
| 422 |
Elapsed: time.Millisecond, |
| 423 |
}, |
| 424 |
}, |
| 425 |
failPolicy: &fail, |
| 426 |
}, |
| 427 |
{ |
| 428 |
name: "test message for failed validations", |
| 429 |
evaluations: []cel.EvaluationResult{ |
| 430 |
{ |
| 431 |
EvalResult: celtypes.False, |
| 432 |
ExpressionAccessor: &ValidationCondition{ |
| 433 |
Reason: &forbiddenReason, |
| 434 |
Expression: "this.expression == unit.test", |
| 435 |
Message: "test", |
| 436 |
}, |
| 437 |
Elapsed: time.Millisecond, |
| 438 |
}, |
| 439 |
}, |
| 440 |
policyDecision: []PolicyDecision{ |
| 441 |
{ |
| 442 |
Action: ActionDeny, |
| 443 |
Reason: metav1.StatusReasonForbidden, |
| 444 |
Message: "test", |
| 445 |
Elapsed: time.Millisecond, |
| 446 |
}, |
| 447 |
}, |
| 448 |
failPolicy: &fail, |
| 449 |
}, |
| 450 |
{ |
| 451 |
name: "test message for failed validations multiple validations", |
| 452 |
evaluations: []cel.EvaluationResult{ |
| 453 |
{ |
| 454 |
EvalResult: celtypes.False, |
| 455 |
ExpressionAccessor: &ValidationCondition{ |
| 456 |
Reason: &forbiddenReason, |
| 457 |
Expression: "this.expression == unit.test", |
| 458 |
Message: "test1", |
| 459 |
}, |
| 460 |
Elapsed: time.Millisecond, |
| 461 |
}, |
| 462 |
{ |
| 463 |
EvalResult: celtypes.False, |
| 464 |
ExpressionAccessor: &ValidationCondition{ |
| 465 |
Reason: &forbiddenReason, |
| 466 |
Expression: "this.expression == unit.test", |
| 467 |
Message: "test2", |
| 468 |
}, |
| 469 |
Elapsed: time.Millisecond, |
| 470 |
}, |
| 471 |
}, |
| 472 |
policyDecision: []PolicyDecision{ |
| 473 |
{ |
| 474 |
Action: ActionDeny, |
| 475 |
Reason: metav1.StatusReasonForbidden, |
| 476 |
Message: "test1", |
| 477 |
Elapsed: time.Millisecond, |
| 478 |
}, |
| 479 |
{ |
| 480 |
Action: ActionDeny, |
| 481 |
Reason: metav1.StatusReasonForbidden, |
| 482 |
Message: "test2", |
| 483 |
Elapsed: time.Millisecond, |
| 484 |
}, |
| 485 |
}, |
| 486 |
failPolicy: &fail, |
| 487 |
}, |
| 488 |
{ |
| 489 |
name: "test filter error", |
| 490 |
evaluations: []cel.EvaluationResult{ |
| 491 |
{ |
| 492 |
EvalResult: celtypes.False, |
| 493 |
ExpressionAccessor: &ValidationCondition{ |
| 494 |
Reason: &forbiddenReason, |
| 495 |
Expression: "this.expression == unit.test", |
| 496 |
Message: "test1", |
| 497 |
}, |
| 498 |
Elapsed: time.Millisecond, |
| 499 |
}, |
| 500 |
}, |
| 501 |
policyDecision: []PolicyDecision{ |
| 502 |
{ |
| 503 |
Action: ActionDeny, |
| 504 |
Message: "test error", |
| 505 |
}, |
| 506 |
}, |
| 507 |
failPolicy: &fail, |
| 508 |
throwError: true, |
| 509 |
}, |
| 510 |
{ |
| 511 |
name: "test filter error multiple evaluations", |
| 512 |
evaluations: []cel.EvaluationResult{ |
| 513 |
{ |
| 514 |
EvalResult: celtypes.False, |
| 515 |
ExpressionAccessor: &ValidationCondition{ |
| 516 |
Reason: &forbiddenReason, |
| 517 |
Expression: "this.expression == unit.test", |
| 518 |
Message: "test1", |
| 519 |
}, |
| 520 |
Elapsed: time.Millisecond, |
| 521 |
}, |
| 522 |
{ |
| 523 |
EvalResult: celtypes.False, |
| 524 |
ExpressionAccessor: &ValidationCondition{ |
| 525 |
Reason: &forbiddenReason, |
| 526 |
Expression: "this.expression == unit.test", |
| 527 |
Message: "test2", |
| 528 |
}, |
| 529 |
Elapsed: time.Millisecond, |
| 530 |
}, |
| 531 |
}, |
| 532 |
policyDecision: []PolicyDecision{ |
| 533 |
{ |
| 534 |
Action: ActionDeny, |
| 535 |
Message: "test error", |
| 536 |
}, |
| 537 |
}, |
| 538 |
failPolicy: &fail, |
| 539 |
throwError: true, |
| 540 |
}, |
| 541 |
{ |
| 542 |
name: "test empty validations with non-empty audit annotations", |
| 543 |
auditEvaluations: []cel.EvaluationResult{ |
| 544 |
{ |
| 545 |
EvalResult: celtypes.String("string value"), |
| 546 |
ExpressionAccessor: &AuditAnnotationCondition{ |
| 547 |
ValueExpression: "'string value'", |
| 548 |
}, |
| 549 |
Elapsed: 2 * time.Millisecond, |
| 550 |
}, |
| 551 |
}, |
| 552 |
failPolicy: &fail, |
| 553 |
auditAnnotations: []PolicyAuditAnnotation{ |
| 554 |
{ |
| 555 |
Action: AuditAnnotationActionPublish, |
| 556 |
Value: "string value", |
| 557 |
Elapsed: 2 * time.Millisecond, |
| 558 |
}, |
| 559 |
}, |
| 560 |
}, |
| 561 |
{ |
| 562 |
name: "test non-empty validations with non-empty audit annotations", |
| 563 |
evaluations: []cel.EvaluationResult{ |
| 564 |
{ |
| 565 |
EvalResult: celtypes.True, |
| 566 |
ExpressionAccessor: &ValidationCondition{ |
| 567 |
Reason: &forbiddenReason, |
| 568 |
Expression: "this.expression == unit.test", |
| 569 |
Message: "test1", |
| 570 |
}, |
| 571 |
Elapsed: time.Millisecond, |
| 572 |
}, |
| 573 |
}, |
| 574 |
auditEvaluations: []cel.EvaluationResult{ |
| 575 |
{ |
| 576 |
EvalResult: celtypes.String("string value"), |
| 577 |
ExpressionAccessor: &AuditAnnotationCondition{ |
| 578 |
ValueExpression: "'string value'", |
| 579 |
}, |
| 580 |
Elapsed: 2 * time.Millisecond, |
| 581 |
}, |
| 582 |
}, |
| 583 |
policyDecision: []PolicyDecision{ |
| 584 |
{ |
| 585 |
Action: ActionAdmit, |
| 586 |
Elapsed: time.Millisecond, |
| 587 |
}, |
| 588 |
}, |
| 589 |
auditAnnotations: []PolicyAuditAnnotation{ |
| 590 |
{ |
| 591 |
Action: AuditAnnotationActionPublish, |
| 592 |
Value: "string value", |
| 593 |
Elapsed: 2 * time.Millisecond, |
| 594 |
}, |
| 595 |
}, |
| 596 |
failPolicy: &fail, |
| 597 |
}, |
| 598 |
{ |
| 599 |
name: "test audit annotations with null return", |
| 600 |
auditEvaluations: []cel.EvaluationResult{ |
| 601 |
{ |
| 602 |
EvalResult: celtypes.NullValue, |
| 603 |
ExpressionAccessor: &AuditAnnotationCondition{ |
| 604 |
ValueExpression: "null", |
| 605 |
}, |
| 606 |
Elapsed: 2 * time.Millisecond, |
| 607 |
}, |
| 608 |
{ |
| 609 |
EvalResult: celtypes.String("string value"), |
| 610 |
ExpressionAccessor: &AuditAnnotationCondition{ |
| 611 |
ValueExpression: "'string value'", |
| 612 |
}, |
| 613 |
Elapsed: 2 * time.Millisecond, |
| 614 |
}, |
| 615 |
}, |
| 616 |
auditAnnotations: []PolicyAuditAnnotation{ |
| 617 |
{ |
| 618 |
Action: AuditAnnotationActionExclude, |
| 619 |
Elapsed: 2 * time.Millisecond, |
| 620 |
}, |
| 621 |
{ |
| 622 |
Action: AuditAnnotationActionPublish, |
| 623 |
Value: "string value", |
| 624 |
Elapsed: 2 * time.Millisecond, |
| 625 |
}, |
| 626 |
}, |
| 627 |
failPolicy: &fail, |
| 628 |
}, |
| 629 |
{ |
| 630 |
name: "test audit annotations with failPolicy=fail", |
| 631 |
auditEvaluations: []cel.EvaluationResult{ |
| 632 |
{ |
| 633 |
Error: fmt.Errorf("valueExpression ''this is not valid CEL' resulted in error: <nil>"), |
| 634 |
ExpressionAccessor: &AuditAnnotationCondition{ |
| 635 |
ValueExpression: "'this is not valid CEL", |
| 636 |
}, |
| 637 |
Elapsed: 2 * time.Millisecond, |
| 638 |
}, |
| 639 |
}, |
| 640 |
auditAnnotations: []PolicyAuditAnnotation{ |
| 641 |
{ |
| 642 |
Action: AuditAnnotationActionError, |
| 643 |
Error: "valueExpression ''this is not valid CEL' resulted in error: <nil>", |
| 644 |
Elapsed: 2 * time.Millisecond, |
| 645 |
}, |
| 646 |
}, |
| 647 |
failPolicy: &fail, |
| 648 |
}, |
| 649 |
{ |
| 650 |
name: "test audit annotations with failPolicy=ignore", |
| 651 |
auditEvaluations: []cel.EvaluationResult{ |
| 652 |
{ |
| 653 |
Error: fmt.Errorf("valueExpression ''this is not valid CEL' resulted in error: <nil>"), |
| 654 |
ExpressionAccessor: &AuditAnnotationCondition{ |
| 655 |
ValueExpression: "'this is not valid CEL", |
| 656 |
}, |
| 657 |
Elapsed: 2 * time.Millisecond, |
| 658 |
}, |
| 659 |
}, |
| 660 |
auditAnnotations: []PolicyAuditAnnotation{ |
| 661 |
{ |
| 662 |
Action: AuditAnnotationActionExclude, // TODO: is this right? |
| 663 |
Error: "valueExpression ''this is not valid CEL' resulted in error: <nil>", |
| 664 |
Elapsed: 2 * time.Millisecond, |
| 665 |
}, |
| 666 |
}, |
| 667 |
failPolicy: &ignore, |
| 668 |
}, |
| 669 |
{ |
| 670 |
name: "messageExpression successful, empty message", |
| 671 |
evaluations: []cel.EvaluationResult{ |
| 672 |
{ |
| 673 |
EvalResult: celtypes.False, |
| 674 |
ExpressionAccessor: &ValidationCondition{ |
| 675 |
Reason: &forbiddenReason, |
| 676 |
Expression: "this.expression == unit.test", |
| 677 |
}, |
| 678 |
Elapsed: time.Millisecond, |
| 679 |
}, |
| 680 |
}, |
| 681 |
messageEvaluations: []cel.EvaluationResult{ |
| 682 |
{ |
| 683 |
EvalResult: celtypes.String("evaluated message"), |
| 684 |
Elapsed: time.Millisecond, |
| 685 |
}, |
| 686 |
}, |
| 687 |
policyDecision: []PolicyDecision{ |
| 688 |
{ |
| 689 |
Action: ActionDeny, |
| 690 |
Message: "evaluated message", |
| 691 |
Reason: forbiddenReason, |
| 692 |
Elapsed: time.Millisecond, |
| 693 |
}, |
| 694 |
}, |
| 695 |
}, |
| 696 |
{ |
| 697 |
name: "messageExpression for multiple validations", |
| 698 |
evaluations: []cel.EvaluationResult{ |
| 699 |
{ |
| 700 |
EvalResult: celtypes.False, |
| 701 |
ExpressionAccessor: &ValidationCondition{ |
| 702 |
Reason: &forbiddenReason, |
| 703 |
Expression: "this.expression == unit.test", |
| 704 |
Message: "I am not overwritten", |
| 705 |
}, |
| 706 |
Elapsed: time.Millisecond, |
| 707 |
}, |
| 708 |
{ |
| 709 |
EvalResult: celtypes.False, |
| 710 |
ExpressionAccessor: &ValidationCondition{ |
| 711 |
Reason: &forbiddenReason, |
| 712 |
Expression: "this.expression == unit.test", |
| 713 |
Message: "I am overwritten", |
| 714 |
}, |
| 715 |
Elapsed: time.Millisecond, |
| 716 |
}, |
| 717 |
}, |
| 718 |
messageEvaluations: []cel.EvaluationResult{ |
| 719 |
{}, |
| 720 |
{ |
| 721 |
EvalResult: celtypes.String("evaluated message"), |
| 722 |
Elapsed: time.Millisecond, |
| 723 |
}, |
| 724 |
}, |
| 725 |
policyDecision: []PolicyDecision{ |
| 726 |
{ |
| 727 |
Action: ActionDeny, |
| 728 |
Message: "I am not overwritten", |
| 729 |
Reason: forbiddenReason, |
| 730 |
Elapsed: time.Millisecond, |
| 731 |
}, |
| 732 |
{ |
| 733 |
Action: ActionDeny, |
| 734 |
Message: "evaluated message", |
| 735 |
Reason: forbiddenReason, |
| 736 |
Elapsed: time.Millisecond, |
| 737 |
}, |
| 738 |
}, |
| 739 |
}, |
| 740 |
{ |
| 741 |
name: "messageExpression successful, overwritten message", |
| 742 |
evaluations: []cel.EvaluationResult{ |
| 743 |
{ |
| 744 |
EvalResult: celtypes.False, |
| 745 |
ExpressionAccessor: &ValidationCondition{ |
| 746 |
Reason: &forbiddenReason, |
| 747 |
Expression: "this.expression == unit.test", |
| 748 |
Message: "I am overwritten", |
| 749 |
}, |
| 750 |
Elapsed: time.Millisecond, |
| 751 |
}, |
| 752 |
}, |
| 753 |
messageEvaluations: []cel.EvaluationResult{ |
| 754 |
{ |
| 755 |
EvalResult: celtypes.String("evaluated message"), |
| 756 |
Elapsed: time.Millisecond, |
| 757 |
}, |
| 758 |
}, |
| 759 |
policyDecision: []PolicyDecision{ |
| 760 |
{ |
| 761 |
Action: ActionDeny, |
| 762 |
Message: "evaluated message", |
| 763 |
Reason: forbiddenReason, |
| 764 |
Elapsed: time.Millisecond, |
| 765 |
}, |
| 766 |
}, |
| 767 |
}, |
| 768 |
{ |
| 769 |
name: "messageExpression user failure", |
| 770 |
evaluations: []cel.EvaluationResult{ |
| 771 |
{ |
| 772 |
EvalResult: celtypes.False, |
| 773 |
ExpressionAccessor: &ValidationCondition{ |
| 774 |
Reason: &forbiddenReason, |
| 775 |
Expression: "this.expression == unit.test", |
| 776 |
Message: "test1", |
| 777 |
}, |
| 778 |
Elapsed: time.Millisecond, |
| 779 |
}, |
| 780 |
}, |
| 781 |
messageEvaluations: []cel.EvaluationResult{ |
| 782 |
{ |
| 783 |
Error: &apiservercel.Error{Type: apiservercel.ErrorTypeInvalid}, |
| 784 |
Elapsed: time.Millisecond, |
| 785 |
}, |
| 786 |
}, |
| 787 |
policyDecision: []PolicyDecision{ |
| 788 |
{ |
| 789 |
Action: ActionDeny, |
| 790 |
Message: "test1", // original message used |
| 791 |
Reason: forbiddenReason, |
| 792 |
Elapsed: time.Millisecond, |
| 793 |
}, |
| 794 |
}, |
| 795 |
}, |
| 796 |
{ |
| 797 |
name: "messageExpression eval to empty", |
| 798 |
evaluations: []cel.EvaluationResult{ |
| 799 |
{ |
| 800 |
EvalResult: celtypes.False, |
| 801 |
ExpressionAccessor: &ValidationCondition{ |
| 802 |
Reason: &forbiddenReason, |
| 803 |
Expression: "this.expression == unit.test", |
| 804 |
Message: "test1", |
| 805 |
}, |
| 806 |
Elapsed: time.Millisecond, |
| 807 |
}, |
| 808 |
}, |
| 809 |
messageEvaluations: []cel.EvaluationResult{ |
| 810 |
{ |
| 811 |
EvalResult: celtypes.String(" "), |
| 812 |
Elapsed: time.Millisecond, |
| 813 |
}, |
| 814 |
}, |
| 815 |
policyDecision: []PolicyDecision{ |
| 816 |
{ |
| 817 |
Action: ActionDeny, |
| 818 |
Message: "test1", |
| 819 |
Reason: forbiddenReason, |
| 820 |
Elapsed: time.Millisecond, |
| 821 |
}, |
| 822 |
}, |
| 823 |
}, |
| 824 |
{ |
| 825 |
name: "messageExpression eval to multi-line", |
| 826 |
evaluations: []cel.EvaluationResult{ |
| 827 |
{ |
| 828 |
EvalResult: celtypes.False, |
| 829 |
ExpressionAccessor: &ValidationCondition{ |
| 830 |
Reason: &forbiddenReason, |
| 831 |
Expression: "this.expression == unit.test", |
| 832 |
Message: "test1", |
| 833 |
}, |
| 834 |
Elapsed: time.Millisecond, |
| 835 |
}, |
| 836 |
}, |
| 837 |
messageEvaluations: []cel.EvaluationResult{ |
| 838 |
{ |
| 839 |
EvalResult: celtypes.String("hello\nthere"), |
| 840 |
Elapsed: time.Millisecond, |
| 841 |
}, |
| 842 |
}, |
| 843 |
policyDecision: []PolicyDecision{ |
| 844 |
{ |
| 845 |
Action: ActionDeny, |
| 846 |
Message: "test1", |
| 847 |
Reason: forbiddenReason, |
| 848 |
Elapsed: time.Millisecond, |
| 849 |
}, |
| 850 |
}, |
| 851 |
}, |
| 852 |
{ |
| 853 |
name: "messageExpression eval result too long", |
| 854 |
evaluations: []cel.EvaluationResult{ |
| 855 |
{ |
| 856 |
EvalResult: celtypes.False, |
| 857 |
ExpressionAccessor: &ValidationCondition{ |
| 858 |
Reason: &forbiddenReason, |
| 859 |
Expression: "this.expression == unit.test", |
| 860 |
Message: "test1", |
| 861 |
}, |
| 862 |
Elapsed: time.Millisecond, |
| 863 |
}, |
| 864 |
}, |
| 865 |
messageEvaluations: []cel.EvaluationResult{ |
| 866 |
{ |
| 867 |
EvalResult: celtypes.String(strings.Repeat("x", 5*1024+1)), |
| 868 |
}, |
| 869 |
}, |
| 870 |
policyDecision: []PolicyDecision{ |
| 871 |
{ |
| 872 |
Action: ActionDeny, |
| 873 |
Message: "test1", |
| 874 |
Reason: forbiddenReason, |
| 875 |
Elapsed: time.Millisecond, |
| 876 |
}, |
| 877 |
}, |
| 878 |
}, |
| 879 |
{ |
| 880 |
name: "messageExpression eval to null", |
| 881 |
evaluations: []cel.EvaluationResult{ |
| 882 |
{ |
| 883 |
EvalResult: celtypes.False, |
| 884 |
ExpressionAccessor: &ValidationCondition{ |
| 885 |
Reason: &forbiddenReason, |
| 886 |
Expression: "this.expression == unit.test", |
| 887 |
Message: "test1", |
| 888 |
}, |
| 889 |
Elapsed: time.Millisecond, |
| 890 |
}, |
| 891 |
}, |
| 892 |
messageEvaluations: []cel.EvaluationResult{ |
| 893 |
{ |
| 894 |
EvalResult: celtypes.NullValue, |
| 895 |
Elapsed: time.Millisecond, |
| 896 |
}, |
| 897 |
}, |
| 898 |
policyDecision: []PolicyDecision{ |
| 899 |
{ |
| 900 |
Action: ActionDeny, |
| 901 |
Message: "test1", |
| 902 |
Reason: forbiddenReason, |
| 903 |
Elapsed: time.Millisecond, |
| 904 |
}, |
| 905 |
}, |
| 906 |
}, |
| 907 |
{ |
| 908 |
name: "messageExpression out of budget after successful eval of expression", |
| 909 |
evaluations: []cel.EvaluationResult{ |
| 910 |
{ |
| 911 |
EvalResult: celtypes.False, |
| 912 |
ExpressionAccessor: &ValidationCondition{ |
| 913 |
Reason: &forbiddenReason, |
| 914 |
Expression: "this.expression == unit.test", |
| 915 |
Message: "test1", |
| 916 |
}, |
| 917 |
Elapsed: time.Millisecond, |
| 918 |
}, |
| 919 |
}, |
| 920 |
messageEvaluations: []cel.EvaluationResult{ |
| 921 |
{ |
| 922 |
EvalResult: celtypes.StringType, // does not matter |
| 923 |
}, |
| 924 |
}, |
| 925 |
policyDecision: []PolicyDecision{ |
| 926 |
{ |
| 927 |
Action: ActionDeny, |
| 928 |
Message: "running out of cost budget", |
| 929 |
Elapsed: time.Millisecond, |
| 930 |
}, |
| 931 |
}, |
| 932 |
costBudget: 1, // shared between expression and messageExpression, needs 1 + 1 = 2 in total |
| 933 |
}, |
| 934 |
{ |
| 935 |
name: "no match surpresses failure", |
| 936 |
matcher: &fakeCELMatcher{matches: false}, |
| 937 |
evaluations: []cel.EvaluationResult{ |
| 938 |
{ |
| 939 |
Error: errors.New("expected"), |
| 940 |
ExpressionAccessor: &ValidationCondition{}, |
| 941 |
Elapsed: time.Millisecond, |
| 942 |
}, |
| 943 |
}, |
| 944 |
policyDecision: []PolicyDecision{}, |
| 945 |
failPolicy: &fail, |
| 946 |
}, |
| 947 |
{ |
| 948 |
name: "match error => presumed match", |
| 949 |
matcher: &fakeCELMatcher{matches: true, error: fmt.Errorf("test error")}, |
| 950 |
evaluations: []cel.EvaluationResult{ |
| 951 |
{ |
| 952 |
Error: errors.New("expected"), |
| 953 |
ExpressionAccessor: &ValidationCondition{}, |
| 954 |
Elapsed: time.Millisecond, |
| 955 |
}, |
| 956 |
}, |
| 957 |
policyDecision: []PolicyDecision{ |
| 958 |
{ |
| 959 |
Action: ActionDeny, |
| 960 |
}, |
| 961 |
}, |
| 962 |
failPolicy: &fail, |
| 963 |
}, |
| 964 |
} |
| 965 |
for _, tc := range cases { |
| 966 |
t.Run(tc.name, func(t *testing.T) { |
| 967 |
var matcher matchconditions.Matcher |
| 968 |
if tc.matcher == nil { |
| 969 |
matcher = &fakeCELMatcher{matches: true} |
| 970 |
} else { |
| 971 |
matcher = tc.matcher |
| 972 |
} |
| 973 |
v := validator{ |
| 974 |
failPolicy: tc.failPolicy, |
| 975 |
celMatcher: matcher, |
| 976 |
validationFilter: &fakeCelFilter{ |
| 977 |
evaluations: tc.evaluations, |
| 978 |
throwError: tc.throwError, |
| 979 |
}, |
| 980 |
messageFilter: &fakeCelFilter{ |
| 981 |
evaluations: tc.messageEvaluations, |
| 982 |
throwError: tc.throwError, |
| 983 |
}, |
| 984 |
auditAnnotationFilter: &fakeCelFilter{ |
| 985 |
evaluations: tc.auditEvaluations, |
| 986 |
throwError: tc.throwError, |
| 987 |
}, |
| 988 |
} |
| 989 |
ctx := context.TODO() |
| 990 |
var budget int64 = celconfig.RuntimeCELCostBudget |
| 991 |
if tc.costBudget != 0 { |
| 992 |
budget = tc.costBudget |
| 993 |
} |
| 994 |
validateResult := v.Validate(ctx, fakeVersionedAttr.GetResource(), fakeVersionedAttr, nil, nil, budget, nil) |
| 995 |
|
| 996 |
require.Equal(t, len(validateResult.Decisions), len(tc.policyDecision)) |
| 997 |
|
| 998 |
for i, policyDecision := range tc.policyDecision { |
| 999 |
if policyDecision.Action != validateResult.Decisions[i].Action { |
| 1000 |
t.Errorf("Expected policy decision kind '%v' but got '%v'", policyDecision.Action, validateResult.Decisions[i].Action) |
| 1001 |
} |
| 1002 |
if !strings.Contains(validateResult.Decisions[i].Message, policyDecision.Message) { |
| 1003 |
t.Errorf("Expected policy decision message contains '%v' but got '%v'", policyDecision.Message, validateResult.Decisions[i].Message) |
| 1004 |
} |
| 1005 |
if policyDecision.Reason != validateResult.Decisions[i].Reason { |
| 1006 |
t.Errorf("Expected policy decision reason '%v' but got '%v'", policyDecision.Reason, validateResult.Decisions[i].Reason) |
| 1007 |
} |
| 1008 |
if policyDecision.Elapsed != validateResult.Decisions[i].Elapsed { |
| 1009 |
t.Errorf("Expected policy decision elapsed time '%v', but got '%v'", policyDecision.Elapsed, validateResult.Decisions[i].Elapsed) |
| 1010 |
} |
| 1011 |
} |
| 1012 |
require.Equal(t, len(tc.auditEvaluations), len(validateResult.AuditAnnotations)) |
| 1013 |
|
| 1014 |
for i, auditAnnotation := range tc.auditAnnotations { |
| 1015 |
actual := validateResult.AuditAnnotations[i] |
| 1016 |
if auditAnnotation.Action != actual.Action { |
| 1017 |
t.Errorf("Expected policy audit annotation action '%v' but got '%v'", auditAnnotation.Action, actual.Action) |
| 1018 |
} |
| 1019 |
if auditAnnotation.Error != actual.Error { |
| 1020 |
t.Errorf("Expected audit annotation error '%v' but got '%v'", auditAnnotation.Error, actual.Error) |
| 1021 |
} |
| 1022 |
if auditAnnotation.Value != actual.Value { |
| 1023 |
t.Errorf("Expected policy audit annotation value '%v' but got '%v'", auditAnnotation.Value, actual.Value) |
| 1024 |
} |
| 1025 |
if auditAnnotation.Elapsed != actual.Elapsed { |
| 1026 |
t.Errorf("Expected policy audit annotation elapsed time '%v', but got '%v'", auditAnnotation.Elapsed, actual.Elapsed) |
| 1027 |
} |
| 1028 |
} |
| 1029 |
}) |
| 1030 |
} |
| 1031 |
} |
| 1032 |
|
| 1033 |
func TestContextCanceled(t *testing.T) { |
| 1034 |
fail := v1.Fail |
| 1035 |
|
| 1036 |
fakeAttr := admission.NewAttributesRecord(nil, nil, schema.GroupVersionKind{}, "default", "foo", schema.GroupVersionResource{}, "", admission.Create, nil, false, nil) |
| 1037 |
fakeVersionedAttr, _ := admission.NewVersionedAttributes(fakeAttr, schema.GroupVersionKind{}, nil) |
| 1038 |
fc := cel.NewConditionCompiler(environment.MustBaseEnvSet(environment.DefaultCompatibilityVersion(), true)) |
| 1039 |
f := fc.CompileCondition([]cel.ExpressionAccessor{&ValidationCondition{Expression: "[1,2,3,4,5,6,7,8,9,10].map(x, [1,2,3,4,5,6,7,8,9,10].map(y, x*y)) == []"}}, cel.OptionalVariableDeclarations{HasParams: false, HasAuthorizer: false}, environment.StoredExpressions) |
| 1040 |
v := validator{ |
| 1041 |
failPolicy: &fail, |
| 1042 |
celMatcher: &fakeCELMatcher{matches: true}, |
| 1043 |
validationFilter: f, |
| 1044 |
messageFilter: f, |
| 1045 |
auditAnnotationFilter: &fakeCelFilter{ |
| 1046 |
evaluations: nil, |
| 1047 |
throwError: false, |
| 1048 |
}, |
| 1049 |
} |
| 1050 |
ctx, cancel := context.WithCancel(context.TODO()) |
| 1051 |
cancel() |
| 1052 |
validationResult := v.Validate(ctx, fakeVersionedAttr.GetResource(), fakeVersionedAttr, nil, nil, celconfig.RuntimeCELCostBudget, nil) |
| 1053 |
if len(validationResult.Decisions) != 1 || !strings.Contains(validationResult.Decisions[0].Message, "operation interrupted") { |
| 1054 |
t.Errorf("Expected 'operation interrupted' but got %v", validationResult.Decisions) |
| 1055 |
} |
| 1056 |
} |