mirror of
https://github.com/Unleash/unleash.git
synced 2024-12-22 19:07:54 +01:00
add joy validation to client input data
This commit is contained in:
parent
484f7dcd9d
commit
70d596d0af
@ -3,6 +3,7 @@
|
|||||||
const logger = require('../logger');
|
const logger = require('../logger');
|
||||||
const ClientMetrics = require('../client-metrics');
|
const ClientMetrics = require('../client-metrics');
|
||||||
const ClientMetricsService = require('../client-metrics/service');
|
const ClientMetricsService = require('../client-metrics/service');
|
||||||
|
const joi = require('joi');
|
||||||
|
|
||||||
module.exports = function (app, config) {
|
module.exports = function (app, config) {
|
||||||
const {
|
const {
|
||||||
@ -27,33 +28,67 @@ module.exports = function (app, config) {
|
|||||||
res.json(metrics.getTogglesMetrics());
|
res.json(metrics.getTogglesMetrics());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const clientMetricsSchema = joi.object().keys({
|
||||||
|
appName: joi.string().required(),
|
||||||
|
instanceId: joi.string().required(),
|
||||||
|
bucket: joi.object().required()
|
||||||
|
.keys({
|
||||||
|
start: joi.date().required(),
|
||||||
|
stop: joi.date().required(),
|
||||||
|
toggles: joi.object()
|
||||||
|
.required()
|
||||||
|
.unknown()
|
||||||
|
.min(1)
|
||||||
|
.max(1000),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
app.post('/client/metrics', (req, res) => {
|
app.post('/client/metrics', (req, res) => {
|
||||||
try {
|
try {
|
||||||
const data = typeof req.body === 'string' ? JSON.parse(req.body) : req.body;
|
const data = typeof req.body === 'string' ? JSON.parse(req.body) : req.body;
|
||||||
|
const result = joi.validate(data, clientMetricsSchema);
|
||||||
|
if (result.error) {
|
||||||
|
throw result.error;
|
||||||
|
}
|
||||||
service
|
service
|
||||||
.insert(data)
|
.insert(result.value)
|
||||||
.catch(e => logger.error('Error inserting metrics data', e));
|
.catch(e => logger.error('Error inserting metrics data', e));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.error('Error receiving metrics', e);
|
logger.error('Error receiving metrics', e);
|
||||||
}
|
}
|
||||||
|
|
||||||
res.end();
|
res.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const clientRegisterSchema = joi.object().keys({
|
||||||
|
appName: joi.string().required(),
|
||||||
|
instanceId: joi.string().required(),
|
||||||
|
strategies: joi.array()
|
||||||
|
.required()
|
||||||
|
.items(joi.string(), joi.any().strip()),
|
||||||
|
started: joi.date().required(),
|
||||||
|
interval: joi.number().required(),
|
||||||
|
});
|
||||||
|
|
||||||
app.post('/client/register', (req, res) => {
|
app.post('/client/register', (req, res) => {
|
||||||
const data = req.body;
|
const data = req.body;
|
||||||
const clientIp = req.ip;
|
const clientIp = req.ip;
|
||||||
console.log(data);
|
|
||||||
clientStrategyStore.insert(data.appName, data.strategies)
|
|
||||||
.then(() => clientInstanceStore.insert({
|
|
||||||
appName: data.appName,
|
|
||||||
instanceId: data.instanceId,
|
|
||||||
clientIp,
|
|
||||||
}))
|
|
||||||
.then(() => console.log('new client registerd'))
|
|
||||||
.catch((error) => logger.error('Error registering client', error));
|
|
||||||
|
|
||||||
res.end();
|
joi.validate(data, clientRegisterSchema, (err, cleaned) => {
|
||||||
|
if (err) {
|
||||||
|
return res.json(400, err);
|
||||||
|
}
|
||||||
|
|
||||||
|
clientStrategyStore.insert(cleaned.appName, cleaned.strategies)
|
||||||
|
.then(() => clientInstanceStore.insert({
|
||||||
|
appName: cleaned.appName,
|
||||||
|
instanceId: cleaned.instanceId,
|
||||||
|
clientIp,
|
||||||
|
}))
|
||||||
|
.then(() => console.log('new client registerd'))
|
||||||
|
.catch((error) => logger.error('Error registering client', error));
|
||||||
|
|
||||||
|
res.end();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/client/strategies', (req, res) => {
|
app.get('/client/strategies', (req, res) => {
|
||||||
|
@ -57,6 +57,7 @@
|
|||||||
"express": "4.14.0",
|
"express": "4.14.0",
|
||||||
"express-validator": "2.20.8",
|
"express-validator": "2.20.8",
|
||||||
"install": "^0.8.1",
|
"install": "^0.8.1",
|
||||||
|
"joi": "^9.2.0",
|
||||||
"knex": "^0.12.6",
|
"knex": "^0.12.6",
|
||||||
"log4js": "^0.6.38",
|
"log4js": "^0.6.38",
|
||||||
"moment": "^2.15.2",
|
"moment": "^2.15.2",
|
||||||
|
Loading…
Reference in New Issue
Block a user