chore_: ignore sensitive fields of WalletConfig when logging

This commit is contained in:
frank 2024-08-08 16:35:11 +08:00 committed by Andrea Maria Piana
parent 45cea612d5
commit 085d0228aa
2 changed files with 34 additions and 0 deletions

View File

@ -583,6 +583,20 @@ type WalletConfig struct {
EnableCelerBridge bool `json:"EnableCelerBridge"` EnableCelerBridge bool `json:"EnableCelerBridge"`
} }
// MarshalJSON custom marshalling to avoid exposing sensitive data in log,
// there's a function called `startNode` will log NodeConfig which include WalletConfig
func (wc WalletConfig) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Enabled bool `json:"Enabled"`
StatusProxyEnabled bool `json:"StatusProxyEnabled"`
EnableCelerBridge bool `json:"EnableCelerBridge"`
}{
Enabled: wc.Enabled,
StatusProxyEnabled: wc.StatusProxyEnabled,
EnableCelerBridge: wc.EnableCelerBridge,
})
}
// LocalNotificationsConfig extra configuration for localnotifications.Service. // LocalNotificationsConfig extra configuration for localnotifications.Service.
type LocalNotificationsConfig struct { type LocalNotificationsConfig struct {
Enabled bool Enabled bool

View File

@ -1,6 +1,7 @@
package params_test package params_test
import ( import (
"encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"path" "path"
@ -338,3 +339,22 @@ func TestNodeConfigValidate(t *testing.T) {
}) })
} }
} }
func TestMarshalWalletConfigJSON(t *testing.T) {
walletConfig := params.WalletConfig{
OpenseaAPIKey: "some-key",
RaribleMainnetAPIKey: "some-key2",
}
bytes, err := json.Marshal(walletConfig)
require.NoError(t, err)
// check if sensitive fields are not present
require.NotContains(t, string(bytes), "OpenseaAPIKey")
require.Contains(t, string(bytes), "StatusProxyEnabled")
// check if deserializing are still working with sensitive fields
walletConfig = params.WalletConfig{}
err = json.Unmarshal([]byte(`{"OpenseaAPIKey":"some-key", "StatusProxyEnabled":true}`), &walletConfig)
require.NoError(t, err)
require.Equal(t, "some-key", walletConfig.OpenseaAPIKey)
require.True(t, walletConfig.StatusProxyEnabled)
}