Skip to content
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

Skip location prompt if backend is unambiguous for apphosting:secrets:grantaccess #7060

Merged
merged 5 commits into from Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/commands/apphosting-secrets-grantaccess.ts
Expand Up @@ -7,11 +7,11 @@ import * as secretManager from "../gcp/secretManager";
import { requirePermissions } from "../requirePermissions";
import * as apphosting from "../gcp/apphosting";
import * as secrets from "../apphosting/secrets";
import { promptLocation } from "../apphosting";
import { getBackendForAmbiguousLocation } from "../apphosting";

export const command = new Command("apphosting:secrets:grantaccess <secretName>")
.description("grant service accounts permissions to the provided secret")
.option("-l, --location <location>", "backend location")
.option("-l, --location <location>", "backend location", "-")
.option("-b, --backend <backend>", "backend name")
.before(requireAuth)
.before(secretManager.ensureApi)
Expand All @@ -34,20 +34,24 @@ export const command = new Command("apphosting:secrets:grantaccess <secretName>"
);
}

let location = options.location as string;

location =
location || (await promptLocation(projectId, "Please select the location of your backend"));

// TODO: consider showing dialog if --backend is missing

const exists = await secretManager.secretExists(projectId, secretName);
if (!exists) {
throw new FirebaseError(`Cannot find secret ${secretName}`);
}

const backendId = options.backend as string;
const backend = await apphosting.getBackend(projectId, location, backendId);
const location = options.location as string;
let backend: apphosting.Backend;
if (location === "" || location === "-") {
backend = await getBackendForAmbiguousLocation(
projectId,
backendId,
"Please select the location of your backend:",
);
} else {
backend = await apphosting.getBackend(projectId, location, backendId);
}

const accounts = secrets.toMulti(secrets.serviceAccountsForBackend(projectNumber, backend));

await secrets.grantSecretAccess(projectId, projectNumber, secretName, accounts);
Expand Down
4 changes: 2 additions & 2 deletions src/test/apphosting/index.spec.ts
Expand Up @@ -227,7 +227,7 @@ describe("apphosting setup functions", () => {
});

it("returns a location selection", async () => {
const location = await promptLocation(projectId);
const location = await promptLocation(projectId, /* prompt= */ "");
expect(location).to.be.eq("us-central1");
});

Expand Down Expand Up @@ -305,7 +305,7 @@ describe("apphosting setup functions", () => {
getBackendForAmbiguousLocation(
projectId,
"foo",
/* prompt= */ "Please select the location of the backend you'd like to delete:",
"Please select the location of the backend you'd like to delete:",
),
).to.eventually.equal(backendFoo);

Expand Down