Skip to content

Commit 6f453d7

Browse files
committed
Update dynamic DG to use fieldfilter functions
Signed-off-by: Charlie Egan <[email protected]>
1 parent 7ed0f9e commit 6f453d7

File tree

3 files changed

+27
-55
lines changed

3 files changed

+27
-55
lines changed

pkg/datagatherer/k8s/dynamic.go

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -164,69 +164,41 @@ func (g *DataGathererDynamic) Fetch() (interface{}, error) {
164164
}
165165

166166
func redactList(list *unstructured.UnstructuredList) error {
167-
// In principal we could only redact the list if it's kind is SecretList or
168-
// a generic mixed List, however the test suite does not set the list kind
169-
// and it is safer to always check for Secrets.
170167
for i := range list.Items {
171168
// Determine the kind of items in case this is a generic 'mixed' list.
172169
gvks, _, err := scheme.Scheme.ObjectKinds(&list.Items[i])
173170
if err != nil {
174171
return errors.WithStack(err)
175172
}
176173

177-
object := list.Items[i]
174+
resource := list.Items[i]
178175

179176
for _, gvk := range gvks {
180177
// If this item is a Secret then we need to redact it.
181178
if gvk.Kind == "Secret" && (gvk.Group == "core" || gvk.Group == "") {
179+
Select([]string{
180+
"kind",
181+
"apiVersion",
182+
"metadata.name",
183+
"metadata.namespace",
184+
"type",
185+
"/data/tls.crt",
186+
"/data/ca.crt",
187+
}, &resource)
182188

183-
// If the secret is a tls secret, we redact all data other then
184-
// the tls.crt and ca.crt. This is because we need to inspect
185-
// the certificate to make recommendations.
186-
if object.Object["type"] == "kubernetes.io/tls" {
187-
secretData, ok := object.Object["data"].(map[string]interface{})
188-
if ok {
189-
for k := range secretData {
190-
// Only these two keys will be sent, all others are
191-
// deleted
192-
if k != "tls.crt" && k != "ca.crt" {
193-
delete(secretData, k)
194-
}
195-
}
196-
} else {
197-
// If secret is not string mapping, redact all secret data
198-
object.Object["data"] = map[string]interface{}{}
199-
}
200-
} else {
201-
// Redact all secret data for non-tls secrets
202-
object.Object["data"] = map[string]interface{}{}
203-
}
204-
205-
metadata, metadataPresent := object.Object["metadata"].(map[string]interface{})
206-
if metadataPresent {
207-
// Redact last-applied-configuration annotation if set
208-
annotations, present := metadata["annotations"].(map[string]interface{})
209-
if present {
210-
_, annotationPresent := annotations["kubectl.kubernetes.io/last-applied-configuration"]
211-
if annotationPresent {
212-
annotations["kubectl.kubernetes.io/last-applied-configuration"] = "redacted"
213-
}
214-
metadata["annotations"] = annotations
215-
}
216-
}
217189
// break when the object has been processed as a secret, no
218190
// other kinds have redact modifications
219191
break
220192
}
221193

222-
metadata, metadataPresent := object.Object["metadata"].(map[string]interface{})
223-
if metadataPresent {
224-
// Drop managed fields if set
225-
if _, present := metadata["managedFields"]; present {
226-
delete(metadata, "managedFields")
227-
}
228-
}
194+
// remove managedFields from all resources
195+
Redact([]string{
196+
"metadata.managedFields",
197+
}, &resource)
229198
}
199+
200+
// update the object in the list
201+
list.Items[i] = resource
230202
}
231203
return nil
232204
}

pkg/datagatherer/k8s/dynamic_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ func getObject(version, kind, name, namespace string, withManagedFields bool) *u
4141

4242
func getSecret(name, namespace string, data map[string]interface{}, isTLS bool, withLastApplied bool) *unstructured.Unstructured {
4343
object := getObject("v1", "Secret", name, namespace, false)
44-
object.Object["data"] = data
44+
45+
if data != nil {
46+
object.Object["data"] = data
47+
}
4548

4649
object.Object["type"] = "Opaque"
4750
if isTLS {
@@ -56,10 +59,6 @@ func getSecret(name, namespace string, data map[string]interface{}, isTLS bool,
5659
metadata["annotations"] = map[string]interface{}{
5760
"kubectl.kubernetes.io/last-applied-configuration": string(jsonData),
5861
}
59-
} else { // generate an expected redacted secret
60-
metadata["annotations"] = map[string]interface{}{
61-
"kubectl.kubernetes.io/last-applied-configuration": "redacted",
62-
}
6362
}
6463

6564
return object
@@ -164,8 +163,8 @@ func TestDynamicGatherer_Fetch(t *testing.T) {
164163
}, false, true),
165164
},
166165
expected: asUnstructuredList(
167-
getSecret("testsecret", "testns1", map[string]interface{}{}, false, false),
168-
getSecret("anothertestsecret", "testns2", map[string]interface{}{}, false, false),
166+
getSecret("testsecret", "testns1", nil, false, false),
167+
getSecret("anothertestsecret", "testns2", nil, false, false),
169168
),
170169
},
171170
"Secret of type kubernetes.io/tls should have crts and not keys": {
@@ -188,7 +187,7 @@ func TestDynamicGatherer_Fetch(t *testing.T) {
188187
"ca.crt": "value",
189188
}, true, false),
190189
// all other keys removed
191-
getSecret("anothertestsecret", "testns2", map[string]interface{}{}, true, false),
190+
getSecret("anothertestsecret", "testns2", nil, true, false),
192191
),
193192
},
194193
"Foos in different namespaces should be returned if they are in the namespace list for the gatherer": {
@@ -240,6 +239,9 @@ func TestDynamicGatherer_Fetch(t *testing.T) {
240239
}
241240
if diff, equal := messagediff.PrettyDiff(test.expected, res); !equal {
242241
t.Errorf("\n%s", diff)
242+
expectedJSON, _ := json.MarshalIndent(test.expected, "", " ")
243+
gotJSON, _ := json.MarshalIndent(res, "", " ")
244+
t.Fatalf("unexpected JSON: \ngot \n%s\nwant\n%s", string(gotJSON), expectedJSON)
243245
}
244246
})
245247
}

pkg/datagatherer/k8s/fieldfilter.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ func Redact(fields []string, resource *unstructured.Unstructured) error {
8080
}
8181
}
8282

83-
fmt.Println(jsonParsed.String())
84-
8583
// load the filtered JSON back into the resource
8684
err = json.Unmarshal(jsonParsed.Bytes(), resource)
8785
if err != nil {

0 commit comments

Comments
 (0)