Skip to content

Commit

Permalink
Merge pull request #216 from replicatedhq/laverya/redactor-reformat
Browse files Browse the repository at this point in the history
redactor reformat
  • Loading branch information
laverya authored Jun 15, 2020
2 parents d98172e + 6e87448 commit 89b4979
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 138 deletions.
23 changes: 15 additions & 8 deletions pkg/apis/troubleshoot/v1beta1/redact_shared.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package v1beta1

type MultiLine struct {
type Regex struct {
Selector string `json:"selector,omitempty" yaml:"selector,omitempty"`
Redactor string `json:"redactor,omitempty" yaml:"redactor,omitempty"`
}

type FileSelector struct {
File string `json:"file,omitempty" yaml:"file,omitempty"`
Files []string `json:"files,omitempty" yaml:"files,omitempty"`
}

type Removals struct {
Values []string `json:"values,omitempty" yaml:"values,omitempty"`
Regex []Regex `json:"regex,omitempty" yaml:"regex,omitempty"`
YamlPath []string `json:"yamlPath,omitempty" yaml:"yamlPath,omitempty"`
}

type Redact struct {
Name string `json:"name,omitempty" yaml:"name,omitempty"`
File string `json:"file,omitempty" yaml:"file,omitempty"`
Files []string `json:"files,omitempty" yaml:"files,omitempty"`
Values []string `json:"values,omitempty" yaml:"values,omitempty"`
Regex []string `json:"regex,omitempty" yaml:"regex,omitempty"`
MultiLine []MultiLine `json:"multiLine,omitempty" yaml:"multiLine,omitempty"`
Yaml []string `json:"yaml,omitempty" yaml:"yaml,omitempty"`
Name string `json:"name,omitempty" yaml:"name,omitempty"`
FileSelector FileSelector `json:"fileSelector,omitempty" yaml:"fileSelector,omitempty"`
Removals Removals `json:"removals,omitempty" yaml:"removals,omitempty"`
}
107 changes: 67 additions & 40 deletions pkg/apis/troubleshoot/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 62 additions & 42 deletions pkg/collect/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ pwd=somethinggoeshere;`,
},
Redactors: []*troubleshootv1beta1.Redact{
{
Name: "",
File: "",
Files: nil,
Values: nil,
Regex: []string{
`abc`,
`(another)(?P<mask>.*)(here)`,
Name: "",
Removals: troubleshootv1beta1.Removals{
Values: nil,
Regex: []troubleshootv1beta1.Regex{
{Redactor: `abc`},
{Redactor: `(another)(?P<mask>.*)(here)`},
},
},
},
},
Expand All @@ -65,11 +65,15 @@ pwd=somethinggoeshere;`,
},
Redactors: []*troubleshootv1beta1.Redact{
{
Name: "",
File: "data/*",
Values: nil,
Regex: []string{
`(another)(?P<mask>.*)(here)`,
Name: "",
FileSelector: troubleshootv1beta1.FileSelector{
File: "data/*",
},
Removals: troubleshootv1beta1.Removals{
Values: nil,
Regex: []troubleshootv1beta1.Regex{
{Redactor: `(another)(?P<mask>.*)(here)`},
},
},
},
},
Expand All @@ -96,11 +100,15 @@ pwd=somethinggoeshere;`,
},
Redactors: []*troubleshootv1beta1.Redact{
{
Name: "",
File: "notdata/*",
Values: nil,
Regex: []string{
`(another)(?P<mask>.*)(here)`,
Name: "",
FileSelector: troubleshootv1beta1.FileSelector{
File: "notdata/*",
},
Removals: troubleshootv1beta1.Removals{
Values: nil,
Regex: []troubleshootv1beta1.Regex{
{Redactor: `(another)(?P<mask>.*)(here)`},
},
},
},
},
Expand Down Expand Up @@ -128,13 +136,17 @@ pwd=somethinggoeshere;`,
Redactors: []*troubleshootv1beta1.Redact{
{
Name: "",
Files: []string{
"notData/*",
"data/*",
FileSelector: troubleshootv1beta1.FileSelector{
Files: []string{
"notData/*",
"data/*",
},
},
Values: nil,
Regex: []string{
`(another)(?P<mask>.*)(here)`,
Removals: troubleshootv1beta1.Removals{
Values: nil,
Regex: []troubleshootv1beta1.Regex{
{Redactor: `(another)(?P<mask>.*)(here)`},
},
},
},
},
Expand Down Expand Up @@ -162,13 +174,17 @@ pwd=somethinggoeshere;`,
Redactors: []*troubleshootv1beta1.Redact{
{
Name: "",
Files: []string{
"data/*/*",
FileSelector: troubleshootv1beta1.FileSelector{
Files: []string{
"data/*/*",
},
},
Values: []string{
`abc`,
`123`,
`another`,
Removals: troubleshootv1beta1.Removals{
Values: []string{
`abc`,
`123`,
`another`,
},
},
},
},
Expand All @@ -194,8 +210,10 @@ another line here`,
},
Redactors: []*troubleshootv1beta1.Redact{
{
Yaml: []string{
`abc`,
Removals: troubleshootv1beta1.Removals{
YamlPath: []string{
`abc`,
},
},
},
},
Expand Down Expand Up @@ -223,10 +241,12 @@ abc`,
},
Redactors: []*troubleshootv1beta1.Redact{
{
MultiLine: []troubleshootv1beta1.MultiLine{
{
Selector: "abc",
Redactor: "xyz(123)",
Removals: troubleshootv1beta1.Removals{
Regex: []troubleshootv1beta1.Regex{
{
Selector: "abc",
Redactor: "xyz(123)",
},
},
},
},
Expand Down Expand Up @@ -287,13 +307,13 @@ pwd=somethinggoeshere;`,
},
Redactors: []*troubleshootv1beta1.Redact{
{
Name: "",
File: "",
Files: nil,
Values: nil,
Regex: []string{
`abc`,
`(another)(?P<mask>.*)(here)`,
Name: "",
Removals: troubleshootv1beta1.Removals{
Values: nil,
Regex: []troubleshootv1beta1.Regex{
{Redactor: `abc`},
{Redactor: `(another)(?P<mask>.*)(here)`},
},
},
},
},
Expand Down
40 changes: 20 additions & 20 deletions pkg/redact/redact.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,27 +104,27 @@ func buildAdditionalRedactors(path string, redacts []*troubleshootv1beta1.Redact
continue
}

for j, re := range redact.Regex {
r, err := NewSingleLineRedactor(re, MASK_TEXT, path, redactorName(i, j, redact.Name, "regex"), false)
if err != nil {
return nil, errors.Wrapf(err, "redactor %q", re)
}
additionalRedactors = append(additionalRedactors, r)
}

for j, literal := range redact.Values {
for j, literal := range redact.Removals.Values {
additionalRedactors = append(additionalRedactors, literalString(literal, path, redactorName(i, j, redact.Name, "literal")))
}

for j, re := range redact.MultiLine {
r, err := NewMultiLineRedactor(re.Selector, re.Redactor, MASK_TEXT, path, redactorName(i, j, redact.Name, "multiLine"), false)
if err != nil {
return nil, errors.Wrapf(err, "multiline redactor %+v", re)
for j, re := range redact.Removals.Regex {
var newRedactor Redactor
if re.Selector != "" {
newRedactor, err = NewMultiLineRedactor(re.Selector, re.Redactor, MASK_TEXT, path, redactorName(i, j, redact.Name, "multiLine"), false)
if err != nil {
return nil, errors.Wrapf(err, "multiline redactor %+v", re)
}
} else {
newRedactor, err = NewSingleLineRedactor(re.Redactor, MASK_TEXT, path, redactorName(i, j, redact.Name, "regex"), false)
if err != nil {
return nil, errors.Wrapf(err, "redactor %q", re)
}
}
additionalRedactors = append(additionalRedactors, r)
additionalRedactors = append(additionalRedactors, newRedactor)
}

for j, yaml := range redact.Yaml {
for j, yaml := range redact.Removals.YamlPath {
r := NewYamlRedactor(yaml, path, redactorName(i, j, redact.Name, "yaml"))
additionalRedactors = append(additionalRedactors, r)
}
Expand All @@ -133,21 +133,21 @@ func buildAdditionalRedactors(path string, redacts []*troubleshootv1beta1.Redact
}

func redactMatchesPath(path string, redact *troubleshootv1beta1.Redact) (bool, error) {
if redact.File == "" && len(redact.Files) == 0 {
if redact.FileSelector.File == "" && len(redact.FileSelector.Files) == 0 {
return true, nil
}

globs := []glob.Glob{}

if redact.File != "" {
newGlob, err := glob.Compile(redact.File, '/')
if redact.FileSelector.File != "" {
newGlob, err := glob.Compile(redact.FileSelector.File, '/')
if err != nil {
return false, errors.Wrapf(err, "invalid file glob string %q", redact.File)
return false, errors.Wrapf(err, "invalid file glob string %q", redact.FileSelector.File)
}
globs = append(globs, newGlob)
}

for i, fileGlobString := range redact.Files {
for i, fileGlobString := range redact.FileSelector.Files {
newGlob, err := glob.Compile(fileGlobString, '/')
if err != nil {
return false, errors.Wrapf(err, "invalid file glob string %d %q", i, fileGlobString)
Expand Down
Loading

0 comments on commit 89b4979

Please sign in to comment.