forked from mozilla/blurts-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
for mozilla#465: start updatebreaches.js script
- Loading branch information
1 parent
3cba99a
commit 93e6d16
Showing
3 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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,80 @@ | ||
"use strict"; | ||
|
||
const got = require("got"); | ||
|
||
const AppConstants = require("../app-constants"); | ||
const HIBP = require("../hibp"); | ||
|
||
|
||
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) { | ||
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); | ||
|
||
if (newBreaches.length <= 0) { | ||
console.log("No new breaches detected."); | ||
process.exit(0); | ||
} | ||
|
||
console.log(`${newBreaches.length} new breach(es) found.`); | ||
|
||
for (const breach of newBreaches) { | ||
const data = { | ||
Name: breach.Name, | ||
Domain: breach.Domain, | ||
BreachDate: breach.BreachDate, | ||
PwnCount: breach.PwnCount, | ||
AddedDate: breach.AddedDate, | ||
}; | ||
|
||
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}), | ||
}); | ||
} 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"}}), | ||
}); | ||
})(); |