Skip to content

Commit

Permalink
Fix typing error in secrets package and organize its adapters into a …
Browse files Browse the repository at this point in the history
…separate subdirectory.
  • Loading branch information
danielnaab committed Jul 12, 2024
1 parent 8280a08 commit 89e918f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {}
Expand Down
43 changes: 43 additions & 0 deletions packages/secrets/src/lib/adapters/index.ts
Original file line number Diff line number Diff line change
@@ -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<SecretsVault> => {
const result = getSecretMapFromJsonString(jsonString);
if (result.success) {
return r.success(new InMemorySecretsVault(result.data));
} else {
return r.failure(result.error);
}
};
48 changes: 2 additions & 46 deletions packages/secrets/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -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<SecretsVault> => {
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<SecretMap> => {
const secretKeys = await vault.getSecretKeys();
Expand Down
2 changes: 1 addition & 1 deletion packages/secrets/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SecretKey, SecretValue>;

const secretMap = z.record(z.string());
Expand Down

0 comments on commit 89e918f

Please sign in to comment.