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