diff --git a/config/apiban.go b/config/apiban.go new file mode 100644 index 000000000..93b0ba15d --- /dev/null +++ b/config/apiban.go @@ -0,0 +1,50 @@ +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package config + +import "github.com/cgrates/cgrates/utils" + +// APIBanCfg the config for the APIBan Keys +type APIBanCfg struct { + Enabled bool + 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 { + ban.Keys[i] = key + } + } + return nil +} + +func (ban *APIBanCfg) AsMapInterface() map[string]interface{} { + return map[string]interface{}{ + utils.EnabledCfg: ban.Enabled, + utils.KeysCfg: ban.Keys, + } +} diff --git a/config/apibancfg_test.go b/config/apibancfg_test.go new file mode 100644 index 000000000..675757f8f --- /dev/null +++ b/config/apibancfg_test.go @@ -0,0 +1,83 @@ +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ +package config + +import ( + "reflect" + "testing" + + "github.com/cgrates/cgrates/utils" +) + +func TestAPIBanCfgloadFromJsonCfg(t *testing.T) { + var alS, expected APIBanCfg + if err := alS.loadFromJsonCfg(nil); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(alS, expected) { + t.Errorf("Expected: %+v ,recived: %+v", expected, alS) + } + if err := alS.loadFromJsonCfg(new(APIBanJsonCfg)); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(alS, expected) { + t.Errorf("Expected: %+v ,recived: %+v", expected, alS) + } + cfgJSONStr := `{ + "apiban":{ // APIBan config + "enabled":false, // starts APIBan service: . + "keys": ["key1","key2"] + }, + +}` + expected = APIBanCfg{ + Enabled: false, + Keys: []string{"key1", "key2"}, + } + if jsnCfg, err := NewCgrJsonCfgFromBytes([]byte(cfgJSONStr)); err != nil { + t.Error(err) + } else if jsnalS, err := jsnCfg.ApiBanCfgJson(); err != nil { + t.Error(err) + } else if err = alS.loadFromJsonCfg(jsnalS); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, alS) { + t.Errorf("Expected: %+v , recived: %+v", expected, alS) + } +} + +func TestAPIBanCfgAsMapInterface(t *testing.T) { + var alS APIBanCfg + cfgJSONStr := `{ + "apiban":{ + "enabled":false, + "keys": ["key1","key2"] + }, + +}` + eMap := map[string]interface{}{ + "enabled": false, + "keys": []string{"key1", "key2"}, + } + if jsnCfg, err := NewCgrJsonCfgFromBytes([]byte(cfgJSONStr)); err != nil { + t.Error(err) + } else if jsnalS, err := jsnCfg.ApiBanCfgJson(); err != nil { + t.Error(err) + } else if err = alS.loadFromJsonCfg(jsnalS); err != nil { + t.Error(err) + } else if rcv := alS.AsMapInterface(); !reflect.DeepEqual(eMap, rcv) { + t.Errorf("\nExpected: %+v\nRecived: %+v", utils.ToJSON(eMap), utils.ToJSON(rcv)) + } +} diff --git a/config/config.go b/config/config.go index ba7f48d94..cc65d88e2 100755 --- a/config/config.go +++ b/config/config.go @@ -191,6 +191,7 @@ func NewDefaultCGRConfig() (cfg *CGRConfig, err error) { cfg.rateSCfg = new(RateSCfg) cfg.sipAgentCfg = new(SIPAgentCfg) cfg.configSCfg = new(ConfigSCfg) + cfg.apiBanCfg = new(APIBanCfg) cfg.ConfigReloads = make(map[string]chan struct{}) @@ -314,6 +315,7 @@ type CGRConfig struct { rateSCfg *RateSCfg // RateS config sipAgentCfg *SIPAgentCfg // SIPAgent config configSCfg *ConfigSCfg //ConfigS config + apiBanCfg *APIBanCfg // APIBan config } var posibleLoaderTypes = utils.NewStringSet([]string{utils.MetaAttributes, @@ -380,7 +382,7 @@ func (cfg *CGRConfig) loadFromJsonCfg(jsnCfg *CgrJsonCfg) (err error) { cfg.loadLoaderCgrCfg, cfg.loadMigratorCgrCfg, cfg.loadTlsCgrCfg, cfg.loadAnalyzerCgrCfg, cfg.loadApierCfg, cfg.loadErsCfg, cfg.loadEesCfg, cfg.loadRateSCfg, cfg.loadSIPAgentCfg, cfg.loadDispatcherHCfg, - cfg.loadConfigSCfg} { + cfg.loadConfigSCfg, cfg.loadAPIBanCgrCfg} { if err = loadFunc(jsnCfg); err != nil { return } @@ -720,6 +722,15 @@ func (cfg *CGRConfig) loadAnalyzerCgrCfg(jsnCfg *CgrJsonCfg) (err error) { return cfg.analyzerSCfg.loadFromJsonCfg(jsnAnalyzerCgrCfg) } +// loadAPIBanCgrCfg loads the Analyzer section of the configuration +func (cfg *CGRConfig) loadAPIBanCgrCfg(jsnCfg *CgrJsonCfg) (err error) { + var jsnAnalyzerCgrCfg *APIBanJsonCfg + if jsnAnalyzerCgrCfg, err = jsnCfg.ApiBanCfgJson(); err != nil { + return + } + return cfg.apiBanCfg.loadFromJsonCfg(jsnAnalyzerCgrCfg) +} + // loadApierCfg loads the Apier section of the configuration func (cfg *CGRConfig) loadApierCfg(jsnCfg *CgrJsonCfg) (err error) { var jsnApierCfg *ApierJsonCfg @@ -1080,6 +1091,13 @@ func (cfg *CGRConfig) ConfigSCfg() *ConfigSCfg { return cfg.configSCfg } +// APIBanCfg reads the Apier configuration +func (cfg *CGRConfig) APIBanCfg() *APIBanCfg { + cfg.lks[APIBanCfgJson].Lock() + defer cfg.lks[APIBanCfgJson].Unlock() + return cfg.apiBanCfg +} + // GetReloadChan returns the reload chanel for the given section func (cfg *CGRConfig) GetReloadChan(sectID string) chan struct{} { return cfg.rldChans[sectID] @@ -1176,6 +1194,8 @@ func (cfg *CGRConfig) V1GetConfigSection(args *SectionWithOpts, reply *map[strin jsonString = utils.ToJSON(cfg.TemplatesCfg()) case ConfigSJson: jsonString = utils.ToJSON(cfg.ConfigSCfg()) + case APIBanCfgJson: + jsonString = utils.ToJSON(cfg.APIBanCfg()) default: return errors.New("Invalid section") } @@ -1304,6 +1324,7 @@ func (cfg *CGRConfig) getLoadFunctions() map[string]func(*CgrJsonCfg) error { SIPAgentJson: cfg.loadSIPAgentCfg, TemplatesJson: cfg.loadTemplateSCfg, ConfigSJson: cfg.loadConfigSCfg, + APIBanCfgJson: cfg.loadAPIBanCgrCfg, } } @@ -1527,6 +1548,7 @@ func (cfg *CGRConfig) reloadSections(sections ...string) (err error) { case STORDB_JSN: // reloaded before case LISTEN_JSN: case TlsCfgJson: // nothing to reload + case APIBanCfgJson: // nothing to reload case HTTP_JSN: case SCHEDULER_JSN: cfg.rldChans[SCHEDULER_JSN] <- struct{}{} @@ -1638,5 +1660,6 @@ func (cfg *CGRConfig) AsMapInterface(separator string) map[string]interface{} { utils.AnalyzerSCfg: cfg.analyzerSCfg.AsMapInterface(), utils.Apier: cfg.apier.AsMapInterface(), utils.ErsCfg: cfg.ersCfg.AsMapInterface(separator), + APIBanCfgJson: cfg.apiBanCfg.AsMapInterface(), } } diff --git a/config/config_defaults.go b/config/config_defaults.go index 18b6841e5..4d543523f 100755 --- a/config/config_defaults.go +++ b/config/config_defaults.go @@ -1003,4 +1003,9 @@ const CGRATES_CFG_JSON = ` }, +"apiban": { + "enabled": false, + "keys": [], +}, + }` diff --git a/config/config_json.go b/config/config_json.go index 6e7264748..74738e669 100644 --- a/config/config_json.go +++ b/config/config_json.go @@ -64,6 +64,7 @@ const ( SIPAgentJson = "sip_agent" TemplatesJson = "templates" ConfigSJson = "configs" + APIBanCfgJson = "apiban" ) var ( @@ -71,7 +72,7 @@ var ( CACHE_JSN, FilterSjsn, RALS_JSN, CDRS_JSN, ERsJson, SessionSJson, AsteriskAgentJSN, FreeSWITCHAgentJSN, KamailioAgentJSN, DA_JSN, RA_JSN, HttpAgentJson, DNSAgentJson, ATTRIBUTE_JSN, ChargerSCfgJson, RESOURCES_JSON, STATS_JSON, THRESHOLDS_JSON, RouteSJson, LoaderJson, MAILER_JSN, SURETAX_JSON, CgrLoaderCfgJson, CgrMigratorCfgJson, DispatcherSJson, - AnalyzerCfgJson, ApierS, EEsJson, RateSJson, SIPAgentJson, DispatcherHJson, TemplatesJson, ConfigSJson} + AnalyzerCfgJson, ApierS, EEsJson, RateSJson, SIPAgentJson, DispatcherHJson, TemplatesJson, ConfigSJson, APIBanCfgJson} ) // Loads the json config out of io.Reader, eg other sources than file, maybe over http @@ -557,3 +558,15 @@ func (self CgrJsonCfg) ConfigSJsonCfg() (*ConfigSCfgJson, error) { } return cfg, nil } + +func (self CgrJsonCfg) ApiBanCfgJson() (*APIBanJsonCfg, error) { + rawCfg, hasKey := self[APIBanCfgJson] + if !hasKey { + return nil, nil + } + cfg := new(APIBanJsonCfg) + if err := json.Unmarshal(*rawCfg, cfg); err != nil { + return nil, err + } + return cfg, nil +} diff --git a/config/libconfig_json.go b/config/libconfig_json.go index c9d4d6cbd..332d8eb27 100755 --- a/config/libconfig_json.go +++ b/config/libconfig_json.go @@ -652,3 +652,8 @@ type ConfigSCfgJson struct { Url *string Root_dir *string } + +type APIBanJsonCfg struct { + Enabled *bool + Keys *[]string +} diff --git a/utils/consts.go b/utils/consts.go index c9f03eafb..65385227c 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -2292,7 +2292,11 @@ const ( AnalyzerSCfg = "analyzers" // from JSON Apier = "apiers" // from JSON ErsCfg = "ers" // from JSON +) +// APIBanCfg +const ( + KeysCfg = "keys" ) // STIR/SHAKEN