Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Don't log secret of s3. #292

Merged
merged 8 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewBackupCommand() *cobra.Command {
return err
}
utils.LogBRInfo()
utils.LogArguments(c)
task.LogArguments(c)

// Do not run ddl worker in BR.
ddl.RunWorker = false
Expand Down
2 changes: 1 addition & 1 deletion cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func NewRestoreCommand() *cobra.Command {
return err
}
utils.LogBRInfo()
utils.LogArguments(c)
task.LogArguments(c)

// Do not run stat worker in BR.
session.DisableStats4Test()
Expand Down
2 changes: 1 addition & 1 deletion cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewValidateCommand() *cobra.Command {
return err
}
utils.LogBRInfo()
utils.LogArguments(c)
task.LogArguments(c)
return nil
},
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/backup/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,12 @@ func SendBackup(
req kvproto.BackupRequest,
respFn func(*kvproto.BackupResponse) error,
) error {
log.Info("try backup", zap.Any("backup request", req))
log.Info("try backup",
zap.Binary("StartKey", req.StartKey),
zap.Binary("EndKey", req.EndKey),
zap.Uint64("RateLimit", req.RateLimit),
zap.Uint32("Concurrency", req.Concurrency),
YuJuncen marked this conversation as resolved.
Show resolved Hide resolved
)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
bcli, err := client.Backup(ctx, &req)
Expand Down
28 changes: 28 additions & 0 deletions pkg/task/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import (
"context"
"crypto/tls"
"fmt"
"net/url"
"regexp"
"strings"

"github.com/pingcap/log"
"go.uber.org/zap"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the empty line.

"github.com/gogo/protobuf/proto"
"github.com/pingcap/errors"
"github.com/pingcap/kvproto/pkg/backup"
Expand Down Expand Up @@ -286,3 +290,27 @@ func escapeFilterName(name string) string {
}
return "~^" + regexp.QuoteMeta(name) + "$"
}

// flagToZapField checks whether this flag can be logged,
// if need to log, return its zap field. Or return a field with hidden value.
func flagToZapField(f *pflag.Flag) zap.Field {
if f.Name == flagStorage {
hiddenQuery, err := url.Parse(f.Value.String())
if err != nil {
return zap.String(f.Name, "<invalid URI>")
}
// hide all query here.
hiddenQuery.RawQuery = ""
return zap.Stringer(f.Name, hiddenQuery)
}
return zap.Stringer(f.Name, f.Value)
}

// LogArguments prints origin command arguments.
func LogArguments(cmd *cobra.Command) {
var fields []zap.Field
cmd.Flags().VisitAll(func(f *pflag.Flag) {
fields = append(fields, flagToZapField(f))
YuJuncen marked this conversation as resolved.
Show resolved Hide resolved
})
log.Info("arguments", fields...)
}
37 changes: 37 additions & 0 deletions pkg/task/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package task
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add license header.


import (
"fmt"

. "github.com/pingcap/check"
"github.com/spf13/pflag"
)

var _ = Suite(&testCommonSuite{})

type testCommonSuite struct{}

type fakeValue string

func (f fakeValue) String() string {
return string(f)
}

func (f fakeValue) Set(string) error {
panic("implement me")
}

func (f fakeValue) Type() string {
panic("implement me")
}

func (*testCommonSuite) TestUrlNoQuery(c *C) {
flag := &pflag.Flag{
Name: flagStorage,
Value: fakeValue("s3://some/what?secret=a123456789&key=987654321"),
}

field := flagToZapField(flag)
c.Assert(field.Key, Equals, flagStorage)
c.Assert(field.Interface.(fmt.Stringer).String(), Equals, "s3://some/what")
}
11 changes: 0 additions & 11 deletions pkg/utils/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (

"github.com/pingcap/log"
"github.com/pingcap/tidb/util/israce"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -45,12 +43,3 @@ func BRInfo() string {
fmt.Fprintf(&buf, "Race Enabled: %t", israce.RaceEnabled)
return buf.String()
}

// LogArguments prints origin command arguments.
func LogArguments(cmd *cobra.Command) {
var fields []zap.Field
cmd.Flags().VisitAll(func(f *pflag.Flag) {
fields = append(fields, zap.Stringer(f.Name, f.Value))
})
log.Info("arguments", fields...)
}
28 changes: 26 additions & 2 deletions tests/br_s3/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,39 @@ done

# backup full
echo "backup start..."
run_br --pd $PD_ADDR backup full -s "s3://mybucket/$DB?endpoint=http://$S3_ENDPOINT"
BACKUP_LOG="backup.log"
rm -f $BACKUP_LOG
unset BR_LOG_TO_TERM
run_br --pd $PD_ADDR backup full -s "s3://mybucket/$DB?endpoint=http://$S3_ENDPOINT" \
--log-file $BACKUP_LOG || \
( ( cat $BACKUP_LOG || BR_LOG_TO_TERM=1) && exit 1 )
cat $BACKUP_LOG
BR_LOG_TO_TERM=1

if grep -i $MINIO_SECRET_KEY $BACKUP_LOG; then
echo "Secret key logged in log. Please remove them."
exit 1
fi

for i in $(seq $DB_COUNT); do
run_sql "DROP DATABASE $DB${i};"
done

# restore full
echo "restore start..."
run_br restore full -s "s3://mybucket/$DB" --pd $PD_ADDR --s3.endpoint="http://$S3_ENDPOINT"
RESTORE_LOG="restore.log"
rm -f $RESTORE_LOG
unset BR_LOG_TO_TERM
run_br restore full -s "s3://mybucket/$DB" --pd $PD_ADDR --s3.endpoint="http://$S3_ENDPOINT" \
--log-file $RESTORE_LOG || \
( ( cat $RESTORE_LOG || BR_LOG_TO_TERM=1) && exit 1 )
cat $RESTORE_LOG
BR_LOG_TO_TERM=1

if grep -i $MINIO_SECRET_KEY $RESTORE_LOG; then
echo "Secret key logged in log. Please remove them."
exit 1
fi

for i in $(seq $DB_COUNT); do
row_count_new[${i}]=$(run_sql "SELECT COUNT(*) FROM $DB${i}.$TABLE;" | awk '/COUNT/{print $2}')
Expand Down