Skip to content
This repository was archived by the owner on Jun 21, 2019. It is now read-only.
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
43 changes: 43 additions & 0 deletions api/v1/controllers/EndpointController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const bodyParser = require('body-parser');
const router = require('express').Router();

const EndpointAccessRequest = require('../requests/EndpointAccessRequest');
const middleware = require('../middleware');
const roles = require('../utils/roles');

const EndpointService = require('../services/EndpointService');

function modifyEndpointAccess(req, res, next) {
EndpointService.modifyEndpointAccess(req.body.endpoint, req.body.enabled);

res.body = req.body;

return next();
}

function getEndpointAccess(req, res, next) {
res.body = {};
res.body.endpoint = req.query.endpoint;
EndpointService.getEndpointAccess(req.query.endpoint,
() => {
res.body.enabled = true;
next();
},
() => {
res.body.enabled = false;
next();
}
)
}


router.use(bodyParser.json());
router.use(middleware.auth);

router.post('/', middleware.request(EndpointAccessRequest), middleware.permission(roles.ADMIN), modifyEndpointAccess);
router.get('/', middleware.permission(roles.ADMIN), getEndpointAccess);

router.use(middleware.response);
router.use(middleware.errors);

module.exports.router = router;
1 change: 1 addition & 0 deletions api/v1/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module.exports = {
AnnouncementController: require('./AnnouncementController.js'),
AuthController: require('./AuthController.js'),
EcosystemController: require('./EcosystemController.js'),
EndpointController: require('./EndpointController.js'),
EventController: require('./EventController.js'),
UploadController: require('./UploadController.js'),
UserController: require('./UserController.js'),
Expand Down
22 changes: 22 additions & 0 deletions api/v1/errors/EndpointNotAvailableError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const ApiError = require('./ApiError');

const ERROR_TYPE = 'EndpointNotAvailableError';
const ERROR_TITLE = 'Endpoint Not Available';
const STATUS_CODE = 400;

const DEFAULT_MESSAGE = 'This endpoint has been disabled';

function EndpointNotAvailableError(message, source) {
ApiError.call(this, message, source);

this.type = ERROR_TYPE;
this.status = STATUS_CODE;
this.title = ERROR_TITLE;
this.message = (message) ? message : DEFAULT_MESSAGE;
this.source = (source) ? source : null;
}

EndpointNotAvailableError.prototype = Object.create(ApiError.prototype);
EndpointNotAvailableError.prototype.constructor = EndpointNotAvailableError;

module.exports = EndpointNotAvailableError;
3 changes: 2 additions & 1 deletion api/v1/errors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ module.exports = {
TokenExpirationError: require('./TokenExpirationError.js'),
InvalidTrackingStateError: require('./InvalidTrackingStateError'),
UnauthorizedError: require('./UnauthorizedError.js'),
UnprocessableRequestError: require('./UnprocessableRequestError.js')
UnprocessableRequestError: require('./UnprocessableRequestError.js'),
EndpointNotAvailableError: require('./EndpointNotAvailableError.js')
};
2 changes: 2 additions & 0 deletions api/v1/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ v1.use((req, res, next) => {
});

v1.use(middleware.ratelimiting);
v1.use(middleware.enabled);

// set up CORS to allow for usage from different origins
// we may remove this in the future
Expand All @@ -37,6 +38,7 @@ v1.use('/stats', controllers.StatsController.router);
v1.use('/tracking', controllers.TrackingController.router);
v1.use('/mail', controllers.MailController.router);
v1.use('/event', controllers.EventController.router);
v1.use('/endpointaccess', controllers.EndpointController.router);

// logs resolved requests (the request once processed by various middleware) and outgoing responses
v1.use((req, res, next) => {
Expand Down
23 changes: 23 additions & 0 deletions api/v1/middleware/enabled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const errors = require('../errors');
const cache = require('../../cache').instance();

const Endpoint = require('../models/Endpoint');

const url = require('url');

const EndpointService = require('../services/EndpointService');

module.exports = (req, res, next) => {
const endpointPath = url.parse(req.url).pathname;

console.log('endpt is ' + endpointPath);

EndpointService.getEndpointAccess(endpointPath,
() => {
return next();
},
() => {
return next(new errors.EndpointNotAvailableError());
}
);
};
3 changes: 2 additions & 1 deletion api/v1/middleware/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ module.exports = {
response: require('./response.js'),
request: require('./request.js'),
upload: require('./upload.js'),
ratelimiting: require('./ratelimiting.js')
ratelimiting: require('./ratelimiting.js'),
enabled: require('./enabled.js')
};
12 changes: 12 additions & 0 deletions api/v1/models/Endpoint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const Model = require('./Model');

const Endpoint = Model.extend({
tableName: 'endpoints',
idAttribute: 'endpoint',
validations: {
endpoint: ['required', 'string', 'maxLength:100'],
enabled: ['required', 'boolean']
}
});

module.exports = Endpoint;
3 changes: 2 additions & 1 deletion api/v1/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ module.exports = {
Mentor: require('./Mentor'),
MentorProjectIdea: require('./MentorProjectIdea'),
UniversalTrackingItem: require('./TrackingEvent'),
NetworkCredential: require('./NetworkCredential')
NetworkCredential: require('./NetworkCredential'),
Endpoint: require('./Endpoint')
};
21 changes: 21 additions & 0 deletions api/v1/requests/EndpointAccessRequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const Request = require('./Request');

const bodyRequired = [ 'endpoint' ];
const bodyAllowed = [ 'enabled' ];
const bodyValidations = {
'endpoint': ['required', 'string'],
'enabled': [ 'boolean' ]
};

function EndpointAccessRequest(headers, body) {
Request.call(this, headers, body);

this.bodyRequired = bodyRequired;
this.bodyAllowed = bodyAllowed;
this.bodyValidations = bodyValidations;
}

EndpointAccessRequest.prototype = Object.create(Request.prototype);
EndpointAccessRequest.prototype.constructor = EndpointAccessRequest;

module.exports = EndpointAccessRequest;
1 change: 1 addition & 0 deletions api/v1/requests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = {
AccreditedUserCreationRequest: require('./AccreditedUserCreationRequest'),
BasicAuthRequest: require('./BasicAuthRequest'),
EcosystemCreationRequest: require('./EcosystemCreationRequest'),
EndpointAccessRequest: require('./EndpointAccessRequest'),
EventCreationRequest: require('./EventCreationRequest'),
LocationCreationRequest: require('./LocationCreationRequest'),
MentorRequest: require('./MentorRequest'),
Expand Down
51 changes: 51 additions & 0 deletions api/v1/services/EndpointService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const Endpoint = require('../models/Endpoint');

const cache = require('../../cache').instance();

const logger = require('../../logging');

function modifyEndpointAccess(reqEndpoint, reqEnabled) {
// Write enabled / disabled state to database
Endpoint.query({where: {endpoint: reqEndpoint}}).fetch().then((endpointModel) => {
const methodType = ((endpointModel == null) ? 'insert' : 'update');
Endpoint.forge({
endpoint: reqEndpoint,
enabled: reqEnabled
}).save(null, {method: methodType});
// Write enabled / disabled state to cache
cache.set(reqEndpoint, reqEnabled);
});

// Log the endpoint access change here
logger.debug('EndpointStateChanged: %s is %s.',
reqEndpoint,
reqEnabled ? 'enabled' : 'disabled'
);
}

function getEndpointAccess(reqEndpoint, onEnabled, onDisabled) {
return cache.get(reqEndpoint, (err, reply) => {
console.log('reply is ' + reply);
if (reply == 'true') {
onEnabled();
return;
} else if (reply == 'false') {
console.log('good');
onDisabled();
return;
}
Endpoint.query({where: {endpoint: reqEndpoint}}).fetch().then((endpointModel) => {
if (endpointModel == null || endpointModel.attributes.enabled[0] == 1) {
cache.set(reqEndpoint, 'true');
onEnabled();
return;
}
cache.set(reqEndpoint, 'false');
onDisabled();
return;
});
});
}

module.exports.modifyEndpointAccess = modifyEndpointAccess;
module.exports.getEndpointAccess = getEndpointAccess;
1 change: 1 addition & 0 deletions api/v1/services/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
AuthService: require('./AuthService'),
EcosystemService: require('./EcosystemService'),
EndpointService: require('./EndpointService'),
EventService: require('./EventService'),
MailService: require('./MailService'),
PermissionService: require('./PermissionService'),
Expand Down
5 changes: 5 additions & 0 deletions database/migration/V20171121_1645__addEndpointToggle.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE `endpoints` (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

endpoint_state

`endpoint` VARCHAR(50) NOT NULL,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's up this to 127 to leave a healthy margin

`enabled` BIT(1),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use TINYINT(1) for consistency with the rest of the schema

PRIMARY KEY (`endpoint`)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE `endpoints`;