|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package e2e |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "encoding/base64" |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + "slices" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + . "github.com/onsi/gomega" |
| 15 | + corev1 "k8s.io/api/core/v1" |
| 16 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 17 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 19 | + |
| 20 | + bootcv1alpha1 "github.com/bootc-dev/bootc-operator/api/v1alpha1" |
| 21 | + "github.com/bootc-dev/bootc-operator/test/e2e/e2eutil" |
| 22 | + testutil "github.com/bootc-dev/bootc-operator/test/util" |
| 23 | +) |
| 24 | + |
| 25 | +// TestPullSecretNamespaces verifies that pools can only use pull secrets |
| 26 | +// from the namespaces the operator is configured to read from. The |
| 27 | +// operator must already allow E2E_PULL_SECRET_NAMESPACE (make deploy-bink |
| 28 | +// does). A pool referencing a secret in a namespace the test creates must |
| 29 | +// be Degraded without claiming its node; switched to a secret in the |
| 30 | +// allowed namespace, its node must update through the authenticated |
| 31 | +// registry. |
| 32 | +func TestPullSecretNamespaces(t *testing.T) { |
| 33 | + e2eutil.Providers(t, "bink", "eks") |
| 34 | + t.Parallel() |
| 35 | + g := NewWithT(t) |
| 36 | + g.SetDefaultEventuallyTimeout(pollTimeout) |
| 37 | + g.SetDefaultEventuallyPollingInterval(pollInterval) |
| 38 | + |
| 39 | + env := e2eutil.New(t) |
| 40 | + if env.RegistryUser() == "" || env.RegistryPassword() == "" { |
| 41 | + t.Skip("E2E_REGISTRY_USER / E2E_REGISTRY_PASSWORD not set") |
| 42 | + } |
| 43 | + allowedNS := os.Getenv("E2E_PULL_SECRET_NAMESPACE") |
| 44 | + if allowedNS == "" { |
| 45 | + t.Skip("E2E_PULL_SECRET_NAMESPACE not set") |
| 46 | + } |
| 47 | + |
| 48 | + ctx := context.Background() |
| 49 | + configuredNamespaces := controllerPullSecretNamespaces(t, g, ctx, env.Client) |
| 50 | + if !slices.Contains(configuredNamespaces, allowedNS) { |
| 51 | + t.Fatalf( |
| 52 | + "operator must allow pull secrets from %q; configured namespaces: %v", |
| 53 | + allowedNS, |
| 54 | + configuredNamespaces, |
| 55 | + ) |
| 56 | + } |
| 57 | + |
| 58 | + deniedNS := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: env.TestID() + "-denied"}} |
| 59 | + g.Expect(env.Client.Create(ctx, deniedNS)).To(Succeed()) |
| 60 | + t.Cleanup(func() { _ = env.Client.Delete(context.Background(), deniedNS) }) |
| 61 | + |
| 62 | + nodeName := env.AddNode(t) |
| 63 | + |
| 64 | + deniedSecret := registrySecret(env, env.TestID()+"-pull-secret", deniedNS.Name) |
| 65 | + g.Expect(env.Client.Create(ctx, deniedSecret)).To(Succeed()) |
| 66 | + allowedSecret := registrySecret(env, env.TestID()+"-pull-secret", allowedNS) |
| 67 | + g.Expect(env.Client.Create(ctx, allowedSecret)).To(Succeed()) |
| 68 | + t.Cleanup(func() { _ = env.Client.Delete(context.Background(), allowedSecret) }) |
| 69 | + |
| 70 | + authImageRef := env.AuthImageRef() |
| 71 | + pool := env.NewPool("psns", authImageRef, |
| 72 | + testutil.WithPullSecret(deniedSecret.Name, deniedSecret.Namespace), |
| 73 | + ) |
| 74 | + g.Expect(env.Client.Create(ctx, pool)).To(Succeed()) |
| 75 | + |
| 76 | + // Phase 1: the secret exists, but its namespace is not allowed. |
| 77 | + g.Eventually(func() ([]metav1.Condition, error) { |
| 78 | + var p bootcv1alpha1.BootcNodePool |
| 79 | + err := env.Client.Get(ctx, client.ObjectKeyFromObject(pool), &p) |
| 80 | + return p.Status.Conditions, err |
| 81 | + }).Should(ContainElement(And( |
| 82 | + HaveField("Type", bootcv1alpha1.PoolDegraded), |
| 83 | + HaveField("Status", metav1.ConditionTrue), |
| 84 | + HaveField("Reason", bootcv1alpha1.PoolSecretError), |
| 85 | + HaveField("Message", ContainSubstring(fmt.Sprintf("%q", deniedNS.Name))), |
| 86 | + ))) |
| 87 | + g.Consistently(func() bool { |
| 88 | + var bn bootcv1alpha1.BootcNode |
| 89 | + err := env.Client.Get(ctx, client.ObjectKey{Name: nodeName}, &bn) |
| 90 | + return apierrors.IsNotFound(err) |
| 91 | + }).WithTimeout(10*time.Second).Should(BeTrue(), |
| 92 | + "no BootcNode should be created for a pool with a denied secret") |
| 93 | + |
| 94 | + t.Logf("Pool %q is Degraded for secret in namespace %q", pool.Name, deniedNS.Name) |
| 95 | + |
| 96 | + // Phase 2: a secret in the allowed namespace is used to pull the image. |
| 97 | + modified := pool.DeepCopy() |
| 98 | + modified.Spec.PullSecretRef = &bootcv1alpha1.PullSecretRef{ |
| 99 | + Name: allowedSecret.Name, Namespace: allowedSecret.Namespace, |
| 100 | + } |
| 101 | + g.Expect(env.Client.Patch(ctx, modified, client.MergeFrom(pool))).To(Succeed()) |
| 102 | + *pool = *modified |
| 103 | + |
| 104 | + testutil.WaitForNodeIdle(t, g, ctx, env.Client, nodeName, 8*time.Minute, |
| 105 | + HaveField("Booted", HaveField("Image", Equal(authImageRef))), |
| 106 | + ) |
| 107 | + g.Eventually(fetchPoolStatus(ctx, env.Client, pool)). |
| 108 | + Should(poolAllUpdated(1, env.NodeImageUpdateDigest())) |
| 109 | + |
| 110 | + t.Logf("Node %q updated with a pull secret from namespace %q", nodeName, allowedNS) |
| 111 | +} |
| 112 | + |
| 113 | +// registrySecret returns a dockerconfigjson Secret with the credentials of |
| 114 | +// the authenticated e2e registry. |
| 115 | +func registrySecret(env *e2eutil.Env, name, namespace string) *corev1.Secret { |
| 116 | + auth := base64.StdEncoding.EncodeToString( |
| 117 | + []byte(env.RegistryUser() + ":" + env.RegistryPassword()), |
| 118 | + ) |
| 119 | + return &corev1.Secret{ |
| 120 | + ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: namespace}, |
| 121 | + Type: corev1.SecretTypeDockerConfigJson, |
| 122 | + Data: map[string][]byte{ |
| 123 | + corev1.DockerConfigJsonKey: fmt.Appendf(nil, |
| 124 | + `{"auths":{%q:{"auth":%q}}}`, env.RegistryHost(), auth), |
| 125 | + }, |
| 126 | + } |
| 127 | +} |
0 commit comments