diff --git a/cmd/troubleshoot/cli/redact.go b/cmd/troubleshoot/cli/redact.go index dfa8f1cc9..88ad4357d 100644 --- a/cmd/troubleshoot/cli/redact.go +++ b/cmd/troubleshoot/cli/redact.go @@ -54,7 +54,7 @@ For more information on redactors visit https://troubleshoot.sh/docs/redact/ } // 4. Perform redaction on the bundle - err = collect.RedactResult(bundleDir, collectorResult, redactors) + err = collect.RedactResult(bundleDir, collectorResult, redactors, nil) if err != nil { return errors.Wrap(err, "failed to redact support bundle") } diff --git a/pkg/analyze/node_resources.go b/pkg/analyze/node_resources.go index f375d2899..337c56693 100644 --- a/pkg/analyze/node_resources.go +++ b/pkg/analyze/node_resources.go @@ -48,9 +48,9 @@ func (a *AnalyzeNodeResources) Analyze(getFile getCollectedFileContents, findFil return []*AnalyzeResult{result}, nil } -func (a *AnalyzeNodeResources) analyzeNodeResources(analyzer *troubleshootv1beta2.NodeResources, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) { +func (a *AnalyzeNodeResources) analyzeNodeResources(analyzer *troubleshootv1beta2.NodeResources, getFile getCollectedFileContents) (*AnalyzeResult, error) { - collected, err := getCollectedFileContents(fmt.Sprintf("%s/%s.json", constants.CLUSTER_RESOURCES_DIR, constants.CLUSTER_RESOURCES_NODES)) + collected, err := getFile(fmt.Sprintf("%s/%s.json", constants.CLUSTER_RESOURCES_DIR, constants.CLUSTER_RESOURCES_NODES)) if err != nil { return nil, errors.Wrap(err, "failed to get contents of nodes.json") } diff --git a/pkg/apis/troubleshoot/v1beta2/collector_shared.go b/pkg/apis/troubleshoot/v1beta2/collector_shared.go index 554f732e4..eb22fe240 100644 --- a/pkg/apis/troubleshoot/v1beta2/collector_shared.go +++ b/pkg/apis/troubleshoot/v1beta2/collector_shared.go @@ -14,7 +14,8 @@ import ( type CollectorMeta struct { CollectorName string `json:"collectorName,omitempty" yaml:"collectorName,omitempty"` // +optional - Exclude *multitype.BoolOrString `json:"exclude,omitempty" yaml:"exclude,omitempty"` + Exclude *multitype.BoolOrString `json:"exclude,omitempty" yaml:"exclude,omitempty"` + SkipRedaction bool `json:"skipRedaction,omitempty" yaml:"skipRedaction,omitempty"` } type ClusterInfo struct { diff --git a/pkg/apis/troubleshoot/v1beta2/hostcollector_shared.go b/pkg/apis/troubleshoot/v1beta2/hostcollector_shared.go index 383d6102a..2e1550ddd 100644 --- a/pkg/apis/troubleshoot/v1beta2/hostcollector_shared.go +++ b/pkg/apis/troubleshoot/v1beta2/hostcollector_shared.go @@ -7,7 +7,8 @@ import ( type HostCollectorMeta struct { CollectorName string `json:"collectorName,omitempty" yaml:"collectorName,omitempty"` // +optional - Exclude *multitype.BoolOrString `json:"exclude,omitempty" yaml:"exclude,omitempty"` + Exclude *multitype.BoolOrString `json:"exclude,omitempty" yaml:"exclude,omitempty"` + SkipRedaction bool `json:"skipRedaction,omitempty" yaml:"skipRedaction,omitempty"` } type CPU struct { diff --git a/pkg/collect/ceph.go b/pkg/collect/ceph.go index 734353a26..be1763354 100644 --- a/pkg/collect/ceph.go +++ b/pkg/collect/ceph.go @@ -158,6 +158,10 @@ func (c *CollectCeph) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectCeph) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectCeph) Collect(progressChan chan<- interface{}) (CollectorResult, error) { ctx := context.TODO() diff --git a/pkg/collect/certificates.go b/pkg/collect/certificates.go index 4fff00d7b..b0661b914 100644 --- a/pkg/collect/certificates.go +++ b/pkg/collect/certificates.go @@ -57,6 +57,10 @@ func (c *CollectCertificates) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectCertificates) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectCertificates) Collect(progressChan chan<- interface{}) (CollectorResult, error) { output := NewResult() diff --git a/pkg/collect/cluster_info.go b/pkg/collect/cluster_info.go index 10aa5e6d9..4aefd0c91 100644 --- a/pkg/collect/cluster_info.go +++ b/pkg/collect/cluster_info.go @@ -33,6 +33,10 @@ func (c *CollectClusterInfo) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectClusterInfo) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectClusterInfo) Collect(progressChan chan<- interface{}) (CollectorResult, error) { client, err := kubernetes.NewForConfig(c.ClientConfig) if err != nil { diff --git a/pkg/collect/cluster_resources.go b/pkg/collect/cluster_resources.go index c8e447083..7f2ca6895 100644 --- a/pkg/collect/cluster_resources.go +++ b/pkg/collect/cluster_resources.go @@ -52,6 +52,10 @@ func (c *CollectClusterResources) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectClusterResources) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectClusterResources) Merge(allCollectors []Collector) ([]Collector, error) { var result []Collector uniqueNamespaces := make(map[string]bool) diff --git a/pkg/collect/collectd.go b/pkg/collect/collectd.go index 6cda082bb..99629fbc6 100644 --- a/pkg/collect/collectd.go +++ b/pkg/collect/collectd.go @@ -26,6 +26,10 @@ func (c *CollectCollectd) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectCollectd) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectCollectd) Collect(progressChan chan<- interface{}) (CollectorResult, error) { copyFromHost := &troubleshootv1beta2.CopyFromHost{ CollectorMeta: c.Collector.CollectorMeta, diff --git a/pkg/collect/collector.go b/pkg/collect/collector.go index b8ccbf63d..0a96d28d4 100644 --- a/pkg/collect/collector.go +++ b/pkg/collect/collector.go @@ -18,6 +18,7 @@ import ( type Collector interface { Title() string IsExcluded() (bool, error) + SkipRedaction() bool GetRBACErrors() []error HasRBACErrors() bool CheckRBAC(ctx context.Context, c Collector, collector *troubleshootv1beta2.Collect, clientConfig *rest.Config, namespace string) error @@ -52,7 +53,7 @@ func isExcluded(excludeVal *multitype.BoolOrString) (bool, error) { return parsed, nil } -func GetCollector(collector *troubleshootv1beta2.Collect, bundlePath string, namespace string, clientConfig *rest.Config, client kubernetes.Interface, sinceTime *time.Time) (interface{}, bool) { +func GetCollector(collector *troubleshootv1beta2.Collect, bundlePath string, namespace string, clientConfig *rest.Config, client kubernetes.Interface, sinceTime *time.Time) (Collector, bool) { ctx := context.TODO() diff --git a/pkg/collect/collector_test.go b/pkg/collect/collector_test.go index 9362d4b39..8d578491b 100644 --- a/pkg/collect/collector_test.go +++ b/pkg/collect/collector_test.go @@ -277,8 +277,7 @@ pwd=somethinggoeshere;`, var result CollectorResult - collector, _ := GetCollector(tt.Collect, "", "", nil, nil, nil) - regCollector, _ := collector.(Collector) + regCollector, _ := GetCollector(tt.Collect, "", "", nil, nil, nil) if excluded, err := regCollector.IsExcluded(); !excluded { req.NoError(err) @@ -286,7 +285,7 @@ pwd=somethinggoeshere;`, result, err = regCollector.Collect(nil) req.NoError(err) - err = RedactResult("", result, tt.Redactors) + err = RedactResult("", result, tt.Redactors, nil) req.NoError(err) } @@ -346,8 +345,7 @@ pwd=somethinggoeshere;`, var result CollectorResult - collector, _ := GetCollector(tt.Collect, "", "", nil, nil, nil) - regCollector, _ := collector.(Collector) + regCollector, _ := GetCollector(tt.Collect, "", "", nil, nil, nil) if excluded, err := regCollector.IsExcluded(); !excluded { req.NoError(err) diff --git a/pkg/collect/configmap.go b/pkg/collect/configmap.go index 20e6e7ff7..c9cdf6ca5 100644 --- a/pkg/collect/configmap.go +++ b/pkg/collect/configmap.go @@ -47,6 +47,10 @@ func (c *CollectConfigMap) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectConfigMap) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectConfigMap) Collect(progressChan chan<- interface{}) (CollectorResult, error) { output := NewResult() diff --git a/pkg/collect/copy.go b/pkg/collect/copy.go index 541e31818..e00e018f2 100644 --- a/pkg/collect/copy.go +++ b/pkg/collect/copy.go @@ -37,6 +37,10 @@ func (c *CollectCopy) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectCopy) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + // Copy function gets a file or folder from a container specified in the specs. func (c *CollectCopy) Collect(progressChan chan<- interface{}) (CollectorResult, error) { client, err := kubernetes.NewForConfig(c.ClientConfig) @@ -101,10 +105,8 @@ func copyFilesFromPod(ctx context.Context, dstPath string, clientConfig *restcli req.VersionedParams(&corev1.PodExecOptions{ Command: command, Container: containerName, - Stdin: true, - Stdout: false, + Stdout: true, Stderr: true, - TTY: false, }, parameterCodec) exec, err := remotecommand.NewSPDYExecutor(clientConfig, "POST", req.URL()) @@ -162,11 +164,9 @@ func copyFilesFromPod(ctx context.Context, dstPath string, clientConfig *restcli } var stderr bytes.Buffer - copyError = exec.Stream(remotecommand.StreamOptions{ - Stdin: nil, + copyError = exec.StreamWithContext(ctx, remotecommand.StreamOptions{ Stdout: stdoutWriter, Stderr: &stderr, - Tty: false, }) if copyError != nil { return result, stderr.Bytes(), errors.Wrap(copyError, "failed to stream command output") diff --git a/pkg/collect/copy_from_host.go b/pkg/collect/copy_from_host.go index 0a9ea574c..1b4b06c07 100644 --- a/pkg/collect/copy_from_host.go +++ b/pkg/collect/copy_from_host.go @@ -49,6 +49,10 @@ func (c *CollectCopyFromHost) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectCopyFromHost) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + // copies a file or directory from a host or hosts to include in the bundle. func (c *CollectCopyFromHost) Collect(progressChan chan<- interface{}) (CollectorResult, error) { var namespace string diff --git a/pkg/collect/data.go b/pkg/collect/data.go index 2e79583d4..ec57609c7 100644 --- a/pkg/collect/data.go +++ b/pkg/collect/data.go @@ -28,6 +28,10 @@ func (c *CollectData) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectData) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectData) Collect(progressChan chan<- interface{}) (CollectorResult, error) { bundlePath := filepath.Join(c.Collector.Name, c.Collector.CollectorName) diff --git a/pkg/collect/dns.go b/pkg/collect/dns.go index 9a4f88d6d..35b4d88f0 100644 --- a/pkg/collect/dns.go +++ b/pkg/collect/dns.go @@ -62,6 +62,10 @@ func (c *CollectDNS) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectDNS) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectDNS) Collect(progressChan chan<- interface{}) (CollectorResult, error) { ctx, cancel := context.WithTimeout(c.Context, time.Duration(60*time.Second)) diff --git a/pkg/collect/etcd.go b/pkg/collect/etcd.go index 5a5f0bf20..d265e2347 100644 --- a/pkg/collect/etcd.go +++ b/pkg/collect/etcd.go @@ -51,6 +51,10 @@ func (c *CollectEtcd) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectEtcd) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectEtcd) Collect(progressChan chan<- interface{}) (CollectorResult, error) { debugInstance := etcdDebug{ context: c.Context, diff --git a/pkg/collect/exec.go b/pkg/collect/exec.go index c2002526d..de432b3df 100644 --- a/pkg/collect/exec.go +++ b/pkg/collect/exec.go @@ -34,6 +34,10 @@ func (c *CollectExec) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectExec) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectExec) Collect(progressChan chan<- interface{}) (CollectorResult, error) { if c.Collector.Timeout == "" { return execWithoutTimeout(c.ClientConfig, c.BundlePath, c.Collector) diff --git a/pkg/collect/goldpinger.go b/pkg/collect/goldpinger.go index b7a7eb801..280c49ab7 100644 --- a/pkg/collect/goldpinger.go +++ b/pkg/collect/goldpinger.go @@ -49,6 +49,10 @@ func (c *CollectGoldpinger) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectGoldpinger) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectGoldpinger) Collect(progressChan chan<- interface{}) (CollectorResult, error) { output := NewResult() var results []byte diff --git a/pkg/collect/helm.go b/pkg/collect/helm.go index d3d2477ec..854a02bc1 100644 --- a/pkg/collect/helm.go +++ b/pkg/collect/helm.go @@ -53,6 +53,10 @@ func (c *CollectHelm) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectHelm) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectHelm) Collect(progressChan chan<- interface{}) (CollectorResult, error) { output := NewResult() diff --git a/pkg/collect/host_block_device.go b/pkg/collect/host_block_device.go index b2feccbb4..adeb0a50f 100644 --- a/pkg/collect/host_block_device.go +++ b/pkg/collect/host_block_device.go @@ -45,6 +45,10 @@ func (c *CollectHostBlockDevices) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostBlockDevices) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + func (c *CollectHostBlockDevices) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { cmd := exec.Command("lsblk", "--noheadings", "--bytes", "--pairs", "-o", lsblkColumns) stdout, err := cmd.Output() diff --git a/pkg/collect/host_certificate.go b/pkg/collect/host_certificate.go index c6da05db3..8525ba303 100644 --- a/pkg/collect/host_certificate.go +++ b/pkg/collect/host_certificate.go @@ -30,6 +30,10 @@ func (c *CollectHostCertificate) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostCertificate) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + func (c *CollectHostCertificate) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { var result = KeyPairValid diff --git a/pkg/collect/host_certificates_collection.go b/pkg/collect/host_certificates_collection.go index 3abe98c8e..c7169cf60 100644 --- a/pkg/collect/host_certificates_collection.go +++ b/pkg/collect/host_certificates_collection.go @@ -34,6 +34,10 @@ func (c *CollectHostCertificatesCollection) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostCertificatesCollection) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + func (c *CollectHostCertificatesCollection) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { var results []HostCertificatesCollection diff --git a/pkg/collect/host_cgroup.go b/pkg/collect/host_cgroup.go index b046bd24b..dc4bc4fd3 100644 --- a/pkg/collect/host_cgroup.go +++ b/pkg/collect/host_cgroup.go @@ -44,6 +44,10 @@ func (c *CollectHostCGroups) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostCGroups) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + func (c *CollectHostCGroups) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { // https://man7.org/linux/man-pages/man7/cgroups.7.html // Implementation is based on https://github.com/k0sproject/k0s/blob/main/internal/pkg/sysinfo/probes/linux/cgroups.go diff --git a/pkg/collect/host_collector.go b/pkg/collect/host_collector.go index 855c06ecd..43ed73521 100644 --- a/pkg/collect/host_collector.go +++ b/pkg/collect/host_collector.go @@ -23,6 +23,7 @@ import ( type HostCollector interface { Title() string IsExcluded() (bool, error) + SkipRedaction() bool Collect(progressChan chan<- interface{}) (map[string][]byte, error) } diff --git a/pkg/collect/host_copy.go b/pkg/collect/host_copy.go index 2e7201f13..ea4b59f30 100644 --- a/pkg/collect/host_copy.go +++ b/pkg/collect/host_copy.go @@ -23,6 +23,10 @@ func (c *CollectHostCopy) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostCopy) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + func (c *CollectHostCopy) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { // 1. Construct subdirectory path in the bundle path to copy files into // output.SaveResult() will create the directory if it doesn't exist diff --git a/pkg/collect/host_cpu.go b/pkg/collect/host_cpu.go index 23bb47dfc..e255f55d6 100644 --- a/pkg/collect/host_cpu.go +++ b/pkg/collect/host_cpu.go @@ -33,6 +33,10 @@ func (c *CollectHostCPU) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostCPU) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + func (c *CollectHostCPU) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { cpuInfo := CPUInfo{} diff --git a/pkg/collect/host_disk_usage.go b/pkg/collect/host_disk_usage.go index b9db44b03..b8cf44598 100644 --- a/pkg/collect/host_disk_usage.go +++ b/pkg/collect/host_disk_usage.go @@ -30,6 +30,10 @@ func (c *CollectHostDiskUsage) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostDiskUsage) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + func (c *CollectHostDiskUsage) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { result := map[string][]byte{} diff --git a/pkg/collect/host_dns.go b/pkg/collect/host_dns.go index b8aad47dd..58925c7d1 100644 --- a/pkg/collect/host_dns.go +++ b/pkg/collect/host_dns.go @@ -48,6 +48,10 @@ func (c *CollectHostDNS) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostDNS) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + func (c *CollectHostDNS) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { names := c.hostCollector.Hostnames diff --git a/pkg/collect/host_filesystem_performance.go b/pkg/collect/host_filesystem_performance.go index 36b102025..7c342374f 100644 --- a/pkg/collect/host_filesystem_performance.go +++ b/pkg/collect/host_filesystem_performance.go @@ -51,6 +51,10 @@ func (c *CollectHostFilesystemPerformance) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostFilesystemPerformance) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + func (c *CollectHostFilesystemPerformance) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { return collectHostFilesystemPerformance(c.hostCollector, c.BundlePath) } diff --git a/pkg/collect/host_http.go b/pkg/collect/host_http.go index 6bf2b194c..8742d8a62 100644 --- a/pkg/collect/host_http.go +++ b/pkg/collect/host_http.go @@ -22,6 +22,10 @@ func (c *CollectHostHTTP) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostHTTP) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + func (c *CollectHostHTTP) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { httpCollector := c.hostCollector diff --git a/pkg/collect/host_httploadbalancer.go b/pkg/collect/host_httploadbalancer.go index 5eebea06a..3ebcaa400 100644 --- a/pkg/collect/host_httploadbalancer.go +++ b/pkg/collect/host_httploadbalancer.go @@ -31,6 +31,10 @@ func (c *CollectHostHTTPLoadBalancer) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostHTTPLoadBalancer) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + func (c *CollectHostHTTPLoadBalancer) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { listenAddress := fmt.Sprintf("0.0.0.0:%d", c.hostCollector.Port) diff --git a/pkg/collect/host_ipv4interfaces.go b/pkg/collect/host_ipv4interfaces.go index e417a8955..ce0aaba52 100644 --- a/pkg/collect/host_ipv4interfaces.go +++ b/pkg/collect/host_ipv4interfaces.go @@ -25,6 +25,10 @@ func (c *CollectHostIPV4Interfaces) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostIPV4Interfaces) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + func (c *CollectHostIPV4Interfaces) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { var ipv4Interfaces []net.Interface diff --git a/pkg/collect/host_journald.go b/pkg/collect/host_journald.go index 57dbbffa0..c095e3840 100644 --- a/pkg/collect/host_journald.go +++ b/pkg/collect/host_journald.go @@ -30,6 +30,10 @@ func (c *CollectHostJournald) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostJournald) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + func (c *CollectHostJournald) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { // collector name check diff --git a/pkg/collect/host_kernel_configs.go b/pkg/collect/host_kernel_configs.go index 5260bc5e0..42beab0af 100644 --- a/pkg/collect/host_kernel_configs.go +++ b/pkg/collect/host_kernel_configs.go @@ -41,6 +41,10 @@ func (c *CollectHostKernelConfigs) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostKernelConfigs) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + func (c *CollectHostKernelConfigs) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { kernelRelease, err := getKernelRelease() diff --git a/pkg/collect/host_kernel_modules.go b/pkg/collect/host_kernel_modules.go index 7aef139c9..fad9a6f1e 100644 --- a/pkg/collect/host_kernel_modules.go +++ b/pkg/collect/host_kernel_modules.go @@ -61,6 +61,10 @@ func (c *CollectHostKernelModules) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostKernelModules) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + // Collect the kernel module status from the host. Modules are returned as a // map keyed on the module name used by the kernel, e.g: // diff --git a/pkg/collect/host_memory.go b/pkg/collect/host_memory.go index 56798ee23..6e5517be2 100644 --- a/pkg/collect/host_memory.go +++ b/pkg/collect/host_memory.go @@ -29,6 +29,10 @@ func (c *CollectHostMemory) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostMemory) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + func (c *CollectHostMemory) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { memoryInfo := MemoryInfo{} diff --git a/pkg/collect/host_network_namespace_connectivity.go b/pkg/collect/host_network_namespace_connectivity.go index 71890919d..660c04a38 100644 --- a/pkg/collect/host_network_namespace_connectivity.go +++ b/pkg/collect/host_network_namespace_connectivity.go @@ -112,6 +112,10 @@ func (c *CollectHostNetworkNamespaceConnectivity) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostNetworkNamespaceConnectivity) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + // marshal marshals the network namespace connectivity info into a JSON file, // writes it to the bundle path and returns the file name and the data. func (c *CollectHostNetworkNamespaceConnectivity) marshal(info *NetworkNamespaceConnectivityInfo) (map[string][]byte, error) { diff --git a/pkg/collect/host_os_info.go b/pkg/collect/host_os_info.go index 63346ba5f..91eb901dd 100644 --- a/pkg/collect/host_os_info.go +++ b/pkg/collect/host_os_info.go @@ -38,6 +38,10 @@ func (c *CollectHostOS) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostOS) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + func (c *CollectHostOS) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { infoStat, err := osutils.Info() if err != nil { diff --git a/pkg/collect/host_run.go b/pkg/collect/host_run.go index 7a2e8d37e..d1bd3c445 100644 --- a/pkg/collect/host_run.go +++ b/pkg/collect/host_run.go @@ -38,6 +38,10 @@ func (c *CollectHostRun) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostRun) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + func (c *CollectHostRun) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { var ( cmdOutputTempDir string diff --git a/pkg/collect/host_services.go b/pkg/collect/host_services.go index 01a29e53d..69c10040b 100644 --- a/pkg/collect/host_services.go +++ b/pkg/collect/host_services.go @@ -35,6 +35,10 @@ func (c *CollectHostServices) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostServices) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + func (c *CollectHostServices) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { var devices []ServiceInfo diff --git a/pkg/collect/host_subnetavailable.go b/pkg/collect/host_subnetavailable.go index 2bd72c445..5e7875ee9 100644 --- a/pkg/collect/host_subnetavailable.go +++ b/pkg/collect/host_subnetavailable.go @@ -44,6 +44,10 @@ func (c *CollectHostSubnetAvailable) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostSubnetAvailable) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + func (c *CollectHostSubnetAvailable) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { procNetRoute, err := os.ReadFile("/proc/net/route") if err != nil { diff --git a/pkg/collect/host_sysctl.go b/pkg/collect/host_sysctl.go index f69c4f632..85bd52a60 100644 --- a/pkg/collect/host_sysctl.go +++ b/pkg/collect/host_sysctl.go @@ -34,6 +34,10 @@ func (c *CollectHostSysctl) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostSysctl) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + func (c *CollectHostSysctl) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { klog.V(2).Info("Running sysctl collector") cmd := execCommand("sysctl", "-a") diff --git a/pkg/collect/host_system_package.go b/pkg/collect/host_system_package.go index 8897e2bf5..1f33147e5 100644 --- a/pkg/collect/host_system_package.go +++ b/pkg/collect/host_system_package.go @@ -40,6 +40,10 @@ func (c *CollectHostSystemPackages) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostSystemPackages) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + func (c *CollectHostSystemPackages) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { info := SystemPackagesInfo{} diff --git a/pkg/collect/host_tcp_connect.go b/pkg/collect/host_tcp_connect.go index eaa95b397..c1cb5b49e 100644 --- a/pkg/collect/host_tcp_connect.go +++ b/pkg/collect/host_tcp_connect.go @@ -25,6 +25,10 @@ func (c *CollectHostTCPConnect) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostTCPConnect) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + func (c *CollectHostTCPConnect) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { address := c.hostCollector.Address diff --git a/pkg/collect/host_tcploadbalancer.go b/pkg/collect/host_tcploadbalancer.go index 90c8ae281..926d495c5 100644 --- a/pkg/collect/host_tcploadbalancer.go +++ b/pkg/collect/host_tcploadbalancer.go @@ -24,6 +24,10 @@ func (c *CollectHostTCPLoadBalancer) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostTCPLoadBalancer) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + func (c *CollectHostTCPLoadBalancer) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { listenAddress := fmt.Sprintf("0.0.0.0:%d", c.hostCollector.Port) dialAddress := c.hostCollector.Address diff --git a/pkg/collect/host_tcpportstatus.go b/pkg/collect/host_tcpportstatus.go index 54b6bd45f..b33d7e726 100644 --- a/pkg/collect/host_tcpportstatus.go +++ b/pkg/collect/host_tcpportstatus.go @@ -25,6 +25,10 @@ func (c *CollectHostTCPPortStatus) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostTCPPortStatus) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + func (c *CollectHostTCPPortStatus) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { dialAddress := "" listenAddress := fmt.Sprintf("0.0.0.0:%d", c.hostCollector.Port) diff --git a/pkg/collect/host_time.go b/pkg/collect/host_time.go index 7ecff185e..e0cbaa6f4 100644 --- a/pkg/collect/host_time.go +++ b/pkg/collect/host_time.go @@ -36,6 +36,10 @@ func (c *CollectHostTime) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostTime) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + func (c *CollectHostTime) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { timeInfo := TimeInfo{} diff --git a/pkg/collect/host_udpportstatus.go b/pkg/collect/host_udpportstatus.go index bb0535aee..2a4625de2 100644 --- a/pkg/collect/host_udpportstatus.go +++ b/pkg/collect/host_udpportstatus.go @@ -24,6 +24,10 @@ func (c *CollectHostUDPPortStatus) IsExcluded() (bool, error) { return isExcluded(c.hostCollector.Exclude) } +func (c *CollectHostUDPPortStatus) SkipRedaction() bool { + return c.hostCollector.SkipRedaction +} + func (c *CollectHostUDPPortStatus) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { listenAddress := net.UDPAddr{ IP: net.ParseIP("0.0.0.0"), diff --git a/pkg/collect/http.go b/pkg/collect/http.go index 05fcd93a4..2aaf4dbe0 100644 --- a/pkg/collect/http.go +++ b/pkg/collect/http.go @@ -49,6 +49,10 @@ func (c *CollectHTTP) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectHTTP) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectHTTP) Collect(progressChan chan<- interface{}) (CollectorResult, error) { var response *http.Response var err error diff --git a/pkg/collect/k8s_metrics.go b/pkg/collect/k8s_metrics.go index 6276ab848..e1ddde40c 100644 --- a/pkg/collect/k8s_metrics.go +++ b/pkg/collect/k8s_metrics.go @@ -41,6 +41,10 @@ func (c *CollectMetrics) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectMetrics) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectMetrics) Collect(progressChan chan<- interface{}) (CollectorResult, error) { output := NewResult() resultLists := make(map[string][]custom_metrics.MetricValue) diff --git a/pkg/collect/k8s_node_metrics.go b/pkg/collect/k8s_node_metrics.go index 6eb954794..772b21530 100644 --- a/pkg/collect/k8s_node_metrics.go +++ b/pkg/collect/k8s_node_metrics.go @@ -35,6 +35,10 @@ func (c *CollectNodeMetrics) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectNodeMetrics) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectNodeMetrics) Collect(progressChan chan<- interface{}) (CollectorResult, error) { output := NewResult() nodesMap := c.constructNodesMap() diff --git a/pkg/collect/logs.go b/pkg/collect/logs.go index bf854d103..c1ddd8465 100644 --- a/pkg/collect/logs.go +++ b/pkg/collect/logs.go @@ -37,6 +37,10 @@ func (c *CollectLogs) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectLogs) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectLogs) Collect(progressChan chan<- interface{}) (CollectorResult, error) { client, err := kubernetes.NewForConfig(c.ClientConfig) if err != nil { diff --git a/pkg/collect/longhorn.go b/pkg/collect/longhorn.go index 0a00c5c3f..ecb78996a 100644 --- a/pkg/collect/longhorn.go +++ b/pkg/collect/longhorn.go @@ -48,6 +48,10 @@ func (c *CollectLonghorn) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectLonghorn) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectLonghorn) Collect(progressChan chan<- interface{}) (CollectorResult, error) { ctx := context.TODO() diff --git a/pkg/collect/mssql.go b/pkg/collect/mssql.go index c69840126..022c9730d 100644 --- a/pkg/collect/mssql.go +++ b/pkg/collect/mssql.go @@ -35,6 +35,10 @@ func (c *CollectMssql) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectMssql) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectMssql) Collect(progressChan chan<- interface{}) (CollectorResult, error) { databaseConnection := DatabaseConnection{} diff --git a/pkg/collect/mysql.go b/pkg/collect/mysql.go index f292d56c8..07055a674 100644 --- a/pkg/collect/mysql.go +++ b/pkg/collect/mysql.go @@ -32,6 +32,10 @@ func (c *CollectMysql) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectMysql) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectMysql) Collect(progressChan chan<- interface{}) (CollectorResult, error) { databaseConnection := DatabaseConnection{} diff --git a/pkg/collect/postgres.go b/pkg/collect/postgres.go index b2a82609e..d764c6bd0 100644 --- a/pkg/collect/postgres.go +++ b/pkg/collect/postgres.go @@ -35,6 +35,10 @@ func (c *CollectPostgres) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectPostgres) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectPostgres) createConnectConfig() (*pgx.ConnConfig, error) { if c.Collector.URI == "" { return nil, errors.New("postgres uri cannot be empty") diff --git a/pkg/collect/redact.go b/pkg/collect/redact.go index 095729de8..78139048d 100644 --- a/pkg/collect/redact.go +++ b/pkg/collect/redact.go @@ -22,7 +22,7 @@ import ( // other goroutines for each redactor spec. const MAX_CONCURRENT_REDACTORS = 10 -func RedactResult(bundlePath string, input CollectorResult, additionalRedactors []*troubleshootv1beta2.Redact) error { +func RedactResult(bundlePath string, input CollectorResult, additionalRedactors []*troubleshootv1beta2.Redact, skipRedaction map[string]bool) error { wg := &sync.WaitGroup{} // Error channel to capture errors from goroutines @@ -39,6 +39,10 @@ func RedactResult(bundlePath string, input CollectorResult, additionalRedactors defer wg.Done() defer func() { <-limitCh }() // free up after the function execution has run + if skipRedaction[file] { + return + } + var reader io.Reader var readerCloseFn func() error // Function to close reader if needed if data == nil { @@ -116,7 +120,7 @@ func RedactResult(bundlePath string, input CollectorResult, additionalRedactors return } - err = RedactResult(tmpDir, subResult, additionalRedactors) + err = RedactResult(tmpDir, subResult, additionalRedactors, nil) if err != nil { errorCh <- errors.Wrap(err, "failed to redact file") return diff --git a/pkg/collect/redis.go b/pkg/collect/redis.go index 2a0d11a73..e0fdac30e 100644 --- a/pkg/collect/redis.go +++ b/pkg/collect/redis.go @@ -33,6 +33,10 @@ func (c *CollectRedis) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectRedis) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectRedis) createClient() (*redis.Client, error) { opt, err := redis.ParseURL(c.Collector.URI) if err != nil { diff --git a/pkg/collect/registry.go b/pkg/collect/registry.go index c343ab0e6..47602daad 100644 --- a/pkg/collect/registry.go +++ b/pkg/collect/registry.go @@ -56,6 +56,10 @@ func (c *CollectRegistry) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectRegistry) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectRegistry) Collect(progressChan chan<- interface{}) (CollectorResult, error) { registryInfo := RegistryInfo{ Images: map[string]RegistryImage{}, diff --git a/pkg/collect/remote_collector.go b/pkg/collect/remote_collector.go index 0d77dfe18..7e8af11e1 100644 --- a/pkg/collect/remote_collector.go +++ b/pkg/collect/remote_collector.go @@ -112,7 +112,7 @@ func (c *RemoteCollector) RunCollectorSync(globalRedactors []*troubleshootv1beta return result, nil } - if err = RedactResult("", result, globalRedactors); err != nil { + if err = RedactResult("", result, globalRedactors, nil); err != nil { // Returning result on error to be consistent with local collector. return result, errors.Wrap(err, "failed to redact remote collector results") } diff --git a/pkg/collect/run.go b/pkg/collect/run.go index 629ae1ef1..185c1a5ff 100644 --- a/pkg/collect/run.go +++ b/pkg/collect/run.go @@ -27,6 +27,10 @@ func (c *CollectRun) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectRun) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectRun) Collect(progressChan chan<- interface{}) (CollectorResult, error) { pullPolicy := corev1.PullIfNotPresent if c.Collector.ImagePullPolicy != "" { diff --git a/pkg/collect/run_daemonset.go b/pkg/collect/run_daemonset.go index 3ef57fcc7..4a479ce1d 100644 --- a/pkg/collect/run_daemonset.go +++ b/pkg/collect/run_daemonset.go @@ -44,6 +44,10 @@ func (c *CollectRunDaemonSet) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectRunDaemonSet) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectRunDaemonSet) Collect(progressChan chan<- interface{}) (CollectorResult, error) { ctx := context.Background() diff --git a/pkg/collect/run_pod.go b/pkg/collect/run_pod.go index 64ab86276..2e38dff40 100644 --- a/pkg/collect/run_pod.go +++ b/pkg/collect/run_pod.go @@ -44,6 +44,10 @@ func (c *CollectRunPod) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectRunPod) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectRunPod) Collect(progressChan chan<- interface{}) (result CollectorResult, err error) { ctx := context.Background() result = NewResult() diff --git a/pkg/collect/secret.go b/pkg/collect/secret.go index 7dc139954..6320a00bc 100644 --- a/pkg/collect/secret.go +++ b/pkg/collect/secret.go @@ -44,6 +44,10 @@ func (c *CollectSecret) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectSecret) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectSecret) Collect(progressChan chan<- interface{}) (CollectorResult, error) { output := NewResult() diff --git a/pkg/collect/sonobuoy_results.go b/pkg/collect/sonobuoy_results.go index deb763b69..ac7e8529a 100644 --- a/pkg/collect/sonobuoy_results.go +++ b/pkg/collect/sonobuoy_results.go @@ -46,6 +46,10 @@ func (c *CollectSonobuoyResults) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectSonobuoyResults) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectSonobuoyResults) Collect(progressChan chan<- interface{}) (CollectorResult, error) { namespace := DefaultSonobuoyNamespace if c.Collector.Namespace != "" { diff --git a/pkg/collect/sysctl.go b/pkg/collect/sysctl.go index b93721061..af4add7be 100644 --- a/pkg/collect/sysctl.go +++ b/pkg/collect/sysctl.go @@ -34,6 +34,10 @@ func (c *CollectSysctl) IsExcluded() (bool, error) { return isExcluded(c.Collector.Exclude) } +func (c *CollectSysctl) SkipRedaction() bool { + return c.Collector.SkipRedaction +} + func (c *CollectSysctl) Collect(progressChan chan<- interface{}) (CollectorResult, error) { if c.Collector.Timeout != "" { timeout, err := time.ParseDuration(c.Collector.Timeout) diff --git a/pkg/preflight/collect.go b/pkg/preflight/collect.go index d6ffeefc2..b0eef1612 100644 --- a/pkg/preflight/collect.go +++ b/pkg/preflight/collect.go @@ -184,15 +184,13 @@ func CollectWithContext(ctx context.Context, opts CollectOpts, p *troubleshootv1 allCollectedData := make(map[string][]byte) for _, desiredCollector := range collectSpecs { - if collectorInterface, ok := collect.GetCollector(desiredCollector, opts.BundlePath, opts.Namespace, opts.KubernetesRestConfig, k8sClient, nil); ok { - if collector, ok := collectorInterface.(collect.Collector); ok { - err := collector.CheckRBAC(ctx, collector, desiredCollector, opts.KubernetesRestConfig, opts.Namespace) - if err != nil { - return nil, errors.Wrap(err, "failed to check RBAC for collectors") - } - collectorType := reflect.TypeOf(collector) - allCollectorsMap[collectorType] = append(allCollectorsMap[collectorType], collector) + if collector, ok := collect.GetCollector(desiredCollector, opts.BundlePath, opts.Namespace, opts.KubernetesRestConfig, k8sClient, nil); ok { + err := collector.CheckRBAC(ctx, collector, desiredCollector, opts.KubernetesRestConfig, opts.Namespace) + if err != nil { + return nil, errors.Wrap(err, "failed to check RBAC for collectors") } + collectorType := reflect.TypeOf(collector) + allCollectorsMap[collectorType] = append(allCollectorsMap[collectorType], collector) } } diff --git a/pkg/redact/literal_test.go b/pkg/redact/literal_test.go new file mode 100644 index 000000000..a1509a67a --- /dev/null +++ b/pkg/redact/literal_test.go @@ -0,0 +1,48 @@ +package redact + +import ( + "bytes" + "io" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestLiteralRedactor(t *testing.T) { + tests := []struct { + name string + match string + inputString string + wantString string + wantRedactions RedactionList + }{ + { + name: "Multiple newlines with no match", + match: "secret", + inputString: "no match\n\n no match \n\n", + wantString: "no match\n\n no match \n\n", + wantRedactions: RedactionList{ + ByRedactor: map[string][]Redaction{}, + ByFile: map[string][]Redaction{}, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + req := require.New(t) + ResetRedactionList() + + redactor := literalString([]byte(tt.match), "testfile", tt.name) + outReader := redactor.Redact(bytes.NewReader([]byte(tt.inputString)), "") + + gotBytes, err := io.ReadAll(outReader) + req.NoError(err) + req.Equal(tt.wantString, string(gotBytes)) + + actualRedactions := GetRedactionList() + ResetRedactionList() + req.Equal(tt.wantRedactions, actualRedactions) + }) + } +} \ No newline at end of file diff --git a/pkg/supportbundle/collect.go b/pkg/supportbundle/collect.go index 3866b2859..f78e12e64 100644 --- a/pkg/supportbundle/collect.go +++ b/pkg/supportbundle/collect.go @@ -47,6 +47,7 @@ func runHostCollectors(ctx context.Context, hostCollectors []*troubleshootv1beta var err error var collectResult map[string][]byte + var skipRedaction map[string]bool if opts.RunHostCollectorsInPod { collectResult, err = runRemoteHostCollectors(ctx, hostCollectors, bundlePath, opts) @@ -54,7 +55,7 @@ func runHostCollectors(ctx context.Context, hostCollectors []*troubleshootv1beta return collectResult, err } } else { - collectResult = runLocalHostCollectors(ctx, hostCollectors, bundlePath, opts) + collectResult, skipRedaction = runLocalHostCollectors(ctx, hostCollectors, bundlePath, opts) } // redact result if any @@ -66,7 +67,7 @@ func runHostCollectors(ctx context.Context, hostCollectors []*troubleshootv1beta if opts.Redact { _, span := otel.Tracer(constants.LIB_TRACER_NAME).Start(ctx, "Host collectors") span.SetAttributes(attribute.String("type", "Redactors")) - err := collect.RedactResult(bundlePath, collectResult, globalRedactors) + err := collect.RedactResult(bundlePath, collectResult, globalRedactors, skipRedaction) if err != nil { err = errors.Wrap(err, "failed to redact host collector results") span.SetStatus(codes.Error, err.Error()) @@ -102,15 +103,13 @@ func runCollectors(ctx context.Context, collectors []*troubleshootv1beta2.Collec allCollectedData := make(map[string][]byte) for _, desiredCollector := range collectSpecs { - if collectorInterface, ok := collect.GetCollector(desiredCollector, bundlePath, opts.Namespace, opts.KubernetesRestConfig, k8sClient, opts.SinceTime); ok { - if collector, ok := collectorInterface.(collect.Collector); ok { - err := collector.CheckRBAC(ctx, collector, desiredCollector, opts.KubernetesRestConfig, opts.Namespace) - if err != nil { - return nil, errors.Wrap(err, "failed to check RBAC for collectors") - } - collectorType := reflect.TypeOf(collector) - allCollectorsMap[collectorType] = append(allCollectorsMap[collectorType], collector) + if collector, ok := collect.GetCollector(desiredCollector, bundlePath, opts.Namespace, opts.KubernetesRestConfig, k8sClient, opts.SinceTime); ok { + err := collector.CheckRBAC(ctx, collector, desiredCollector, opts.KubernetesRestConfig, opts.Namespace) + if err != nil { + return nil, errors.Wrap(err, "failed to check RBAC for collectors") } + collectorType := reflect.TypeOf(collector) + allCollectorsMap[collectorType] = append(allCollectorsMap[collectorType], collector) } } @@ -142,6 +141,8 @@ func runCollectors(ctx context.Context, collectors []*troubleshootv1beta2.Collec // move Copy Collectors if any to the end of the execution list allCollectors = collect.EnsureCopyLast(allCollectors) + skipRedaction := map[string]bool{} + for _, collector := range allCollectors { _, span := otel.Tracer(constants.LIB_TRACER_NAME).Start(ctx, collector.Title()) span.SetAttributes(attribute.String("type", reflect.TypeOf(collector).String())) @@ -174,6 +175,7 @@ func runCollectors(ctx context.Context, collectors []*troubleshootv1beta2.Collec for k, v := range result { allCollectedData[k] = v + skipRedaction[k] = collector.SkipRedaction() } span.End() } @@ -189,7 +191,7 @@ func runCollectors(ctx context.Context, collectors []*troubleshootv1beta2.Collec // TODO: Should we record how long each redactor takes? _, span := otel.Tracer(constants.LIB_TRACER_NAME).Start(ctx, "In-cluster collectors") span.SetAttributes(attribute.String("type", "Redactors")) - err := collect.RedactResult(bundlePath, collectResult, globalRedactors) + err := collect.RedactResult(bundlePath, collectResult, globalRedactors, skipRedaction) if err != nil { err := errors.Wrap(err, "failed to redact in cluster collector results") span.SetStatus(codes.Error, err.Error()) @@ -214,7 +216,7 @@ func findFileName(basename, extension string) (string, error) { } name = fmt.Sprintf("%s (%d)", basename, n) - n = n + 1 + n++ } } @@ -228,7 +230,7 @@ func getAnalysisFile(analyzeResults []*analyze.AnalyzeResult) (io.Reader, error) return bytes.NewBuffer(analysis), nil } -func runLocalHostCollectors(ctx context.Context, hostCollectors []*troubleshootv1beta2.HostCollect, bundlePath string, opts SupportBundleCreateOpts) map[string][]byte { +func runLocalHostCollectors(ctx context.Context, hostCollectors []*troubleshootv1beta2.HostCollect, bundlePath string, opts SupportBundleCreateOpts) (map[string][]byte, map[string]bool) { collectSpecs := make([]*troubleshootv1beta2.HostCollect, 0) collectSpecs = append(collectSpecs, hostCollectors...) @@ -242,6 +244,8 @@ func runLocalHostCollectors(ctx context.Context, hostCollectors []*troubleshootv } } + skipRedaction := map[string]bool{} + for _, collector := range collectors { // TODO: Add context to host collectors _, span := otel.Tracer(constants.LIB_TRACER_NAME).Start(ctx, collector.Title()) @@ -264,10 +268,11 @@ func runLocalHostCollectors(ctx context.Context, hostCollectors []*troubleshootv span.End() for k, v := range result { allCollectedData[k] = v + skipRedaction[k] = collector.SkipRedaction() } } - return allCollectedData + return allCollectedData, skipRedaction } // getExecOutputs executes `collect -` with collector data passed to stdin and returns stdout, stderr and error diff --git a/test/e2e/support-bundle/cluster_resources_e2e_test.go b/test/e2e/support-bundle/cluster_resources_e2e_test.go index 8aad8506e..d0854f58c 100644 --- a/test/e2e/support-bundle/cluster_resources_e2e_test.go +++ b/test/e2e/support-bundle/cluster_resources_e2e_test.go @@ -3,11 +3,13 @@ package e2e import ( "bytes" "context" + "encoding/json" "fmt" "os" "os/exec" "testing" + "github.com/replicatedhq/troubleshoot/pkg/convert" "golang.org/x/exp/slices" "sigs.k8s.io/e2e-framework/pkg/envconf" "sigs.k8s.io/e2e-framework/pkg/features" @@ -63,18 +65,33 @@ func TestClusterResources(t *testing.T) { }, } + analysisTests := []struct { + name string + severity convert.Severity + detail string + }{ + { + name: "total.cpu.cores.in.the.cluster.is.2.or.greater", + severity: convert.SeverityDebug, + detail: "There are at least 2 cores in the cluster.", + }, + } + feature := features.New("Cluster Resouces Test"). Assess("check support bundle catch cluster resouces", func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context { - var out bytes.Buffer + var stdout, stderr bytes.Buffer supportBundleName := "cluster-resources" tarPath := fmt.Sprintf("%s.tar.gz", supportBundleName) targetFolder := fmt.Sprintf("%s/cluster-resources/", supportBundleName) - cmd := exec.CommandContext(ctx, sbBinary(), "spec/clusterResources.yaml", "--interactive=false", fmt.Sprintf("-o=%s", supportBundleName)) - cmd.Stdout = &out + cmd := exec.CommandContext(ctx, sbBinary(), "spec/clusterResources.yaml", "-v=2", "--interactive=false", fmt.Sprintf("-o=%s", supportBundleName)) + cmd.Stdout = &stdout + cmd.Stderr = &stderr err := cmd.Run() if err != nil { t.Fatal(err) } + t.Log("Stdout: ", stdout.String()) + t.Log("Stderr: ", stderr.String()) defer func() { err := os.Remove(fmt.Sprintf("%s.tar.gz", supportBundleName)) @@ -105,6 +122,37 @@ func TestClusterResources(t *testing.T) { } } + targetFile := fmt.Sprintf("%s/analysis.json", supportBundleName) + analysisJSON, err := readFileFromTar(tarPath, targetFile) + if err != nil { + t.Fatal(err) + } + t.Log("analysisJSON: ", string(analysisJSON)) + + analysisResults := []convert.Result{} + err = json.Unmarshal(analysisJSON, &analysisResults) + if err != nil { + t.Fatal(err) + } + + for _, test := range analysisTests { + found := false + for _, result := range analysisResults { + if result.Name == test.name { + found = true + if result.Severity != test.severity { + t.Fatalf("Expected severity %s, got %s", test.severity, result.Severity) + } + if result.Insight.Detail != test.detail { + t.Fatalf("Expected detail %s, got %s", test.detail, result.Insight.Detail) + } + } + } + if !found { + t.Fatalf("Expected result %q not found", test.name) + } + } + return ctx }).Feature() testenv.Test(t, feature) diff --git a/test/e2e/support-bundle/spec/clusterResources.yaml b/test/e2e/support-bundle/spec/clusterResources.yaml index 705bde8cc..b0dd7aaf8 100644 --- a/test/e2e/support-bundle/spec/clusterResources.yaml +++ b/test/e2e/support-bundle/spec/clusterResources.yaml @@ -5,3 +5,12 @@ metadata: spec: collectors: - clusterResources: {} + analyzers: + - nodeResources: + checkName: Total CPU Cores in the cluster is 2 or greater + outcomes: + - fail: + when: "sum(cpuCapacity) < 2" + message: "The cluster must contain at least 2 cores." + - pass: + message: "There are at least 2 cores in the cluster."