add methods to get and clear redactions

This commit is contained in:
Andrew Lavery
2020-05-20 10:28:46 -04:00
parent d37ace6e51
commit fb2f028fb5
6 changed files with 35 additions and 13 deletions

View File

@@ -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 {

View File

@@ -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,

View File

@@ -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,

View File

@@ -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)

View File

@@ -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,

View File

@@ -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