From 27a895332617b750f1dc7274fb2e5a50be632d88 Mon Sep 17 00:00:00 2001 From: Kiran Niranjan Date: Thu, 22 Feb 2024 13:59:19 +0530 Subject: [PATCH] SDA-4479 - Backup and restore global config (#2099) --- spec/plistHandler.spec.ts | 3 ++- src/app/config-handler.ts | 56 ++++++++++++++++++++++++++++----------- src/app/plist-handler.ts | 41 +++++++++++++++++++++++----- 3 files changed, 77 insertions(+), 23 deletions(-) diff --git a/spec/plistHandler.spec.ts b/spec/plistHandler.spec.ts index dbbf71ba..ead59140 100644 --- a/spec/plistHandler.spec.ts +++ b/spec/plistHandler.spec.ts @@ -11,7 +11,7 @@ describe('Plist Handler', () => { betaAutoUpdateChannelEnabled: undefined, bringToFront: undefined, browserLoginAutoConnect: undefined, - chromeFlags: { + customFlags: { authNegotiateDelegateWhitelist: undefined, authServerWhitelist: undefined, disableThrottling: undefined, @@ -49,6 +49,7 @@ describe('Plist Handler', () => { url: undefined, userDataPath: undefined, whitelistUrl: undefined, + chromeFlags: undefined, }); }); }); diff --git a/src/app/config-handler.ts b/src/app/config-handler.ts index 5989ba62..1be3d17b 100644 --- a/src/app/config-handler.ts +++ b/src/app/config-handler.ts @@ -16,7 +16,11 @@ import { SDAUserSessionActionTypes, } from './bi/interface'; import { terminateC9Shell } from './c9-shell-handler'; -import { getAllUserDefaults, initializePlistFile } from './plist-handler'; +import { + getAllUserDefaults, + initializePlistFile, + setPlistFromPreviousSettings, +} from './plist-handler'; import { appStats } from './stats'; const writeFile = util.promisify(fs.writeFile); @@ -706,16 +710,24 @@ class Config { * Creates a backup of the global config file */ public backupGlobalConfig() { - fs.copyFileSync(this.globalConfigPath, this.tempGlobalConfigFilePath); + const installVariant = systemPreferences.getUserDefault( + 'installVariant', + 'string', + ); + // If we already have a valid install variant in the plist + // we have already migrated the data form global config to plist + if (installVariant === '') { + fs.copyFileSync(this.globalConfigPath, this.tempGlobalConfigFilePath); + } } /** * Overwrites the global config file with the backed up config file */ - public copyGlobalConfig() { + public copyGlobalConfig(settings: IConfig) { try { - if (fs.existsSync(this.tempGlobalConfigFilePath)) { - fs.copyFileSync(this.tempGlobalConfigFilePath, this.globalConfigPath); + if (settings) { + setPlistFromPreviousSettings(settings); fs.unlinkSync(this.tempGlobalConfigFilePath); } } catch (e) { @@ -792,6 +804,29 @@ class Config { */ private readGlobalConfig() { if (isMac) { + if (fs.existsSync(this.tempGlobalConfigFilePath)) { + this.globalConfig = this.parseConfigData( + fs.readFileSync(this.tempGlobalConfigFilePath, 'utf8'), + ); + logger.info( + `config-handler: temp global config exists using this file: `, + this.tempGlobalConfigFilePath, + this.globalConfig, + ); + this.copyGlobalConfig(this.globalConfig as IConfig); + return; + } + if (!this.installVariant || this.installVariant === '') { + logger.info( + `config-handler: Initializing new plist file: `, + this.installVariant, + ); + initializePlistFile(this.postInstallScriptPath); + } + this.installVariant = systemPreferences.getUserDefault( + 'installVariant', + 'string', + ); this.globalConfig = getAllUserDefaults(); logger.info( `config-handler: Global configuration from plist: `, @@ -822,17 +857,6 @@ class Config { 'installVariant', 'string', ); - if (!this.installVariant || this.installVariant === '') { - logger.info( - `config-handler: Initializing new plist file: `, - this.installVariant, - ); - initializePlistFile(this.postInstallScriptPath); - this.installVariant = systemPreferences.getUserDefault( - 'installVariant', - 'string', - ); - } logger.info( `config-handler: Install variant from plist: `, this.installVariant, diff --git a/src/app/plist-handler.ts b/src/app/plist-handler.ts index f4eed5d2..24a1ba92 100644 --- a/src/app/plist-handler.ts +++ b/src/app/plist-handler.ts @@ -1,6 +1,7 @@ import { execSync } from 'child_process'; import { systemPreferences } from 'electron'; import { logger } from '../common/logger'; +import { getGuid } from '../common/utils'; import { IConfig } from './config-handler'; const GENERAL_SETTINGS = { @@ -40,7 +41,7 @@ const NOTIFICATION_SETTINGS = { display: 'string', }; -const CHROME_FLAGS = { +const CUSTOM_FLAGS = { authServerWhitelist: 'string', authNegotiateDelegateWhitelist: 'string', disableThrottling: 'string', @@ -74,13 +75,13 @@ export const getAllUserDefaults = (): IConfig => { NOTIFICATION_SETTINGS[key], ); }); - Object.keys(CHROME_FLAGS).map((key) => { - if (!settings.chromeFlags) { - settings.chromeFlags = {}; + Object.keys(CUSTOM_FLAGS).map((key) => { + if (!settings.customFlags) { + settings.customFlags = {}; } - settings.chromeFlags[key] = systemPreferences.getUserDefault( + settings.customFlags[key] = systemPreferences.getUserDefault( key, - CHROME_FLAGS[key], + CUSTOM_FLAGS[key], ); }); Object.keys(PERMISSIONS).map((key) => { @@ -96,6 +97,34 @@ export const getAllUserDefaults = (): IConfig => { return settings; }; +export const setPlistFromPreviousSettings = (settings: IConfig) => { + Object.keys(GENERAL_SETTINGS).map((key) => { + systemPreferences.setUserDefault(key, GENERAL_SETTINGS[key], settings[key]); + }); + Object.keys(NOTIFICATION_SETTINGS).map((key) => { + systemPreferences.setUserDefault( + key, + NOTIFICATION_SETTINGS[key], + settings.notificationSettings[key], + ); + }); + Object.keys(CUSTOM_FLAGS).map((key) => { + systemPreferences.setUserDefault( + key, + CUSTOM_FLAGS[key], + settings.customFlags[key], + ); + }); + Object.keys(PERMISSIONS).map((key) => { + systemPreferences.setUserDefault( + key, + PERMISSIONS[key], + settings.permissions[key], + ); + }); + systemPreferences.setUserDefault('installVariant', 'string', getGuid()); +}; + /** * Initialize plist file */