Skip to content

Commit db8a834

Browse files
committed
inject salt configuration into config store
1 parent 679b3e1 commit db8a834

2 files changed

Lines changed: 43 additions & 12 deletions

File tree

src/config/configStore.ts

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,17 @@ export class ConfigStore {
5959
loadError: string | undefined;
6060
usingUI: boolean;
6161

62+
algorithmId: string;
6263
appConfig: AppConfigData = DEFAULT_APP_CONFIG;
6364
filePaths: ConfigStorePaths;
64-
algorithmId: string;
65+
saltConfiguration?: Config.FileBasedSalt | Config.StringBasedSalt;
6566

66-
constructor(filePaths: ConfigStorePaths, algorithmId: string, usingUI: boolean = false) {
67-
this.lastUpdated = new Date();
67+
constructor(filePaths: ConfigStorePaths, algorithmId: string, usingUI: boolean = false, saltConfiguration?: Config.FileBasedSalt | Config.StringBasedSalt) {
68+
this.usingUI = usingUI;
6869
this.filePaths = filePaths;
70+
this.lastUpdated = new Date();
6971
this.algorithmId = algorithmId;
70-
this.usingUI = usingUI;
72+
this.saltConfiguration = saltConfiguration;
7173
}
7274

7375
getConfig = () => this.data;
@@ -84,7 +86,12 @@ export class ConfigStore {
8486
this.appConfig = loadAppConfig(this.getAppConfigFilePath());
8587

8688
// attempt to load the default app config
87-
const userConfigLoad = loadConfig({ configPath: this.getConfigFilePath(), algorithmId: this.getAlgorithmId() });
89+
const userConfigLoad = loadConfig({
90+
configPath: this.getConfigFilePath(),
91+
algorithmId: this.getAlgorithmId(),
92+
embeddedSalt: this.saltConfiguration,
93+
usingUI: true
94+
});
8895

8996
// if the load succesds we have a valid config -- use it as a
9097
// user-provided one
@@ -97,7 +104,12 @@ export class ConfigStore {
97104
log('User config validation not successful - attempting to load backup config');
98105
// if the default config load failed use the backup default
99106
// from the app distribution
100-
const backupConfigLoad = loadConfig({ configPath: this.getBackupConfigFilePath(), algorithmId: this.getAlgorithmId()});
107+
const backupConfigLoad = loadConfig({
108+
configPath: this.getBackupConfigFilePath(),
109+
algorithmId: this.getAlgorithmId(),
110+
embeddedSalt: this.saltConfiguration,
111+
usingUI: true
112+
});
101113

102114
// if the load succesds we have a valid config -- use it as
103115
// a config-from-backup
@@ -136,7 +148,12 @@ export class ConfigStore {
136148
// The config data used by the application is updated after the save
137149
updateUserConfig(userConfigFilePath: string) {
138150
// attempt to load & validate the config data
139-
const userConfigLoad = loadConfig({ configPath: userConfigFilePath, algorithmId: this.getAlgorithmId()});
151+
const userConfigLoad = loadConfig({
152+
configPath: userConfigFilePath,
153+
algorithmId: this.getAlgorithmId(),
154+
embeddedSalt: this.saltConfiguration,
155+
usingUI: true
156+
});
140157

141158
// if failed return the error message
142159
if (!userConfigLoad.success) {
@@ -168,7 +185,12 @@ export class ConfigStore {
168185
}
169186

170187
log('[removeUserConfig] Trying to load backup config file');
171-
const backupConfigLoad = loadConfig({ configPath: this.getBackupConfigFilePath(), algorithmId: this.getAlgorithmId()});
188+
const backupConfigLoad = loadConfig({
189+
configPath: this.getBackupConfigFilePath(),
190+
algorithmId: this.getAlgorithmId(),
191+
embeddedSalt: this.saltConfiguration,
192+
usingUI: true
193+
});
172194

173195
// if failed return the error message (do not delete the user config yet)
174196
if (!backupConfigLoad.success) {
@@ -273,7 +295,14 @@ export class ConfigStore {
273295
}
274296
}
275297

276-
export function makeConfigStore({filePaths, algorithmId, usingUI}: {filePaths: ConfigStorePaths, algorithmId: string, usingUI: boolean}) {
298+
interface ConfigStoreInput {
299+
usingUI: boolean;
300+
algorithmId: string;
301+
filePaths: ConfigStorePaths;
302+
saltConfiguration?: Config.FileBasedSalt | Config.StringBasedSalt;
303+
}
304+
305+
export function makeConfigStore({ filePaths, algorithmId, usingUI, saltConfiguration }: ConfigStoreInput) {
277306
if (!filePaths || !algorithmId) throw new Error(`ConfigStore params MUST be provided.`);
278-
return new ConfigStore(filePaths, algorithmId, usingUI);
307+
return new ConfigStore(filePaths, algorithmId, usingUI, saltConfiguration);
279308
}

src/config/loadConfig.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export function loadConfig({ configPath, algorithmId, embeddedSalt, usingUI=fals
116116
return tryLoadSaltFile({ saltFilePath, validatorRegexp, configData, lastUpdateDate, label: "embedded salt" });
117117
}
118118

119-
return { success: false, error: `No salt configuration provided: either specify salt in config file, or pass in path on config load.`, isSaltFileError: true, config: configData };
119+
return { success: false, error: `No salt configuration provided in conguration file or embedded.`, isSaltFileError: true, config: configData };
120120
}
121121

122122
interface TryLoadSaltFileInput {
@@ -127,7 +127,7 @@ interface TryLoadSaltFileInput {
127127
label: string;
128128
}
129129

130-
function tryLoadSaltFile({ saltFilePath, validatorRegexp, configData, lastUpdateDate, label="salt"}: TryLoadSaltFileInput): LoadConfigResult {
130+
function tryLoadSaltFile({ saltFilePath, validatorRegexp, configData, lastUpdateDate }: TryLoadSaltFileInput): LoadConfigResult {
131131
log('[INFO] Loading salt from', saltFilePath);
132132

133133
const loadSaltResponse = loadSaltFile({ saltFilePath, validatorRegexp });
@@ -141,5 +141,7 @@ function tryLoadSaltFile({ saltFilePath, validatorRegexp, configData, lastUpdate
141141

142142
// update the config to be of salt type: "STRING" with loaded file data
143143
configData.algorithm.salt = { source: "STRING", value: loadSaltResponse.data }
144+
// update the signature since we have changed the salt configuration
145+
configData.meta.signature = generateConfigHash(configData);
144146
return { success: true, lastUpdated: lastUpdateDate, config: configData };
145147
}

0 commit comments

Comments
 (0)