add limits for adding account-level resources

This commit is contained in:
Dave Horton
2021-08-04 07:49:44 -04:00
parent bc3b5bb1dc
commit f7fcbd4c7c
4 changed files with 68 additions and 2 deletions

1
.gitignore vendored
View File

@@ -46,3 +46,4 @@ create_db.sql
.env
test/postgres-data
db/remove-account.sh

View File

@@ -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 = {
setupFreeTrial,
createTestCdrs,
@@ -175,5 +231,6 @@ module.exports = {
parseAccountSid,
parseServiceProviderSid,
hasAccountPermissions,
hasServiceProviderPermissions
hasServiceProviderPermissions,
checkLimits
};

View File

@@ -5,9 +5,17 @@ const path = require('path');
const swaggerDocument = YAML.load(path.resolve(__dirname, '../swagger/swagger.yaml'));
const api = require('./api');
const stripe = require('./stripe');
const {checkLimits} = require('./api/utils');
const routes = express.Router();
routes.post([
'/v1/Applications',
'/v1/VoipCarriers',
'/v1/SipGateways',
'/v1/PhoneNumbers',
'/v1/Accounts'
], checkLimits);
routes.use('/v1', api);
routes.use('/stripe', stripe);
routes.use('/swagger', swaggerUi.serve);

View File

@@ -5,7 +5,7 @@
"main": "app.js",
"scripts": {
"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",
"coverage": "./node_modules/.bin/nyc --reporter html --report-dir ./coverage npm run test",
"jslint": "eslint app.js lib"