diff --git a/config/libconfig_json.go b/config/libconfig_json.go index fdb1bf0bc..6e803257b 100644 --- a/config/libconfig_json.go +++ b/config/libconfig_json.go @@ -265,11 +265,18 @@ type RPCConnsJson struct { // Represents one connection instance towards a rater/cdrs server type RemoteHostJson struct { - Id *string - Address *string - Transport *string - Synchronous *bool - Tls *bool + Id *string + Address *string + Transport *string + Synchronous *bool + Tls *bool + Key_path *string + Cert_path *string + Ca_path *string + Conn_attempts *int + Reconnects *int + Connect_timeout *string + Reply_timeout *string } type AstConnJsonCfg struct { diff --git a/config/rpcconn.go b/config/rpcconn.go index 7e81fdfde..9754ee0b7 100644 --- a/config/rpcconn.go +++ b/config/rpcconn.go @@ -19,6 +19,8 @@ along with this program. If not, see package config import ( + "time" + "github.com/cgrates/cgrates/utils" "github.com/cgrates/rpcclient" ) @@ -117,14 +119,21 @@ func (rC RPCConn) Clone() (cln *RPCConn) { // RemoteHost connection config type RemoteHost struct { - ID string - Address string - Transport string - Synchronous bool - TLS bool + ID string + Address string + Transport string + Synchronous bool + ConnectAttempts int + Reconnects int + ConnectTimeout time.Duration + ReplyTimeout time.Duration + TLS bool + ClientKey string + ClientCertificate string + CaCertificate string } -func (rh *RemoteHost) loadFromJSONCfg(jsnCfg *RemoteHostJson) { +func (rh *RemoteHost) loadFromJSONCfg(jsnCfg *RemoteHostJson) (err error) { if jsnCfg == nil { return } @@ -146,6 +155,32 @@ func (rh *RemoteHost) loadFromJSONCfg(jsnCfg *RemoteHostJson) { if jsnCfg.Tls != nil { rh.TLS = *jsnCfg.Tls } + if jsnCfg.Key_path != nil { + rh.ClientKey = *jsnCfg.Key_path + } + if jsnCfg.Cert_path != nil { + rh.ClientCertificate = *jsnCfg.Cert_path + } + if jsnCfg.Ca_path != nil { + rh.CaCertificate = *jsnCfg.Ca_path + } + if jsnCfg.Conn_attempts != nil { + rh.ConnectAttempts = *jsnCfg.Conn_attempts + } + if jsnCfg.Reconnects != nil { + rh.Reconnects = *jsnCfg.Reconnects + } + if jsnCfg.Connect_timeout != nil { + if rh.ConnectTimeout, err = utils.ParseDurationWithNanosecs(*jsnCfg.Connect_timeout); err != nil { + return err + } + } + if jsnCfg.Reply_timeout != nil { + if rh.ReplyTimeout, err = utils.ParseDurationWithNanosecs(*jsnCfg.Reply_timeout); err != nil { + return err + } + } + return } // AsMapInterface returns the config as a map[string]interface{} @@ -161,7 +196,28 @@ func (rh *RemoteHost) AsMapInterface() (mp map[string]interface{}) { mp[utils.SynchronousCfg] = rh.Synchronous } if rh.TLS { - mp[utils.TLS] = rh.TLS + mp[utils.TLSNoCaps] = rh.TLS + } + if rh.ClientKey != utils.EmptyString { + mp[utils.KeyPathCgr] = rh.ClientKey + } + if rh.ClientCertificate != utils.EmptyString { + mp[utils.CertPathCgr] = rh.ClientCertificate + } + if rh.CaCertificate != utils.EmptyString { + mp[utils.CAPathCgr] = rh.CaCertificate + } + if rh.ConnectAttempts != 0 { + mp[utils.ConnectAttemptsCfg] = rh.ConnectAttempts + } + if rh.Reconnects != 0 { + mp[utils.ReconnectsCfg] = rh.Reconnects + } + if rh.ConnectTimeout != 0 { + mp[utils.ConnectTimeoutCfg] = rh.ConnectTimeout + } + if rh.ReplyTimeout != 0 { + mp[utils.ReplyTimeoutCfg] = rh.ReplyTimeout } return } @@ -169,11 +225,18 @@ func (rh *RemoteHost) AsMapInterface() (mp map[string]interface{}) { // Clone returns a deep copy of RemoteHost func (rh RemoteHost) Clone() (cln *RemoteHost) { return &RemoteHost{ - ID: rh.ID, - Address: rh.Address, - Transport: rh.Transport, - Synchronous: rh.Synchronous, - TLS: rh.TLS, + ID: rh.ID, + Address: rh.Address, + Transport: rh.Transport, + Synchronous: rh.Synchronous, + ConnectAttempts: rh.ConnectAttempts, + Reconnects: rh.Reconnects, + ConnectTimeout: rh.ConnectTimeout, + ReplyTimeout: rh.ReplyTimeout, + TLS: rh.TLS, + ClientKey: rh.ClientKey, + ClientCertificate: rh.ClientCertificate, + CaCertificate: rh.CaCertificate, } } @@ -191,7 +254,14 @@ func UpdateRPCCons(rpcConns RPCConns, newHosts map[string]*RemoteHost) (connIDs rh.Address = newHost.Address rh.Transport = newHost.Transport rh.Synchronous = newHost.Synchronous + rh.ConnectAttempts = newHost.ConnectAttempts + rh.Reconnects = newHost.Reconnects + rh.ConnectTimeout = newHost.ConnectTimeout + rh.ReplyTimeout = newHost.ReplyTimeout rh.TLS = newHost.TLS + rh.ClientKey = newHost.ClientKey + rh.ClientCertificate = newHost.ClientCertificate + rh.CaCertificate = newHost.CaCertificate } } return @@ -210,7 +280,14 @@ func RemoveRPCCons(rpcConns RPCConns, hosts utils.StringSet) (connIDs utils.Stri rh.Address = "" rh.Transport = "" rh.Synchronous = false + rh.ConnectAttempts = 0 + rh.Reconnects = 0 + rh.ConnectTimeout = 0 + rh.ReplyTimeout = 0 rh.TLS = false + rh.ClientKey = "" + rh.ClientCertificate = "" + rh.CaCertificate = "" } } return diff --git a/config/rpcconn_test.go b/config/rpcconn_test.go index a785767ed..212679080 100644 --- a/config/rpcconn_test.go +++ b/config/rpcconn_test.go @@ -21,6 +21,7 @@ package config import ( "reflect" "testing" + "time" "github.com/cgrates/cgrates/utils" "github.com/cgrates/rpcclient" @@ -32,10 +33,17 @@ func TestRPCConnsloadFromJsonCfgCase1(t *testing.T) { PoolSize: utils.IntPointer(1), Conns: &[]*RemoteHostJson{ { - Address: utils.StringPointer("127.0.0.1:2012"), - Transport: utils.StringPointer("*json"), - Synchronous: utils.BoolPointer(false), - Tls: utils.BoolPointer(false), + Address: utils.StringPointer("127.0.0.1:2012"), + Transport: utils.StringPointer("*json"), + Synchronous: utils.BoolPointer(false), + Tls: utils.BoolPointer(false), + Key_path: utils.StringPointer("key_path"), + Cert_path: utils.StringPointer("cert_path"), + Ca_path: utils.StringPointer("ca_path"), + Conn_attempts: utils.IntPointer(5), + Reconnects: utils.IntPointer(2), + Connect_timeout: utils.StringPointer("1m"), + Reply_timeout: utils.StringPointer("1m"), }, }, } @@ -78,10 +86,17 @@ func TestRPCConnsloadFromJsonCfgCase1(t *testing.T) { PoolSize: 1, Conns: []*RemoteHost{ { - Address: "127.0.0.1:2012", - Transport: "*json", - Synchronous: false, - TLS: false, + Address: "127.0.0.1:2012", + Transport: "*json", + Synchronous: false, + ConnectAttempts: 5, + Reconnects: 2, + ConnectTimeout: 1 * time.Minute, + ReplyTimeout: 1 * time.Minute, + TLS: false, + ClientKey: "key_path", + ClientCertificate: "cert_path", + CaCertificate: "ca_path", }, }, }, @@ -135,10 +150,17 @@ func TestRPCConnsloadFromJsonCfgCase2(t *testing.T) { PoolSize: 0, Conns: []*RemoteHost{ { - Address: "127.0.0.1:2012", - Transport: "*json", - Synchronous: false, - TLS: false, + Address: "127.0.0.1:2012", + Transport: "*json", + Synchronous: false, + ConnectAttempts: 0, + Reconnects: 0, + ConnectTimeout: 0 * time.Minute, + ReplyTimeout: 0 * time.Minute, + TLS: false, + ClientKey: "", + ClientCertificate: "", + CaCertificate: "", }, }, }, @@ -154,7 +176,22 @@ func TestRPCConnsAsMapInterface(t *testing.T) { cfgJSONStr := `{ "rpc_conns": { "*localhost": { - "conns": [{"address": "127.0.0.1:2012", "transport":"*json"}], + "conns": [ + { + "address": "127.0.0.1:2012", + "transport":"*json", + "id": "id_example", + "synchronous": true, + "tls": true, + "key_path": "path_to_key", + "cert_path": "path_to_cert", + "ca_path": "path_to_ca", + "connect_attempts": 5, + "reconnects": 3, + "connect_timeout": "1m", + "reply_timeout": "1m" + } + ], }, }, }` @@ -174,8 +211,17 @@ func TestRPCConnsAsMapInterface(t *testing.T) { utils.StrategyCfg: utils.MetaFirst, utils.Conns: []map[string]interface{}{ { - utils.AddressCfg: "127.0.0.1:2012", - utils.TransportCfg: "*json", + utils.AddressCfg: "127.0.0.1:2012", + utils.TransportCfg: "*json", + utils.IDCfg: "id_example", + utils.SynchronousCfg: true, + utils.TLSNoCaps: true, + utils.KeyPathCgr: "path_to_key", + utils.CertPathCgr: "path_to_cert", + utils.CAPathCgr: "path_to_ca", + utils.ReconnectsCfg: 3, + utils.ConnectTimeoutCfg: 1 * time.Minute, + utils.ReplyTimeoutCfg: 1 * time.Minute, }, }, }, @@ -212,7 +258,7 @@ func TestRpcConnAsMapInterface1(t *testing.T) { "rpc_conns": { "*localhost": { "conns": [ - {"address": "127.0.0.1:2018", "TLS": true, "synchronous": true, "transport": "*json"}, + {"address": "127.0.0.1:2018", "tls": true, "synchronous": true, "transport": "*json"}, ], "poolSize": 2, }, @@ -252,7 +298,7 @@ func TestRpcConnAsMapInterface1(t *testing.T) { utils.MetaLocalHost: map[string]interface{}{ utils.Conns: []map[string]interface{}{ { - utils.TLS: true, + utils.TLSNoCaps: true, utils.AddressCfg: "127.0.0.1:2018", utils.SynchronousCfg: true, utils.TransportCfg: "*json", @@ -288,10 +334,17 @@ func TestRPCConnsClone(t *testing.T) { PoolSize: 1, Conns: []*RemoteHost{ { - Address: "127.0.0.1:2012", - Transport: "*json", - Synchronous: false, - TLS: false, + Address: "127.0.0.1:2012", + Transport: "*json", + Synchronous: false, + ConnectAttempts: 0, + Reconnects: 0, + ConnectTimeout: 1 * time.Minute, + ReplyTimeout: 1 * time.Minute, + TLS: false, + ClientKey: "", + ClientCertificate: "", + CaCertificate: "", }, }, }, @@ -331,11 +384,18 @@ func TestUpdateRPCCons(t *testing.T) { newHosts := map[string]*RemoteHost{ "RPC1": { - ID: "RPC1", - Address: utils.MetaInternal, - Transport: utils.EmptyString, - Synchronous: true, - TLS: true, + ID: "RPC1", + Address: utils.MetaInternal, + Transport: utils.EmptyString, + Synchronous: true, + ConnectAttempts: 2, + Reconnects: 2, + ConnectTimeout: 1 * time.Minute, + ReplyTimeout: 1 * time.Minute, + TLS: true, + ClientKey: "key", + ClientCertificate: "cert", + CaCertificate: "ca", }, } expectedID := utils.StringSet{utils.MetaInternal: {}} @@ -345,11 +405,18 @@ func TestUpdateRPCCons(t *testing.T) { PoolSize: 0, Conns: []*RemoteHost{ { - ID: "RPC1", - Address: utils.MetaInternal, - Transport: utils.EmptyString, - Synchronous: true, - TLS: true, + ID: "RPC1", + Address: utils.MetaInternal, + Transport: utils.EmptyString, + Synchronous: true, + ConnectAttempts: 2, + Reconnects: 2, + ConnectTimeout: 1 * time.Minute, + ReplyTimeout: 1 * time.Minute, + TLS: true, + ClientKey: "key", + ClientCertificate: "cert", + CaCertificate: "ca", }, { ID: "RPC2", @@ -383,11 +450,18 @@ func TestRemoveRPCCons(t *testing.T) { TLS: false, }, { - ID: "RPC2", - Address: utils.MetaInternal, - Transport: utils.EmptyString, - Synchronous: false, - TLS: false, + ID: "RPC2", + Address: utils.MetaInternal, + Transport: utils.EmptyString, + Synchronous: false, + ConnectAttempts: 2, + Reconnects: 2, + ConnectTimeout: 1 * time.Minute, + ReplyTimeout: 1 * time.Minute, + TLS: false, + ClientKey: "key", + ClientCertificate: "cert", + CaCertificate: "ca", }, }, },