From f8ce4e65836ef9a6e852bad99b18965c078f6e04 Mon Sep 17 00:00:00 2001 From: John Livingston Date: Tue, 7 Dec 2021 11:59:32 +0100 Subject: [PATCH] Config: preparing the arrival of new config files (for bots). Code refactoring. --- server/lib/diagnostic/prosody.ts | 57 +++++++++++--------- server/lib/prosody/config.ts | 85 +++++++++++++++++++----------- server/lib/prosody/config/paths.ts | 1 + server/lib/prosody/ctl.ts | 27 +++++----- 4 files changed, 102 insertions(+), 68 deletions(-) diff --git a/server/lib/diagnostic/prosody.ts b/server/lib/diagnostic/prosody.ts index 5ba10e49..d4bfc0ef 100644 --- a/server/lib/diagnostic/prosody.ts +++ b/server/lib/diagnostic/prosody.ts @@ -1,4 +1,4 @@ -import { getProsodyConfig, getProsodyConfigContentForDiagnostic, getWorkingDir } from '../prosody/config' +import { getProsodyConfig, getWorkingDir } from '../prosody/config' import { getProsodyAbout, testProsodyCorrectlyRunning } from '../prosody/ctl' import { newResult, TestResult } from './utils' import { getAPIKey } from '../apikey' @@ -24,7 +24,6 @@ export async function diagProsody (test: string, options: RegisterServerOptions) let prosodyHost: string try { const wantedConfig = await getProsodyConfig(options) - const filePath = wantedConfig.paths.config result.messages.push(`Prosody will run on port '${wantedConfig.port}'`) prosodyPort = wantedConfig.port @@ -54,34 +53,40 @@ export async function diagProsody (test: string, options: RegisterServerOptions) result.messages.push(`The Demo bot is active for videos: ${wantedConfig.bots.demo.join(', ')}`) } - await fs.promises.access(filePath, fs.constants.R_OK) // throw an error if file does not exist. - result.messages.push(`The prosody configuration file (${filePath}) exists`) - const actualContent = await fs.promises.readFile(filePath, { - encoding: 'utf-8' - }) + const configFiles = wantedConfig.getConfigFiles() + for (const configFile of configFiles) { + const filePath = configFile.path + const configFileKey = configFile.key - result.debug.push({ - title: 'Current prosody configuration', - // we have to hide secret keys and other values. - // But here, we haven't them for actualContent. - // So we will use values in wantedConfig, hopping it is enough. - message: getProsodyConfigContentForDiagnostic(wantedConfig, actualContent) - }) - - const wantedContent = wantedConfig.content - if (actualContent === wantedContent) { - result.messages.push('Prosody configuration file content is correct.') - } else { - result.messages.push('Prosody configuration file content is not correct.') - result.debug.push({ - title: 'Prosody configuration should be', - // we have to hide secret keys and other values: - message: getProsodyConfigContentForDiagnostic(wantedConfig) + await fs.promises.access(filePath, fs.constants.R_OK) // throw an error if file does not exist. + result.messages.push(`The prosody '${configFileKey}' configuration file (${filePath}) exists`) + const actualContent = await fs.promises.readFile(filePath, { + encoding: 'utf-8' }) - return result + + result.debug.push({ + title: `Current prosody '${configFileKey}' configuration`, + // we have to hide secret keys and other values. + // But here, we haven't them for actualContent. + // So we will use values in wantedConfig, hopping it is enough. + message: wantedConfig.contentForDiagnostic(actualContent) + }) + + const wantedContent = configFile.content + if (actualContent === wantedContent) { + result.messages.push(`Prosody configuration file '${configFileKey}' content is correct.`) + } else { + result.messages.push(`Prosody configuration file '${configFileKey}'' content is not correct.`) + result.debug.push({ + title: `Prosody configuration '${configFileKey}' should be`, + // we have to hide secret keys and other values: + message: wantedConfig.contentForDiagnostic(wantedContent) + }) + return result + } } } catch (error) { - result.messages.push('Error when requiring the prosody config file: ' + (error as string)) + result.messages.push('Error when testing the prosody config: ' + (error as string)) return result } diff --git a/server/lib/prosody/config.ts b/server/lib/prosody/config.ts index cc5a30df..3503a4c0 100644 --- a/server/lib/prosody/config.ts +++ b/server/lib/prosody/config.ts @@ -60,6 +60,7 @@ async function getProsodyFilePaths (options: RegisterServerOptions): Promise + +class ProsodyConfig { + constructor ( + private readonly configFiles: ProsodyConfigFiles, + public paths: ProsodyFilePaths, + public host: string, + public port: string, + public baseApiUrl: string, + public roomType: 'video' | 'channel', + public logByDefault: boolean, + public logExpiration: ConfigLogExpiration, + public bots: ProsodyConfigBots, + public valuesToHideInDiagnostic: {[key: string]: string} + ) {} + + public getConfigFiles (): ProsodyConfigFiles { + return this.configFiles + } + + public contentForDiagnostic (content: string): string { + let r: string = content + for (const key in this.valuesToHideInDiagnostic) { + // replaceAll not available, using trick: + r = r.split(this.valuesToHideInDiagnostic[key]).join(`***${key}***`) + } + return r + } } + async function getProsodyConfig (options: RegisterServerOptions): Promise { const logger = options.peertubeHelpers.logger logger.debug('Calling getProsodyConfig') @@ -182,18 +207,24 @@ async function getProsodyConfig (options: RegisterServerOptions): Promise { @@ -204,12 +235,16 @@ async function writeProsodyConfig (options: RegisterServerOptions): Promise