-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include log text in snapshots (#597)
- Loading branch information
1 parent
9b1a707
commit a25800b
Showing
18 changed files
with
334 additions
and
701 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,41 @@ | ||
package logs | ||
|
||
import ( | ||
"slices" | ||
"sort" | ||
|
||
"github.com/pganalyze/collector/state" | ||
) | ||
|
||
type logRange struct { | ||
start int64 | ||
end int64 // When working with this range, the character at the index of end is *excluded* | ||
} | ||
|
||
const replacementChar = 'X' | ||
|
||
// ReplaceSecrets - Replaces the secrets of the specified kind with the replacement character in the text | ||
func ReplaceSecrets(input []byte, logLines []state.LogLine, filterLogSecret []state.LogSecretKind) []byte { | ||
var goodRanges []logRange | ||
const replacement = "[redacted]" | ||
|
||
func ReplaceSecrets(input []byte, logLines []state.LogLine, filterLogSecret []state.LogSecretKind) { | ||
filterUnidentified := false | ||
for _, k := range filterLogSecret { | ||
if k == state.UnidentifiedLogSecret { | ||
filterUnidentified = true | ||
} | ||
} | ||
|
||
for _, logLine := range logLines { | ||
goodRanges = append(goodRanges, logRange{start: logLine.ByteStart, end: logLine.ByteContentStart}) | ||
if logLine.ReviewedForSecrets { | ||
for idx, logLine := range logLines { | ||
if filterUnidentified && logLines[idx].Classification == 0 { | ||
logLines[idx].Content = replacement + "\n" | ||
} else { | ||
content := input[logLines[idx].ByteContentStart:logLines[idx].ByteEnd] | ||
sort.Slice(logLine.SecretMarkers, func(i, j int) bool { | ||
return logLine.SecretMarkers[i].ByteStart < logLine.SecretMarkers[j].ByteEnd | ||
}) | ||
|
||
// We're creating a good range when we find a filtered secret or the end (everything before is marked as good) | ||
nextIdxToEvaluate := logLine.ByteContentStart | ||
bytesChecked := 0 | ||
offset := 0 | ||
for _, m := range logLine.SecretMarkers { | ||
filter := false | ||
for _, k := range filterLogSecret { | ||
if m.Kind == k { | ||
filter = true | ||
if m.Kind == k && m.ByteStart > bytesChecked { | ||
content = slices.Replace(content, m.ByteStart-offset, m.ByteEnd-offset, []byte(replacement)...) | ||
bytesChecked = m.ByteEnd | ||
offset += (m.ByteEnd - m.ByteStart) - len(replacement) | ||
} | ||
} | ||
if filter { | ||
firstFilteredIdx := logLine.ByteContentStart + int64(m.ByteStart) | ||
goodRanges = append(goodRanges, logRange{start: nextIdxToEvaluate, end: firstFilteredIdx}) | ||
nextIdxToEvaluate = logLine.ByteContentStart + int64(m.ByteEnd) | ||
} | ||
} | ||
// No more markers means the rest of the line is safe | ||
if nextIdxToEvaluate < logLine.ByteEnd { | ||
goodRanges = append(goodRanges, logRange{start: nextIdxToEvaluate, end: logLine.ByteEnd}) | ||
} | ||
} else if !filterUnidentified { | ||
goodRanges = append(goodRanges, logRange{start: logLine.ByteContentStart, end: logLine.ByteEnd}) | ||
} | ||
} | ||
sort.Slice(goodRanges, func(i, j int) bool { | ||
return goodRanges[i].start < goodRanges[j].start | ||
}) | ||
|
||
lastGood := int64(0) | ||
for _, r := range goodRanges { | ||
for i := lastGood; i < r.start; i++ { | ||
input[i] = replacementChar | ||
} | ||
lastGood = r.end | ||
} | ||
if len(goodRanges) > 0 || filterUnidentified { | ||
for i := lastGood; i < int64(len(input)); i++ { | ||
input[i] = replacementChar | ||
logLines[idx].Content = string(content) | ||
} | ||
} | ||
return input | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.