|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/pkg/errors" |
| 9 | + analyzer "github.com/replicatedhq/troubleshoot/pkg/analyze" |
| 10 | + "github.com/replicatedhq/troubleshoot/pkg/collect" |
| 11 | + "github.com/replicatedhq/troubleshoot/pkg/logger" |
| 12 | + "github.com/replicatedhq/troubleshoot/pkg/supportbundle" |
| 13 | + "github.com/spf13/cobra" |
| 14 | + "github.com/spf13/viper" |
| 15 | +) |
| 16 | + |
| 17 | +func Redact() *cobra.Command { |
| 18 | + cmd := &cobra.Command{ |
| 19 | + Use: "redact [urls...]", |
| 20 | + Args: cobra.MinimumNArgs(1), // TODO |
| 21 | + Short: "Redact information from a generated support bundle archive", |
| 22 | + Long: `Redaction is the process of masking sensitive information from collected data in a support bundle. |
| 23 | +This is done using rules defined in the list of redactor manifests provided in the [urls...] command line |
| 24 | +argument. Default built in redactors will also be run, but these would have been run when the support |
| 25 | +bundle was generated. After redaction, the support bundle is archived once more. The resulting file will |
| 26 | +be stored in the current directory in the path provided by the --output flag. |
| 27 | +
|
| 28 | +The [urls...] argument is a list of either oci://.., http://.., https://.. or local paths to yaml files. |
| 29 | +
|
| 30 | +For more information on redactors visit https://troubleshoot.sh/docs/redact/ |
| 31 | + `, |
| 32 | + PreRunE: func(cmd *cobra.Command, args []string) error { |
| 33 | + return viper.BindPFlags(cmd.Flags()) |
| 34 | + }, |
| 35 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 36 | + v := viper.GetViper() |
| 37 | + |
| 38 | + logger.SetQuiet(v.GetBool("quiet")) |
| 39 | + |
| 40 | + // 1. Decode redactors from provided URLs |
| 41 | + redactors, err := supportbundle.GetRedactorsFromURIs(args) |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + |
| 46 | + // 2. Download the bundle and extract it |
| 47 | + tmpDir, bundleDir, err := analyzer.DownloadAndExtractSupportBundle(v.GetString("bundle")) |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + defer os.RemoveAll(tmpDir) |
| 52 | + |
| 53 | + // 3. Represent bundle as a CollectorResult |
| 54 | + collectorResult, err := collect.CollectorResultFromBundle(bundleDir) |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + |
| 59 | + // 4. Perform redaction on the bundle |
| 60 | + err = collect.RedactResult(bundleDir, collectorResult, redactors) |
| 61 | + if err != nil { |
| 62 | + return errors.Wrap(err, "failed to redact support bundle") |
| 63 | + } |
| 64 | + |
| 65 | + // 5. Compress the bundle once more after redacting |
| 66 | + output := v.GetString("output") |
| 67 | + if output == "" { |
| 68 | + output = fmt.Sprintf("redacted-support-bundle-%s.tar.gz", time.Now().Format("2006-01-02T15_04_05")) |
| 69 | + } |
| 70 | + err = collectorResult.ArchiveSupportBundle(bundleDir, output) |
| 71 | + if err != nil { |
| 72 | + return errors.Wrap(err, "failed to create support bundle archive") |
| 73 | + } |
| 74 | + fmt.Println("Redacted support bundle:", output) |
| 75 | + return nil |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + cmd.Flags().String("bundle", "", "file path of the support bundle archive to redact") |
| 80 | + cmd.MarkFlagRequired("bundle") |
| 81 | + cmd.Flags().BoolP("quiet", "q", false, "enable/disable error messaging and only show parseable output") |
| 82 | + 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\")") |
| 83 | + |
| 84 | + return cmd |
| 85 | +} |
0 commit comments