Skip to content

Commit

Permalink
refactor(api): Update return type while fetching secrets and variables (
Browse files Browse the repository at this point in the history
  • Loading branch information
Ritika1705 authored Jun 9, 2024
1 parent 020bbf6 commit fd36abd
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 17 deletions.
7 changes: 3 additions & 4 deletions apps/api/src/secret/secret.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -840,10 +840,9 @@ describe('Secret Controller Tests', () => {

expect(response.statusCode).toBe(200)
expect(response.json().length).toBe(1)

const secret = response.json()[0]

expect(secret.versions[0].value).toEqual('Secret 1 value')
const envSecret = response.json()[0]
expect(envSecret.environment).toHaveProperty('id')
expect(envSecret.environment).toHaveProperty('name')
})

it('should not be able to fetch all secrets decrypted if the project does not store the private key', async () => {
Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/secret/secret.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ export interface SecretWithEnvironment extends Secret {
}

export type SecretWithProjectAndVersion = SecretWithProject & SecretWithVersion
export type SecretWithVersionAndEnvironment = SecretWithVersion &
SecretWithEnvironment
47 changes: 36 additions & 11 deletions apps/api/src/secret/service/secret.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { encrypt } from '../../common/encrypt'
import {
SecretWithProject,
SecretWithProjectAndVersion,
SecretWithVersion
SecretWithVersionAndEnvironment
} from '../secret.types'
import createEvent from '../../common/create-event'
import getDefaultEnvironmentOfProject from '../../common/get-default-project-environment'
Expand Down Expand Up @@ -430,7 +430,12 @@ export class SecretService {
sort: string,
order: string,
search: string
) {
): Promise<
{
environment: { id: string; name: string }
secrets: any[]
}[]
> {
// Fetch the project
const project =
await this.authorityCheckerService.checkAuthorityOverProject({
Expand All @@ -455,7 +460,7 @@ export class SecretService {
)
}

const secrets = (await this.prisma.secret.findMany({
const secrets = await this.prisma.secret.findMany({
where: {
projectId,
pendingCreation: false,
Expand Down Expand Up @@ -488,22 +493,42 @@ export class SecretService {
orderBy: {
[sort]: order
}
})) as SecretWithVersion[]
})

if (decryptValue) {
for (const secret of secrets) {
// Decrypt the secret value
for (let i = 0; i < secret.versions.length; i++) {
// Group variables by environment
const secretsByEnvironment: {
[key: string]: {
environment: { id: string; name: string }
secrets: any[]
}
} = {}

for (const secret of secrets) {
// Decrypt the secret value
for (let i = 0; i < secret.versions.length; i++) {
const version = secret.versions[i]
// Optionally decrypt secret value if decryptValue is true
if (decryptValue) {
const decryptedValue = await decrypt(
project.privateKey,
secret.versions[i].value
version.value
)
secret.versions[i].value = decryptedValue
version.value = decryptedValue
}
}

const { id, name } = secret.environment
if (!secretsByEnvironment[id]) {
secretsByEnvironment[id] = {
environment: { id, name },
secrets: []
}
}
secretsByEnvironment[id].secrets.push(secret)
}

return secrets
// Convert the object to an array and return
return Object.values(secretsByEnvironment)
}

private async secretExists(
Expand Down
30 changes: 28 additions & 2 deletions apps/api/src/variable/service/variable.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,12 @@ export class VariableService {
sort: string,
order: string,
search: string
) {
): Promise<
{
environment: { id: string; name: string }
variables: any[]
}[]
> {
// Check if the user has the required authorities in the project
await this.authorityCheckerService.checkAuthorityOverProject({
userId: user.id,
Expand All @@ -422,7 +427,7 @@ export class VariableService {
prisma: this.prisma
})

return await this.prisma.variable.findMany({
const variables = await this.prisma.variable.findMany({
where: {
projectId,
pendingCreation: false,
Expand Down Expand Up @@ -456,6 +461,27 @@ export class VariableService {
[sort]: order
}
})

// Group variables by environment
const variablesByEnvironment: {
[key: string]: {
environment: { id: string; name: string }
variables: any[]
}
} = {}
for (const variable of variables) {
const { id, name } = variable.environment
if (!variablesByEnvironment[id]) {
variablesByEnvironment[id] = {
environment: { id, name },
variables: []
}
}
variablesByEnvironment[id].variables.push(variable)
}

// Convert the object to an array and return
return Object.values(variablesByEnvironment)
}

private async variableExists(
Expand Down
3 changes: 3 additions & 0 deletions apps/api/src/variable/variable.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,9 @@ describe('Variable Controller Tests', () => {

expect(response.statusCode).toBe(200)
expect(response.json().length).toBe(1)
const envVariable = response.json()[0]
expect(envVariable.environment).toHaveProperty('id')
expect(envVariable.environment).toHaveProperty('name')
})

it('should not be able to fetch all variables if the user has no access to the project', async () => {
Expand Down

0 comments on commit fd36abd

Please sign in to comment.