From bdf94f6c5163eeef6193bc6dfe877561b5cecb84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claus-Peter=20H=C3=BCbner?= Date: Wed, 26 Apr 2023 00:25:28 +0200 Subject: [PATCH] introduce .env.devop file containing topic, seed and keypair --- dht-node/src/config/devop.ts | 13 +++++++++ dht-node/src/config/tools.ts | 53 ++++++++++++++++++++++++++++++++++ dht-node/src/dht_node/index.ts | 16 ++++++++-- dht-node/src/index.ts | 25 +++++++++++----- 4 files changed, 97 insertions(+), 10 deletions(-) create mode 100644 dht-node/src/config/devop.ts create mode 100644 dht-node/src/config/tools.ts diff --git a/dht-node/src/config/devop.ts b/dht-node/src/config/devop.ts new file mode 100644 index 000000000..410953fbf --- /dev/null +++ b/dht-node/src/config/devop.ts @@ -0,0 +1,13 @@ +// ATTENTION: DO NOT PUT ANY SECRETS IN HERE (or the .env) +import dotenv from 'dotenv' +import { getDevOpEnvValue } from './tools' +dotenv.config() + +const DEVOP = { + FEDERATION_DHT_TOPIC: getDevOpEnvValue('FEDERATION_DHT_TOPIC') || null, + FEDERATION_DHT_SEED: getDevOpEnvValue('FEDERATION_DHT_SEED') || null, + HOME_COMMUNITY_PUBLICKEY: getDevOpEnvValue('HOME_COMMUNITY_PUBLICKEY') || null, + HOME_COMMUNITY_PRIVATEKEY: getDevOpEnvValue('HOME_COMMUNITY_PRIVATEKEY') || null, +} + +export default DEVOP diff --git a/dht-node/src/config/tools.ts b/dht-node/src/config/tools.ts new file mode 100644 index 000000000..cc92f6fc2 --- /dev/null +++ b/dht-node/src/config/tools.ts @@ -0,0 +1,53 @@ +/** eslint-disable n/no-sync */ +import { logger } from '@/server/logger' +import fs = require('fs') +import os = require('os') +import path = require('path') + +const envFilePath = path.resolve(__dirname, './../../.env.devop') + +// read .env file & convert to array +const readEnvVars = () => { + if (!fs.existsSync(envFilePath)) { + logger.info(`devop config file ${envFilePath} will be created...`) + fs.writeFileSync(envFilePath, '', 'utf8') + } + return fs.readFileSync(envFilePath, 'utf-8').split(os.EOL) +} + +/** + * Finds the key in .env files and returns the corresponding value + * + * @param {string} key Key to find + * @returns {string|null} Value of the key + */ +export const getDevOpEnvValue = (key: string): string | null => { + // find the line that contains the key (exact match) + const matchedLine = readEnvVars().find((line) => line.split('=')[0] === key) + // split the line (delimiter is '=') and return the item at index 2 + return matchedLine !== undefined ? matchedLine.split('=')[1] : null +} + +/** + * Updates value for existing key or creates a new key=value line + * + * This function is a modified version of https://stackoverflow.com/a/65001580/3153583 + * + * @param {string} key Key to update/insert + * @param {string} value Value to update/insert + */ +export const setDevOpEnvValue = (key: string, value: string): void => { + const envVars = readEnvVars() + const targetLine = envVars.find((line) => line.split('=')[0] === key) + if (targetLine !== undefined) { + // update existing line + const targetLineIndex = envVars.indexOf(targetLine) + // replace the key/value with the new value + envVars.splice(targetLineIndex, 1, `${key}="${value}"`) + } else { + // create new key value + envVars.push(`${key}="${value}"`) + } + // write everything back to the file system + fs.writeFileSync(envFilePath, envVars.join(os.EOL)) +} diff --git a/dht-node/src/dht_node/index.ts b/dht-node/src/dht_node/index.ts index d101037ae..b91029dee 100644 --- a/dht-node/src/dht_node/index.ts +++ b/dht-node/src/dht_node/index.ts @@ -4,10 +4,19 @@ import DHT from '@hyperswarm/dht' import { logger } from '@/server/logger' import CONFIG from '@/config' import { Community as DbCommunity } from '@entity/Community' +import DEVOP from '@/config/devop' +import { setDevOpEnvValue } from '@/config/tools' const KEY_SECRET_SEEDBYTES = 32 -const getSeed = (): Buffer | null => - CONFIG.FEDERATION_DHT_SEED ? Buffer.alloc(KEY_SECRET_SEEDBYTES, CONFIG.FEDERATION_DHT_SEED) : null +const getSeed = (): Buffer | null => { + let dhtseed = DEVOP.FEDERATION_DHT_SEED + logger.debug('dhtseed set by DEVOP.FEDERATION_DHT_SEED={}', DEVOP.FEDERATION_DHT_SEED) + if (!dhtseed) { + dhtseed = CONFIG.FEDERATION_DHT_SEED + logger.debug('dhtseed overwritten by CONFIG.FEDERATION_DHT_SEED={}', CONFIG.FEDERATION_DHT_SEED) + } + return dhtseed ? Buffer.alloc(KEY_SECRET_SEEDBYTES, dhtseed) : null +} const POLLTIME = 20000 const SUCCESSTIME = 120000 @@ -30,6 +39,9 @@ export const startDHT = async (topic: string): Promise => { const keyPair = DHT.keyPair(getSeed()) logger.info(`keyPairDHT: publicKey=${keyPair.publicKey.toString('hex')}`) logger.debug(`keyPairDHT: secretKey=${keyPair.secretKey.toString('hex')}`) + // insert or update keyPair in .env.devop file + setDevOpEnvValue('HOME_COMMUNITY_PUBLICKEY', keyPair.publicKey.toString('hex')) + setDevOpEnvValue('HOME_COMMUNITY_PRIVATEKEY', keyPair.secretKey.toString('hex')) const ownApiVersions = await writeHomeCommunityEnries(keyPair.publicKey) logger.info(`ApiList: ${JSON.stringify(ownApiVersions)}`) diff --git a/dht-node/src/index.ts b/dht-node/src/index.ts index 2315c77df..19fc014c8 100644 --- a/dht-node/src/index.ts +++ b/dht-node/src/index.ts @@ -3,6 +3,7 @@ import { startDHT } from '@/dht_node/index' // config import CONFIG from './config' +import DEVOP from './config/devop' import { logger } from './server/logger' import connection from './typeorm/connection' import { checkDBVersion } from './typeorm/DBVersion' @@ -21,14 +22,22 @@ async function main() { logger.fatal('Fatal: Database Version incorrect') throw new Error('Fatal: Database Version incorrect') } - - // eslint-disable-next-line no-console - console.log( - `starting Federation on ${CONFIG.FEDERATION_DHT_TOPIC} ${ - CONFIG.FEDERATION_DHT_SEED ? 'with seed...' : 'without seed...' - }`, - ) - await startDHT(CONFIG.FEDERATION_DHT_TOPIC) + // first read from .env.devop if exist + let dhttopic = DEVOP.FEDERATION_DHT_TOPIC + logger.debug('dhttopic set by DEVOP.FEDERATION_DHT_TOPIC={}', DEVOP.FEDERATION_DHT_TOPIC) + if(!dhttopic) { + dhttopic = CONFIG.FEDERATION_DHT_TOPIC + logger.debug('dhttopic overwritten by CONFIG.FEDERATION_DHT_TOPIC={}', CONFIG.FEDERATION_DHT_TOPIC) + } + let dhtseed = DEVOP.FEDERATION_DHT_SEED + logger.debug('dhtseed set by DEVOP.FEDERATION_DHT_SEED={}', DEVOP.FEDERATION_DHT_SEED) + if(!dhtseed) { + dhtseed = CONFIG.FEDERATION_DHT_SEED + logger.debug('dhtseed overwritten by CONFIG.FEDERATION_DHT_SEED={}', CONFIG.FEDERATION_DHT_SEED) + } + logger.info( + `starting Federation on ${dhttopic} ${dhtseed ? 'with seed...' : 'without seed...'}`) + await startDHT(dhttopic) } main().catch((e) => {