itlab-internal-services 2.1.2 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,7 +5,7 @@ const common_1 = require("@nestjs/common");
5
5
  function useHttpLogger(app) {
6
6
  app.use((req, res, next) => {
7
7
  const { method, url } = req;
8
- const ignore = ['/alive', '/favicon.ico'];
8
+ const ignore = ['/alive', '/alive-db', '/favicon.ico'];
9
9
  if (url.includes('swagger') || url.startsWith('/api') || ignore.includes(url)) {
10
10
  next();
11
11
  return;
@@ -1,4 +1,4 @@
1
- import { CallHandler, ExecutionContext, NestInterceptor } from '@nestjs/common';
1
+ import { CallHandler, ExecutionContext, INestApplication, NestInterceptor } from '@nestjs/common';
2
2
  import { Observable } from 'rxjs';
3
3
  export declare class ErrorsInterceptor implements NestInterceptor {
4
4
  private readonly dictionary;
@@ -7,4 +7,5 @@ export declare class ErrorsInterceptor implements NestInterceptor {
7
7
  intercept(_context: ExecutionContext, next: CallHandler): Observable<any>;
8
8
  protected transform(message: string, parent: string[]): string;
9
9
  }
10
- export declare const UseErrorsInterceptor: (dictionary?: Record<string, string>) => <TFunction extends Function, Y>(target: object | TFunction, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void;
10
+ export declare const UsesErrorsInterceptor: (dictionary?: Record<string, string>) => <TFunction extends Function, Y>(target: object | TFunction, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void;
11
+ export declare const useGlobalErrrorsInterceptor: (app: INestApplication, dictionary?: Record<string, string>) => void;
@@ -9,7 +9,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.UseErrorsInterceptor = exports.ErrorsInterceptor = void 0;
12
+ exports.useGlobalErrrorsInterceptor = exports.UsesErrorsInterceptor = exports.ErrorsInterceptor = void 0;
13
13
  const common_1 = require("@nestjs/common");
14
14
  const rxjs_1 = require("rxjs");
15
15
  let ErrorsInterceptor = class ErrorsInterceptor {
@@ -56,5 +56,9 @@ ErrorsInterceptor = __decorate([
56
56
  __metadata("design:paramtypes", [Object])
57
57
  ], ErrorsInterceptor);
58
58
  exports.ErrorsInterceptor = ErrorsInterceptor;
59
- const UseErrorsInterceptor = (dictionary) => (0, common_1.applyDecorators)((0, common_1.UseInterceptors)(new ErrorsInterceptor(dictionary)));
60
- exports.UseErrorsInterceptor = UseErrorsInterceptor;
59
+ const UsesErrorsInterceptor = (dictionary) => (0, common_1.applyDecorators)((0, common_1.UseInterceptors)(new ErrorsInterceptor(dictionary)));
60
+ exports.UsesErrorsInterceptor = UsesErrorsInterceptor;
61
+ const useGlobalErrrorsInterceptor = (app, dictionary) => {
62
+ app.useGlobalInterceptors(new ErrorsInterceptor(dictionary));
63
+ };
64
+ exports.useGlobalErrrorsInterceptor = useGlobalErrrorsInterceptor;
@@ -1,7 +1,8 @@
1
1
  import { Response } from 'express';
2
2
  export declare class LivenessProbe {
3
3
  private axios;
4
- private favicon;
5
- getAlive(): string;
4
+ getAlive(): {
5
+ ok: number;
6
+ };
6
7
  getFavicon(res: Response): Promise<void>;
7
8
  }
@@ -28,17 +28,17 @@ exports.LivenessProbe = void 0;
28
28
  const common_1 = require("@nestjs/common");
29
29
  const swagger_1 = require("@nestjs/swagger");
30
30
  const axios_1 = __importDefault(require("axios"));
31
+ const favicon = 'https://file.svi-itlab.com/650d514c58cbe60df3494bc0';
31
32
  let LivenessProbe = class LivenessProbe {
32
33
  constructor() {
33
34
  this.axios = axios_1.default.create();
34
- this.favicon = 'https://file.svi-itlab.com/650d514c58cbe60df3494bc0';
35
35
  }
36
36
  getAlive() {
37
- return 'service is up and running';
37
+ return { ok: 1 };
38
38
  }
39
39
  getFavicon(res) {
40
40
  return __awaiter(this, void 0, void 0, function* () {
41
- const { data } = yield this.axios.get(this.favicon, { responseType: 'stream' });
41
+ const { data } = yield this.axios.get(favicon, { responseType: 'stream' });
42
42
  data.pipe(res);
43
43
  });
44
44
  }
@@ -47,7 +47,7 @@ __decorate([
47
47
  (0, common_1.Get)('alive'),
48
48
  __metadata("design:type", Function),
49
49
  __metadata("design:paramtypes", []),
50
- __metadata("design:returntype", String)
50
+ __metadata("design:returntype", void 0)
51
51
  ], LivenessProbe.prototype, "getAlive", null);
52
52
  __decorate([
53
53
  (0, common_1.Get)('favicon.ico'),
@@ -1,12 +1,47 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
2
18
  var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
19
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
20
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
21
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
22
  return c > 3 && r && Object.defineProperty(target, key, r), r;
7
23
  };
8
- var __importDefault = (this && this.__importDefault) || function (mod) {
9
- return (mod && mod.__esModule) ? mod : { "default": mod };
24
+ var __importStar = (this && this.__importStar) || function (mod) {
25
+ if (mod && mod.__esModule) return mod;
26
+ var result = {};
27
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
28
+ __setModuleDefault(result, mod);
29
+ return result;
30
+ };
31
+ var __metadata = (this && this.__metadata) || function (k, v) {
32
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
33
+ };
34
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
35
+ return function (target, key) { decorator(target, key, paramIndex); }
36
+ };
37
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
38
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
39
+ return new (P || (P = Promise))(function (resolve, reject) {
40
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
41
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
42
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
43
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
44
+ });
10
45
  };
11
46
  var DatabaseModule_1;
12
47
  Object.defineProperty(exports, "__esModule", { value: true });
@@ -14,8 +49,11 @@ exports.DatabaseModule = void 0;
14
49
  const common_1 = require("@nestjs/common");
15
50
  const config_1 = require("@nestjs/config");
16
51
  const mongoose_1 = require("@nestjs/mongoose");
17
- const mongoose_2 = __importDefault(require("mongoose"));
52
+ const swagger_1 = require("@nestjs/swagger");
53
+ const fs_1 = require("fs");
54
+ const mongoose_2 = __importStar(require("mongoose"));
18
55
  const path_1 = require("path");
56
+ const exceptions_1 = require("../../exceptions");
19
57
  const model_service_1 = require("./model-service/model.service");
20
58
  const populate_service_1 = require("./populate-service/populate.service");
21
59
  const service_mapper_service_1 = require("./service-mapper-service/service-mapper.service");
@@ -31,33 +69,51 @@ let DatabaseModule = DatabaseModule_1 = class DatabaseModule {
31
69
  inject: [config_1.ConfigService],
32
70
  useFactory: (configService) => {
33
71
  mongoose_2.default.set('strictQuery', true);
34
- const production = configService.getOrThrow('NODE_ENV', 'development') === 'production';
35
- if (production)
36
- this.logger.log('Running in production mode... Connecting internally without tls');
37
- else
38
- this.logger.log('Running in development mode... Connecting externally with tls');
72
+ const mongoSrv = configService.getOrThrow('MONGO_SRV');
39
73
  const username = configService.getOrThrow('MONGO_USERNAME');
40
74
  const password = configService.getOrThrow('MONGO_PASSWORD');
41
- const generalOpts = {
42
- uri: configService.getOrThrow('MONGO_SRV'),
75
+ const production = configService.getOrThrow('NODE_ENV', 'development') === 'production';
76
+ const tlsDir = (0, path_1.join)(process.cwd(), configService.get('MONGO_CERT_DIR', 'mongo-tls'));
77
+ const tlsCAFile = (0, path_1.join)(tlsDir, 'hub-services-mongodb.ca.pem');
78
+ const tlsCertificateKeyFile = (0, path_1.join)(tlsDir, 'hub-services-mongodb.cert-key.pem');
79
+ if (production) {
80
+ this.logger.log('Running in production mode... Creating certificates');
81
+ (0, fs_1.mkdirSync)(tlsDir, { recursive: true });
82
+ const caCrt = configService.getOrThrow('MONGO_TLS_CA_CRT');
83
+ const tlsCrt = configService.getOrThrow('MONGO_TLS_TLS_CRT');
84
+ const tlsKey = configService.getOrThrow('MONGO_TLS_TLS_KEY');
85
+ (0, fs_1.rmSync)(tlsCAFile, { force: true, recursive: true });
86
+ (0, fs_1.writeFileSync)(tlsCAFile, caCrt);
87
+ this.logger.log('Created Certificate Authority (.pem)');
88
+ (0, fs_1.rmSync)(tlsCertificateKeyFile, { force: true, recursive: true });
89
+ (0, fs_1.writeFileSync)(tlsCertificateKeyFile, `${tlsCrt}\n${tlsKey}`);
90
+ this.logger.log('Created Client Certificate and Key (.pem)');
91
+ }
92
+ else {
93
+ this.logger.log(`Running in development mode... Using local certificates in ${tlsDir}`);
94
+ if (!(0, fs_1.existsSync)(tlsCAFile))
95
+ this.logger.error("Couldn't locate Certificate Authority (.pem) at " + tlsCAFile);
96
+ if (!(0, fs_1.existsSync)(tlsCertificateKeyFile))
97
+ this.logger.error("Couldn't locate Client Certificate and Key (.pem) at " + tlsCertificateKeyFile);
98
+ }
99
+ return {
100
+ uri: `mongodb+srv://${mongoSrv}/`,
43
101
  auth: { username, password },
44
102
  appName: options.appName,
45
103
  dbName: options.dbName,
46
104
  authMechanism: 'SCRAM-SHA-256',
47
105
  authSource: 'admin',
48
- replicaSet: 'organisation-hub-mongodb',
106
+ replicaSet: 'hub-services-mongodb',
49
107
  serverSelectionTimeoutMS: 2000,
50
- };
51
- const certDir = () => configService.getOrThrow('MONGO_CERT_DIR');
52
- const tlsOpts = () => ({
108
+ retryAttempts: 3,
53
109
  tls: true,
54
- tlsCAFile: (0, path_1.join)(process.cwd(), certDir(), 'ca.crt'),
55
- tlsCertificateKeyFile: (0, path_1.join)(process.cwd(), certDir(), 'certificate-key.pem'),
56
- });
57
- return Object.assign({}, generalOpts, production ? {} : tlsOpts());
110
+ tlsCAFile,
111
+ tlsCertificateKeyFile,
112
+ };
58
113
  },
59
114
  }),
60
115
  ],
116
+ controllers: [DatabaseLivenessProbe],
61
117
  };
62
118
  }
63
119
  };
@@ -66,3 +122,28 @@ DatabaseModule = DatabaseModule_1 = __decorate([
66
122
  (0, common_1.Module)({})
67
123
  ], DatabaseModule);
68
124
  exports.DatabaseModule = DatabaseModule;
125
+ let DatabaseLivenessProbe = class DatabaseLivenessProbe {
126
+ constructor(connection) {
127
+ this.connection = connection;
128
+ }
129
+ alive() {
130
+ return __awaiter(this, void 0, void 0, function* () {
131
+ const result = yield this.connection.db.admin().ping();
132
+ if (result && result.ok)
133
+ return { ok: 1 };
134
+ throw new exceptions_1.ErrorException('Could not connect to MongoDB', common_1.HttpStatus.UNAUTHORIZED);
135
+ });
136
+ }
137
+ };
138
+ __decorate([
139
+ (0, common_1.Get)('/alive-db'),
140
+ __metadata("design:type", Function),
141
+ __metadata("design:paramtypes", []),
142
+ __metadata("design:returntype", Promise)
143
+ ], DatabaseLivenessProbe.prototype, "alive", null);
144
+ DatabaseLivenessProbe = __decorate([
145
+ (0, swagger_1.ApiTags)('Kubernetes DB Liveness Probe'),
146
+ (0, common_1.Controller)(),
147
+ __param(0, (0, mongoose_1.InjectConnection)()),
148
+ __metadata("design:paramtypes", [mongoose_2.Connection])
149
+ ], DatabaseLivenessProbe);
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "email": "timo.scheuermann@sv-informatik.de",
6
6
  "url": "https://timos.design"
7
7
  },
8
- "version": "2.1.2",
8
+ "version": "2.2.0",
9
9
  "type": "commonjs",
10
10
  "files": [
11
11
  "dist"