@@ -167,73 +167,6 @@ func ConnectionE(t *testing.T, publicHostName, publicHostIP, privateHostName, pr
167
167
return output , err
168
168
}
169
169
170
- // connectToHostsWithMultipleUsers establishes SSH connections to a host using multiple user credentials.
171
- // It takes the public and private IP addresses and host names for two different users.
172
- // Returns two SSH clients for the respective users, along with any errors encountered during the process.
173
- func ConnectToHostsWithMultipleUsers (publicHostName , publicHostIP , privateHostName , privateHostIP string ) (* ssh.Client , * ssh.Client , error , error ) {
174
- // Get the SSH private key file path for the first user from the environment variable
175
- sshKeyFilePathUserOne := os .Getenv ("SSHFILEPATH" )
176
- // Check if the file exists
177
- if _ , err := os .Stat (sshKeyFilePathUserOne ); os .IsNotExist (err ) {
178
- return nil , nil , fmt .Errorf ("SSH private key file '%s' does not exist" , sshKeyFilePathUserOne ), nil
179
- } else if err != nil {
180
- return nil , nil , fmt .Errorf ("error checking SSH private key file: %v" , err ), nil
181
- }
182
- sshKeyUserOne , errUserOne := getSshKeyFile (sshKeyFilePathUserOne )
183
- if errUserOne != nil {
184
- return nil , nil , fmt .Errorf ("failed to get SSH key for user one: %w" , errUserOne ), nil
185
- }
186
-
187
- // Get the SSH private key file path for the second user from the environment variable
188
- sshKeyFilePathUserTwo := os .Getenv ("SSHFILEPATHTWO" )
189
- // Check if the file exists
190
- if _ , err := os .Stat (sshKeyFilePathUserTwo ); os .IsNotExist (err ) {
191
- return nil , nil , nil , fmt .Errorf ("SSH private key file '%s' does not exist" , sshKeyFilePathUserTwo )
192
- } else if err != nil {
193
- return nil , nil , nil , fmt .Errorf ("error checking SSH private key file: %v" , err )
194
- }
195
- sshKeyUserTwo , errUserTwo := getSshKeyFile (sshKeyFilePathUserTwo )
196
- if errUserTwo != nil {
197
- return nil , nil , nil , fmt .Errorf ("failed to get SSH key for user two: %w" , errUserTwo )
198
- }
199
-
200
- // Combine errors for better readability
201
- var combinedErrUserOne error
202
- if errUserOne != nil {
203
- combinedErrUserOne = fmt .Errorf ("user one SSH key error: %v" , errUserOne )
204
- }
205
- var combinedErrUserTwo error
206
- if errUserTwo != nil {
207
- combinedErrUserTwo = fmt .Errorf ("user two SSH key error: %v" , errUserTwo )
208
- }
209
-
210
- if combinedErrUserOne != nil && combinedErrUserTwo != nil {
211
- return nil , nil , combinedErrUserOne , combinedErrUserTwo
212
- }
213
-
214
- // Create SSH configurations for each user and host combination
215
- sshConfigUserOnePrivate := getSshConfig (sshKeyUserOne , privateHostName )
216
- sshConfigUserOnePublic := getSshConfig (sshKeyUserOne , publicHostName )
217
- sshConfigUserTwoPrivate := getSshConfig (sshKeyUserTwo , privateHostName )
218
- sshConfigUserTwoPublic := getSshConfig (sshKeyUserTwo , publicHostName )
219
-
220
- // Establish SSH connections for each user to the host
221
- clientUserOne , errUserOne := sshClientJumpHost (sshConfigUserOnePrivate , sshConfigUserOnePublic , publicHostIP + ":22" , privateHostIP + ":22" )
222
- clientUserTwo , errUserTwo := sshClientJumpHost (sshConfigUserTwoPrivate , sshConfigUserTwoPublic , publicHostIP + ":22" , privateHostIP + ":22" )
223
-
224
- // Combine errors for better readability
225
- var combinedErrClientUserOne error
226
- if errUserOne != nil {
227
- combinedErrClientUserOne = fmt .Errorf ("user one unable to log in to the node: %v" , errUserOne )
228
- }
229
- var combinedErrClientUserTwo error
230
- if errUserTwo != nil {
231
- combinedErrClientUserTwo = fmt .Errorf ("user two unable to log in to the node: %v" , errUserTwo )
232
- }
233
-
234
- return clientUserOne , clientUserTwo , combinedErrClientUserOne , combinedErrClientUserTwo
235
- }
236
-
237
170
func ConnectToHostAsLDAPUser (publicHostName , publicHostIP , privateHostIP , ldapUser , ldapPassword string ) (* ssh.Client , error ) {
238
171
239
172
sshFilePath := os .Getenv ("SSH_FILE_PATH" )
@@ -267,3 +200,65 @@ func ConnectToHostAsLDAPUser(publicHostName, publicHostIP, privateHostIP, ldapUs
267
200
}
268
201
return sClient , nil
269
202
}
203
+
204
+ // ConnectToHostsWithMultipleUsers establishes SSH connections to a host using multiple user credentials.
205
+ // It takes the public and private IP addresses and host names for two different users.
206
+ // Returns two SSH clients for the respective users, along with any errors encountered during the process.
207
+ func ConnectToHostsWithMultipleUsers (publicHostName , publicHostIP , privateHostName , privateHostIP string ) (* ssh.Client , * ssh.Client , error , error ) {
208
+
209
+ // Get the SSH private key file path for the first user from the environment variable
210
+ sshFilePath := os .Getenv ("SSH_FILE_PATH" )
211
+
212
+ // Check if the file exists
213
+ _ , err := os .Stat (sshFilePath )
214
+ if os .IsNotExist (err ) {
215
+ return nil , nil , nil , fmt .Errorf ("SSH private key file '%s' does not exist" , sshFilePath )
216
+ } else if err != nil {
217
+ return nil , nil , nil , fmt .Errorf ("error checking SSH private key file: %v" , err )
218
+ }
219
+
220
+ // Get the SSH key for the first user
221
+ key , err := getSshKeyFile (sshFilePath )
222
+ if err != nil {
223
+ return nil , nil , nil , fmt .Errorf ("failed to get SSH key: %w" , err )
224
+ }
225
+
226
+ // Create SSH configurations for the first user
227
+ sshConfigUserOnePrivate := getSshConfig (key , publicHostName )
228
+ sshConfigUserOnePublic := getSshConfig (key , privateHostName )
229
+
230
+ // Establish SSH connection for the first user
231
+ clientUserOne , combinedErrClientUserOne := sshClientJumpHost (sshConfigUserOnePrivate , sshConfigUserOnePublic , publicHostIP + ":22" , privateHostIP + ":22" )
232
+ if combinedErrClientUserOne != nil {
233
+ return nil , nil , nil , fmt .Errorf ("unable to log in to the node: %w" , combinedErrClientUserOne )
234
+ }
235
+
236
+ // Get the SSH private key file path for the second user from the environment variable
237
+ sshFilePathTwo := os .Getenv ("SSH_FILE_PATH_TWO" )
238
+
239
+ // Check if the file exists
240
+ _ , err = os .Stat (sshFilePathTwo )
241
+ if os .IsNotExist (err ) {
242
+ return nil , nil , nil , fmt .Errorf ("SSH private key file '%s' does not exist" , sshFilePathTwo )
243
+ } else if err != nil {
244
+ return nil , nil , nil , fmt .Errorf ("error checking SSH private key file: %v" , err )
245
+ }
246
+
247
+ // Get the SSH key for the second user
248
+ key2 , err2 := getSshKeyFile (sshFilePathTwo )
249
+ if err2 != nil {
250
+ return nil , nil , nil , fmt .Errorf ("failed to get SSH key: %w" , err2 )
251
+ }
252
+
253
+ // Create SSH configurations for the second user
254
+ sshConfigUserTwoPrivate := getSshConfig (key2 , publicHostName )
255
+ sshConfigUserTwoPublic := getSshConfig (key2 , privateHostName )
256
+
257
+ // Establish SSH connection for the second user
258
+ clientUserTwo , combinedErrClientUserTwo := sshClientJumpHost (sshConfigUserTwoPrivate , sshConfigUserTwoPublic , publicHostIP + ":22" , privateHostIP + ":22" )
259
+ if combinedErrClientUserTwo != nil {
260
+ return nil , nil , nil , fmt .Errorf ("unable to log in to the node: %w" , combinedErrClientUserTwo )
261
+ }
262
+
263
+ return clientUserOne , clientUserTwo , combinedErrClientUserOne , combinedErrClientUserTwo
264
+ }
0 commit comments