Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add region param #260

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .envrc

This file was deleted.

2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* @adikari @ACloudGuru/principal-engineer-team
* @ACloudGuru/principal-engineering-team
3 changes: 1 addition & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
nodejs 16.16.0
direnv 2.28.0
nodejs 18.18.0
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,20 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [v6.0.0](2024-03-19)

### Added
- Supports AWS_REGION env var, with default fallback as `us-east-1`

### Fixed
- Dependencies updated and vulnerability in `"es6-weak-map": "^2.0.3"` removed

### Removed
- Dropped support for NodeJS `<18.18`
- `direnv` tool from tool-versions and `.envrc` file, redundant for linking node module binaries

## [v5.3.1](2022-05-16)

### Fixed
- using `-m` when there's no missing secrets doesn't crash anymore

Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Options:
-s, --stage [stage] Specify stage to run on. (required)
-c, --config [config] Path to oprah configuration (default: "oprah.yml")
-i, --interactive specify values through command line
-r, --removing [removing] Removing orphan configs or secrets (default: false)
-h, --help display help for command

Commands:
Expand Down Expand Up @@ -197,6 +198,17 @@ Options:
-h, --help display help for command
```

### Examples

Sample of current usage:

#### Fetching individual values

[BFF](https://github.com/ACloudGuru/school/blob/212fcc9fc983d6efb49b2a7c28ff0854ebb631c1/backend/services/bff/bin/push-schema#L54)
```
export APOLLO_KEY=$(yarn --silent oprah fetch -k ENGINE_API_KEY -s "$ACG_ENV" | jq -er ".ENGINE_API_KEY")
```

### License

Feel free to use the code, it's released using the MIT license.
Expand All @@ -209,3 +221,9 @@ Feel free to use the code, it's released using the MIT license.
[npm-url]: https://www.npmjs.com/package/oprah
[codacy-image]: https://api.codacy.com/project/badge/Grade/6464d14b26214357ba838d2cdbdfcb8e
[codacy-url]: https://www.codacy.com/app/subash.adhikari/oprah?utm_source=github.com&utm_medium=referral&utm_content=ACloudGuru/oprah&utm_campaign=Badge_Grade

## TODO:

- [ ] Remove init method - does nothing but validate providers, can be done in CLI definition
- [ ] Refactor for latest AWS SDK
- [ ] Set AWS SDK as peer dependency
84 changes: 48 additions & 36 deletions bin/oprah
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
const chalk = require('chalk');
const program = require('commander');

const pjson = require('../package.json');
const { version } = require('../package.json');
const { makeOprah } = require('../lib/make-oprah');
const { logError, log } = require('../lib/utils/logger');

let oprahPromise = null;

program
.version(pjson.version)
.version(version)
.option('-s, --stage [stage]', 'Specify stage to run on. (required)')
.option('-c, --config [config]', 'Path to oprah configuration', 'oprah.yml');
.option('-c, --config [config]', 'Path to oprah configuration', 'oprah.yml')
.option('--region [region]', 'AWS region to use (default: us-east-1)');

program
.command('run')
Expand All @@ -23,31 +24,43 @@ program
)
.option('-i, --interactive', 'Run on interactive mode')
.option('-m, --missing', 'Only prompt missing values in interactive mode')
.option('-r, --removing [removing]', 'Removing orphan configs or secrets')
.action(({ interactive, variables, missing, removing = false }) => {
let parsedVariables = {};

try {
if (variables) {
parsedVariables = JSON.parse(variables);
.option(
'-r, --removing [removing]',
'Removing orphan configs or secrets (default: false)'
)
.action(
({
interactive,
variables,
missing,
removing = false,
region = 'us-east-1'
}) => {
let parsedVariables = {};

try {
if (variables) {
parsedVariables = JSON.parse(variables);
}
} catch (error) {
logError(`Variables must be in JSON format!! ${error.message}.`);
program.help();
}
} catch (error) {
logError(`Variables must be in JSON format!! ${error.message}.`);
program.help();
}

const { stage, config } = program.opts();
const { stage, config } = program.opts();

const params = {
stage,
interactive,
missingOnly: missing,
config,
variables: parsedVariables
};
const params = {
stage,
interactive,
missingOnly: missing,
config,
variables: parsedVariables,
region
};

oprahPromise = makeOprah(params).run({ removing });
});
oprahPromise = makeOprah(params).run({ removing });
}
);

program
.command('init')
Expand Down Expand Up @@ -82,8 +95,8 @@ program
)
.option('-C, --config-only [configOnly]', 'Only export configs')
.action(({ path, target, configOnly = false }) => {
const { stage, config } = program.opts();
oprahPromise = makeOprah({ stage, config }).export(
const { stage, config, region } = program.opts();
oprahPromise = makeOprah({ stage, config, region }).export(
path || (target === 'env' ? '.env_oprah' : '/tmp/oprah-exports.json'),
target || 'json',
configOnly
Expand All @@ -100,8 +113,8 @@ program
'The location of the secrets and configuration file (default: "/tmp/oprah-exports.json")'
)
.action(({ path }) => {
const { stage, config } = program.opts();
oprahPromise = makeOprah({ stage, config }).import(
const { stage, config, region } = program.opts();
oprahPromise = makeOprah({ stage, config, region }).import(
path || '/tmp/oprah-exports.json'
);
});
Expand All @@ -110,20 +123,20 @@ program
.command('list')
.description('List all remote configurations and secrets.')
.action(() => {
const { stage, config } = program.opts();
oprahPromise = makeOprah({ stage, config }).list();
const { stage, config, region } = program.opts();
oprahPromise = makeOprah({ stage, config, region }).list();
});

program
.command('fetch')
.description('Fetch config or secret')
.option(
'-k, --keys [keys]',
'Comma seperated configs to fetch (example: "SOME_CONFIG,ANOTHER_CONFIG")'
'Comma separated configs to fetch (example: "SOME_CONFIG,ANOTHER_CONFIG")'
)
.action(({ keys }) => {
const { stage, config } = program.opts();
oprahPromise = makeOprah({ stage, config }).fetch({
const { stage, config, region } = program.opts();
oprahPromise = makeOprah({ stage, config, region }).fetch({
keys: keys.split(',')
});
});
Expand All @@ -133,15 +146,14 @@ program
.description('Cleaning up orphan configs or secrets')
.option('-d, --dry-run [dryRun]', 'Execute a dry run')
.action(({ dryRun }) => {
const { stage, config } = program.opts();
oprahPromise = makeOprah({ stage, config }).cleanUp({
const { stage, config, region } = program.opts();
oprahPromise = makeOprah({ stage, config, region }).cleanUp({
dryRun
});
});

function displayHelpAndExit() {
program.help();
process.exit(1);
}

program.on('command:*', () => {
Expand Down
2 changes: 1 addition & 1 deletion examples/ddb-configs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ config:
DB_TABLE: "some database table name for ${stage}"

secret:
keyId: arn:aws:kms:us-east-1:574504572869:key/65def1bd-e786-4334-a17f-4cc0af72fed3
keyId: arn:aws:kms:us-east-1:XXXXXXXXXXXX:key/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
path: /${stage}/secret
required:
DB_PASSWORD: "secret database password"
3 changes: 2 additions & 1 deletion examples/ssm-configs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ config:
DB_TABLE: "some database table name for ${stage}"

secret:
keyId: arn:aws:kms:us-east-1:574504572869:key/65def1bd-e786-4334-a17f-4cc0af72fed3
keyId: arn:aws:kms:us-east-1:XXXXXXXXXXXX:key/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
path: /${stage}/secret
required:
DB_PASSWORD: "secret database password"

9 changes: 9 additions & 0 deletions examples/ssm-simple-example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
service: example-service
provider:
name: ssm
config:
path: /${stage}/config
defaults:
OPTIONAL_CONFIG_EXAMPLE: optional-config-example-default-value
required:
REQUIRED_CONFIG_EXAMPLE: "provide a value in interactive mode"
15 changes: 15 additions & 0 deletions lib/services/parameter-store/stores/ssm/make-delete-parameters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const chunk = require('lodash/chunk');

const makeDeleteParameters = ({ ssm }) => ({ parameterNames }) => {
const chunks = chunk(parameterNames, 10);
const promises = chunks.map(chunkedParameterNames =>
ssm
.deleteParameters({
Names: chunkedParameterNames
})
.promise()
);
return Promise.all(promises);
};

module.exports = { makeDeleteParameters };
10 changes: 10 additions & 0 deletions lib/services/parameter-store/stores/ssm/make-ssm-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const AWS = require('aws-sdk');

const makeSsmClient = ({ region }) => {
AWS.config.update({ region });
return new AWS.SSM();
};

module.exports = {
makeSsmClient
};
2 changes: 1 addition & 1 deletion lib/services/settings/make-get-settings.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const get = require('lodash/get');
const deepMap = require('deep-map');
const { readFile } = require('./fs/readFile');
const { interpolate } = require('./interpolate');
const { getValidatedSettings } = require('./get-validated-settings');
const { getAccountId } = require('./get-account-id');
const { getRegion } = require('./get-region');
const { getOutputs } = require('../cf/get-outputs');
const { deepMap } = require('../../utils/deep-map');

const appendConfigParameters = settings => {
const path = get(settings, 'config.path');
Expand Down
18 changes: 18 additions & 0 deletions lib/utils/deep-map/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const deepMap = (obj, fn) => {
if (Array.isArray(obj)) {
return obj.map(value => deepMap(value, fn));
}

if (typeof obj === 'object' && obj !== null) {
return Object.keys(obj).reduce((acc, key) => {
acc[key] = deepMap(obj[key], fn);
return acc;
}, {});
}

return fn(obj);
};

module.exports = {
deepMap
};
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
{
"name": "oprah",
"version": "5.6.0",
"version": "6.0.0",
"description": "Package to deploy parameters to AWS",
"repository": "https://github.com/ACloudGuru/oprah.git",
"author": "subash adhikari <subash.adhikari@acloud.guru>",
"engines": {
"node": ">=12.14.0"
"node": ">=18.18.0"
},
"contributors": [
"DevOps Team <cloud-skills-devops@pluralsight.com>",
"Jaepil Kim <jaepil-kim@pluralsight.com>",
"Patrick Shiel <patrick-shiel@pluralsight.com>"
"Patrick Shiel <patrick-shiel@pluralsight.com>",
"Tim Kinnane <tim-kinnane@pluralsight.com>"
],
"license": "MIT",
"bin": {
Expand All @@ -19,6 +20,8 @@
"scripts": {
"lint": "yarn eslint .",
"test": "yarn jest --coverage",
"example:run": "bin/oprah -c examples/ssm-simple-example.yml run -i",
"example:list": "bin/oprah -c examples/ssm-simple-example.yml list",
"postinstall": "husky install",
"prepublishOnly": "pinst --disable",
"postpublish": "pinst --enable"
Expand All @@ -39,7 +42,6 @@
"commander": "^7.1.0",
"dataloader": "^2.0.0",
"deep-map": "^2.0.0",
"es6-weak-map": "^2.0.3",
"js-yaml": "^4.0.0",
"lodash": "^4.17.21",
"prompt": "^1.1.0",
Expand Down
Loading
Loading