Skip to content

Commit

Permalink
Merge branch 'main' into fix/sentry-no-user
Browse files Browse the repository at this point in the history
  • Loading branch information
timonmasberg authored Feb 1, 2024
2 parents 40cdd38 + beb0d5b commit 78a6520
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 16 deletions.
4 changes: 2 additions & 2 deletions apps/api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ RUN npm --omit=dev ci
COPY ./dist/apps/api ./

# Use distroless for maximum security: https://github.com/GoogleContainerTools/distroless
FROM gcr.io/distroless/nodejs${NODE_VERSION}-debian11
FROM gcr.io/distroless/nodejs${NODE_VERSION}-debian12:nonroot

COPY --from=builder /app /app
COPY --chown=root:root --chmod=655 --from=builder /app /app
WORKDIR /app

ENV PORT=3333
Expand Down
3 changes: 2 additions & 1 deletion apps/api/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { SharedKernel, errorFormatterFactory } from '@kordis/api/shared';
import { AppResolver } from './app.resolver';
import { AppService } from './app.service';
import { GraphqlSubscriptionsController } from './controllers/graphql-subscriptions.controller';
import { HealthCheckController } from './controllers/health-check.controller';
import environment from './environment';

const FEATURE_MODULES = [OrganizationModule];
Expand Down Expand Up @@ -69,6 +70,6 @@ const UTILITY_MODULES = [
...FEATURE_MODULES,
],
providers: [AppService, AppResolver],
controllers: [GraphqlSubscriptionsController],
controllers: [GraphqlSubscriptionsController, HealthCheckController],
})
export class AppModule {}
19 changes: 19 additions & 0 deletions apps/api/src/app/controllers/health-check.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Test, TestingModule } from '@nestjs/testing';

import { HealthCheckController } from './health-check.controller';

describe('HealthCheckController', () => {
let controller: HealthCheckController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [HealthCheckController],
}).compile();

controller = module.get<HealthCheckController>(HealthCheckController);
});

it('should return a resolved promise for the healthCheck method', async () => {
await expect(controller.healthCheck()).resolves.toBeUndefined();
});
});
10 changes: 10 additions & 0 deletions apps/api/src/app/controllers/health-check.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { All, Controller, HttpCode } from '@nestjs/common';

@Controller('health-check')
export class HealthCheckController {
@All()
@HttpCode(200)
healthCheck(): Promise<void> {
return Promise.resolve();
}
}
28 changes: 26 additions & 2 deletions libs/api/auth/src/lib/interceptors/auth.interceptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { CallHandler, UnauthorizedException } from '@nestjs/common';
import { Observable, firstValueFrom, of } from 'rxjs';

import { KordisRequest } from '@kordis/api/shared';
import { createGqlContextForRequest } from '@kordis/api/test-helpers';
import {
createGqlContextForRequest,
createHttpContextForRequest,
} from '@kordis/api/test-helpers';
import { AuthUser } from '@kordis/shared/auth';

import { AuthUserExtractorStrategy } from '../auth-user-extractor-strategies/auth-user-extractor.strategy';
Expand Down Expand Up @@ -38,7 +41,7 @@ describe('AuthInterceptor', () => {
).rejects.toThrow(UnauthorizedException);
});

it('should continue request pipeline', async () => {
it('should continue request pipeline for authenticated request', async () => {
jest.spyOn(mockAuthUserExtractor, 'getUserFromRequest').mockReturnValue({
id: '123',
firstName: 'foo',
Expand All @@ -65,4 +68,25 @@ describe('AuthInterceptor', () => {
firstValueFrom(service.intercept(httpCtx, handler)),
).resolves.toBeTruthy();
});

it('should continue request pipeline for health-check request', async () => {
const handler = createMock<CallHandler>({
handle(): Observable<boolean> {
return of(true);
},
});

await expect(
firstValueFrom(
service.intercept(
createHttpContextForRequest(
createMock<KordisRequest>({
path: '/health-check',
}),
),
handler,
),
),
).resolves.toBeTruthy();
});
});
4 changes: 4 additions & 0 deletions libs/api/auth/src/lib/interceptors/auth.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export class AuthInterceptor implements NestInterceptor {
req = ctx.getContext<KordisGqlContext>().req;
} else {
req = context.switchToHttp().getRequest<KordisRequest>();

if (req.path === '/health-check') {
return next.handle();
}
}

const possibleAuthUser = this.authUserExtractor.getUserFromRequest(req);
Expand Down
36 changes: 27 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@trivago/prettier-plugin-sort-imports": "^4.1.1",
"@types/dompurify": "^3.0.5",
"@types/jest": "29.4.4",
"@types/node": "20.11.13",
"@types/node": "20.11.14",
"@types/trusted-types": "^2.0.7",
"@typescript-eslint/eslint-plugin": "6.14.0",
"@typescript-eslint/parser": "6.14.0",
Expand Down Expand Up @@ -97,7 +97,7 @@
"@nestjs/mongoose": "^10.0.2",
"@nestjs/platform-express": "10.2.4",
"@opentelemetry/instrumentation-graphql": "^0.37.0",
"@opentelemetry/instrumentation-mongoose": "^0.34.0",
"@opentelemetry/instrumentation-mongoose": "^0.35.0",
"@opentelemetry/instrumentation-pino": "^0.34.0",
"@opentelemetry/sdk-node": "^0.48.0",
"@opentelemetry/sdk-trace-base": "^1.14.0",
Expand Down

0 comments on commit 78a6520

Please sign in to comment.