From 6ed6df2f83d18a1ccfc2da5b47016a4d19a22877 Mon Sep 17 00:00:00 2001 From: evlekht Date: Fri, 22 Nov 2024 13:44:48 +0400 Subject: [PATCH] Fix config to allow ip addr in host fields (#66) --- .gitignore | 1 + config/config.go | 97 +++++++++++------------------ config/config_reader.go | 48 ++++++-------- examples/rpc/client.go | 5 +- internal/app/app.go | 4 +- internal/matrix/matrix_messenger.go | 2 +- internal/rpc/client/client.go | 2 +- internal/tracing/exporter.go | 2 +- 8 files changed, 61 insertions(+), 100 deletions(-) diff --git a/.gitignore b/.gitignore index 8d0111b7..80354605 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ __debug* ./camino-messenger-bot-supplier.yaml ./camino-messenger-bot-distributor.yaml +cmb-config distributor-bot-db supplier-bot-db diff --git a/config/config.go b/config/config.go index e3f49560..d987b98f 100644 --- a/config/config.go +++ b/config/config.go @@ -4,7 +4,6 @@ import ( "crypto/ecdsa" "encoding/hex" "math/big" - "net/url" "time" "github.com/ethereum/go-ethereum/common" @@ -21,7 +20,7 @@ type Config struct { BotKey *ecdsa.PrivateKey CMAccountAddress common.Address - ChainRPCURL url.URL + ChainRPCURL string BookingTokenAddress common.Address NetworkFeeRecipientBotAddress common.Address @@ -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 @@ -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"` @@ -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(), diff --git a/config/config_reader.go b/config/config_reader.go index 4b031bbe..dd2934f0 100644 --- a/config/config_reader.go +++ b/config/config_reader.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "math/big" - "net/url" "os" "strings" "time" @@ -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 } @@ -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), diff --git a/examples/rpc/client.go b/examples/rpc/client.go index f1788767..777a362f 100755 --- a/examples/rpc/client.go +++ b/examples/rpc/client.go @@ -5,7 +5,6 @@ import ( "encoding/csv" "flag" "fmt" - "net/url" "os" "sort" "strconv" @@ -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 diff --git a/internal/app/app.go b/internal/app/app.go index 9dab0b61..e771c574 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -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 @@ -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, diff --git a/internal/matrix/matrix_messenger.go b/internal/matrix/matrix_messenger.go index 2782ead4..9ddbba48 100644 --- a/internal/matrix/matrix_messenger.go +++ b/internal/matrix/matrix_messenger.go @@ -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 diff --git a/internal/rpc/client/client.go b/internal/rpc/client/client.go index b9cb2240..4fa8a5f4 100644 --- a/internal/rpc/client/client.go +++ b/internal/rpc/client/client.go @@ -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 } diff --git a/internal/tracing/exporter.go b/internal/tracing/exporter.go index 683861b4..7a0f3a1d 100644 --- a/internal/tracing/exporter.go +++ b/internal/tracing/exporter.go @@ -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 {