|
1 | 1 | package webhook |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "strings" |
5 | 6 | "testing" |
6 | 7 |
|
7 | 8 | "github.com/google/go-cmp/cmp" |
8 | 9 | "github.com/google/go-cmp/cmp/cmpopts" |
9 | 10 | "github.com/stretchr/testify/assert" |
| 11 | + admv1 "k8s.io/api/admissionregistration/v1" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + "k8s.io/apimachinery/pkg/runtime" |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 15 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
10 | 16 | "sigs.k8s.io/controller-runtime/pkg/manager" |
11 | 17 |
|
12 | 18 | "github.com/kubefleet-dev/kubefleet/cmd/hubagent/options" |
13 | 19 | "github.com/kubefleet-dev/kubefleet/pkg/utils" |
| 20 | + testmanager "github.com/kubefleet-dev/kubefleet/test/utils/manager" |
14 | 21 | ) |
15 | 22 |
|
16 | | -const testWebhookServiceName = "test-webhook" |
17 | | - |
18 | 23 | func TestBuildFleetMutatingWebhooks(t *testing.T) { |
19 | 24 | url := options.WebhookClientConnectionType("url") |
20 | 25 | testCases := map[string]struct { |
@@ -340,3 +345,250 @@ func TestCreateClientConfig(t *testing.T) { |
340 | 345 | }) |
341 | 346 | } |
342 | 347 | } |
| 348 | + |
| 349 | +func TestCheckCAInjection(t *testing.T) { |
| 350 | + testCases := map[string]struct { |
| 351 | + config Config |
| 352 | + existingObjects []client.Object |
| 353 | + expectError bool |
| 354 | + expectedErrorMsg string |
| 355 | + }{ |
| 356 | + "useCertManager is false - returns nil without checking": { |
| 357 | + config: Config{ |
| 358 | + useCertManager: false, |
| 359 | + enableGuardRail: false, |
| 360 | + }, |
| 361 | + expectError: false, |
| 362 | + }, |
| 363 | + "useCertManager is true, all CA bundles present": { |
| 364 | + config: Config{ |
| 365 | + useCertManager: true, |
| 366 | + enableGuardRail: false, |
| 367 | + }, |
| 368 | + existingObjects: []client.Object{ |
| 369 | + &admv1.MutatingWebhookConfiguration{ |
| 370 | + ObjectMeta: metav1.ObjectMeta{Name: fleetMutatingWebhookCfgName}, |
| 371 | + Webhooks: []admv1.MutatingWebhook{ |
| 372 | + { |
| 373 | + Name: "test-mutating-webhook", |
| 374 | + ClientConfig: admv1.WebhookClientConfig{ |
| 375 | + CABundle: []byte("fake-ca-bundle"), |
| 376 | + }, |
| 377 | + }, |
| 378 | + }, |
| 379 | + }, |
| 380 | + &admv1.ValidatingWebhookConfiguration{ |
| 381 | + ObjectMeta: metav1.ObjectMeta{Name: fleetValidatingWebhookCfgName}, |
| 382 | + Webhooks: []admv1.ValidatingWebhook{ |
| 383 | + { |
| 384 | + Name: "test-validating-webhook-1", |
| 385 | + ClientConfig: admv1.WebhookClientConfig{ |
| 386 | + CABundle: []byte("fake-ca-bundle"), |
| 387 | + }, |
| 388 | + }, |
| 389 | + { |
| 390 | + Name: "test-validating-webhook-2", |
| 391 | + ClientConfig: admv1.WebhookClientConfig{ |
| 392 | + CABundle: []byte("fake-ca-bundle"), |
| 393 | + }, |
| 394 | + }, |
| 395 | + }, |
| 396 | + }, |
| 397 | + }, |
| 398 | + expectError: false, |
| 399 | + }, |
| 400 | + "useCertManager is true, mutating webhook missing CA bundle": { |
| 401 | + config: Config{ |
| 402 | + useCertManager: true, |
| 403 | + enableGuardRail: false, |
| 404 | + }, |
| 405 | + existingObjects: []client.Object{ |
| 406 | + &admv1.MutatingWebhookConfiguration{ |
| 407 | + ObjectMeta: metav1.ObjectMeta{Name: fleetMutatingWebhookCfgName}, |
| 408 | + Webhooks: []admv1.MutatingWebhook{ |
| 409 | + { |
| 410 | + Name: "test-mutating-webhook", |
| 411 | + ClientConfig: admv1.WebhookClientConfig{ |
| 412 | + CABundle: nil, // Missing CA bundle |
| 413 | + }, |
| 414 | + }, |
| 415 | + }, |
| 416 | + }, |
| 417 | + &admv1.ValidatingWebhookConfiguration{ |
| 418 | + ObjectMeta: metav1.ObjectMeta{Name: fleetValidatingWebhookCfgName}, |
| 419 | + Webhooks: []admv1.ValidatingWebhook{ |
| 420 | + { |
| 421 | + Name: "test-validating-webhook", |
| 422 | + ClientConfig: admv1.WebhookClientConfig{ |
| 423 | + CABundle: []byte("fake-ca-bundle"), |
| 424 | + }, |
| 425 | + }, |
| 426 | + }, |
| 427 | + }, |
| 428 | + }, |
| 429 | + expectError: true, |
| 430 | + expectedErrorMsg: "test-mutating-webhook is missing CA bundle", |
| 431 | + }, |
| 432 | + "useCertManager is true, validating webhook missing CA bundle": { |
| 433 | + config: Config{ |
| 434 | + useCertManager: true, |
| 435 | + enableGuardRail: false, |
| 436 | + }, |
| 437 | + existingObjects: []client.Object{ |
| 438 | + &admv1.MutatingWebhookConfiguration{ |
| 439 | + ObjectMeta: metav1.ObjectMeta{Name: fleetMutatingWebhookCfgName}, |
| 440 | + Webhooks: []admv1.MutatingWebhook{ |
| 441 | + { |
| 442 | + Name: "test-mutating-webhook", |
| 443 | + ClientConfig: admv1.WebhookClientConfig{ |
| 444 | + CABundle: []byte("fake-ca-bundle"), |
| 445 | + }, |
| 446 | + }, |
| 447 | + }, |
| 448 | + }, |
| 449 | + &admv1.ValidatingWebhookConfiguration{ |
| 450 | + ObjectMeta: metav1.ObjectMeta{Name: fleetValidatingWebhookCfgName}, |
| 451 | + Webhooks: []admv1.ValidatingWebhook{ |
| 452 | + { |
| 453 | + Name: "test-validating-webhook-1", |
| 454 | + ClientConfig: admv1.WebhookClientConfig{ |
| 455 | + CABundle: []byte("fake-ca-bundle"), |
| 456 | + }, |
| 457 | + }, |
| 458 | + { |
| 459 | + Name: "test-validating-webhook-2", |
| 460 | + ClientConfig: admv1.WebhookClientConfig{ |
| 461 | + CABundle: []byte{}, // Empty CA bundle |
| 462 | + }, |
| 463 | + }, |
| 464 | + }, |
| 465 | + }, |
| 466 | + }, |
| 467 | + expectError: true, |
| 468 | + expectedErrorMsg: "test-validating-webhook-2 is missing CA bundle", |
| 469 | + }, |
| 470 | + "useCertManager is true with guard rail, all CA bundles present": { |
| 471 | + config: Config{ |
| 472 | + useCertManager: true, |
| 473 | + enableGuardRail: true, |
| 474 | + }, |
| 475 | + existingObjects: []client.Object{ |
| 476 | + &admv1.MutatingWebhookConfiguration{ |
| 477 | + ObjectMeta: metav1.ObjectMeta{Name: fleetMutatingWebhookCfgName}, |
| 478 | + Webhooks: []admv1.MutatingWebhook{ |
| 479 | + { |
| 480 | + Name: "test-mutating-webhook", |
| 481 | + ClientConfig: admv1.WebhookClientConfig{ |
| 482 | + CABundle: []byte("fake-ca-bundle"), |
| 483 | + }, |
| 484 | + }, |
| 485 | + }, |
| 486 | + }, |
| 487 | + &admv1.ValidatingWebhookConfiguration{ |
| 488 | + ObjectMeta: metav1.ObjectMeta{Name: fleetValidatingWebhookCfgName}, |
| 489 | + Webhooks: []admv1.ValidatingWebhook{ |
| 490 | + { |
| 491 | + Name: "test-validating-webhook", |
| 492 | + ClientConfig: admv1.WebhookClientConfig{ |
| 493 | + CABundle: []byte("fake-ca-bundle"), |
| 494 | + }, |
| 495 | + }, |
| 496 | + }, |
| 497 | + }, |
| 498 | + &admv1.ValidatingWebhookConfiguration{ |
| 499 | + ObjectMeta: metav1.ObjectMeta{Name: fleetGuardRailWebhookCfgName}, |
| 500 | + Webhooks: []admv1.ValidatingWebhook{ |
| 501 | + { |
| 502 | + Name: "test-guard-rail-webhook", |
| 503 | + ClientConfig: admv1.WebhookClientConfig{ |
| 504 | + CABundle: []byte("fake-ca-bundle"), |
| 505 | + }, |
| 506 | + }, |
| 507 | + }, |
| 508 | + }, |
| 509 | + }, |
| 510 | + expectError: false, |
| 511 | + }, |
| 512 | + "useCertManager is true with guard rail, guard rail webhook missing CA bundle": { |
| 513 | + config: Config{ |
| 514 | + useCertManager: true, |
| 515 | + enableGuardRail: true, |
| 516 | + }, |
| 517 | + existingObjects: []client.Object{ |
| 518 | + &admv1.MutatingWebhookConfiguration{ |
| 519 | + ObjectMeta: metav1.ObjectMeta{Name: fleetMutatingWebhookCfgName}, |
| 520 | + Webhooks: []admv1.MutatingWebhook{ |
| 521 | + { |
| 522 | + Name: "test-mutating-webhook", |
| 523 | + ClientConfig: admv1.WebhookClientConfig{ |
| 524 | + CABundle: []byte("fake-ca-bundle"), |
| 525 | + }, |
| 526 | + }, |
| 527 | + }, |
| 528 | + }, |
| 529 | + &admv1.ValidatingWebhookConfiguration{ |
| 530 | + ObjectMeta: metav1.ObjectMeta{Name: fleetValidatingWebhookCfgName}, |
| 531 | + Webhooks: []admv1.ValidatingWebhook{ |
| 532 | + { |
| 533 | + Name: "test-validating-webhook", |
| 534 | + ClientConfig: admv1.WebhookClientConfig{ |
| 535 | + CABundle: []byte("fake-ca-bundle"), |
| 536 | + }, |
| 537 | + }, |
| 538 | + }, |
| 539 | + }, |
| 540 | + &admv1.ValidatingWebhookConfiguration{ |
| 541 | + ObjectMeta: metav1.ObjectMeta{Name: fleetGuardRailWebhookCfgName}, |
| 542 | + Webhooks: []admv1.ValidatingWebhook{ |
| 543 | + { |
| 544 | + Name: "test-guard-rail-webhook", |
| 545 | + ClientConfig: admv1.WebhookClientConfig{ |
| 546 | + CABundle: nil, // Missing CA bundle |
| 547 | + }, |
| 548 | + }, |
| 549 | + }, |
| 550 | + }, |
| 551 | + }, |
| 552 | + expectError: true, |
| 553 | + expectedErrorMsg: "test-guard-rail-webhook is missing CA bundle", |
| 554 | + }, |
| 555 | + "useCertManager is true, webhook configuration not found": { |
| 556 | + config: Config{ |
| 557 | + useCertManager: true, |
| 558 | + enableGuardRail: false, |
| 559 | + }, |
| 560 | + existingObjects: []client.Object{}, |
| 561 | + expectError: true, |
| 562 | + expectedErrorMsg: "failed to get MutatingWebhookConfiguration", |
| 563 | + }, |
| 564 | + } |
| 565 | + |
| 566 | + for name, tc := range testCases { |
| 567 | + t.Run(name, func(t *testing.T) { |
| 568 | + // Create a fake client with a proper scheme |
| 569 | + scheme := runtime.NewScheme() |
| 570 | + _ = admv1.AddToScheme(scheme) |
| 571 | + fakeClient := fake.NewClientBuilder(). |
| 572 | + WithScheme(scheme). |
| 573 | + WithObjects(tc.existingObjects...). |
| 574 | + Build() |
| 575 | + |
| 576 | + // Create a fake manager that returns our fake client |
| 577 | + tc.config.mgr = &testmanager.FakeManager{Client: fakeClient} |
| 578 | + |
| 579 | + err := tc.config.CheckCAInjection(context.Background()) |
| 580 | + |
| 581 | + if tc.expectError { |
| 582 | + if err == nil { |
| 583 | + t.Errorf("Expected error but got nil") |
| 584 | + } else if !strings.Contains(err.Error(), tc.expectedErrorMsg) { |
| 585 | + t.Errorf("Expected error message to contain %q, but got: %v", tc.expectedErrorMsg, err) |
| 586 | + } |
| 587 | + } else { |
| 588 | + if err != nil { |
| 589 | + t.Errorf("Expected no error but got: %v", err) |
| 590 | + } |
| 591 | + } |
| 592 | + }) |
| 593 | + } |
| 594 | +} |
0 commit comments