From 33ca2040a88495fc92f040ce54b77e1124f09ec7 Mon Sep 17 00:00:00 2001 From: "Patrik J. Braun" Date: Sun, 30 Jul 2023 14:06:25 +0200 Subject: [PATCH] Moving sendMail checking to be only on startup #683 --- src/backend/Environment.ts | 6 ++++++ src/backend/middlewares/RenderingMWs.ts | 1 - src/backend/model/diagnostics/ConfigDiagnostics.ts | 10 ++++++---- src/common/config/private/Config.ts | 4 ++++ src/common/config/private/PrivateConfig.ts | 10 ++++++++-- .../integration/routers/admin/SettingsRouter.ts | 5 ++--- 6 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 src/backend/Environment.ts diff --git a/src/backend/Environment.ts b/src/backend/Environment.ts new file mode 100644 index 00000000..85bdb7c2 --- /dev/null +++ b/src/backend/Environment.ts @@ -0,0 +1,6 @@ +/** + * Keeps the environment context + */ +export const ServerEnvironment = { + sendMailAvailable: false +}; diff --git a/src/backend/middlewares/RenderingMWs.ts b/src/backend/middlewares/RenderingMWs.ts index 926d5194..27e89cac 100644 --- a/src/backend/middlewares/RenderingMWs.ts +++ b/src/backend/middlewares/RenderingMWs.ts @@ -109,7 +109,6 @@ export class RenderingMWs { res: Response ): Promise { const originalConf = await Config.original(); - await ConfigDiagnostics.checkEnvironment(originalConf); // These are sensitive information, do not send to the client side originalConf.Server.sessionSecret = null; const message = new Message( diff --git a/src/backend/model/diagnostics/ConfigDiagnostics.ts b/src/backend/model/diagnostics/ConfigDiagnostics.ts index 334cd7f3..f803763e 100644 --- a/src/backend/model/diagnostics/ConfigDiagnostics.ts +++ b/src/backend/model/diagnostics/ConfigDiagnostics.ts @@ -28,6 +28,7 @@ import {SearchQueryTypes, TextSearch,} from '../../../common/entities/SearchQuer import {Utils} from '../../../common/Utils'; import {createTransport} from 'nodemailer'; import {EmailMessagingType, MessagingConfig} from '../../../common/config/private/MessagingConfig'; +import {ServerEnvironment} from '../../Environment'; const LOG_TAG = '[ConfigDiagnostics]'; @@ -84,8 +85,8 @@ export class ConfigDiagnostics { private static async testEmailMessagingConfig(Messaging: MessagingConfig, config: PrivateConfigClass): Promise { Logger.debug(LOG_TAG, 'Testing EmailMessaging config'); - if(Messaging.Email.type === EmailMessagingType.sendmail && !Config.Environment.sendMailAvailable){ - throw new Error('sendmail e-mail sending method is not supported as the sendmail application cannot be found in the OS.') + if (Messaging.Email.type === EmailMessagingType.sendmail && !Config.Environment.sendMailAvailable) { + throw new Error('sendmail e-mail sending method is not supported as the sendmail application cannot be found in the OS.'); } } @@ -299,7 +300,7 @@ export class ConfigDiagnostics { } - static async checkEnvironment(Config:PrivateConfigClass): Promise { + static async checkAndSetEnvironment(): Promise { Logger.debug(LOG_TAG, 'Checking sendmail availability'); const transporter = createTransport({ sendmail: true, @@ -309,6 +310,7 @@ export class ConfigDiagnostics { } catch (e) { Config.Environment.sendMailAvailable = false; } + ServerEnvironment.sendMailAvailable = Config.Environment.sendMailAvailable; if (!Config.Environment.sendMailAvailable) { Config.Messaging.Email.type = EmailMessagingType.SMTP; Logger.info(LOG_TAG, 'Sendmail is not available on the OS. You will need to use an SMTP server if you wish the app to send mails.'); @@ -322,7 +324,7 @@ export class ConfigDiagnostics { } try { - await ConfigDiagnostics.checkEnvironment(Config); + await ConfigDiagnostics.checkAndSetEnvironment(); } catch (ex) { const err: Error = ex; NotificationManager.error( diff --git a/src/common/config/private/Config.ts b/src/common/config/private/Config.ts index bf517148..b8b74d40 100644 --- a/src/common/config/private/Config.ts +++ b/src/common/config/private/Config.ts @@ -6,6 +6,9 @@ import {ConfigClass, ConfigClassBuilder} from 'typeconfig/node'; import {IConfigClass} from 'typeconfig/common'; import {PasswordHelper} from '../../../backend/model/PasswordHelper'; import {TAGS} from '../public/ClientConfig'; +import {ServerEnvironment} from '../../../backend/Environment'; +import {EmailMessagingType} from './MessagingConfig'; +import {Logger} from '../../../backend/Logger'; declare const process: any; @@ -79,6 +82,7 @@ export class PrivateConfigClass extends ServerConfig { require('../../../../package.json').buildCommitHash; this.Environment.upTime = upTime; this.Environment.isDocker = !!process.env.PI_DOCKER; + this.Environment.sendMailAvailable = ServerEnvironment.sendMailAvailable; } async original(): Promise { diff --git a/src/common/config/private/PrivateConfig.ts b/src/common/config/private/PrivateConfig.ts index 08ca3a71..e9b60a19 100644 --- a/src/common/config/private/PrivateConfig.ts +++ b/src/common/config/private/PrivateConfig.ts @@ -30,7 +30,7 @@ import {DefaultsJobs} from '../../entities/job/JobDTO'; import {SearchQueryDTO, SearchQueryTypes, TextSearch,} from '../../entities/SearchQueryDTO'; import {SortingMethods} from '../../entities/SortingMethods'; import {UserRoles} from '../../entities/UserDTO'; -import {MessagingConfig} from './MessagingConfig'; +import {EmailMessagingType, MessagingConfig} from './MessagingConfig'; declare let $localize: (s: TemplateStringsArray) => string; @@ -904,6 +904,7 @@ export class ServerPreviewConfig { @SubConfigClass({softReadonly: true}) export class ServerMediaConfig extends ClientMediaConfig { @ConfigProperty({ + tags: { name: $localize`Images folder`, priority: ConfigPriority.basic, @@ -1050,8 +1051,13 @@ export class ServerEnvironmentConfig { buildCommitHash: string | undefined; @ConfigProperty({volatile: true}) isDocker: boolean | undefined; - @ConfigProperty({ + @ConfigProperty({ volatile: true, + onNewValue: (value, config) => { + if (value === false) { + config.Messaging.Email.type = EmailMessagingType.SMTP; + } + }, description: 'App updates on start-up if sendmail binary is available' }) sendMailAvailable: boolean | undefined; diff --git a/test/backend/integration/routers/admin/SettingsRouter.ts b/test/backend/integration/routers/admin/SettingsRouter.ts index 36663cf0..6f65578f 100644 --- a/test/backend/integration/routers/admin/SettingsRouter.ts +++ b/test/backend/integration/routers/admin/SettingsRouter.ts @@ -1,12 +1,12 @@ import * as path from 'path'; import * as fs from 'fs'; import {Config} from '../../../../../src/common/config/private/Config'; -import {SQLConnection} from '../../../../../src/backend/model/database/SQLConnection'; import {Server} from '../../../../../src/backend/server'; import {DatabaseType, ServerConfig} from '../../../../../src/common/config/private/PrivateConfig'; import {ProjectPath} from '../../../../../src/backend/ProjectPath'; import {TAGS} from '../../../../../src/common/config/public/ClientConfig'; import {ObjectManagers} from '../../../../../src/backend/model/ObjectManagers'; +import {UserRoles} from '../../../../../src/common/entities/UserDTO'; process.env.NODE_ENV = 'test'; const chai: any = require('chai'); @@ -34,9 +34,8 @@ describe('SettingsRouter', () => { describe('/GET settings', () => { it('it should GET the settings', async () => { Config.Users.authenticationRequired = false; + Config.Users.unAuthenticatedUserRole = UserRoles.Admin; const originalSettings = await Config.original(); - // originalSettings.Server.sessionSecret = null; - // originalSettings.Users.enforcedUsers = null; const srv = new Server(); await srv.onStarted.wait(); const result = await chai.request(srv.App)