4 Commits

Author SHA1 Message Date
Morten Olsen
70f1cf9134 chore: updated dependencies 2025-10-03 14:59:39 +02:00
Morten Olsen
815ac86873 feat: support env expansion 2025-10-02 15:47:44 +02:00
Morten Olsen
14c4d5c386 fix: support more than 10 SSM parameters 2025-09-10 14:09:09 +02:00
Morten Olsen
00786d5508 chore: minor QoL improvements 2025-08-08 20:22:10 +02:00
9 changed files with 1427 additions and 1246 deletions

0
.npmrc Normal file
View File

View File

@@ -117,7 +117,7 @@ override the SSM-resolved values. To avoid this:
- Use `.env.with-ssm` instead of `.env` for SSM references - Use `.env.with-ssm` instead of `.env` for SSM references
- Or use environment variable substitution if your app supports it: - Or use environment variable substitution if your app supports it:
`${API_KEY:-SSM:/myapp/api-key}` `API_KEY=${API_KEY:-SSM:/myapp/api-key}`
### 🚀 Deployment Considerations ### 🚀 Deployment Considerations

View File

@@ -6,36 +6,37 @@
"license": "GPL-3.0", "license": "GPL-3.0",
"scripts": { "scripts": {
"test:lint": "eslint", "test:lint": "eslint",
"build": "ncc build src/start.ts -o dist", "build": "ncc build src/start.ts -s -o dist",
"build:dev": "ncc build src/start.ts -o dist --watch", "build:dev": "ncc build src/start.ts -s -o dist --watch",
"test:unit": "vitest --run --passWithNoTests", "test:unit": "vitest --run --passWithNoTests",
"test": "pnpm run \"/^test:/\"" "test": "pnpm run \"/^test:/\""
}, },
"packageManager": "pnpm@10.6.0", "packageManager": "pnpm@10.17.0",
"files": [ "files": [
"dist" "dist"
], ],
"devDependencies": { "devDependencies": {
"@aws-sdk/client-ssm": "^3.863.0", "@aws-sdk/client-ssm": "^3.901.0",
"@aws-sdk/client-sts": "^3.901.0",
"@dotenvx/dotenvx": "^1.51.0",
"@eslint/eslintrc": "3.3.1", "@eslint/eslintrc": "3.3.1",
"@eslint/js": "9.32.0", "@eslint/js": "9.36.0",
"@pnpm/find-workspace-packages": "6.0.9", "@pnpm/find-workspace-packages": "6.0.9",
"@types/node": "24.2.0", "@types/node": "24.6.2",
"@types/yargs": "^17.0.33", "@types/yargs": "^17.0.33",
"@vercel/ncc": "^0.38.3", "@vercel/ncc": "^0.38.4",
"@vitest/coverage-v8": "3.2.4", "@vitest/coverage-v8": "3.2.4",
"dotenv": "^17.2.1", "eslint": "9.36.0",
"eslint": "9.32.0",
"eslint-config-prettier": "10.1.8", "eslint-config-prettier": "10.1.8",
"eslint-plugin-import": "2.32.0", "eslint-plugin-import": "2.32.0",
"eslint-plugin-prettier": "5.5.4", "eslint-plugin-prettier": "5.5.4",
"execa": "^9.6.0", "execa": "^9.6.0",
"prettier": "3.6.2", "prettier": "3.6.2",
"typescript": "5.9.2", "typescript": "5.9.3",
"typescript-eslint": "8.39.0", "typescript-eslint": "8.45.0",
"vitest": "3.2.4", "vitest": "3.2.4",
"yargs": "^18.0.0" "yargs": "^18.0.0"
}, },
"name": "@morten-olsen/with-ssm", "name": "@morten-olsen/with-ssm",
"version": "1.0.0" "version": "1.0.0"
} }

2484
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -5,48 +5,41 @@ import { exec } from './utils/exec.js';
import { getEnv } from './utils/env.js'; import { getEnv } from './utils/env.js';
import { replaceParams } from './utils/ssm.js'; import { replaceParams } from './utils/ssm.js';
const main = async () => { const argv = await yargs(hideBin(process.argv))
const argv = await yargs(hideBin(process.argv)) .usage('Usage: $0 [options] -- <command>')
.usage('Usage: $0 [options] -- <command>') .option('region', {
.option('region', { type: 'string',
type: 'string', description: 'The AWS region to use for SSM.',
description: 'The AWS region to use for SSM.', })
}) .option('profile', {
.option('profile', { type: 'string',
type: 'string', description: 'The AWS profile to use from your credentials file.',
description: 'The AWS profile to use from your credentials file.', })
}) .option('file', {
.option('file', { alias: 'f',
alias: 'f', type: 'string',
type: 'string', description: 'The file to use for environment variables. (multiple files can be specified)',
description: 'The file to use for environment variables. (multiple files can be specified)', default: ['.env', '.env.with-ssm'],
default: ['.env', '.env.with-ssm'], })
}) .demandCommand(1, 'Error: You must provide a command to execute after --')
.demandCommand(1, 'Error: You must provide a command to execute after --') .alias('h', 'help')
.alias('h', 'help') .epilogue('For more information, check the documentation.')
.epilogue('For more information, check the documentation.') .parse();
.parse();
const command = argv._[0] as string; const command = argv._[0] as string;
const commandArgs = argv._.slice(1).map(String); const commandArgs = argv._.slice(1).map(String);
if (!command) { if (!command) {
console.error('No command provided'); console.error('No command provided');
process.exit(1);
}
const files = argv.file && Array.isArray(argv.file) ? argv.file : [argv.file];
const hostEnv = await getEnv(files);
const env = await replaceParams(hostEnv);
exec({
command,
env,
args: commandArgs,
});
};
main().catch((err) => {
console.error(err);
process.exit(1); process.exit(1);
}
const files = argv.file && Array.isArray(argv.file) ? argv.file : [argv.file];
const hostEnv = await getEnv(files);
const env = await replaceParams(hostEnv);
exec({
command,
env,
args: commandArgs,
}); });

20
src/utils/aws.ts Normal file
View File

@@ -0,0 +1,20 @@
import { STSClient, GetCallerIdentityCommand } from '@aws-sdk/client-sts';
const ensureAWS = async (region?: string, profile?: string) => {
const sts = new STSClient({
region,
profile,
});
const command = new GetCallerIdentityCommand({});
try {
await sts.send(command);
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
console.error('Failed to get caller identity', errorMessage);
process.exit(1);
}
};
export { ensureAWS };

View File

@@ -1,14 +0,0 @@
const splitArgs = (args: string[]) => {
const separatorIndex = args.indexOf('--');
const actionArgs = args.slice(0, separatorIndex);
const command = args[separatorIndex + 1];
const commandArgs = args.slice(separatorIndex + 2);
return {
actionArgs,
command,
commandArgs,
};
};
export { splitArgs };

View File

@@ -2,7 +2,7 @@ import { existsSync } from 'node:fs';
import { readFile } from 'node:fs/promises'; import { readFile } from 'node:fs/promises';
import { resolve } from 'node:path'; import { resolve } from 'node:path';
import { parse } from 'dotenv'; import { parse } from '@dotenvx/dotenvx';
import { debug } from './debug.js'; import { debug } from './debug.js';

View File

@@ -1,6 +1,7 @@
import { GetParametersCommand, SSMClient } from '@aws-sdk/client-ssm'; import { GetParametersCommand, SSMClient, type Parameter } from '@aws-sdk/client-ssm';
import { debug } from './debug.js'; import { debug } from './debug.js';
import { ensureAWS } from './aws.js';
const PREFIX = 'SSM:'; const PREFIX = 'SSM:';
@@ -13,11 +14,6 @@ const replaceParams = async (
env: Record<string, string | undefined>, env: Record<string, string | undefined>,
{ region, profile }: ReplaceParamsOptions = {}, { region, profile }: ReplaceParamsOptions = {},
) => { ) => {
const ssm = new SSMClient({
region,
profile,
});
const names = Object.entries(env) const names = Object.entries(env)
.filter(([, value]) => value?.startsWith(PREFIX)) .filter(([, value]) => value?.startsWith(PREFIX))
.map(([, value]) => value?.slice(PREFIX.length)) .map(([, value]) => value?.slice(PREFIX.length))
@@ -30,18 +26,47 @@ const replaceParams = async (
return env; return env;
} }
const command = new GetParametersCommand({ await ensureAWS(region, profile);
Names: names, const ssm = new SSMClient({
WithDecryption: true, region,
profile,
}); });
// Chunk names into groups of 10 (AWS SSM GetParametersCommand limit)
const chunks: string[][] = [];
debug(`Chunking ${names.length} names into groups of 10`);
for (let i = 0; i < names.length; i += 10) {
chunks.push(names.slice(i, i + 10));
}
const response = await ssm.send(command); debug(`Processing ${chunks.length} chunks`);
if (response.InvalidParameters?.length || 0 > 0) {
console.error('Invalid SSM parameters', response.InvalidParameters); // Fetch parameters in chunks and combine results
const allParams: Parameter[] = [];
const allInvalidParams: string[] = [];
for (const chunk of chunks) {
const command = new GetParametersCommand({
Names: chunk,
WithDecryption: true,
});
const response = await ssm.send(command);
if (response.Parameters) {
allParams.push(...response.Parameters);
}
if (response.InvalidParameters) {
allInvalidParams.push(...response.InvalidParameters);
}
}
if (allInvalidParams.length > 0) {
console.error('Invalid SSM parameters', allInvalidParams);
process.exit(1); process.exit(1);
} }
const params = response.Parameters ?? []; const params = allParams;
return Object.fromEntries( return Object.fromEntries(
Object.entries(env).map(([key, value]) => { Object.entries(env).map(([key, value]) => {