All files / lib/packages/shared-utils secrets-manager.ts

100% Statements 12/12
100% Branches 6/6
50% Functions 1/2
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28              45x 45x   45x   45x 45x 1x     44x 44x 44x 4x   40x   5x      
import {
  SecretsManagerClient,
  GetSecretValueCommand,
  DescribeSecretCommand,
} from "@aws-sdk/client-secrets-manager";
 
export async function getSecret(secretId: string, region: string = "us-east-1"): Promise<string> {
  const client = new SecretsManagerClient({ region });
  try {
    // Check if the secret is marked for deletion
    const describeCommand = new DescribeSecretCommand({ SecretId: secretId });
 
    const secretMetadata = await client.send(describeCommand);
    if (secretMetadata.DeletedDate) {
      throw new Error(`Secret ${secretId} is marked for deletion and will not be used.`);
    }
 
    const command = new GetSecretValueCommand({ SecretId: secretId });
    const data = await client.send(command);
    if (!data.SecretString) {
      throw `Secret ${secretId} has no SecretString field present in response`;
    }
    return data.SecretString;
  } catch (error: unknown) {
    throw new Error(`Failed to fetch secret ${secretId}: ${error}`);
  }
}