Skip to content

Commit

Permalink
refactor: consolidated secret loading code into one generic reusable …
Browse files Browse the repository at this point in the history
…function
  • Loading branch information
chapati23 committed Jul 23, 2024
1 parent 447baf6 commit cc89d9d
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 82 deletions.
24 changes: 0 additions & 24 deletions src/get-discord-webhook-url.ts

This file was deleted.

28 changes: 0 additions & 28 deletions src/get-quicknode-security-token.ts

This file was deleted.

31 changes: 31 additions & 0 deletions src/get-secret.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { SecretManagerServiceClient } from "@google-cloud/secret-manager";
import config from "./config.js";

/**
* Load a secret from Secret Manager
*/
export default async function getSecret(secretId: string): Promise<string> {
try {
const secretManager = new SecretManagerServiceClient();
const secretFullResourceName = `projects/${config.GCP_PROJECT_ID}/secrets/${secretId}/versions/latest`;
const [version] = await secretManager.accessSecretVersion({
name: secretFullResourceName,
});

const secret = version.payload?.data?.toString();

if (!secret) {
throw new Error(
`Secret '${secretId}' is empty or undefined. Please check the secret in Secret Manager.`,
);
}

return secret;
} catch (error) {
console.error(
`Failed to retrieve secret '${secretId}' from secret manager:`,
error,
);
throw error;
}
}
24 changes: 0 additions & 24 deletions src/get-telegram-bot-token.ts

This file was deleted.

5 changes: 3 additions & 2 deletions src/send-discord-notification.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EmbedBuilder, WebhookClient } from "discord.js";
import getDiscordWebhookUrl from "./get-discord-webhook-url.js";
import config from "./config";
import getSecret from "./get-secret.js";
import type { ProposalCreatedEvent } from "./types";

export default async function sendDiscordNotification(
Expand Down Expand Up @@ -30,7 +31,7 @@ export default async function sendDiscordNotification(
.setColor(0xa6e5f6);

const discordWebhookClient = new WebhookClient({
url: await getDiscordWebhookUrl(),
url: await getSecret(config.DISCORD_WEBHOOK_URL_SECRET_ID),
});

await discordWebhookClient.send({
Expand Down
4 changes: 2 additions & 2 deletions src/send-telegram-notification.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import config from "./config.js";
import getTelegramBotToken from "./get-telegram-bot-token";
import getSecret from "./get-secret.js";
import { ProposalCreatedEvent } from "./types";

export default async function sendTelegramNotification(
event: ProposalCreatedEvent,
txHash: string,
) {
const botToken = await getTelegramBotToken();
const botToken = await getSecret(config.TELEGRAM_BOT_TOKEN_SECRET_ID);
const botUrl = `https://api.telegram.org/bot${botToken}/sendMessage`;

const { title, description } = JSON.parse(event.args.description) as {
Expand Down
7 changes: 5 additions & 2 deletions src/validate-request-origin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import type { Request } from "@google-cloud/functions-framework";
import crypto from "crypto";
import getQuicknodeSecurityToken from "./get-quicknode-security-token";
import config from "./config";
import getSecret from "./get-secret";

export default async function validateRequestOrigin(req: Request) {
const quicknodeSecurityToken = await getQuicknodeSecurityToken();
const quicknodeSecurityToken = await getSecret(
config.QUICKNODE_SECURITY_TOKEN_SECRET_ID,
);
const givenSignature = req.headers["x-qn-signature"];
const nonce = req.headers["x-qn-nonce"];
const contentHash = req.headers["x-qn-content-hash"];
Expand Down

0 comments on commit cc89d9d

Please sign in to comment.