-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
user-deletion-service.ts
216 lines (189 loc) · 8.67 KB
/
user-deletion-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
* Copyright (c) 2020 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License-AGPL.txt in the project root for license information.
*/
import { injectable, inject } from "inversify";
import { UserDB, WorkspaceDB, UserStorageResourcesDB, TeamDB, ProjectDB } from "@gitpod/gitpod-db/lib";
import { User, Workspace } from "@gitpod/gitpod-protocol";
import { StorageClient } from "../storage/storage-client";
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
import { WorkspaceManagerClientProvider } from "@gitpod/ws-manager/lib/client-provider";
import { StopWorkspaceRequest, StopWorkspacePolicy } from "@gitpod/ws-manager/lib";
import { WorkspaceDeletionService } from "../workspace/workspace-deletion-service";
import { AuthProviderService } from "../auth/auth-provider-service";
import { IAnalyticsWriter } from "@gitpod/gitpod-protocol/lib/analytics";
import { Config } from "../config";
@injectable()
export class UserDeletionService {
@inject(Config) protected readonly config: Config;
@inject(UserDB) protected readonly db: UserDB;
@inject(WorkspaceDB) protected readonly workspaceDb: WorkspaceDB;
@inject(UserStorageResourcesDB) protected readonly userStorageResourcesDb: UserStorageResourcesDB;
@inject(TeamDB) protected readonly teamDb: TeamDB;
@inject(ProjectDB) protected readonly projectDb: ProjectDB;
@inject(StorageClient) protected readonly storageClient: StorageClient;
@inject(WorkspaceManagerClientProvider)
protected readonly workspaceManagerClientProvider: WorkspaceManagerClientProvider;
@inject(WorkspaceDeletionService) protected readonly workspaceDeletionService: WorkspaceDeletionService;
@inject(AuthProviderService) protected readonly authProviderService: AuthProviderService;
@inject(IAnalyticsWriter) protected readonly analytics: IAnalyticsWriter;
/**
* This method deletes a User logically. The contract here is that after running this method without receiving an
* error, the system does not contain any data that is relatable to the actual person in the sense of the GDPR.
* To guarantee that, but also maintain traceability
* we anonymize data that might contain user related/relatable data and keep the entities itself (incl. ids).
*/
async deleteUser(id: string): Promise<void> {
const user = await this.db.findUserById(id);
if (!user) {
throw new Error(`No user with id ${id} found!`);
}
if (user.markedDeleted === true) {
log.debug({ userId: id }, "Is deleted but markDeleted already set. Continuing.");
}
// Stop all workspaces
await this.stopWorkspaces(user);
// Auth Providers
const authProviders = await this.authProviderService.getAuthProvidersOfUser(user);
for (const provider of authProviders) {
try {
await this.authProviderService.deleteAuthProvider(provider);
} catch (error) {
log.error({ userId: id }, "Failed to delete user's auth provider.", error);
}
}
// User
await this.db.transaction(async (db) => {
this.anonymizeUser(user);
this.deleteIdentities(user);
await this.deleteTokens(db, user);
user.lastVerificationTime = undefined;
user.markedDeleted = true;
await db.storeUser(user);
});
await Promise.all([
// Workspace
this.anonymizeAllWorkspaces(id),
// UserStorageResourcesDB
this.userStorageResourcesDb.deleteAllForUser(user.id),
// Bucket
this.deleteUserBucket(id),
// Teams owned only by this user
this.deleteSoleOwnedTeams(id),
// Team memberships
this.deleteTeamMemberships(id),
// User projects
this.deleteUserProjects(id),
]);
// Track the deletion Event for Analytics Purposes
this.analytics.track({
userId: user.id,
event: "deletion",
properties: {
deleted_at: new Date().toISOString(),
},
});
this.analytics.identify({
userId: user.id,
traits: {
github_slug: "deleted-user",
gitlab_slug: "deleted-user",
bitbucket_slug: "deleted-user",
email: "deleted-user",
full_name: "deleted-user",
name: "deleted-user",
},
});
}
protected async stopWorkspaces(user: User) {
const runningWorkspaces = await this.workspaceDb.findRunningInstancesWithWorkspaces(undefined, user.id);
await Promise.all(
runningWorkspaces.map(async (info) => {
const wsi = info.latestInstance;
const req = new StopWorkspaceRequest();
req.setId(wsi.id);
req.setPolicy(StopWorkspacePolicy.NORMALLY);
try {
const manager = await this.workspaceManagerClientProvider.get(wsi.region);
await manager.stopWorkspace({}, req);
} catch (err) {
log.debug(
{
userId: info.workspace.ownerId,
workspaceId: info.workspace.id,
instanceId: wsi.id,
},
"Unable to stop workspace on account deletion",
err,
);
// We cannot find a workspace manager client for this instances' region, or there is any other error while stopping that workspace properly.
// This might be the case if the workspace cluster which we try to stop an instance on is no longer registered.
// This is bad state, but might happen anyway. Instead of failing, we mark the workspace instance as stopped.
await this.workspaceDb.updateInstancePartial(wsi.id, {
status: {
...wsi.status,
phase: "stopped",
},
});
}
}),
);
}
protected anonymizeUser(user: User) {
user.avatarUrl = "deleted-avatarUrl";
user.fullName = "deleted-fullName";
user.name = "deleted-Name";
if (user.verificationPhoneNumber) {
user.verificationPhoneNumber = "deleted-phoneNumber";
}
}
protected deleteIdentities(user: User) {
for (const identity of user.identities) {
identity.deleted = true; // This triggers the HARD DELETION of the identity
}
}
protected async deleteTokens(db: UserDB, user: User) {
const tokenDeletions = user.identities.map((identity) => db.deleteTokens(identity));
await Promise.all(tokenDeletions);
}
protected async anonymizeAllWorkspaces(userId: string) {
const workspaces = await this.workspaceDb.findWorkspacesByUser(userId);
await Promise.all(
workspaces.map(async (ws) => {
this.anonymizeWorkspace(ws);
await this.workspaceDb.store(ws);
}),
);
}
protected async deleteUserBucket(userId: string) {
try {
await this.storageClient.deleteUserContent(userId);
} catch (error) {
log.error({ userId }, "Failed to delete user bucket.", error);
}
}
protected async deleteTeamMemberships(userId: string) {
const teams = await this.teamDb.findTeamsByUser(userId);
await Promise.all(teams.map((t) => this.teamDb.removeMemberFromTeam(userId, t.id)));
}
protected async deleteSoleOwnedTeams(userId: string) {
const ownedTeams = await this.teamDb.findTeamsByUserAsSoleOwner(userId);
for (const team of ownedTeams) {
const teamProjects = await this.projectDb.findTeamProjects(team.id);
await Promise.all(teamProjects.map((project) => this.projectDb.markDeleted(project.id)));
}
await Promise.all(ownedTeams.map((t) => this.teamDb.deleteTeam(t.id)));
}
protected async deleteUserProjects(id: string) {
const userProjects = await this.projectDb.findUserProjects(id);
await Promise.all(userProjects.map((project) => this.projectDb.markDeleted(project.id)));
}
anonymizeWorkspace(ws: Workspace) {
ws.context.title = "deleted-title";
ws.context.normalizedContextURL = "deleted-normalizedContextURL";
ws.contextURL = "deleted-contextURL";
ws.description = "deleted-description";
ws.context = {} as any;
}
}