Skip to content

Commit

Permalink
[TECH] Améliorer les logs de la route /api/token (PIX-15710)
Browse files Browse the repository at this point in the history
  • Loading branch information
pix-service-auto-merge authored Dec 12, 2024
2 parents ccd8fcf + c93798c commit e67fa40
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
import { logger } from '../../shared/infrastructure/utils/logger.js';
import { monitoringTools } from '../../shared/infrastructure/monitoring-tools.js';
import { generateHash } from '../infrastructure/utils/crypto.js';

async function monitorApiTokenRoute(request, h, dependencies = { logger }) {
async function monitorApiTokenRoute(request, h, dependencies = { monitoringTools }) {
const { username, refresh_token, grant_type, scope } = request.payload;

if (grant_type === 'password') {
const hash = generateHash(username);
dependencies.logger.warn({ hash, grant_type, scope }, 'Authentication attempt');
dependencies.monitoringTools.logWarnWithCorrelationIds({
message: 'Authentication attempt',
hash,
grant_type,
scope,
});
} else if (grant_type === 'refresh_token') {
const hash = generateHash(refresh_token);
dependencies.logger.warn({ hash, grant_type, scope }, 'Authentication attempt');
dependencies.monitoringTools.logWarnWithCorrelationIds({
message: 'Authentication attempt',
hash,
grant_type,
scope,
});
} else {
dependencies.logger.warn(request.payload, 'Authentication attempt with unknown method');
dependencies.monitoringTools.logWarnWithCorrelationIds({
message: 'Authentication attempt with unknown method',
...request.payload,
});
}

return true;
Expand Down
1 change: 1 addition & 0 deletions api/src/shared/infrastructure/monitoring-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ const monitoringTools = {
incrementInContext,
installHapiHook,
logErrorWithCorrelationIds,
logWarnWithCorrelationIds,
logInfoWithCorrelationIds,
pushInContext,
setInContext,
Expand Down
13 changes: 8 additions & 5 deletions api/src/shared/infrastructure/plugins/pino.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import crypto from 'node:crypto';

import { stdSerializers } from 'pino';

import { monitoringTools } from '../../../../src/shared/infrastructure/monitoring-tools.js';
import { generateHash } from '../../../identity-access-management/infrastructure/utils/crypto.js';
import { config } from '../../config.js';
import { logger } from '../utils/logger.js';

Expand All @@ -17,11 +16,15 @@ function requestSerializer(req) {
};

if (!config.hapi.enableRequestMonitoring) return enhancedReq;

// monitor api token route
const context = monitoringTools.getContext();
if (context?.request?.route?.path === '/api/token') {
const hash = crypto.createHash('sha256');
const username = context?.request?.payload?.username;
enhancedReq.usernameHash = username ? hash.update(username).digest('hex') : '-';
const { username, refresh_token, grant_type, scope } = context.request.payload || {};
enhancedReq.grantType = grant_type || '-';
enhancedReq.scope = scope || '-';
enhancedReq.usernameHash = generateHash(username) || '-';
enhancedReq.refreshTokenHash = generateHash(refresh_token) || '-';
}

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ describe('Unit | Identity Access Management | Application | monitor-pre-handlers
const grant_type = 'password';
const scope = 'pix-app';
const hash = generateHash(username);
const logger = { warn: sinon.stub() };
const monitoringTools = { logWarnWithCorrelationIds: sinon.stub() };
const request = { payload: { grant_type, username, scope } };

// when
monitorPreHandlers.monitorApiTokenRoute(request, hFake, { logger });
monitorPreHandlers.monitorApiTokenRoute(request, hFake, { monitoringTools });

// then
expect(logger.warn).to.have.been.calledWith({ hash, grant_type, scope }, 'Authentication attempt');
expect(monitoringTools.logWarnWithCorrelationIds).to.have.been.calledWith({
message: 'Authentication attempt',
hash,
grant_type,
scope,
});
});

it('logs authentication attempt with grant type refresh token', async function () {
Expand All @@ -26,27 +31,35 @@ describe('Unit | Identity Access Management | Application | monitor-pre-handlers
const grant_type = 'refresh_token';
const scope = 'pix-app';
const hash = generateHash(refresh_token);
const logger = { warn: sinon.stub() };
const monitoringTools = { logWarnWithCorrelationIds: sinon.stub() };
const request = { payload: { grant_type, refresh_token, scope } };

// when
monitorPreHandlers.monitorApiTokenRoute(request, hFake, { logger });
monitorPreHandlers.monitorApiTokenRoute(request, hFake, { monitoringTools });

// then
expect(logger.warn).to.have.been.calledWith({ hash, grant_type, scope }, 'Authentication attempt');
expect(monitoringTools.logWarnWithCorrelationIds).to.have.been.calledWith({
message: 'Authentication attempt',
hash,
grant_type,
scope,
});
});

it('logs authentication attempt with grant type unknown', async function () {
// given
const grant_type = 'unknown';
const logger = { warn: sinon.stub() };
const monitoringTools = { logWarnWithCorrelationIds: sinon.stub() };
const request = { payload: { foo: 'bar', grant_type } };

// when
monitorPreHandlers.monitorApiTokenRoute(request, hFake, { logger });
monitorPreHandlers.monitorApiTokenRoute(request, hFake, { monitoringTools });

// then
expect(logger.warn).to.have.been.calledWith(request.payload, 'Authentication attempt with unknown method');
expect(monitoringTools.logWarnWithCorrelationIds).to.have.been.calledWith({
message: 'Authentication attempt with unknown method',
...request.payload,
});
});
});
});

0 comments on commit e67fa40

Please sign in to comment.