Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #475 from Isabello/472-disable_api
Browse files Browse the repository at this point in the history
Provide mechanisms to disable APIs and toggle Whitelists
  • Loading branch information
karmacoma authored Mar 14, 2017
2 parents c488b69 + 7a73199 commit 9fd150c
Show file tree
Hide file tree
Showing 16 changed files with 59 additions and 24 deletions.
10 changes: 7 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,14 @@ d.run(function () {

if (parts.length > 1) {
if (parts[1] === 'api') {
if (!checkIpInList(scope.config.api.access.whiteList, ip, true)) {
res.sendStatus(403);
} else {
if (scope.config.api.access.public === true) {
next();
} else {
if (checkIpInList(scope.config.api.access.whiteList, ip, false)) {
next();
} else {
res.sendStatus(403);
}
}
} else if (parts[1] === 'peer') {
if (checkIpInList(scope.config.peers.blackList, ip, false)) {
Expand Down
9 changes: 7 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
]
},
"api": {
"enabled": true,
"access": {
"whiteList": []
"public": false,
"whiteList": ["127.0.0.1"]
},
"options": {
"limits": {
Expand All @@ -35,6 +37,7 @@
}
},
"peers": {
"enabled": true,
"list": [
{
"ip": "40.68.214.86",
Expand Down Expand Up @@ -77,7 +80,9 @@
"port": 8000
}
],
"blackList": [],
"access": {
"blackList": []
},
"options": {
"limits": {
"max": 0,
Expand Down
8 changes: 7 additions & 1 deletion helpers/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function Map (root, config) {
* @overview Router stub
* @returns {*}
*/
var Router = function () {
var Router = function (config) {
var router = require('express').Router();

router.use(function (req, res, next) {
Expand All @@ -38,6 +38,12 @@ var Router = function () {

router.map = Map;

if (config.enabled === false) {
router.use(function (req, res, next) {
res.status(500).send({success: false, error: 'API endpoint disabled'});
});
}

return router;
};

Expand Down
2 changes: 1 addition & 1 deletion modules/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function Accounts (cb, scope) {

// Private methods
__private.attachApi = function () {
var router = new Router();
var router = new Router(library.config.api);

router.use(function (req, res, next) {
if (modules) { return next(); }
Expand Down
2 changes: 1 addition & 1 deletion modules/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function Blocks (cb, scope) {

// Private methods
__private.attachApi = function () {
var router = new Router();
var router = new Router(library.config.api);

router.use(function (req, res, next) {
if (modules) { return next(); }
Expand Down
2 changes: 1 addition & 1 deletion modules/dapps.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function DApps (cb, scope) {
}

__private.attachApi = function () {
var router = new Router();
var router = new Router(library.config.api);

router.use(function (req, res, next) {
if (modules) { return next(); }
Expand Down
2 changes: 1 addition & 1 deletion modules/delegates.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function Delegates (cb, scope) {

// Private methods
__private.attachApi = function () {
var router = new Router();
var router = new Router(library.config.api);

router.use(function (req, res, next) {
if (modules && __private.loaded) { return next(); }
Expand Down
2 changes: 1 addition & 1 deletion modules/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ __private.initalize = function () {
};

__private.attachApi = function () {
var router = new Router();
var router = new Router(library.config.api);

router.get('/status/ping', function (req, res) {
__private.ping(function (status, body) {
Expand Down
2 changes: 1 addition & 1 deletion modules/multisignatures.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function Multisignatures (cb, scope) {

// Private methods
__private.attachApi = function () {
var router = new Router();
var router = new Router(library.config.api);

router.use(function (req, res, next) {
if (modules) { return next(); }
Expand Down
2 changes: 1 addition & 1 deletion modules/peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function Peers (cb, scope) {

// Private methods
__private.attachApi = function () {
var router = new Router();
var router = new Router(library.config.api);

router.use(function (req, res, next) {
if (modules) { return next(); }
Expand Down
2 changes: 1 addition & 1 deletion modules/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function Server (cb, scope) {

// Private methods
__private.attachApi = function () {
var router = new Router();
var router = new Router({});

router.use(function (req, res, next) {
if (modules) { return next(); }
Expand Down
2 changes: 1 addition & 1 deletion modules/signatures.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function Signatures (cb, scope) {

// Private methods
__private.attachApi = function () {
var router = new Router();
var router = new Router(library.config.api);

router.use(function (req, res, next) {
if (modules) { return next(); }
Expand Down
2 changes: 1 addition & 1 deletion modules/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function Transactions (cb, scope) {

// Private methods
__private.attachApi = function () {
var router = new Router();
var router = new Router(library.config.api);

router.use(function (req, res, next) {
if (modules) { return next(); }
Expand Down
2 changes: 1 addition & 1 deletion modules/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function Transport (cb, scope) {

// Private methods
__private.attachApi = function () {
var router = new Router();
var router = new Router(library.config.peers);

router.use(function (req, res, next) {
res.set(__private.headers);
Expand Down
25 changes: 20 additions & 5 deletions schema/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,20 @@ module.exports = {
api: {
type: 'object',
properties: {
enabled: {
type: 'boolean'
},
access: {
type: 'object',
properties: {
public: {
type: 'boolean'
},
whiteList: {
type: 'array'
}
},
required: ['whiteList']
required: ['public', 'whiteList']
},
options: {
type: 'object',
Expand All @@ -110,16 +116,25 @@ module.exports = {
required: ['limits']
}
},
required: ['access', 'options']
required: ['enabled', 'access', 'options']
},
peers: {
type: 'object',
properties: {
enabled: {
type: 'boolean'
},
list: {
type: 'array'
},
blackList: {
type: 'array'
access: {
type: 'object',
properties: {
blackList: {
type: 'array'
}
},
required: ['blackList']
},
options: {
properties: {
Expand Down Expand Up @@ -148,7 +163,7 @@ module.exports = {
required: ['limits', 'timeout']
}
},
required: ['list', 'blackList', 'options']
required: ['enabled', 'list', 'access', 'options']
},
broadcasts: {
type: 'object',
Expand Down
9 changes: 7 additions & 2 deletions test/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
]
},
"api": {
"enabled": true,
"access": {
"whiteList": []
"public": true,
"whiteList": ["127.0.0.1"]
},
"options": {
"limits": {
Expand All @@ -35,8 +37,11 @@
}
},
"peers": {
"enabled": true,
"list": [],
"blackList": [],
"access": {
"blackList": []
},
"options": {
"limits": {
"max": 0,
Expand Down

0 comments on commit 9fd150c

Please sign in to comment.