Skip to content

Commit 1d7e152

Browse files
authored
feat: print str cleanup (#1459)
1 parent 4bb9def commit 1d7e152

2 files changed

Lines changed: 93 additions & 5 deletions

File tree

base/gfspconfig/config.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,42 @@ func (cfg *GfSpConfig) Apply(opts ...Option) error {
7474
return nil
7575
}
7676

77-
// String returns the detail GfSp configuration.
77+
const redactedPlaceholder = "[REDACTED]"
78+
79+
// String returns the detail GfSp configuration with secrets redacted.
7880
func (cfg *GfSpConfig) String() string {
79-
customize := cfg.Customize
80-
cfg.Customize = nil
81-
bz, err := toml.Marshal(cfg)
81+
redacted := *cfg
82+
redacted.Customize = nil
83+
84+
redacted.SpAccount = SpAccountConfig{
85+
SpOperatorAddress: cfg.SpAccount.SpOperatorAddress,
86+
OperatorPrivateKey: redactIfSet(cfg.SpAccount.OperatorPrivateKey),
87+
FundingPrivateKey: redactIfSet(cfg.SpAccount.FundingPrivateKey),
88+
SealPrivateKey: redactIfSet(cfg.SpAccount.SealPrivateKey),
89+
ApprovalPrivateKey: redactIfSet(cfg.SpAccount.ApprovalPrivateKey),
90+
GcPrivateKey: redactIfSet(cfg.SpAccount.GcPrivateKey),
91+
BlsPrivateKey: redactIfSet(cfg.SpAccount.BlsPrivateKey),
92+
}
93+
94+
redacted.P2P.P2PPrivateKey = redactIfSet(cfg.P2P.P2PPrivateKey)
95+
96+
redacted.SpDB.Passwd = redactIfSet(cfg.SpDB.Passwd)
97+
redacted.BsDB.Passwd = redactIfSet(cfg.BsDB.Passwd)
98+
99+
bz, err := toml.Marshal(&redacted)
82100
if err != nil {
83101
return ""
84102
}
85-
cfg.Customize = customize
86103
return string(bz)
87104
}
88105

106+
func redactIfSet(v string) string {
107+
if v != "" {
108+
return redactedPlaceholder
109+
}
110+
return ""
111+
}
112+
89113
type ChainConfig struct {
90114
ChainID string `comment:"required"`
91115
ChainAddress []string `comment:"required"`

base/gfspconfig/config_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package gfspconfig
22

33
import (
44
"errors"
5+
"strings"
56
"testing"
67

78
"github.com/stretchr/testify/assert"
9+
10+
storeconfig "github.com/bnb-chain/greenfield-storage-provider/store/config"
811
)
912

1013
var mockErr = errors.New("mock error")
@@ -28,3 +31,64 @@ func TestGfSpConfig_StringSuccess(t *testing.T) {
2831
result := cfg.String()
2932
assert.NotNil(t, result)
3033
}
34+
35+
func TestGfSpConfig_StringRedactsSecrets(t *testing.T) {
36+
cfg := &GfSpConfig{
37+
Env: "mainnet",
38+
SpAccount: SpAccountConfig{
39+
SpOperatorAddress: "0xABCD",
40+
OperatorPrivateKey: "secret_operator",
41+
FundingPrivateKey: "secret_funding",
42+
SealPrivateKey: "secret_seal",
43+
ApprovalPrivateKey: "secret_approval",
44+
GcPrivateKey: "secret_gc",
45+
BlsPrivateKey: "secret_bls",
46+
},
47+
P2P: P2PConfig{
48+
P2PPrivateKey: "secret_p2p",
49+
},
50+
SpDB: storeconfig.SQLDBConfig{Passwd: "secret_spdb"},
51+
BsDB: storeconfig.SQLDBConfig{Passwd: "secret_bsdb"},
52+
}
53+
54+
result := cfg.String()
55+
56+
secrets := []string{
57+
"secret_operator", "secret_funding", "secret_seal",
58+
"secret_approval", "secret_gc", "secret_bls",
59+
"secret_p2p", "secret_spdb", "secret_bsdb",
60+
}
61+
for _, s := range secrets {
62+
assert.False(t, strings.Contains(result, s), "output must not contain plaintext secret: %s", s)
63+
}
64+
65+
// 9 sensitive fields should each appear as [REDACTED]
66+
assert.Equal(t, 9, strings.Count(result, redactedPlaceholder),
67+
"output must contain exactly 9 [REDACTED] placeholders")
68+
69+
assert.Contains(t, result, "0xABCD", "non-secret fields must still be present")
70+
}
71+
72+
func TestGfSpConfig_StringDoesNotMutateOriginal(t *testing.T) {
73+
cfg := &GfSpConfig{
74+
Env: "mainnet",
75+
SpAccount: SpAccountConfig{
76+
OperatorPrivateKey: "original_key",
77+
},
78+
P2P: P2PConfig{
79+
P2PPrivateKey: "original_p2p",
80+
},
81+
}
82+
83+
_ = cfg.String()
84+
85+
assert.Equal(t, "original_key", cfg.SpAccount.OperatorPrivateKey, "String() must not mutate original config")
86+
assert.Equal(t, "original_p2p", cfg.P2P.P2PPrivateKey, "String() must not mutate original config")
87+
}
88+
89+
func TestGfSpConfig_StringEmptySecretsNotRedacted(t *testing.T) {
90+
cfg := &GfSpConfig{Env: "mainnet"}
91+
result := cfg.String()
92+
assert.False(t, strings.Contains(result, redactedPlaceholder),
93+
"empty secret fields should not produce [REDACTED]")
94+
}

0 commit comments

Comments
 (0)