mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-17 22:29:55 +05:00
RADIUS Auth with integration tests
This commit is contained in:
@@ -417,7 +417,7 @@ func fieldOutVal(m *diam.Message, cfgFld *config.CfgCdrField, extraParam interfa
|
||||
case utils.MetaGrouped: // GroupedAVP
|
||||
outVal = composedFieldvalue(m, cfgFld.Value, passAtIndex, processorVars)
|
||||
}
|
||||
if fmtValOut, err = utils.FmtFieldWidth(outVal, cfgFld.Width, cfgFld.Strip, cfgFld.Padding, cfgFld.Mandatory); err != nil {
|
||||
if fmtValOut, err = utils.FmtFieldWidth(cfgFld.Tag, outVal, cfgFld.Width, cfgFld.Strip, cfgFld.Padding, cfgFld.Mandatory); err != nil {
|
||||
utils.Logger.Warning(fmt.Sprintf("<Diameter> Error when processing field template with tag: %s, error: %s", cfgFld.Tag, err.Error()))
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -126,7 +126,7 @@ func radFieldOutVal(pkt *radigo.Packet, processorVars map[string]string,
|
||||
default:
|
||||
return "", fmt.Errorf("unsupported configuration field type: <%s>", cfgFld.Type)
|
||||
}
|
||||
if outVal, err = utils.FmtFieldWidth(outVal, cfgFld.Width, cfgFld.Strip, cfgFld.Padding, cfgFld.Mandatory); err != nil {
|
||||
if outVal, err = utils.FmtFieldWidth(cfgFld.Tag, outVal, cfgFld.Width, cfgFld.Strip, cfgFld.Padding, cfgFld.Mandatory); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return
|
||||
|
||||
@@ -45,6 +45,7 @@ ATTRIBUTE Password 2 string
|
||||
|
||||
# Alias values
|
||||
VALUE Framed-Protocol PPP 1
|
||||
VALUE Service-Type SIP-Caller-AVPs 30 # Proprietary, avp_radius
|
||||
|
||||
# Vendors
|
||||
VENDOR Cisco 9
|
||||
|
||||
@@ -73,9 +73,8 @@ type RadiusAgent struct {
|
||||
// handleAuth handles RADIUS Authorization request
|
||||
func (ra *RadiusAgent) handleAuth(req *radigo.Packet) (rpl *radigo.Packet, err error) {
|
||||
req.SetAVPValues() // populate string values in AVPs
|
||||
utils.Logger.Debug(fmt.Sprintf("RadiusAgent handleAuth, received request: %+v", req))
|
||||
procVars := map[string]string{
|
||||
MetaRadAuth: "true",
|
||||
MetaRadReqType: MetaRadAuth,
|
||||
}
|
||||
rpl = req.Reply()
|
||||
rpl.Code = radigo.AccessAccept
|
||||
@@ -103,16 +102,15 @@ func (ra *RadiusAgent) handleAuth(req *radigo.Packet) (rpl *radigo.Packet, err e
|
||||
// supports: Acct-Status-Type = Start, Interim-Update, Stop
|
||||
func (ra *RadiusAgent) handleAcct(req *radigo.Packet) (rpl *radigo.Packet, err error) {
|
||||
req.SetAVPValues() // populate string values in AVPs
|
||||
utils.Logger.Debug(fmt.Sprintf("Received request: %s", utils.ToJSON(req)))
|
||||
procVars := make(map[string]string)
|
||||
if avps := req.AttributesWithName("Acct-Status-Type", ""); len(avps) != 0 { // populate accounting type
|
||||
switch avps[0].GetStringValue() { // first AVP found will give out the type of accounting
|
||||
case "Start":
|
||||
procVars[MetaRadAcctStart] = "true"
|
||||
procVars[MetaRadReqType] = MetaRadAcctStart
|
||||
case "Interim-Update":
|
||||
procVars[MetaRadAcctUpdate] = "true"
|
||||
procVars[MetaRadReqType] = MetaRadAcctUpdate
|
||||
case "Stop":
|
||||
procVars[MetaRadAcctStop] = "true"
|
||||
procVars[MetaRadReqType] = MetaRadAcctStop
|
||||
}
|
||||
}
|
||||
rpl = req.Reply()
|
||||
@@ -144,6 +142,7 @@ func (ra *RadiusAgent) processRequest(reqProcessor *config.RARequestProcessor,
|
||||
for _, fldFilter := range reqProcessor.RequestFilter {
|
||||
if !radPassesFieldFilter(req, processorVars, fldFilter) {
|
||||
passesAllFilters = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if !passesAllFilters { // Not going with this processor further
|
||||
|
||||
138
agents/radagent_it_test.go
Normal file
138
agents/radagent_it_test.go
Normal file
@@ -0,0 +1,138 @@
|
||||
// +build integration
|
||||
|
||||
/*
|
||||
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 <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package agents
|
||||
|
||||
import (
|
||||
"net/rpc"
|
||||
"net/rpc/jsonrpc"
|
||||
"path"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cgrates/cgrates/config"
|
||||
"github.com/cgrates/cgrates/engine"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
"github.com/cgrates/radigo"
|
||||
)
|
||||
|
||||
var raCfgPath string
|
||||
var raCfg *config.CGRConfig
|
||||
var raSMGrpc *rpc.Client
|
||||
var raAuthClnt, raAcctClnt *radigo.Client
|
||||
|
||||
func TestRAitInitCfg(t *testing.T) {
|
||||
raCfgPath = path.Join(*dataDir, "conf", "samples", "radagent")
|
||||
// Init config first
|
||||
var err error
|
||||
raCfg, err = config.NewCGRConfigFromFolder(raCfgPath)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
raCfg.DataFolderPath = *dataDir // Share DataFolderPath through config towards StoreDb for Flush()
|
||||
config.SetCgrConfig(raCfg)
|
||||
}
|
||||
|
||||
// Remove data in both rating and accounting db
|
||||
func TestRAitResetDataDb(t *testing.T) {
|
||||
if err := engine.InitDataDb(raCfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Wipe out the cdr database
|
||||
func TestRAitResetStorDb(t *testing.T) {
|
||||
if err := engine.InitStorDb(raCfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Start CGR Engine
|
||||
func TestRAitStartEngine(t *testing.T) {
|
||||
if _, err := engine.StopStartEngine(raCfgPath, *waitRater); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Connect rpc client to rater
|
||||
func TestRAitApierRpcConn(t *testing.T) {
|
||||
var err error
|
||||
raSMGrpc, err = jsonrpc.Dial("tcp", raCfg.RPCJSONListen) // We connect over JSON so we can also troubleshoot if needed
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Load the tariff plan, creating accounts and their balances
|
||||
func TestRAitTPFromFolder(t *testing.T) {
|
||||
attrs := &utils.AttrLoadTpFromFolder{FolderPath: path.Join(*dataDir, "tariffplans", "tutorial")}
|
||||
var loadInst utils.LoadInstance
|
||||
if err := raSMGrpc.Call("ApierV2.LoadTariffPlanFromFolder", attrs, &loadInst); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
time.Sleep(time.Duration(*waitRater) * time.Millisecond) // Give time for scheduler to execute topups
|
||||
}
|
||||
|
||||
func TestRAitAuth(t *testing.T) {
|
||||
if raAuthClnt, err = radigo.NewClient("udp", "127.0.0.1:1812", "CGRateS.org", dictRad, 1, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
authReq := raAuthClnt.NewRequest(radigo.AccessRequest, 1) // emulates Kamailio packet out of radius_load_caller_avps()
|
||||
if err := authReq.AddAVPWithName("User-Name", "1001", ""); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := authReq.AddAVPWithName("Service-Type", "SIP-Caller-AVPs", ""); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := authReq.AddAVPWithName("Called-Station-Id", "1002", ""); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := authReq.AddAVPWithName("Acct-Session-Id", "2ca13afce9b2d76e15de7e1ec6568fc8@0:0:0:0:0:0:0:0", ""); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := authReq.AddAVPWithName("Sip-From-Tag", "7f30055f", ""); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := authReq.AddAVPWithName("NAS-IP-Address", "127.0.0.1", ""); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := authReq.AddAVPWithName("Event-Timestamp", "1497106115", ""); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
reply, err := raAuthClnt.SendRequest(authReq)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if reply.Code != radigo.AccessAccept {
|
||||
t.Errorf("Received reply: %+v", reply)
|
||||
}
|
||||
if len(reply.AVPs) != 1 { // make sure max duration is received
|
||||
t.Errorf("Received AVPs: %+v", reply.AVPs)
|
||||
} else if !reflect.DeepEqual([]byte("session_max_time#10800"), reply.AVPs[0].RawValue) {
|
||||
t.Errorf("Received: %s", string(reply.AVPs[0].RawValue))
|
||||
}
|
||||
}
|
||||
|
||||
func TestRAitStopCgrEngine(t *testing.T) {
|
||||
if err := engine.KillEngine(100); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user