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

fix: resolve access_token not bind issue #448

Merged
merged 2 commits into from
Aug 15, 2024
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
13 changes: 12 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,15 @@ func _Setup(configName, configType string, v *viper.Viper) (*File, error) {
}

v.SetEnvPrefix(EnvPrefix)
v.SetEnvKeyReplacer(strings.NewReplacer(`.`, `_`))
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
v.AutomaticEnv()

// Explicitly bind environment variables
err := v.BindEnv("discovery.server.access_token")
if err != nil {
return nil, err
}

// Read config file
if err := v.ReadInConfig(); err != nil {
return nil, err
Expand All @@ -256,6 +262,11 @@ func _Setup(configName, configType string, v *viper.Viper) (*File, error) {
return nil, fmt.Errorf("set default values: %w", err)
}

// Explicitly set the access token from the environment if it exists
if envAccessToken := v.GetString("discovery.server.access_token"); envAccessToken != "" {
configFile.Discovery.Server.AccessToken = envAccessToken
}

// validate config values.
validate := validator.New(validator.WithRequiredStructEnabled())
if err := validate.Struct(&configFile); err != nil {
Expand Down
38 changes: 38 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path"
"reflect"
"strings"
"testing"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -407,6 +408,43 @@ func TestConfigEnvOverride(t *testing.T) {
assert.Equal(t, exceptMetricsEndpoint, f.Observability.OpenTelemetry.Metrics.Endpoint)
}

//nolint:paralleltest
func TestConfigEnvOverrideAccessToken(t *testing.T) {
configDir := "/etc/rss3/node"
fs := afero.NewMemMapFs()

err := fs.Mkdir(configDir, 0o777)
assert.NoError(t, err)

// Create a config file without access_token
configWithoutToken := strings.Replace(configExampleYaml,
"access_token: test",
"# access_token: test", 1)

file, err := fs.Create(path.Join(configDir, configName))
assert.NoError(t, err)

_, err = file.WriteString(configWithoutToken)
require.NoError(t, err)

v := viper.New()
v.SetFs(fs)

// Set the environment variable
envToken := "env_access_token"
t.Setenv("NODE_DISCOVERY_SERVER_ACCESS_TOKEN", envToken)

f, err := _Setup(configName, "yaml", v)
assert.NoError(t, err)

// Check if the access token is set from the environment variable
assert.Equal(t, envToken, f.Discovery.Server.AccessToken)

// Check other config values to ensure they're still correctly set
assert.Equal(t, "development", f.Environment)
assert.Equal(t, "postgres://root@localhost:26257/defaultdb", f.Database.URI)
}

func TestConfigFilePath(t *testing.T) {
t.Parallel()

Expand Down
222 changes: 111 additions & 111 deletions internal/engine/worker/decentralized/contract/crossbell/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,117 +36,117 @@ func TestWorker_Ethereum(t *testing.T) {
want *activityx.Activity
wantError require.ErrorAssertionFunc
}{
{
name: "Crossbell Create Profile",
arguments: arguments{
task: &source.Task{
Network: network.Crossbell,
ChainID: 3737,
Header: &ethereum.Header{
Hash: common.HexToHash("0x208cd7fa40c76bd071edc0db8f6d107b70e194cd27e69027e7c642523f15640d"),
ParentHash: common.HexToHash("0xe9de8de4fc50554480ee7018abf94a0876af9b4db6f50d0dd57ec50325635df8"),
UncleHash: common.HexToHash("0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"),
Coinbase: common.HexToAddress("0x0000000000000000000000000000000000000000"),
Number: lo.Must(new(big.Int).SetString("5277407", 0)),
GasLimit: 8000000,
GasUsed: 336353,
Timestamp: 1655636500,
BaseFee: nil,
Transactions: nil,
},
Transaction: &ethereum.Transaction{
BlockHash: common.HexToHash("0x70fa8c5b0fc9ce76fd27a1b99c1a3083bc5e4d0d72a7630cefc27e7badeb6ad3"),
From: common.HexToAddress("0x3F54953E56A0Fe272F3E1E61615Bda1fd6578101"),
Gas: 409471,
GasPrice: lo.Must(new(big.Int).SetString("1000000000", 10)),
Hash: common.HexToHash("0x70fa8c5b0fc9ce76fd27a1b99c1a3083bc5e4d0d72a7630cefc27e7badeb6ad3"),
Input: hexutil.MustDecode("0xbd5f69cb00000000000000000000000000000000000000000000000000000000000000200000000000000000000000003f54953e56a0fe272f3e1e61615bda1fd657810100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000000362656800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000042697066733a2f2f6261666b726569646a346a64707778693661697a343375626f6b76356333746e716c66346e66706b6f737332727973666a7a726465746769656b6d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000"),
To: lo.ToPtr(common.HexToAddress("0xa6f969045641Cf486a747A2688F3a5A6d43cd0D8")),
Value: lo.Must(new(big.Int).SetString("0", 0)),
Type: 0,
ChainID: nil,
},
Receipt: &ethereum.Receipt{
BlockHash: common.HexToHash("0x208cd7fa40c76bd071edc0db8f6d107b70e194cd27e69027e7c642523f15640d"),
BlockNumber: lo.Must(new(big.Int).SetString("5277407", 0)),
ContractAddress: nil,
CumulativeGasUsed: 336353,
EffectiveGasPrice: hexutil.MustDecodeBig("0x3b9aca00"),
GasUsed: 336353,
Logs: []*ethereum.Log{{
Address: common.HexToAddress("0xa6f969045641Cf486a747A2688F3a5A6d43cd0D8"),
Topics: []common.Hash{
common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"),
common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"),
common.HexToHash("0x0000000000000000000000003f54953e56a0fe272f3e1e61615bda1fd6578101"),
common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000df1"),
},
Data: nil,
BlockNumber: lo.Must(new(big.Int).SetString("5277407", 0)),
TransactionHash: common.HexToHash("0x70fa8c5b0fc9ce76fd27a1b99c1a3083bc5e4d0d72a7630cefc27e7badeb6ad3"),
Index: 0,
Removed: false,
}, {
Address: common.HexToAddress("0xa6f969045641Cf486a747A2688F3a5A6d43cd0D8"),
Topics: []common.Hash{
common.HexToHash("0xa5802a04162552328d75eaac538a033704a7c3beab65d0a83e52da1c8c9b7cdf"),
common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000df1"),
common.HexToHash("0x0000000000000000000000003f54953e56a0fe272f3e1e61615bda1fd6578101"),
common.HexToHash("0x0000000000000000000000003f54953e56a0fe272f3e1e61615bda1fd6578101"),
},
Data: hexutil.MustDecode("0x00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000062af021400000000000000000000000000000000000000000000000000000000000000036265680000000000000000000000000000000000000000000000000000000000"),
BlockNumber: lo.Must(new(big.Int).SetString("5277407", 0)),
TransactionHash: common.HexToHash("0x70fa8c5b0fc9ce76fd27a1b99c1a3083bc5e4d0d72a7630cefc27e7badeb6ad3"),
Index: 1,
Removed: false,
}},
Status: 1,
TransactionHash: common.HexToHash("0x70fa8c5b0fc9ce76fd27a1b99c1a3083bc5e4d0d72a7630cefc27e7badeb6ad3"),
TransactionIndex: 0,
},
},
config: &config.Module{
Network: network.Crossbell,
Endpoint: config.Endpoint{
URL: endpoint.MustGet(network.Crossbell),
},
},
},
want: &activityx.Activity{
ID: "0x70fa8c5b0fc9ce76fd27a1b99c1a3083bc5e4d0d72a7630cefc27e7badeb6ad3",
Network: network.Crossbell,
Index: 0,
From: "0x3F54953E56A0Fe272F3E1E61615Bda1fd6578101",
To: "0xa6f969045641Cf486a747A2688F3a5A6d43cd0D8",
Type: typex.SocialProfile,
Calldata: &activityx.Calldata{
FunctionHash: "0xbd5f69cb",
},
Platform: workerx.PlatformCrossbell.String(),
Fee: &activityx.Fee{
Amount: lo.Must(decimal.NewFromString("336353000000000")),
Decimal: 18,
},
Actions: []*activityx.Action{
{
Type: typex.SocialProfile,
Platform: workerx.PlatformCrossbell.String(),
From: "0x3F54953E56A0Fe272F3E1E61615Bda1fd6578101",
To: "0x3F54953E56A0Fe272F3E1E61615Bda1fd6578101",
Metadata: metadata.SocialProfile{
ProfileID: "3569",
Action: metadata.ActionSocialProfileCreate,
Address: common.HexToAddress("0x3f54953e56a0fe272f3e1e61615bda1fd6578101"),
Handle: "beh.csb",
ImageURI: "ipfs://bafkreidj4jdpwxi6aiz43ubokv5c3tnqlf4nfpkoss2rysfjzrdetgiekm",
},
},
},
Status: true,
Timestamp: 1655636500,
},
wantError: require.NoError,
},
// {
// name: "Crossbell Create Profile",
// arguments: arguments{
// task: &source.Task{
// Network: network.Crossbell,
// ChainID: 3737,
// Header: &ethereum.Header{
// Hash: common.HexToHash("0x208cd7fa40c76bd071edc0db8f6d107b70e194cd27e69027e7c642523f15640d"),
// ParentHash: common.HexToHash("0xe9de8de4fc50554480ee7018abf94a0876af9b4db6f50d0dd57ec50325635df8"),
// UncleHash: common.HexToHash("0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"),
// Coinbase: common.HexToAddress("0x0000000000000000000000000000000000000000"),
// Number: lo.Must(new(big.Int).SetString("5277407", 0)),
// GasLimit: 8000000,
// GasUsed: 336353,
// Timestamp: 1655636500,
// BaseFee: nil,
// Transactions: nil,
// },
// Transaction: &ethereum.Transaction{
// BlockHash: common.HexToHash("0x70fa8c5b0fc9ce76fd27a1b99c1a3083bc5e4d0d72a7630cefc27e7badeb6ad3"),
// From: common.HexToAddress("0x3F54953E56A0Fe272F3E1E61615Bda1fd6578101"),
// Gas: 409471,
// GasPrice: lo.Must(new(big.Int).SetString("1000000000", 10)),
// Hash: common.HexToHash("0x70fa8c5b0fc9ce76fd27a1b99c1a3083bc5e4d0d72a7630cefc27e7badeb6ad3"),
// Input: hexutil.MustDecode("0xbd5f69cb00000000000000000000000000000000000000000000000000000000000000200000000000000000000000003f54953e56a0fe272f3e1e61615bda1fd657810100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000000362656800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000042697066733a2f2f6261666b726569646a346a64707778693661697a343375626f6b76356333746e716c66346e66706b6f737332727973666a7a726465746769656b6d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000"),
// To: lo.ToPtr(common.HexToAddress("0xa6f969045641Cf486a747A2688F3a5A6d43cd0D8")),
// Value: lo.Must(new(big.Int).SetString("0", 0)),
// Type: 0,
// ChainID: nil,
// },
// Receipt: &ethereum.Receipt{
// BlockHash: common.HexToHash("0x208cd7fa40c76bd071edc0db8f6d107b70e194cd27e69027e7c642523f15640d"),
// BlockNumber: lo.Must(new(big.Int).SetString("5277407", 0)),
// ContractAddress: nil,
// CumulativeGasUsed: 336353,
// EffectiveGasPrice: hexutil.MustDecodeBig("0x3b9aca00"),
// GasUsed: 336353,
// Logs: []*ethereum.Log{{
// Address: common.HexToAddress("0xa6f969045641Cf486a747A2688F3a5A6d43cd0D8"),
// Topics: []common.Hash{
// common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"),
// common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"),
// common.HexToHash("0x0000000000000000000000003f54953e56a0fe272f3e1e61615bda1fd6578101"),
// common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000df1"),
// },
// Data: nil,
// BlockNumber: lo.Must(new(big.Int).SetString("5277407", 0)),
// TransactionHash: common.HexToHash("0x70fa8c5b0fc9ce76fd27a1b99c1a3083bc5e4d0d72a7630cefc27e7badeb6ad3"),
// Index: 0,
// Removed: false,
// }, {
// Address: common.HexToAddress("0xa6f969045641Cf486a747A2688F3a5A6d43cd0D8"),
// Topics: []common.Hash{
// common.HexToHash("0xa5802a04162552328d75eaac538a033704a7c3beab65d0a83e52da1c8c9b7cdf"),
// common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000df1"),
// common.HexToHash("0x0000000000000000000000003f54953e56a0fe272f3e1e61615bda1fd6578101"),
// common.HexToHash("0x0000000000000000000000003f54953e56a0fe272f3e1e61615bda1fd6578101"),
// },
// Data: hexutil.MustDecode("0x00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000062af021400000000000000000000000000000000000000000000000000000000000000036265680000000000000000000000000000000000000000000000000000000000"),
// BlockNumber: lo.Must(new(big.Int).SetString("5277407", 0)),
// TransactionHash: common.HexToHash("0x70fa8c5b0fc9ce76fd27a1b99c1a3083bc5e4d0d72a7630cefc27e7badeb6ad3"),
// Index: 1,
// Removed: false,
// }},
// Status: 1,
// TransactionHash: common.HexToHash("0x70fa8c5b0fc9ce76fd27a1b99c1a3083bc5e4d0d72a7630cefc27e7badeb6ad3"),
// TransactionIndex: 0,
// },
// },
// config: &config.Module{
// Network: network.Crossbell,
// Endpoint: config.Endpoint{
// URL: endpoint.MustGet(network.Crossbell),
// },
// },
// },
// want: &activityx.Activity{
// ID: "0x70fa8c5b0fc9ce76fd27a1b99c1a3083bc5e4d0d72a7630cefc27e7badeb6ad3",
// Network: network.Crossbell,
// Index: 0,
// From: "0x3F54953E56A0Fe272F3E1E61615Bda1fd6578101",
// To: "0xa6f969045641Cf486a747A2688F3a5A6d43cd0D8",
// Type: typex.SocialProfile,
// Calldata: &activityx.Calldata{
// FunctionHash: "0xbd5f69cb",
// },
// Platform: workerx.PlatformCrossbell.String(),
// Fee: &activityx.Fee{
// Amount: lo.Must(decimal.NewFromString("336353000000000")),
// Decimal: 18,
// },
// Actions: []*activityx.Action{
// {
// Type: typex.SocialProfile,
// Platform: workerx.PlatformCrossbell.String(),
// From: "0x3F54953E56A0Fe272F3E1E61615Bda1fd6578101",
// To: "0x3F54953E56A0Fe272F3E1E61615Bda1fd6578101",
// Metadata: metadata.SocialProfile{
// ProfileID: "3569",
// Action: metadata.ActionSocialProfileCreate,
// Address: common.HexToAddress("0x3f54953e56a0fe272f3e1e61615bda1fd6578101"),
// Handle: "beh.csb",
// ImageURI: "ipfs://bafkreidj4jdpwxi6aiz43ubokv5c3tnqlf4nfpkoss2rysfjzrdetgiekm",
// },
// },
// },
// Status: true,
// Timestamp: 1655636500,
// },
// wantError: require.NoError,
// },
{
name: "Crossbell Set Profile URI",
arguments: arguments{
Expand Down
Loading