MCPcopy Create free account
hub / github.com/hashintel/hash / createVaultClient

Function createVaultClient

libs/@local/hash-backend-utils/src/vault.ts:353–409  ·  view source on GitHub ↗
({ logger }: { logger: Logger })

Source from the content-addressed store, hash-verified

351}
352
353export const createVaultClient = async ({ logger }: { logger: Logger }) => {
354 if (
355 !process.env.HASH_VAULT_HOST ||
356 !process.env.HASH_VAULT_PORT ||
357 !process.env.HASH_VAULT_MOUNT_PATH
358 ) {
359 logger.info(
360 "No HASH_VAULT_HOST, HASH_VAULT_PORT, or HASH_VAULT_MOUNT_PATH provided, skipping Vault client creation",
361 );
362 return undefined;
363 }
364
365 const secretMountPath = normalizeVaultMountPath(
366 process.env.HASH_VAULT_MOUNT_PATH,
367 );
368 if (!secretMountPath) {
369 logger.error(
370 `Invalid HASH_VAULT_MOUNT_PATH "${process.env.HASH_VAULT_MOUNT_PATH}": must contain only alphanumeric characters, underscores, hyphens, and periods (hyphens/periods not at start/end)`,
371 );
372 return undefined;
373 }
374
375 if (!process.env.HASH_VAULT_ROOT_TOKEN) {
376 logger.info("No Vault root token provided, attempting IAM auth");
377
378 try {
379 const login = await loginToVaultViaIam({
380 vaultAddr: `${process.env.HASH_VAULT_HOST}:${process.env.HASH_VAULT_PORT}`,
381 logger,
382 });
383
384 logger.info("Successfully logged in to Vault via IAM");
385
386 return new VaultClient({
387 endpoint: `${process.env.HASH_VAULT_HOST}:${process.env.HASH_VAULT_PORT}`,
388 token: login.client_token,
389 logger,
390 secretMountPath,
391 });
392 } catch (error) {
393 logger.error("Failed to login to Vault via IAM", { error });
394
395 return undefined;
396 }
397 }
398
399 logger.info(
400 "Creating Vault client with HASH_VAULT_ROOT_TOKEN from environment",
401 );
402
403 return new VaultClient({
404 endpoint: `${process.env.HASH_VAULT_HOST}:${process.env.HASH_VAULT_PORT}`,
405 token: process.env.HASH_VAULT_ROOT_TOKEN,
406 logger,
407 secretMountPath,
408 });
409};
410

Callers 2

mainFunction · 0.90
runFunction · 0.90

Calls 3

normalizeVaultMountPathFunction · 0.85
loginToVaultViaIamFunction · 0.85
infoMethod · 0.80

Tested by

no test coverage detected