Skip to content

Commit

Permalink
Catch auth channel update on channel deletion (#3024)
Browse files Browse the repository at this point in the history
* wrap and catch error if deleting domain fails

* migrate gcp auth api to apiv2

* add changelog

* fix a word
  • Loading branch information
bkendall committed Jan 12, 2021
1 parent 79a3878 commit 87434cc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -0,0 +1 @@
- Catches errors while updating authorized domains when deleting channels, printing a warning instead of failing.
30 changes: 19 additions & 11 deletions src/commands/hosting-channel-delete.ts
@@ -1,20 +1,15 @@
import { bold, underline } from "cli-color";
import marked from "marked";

import { Command } from "../command";
import { consoleUrl, logLabeledSuccess, logLabeledWarning } from "../utils";
import { deleteChannel, normalizeName, getChannel, removeAuthDomain } from "../hosting/api";
import { promptOnce } from "../prompt";
import { requireHostingSite } from "../requireHostingSite";
import { requirePermissions } from "../requirePermissions";
import * as getProjectId from "../getProjectId";
import * as requireConfig from "../requireConfig";
import { logLabeledSuccess } from "../utils";
import { promptOnce } from "../prompt";
import { requireHostingSite } from "../requireHostingSite";

interface ChannelInfo {
target: string | null;
site: string;
url: string;
expireTime: string;
}
import logger from "../logger";

export default new Command("hosting:channel:delete <channelId>")
.description("delete a Firebase Hosting channel")
Expand Down Expand Up @@ -51,7 +46,20 @@ export default new Command("hosting:channel:delete <channelId>")

await deleteChannel(projectId, siteId, channelId);
if (channel) {
await removeAuthDomain(projectId, channel.url);
try {
await removeAuthDomain(projectId, channel.url);
} catch (e) {
logLabeledWarning(
"hosting:channel",
marked(
`Unable to remove channel domain from Firebase Auth. Visit the Firebase Console at ${consoleUrl(
projectId,
"/authentication/providers"
)}`
)
);
logger.debug("[hosting] unable to remove auth domain", e);
}
}

logLabeledSuccess(
Expand Down
44 changes: 27 additions & 17 deletions src/gcp/auth.ts
@@ -1,24 +1,34 @@
import * as api from "../api";
import { Client } from "../apiv2";
import { identityOrigin } from "../api";

const apiClient = new Client({ urlPrefix: identityOrigin, auth: true });

/**
* Returns the list of authorized domains.
* @param project project identifier.
* @return authorized domains.
*/
export async function getAuthDomains(project: string): Promise<string[]> {
const res = await api.request("GET", `/admin/v2/projects/${project}/config`, {
auth: true,
origin: api.identityOrigin,
});
return res?.body?.authorizedDomains;
const res = await apiClient.get<{ authorizedDomains: string[] }>(
`/admin/v2/projects/${project}/config`
);
return res.body.authorizedDomains;
}

/**
* Updates the list of authorized domains.
* @param project project identifier.
* @param authDomains full list of authorized domains.
* @return authorized domains.
*/
export async function updateAuthDomains(project: string, authDomains: string[]): Promise<string[]> {
const resp = await api.request(
"PATCH",
`/admin/v2/projects/${project}/config?update_mask=authorizedDomains`,
{
auth: true,
origin: api.identityOrigin,
data: {
authorizedDomains: authDomains,
},
}
const res = await apiClient.patch<
{ authorizedDomains: string[] },
{ authorizedDomains: string[] }
>(
`/admin/v2/projects/${project}/config`,
{ authorizedDomains: authDomains },
{ queryParams: { update_mask: "authorizedDomains" } }
);
return resp?.body?.authorizedDomains;
return res.body.authorizedDomains;
}

0 comments on commit 87434cc

Please sign in to comment.