Skip to content

Commit

Permalink
feat(APIM-344): change how string redaction helper is called
Browse files Browse the repository at this point in the history
  • Loading branch information
avaitonis committed Jul 18, 2023
1 parent 8b84a14 commit 9f3442a
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 173 deletions.
4 changes: 2 additions & 2 deletions src/constants/redact-strings.constant.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const REDACT_STRINGS = [
{ searchValue: /(Login failed for user ').*(')/g, replaceValue: '$1[Redacted]$2' },
{ searchValue: process.env.DATABASE_USERNAME, replaceValue: '[RedactedDomain]' },
{ searchValue: process.env.DATABASE_PASSWORD, replaceValue: '[RedactedDomain]' },
{ searchValue: process.env.DATABASE_USERNAME, replaceValue: '[Redacted]' },
{ searchValue: process.env.DATABASE_PASSWORD, replaceValue: '[Redacted]' },
{ searchValue: process.env.DATABASE_MDM_HOST, replaceValue: '[RedactedDomain]' },
{ searchValue: process.env.DATABASE_CEDAR_HOST, replaceValue: '[RedactedDomain]' },
{ searchValue: process.env.DATABASE_NUMBER_GENERATOR_HOST, replaceValue: '[RedactedDomain]' },
Expand Down
88 changes: 0 additions & 88 deletions src/helpers/redact-errors.helper.test.ts

This file was deleted.

29 changes: 0 additions & 29 deletions src/helpers/redact-errors.helper.ts

This file was deleted.

28 changes: 28 additions & 0 deletions src/logging/log-keys-to-redact.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,33 @@ describe('logKeysToRedact', () => {
expect(result).toContain(buildKeyToRedact([logKey, 'options', 'cause', 'innerError', sensitiveChildKeys[0]]));
expect(result).toContain(buildKeyToRedact([logKey, 'options', 'cause', 'innerError', sensitiveChildKeys[1]]));
});

it('includes all sensitive child keys of an dbError', () => {
const { logKey, sensitiveChildKeys } = options.dbError;

expect(result).toContain(buildKeyToRedact([logKey, sensitiveChildKeys[0]]));
expect(result).toContain(buildKeyToRedact([logKey, sensitiveChildKeys[1]]));
});

it('includes all sensitive child keys of an original dbError', () => {
const { logKey, sensitiveChildKeys } = options.dbError;

expect(result).toContain(buildKeyToRedact([logKey, 'originalError', 'info', sensitiveChildKeys[0]]));
expect(result).toContain(buildKeyToRedact([logKey, 'originalError', 'info', sensitiveChildKeys[1]]));
});

it('includes all sensitive child keys of an dbError driverError', () => {
const { logKey, sensitiveChildKeys } = options.dbError;

expect(result).toContain(buildKeyToRedact([logKey, 'driverError', sensitiveChildKeys[0]]));
expect(result).toContain(buildKeyToRedact([logKey, 'driverError', sensitiveChildKeys[1]]));
});

it(`includes all sensitive child keys of an dbError driverError's original error`, () => {
const { logKey, sensitiveChildKeys } = options.dbError;

expect(result).toContain(buildKeyToRedact([logKey, 'driverError', 'originalError', 'info', sensitiveChildKeys[0]]));
expect(result).toContain(buildKeyToRedact([logKey, 'driverError', 'originalError', 'info', sensitiveChildKeys[1]]));
});
});
});
8 changes: 7 additions & 1 deletion src/main.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { HEADERS_LOG_KEY, OUTGOING_REQUEST_LOG_KEY } from '@ukef/modules/http/ht
import { MdmModule } from '@ukef/modules/mdm.module';
import { LoggerModule } from 'nestjs-pino';

import { REDACT_STRING_PATHS, REDACT_STRINGS } from './constants';
import { redactStringsInLogArgs } from './helpers/redact-strings-in-log-args.helper';
import { logKeysToRedact } from './logging/log-keys-to-redact';
import { LoggingInterceptor } from './logging/logging-interceptor.helper';

Expand All @@ -30,6 +32,11 @@ import { LoggingInterceptor } from './logging/logging-interceptor.helper';
singleLine: true,
},
},
hooks: {
logMethod(inputArgs, method) {
return method.apply(this, redactStringsInLogArgs(config.get<boolean>('app.redactLogs'), REDACT_STRING_PATHS, REDACT_STRINGS, inputArgs));
},
},
redact: logKeysToRedact({
redactLogs: config.get<boolean>('app.redactLogs'),
clientRequest: {
Expand Down Expand Up @@ -60,7 +67,6 @@ import { LoggingInterceptor } from './logging/logging-interceptor.helper';
}),
MdmModule,
],
controllers: [],
providers: [{ provide: APP_INTERCEPTOR, useClass: LoggingInterceptor }],
})
export class MainModule {}
9 changes: 3 additions & 6 deletions src/modules/constants/constants.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Injectable, InternalServerErrorException, NotFoundException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { InjectRepository } from '@nestjs/typeorm';
import { DATABASE, REDACT_STRING_PATHS, REDACT_STRINGS } from '@ukef/constants';
import { DATABASE } from '@ukef/constants';
import { DbResponseHelper } from '@ukef/helpers/db-response.helper';
import { redactError } from '@ukef/helpers/redact-errors.helper';
import { PinoLogger } from 'nestjs-pino';
import { Repository } from 'typeorm';

Expand All @@ -15,7 +13,6 @@ export class ConstantsService {
@InjectRepository(ConstantSpiEntity, DATABASE.CIS)
private readonly constantsCisRepository: Repository<ConstantSpiEntity>,
private readonly logger: PinoLogger,
private readonly config: ConfigService,
) {}

async find(oecdRiskCategory: number, category: string): Promise<ConstantSpiEntity[]> {
Expand All @@ -38,10 +35,10 @@ export class ConstantsService {
return transformedResults;
} catch (err) {
if (err instanceof NotFoundException) {
this.logger.warn(redactError(this.config.get<boolean>('app.redactLogs'), REDACT_STRING_PATHS, REDACT_STRINGS, err));
this.logger.warn(err);
throw err;
}
this.logger.error(redactError(this.config.get<boolean>('app.redactLogs'), REDACT_STRING_PATHS, REDACT_STRINGS, err));
this.logger.error(err);
throw new InternalServerErrorException();
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/modules/currencies/currencies.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Injectable, InternalServerErrorException, NotFoundException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { InjectRepository } from '@nestjs/typeorm';
import { DATABASE, DATE, REDACT_STRING_PATHS, REDACT_STRINGS } from '@ukef/constants';
import { DATABASE, DATE } from '@ukef/constants';
import { DbResponseHelper } from '@ukef/helpers/db-response.helper';
import { redactError } from '@ukef/helpers/redact-errors.helper';
import { PinoLogger } from 'nestjs-pino';
import { DataSource, Equal, Repository } from 'typeorm';

Expand All @@ -20,7 +18,6 @@ export class CurrenciesService {
@InjectRepository(CurrencyExchangeEntity, DATABASE.CEDAR)
private readonly currencyExchangeRepository: Repository<CurrencyExchangeEntity>,
private readonly logger: PinoLogger,
private readonly config: ConfigService,
) {}

async findAll(): Promise<CurrencyEntity[]> {
Expand Down Expand Up @@ -69,10 +66,10 @@ export class CurrenciesService {
return renamedResults;
} catch (err) {
if (err instanceof NotFoundException) {
this.logger.warn(redactError(this.config.get<boolean>('app.redactLogs'), REDACT_STRING_PATHS, REDACT_STRINGS, err));
this.logger.warn(err);
throw err;
} else {
this.logger.error(redactError(this.config.get<boolean>('app.redactLogs'), REDACT_STRING_PATHS, REDACT_STRINGS, err));
this.logger.error(err);
throw new InternalServerErrorException();
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/modules/exposure-period/exposure-period.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Injectable, InternalServerErrorException, NotFoundException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { InjectDataSource } from '@nestjs/typeorm';
import { DATABASE, REDACT_STRING_PATHS, REDACT_STRINGS } from '@ukef/constants';
import { redactError } from '@ukef/helpers/redact-errors.helper';
import { DATABASE } from '@ukef/constants';
import { PinoLogger } from 'nestjs-pino';
import { DataSource } from 'typeorm';

Expand All @@ -14,7 +12,6 @@ export class ExposurePeriodService {
@InjectDataSource(DATABASE.MDM)
private readonly mdmDataSource: DataSource,
private readonly logger: PinoLogger,
private readonly config: ConfigService,
) {}

async calculate(startDate: string, endDate: string, productGroup: string): Promise<ExposurePeriodDto> {
Expand All @@ -29,10 +26,10 @@ export class ExposurePeriodService {
return { exposurePeriod: results[0].EXPOSURE_PERIOD };
} catch (err) {
if (err instanceof NotFoundException) {
this.logger.warn(redactError(this.config.get<boolean>('app.redactLogs'), REDACT_STRING_PATHS, REDACT_STRINGS, err));
this.logger.warn(err);
throw err;
} else {
this.logger.error(redactError(this.config.get<boolean>('app.redactLogs'), REDACT_STRING_PATHS, REDACT_STRINGS, err));
this.logger.error(err);
throw new InternalServerErrorException();
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/modules/interest-rates/interest-rates.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Injectable, InternalServerErrorException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { InjectRepository } from '@nestjs/typeorm';
import { DATABASE, DATE, REDACT_STRING_PATHS, REDACT_STRINGS } from '@ukef/constants';
import { redactError } from '@ukef/helpers/redact-errors.helper';
import { DATABASE, DATE } from '@ukef/constants';
import { PinoLogger } from 'nestjs-pino';
import { Equal, Repository } from 'typeorm';

Expand All @@ -14,14 +12,13 @@ export class InterestRatesService {
@InjectRepository(InterestRatesEntity, DATABASE.CEDAR)
private readonly interestRates: Repository<InterestRatesEntity>,
private readonly logger: PinoLogger,
private readonly config: ConfigService,
) {}

findAll(): Promise<InterestRatesEntity[]> {
try {
return this.interestRates.find({ where: { effectiveTo: Equal(new Date(DATE.MAXIMUM_LIMIT)) } });
} catch (err) {
this.logger.error(redactError(this.config.get<boolean>('app.redactLogs'), REDACT_STRING_PATHS, REDACT_STRINGS, err));
this.logger.error(err);
throw new InternalServerErrorException();
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/modules/markets/markets.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Injectable, InternalServerErrorException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { InjectRepository } from '@nestjs/typeorm';
import { DATABASE, REDACT_STRING_PATHS, REDACT_STRINGS } from '@ukef/constants';
import { DATABASE } from '@ukef/constants';
import { DbResponseHelper } from '@ukef/helpers/db-response.helper';
import { redactError } from '@ukef/helpers/redact-errors.helper';
import { PinoLogger } from 'nestjs-pino';
import { Repository } from 'typeorm';

Expand All @@ -15,7 +13,6 @@ export class MarketsService {
@InjectRepository(MarketEntity, DATABASE.CIS)
private readonly marketsRepository: Repository<MarketEntity>,
private readonly logger: PinoLogger,
private readonly config: ConfigService,
) {}

async find(active?: string, search?: string): Promise<MarketEntity[]> {
Expand Down Expand Up @@ -48,7 +45,7 @@ export class MarketsService {

return mappedResults;
} catch (err: any) {
this.logger.error(redactError(this.config.get<boolean>('app.redactLogs'), REDACT_STRING_PATHS, REDACT_STRINGS, err));
this.logger.error(err);
throw new InternalServerErrorException();
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/modules/numbers/numbers.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { BadRequestException, Injectable, InternalServerErrorException, NotFoundException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { InjectRepository } from '@nestjs/typeorm';
import { DATABASE, REDACT_STRING_PATHS, REDACT_STRINGS } from '@ukef/constants';
import { redactError } from '@ukef/helpers/redact-errors.helper';
import { DATABASE } from '@ukef/constants';
import { PinoLogger } from 'nestjs-pino';
import { Repository } from 'typeorm';

Expand All @@ -15,7 +13,6 @@ export class NumbersService {
@InjectRepository(UkefId, DATABASE.NUMBER_GENERATOR)
private readonly numberRepository: Repository<UkefId>,
private readonly logger: PinoLogger,
private readonly config: ConfigService,
) {}

async create(createUkefIdDto: CreateUkefIdDto[]): Promise<UkefId[]> {
Expand Down Expand Up @@ -46,10 +43,10 @@ export class NumbersService {
return this.mapFieldsFromDbToApi(dbNumber[0]);
} catch (err) {
if (err instanceof NotFoundException || err instanceof BadRequestException) {
this.logger.warn(redactError(this.config.get<boolean>('app.redactLogs'), REDACT_STRING_PATHS, REDACT_STRINGS, err));
this.logger.warn(err);
throw err;
} else {
this.logger.error(redactError(this.config.get<boolean>('app.redactLogs'), REDACT_STRING_PATHS, REDACT_STRINGS, err));
this.logger.error(err);
throw new InternalServerErrorException();
}
}
Expand Down
Loading

0 comments on commit 9f3442a

Please sign in to comment.