Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(api): Update return type while fetching secrets and variables #264

Merged
merged 4 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
46 changes: 36 additions & 10 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 Down Expand Up @@ -488,22 +493,43 @@ export class SecretService {
orderBy: {
[sort]: order
}
})) as SecretWithVersion[]
})) as unknown as SecretWithVersionAndEnvironment[]
rajdip-b marked this conversation as resolved.
Show resolved Hide resolved

if (decryptValue) {
for (const secret of secrets) {
// Decrypt the secret value
for (let i = 0; i < secret.versions.length; i++) {
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
}
}
}

// Group variables by environment
const secretsByEnvironment: {
[key: string]: {
environment: { id: string; name: string }
secrets: any[]
}
} = {}
for (const secret of secrets) {
rajdip-b marked this conversation as resolved.
Show resolved Hide resolved
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
Loading