-
Notifications
You must be signed in to change notification settings - Fork 3
/
keypair.go
60 lines (49 loc) · 1.58 KB
/
keypair.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
package main
import (
"fmt"
"os"
"runtime"
"github.com/Sirupsen/logrus"
"github.com/aws/aws-sdk-go/service/ec2"
)
func createKeyPair(ec2conn *ec2.EC2, tempKeyPairName string, keyPath string) error {
logrus.Debugf("Creating temporary keypair: %s", tempKeyPairName)
keyResp, err := ec2conn.CreateKeyPair(&ec2.CreateKeyPairInput{
KeyName: &tempKeyPairName})
if err != nil {
return fmt.Errorf("Error creating temporary keypair: %s", err)
}
// Set some state data for use in future
privateKey := *keyResp.KeyMaterial
// output the private key to the working directory
logrus.Infof("Saving key for debug purposes: %s", keyPath)
f, err := os.Create(keyPath)
if err != nil {
return fmt.Errorf("Error saving debug key: %s", err)
}
defer f.Close()
// Write the key out
if _, err := f.Write([]byte(privateKey)); err != nil {
return fmt.Errorf("Error saving debug key: %s", err)
}
// Chmod it so that it is SSH ready
if runtime.GOOS != "windows" {
if err := f.Chmod(0600); err != nil {
return fmt.Errorf("Error setting permissions of debug key: %s", err)
}
}
return nil
}
func deleteKeyPair(ec2conn *ec2.EC2, keyName string, keyPath string) error {
// Remove the keypair
logrus.Debug("Deleting temporary keypair...")
_, err := ec2conn.DeleteKeyPair(&ec2.DeleteKeyPairInput{KeyName: &keyName})
if err != nil {
return fmt.Errorf("Error cleaning up keypair. Please delete the key %s manually: %v", keyName, err)
}
// Also remove the physical key
if err := os.Remove(keyPath); err != nil {
return fmt.Errorf("Error removing debug key '%s': %s", keyPath, err)
}
return nil
}