mirror of
https://github.com/jambonz/jambonz-api-server.git
synced 2026-10-09 21:14:00 +00:00
add limits for adding account-level resources
This commit is contained in:
@@ -46,3 +46,4 @@ create_db.sql
|
|||||||
.env
|
.env
|
||||||
|
|
||||||
test/postgres-data
|
test/postgres-data
|
||||||
|
db/remove-account.sh
|
||||||
|
|||||||
+58
-1
@@ -168,6 +168,62 @@ const hasServiceProviderPermissions = (req, res, next) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const checkLimits = async(req, res, next) => {
|
||||||
|
const logger = req.app.locals.logger;
|
||||||
|
if (process.env.APPLY_JAMBONZ_DB_LIMITS && req.user.hasScope('account')) {
|
||||||
|
const account_sid = req.user.account_sid;
|
||||||
|
const url = req.originalUrl;
|
||||||
|
let sql;
|
||||||
|
let limit;
|
||||||
|
|
||||||
|
if (/Applications/.test(url)) {
|
||||||
|
limit = 50;
|
||||||
|
sql = 'SELECT count(*) as count from applications where account_sid = ?';
|
||||||
|
}
|
||||||
|
else if (/VoipCarriers/.test(url)) {
|
||||||
|
limit = 10;
|
||||||
|
sql = 'SELECT count(*) as count from voip_carriers where account_sid = ?';
|
||||||
|
}
|
||||||
|
else if (/SipGateways/.test(url)) {
|
||||||
|
limit = 150;
|
||||||
|
sql = `SELECT count(*) as count
|
||||||
|
from sip_gateways
|
||||||
|
where voip_carrier_sid IN (
|
||||||
|
SELECT voip_carrier_sid from voip_carriers
|
||||||
|
where account_sid = ?
|
||||||
|
)`;
|
||||||
|
}
|
||||||
|
else if (/PhoneNumbers/.test(url)) {
|
||||||
|
limit = 200;
|
||||||
|
sql = 'SELECT count(*) as count from phone_numbers where account_sid = ?';
|
||||||
|
}
|
||||||
|
else if (/SpeechCredentials/.test(url)) {
|
||||||
|
limit = 10;
|
||||||
|
sql = 'SELECT count(*) as count from speech_credentials where account_sid = ?';
|
||||||
|
}
|
||||||
|
else if (/ApiKeys/.test(url)) {
|
||||||
|
limit = 10;
|
||||||
|
sql = 'SELECT count(*) as count from api_keys where account_sid = ?';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sql) {
|
||||||
|
try {
|
||||||
|
const [r] = await promisePool.execute(sql, [account_sid]);
|
||||||
|
if (r[0].count >= limit) {
|
||||||
|
res.status(422).json({
|
||||||
|
status: 'fail',
|
||||||
|
message: `exceeded limits - you have created ${r.count} instances of this resource`
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
logger.error({err}, 'Error checking limits');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
setupFreeTrial,
|
setupFreeTrial,
|
||||||
createTestCdrs,
|
createTestCdrs,
|
||||||
@@ -175,5 +231,6 @@ module.exports = {
|
|||||||
parseAccountSid,
|
parseAccountSid,
|
||||||
parseServiceProviderSid,
|
parseServiceProviderSid,
|
||||||
hasAccountPermissions,
|
hasAccountPermissions,
|
||||||
hasServiceProviderPermissions
|
hasServiceProviderPermissions,
|
||||||
|
checkLimits
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,9 +5,17 @@ const path = require('path');
|
|||||||
const swaggerDocument = YAML.load(path.resolve(__dirname, '../swagger/swagger.yaml'));
|
const swaggerDocument = YAML.load(path.resolve(__dirname, '../swagger/swagger.yaml'));
|
||||||
const api = require('./api');
|
const api = require('./api');
|
||||||
const stripe = require('./stripe');
|
const stripe = require('./stripe');
|
||||||
|
const {checkLimits} = require('./api/utils');
|
||||||
|
|
||||||
const routes = express.Router();
|
const routes = express.Router();
|
||||||
|
|
||||||
|
routes.post([
|
||||||
|
'/v1/Applications',
|
||||||
|
'/v1/VoipCarriers',
|
||||||
|
'/v1/SipGateways',
|
||||||
|
'/v1/PhoneNumbers',
|
||||||
|
'/v1/Accounts'
|
||||||
|
], checkLimits);
|
||||||
routes.use('/v1', api);
|
routes.use('/v1', api);
|
||||||
routes.use('/stripe', stripe);
|
routes.use('/stripe', stripe);
|
||||||
routes.use('/swagger', swaggerUi.serve);
|
routes.use('/swagger', swaggerUi.serve);
|
||||||
|
|||||||
+1
-1
@@ -5,7 +5,7 @@
|
|||||||
"main": "app.js",
|
"main": "app.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node app.js",
|
"start": "node app.js",
|
||||||
"test": "NODE_ENV=test JAMBONES_CURRENCY=USD JWT_SECRET=foobarbazzle JAMBONES_MYSQL_HOST=127.0.0.1 JAMBONES_MYSQL_PORT=3360 JAMBONES_MYSQL_USER=jambones_test JAMBONES_MYSQL_PASSWORD=jambones_test JAMBONES_MYSQL_DATABASE=jambones_test JAMBONES_REDIS_HOST=localhost JAMBONES_REDIS_PORT=16379 JAMBONES_TIME_SERIES_HOST=127.0.0.1 JAMBONES_LOGLEVEL=error JAMBONES_CREATE_CALL_URL=http://localhost/v1/createCall node test/ ",
|
"test": "NODE_ENV=test APPLY_JAMBONZ_DB_LIMITS=1 JWT_SECRET=foobarbazzle JAMBONES_MYSQL_HOST=127.0.0.1 JAMBONES_MYSQL_PORT=3360 JAMBONES_MYSQL_USER=jambones_test JAMBONES_MYSQL_PASSWORD=jambones_test JAMBONES_MYSQL_DATABASE=jambones_test JAMBONES_REDIS_HOST=localhost JAMBONES_REDIS_PORT=16379 JAMBONES_TIME_SERIES_HOST=127.0.0.1 JAMBONES_LOGLEVEL=error JAMBONES_CREATE_CALL_URL=http://localhost/v1/createCall node test/ ",
|
||||||
"integration-test": "NODE_ENV=test JAMBONES_TIME_SERIES_HOST=127.0.0.1 AWS_REGION='us-east-1' JAMBONES_CURRENCY=USD JWT_SECRET=foobarbazzle JAMBONES_MYSQL_HOST=127.0.0.1 JAMBONES_MYSQL_PORT=3360 JAMBONES_MYSQL_USER=jambones_test JAMBONES_MYSQL_PASSWORD=jambones_test JAMBONES_MYSQL_DATABASE=jambones_test JAMBONES_REDIS_HOST=localhost JAMBONES_REDIS_PORT=16379 JAMBONES_LOGLEVEL=debug JAMBONES_CREATE_CALL_URL=http://localhost/v1/createCall node test/serve-integration.js",
|
"integration-test": "NODE_ENV=test JAMBONES_TIME_SERIES_HOST=127.0.0.1 AWS_REGION='us-east-1' JAMBONES_CURRENCY=USD JWT_SECRET=foobarbazzle JAMBONES_MYSQL_HOST=127.0.0.1 JAMBONES_MYSQL_PORT=3360 JAMBONES_MYSQL_USER=jambones_test JAMBONES_MYSQL_PASSWORD=jambones_test JAMBONES_MYSQL_DATABASE=jambones_test JAMBONES_REDIS_HOST=localhost JAMBONES_REDIS_PORT=16379 JAMBONES_LOGLEVEL=debug JAMBONES_CREATE_CALL_URL=http://localhost/v1/createCall node test/serve-integration.js",
|
||||||
"coverage": "./node_modules/.bin/nyc --reporter html --report-dir ./coverage npm run test",
|
"coverage": "./node_modules/.bin/nyc --reporter html --report-dir ./coverage npm run test",
|
||||||
"jslint": "eslint app.js lib"
|
"jslint": "eslint app.js lib"
|
||||||
|
|||||||
Reference in New Issue
Block a user