refactor: skip cert file creation
Create the certificate in memory, insert it into the DB, and save it to a text file instead.
This commit is contained in:
parent
99dd90d51a
commit
27e2f26d11
1 changed files with 36 additions and 51 deletions
|
|
@ -1,7 +1,8 @@
|
||||||
const { fork } = require('node:child_process')
|
const { fork } = require('node:child_process')
|
||||||
const { createHash } = require('node:crypto')
|
const { createHash } = require('node:crypto')
|
||||||
const { mkdirSync, rmSync } = require('node:fs')
|
const { createWriteStream } = require('node:fs')
|
||||||
const { readFile } = require('node:fs/promises')
|
const { readFile } = require('node:fs/promises')
|
||||||
|
const { EOL } = require('node:os')
|
||||||
const { join } = require('node:path')
|
const { join } = require('node:path')
|
||||||
|
|
||||||
require('../../lib/environment-helper')
|
require('../../lib/environment-helper')
|
||||||
|
|
@ -16,7 +17,7 @@ const cli = CLI({
|
||||||
grammar: [
|
grammar: [
|
||||||
[["--help"], "Show this help message"],
|
[["--help"], "Show this help message"],
|
||||||
[["--machine", "PATH"], "Path to the machine's source code root"],
|
[["--machine", "PATH"], "Path to the machine's source code root"],
|
||||||
[["--fake_data_dir", "PATH"], "Where to save the fake machines' data"],
|
[["--device_ids_path", "PATH"], "Where to save the list of device IDs"],
|
||||||
[["-n", "NUMBER"], "Number of fake machines to create"],
|
[["-n", "NUMBER"], "Number of fake machines to create"],
|
||||||
[["--replace_existing"], "Remove machines of previous runs"],
|
[["--replace_existing"], "Remove machines of previous runs"],
|
||||||
],
|
],
|
||||||
|
|
@ -29,66 +30,50 @@ const help = (exit_code) => {
|
||||||
return exit_code
|
return exit_code
|
||||||
}
|
}
|
||||||
|
|
||||||
const compute_machine_id = pem_path => (
|
const close_stream = (stream) => (
|
||||||
readFile(pem_path, { encoding: 'utf8' })
|
new Promise((resolve, reject) => (
|
||||||
.then(cert => cert.split('\r\n'))
|
stream.close((err) => err ? reject(err) : resolve())
|
||||||
.then(cert => Buffer.from(cert.slice(1, cert.length-2).join(''), 'base64'))
|
))
|
||||||
.then(raw => createHash('sha256').update(raw).digest('hex'))
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const create_fake_machine = async (gencerts_path, machine_data_dir, i) => (
|
const compute_machine_id = cert => {
|
||||||
new Promise((resolve, reject) => {
|
cert = cert.split('\r\n')
|
||||||
mkdirSync(machine_data_dir, { recursive: true, mode: 0o750 })
|
const raw = Buffer.from(cert.slice(1, cert.length-2).join(''), 'base64')
|
||||||
|
return createHash('sha256').update(raw).digest('hex')
|
||||||
|
}
|
||||||
|
|
||||||
console.log("Creating fake machine number", i)
|
const create_fake_machine = async (device_ids_file, self_sign, i) => {
|
||||||
const gc = fork(gencerts_path, [machine_data_dir], {
|
console.log("creating machine", i)
|
||||||
cwd: process.cwd(),
|
const { cert } = await self_sign.generateCertificate()
|
||||||
encoding: 'utf8',
|
const device_id = compute_machine_id(cert)
|
||||||
})
|
await db.none(
|
||||||
|
`INSERT INTO devices (device_id, cassette1, cassette2, paired, display, created, name, last_online, location)
|
||||||
gc.on('error', (error) => {
|
VALUES ($1, 0, 0, 't', 't', now(), $2, now(), '{}'::json)`,
|
||||||
console.log(error)
|
[device_id, `machine_${i}`]
|
||||||
resolve(EXIT.EXCEPTION)
|
|
||||||
})
|
|
||||||
|
|
||||||
gc.on('exit', (code, signal) => {
|
|
||||||
console.error("lamassu-server code:", code)
|
|
||||||
console.error("lamassu-server signal:", signal)
|
|
||||||
resolve(typeof(code) === 'number' ? code : EXIT.EXCEPTION)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
)
|
)
|
||||||
|
device_ids_file.write(device_id + EOL)
|
||||||
|
console.log("created machine", i, "with device ID", device_id)
|
||||||
|
}
|
||||||
|
|
||||||
const create_fake_machines = async ({ machine, fake_data_dir, n, replace_existing }) => {
|
const create_fake_machines = async ({ machine, device_ids_path, n, replace_existing }) => {
|
||||||
n = parseInt(n)
|
n = parseInt(n)
|
||||||
if (Number.isNaN(n) || n <= 0) {
|
if (Number.isNaN(n) || n <= 0) {
|
||||||
console.error("Expected n to be a positive number, got", n)
|
console.error("Expected n to be a positive number, got", n)
|
||||||
return help(EXIT.BADARGS)
|
return help(EXIT.BADARGS)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (replace_existing) {
|
const device_ids_file = createWriteStream(device_ids_path, {
|
||||||
rmSync(fake_data_dir, { recursive: true, force: true })
|
flags: replace_existing ? "w" : "a",
|
||||||
await db.none("DELETE FROM devices")
|
mode: 0o640,
|
||||||
}
|
flush: true,
|
||||||
|
})
|
||||||
|
if (replace_existing) await db.none("DELETE FROM devices")
|
||||||
|
|
||||||
/* Create the root data directory */
|
const self_sign = require(join(process.cwd(), machine, "lib", "self_sign"))
|
||||||
mkdirSync(fake_data_dir, { recursive: true, mode: 0o750 })
|
for (let i = 0; i < n; i++)
|
||||||
|
await create_fake_machine(device_ids_file, self_sign, i)
|
||||||
|
|
||||||
const gencerts_path = join(machine, "tools", "generate-certificates")
|
await close_stream(device_ids_file)
|
||||||
for (let i = 0; i < n; i++) {
|
|
||||||
const machine_data_dir = join(fake_data_dir, i.toString())
|
|
||||||
const exit_code = await create_fake_machine(gencerts_path, machine_data_dir, i)
|
|
||||||
if (exit_code !== EXIT.OK)
|
|
||||||
return exit_code
|
|
||||||
|
|
||||||
const device_id = await compute_machine_id(join(machine_data_dir, "client.pem"))
|
|
||||||
|
|
||||||
await db.none(
|
|
||||||
`INSERT INTO devices (device_id, cassette1, cassette2, paired, display, created, name, last_online, location)
|
|
||||||
VALUES ($1, 0, 0, 't', 't', now(), $2, now(), '{}'::json)`,
|
|
||||||
[device_id, `machine_${i}`]
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return EXIT.OK
|
return EXIT.OK
|
||||||
}
|
}
|
||||||
|
|
@ -103,7 +88,7 @@ const run = async (args) => {
|
||||||
if (options.help)
|
if (options.help)
|
||||||
return help(EXIT.OK)
|
return help(EXIT.OK)
|
||||||
|
|
||||||
const missing_options = ["n", "machine", "fake_data_dir"].filter((opt) => !options[opt])
|
const missing_options = ["n", "machine", "device_ids_path"].filter((opt) => !options[opt])
|
||||||
if (missing_options.length > 0) {
|
if (missing_options.length > 0) {
|
||||||
console.error("The following options are required:", missing_options.join(", "))
|
console.error("The following options are required:", missing_options.join(", "))
|
||||||
return help(EXIT.BADARGS)
|
return help(EXIT.BADARGS)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue