|
| 1 | +/* |
| 2 | +Copyright 2021 The cert-manager 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 test |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | + corev1 "k8s.io/api/core/v1" |
| 28 | + rbacv1 "k8s.io/api/rbac/v1" |
| 29 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 30 | + "k8s.io/apimachinery/pkg/runtime" |
| 31 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 32 | + "k8s.io/client-go/rest" |
| 33 | + "sigs.k8s.io/controller-runtime/pkg/cache" |
| 34 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 35 | + "sigs.k8s.io/controller-runtime/pkg/envtest" |
| 36 | + |
| 37 | + multiscopedcache "github.com/cert-manager/trust-manager/pkg/bundle/cache" |
| 38 | +) |
| 39 | + |
| 40 | +func TestCache(t *testing.T) { |
| 41 | + env := &envtest.Environment{} |
| 42 | + |
| 43 | + _, err := env.Start() |
| 44 | + require.NoError(t, err) |
| 45 | + defer func() { |
| 46 | + err := env.Stop() |
| 47 | + require.NoError(t, err) |
| 48 | + }() |
| 49 | + |
| 50 | + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) |
| 51 | + defer cancel() |
| 52 | + |
| 53 | + namespace := "test-namespace" |
| 54 | + |
| 55 | + // Create a service account that can only retrieve secrets in a single namespace. |
| 56 | + var cacheRestConfig *rest.Config |
| 57 | + { |
| 58 | + godClient, err := client.New(env.Config, client.Options{}) |
| 59 | + require.NoError(t, err) |
| 60 | + |
| 61 | + ns := &corev1.Namespace{ |
| 62 | + ObjectMeta: metav1.ObjectMeta{ |
| 63 | + Name: namespace, |
| 64 | + }, |
| 65 | + } |
| 66 | + err = godClient.Create(ctx, ns) |
| 67 | + require.NoError(t, err) |
| 68 | + |
| 69 | + sa := &corev1.ServiceAccount{ |
| 70 | + ObjectMeta: metav1.ObjectMeta{ |
| 71 | + Name: "cache-sa", |
| 72 | + Namespace: namespace, |
| 73 | + }, |
| 74 | + } |
| 75 | + err = godClient.Create(ctx, sa) |
| 76 | + require.NoError(t, err) |
| 77 | + |
| 78 | + role := &rbacv1.Role{ |
| 79 | + ObjectMeta: metav1.ObjectMeta{ |
| 80 | + Name: "cache-role", |
| 81 | + Namespace: namespace, |
| 82 | + }, |
| 83 | + Rules: []rbacv1.PolicyRule{ |
| 84 | + { |
| 85 | + Verbs: []string{"list", "watch"}, |
| 86 | + APIGroups: []string{""}, |
| 87 | + Resources: []string{"secrets"}, |
| 88 | + }, |
| 89 | + }, |
| 90 | + } |
| 91 | + err = godClient.Create(ctx, role) |
| 92 | + require.NoError(t, err) |
| 93 | + |
| 94 | + rolebinding := rbacv1.RoleBinding{ |
| 95 | + ObjectMeta: metav1.ObjectMeta{ |
| 96 | + Name: "cache-rolebinding", |
| 97 | + Namespace: namespace, |
| 98 | + }, |
| 99 | + RoleRef: rbacv1.RoleRef{ |
| 100 | + APIGroup: "rbac.authorization.k8s.io", |
| 101 | + Kind: "Role", |
| 102 | + Name: "cache-role", |
| 103 | + }, |
| 104 | + Subjects: []rbacv1.Subject{ |
| 105 | + { |
| 106 | + Kind: "ServiceAccount", |
| 107 | + Name: "cache-sa", |
| 108 | + Namespace: namespace, |
| 109 | + }, |
| 110 | + }, |
| 111 | + } |
| 112 | + err = godClient.Create(ctx, &rolebinding) |
| 113 | + require.NoError(t, err) |
| 114 | + |
| 115 | + // Create a config that uses the service account. |
| 116 | + cacheRestConfig = rest.CopyConfig(env.Config) |
| 117 | + cacheRestConfig.Impersonate.UserName = fmt.Sprintf("system:serviceaccount:%s:%s", namespace, "cache-sa") |
| 118 | + cacheRestConfig.Impersonate.UID = string(sa.UID) |
| 119 | + |
| 120 | + // Create a secret that the service account can access. |
| 121 | + secret := &corev1.Secret{ |
| 122 | + ObjectMeta: metav1.ObjectMeta{ |
| 123 | + Name: "test-secret", |
| 124 | + Namespace: namespace, |
| 125 | + }, |
| 126 | + Data: map[string][]byte{ |
| 127 | + "test": []byte("test"), |
| 128 | + }, |
| 129 | + } |
| 130 | + err = godClient.Create(ctx, secret) |
| 131 | + require.NoError(t, err) |
| 132 | + } |
| 133 | + |
| 134 | + scheme := runtime.NewScheme() |
| 135 | + require.NoError(t, corev1.AddToScheme(scheme)) |
| 136 | + newCache := multiscopedcache.NewMultiScopedCache(scheme, namespace, []schema.GroupKind{{Group: "", Kind: "Secret"}}) |
| 137 | + |
| 138 | + cache, err := newCache(cacheRestConfig, cache.Options{}) |
| 139 | + require.NoError(t, err) |
| 140 | + |
| 141 | + done := make(chan error) |
| 142 | + go func() { |
| 143 | + done <- cache.Start(ctx) |
| 144 | + }() |
| 145 | + defer func() { |
| 146 | + require.NoError(t, <-done) |
| 147 | + }() |
| 148 | + |
| 149 | + assert.True(t, cache.WaitForCacheSync(ctx), "failed to sync cache") |
| 150 | + |
| 151 | + secret := &corev1.Secret{} |
| 152 | + err = cache.Get(ctx, client.ObjectKey{ |
| 153 | + Namespace: namespace, |
| 154 | + Name: "test-secret", |
| 155 | + }, secret) |
| 156 | + require.NoError(t, err) |
| 157 | + |
| 158 | + require.Equal(t, []byte("test"), secret.Data["test"]) |
| 159 | +} |
0 commit comments