@firebase-function-kits/delete-user-data 0.0.1 → 0.0.2-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -0
- package/README.md +151 -0
- package/lib/config.d.ts +22 -0
- package/lib/config.d.ts.map +1 -0
- package/lib/config.js +86 -0
- package/lib/config.js.map +1 -0
- package/lib/events.d.ts +18 -0
- package/lib/events.d.ts.map +1 -0
- package/lib/events.js +71 -0
- package/lib/events.js.map +1 -0
- package/lib/export-config.d.ts +55 -0
- package/lib/export-config.d.ts.map +1 -0
- package/lib/export-config.js +56 -0
- package/lib/export-config.js.map +1 -0
- package/lib/handlers.d.ts +35 -0
- package/lib/handlers.d.ts.map +1 -0
- package/lib/handlers.js +243 -0
- package/lib/handlers.js.map +1 -0
- package/lib/helpers.d.ts +19 -0
- package/lib/helpers.d.ts.map +1 -0
- package/lib/helpers.js +39 -0
- package/lib/helpers.js.map +1 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +112 -0
- package/lib/index.js.map +1 -0
- package/lib/lib.d.ts +23 -0
- package/lib/lib.d.ts.map +1 -0
- package/lib/lib.js +38 -0
- package/lib/lib.js.map +1 -0
- package/lib/logs.d.ts +26 -0
- package/lib/logs.d.ts.map +1 -0
- package/lib/logs.js +116 -0
- package/lib/logs.js.map +1 -0
- package/lib/recursiveDelete.d.ts +18 -0
- package/lib/recursiveDelete.d.ts.map +1 -0
- package/lib/recursiveDelete.js +37 -0
- package/lib/recursiveDelete.js.map +1 -0
- package/lib/runBatchPubSubDeletions.d.ts +27 -0
- package/lib/runBatchPubSubDeletions.d.ts.map +1 -0
- package/lib/runBatchPubSubDeletions.js +47 -0
- package/lib/runBatchPubSubDeletions.js.map +1 -0
- package/lib/runCustomSearchFunction.d.ts +18 -0
- package/lib/runCustomSearchFunction.d.ts.map +1 -0
- package/lib/runCustomSearchFunction.js +78 -0
- package/lib/runCustomSearchFunction.js.map +1 -0
- package/lib/search.d.ts +19 -0
- package/lib/search.d.ts.map +1 -0
- package/lib/search.js +29 -0
- package/lib/search.js.map +1 -0
- package/package.json +37 -4
- package/src/config.ts +98 -0
- package/src/events.ts +38 -0
- package/src/export-config.ts +112 -0
- package/src/handlers.ts +271 -0
- package/src/helpers.ts +42 -0
- package/src/index.ts +100 -0
- package/src/lib.ts +41 -0
- package/src/logs.ts +127 -0
- package/src/recursiveDelete.ts +42 -0
- package/src/runBatchPubSubDeletions.ts +66 -0
- package/src/runCustomSearchFunction.ts +50 -0
- package/src/search.ts +37 -0
- package/tests/config.test.ts +180 -0
- package/tests/export-config.test.ts +117 -0
- package/tests/fakes.ts +325 -0
- package/tests/handlers.test.ts +517 -0
- package/tests/helpers.test.ts +126 -0
- package/tests/lib.test.ts +40 -0
- package/tests/recursiveDelete.test.ts +102 -0
- package/tests/runBatchPubSubDeletions.test.ts +164 -0
- package/tests/runCustomSearchFunction.test.ts +125 -0
- package/tests/search.test.ts +127 -0
- package/tsconfig.json +18 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/index.js +0 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 Google LLC
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export type FirestoreDeleteMode = "recursive" | "shallow";
|
|
18
|
+
|
|
19
|
+
export interface DeleteUserDataConfig {
|
|
20
|
+
firestorePaths?: string;
|
|
21
|
+
firestoreDatabaseId?: string;
|
|
22
|
+
firestoreDeleteMode?: FirestoreDeleteMode;
|
|
23
|
+
rtdbInstance?: string;
|
|
24
|
+
rtdbLocation?: string;
|
|
25
|
+
rtdbPaths?: string;
|
|
26
|
+
storageBucket?: string;
|
|
27
|
+
storagePaths?: string;
|
|
28
|
+
enableAutoDiscovery?: boolean;
|
|
29
|
+
searchDepth?: number;
|
|
30
|
+
searchFields?: string;
|
|
31
|
+
searchFunction?: string;
|
|
32
|
+
instanceId: string;
|
|
33
|
+
discoveryTopicName?: string;
|
|
34
|
+
deletionTopicName?: string;
|
|
35
|
+
projectId?: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface ResolvedDeleteUserDataConfig {
|
|
39
|
+
firestorePaths?: string;
|
|
40
|
+
firestoreDatabaseId: string;
|
|
41
|
+
firestoreDeleteMode: FirestoreDeleteMode;
|
|
42
|
+
rtdbInstance?: string;
|
|
43
|
+
rtdbLocation?: string;
|
|
44
|
+
rtdbPaths?: string;
|
|
45
|
+
storageBucket?: string;
|
|
46
|
+
storagePaths?: string;
|
|
47
|
+
enableAutoDiscovery: boolean;
|
|
48
|
+
searchDepth: number;
|
|
49
|
+
searchFields: string;
|
|
50
|
+
searchFunction?: string;
|
|
51
|
+
instanceId: string;
|
|
52
|
+
discoveryTopicName: string;
|
|
53
|
+
deletionTopicName: string;
|
|
54
|
+
projectId?: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const DEFAULT_FIRESTORE_DATABASE_ID = "(default)";
|
|
58
|
+
const DEFAULT_FIRESTORE_DELETE_MODE: FirestoreDeleteMode = "shallow";
|
|
59
|
+
const DEFAULT_SEARCH_DEPTH = 3;
|
|
60
|
+
const DEFAULT_SEARCH_FIELDS = "id,uid,userId";
|
|
61
|
+
function topicName(
|
|
62
|
+
name: string | undefined,
|
|
63
|
+
instanceId: string,
|
|
64
|
+
suffix: string
|
|
65
|
+
): string {
|
|
66
|
+
return name ?? `kit-${instanceId}-${suffix}`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function resolveDeleteUserDataConfig(
|
|
70
|
+
config: DeleteUserDataConfig
|
|
71
|
+
): ResolvedDeleteUserDataConfig {
|
|
72
|
+
const instanceId = config.instanceId;
|
|
73
|
+
return {
|
|
74
|
+
firestorePaths: config.firestorePaths,
|
|
75
|
+
firestoreDatabaseId:
|
|
76
|
+
config.firestoreDatabaseId ?? DEFAULT_FIRESTORE_DATABASE_ID,
|
|
77
|
+
firestoreDeleteMode:
|
|
78
|
+
config.firestoreDeleteMode ?? DEFAULT_FIRESTORE_DELETE_MODE,
|
|
79
|
+
rtdbInstance: config.rtdbInstance,
|
|
80
|
+
rtdbLocation: config.rtdbLocation,
|
|
81
|
+
rtdbPaths: config.rtdbPaths,
|
|
82
|
+
storageBucket: config.storageBucket,
|
|
83
|
+
storagePaths: config.storagePaths,
|
|
84
|
+
enableAutoDiscovery: config.enableAutoDiscovery ?? false,
|
|
85
|
+
searchDepth: config.searchDepth ?? DEFAULT_SEARCH_DEPTH,
|
|
86
|
+
searchFields: config.searchFields ?? DEFAULT_SEARCH_FIELDS,
|
|
87
|
+
searchFunction: config.searchFunction,
|
|
88
|
+
instanceId,
|
|
89
|
+
discoveryTopicName: topicName(
|
|
90
|
+
config.discoveryTopicName,
|
|
91
|
+
instanceId,
|
|
92
|
+
"discovery"
|
|
93
|
+
),
|
|
94
|
+
deletionTopicName: topicName(
|
|
95
|
+
config.deletionTopicName,
|
|
96
|
+
instanceId,
|
|
97
|
+
"deletion"
|
|
98
|
+
),
|
|
99
|
+
projectId: config.projectId,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function getDatabaseUrl(
|
|
104
|
+
selectedDatabaseInstance: string | undefined,
|
|
105
|
+
selectedDatabaseLocation: string | undefined
|
|
106
|
+
): string | null {
|
|
107
|
+
if (!selectedDatabaseLocation || !selectedDatabaseInstance) return null;
|
|
108
|
+
if (selectedDatabaseLocation === "us-central1") {
|
|
109
|
+
return `https://${selectedDatabaseInstance}.firebaseio.com`;
|
|
110
|
+
}
|
|
111
|
+
return `https://${selectedDatabaseInstance}.${selectedDatabaseLocation}.firebasedatabase.app`;
|
|
112
|
+
}
|
package/src/handlers.ts
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 Google LLC
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import type * as admin from "firebase-admin";
|
|
18
|
+
import type { DocumentReference } from "firebase-admin/firestore";
|
|
19
|
+
import { FieldPath } from "firebase-admin/firestore";
|
|
20
|
+
import chunk from "lodash.chunk";
|
|
21
|
+
import * as events from "./events";
|
|
22
|
+
import { extractUserPaths, hasValidUserPath } from "./helpers";
|
|
23
|
+
import * as logs from "./logs";
|
|
24
|
+
import { recursiveDelete } from "./recursiveDelete";
|
|
25
|
+
import {
|
|
26
|
+
type PublisherContext,
|
|
27
|
+
runBatchPubSubDeletions,
|
|
28
|
+
} from "./runBatchPubSubDeletions";
|
|
29
|
+
import { runCustomSearchFunction } from "./runCustomSearchFunction";
|
|
30
|
+
import { search } from "./search";
|
|
31
|
+
|
|
32
|
+
export interface HandlerContext extends PublisherContext {
|
|
33
|
+
firestore: admin.firestore.Firestore;
|
|
34
|
+
storage: admin.storage.Storage;
|
|
35
|
+
database: admin.database.Database;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface DeleteMessageData {
|
|
39
|
+
paths: string[];
|
|
40
|
+
uid: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface SearchMessageData {
|
|
44
|
+
path: string;
|
|
45
|
+
depth: number;
|
|
46
|
+
uid: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export async function handleDeletion(
|
|
50
|
+
data: DeleteMessageData,
|
|
51
|
+
ctx: HandlerContext
|
|
52
|
+
): Promise<void> {
|
|
53
|
+
const { paths, uid } = data;
|
|
54
|
+
const batchArray: admin.firestore.WriteBatch[] = [];
|
|
55
|
+
const invalidPaths: string[] = [];
|
|
56
|
+
|
|
57
|
+
for (const chunkedPaths of chunk<string>(paths, 450)) {
|
|
58
|
+
const batch = ctx.firestore.batch();
|
|
59
|
+
for (const path of chunkedPaths) {
|
|
60
|
+
const docRef = ctx.firestore.doc(path);
|
|
61
|
+
const isValidPath = await hasValidUserPath(
|
|
62
|
+
docRef,
|
|
63
|
+
path,
|
|
64
|
+
uid,
|
|
65
|
+
ctx.config.searchFields
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
if (!isValidPath) {
|
|
69
|
+
invalidPaths.push(path);
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (ctx.config.firestoreDeleteMode === "recursive") {
|
|
73
|
+
await recursiveDelete(path, ctx.firestore);
|
|
74
|
+
} else {
|
|
75
|
+
batch.delete(docRef);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
batchArray.push(batch);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
await Promise.all(batchArray.map((batch) => batch.commit()));
|
|
82
|
+
|
|
83
|
+
if (invalidPaths.length > 0) {
|
|
84
|
+
logs.warnInvalidPaths(invalidPaths.length, uid);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
await events.publishDeletionEvent("firestore", {
|
|
88
|
+
uid,
|
|
89
|
+
documentPaths: paths,
|
|
90
|
+
invalidPaths,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export async function handleSearch(
|
|
95
|
+
data: SearchMessageData,
|
|
96
|
+
ctx: HandlerContext
|
|
97
|
+
): Promise<void> {
|
|
98
|
+
const { path, depth, uid } = data;
|
|
99
|
+
const nextDepth = depth + 1;
|
|
100
|
+
const collection = ctx.firestore.collection(path);
|
|
101
|
+
|
|
102
|
+
if (depth > ctx.config.searchDepth) return;
|
|
103
|
+
|
|
104
|
+
if (collection.id === uid) {
|
|
105
|
+
await recursiveDelete(path, ctx.firestore);
|
|
106
|
+
await events.publishDeletionEvent("firestore", {
|
|
107
|
+
uid,
|
|
108
|
+
collectionPath: collection.path,
|
|
109
|
+
});
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const documentReferences = await collection.listDocuments();
|
|
114
|
+
const documentReferencesToSearch: DocumentReference[] = [];
|
|
115
|
+
const pathsToDelete: string[] = [];
|
|
116
|
+
|
|
117
|
+
await Promise.all(
|
|
118
|
+
documentReferences.map(async (reference) => {
|
|
119
|
+
if (nextDepth <= ctx.config.searchDepth) {
|
|
120
|
+
await search(uid, nextDepth, ctx.firestore, ctx, reference);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (reference.id === uid) {
|
|
124
|
+
pathsToDelete.push(reference.path);
|
|
125
|
+
} else if (ctx.config.searchFields) {
|
|
126
|
+
documentReferencesToSearch.push(reference);
|
|
127
|
+
}
|
|
128
|
+
})
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
if (documentReferencesToSearch.length > 0) {
|
|
132
|
+
const snapshots = await ctx.firestore.getAll(...documentReferencesToSearch);
|
|
133
|
+
for (const snapshot of snapshots) {
|
|
134
|
+
if (snapshot.exists) {
|
|
135
|
+
for (const field of ctx.config.searchFields.split(",")) {
|
|
136
|
+
if (snapshot.get(new FieldPath(field)) === uid) {
|
|
137
|
+
pathsToDelete.push(snapshot.ref.path);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
await runBatchPubSubDeletions({ firestorePaths: pathsToDelete }, uid, ctx);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export async function handleClear(
|
|
148
|
+
uid: string,
|
|
149
|
+
ctx: HandlerContext
|
|
150
|
+
): Promise<void> {
|
|
151
|
+
logs.start(ctx.config);
|
|
152
|
+
|
|
153
|
+
const promises: Promise<void>[] = [];
|
|
154
|
+
if (ctx.config.firestorePaths) {
|
|
155
|
+
promises.push(clearFirestoreData(ctx.config.firestorePaths, uid, ctx));
|
|
156
|
+
} else {
|
|
157
|
+
logs.firestoreNotConfigured();
|
|
158
|
+
}
|
|
159
|
+
if (ctx.config.rtdbPaths) {
|
|
160
|
+
promises.push(clearDatabaseData(ctx.config.rtdbPaths, uid, ctx));
|
|
161
|
+
} else {
|
|
162
|
+
logs.rtdbNotConfigured();
|
|
163
|
+
}
|
|
164
|
+
if (ctx.config.storagePaths) {
|
|
165
|
+
promises.push(clearStorageData(ctx.config.storagePaths, uid, ctx));
|
|
166
|
+
} else {
|
|
167
|
+
logs.storageNotConfigured();
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
await Promise.all(promises);
|
|
171
|
+
|
|
172
|
+
if (ctx.config.enableAutoDiscovery) {
|
|
173
|
+
await search(uid, 1, ctx.firestore, ctx);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (ctx.config.searchFunction) {
|
|
177
|
+
await runCustomSearchFunction(uid, ctx);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
logs.complete(uid);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async function clearDatabaseData(
|
|
184
|
+
databasePaths: string,
|
|
185
|
+
uid: string,
|
|
186
|
+
ctx: HandlerContext
|
|
187
|
+
): Promise<void> {
|
|
188
|
+
logs.rtdbDeleting();
|
|
189
|
+
const paths = extractUserPaths(databasePaths, uid);
|
|
190
|
+
await Promise.all(
|
|
191
|
+
paths.map(async (path) => {
|
|
192
|
+
try {
|
|
193
|
+
logs.rtdbPathDeleting(path);
|
|
194
|
+
await ctx.database.ref(path).remove();
|
|
195
|
+
logs.rtdbPathDeleted(path);
|
|
196
|
+
} catch (err) {
|
|
197
|
+
logs.rtdbPathError(path, err as Error);
|
|
198
|
+
}
|
|
199
|
+
})
|
|
200
|
+
);
|
|
201
|
+
await events.publishDeletionEvent("database", { uid, paths });
|
|
202
|
+
logs.rtdbDeleted();
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async function clearStorageData(
|
|
206
|
+
storagePaths: string,
|
|
207
|
+
uid: string,
|
|
208
|
+
ctx: HandlerContext
|
|
209
|
+
): Promise<void> {
|
|
210
|
+
logs.storageDeleting();
|
|
211
|
+
const paths = extractUserPaths(storagePaths, uid);
|
|
212
|
+
await Promise.all(
|
|
213
|
+
paths.map(async (path) => {
|
|
214
|
+
const parts = path.split("/");
|
|
215
|
+
const bucketName = parts[0];
|
|
216
|
+
const bucket =
|
|
217
|
+
bucketName === "{DEFAULT}"
|
|
218
|
+
? ctx.storage.bucket(ctx.config.storageBucket)
|
|
219
|
+
: ctx.storage.bucket(bucketName);
|
|
220
|
+
const prefix = parts.slice(1).join("/");
|
|
221
|
+
try {
|
|
222
|
+
logs.storagePathDeleting(prefix);
|
|
223
|
+
await bucket.deleteFiles({ prefix });
|
|
224
|
+
logs.storagePathDeleted(prefix);
|
|
225
|
+
} catch (err) {
|
|
226
|
+
if ((err as { code?: number }).code === 404) {
|
|
227
|
+
logs.storagePath404(prefix);
|
|
228
|
+
} else {
|
|
229
|
+
logs.storagePathError(prefix, err as Error);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
})
|
|
233
|
+
);
|
|
234
|
+
await events.publishDeletionEvent("storage", { uid, paths });
|
|
235
|
+
logs.storageDeleted();
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
async function clearFirestoreData(
|
|
239
|
+
firestorePaths: string,
|
|
240
|
+
uid: string,
|
|
241
|
+
ctx: HandlerContext
|
|
242
|
+
): Promise<void> {
|
|
243
|
+
logs.firestoreDeleting();
|
|
244
|
+
const paths = extractUserPaths(firestorePaths, uid);
|
|
245
|
+
await Promise.all(
|
|
246
|
+
paths.map(async (path) => {
|
|
247
|
+
try {
|
|
248
|
+
const isRecursive = ctx.config.firestoreDeleteMode === "recursive";
|
|
249
|
+
if (!isRecursive) {
|
|
250
|
+
logs.firestorePathDeleting(path, false);
|
|
251
|
+
await ctx.firestore.runTransaction((transaction) => {
|
|
252
|
+
transaction.delete(ctx.firestore.doc(path));
|
|
253
|
+
return Promise.resolve();
|
|
254
|
+
});
|
|
255
|
+
logs.firestorePathDeleted(path, false);
|
|
256
|
+
} else {
|
|
257
|
+
logs.firestorePathDeleting(path, true);
|
|
258
|
+
await recursiveDelete(path, ctx.firestore);
|
|
259
|
+
logs.firestorePathDeleted(path, true);
|
|
260
|
+
}
|
|
261
|
+
} catch (err) {
|
|
262
|
+
logs.firestorePathError(path, err as Error);
|
|
263
|
+
}
|
|
264
|
+
})
|
|
265
|
+
);
|
|
266
|
+
await events.publishDeletionEvent("firestore", {
|
|
267
|
+
uid,
|
|
268
|
+
documentPaths: paths,
|
|
269
|
+
});
|
|
270
|
+
logs.firestoreDeleted();
|
|
271
|
+
}
|
package/src/helpers.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 Google LLC
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { type DocumentReference, FieldPath } from "firebase-admin/firestore";
|
|
18
|
+
|
|
19
|
+
export const hasValidUserPath = async (
|
|
20
|
+
ref: DocumentReference,
|
|
21
|
+
path: string,
|
|
22
|
+
uid: string,
|
|
23
|
+
searchFields: string
|
|
24
|
+
): Promise<boolean> => {
|
|
25
|
+
if (path.includes(uid)) return true;
|
|
26
|
+
if (searchFields.length === 0) return false;
|
|
27
|
+
|
|
28
|
+
const snapshot = await ref.get();
|
|
29
|
+
if (snapshot.exists) {
|
|
30
|
+
for (const field of searchFields.split(",").filter(Boolean)) {
|
|
31
|
+
const fieldValue = snapshot.get(new FieldPath(field));
|
|
32
|
+
if (typeof fieldValue === "string" && fieldValue.includes(uid)) {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return false;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const extractUserPaths = (paths: string, uid: string): string[] =>
|
|
42
|
+
paths.split(",").map((path) => path.replace(/{UID}/g, uid));
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2019 Google LLC
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { PubSub } from "@google-cloud/pubsub";
|
|
18
|
+
import * as admin from "firebase-admin";
|
|
19
|
+
import { getFirestore } from "firebase-admin/firestore";
|
|
20
|
+
import * as functionsV1 from "firebase-functions/v1";
|
|
21
|
+
import type { Role } from "firebase-functions/v2";
|
|
22
|
+
import { requiresRole } from "firebase-functions/v2";
|
|
23
|
+
import { onMessagePublished } from "firebase-functions/v2/pubsub";
|
|
24
|
+
import { CONFIG_EXPRESSIONS, configFromEnv } from "./config";
|
|
25
|
+
import * as events from "./events";
|
|
26
|
+
import { getDatabaseUrl, resolveDeleteUserDataConfig } from "./export-config";
|
|
27
|
+
import {
|
|
28
|
+
type HandlerContext,
|
|
29
|
+
handleClear,
|
|
30
|
+
handleDeletion as runDeletion,
|
|
31
|
+
handleSearch as runSearch,
|
|
32
|
+
} from "./handlers";
|
|
33
|
+
import * as logs from "./logs";
|
|
34
|
+
|
|
35
|
+
export * from "./lib";
|
|
36
|
+
|
|
37
|
+
const REQUIRED_ROLES: ReadonlyArray<Role> = [
|
|
38
|
+
"roles/datastore.owner",
|
|
39
|
+
"roles/firebasedatabase.admin",
|
|
40
|
+
"roles/storage.admin",
|
|
41
|
+
"roles/pubsub.admin",
|
|
42
|
+
// Gen2 event triggers need Eventarc receive and run.invoker on the function SA.
|
|
43
|
+
"roles/eventarc.eventReceiver",
|
|
44
|
+
"roles/run.invoker",
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
for (const role of REQUIRED_ROLES) {
|
|
48
|
+
requiresRole(role);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
let ctx: HandlerContext | undefined;
|
|
52
|
+
|
|
53
|
+
function getContext(): HandlerContext {
|
|
54
|
+
if (ctx) {
|
|
55
|
+
return ctx;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const resolved = resolveDeleteUserDataConfig(configFromEnv());
|
|
59
|
+
const databaseURL = getDatabaseUrl(
|
|
60
|
+
resolved.rtdbInstance,
|
|
61
|
+
resolved.rtdbLocation
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
if (admin.apps.length === 0) {
|
|
65
|
+
admin.initializeApp({
|
|
66
|
+
credential: admin.credential.applicationDefault(),
|
|
67
|
+
...(databaseURL ? { databaseURL } : {}),
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
events.setupEventChannel();
|
|
72
|
+
logs.init(resolved);
|
|
73
|
+
|
|
74
|
+
ctx = {
|
|
75
|
+
firestore: getFirestore(resolved.firestoreDatabaseId),
|
|
76
|
+
storage: admin.storage(),
|
|
77
|
+
database: admin.database(),
|
|
78
|
+
pubsub: new PubSub({ projectId: resolved.projectId }),
|
|
79
|
+
config: resolved,
|
|
80
|
+
};
|
|
81
|
+
return ctx;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export const clearData = functionsV1.auth.user().onDelete((user) => {
|
|
85
|
+
return handleClear(user.uid, getContext());
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
export const handleSearch = onMessagePublished(
|
|
89
|
+
{
|
|
90
|
+
topic: CONFIG_EXPRESSIONS.discoveryTopicName,
|
|
91
|
+
},
|
|
92
|
+
(event) => runSearch(event.data.message.json, getContext())
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
export const handleDeletion = onMessagePublished(
|
|
96
|
+
{
|
|
97
|
+
topic: CONFIG_EXPRESSIONS.deletionTopicName,
|
|
98
|
+
},
|
|
99
|
+
(event) => runDeletion(event.data.message.json, getContext())
|
|
100
|
+
);
|
package/src/lib.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 Google LLC
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export {
|
|
18
|
+
type DeleteUserDataConfig,
|
|
19
|
+
type FirestoreDeleteMode,
|
|
20
|
+
getDatabaseUrl,
|
|
21
|
+
type ResolvedDeleteUserDataConfig,
|
|
22
|
+
resolveDeleteUserDataConfig,
|
|
23
|
+
} from "./export-config";
|
|
24
|
+
export {
|
|
25
|
+
type DeleteMessageData,
|
|
26
|
+
type HandlerContext,
|
|
27
|
+
handleClear,
|
|
28
|
+
handleDeletion,
|
|
29
|
+
handleSearch,
|
|
30
|
+
type SearchMessageData,
|
|
31
|
+
} from "./handlers";
|
|
32
|
+
export { extractUserPaths, hasValidUserPath } from "./helpers";
|
|
33
|
+
export { recursiveDelete } from "./recursiveDelete";
|
|
34
|
+
export {
|
|
35
|
+
type DeletionPaths,
|
|
36
|
+
type PublisherContext,
|
|
37
|
+
publishSearch,
|
|
38
|
+
runBatchPubSubDeletions,
|
|
39
|
+
} from "./runBatchPubSubDeletions";
|
|
40
|
+
export { runCustomSearchFunction } from "./runCustomSearchFunction";
|
|
41
|
+
export { search } from "./search";
|
package/src/logs.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2019 Google LLC
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { logger } from "firebase-functions";
|
|
18
|
+
import type { ResolvedDeleteUserDataConfig } from "./export-config";
|
|
19
|
+
|
|
20
|
+
export const complete = (uid: string) => {
|
|
21
|
+
logger.log(`Successfully removed data for user: ${uid}`);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const firestoreDeleted = () => {
|
|
25
|
+
logger.log("Finished deleting user data from Cloud Firestore");
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const firestoreDeleting = () => {
|
|
29
|
+
logger.log("Deleting user data from Cloud Firestore");
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const firestoreNotConfigured = () => {
|
|
33
|
+
logger.log("Cloud Firestore paths are not configured, skipping");
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const firestorePathDeleted = (path: string, recursive: boolean) => {
|
|
37
|
+
logger.log(
|
|
38
|
+
`Deleted: '${path}' from Cloud Firestore ${
|
|
39
|
+
recursive ? "with recursive delete" : ""
|
|
40
|
+
}`
|
|
41
|
+
);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export const firestorePathDeleting = (path: string, recursive: boolean) => {
|
|
45
|
+
logger.log(
|
|
46
|
+
`Deleting: '${path}' from Cloud Firestore ${
|
|
47
|
+
recursive ? "with recursive delete" : ""
|
|
48
|
+
}`
|
|
49
|
+
);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const firestorePathError = (path: string, err: Error) => {
|
|
53
|
+
logger.error(`Error when deleting: '${path}' from Cloud Firestore`, err);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export const init = (config: ResolvedDeleteUserDataConfig) => {
|
|
57
|
+
logger.log("Initializing extension with configuration", config);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export const rtdbDeleted = () => {
|
|
61
|
+
logger.log("Finished deleting user data from the Realtime Database");
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export const rtdbDeleting = () => {
|
|
65
|
+
logger.log("Deleting user data from the Realtime Database");
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export const rtdbPathDeleted = (path: string) => {
|
|
69
|
+
logger.log(`Deleted: '${path}' from the Realtime Database`);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export const rtdbNotConfigured = () => {
|
|
73
|
+
logger.log("Realtime Database paths are not configured, skipping");
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export const rtdbPathDeleting = (path: string) => {
|
|
77
|
+
logger.log(`Deleting: '${path}' from the Realtime Database`);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export const rtdbPathError = (path: string, err: Error) => {
|
|
81
|
+
logger.error(
|
|
82
|
+
`Error when deleting: '${path}' from the Realtime Database`,
|
|
83
|
+
err
|
|
84
|
+
);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export const start = (config: ResolvedDeleteUserDataConfig) => {
|
|
88
|
+
logger.log("Started extension execution with configuration", config);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export const storageDeleted = () => {
|
|
92
|
+
logger.log("Finished deleting user data from Cloud Storage");
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export const storageDeleting = () => {
|
|
96
|
+
logger.log("Deleting user data from Cloud Storage");
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
export const storageNotConfigured = () => {
|
|
100
|
+
logger.log("Cloud Storage paths are not configured, skipping");
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
export const storagePathDeleted = (path: string) => {
|
|
104
|
+
logger.log(`Deleted: '${path}' from Cloud Storage`);
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export const storagePathDeleting = (path: string) => {
|
|
108
|
+
logger.log(`Deleting: '${path}' from Cloud Storage`);
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
export const storagePath404 = (path: string) => {
|
|
112
|
+
logger.log(`File: '${path}' does not exist in Cloud Storage, skipping`);
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
export const storagePathError = (path: string, err: Error) => {
|
|
116
|
+
logger.error(`Error deleting: '${path}' from Cloud Storage`, err);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
export const customFunctionError = (err: Error) => {
|
|
120
|
+
logger.error(`Call to custom hook function threw an error`, err);
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
export function warnInvalidPaths(invalidPathCount: number, uid: string) {
|
|
124
|
+
logger.warn(
|
|
125
|
+
`Attempted to delete ${invalidPathCount} invalid paths for deleted user ${uid}`
|
|
126
|
+
);
|
|
127
|
+
}
|