Skip to content
This repository was archived by the owner on Dec 29, 2019. It is now read-only.

Commit 8cbff8a

Browse files
committed
fresh .git folder to protect hard-coded secrets changed to env vars
0 parents  commit 8cbff8a

8 files changed

+996
-0
lines changed

.gitignore

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# Directory for instrumented libs generated by jscoverage/JSCover
15+
lib-cov
16+
17+
# Coverage directory used by tools like istanbul
18+
coverage
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24+
.grunt
25+
26+
# Bower dependency directory (https://bower.io/)
27+
bower_components
28+
29+
# node-waf configuration
30+
.lock-wscript
31+
32+
# Compiled binary addons (http://nodejs.org/api/addons.html)
33+
build/Release
34+
35+
# Dependency directories
36+
node_modules/
37+
jspm_packages/
38+
39+
# Typescript v1 declaration files
40+
typings/
41+
42+
# Optional npm cache directory
43+
.npm
44+
45+
# Optional eslint cache
46+
.eslintcache
47+
48+
# Optional REPL history
49+
.node_repl_history
50+
51+
# Output of 'npm pack'
52+
*.tgz
53+
54+
# Yarn Integrity file
55+
.yarn-integrity
56+
57+
# dotenv environment variables file
58+
.env
59+

.nvmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8.1.3

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# hss-notes-store-test-data
2+
test data uploader/generator for notes app
3+
4+
## Setup
5+
### Requirements
6+
`export AWS_DEFAULT_PROFILE='my-mfa-profile'`
7+
- [] aws cli install
8+
- [] .aws config file
9+
- [] AWS account access to another account thru assumedRole MFA
10+
- [] nvm install
11+
- [] global yarn install
12+
13+
AWS sdk for javascript doesn't provide the same support for assumedRoles with MFAs
14+
as Go sdk.
15+
16+
## Running the App
17+
In the command prompt, use the aws cli to trigger the MFA prompt:
18+
`aws ec2 describe-instances`

index.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const AWS = require('aws-sdk');
2+
const fs = require('fs');
3+
4+
const createNote = require('./src/create-note');
5+
6+
// Get AWS assumeRole to access sandbox account
7+
require('./src/assume-role-from-cli.js');
8+
AWS.config.update({region: 'us-west-2'});
9+
10+
function inputParams(table, obj) {
11+
const params = {
12+
TableName: table,
13+
Item: {
14+
ID: { S: obj.ID },
15+
Author: { S: obj.Author },
16+
Title: { S: obj.Title },
17+
Text: { S: obj.Text }
18+
}
19+
};
20+
return params;
21+
}
22+
23+
const ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
24+
function putNote(params) {
25+
return new Promise((resolve, reject) => {
26+
if (!params) return reject('Invalid Params');
27+
28+
ddb.putItem(params, (err, data) => {
29+
if (err) return reject(err);
30+
31+
// empty data without error is a success
32+
resolve(data);
33+
});
34+
});
35+
}
36+
37+
function populateNotes(cnt = 0) {
38+
if (cnt >= 100) {
39+
throw new Error('Create record count must be less than 100');
40+
}
41+
42+
for (let i = 0; i < cnt; i += 1) {
43+
const note = createNote();
44+
const table = 'Note';
45+
const params = inputParams(table, note);
46+
if (params) {
47+
putNote(params)
48+
.then(res => {
49+
console.log('success', res);
50+
})
51+
.catch(err => {
52+
console.log('err', err);
53+
});
54+
}
55+
}
56+
57+
}
58+
populateNotes(1);

package.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "hss-notes-store-test-data",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"repository": "https://github.com/harmonsoftwaresolutions/hss-notes-store-test-data.git",
6+
"author": "Evan Harmon <[email protected]>",
7+
"license": "none",
8+
"dependencies": {
9+
"aws-sdk": "^2.80.0",
10+
"faker": "^4.1.0",
11+
"ini": "^1.3.4"
12+
},
13+
"devDependencies": {
14+
"eslint": "^4.1.1"
15+
}
16+
}

src/assume-role-from-cli.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const AWS = require('aws-sdk');
2+
const ini = require('ini');
3+
const fs = require('fs');
4+
5+
fs.readFileSync(`${process.env.HOME}/.aws/config`, 'utf-8');
6+
7+
// SDK doesn't provide assumeRole MFA for other accounts in config like GO sdk
8+
const awsProfile = process.env.AWS_DEFAULT_PROFILE;
9+
if (awsProfile) {
10+
try {
11+
const HOME = process.env.HOME;
12+
const configFile = fs.readFileSync(`${HOME}/.aws/config`, 'utf-8');
13+
const configIni = ini.parse(configFile);
14+
const awsProfileConfig = configIni[`profile ${awsProfile}`];
15+
if (awsProfileConfig && awsProfileConfig.role_arn) {
16+
const roleArn = awsProfileConfig.role_arn.replace(/:/g, '_').replace(/[^A-Za-z0-9\-_]/g, '-');
17+
const awsCliCacheFilename = `${awsProfile}--${roleArn}`;
18+
const awsCliCachePath = `${HOME}/.aws/cli/cache/${awsCliCacheFilename}.json`;
19+
const cacheFile = fs.readFileSync(awsCliCachePath, 'utf-8');
20+
const awsCliCache = JSON.parse(cacheFile);
21+
22+
const sts = new AWS.STS();
23+
AWS.config.credentials = sts.credentialsFrom(awsCliCache, awsCliCache);
24+
}
25+
} catch (err) {
26+
console.log(err)
27+
}
28+
}

src/create-note.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const faker = require('faker');
2+
3+
module.exports = function createNote() {
4+
const note = {
5+
ID: faker.random.uuid(),
6+
Author: faker.name.firstName() + " " + faker.name.lastName(),
7+
Title: faker.lorem.words(),
8+
Text: faker.lorem.paragraph(),
9+
};
10+
return note;
11+
};

0 commit comments

Comments
 (0)