This repository was archived by the owner on Jun 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Allow admins to enabled / disabled endpoints #168
Open
ASankaran
wants to merge
17
commits into
HackIllinois:staging
Choose a base branch
from
ASankaran:feature/endpoint-controls
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
eea41f1
Added endpoint for enabling and disabling endpoints
ASankaran a9a5be9
Added middleware to block access to disabled endpoints
ASankaran 573225f
Fixed comparism
ASankaran bcb1d01
Added migration and revert scripts
ASankaran 46c0127
Update endpoint states in database
ASankaran ddf2f05
Read / Write endpoint states from database. Disabled reddis caching o…
ASankaran f00c888
Added endpoint state to response
ASankaran 33ef933
Added function to check state of endpoint
ASankaran aca6eef
Use cacheing for endpoint states
ASankaran 4b66385
Fixed commented out admin restrictions
ASankaran 3b1dd37
Restricted enabling / disabling of endpoints to admin
ASankaran 0ffded4
Fixed linter errors
ASankaran 642486f
Moved querying of endpoint state to GET request
ASankaran e30a4f6
Log when an admin enabled / disables an endpoint
ASankaran d35923e
Fixed lint errors
ASankaran 9ef1d43
Removed timestamps for endpoints in db
ASankaran 2772092
Moved endpoint code into service
ASankaran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
| } | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| CREATE TABLE `endpoints` ( | ||
| `endpoint` VARCHAR(50) NOT NULL, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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`) | ||
| ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| DROP TABLE `endpoints`; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
endpoint_state