From 89e918fa6887238b0aad238a468b917728ca11fa Mon Sep 17 00:00:00 2001 From: Daniel Naab Date: Fri, 12 Jul 2024 07:25:01 -0500 Subject: [PATCH] Fix typing error in secrets package and organize its adapters into a separate subdirectory. --- .../src/lib/{ => adapters}/aws-param-store.ts | 4 +- .../src/lib/{ => adapters}/in-memory.ts | 2 +- packages/secrets/src/lib/adapters/index.ts | 43 +++++++++++++++++ packages/secrets/src/lib/index.ts | 48 +------------------ packages/secrets/src/lib/types.ts | 2 +- 5 files changed, 49 insertions(+), 50 deletions(-) rename packages/secrets/src/lib/{ => adapters}/aws-param-store.ts (97%) rename packages/secrets/src/lib/{ => adapters}/in-memory.ts (87%) create mode 100644 packages/secrets/src/lib/adapters/index.ts diff --git a/packages/secrets/src/lib/aws-param-store.ts b/packages/secrets/src/lib/adapters/aws-param-store.ts similarity index 97% rename from packages/secrets/src/lib/aws-param-store.ts rename to packages/secrets/src/lib/adapters/aws-param-store.ts index a3e6cc4a..3b1d4fca 100644 --- a/packages/secrets/src/lib/aws-param-store.ts +++ b/packages/secrets/src/lib/adapters/aws-param-store.ts @@ -8,7 +8,7 @@ import { ParameterNotFound, } from '@aws-sdk/client-ssm'; -import type { SecretKey, SecretMap, SecretValue, SecretsVault } from './types'; +import type { SecretKey, SecretMap, SecretValue, SecretsVault } from '../types'; export class AWSParameterStoreSecretsVault implements SecretsVault { async getSecret(key: SecretKey) { @@ -55,7 +55,7 @@ export class AWSParameterStoreSecretsVault implements SecretsVault { } } - async setSecret(key: string, value: string) { + async setSecret(key: SecretKey, value: SecretValue) { const client = new SSMClient(); try { await client.send( diff --git a/packages/secrets/src/lib/in-memory.ts b/packages/secrets/src/lib/adapters/in-memory.ts similarity index 87% rename from packages/secrets/src/lib/in-memory.ts rename to packages/secrets/src/lib/adapters/in-memory.ts index 4b829068..c6696a5f 100644 --- a/packages/secrets/src/lib/in-memory.ts +++ b/packages/secrets/src/lib/adapters/in-memory.ts @@ -1,4 +1,4 @@ -import type { SecretKey, SecretMap, SecretValue, SecretsVault } from './types'; +import type { SecretMap, SecretsVault } from '../types'; export class InMemorySecretsVault implements SecretsVault { constructor(private secretMap: SecretMap) {} diff --git a/packages/secrets/src/lib/adapters/index.ts b/packages/secrets/src/lib/adapters/index.ts new file mode 100644 index 00000000..40472729 --- /dev/null +++ b/packages/secrets/src/lib/adapters/index.ts @@ -0,0 +1,43 @@ +import { promises as fs } from 'fs'; + +import * as r from '@atj/common'; + +import { AWSParameterStoreSecretsVault } from './aws-param-store'; +import { getSecretMapFromJsonString, type SecretsVault } from '../types'; +import { InMemorySecretsVault } from './in-memory'; + +/** + * Returns either a production vault or an in-memory vault initialized with the + * contents of a JSON file. + * @param jsonFilePath Optional path to a local JSON file that will stand-in + * for a secrets vault. + * @returns In-memory or production vault. + */ +export const getSecretsVault = async (jsonFilePath?: string) => { + if (jsonFilePath) { + const maybeJsonString = (await fs.readFile(jsonFilePath)).toString(); + const result = createInMemorySecretsVault(maybeJsonString); + if (result.success) { + return result.data; + } else { + throw new Error(result.error); + } + } else { + return getAWSSecretsVault(); + } +}; + +export const getAWSSecretsVault = (): SecretsVault => { + return new AWSParameterStoreSecretsVault(); +}; + +export const createInMemorySecretsVault = ( + jsonString?: any +): r.Result => { + const result = getSecretMapFromJsonString(jsonString); + if (result.success) { + return r.success(new InMemorySecretsVault(result.data)); + } else { + return r.failure(result.error); + } +}; diff --git a/packages/secrets/src/lib/index.ts b/packages/secrets/src/lib/index.ts index 952a2c53..d26c7aaf 100644 --- a/packages/secrets/src/lib/index.ts +++ b/packages/secrets/src/lib/index.ts @@ -1,51 +1,7 @@ -import { promises as fs } from 'fs'; +import { type SecretMap, type SecretsVault } from './types'; -import * as r from '@atj/common'; - -import { AWSParameterStoreSecretsVault } from './aws-param-store'; -import { - getSecretMapFromJsonString, - type SecretMap, - type SecretsVault, -} from './types'; -import { InMemorySecretsVault } from './in-memory'; export { getSecretMapFromJsonString } from './types'; - -/** - * Returns either a production vault or an in-memory vault initialized with the - * contents of a JSON file. - * @param jsonFilePath Optional path to a local JSON file that will stand-in - * for a secrets vault. - * @returns In-memory or production vault. - */ -export const getSecretsVault = async (jsonFilePath?: string) => { - if (jsonFilePath) { - const maybeJsonString = (await fs.readFile(jsonFilePath)).toString(); - const result = createInMemorySecretsVault(maybeJsonString); - if (result.success) { - return result.data; - } else { - throw new Error(result.error); - } - } else { - return getAWSSecretsVault(); - } -}; - -export const getAWSSecretsVault = (): SecretsVault => { - return new AWSParameterStoreSecretsVault(); -}; - -export const createInMemorySecretsVault = ( - jsonString?: any -): r.Result => { - const result = getSecretMapFromJsonString(jsonString); - if (result.success) { - return r.success(new InMemorySecretsVault(result.data)); - } else { - return r.failure(result.error); - } -}; +export * from './adapters'; export const getSecretMap = async (vault: SecretsVault): Promise => { const secretKeys = await vault.getSecretKeys(); diff --git a/packages/secrets/src/lib/types.ts b/packages/secrets/src/lib/types.ts index 9ac9efce..9fe46528 100644 --- a/packages/secrets/src/lib/types.ts +++ b/packages/secrets/src/lib/types.ts @@ -2,7 +2,7 @@ import * as z from 'zod'; import { Result } from '@atj/common/src'; export type SecretKey = string; -export type SecretValue = string; +export type SecretValue = string | undefined; export type SecretMap = Record; const secretMap = z.record(z.string());