1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-11-01 19:07:38 +01:00
unleash.unleash/src/lib/util/anonymise.ts
Nuno Góis b013d4286c
fix: log project access events (#3683)
https://linear.app/unleash/issue/2-992/register-event-when-granting-access-to-a-project

 - Correctly registers a new `project-access-added` event;
- Adds an anonymization method that supports anonymizing objects,
recursively searching for keys to anonymize;
- Fixes the event label used by `ProjectUserUpdateRoleEvent` to be
`PROJECT_USER_ROLE_CHANGED`;


![image](https://user-images.githubusercontent.com/14320932/236216227-bf6e5dff-f509-48a4-ba7c-9f37e23e87c0.png)
2023-05-09 11:13:38 +02:00

33 lines
970 B
TypeScript

import { createHash } from 'crypto';
export function anonymise(s: string): string {
const hash = createHash('sha256')
.update(s, 'utf-8')
.digest('hex')
.slice(0, 9);
return `${hash}@unleash.run`;
}
export function anonymiseKeys<T>(object: T, keys: string[]): T {
if (typeof object !== 'object' || object === null) {
return object;
}
if (Array.isArray(object)) {
return object.map((item) => anonymiseKeys(item, keys)) as unknown as T;
} else {
return Object.keys(object).reduce((result, key) => {
if (
keys.includes(key) &&
result[key] !== undefined &&
result[key] !== null
) {
result[key] = anonymise(result[key]);
} else if (typeof result[key] === 'object') {
result[key] = anonymiseKeys(result[key], keys);
}
return result;
}, object);
}
}