Skip to content

Commit

Permalink
anonymizeIp
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkirtzel committed Nov 19, 2024
1 parent 042a294 commit 5f37825
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
15 changes: 14 additions & 1 deletion packages/connectors/gcp/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,27 @@ describe('connector GCP', () => {
country: 'DE',
encoding: 'gzip',
hash: expect.any(String),
ip: '127.0.0.1',
ip: '127.0.0.0',
language: 'ts',
origin: 'localhost',
region: 'Hamburg',
userAgent: 'Mozilla/5.0',
});
});

test('anonymizeIp', async () => {
const first = await connectorGCPHttpFunction(request, {
anonymizeIp: true,
});
const second = await connectorGCPHttpFunction(request, {
anonymizeIp: false,
});

expect(first).toStrictEqual(expect.objectContaining({ ip: '127.0.0.0' }));
expect(second).toStrictEqual(expect.objectContaining({ ip: '127.0.0.1' }));
expect(first.hash).not.toEqual(second.hash);
});

test('hash', async () => {
expect(
await connectorGCPHttpFunction(request, { hash: 'fingerprint' }),
Expand Down
10 changes: 7 additions & 3 deletions packages/connectors/gcp/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import type { Request } from '@elbwalker/types';
import type { Request as GCPRequest } from '@google-cloud/functions-framework';
import type { HttpFunction } from './types';
import { getHashNode, isDefined } from '@elbwalker/utils';
import { anonymizeIP, getHashNode, isDefined } from '@elbwalker/utils';

export * as ConnectorGCP from './types';

export async function connectorGCPHttpFunction(
request: GCPRequest,
options: HttpFunction = { hash: 'hash' },
options: HttpFunction = {},
): Promise<Request.Context> {
const context: Request.Context = {};
const { hash } = options;
const { anonymizeIp = true, hash = 'hash' } = options;

const headerMapping: Record<string, keyof typeof context> = {
origin: 'origin',
Expand All @@ -28,6 +28,10 @@ export async function connectorGCPHttpFunction(
if (isDefined(value)) context[key] = value;
});

// Anonymize IP address before processing it
if (context.ip && anonymizeIp) context.ip = anonymizeIP(context.ip);

// Create a temporary hash as a fingerprint
if (hash)
context[hash as keyof Request.Context] = await getHashNode(
'' +
Expand Down
1 change: 1 addition & 0 deletions packages/connectors/gcp/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import type { Request as GCPRequest } from '@google-cloud/functions-framework';
export type Request = GCPRequest;

export interface HttpFunction {
anonymizeIp?: boolean;
hash?: string | false;
}

0 comments on commit 5f37825

Please sign in to comment.