From 737328d7b03f20d29fa408cdddcf76fac959b65e Mon Sep 17 00:00:00 2001 From: sithlord48 Date: Thu, 6 Mar 2025 19:04:55 -0500 Subject: [PATCH] refactor: move TlsCertPath to Settings newkey: security/certPath <= General/tlsCertPath remove tlsCertPath from appConfig --- src/apps/deskflow-gui/MainWindow.cpp | 8 +++--- .../deskflow-gui/dialogs/SettingsDialog.cpp | 9 ++++--- src/lib/common/Settings.cpp | 7 +++-- src/lib/common/Settings.h | 2 ++ src/lib/gui/config/AppConfig.cpp | 26 ++----------------- src/lib/gui/config/AppConfig.h | 14 +--------- src/lib/gui/config/IAppConfig.h | 2 -- src/lib/gui/core/CoreProcess.cpp | 2 +- src/lib/gui/diagnostic.cpp | 4 +-- src/lib/gui/paths.h | 7 ----- src/lib/gui/tls/TlsUtility.cpp | 6 ++--- src/test/shared/gui/mocks/AppConfigMock.h | 2 -- .../unittests/gui/config/AppConfigTests.cpp | 2 -- 13 files changed, 26 insertions(+), 65 deletions(-) diff --git a/src/apps/deskflow-gui/MainWindow.cpp b/src/apps/deskflow-gui/MainWindow.cpp index 092c4a733..5f4a0b00a 100644 --- a/src/apps/deskflow-gui/MainWindow.cpp +++ b/src/apps/deskflow-gui/MainWindow.cpp @@ -388,7 +388,8 @@ void MainWindow::configScopesSaving() void MainWindow::appConfigTlsChanged() { - if (m_tlsUtility.isEnabled() && !QFile::exists(m_appConfig.tlsCertPath())) { + const auto certificate = Settings::value(Settings::Security::Certificate).toString(); + if (m_tlsUtility.isEnabled() && !QFile::exists(certificate)) { m_tlsUtility.generateCertificate(); } updateSecurityIcon(m_lblSecurityStatus->isVisible()); @@ -1149,12 +1150,13 @@ QString MainWindow::trustedFingerprintDb() bool MainWindow::regenerateLocalFingerprints() { - if (!QFile::exists(m_appConfig.tlsCertPath()) && !m_tlsUtility.generateCertificate()) { + const auto certificate = Settings::value(Settings::Security::Certificate).toString(); + if (!QFile::exists(certificate) && !m_tlsUtility.generateCertificate()) { return false; } TlsCertificate tls; - if (!tls.generateFingerprint(m_appConfig.tlsCertPath())) { + if (!tls.generateFingerprint(certificate)) { return false; } diff --git a/src/apps/deskflow-gui/dialogs/SettingsDialog.cpp b/src/apps/deskflow-gui/dialogs/SettingsDialog.cpp index cdec9121e..040cd3c6b 100644 --- a/src/apps/deskflow-gui/dialogs/SettingsDialog.cpp +++ b/src/apps/deskflow-gui/dialogs/SettingsDialog.cpp @@ -152,7 +152,7 @@ void SettingsDialog::accept() Settings::setValue(Settings::Gui::Autohide, ui->cbAutoHide->isChecked()); Settings::setValue(Settings::Gui::AutoUpdateCheck, ui->cbAutoUpdate->isChecked()); m_appConfig.setPreventSleep(ui->cbPreventSleep->isChecked()); - m_appConfig.setTlsCertPath(ui->lineTlsCertPath->text()); + Settings::setValue(Settings::Security::Certificate, ui->lineTlsCertPath->text()); Settings::setValue(Settings::Security::KeySize, ui->comboTlsKeyLength->currentText().toInt()); Settings::setValue(Settings::Security::TlsEnabled, ui->groupSecurity->isChecked()); m_appConfig.setLanguageSync(ui->cbLanguageSync->isChecked()); @@ -210,8 +210,9 @@ void SettingsDialog::loadFromConfig() void SettingsDialog::updateTlsControls() { - if (QFile(m_appConfig.tlsCertPath()).exists()) { - updateKeyLengthOnFile(m_appConfig.tlsCertPath()); + const auto certificate = Settings::value(Settings::Security::Certificate).toString(); + if (QFile(certificate).exists()) { + updateKeyLengthOnFile(certificate); } else { const auto keyLengthText = Settings::value(Settings::Security::KeySize).toString(); ui->comboTlsKeyLength->setCurrentText(keyLengthText); @@ -221,7 +222,7 @@ void SettingsDialog::updateTlsControls() const auto writable = m_appConfig.isActiveScopeWritable(); const auto enabled = writable && tlsEnabled; - ui->lineTlsCertPath->setText(m_appConfig.tlsCertPath()); + ui->lineTlsCertPath->setText(certificate); ui->cbRequireClientCert->setChecked(m_appConfig.requireClientCerts()); ui->groupSecurity->setChecked(tlsEnabled); diff --git a/src/lib/common/Settings.cpp b/src/lib/common/Settings.cpp index 4583cbb84..c129d34d0 100644 --- a/src/lib/common/Settings.cpp +++ b/src/lib/common/Settings.cpp @@ -74,8 +74,8 @@ QVariant Settings::defaultValue(const QString &key) return false; } - if ((key == Gui::CloseToTray) || (key == Gui::LogExpanded) || (key == Gui::SymbolicTrayIcon) - || (key == Gui::CloseReminder) || (key == Security::TlsEnabled)) { + if ((key == Gui::CloseToTray) || (key == Gui::LogExpanded) || (key == Gui::SymbolicTrayIcon) || + (key == Gui::CloseReminder) || (key == Security::TlsEnabled)) { return true; } @@ -85,6 +85,9 @@ QVariant Settings::defaultValue(const QString &key) if (key == Security::KeySize) return 2048; + if (key == Settings::Security::Certificate) + return QStringLiteral("%1/%2/%3").arg(instance()->settingsPath(), kTlsDirName, kTlsCertificateFilename); + return QVariant(); } diff --git a/src/lib/common/Settings.h b/src/lib/common/Settings.h index c6939b4d2..975d45781 100644 --- a/src/lib/common/Settings.h +++ b/src/lib/common/Settings.h @@ -47,6 +47,7 @@ public: }; struct Security { + inline static const auto Certificate = QStringLiteral("security/certificate"); inline static const auto KeySize = QStringLiteral("security/keySize"); inline static const auto TlsEnabled = QStringLiteral("security/tlsEnabled"); }; @@ -90,6 +91,7 @@ private: , Gui::LogExpanded , Gui::SymbolicTrayIcon , Gui::WindowGeometry + , Security::Certificate , Security::KeySize , Security::TlsEnabled }; diff --git a/src/lib/gui/config/AppConfig.cpp b/src/lib/gui/config/AppConfig.cpp index 6e7c7afc4..123d2a1a3 100644 --- a/src/lib/gui/config/AppConfig.cpp +++ b/src/lib/gui/config/AppConfig.cpp @@ -59,7 +59,7 @@ const char *const AppConfig::m_SettingsName[] = { "useInternalConfig", "groupClientChecked", "serverHostname", - "tlsCertPath", + "", // 26 cert path moved to deskflow settings "", // 27 key length Moved to Deskflow settings "preventSleep", "languageSync", @@ -85,8 +85,7 @@ const char *const AppConfig::m_SettingsName[] = { AppConfig::AppConfig(deskflow::gui::IConfigScopes &scopes, std::shared_ptr deps) : m_Scopes(scopes), m_pDeps(deps), - m_ScreenName(deps->hostname()), - m_TlsCertPath(deps->defaultTlsCertPath()) + m_ScreenName(deps->hostname()) { determineScope(); recall(); @@ -131,7 +130,6 @@ void AppConfig::recallFromCurrentScope() m_LanguageSync = getFromCurrentScope(kLanguageSync, m_LanguageSync).toBool(); m_InvertScrollDirection = getFromCurrentScope(kInvertScrollDirection, m_InvertScrollDirection).toBool(); m_EnableService = getFromCurrentScope(kEnableService, m_EnableService).toBool(); - m_TlsCertPath = getFromCurrentScope(kTlsCertPath, m_TlsCertPath).toString(); m_RequireClientCert = getFromCurrentScope(kRequireClientCert, m_RequireClientCert).toBool(); } @@ -182,11 +180,6 @@ void AppConfig::commit() setInCurrentScope(kEnableService, m_EnableService); setInCurrentScope(kRequireClientCert, m_RequireClientCert); } - - if (m_TlsChanged) { - m_TlsChanged = false; - Q_EMIT tlsChanged(); - } } void AppConfig::determineScope() @@ -462,11 +455,6 @@ bool AppConfig::preventSleep() const return m_PreventSleep; } -QString AppConfig::tlsCertPath() const -{ - return m_TlsCertPath; -} - bool AppConfig::enableService() const { return m_EnableService; @@ -515,16 +503,6 @@ const QString &AppConfig::serverHostname() const // Begin setters /////////////////////////////////////////////////////////////////////////////// -void AppConfig::setTlsCertPath(const QString &value) -{ - if (m_TlsCertPath != value) { - // deliberately only set the changed flag if there was a change. - // it's important not to set this flag to false here. - m_TlsChanged = true; - } - m_TlsCertPath = value; -} - void AppConfig::setServerGroupChecked(bool newValue) { m_ServerGroupChecked = newValue; diff --git a/src/lib/gui/config/AppConfig.h b/src/lib/gui/config/AppConfig.h index 77b58d5f7..8b44f51d3 100644 --- a/src/lib/gui/config/AppConfig.h +++ b/src/lib/gui/config/AppConfig.h @@ -80,7 +80,7 @@ private: kUseInternalConfig = 23, kClientGroupChecked = 24, kServerHostname = 25, - kTlsCertPath = 26, + // 26 = kTlsCertPath moved to deskflow settings // 27 = tlsKeyLength Moved to deskflow settings kPreventSleep = 28, kLanguageSync = 29, @@ -107,10 +107,6 @@ public: struct Deps { virtual ~Deps() = default; - virtual QString defaultTlsCertPath() const - { - return deskflow::gui::paths::defaultTlsCertPath(); - } virtual QString hostname() const { return QHostInfo::localHostName(); @@ -134,7 +130,6 @@ public: IConfigScopes &scopes() const override; ProcessMode processMode() const override; ElevateMode elevateMode() const override; - QString tlsCertPath() const override; QString logLevelText() const override; const QString &screenName() const override; bool logToFile() const override; @@ -181,7 +176,6 @@ public: void setLanguageSync(bool b) override; void setPreventSleep(bool b) override; void setEnableService(bool enabled) override; - void setTlsCertPath(const QString &path) override; void setRequireClientCerts(bool requireClientCerts) override; // @@ -248,11 +242,6 @@ private: /// @param [in] scope which should be loaded. void loadScope(IConfigScopes::Scope scope); - /** - * @brief Gets a TLS certificate path based on the user's profile dir. - */ - QString defaultTlsCertPath() const; - // Used to make the server and client names on windows. #ifdef Q_OS_WIN inline static const auto s_winExeTemplate = QStringLiteral("%1.exe"); @@ -300,7 +289,6 @@ private: deskflow::gui::IConfigScopes &m_Scopes; std::shared_ptr m_pDeps; QString m_ScreenName; - QString m_TlsCertPath; signals: void tlsChanged(); diff --git a/src/lib/gui/config/IAppConfig.h b/src/lib/gui/config/IAppConfig.h index 7319ca7ec..2367d32fc 100644 --- a/src/lib/gui/config/IAppConfig.h +++ b/src/lib/gui/config/IAppConfig.h @@ -32,7 +32,6 @@ public: // virtual IConfigScopes &scopes() const = 0; - virtual QString tlsCertPath() const = 0; virtual ProcessMode processMode() const = 0; virtual ElevateMode elevateMode() const = 0; virtual QString logLevelText() const = 0; @@ -70,7 +69,6 @@ public: virtual void setLogFilename(const QString &logFilename) = 0; virtual void setElevateMode(ElevateMode elevateMode) = 0; virtual void setPreventSleep(bool preventSleep) = 0; - virtual void setTlsCertPath(const QString &tlsCertPath) = 0; virtual void setLanguageSync(bool languageSync) = 0; virtual void setInvertScrollDirection(bool invertScrollDirection) = 0; virtual void setEnableService(bool enableService) = 0; diff --git a/src/lib/gui/core/CoreProcess.cpp b/src/lib/gui/core/CoreProcess.cpp index cd18fe03b..d45d5bf7a 100644 --- a/src/lib/gui/core/CoreProcess.cpp +++ b/src/lib/gui/core/CoreProcess.cpp @@ -561,7 +561,7 @@ bool CoreProcess::addServerArgs(QStringList &args, QString &app) qCritical("failed to persist tls certificate"); return false; } - args << "--tls-cert" << m_appConfig.tlsCertPath(); + args << "--tls-cert" << Settings::value(Settings::Security::Certificate).toString(); } return true; diff --git a/src/lib/gui/diagnostic.cpp b/src/lib/gui/diagnostic.cpp index 81e140bcb..eeb667d68 100644 --- a/src/lib/gui/diagnostic.cpp +++ b/src/lib/gui/diagnostic.cpp @@ -9,7 +9,7 @@ #include "config/ConfigScopes.h" #include "paths.h" -#include +#include #include #include @@ -27,7 +27,7 @@ void restart() QProcess::startDetached(program, arguments); qDebug("exiting current process"); - QApplication::exit(); + QCoreApplication::exit(); } void clearSettings(ConfigScopes &scopes, bool enableRestart) diff --git a/src/lib/gui/paths.h b/src/lib/gui/paths.h index bde6990f6..cdfdaf937 100644 --- a/src/lib/gui/paths.h +++ b/src/lib/gui/paths.h @@ -46,11 +46,4 @@ inline QDir coreProfileDir() return QDir(coreTool.getProfileDir()); } -inline QString defaultTlsCertPath() -{ - const auto root = coreProfileDir(); - const auto sslDirPath = QDir(root.filePath(kTlsDirName)); - return sslDirPath.filePath(kTlsCertificateFilename); -} - } // namespace deskflow::gui::paths diff --git a/src/lib/gui/tls/TlsUtility.cpp b/src/lib/gui/tls/TlsUtility.cpp index fad64ca97..61553d95f 100644 --- a/src/lib/gui/tls/TlsUtility.cpp +++ b/src/lib/gui/tls/TlsUtility.cpp @@ -38,15 +38,15 @@ bool TlsUtility::generateCertificate() } auto length = Settings::value(Settings::Security::KeySize).toInt(); - - return m_certificate.generateCertificate(m_appConfig.tlsCertPath(), length); + const auto certificate = Settings::value(Settings::Security::Certificate).toString(); + return m_certificate.generateCertificate(certificate, length); } bool TlsUtility::persistCertificate() { qDebug("persisting tls certificate"); - if (QFile::exists(m_appConfig.tlsCertPath())) { + if (QFile::exists(Settings::value(Settings::Security::Certificate).toString())) { qDebug("tls certificate already exists"); return true; } diff --git a/src/test/shared/gui/mocks/AppConfigMock.h b/src/test/shared/gui/mocks/AppConfigMock.h index 2662c11ce..c5257e03d 100644 --- a/src/test/shared/gui/mocks/AppConfigMock.h +++ b/src/test/shared/gui/mocks/AppConfigMock.h @@ -33,7 +33,6 @@ public: // MOCK_METHOD(deskflow::gui::IConfigScopes &, scopes, (), (const, override)); - MOCK_METHOD(QString, tlsCertPath, (), (const, override)); MOCK_METHOD(ProcessMode, processMode, (), (const, override)); MOCK_METHOD(ElevateMode, elevateMode, (), (const, override)); MOCK_METHOD(QString, logLevelText, (), (const, override)); @@ -71,7 +70,6 @@ public: MOCK_METHOD(void, setLogFilename, (const QString &logFilename), (override)); MOCK_METHOD(void, setElevateMode, (ElevateMode elevateMode), (override)); MOCK_METHOD(void, setPreventSleep, (bool preventSleep), (override)); - MOCK_METHOD(void, setTlsCertPath, (const QString &tlsCertPath), (override)); MOCK_METHOD(void, setLanguageSync, (bool languageSync), (override)); MOCK_METHOD(void, setInvertScrollDirection, (bool invertScrollDirection), (override)); MOCK_METHOD(void, setEnableService, (bool enableService), (override)); diff --git a/src/test/unittests/gui/config/AppConfigTests.cpp b/src/test/unittests/gui/config/AppConfigTests.cpp index 80dd9d3c3..e96a6538f 100644 --- a/src/test/unittests/gui/config/AppConfigTests.cpp +++ b/src/test/unittests/gui/config/AppConfigTests.cpp @@ -41,7 +41,6 @@ struct DepsMock : public AppConfig::Deps { DepsMock() { - ON_CALL(*this, defaultTlsCertPath()).WillByDefault(Return("stub")); ON_CALL(*this, hostname()).WillByDefault(Return("stub")); } @@ -50,7 +49,6 @@ struct DepsMock : public AppConfig::Deps return std::make_shared>(); } - MOCK_METHOD(QString, defaultTlsCertPath, (), (const, override)); MOCK_METHOD(QString, hostname, (), (const, override)); };