From fb2f028fb5eddcb6285cfce893d5a36a4d513321 Mon Sep 17 00:00:00 2001 From: Andrew Lavery Date: Wed, 20 May 2020 10:28:46 -0400 Subject: [PATCH] add methods to get and clear redactions --- .../troubleshoot/v1beta1/collector_types.go | 5 +-- pkg/redact/literal.go | 2 +- pkg/redact/multi_line.go | 2 +- pkg/redact/redact.go | 35 +++++++++++++++---- pkg/redact/single_line.go | 2 +- pkg/redact/yaml.go | 2 +- 6 files changed, 35 insertions(+), 13 deletions(-) diff --git a/pkg/apis/troubleshoot/v1beta1/collector_types.go b/pkg/apis/troubleshoot/v1beta1/collector_types.go index f193b3b7..fe91e1f4 100644 --- a/pkg/apis/troubleshoot/v1beta1/collector_types.go +++ b/pkg/apis/troubleshoot/v1beta1/collector_types.go @@ -21,8 +21,9 @@ import ( ) type ResultRequest struct { - URI string `json:"uri" yaml:"uri"` - Method string `json:"method" yaml:"method"` + URI string `json:"uri" yaml:"uri"` + Method string `json:"method" yaml:"method"` + RedactURI string `json:"redactUri" yaml:"redactUri"` // the URI to POST redaction reports to } type AfterCollection struct { diff --git a/pkg/redact/literal.go b/pkg/redact/literal.go index e9893ded..56a041d5 100644 --- a/pkg/redact/literal.go +++ b/pkg/redact/literal.go @@ -53,7 +53,7 @@ func (r literalRedactor) Redact(input io.Reader) io.Reader { } if clean != line { - go addRedaction(Redaction{ + addRedaction(Redaction{ RedactorName: r.redactName, CharactersRemoved: len(line) - len(clean), Line: lineNum, diff --git a/pkg/redact/multi_line.go b/pkg/redact/multi_line.go index fd324116..8209ba39 100644 --- a/pkg/redact/multi_line.go +++ b/pkg/redact/multi_line.go @@ -70,7 +70,7 @@ func (r *MultiLineRedactor) Redact(input io.Reader) io.Reader { // if clean is not equal to line2, a redaction was performed if clean != line2 { - go addRedaction(Redaction{ + addRedaction(Redaction{ RedactorName: r.redactName, CharactersRemoved: len(line2) - len(clean), Line: lineNum, diff --git a/pkg/redact/redact.go b/pkg/redact/redact.go index b95d8a9a..bddde7ea 100644 --- a/pkg/redact/redact.go +++ b/pkg/redact/redact.go @@ -20,6 +20,7 @@ const ( var allRedactions RedactionList var redactionListMut sync.Mutex +var pendingRedactions sync.WaitGroup func init() { allRedactions = RedactionList{ @@ -45,13 +46,6 @@ type Redaction struct { File string } -func addRedaction(redaction Redaction) { - redactionListMut.Lock() - defer redactionListMut.Unlock() - allRedactions.ByRedactor[redaction.RedactorName] = append(allRedactions.ByRedactor[redaction.RedactorName], redaction) - allRedactions.ByFile[redaction.File] = append(allRedactions.ByFile[redaction.File], redaction) -} - func Redact(input []byte, path string, additionalRedactors []*troubleshootv1beta1.Redact) ([]byte, error) { redactors, err := getRedactors(path) if err != nil { @@ -77,6 +71,22 @@ func Redact(input []byte, path string, additionalRedactors []*troubleshootv1beta return redacted, nil } +func GetRedactionList() RedactionList { + pendingRedactions.Wait() + redactionListMut.Lock() + defer redactionListMut.Unlock() + return allRedactions +} + +func ResetRedactionList() { + redactionListMut.Lock() + defer redactionListMut.Unlock() + allRedactions = RedactionList{ + ByRedactor: map[string][]Redaction{}, + ByFile: map[string][]Redaction{}, + } +} + func buildAdditionalRedactors(path string, redacts []*troubleshootv1beta1.Redact) ([]Redactor, error) { additionalRedactors := []Redactor{} for i, redact := range redacts { @@ -281,6 +291,17 @@ func readLine(r *bufio.Reader) (string, error) { return string(completeLine), nil } +func addRedaction(redaction Redaction) { + pendingRedactions.Add(1) + go func(redaction Redaction) { + redactionListMut.Lock() + defer redactionListMut.Unlock() + defer pendingRedactions.Done() + allRedactions.ByRedactor[redaction.RedactorName] = append(allRedactions.ByRedactor[redaction.RedactorName], redaction) + allRedactions.ByFile[redaction.File] = append(allRedactions.ByFile[redaction.File], redaction) + }(redaction) +} + func redactorName(redactorNum, withinRedactorNum int, redactorName, redactorType, redactorLiteral string) string { if redactorName != "" { return fmt.Sprintf("%s-%d", redactorName, withinRedactorNum) diff --git a/pkg/redact/single_line.go b/pkg/redact/single_line.go index 0b01f186..3a43151f 100644 --- a/pkg/redact/single_line.go +++ b/pkg/redact/single_line.go @@ -62,7 +62,7 @@ func (r *SingleLineRedactor) Redact(input io.Reader) io.Reader { // if clean is not equal to line, a redaction was performed if clean != line { - go addRedaction(Redaction{ + addRedaction(Redaction{ RedactorName: r.redactName, CharactersRemoved: len(line) - len(clean), Line: lineNum, diff --git a/pkg/redact/yaml.go b/pkg/redact/yaml.go index 0c3b4883..a88c1e13 100644 --- a/pkg/redact/yaml.go +++ b/pkg/redact/yaml.go @@ -64,7 +64,7 @@ func (r *YamlRedactor) Redact(input io.Reader) io.Reader { buf := bytes.NewBuffer(newBytes) buf.WriteTo(writer) - go addRedaction(Redaction{ + addRedaction(Redaction{ RedactorName: r.redactName, CharactersRemoved: len(doc) - len(newBytes), Line: 0, // line 0 because we have no way to tell what line was impacted