From d44ae243edba6ac18fc72508e52e5f6d44cded0b Mon Sep 17 00:00:00 2001 From: Cabecinha84 Date: Tue, 14 Jan 2025 16:55:04 +0000 Subject: [PATCH] code clean up and optimizations --- ZelBack/src/services/appsService.js | 66 ++++++++++--------------- ZelBack/src/services/explorerService.js | 2 + ZelBack/src/services/serviceManager.js | 2 + 3 files changed, 30 insertions(+), 40 deletions(-) diff --git a/ZelBack/src/services/appsService.js b/ZelBack/src/services/appsService.js index 230d5aa92..ea6b5fec3 100644 --- a/ZelBack/src/services/appsService.js +++ b/ZelBack/src/services/appsService.js @@ -4874,94 +4874,80 @@ async function getUserBlockedRepositores() { * @param {boolean} registration informs if it's an app registration or not. */ async function checkAppSecrets(appName, appComponentSpecs, appOwner, registration = false) { - log.info('checkAppSecrets - starting'); - log.info(`checkAppSecrets - appOwner: ${appOwner}`); - - // Normalize PGP secrets for consistent comparison + // Normalize PGP secrets string const normalizePGP = (pgpMessage) => { if (!pgpMessage) return ''; return pgpMessage.replace(/\s+/g, '').replace(/\\n/g, '').trim(); }; const appComponentSecrets = normalizePGP(appComponentSpecs.secrets); - log.info(`checkAppSecrets - normalized appComponentSecrets: ${appComponentSecrets}`); // Database connection const db = dbHelper.databaseConnection(); const database = db.db(config.database.appsglobal.database); const projection = { projection: { _id: 0 } }; + const query = { + $and: [ + { version: 7 }, + { nodes: { $exists: true, $ne: [] } }, + ], + }; // Query global apps - const results = await dbHelper.findInDatabase(database, globalAppsInformation, {}, projection); + const results = await dbHelper.findInDatabase(database, globalAppsInformation, query, projection); let foundSecretsWithSameAppName = false; let foundSecretsWithDifferentAppName = false; // eslint-disable-next-line no-restricted-syntax for (const app of results) { - if (app.version >= 7 && app.nodes.length > 0) { - // eslint-disable-next-line no-restricted-syntax - for (const component of app.compose) { - const normalizedComponentSecret = normalizePGP(component.secrets); - - if (normalizedComponentSecret === appComponentSecrets) { - if (registration) { - throw new Error( - `Provided component '${appComponentSpecs.name}' secrets are not valid (duplicate in app: '${app.name}')`, - ); - } else if (app.name !== appName) { - foundSecretsWithDifferentAppName = true; - } else { - foundSecretsWithSameAppName = true; - } + // eslint-disable-next-line no-restricted-syntax + for (const component of app.compose.filter((comp) => comp.secrets)) { + const normalizedComponentSecret = normalizePGP(component.secrets); + if (normalizedComponentSecret === appComponentSecrets) { + if (registration) { + throw new Error( + `Provided component '${appComponentSpecs.name}' secrets are not valid (duplicate in app: '${app.name}')`, + ); + } else if (app.name !== appName) { + foundSecretsWithDifferentAppName = true; + } else { + foundSecretsWithSameAppName = true; } } } } if (!registration && foundSecretsWithDifferentAppName && !foundSecretsWithSameAppName) { - throw new Error('Provided component(s) secrets are not valid (conflict with another app).'); + throw new Error('Component(s) secrets are not valid (conflict with another app).'); } // Query permanent app messages const appsQuery = { $and: [ - { 'appSpecifications.name': 'encrypted' }, { 'appSpecifications.version': 7 }, { 'appSpecifications.nodes': { $exists: true, $ne: [] } }, ], }; - log.info('checkAppSecrets - checking permanentAppMessages'); const permanentAppMessages = await dbHelper.findInDatabase(database, globalAppsMessages, appsQuery, projection); - log.info(`checkAppSecrets - permanentAppMessages found: ${permanentAppMessages.length}`); const processedSecrets = new Set(); // eslint-disable-next-line no-restricted-syntax for (const message of permanentAppMessages) { // eslint-disable-next-line no-restricted-syntax - for (const component of message.appSpecifications.compose) { + for (const component of message.appSpecifications.compose.filter((comp) => comp.secrets)) { const normalizedComponentSecret = normalizePGP(component.secrets); // eslint-disable-next-line no-continue if (processedSecrets.has(normalizedComponentSecret)) continue; processedSecrets.add(normalizedComponentSecret); - log.info(`checkAppSecrets - component secret: ${normalizedComponentSecret}`); - - if (normalizedComponentSecret === appComponentSecrets) { - log.info('checkAppSecrets - found same secret'); - log.info(`checkAppSecrets - appOwner: ${appOwner}`); - log.info(`checkAppSecrets - message owner: ${message.appSpecifications.owner}`); - - if (message.appSpecifications.owner !== appOwner) { - throw new Error( - `Provided component '${appComponentSpecs.name}' secrets are not valid (owner mismatch: '${message.appSpecifications.owner}').`, - ); - } + if (normalizedComponentSecret === appComponentSecrets && message.appSpecifications.owner !== appOwner) { + throw new Error( + `Component '${appComponentSpecs.name}' secrets are not valid - registered already with different app owner').`, + ); } } } - - log.info('checkAppSecrets - completed successfully'); } /** diff --git a/ZelBack/src/services/explorerService.js b/ZelBack/src/services/explorerService.js index 606cf5c72..68eea24f9 100644 --- a/ZelBack/src/services/explorerService.js +++ b/ZelBack/src/services/explorerService.js @@ -800,6 +800,8 @@ async function initiateBlockProcessor(restoreDatabase, deepRestore, reindexOrRes await databaseGlobal.collection(config.database.appsglobal.collections.appsInformation).createIndex({ repotag: 1 }, { name: 'query for getting zelapp based on image' }); await databaseGlobal.collection(config.database.appsglobal.collections.appsInformation).createIndex({ height: 1 }, { name: 'query for getting zelapp based on last height update' }); // we need to know the height of app adjustment await databaseGlobal.collection(config.database.appsglobal.collections.appsInformation).createIndex({ hash: 1 }, { name: 'query for getting zelapp based on last hash' }); // todo evaluate unique: true // we need to know the hash of the last message update which is the true identifier + await databaseGlobal.collection(config.database.appsglobal.collections.appsInformation).createIndex({ version: 1 }, { name: 'query for getting app based on version' }); + await databaseGlobal.collection(config.database.appsglobal.collections.appsInformation).createIndex({ nodes: 1 }, { name: 'query for getting app based on nodes' }); await database.collection(config.database.appsglobal.collections.appsLocations).createIndex({ name: 1 }, { name: 'query for getting zelapp location based on zelapp specs name' }); await database.collection(config.database.appsglobal.collections.appsLocations).createIndex({ hash: 1 }, { name: 'query for getting zelapp location based on zelapp hash' }); await database.collection(config.database.appsglobal.collections.appsLocations).createIndex({ ip: 1 }, { name: 'query for getting zelapp location based on ip' }); diff --git a/ZelBack/src/services/serviceManager.js b/ZelBack/src/services/serviceManager.js index a40c45f68..125e0eb58 100644 --- a/ZelBack/src/services/serviceManager.js +++ b/ZelBack/src/services/serviceManager.js @@ -82,6 +82,8 @@ async function startFluxFunctions() { await databaseTemp.collection(config.database.appsglobal.collections.appsMessages).dropIndex({ hash: 1 }, { name: 'query for getting zelapp message based on hash' }).catch(() => { console.log('Welcome to FluxOS'); }); // drop old index or display message for new installations await databaseTemp.collection(config.database.appsglobal.collections.appsMessages).createIndex({ 'appSpecifications.version': 1 }, { name: 'query for getting app message based on version' }); await databaseTemp.collection(config.database.appsglobal.collections.appsMessages).createIndex({ 'appSpecifications.nodes': 1 }, { name: 'query for getting app message based on nodes' }); + await databaseTemp.collection(config.database.appsglobal.collections.appsInformation).createIndex({ version: 1 }, { name: 'query for getting app based on version' }); + await databaseTemp.collection(config.database.appsglobal.collections.appsInformation).createIndex({ nodes: 1 }, { name: 'query for getting app based on nodes' }); // more than 2 hours and 5m. Meaning we have not received status message for a long time. So that node is no longer on a network or app is down. await databaseTemp.collection(config.database.appsglobal.collections.appsLocations).createIndex({ broadcastedAt: 1 }, { expireAfterSeconds: 7500 }); await databaseTemp.collection(config.database.appsglobal.collections.appsLocations).createIndex({ name: 1 }, { name: 'query for getting zelapp location based on zelapp specs name' });