Skip to content

Commit

Permalink
code clean up and optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Cabecinha84 committed Jan 14, 2025
1 parent 17da9fa commit d44ae24
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 40 deletions.
66 changes: 26 additions & 40 deletions ZelBack/src/services/appsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down
2 changes: 2 additions & 0 deletions ZelBack/src/services/explorerService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down
2 changes: 2 additions & 0 deletions ZelBack/src/services/serviceManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down

0 comments on commit d44ae24

Please sign in to comment.