Skip to content

Commit

Permalink
implemented suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdip-b committed Apr 10, 2024
1 parent 98d88b1 commit f131958
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 44 deletions.
1 change: 1 addition & 0 deletions .github/workflows/validate-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ jobs:
GITHUB_CLIENT_ID: dummy
GITHUB_CLIENT_SECRET: dummy
GITHUB_CALLBACK_URL: dummy
REDIS_URL: redis://localhost:6379
run: pnpm run e2e:api

- name: Upload e2e test coverage reports to Codecov
Expand Down
10 changes: 7 additions & 3 deletions apps/api/src/auth/guard/auth/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class AuthGuard implements CanActivate {
}

private getAuthType(request: any): 'JWT' | 'API_KEY' | 'NONE' {
const headers = request.headers || request.handshake.headers // For websockets
const headers = this.getHeaders(request)
if (headers[X_KEYSHADE_TOKEN]) {
return 'API_KEY'
}
Expand All @@ -144,16 +144,20 @@ export class AuthGuard implements CanActivate {
}

private extractTokenFromHeader(request: any): string | undefined {
const headers = request.headers || request.handshake.headers // For websockets
const headers = this.getHeaders(request)
const [type, token] = headers.authorization?.split(' ') ?? []
return type === 'Bearer' ? token : undefined
}

private extractApiKeyFromHeader(request: any): string | undefined {
const headers = request.headers || request.handshake.headers // For websockets
const headers = this.getHeaders(request)
if (Array.isArray(headers[X_KEYSHADE_TOKEN])) {
throw new Error('Bad auth')
}
return headers[X_KEYSHADE_TOKEN]
}

private getHeaders(request: any): any {
return request.headers || request.handshake.headers // For websockets
}
}
9 changes: 8 additions & 1 deletion apps/api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,14 @@ async function initializeNestApp() {
const logger = new CustomLogger()
const app = await NestFactory.create(AppModule, {
logger,
cors: false
cors: {
origin: process.env.CORS_ORIGIN || '*',
credentials: true,
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
allowedHeaders: 'Content-Type, Accept, Authorization, x-keyshade-token',
preflightContinue: false,
optionsSuccessStatus: 204
}
})
app.use(Sentry.Handlers.requestHandler())
app.use(Sentry.Handlers.tracingHandler())
Expand Down
48 changes: 30 additions & 18 deletions apps/api/src/secret/service/secret.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,15 +595,21 @@ export class SecretService {
}
})

await this.redis.publish(
CHANGE_NOTIFIER_RSC,
JSON.stringify({
environmentId: secret.environmentId,
name: secret.name,
value: dto.value,
isSecret: true
})
)
try {
await this.redis.publish(
CHANGE_NOTIFIER_RSC,
JSON.stringify({
environmentId: secret.environmentId,
name: secret.name,
value: dto.value,
isSecret: true
})
)
} catch (error) {
this.logger.error(
this.logger.error(`Error publishing secret update to Redis: ${error}`)
)
}
} else {
result = await this.prisma.secret.update({
where: {
Expand Down Expand Up @@ -698,15 +704,21 @@ export class SecretService {
}
})

await this.redis.publish(
CHANGE_NOTIFIER_RSC,
JSON.stringify({
environmentId: secret.environmentId,
name: secret.name,
value: secret.versions[rollbackVersion - 1].value,
isSecret: true
})
)
try {
await this.redis.publish(
CHANGE_NOTIFIER_RSC,
JSON.stringify({
environmentId: secret.environmentId,
name: secret.name,
value: secret.versions[rollbackVersion - 1].value,
isSecret: true
})
)
} catch (error) {
this.logger.error(
this.logger.error(`Error publishing secret update to Redis: ${error}`)
)
}

await createEvent(
{
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/socket/redis.adapter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IoAdapter } from '@nestjs/platform-socket.io'
import { ServerOptions } from 'socket.io'
import { Server, ServerOptions } from 'socket.io'
import { createAdapter } from '@socket.io/redis-adapter'
import { createClient } from 'redis'

Expand All @@ -18,7 +18,7 @@ export class RedisIoAdapter extends IoAdapter {
this.adapterConstructor = createAdapter(pubClient, subClient)
}

createIOServer(port: number, options?: ServerOptions): any {
createIOServer(port: number, options?: ServerOptions): Server {
const server = super.createIOServer(port, options)
server.adapter(this.adapterConstructor)
return server
Expand Down
44 changes: 26 additions & 18 deletions apps/api/src/variable/service/variable.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,15 +542,19 @@ export class VariableService {
}
})

await this.redis.publish(
CHANGE_NOTIFIER_RSC,
JSON.stringify({
environmentId: variable.environmentId,
name: variable.name,
value: dto.value,
isSecret: false
})
)
try {
await this.redis.publish(
CHANGE_NOTIFIER_RSC,
JSON.stringify({
environmentId: variable.environmentId,
name: variable.name,
value: dto.value,
isSecret: false
})
)
} catch (error) {
this.logger.error(`Error publishing variable update to Redis: ${error}`)
}
} else {
result = await this.prisma.variable.update({
where: {
Expand Down Expand Up @@ -646,15 +650,19 @@ export class VariableService {
}
})

await this.redis.publish(
CHANGE_NOTIFIER_RSC,
JSON.stringify({
environmentId: variable.environmentId,
name: variable.name,
value: variable.versions[rollbackVersion - 1].value,
isSecret: false
})
)
try {
await this.redis.publish(
CHANGE_NOTIFIER_RSC,
JSON.stringify({
environmentId: variable.environmentId,
name: variable.name,
value: variable.versions[rollbackVersion - 1].value,
isSecret: false
})
)
} catch (error) {
this.logger.error(`Error publishing variable update to Redis: ${error}`)
}

await createEvent(
{
Expand Down
4 changes: 2 additions & 2 deletions apps/api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"strictNullChecks": false,
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true
}
}

0 comments on commit f131958

Please sign in to comment.