Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: add clientIP and path to log context. #554

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion registry/server/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ export default async (app: Express, settingsService: SettingsService, config: an
return res.end(`<pre>${info.message}</pre><br><a href="/">Go to main page</a>`);
}

getLogger().info(`User ${user.identifier} authenticated via OpenID`)
getLogger().info(`User ${user.identifier} authenticated via OpenID`);
req.logIn(user, function (err) {
if (err) {
return next(err);
Expand Down
14 changes: 13 additions & 1 deletion registry/server/middleware/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RequestHandler } from 'express';
import { Request, RequestHandler } from 'express';
import { AsyncLocalStorage } from 'node:async_hooks';
import { v4 } from 'uuid';

Expand All @@ -10,6 +10,8 @@ export interface Store {
reqId: string;
domain: string;
user?: User;
path: string;
clientIp?: string;
}

export const storage = new AsyncLocalStorage<TypedMap<Store>>();
Expand All @@ -20,6 +22,16 @@ export const contextMiddleware: RequestHandler = (req, res, next) => {
storage.run(store, () => {
store.set('reqId', getPluginManagerInstance().getReportingPlugin().genReqId?.() ?? v4());
store.set('domain', req.hostname);
store.set('path', req.path);
store.set('clientIp', getUserIp(req));
next();
});
};

function getUserIp(req: Request) {
const forwarded = req.headers['x-forwarded-for'];
const headerValue = Array.isArray(forwarded)
? forwarded[0] // If it's an array, take the first one
: forwarded;
return typeof headerValue === 'string' ? headerValue.split(',')[0] : req.socket.remoteAddress;
}
52 changes: 52 additions & 0 deletions registry/tests/context.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';
import express from 'express';
import request from 'supertest';
import { contextMiddleware, storage } from '../server/middleware/context';

function createTestAppWithContextMiddleware() {
// Create a new express application
const app = express();

// Add the contextMiddleware to the express application
app.use(contextMiddleware);

// Define a test route that will retrieve the values from the storage
app.get('/test', (req, res) => {
// Access the storage directly
const store = storage.getStore();
if (store) {
res.send({
reqId: store.get('reqId'),
domain: store.get('domain'),
path: store.get('path'),
clientIp: store.get('clientIp'),
});
} else {
res.status(500).send('No store found');
}
});
return app;
}

describe('contextMiddleware', () => {
it('should set the correct store values', (done) => {
const app = createTestAppWithContextMiddleware();

// Use supertest to simulate a request to the test route
request(app)
.get('/test?test=1')
.set('X-Forwarded-For', '10.10.10.1,12.13.14.15')
.expect(200)
.end((err, response) => {
if (err) return done(err);

const body = response.body;
expect(body.domain).to.equal('127.0.0.1');
expect(body.path).to.equal('/test');
expect(body.clientIp).to.equal('10.10.10.1');

done();
});
});
});