Skip to content

Commit

Permalink
Test logging changes
Browse files Browse the repository at this point in the history
  • Loading branch information
alivenichoppa committed Nov 11, 2024
1 parent 033eb0a commit 44d983d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
4 changes: 4 additions & 0 deletions app/app.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Express as loggingExpress, Logger } from '@hmcts/nodejs-logging';
import config from 'config';
import cookieParser from 'cookie-parser';
import csurf from 'csurf';
Expand All @@ -6,6 +7,7 @@ import express from 'express';
import helmet from 'helmet';
import webpack from 'webpack';
import webpackDevMiddleware, { Options } from 'webpack-dev-middleware';
import { LoggerInstance } from 'winston';
import internationalization from '../locale/en.json';
import webpackDevConfig from '../webpack/webpack.dev.js';
import { configureIdam, configureLogger, configureNunjucks, configureS2S } from './app-config';
Expand All @@ -20,6 +22,7 @@ import { setupSession } from './session';
import { getUrl } from './utils/url-utils';

const uuid = require('uuid');
const logger: LoggerInstance = Logger.getLogger('app.ts');

function createApp() {
const app: express.Application = express();
Expand Down Expand Up @@ -148,6 +151,7 @@ function configureHelmet(app) {
}
}));
app.use(helmet.noCache());
app.use(loggingExpress.accessLogger());
}

export {
Expand Down
21 changes: 12 additions & 9 deletions app/service/s2s-service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Logger } from '@hmcts/nodejs-logging';
import axios from 'axios';
import { LoggerInstance } from 'winston';
import { setupSecrets } from '../setupSecrets';
import { isJWTExpired } from '../utils/jwt-utils';
import Logger, { getLogLabel } from '../utils/logger';
// import Logger, { getLogLabel } from '../utils/logger';

const config = setupSecrets();

Expand All @@ -12,8 +14,9 @@ const proxyHost: string = config.get('proxy.host');
const proxyPort: number = config.get('proxy.port');
const microServiceName: string = config.get('s2s.microserviceName');

const logger: Logger = new Logger();
const logLabel: string = getLogLabel(__filename);
const logger: LoggerInstance = Logger.getLogger('s2s-service.ts');
// const logger: Logger = new Logger();
// const logLabel: string = getLogLabel(__filename);

interface IS2SService {
buildRequest: () => {};
Expand All @@ -24,7 +27,7 @@ interface IS2SService {

export default class S2SService implements IS2SService {
private static instance: S2SService;
private initialization;
private initialization: Promise<void>;
private serviceToken: string;

public static getInstance(): S2SService {
Expand Down Expand Up @@ -66,27 +69,27 @@ export default class S2SService implements IS2SService {
* Note: This token is stored in memory and this token is only valid for 3 hours.
*/
async requestServiceToken() {
logger.trace('Attempting to request a S2S token', logLabel);
logger.info('Attempting to request a S2S token');
const request = await this.buildRequest();
let proxyConfig;
if (process.env.NODE_ENV === 'development' && !s2sUrl.startsWith('http://localhost')) {
proxyConfig = { proxy: { host: proxyHost, port: proxyPort } };
}
let res;
for (let i = 0; i < 3; i++) {
for (let i = 0; i < 5; i++) {
try {
res = await axios.post(request.uri, request.body, proxyConfig);
break;
} catch (err) {
logger.exception(err, logLabel);
logger.error(err);
i++;
}
}
if (res && res.data) {
this.serviceToken = res.data;
logger.trace('Received S2S token and stored token', logLabel);
logger.info('Received S2S token and stored token');
} else {
logger.exception('Could not retrieve S2S token', logLabel);
logger.info('Could not retrieve S2S token');
}
}

Expand Down
17 changes: 13 additions & 4 deletions app/utils/jwt-utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { Logger } from '@hmcts/nodejs-logging';
import { LoggerInstance } from 'winston';
import { jwtRepack } from './jwt-repack';
import Logger, { getLogLabel } from './logger';
// import Logger, { getLogLabel } from './logger';

const logger: Logger = new Logger();
const logLabel: string = getLogLabel(__filename);
// const logger: Logger = new Logger();
// const logLabel: string = getLogLabel(__filename);
const logger: LoggerInstance = Logger.getLogger('jwt-utils.ts');

export function decodeJWTToken(jwtToken: string) {
let decoded;
try {
decoded = jwtRepack.decode(jwtToken);
} catch (err) {
logger.exception(err, logLabel);
logger.error(err);
throw new Error(err);
}
return decoded;
Expand All @@ -24,11 +27,17 @@ export function isJWTExpired(jwtToken: string) {
let offset = 5 * 60; // 5 minutes
let isExpiredToken = false;

if (!jwtToken) {
logger.info('S2S token is null or empty.');
return true;
}

const decoded = decodeJWTToken(jwtToken);

const currentTime = Math.round(new Date().getTime() / 1000);

if (decoded && decoded.exp < (currentTime + offset)) {
logger.info('S2S token has expired.');
isExpiredToken = true;
}
return isExpiredToken;
Expand Down

0 comments on commit 44d983d

Please sign in to comment.