From cab3fc7f4e79840da757b59b4b01e1d0f131a60a Mon Sep 17 00:00:00 2001 From: Diamon Wiggins <38189728+diamonwiggins@users.noreply.github.com> Date: Fri, 1 Jul 2022 18:57:58 -0400 Subject: [PATCH] Redact Host Collectors in Support Bundles (#614) Add redactors for Host Collectors --- pkg/collect/collector.go | 2 +- pkg/collect/redact.go | 4 ++-- pkg/collect/remote_collector.go | 2 +- pkg/supportbundle/collect.go | 15 ++++++++++++++- pkg/supportbundle/supportbundle.go | 2 +- 5 files changed, 19 insertions(+), 6 deletions(-) diff --git a/pkg/collect/collector.go b/pkg/collect/collector.go index 1b4adafb..f887a704 100644 --- a/pkg/collect/collector.go +++ b/pkg/collect/collector.go @@ -293,7 +293,7 @@ func (c *Collector) RunCollectorSync(clientConfig *rest.Config, client kubernete } if c.Redact { - err = redactResult(c.BundlePath, result, globalRedactors) + err = RedactResult(c.BundlePath, result, globalRedactors) err = errors.Wrap(err, "failed to redact") } diff --git a/pkg/collect/redact.go b/pkg/collect/redact.go index 7489c5f6..00dbb697 100644 --- a/pkg/collect/redact.go +++ b/pkg/collect/redact.go @@ -15,7 +15,7 @@ import ( "github.com/replicatedhq/troubleshoot/pkg/redact" ) -func redactResult(bundlePath string, input CollectorResult, additionalRedactors []*troubleshootv1beta2.Redact) error { +func RedactResult(bundlePath string, input CollectorResult, additionalRedactors []*troubleshootv1beta2.Redact) error { for k, v := range input { var reader io.Reader if v == nil { @@ -46,7 +46,7 @@ func redactResult(bundlePath string, input CollectorResult, additionalRedactors if err != nil { return errors.Wrap(err, "failed to decompress file") } - err = redactResult(tmpDir, subResult, additionalRedactors) + err = RedactResult(tmpDir, subResult, additionalRedactors) if err != nil { return errors.Wrap(err, "failed to redact file") } diff --git a/pkg/collect/remote_collector.go b/pkg/collect/remote_collector.go index 128f9c29..fa250260 100644 --- a/pkg/collect/remote_collector.go +++ b/pkg/collect/remote_collector.go @@ -107,7 +107,7 @@ func (c *RemoteCollector) RunCollectorSync(globalRedactors []*troubleshootv1beta return result, nil } - if err = redactResult("", result, globalRedactors); err != nil { + if err = RedactResult("", result, globalRedactors); err != nil { // Returning result on error to be consistent with local collector. return result, errors.Wrap(err, "failed to redact") } diff --git a/pkg/supportbundle/collect.go b/pkg/supportbundle/collect.go index 7894050d..fbe4564f 100644 --- a/pkg/supportbundle/collect.go +++ b/pkg/supportbundle/collect.go @@ -20,7 +20,7 @@ import ( "k8s.io/client-go/kubernetes" ) -func runHostCollectors(opts SupportBundleCreateOpts, hostCollectors []*troubleshootv1beta2.HostCollect, bundlePath string) (collect.CollectorResult, error) { +func runHostCollectors(hostCollectors []*troubleshootv1beta2.HostCollect, additionalRedactors *troubleshootv1beta2.Redactor, bundlePath string, opts SupportBundleCreateOpts) (collect.CollectorResult, error) { collectSpecs := make([]*troubleshootv1beta2.HostCollect, 0, 0) collectSpecs = append(collectSpecs, hostCollectors...) @@ -54,6 +54,19 @@ func runHostCollectors(opts SupportBundleCreateOpts, hostCollectors []*troublesh collectResult = allCollectedData + globalRedactors := []*troubleshootv1beta2.Redact{} + if additionalRedactors != nil { + globalRedactors = additionalRedactors.Spec.Redactors + } + + if opts.Redact { + err := collect.RedactResult(bundlePath, collectResult, globalRedactors) + if err != nil { + err = errors.Wrap(err, "failed to redact") + return collectResult, err + } + } + return collectResult, nil } diff --git a/pkg/supportbundle/supportbundle.go b/pkg/supportbundle/supportbundle.go index 877aba42..872d087b 100644 --- a/pkg/supportbundle/supportbundle.go +++ b/pkg/supportbundle/supportbundle.go @@ -86,7 +86,7 @@ func CollectSupportBundleFromSpec(spec *troubleshootv1beta2.SupportBundleSpec, a return nil, errors.Wrap(err, "create bundle dir") } - hostFiles, err := runHostCollectors(opts, spec.HostCollectors, bundlePath) + hostFiles, err := runHostCollectors(spec.HostCollectors, additionalRedactors, bundlePath, opts) if err != nil { return nil, errors.Wrap(err, "failed to run host collectors") }