@@ -2,7 +2,6 @@ package controllers
22
33import (
44 "context"
5- "fmt"
65 "reflect"
76 "time"
87
@@ -219,7 +218,6 @@ var _ = Describe("SyncedSecret Controller", func() {
219218 })
220219
221220 Context ("For a single SyncedSecret (using Data) with AWSAccountID" , func () {
222- // TODO do a test for DataFrom as well
223221 secretKey := types.NamespacedName {
224222 Name : "another-secret-name" ,
225223 Namespace : TEST_NAMESPACE2 ,
@@ -245,7 +243,7 @@ var _ = Describe("SyncedSecret Controller", func() {
245243 Namespace : secretKey .Namespace ,
246244 },
247245 AWSAccountID : _s ("12345678910" ),
248- IAMRole : _s ("test" ), // TODO Make this optional in CRD
246+ IAMRole : _s ("test" ),
249247 Data : []* secretsv1.SecretField {
250248 {
251249 Name : _s ("DB_NAME" ),
@@ -418,18 +416,17 @@ var _ = Describe("SyncedSecret Controller", func() {
418416 })
419417
420418 Context ("For a single SyncedSecret (using DataFrom) with AWSAccountID" , func () {
421- // TODO do a test for DataFrom as well
422419 secretKey := types.NamespacedName {
423420 Name : "secret-name-from-data" ,
424- Namespace : TEST_NAMESPACE2 ,
421+ Namespace : TEST_NAMESPACE3 ,
425422 }
426423
427424 resourceVersion := ""
428425
429426 It ("Should Create K8S Secrets for SyncedSecret (using Data) CRD with AWSAccountID" , func () {
430427 MockSecretsOutput .SecretsValueOutput = & secretsmanager.GetSecretValueOutput {
431- SecretString : _s (`{"database_name ":"secretDB","database_pass ":"cupofcoffee", "database_name1":"secretDB02 "}` ),
432- VersionId : _s (`005 ` ),
428+ SecretString : _s (`{"DB_NAME ":"secretDB","DB_PASS ":"cupofcoffee"}` ),
429+ VersionId : _s (`006 ` ),
433430 }
434431
435432 toCreate := & secretsv1.SyncedSecret {
@@ -444,32 +441,12 @@ var _ = Describe("SyncedSecret Controller", func() {
444441 Namespace : secretKey .Namespace ,
445442 },
446443 AWSAccountID : _s ("12345678910" ),
447- IAMRole : _s ("test" ), // TODO Make this optional in CRD
444+ IAMRole : _s ("test" ),
448445 DataFrom : & secretsv1.DataFrom {
449446 SecretRef : & secretsv1.SecretRef {
450- Name : _s ("random/aws/secret004 " ),
447+ Name : _s ("random/aws/secret005 " ),
451448 },
452449 },
453- // Data: []*secretsv1.SecretField{
454- // {
455- // Name: _s("DB_NAME"),
456- // ValueFrom: &secretsv1.ValueFrom{
457- // SecretKeyRef: &secretsv1.SecretKeyRef{
458- // Name: _s("random/aws/secret004"),
459- // Key: _s("database_name"),
460- // },
461- // },
462- // },
463- // {
464- // Name: _s("DB_PASS"),
465- // ValueFrom: &secretsv1.ValueFrom{
466- // SecretKeyRef: &secretsv1.SecretKeyRef{
467- // Name: _s("random/aws/secret004"),
468- // Key: _s("database_pass"),
469- // },
470- // },
471- // },
472- // },
473450 },
474451 }
475452 secretExpect := & corev1.Secret {
@@ -492,9 +469,6 @@ var _ = Describe("SyncedSecret Controller", func() {
492469 return k8serrors .IsNotFound (err )
493470 }, timeout , interval ).Should (BeFalse ())
494471
495- fmt .Printf ("fetchedSecret.Data %v" , fetchedSecret .Data )
496- fmt .Printf ("secretExpect.Data %v" , fetchedSecret .Data )
497-
498472 // we need to ensure that that secretExpect.Data is a subset of fetchedSecret.Data
499473 // the kubernetes client.go doesn't base64 values this is something that kubectl maybe does
500474 Expect (reflect .DeepEqual (fetchedSecret .Data , secretExpect .Data )).To (BeTrue ())
@@ -506,121 +480,99 @@ var _ = Describe("SyncedSecret Controller", func() {
506480
507481 })
508482
509- // It("Should update k8s secret object if there is change in AwsSecret CRD with AWSAccountID", func() {
510- // MockSecretsOutput.SecretsValueOutput = &secretsmanager.GetSecretValueOutput{
511- // SecretString: _s(`{"database_name":"secretDB","database_pass":"cupofcoffee", "database_name1":"secretDB02"}`),
512- // VersionId: _s(`005`),
513- // }
514- // toUpdate := &secretsv1.SyncedSecret{
515- // ObjectMeta: metav1.ObjectMeta{
516- // Name: secretKey.Name,
517- // Namespace: secretKey.Namespace,
518- // ResourceVersion: resourceVersion,
519- // },
520- // Spec: secretsv1.SyncedSecretSpec{
521- // SecretMetadata: metav1.ObjectMeta{
522- // Name: secretKey.Name,
523- // Namespace: secretKey.Namespace,
524- // },
525- // IAMRole: _s("test"),
526- // AWSAccountID: _s("12345678910"),
527- // Data: []*secretsv1.SecretField{
528- // {
529- // Name: _s("DB_NAME"),
530- // ValueFrom: &secretsv1.ValueFrom{
531- // SecretKeyRef: &secretsv1.SecretKeyRef{
532- // Name: _s("random/aws/secret003"),
533- // Key: _s("database_name1"),
534- // },
535- // },
536- // },
537- // {
538- // Name: _s("DB_PASS"),
539- // ValueFrom: &secretsv1.ValueFrom{
540- // SecretKeyRef: &secretsv1.SecretKeyRef{
541- // Name: _s("random/aws/secret003"),
542- // Key: _s("database_pass"),
543- // },
544- // },
545- // },
546- // },
547- // },
548- // }
549-
550- // secretExpect := &corev1.Secret{
551- // ObjectMeta: metav1.ObjectMeta{
552- // Name: secretKey.Name,
553- // Namespace: secretKey.Namespace,
554- // },
555- // Type: "Opaque",
556- // Data: map[string][]byte{
557- // "DB_NAME": []byte("secretDB02"),
558- // "DB_PASS": []byte("cupofcoffee"),
559- // },
560- // }
561-
562- // Expect(k8sClient.Update(context.Background(), toUpdate)).Should(Succeed())
563-
564- // fetchedSecret := &corev1.Secret{}
565- // Eventually(func() bool {
566- // k8sClient.Get(context.Background(), secretKey, fetchedSecret)
567- // return reflect.DeepEqual(fetchedSecret.Data, secretExpect.Data)
568- // }, timeout, interval).Should(BeTrue())
569-
570- // fetchedCfSecret := &secretsv1.SyncedSecret{}
571- // err := k8sClient.Get(context.Background(), secretKey, fetchedCfSecret)
572- // Expect(err).ToNot(HaveOccurred())
573- // resourceVersion = fetchedCfSecret.ResourceVersion
574- // })
575-
576- // It("Should update the k8s secret object if the mapped AWS Secret changes with AWSAccountID", func() {
577- // MockSecretsOutput.SecretsValueOutput = &secretsmanager.GetSecretValueOutput{
578- // SecretString: _s(`{"database_pass":"cupoftea", "database_name1":"secretDB02"}`),
579- // VersionId: _s(`006`),
580- // }
581-
582- // MockSecretsOutput.SecretsPageOutput = &secretsmanager.ListSecretsOutput{
583- // SecretList: []*secretsmanager.SecretListEntry{
584- // {
585- // Name: _s("random/aws/secret003"),
586- // LastChangedDate: _t(time_now.AddDate(0, 0, -2)),
587- // SecretVersionsToStages: map[string][]*string{
588- // "002": []*string{
589- // _s("AWSCURRENT"),
590- // },
591- // },
592- // }, {
593- // Name: _s("random/aws/secret003"),
594- // LastChangedDate: _t(time_now.AddDate(0, 0, -1)),
595- // SecretVersionsToStages: map[string][]*string{
596- // "005": {
597- // _s("AWSPREVIOUS"),
598- // },
599- // "006": {
600- // _s("AWSCURRENT"),
601- // },
602- // },
603- // },
604- // },
605- // }
606-
607- // secretExpect := &corev1.Secret{
608- // ObjectMeta: metav1.ObjectMeta{
609- // Name: secretKey.Name,
610- // Namespace: secretKey.Namespace,
611- // },
612- // Type: "Opaque",
613- // Data: map[string][]byte{
614- // "DB_NAME": []byte("secretDB02"),
615- // "DB_PASS": []byte("cupoftea"),
616- // },
617- // }
618-
619- // fetchedSecret := &corev1.Secret{}
620- // Eventually(func() bool {
621- // k8sClient.Get(context.Background(), secretKey, fetchedSecret)
622- // return reflect.DeepEqual(fetchedSecret.Data, secretExpect.Data)
623- // }, timeout, interval).Should(BeTrue())
624- // })
483+ It ("Should update k8s secret object if there is change in AwsSecret CRD with AWSAccountID" , func () {
484+ MockSecretsOutput .SecretsValueOutput = & secretsmanager.GetSecretValueOutput {
485+ SecretString : _s (`{"DB_NAME":"secretDB02","DB_PASS":"cupofcoffee"}` ),
486+ VersionId : _s (`006` ),
487+ }
488+
489+ toUpdate := & secretsv1.SyncedSecret {
490+ ObjectMeta : metav1.ObjectMeta {
491+ Name : secretKey .Name ,
492+ Namespace : secretKey .Namespace ,
493+ ResourceVersion : resourceVersion ,
494+ },
495+ Spec : secretsv1.SyncedSecretSpec {
496+ SecretMetadata : metav1.ObjectMeta {
497+ Name : secretKey .Name ,
498+ Namespace : secretKey .Namespace ,
499+ },
500+ IAMRole : _s ("test" ),
501+ AWSAccountID : _s ("12345678910" ),
502+ DataFrom : & secretsv1.DataFrom {
503+ SecretRef : & secretsv1.SecretRef {
504+ Name : _s ("random/aws/secret006" ),
505+ },
506+ },
507+ },
508+ }
509+
510+ secretExpect := & corev1.Secret {
511+ ObjectMeta : metav1.ObjectMeta {
512+ Name : secretKey .Name ,
513+ Namespace : secretKey .Namespace ,
514+ },
515+ Type : "Opaque" ,
516+ Data : map [string ][]byte {
517+ "DB_NAME" : []byte ("secretDB02" ),
518+ "DB_PASS" : []byte ("cupofcoffee" ),
519+ },
520+ }
521+
522+ Expect (k8sClient .Update (context .Background (), toUpdate )).Should (Succeed ())
523+
524+ fetchedSecret := & corev1.Secret {}
525+ Eventually (func () bool {
526+ k8sClient .Get (context .Background (), secretKey , fetchedSecret )
527+ return reflect .DeepEqual (fetchedSecret .Data , secretExpect .Data )
528+ }, timeout , interval ).Should (BeTrue ())
529+
530+ fetchedCfSecret := & secretsv1.SyncedSecret {}
531+ err := k8sClient .Get (context .Background (), secretKey , fetchedCfSecret )
532+ Expect (err ).ToNot (HaveOccurred ())
533+ resourceVersion = fetchedCfSecret .ResourceVersion
534+ })
535+
536+ It ("Should update the k8s secret object if the mapped AWS Secret changes with AWSAccountID" , func () {
537+ MockSecretsOutput .SecretsValueOutput = & secretsmanager.GetSecretValueOutput {
538+ SecretString : _s (`{"DB_PASS":"cupoftea3", "DB_NAME":"secretDB03"}` ),
539+ VersionId : _s (`007` ),
540+ }
541+
542+ MockSecretsOutput .SecretsPageOutput = & secretsmanager.ListSecretsOutput {
543+ SecretList : []* secretsmanager.SecretListEntry {
544+ {
545+ Name : _s ("random/aws/secret006" ),
546+ LastChangedDate : _t (time_now .AddDate (0 , 0 , - 1 )),
547+ SecretVersionsToStages : map [string ][]* string {
548+ "006" : {
549+ _s ("AWSPREVIOUS" ),
550+ },
551+ "007" : {
552+ _s ("AWSCURRENT" ),
553+ },
554+ },
555+ },
556+ },
557+ }
558+
559+ secretExpect := & corev1.Secret {
560+ ObjectMeta : metav1.ObjectMeta {
561+ Name : secretKey .Name ,
562+ Namespace : secretKey .Namespace ,
563+ },
564+ Type : "Opaque" ,
565+ Data : map [string ][]byte {
566+ "DB_NAME" : []byte ("secretDB03" ),
567+ "DB_PASS" : []byte ("cupoftea3" ),
568+ },
569+ }
570+
571+ fetchedSecret := & corev1.Secret {}
572+ Eventually (func () bool {
573+ k8sClient .Get (context .Background (), secretKey , fetchedSecret )
574+ return reflect .DeepEqual (fetchedSecret .Data , secretExpect .Data )
575+ }, timeout , interval ).Should (BeTrue ())
576+ })
625577 })
626578})
0 commit comments