-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wbn-sign: Add support for calculating the Web Bundle ID with CLI tool
This adds the support to automatically calculate the Web Bundle ID also when using the package's Node CLI tool without Webpack / Rollup plugins. In the case of a bash script, the Web Bundle ID can then be saved into e.g. an environment variable or a file as instructed in the readme.
- Loading branch information
Showing
11 changed files
with
224 additions
and
97 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env node | ||
import { main } from '../lib/cli-dump-id.js'; | ||
|
||
main(); |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/usr/bin/env node | ||
import { main } from '../lib/cli.js'; | ||
import { main } from '../lib/cli-sign.js'; | ||
|
||
main(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,38 @@ | ||
import commander from 'commander'; | ||
import { WebBundleId } from './wbn-sign.js'; | ||
import * as fs from 'fs'; | ||
import { greenConsoleLog, parseMaybeEncryptedKey } from './utils/cli-utils.js'; | ||
import { KeyObject } from 'crypto'; | ||
|
||
const program = new commander.Command() | ||
.name('wbn-dump-id') | ||
.description( | ||
'A simple CLI tool to dump the Web Bundle ID matching to the given private key.' | ||
); | ||
|
||
function readOptions() { | ||
return program | ||
.requiredOption( | ||
'-k, --privateKey <file>', | ||
'Reads an ed25519 private key from the given path. (required)' | ||
) | ||
.option( | ||
'-s, --withIwaScheme', | ||
'Dumps the Web Bundle ID with isolated-app:// scheme. By default it only dumps the ID. (optional)', | ||
/*defaultValue=*/ false | ||
) | ||
.parse(process.argv); | ||
} | ||
|
||
export async function main() { | ||
const options = readOptions(); | ||
const parsedPrivateKey: KeyObject = await parseMaybeEncryptedKey( | ||
fs.readFileSync(options.privateKey) | ||
); | ||
|
||
const webBundleId: string = options.withIwaScheme | ||
? new WebBundleId(parsedPrivateKey).serializeWithIsolatedWebAppOrigin() | ||
: new WebBundleId(parsedPrivateKey).serialize(); | ||
|
||
greenConsoleLog(webBundleId); | ||
} |
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,48 @@ | ||
import commander from 'commander'; | ||
import { | ||
NodeCryptoSigningStrategy, | ||
IntegrityBlockSigner, | ||
WebBundleId, | ||
} from './wbn-sign.js'; | ||
import * as fs from 'fs'; | ||
import { greenConsoleLog, parseMaybeEncryptedKey } from './utils/cli-utils.js'; | ||
import { KeyObject } from 'crypto'; | ||
|
||
const program = new commander.Command() | ||
.name('wbn-sign') | ||
.description( | ||
'A simple CLI tool to sign the given web bundle with the given private key.' | ||
); | ||
|
||
function readOptions() { | ||
return program | ||
.requiredOption( | ||
'-i, --input <file>', | ||
'input web bundle to be signed (required)' | ||
) | ||
.requiredOption( | ||
'-k, --privateKey <file>', | ||
'path to ed25519 private key (required)' | ||
) | ||
.option( | ||
'-o, --output <file>', | ||
'signed web bundle output file', | ||
/*defaultValue=*/ 'signed.swbn' | ||
) | ||
.parse(process.argv); | ||
} | ||
|
||
export async function main() { | ||
const options = readOptions(); | ||
const webBundle = fs.readFileSync(options.input); | ||
const parsedPrivateKey: KeyObject = await parseMaybeEncryptedKey( | ||
fs.readFileSync(options.privateKey) | ||
); | ||
const signer = new IntegrityBlockSigner( | ||
webBundle, | ||
new NodeCryptoSigningStrategy(parsedPrivateKey) | ||
); | ||
const { signedWebBundle } = await signer.sign(); | ||
greenConsoleLog(`${new WebBundleId(parsedPrivateKey)}`); | ||
fs.writeFileSync(options.output, signedWebBundle); | ||
} |
This file was deleted.
Oops, something went wrong.
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,55 @@ | ||
import tty from 'tty'; | ||
import { KeyObject } from 'crypto'; | ||
import { parsePemKey, readPassphrase } from '../wbn-sign.js'; | ||
|
||
// Parses either an unencrypted or encrypted private key. For encrypted keys, it | ||
// reads the passphrase to decrypt them from either the | ||
// `WEB_BUNDLE_SIGNING_PASSPHRASE` environment variable, or, if not set, prompts | ||
// the user for the passphrase. | ||
export async function parseMaybeEncryptedKey( | ||
privateKeyFile: Buffer | ||
): Promise<KeyObject> { | ||
// Read unencrypted private key. | ||
try { | ||
return parsePemKey(privateKeyFile); | ||
} catch (e) { | ||
console.warn('This key is probably an encrypted private key.'); | ||
} | ||
|
||
const hasEnvVarSet = | ||
process.env.WEB_BUNDLE_SIGNING_PASSPHRASE && | ||
process.env.WEB_BUNDLE_SIGNING_PASSPHRASE !== ''; | ||
|
||
// Read encrypted private key. | ||
try { | ||
return parsePemKey( | ||
privateKeyFile, | ||
hasEnvVarSet | ||
? process.env.WEB_BUNDLE_SIGNING_PASSPHRASE | ||
: await readPassphrase() | ||
); | ||
} catch (e) { | ||
throw Error( | ||
`Failed decrypting encrypted private key with passphrase read from ${ | ||
hasEnvVarSet | ||
? '`WEB_BUNDLE_SIGNING_PASSPHRASE` environment variable' | ||
: 'prompt' | ||
}` | ||
); | ||
} | ||
} | ||
|
||
export function greenConsoleLog(text: string): void { | ||
const logColor = { green: '\x1b[32m', reset: '\x1b[0m' }; | ||
|
||
// @ts-expect-error Unknown property `fd`. | ||
const fileDescriptor: number = process.stdout.fd ?? 1; | ||
|
||
// If the log is used for non-terminal (fd != 1), e.g., setting an environment | ||
// variable, it shouldn't have any formatting. | ||
console.log( | ||
tty.isatty(fileDescriptor) | ||
? `${logColor.green}${text}${logColor.reset}` | ||
: text | ||
); | ||
} |
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
Oops, something went wrong.