From 52de41c9bc95b3a314d436ed36c8309afb3d5264 Mon Sep 17 00:00:00 2001 From: Hoan Luu Huu <110280845+xquanluu@users.noreply.github.com> Date: Tue, 20 Aug 2024 08:44:15 +0700 Subject: [PATCH] support configuration to limit minimum value sipgatewa netmask can be used (#344) --- lib/routes/api/sip-gateways.js | 7 +++++ test/sip-gateways.js | 47 ++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/lib/routes/api/sip-gateways.js b/lib/routes/api/sip-gateways.js index 20b6a889..3c92a09c 100644 --- a/lib/routes/api/sip-gateways.js +++ b/lib/routes/api/sip-gateways.js @@ -41,6 +41,7 @@ const checkUserScope = async(req, voip_carrier_sid) => { const validate = async(req, sid) => { const {lookupSipGatewayBySid} = req.app.locals; + const {netmask} = req.body; let voip_carrier_sid; if (sid) { @@ -52,6 +53,12 @@ const validate = async(req, sid) => { voip_carrier_sid = req.body.voip_carrier_sid; if (!voip_carrier_sid) throw new DbErrorBadRequest('missing voip_carrier_sid'); } + if (netmask && + process.env.JAMBONZ_MIN_GATEWAY_NETMASK && + parseInt(netmask) < process.env.JAMBONZ_MIN_GATEWAY_NETMASK) { + throw new DbErrorBadRequest( + `netmask required to have value equal or greater than ${process.env.JAMBONZ_MIN_GATEWAY_NETMASK}`); + } await checkUserScope(req, voip_carrier_sid); }; diff --git a/test/sip-gateways.js b/test/sip-gateways.js index 7278c5e2..97ac8b42 100644 --- a/test/sip-gateways.js +++ b/test/sip-gateways.js @@ -17,6 +17,53 @@ test('sip gateway tests', async(t) => { let result; const voip_carrier_sid = await createVoipCarrier(request); + /* add a invalid sip gateway */ + const STORED_JAMBONZ_MIN_GATEWAY_NETMASK = process.env.JAMBONZ_MIN_GATEWAY_NETMASK; + process.env.JAMBONZ_MIN_GATEWAY_NETMASK = 24; + + result = await request.post('/SipGateways', { + resolveWithFullResponse: true, + auth: authAdmin, + json: true, + simple: false, + body: { + voip_carrier_sid, + ipv4: '1.2.3.4', + netmask: 1, + inbound: true, + outbound: true, + protocol: 'tcp' + } + }); + t.ok(result.statusCode === 400, 'successfully created sip gateway '); + + result = await request.post('/SipGateways', { + resolveWithFullResponse: true, + auth: authAdmin, + json: true, + body: { + voip_carrier_sid, + ipv4: '1.2.3.4', + netmask: 24, + inbound: true, + outbound: true, + protocol: 'tcp' + } + }); + t.ok(result.statusCode === 201, 'successfully created sip gateway '); + + process.env.JAMBONZ_MIN_GATEWAY_NETMASK = STORED_JAMBONZ_MIN_GATEWAY_NETMASK; + + /* delete sip gateways */ + result = await request.delete(`/SipGateways/${result.body.sid}`, { + resolveWithFullResponse: true, + simple: false, + json: true, + auth: authAdmin + }); + //console.log(`result: ${JSON.stringify(result)}`); + t.ok(result.statusCode === 204, 'successfully deleted sip gateway'); + /* add a sip gateway */ result = await request.post('/SipGateways', { resolveWithFullResponse: true,