@@ -40,7 +40,7 @@ func TestAddKMSPluginToPodSpec(t *testing.T) {
4040 Addr : "https://vault.example.com:8200" ,
4141 Namespace : "my-namespace" ,
4242 KeyName : "my-key" ,
43- }, "kms-secret-id-555" ),
43+ }, "unix:///var/run/kmsplugin/kms-555.sock" , " kms-secret-id-555" ),
4444 featureGateAccessor : featuregates .NewHardcodedFeatureGateAccess (
4545 []configv1.FeatureGateName {features .FeatureGateKMSEncryption },
4646 nil ,
@@ -55,6 +55,140 @@ func TestAddKMSPluginToPodSpec(t *testing.T) {
5555 },
5656 kmsPluginImage : "quay.io/example/vault-kms:v1" ,
5757 },
58+ {
59+ name : "KMS provider name with extra dashes" ,
60+ podSpec : & corev1.PodSpec {
61+ Containers : []corev1.Container {
62+ {Name : "kube-apiserver" },
63+ },
64+ },
65+ expectedPodSpec : expectedPodSpecWithKMSSidecar ("quay.io/example/vault-kms:v1" , & vaultConfiguration {
66+ RoleID : "test-role-id" ,
67+ Addr : "https://vault.example.com:8200" ,
68+ Namespace : "my-namespace" ,
69+ KeyName : "my-key" ,
70+ }, "unix:///var/run/kmsplugin/kms-3.sock" , "kms-secret-id-3" ),
71+ featureGateAccessor : featuregates .NewHardcodedFeatureGateAccess (
72+ []configv1.FeatureGateName {features .FeatureGateKMSEncryption },
73+ nil ,
74+ ),
75+ secrets : []* corev1.Secret {
76+ newEncryptionConfigSecretWithKMSName (t , "vault-kms-3_secrets" , "unix:///var/run/kmsplugin/kms-3.sock" , & vaultConfiguration {
77+ RoleID : "test-role-id" ,
78+ Addr : "https://vault.example.com:8200" ,
79+ Namespace : "my-namespace" ,
80+ KeyName : "my-key" ,
81+ }, 3 ),
82+ },
83+ kmsPluginImage : "quay.io/example/vault-kms:v1" ,
84+ },
85+ {
86+ name : "feature gate disabled: pod spec unchanged" ,
87+ podSpec : & corev1.PodSpec {
88+ Containers : []corev1.Container {
89+ {Name : "kube-apiserver" },
90+ },
91+ },
92+ expectedPodSpec : & corev1.PodSpec {
93+ Containers : []corev1.Container {
94+ {Name : "kube-apiserver" },
95+ },
96+ },
97+ featureGateAccessor : featuregates .NewHardcodedFeatureGateAccess (
98+ nil ,
99+ []configv1.FeatureGateName {features .FeatureGateKMSEncryption },
100+ ),
101+ },
102+ {
103+ name : "encryption config secret not found: pod spec unchanged" ,
104+ podSpec : & corev1.PodSpec {
105+ Containers : []corev1.Container {
106+ {Name : "kube-apiserver" },
107+ },
108+ },
109+ expectedPodSpec : & corev1.PodSpec {
110+ Containers : []corev1.Container {
111+ {Name : "kube-apiserver" },
112+ },
113+ },
114+ featureGateAccessor : featuregates .NewHardcodedFeatureGateAccess (
115+ []configv1.FeatureGateName {features .FeatureGateKMSEncryption },
116+ nil ,
117+ ),
118+ secrets : []* corev1.Secret {},
119+ },
120+ {
121+ name : "no KMS provider in EncryptionConfiguration: pod spec unchanged" ,
122+ podSpec : & corev1.PodSpec {
123+ Containers : []corev1.Container {
124+ {Name : "kube-apiserver" },
125+ },
126+ },
127+ expectedPodSpec : & corev1.PodSpec {
128+ Containers : []corev1.Container {
129+ {Name : "kube-apiserver" },
130+ },
131+ },
132+ featureGateAccessor : featuregates .NewHardcodedFeatureGateAccess (
133+ []configv1.FeatureGateName {features .FeatureGateKMSEncryption },
134+ nil ,
135+ ),
136+ secrets : []* corev1.Secret {
137+ {
138+ ObjectMeta : metav1.ObjectMeta {
139+ Name : "encryption-config-openshift-kube-apiserver" ,
140+ Namespace : "openshift-config-managed" ,
141+ },
142+ Data : map [string ][]byte {
143+ "encryption-config" : []byte (`
144+ apiVersion: apiserver.config.k8s.io/v1
145+ kind: EncryptionConfiguration
146+ resources:
147+ - resources:
148+ - secrets
149+ providers:
150+ - identity: {}
151+ ` ),
152+ },
153+ },
154+ },
155+ },
156+ {
157+ name : "malformed KMS provider name: error" ,
158+ podSpec : & corev1.PodSpec {
159+ Containers : []corev1.Container {
160+ {Name : "kube-apiserver" },
161+ },
162+ },
163+ featureGateAccessor : featuregates .NewHardcodedFeatureGateAccess (
164+ []configv1.FeatureGateName {features .FeatureGateKMSEncryption },
165+ nil ,
166+ ),
167+ secrets : []* corev1.Secret {
168+ {
169+ ObjectMeta : metav1.ObjectMeta {
170+ Name : "encryption-config-openshift-kube-apiserver" ,
171+ Namespace : "openshift-config-managed" ,
172+ },
173+ Data : map [string ][]byte {
174+ "encryption-config" : []byte (`
175+ apiVersion: apiserver.config.k8s.io/v1
176+ kind: EncryptionConfiguration
177+ resources:
178+ - resources:
179+ - secrets
180+ providers:
181+ - kms:
182+ apiVersion: v2
183+ name: invalid-name
184+ endpoint: unix:///var/run/kmsplugin/kms.sock
185+ timeout: 10s
186+ ` ),
187+ },
188+ },
189+ },
190+ wantErr : "unexpected KMS provider name format" ,
191+ },
58192 }
59193
60194 for _ , tt := range tests {
@@ -78,7 +212,7 @@ func TestAddKMSPluginToPodSpec(t *testing.T) {
78212 }
79213}
80214
81- func expectedPodSpecWithKMSSidecar (image string , config * vaultConfiguration , keySecretID string ) * corev1.PodSpec {
215+ func expectedPodSpecWithKMSSidecar (image string , config * vaultConfiguration , endpoint , keySecretID string ) * corev1.PodSpec {
82216 directoryOrCreate := corev1 .HostPathDirectoryOrCreate
83217 return & corev1.PodSpec {
84218 Containers : []corev1.Container {
@@ -98,14 +232,15 @@ func expectedPodSpecWithKMSSidecar(image string, config *vaultConfiguration, key
98232 Command : []string {"/bin/sh" , "-c" },
99233 Args : []string {fmt .Sprintf (`
100234 exec /vault-kube-kms \
101- -listen-address=unix:///var/run/kmsplugin/kms.sock \
235+ -listen-address=%s \
102236 -vault-address=%s \
103237 -vault-namespace=%s \
104238 -transit-mount=transit \
105239 -transit-key=%s \
106240 -log-level=debug-extended \
107241 -approle-role-id=%s \
108242 -approle-secret-id-path=/etc/kubernetes/static-pod-resources/%s` ,
243+ endpoint ,
109244 config .Addr ,
110245 config .Namespace ,
111246 config .KeyName ,
@@ -155,10 +290,10 @@ resources:
155290 - secrets
156291 providers:
157292 - kms:
158- name: vault
159- endpoint: unix:///var/run/kmsplugin/ kms.sock
160- cachesize: 1000
161- timeout: 3s
293+ apiVersion: v2
294+ name: kms-555_secrets
295+ endpoint: unix:///var/run/kmsplugin/kms-555.sock
296+ timeout: 10s
162297 - identity: {}
163298`
164299 providerConfig , err := json .Marshal (config )
@@ -177,6 +312,39 @@ resources:
177312 }
178313}
179314
315+ func newEncryptionConfigSecretWithKMSName (t * testing.T , kmsName , endpoint string , config * vaultConfiguration , keyID int ) * corev1.Secret {
316+ t .Helper ()
317+
318+ encryptionConfig := fmt .Sprintf (`
319+ apiVersion: apiserver.config.k8s.io/v1
320+ kind: EncryptionConfiguration
321+ resources:
322+ - resources:
323+ - secrets
324+ providers:
325+ - kms:
326+ apiVersion: v2
327+ name: %s
328+ endpoint: %s
329+ timeout: 10s
330+ - identity: {}
331+ ` , kmsName , endpoint )
332+ providerConfig , err := json .Marshal (config )
333+ require .NoError (t , err )
334+
335+ return & corev1.Secret {
336+ ObjectMeta : metav1.ObjectMeta {
337+ Name : "encryption-config-openshift-kube-apiserver" ,
338+ Namespace : "openshift-config-managed" ,
339+ },
340+ Data : map [string ][]byte {
341+ "encryption-config" : []byte (encryptionConfig ),
342+ fmt .Sprintf ("kms-provider-config-%d" , keyID ): providerConfig ,
343+ fmt .Sprintf ("kms-secret-id-%d" , keyID ): []byte ("some-secret-id" ),
344+ },
345+ }
346+ }
347+
180348// secretLister implements corev1listers.SecretLister backed by a fake client.
181349type secretLister struct {
182350 client * fake.Clientset
0 commit comments