/
step_create_ssh_key.go
109 lines (94 loc) · 2.8 KB
/
step_create_ssh_key.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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package sakuracloud
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"os"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
"golang.org/x/crypto/ssh"
)
type stepCreateSSHKey struct {
Debug bool
DebugKeyPath string
}
func (s *stepCreateSSHKey) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(Config)
stepStartMsg(ui, s.Debug, "CreateSSHKey")
var privateKeys []string
if c.Comm.SSHPrivateKeyFile != "" {
bytes, err := os.ReadFile(c.Comm.SSHPrivateKeyFile)
if err != nil {
err := fmt.Errorf("Error reading ssh key file: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
privateKeys = append(privateKeys, string(bytes))
}
if len(c.Comm.SSHPrivateKey) > 0 {
privateKeys = append(privateKeys, string(c.Comm.SSHPrivateKey))
}
if c.Comm.SSHPrivateKeyFile == "" && len(c.Comm.SSHPrivateKey) == 0 {
ui.Say("\tCreating temporary SSH key for instance...")
pkey, err := s.generatePrivateKey()
if err != nil {
err := fmt.Errorf("Error creating temporary ssh key: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
privateKeys = append(privateKeys, pkey)
state.Put("privateKey", pkey)
}
if !c.DisableGeneratePublicKey {
// generate public keys for SakuraCloud Disk Edit API
var publicKeys []string
if len(c.Comm.SSHPublicKey) > 0 {
publicKeys = append(publicKeys, string(c.Comm.SSHPublicKey))
}
for _, privateKey := range privateKeys {
signer, err := ssh.ParsePrivateKey([]byte(privateKey))
if err != nil {
err := fmt.Errorf("Error creating ssh public key: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
pubKey := string(ssh.MarshalAuthorizedKey(signer.PublicKey()))
publicKeys = append(publicKeys, pubKey)
}
state.Put("publicKeys", publicKeys)
}
if s.Debug {
pkey, ok := state.GetOk("privateKey")
if ok {
ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath))
if err := os.WriteFile(s.DebugKeyPath, []byte(pkey.(string)), 0600); err != nil {
state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
return multistep.ActionHalt
}
}
}
stepEndMsg(ui, s.Debug, "CreateSSHKey")
return multistep.ActionContinue
}
func (s *stepCreateSSHKey) Cleanup(state multistep.StateBag) {
// no cleanup
}
func (s *stepCreateSSHKey) generatePrivateKey() (string, error) {
priv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return "", err
}
privBlk := pem.Block{
Type: "RSA PRIVATE KEY",
Headers: nil,
Bytes: x509.MarshalPKCS1PrivateKey(priv),
}
return string(pem.EncodeToMemory(&privBlk)), nil
}