mirror of
https://github.com/IT4Change/gradido.git
synced 2026-02-06 09:56:05 +00:00
introduce .env.devop file containing topic, seed and keypair
This commit is contained in:
parent
a4351ba881
commit
bdf94f6c51
13
dht-node/src/config/devop.ts
Normal file
13
dht-node/src/config/devop.ts
Normal file
@ -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
|
||||
53
dht-node/src/config/tools.ts
Normal file
53
dht-node/src/config/tools.ts
Normal file
@ -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))
|
||||
}
|
||||
@ -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<void> => {
|
||||
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)}`)
|
||||
|
||||
@ -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) => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user