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

Fix/re create missing logic #15

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
meta {
name: Fetch all by project and environment
type: http
seq: 5
}

get {
url: {{BASE_URL}}/api/secret/:project_slug/:environment_slug
body: none
auth: bearer
}

params:path {
project_slug:
environment_slug:
}

auth:bearer {
token: {{JWT}}
}

docs {
## Description

Fetches all the secrets for a particular pair of project and environment. Used by the CLI to prefetch the existing secrets.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
meta {
name: Fetch all by project and environment
type: http
seq: 5
}

get {
url: {{BASE_URL}}/api/variable/:project_slug/:environment_slug
body: none
auth: bearer
}

params:path {
project_slug: project-1-uzukc
environment_slug: alpha-l7xvp
}

auth:bearer {
token: {{JWT}}
}

docs {
## Description

Fetches all the variables for a particular pair of project and environment. Used by the CLI to prefetch the existing variables.
}
3 changes: 2 additions & 1 deletion apps/api/jest.e2e-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ export default {
forceExit: true,
displayName: 'api',
testEnvironment: 'node',
testMatch: ['**/*.e2e.spec.ts'],
testMatch: ['**/project.e2e.spec.ts'],
testTimeout: 10000,
transform: {
'^.+\\.[tj]sx?$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }]
},
Expand Down
48 changes: 26 additions & 22 deletions apps/api/src/auth/service/authorization.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { SecretWithProjectAndVersion } from '@/secret/secret.types'
import { IntegrationWithWorkspace } from '@/integration/integration.types'
import { AuthorizationParams } from './authorization.types'
import { AuthenticatedUser } from '@/user/user.types'
import { Workspace, User } from '@prisma/client'
import { Workspace } from '@prisma/client'
import { PrismaService } from '@/prisma/prisma.service'
import { CustomLoggerService } from '@/common/logger.service'
import { InternalServerErrorException, NotFoundException } from '@nestjs/common'
Expand Down Expand Up @@ -52,16 +52,24 @@ export class AuthorizationService {
public async authorizeUserAccessToProject(
params: AuthorizationParams
): Promise<ProjectWithSecrets> {
console.log(
`${Date.now()}: authorizeUserAccessToProject ${params.entity.slug} - started`
)
const project =
await this.authorityCheckerService.checkAuthorityOverProject(params)

const workspace = await this.getWorkspace(
params.user.id,
project.workspaceId
console.log(
`${Date.now()}: Project acces authorized ${project.name} WorkspaceId: ${project.workspaceId}`
)

const workspace = await this.getWorkspace(project.workspaceId)

this.checkUserHasAccessToWorkspace(params.user, workspace)

console.log(
`${Date.now()}: authorizeUserAccessToProject ${params.entity.slug} - finished`
)

return project
}

Expand All @@ -80,10 +88,7 @@ export class AuthorizationService {
const environment =
await this.authorityCheckerService.checkAuthorityOverEnvironment(params)

const workspace = await this.getWorkspace(
params.user.id,
environment.project.workspaceId
)
const workspace = await this.getWorkspace(environment.project.workspaceId)

this.checkUserHasAccessToWorkspace(params.user, workspace)

Expand All @@ -105,10 +110,7 @@ export class AuthorizationService {
const variable =
await this.authorityCheckerService.checkAuthorityOverVariable(params)

const workspace = await this.getWorkspace(
params.user.id,
variable.project.workspaceId
)
const workspace = await this.getWorkspace(variable.project.workspaceId)

this.checkUserHasAccessToWorkspace(params.user, workspace)

Expand All @@ -130,10 +132,7 @@ export class AuthorizationService {
const secret =
await this.authorityCheckerService.checkAuthorityOverSecret(params)

const workspace = await this.getWorkspace(
params.user.id,
secret.project.workspaceId
)
const workspace = await this.getWorkspace(secret.project.workspaceId)

this.checkUserHasAccessToWorkspace(params.user, workspace)

Expand Down Expand Up @@ -162,16 +161,12 @@ export class AuthorizationService {

/**
* Fetches the requested workspace specified by userId and the filter.
* @param userId The id of the user
* @param filter The filter optionally including the workspace id, slug or name
* @param workspaceId The workspace id
* @returns The requested workspace
* @throws InternalServerErrorException if there's an error when communicating with the database
* @throws NotFoundException if the workspace is not found
*/
private async getWorkspace(
userId: User['id'],
workspaceId: Workspace['id']
): Promise<Workspace> {
private async getWorkspace(workspaceId: Workspace['id']): Promise<Workspace> {
let workspace: Workspace

try {
Expand All @@ -186,6 +181,15 @@ export class AuthorizationService {
}

if (!workspace) {
console.log(`${Date.now()}: Workspace not found: ${workspaceId}`)

const workspaces = await this.prisma.workspace.findMany()

console.log(`${Date.now()}: Workspaces: ${workspaces.length}`)
for (const workspace of workspaces) {
console.log(`${workspace.name}: ${workspace.id}`)
}

throw new NotFoundException(`Workspace ${workspaceId} not found`)
}

Expand Down
Loading
Loading