Skip to content

Commit ac8f397

Browse files
Auto replace key pair (#2020)
* auto replace key pair * auto replace key pair * fix bugs * fix bugs * fix bugs * fix bugs
1 parent 63c455b commit ac8f397

File tree

1 file changed

+37
-35
lines changed

1 file changed

+37
-35
lines changed

cmd/nodecmd/create_aws.go

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package nodecmd
44

55
import (
66
"fmt"
7+
"os"
78
"os/exec"
89
"strings"
910

@@ -225,51 +226,52 @@ func createEC2Instances(ec2Svc map[string]*awsAPI.AwsCloud,
225226
if err != nil {
226227
return instanceIDs, elasticIPs, sshCertPath, keyPairName, err
227228
}
228-
if !keyPairExists {
229-
switch {
230-
case useSSHAgent:
231-
ux.Logger.PrintToUser("Using ssh agent identity %s to create key pair %s in AWS[%s]", sshIdentity, keyPairName[region], region)
232-
if err := ec2Svc[region].UploadSSHIdentityKeyPair(regionConf[region].Prefix, sshIdentity); err != nil {
233-
return instanceIDs, elasticIPs, sshCertPath, keyPairName, err
234-
}
235-
case !useSSHAgent && certInSSHDir:
236-
ux.Logger.PrintToUser("Default Key Pair named %s already exists on your .ssh directory but not on AWS", regionConf[region].Prefix)
237-
ux.Logger.PrintToUser("We need to create a new Key Pair in AWS as we can't find Key Pair named %s in AWS[%s]", regionConf[region].Prefix, region)
238-
keyPairName[region], err = promptKeyPairName(ec2Svc[region])
239-
if err != nil {
240-
return instanceIDs, elasticIPs, sshCertPath, keyPairName, err
241-
}
242-
if err := ec2Svc[region].CreateAndDownloadKeyPair(regionConf[region].Prefix, privKey); err != nil {
243-
return instanceIDs, elasticIPs, sshCertPath, keyPairName, err
244-
}
245-
case !useSSHAgent && !certInSSHDir:
246-
ux.Logger.PrintToUser(fmt.Sprintf("Creating new key pair %s in AWS[%s]", keyPairName, region))
247-
if err := ec2Svc[region].CreateAndDownloadKeyPair(regionConf[region].Prefix, privKey); err != nil {
248-
return instanceIDs, elasticIPs, sshCertPath, keyPairName, err
229+
if replaceKeyPair && !forMonitoring {
230+
// delete existing key pair on AWS console and download the newly created key pair file
231+
// in .ssh dir (will overwrite existing file in .ssh dir)
232+
if keyPairExists {
233+
if err := ec2Svc[region].DeleteKeyPair(regionConf[region].Prefix); err != nil {
234+
return instanceIDs, elasticIPs, sshCertPath, keyPairName, fmt.Errorf("unable to delete existing key pair %s in AWS console due to %w", regionConf[region].Prefix, err)
249235
}
250236
}
237+
if err = os.RemoveAll(privKey); err != nil {
238+
return instanceIDs, elasticIPs, sshCertPath, keyPairName, fmt.Errorf("unable to delete existing key pair file %s in .ssh dir due to %w", privKey, err)
239+
}
240+
if err := ec2Svc[region].CreateAndDownloadKeyPair(regionConf[region].Prefix, privKey); err != nil {
241+
return instanceIDs, elasticIPs, sshCertPath, keyPairName, err
242+
}
251243
} else {
252-
// keypair exists
253-
switch {
254-
case useSSHAgent:
255-
ux.Logger.PrintToUser("Using existing key pair %s in AWS[%s] via ssh-agent", keyPairName[region], region)
256-
case !useSSHAgent && certInSSHDir:
257-
ux.Logger.PrintToUser("Using existing key pair %s in AWS[%s]", keyPairName[region], region)
258-
case !useSSHAgent && !certInSSHDir:
259-
if replaceKeyPair {
260-
// delete default key pair in .ssh dir and recreate default key pair
261-
// in both AWS console and store it in .ssh dir
262-
privKey, err = app.GetSSHCertFilePath(keyPairName[region] + constants.CertSuffix)
244+
if !keyPairExists {
245+
switch {
246+
case useSSHAgent:
247+
ux.Logger.PrintToUser("Using ssh agent identity %s to create key pair %s in AWS[%s]", sshIdentity, keyPairName[region], region)
248+
if err := ec2Svc[region].UploadSSHIdentityKeyPair(regionConf[region].Prefix, sshIdentity); err != nil {
249+
return instanceIDs, elasticIPs, sshCertPath, keyPairName, err
250+
}
251+
case !useSSHAgent && certInSSHDir:
252+
ux.Logger.PrintToUser("Default Key Pair named %s already exists on your .ssh directory but not on AWS", regionConf[region].Prefix)
253+
ux.Logger.PrintToUser("We need to create a new Key Pair in AWS as we can't find Key Pair named %s in AWS[%s]", regionConf[region].Prefix, region)
254+
keyPairName[region], err = promptKeyPairName(ec2Svc[region])
263255
if err != nil {
264256
return instanceIDs, elasticIPs, sshCertPath, keyPairName, err
265257
}
266-
if err := ec2Svc[region].DeleteKeyPair(regionConf[region].Prefix); err != nil {
258+
if err := ec2Svc[region].CreateAndDownloadKeyPair(regionConf[region].Prefix, privKey); err != nil {
267259
return instanceIDs, elasticIPs, sshCertPath, keyPairName, err
268260
}
269-
if err := ec2Svc[region].CreateAndDownloadKeyPair(keyPairName[region], privKey); err != nil {
261+
case !useSSHAgent && !certInSSHDir:
262+
ux.Logger.PrintToUser(fmt.Sprintf("Creating new key pair %s in AWS[%s]", keyPairName, region))
263+
if err := ec2Svc[region].CreateAndDownloadKeyPair(regionConf[region].Prefix, privKey); err != nil {
270264
return instanceIDs, elasticIPs, sshCertPath, keyPairName, err
271265
}
272-
} else {
266+
}
267+
} else {
268+
// keypair exists
269+
switch {
270+
case useSSHAgent:
271+
ux.Logger.PrintToUser("Using existing key pair %s in AWS[%s] via ssh-agent", keyPairName[region], region)
272+
case !useSSHAgent && certInSSHDir:
273+
ux.Logger.PrintToUser("Using existing key pair %s in AWS[%s]", keyPairName[region], region)
274+
case !useSSHAgent && !certInSSHDir:
273275
ux.Logger.PrintToUser("Default Key Pair named %s already exists in AWS[%s]", keyPairName[region], region)
274276
ux.Logger.PrintToUser("We need to create a new Key Pair in AWS as we can't find Key Pair named %s in your .ssh directory", keyPairName[region])
275277
keyPairName[region], err = promptKeyPairName(ec2Svc[region])

0 commit comments

Comments
 (0)