Create crypto key at tenant creation fix
MODIFY
src/storage/mongodb/MongoDBStorage.ts
src/storage/mongodb/MongoDBStorage.ts
import { ChangeStream, ChangeStreamOptions, ClientSession, Collection, Db, GridFSBucket, MongoClient } from 'mongodb';
+import { CryptoSettingsType, KeySetting } from '../../types/Setting';
import BackendError from '../../exception/BackendError';
import Configuration from '../../utils/Configuration';
@@ -9,7 +10,9 @@ import LockingManager from '../../locking/LockingManager';
import Logging from '../../utils/Logging';
import MigrationConfiguration from '../../types/configuration/MigrationConfiguration';
import { ServerAction } from '../../types/Server';
+import SettingStorage from './SettingStorage';
import StorageCfg from '../../types/configuration/StorageConfiguration';
+import TenantComponents from '../../types/TenantComponents';
import Utils from '../../utils/Utils';
import cluster from 'cluster';
import mongoUriBuilder from 'mongo-uri-builder';
@@ -162,6 +165,32 @@ export default class MongoDBStorage {
});
}
+ public async createInitialSettingsForTenant(tenantID: string): Promise<void> {
+ await this.createInitialCryptoSetting(tenantID);
+ }
+
+ public async createInitialCryptoSetting(tenantID: string): Promise<void> {
+ // Check for settings in db
+ const keySettings = await SettingStorage.getCryptoSettings(tenantID);
+ // Generate Crypto Key Settings
+ if (!keySettings) {
+ const keySettingToSave = {
+ identifier: TenantComponents.CRYPTO,
+ type: CryptoSettingsType.CRYPTO,
+ crypto: {
+ key: Utils.generateKey(),
+ keyProperties: {
+ blockCypher: 'AES',
+ keySize: 256,
+ operationMode: 'GCM'
+ }
+ }
+ } as KeySetting;
+ // Save Crypto Key Settings
+ await SettingStorage.saveCryptoSettings(tenantID, keySettingToSave);
+ }
+ }
+
public async deleteTenantDatabase(tenantID: string): Promise<void> {
// Not the Default tenant
if (tenantID !== Constants.DEFAULT_TENANT) {

MODIFY
src/storage/mongodb/TenantStorage.ts
src/storage/mongodb/TenantStorage.ts
@@ -121,6 +121,8 @@ export default class TenantStorage {
const uniqueTimerID = Logging.traceStart(tenantID, MODULE_NAME, 'createTenantDB');
// Create tenant collections
await global.database.checkAndCreateTenantDatabase(tenantID);
+ // Create initial settings for tenant
+ await global.database.createInitialSettingsForTenant(tenantID);
// Debug
Logging.traceEnd(tenantID, MODULE_NAME, 'createTenantDB', uniqueTimerID, { tenantID });
}

MODIFY
src/utils/Utils.ts
src/utils/Utils.ts
@@ -1358,4 +1358,8 @@ export default class Utils {
public static buildAlgorithm(properties: CryptoKeyProperties): string {
return `${properties.blockCypher.toLowerCase()}-${properties.keySize}-${properties.operationMode.toLowerCase()}`;
}
+
+ public static generateKey(): string {
+ return crypto.randomBytes(32).toString('hex');
+ }
}

Create crypto key at tenant creation fix