Skip to content

Commit eecf784

Browse files
committed
annot-exclusion: fix unit test and use a realistic regex in tests
1 parent d6dd61c commit eecf784

File tree

2 files changed

+46
-37
lines changed

2 files changed

+46
-37
lines changed

deploy/charts/venafi-kubernetes-agent/values.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,15 +243,15 @@ config:
243243
# are affected. The objects are still pushed, but the specified annotations
244244
# and labels are removed before being sent to the Venafi Control Plane.
245245
#
246-
# If you would like to exclude annotations keys that contain the word
247-
# `secret`, use the regular expression `.*secret.*`. The leading and ending .*
248-
# are important if you want to filter out keys that contain `secret` anywhere
249-
# in the key string.
246+
# If you would like to exclude annotations keys that contain the word `word`,
247+
# use the regular expression `.*word.*`. The leading and ending .* are
248+
# important if you want to filter out keys that contain `word` anywhere in the
249+
# key string.
250250
#
251251
# Note that the annotation `kubectl.kubernetes.io/last-applied-configuration`
252252
# is already excluded by default, you don't need to exclude it explicitly.
253253
#
254-
# Example: excludeAnnotationKeysRegex: [".*secret.*"]
254+
# Example: excludeAnnotationKeysRegex: ["kapp\.k14s\.io\/original.*"]
255255
excludeAnnotationKeysRegex: []
256256
excludeLabelKeysRegex: []
257257

pkg/datagatherer/k8s/dynamic_test.go

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,6 @@ func TestDynamicGatherer_Fetch(t *testing.T) {
380380
// init the datagatherer's informer with the client
381381
// add/delete resources watched by the data gatherer
382382
// check the expected result
383-
emptyScheme := runtime.NewScheme()
384383
tests := map[string]struct {
385384
config ConfigDynamic
386385
excludeAnnotsKeys []string
@@ -599,31 +598,41 @@ func TestDynamicGatherer_Fetch(t *testing.T) {
599598
},
600599
},
601600
},
602-
"excluded annotations are removed on secrets and CRDs": {
603-
config: ConfigDynamic{GroupVersionResource: schema.GroupVersionResource{Group: "", Version: "v1", Resource: "secrets"}},
604-
excludeAnnotsKeys: []string{".*secret.*"},
605-
addObjects: []runtime.Object{
606-
getObjectAnnot("v1", "Secret", "s0", "n1", map[string]interface{}{"normal-annot": "value"}, nil),
607-
getObjectAnnot("v1", "Secret", "s1", "n1", nil, map[string]interface{}{"normal-label": "value"}),
608-
getObjectAnnot("v1", "Secret", "s2", "n1", map[string]interface{}{"super-secret-annot": "value"}, nil),
609-
getObjectAnnot("v1", "Secret", "s3", "n1", nil, map[string]interface{}{"super-secret-label": "value"}),
610-
611-
getObjectAnnot("route.openshift.io/v1", "Route", "r0", "n1", map[string]interface{}{"normal-annot": "value"}, nil),
612-
getObjectAnnot("route.openshift.io/v1", "Route", "r1", "n1", nil, map[string]interface{}{"normal-label": "value"}),
613-
getObjectAnnot("route.openshift.io/v1", "Route", "r2", "n1", map[string]interface{}{"super-secret-annot": "value"}, nil),
614-
getObjectAnnot("route.openshift.io/v1", "Route", "r3", "n1", nil, map[string]interface{}{"super-secret-label": "value"}),
615-
},
616-
expected: []*api.GatheredResource{
617-
{Resource: getObjectAnnot("v1", "Secret", "s0", "n1", map[string]interface{}{"normal-annot": "value"}, nil)},
618-
{Resource: getObjectAnnot("v1", "Secret", "s1", "n1", nil, map[string]interface{}{"normal-label": "value"})},
619-
{Resource: getObjectAnnot("v1", "Secret", "s2", "n1", nil, nil)},
620-
{Resource: getObjectAnnot("v1", "Secret", "s3", "n1", nil, nil)},
621-
622-
{Resource: getObjectAnnot("route.openshift.io/v1", "Route", "r0", "n1", map[string]interface{}{"normal-annot": "value"}, nil)},
623-
{Resource: getObjectAnnot("route.openshift.io/v1", "Route", "r1", "n1", nil, map[string]interface{}{"normal-label": "value"})},
624-
{Resource: getObjectAnnot("route.openshift.io/v1", "Route", "r2", "n1", nil, nil)},
625-
{Resource: getObjectAnnot("route.openshift.io/v1", "Route", "r3", "n1", nil, nil)},
626-
},
601+
"excluded annotations are removed for unstructured-based gatherers such as secrets": {
602+
config: ConfigDynamic{GroupVersionResource: schema.GroupVersionResource{Group: "", Version: "v1", Resource: "secrets"}},
603+
604+
// To give a realistic regex in this test case, let's use the
605+
// example of the Kapp project that uses four annotations that all
606+
// start with `kapp.k14s.io/original*`. These annotations are
607+
// similar to `kubectl.kubernetes.io/last-applied-configuration` in
608+
// that they may contain sensitive information. From [1], they may
609+
// look like this:
610+
//
611+
// kapp.k14s.io/original: |
612+
// {"apiVersion":"v1","kind":"Secret","spec":{"data": {"password": "cGFzc3dvcmQ=","username": "bXl1c2VybmFtZQ=="}}}
613+
// kapp.k14s.io/original-diff: |
614+
// - type: test
615+
// path: /data
616+
// value:
617+
// password: cygpcGVyUzNjcmV0UEBhc3N3b3JkIQ==
618+
// username: bXl1c2VybmFtZQ==
619+
//
620+
// [1]: https://github.com/carvel-dev/kapp/issues/90#issuecomment-602074356
621+
excludeAnnotsKeys: []string{`kapp\.k14s\.io\/original.*`},
622+
623+
// We haven't found convincing examples of labels that may contain
624+
// sensitive information in the wild, so let's go with a dumb
625+
// example.
626+
excludeLabelKeys: []string{`.*sensitive.*`},
627+
628+
addObjects: []runtime.Object{getObjectAnnot("v1", "Secret", "s0", "n1",
629+
map[string]interface{}{"kapp.k14s.io/original": "foo", "kapp.k14s.io/original-diff": "bar", "normal": "true"},
630+
map[string]interface{}{"is-sensitive-label": "true", "prod": "true"},
631+
)},
632+
expected: []*api.GatheredResource{{Resource: getObjectAnnot("v1", "Secret", "s0", "n1",
633+
map[string]interface{}{"normal": "true"},
634+
map[string]interface{}{"prod": "true"},
635+
)}},
627636
},
628637
}
629638

@@ -632,12 +641,12 @@ func TestDynamicGatherer_Fetch(t *testing.T) {
632641
var wg sync.WaitGroup
633642
ctx := context.Background()
634643
gvrToListKind := map[schema.GroupVersionResource]string{
635-
schema.GroupVersionResource{Group: "foobar", Version: "v1", Resource: "foos"}: "UnstructuredList",
636-
schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"}: "UnstructuredList",
637-
schema.GroupVersionResource{Group: "", Version: "v1", Resource: "secrets"}: "UnstructuredList",
638-
schema.GroupVersionResource{Group: "", Version: "v1", Resource: "namespaces"}: "UnstructuredList",
644+
{Group: "foobar", Version: "v1", Resource: "foos"}: "UnstructuredList",
645+
{Group: "apps", Version: "v1", Resource: "deployments"}: "UnstructuredList",
646+
{Group: "", Version: "v1", Resource: "secrets"}: "UnstructuredList",
647+
{Group: "", Version: "v1", Resource: "namespaces"}: "UnstructuredList",
639648
}
640-
cl := fake.NewSimpleDynamicClientWithCustomListKinds(emptyScheme, gvrToListKind, tc.addObjects...)
649+
cl := fake.NewSimpleDynamicClientWithCustomListKinds(runtime.NewScheme(), gvrToListKind, tc.addObjects...)
641650
// init the datagatherer's informer with the client
642651
dg, err := tc.config.newDataGathererWithClient(ctx, cl, nil)
643652
if err != nil {
@@ -927,7 +936,7 @@ func TestDynamicGathererNativeResources_Fetch(t *testing.T) {
927936
// (would require a lot of changes to the testing func). Ideally we
928937
// should test all native resources such as Service, Deployment,
929938
// Ingress, Namespace, and so on.
930-
"excluded annotations are removed native resources: pods, namespaces, etc": {
939+
"excluded annotations are removed for typed resources gatherers such as pods": {
931940
config: ConfigDynamic{GroupVersionResource: podGVR},
932941
excludeAnnotsKeys: []string{"secret"},
933942
excludeLabelKeys: []string{"secret"},

0 commit comments

Comments
 (0)