Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions kits/delete-user-data/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- fix: `firebase ext:migrate --non-interactive` no longer fails and rolls back. The kit had two settings the extension never had, `DISCOVERY_TOPIC_NAME` and `DELETION_TOPIC_NAME`, so `ext:migrate` had no value to write for them: a non-interactive migration failed with `no value for the following environment variables: DISCOVERY_TOPIC_NAME, DELETION_TOPIC_NAME`, and an interactive one stopped to ask for both. The settings are gone, and the topics are always `kit-<instance id>-discovery` and `kit-<instance id>-deletion`, as the extension named its topics from its instance id. If you set either one, it is now ignored and the functions move to these topics on the next deploy; delete the old topics once nothing is queued on them. `DeleteUserDataConfig` no longer takes `discoveryTopicName` or `deletionTopicName`.

- fix: the README now recommends `.env.<projectId>` for instance config and has an Uninstall section. `firebase functions:kits:uninstall` only deletes deployed resources for a project with a `.env.<projectId>` file, so with the plain `.env` the README suggested before, it removed the instance from `firebase.json` and exited without an error but left the functions running and deleting user data. The new section also covers the Pub/Sub topics and IAM role bindings that uninstall leaves behind.

- fix: `EXT_SELECTED_EVENTS` is validated at deploy, as the extension's install did. firebase-admin splits the list on commas without trimming, so `a, b, c` published only `a` and silently dropped the rest, and a type with different case or a typo was dropped without a warning. The deploy now fails if an entry has spaces or different case, isn't one of the three event types, or is listed twice. It also fails if event types are listed without `EVENTARC_CHANNEL`, or the channel isn't a full `projects/<project id>/locations/<location>/channels/<channel id>` name in this project and a location the extension supported (`us-central1`, `us-west1`, `europe-west4` or `asia-northeast1`); before, events were silently turned off. Empty entries are still skipped, and an unset or empty value still publishes nothing.
Expand Down
13 changes: 3 additions & 10 deletions kits/delete-user-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ overridden there.
| `searchFields` | `AUTO_DISCOVERY_SEARCH_FIELDS` | no | `id,uid,userId` | Fields treated as user ids |
| `searchFunction` | `SEARCH_FUNCTION` | no | (empty) | URL of a custom search function that returns paths to delete |
| — | `FUNCTION_DEFAULT_REGION` | no | `us-central1` | Region the functions deploy to |
| `discoveryTopicName` | `DISCOVERY_TOPIC_NAME` | no | `kit-<instance id>-discovery` | Pub/Sub discovery topic |
| `deletionTopicName` | `DELETION_TOPIC_NAME` | no | `kit-<instance id>-deletion` | Pub/Sub deletion topic |

## Multiple instances

Expand Down Expand Up @@ -232,9 +230,8 @@ fail with "contains no .env file for current project". Next to
Uninstalling the extension removed the same resources and kept its two Pub/Sub
topics. The kit also keeps two things:

- The topics `kit-<instance id>-discovery` and `kit-<instance id>-deletion` (or
the names in `DISCOVERY_TOPIC_NAME` and `DELETION_TOPIC_NAME`). Delete them
with `gcloud pubsub topics delete <topic>`.
- The topics `kit-<instance id>-discovery` and `kit-<instance id>-deletion`.
Delete them with `gcloud pubsub topics delete <topic>`.
- The deleted service account's seven project roles, which the extension
removed. They no longer grant access, and show in your project's IAM policy
as members like
Expand Down Expand Up @@ -272,11 +269,7 @@ Discovery and deletion topics are now `kit-<instance id>-discovery` and
`kit-<instance id>-deletion`, where the extension used an `ext-` prefix. The
Firebase CLI creates them for you on deploy, so there is no manual setup step,
but the old topics from an extension install are not reused and can be deleted
once you have migrated.

You can also override both names with `DISCOVERY_TOPIC_NAME` and
`DELETION_TOPIC_NAME`, which the extension did not allow. Change them together,
since one function publishes to a topic the other is triggered by.
once you have migrated. As in the extension, the names can't be changed.

### Functions deploy to `FUNCTION_DEFAULT_REGION`

Expand Down
21 changes: 8 additions & 13 deletions kits/delete-user-data/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
select,
storageBucket,
} from "firebase-functions/params";
import type { DeleteUserDataConfig } from "./export-config";
import { type DeleteUserDataConfig, topicName } from "./export-config";

// firebase-tools injects this for kit instances (set to the instance's key in
// firebase.json) during discovery, in the emulator, and on deployed functions.
Expand Down Expand Up @@ -172,20 +172,17 @@ const params = {
"Where do you want to deploy the functions created for this kit? You usually want a location close to your database.",
default: "us-central1",
}),
// Non-empty defaults so Pub/Sub trigger bindings resolve during deploy
// discovery without freezing an empty topic name into the manifest.
discoveryTopicName: defineString("DISCOVERY_TOPIC_NAME", {
default: `kit-${instanceId}-discovery`,
}),
deletionTopicName: defineString("DELETION_TOPIC_NAME", {
default: `kit-${instanceId}-deletion`,
}),
};

// Topic names are derived from the instance id, as the extension's were, rather
// than declared as params. The instance id is already set during deploy
// discovery, and a param here would be a setting the extension never had, so
// `ext:migrate` would have no value for it and a non-interactive migration
// would fail.
export const CONFIG_EXPRESSIONS = {
functionRegion: params.functionRegion,
discoveryTopicName: params.discoveryTopicName,
deletionTopicName: params.deletionTopicName,
discoveryTopicName: topicName(instanceId, "discovery"),
deletionTopicName: topicName(instanceId, "deletion"),
} as const;

function optional(value: string): string | undefined {
Expand Down Expand Up @@ -227,8 +224,6 @@ export function configFromEnv(): DeleteUserDataConfig {
searchFields: optional(params.searchFields.value()),
searchFunction: optional(params.searchFunction.value()),
instanceId,
discoveryTopicName: optional(params.discoveryTopicName.value()),
deletionTopicName: optional(params.deletionTopicName.value()),
projectId: projectID.value(),
};
}
Expand Down
29 changes: 12 additions & 17 deletions kits/delete-user-data/src/export-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ export interface DeleteUserDataConfig {
searchFields?: string;
searchFunction?: string;
instanceId: string;
discoveryTopicName?: string;
deletionTopicName?: string;
projectId?: string;
}

Expand All @@ -58,12 +56,17 @@ const DEFAULT_FIRESTORE_DATABASE_ID = "(default)";
const DEFAULT_FIRESTORE_DELETE_MODE: FirestoreDeleteMode = "shallow";
const DEFAULT_SEARCH_DEPTH = 3;
const DEFAULT_SEARCH_FIELDS = "id,uid,userId";
function topicName(
name: string | undefined,
instanceId: string,
suffix: string

/**
* The Pub/Sub topic an instance publishes to and is triggered by. The extension
* named its topics the same way from its instance id (with an `ext-` prefix)
* and offered no setting to change them.
*/
export function topicName(
instanceId: string | undefined,
suffix: "discovery" | "deletion"
): string {
return name ?? `kit-${instanceId}-${suffix}`;
return `kit-${instanceId}-${suffix}`;
}

export function resolveDeleteUserDataConfig(
Expand All @@ -86,16 +89,8 @@ export function resolveDeleteUserDataConfig(
searchFields: config.searchFields ?? DEFAULT_SEARCH_FIELDS,
searchFunction: config.searchFunction,
instanceId,
discoveryTopicName: topicName(
config.discoveryTopicName,
instanceId,
"discovery"
),
deletionTopicName: topicName(
config.deletionTopicName,
instanceId,
"deletion"
),
discoveryTopicName: topicName(instanceId, "discovery"),
deletionTopicName: topicName(instanceId, "deletion"),
projectId: config.projectId,
};
}
Expand Down
35 changes: 16 additions & 19 deletions kits/delete-user-data/tests/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ const select = vi.fn((options: Record<string, string>) => ({
},
}));

function cel(value: unknown): string {
return value instanceof FakeExpression ? value.toCEL() : String(value);
}

vi.mock("firebase-functions/params", () => ({
Expression: FakeExpression,
defineInt,
Expand Down Expand Up @@ -162,8 +158,6 @@ describe("configFromEnv", () => {
"STORAGE_PATHS",
"AUTO_DISCOVERY_SEARCH_FIELDS",
"SEARCH_FUNCTION",
"DISCOVERY_TOPIC_NAME",
"DELETION_TOPIC_NAME",
])
);
expect(defineInt.mock.calls).toContainEqual([
Expand Down Expand Up @@ -212,23 +206,26 @@ describe("configFromEnv", () => {
);
});

test("defaults the topic names to kit-{instanceId}-*", async () => {
test("derives the trigger topics from the instance id", async () => {
const { CONFIG_EXPRESSIONS } = await importConfig();

expect(cel(CONFIG_EXPRESSIONS.discoveryTopicName)).toBe(
"{{ params.DISCOVERY_TOPIC_NAME }}"
expect(CONFIG_EXPRESSIONS.discoveryTopicName).toBe(
"kit-test-instance-discovery"
);
expect(cel(CONFIG_EXPRESSIONS.deletionTopicName)).toBe(
"{{ params.DELETION_TOPIC_NAME }}"
expect(CONFIG_EXPRESSIONS.deletionTopicName).toBe(
"kit-test-instance-deletion"
);
expect(defineString.mock.calls).toContainEqual([
"DISCOVERY_TOPIC_NAME",
{ default: "kit-test-instance-discovery" },
]);
expect(defineString.mock.calls).toContainEqual([
"DELETION_TOPIC_NAME",
{ default: "kit-test-instance-deletion" },
]);
});

// The extension had no topic settings, so `ext:migrate` has no value to
// write for them, and a non-interactive migration fails on any param the
// CLI can't fill (#3260).
test("does not declare topic-name params", async () => {
await importConfig();

const declared = defineString.mock.calls.map(([name]) => name);
expect(declared).not.toContain("DISCOVERY_TOPIC_NAME");
expect(declared).not.toContain("DELETION_TOPIC_NAME");
});

test("defaults the storage bucket to the project bucket param", async () => {
Expand Down
11 changes: 0 additions & 11 deletions kits/delete-user-data/tests/export-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,6 @@ describe("resolveDeleteUserDataConfig", () => {
expect(config.deletionTopicName).toBe("kit-my-instance-deletion");
});

test("honours explicit topic names", () => {
const config = resolveDeleteUserDataConfig({
instanceId: "my-instance",
discoveryTopicName: "custom-discovery",
deletionTopicName: "custom-deletion",
});

expect(config.discoveryTopicName).toBe("custom-discovery");
expect(config.deletionTopicName).toBe("custom-deletion");
});

test("passes through the supplied values", () => {
const config = resolveDeleteUserDataConfig({
instanceId: "my-instance",
Expand Down
6 changes: 3 additions & 3 deletions kits/delete-user-data/tests/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ describe("search", () => {
expect(discoveryMessages(ctx)).toEqual([]);
});

test("publishes to the configured discovery topic", async () => {
test("publishes to the instance's discovery topic", async () => {
const firestore = createFakeFirestore({ "users/doc1": { foo: "bar" } });
const ctx = makeContext({
firestore,
config: {
discoveryTopicName: "custom-discovery",
instanceId: "my-instance",
projectId: "demo-test",
},
});
Expand All @@ -119,7 +119,7 @@ describe("search", () => {

expect(ctx.pubsub.published).toEqual([
{
topic: "projects/demo-test/topics/custom-discovery",
topic: "projects/demo-test/topics/kit-my-instance-discovery",
json: { path: "users", uid: UID, depth: 1 },
},
]);
Expand Down
Loading