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 { createHash } = require('node:crypto')
|
||||
const { mkdirSync, rmSync } = require('node:fs')
|
||||
const { createWriteStream } = require('node:fs')
|
||||
const { readFile } = require('node:fs/promises')
|
||||
const { EOL } = require('node:os')
|
||||
const { join } = require('node:path')
|
||||
|
||||
require('../../lib/environment-helper')
|
||||
|
|
@ -16,7 +17,7 @@ const cli = CLI({
|
|||
grammar: [
|
||||
[["--help"], "Show this help message"],
|
||||
[["--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"],
|
||||
[["--replace_existing"], "Remove machines of previous runs"],
|
||||
],
|
||||
|
|
@ -29,66 +30,50 @@ const help = (exit_code) => {
|
|||
return exit_code
|
||||
}
|
||||
|
||||
const compute_machine_id = pem_path => (
|
||||
readFile(pem_path, { encoding: 'utf8' })
|
||||
.then(cert => cert.split('\r\n'))
|
||||
.then(cert => Buffer.from(cert.slice(1, cert.length-2).join(''), 'base64'))
|
||||
.then(raw => createHash('sha256').update(raw).digest('hex'))
|
||||
const close_stream = (stream) => (
|
||||
new Promise((resolve, reject) => (
|
||||
stream.close((err) => err ? reject(err) : resolve())
|
||||
))
|
||||
)
|
||||
|
||||
const create_fake_machine = async (gencerts_path, machine_data_dir, i) => (
|
||||
new Promise((resolve, reject) => {
|
||||
mkdirSync(machine_data_dir, { recursive: true, mode: 0o750 })
|
||||
const compute_machine_id = cert => {
|
||||
cert = cert.split('\r\n')
|
||||
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 gc = fork(gencerts_path, [machine_data_dir], {
|
||||
cwd: process.cwd(),
|
||||
encoding: 'utf8',
|
||||
})
|
||||
const create_fake_machine = async (device_ids_file, self_sign, i) => {
|
||||
console.log("creating machine", i)
|
||||
const { cert } = await self_sign.generateCertificate()
|
||||
const device_id = compute_machine_id(cert)
|
||||
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}`]
|
||||
)
|
||||
device_ids_file.write(device_id + EOL)
|
||||
console.log("created machine", i, "with device ID", device_id)
|
||||
}
|
||||
|
||||
gc.on('error', (error) => {
|
||||
console.log(error)
|
||||
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)
|
||||
})
|
||||
})
|
||||
)
|
||||
|
||||
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)
|
||||
if (Number.isNaN(n) || n <= 0) {
|
||||
console.error("Expected n to be a positive number, got", n)
|
||||
return help(EXIT.BADARGS)
|
||||
}
|
||||
|
||||
if (replace_existing) {
|
||||
rmSync(fake_data_dir, { recursive: true, force: true })
|
||||
await db.none("DELETE FROM devices")
|
||||
}
|
||||
const device_ids_file = createWriteStream(device_ids_path, {
|
||||
flags: replace_existing ? "w" : "a",
|
||||
mode: 0o640,
|
||||
flush: true,
|
||||
})
|
||||
if (replace_existing) await db.none("DELETE FROM devices")
|
||||
|
||||
/* Create the root data directory */
|
||||
mkdirSync(fake_data_dir, { recursive: true, mode: 0o750 })
|
||||
const self_sign = require(join(process.cwd(), machine, "lib", "self_sign"))
|
||||
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")
|
||||
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}`]
|
||||
)
|
||||
}
|
||||
await close_stream(device_ids_file)
|
||||
|
||||
return EXIT.OK
|
||||
}
|
||||
|
|
@ -103,7 +88,7 @@ const run = async (args) => {
|
|||
if (options.help)
|
||||
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) {
|
||||
console.error("The following options are required:", missing_options.join(", "))
|
||||
return help(EXIT.BADARGS)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue