chore: Anonymise ip addresses in rudderstack events (#11066)

This commit is contained in:
Federico Meini 2024-10-03 10:42:29 +02:00 committed by GitHub
parent 835824cf53
commit 86c632aabf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 113 additions and 28 deletions

View file

@ -267,6 +267,44 @@ describe('Telemetry', () => {
expect(execBuffer['2'].prod_success?.first).toEqual(execTime1); expect(execBuffer['2'].prod_success?.first).toEqual(execTime1);
}); });
}); });
describe('Rudderstack', () => {
test("should call rudderStack.identify() with a fake IP address to instruct Rudderstack to not use the user's IP address", () => {
const traits = {
name: 'Test User',
age: 30,
isActive: true,
};
telemetry.identify(traits);
const expectedArgs = {
userId: instanceId,
traits: { ...traits, instanceId },
context: {
ip: '0.0.0.0', // RudderStack anonymized IP
},
};
expect(mockRudderStack.identify).toHaveBeenCalledWith(expectedArgs);
});
test("should call rudderStack.track() with a fake IP address to instruct Rudderstack to not use the user's IP address", () => {
const eventName = 'Test Event';
const properties = { user_id: '1234' };
telemetry.track(eventName, properties);
expect(mockRudderStack.track).toHaveBeenCalledWith(
expect.objectContaining({
event: eventName,
context: {
ip: '0.0.0.0', // RudderStack anonymized IP
},
}),
);
});
});
}); });
const fakeJestSystemTime = (dateTime: string | Date): Date => { const fakeJestSystemTime = (dateTime: string | Date): Date => {

View file

@ -188,6 +188,10 @@ export class Telemetry {
this.rudderStack.identify({ this.rudderStack.identify({
userId: instanceId, userId: instanceId,
traits: { ...traits, instanceId }, traits: { ...traits, instanceId },
context: {
// provide a fake IP address to instruct RudderStack to not use the user's IP address
ip: '0.0.0.0',
},
}); });
} }
@ -212,13 +216,18 @@ export class Telemetry {
userId: `${instanceId}${user_id ? `#${user_id}` : ''}`, userId: `${instanceId}${user_id ? `#${user_id}` : ''}`,
event: eventName, event: eventName,
properties: updatedProperties, properties: updatedProperties,
context: {},
}; };
if (withPostHog) { if (withPostHog) {
this.postHog?.track(payload); this.postHog?.track(payload);
} }
return this.rudderStack.track(payload); return this.rudderStack.track({
...payload,
// provide a fake IP address to instruct RudderStack to not use the user's IP address
context: { ...payload.context, ip: '0.0.0.0' },
});
} }
// test helpers // test helpers

View file

@ -38,9 +38,11 @@ describe('telemetry', () => {
telemetry.identify(userId, instanceId); telemetry.identify(userId, instanceId);
expect(identifyFunction).toHaveBeenCalledTimes(1); expect(identifyFunction).toHaveBeenCalledTimes(1);
expect(identifyFunction).toHaveBeenCalledWith(`${instanceId}#${userId}`, { expect(identifyFunction).toHaveBeenCalledWith(
instance_id: instanceId, `${instanceId}#${userId}`,
}); { instance_id: instanceId },
{ context: { ip: '0.0.0.0' } },
);
}); });
it('Rudderstack identify method should be called when proving userId and versionCli ', () => { it('Rudderstack identify method should be called when proving userId and versionCli ', () => {
@ -60,10 +62,14 @@ describe('telemetry', () => {
telemetry.identify(userId, instanceId, versionCli); telemetry.identify(userId, instanceId, versionCli);
expect(identifyFunction).toHaveBeenCalledTimes(1); expect(identifyFunction).toHaveBeenCalledTimes(1);
expect(identifyFunction).toHaveBeenCalledWith(`${instanceId}#${userId}`, { expect(identifyFunction).toHaveBeenCalledWith(
instance_id: instanceId, `${instanceId}#${userId}`,
version_cli: versionCli, {
}); instance_id: instanceId,
version_cli: versionCli,
},
{ context: { ip: '0.0.0.0' } },
);
}); });
it('Rudderstack identify method should be called when proving userId and versionCli and projectId', () => { it('Rudderstack identify method should be called when proving userId and versionCli and projectId', () => {
@ -84,10 +90,14 @@ describe('telemetry', () => {
telemetry.identify(userId, instanceId, versionCli, projectId); telemetry.identify(userId, instanceId, versionCli, projectId);
expect(identifyFunction).toHaveBeenCalledTimes(1); expect(identifyFunction).toHaveBeenCalledTimes(1);
expect(identifyFunction).toHaveBeenCalledWith(`${instanceId}#${userId}#${projectId}`, { expect(identifyFunction).toHaveBeenCalledWith(
instance_id: instanceId, `${instanceId}#${userId}#${projectId}`,
version_cli: versionCli, {
}); instance_id: instanceId,
version_cli: versionCli,
},
{ context: { ip: '0.0.0.0' } },
);
}); });
it('Rudderstack identify method should be called when proving userId and deployment type is cloud ', () => { it('Rudderstack identify method should be called when proving userId and deployment type is cloud ', () => {
@ -111,11 +121,15 @@ describe('telemetry', () => {
telemetry.identify(userId, instanceId, versionCli); telemetry.identify(userId, instanceId, versionCli);
expect(identifyFunction).toHaveBeenCalledTimes(1); expect(identifyFunction).toHaveBeenCalledTimes(1);
expect(identifyFunction).toHaveBeenCalledWith(`${instanceId}#${userId}`, { expect(identifyFunction).toHaveBeenCalledWith(
instance_id: instanceId, `${instanceId}#${userId}`,
version_cli: versionCli, {
user_cloud_id: userCloudId, instance_id: instanceId,
}); version_cli: versionCli,
user_cloud_id: userCloudId,
},
{ context: { ip: '0.0.0.0' } },
);
}); });
it('Rudderstack identify method should be called when proving userId and deployment type is cloud', () => { it('Rudderstack identify method should be called when proving userId and deployment type is cloud', () => {
@ -139,11 +153,15 @@ describe('telemetry', () => {
telemetry.identify(userId, instanceId, versionCli); telemetry.identify(userId, instanceId, versionCli);
expect(identifyFunction).toHaveBeenCalledTimes(1); expect(identifyFunction).toHaveBeenCalledTimes(1);
expect(identifyFunction).toHaveBeenCalledWith(`${instanceId}#${userId}`, { expect(identifyFunction).toHaveBeenCalledWith(
instance_id: instanceId, `${instanceId}#${userId}`,
version_cli: versionCli, {
user_cloud_id: userCloudId, instance_id: instanceId,
}); version_cli: versionCli,
user_cloud_id: userCloudId,
},
{ context: { ip: '0.0.0.0' } },
);
}); });
it('Rudderstack reset method should be called when proving userId and deployment type is cloud', () => { it('Rudderstack reset method should be called when proving userId and deployment type is cloud', () => {
@ -175,10 +193,14 @@ describe('telemetry', () => {
telemetry.track(event, properties, options); telemetry.track(event, properties, options);
expect(trackFunction).toHaveBeenCalledTimes(1); expect(trackFunction).toHaveBeenCalledTimes(1);
expect(trackFunction).toHaveBeenCalledWith(event, { expect(trackFunction).toHaveBeenCalledWith(
...properties, event,
version_cli: MOCK_VERSION_CLI, {
}); ...properties,
version_cli: MOCK_VERSION_CLI,
},
{ context: { ip: '0.0.0.0' } },
);
}); });
}); });
}); });

View file

@ -94,6 +94,12 @@ export class Telemetry {
this.rudderStack?.identify( this.rudderStack?.identify(
`${instanceId}#${userId}${projectId ? '#' + projectId : ''}`, `${instanceId}#${userId}${projectId ? '#' + projectId : ''}`,
traits, traits,
{
context: {
// provide a fake IP address to instruct RudderStack to not use the user's IP address
ip: '0.0.0.0',
},
},
); );
} else { } else {
this.rudderStack?.reset(); this.rudderStack?.reset();
@ -112,7 +118,12 @@ export class Telemetry {
version_cli: useRootStore().versionCli, version_cli: useRootStore().versionCli,
}; };
this.rudderStack.track(event, updatedProperties); this.rudderStack.track(event, updatedProperties, {
context: {
// provide a fake IP address to instruct RudderStack to not use the user's IP address
ip: '0.0.0.0',
},
});
if (options.withPostHog) { if (options.withPostHog) {
usePostHog().capture(event, updatedProperties); usePostHog().capture(event, updatedProperties);
@ -136,7 +147,12 @@ export class Telemetry {
properties.theme = useUIStore().appliedTheme; properties.theme = useUIStore().appliedTheme;
const category = route.meta?.telemetry?.pageCategory || 'Editor'; const category = route.meta?.telemetry?.pageCategory || 'Editor';
this.rudderStack.page(category, pageName, properties); this.rudderStack.page(category, pageName, properties, {
context: {
// provide a fake IP address to instruct RudderStack to not use the user's IP address
ip: '0.0.0.0',
},
});
} else { } else {
this.pageEventQueue.push({ this.pageEventQueue.push({
route, route,