@@ -2,6 +2,8 @@ package datadogagent
2
2
3
3
import (
4
4
"context"
5
+ "crypto/sha256"
6
+ "encoding/hex"
5
7
"fmt"
6
8
"testing"
7
9
"time"
@@ -650,3 +652,164 @@ func defaultProfile() v1alpha1.DatadogAgentProfile {
650
652
},
651
653
}
652
654
}
655
+
656
+ func Test_updateSecretHash (t * testing.T ) {
657
+ sch := runtime .NewScheme ()
658
+ _ = scheme .AddToScheme (sch )
659
+ _ = v1alpha1 .AddToScheme (sch )
660
+ _ = v2alpha1 .AddToScheme (sch )
661
+
662
+ tests := []struct {
663
+ name string
664
+ dda * v2alpha1.DatadogAgent
665
+ secret * corev1.Secret
666
+ expectedEnv string
667
+ expectedValue string
668
+ }{
669
+ {
670
+ name : "API key secret present" ,
671
+ dda : & v2alpha1.DatadogAgent {
672
+ ObjectMeta : metav1.ObjectMeta {
673
+ Name : "test-dda" ,
674
+ Namespace : "default" ,
675
+ },
676
+ Spec : v2alpha1.DatadogAgentSpec {
677
+ Global : & v2alpha1.GlobalConfig {
678
+ Credentials : & v2alpha1.DatadogCredentials {
679
+ APISecret : & v2alpha1.SecretConfig {
680
+ SecretName : "test-secret" ,
681
+ },
682
+ },
683
+ },
684
+ },
685
+ },
686
+ secret : & corev1.Secret {
687
+ ObjectMeta : metav1.ObjectMeta {
688
+ Name : "test-secret" ,
689
+ Namespace : "default" ,
690
+ },
691
+ Data : map [string ][]byte {
692
+ "api-key" : []byte ("test-api-key" ),
693
+ },
694
+ },
695
+ expectedEnv : "API_SECRET_HASH" ,
696
+ expectedValue : "test-api-key" ,
697
+ },
698
+ {
699
+ name : "API key secret not present" ,
700
+ dda : & v2alpha1.DatadogAgent {
701
+ ObjectMeta : metav1.ObjectMeta {
702
+ Name : "test-dda" ,
703
+ Namespace : "default" ,
704
+ },
705
+ Spec : v2alpha1.DatadogAgentSpec {
706
+ Global : & v2alpha1.GlobalConfig {
707
+ Credentials : & v2alpha1.DatadogCredentials {},
708
+ },
709
+ },
710
+ },
711
+ secret : nil ,
712
+ expectedEnv : "" ,
713
+ expectedValue : "" ,
714
+ },
715
+ {
716
+ name : "API key secret was used but not anymore" ,
717
+ dda : & v2alpha1.DatadogAgent {
718
+ ObjectMeta : metav1.ObjectMeta {
719
+ Name : "test-dda" ,
720
+ Namespace : "default" ,
721
+ },
722
+ Spec : v2alpha1.DatadogAgentSpec {
723
+ Global : & v2alpha1.GlobalConfig {
724
+ Credentials : & v2alpha1.DatadogCredentials {},
725
+ Env : []corev1.EnvVar {
726
+ {
727
+ Name : "API_SECRET_HASH" ,
728
+ Value : "old-hash" ,
729
+ },
730
+ },
731
+ },
732
+ },
733
+ },
734
+ secret : nil ,
735
+ expectedEnv : "" ,
736
+ expectedValue : "" ,
737
+ },
738
+ {
739
+ name : "API key wasn't used, but now is" ,
740
+ dda : & v2alpha1.DatadogAgent {
741
+ ObjectMeta : metav1.ObjectMeta {
742
+ Name : "test-dda" ,
743
+ Namespace : "default" ,
744
+ },
745
+ Spec : v2alpha1.DatadogAgentSpec {
746
+ Global : & v2alpha1.GlobalConfig {
747
+ Credentials : & v2alpha1.DatadogCredentials {
748
+ APISecret : & v2alpha1.SecretConfig {
749
+ SecretName : "test-secret" ,
750
+ },
751
+ },
752
+ },
753
+ },
754
+ },
755
+ secret : & corev1.Secret {
756
+ ObjectMeta : metav1.ObjectMeta {
757
+ Name : "test-secret" ,
758
+ Namespace : "default" ,
759
+ },
760
+ Data : map [string ][]byte {
761
+ "api-key" : []byte ("test-api-key" ),
762
+ },
763
+ },
764
+ expectedEnv : "API_SECRET_HASH" ,
765
+ expectedValue : "test-api-key" ,
766
+ },
767
+ }
768
+
769
+ for _ , tt := range tests {
770
+ t .Run (tt .name , func (t * testing.T ) {
771
+ var objs []client.Object
772
+ objs = append (objs , tt .dda )
773
+ if tt .secret != nil {
774
+ objs = append (objs , tt .secret )
775
+ }
776
+
777
+ client := fake .NewClientBuilder ().WithScheme (sch ).WithObjects (objs ... ).Build ()
778
+ reconciler := & Reconciler {
779
+ client : client ,
780
+ log : logf .Log .WithName ("Test_updateSecretHash" ),
781
+ options : ReconcilerOptions {
782
+ DatadogAgentProfileEnabled : true ,
783
+ },
784
+ }
785
+
786
+ // Call the updateSecretHash function
787
+ reconciler .updateSecretHash (context .Background (), tt .dda )
788
+
789
+ // Verify that the secret hash was appended to spec.global.env if secret is present
790
+ if tt .secret != nil {
791
+ expectedHash := sha256 .New ()
792
+ expectedHash .Write ([]byte (tt .expectedValue ))
793
+ secretHash := hex .EncodeToString (expectedHash .Sum (nil ))
794
+
795
+ found := false
796
+ for _ , envVar := range tt .dda .Spec .Global .Env {
797
+ if envVar .Name == tt .expectedEnv && envVar .Value == secretHash {
798
+ found = true
799
+ break
800
+ }
801
+ }
802
+ assert .True (t , found , fmt .Sprintf ("%s not found in spec.global.env" , tt .expectedEnv ))
803
+ } else {
804
+ found := false
805
+ for _ , envVar := range tt .dda .Spec .Global .Env {
806
+ if envVar .Name == "API_SECRET_HASH" {
807
+ found = true
808
+ break
809
+ }
810
+ }
811
+ assert .False (t , found , "API_SECRET_HASH should not be present in spec.global.env" )
812
+ }
813
+ })
814
+ }
815
+ }
0 commit comments