Skip to content

Commit

Permalink
refactor into lib/remote-settings module
Browse files Browse the repository at this point in the history
  • Loading branch information
groovecoder committed Apr 18, 2019
1 parent 5b1269b commit 68f6ac6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 41 deletions.
51 changes: 51 additions & 0 deletions lib/remote-settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"use strict";

const got = require("got");
const AppConstants = require("../app-constants");


const BREACHES_COLLECTION = "fxmonitor-breaches";
const FX_RS_COLLECTION = `${AppConstants.FX_REMOTE_SETTINGS_WRITER_SERVER}/buckets/main-workspace/collections/${BREACHES_COLLECTION}`;
const FX_RS_RECORDS = `${FX_RS_COLLECTION}/records`;
const FX_RS_BEARER_TOKEN = AppConstants.FX_REMOTE_SETTINGS_BEARER_TOKEN;

const RemoteSettings = {

async whichBreachesAreNotInRemoteSettingsYet(breaches) {
const fxRSRecords = await got(FX_RS_RECORDS, {
json: true,
headers: {
"authorization": `Bearer ${FX_RS_BEARER_TOKEN}`,
},
});
const remoteSettingsBreachesSet = new Set(
fxRSRecords.body.data.map(b => b.Name)
);

return breaches.filter( ({Name}) => !remoteSettingsBreachesSet.has(Name) );
},

async postNewBreachToBreachesCollection(data) {
// Create the record
await got.post(FX_RS_RECORDS, {
headers: {
"Content-Type": "application/json",
"authorization": `Bearer ${FX_RS_BEARER_TOKEN}`,
},
body: JSON.stringify({data: data}),
});
},

async requestReviewOnBreachesCollection() {
await got.patch(FX_RS_COLLECTION, {
headers: {
"Content-Type": "application/json",
"authorization": `Bearer ${FX_RS_BEARER_TOKEN}`,
},
body: JSON.stringify({data: {status: "to-review"}}),
});
},
};


module.exports = RemoteSettings;
54 changes: 13 additions & 41 deletions scripts/updatebreaches.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,24 @@
"use strict";

const got = require("got");

const AppConstants = require("../app-constants");
const HIBP = require("../hibp");
const RemoteSettings = require("../lib/remote-settings");


const BREACHES_COLLECTION = "fxmonitor-breaches";
const FX_RS_RECORDS = `${AppConstants.FX_REMOTE_SETTINGS_WRITER_SERVER}/buckets/main-workspace/collections/${BREACHES_COLLECTION}/records`;
const FX_RS_COLLECTION = `${AppConstants.FX_REMOTE_SETTINGS_WRITER_SERVER}/buckets/main-workspace/collections/${BREACHES_COLLECTION}`;
const FX_RS_BEARER_TOKEN = AppConstants.FX_REMOTE_SETTINGS_BEARER_TOKEN;


if (!FX_RS_BEARER_TOKEN) {
if (
!AppConstants.FX_REMOTE_SETTINGS_BEARER_TOKEN ||
!AppConstants.FX_REMOTE_SETTINGS_WRITER_SERVER
) {
console.error("updatebreaches requires FX_RS_BEARER_TOKEN.");
process.exit(1);
}


async function whichBreachesAreNotInRemoteSettingsYet(hibpBreaches) {
const fxRSRecords = await got(FX_RS_RECORDS, {
json: true,
headers: {
"authorization": `Bearer ${FX_RS_BEARER_TOKEN}`,
},
});
const remoteSettingsBreachesSet = new Set(
fxRSRecords.body.data.map(b => b.Name)
);

return hibpBreaches.filter( ({Name}) => !remoteSettingsBreachesSet.has(Name) );
}


(async () => {
const allHibpBreaches = await HIBP.req("/breaches");
const verifiedSiteBreaches = HIBP.filterBreaches(allHibpBreaches.body);

const newBreaches = await whichBreachesAreNotInRemoteSettingsYet(verifiedSiteBreaches);
const newBreaches = await RemoteSettings.whichBreachesAreNotInRemoteSettingsYet(verifiedSiteBreaches);

if (newBreaches.length <= 0) {
console.log("No new breaches detected.");
Expand All @@ -55,26 +36,17 @@ async function whichBreachesAreNotInRemoteSettingsYet(hibpBreaches) {
AddedDate: breach.AddedDate,
};

console.log("New breach detected: \n", data);

try {
// Create the record
await got.post(FX_RS_RECORDS, {
headers: {
"Content-Type": "application/json",
"authorization": `Bearer ${FX_RS_BEARER_TOKEN}`,
},
body: JSON.stringify({data: data}),
});
await RemoteSettings.postNewBreachToBreachesCollection(data);
} catch (e) {
console.error(e);
process.exit(1);
}
}
// Request a review on the collection
await got.patch(FX_RS_COLLECTION, {
headers: {
"Content-Type": "application/json",
"authorization": `Bearer ${FX_RS_BEARER_TOKEN}`,
},
body: JSON.stringify({data: {status: "to-review"}}),
});

console.log("Requesting review on breaches collection");
await RemoteSettings.requestReviewOnBreachesCollection();

})();

0 comments on commit 68f6ac6

Please sign in to comment.