Skip to content
This repository has been archived by the owner on Dec 2, 2020. It is now read-only.

Multi-tenanancy #75

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions bin/collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ var options = require("./collector-config"),
cube = require("../"),
server = cube.server(options);

server.register = function(db, endpoints) {
cube.collector.register(db, endpoints);
};
server.register = cube.collector.register;

server.start();
5 changes: 1 addition & 4 deletions bin/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,5 @@ var options = require("./evaluator-config"),
cube = require("../"),
server = cube.server(options);

server.register = function(db, endpoints) {
cube.evaluator.register(db, endpoints);
};

server.register = cube.evaluator.register;
server.start();
31 changes: 27 additions & 4 deletions lib/cube/collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ var headers = {
"Access-Control-Allow-Origin": "*"
};

exports.register = function(db, endpoints) {
var putter = require("./event").putter(db),
exports.register = function(db, endpoints, authFun, namespaceFun) {
var meta = require("./event").putter(db),
putter = authentication(require("./event").putter(db, namespaceFun), authFun),
poster = post(putter);

//
Expand All @@ -24,6 +25,23 @@ exports.register = function(db, endpoints) {

//
endpoints.udp = putter;

function authentication(putter, authFun) {
if (authFun) {
return function(data) {
authFun(data, function () { putter(data); }, function () {
meta({
type: "failed_authentication",
time: Date.now(),
data: data
});
throw "AuthenticationError: Invalid Credentials";
});
};
} else {
return putter;
}
}
};

function post(putter) {
Expand All @@ -36,8 +54,13 @@ function post(putter) {
try {
JSON.parse(content).forEach(putter);
} catch (e) {
response.writeHead(400, headers);
response.end(JSON.stringify({error: e.toString()}));
if (e.toString() == "AuthenticationError: Invalid Credentials") {
response.writeHead(401, headers);
response.end(JSON.stringify({error: e.toString()}));
} else {
response.writeHead(400, headers);
response.end(JSON.stringify({error: e.toString()}));
}
return;
}
response.writeHead(200, headers);
Expand Down
90 changes: 72 additions & 18 deletions lib/cube/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ var headers = {
"Access-Control-Allow-Origin": "*"
};

exports.register = function(db, endpoints) {
var event = require("./event").getter(db),
metric = require("./metric").getter(db),
types = require("./types").getter(db);
exports.register = function(db, endpoints, authFun, namespaceFun) {
var meta = require("./event").putter(db),
event = authentication(require("./event").getter(db, namespaceFun)),
metric = authentication(require("./metric").getter(db, namespaceFun)),
types = authentication(require("./types").getter(db, namespaceFun));

//
endpoints.ws.push(
Expand Down Expand Up @@ -45,11 +46,22 @@ exports.register = function(db, endpoints) {
if (!("start" in request)) request.start = 0;
if (!(+request.limit <= limitMax)) request.limit = limitMax;

if (event(request, callback) < 0) {
response.writeHead(400, headers);
response.end(JSON.stringify(data[0]));
} else {
response.writeHead(200, headers);
try {
if (event(request, callback) < 0) {
response.writeHead(400, headers);
response.end(JSON.stringify(data[0]));
} else {
response.writeHead(200, headers);
}
} catch (e) {
if (e.toString() == "AuthenticationError: Invalid Credentials") {
response.writeHead(401, headers);
response.end(JSON.stringify({error: e.toString()}));
} else {
response.writeHead(400, headers);
response.end(JSON.stringify({error: e.toString()}));
}
return;
}

function callback(d) {
Expand Down Expand Up @@ -80,11 +92,22 @@ exports.register = function(db, endpoints) {
stop = new Date(request.stop);
if ((stop - start) / step > limit) request.start = new Date(stop - step * limit);

if (metric(request, callback) < 0) {
response.writeHead(400, headers);
response.end(JSON.stringify(data[0]));
} else {
response.writeHead(200, headers);
try {
if (metric(request, callback) < 0) {
response.writeHead(400, headers);
response.end(JSON.stringify(data[0]));
} else {
response.writeHead(200, headers);
}
} catch (e) {
if (e.toString() == "AuthenticationError: Invalid Credentials") {
response.writeHead(401, headers);
response.end(JSON.stringify({error: e.toString()}));
} else {
response.writeHead(400, headers);
response.end(JSON.stringify({error: e.toString()}));
}
return;
}

function callback(d) {
Expand All @@ -94,10 +117,41 @@ exports.register = function(db, endpoints) {
}

function typesGet(request, response) {
types(url.parse(request.url, true).query, function(data) {
response.writeHead(200, headers);
response.end(JSON.stringify(data));
});
try {
types(url.parse(request.url, true).query, function(data) {
response.writeHead(200, headers);
response.end(JSON.stringify(data));
});
} catch (e) {
if (e.toString() == "AuthenticationError: Invalid Credentials") {
response.writeHead(401, headers);
response.end(JSON.stringify({error: e.toString()}));
} else {
response.writeHead(400, headers);
response.end(JSON.stringify({error: e.toString()}));
}
return;
}
}

function authentication(getter) {
if (authFun) {
return function(request, callback) {
authFun(request, function () { getter(request, callback) }, function () {
meta({
type: "failed_authentication",
time: Date.now(),
data: {
ip: request.remoteAddress,
path: request.url
}
});
throw "AuthenticationError: Invalid Credentials";
});
};
} else {
return getter;
}
}
};

Expand Down
Loading