refactor: move TlsCertPath to Settings

newkey: security/certPath <= General/tlsCertPath
remove tlsCertPath from appConfig
This commit is contained in:
sithlord48 2025-03-06 19:04:55 -05:00 committed by Nick Bolton
parent bee0f84556
commit 737328d7b0
13 changed files with 26 additions and 65 deletions

View file

@ -388,7 +388,8 @@ void MainWindow::configScopesSaving()
void MainWindow::appConfigTlsChanged() 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(); m_tlsUtility.generateCertificate();
} }
updateSecurityIcon(m_lblSecurityStatus->isVisible()); updateSecurityIcon(m_lblSecurityStatus->isVisible());
@ -1149,12 +1150,13 @@ QString MainWindow::trustedFingerprintDb()
bool MainWindow::regenerateLocalFingerprints() 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; return false;
} }
TlsCertificate tls; TlsCertificate tls;
if (!tls.generateFingerprint(m_appConfig.tlsCertPath())) { if (!tls.generateFingerprint(certificate)) {
return false; return false;
} }

View file

@ -152,7 +152,7 @@ void SettingsDialog::accept()
Settings::setValue(Settings::Gui::Autohide, ui->cbAutoHide->isChecked()); Settings::setValue(Settings::Gui::Autohide, ui->cbAutoHide->isChecked());
Settings::setValue(Settings::Gui::AutoUpdateCheck, ui->cbAutoUpdate->isChecked()); Settings::setValue(Settings::Gui::AutoUpdateCheck, ui->cbAutoUpdate->isChecked());
m_appConfig.setPreventSleep(ui->cbPreventSleep->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::KeySize, ui->comboTlsKeyLength->currentText().toInt());
Settings::setValue(Settings::Security::TlsEnabled, ui->groupSecurity->isChecked()); Settings::setValue(Settings::Security::TlsEnabled, ui->groupSecurity->isChecked());
m_appConfig.setLanguageSync(ui->cbLanguageSync->isChecked()); m_appConfig.setLanguageSync(ui->cbLanguageSync->isChecked());
@ -210,8 +210,9 @@ void SettingsDialog::loadFromConfig()
void SettingsDialog::updateTlsControls() void SettingsDialog::updateTlsControls()
{ {
if (QFile(m_appConfig.tlsCertPath()).exists()) { const auto certificate = Settings::value(Settings::Security::Certificate).toString();
updateKeyLengthOnFile(m_appConfig.tlsCertPath()); if (QFile(certificate).exists()) {
updateKeyLengthOnFile(certificate);
} else { } else {
const auto keyLengthText = Settings::value(Settings::Security::KeySize).toString(); const auto keyLengthText = Settings::value(Settings::Security::KeySize).toString();
ui->comboTlsKeyLength->setCurrentText(keyLengthText); ui->comboTlsKeyLength->setCurrentText(keyLengthText);
@ -221,7 +222,7 @@ void SettingsDialog::updateTlsControls()
const auto writable = m_appConfig.isActiveScopeWritable(); const auto writable = m_appConfig.isActiveScopeWritable();
const auto enabled = writable && tlsEnabled; const auto enabled = writable && tlsEnabled;
ui->lineTlsCertPath->setText(m_appConfig.tlsCertPath()); ui->lineTlsCertPath->setText(certificate);
ui->cbRequireClientCert->setChecked(m_appConfig.requireClientCerts()); ui->cbRequireClientCert->setChecked(m_appConfig.requireClientCerts());
ui->groupSecurity->setChecked(tlsEnabled); ui->groupSecurity->setChecked(tlsEnabled);

View file

@ -74,8 +74,8 @@ QVariant Settings::defaultValue(const QString &key)
return false; return false;
} }
if ((key == Gui::CloseToTray) || (key == Gui::LogExpanded) || (key == Gui::SymbolicTrayIcon) if ((key == Gui::CloseToTray) || (key == Gui::LogExpanded) || (key == Gui::SymbolicTrayIcon) ||
|| (key == Gui::CloseReminder) || (key == Security::TlsEnabled)) { (key == Gui::CloseReminder) || (key == Security::TlsEnabled)) {
return true; return true;
} }
@ -85,6 +85,9 @@ QVariant Settings::defaultValue(const QString &key)
if (key == Security::KeySize) if (key == Security::KeySize)
return 2048; return 2048;
if (key == Settings::Security::Certificate)
return QStringLiteral("%1/%2/%3").arg(instance()->settingsPath(), kTlsDirName, kTlsCertificateFilename);
return QVariant(); return QVariant();
} }

View file

@ -47,6 +47,7 @@ public:
}; };
struct Security struct Security
{ {
inline static const auto Certificate = QStringLiteral("security/certificate");
inline static const auto KeySize = QStringLiteral("security/keySize"); inline static const auto KeySize = QStringLiteral("security/keySize");
inline static const auto TlsEnabled = QStringLiteral("security/tlsEnabled"); inline static const auto TlsEnabled = QStringLiteral("security/tlsEnabled");
}; };
@ -90,6 +91,7 @@ private:
, Gui::LogExpanded , Gui::LogExpanded
, Gui::SymbolicTrayIcon , Gui::SymbolicTrayIcon
, Gui::WindowGeometry , Gui::WindowGeometry
, Security::Certificate
, Security::KeySize , Security::KeySize
, Security::TlsEnabled , Security::TlsEnabled
}; };

View file

@ -59,7 +59,7 @@ const char *const AppConfig::m_SettingsName[] = {
"useInternalConfig", "useInternalConfig",
"groupClientChecked", "groupClientChecked",
"serverHostname", "serverHostname",
"tlsCertPath", "", // 26 cert path moved to deskflow settings
"", // 27 key length Moved to Deskflow settings "", // 27 key length Moved to Deskflow settings
"preventSleep", "preventSleep",
"languageSync", "languageSync",
@ -85,8 +85,7 @@ const char *const AppConfig::m_SettingsName[] = {
AppConfig::AppConfig(deskflow::gui::IConfigScopes &scopes, std::shared_ptr<Deps> deps) AppConfig::AppConfig(deskflow::gui::IConfigScopes &scopes, std::shared_ptr<Deps> deps)
: m_Scopes(scopes), : m_Scopes(scopes),
m_pDeps(deps), m_pDeps(deps),
m_ScreenName(deps->hostname()), m_ScreenName(deps->hostname())
m_TlsCertPath(deps->defaultTlsCertPath())
{ {
determineScope(); determineScope();
recall(); recall();
@ -131,7 +130,6 @@ void AppConfig::recallFromCurrentScope()
m_LanguageSync = getFromCurrentScope(kLanguageSync, m_LanguageSync).toBool(); m_LanguageSync = getFromCurrentScope(kLanguageSync, m_LanguageSync).toBool();
m_InvertScrollDirection = getFromCurrentScope(kInvertScrollDirection, m_InvertScrollDirection).toBool(); m_InvertScrollDirection = getFromCurrentScope(kInvertScrollDirection, m_InvertScrollDirection).toBool();
m_EnableService = getFromCurrentScope(kEnableService, m_EnableService).toBool(); m_EnableService = getFromCurrentScope(kEnableService, m_EnableService).toBool();
m_TlsCertPath = getFromCurrentScope(kTlsCertPath, m_TlsCertPath).toString();
m_RequireClientCert = getFromCurrentScope(kRequireClientCert, m_RequireClientCert).toBool(); m_RequireClientCert = getFromCurrentScope(kRequireClientCert, m_RequireClientCert).toBool();
} }
@ -182,11 +180,6 @@ void AppConfig::commit()
setInCurrentScope(kEnableService, m_EnableService); setInCurrentScope(kEnableService, m_EnableService);
setInCurrentScope(kRequireClientCert, m_RequireClientCert); setInCurrentScope(kRequireClientCert, m_RequireClientCert);
} }
if (m_TlsChanged) {
m_TlsChanged = false;
Q_EMIT tlsChanged();
}
} }
void AppConfig::determineScope() void AppConfig::determineScope()
@ -462,11 +455,6 @@ bool AppConfig::preventSleep() const
return m_PreventSleep; return m_PreventSleep;
} }
QString AppConfig::tlsCertPath() const
{
return m_TlsCertPath;
}
bool AppConfig::enableService() const bool AppConfig::enableService() const
{ {
return m_EnableService; return m_EnableService;
@ -515,16 +503,6 @@ const QString &AppConfig::serverHostname() const
// Begin setters // 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) void AppConfig::setServerGroupChecked(bool newValue)
{ {
m_ServerGroupChecked = newValue; m_ServerGroupChecked = newValue;

View file

@ -80,7 +80,7 @@ private:
kUseInternalConfig = 23, kUseInternalConfig = 23,
kClientGroupChecked = 24, kClientGroupChecked = 24,
kServerHostname = 25, kServerHostname = 25,
kTlsCertPath = 26, // 26 = kTlsCertPath moved to deskflow settings
// 27 = tlsKeyLength Moved to deskflow settings // 27 = tlsKeyLength Moved to deskflow settings
kPreventSleep = 28, kPreventSleep = 28,
kLanguageSync = 29, kLanguageSync = 29,
@ -107,10 +107,6 @@ public:
struct Deps struct Deps
{ {
virtual ~Deps() = default; virtual ~Deps() = default;
virtual QString defaultTlsCertPath() const
{
return deskflow::gui::paths::defaultTlsCertPath();
}
virtual QString hostname() const virtual QString hostname() const
{ {
return QHostInfo::localHostName(); return QHostInfo::localHostName();
@ -134,7 +130,6 @@ public:
IConfigScopes &scopes() const override; IConfigScopes &scopes() const override;
ProcessMode processMode() const override; ProcessMode processMode() const override;
ElevateMode elevateMode() const override; ElevateMode elevateMode() const override;
QString tlsCertPath() const override;
QString logLevelText() const override; QString logLevelText() const override;
const QString &screenName() const override; const QString &screenName() const override;
bool logToFile() const override; bool logToFile() const override;
@ -181,7 +176,6 @@ public:
void setLanguageSync(bool b) override; void setLanguageSync(bool b) override;
void setPreventSleep(bool b) override; void setPreventSleep(bool b) override;
void setEnableService(bool enabled) override; void setEnableService(bool enabled) override;
void setTlsCertPath(const QString &path) override;
void setRequireClientCerts(bool requireClientCerts) override; void setRequireClientCerts(bool requireClientCerts) override;
// //
@ -248,11 +242,6 @@ private:
/// @param [in] scope which should be loaded. /// @param [in] scope which should be loaded.
void loadScope(IConfigScopes::Scope scope); 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. // Used to make the server and client names on windows.
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
inline static const auto s_winExeTemplate = QStringLiteral("%1.exe"); inline static const auto s_winExeTemplate = QStringLiteral("%1.exe");
@ -300,7 +289,6 @@ private:
deskflow::gui::IConfigScopes &m_Scopes; deskflow::gui::IConfigScopes &m_Scopes;
std::shared_ptr<Deps> m_pDeps; std::shared_ptr<Deps> m_pDeps;
QString m_ScreenName; QString m_ScreenName;
QString m_TlsCertPath;
signals: signals:
void tlsChanged(); void tlsChanged();

View file

@ -32,7 +32,6 @@ public:
// //
virtual IConfigScopes &scopes() const = 0; virtual IConfigScopes &scopes() const = 0;
virtual QString tlsCertPath() const = 0;
virtual ProcessMode processMode() const = 0; virtual ProcessMode processMode() const = 0;
virtual ElevateMode elevateMode() const = 0; virtual ElevateMode elevateMode() const = 0;
virtual QString logLevelText() const = 0; virtual QString logLevelText() const = 0;
@ -70,7 +69,6 @@ public:
virtual void setLogFilename(const QString &logFilename) = 0; virtual void setLogFilename(const QString &logFilename) = 0;
virtual void setElevateMode(ElevateMode elevateMode) = 0; virtual void setElevateMode(ElevateMode elevateMode) = 0;
virtual void setPreventSleep(bool preventSleep) = 0; virtual void setPreventSleep(bool preventSleep) = 0;
virtual void setTlsCertPath(const QString &tlsCertPath) = 0;
virtual void setLanguageSync(bool languageSync) = 0; virtual void setLanguageSync(bool languageSync) = 0;
virtual void setInvertScrollDirection(bool invertScrollDirection) = 0; virtual void setInvertScrollDirection(bool invertScrollDirection) = 0;
virtual void setEnableService(bool enableService) = 0; virtual void setEnableService(bool enableService) = 0;

View file

@ -561,7 +561,7 @@ bool CoreProcess::addServerArgs(QStringList &args, QString &app)
qCritical("failed to persist tls certificate"); qCritical("failed to persist tls certificate");
return false; return false;
} }
args << "--tls-cert" << m_appConfig.tlsCertPath(); args << "--tls-cert" << Settings::value(Settings::Security::Certificate).toString();
} }
return true; return true;

View file

@ -9,7 +9,7 @@
#include "config/ConfigScopes.h" #include "config/ConfigScopes.h"
#include "paths.h" #include "paths.h"
#include <QApplication> #include <QCoreApplication>
#include <QDir> #include <QDir>
#include <QProcess> #include <QProcess>
@ -27,7 +27,7 @@ void restart()
QProcess::startDetached(program, arguments); QProcess::startDetached(program, arguments);
qDebug("exiting current process"); qDebug("exiting current process");
QApplication::exit(); QCoreApplication::exit();
} }
void clearSettings(ConfigScopes &scopes, bool enableRestart) void clearSettings(ConfigScopes &scopes, bool enableRestart)

View file

@ -46,11 +46,4 @@ inline QDir coreProfileDir()
return QDir(coreTool.getProfileDir()); 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 } // namespace deskflow::gui::paths

View file

@ -38,15 +38,15 @@ bool TlsUtility::generateCertificate()
} }
auto length = Settings::value(Settings::Security::KeySize).toInt(); auto length = Settings::value(Settings::Security::KeySize).toInt();
const auto certificate = Settings::value(Settings::Security::Certificate).toString();
return m_certificate.generateCertificate(m_appConfig.tlsCertPath(), length); return m_certificate.generateCertificate(certificate, length);
} }
bool TlsUtility::persistCertificate() bool TlsUtility::persistCertificate()
{ {
qDebug("persisting tls certificate"); qDebug("persisting tls certificate");
if (QFile::exists(m_appConfig.tlsCertPath())) { if (QFile::exists(Settings::value(Settings::Security::Certificate).toString())) {
qDebug("tls certificate already exists"); qDebug("tls certificate already exists");
return true; return true;
} }

View file

@ -33,7 +33,6 @@ public:
// //
MOCK_METHOD(deskflow::gui::IConfigScopes &, scopes, (), (const, override)); MOCK_METHOD(deskflow::gui::IConfigScopes &, scopes, (), (const, override));
MOCK_METHOD(QString, tlsCertPath, (), (const, override));
MOCK_METHOD(ProcessMode, processMode, (), (const, override)); MOCK_METHOD(ProcessMode, processMode, (), (const, override));
MOCK_METHOD(ElevateMode, elevateMode, (), (const, override)); MOCK_METHOD(ElevateMode, elevateMode, (), (const, override));
MOCK_METHOD(QString, logLevelText, (), (const, override)); MOCK_METHOD(QString, logLevelText, (), (const, override));
@ -71,7 +70,6 @@ public:
MOCK_METHOD(void, setLogFilename, (const QString &logFilename), (override)); MOCK_METHOD(void, setLogFilename, (const QString &logFilename), (override));
MOCK_METHOD(void, setElevateMode, (ElevateMode elevateMode), (override)); MOCK_METHOD(void, setElevateMode, (ElevateMode elevateMode), (override));
MOCK_METHOD(void, setPreventSleep, (bool preventSleep), (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, setLanguageSync, (bool languageSync), (override));
MOCK_METHOD(void, setInvertScrollDirection, (bool invertScrollDirection), (override)); MOCK_METHOD(void, setInvertScrollDirection, (bool invertScrollDirection), (override));
MOCK_METHOD(void, setEnableService, (bool enableService), (override)); MOCK_METHOD(void, setEnableService, (bool enableService), (override));

View file

@ -41,7 +41,6 @@ struct DepsMock : public AppConfig::Deps
{ {
DepsMock() DepsMock()
{ {
ON_CALL(*this, defaultTlsCertPath()).WillByDefault(Return("stub"));
ON_CALL(*this, hostname()).WillByDefault(Return("stub")); ON_CALL(*this, hostname()).WillByDefault(Return("stub"));
} }
@ -50,7 +49,6 @@ struct DepsMock : public AppConfig::Deps
return std::make_shared<NiceMock<DepsMock>>(); return std::make_shared<NiceMock<DepsMock>>();
} }
MOCK_METHOD(QString, defaultTlsCertPath, (), (const, override));
MOCK_METHOD(QString, hostname, (), (const, override)); MOCK_METHOD(QString, hostname, (), (const, override));
}; };