Skip to content

Commit a523551

Browse files
authored
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
1 parent d73d5c6 commit a523551

File tree

33 files changed

+502
-154
lines changed

33 files changed

+502
-154
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ sbom/
4141

4242
# Ignore generated support bundles
4343
*.tar.gz
44+
!testdata/supportbundle/*.tar.gz

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ endef
3535

3636
BUILDFLAGS = -tags "netgo containers_image_ostree_stub exclude_graphdriver_devicemapper exclude_graphdriver_btrfs containers_image_openpgp" -installsuffix netgo
3737

38-
all: test support-bundle preflight collect
38+
all: test support-bundle preflight collect analyze
3939

4040
.PHONY: ffi
4141
ffi: fmt vet
@@ -202,8 +202,8 @@ scan:
202202

203203
.PHONY: lint
204204
lint:
205-
golangci-lint run -c .golangci.yaml
205+
golangci-lint run --new -c .golangci.yaml pkg/... cmd/...
206206

207207
.PHONY: lint-and-fix
208208
lint-and-fix:
209-
golangci-lint run --fix -c .golangci.yaml
209+
golangci-lint run --new --fix -c .golangci.yaml pkg/... cmd/...

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ To run a sample preflight check from a sample application, install the preflight
1212
```
1313
curl https://krew.sh/preflight | bash
1414
```
15-
and run, where https://preflight.replicated.com provides an **example** preflight spec:
16-
15+
and run, where https://preflight.replicated.com provides an **example** preflight spec:
16+
1717
```
1818
kubectl preflight https://preflight.replicated.com
1919
```
2020

21-
**NOTE** this is an example. Do **not** use to validate real scenarios.
21+
**NOTE** this is an example. Do **not** use to validate real scenarios.
2222

2323
For more details on creating the custom resource files that drive preflight checks, visit [creating preflight checks](https://troubleshoot.sh/docs/preflight/introduction/).
2424

@@ -31,13 +31,13 @@ To collect a sample support bundle, install the troubleshoot kubectl plugin:
3131
```
3232
curl https://krew.sh/support-bundle | bash
3333
```
34-
and run, where https://support-bundle.replicated.com provides an **example** support bundle spec:
35-
34+
and run, where https://support-bundle.replicated.com provides an **example** support bundle spec:
35+
3636
```
3737
kubectl support-bundle https://support-bundle.replicated.com
3838
```
3939

40-
**NOTE** this is an example. Do **not** use to validate real scenarios.
40+
**NOTE** this is an example. Do **not** use to validate real scenarios.
4141

4242
For more details on creating the custom resource files that drive support-bundle collection, visit [creating collectors](https://troubleshoot.sh/docs/collect/) and [creating analyzers](https://troubleshoot.sh/docs/analyze/).
4343

@@ -47,9 +47,9 @@ And see our other tool [sbctl](https://github.com/replicatedhq/sbctl) that makes
4747

4848
For questions about using Troubleshoot, there's a [Replicated Community](https://help.replicated.com/community) forum, and a [#app-troubleshoot channel in Kubernetes Slack](https://kubernetes.slack.com/channels/app-troubleshoot).
4949

50-
# Software Bill of Materials
51-
A signed SBOM that includes Troubleshoot dependencies is included in each release.
52-
- **troubleshoot-sbom.tgz** contains a software bill of materials for Troubleshoot.
50+
# Software Bill of Materials
51+
A signed SBOM that includes Troubleshoot dependencies is included in each release.
52+
- **troubleshoot-sbom.tgz** contains a software bill of materials for Troubleshoot.
5353
- **troubleshoot-sbom.tgz.sig** is the digital signature for troubleshoot-sbom.tgz
5454
- **key.pub** is the public key from the key pair used to sign troubleshoot-sbom.tgz
5555

cmd/collect/cli/run.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,15 @@ func runCollect(v *viper.Viper, arg string) error {
9191
troubleshootclientsetscheme.AddToScheme(scheme.Scheme)
9292
decode := scheme.Codecs.UniversalDeserializer().Decode
9393

94-
additionalRedactors := &troubleshootv1beta2.Redactor{}
95-
for idx, redactor := range v.GetStringSlice("redactors") {
96-
redactorObj, err := supportbundle.GetRedactorFromURI(redactor)
97-
if err != nil {
98-
return errors.Wrapf(err, "failed to get redactor spec %s, #%d", redactor, idx)
99-
}
94+
redactors, err := supportbundle.GetRedactorsFromURIs(v.GetStringSlice("redactors"))
95+
if err != nil {
96+
return errors.Wrap(err, "failed to get redactors")
97+
}
10098

101-
if redactorObj != nil {
102-
additionalRedactors.Spec.Redactors = append(additionalRedactors.Spec.Redactors, redactorObj.Spec.Redactors...)
103-
}
99+
additionalRedactors := &troubleshootv1beta2.Redactor{
100+
Spec: troubleshootv1beta2.RedactorSpec{
101+
Redactors: redactors,
102+
},
104103
}
105104

106105
for i, additionalDoc := range multidocs {

cmd/troubleshoot/cli/redact.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
}

cmd/troubleshoot/cli/root.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
func RootCmd() *cobra.Command {
1717
cmd := &cobra.Command{
18-
Use: "support-bundle [url]",
18+
Use: "support-bundle [urls...]",
1919
Args: cobra.MinimumNArgs(0),
2020
Short: "Generate a support bundle",
2121
Long: `A support bundle is an archive of files, output, metrics and state
@@ -40,6 +40,7 @@ from a server that can be used to assist when troubleshooting a Kubernetes clust
4040
cobra.OnInitialize(initConfig)
4141

4242
cmd.AddCommand(Analyze())
43+
cmd.AddCommand(Redact())
4344
cmd.AddCommand(VersionCmd())
4445

4546
cmd.Flags().StringSlice("redactors", []string{}, "names of the additional redactors to use")

cmd/troubleshoot/cli/run.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,16 +208,11 @@ func runTroubleshoot(v *viper.Viper, arg []string) error {
208208
return errors.New("no collectors specified in support bundle")
209209
}
210210

211-
for idx, redactor := range v.GetStringSlice("redactors") {
212-
redactorObj, err := supportbundle.GetRedactorFromURI(redactor)
213-
if err != nil {
214-
return errors.Wrapf(err, "failed to get redactor spec %s, #%d", redactor, idx)
215-
}
216-
217-
if redactorObj != nil {
218-
additionalRedactors.Spec.Redactors = append(additionalRedactors.Spec.Redactors, redactorObj.Spec.Redactors...)
219-
}
211+
redactors, err := supportbundle.GetRedactorsFromURIs(v.GetStringSlice("redactors"))
212+
if err != nil {
213+
return errors.Wrap(err, "failed to get redactors")
220214
}
215+
additionalRedactors.Spec.Redactors = append(additionalRedactors.Spec.Redactors, redactors...)
221216

222217
var collectorCB func(chan interface{}, string)
223218
progressChan := make(chan interface{}) // non-zero buffer can result in missed messages

cmd/troubleshoot/cli/version.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88

99
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
10+
"github.com/replicatedhq/troubleshoot/pkg/constants"
1011
"github.com/replicatedhq/troubleshoot/pkg/version"
1112
"github.com/spf13/cobra"
1213
"gopkg.in/yaml.v2"
@@ -26,8 +27,6 @@ func VersionCmd() *cobra.Command {
2627
return cmd
2728
}
2829

29-
const VersionFilename = "version.yaml"
30-
3130
func writeVersionFile(path string) error {
3231
version := troubleshootv1beta2.SupportBundleVersion{
3332
ApiVersion: "troubleshoot.sh/v1beta2",
@@ -41,7 +40,7 @@ func writeVersionFile(path string) error {
4140
return err
4241
}
4342

44-
filename := filepath.Join(path, VersionFilename)
43+
filename := filepath.Join(path, constants.VersionFilename)
4544
err = ioutil.WriteFile(filename, b, 0644)
4645
if err != nil {
4746
return err

docs/preflight.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ preflight [url] [flags]
1717
--as string Username to impersonate for the operation. User could be a regular user or a service account in a namespace.
1818
--as-group stringArray Group to impersonate for the operation, this flag can be repeated to specify multiple groups.
1919
--as-uid string UID to impersonate for the operation.
20-
--cache-dir string Default cache directory (default "/Users/xavpaice/.kube/cache")
20+
--cache-dir string Default cache directory (default "$HOME/.kube/cache")
2121
--certificate-authority string Path to a cert file for the certificate authority
2222
--client-certificate string Path to a client certificate file for TLS
2323
--client-key string Path to a client key file for TLS
@@ -48,4 +48,4 @@ preflight [url] [flags]
4848

4949
* [preflight version](preflight_version.md) - Print the current version and exit
5050

51-
###### Auto generated by spf13/cobra on 21-Nov-2022
51+
###### Auto generated by spf13/cobra on 22-Dec-2022

docs/preflight_version.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ preflight version [flags]
3535

3636
* [preflight](preflight.md) - Run and retrieve preflight checks in a cluster
3737

38-
###### Auto generated by spf13/cobra on 21-Nov-2022
38+
###### Auto generated by spf13/cobra on 22-Dec-2022

0 commit comments

Comments
 (0)