diff --git a/apier/v1/attributes_it_test.go b/apier/v1/attributes_it_test.go index 24807784a..80674b86b 100644 --- a/apier/v1/attributes_it_test.go +++ b/apier/v1/attributes_it_test.go @@ -58,6 +58,7 @@ var ( testAttributeSProcessEventWithNoneSubstitute2, testAttributeSProcessEventWithNoneSubstitute3, testAttributeSProcessEventWithHeader, + //testAttributeSProcessEventSentryPeer, testAttributeSGetAttPrfIDs, testAttributeSSetAlsPrfBrokenReference, testAttributeSGetAlsPrfBeforeSet, @@ -747,16 +748,16 @@ func testAttributeSProcessEventWithHeader(t *testing.T) { // attrPrf1 := &engine.AttributeProfileWithAPIOpts{ // AttributeProfile: &engine.AttributeProfile{ // Tenant: config.CgrConfig().GeneralCfg().DefaultTenant, -// ID: "ATTR_SENTRY", +// ID: "ATTR_SENTRY_IP", // Contexts: []string{utils.MetaAny}, -// FilterIDs: []string{"*sentrypeer:~*req.IP:ip-addresses"}, +// FilterIDs: []string{"*sentrypeer:~*req.IP:*ip"}, // ActivationInterval: &utils.ActivationInterval{ // ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), // }, // Attributes: []*engine.Attribute{ // { -// Path: utils.MetaReq + utils.NestingSep + "Field2", -// Value: config.NewRSRParsersMustCompile("BLACKLIST", utils.InfieldSep), +// Path: utils.MetaReq + utils.NestingSep + "IP", +// Value: config.NewRSRParsersMustCompile("BLACKLIST_IP", utils.InfieldSep), // Type: utils.MetaConstant, // }, // }, @@ -770,21 +771,57 @@ func testAttributeSProcessEventWithHeader(t *testing.T) { // } else if result != utils.OK { // t.Error("Unexpected reply returned", result) // } +// attrPrf2 := &engine.AttributeProfileWithAPIOpts{ +// AttributeProfile: &engine.AttributeProfile{ +// Tenant: config.CgrConfig().GeneralCfg().DefaultTenant, +// ID: "ATTR_SENTRY_NUMBER", +// Contexts: []string{utils.MetaAny}, +// FilterIDs: []string{"*sentrypeer:~*req.Destination:*number"}, +// ActivationInterval: &utils.ActivationInterval{ +// ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), +// }, +// Attributes: []*engine.Attribute{ +// { +// Path: utils.MetaReq + utils.NestingSep + utils.Destination, +// Value: config.NewRSRParsersMustCompile("BLACKLIST_DEST", utils.InfieldSep), +// Type: utils.MetaConstant, +// }, +// }, +// Blocker: true, +// Weight: 5, +// }, +// } +// if err := attrSRPC.Call(utils.APIerSv1SetAttributeProfile, attrPrf2, &result); err != nil { +// t.Error(err) +// } else if result != utils.OK { +// t.Error("Unexpected reply returned", result) +// } // var reply *engine.AttributeProfile // if err := attrSRPC.Call(utils.APIerSv1GetAttributeProfile, -// utils.TenantIDWithAPIOpts{TenantID: &utils.TenantID{Tenant: "cgrates.org", ID: "ATTR_SENTRY"}}, &reply); err != nil { +// utils.TenantIDWithAPIOpts{TenantID: &utils.TenantID{Tenant: "cgrates.org", ID: "ATTR_SENTRY_IP"}}, &reply); err != nil { // t.Fatal(err) // } // reply.Compile() // if !reflect.DeepEqual(attrPrf1.AttributeProfile, reply) { // t.Errorf("Expecting : %+v, received: %+v", alsPrf.AttributeProfile, reply) // } + +// if err := attrSRPC.Call(utils.APIerSv1GetAttributeProfile, +// utils.TenantIDWithAPIOpts{TenantID: &utils.TenantID{Tenant: "cgrates.org", ID: "ATTR_SENTRY_NUMBER"}}, &reply); err != nil { +// t.Fatal(err) +// } +// reply.Compile() +// if !reflect.DeepEqual(attrPrf2.AttributeProfile, reply) { +// t.Errorf("Expecting : %+v, received: %+v", alsPrf.AttributeProfile, reply) +// } + // attrArgs := &utils.CGREvent{ // Tenant: config.CgrConfig().GeneralCfg().DefaultTenant, // ID: "HAttribute", // Event: map[string]any{ -// "IP": "45.155.91.135", +// "IP": "45.155.91.135", +// utils.Destination: "46423112877", // }, // APIOpts: map[string]any{ // utils.OptsAttributesProcessRuns: 1., @@ -792,14 +829,14 @@ func testAttributeSProcessEventWithHeader(t *testing.T) { // }, // } // eRply := &engine.AttrSProcessEventReply{ -// MatchedProfiles: []string{"cgrates.org:ATTR_SENTRY"}, -// AlteredFields: []string{"*req.Field2"}, +// MatchedProfiles: []string{"cgrates.org:ATTR_SENTRY_IP"}, +// AlteredFields: []string{"*req.IP"}, // CGREvent: &utils.CGREvent{ // Tenant: config.CgrConfig().GeneralCfg().DefaultTenant, // ID: "HAttribute", // Event: map[string]any{ -// "Field2": "BLACKLIST", -// "IP": "45.155.91.135", +// "IP": "BLACKLIST_IP", +// utils.Destination: "46423112877", // }, // APIOpts: map[string]any{ // utils.OptsAttributesProcessRuns: 1., diff --git a/config/apiban.go b/config/apiban.go index b839a3271..fccd4ed43 100644 --- a/config/apiban.go +++ b/config/apiban.go @@ -22,17 +22,13 @@ import "github.com/cgrates/cgrates/utils" // APIBanCfg the config for the APIBan Keys type APIBanCfg struct { - Enabled bool - Keys []string + Keys []string } func (ban *APIBanCfg) loadFromJSONCfg(jsnCfg *APIBanJsonCfg) (err error) { if jsnCfg == nil { return } - if jsnCfg.Enabled != nil { - ban.Enabled = *jsnCfg.Enabled - } if jsnCfg.Keys != nil { ban.Keys = make([]string, len(*jsnCfg.Keys)) for i, key := range *jsnCfg.Keys { @@ -45,16 +41,14 @@ func (ban *APIBanCfg) loadFromJSONCfg(jsnCfg *APIBanJsonCfg) (err error) { // AsMapInterface returns the config as a map[string]any func (ban *APIBanCfg) AsMapInterface() map[string]any { return map[string]any{ - utils.EnabledCfg: ban.Enabled, - utils.KeysCfg: ban.Keys, + utils.KeysCfg: ban.Keys, } } // Clone returns a deep copy of APIBanCfg func (ban APIBanCfg) Clone() (cln *APIBanCfg) { cln = &APIBanCfg{ - Enabled: ban.Enabled, - Keys: make([]string, len(ban.Keys)), + Keys: make([]string, len(ban.Keys)), } for i, k := range ban.Keys { cln.Keys[i] = k @@ -64,15 +58,15 @@ func (ban APIBanCfg) Clone() (cln *APIBanCfg) { type SentryPeerCfg struct { Token string - Addr string + Url string } func (sp *SentryPeerCfg) loadFromJSONCfg(jsnCfg *SentryPeerJsonCfg) (err error) { if jsnCfg == nil { return } - if jsnCfg.Addr != nil { - sp.Addr = *jsnCfg.Addr + if jsnCfg.Url != nil { + sp.Url = *jsnCfg.Url } if jsnCfg.Token != nil { sp.Token = *jsnCfg.Token @@ -82,14 +76,14 @@ func (sp *SentryPeerCfg) loadFromJSONCfg(jsnCfg *SentryPeerJsonCfg) (err error) func (sp *SentryPeerCfg) AsMapInterface() map[string]any { return map[string]any{ - "Addres": sp.Addr, - "Token": sp.Token, + "URL": sp.Url, + "Token": sp.Token, } } func (sp *SentryPeerCfg) Clone() (cln *SentryPeerCfg) { cln = &SentryPeerCfg{ - Addr: sp.Addr, + Url: sp.Url, Token: sp.Token, } return diff --git a/config/apibancfg_test.go b/config/apibancfg_test.go index b700776b1..eb03074a9 100644 --- a/config/apibancfg_test.go +++ b/config/apibancfg_test.go @@ -44,8 +44,7 @@ func TestAPIBanCfgloadFromJsonCfg(t *testing.T) { }` expected = APIBanCfg{ - Enabled: false, - Keys: []string{"key1", "key2"}, + Keys: []string{"key1", "key2"}, } if jsnCfg, err := NewCgrJsonCfgFromBytes([]byte(cfgJSONStr)); err != nil { t.Error(err) @@ -84,8 +83,7 @@ func TestAPIBanCfgAsMapInterface(t *testing.T) { func TestAPIBanCfgClone(t *testing.T) { ban := &APIBanCfg{ - Enabled: false, - Keys: []string{"key1", "key2"}, + Keys: []string{"key1", "key2"}, } rcv := ban.Clone() if !reflect.DeepEqual(ban, rcv) { diff --git a/config/config_defaults.go b/config/config_defaults.go index ea8a2b5cd..c9532d5b4 100644 --- a/config/config_defaults.go +++ b/config/config_defaults.go @@ -1267,7 +1267,7 @@ const CGRATES_CFG_JSON = ` "keys": [], }, "sentrypeer":{ - "addr":"", + "url":"", "token":"", } diff --git a/config/config_test.go b/config/config_test.go index 592113d19..84f9c27bf 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -2585,8 +2585,7 @@ func TestConfigsConfig(t *testing.T) { func TestAPIBanConfig(t *testing.T) { expected := &APIBanCfg{ - Enabled: false, - Keys: []string{}, + Keys: []string{}, } cgrConfig := NewDefaultCGRConfig() if err != nil { diff --git a/config/libconfig_json.go b/config/libconfig_json.go index b83830457..7c5912b38 100644 --- a/config/libconfig_json.go +++ b/config/libconfig_json.go @@ -878,7 +878,7 @@ type APIBanJsonCfg struct { type SentryPeerJsonCfg struct { Token *string - Addr *string + Url *string } type CoreSJsonCfg struct { diff --git a/data/conf/samples/tutinternal/cgrates.json b/data/conf/samples/tutinternal/cgrates.json index bbab7e051..422056db8 100644 --- a/data/conf/samples/tutinternal/cgrates.json +++ b/data/conf/samples/tutinternal/cgrates.json @@ -112,7 +112,7 @@ } // "sentrypeer":{ -// "addr":"https://sentrypeer.com/api/", +// "url":"https://sentrypeer.com/api/", // "token":"put token here", // }, diff --git a/data/conf/samples/tutmongo/cgrates.json b/data/conf/samples/tutmongo/cgrates.json index bcd0f4b2e..479fd592f 100644 --- a/data/conf/samples/tutmongo/cgrates.json +++ b/data/conf/samples/tutmongo/cgrates.json @@ -54,7 +54,7 @@ "stats_conns": ["*localhost"], "resources_conns": ["*localhost"], "apiers_conns": ["*localhost"], - "indexed_selects": false, +// "indexed_selects": false, }, @@ -127,9 +127,9 @@ "apiers_conns": ["*internal"], }, -// "sentrypeer":{ -// "addr":"https://sentrypeer.com/api/", -// "token":"put token here", -// }, + //"sentrypeer":{ + //"url":"https://sentrypeer.com/api/", + //"token":"" + //}, } diff --git a/data/conf/samples/tutmysql/cgrates.json b/data/conf/samples/tutmysql/cgrates.json index eef8044b2..5c1db20ec 100644 --- a/data/conf/samples/tutmysql/cgrates.json +++ b/data/conf/samples/tutmysql/cgrates.json @@ -119,7 +119,7 @@ }, // "sentrypeer":{ -// "addr":"https://sentrypeer.com/api/", +// "url":"https://sentrypeer.com/api/", // "token":"put token here", // }, diff --git a/data/conf/samples/tutpostgres/cgrates.json b/data/conf/samples/tutpostgres/cgrates.json index 08741245c..138d9e856 100644 --- a/data/conf/samples/tutpostgres/cgrates.json +++ b/data/conf/samples/tutpostgres/cgrates.json @@ -104,7 +104,7 @@ }, // "sentrypeer":{ -// "addr":"https://sentrypeer.com/api/", +// "url":"https://sentrypeer.com/api/", // "token":"put token here", // }, diff --git a/engine/datamanager.go b/engine/datamanager.go index b24089df2..4d61511c1 100644 --- a/engine/datamanager.go +++ b/engine/datamanager.go @@ -270,7 +270,7 @@ func (dm *DataManager) CacheDataFromDB(prfx string, ids []string, mustBeCached b case utils.MetaAPIBan: _, err = dm.GetAPIBan(utils.EmptyString, config.CgrConfig().APIBanCfg().Keys, false, false, true) case utils.MetaSentryPeer: - _, err = GetSentryPeer(utils.EmptyString, config.CgrConfig().SentryPeerCfg().Addr, config.CgrConfig().SentryPeerCfg().Token, utils.EmptyString, false, true) + _, err = GetSentryPeer(utils.EmptyString, config.CgrConfig().SentryPeerCfg().Url, config.CgrConfig().SentryPeerCfg().Token, utils.EmptyString, false, true) } if err != nil { if err != utils.ErrNotFound && err != utils.ErrDSPProfileNotFound && err != utils.ErrDSPHostNotFound { diff --git a/engine/filterhelpers.go b/engine/filterhelpers.go index 14052ea0a..431455723 100644 --- a/engine/filterhelpers.go +++ b/engine/filterhelpers.go @@ -121,19 +121,27 @@ func WeightFromDynamics(dWs []*utils.DynamicWeight, return 0.0, nil } -func GetSentryPeer(val, addr, token, path string, cacheRead, cacheWrite bool) (found bool, err error) { +func GetSentryPeer(val, url, token, path string, cacheRead, cacheWrite bool) (found bool, err error) { valpath := utils.ConcatenatedKey(path, val) if cacheRead { if x, ok := Cache.Get(utils.MetaSentryPeer, valpath); ok && x != nil { // Attempt to find in cache first return x.(bool), nil } } + switch path { + case "*ip": + url += "ip-addresses/" + case "*number": + url += "phone-numbers/" + } var req *http.Request - if req, err = http.NewRequest("GET", addr+path+"/"+val, nil); err != nil { + if req, err = http.NewRequest("GET", url+val, nil); err != nil { return } - req.Header = http.Header{ - "Authorization": {fmt.Sprintf("Bearer %s", token)}, + if token != "" { + req.Header = http.Header{ + "Authorization": {fmt.Sprintf("Bearer %s", token)}, + } } var resp *http.Response resp, err = http.DefaultClient.Do(req) diff --git a/engine/filters.go b/engine/filters.go index 63015cc30..5c8890995 100644 --- a/engine/filters.go +++ b/engine/filters.go @@ -656,10 +656,10 @@ func (fltr *FilterRule) passSentryPeer(dDP utils.DataProvider) (bool, error) { } return false, err } - if fltr.Values[0] != "phone-numbers" && fltr.Values[0] != "ip-addresses" { + if fltr.Values[0] != "*number" && fltr.Values[0] != "*ip" { return false, fmt.Errorf("invalid value for sentrypeer filter: <%s>", fltr.Values[0]) } - return GetSentryPeer(strVal, config.CgrConfig().SentryPeerCfg().Addr, config.CgrConfig().SentryPeerCfg().Token, fltr.Values[0], true, true) + return GetSentryPeer(strVal, config.CgrConfig().SentryPeerCfg().Url, config.CgrConfig().SentryPeerCfg().Token, fltr.Values[0], true, true) } func parseTime(rsr *config.RSRParser, dDp utils.DataProvider) (_ time.Time, err error) {