-
Notifications
You must be signed in to change notification settings - Fork 741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Anonymize member creation for activities if listed in erasure requests #2711
Open
skwowet
wants to merge
3
commits into
main
Choose a base branch
from
anonymize-user
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
backend/src/database/migrations/U1733322265__anonymizeMemberIdentities.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
alter table "requestedForErasureMemberIdentities" drop constraint "unique_anonymized_member"; | ||
|
||
alter table "requestedForErasureMemberIdentities" add constraint "unique_anonymized_member" unique ("memberId", "platform", "type", "value"); |
1 change: 1 addition & 0 deletions
1
backend/src/database/migrations/V1733322265__anonymizeMemberIdentities.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
alter table "requestedForErasureMemberIdentities" add column "memberId" uuid; |
318 changes: 318 additions & 0 deletions
318
services/apps/data_sink_worker/src/bin/anonymize-member.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,318 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
|
||
import { | ||
PriorityLevelContextRepository, | ||
QueuePriorityContextLoader, | ||
SearchSyncWorkerEmitter, | ||
} from '@crowd/common_services' | ||
import { DbStore, getDbConnection } from '@crowd/data-access-layer/src/database' | ||
import { anonymizeUsername } from '@crowd/data-access-layer/src/gdpr' | ||
import { getServiceChildLogger } from '@crowd/logging' | ||
import { QueueFactory } from '@crowd/queue' | ||
import { getRedisClient } from '@crowd/redis' | ||
import { IMemberIdentity, MemberIdentityType } from '@crowd/types' | ||
|
||
import { DB_CONFIG, QUEUE_CONFIG, REDIS_CONFIG } from '../conf' | ||
|
||
const log = getServiceChildLogger('anonymize-member') | ||
|
||
const processArguments = process.argv.slice(2) | ||
|
||
if (processArguments.length === 0 || processArguments.length % 2 !== 0) { | ||
log.error( | ||
` | ||
Expected argument in pairs which can be any of the following: | ||
- ids "<memberId1>, <memberId2>, ..." | ||
- email [email protected] | ||
- name "John Doe" | ||
- <platform> <value> (e.g. lfid someusername) | ||
`, | ||
) | ||
process.exit(1) | ||
} | ||
|
||
setImmediate(async () => { | ||
const manualCheckFile = `manual_check_member_ids.txt` | ||
const dbConnection = await getDbConnection(DB_CONFIG()) | ||
const store = new DbStore(log, dbConnection) | ||
const queueClient = QueueFactory.createQueueService(QUEUE_CONFIG()) | ||
const redisClient = await getRedisClient(REDIS_CONFIG()) | ||
const priorityLevelRepo = new PriorityLevelContextRepository(new DbStore(log, dbConnection), log) | ||
const loader: QueuePriorityContextLoader = (tenantId: string) => | ||
priorityLevelRepo.loadPriorityLevelContext(tenantId) | ||
|
||
const searchSyncWorkerEmitter = new SearchSyncWorkerEmitter(queueClient, redisClient, loader, log) | ||
await searchSyncWorkerEmitter.init() | ||
|
||
const pairs = [] | ||
for (let i = 0; i < processArguments.length; i += 2) { | ||
pairs.push({ | ||
type: processArguments[i], | ||
value: processArguments[i + 1], | ||
}) | ||
} | ||
|
||
log.info( | ||
`Anonymizing member based on input data: [${pairs | ||
.map((p) => `${p.type} "${p.value}"`) | ||
.join(', ')}]`, | ||
) | ||
|
||
const idParams = pairs.filter((p) => p.type === 'ids') | ||
const idsToAnonymize: string[] = [] | ||
for (const param of idParams) { | ||
idsToAnonymize.push(...param.value.split(',').map((id) => id.trim())) | ||
} | ||
|
||
const memberDataMap: Map<string, any> = new Map() | ||
|
||
if (idsToAnonymize.length > 0) { | ||
for (const memberId of idsToAnonymize) { | ||
try { | ||
await store.transactionally(async (t) => { | ||
let memberData: any | ||
if (memberDataMap.has(memberId)) { | ||
memberData = memberDataMap.get(memberId) | ||
} else { | ||
memberData = await store | ||
.connection() | ||
.one(`select * from members where id = $(memberId)`, { | ||
memberId, | ||
}) | ||
memberDataMap.set(memberId, memberData) | ||
} | ||
|
||
// Get all identities for the member | ||
const identities = await store | ||
.connection() | ||
.any(`select * from "memberIdentities" where "memberId" = $(memberId)`, { memberId }) | ||
|
||
log.info({ tenantId: memberData.tenantId }, 'ANONYMIZING MEMBER DATA...') | ||
|
||
// Anonymize each identity and update the database | ||
for (const identity of identities) { | ||
const hashedUsername = anonymizeUsername( | ||
identity.value, | ||
identity.platform, | ||
identity.type, | ||
) | ||
|
||
await anonymizeMemberInDb(store, identity, hashedUsername) | ||
} | ||
|
||
await searchSyncWorkerEmitter.triggerMemberSync(memberData.tenantId, memberId, true) | ||
}) | ||
} catch (err) { | ||
log.error(err, { memberId }, 'Failed to anonymize member!') | ||
} | ||
} | ||
} else { | ||
const nameIdentity = pairs.find((p) => p.type === 'name') | ||
const otherIdentities = pairs.filter((p) => p.type !== 'name') | ||
|
||
if (otherIdentities.length > 0) { | ||
const conditions: string[] = [] | ||
const params: any = {} | ||
let index = 0 | ||
for (const pair of otherIdentities) { | ||
params[`value_${index}`] = pair.value | ||
if (pair.type === 'email') { | ||
conditions.push( | ||
`(type = '${MemberIdentityType.EMAIL}' and lower(value) = lower($(value_${index})))`, | ||
) | ||
} else { | ||
params[`platform_${index}`] = (pair.type as string).toLowerCase() | ||
conditions.push( | ||
`(platform = $(platform_${index}) and lower(value) = lower($(value_${index})))`, | ||
) | ||
} | ||
|
||
index++ | ||
} | ||
|
||
const query = `select * from "memberIdentities" where ${conditions.join(' or ')}` | ||
const existingIdentities = await store.connection().any(query, params) | ||
|
||
if (existingIdentities.length > 0) { | ||
log.info(`Found ${existingIdentities.length} existing identities to anonymize.`) | ||
|
||
for (const identity of existingIdentities) { | ||
try { | ||
await store.transactionally(async (t) => { | ||
const hashedUsername = anonymizeUsername( | ||
identity.value, | ||
identity.platform, | ||
identity.type, | ||
) | ||
|
||
// Update memberIdentities table | ||
await store.connection().none( | ||
`update "memberIdentities" | ||
set value = $(hashedValue) | ||
where "memberId" = $(memberId) | ||
and platform = $(platform) | ||
and type = $(type)`, | ||
{ | ||
hashedValue: hashedUsername, | ||
memberId: identity.memberId, | ||
platform: identity.platform, | ||
type: identity.type, | ||
}, | ||
) | ||
|
||
// Add to requestedForErasureMemberIdentities | ||
await store.connection().none( | ||
`insert into "requestedForErasureMemberIdentities" | ||
(id, platform, type, value, "memberId") | ||
values ($(id), $(platform), $(type), $(value), $(memberId)) | ||
on conflict do nothing`, | ||
{ | ||
memberId: identity.memberId, | ||
platform: identity.platform, | ||
type: identity.type, | ||
value: hashedUsername, | ||
}, | ||
) | ||
|
||
// Update activities | ||
await store.connection().none( | ||
`update activities | ||
set username = $(hashedValue) | ||
where "memberId" = $(memberId)`, | ||
{ | ||
hashedValue: hashedUsername, | ||
memberId: identity.memberId, | ||
}, | ||
) | ||
|
||
await store.connection().none( | ||
`update activities | ||
set "objectMemberUsername" = $(hashedValue) | ||
where "objectMemberId" = $(memberId)`, | ||
{ | ||
hashedValue: hashedUsername, | ||
memberId: identity.memberId, | ||
}, | ||
) | ||
|
||
await searchSyncWorkerEmitter.triggerMemberSync( | ||
identity.tenantId, | ||
identity.memberId, | ||
true, | ||
) | ||
}) | ||
} catch (err) { | ||
log.error(err, { identity }, 'Failed to anonymize member identity!') | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (nameIdentity) { | ||
const results = await store | ||
.connection() | ||
.any(`select id from members where lower("displayName") = lower($(name))`, { | ||
name: nameIdentity.value.trim(), | ||
}) | ||
|
||
if (results.length > 0) { | ||
addLinesToFile(manualCheckFile, [ | ||
`name: ${nameIdentity.value}, member ids: [${results.map((r) => r.id).join(', ')}]`, | ||
]) | ||
log.warn( | ||
`Found ${results.length} members with name: ${ | ||
nameIdentity.value | ||
}! Manual check required for member ids: [${results.map((r) => r.id).join(', ')}]!`, | ||
) | ||
} | ||
} | ||
} | ||
|
||
process.exit(0) | ||
}) | ||
|
||
function addLinesToFile(filePath: string, lines: string[]) { | ||
try { | ||
fs.mkdirSync(path.dirname(filePath), { recursive: true }) | ||
try { | ||
fs.accessSync(filePath) | ||
fs.appendFileSync(filePath, lines.join('\n') + '\n') | ||
} catch (error) { | ||
fs.writeFileSync(filePath, lines.join('\n') + '\n') | ||
} | ||
} catch (err) { | ||
log.error(err, { filePath }, 'Error while writing to file!') | ||
throw err | ||
} | ||
} | ||
|
||
async function anonymizeMemberInDb( | ||
store: DbStore, | ||
identity: IMemberIdentity, | ||
hashedUsername: string, | ||
) { | ||
// Update member details | ||
// todo: cleanup original member data in members table | ||
await store.connection().none( | ||
`update members | ||
set "displayName" = $(hashedValue) | ||
where id = $(memberId)`, | ||
{ | ||
hashedValue: hashedUsername, | ||
memberId: identity.memberId, | ||
}, | ||
) | ||
|
||
// Update memberIdentities table | ||
await store.connection().none( | ||
`update "memberIdentities" | ||
set value = $(hashedValue) | ||
where "memberId" = $(memberId) | ||
and platform = $(platform) | ||
and type = $(type)`, | ||
{ | ||
hashedValue: hashedUsername, | ||
memberId: identity.memberId, | ||
platform: identity.platform, | ||
type: identity.type, | ||
}, | ||
) | ||
|
||
// Add to requestedForErasureMemberIdentities | ||
await store.connection().none( | ||
`insert into "requestedForErasureMemberIdentities" | ||
(id, platform, type, value, "memberId") | ||
values ($(id), $(platform), $(type), $(value), $(memberId)) | ||
on conflict do nothing`, | ||
{ | ||
id: identity.memberId, | ||
platform: identity.platform, | ||
type: identity.type, | ||
value: hashedUsername, | ||
memberId: identity.memberId, | ||
}, | ||
) | ||
|
||
// Update activities table | ||
await store.connection().none( | ||
`update activities | ||
set "objectMemberUsername" = $(hashedValue) | ||
where "objectMemberId" = $(memberId)`, | ||
{ | ||
hashedValue: hashedUsername, | ||
memberId: identity.memberId, | ||
}, | ||
) | ||
|
||
// Update activities table for member activities | ||
await store.connection().none( | ||
`update activities | ||
set username = $(hashedValue) | ||
where "memberId" = $(memberId)`, | ||
{ | ||
hashedValue: hashedUsername, | ||
memberId: identity.memberId, | ||
}, | ||
) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add path validation in
addLinesToFile
The function should validate the file path to prevent directory traversal attacks.