Skip to content

Commit

Permalink
fix: correctly fetch gcp project id
Browse files Browse the repository at this point in the history
  • Loading branch information
kormide committed Jan 2, 2024
1 parent 3ecac12 commit 09fdf46
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
1 change: 0 additions & 1 deletion e2e/stubs/cloud-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export class CloudFunctions implements StubbedServer {
SMTP_HOST: this.emailAccount.smtp.host,
SMTP_PORT: this.emailAccount.smtp.port.toString(),
NOTIFICATIONS_EMAIL: this.emailAccount.user,
GCP_PROJECT: "test-project",
SECRET_MANAGER_HOST: this.secrets.getHost(),
SECRET_MANAGER_PORT: this.secrets.getPort().toString(),
BAZEL_CENTRAL_REGISTRY: "bazelbuild/bazel-central-registry",
Expand Down
10 changes: 9 additions & 1 deletion src/infrastructure/secrets.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SecretManagerServiceClient } from "@google-cloud/secret-manager";
import gcpMetadata from "gcp-metadata";

export class SecretsClient {
private readonly googleSecretsClient;
Expand All @@ -19,7 +20,7 @@ export class SecretsClient {
}

public async accessSecret(name: string): Promise<string> {
const projectId = process.env.GCP_PROJECT;
const projectId = await getProjectIdOfExecutingCloudFunction();
const secretName = `projects/${projectId}/secrets/${name}/versions/latest`;

const [response] = await this.googleSecretsClient.accessSecretVersion({
Expand All @@ -30,3 +31,10 @@ export class SecretsClient {
return secret;
}
}

async function getProjectIdOfExecutingCloudFunction(): Promise<string> {
if (process.env.INTEGRATION_TESTING) {
return "test-project";
}
return await gcpMetadata.project("numeric-project-id");
}

0 comments on commit 09fdf46

Please sign in to comment.