@@ -45,7 +45,6 @@ func RetrieveUUID(annotations map[string]string) string {
4545}
4646
4747func ExpectedKVv1 (uuid string ) * corev1.Pod {
48- // Injects uuid into expected output for KV v1 secrets
4948 expected := & corev1.Pod {
5049 ObjectMeta : metav1.ObjectMeta {
5150 Annotations : map [string ]string {
@@ -64,7 +63,6 @@ func ExpectedKVv1(uuid string) *corev1.Pod {
6463}
6564
6665func ExpectedKVv2 (uuid string ) * corev1.Pod {
67- // Injects uuid into expected output for KV v2 secrets
6866 expected := & corev1.Pod {
6967 ObjectMeta : metav1.ObjectMeta {
7068 Annotations : map [string ]string {
@@ -82,8 +80,7 @@ func ExpectedKVv2(uuid string) *corev1.Pod {
8280 return expected
8381}
8482
85- func ExpectedKVv3 (uuid string ) * corev1.Pod {
86- // Injects uuid into expected output for KV v2 secrets
83+ func ExpectedExtraAnnotation (uuid string ) * corev1.Pod {
8784 expected := & corev1.Pod {
8885 ObjectMeta : metav1.ObjectMeta {
8986 Annotations : map [string ]string {
@@ -102,8 +99,7 @@ func ExpectedKVv3(uuid string) *corev1.Pod {
10299 return expected
103100}
104101
105- func ExpectedKVv4 (uuid string ) * corev1.Pod {
106- // Injects uuid into expected output for KV v2 secrets
102+ func ExpectedExistingRoleAnnotation (uuid string ) * corev1.Pod {
107103 expected := & corev1.Pod {
108104 ObjectMeta : metav1.ObjectMeta {
109105 Annotations : map [string ]string {
@@ -121,8 +117,7 @@ func ExpectedKVv4(uuid string) *corev1.Pod {
121117 return expected
122118}
123119
124- func ExpectedKVv5 (uuid string ) * corev1.Pod {
125- // Injects uuid into expected output for KV v2 secrets
120+ func ExpectedConfigAnnotation (uuid string ) * corev1.Pod {
126121 expected := & corev1.Pod {
127122 ObjectMeta : metav1.ObjectMeta {
128123 Annotations : map [string ]string {
@@ -140,6 +135,23 @@ func ExpectedKVv5(uuid string) *corev1.Pod {
140135 return expected
141136}
142137
138+ func ExpectedDB (uuid string ) * corev1.Pod {
139+ expected := & corev1.Pod {
140+ ObjectMeta : metav1.ObjectMeta {
141+ Annotations : map [string ]string {
142+ "vault.hashicorp.com/agent-inject" : "true" ,
143+ "vault.hashicorp.com/secret-volume-path" : "/etc/flyte/secrets" ,
144+ "vault.hashicorp.com/role" : "flyte" ,
145+ "vault.hashicorp.com/agent-pre-populate-only" : "true" ,
146+ fmt .Sprintf ("vault.hashicorp.com/agent-inject-secret-%s" , uuid ): "foo" ,
147+ fmt .Sprintf ("vault.hashicorp.com/agent-inject-file-%s" , uuid ): "foo/bar" ,
148+ },
149+ },
150+ Spec : PodSpec ,
151+ }
152+ return expected
153+ }
154+
143155func NewInputPod (annotations map [string ]string ) * corev1.Pod {
144156 // Need to create a new Pod for every test since annotations are otherwise appended to original reference object
145157 p := & corev1.Pod {
@@ -176,70 +188,108 @@ func TestVaultSecretManagerInjector_Inject(t *testing.T) {
176188 wantErr bool
177189 }{
178190 {
179- name : "KVv1 Secret" ,
191+ name : "KVv1 Secret Group Version argument overwrites config " ,
180192 args : args {
181- cfg : config.VaultSecretManagerConfig {Role : "flyte" , KVVersion : config .KVVersion1 },
182- secret : inputSecret ,
183- p : NewInputPod (map [string ]string {}),
193+ cfg : config.VaultSecretManagerConfig {Role : "flyte" , KVVersion : config .KVVersion2 },
194+ secret : & coreIdl.Secret {
195+ Group : "foo" ,
196+ Key : "bar" ,
197+ GroupVersion : "kv1" ,
198+ },
199+ p : NewInputPod (map [string ]string {}),
184200 },
185201 want : ExpectedKVv1 ,
186202 wantErr : false ,
187203 },
188204 {
189- name : "KVv2 Secret" ,
205+ name : "KVv2 Secret Group Version argument overwrites config " ,
190206 args : args {
191- cfg : config.VaultSecretManagerConfig {Role : "flyte" , KVVersion : config .KVVersion2 },
192- secret : inputSecret ,
193- p : NewInputPod (map [string ]string {}),
207+ cfg : config.VaultSecretManagerConfig {Role : "flyte" , KVVersion : config .KVVersion1 },
208+ secret : & coreIdl.Secret {
209+ Group : "foo" ,
210+ Key : "bar" ,
211+ GroupVersion : "kv2" ,
212+ },
213+ p : NewInputPod (map [string ]string {}),
194214 },
195215 want : ExpectedKVv2 ,
196216 wantErr : false ,
197217 },
198218 {
199- name : "KVv3 Secret - extra annotations " ,
219+ name : "Extra annotations from config are added " ,
200220 args : args {
201221 cfg : config.VaultSecretManagerConfig {Role : "flyte" , KVVersion : config .KVVersion2 , Annotations : map [string ]string {
202222 "vault.hashicorp.com/auth-config-type" : "gce" ,
203223 }},
204224 secret : inputSecret ,
205225 p : NewInputPod (map [string ]string {}),
206226 },
207- want : ExpectedKVv3 ,
227+ want : ExpectedExtraAnnotation ,
208228 wantErr : false ,
209229 },
210230 {
211- name : "KVv4 Secret - user override annotation " ,
231+ name : "Already present annotation is not overwritten " ,
212232 args : args {
213233 cfg : config.VaultSecretManagerConfig {Role : "flyte" , KVVersion : config .KVVersion2 , Annotations : map [string ]string {}},
214234 secret : inputSecret ,
215235 p : NewInputPod (map [string ]string {
216236 "vault.hashicorp.com/role" : "my-role" ,
217237 }),
218238 },
219- want : ExpectedKVv4 ,
239+ want : ExpectedExistingRoleAnnotation ,
220240 wantErr : false ,
221241 },
222242 {
223- name : "KVv5 Secret - system override annotation" ,
243+ name : "Config annotation overwrites system default annotation" ,
224244 args : args {
225245 cfg : config.VaultSecretManagerConfig {Role : "flyte" , KVVersion : config .KVVersion2 , Annotations : map [string ]string {
226246 "vault.hashicorp.com/agent-pre-populate-only" : "false" , // override vault.hashicorp.com/agent-pre-populate-only
227247 }},
228248 secret : inputSecret ,
229249 p : NewInputPod (map [string ]string {}),
230250 },
231- want : ExpectedKVv5 ,
251+ want : ExpectedConfigAnnotation ,
232252 wantErr : false ,
233253 },
234254 {
235- name : "Unsupported KV version " ,
255+ name : "DB Secret backend enginge is supported " ,
236256 args : args {
237- cfg : config.VaultSecretManagerConfig {Role : "flyte" , KVVersion : 3 },
238- secret : inputSecret ,
239- p : NewInputPod (map [string ]string {}),
257+ cfg : config.VaultSecretManagerConfig {Role : "flyte" , KVVersion : config .KVVersion1 },
258+ secret : & coreIdl.Secret {
259+ Group : "foo" ,
260+ Key : "bar" ,
261+ GroupVersion : "db" ,
262+ },
263+ p : NewInputPod (map [string ]string {}),
240264 },
241- want : nil ,
242- wantErr : true ,
265+ want : ExpectedDB ,
266+ wantErr : false ,
267+ },
268+ {
269+ name : "Legacy config option V1 is still supported" ,
270+ args : args {
271+ cfg : config.VaultSecretManagerConfig {Role : "flyte" , KVVersion : config .KVVersion1 },
272+ secret : & coreIdl.Secret {
273+ Group : "foo" ,
274+ Key : "bar" ,
275+ },
276+ p : NewInputPod (map [string ]string {}),
277+ },
278+ want : ExpectedKVv1 ,
279+ wantErr : false ,
280+ },
281+ {
282+ name : "Legacy config option V2 is still supported" ,
283+ args : args {
284+ cfg : config.VaultSecretManagerConfig {Role : "flyte" , KVVersion : config .KVVersion2 },
285+ secret : & coreIdl.Secret {
286+ Group : "foo" ,
287+ Key : "bar" ,
288+ },
289+ p : NewInputPod (map [string ]string {}),
290+ },
291+ want : ExpectedKVv2 ,
292+ wantErr : false ,
243293 },
244294 }
245295
0 commit comments