-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate super referral mapping table component
- Loading branch information
Showing
4 changed files
with
177 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
const path = require('path') | ||
const mkdirp = require('mkdirp') | ||
const fs = require('fs-extra') | ||
const request = require('request') | ||
const commander = require('commander') | ||
|
||
const jsonSchemaVersion = 1 | ||
|
||
const createDataJsonFile = (path, body) => { | ||
fs.writeFileSync(path, body) | ||
} | ||
|
||
function downloadMappingTableJsonFile (jsonFileUrl, targetFilePath) { | ||
return new Promise(function (resolve, reject) { | ||
let jsonFileBody = '{}' | ||
|
||
request(jsonFileUrl, async function (error, response, body) { | ||
if (error) { | ||
console.error(`Error from ${jsonFileUrl}:`, error) | ||
return reject(error) | ||
} | ||
if (response && response.statusCode === 200) { | ||
jsonFileBody = body | ||
} | ||
|
||
const data = JSON.parse(jsonFileBody) | ||
// Make sure the data has a schema version so that clients can opt to parse or not | ||
const incomingSchemaVersion = data.schemaVersion | ||
if (!incomingSchemaVersion) { | ||
// Source has no schema version, assume and set current version. | ||
// TODO(petemill): Don't allow this once the source is established to always | ||
// have a schema version. | ||
data.schemaVersion = jsonSchemaVersion | ||
} else if (incomingSchemaVersion !== jsonSchemaVersion) { | ||
// We don't support this file format | ||
console.error(`Error: Cannot parse JSON data at ${jsonFileUrl} since it has a schema version of ${incomingSchemaVersion} but we expected ${jsonSchemaVersion}! This region will not be updated.`) | ||
return reject(error) | ||
} | ||
|
||
createDataJsonFile(targetFilePath, JSON.stringify(data)) | ||
|
||
resolve() | ||
}) | ||
}) | ||
} | ||
|
||
async function generateNTPSuperReferrerMappingTable (dataUrl) { | ||
const rootResourceDir = path.join(path.resolve(), 'build', 'ntp-super-referrer', 'resources', 'mapping-table') | ||
mkdirp.sync(rootResourceDir) | ||
|
||
console.log(`Downloading ${dataUrl}...`) | ||
const targetFilePath = path.join(rootResourceDir, 'mapping-table.json') | ||
await downloadMappingTableJsonFile(dataUrl, targetFilePath) | ||
} | ||
|
||
commander | ||
.option('-d, --data-url <url>', 'url that refers to data that has ntp super referrer') | ||
.parse(process.argv) | ||
|
||
generateNTPSuperReferrerMappingTable(commander.dataUrl) |
109 changes: 109 additions & 0 deletions
109
scripts/packageNTPSuperReferrerMappingTableComponent.js
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,109 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
const childProcess = require('child_process') | ||
const commander = require('commander') | ||
const fs = require('fs-extra') | ||
const mkdirp = require('mkdirp') | ||
const path = require('path') | ||
const replace = require('replace-in-file') | ||
const util = require('../lib/util') | ||
|
||
const stageFiles = (version, outputDir) => { | ||
// Copy resources and manifest file to outputDir. | ||
const resourceDir = path.join(path.resolve(), 'build', 'ntp-super-referrer', 'resources', 'mapping-table', '/') | ||
console.log('copy dir:', resourceDir, ' to:', outputDir) | ||
fs.copySync(resourceDir, outputDir) | ||
|
||
// Fix up the manifest version | ||
const originalManifest = getOriginalManifest() | ||
const outputManifest = path.join(outputDir, 'manifest.json') | ||
console.log('copy manifest file: ', originalManifest, ' to: ', outputManifest) | ||
const replaceOptions = { | ||
files: outputManifest, | ||
from: /0\.0\.0/, | ||
to: version | ||
} | ||
fs.copyFileSync(originalManifest, outputManifest) | ||
replace.sync(replaceOptions) | ||
} | ||
|
||
const generateManifestFile = (publicKey) => { | ||
const manifestFile = getOriginalManifest() | ||
const manifestContent = { | ||
description: 'Brave NTP Super Referrer mapping table component', | ||
key: publicKey, | ||
manifest_version: 2, | ||
name: `Brave NTP Super Referrer mapping table`, | ||
version: '0.0.0' | ||
} | ||
fs.writeFileSync(manifestFile, JSON.stringify(manifestContent)) | ||
} | ||
|
||
const getOriginalManifest = () => { | ||
return path.join(path.resolve(), 'build','ntp-super-referrer', 'mapping-table-manifest.json') | ||
} | ||
|
||
const generatePublicKeyAndID = (privateKeyFile) => { | ||
childProcess.execSync(`openssl rsa -in ${privateKeyFile} -pubout -out public.pub`) | ||
try { | ||
// read contents of the file | ||
const data = fs.readFileSync('public.pub', 'UTF-8'); | ||
|
||
// split the contents by new line | ||
const lines = data.split(/\r?\n/); | ||
let pubKeyString = '' | ||
lines.forEach((line) => { | ||
if (!line.includes('-----')) | ||
pubKeyString += line | ||
}); | ||
console.log(`publicKey: ${pubKeyString}`) | ||
const id = util.getIDFromBase64PublicKey(pubKeyString) | ||
console.log(`componentID: ${id}`) | ||
return [pubKeyString, id] | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
} | ||
|
||
const generateCRXFile = (binary, endpoint, region, componentID, privateKeyFile) => { | ||
const originalManifest = getOriginalManifest() | ||
const rootBuildDir = path.join(path.resolve(), 'build', 'ntp-super-referrer', 'mapping-table') | ||
const stagingDir = path.join(rootBuildDir, 'staging') | ||
const crxOutputDir = path.join(rootBuildDir, 'output') | ||
mkdirp.sync(stagingDir) | ||
mkdirp.sync(crxOutputDir) | ||
util.getNextVersion(endpoint, region, componentID).then((version) => { | ||
const crxFile = path.join(crxOutputDir, `ntp-super-referrer-mapping-table.crx`) | ||
stageFiles(version, stagingDir) | ||
util.generateCRXFile(binary, crxFile, privateKeyFile, stagingDir) | ||
console.log(`Generated ${crxFile} with version number ${version}`) | ||
}) | ||
} | ||
|
||
util.installErrorHandlers() | ||
|
||
commander | ||
.option('-b, --binary <binary>', 'Path to the Chromium based executable to use to generate the CRX file') | ||
.option('-k, --key <file>', 'file containing private key for signing crx file') | ||
.option('-e, --endpoint <endpoint>', 'DynamoDB endpoint to connect to', '')// If setup locally, use http://localhost:8000 | ||
.option('-r, --region <region>', 'The AWS region to use', 'us-east-2') | ||
.parse(process.argv) | ||
|
||
let privateKeyFile = '' | ||
if (fs.existsSync(commander.key)) { | ||
privateKeyFile = commander.key | ||
} else { | ||
throw new Error('Missing or invalid private key') | ||
} | ||
|
||
if (!commander.binary) { | ||
throw new Error('Missing Chromium binary: --binary') | ||
} | ||
|
||
util.createTableIfNotExists(commander.endpoint, commander.region).then(() => { | ||
const [publicKey, componentID] = generatePublicKeyAndID(privateKeyFile) | ||
generateManifestFile(publicKey) | ||
generateCRXFile(commander.binary, commander.endpoint, commander.region, componentID, privateKeyFile) | ||
}) |
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