|
| 1 | +// Copyright 2024 Nutanix. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package credentials |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + corev1 "k8s.io/api/core/v1" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + cabpkv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1" |
| 14 | +) |
| 15 | + |
| 16 | +func Test_generateCredentialsSecretFile(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + |
| 19 | + tests := []struct { |
| 20 | + name string |
| 21 | + configs []providerConfig |
| 22 | + clusterName string |
| 23 | + wantFile *cabpkv1.File |
| 24 | + }{ |
| 25 | + { |
| 26 | + name: "empty configs, expect no file", |
| 27 | + configs: nil, |
| 28 | + clusterName: "test-cluster", |
| 29 | + wantFile: nil, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "config with no static credentials, expect no file", |
| 33 | + configs: []providerConfig{ |
| 34 | + {URL: "https://123456789.dkr.ecr.us-east-1.amazonaws.com"}, |
| 35 | + }, |
| 36 | + clusterName: "test-cluster", |
| 37 | + wantFile: nil, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "config with static credentials, expect a file", |
| 41 | + configs: []providerConfig{ |
| 42 | + { |
| 43 | + URL: "https://myregistry.com", |
| 44 | + Username: "myuser", |
| 45 | + Password: "mypassword", |
| 46 | + }, |
| 47 | + }, |
| 48 | + clusterName: "test-cluster", |
| 49 | + wantFile: &cabpkv1.File{ |
| 50 | + Path: "/etc/kubernetes/static-image-credentials.json", |
| 51 | + Permissions: "0600", |
| 52 | + ContentFrom: &cabpkv1.FileSource{ |
| 53 | + Secret: cabpkv1.SecretFileSource{ |
| 54 | + Name: "test-cluster-static-credential-provider-response", |
| 55 | + Key: "static-credential-provider", |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + } |
| 61 | + |
| 62 | + for idx := range tests { |
| 63 | + tt := tests[idx] |
| 64 | + t.Run(tt.name, func(t *testing.T) { |
| 65 | + t.Parallel() |
| 66 | + |
| 67 | + gotFile := generateCredentialsSecretFile(tt.configs, tt.clusterName) |
| 68 | + assert.Equal(t, tt.wantFile, gotFile) |
| 69 | + }) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func Test_generateCredentialsSecret(t *testing.T) { |
| 74 | + t.Parallel() |
| 75 | + |
| 76 | + tests := []struct { |
| 77 | + name string |
| 78 | + configs []providerConfig |
| 79 | + clusterName string |
| 80 | + namespace string |
| 81 | + wantSecret *corev1.Secret |
| 82 | + }{ |
| 83 | + { |
| 84 | + name: "empty configs, expect no Secret", |
| 85 | + configs: nil, |
| 86 | + clusterName: "test-cluster", |
| 87 | + namespace: "test-namespace", |
| 88 | + wantSecret: nil, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "config with no static credentials, expect no Secret", |
| 92 | + configs: []providerConfig{ |
| 93 | + {URL: "https://123456789.dkr.ecr.us-east-1.amazonaws.com"}, |
| 94 | + }, |
| 95 | + clusterName: "test-cluster", |
| 96 | + namespace: "test-namespace", |
| 97 | + wantSecret: nil, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "config with static credentials, expect a Secret", |
| 101 | + configs: []providerConfig{ |
| 102 | + { |
| 103 | + URL: "https://myregistry.com", |
| 104 | + Username: "myuser", |
| 105 | + Password: "mypassword", |
| 106 | + }, |
| 107 | + }, |
| 108 | + clusterName: "test-cluster", |
| 109 | + namespace: "test-namespace", |
| 110 | + wantSecret: &corev1.Secret{ |
| 111 | + TypeMeta: metav1.TypeMeta{ |
| 112 | + APIVersion: "v1", |
| 113 | + Kind: "Secret", |
| 114 | + }, |
| 115 | + ObjectMeta: metav1.ObjectMeta{ |
| 116 | + Name: "test-cluster-static-credential-provider-response", |
| 117 | + Namespace: "test-namespace", |
| 118 | + Labels: map[string]string{ |
| 119 | + "cluster.x-k8s.io/cluster-name": "test-cluster", |
| 120 | + "clusterctl.cluster.x-k8s.io/move": "", |
| 121 | + }, |
| 122 | + }, |
| 123 | + StringData: map[string]string{ |
| 124 | + "static-credential-provider": `{ |
| 125 | + "kind":"CredentialProviderResponse", |
| 126 | + "apiVersion":"credentialprovider.kubelet.k8s.io/v1", |
| 127 | + "cacheKeyType":"Image", |
| 128 | + "cacheDuration":"0s", |
| 129 | + "auth":{ |
| 130 | + "myregistry.com": {"username": "myuser", "password": "mypassword"} |
| 131 | + } |
| 132 | +}`, |
| 133 | + }, |
| 134 | + Type: corev1.SecretTypeOpaque, |
| 135 | + }, |
| 136 | + }, |
| 137 | + } |
| 138 | + |
| 139 | + for idx := range tests { |
| 140 | + tt := tests[idx] |
| 141 | + t.Run(tt.name, func(t *testing.T) { |
| 142 | + t.Parallel() |
| 143 | + |
| 144 | + gotSecret, err := generateCredentialsSecret(tt.configs, tt.clusterName, tt.namespace) |
| 145 | + require.NoError(t, err) |
| 146 | + assert.Equal(t, tt.wantSecret, gotSecret) |
| 147 | + }) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func Test_kubeletStaticCredentialProviderSecretContents(t *testing.T) { |
| 152 | + t.Parallel() |
| 153 | + |
| 154 | + tests := []struct { |
| 155 | + name string |
| 156 | + configs []providerConfig |
| 157 | + wantContents string |
| 158 | + }{ |
| 159 | + { |
| 160 | + name: "config with no static credentials, expect empty string", |
| 161 | + configs: []providerConfig{ |
| 162 | + {URL: "https://123456789.dkr.ecr.us-east-1.amazonaws.com"}, |
| 163 | + }, |
| 164 | + wantContents: "", |
| 165 | + }, |
| 166 | + { |
| 167 | + name: "config with 'registry-1.docker.io', expect it to also add 'docker.io'", |
| 168 | + configs: []providerConfig{ |
| 169 | + { |
| 170 | + URL: "https://registry-1.docker.io", |
| 171 | + Username: "myuser", |
| 172 | + Password: "mypassword", |
| 173 | + }, |
| 174 | + }, |
| 175 | + wantContents: `{ |
| 176 | + "kind":"CredentialProviderResponse", |
| 177 | + "apiVersion":"credentialprovider.kubelet.k8s.io/v1", |
| 178 | + "cacheKeyType":"Image", |
| 179 | + "cacheDuration":"0s", |
| 180 | + "auth":{ |
| 181 | + "registry-1.docker.io": {"username": "myuser", "password": "mypassword"}, |
| 182 | + "docker.io": {"username": "myuser", "password": "mypassword"} |
| 183 | + } |
| 184 | +}`, |
| 185 | + }, |
| 186 | + { |
| 187 | + name: "multiple configs with some static credentials, expect a string with multiple entries", |
| 188 | + configs: []providerConfig{ |
| 189 | + { |
| 190 | + URL: "https://myregistry.com", |
| 191 | + Username: "myuser", |
| 192 | + Password: "mypassword", |
| 193 | + }, |
| 194 | + {URL: "https://123456789.dkr.ecr.us-east-1.amazonaws.com"}, |
| 195 | + { |
| 196 | + URL: "https://registry-1.docker.io", |
| 197 | + Username: "myuser", |
| 198 | + Password: "mypassword", |
| 199 | + }, |
| 200 | + { |
| 201 | + URL: "https://anotherregistry.com", |
| 202 | + Username: "anotheruser", |
| 203 | + Password: "anotherpassword", |
| 204 | + }, |
| 205 | + }, |
| 206 | + wantContents: `{ |
| 207 | + "kind":"CredentialProviderResponse", |
| 208 | + "apiVersion":"credentialprovider.kubelet.k8s.io/v1", |
| 209 | + "cacheKeyType":"Image", |
| 210 | + "cacheDuration":"0s", |
| 211 | + "auth":{ |
| 212 | + "myregistry.com": {"username": "myuser", "password": "mypassword"}, |
| 213 | + "registry-1.docker.io": {"username": "myuser", "password": "mypassword"}, |
| 214 | + "docker.io": {"username": "myuser", "password": "mypassword"}, |
| 215 | + "anotherregistry.com": {"username": "anotheruser", "password": "anotherpassword"} |
| 216 | + } |
| 217 | +}`, |
| 218 | + }, |
| 219 | + } |
| 220 | + for idx := range tests { |
| 221 | + tt := tests[idx] |
| 222 | + t.Run(tt.name, func(t *testing.T) { |
| 223 | + t.Parallel() |
| 224 | + |
| 225 | + gotContents, err := kubeletStaticCredentialProviderSecretContents(tt.configs) |
| 226 | + require.NoError(t, err) |
| 227 | + assert.Equal(t, tt.wantContents, gotContents) |
| 228 | + }) |
| 229 | + } |
| 230 | +} |
0 commit comments