Skip to content

Commit

Permalink
Fix config to allow ip addr in host fields (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
evlekht authored and sk263 committed Dec 3, 2024
1 parent 982139e commit 6ed6df2
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 100 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ __debug*
./camino-messenger-bot-supplier.yaml
./camino-messenger-bot-distributor.yaml

cmb-config
distributor-bot-db
supplier-bot-db

Expand Down
97 changes: 35 additions & 62 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"crypto/ecdsa"
"encoding/hex"
"math/big"
"net/url"
"time"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -21,7 +20,7 @@ type Config struct {
BotKey *ecdsa.PrivateKey
CMAccountAddress common.Address

ChainRPCURL url.URL
ChainRPCURL string
BookingTokenAddress common.Address

NetworkFeeRecipientBotAddress common.Address
Expand All @@ -40,26 +39,6 @@ type Config struct {
Matrix MatrixConfig
}

type TracingConfig struct {
Enabled bool
HostURL url.URL
Insecure bool
CertFile string
KeyFile string
}

type PartnerPluginConfig struct {
Enabled bool
HostURL url.URL
Unencrypted bool
CACertFile string
}

type MatrixConfig struct {
HostURL url.URL
Store string
}

type SQLiteDBConfig struct {
Common UnparsedSQLiteDBConfig
Scheduler UnparsedSQLiteDBConfig
Expand All @@ -70,6 +49,26 @@ type SQLiteDBConfig struct {
//
//

type TracingConfig struct {
Enabled bool `mapstructure:"enabled"`
Host string `mapstructure:"host"`
Insecure bool `mapstructure:"insecure"`
CertFile string `mapstructure:"cert_file"`
KeyFile string `mapstructure:"key_file"`
}

type PartnerPluginConfig struct {
Enabled bool `mapstructure:"enabled"`
Host string `mapstructure:"host"`
Unencrypted bool `mapstructure:"unencrypted"`
CACertFile string `mapstructure:"ca_file"`
}

type MatrixConfig struct {
Host string `mapstructure:"host"`
Store string
}

type RPCServerConfig struct {
Enabled bool `mapstructure:"enabled"`
Port uint64 `mapstructure:"port"`
Expand Down Expand Up @@ -100,62 +99,36 @@ type UnparsedConfig struct {

ResponseTimeout int64 `mapstructure:"response_timeout"` // milliseconds

PartnerPlugin UnparsedPartnerPluginConfig `mapstructure:"partner_plugin"`
Tracing UnparsedTracingConfig `mapstructure:"tracing"`
Matrix UnparsedMatrixConfig `mapstructure:"matrix"`
PartnerPlugin PartnerPluginConfig `mapstructure:"partner_plugin"`
Tracing TracingConfig `mapstructure:"tracing"`
RPCServer RPCServerConfig `mapstructure:"rpc_server"`

RPCServer RPCServerConfig `mapstructure:"rpc_server"`
DB UnparsedSQLiteDBConfig `mapstructure:"db"`
}

type UnparsedTracingConfig struct {
Enabled bool `mapstructure:"enabled"`
Host string `mapstructure:"host"`
Insecure bool `mapstructure:"insecure"`
CertFile string `mapstructure:"cert_file"`
KeyFile string `mapstructure:"key_file"`
Matrix UnparsedMatrixConfig `mapstructure:"matrix"`
DB UnparsedSQLiteDBConfig `mapstructure:"db"`
}

type UnparsedPartnerPluginConfig struct {
Enabled bool `mapstructure:"enabled"`
Host string `mapstructure:"host"`
Unencrypted bool `mapstructure:"unencrypted"`
CACertFile string `mapstructure:"ca_file"`
type UnparsedSQLiteDBConfig struct {
DBPath string `mapstructure:"path"`
MigrationsPath string `mapstructure:"migrations_path"`
}

type UnparsedMatrixConfig struct {
Host string `mapstructure:"host"`
}

type UnparsedSQLiteDBConfig struct {
DBPath string `mapstructure:"path"`
MigrationsPath string `mapstructure:"migrations_path"`
}

func (cfg *Config) unparse() *UnparsedConfig {
return &UnparsedConfig{
DB: cfg.DB.Common,
RPCServer: cfg.RPCServer,
Tracing: UnparsedTracingConfig{
Enabled: cfg.Tracing.Enabled,
Host: cfg.Tracing.HostURL.String(),
Insecure: cfg.Tracing.Insecure,
CertFile: cfg.Tracing.CertFile,
KeyFile: cfg.Tracing.KeyFile,
},
PartnerPlugin: UnparsedPartnerPluginConfig{
Enabled: cfg.PartnerPlugin.Enabled,
Host: cfg.PartnerPlugin.HostURL.String(),
Unencrypted: cfg.PartnerPlugin.Unencrypted,
CACertFile: cfg.PartnerPlugin.CACertFile,
},
DB: cfg.DB.Common,
RPCServer: cfg.RPCServer,
Tracing: cfg.Tracing,
PartnerPlugin: cfg.PartnerPlugin,
Matrix: UnparsedMatrixConfig{
Host: cfg.Matrix.HostURL.String(),
Host: cfg.Matrix.Host,
},
DeveloperMode: cfg.DeveloperMode,
BotKey: hex.EncodeToString(crypto.FromECDSA(cfg.BotKey)),
CMAccountAddress: cfg.CMAccountAddress.Hex(),
ChainRPCURL: cfg.ChainRPCURL.String(),
ChainRPCURL: cfg.ChainRPCURL,
BookingTokenAddress: cfg.BookingTokenAddress.Hex(),
NetworkFeeRecipientBotAddress: cfg.NetworkFeeRecipientBotAddress.Hex(),
NetworkFeeRecipientCMAccountAddress: cfg.NetworkFeeRecipientCMAccountAddress.Hex(),
Expand Down
48 changes: 18 additions & 30 deletions config/config_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"math/big"
"net/url"
"os"
"strings"
"time"
Expand Down Expand Up @@ -95,27 +94,27 @@ func (cr *reader) parseConfig(cfg *UnparsedConfig) (*Config, error) {
return nil, err
}

chainRPC, err := url.Parse(cfg.ChainRPCURL)
if err != nil {
cr.logger.Errorf("Error parsing C-Chain RPC URL: %s", err)
if !common.IsHexAddress(cfg.CMAccountAddress) {
err := errors.New("invalid CM account address")
cr.logger.Error(err)
return nil, err
}

tracingHost, err := url.Parse(cfg.Tracing.Host)
if err != nil {
cr.logger.Errorf("Error parsing tracing host: %s", err)
if !common.IsHexAddress(cfg.BookingTokenAddress) {
err := errors.New("invalid booking token address")
cr.logger.Error(err)
return nil, err
}

partnerPluginHost, err := url.Parse(cfg.PartnerPlugin.Host)
if err != nil {
cr.logger.Errorf("Error parsing partner plugin host: %s", err)
if !common.IsHexAddress(cfg.NetworkFeeRecipientBotAddress) {
err := errors.New("invalid network fee recipient bot address")
cr.logger.Error(err)
return nil, err
}

matrixHost, err := url.Parse(cfg.Matrix.Host)
if err != nil {
cr.logger.Errorf("Error parsing matrix host: %s", err)
if !common.IsHexAddress(cfg.NetworkFeeRecipientCMAccountAddress) {
err := errors.New("invalid network fee recipient CM account address")
cr.logger.Error(err)
return nil, err
}

Expand All @@ -131,28 +130,17 @@ func (cr *reader) parseConfig(cfg *UnparsedConfig) (*Config, error) {
MigrationsPath: cfg.DB.MigrationsPath + "/cheque_handler",
},
},
RPCServer: cfg.RPCServer,
Tracing: TracingConfig{
Enabled: cfg.Tracing.Enabled,
HostURL: *tracingHost,
Insecure: cfg.Tracing.Insecure,
CertFile: cfg.Tracing.CertFile,
KeyFile: cfg.Tracing.KeyFile,
},
PartnerPlugin: PartnerPluginConfig{
Enabled: cfg.PartnerPlugin.Enabled,
HostURL: *partnerPluginHost,
Unencrypted: cfg.PartnerPlugin.Unencrypted,
CACertFile: cfg.PartnerPlugin.CACertFile,
},
RPCServer: cfg.RPCServer,
Tracing: cfg.Tracing,
PartnerPlugin: cfg.PartnerPlugin,
Matrix: MatrixConfig{
HostURL: *matrixHost,
Store: cfg.DB.DBPath + "/matrix",
Host: cfg.Matrix.Host,
Store: cfg.DB.DBPath + "/matrix",
},
DeveloperMode: cfg.DeveloperMode,
BotKey: botKey,
CMAccountAddress: common.HexToAddress(cfg.CMAccountAddress),
ChainRPCURL: *chainRPC,
ChainRPCURL: cfg.ChainRPCURL,
BookingTokenAddress: common.HexToAddress(cfg.BookingTokenAddress),
NetworkFeeRecipientBotAddress: common.HexToAddress(cfg.NetworkFeeRecipientBotAddress),
NetworkFeeRecipientCMAccountAddress: common.HexToAddress(cfg.NetworkFeeRecipientCMAccountAddress),
Expand Down
5 changes: 2 additions & 3 deletions examples/rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/csv"
"flag"
"fmt"
"net/url"
"os"
"sort"
"strconv"
Expand Down Expand Up @@ -41,10 +40,10 @@ func main() {
recipient := flag.String("recipient", "@0xeb3D6560a5eCf3e00b68a4b2899FEc93419F06B9:", "Recipient c-chain address (format: @[...]:messenger.chain4travel.com")
caCertFile := flag.String("ca-cert-file", "", "CA certificate file (optional)")
flag.Parse()
hostURL, _ := url.Parse(fmt.Sprintf("%s:%d", *host, *port))
hostURL := fmt.Sprintf("%s:%d", *host, *port)

ppConfig := config.PartnerPluginConfig{
HostURL: *hostURL,
Host: hostURL,
Unencrypted: *caCertFile == "",
}
ppConfig.CACertFile = *caCertFile
Expand Down
4 changes: 2 additions & 2 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const (

func NewApp(ctx context.Context, cfg *config.Config, logger *zap.SugaredLogger) (*App, error) {
// c-chain evm client && chain id
evmClient, err := ethclient.Dial(cfg.ChainRPCURL.String())
evmClient, err := ethclient.Dial(cfg.ChainRPCURL)
if err != nil {
logger.Errorf("Failed to connect to the Ethereum client: %v", err)
return nil, err
Expand Down Expand Up @@ -143,7 +143,7 @@ func NewApp(ctx context.Context, cfg *config.Config, logger *zap.SugaredLogger)
}

botAddress := crypto.PubkeyToAddress(cfg.BotKey.PublicKey)
botUserID := messaging.UserIDFromAddress(botAddress, cfg.Matrix.HostURL.String())
botUserID := messaging.UserIDFromAddress(botAddress, cfg.Matrix.Host)

messageProcessor := messaging.NewProcessor(
matrixMessenger,
Expand Down
2 changes: 1 addition & 1 deletion internal/matrix/matrix_messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
var _ messaging.Messenger = (*messenger)(nil)

func NewMessenger(cfg config.MatrixConfig, botKey *ecdsa.PrivateKey, logger *zap.SugaredLogger) (messaging.Messenger, error) {
c, err := mautrix.NewClient(cfg.HostURL.String(), "", "")
c, err := mautrix.NewClient(cfg.Host, "", "")
if err != nil {
logger.Errorf("failed to create matrix client: %v", err)
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion internal/rpc/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewClient(cfg config.PartnerPluginConfig, logger *zap.SugaredLogger) (*RPCC
opts = append(opts, grpc.WithTransportCredentials(tlsCreds))
}

clientConnection, err := grpc.NewClient(cfg.HostURL.String(), opts...)
clientConnection, err := grpc.NewClient(cfg.Host, opts...)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/tracing/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (
func newExporter(cfg *config.TracingConfig) (trace.SpanExporter, error) {
var client otlptrace.Client
opts := []otlptracegrpc.Option{
otlptracegrpc.WithEndpoint(cfg.HostURL.String()),
otlptracegrpc.WithEndpoint(cfg.Host),
otlptracegrpc.WithTimeout(exportTimeout),
}
if cfg.Insecure {
Expand Down

0 comments on commit 6ed6df2

Please sign in to comment.