Skip to content

Commit

Permalink
feat: add caller ip in action context.caller.request.ip (#1196)
Browse files Browse the repository at this point in the history
  • Loading branch information
EnkiP authored Oct 18, 2024
1 parent 48cd880 commit 25aa5b8
Show file tree
Hide file tree
Showing 23 changed files with 361 additions and 56 deletions.
3 changes: 2 additions & 1 deletion packages/agent/src/utils/query-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export default class QueryStringParser {

static parseCaller(context: Context): Caller {
const timezone = context.request.query.timezone?.toString();
const { ip } = context.request;

if (!timezone) {
throw new ValidationError('Missing timezone');
Expand All @@ -140,7 +141,7 @@ export default class QueryStringParser {
QueryStringParser.VALID_TIMEZONES.add(timezone);
}

return { ...context.state.user, timezone, requestId: uuidv4() };
return { ...context.state.user, timezone, requestId: uuidv4(), request: { ip } };
}

static parsePagination(context: Context): Page {
Expand Down
14 changes: 12 additions & 2 deletions packages/agent/test/routes/access/api-chart-collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ describe('CollectionApiChartRoute', () => {
await route.handleApiChart(context);

expect(dataSource.getCollection('books').renderChart).toHaveBeenCalledWith(
{ email: 'marty@doclabs.com', requestId: expect.any(String), timezone: 'Europe/Paris' },
{
email: 'marty@doclabs.com',
requestId: expect.any(String),
request: { ip: expect.any(String) },
timezone: 'Europe/Paris',
},
'myChart',
[123],
);
Expand All @@ -72,7 +77,12 @@ describe('CollectionApiChartRoute', () => {
await route.handleSmartChart(context);

expect(dataSource.getCollection('books').renderChart).toHaveBeenCalledWith(
{ email: 'marty@doclabs.com', requestId: expect.any(String), timezone: 'Europe/Paris' },
{
email: 'marty@doclabs.com',
requestId: expect.any(String),
request: { ip: expect.any(String) },
timezone: 'Europe/Paris',
},
'myChart',
[123],
);
Expand Down
14 changes: 12 additions & 2 deletions packages/agent/test/routes/access/api-chart-datasource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ describe('DataSourceApiChartRoute', () => {
await route.handleApiChart(context);

expect(dataSource.renderChart).toHaveBeenCalledWith(
{ email: 'marty@doclabs.com', requestId: expect.any(String), timezone: 'Europe/Paris' },
{
email: 'marty@doclabs.com',
requestId: expect.any(String),
request: { ip: expect.any(String) },
timezone: 'Europe/Paris',
},
'myChart',
);
expect(context.response.body).toMatchObject({
Expand All @@ -64,7 +69,12 @@ describe('DataSourceApiChartRoute', () => {
await route.handleSmartChart(context);

expect(dataSource.renderChart).toHaveBeenCalledWith(
{ email: 'marty@doclabs.com', requestId: expect.any(String), timezone: 'Europe/Paris' },
{
email: 'marty@doclabs.com',
requestId: expect.any(String),
request: { ip: expect.any(String) },
timezone: 'Europe/Paris',
},
'myChart',
);
expect(context.response.body).toStrictEqual({ countCurrent: 12 });
Expand Down
85 changes: 73 additions & 12 deletions packages/agent/test/routes/access/chart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,12 @@ describe('ChartRoute', () => {
await chart.handleChart(context);

expect(dataSource.getCollection('books').aggregate).toHaveBeenCalledWith(
{ ...user, requestId: expect.any(String), timezone: 'Europe/Paris' },
{
...user,
requestId: expect.any(String),
request: { ip: expect.any(String) },
timezone: 'Europe/Paris',
},
{ conditionTree: null, search: null, searchExtended: false, segment: null },
{ field: undefined, groups: undefined, operation: 'Count' },
);
Expand Down Expand Up @@ -259,7 +264,12 @@ describe('ChartRoute', () => {

expect(dataSource.getCollection('books').aggregate).toHaveBeenCalledTimes(1);
expect(dataSource.getCollection('books').aggregate).toHaveBeenCalledWith(
{ ...user, requestId: expect.any(String), timezone: 'Europe/Paris' },
{
...user,
requestId: expect.any(String),
request: { ip: expect.any(String) },
timezone: 'Europe/Paris',
},
{
conditionTree: { field: 'name', operator: 'Present', value: null },
search: null,
Expand Down Expand Up @@ -308,6 +318,7 @@ describe('ChartRoute', () => {
{
email: 'john.doe@domain.com',
requestId: expect.any(String),
request: { ip: expect.any(String) },
timezone: 'Europe/Paris',
},
{
Expand Down Expand Up @@ -393,7 +404,12 @@ describe('ChartRoute', () => {
await chart.handleChart(context);

expect(dataSource.getCollection('books').aggregate).toHaveBeenCalledWith(
{ ...user, requestId: expect.any(String), timezone: 'Europe/Paris' },
{
...user,
requestId: expect.any(String),
request: { ip: expect.any(String) },
timezone: 'Europe/Paris',
},
{ conditionTree: null, search: null, searchExtended: false, segment: null },
{ field: undefined, groups: undefined, operation: 'Count' },
);
Expand Down Expand Up @@ -434,7 +450,12 @@ describe('ChartRoute', () => {
await chart.handleChart(context);

expect(dataSource.getCollection('books').aggregate).toHaveBeenCalledWith(
{ email: 'john.doe@domain.com', requestId: expect.any(String), timezone: 'Europe/Paris' },
{
email: 'john.doe@domain.com',
requestId: expect.any(String),
request: { ip: expect.any(String) },
timezone: 'Europe/Paris',
},
{
conditionTree: { field: 'title', operator: 'NotContains', value: '[test]' },
search: null,
Expand Down Expand Up @@ -478,7 +499,12 @@ describe('ChartRoute', () => {
await chart.handleChart(context);

expect(dataSource.getCollection('books').aggregate).toHaveBeenCalledWith(
{ ...user, requestId: expect.any(String), timezone: 'Europe/Paris' },
{
...user,
requestId: expect.any(String),
request: { ip: expect.any(String) },
timezone: 'Europe/Paris',
},
{ conditionTree: null, search: null, searchExtended: false, segment: null },
{ field: undefined, groups: [{ field: 'author:firstName' }], operation: 'Count' },
);
Expand Down Expand Up @@ -530,7 +556,12 @@ describe('ChartRoute', () => {
await chart.handleChart(context);

expect(dataSource.getCollection('books').aggregate).toHaveBeenCalledWith(
{ email: 'john.doe@domain.com', requestId: expect.any(String), timezone: 'Europe/Paris' },
{
email: 'john.doe@domain.com',
requestId: expect.any(String),
request: { ip: expect.any(String) },
timezone: 'Europe/Paris',
},
{
conditionTree: { field: 'title', operator: 'NotContains', value: '[test]' },
search: null,
Expand Down Expand Up @@ -584,7 +615,12 @@ describe('ChartRoute', () => {
await chart.handleChart(context);

expect(dataSource.getCollection('books').aggregate).toHaveBeenCalledWith(
{ ...user, requestId: expect.any(String), timezone: 'Europe/Paris' },
{
...user,
requestId: expect.any(String),
request: { ip: expect.any(String) },
timezone: 'Europe/Paris',
},
{
conditionTree: factories.conditionTreeLeaf.build({
operator: 'Present',
Expand Down Expand Up @@ -649,7 +685,12 @@ describe('ChartRoute', () => {
await chart.handleChart(context);

expect(dataSource.getCollection('books').aggregate).toHaveBeenCalledWith(
{ ...user, requestId: expect.any(String), timezone: 'Europe/Paris' },
{
...user,
requestId: expect.any(String),
request: { ip: expect.any(String) },
timezone: 'Europe/Paris',
},
{
conditionTree: factories.conditionTreeBranch.build({
aggregator: 'And',
Expand Down Expand Up @@ -723,7 +764,12 @@ describe('ChartRoute', () => {
await chart.handleChart(context);

expect(dataSource.getCollection('books').aggregate).toHaveBeenCalledWith(
{ ...user, requestId: expect.any(String), timezone: 'Europe/Paris' },
{
...user,
requestId: expect.any(String),
request: { ip: expect.any(String) },
timezone: 'Europe/Paris',
},
{ conditionTree: null, search: null, searchExtended: false, segment: null },
{ field: 'id', groups: [{ field: 'author:id' }], operation: 'Sum' },
2,
Expand Down Expand Up @@ -769,7 +815,12 @@ describe('ChartRoute', () => {
await chart.handleChart(context);

expect(dataSource.getCollection('books').aggregate).toHaveBeenCalledWith(
{ ...user, requestId: expect.any(String), timezone: 'Europe/Paris' },
{
...user,
requestId: expect.any(String),
request: { ip: expect.any(String) },
timezone: 'Europe/Paris',
},
{ conditionTree: null, search: null, searchExtended: false, segment: null },
{ operation: 'Count', field: null, groups: [{ field: 'publisher:id' }] },
2,
Expand Down Expand Up @@ -822,7 +873,12 @@ describe('ChartRoute', () => {
await chart.handleChart(context);

expect(dataSource.getCollection('books').aggregate).toHaveBeenCalledWith(
{ email: 'john.doe@domain.com', requestId: expect.any(String), timezone: 'Europe/Paris' },
{
email: 'john.doe@domain.com',
requestId: expect.any(String),
request: { ip: expect.any(String) },
timezone: 'Europe/Paris',
},
{
conditionTree: {
field: 'author:name',
Expand Down Expand Up @@ -879,7 +935,12 @@ describe('ChartRoute', () => {
await chart.handleChart(context);

expect(dataSource.getCollection('books').aggregate).toHaveBeenCalledWith(
{ ...user, requestId: expect.any(String), timezone: 'Europe/Paris' },
{
...user,
requestId: expect.any(String),
request: { ip: expect.any(String) },
timezone: 'Europe/Paris',
},
{ conditionTree: null, search: null, searchExtended: false, segment: null },
{ groups: [{ field: 'author:id' }], operation: 'Count' },
2,
Expand Down
14 changes: 12 additions & 2 deletions packages/agent/test/routes/access/count-related.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,12 @@ describe('CountRelatedRoute', () => {
dataSource.getCollection('books'),
['2d162303-78bf-599e-b197-93590ac3d315'],
'myBookPersons',
{ email: 'john.doe@domain.com', requestId: expect.any(String), timezone: 'Europe/Paris' },
{
email: 'john.doe@domain.com',
requestId: expect.any(String),
request: { ip: expect.any(String) },
timezone: 'Europe/Paris',
},
new Filter({
search: 'searched argument',
searchExtended: false,
Expand Down Expand Up @@ -223,7 +228,12 @@ describe('CountRelatedRoute', () => {
dataSource.getCollection('books'),
['2d162303-78bf-599e-b197-93590ac3d315'],
'myBookPersons',
{ email: 'john.doe@domain.com', requestId: expect.any(String), timezone: 'Europe/Paris' },
{
email: 'john.doe@domain.com',
requestId: expect.any(String),
request: { ip: expect.any(String) },
timezone: 'Europe/Paris',
},
new Filter({
search: 'searched argument',
searchExtended: false,
Expand Down
14 changes: 12 additions & 2 deletions packages/agent/test/routes/access/count.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ describe('CountRoute', () => {
await count.handleCount(context);

expect(aggregateSpy).toHaveBeenCalledWith(
{ email: 'john.doe@domain.com', requestId: expect.any(String), timezone: 'Europe/Paris' },
{
email: 'john.doe@domain.com',
requestId: expect.any(String),
request: { ip: expect.any(String) },
timezone: 'Europe/Paris',
},
{ conditionTree: null, search: null, searchExtended: false, segment: null },
{ operation: 'Count' },
);
Expand Down Expand Up @@ -75,7 +80,12 @@ describe('CountRoute', () => {
await count.handleCount(context);

expect(aggregateSpy).toHaveBeenCalledWith(
{ email: 'john.doe@domain.com', requestId: expect.any(String), timezone: 'Europe/Paris' },
{
email: 'john.doe@domain.com',
requestId: expect.any(String),
request: { ip: expect.any(String) },
timezone: 'Europe/Paris',
},
{
conditionTree: {
field: 'title',
Expand Down
28 changes: 24 additions & 4 deletions packages/agent/test/routes/access/csv-related.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@ describe('CsvRelatedRoute', () => {

await readCsv(context.response.body as AsyncGenerator<string>);
expect(csvGenerator).toHaveBeenCalledWith(
{ email: 'john.doe@domain.com', requestId: expect.any(String), timezone: 'Europe/Paris' },
{
email: 'john.doe@domain.com',
requestId: expect.any(String),
request: { ip: expect.any(String) },
timezone: 'Europe/Paris',
},
new Projection('id', 'name'),
'id,name',
paginatedFilter,
Expand All @@ -134,7 +139,12 @@ describe('CsvRelatedRoute', () => {
dataSource.getCollection('books'),
['123e4567-e89b-12d3-a456-111111111111'],
'myPersons',
{ email: 'john.doe@domain.com', requestId: expect.any(String), timezone: 'Europe/Paris' },
{
email: 'john.doe@domain.com',
requestId: expect.any(String),
request: { ip: expect.any(String) },
timezone: 'Europe/Paris',
},
expect.any(PaginatedFilter),
expect.any(Projection),
);
Expand Down Expand Up @@ -216,7 +226,12 @@ describe('CsvRelatedRoute', () => {

await readCsv(context.response.body as AsyncGenerator<string>);
expect(csvGenerator).toHaveBeenCalledWith(
{ email: 'john.doe@domain.com', requestId: expect.any(String), timezone: 'Europe/Paris' },
{
email: 'john.doe@domain.com',
requestId: expect.any(String),
request: { ip: expect.any(String) },
timezone: 'Europe/Paris',
},
new Projection('id', 'name'),
'id,name',
paginatedFilter,
Expand All @@ -228,7 +243,12 @@ describe('CsvRelatedRoute', () => {
dataSource.getCollection('books'),
['123e4567-e89b-12d3-a456-111111111111'],
'myPersons',
{ email: 'john.doe@domain.com', requestId: expect.any(String), timezone: 'Europe/Paris' },
{
email: 'john.doe@domain.com',
requestId: expect.any(String),
request: { ip: expect.any(String) },
timezone: 'Europe/Paris',
},
expect.any(PaginatedFilter),
expect.any(Projection),
);
Expand Down
7 changes: 6 additions & 1 deletion packages/agent/test/routes/access/csv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ describe('CsvRoute', () => {

await readCsv(context.response.body as AsyncGenerator<string>);
expect(csvGenerator).toHaveBeenCalledWith(
{ email: 'john.doe@domain.com', requestId: expect.any(String), timezone: 'Europe/Paris' },
{
email: 'john.doe@domain.com',
requestId: expect.any(String),
request: { ip: expect.any(String) },
timezone: 'Europe/Paris',
},
['id', 'name'],
'id,name',
paginatedFilter,
Expand Down
Loading

0 comments on commit 25aa5b8

Please sign in to comment.