Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change SSH RSA key by modern ED25519 key #8

Merged
merged 1 commit into from
Oct 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions builder/exoscale/step_create_ssh_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ package exoscale

import (
"context"
"crypto/ed25519"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"os"
"path/filepath"

egoscale "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/hashicorp/packer-plugin-sdk/communicator/sshkey"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
"golang.org/x/crypto/ssh"
)

type stepCreateSSHKey struct{}
Expand All @@ -41,37 +39,30 @@ func (s *stepCreateSSHKey) Run(ctx context.Context, state multistep.StateBag) mu

config.InstanceSSHKey = "packer-" + buildID

sshPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048)
publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
ui.Error(fmt.Sprintf("error generating SSH private key: %v", err))
return multistep.ActionHalt
}
if err = sshPrivateKey.Validate(); err != nil {
ui.Error(fmt.Sprintf("error generating SSH private key: %v", err))
return multistep.ActionHalt
}

sshPublicKey, err := ssh.NewPublicKey(&sshPrivateKey.PublicKey)
pair, err := sshkey.PairFromED25519(publicKey, privateKey)
if err != nil {
ui.Error(fmt.Sprintf("error generating SSH public key: %v", err))
ui.Error(fmt.Sprintf("error creating temporary ssh key: %s", err))
return multistep.ActionHalt
}

_, err = exo.RegisterSSHKey(
ctx,
zone,
config.InstanceSSHKey,
string(ssh.MarshalAuthorizedKey(sshPublicKey)),
string(pair.Public),
)
if err != nil {
ui.Error(fmt.Sprintf("unable to register SSH key: %v", err))
return multistep.ActionHalt
}

config.Comm.SSHPrivateKey = pem.EncodeToMemory(&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(sshPrivateKey),
})
config.Comm.SSHPrivateKey = pair.Private

state.Put("delete_ssh_key", true) // Flag the key for deletion once the build is successfully completed.

Expand Down