From 1b81a2e272bba232cc03a095faeafafe7fdcc714 Mon Sep 17 00:00:00 2001 From: Anantshree Chandola Date: Thu, 6 Mar 2025 14:02:18 +0530 Subject: [PATCH] updates (#12133) --- .../organization_constants.service.js | 2 +- .../organization-constants/controller.ts | 41 ++++++++++--- .../organization-constants/repository.ts | 32 +++++++--- .../modules/organization-constants/service.ts | 61 ++++++++++++------- 4 files changed, 95 insertions(+), 41 deletions(-) diff --git a/frontend/src/_services/organization_constants.service.js b/frontend/src/_services/organization_constants.service.js index 0ab57eb776..cd85a6fe15 100644 --- a/frontend/src/_services/organization_constants.service.js +++ b/frontend/src/_services/organization_constants.service.js @@ -15,7 +15,7 @@ export const orgEnvironmentConstantService = { function getAll(type = null) { const requestOptions = { method: 'GET', headers: authHeader(), credentials: 'include' }; const queryParams = type ? `?type=${type}` : ''; - return fetch(`${config.apiUrl}/organization-constants${queryParams}`, requestOptions).then(handleResponse); + return fetch(`${config.apiUrl}/organization-constants/decrypted${queryParams}`, requestOptions).then(handleResponse); } function create(name, value, type, environments) { diff --git a/server/src/modules/organization-constants/controller.ts b/server/src/modules/organization-constants/controller.ts index 9a787ffb48..39862dee29 100644 --- a/server/src/modules/organization-constants/controller.ts +++ b/server/src/modules/organization-constants/controller.ts @@ -22,30 +22,51 @@ export class OrganizationConstantController implements IOrganizationConstantCont @InitFeature(FEATURE_KEY.GET) @Get() async get(@User() user, @Query('type') type: OrganizationConstantType) { - const result = await this.organizationConstantsService.allEnvironmentConstants(user.organizationId); + const result = await this.organizationConstantsService.allEnvironmentConstants(user.organizationId, false, type); return { constants: result }; } + @UseGuards(JwtAuthGuard, FeatureAbilityGuard) + @InitFeature(FEATURE_KEY.GET_DECRYPTED_CONSTANTS) + @Get('decrypted') + async getDecryptedConstants(@User() user, @Query('type') type: OrganizationConstantType) { + const result = await this.organizationConstantsService.allEnvironmentConstants(user.organizationId, true, type); + return { constants: result }; + } + + @UseGuards(JwtAuthGuard, FeatureAbilityGuard) + @InitFeature(FEATURE_KEY.GET_SECRETS) + @Get('secrets') + async getAllSecrets(@User() user) { + const result = await this.organizationConstantsService.allEnvironmentConstants( + user.organizationId, + false, + OrganizationConstantType.SECRET + ); + return { constants: result }; + } + + //by default, this api fetches only global constants (for public apps, need to fetch app to get orgId in the public guard) @UseGuards(AppAuthGuard) - @Get('public/:app_slug') @InitFeature(FEATURE_KEY.GET_PUBLIC) - async getConstantsFromPublicApp(@App() app, @Query('environmentId') environmentId) { - const result = await this.organizationConstantsService.getConstantsForEnvironment( + @Get('public/:slug') + async getConstantsFromPublicApp(@App() app) { + const result = await this.organizationConstantsService.allEnvironmentConstants( app.organizationId, - environmentId, + false, OrganizationConstantType.GLOBAL ); return { constants: result }; } //by default, this api fetches only global constants - @UseGuards(JwtAuthGuard, FeatureAbilityGuard) - @Get(':app_slug') + @UseGuards(JwtAuthGuard) @InitFeature(FEATURE_KEY.GET_FROM_APP) - async getConstantsFromApp(@User() user, @Query('environmentId') environmentId) { - const result = await this.organizationConstantsService.getConstantsForEnvironment( + @Get(':app_slug') + async getConstantsFromApp(@User() user) { + const result = await this.organizationConstantsService.allEnvironmentConstants( user.organizationId, - environmentId, + false, OrganizationConstantType.GLOBAL ); return { constants: result }; diff --git a/server/src/modules/organization-constants/repository.ts b/server/src/modules/organization-constants/repository.ts index 87e83cefdd..ddd1ba4b4a 100644 --- a/server/src/modules/organization-constants/repository.ts +++ b/server/src/modules/organization-constants/repository.ts @@ -10,9 +10,16 @@ export class OrganizationConstantRepository extends Repository { return await dbTransactionWrap(async (manager: EntityManager) => { - const result = await this.organizationConstantRepository.findAllByOrganizationId(organizationId); + const result = await this.organizationConstantRepository.findAllByOrganizationId(organizationId, type); const appEnvironments = await this.appEnvironmentUtilService.getAll(organizationId); const constantsWithValues = await Promise.all( result.map(async (constant) => { + // Skip processing values if type is SECRET and decryptSecretValue is false + if (constant.type === OrganizationConstantType.SECRET && !decryptSecretValue) { + return { + name: constant.constantName, + }; + } const values = await Promise.all( appEnvironments.map(async (env) => { const value = constant.orgEnvironmentConstantValues.find((value) => value.environmentId === env.id); + let resolvedValue = ''; + if (value) { + if (constant.type === OrganizationConstantType.SECRET) { + resolvedValue = decryptSecretValue + ? await this.organizationConstantsUtilService.decryptSecret(organizationId, value.value) + : secretValue; + } else { + resolvedValue = await this.organizationConstantsUtilService.decryptSecret( + organizationId, + value.value + ); // Constant type values are always decrypted + } + } return { environmentName: env.name, - value: - value && value.value.length > 0 - ? await this.organizationConstantsUtilService.decryptSecret(organizationId, value.value) - : '', - id: value.environmentId, + value: resolvedValue, }; }) ); @@ -62,22 +77,26 @@ export class OrganizationConstantsService implements IOrganizationConstantsServi environmentId: string, type?: OrganizationConstantType ): Promise { - return await dbTransactionWrap(async (manager: EntityManager) => { - const result = await this.organizationConstantRepository.findByEnvironment(organizationId, environmentId); + return dbTransactionWrap(async (manager: EntityManager) => { + const result = await this.organizationConstantRepository.findByEnvironment(organizationId, environmentId, type); - const constantsWithValues = result.map(async (constant) => { - const decryptedValue = await this.organizationConstantsUtilService.decryptSecret( - organizationId, - constant.orgEnvironmentConstantValues[0].value - ); - return { - id: constant.id, - name: constant.constantName, - value: decryptedValue, - }; - }); + return await Promise.all( + result.map(async (constant) => { + const resolvedValue = !(constant.type === OrganizationConstantType.SECRET) + ? await this.organizationConstantsUtilService.decryptSecret( + organizationId, + constant.orgEnvironmentConstantValues[0].value + ) + : secretValue; - return Promise.all(constantsWithValues); + return { + id: constant.id, + name: constant.constantName, + type: constant.type, + value: resolvedValue, + }; + }) + ); }); }