-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: consolidated secret loading code into one generic reusable …
…function
- Loading branch information
Showing
7 changed files
with
41 additions
and
82 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters