2020-08-07 23:57:23 +03:00
|
|
|
"use strict";
|
|
|
|
|
2020-08-10 21:15:05 +03:00
|
|
|
// TODO: Confirm db row has index
|
2020-08-08 00:47:08 +03:00
|
|
|
|
2020-08-07 23:57:23 +03:00
|
|
|
const Knex = require("knex");
|
|
|
|
const knexConfig = require("../db/knexfile");
|
|
|
|
const knex = Knex(knexConfig);
|
|
|
|
|
|
|
|
const HIBP = require("../hibp");
|
|
|
|
|
2020-08-08 00:47:08 +03:00
|
|
|
async function checkIfBreachesExist(sha1, breaches) {
|
|
|
|
const breachResults = await HIBP.getBreachesForEmail(sha1, breaches, true);
|
2020-08-07 23:57:23 +03:00
|
|
|
|
|
|
|
if (breachResults.length > 1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-10 21:15:05 +03:00
|
|
|
function getArgsValue(argument) {
|
|
|
|
const cliArguments = process.argv;
|
|
|
|
|
|
|
|
if (cliArguments.indexOf(argument) < 0) {
|
|
|
|
throw new Error(`You are missing the argument: ${argument}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const arguemntIndex = cliArguments.indexOf(argument);
|
|
|
|
const value = cliArguments[(arguemntIndex + 1)];
|
|
|
|
|
|
|
|
if (!value ) {
|
|
|
|
throw new Error(`No value set for ${argument}.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const valueNumber = parseInt(value);
|
|
|
|
|
|
|
|
if (Number.isNaN(valueNumber)) {
|
|
|
|
throw new Error(`The value for ${argument} is not an interger.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return valueNumber;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-07 23:57:23 +03:00
|
|
|
(async () => {
|
2020-08-08 00:47:08 +03:00
|
|
|
console.log("init");
|
|
|
|
|
2020-08-10 21:15:05 +03:00
|
|
|
console.log(process.argv);
|
|
|
|
|
2020-08-08 00:47:08 +03:00
|
|
|
const allHibpBreachesResp = await HIBP.req("/breaches");
|
|
|
|
const allHibpBreaches = allHibpBreachesResp.body;
|
2020-08-07 23:57:23 +03:00
|
|
|
|
2020-08-10 21:15:05 +03:00
|
|
|
const limitQuery = getArgsValue("--limit");
|
|
|
|
const cohortSize = getArgsValue("--cohort-size");
|
2020-08-08 00:47:08 +03:00
|
|
|
|
2020-08-10 21:15:05 +03:00
|
|
|
// "SELECT primary_email, primary_sha1 FROM subscribers WHERE signup_language LIKE 'en%' AND breaches_resolved IS NULL ORDER BY random();"
|
2020-08-08 00:47:08 +03:00
|
|
|
|
2020-08-10 21:15:05 +03:00
|
|
|
const results = await knex("subscribers").where("signup_language", "like", "en%").andWhere({breaches_resolved: null}).orderByRaw("RANDOM()").limit(limitQuery).select("primary_email", "primary_sha1");
|
2020-08-07 23:57:23 +03:00
|
|
|
|
|
|
|
const cohort = [];
|
|
|
|
|
2020-08-08 00:47:08 +03:00
|
|
|
|
|
|
|
for (const record of results) {
|
|
|
|
// console.log(record);
|
2020-08-10 21:15:05 +03:00
|
|
|
if (cohort.length > cohortSize) {
|
2020-08-07 23:57:23 +03:00
|
|
|
// Print Cohort
|
|
|
|
console.log(cohort);
|
2020-08-08 00:47:08 +03:00
|
|
|
break;
|
2020-08-07 23:57:23 +03:00
|
|
|
}
|
|
|
|
|
2020-08-08 00:47:08 +03:00
|
|
|
const sha1 = record.primary_sha1;
|
|
|
|
const isValidCohortMember = await checkIfBreachesExist(sha1, allHibpBreaches);
|
|
|
|
if (isValidCohortMember) { cohort.push(record.primary_email); }
|
|
|
|
}
|
|
|
|
|
2020-08-10 21:15:05 +03:00
|
|
|
console.log(cohort.toString());
|
2020-08-08 00:47:08 +03:00
|
|
|
|
2020-08-07 23:57:23 +03:00
|
|
|
})();
|