rds_ssm_connect 1.0.4 → 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.
- package/connect.js +102 -99
- package/package.json +2 -4
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
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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
|
-
//
|
|
96
|
-
const
|
|
97
|
-
const
|
|
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
|
-
|
|
100
|
-
|
|
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
|
-
});
|
|
114
|
+
portForwardingProcess.stdout.on('data', (data) => {
|
|
115
|
+
console.log(data.toString().trim());
|
|
118
116
|
});
|
|
119
117
|
|
|
120
|
-
|
|
118
|
+
portForwardingProcess.stderr.on('data', (data) => {
|
|
121
119
|
console.error(`Command execution error: ${data.toString()}`);
|
|
122
120
|
});
|
|
123
121
|
});
|
|
124
122
|
|
|
125
|
-
|
|
123
|
+
rdsEndpointProcess.stderr.on('data', (data) => {
|
|
126
124
|
console.error(`Command execution error: ${data.toString()}`);
|
|
127
125
|
});
|
|
128
126
|
});
|
|
129
127
|
|
|
130
|
-
|
|
128
|
+
instanceIdProcess.stderr.on('data', (data) => {
|
|
131
129
|
console.error(`Command execution error: ${data.toString()}`);
|
|
132
130
|
});
|
|
133
131
|
});
|
|
134
132
|
|
|
135
|
-
|
|
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
|
+
"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
|
-
"
|
|
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"
|