Skip to content

Commit

Permalink
Redact utility statements by default (#588)
Browse files Browse the repository at this point in the history
  • Loading branch information
seanlinsley authored Aug 29, 2024
1 parent 44486e8 commit 6ea5a56
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 11 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ require (
github.com/fatih/color v1.16.0
github.com/google/uuid v1.6.0
github.com/gorilla/websocket v1.5.1
github.com/pganalyze/pg_query_go/v5 v5.1.0
github.com/pganalyze/pg_query_go/v5 v5.1.1-0.20240829182208-c3a818d346a9
github.com/prometheus/procfs v0.7.3
go.opentelemetry.io/otel v1.19.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ github.com/ogier/pflag v0.0.0-20160129220114-45c278ab3607 h1:db+rES1EpSjP45xOU3h
github.com/ogier/pflag v0.0.0-20160129220114-45c278ab3607/go.mod h1:zkFki7tvTa0tafRvTBIZTvzYyAu6kQhPZFnshFFPE+g=
github.com/papertrail/go-tail v0.0.0-20180509224916-973c153b0431 h1:i1egM7gz4bPxLCIwBJOkpk6TqHpjTnL4dE1xdN/4dcs=
github.com/papertrail/go-tail v0.0.0-20180509224916-973c153b0431/go.mod h1:dMID0RaS2a5rhpOjC4RsAKitU6WGgkFBZnPVffL69b8=
github.com/pganalyze/pg_query_go/v5 v5.1.0 h1:MlxQqHZnvA3cbRQYyIrjxEjzo560P6MyTgtlaf3pmXg=
github.com/pganalyze/pg_query_go/v5 v5.1.0/go.mod h1:FsglvxidZsVN+Ltw3Ai6nTgPVcK2BPukH3jCDEqc1Ug=
github.com/pganalyze/pg_query_go/v5 v5.1.1-0.20240829182208-c3a818d346a9 h1:9aymnVwCh0j7i+Myb1x3g+Xm1JtAk4TrTAyqBwCWhN8=
github.com/pganalyze/pg_query_go/v5 v5.1.1-0.20240829182208-c3a818d346a9/go.mod h1:FsglvxidZsVN+Ltw3Ai6nTgPVcK2BPukH3jCDEqc1Ug=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down
21 changes: 21 additions & 0 deletions logs/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/pganalyze/collector/logs/util"
"github.com/pganalyze/collector/output/pganalyze_collector"
"github.com/pganalyze/collector/state"
pg_query "github.com/pganalyze/pg_query_go/v5"
)

type match struct {
Expand Down Expand Up @@ -2246,6 +2247,9 @@ func AnalyzeBackendLogLines(logLines []state.LogLine) (logLinesOut []state.LogLi

logLine, statementLine, detailLine, contextLine, hintLine, samples = classifyAndSetDetails(logLine, statementLine, detailLine, contextLine, hintLine, samples)

markUtilitySecret(&logLine)
markUtilitySecret(&statementLine)

if statementLineIdx != 0 {
logLines[statementLineIdx] = statementLine
}
Expand All @@ -2270,3 +2274,20 @@ func AnalyzeBackendLogLines(logLines []state.LogLine) (logLinesOut []state.LogLi

return
}

func markUtilitySecret(line *state.LogLine) {
for _, m := range line.SecretMarkers {
if m.Kind == state.StatementTextLogSecret {
query := line.Content[m.ByteStart:m.ByteEnd]
normalized, err := pg_query.NormalizeUtility(query)
if err == nil && query != normalized {
line.SecretMarkers = append(line.SecretMarkers, state.LogSecretMarker{
ByteStart: m.ByteStart,
ByteEnd: m.ByteEnd,
Kind: state.CredentialLogSecret,
})
return
}
}
}
}
28 changes: 28 additions & 0 deletions logs/analyze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,34 @@ var tests = []testpair{
}},
nil,
},
{
[]state.LogLine{{
Content: "duration: 3205.800 ms execute a2: CREATE ROLE postgres PASSWORD 'xyz'\n",
LogLevel: pganalyze_collector.LogLineInformation_LOG,
}},
[]state.LogLine{{
Query: "CREATE ROLE postgres PASSWORD 'xyz'",
LogLevel: pganalyze_collector.LogLineInformation_LOG,
Classification: pganalyze_collector.LogLineInformation_STATEMENT_DURATION,
ReviewedForSecrets: true,
SecretMarkers: []state.LogSecretMarker{
{
ByteStart: 35,
ByteEnd: 70,
Kind: state.StatementTextLogSecret,
},
{
ByteStart: 35,
ByteEnd: 70,
Kind: state.CredentialLogSecret,
},
},
}},
[]state.PostgresQuerySample{{
Query: "CREATE ROLE postgres PASSWORD 'xyz'",
RuntimeMs: 3205.8,
}},
},
// Statement log
{
[]state.LogLine{{
Expand Down
2 changes: 0 additions & 2 deletions vendor/github.com/pganalyze/pg_query_go/v5/CHANGELOG.md

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

4 changes: 2 additions & 2 deletions vendor/github.com/pganalyze/pg_query_go/v5/Makefile

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

2 changes: 1 addition & 1 deletion vendor/github.com/pganalyze/pg_query_go/v5/README.md

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

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

18 changes: 18 additions & 0 deletions vendor/github.com/pganalyze/pg_query_go/v5/parser/parser.go

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

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

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

5 changes: 5 additions & 0 deletions vendor/github.com/pganalyze/pg_query_go/v5/pg_query.go

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

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ github.com/ogier/pflag
# github.com/papertrail/go-tail v0.0.0-20180509224916-973c153b0431
## explicit
github.com/papertrail/go-tail/follower
# github.com/pganalyze/pg_query_go/v5 v5.1.0
# github.com/pganalyze/pg_query_go/v5 v5.1.1-0.20240829182208-c3a818d346a9
## explicit; go 1.14
github.com/pganalyze/pg_query_go/v5
github.com/pganalyze/pg_query_go/v5/parser
Expand Down

0 comments on commit 6ea5a56

Please sign in to comment.