-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(redactors): Run redactors on an existing support bundle (#887)
* feat(redactors): Run redactors on an existing support bundle Add redact subcommand to support-bundle to allow running redactors on an existing bundle to creating a new redacted bundle. The command will be launched like so support-bundle redact <redactor urls> --bundle support-bundle.tar.gz Fixes: #705
- Loading branch information
Showing
33 changed files
with
502 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,4 @@ sbom/ | |
|
||
# Ignore generated support bundles | ||
*.tar.gz | ||
!testdata/supportbundle/*.tar.gz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
analyzer "github.com/replicatedhq/troubleshoot/pkg/analyze" | ||
"github.com/replicatedhq/troubleshoot/pkg/collect" | ||
"github.com/replicatedhq/troubleshoot/pkg/logger" | ||
"github.com/replicatedhq/troubleshoot/pkg/supportbundle" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func Redact() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "redact [urls...]", | ||
Args: cobra.MinimumNArgs(1), // TODO | ||
Short: "Redact information from a generated support bundle archive", | ||
Long: `Redaction is the process of masking sensitive information from collected data in a support bundle. | ||
This is done using rules defined in the list of redactor manifests provided in the [urls...] command line | ||
argument. Default built in redactors will also be run, but these would have been run when the support | ||
bundle was generated. After redaction, the support bundle is archived once more. The resulting file will | ||
be stored in the current directory in the path provided by the --output flag. | ||
The [urls...] argument is a list of either oci://.., http://.., https://.. or local paths to yaml files. | ||
For more information on redactors visit https://troubleshoot.sh/docs/redact/ | ||
`, | ||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||
return viper.BindPFlags(cmd.Flags()) | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
v := viper.GetViper() | ||
|
||
logger.SetQuiet(v.GetBool("quiet")) | ||
|
||
// 1. Decode redactors from provided URLs | ||
redactors, err := supportbundle.GetRedactorsFromURIs(args) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// 2. Download the bundle and extract it | ||
tmpDir, bundleDir, err := analyzer.DownloadAndExtractSupportBundle(v.GetString("bundle")) | ||
if err != nil { | ||
return err | ||
} | ||
defer os.RemoveAll(tmpDir) | ||
|
||
// 3. Represent bundle as a CollectorResult | ||
collectorResult, err := collect.CollectorResultFromBundle(bundleDir) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// 4. Perform redaction on the bundle | ||
err = collect.RedactResult(bundleDir, collectorResult, redactors) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to redact support bundle") | ||
} | ||
|
||
// 5. Compress the bundle once more after redacting | ||
output := v.GetString("output") | ||
if output == "" { | ||
output = fmt.Sprintf("redacted-support-bundle-%s.tar.gz", time.Now().Format("2006-01-02T15_04_05")) | ||
} | ||
err = collectorResult.ArchiveSupportBundle(bundleDir, output) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to create support bundle archive") | ||
} | ||
fmt.Println("Redacted support bundle:", output) | ||
return nil | ||
}, | ||
} | ||
|
||
cmd.Flags().String("bundle", "", "file path of the support bundle archive to redact") | ||
cmd.MarkFlagRequired("bundle") | ||
cmd.Flags().BoolP("quiet", "q", false, "enable/disable error messaging and only show parseable output") | ||
cmd.Flags().StringP("output", "o", "", "file path of where to save the redacted support bundle archive (default \"redacted-support-bundle-YYYY-MM-DDTHH_MM_SS.tar.gz\")") | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
## support-bundle redact | ||
|
||
Redact information from a generated support bundle archive | ||
|
||
### Synopsis | ||
|
||
Redaction is the process of masking sensitive information from collected data in a support bundle. | ||
This is done using rules defined in the list of redactor manifests provided in the [urls...] command line | ||
argument. Default built in redactors will also be run, but these would have been run when the support | ||
bundle was generated. After redaction, the support bundle is archived once more. The resulting file will | ||
be stored in the current directory in the path provided by the --output flag. | ||
|
||
The [urls...] argument is a list of either oci://.., http://.., https://.. or local paths to yaml files. | ||
|
||
For more information on redactors visit https://troubleshoot.sh/docs/redact/ | ||
|
||
|
||
``` | ||
support-bundle redact [urls...] [flags] | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
--bundle string file path of the support bundle archive to redact | ||
-h, --help help for redact | ||
-o, --output string file path of where to save the redacted support bundle archive (default "redacted-support-bundle-YYYY-MM-DDTHH_MM_SS.tar.gz") | ||
-q, --quiet enable/disable error messaging and only show parseable output | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [support-bundle](support-bundle.md) - Generate a support bundle | ||
|
||
###### Auto generated by spf13/cobra on 22-Dec-2022 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
apiVersion: troubleshoot.sh/v1beta2 | ||
kind: Redactor | ||
metadata: | ||
name: e2e-redactor | ||
spec: | ||
redactors: | ||
- name: redact-static-text | ||
removals: | ||
values: | ||
- static |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package testutils | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func GetTestFixture(t *testing.T, path string) string { | ||
t.Helper() | ||
p := filepath.Join("../../testdata", path) | ||
b, err := os.ReadFile(p) | ||
require.NoError(t, err) | ||
return string(b) | ||
} | ||
|
||
// FileDir returns the directory of the current source file. | ||
func FileDir() string { | ||
_, filename, _, _ := runtime.Caller(0) | ||
return filepath.Dir(filename) | ||
} |
Oops, something went wrong.