Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lightning: redact external storage url (#59256) #59372

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions br/pkg/lightning/config/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ go_library(
"//br/pkg/lightning/log",
"//br/pkg/version/build",
"//config",
"//parser/ast",
"//parser/mysql",
"//util",
"//util/table-filter",
Expand Down
11 changes: 11 additions & 0 deletions br/pkg/lightning/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/pingcap/tidb/br/pkg/lightning/common"
"github.com/pingcap/tidb/br/pkg/lightning/log"
tidbcfg "github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/util"
filter "github.com/pingcap/tidb/util/table-filter"
Expand Down Expand Up @@ -162,6 +163,16 @@ func (cfg *Config) String() string {
return string(bytes)
}

// Redact redacts the sensitive information.
func (cfg *Config) Redact() string {
originDir := cfg.Mydumper.SourceDir
defer func() {
cfg.Mydumper.SourceDir = originDir
}()
cfg.Mydumper.SourceDir = ast.RedactURL(cfg.Mydumper.SourceDir)
return cfg.String()
}

func (cfg *Config) ToTLS() (*common.TLS, error) {
hostPort := net.JoinHostPort(cfg.TiDB.Host, strconv.Itoa(cfg.TiDB.StatusPort))
return common.NewTLS(
Expand Down
27 changes: 27 additions & 0 deletions br/pkg/lightning/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1019,3 +1019,30 @@ func TestCreateSeveralConfigsWithDifferentFilters(t *testing.T) {
))
require.True(t, common.StringSliceEqual(config.GetDefaultFilter(), originalDefaultCfg))
}

func TestRedactConfig(t *testing.T) {
tests := []struct {
origin string
redact string
}{
{"", ""},
{":", ":"},
{"~/file", "~/file"},
{"gs://bucket/file", "gs://bucket/file"},
{"gs://bucket/file?access-key=123", "gs://bucket/file?access-key=123"},
{"gs://bucket/file?secret-access-key=123", "gs://bucket/file?secret-access-key=123"},
{"s3://bucket/file", "s3://bucket/file"},
{"s3://bucket/file?other-key=123", "s3://bucket/file?other-key=123"},
{"s3://bucket/file?access-key=123", "s3://bucket/file?access-key=xxxxxx"},
{"s3://bucket/file?secret-access-key=123", "s3://bucket/file?secret-access-key=xxxxxx"},
{"s3://bucket/file?access_key=123", "s3://bucket/file?access_key=xxxxxx"},
{"s3://bucket/file?secret_access_key=123", "s3://bucket/file?secret_access_key=xxxxxx"},
}
for _, tt := range tests {
cfg := config.NewConfig()
cfg.Mydumper.SourceDir = tt.origin

require.Contains(t, cfg.Redact(), tt.redact)
require.Contains(t, cfg.String(), tt.origin)
}
}
2 changes: 1 addition & 1 deletion br/pkg/lightning/lightning.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ var (

func (l *Lightning) run(taskCtx context.Context, taskCfg *config.Config, o *options) (err error) {
build.LogInfo(build.Lightning)
o.logger.Info("cfg", zap.Stringer("cfg", taskCfg))
o.logger.Info("cfg", zap.String("cfg", taskCfg.Redact()))

utils.LogEnvVariables()

Expand Down
25 changes: 25 additions & 0 deletions parser/ast/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3287,6 +3287,31 @@ func (n *BRIEStmt) Restore(ctx *format.RestoreCtx) error {
return nil
}

// RedactURL redacts the secret tokens in the URL. only S3 url need redaction for now.
// if the url is not a valid url, return the original string.
func RedactURL(str string) string {
// FIXME: this solution is not scalable, and duplicates some logic from BR.
u, err := url.Parse(str)
if err != nil {
return str
}
scheme := u.Scheme
switch strings.ToLower(scheme) {
case "s3", "ks3":
values := u.Query()
for k := range values {
// see below on why we normalize key
// https://github.com/pingcap/tidb/blob/a7c0d95f16ea2582bb569278c3f829403e6c3a7e/br/pkg/storage/parse.go#L163
normalizedKey := strings.ToLower(strings.ReplaceAll(k, "_", "-"))
if normalizedKey == "access-key" || normalizedKey == "secret-access-key" || normalizedKey == "session-token" {
values[k] = []string{"xxxxxx"}
}
}
u.RawQuery = values.Encode()
}
return u.String()
}

// SecureText implements SensitiveStmtNode
func (n *BRIEStmt) SecureText() string {
// FIXME: this solution is not scalable, and duplicates some logic from BR.
Expand Down