Skip to content

Commit

Permalink
export works as a json output by default
Browse files Browse the repository at this point in the history
  • Loading branch information
gagoar committed May 13, 2020
1 parent 14eccb9 commit b45b308
Show file tree
Hide file tree
Showing 9 changed files with 5,321 additions and 12 deletions.
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v14.1.0
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,19 @@
"dateformat": "^3.0.3",
"debug": "^4.1.1",
"envalid": "^6.0.1",
"lodash.groupby": "^4.6.0",
"ora": "^4.0.4"
},
"devDependencies": {
"ajv-keywords": "^3.4.1",
"@types/dateformat": "^3.0.1",
"ts-node": "^8.10.1",
"@types/debug": "^4.1.5",
"@types/jest": "^25.2.1",
"@types/node": "^13.11.0",
"@typescript-eslint/eslint-plugin": "^2.27.0",
"@typescript-eslint/parser": "^2.27.0",
"@types/lodash.groupby": "^4.6.6",
"@zeit/ncc": "^0.22.1",
"ajv-keywords": "^3.4.1",
"eslint": "^6.0.0",
"eslint-config-prettier": "^6.10.1",
"husky": "^4.2.5",
Expand All @@ -112,6 +113,7 @@
"prettier-eslint-cli": "^5.0.0",
"pretty-quick": "^2.0.1",
"ts-jest": "^25.5.1",
"ts-node": "^8.10.1",
"tslib": "^1.11.1",
"typescript": "^3.8.3"
}
Expand Down
5 changes: 0 additions & 5 deletions src/actions/export.ts

This file was deleted.

65 changes: 65 additions & 0 deletions src/actions/exportAsTemplate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import SSM from 'aws-sdk/clients/ssm';
import groupBy from 'lodash.groupby';

import { Options } from "../types";
import { REGION, API_VERSION, MAX_RESULTS_FOR_PATH, Environment } from '../utils/constants';
import { normalizeSecretKey } from '../utils/normalizeSecretKey';
interface Input extends Options { templateName?: string, custom?: string };

const getParametersByPath = async (params: SSM.GetParametersByPathRequest, region: string): Promise<SSM.ParameterList> => {

const ssm = new SSM({ apiVersion: API_VERSION, region });
const { Parameters: parameters = [], NextToken } = await ssm.getParametersByPath(params).promise();

if (NextToken) {
const moreParameters = await getParametersByPath({ ...params, NextToken }, region);
return [...parameters, ...moreParameters];
} else {
return parameters;
}
}
const getKeys = async ({ prefix, region }: { prefix: string, region: string }) => {
const parameters = await getParametersByPath({
Path: `/${prefix}`,
MaxResults: MAX_RESULTS_FOR_PATH,
Recursive: true,
WithDecryption: true,
}, region);

const secretPairs = parameters.map(({ Name = '', Value: value = '' }) => {

const { name, environment } = normalizeSecretKey(Name, prefix);
return {
name,
environment,
value,
}
});
return groupBy(secretPairs, ({ environment }: { environment: string }) => environment);
}
export const exportAsTemplate = async ({ prefix, environment = Environment.all, region = REGION }: Input): Promise<string> => {

try {
const groupedSecrets = await getKeys({
prefix,
region,
});

const baseSecrets = groupedSecrets[Environment.all].reduce(((memo, secret) => {
return Object.keys(memo).length ? { ...memo, [secret.name]: secret.value } : { [secret.name]: secret.value };
}), {} as Record<string, string>);;

if (environment !== Environment.all && environment in groupedSecrets) {
const envSecrets: Record<string, string> = groupedSecrets[environment].reduce(((memo, secret) => {
return { ...memo, [secret.name]: secret.value };
}));

return JSON.stringify({ ...baseSecrets, ...envSecrets }, null, 2);
} else {
return JSON.stringify(baseSecrets, null, 2);
}
} catch (e) {
console.error('something went awfully wrong', e);
throw e;
}
};
3 changes: 1 addition & 2 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ export { listParameters } from './listParameters';
export { getParameter } from './getParameter';
export { setParameter } from './setParameter';
export { deleteParameter } from './deleteParameter';


export { exportAsTemplate } from './exportAsTemplate';
7 changes: 4 additions & 3 deletions src/bin/cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import program from 'commander';
import { listParameters, getParameter, setParameter, deleteParameter } from '../';
import { setAWSCredentials } from '../utils/setAWSCredentials';
import { listParameters, getParameter, setParameter, deleteParameter, exportAsTemplate } from '../';

interface Options { prefix: string, awsProfile?: string, environment?: string, awsRegion?: string, awsAccessKeyId?: string, awsSecretAccessKey?: string, awsSessionToken?: string }

Expand Down Expand Up @@ -92,12 +92,13 @@ program
.command('export [templateName]')
.description('export all keys, a template can be chosen out of the built ones or specify a --customTemplate, by default it exports to the shell')
.option('--customTemplate <path/to/the/custom/function.js>, pass the arguments to customTemplate that should be a js function exporting by default the handler')
.action(async (name: string, command: Command): Promise<void> => {
.action(async (templateName: string | undefined, command: Command): Promise<void> => {

const { params, credentials } = getGlobalOptions(command);

setAWSCredentials(credentials);
await deleteParameter({ ...params, name });
const response = await exportAsTemplate({ ...params, templateName });
console.log(response);
});

program.parse(process.argv);
Expand Down
6 changes: 6 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const API_VERSION = '2014-11-06';
export const REGION = 'us-east-1';
export const MAX_RESULTS = 50;
export const MAX_RESULTS_FOR_PATH = 10;
export const DATE_FORMAT = "dddd, mmmm dS, yyyy, h:MM:ss TT (Z)";

export const SUCCESS_SYMBOL = '💫';
Expand All @@ -11,4 +12,9 @@ export enum Environment {
staging = 'staging',
test = 'test',
all = 'all'
}

export enum Templates {
shell = 'shell',
terraform1 = 'terraform1',
}
Loading

0 comments on commit b45b308

Please sign in to comment.