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 c5e7ca3827
commit b38a447b1b
2 changed files with 34 additions and 0 deletions

View File

@ -583,6 +583,20 @@ type WalletConfig struct {
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.
type LocalNotificationsConfig struct {
Enabled bool

View File

@ -1,6 +1,7 @@
package params_test
import (
"encoding/json"
"fmt"
"io/ioutil"
"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)
}