forked from mozilla-services/screenshots
-
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.
Submit shot image to Watchdog on save. (mozilla-services#4377)
- Loading branch information
Showing
5 changed files
with
205 additions
and
2 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
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,141 @@ | ||
const config = require("./config").getProperties(); | ||
const mozlog = require("./logging").mozlog("server"); | ||
const db = require("./db"); | ||
const { Shot } = require("./servershot"); | ||
const util = require("util"); | ||
const { URL } = require("url"); | ||
const fetch = require("node-fetch"); | ||
const Hawk = require("hawk"); | ||
const FormData = require("form-data"); | ||
const genUuid = require("nodify-uuid"); | ||
|
||
const isEnabled = config.watchdog.enable; | ||
const isProperlyConfigured = validateConfiguration(); | ||
|
||
function validateConfiguration() { | ||
if (!isEnabled) { | ||
// Do no warn about config values if Watchdog is not enabled. | ||
return false; | ||
} | ||
|
||
let isConfigValid = true; | ||
|
||
if (!config.watchdog.id || !config.watchdog.key || !config.watchdog.algorithm) { | ||
mozlog.warn("watchdog-invalid-config", {msg: "Watchdog credentials configuration values are missing."}); | ||
isConfigValid = false; | ||
} | ||
|
||
if (!config.watchdog.submissionUrl) { | ||
mozlog.warn("watchdog-invalid-config", {msg: "Watchdog submission URL configuration value is missing."}); | ||
isConfigValid = false; | ||
} else { | ||
try { | ||
new URL(config.watchdog.submissionUrl); | ||
} catch (e) { | ||
mozlog.warn("watchdog-invalid-config", {msg: "Watchdog submission URL is not a valid URL.", err: e}); | ||
isConfigValid = false; | ||
} | ||
} | ||
|
||
return isConfigValid; | ||
} | ||
|
||
let positiveEmailList; | ||
if (config.watchdog.positiveEmail) { | ||
positiveEmailList = config.watchdog.positiveEmail.split(";"); | ||
} | ||
|
||
let denominator = config.watchdog.throttleDenominator; | ||
if (denominator <= 1) { | ||
denominator = 1; | ||
} | ||
let odometer = 0; | ||
|
||
function shouldSubmitShot() { | ||
if (!isEnabled || !isProperlyConfigured) { | ||
return false; | ||
} | ||
if (denominator === 1) { | ||
return true; | ||
} | ||
|
||
const shouldSubmit = odometer === 0; | ||
odometer = (odometer + 1) % denominator; | ||
|
||
return shouldSubmit; | ||
} | ||
|
||
function getSubmissionId() { | ||
return db.select( | ||
"SELECT nextval(pg_get_serial_sequence('watchdog_submissions', 'id')) AS new_id", | ||
).then(rows => { | ||
if (rows.length) { | ||
return rows[0].new_id; | ||
} | ||
throw new Error("Unable to retrieve a new Watchdog submission id."); | ||
}); | ||
} | ||
|
||
const credentials = { | ||
id: config.watchdog.id, | ||
key: config.watchdog.key, | ||
algorithm: config.watchdog.algorithm | ||
}; | ||
|
||
function submit(shot) { | ||
if (!shouldSubmitShot()) { | ||
return; | ||
} | ||
|
||
const imageUrl = shot.getClip(shot.clipNames()[0]).image.url; | ||
const imageId = imageUrl.split("/").pop(); | ||
const form = new FormData(); | ||
|
||
Shot.getRawBytesForClip( | ||
imageId | ||
).then((obj) => { | ||
if (obj === null) { | ||
throw new Error(`Unable to fetch ${imageId} for Watchdog submission.`); | ||
} | ||
form.append("image", obj.data, { | ||
filename: imageId, | ||
contentType: obj.contentType | ||
}); | ||
|
||
return getSubmissionId(); | ||
}).then(submissionId => { | ||
const uuid = util.promisify(genUuid.generate); | ||
return uuid(genUuid.V_RANDOM).then(nonce => ({submissionId, nonce})); | ||
}).then(({submissionId, nonce}) => { | ||
const callbackUrl = `${shot.backend}/watchdog/${submissionId}?nonce=${nonce}`; | ||
form.append("positive_uri", callbackUrl); | ||
form.append("negative_uri", callbackUrl); | ||
|
||
if (positiveEmailList) { | ||
for (const addr of positiveEmailList) { | ||
form.append("positive_email[]", addr); | ||
} | ||
} | ||
|
||
const req = {method: "POST", body: form}; | ||
const authHeader = Hawk.client.header(config.watchdog.submissionUrl, req.method, {credentials}); | ||
req.headers = Object.assign( | ||
{Accept: "application/json", Authorization: authHeader.header}, | ||
form.getHeaders()); | ||
return fetch(config.watchdog.submissionUrl, req) | ||
.then(res => res.json()) | ||
.then(respJson => ({submissionId, nonce, request_id: respJson.id})); | ||
}).then(({submissionId, nonce, request_id}) => { | ||
return db.insert( | ||
`INSERT INTO watchdog_submissions (id, shot_id, request_id, nonce) | ||
VALUES ($1, $2, $3, $4)`, | ||
[submissionId, shot.id, request_id, nonce.toString()] | ||
); | ||
}).catch(e => { | ||
mozlog.error("watchdog-failed-submission", {err: e}); | ||
}); | ||
} | ||
|
||
exports.Watchdog = { | ||
submit | ||
}; |