@@ -986,7 +986,9 @@ var (
986
986
func TestHideSecretDataSameKeysDifferentValues (t * testing.T ) {
987
987
target , live , err := HideSecretData (
988
988
createSecret (map [string ]string {"key1" : "test" , "key2" : "test" }),
989
- createSecret (map [string ]string {"key1" : "test-1" , "key2" : "test-1" }))
989
+ createSecret (map [string ]string {"key1" : "test-1" , "key2" : "test-1" }),
990
+ nil ,
991
+ )
990
992
require .NoError (t , err )
991
993
992
994
assert .Equal (t , map [string ]interface {}{"key1" : replacement1 , "key2" : replacement1 }, secretData (target ))
@@ -996,7 +998,9 @@ func TestHideSecretDataSameKeysDifferentValues(t *testing.T) {
996
998
func TestHideSecretDataSameKeysSameValues (t * testing.T ) {
997
999
target , live , err := HideSecretData (
998
1000
createSecret (map [string ]string {"key1" : "test" , "key2" : "test" }),
999
- createSecret (map [string ]string {"key1" : "test" , "key2" : "test" }))
1001
+ createSecret (map [string ]string {"key1" : "test" , "key2" : "test" }),
1002
+ nil ,
1003
+ )
1000
1004
require .NoError (t , err )
1001
1005
1002
1006
assert .Equal (t , map [string ]interface {}{"key1" : replacement1 , "key2" : replacement1 }, secretData (target ))
@@ -1006,13 +1010,155 @@ func TestHideSecretDataSameKeysSameValues(t *testing.T) {
1006
1010
func TestHideSecretDataDifferentKeysDifferentValues (t * testing.T ) {
1007
1011
target , live , err := HideSecretData (
1008
1012
createSecret (map [string ]string {"key1" : "test" , "key2" : "test" }),
1009
- createSecret (map [string ]string {"key2" : "test-1" , "key3" : "test-1" }))
1013
+ createSecret (map [string ]string {"key2" : "test-1" , "key3" : "test-1" }),
1014
+ nil ,
1015
+ )
1010
1016
require .NoError (t , err )
1011
1017
1012
1018
assert .Equal (t , map [string ]interface {}{"key1" : replacement1 , "key2" : replacement1 }, secretData (target ))
1013
1019
assert .Equal (t , map [string ]interface {}{"key2" : replacement2 , "key3" : replacement1 }, secretData (live ))
1014
1020
}
1015
1021
1022
+ func TestHideSecretAnnotations (t * testing.T ) {
1023
+ tests := []struct {
1024
+ name string
1025
+ hideAnnots map [string ]bool
1026
+ annots map [string ]interface {}
1027
+ expectedAnnots map [string ]interface {}
1028
+ targetNil bool
1029
+ }{
1030
+ {
1031
+ name : "no hidden annotations" ,
1032
+ hideAnnots : nil ,
1033
+ annots : map [string ]interface {}{"token/value" : "secret" , "key" : "secret-key" , "app" : "test" },
1034
+ expectedAnnots : map [string ]interface {}{"token/value" : "secret" , "key" : "secret-key" , "app" : "test" },
1035
+ },
1036
+ {
1037
+ name : "hide annotations" ,
1038
+ hideAnnots : map [string ]bool {"token/value" : true , "key" : true },
1039
+ annots : map [string ]interface {}{"token/value" : "secret" , "key" : "secret-key" , "app" : "test" },
1040
+ expectedAnnots : map [string ]interface {}{"token/value" : replacement1 , "key" : replacement1 , "app" : "test" },
1041
+ },
1042
+ {
1043
+ name : "hide annotations in last-applied-config" ,
1044
+ hideAnnots : map [string ]bool {"token/value" : true , "key" : true },
1045
+ annots : map [string ]interface {}{
1046
+ "token/value" : "secret" ,
1047
+ "app" : "test" ,
1048
+ "kubectl.kubernetes.io/last-applied-configuration" : `{"apiVersion":"v1","kind":"Secret","metadata":{"annotations":{"app":"test","token/value":"secret","key":"secret-key"},"labels":{"app.kubernetes.io/instance":"test"},"name":"my-secret","namespace":"default"},"type":"Opaque"}` ,
1049
+ },
1050
+ expectedAnnots : map [string ]interface {}{
1051
+ "token/value" : replacement1 ,
1052
+ "app" : "test" ,
1053
+ "kubectl.kubernetes.io/last-applied-configuration" : `{"apiVersion":"v1","kind":"Secret","metadata":{"annotations":{"app":"test","key":"++++++++","token/value":"++++++++"},"labels":{"app.kubernetes.io/instance":"test"},"name":"my-secret","namespace":"default"},"type":"Opaque"}` ,
1054
+ },
1055
+ targetNil : true ,
1056
+ },
1057
+ {
1058
+ name : "special case: hide last-applied-config annotation" ,
1059
+ hideAnnots : map [string ]bool {"kubectl.kubernetes.io/last-applied-configuration" : true },
1060
+ annots : map [string ]interface {}{
1061
+ "token/value" : replacement1 ,
1062
+ "app" : "test" ,
1063
+ "kubectl.kubernetes.io/last-applied-configuration" : `{"apiVersion":"v1","kind":"Secret","metadata":{"annotations":{"app":"test","token/value":"secret","key":"secret-key"},"labels":{"app.kubernetes.io/instance":"test"},"name":"my-secret","namespace":"default"},"type":"Opaque"}` ,
1064
+ },
1065
+ expectedAnnots : map [string ]interface {}{
1066
+ "app" : "test" ,
1067
+ "kubectl.kubernetes.io/last-applied-configuration" : replacement1 ,
1068
+ },
1069
+ targetNil : true ,
1070
+ },
1071
+ {
1072
+ name : "hide annotations for malformed annotations" ,
1073
+ hideAnnots : map [string ]bool {"token/value" : true , "key" : true },
1074
+ annots : map [string ]interface {}{"token/value" : 0 , "key" : "secret" , "app" : true },
1075
+ expectedAnnots : map [string ]interface {}{"token/value" : replacement1 , "key" : replacement1 , "app" : true },
1076
+ },
1077
+ }
1078
+
1079
+ for _ , tt := range tests {
1080
+ t .Run (tt .name , func (t * testing.T ) {
1081
+
1082
+ unSecret := & unstructured.Unstructured {
1083
+ Object : map [string ]interface {}{
1084
+ "apiVersion" : "v1" ,
1085
+ "kind" : "Secret" ,
1086
+ "metadata" : map [string ]interface {}{
1087
+ "name" : "test-secret" ,
1088
+ "annotations" : tt .annots ,
1089
+ },
1090
+ "type" : "Opaque" ,
1091
+ },
1092
+ }
1093
+
1094
+ liveUn := remarshal (unSecret , applyOptions (diffOptionsForTest ()))
1095
+ targetUn := remarshal (unSecret , applyOptions (diffOptionsForTest ()))
1096
+
1097
+ if tt .targetNil {
1098
+ targetUn = nil
1099
+ }
1100
+
1101
+ target , live , err := HideSecretData (targetUn , liveUn , tt .hideAnnots )
1102
+ require .NoError (t , err )
1103
+
1104
+ // verify configured annotations are hidden
1105
+ for _ , obj := range []* unstructured.Unstructured {target , live } {
1106
+ if obj != nil {
1107
+ annots , _ , _ := unstructured .NestedMap (obj .Object , "metadata" , "annotations" )
1108
+ for ek , ev := range tt .expectedAnnots {
1109
+ v , found := annots [ek ]
1110
+ assert .True (t , found )
1111
+ assert .Equal (t , ev , v )
1112
+ }
1113
+ }
1114
+ }
1115
+ })
1116
+ }
1117
+ }
1118
+
1119
+ func TestHideSecretAnnotationsPreserveDifference (t * testing.T ) {
1120
+ hideAnnots := map [string ]bool {"token/value" : true }
1121
+
1122
+ liveUn := & unstructured.Unstructured {
1123
+ Object : map [string ]interface {}{
1124
+ "apiVersion" : "v1" ,
1125
+ "kind" : "Secret" ,
1126
+ "metadata" : map [string ]interface {}{
1127
+ "name" : "test-secret" ,
1128
+ "annotations" : map [string ]interface {}{"token/value" : "secret" , "app" : "test" },
1129
+ },
1130
+ "type" : "Opaque" ,
1131
+ },
1132
+ }
1133
+ targetUn := & unstructured.Unstructured {
1134
+ Object : map [string ]interface {}{
1135
+ "apiVersion" : "v1" ,
1136
+ "kind" : "Secret" ,
1137
+ "metadata" : map [string ]interface {}{
1138
+ "name" : "test-secret" ,
1139
+ "annotations" : map [string ]interface {}{"token/value" : "new-secret" , "app" : "test" },
1140
+ },
1141
+ "type" : "Opaque" ,
1142
+ },
1143
+ }
1144
+
1145
+ liveUn = remarshal (liveUn , applyOptions (diffOptionsForTest ()))
1146
+ targetUn = remarshal (targetUn , applyOptions (diffOptionsForTest ()))
1147
+
1148
+ target , live , err := HideSecretData (targetUn , liveUn , hideAnnots )
1149
+ require .NoError (t , err )
1150
+
1151
+ liveAnnots := live .GetAnnotations ()
1152
+ v , found := liveAnnots ["token/value" ]
1153
+ assert .True (t , found )
1154
+ assert .Equal (t , replacement2 , v )
1155
+
1156
+ targetAnnots := target .GetAnnotations ()
1157
+ v , found = targetAnnots ["token/value" ]
1158
+ assert .True (t , found )
1159
+ assert .Equal (t , replacement1 , v )
1160
+ }
1161
+
1016
1162
func getTargetSecretJsonBytes () []byte {
1017
1163
return []byte (`
1018
1164
{
@@ -1078,7 +1224,7 @@ func TestHideSecretDataHandleEmptySecret(t *testing.T) {
1078
1224
liveSecret := bytesToUnstructured (t , getLiveSecretJsonBytes ())
1079
1225
1080
1226
// when
1081
- target , live , err := HideSecretData (targetSecret , liveSecret )
1227
+ target , live , err := HideSecretData (targetSecret , liveSecret , nil )
1082
1228
1083
1229
// then
1084
1230
assert .NoError (t , err )
@@ -1096,7 +1242,7 @@ func TestHideSecretDataLastAppliedConfig(t *testing.T) {
1096
1242
require .NoError (t , err )
1097
1243
liveSecret .SetAnnotations (map [string ]string {corev1 .LastAppliedConfigAnnotation : string (lastAppliedStr )})
1098
1244
1099
- target , live , err := HideSecretData (targetSecret , liveSecret )
1245
+ target , live , err := HideSecretData (targetSecret , liveSecret , nil )
1100
1246
require .NoError (t , err )
1101
1247
err = json .Unmarshal ([]byte (live .GetAnnotations ()[corev1 .LastAppliedConfigAnnotation ]), & lastAppliedSecret )
1102
1248
require .NoError (t , err )
0 commit comments