import { join } from 'node:path' import { randomBytes } from 'node:crypto' import readline from 'node:readline/promises' import { stdin as input, stdout as output } from 'node:process' import dotenv from 'dotenv' import { line } from './line.js' import { ExportKeyFactory, GenerateKeyFactory } from 'paseto/v3/local' import { deployment } from '@toa.io/operations' import { readFile, writeFile } from 'node:fs/promises' import { context as find } from '../util/find.js' const { Factory } = deployment export async function env(argv) { const path = find(argv.path) const filepath = join(path, argv.as) // a listed component is what someone runs, evicted or not const factory = await Factory.create( path, argv.environment, argv.component === undefined ? {} : { evicted: false } ) const operator = await factory.operator() const variables = operator.variables({ components: argv.component, services: argv.service }) const currentValues = await read(filepath) const result = merge(variables, currentValues) if (argv.dev) { const secrets = await resolveDevSecrets(result) mergeSecrets(result, secrets) } // asked for, where the caller asks to be asked; otherwise what nothing resolved is written // empty, and the file says what is missing where a refusal would only say that something is if (argv.interactive) { const secrets = await promptSecrets(result) mergeSecrets(result, secrets) } await write(filepath, result) } /** * @param path {string} * @returns {Record} */ async function read(path) { let contents try { contents = await readFile(path, 'utf8') } catch { return {} } return dotenv.parse(contents) } /** * @param {string} path * @param {toa.deployment.dependency.Variable[]} values * @return {Promise} */ async function write(path, values) { const contents = values.reduce( (lines, { name, value }) => lines + line(name, value ?? '') + '\n', '' ) await writeFile(path, contents, 'utf8') } /** * @param {toa.deployment.dependency.Variable[]} variables * @param {Record} current * @return {toa.deployment.dependency.Variable[]} */ function merge(variables, current) { return variables.map((variable) => { if (variable.secret === undefined || !current[variable.name]) return variable return { name: variable.name, value: current[variable.name] } }) } export async function promptSecrets(variables) { const rl = readline.createInterface({ input, output }) const secrets = {} for (const variable of variables) { if (variable.secret === undefined) continue const key = getKey(variable.secret) secrets[key] = await promptSecret(key, rl) } rl.close() return secrets } async function promptSecret(key, rl) { if (SECRETS[key] === undefined) SECRETS[key] = await rl.question(`${key}: `) return SECRETS[key] } /** * @param {toa.deployment.dependency.Variable[]} variables * @return {Promise>} */ async function resolveDevSecrets(variables) { const secrets = {} for (const variable of variables) { if (variable.secret === undefined) continue const key = getKey(variable.secret) if (key in secrets) continue if (key in DEV_SECRETS) { secrets[key] = await resolveDevSecret(key, variables) continue } const value = process.env[variable.secret.key] if (value !== undefined && value !== '') secrets[key] = value } return secrets } /** * @param {string} key * @param {toa.deployment.dependency.Variable[]} variables * @return {Promise} */ async function resolveDevSecret(key, variables) { const source = DEV_SECRETS[key] if (source.value !== undefined) return source.value // a user named after the context, as a store's reference setup names one if (source.context === true) return variables.find((variable) => variable.name === 'TOA_CONTEXT')?.value ?? '' if (source.generate === 'jwe') return randomBytes(32).toString('base64url') if (source.generate === true || source.generate === 'paseto') return await ExportKeyFactory().run( await GenerateKeyFactory().run({ extractable: true }) ) throw new Error(`Unknown dev secret source for ${key}`) } /** * @param {toa.deployment.dependency.Variable[]} variables * @param {Record} secrets */ function mergeSecrets(variables, secrets) { for (const variable of variables) { if (variable.secret === undefined) continue const key = getKey(variable.secret) if (!(key in secrets)) continue variable.value = secrets[key] delete variable.secret } } function getKey(secret) { return `${secret.name}/${secret.key}` } const SECRETS = {} /** * @type {Record} */ const DEV_SECRETS = { 'toa-mongodb.default/username': { value: 'developer' }, 'toa-mongodb.default/password': { value: 'secret' }, 'toa-amqp-context.default/username': { value: 'developer' }, 'toa-amqp-context.default/password': { value: 'secret' }, 'toa-chronicles/broker_username': { value: 'developer' }, 'toa-chronicles/broker_password': { value: 'secret' }, 'toa-chronicles/store_username': { context: true }, 'toa-chronicles/store_password': { value: 'secret' }, 'toa-configuration/IDENTITY_TOKENS_KEY0': { generate: 'paseto' }, 'toa-configuration/IDENTITY_TOKENS_ENCRYPTION_KEY0': { generate: 'jwe' } }