rds_ssm_connect 1.0.4 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintrc.yml ADDED
@@ -0,0 +1,8 @@
1
+ env:
2
+ es2021: true
3
+ node: true
4
+ extends: standard
5
+ parserOptions:
6
+ ecmaVersion: latest
7
+ sourceType: module
8
+ rules: {}
@@ -12,6 +12,8 @@ jobs:
12
12
  node-version: '18.x'
13
13
  registry-url: 'https://registry.npmjs.org'
14
14
  - run: npm ci
15
+ - name: Run lint
16
+ run: npm run lint
15
17
  - run: npm publish
16
18
  env:
17
19
  NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
package/connect.js CHANGED
@@ -1,138 +1,144 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  // Import necessary modules
4
- import { spawn } from 'child_process'; // For spawning child processes
5
- import inquirer from 'inquirer'; // For prompting the user for input
6
- import fs from 'fs'; // For reading files
7
- import os from 'os'; // For getting the user's home directory
8
- import path from 'path'; // For working with file paths
4
+ import { spawn } from 'child_process' // For spawning child processes
5
+ import inquirer from 'inquirer' // For prompting the user for input
6
+ import fs from 'fs' // For reading files
7
+ import os from 'os' // For getting the user's home directory
8
+ import path from 'path' // For working with file paths
9
9
 
10
10
  // Get the path to the AWS config file
11
- const awsConfigPath = path.join(os.homedir(), '.aws', 'config');
11
+ const awsConfigPath = path.join(os.homedir(), '.aws', 'config')
12
12
 
13
13
  // Read the contents of the AWS config file
14
- const awsConfig = fs.readFileSync(awsConfigPath, 'utf-8');
14
+ const awsConfig = fs.readFileSync(awsConfigPath, 'utf-8')
15
15
 
16
16
  // Extract environments from AWS config file
17
17
  const ENVS = awsConfig
18
18
  .split('\n')
19
- .filter(line => line.startsWith('[') && line.endsWith(']'))
20
- .map(line => line.slice(1, -1))
21
- .map(line => line.replace('profile ', ''));
22
-
23
- // Define a mapping of environment suffixes to port numbers
24
- const envPortMapping = {
25
- 'dev': '5433',
26
- 'stage': '5434',
27
- 'pre-prod': '5435',
28
- 'prod': '5436',
29
- };
30
-
31
- // Prompt the user to select an environment
32
- inquirer
33
- .prompt([
34
- {
35
- type: 'list',
36
- name: 'ENV',
37
- message: 'Please select the environment:',
38
- choices: ENVS,
39
- },
40
- ])
41
-
42
- .then((answers) => {
43
- const ENV = answers.ENV; // Get the selected environment from the user's answers
44
- console.log(`You selected: ${ENV}`);
45
-
46
- // Extract the environment suffix from the selected environment
47
- const envSuffix = ENV.split('-').pop();
48
-
49
- // Get the corresponding port number for the environment
50
- let portNumber = envPortMapping[envSuffix]; // Declare portNumber as a let variable
51
-
52
- // If no port number is found for the environment, default to 5432
53
- if (!portNumber) {
54
- console.error(`No port number found for environment: ${ENV}. Defaulting to 5432.`);
55
- portNumber = '5432';
56
- }
57
-
58
- // Set up the commands to run inside the aws-vault environment
59
- const awsVaultExecCommand = ['aws-vault', 'exec', ENV, '--'];
60
- const ssmDescribeCommand = 'aws ssm describe-parameters --region us-east-2 --query \'Parameters[?ends_with(Name, `/rds/rds-aurora-password`)].Name\' --output text | head -n 1';
61
-
62
- // Run the commands inside aws-vault environment
63
- const ssmDescribeProcess = spawn('sh', ['-c', `${awsVaultExecCommand.join(' ')} ${ssmDescribeCommand}`]);
64
-
65
- // Get the name of the parameter containing the RDS password
66
- ssmDescribeProcess.stdout.on('data', (data) => {
67
- const PARAM_NAME = data.toString().trim();
68
-
69
- // Get the RDS credentials
70
- const ssmGetCommand = `aws ssm get-parameter --region us-east-2 --name '${PARAM_NAME}' --with-decryption --query Parameter.Value --output text`;
71
- const ssmGetProcess = spawn('sh', ['-c', `${awsVaultExecCommand.join(' ')} ${ssmGetCommand}`]);
72
-
73
- // Parse the JSON output of the ssm get-parameter command to get the RDS credentials
74
- ssmGetProcess.stdout.on('data', (data) => {
75
- const CREDENTIALS = JSON.parse(data.toString());
76
- const USERNAME = CREDENTIALS.user; // Get the RDS username from the credentials
77
- const PASSWORD = CREDENTIALS.password; // Get the RDS password from the credentials
78
-
79
- // Display connection credentials and connection string
80
- console.log(`Your connection string is: psql -h localhost -p ${portNumber} -U ${USERNAME} -d emr`);
81
- console.log(`Use the password: ${PASSWORD}`);
82
-
83
- // Get the ID of the bastion instance
84
- const instanceIdCommand = `aws ec2 describe-instances --region us-east-2 --filters "Name=tag:Name,Values='*bastion*'" --query "Reservations[].Instances[].[InstanceId]" --output text`;
85
- const instanceIdProcess = spawn('sh', ['-c', `${awsVaultExecCommand.join(' ')} ${instanceIdCommand}`]);
86
-
87
- instanceIdProcess.stdout.on('data', (data) => {
88
- const INSTANCE_ID = data.toString().trim();
89
-
90
- if (!INSTANCE_ID) {
91
- console.error('Failed to find the instance with tag Name=*bastion*.');
92
- return;
19
+ .filter(line => line.startsWith('[') && line.endsWith(']'))
20
+ .map(line => line.slice(1, -1))
21
+ .map(line => line.replace('profile ', ''))
22
+
23
+ // Define a mapping of environment suffixes to port numbers
24
+ const envPortMapping = {
25
+ dev: '5433',
26
+ stage: '5434',
27
+ 'pre-prod': '5435',
28
+ prod: '5436'
29
+ }
30
+
31
+ // Define the table name
32
+ const TABLE_NAME = 'emr'
33
+
34
+ // Define the AWS region
35
+ const REGION = 'us-east-2'
36
+
37
+ // Prompt the user to select an environment
38
+ inquirer
39
+ .prompt([
40
+ {
41
+ type: 'list',
42
+ name: 'ENV',
43
+ message: 'Please select the environment:',
44
+ choices: ENVS
45
+ }
46
+ ])
47
+
48
+ .then((answers) => {
49
+ const ENV = answers.ENV // Get the selected environment from the user's answers
50
+ console.log(`You selected: ${ENV}`)
51
+
52
+ // Extract the environment suffix from the selected environment
53
+ const envSuffix = ENV.split('-').pop()
54
+
55
+ // Get the corresponding port number for the environment
56
+ let portNumber = envPortMapping[envSuffix] // Declare portNumber as a let variable
57
+
58
+ // If no port number is found for the environment, default to 5432
59
+ if (!portNumber) {
60
+ console.error(`No port number found for environment: ${ENV}. Defaulting to 5432.`)
61
+ portNumber = '5432'
62
+ }
63
+
64
+ // Set up the commands to run inside the aws-vault environment
65
+ const awsVaultExecCommand = ['aws-vault', 'exec', ENV, '--']
66
+ const ssmDescribeCommand = `aws ssm describe-parameters --region ${REGION} --query "Parameters[?ends_with(Name, '/rds/rds-aurora-password')].Name" --output text | head -n 1`
67
+
68
+ // Run the commands inside aws-vault environment
69
+ const ssmDescribeProcess = spawn('sh', ['-c', `${awsVaultExecCommand.join(' ')} ${ssmDescribeCommand}`])
70
+
71
+ // Get the name of the parameter containing the RDS password
72
+ ssmDescribeProcess.stdout.on('data', (data) => {
73
+ const PARAM_NAME = data.toString().trim()
74
+
75
+ // Get the RDS credentials
76
+ const ssmGetCommand = `aws ssm get-parameter --region ${REGION} --name '${PARAM_NAME}' --with-decryption --query Parameter.Value --output text`
77
+ const ssmGetProcess = spawn('sh', ['-c', `${awsVaultExecCommand.join(' ')} ${ssmGetCommand}`])
78
+
79
+ // Parse the JSON output of the ssm get-parameter command to get the RDS credentials
80
+ ssmGetProcess.stdout.on('data', (data) => {
81
+ const CREDENTIALS = JSON.parse(data.toString())
82
+ const USERNAME = CREDENTIALS.user // Get the RDS username from the credentials
83
+ const PASSWORD = CREDENTIALS.password // Get the RDS password from the credentials
84
+
85
+ // Display connection credentials and connection string
86
+ console.log(`Your connection string is: psql -h localhost -p ${portNumber} -U ${USERNAME} -d ${TABLE_NAME}`)
87
+ console.log(`Use the password: ${PASSWORD}`)
88
+
89
+ // Get the ID of the bastion instance
90
+ const instanceIdCommand = `aws ec2 describe-instances --region ${REGION} --filters "Name=tag:Name,Values='*bastion*'" --query "Reservations[].Instances[].[InstanceId]" --output text`
91
+ const instanceIdProcess = spawn('sh', ['-c', `${awsVaultExecCommand.join(' ')} ${instanceIdCommand}`])
92
+
93
+ instanceIdProcess.stdout.on('data', (data) => {
94
+ const INSTANCE_ID = data.toString().trim()
95
+
96
+ if (!INSTANCE_ID) {
97
+ console.error('Failed to find the instance with tag Name=*bastion*.')
98
+ return
99
+ }
100
+
101
+ // Get the endpoint of the RDS cluster
102
+ const rdsEndpointCommand = `aws rds describe-db-clusters --region ${REGION} --query "DBClusters[?contains(DBClusterIdentifier, 'rds-aurora')].Endpoint" --output text`
103
+ const rdsEndpointProcess = spawn('sh', ['-c', `${awsVaultExecCommand.join(' ')} ${rdsEndpointCommand}`])
104
+
105
+ rdsEndpointProcess.stdout.on('data', (data) => {
106
+ const RDS_ENDPOINT = data.toString().trim()
107
+
108
+ if (!RDS_ENDPOINT) {
109
+ console.error('Failed to find the RDS endpoint.')
110
+ return
93
111
  }
94
112
 
95
- // Get the endpoint of the RDS cluster
96
- const rdsEndpointCommand = `aws rds describe-db-clusters --region us-east-2 --query "DBClusters[?contains(DBClusterIdentifier, 'rds-aurora')].Endpoint" --output text`;
97
- const rdsEndpointProcess = spawn('sh', ['-c', `${awsVaultExecCommand.join(' ')} ${rdsEndpointCommand}`]);
98
-
99
- rdsEndpointProcess.stdout.on('data', (data) => {
100
- const RDS_ENDPOINT = data.toString().trim();
101
-
102
- if (!RDS_ENDPOINT) {
103
- console.error('Failed to find the RDS endpoint.');
104
- return;
105
- }
106
-
107
- // Start a port forwarding session to the RDS cluster
108
- const portForwardingCommand = `aws ssm start-session --target ${INSTANCE_ID} --document-name AWS-StartPortForwardingSessionToRemoteHost --parameters "host=${RDS_ENDPOINT},portNumber='5432',localPortNumber='${portNumber}'" --cli-connect-timeout 0`;
109
- const portForwardingProcess = spawn('sh', ['-c', `${awsVaultExecCommand.join(' ')} ${portForwardingCommand}`]);
110
-
111
- portForwardingProcess.stdout.on('data', (data) => {
112
- console.log(data.toString().trim());
113
- });
114
-
115
- portForwardingProcess.stderr.on('data', (data) => {
116
- console.error(`Command execution error: ${data.toString()}`);
117
- });
118
- });
119
-
120
- rdsEndpointProcess.stderr.on('data', (data) => {
121
- console.error(`Command execution error: ${data.toString()}`);
122
- });
123
- });
124
-
125
- instanceIdProcess.stderr.on('data', (data) => {
126
- console.error(`Command execution error: ${data.toString()}`);
127
- });
128
- });
129
-
130
- ssmGetProcess.stderr.on('data', (data) => {
131
- console.error(`Command execution error: ${data.toString()}`);
132
- });
133
- });
134
-
135
- ssmDescribeProcess.stderr.on('data', (data) => {
136
- console.error(`Command execution error: ${data.toString()}`);
137
- });
138
- });
113
+ // Start a port forwarding session to the RDS cluster
114
+ const portForwardingCommand = `aws ssm start-session --target ${INSTANCE_ID} --document-name AWS-StartPortForwardingSessionToRemoteHost --parameters "host=${RDS_ENDPOINT},portNumber='5432',localPortNumber='${portNumber}'" --cli-connect-timeout 0`
115
+ const portForwardingProcess = spawn('sh', ['-c', `${awsVaultExecCommand.join(' ')} ${portForwardingCommand}`])
116
+
117
+ portForwardingProcess.stdout.on('data', (data) => {
118
+ console.log(data.toString().trim())
119
+ })
120
+
121
+ portForwardingProcess.stderr.on('data', (data) => {
122
+ console.error(`Command execution error: ${data.toString()}`)
123
+ })
124
+ })
125
+
126
+ rdsEndpointProcess.stderr.on('data', (data) => {
127
+ console.error(`Command execution error: ${data.toString()}`)
128
+ })
129
+ })
130
+
131
+ instanceIdProcess.stderr.on('data', (data) => {
132
+ console.error(`Command execution error: ${data.toString()}`)
133
+ })
134
+ })
135
+
136
+ ssmGetProcess.stderr.on('data', (data) => {
137
+ console.error(`Command execution error: ${data.toString()}`)
138
+ })
139
+ })
140
+
141
+ ssmDescribeProcess.stderr.on('data', (data) => {
142
+ console.error(`Command execution error: ${data.toString()}`)
143
+ })
144
+ })
package/package.json CHANGED
@@ -1,16 +1,25 @@
1
1
  {
2
2
  "name": "rds_ssm_connect",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "type": "module",
5
5
  "dependencies": {
6
6
  "@aws-sdk/client-ec2": "^3.363.0",
7
7
  "@aws-sdk/client-rds": "^3.363.0",
8
8
  "@aws-sdk/client-ssm": "^3.363.0",
9
- "aws-sdk": "^2.1411.0",
10
- "inquirer": "^8.2.5",
11
- "node-jq": "^2.3.5"
9
+ "inquirer": "^8.2.5"
12
10
  },
13
11
  "bin": {
14
12
  "rds_ssm_connect": "./connect.js"
13
+ },
14
+ "scripts": {
15
+ "lint": "eslint .",
16
+ "lint:fix": "eslint . --fix"
17
+ },
18
+ "devDependencies": {
19
+ "eslint": "^8.44.0",
20
+ "eslint-config-standard": "^17.1.0",
21
+ "eslint-plugin-import": "^2.27.5",
22
+ "eslint-plugin-n": "^16.0.1",
23
+ "eslint-plugin-promise": "^6.1.1"
15
24
  }
16
25
  }