rds_ssm_connect 1.0.3 → 1.0.5

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.
Files changed (3) hide show
  1. package/README.md +1 -2
  2. package/connect.js +102 -99
  3. package/package.json +2 -4
package/README.md CHANGED
@@ -54,5 +54,4 @@ After the user selects an environment, the application executes a series of AWS
54
54
  - Describing RDS DB clusters to get the endpoint of the RDS cluster.
55
55
  - Starting an AWS SSM session to forward a local port to the RDS cluster.
56
56
 
57
- The application logs the output of each command and any errors that occur.
58
-
57
+ The application logs the output of each command and any errors that occur.
package/connect.js CHANGED
@@ -16,123 +16,126 @@ const awsConfig = fs.readFileSync(awsConfigPath, 'utf-8');
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*.');
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
+ // Prompt the user to select an environment
35
+ inquirer
36
+ .prompt([
37
+ {
38
+ type: 'list',
39
+ name: 'ENV',
40
+ message: 'Please select the environment:',
41
+ choices: ENVS,
42
+ },
43
+ ])
44
+
45
+ .then((answers) => {
46
+ const ENV = answers.ENV; // Get the selected environment from the user's answers
47
+ console.log(`You selected: ${ENV}`);
48
+
49
+ // Extract the environment suffix from the selected environment
50
+ const envSuffix = ENV.split('-').pop();
51
+
52
+ // Get the corresponding port number for the environment
53
+ let portNumber = envPortMapping[envSuffix]; // Declare portNumber as a let variable
54
+
55
+ // If no port number is found for the environment, default to 5432
56
+ if (!portNumber) {
57
+ console.error(`No port number found for environment: ${ENV}. Defaulting to 5432.`);
58
+ portNumber = '5432';
59
+ }
60
+
61
+ // Set up the commands to run inside the aws-vault environment
62
+ const awsVaultExecCommand = ['aws-vault', 'exec', ENV, '--'];
63
+ 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';
64
+
65
+ // Run the commands inside aws-vault environment
66
+ const ssmDescribeProcess = spawn('sh', ['-c', `${awsVaultExecCommand.join(' ')} ${ssmDescribeCommand}`]);
67
+
68
+ // Get the name of the parameter containing the RDS password
69
+ ssmDescribeProcess.stdout.on('data', (data) => {
70
+ const PARAM_NAME = data.toString().trim();
71
+
72
+ // Get the RDS credentials
73
+ const ssmGetCommand = `aws ssm get-parameter --region us-east-2 --name '${PARAM_NAME}' --with-decryption --query Parameter.Value --output text`;
74
+ const ssmGetProcess = spawn('sh', ['-c', `${awsVaultExecCommand.join(' ')} ${ssmGetCommand}`]);
75
+
76
+ // Parse the JSON output of the ssm get-parameter command to get the RDS credentials
77
+ ssmGetProcess.stdout.on('data', (data) => {
78
+ const CREDENTIALS = JSON.parse(data.toString());
79
+ const USERNAME = CREDENTIALS.user; // Get the RDS username from the credentials
80
+ const PASSWORD = CREDENTIALS.password; // Get the RDS password from the credentials
81
+
82
+ // Display connection credentials and connection string
83
+ console.log(`Your connection string is: psql -h localhost -p ${portNumber} -U ${USERNAME} -d ${TABLE_NAME}`);
84
+ console.log(`Use the password: ${PASSWORD}`);
85
+
86
+ // Get the ID of the bastion instance
87
+ const instanceIdCommand = `aws ec2 describe-instances --region us-east-2 --filters "Name=tag:Name,Values='*bastion*'" --query "Reservations[].Instances[].[InstanceId]" --output text`;
88
+ const instanceIdProcess = spawn('sh', ['-c', `${awsVaultExecCommand.join(' ')} ${instanceIdCommand}`]);
89
+
90
+ instanceIdProcess.stdout.on('data', (data) => {
91
+ const INSTANCE_ID = data.toString().trim();
92
+
93
+ if (!INSTANCE_ID) {
94
+ console.error('Failed to find the instance with tag Name=*bastion*.');
95
+ return;
96
+ }
97
+
98
+ // Get the endpoint of the RDS cluster
99
+ const rdsEndpointCommand = `aws rds describe-db-clusters --region us-east-2 --query "DBClusters[?contains(DBClusterIdentifier, 'rds-aurora')].Endpoint" --output text`;
100
+ const rdsEndpointProcess = spawn('sh', ['-c', `${awsVaultExecCommand.join(' ')} ${rdsEndpointCommand}`]);
101
+
102
+ rdsEndpointProcess.stdout.on('data', (data) => {
103
+ const RDS_ENDPOINT = data.toString().trim();
104
+
105
+ if (!RDS_ENDPOINT) {
106
+ console.error('Failed to find the RDS endpoint.');
92
107
  return;
93
108
  }
94
109
 
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}`]);
110
+ // Start a port forwarding session to the RDS cluster
111
+ 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`;
112
+ const portForwardingProcess = spawn('sh', ['-c', `${awsVaultExecCommand.join(' ')} ${portForwardingCommand}`]);
98
113
 
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='${portNumber}',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
- });
114
+ portForwardingProcess.stdout.on('data', (data) => {
115
+ console.log(data.toString().trim());
118
116
  });
119
117
 
120
- rdsEndpointProcess.stderr.on('data', (data) => {
118
+ portForwardingProcess.stderr.on('data', (data) => {
121
119
  console.error(`Command execution error: ${data.toString()}`);
122
120
  });
123
121
  });
124
122
 
125
- instanceIdProcess.stderr.on('data', (data) => {
123
+ rdsEndpointProcess.stderr.on('data', (data) => {
126
124
  console.error(`Command execution error: ${data.toString()}`);
127
125
  });
128
126
  });
129
127
 
130
- ssmGetProcess.stderr.on('data', (data) => {
128
+ instanceIdProcess.stderr.on('data', (data) => {
131
129
  console.error(`Command execution error: ${data.toString()}`);
132
130
  });
133
131
  });
134
132
 
135
- ssmDescribeProcess.stderr.on('data', (data) => {
133
+ ssmGetProcess.stderr.on('data', (data) => {
136
134
  console.error(`Command execution error: ${data.toString()}`);
137
135
  });
138
136
  });
137
+
138
+ ssmDescribeProcess.stderr.on('data', (data) => {
139
+ console.error(`Command execution error: ${data.toString()}`);
140
+ });
141
+ });
package/package.json CHANGED
@@ -1,14 +1,12 @@
1
1
  {
2
2
  "name": "rds_ssm_connect",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
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"