Skip to content

Commit

Permalink
Merge pull request #20 from SkynetLabs/karol/sky-1216-block-traffic-o…
Browse files Browse the repository at this point in the history
…n-takedown-servers-but-do

support new env variable for blocking public access
  • Loading branch information
kwypchlo authored Jul 6, 2022
2 parents 7131da3 + 1f16f21 commit 3c19d7e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/api/disabled.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const db = require("../db");
const { getDisabledServerReason } = require("../utils");

// returns a disabled flag status
module.exports = (req, res) => {
const disabled = db.get("disabled").value();
const manualDisabledReason = db.get("disabled").value();

res.send({ disabled });
res.send({ disabled: getDisabledServerReason(manualDisabledReason) });
};
5 changes: 4 additions & 1 deletion src/api/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { StatusCodes } = require("http-status-codes");
const { sum, sumBy } = require("lodash");
const db = require("../db");
const { getDisabledServerReason } = require("../utils");

/**
* Get status code that should be returned in the API response.
Expand Down Expand Up @@ -54,7 +55,9 @@ function getMostRecentCriticalEntry() {
* Get the disabled flag state (manual portal disable)
*/
function getDisabled() {
return db.get("disabled").value();
const manualDisabledReason = db.get("disabled").value();

return getDisabledServerReason(manualDisabledReason);
}

module.exports = (req, res) => {
Expand Down
18 changes: 18 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,31 @@ function isPortalModuleEnabled(module) {
return process.env.PORTAL_MODULES && process.env.PORTAL_MODULES.indexOf(module) !== -1;
}

/**
* Compute and generate a message indicating a disabled server. Server is disabled when either:
* - disable reason is set manually (non empty)
* - DENY_PUBLIC_ACCESS env variable is set to true (server on takedown)
*/
function getDisabledServerReason(manualDisabledReason) {
// check if a flag that indicates that server should disable public traffic is enabled
if (process.env.DENY_PUBLIC_ACCESS === "true") {
const accessDeniedReason = "Server public access denied"; // generic reason message

// include manual disable reason if server has been manually disabled
return manualDisabledReason ? `${manualDisabledReason} & ${accessDeniedReason}` : accessDeniedReason;
}

return manualDisabledReason;
}

module.exports = {
calculateElapsedTime,
getYesterdayISOString,
getResponseContent,
ensureValidJSON,
getAuthCookie,
isPortalModuleEnabled,
getDisabledServerReason,
ipCheckService,
ipRegex,
};

0 comments on commit 3c19d7e

Please sign in to comment.