diff --git a/hcloudimages/client.go b/hcloudimages/client.go index 7240ef7..1ead805 100644 --- a/hcloudimages/client.go +++ b/hcloudimages/client.go @@ -10,6 +10,7 @@ import ( "time" "github.com/hetznercloud/hcloud-go/v2/hcloud" + "github.com/hetznercloud/hcloud-go/v2/hcloud/exp/kit/sshutils" "golang.org/x/crypto/ssh" "github.com/apricote/hcloud-upload-image/hcloudimages/contextlogger" @@ -17,7 +18,6 @@ import ( "github.com/apricote/hcloud-upload-image/hcloudimages/internal/control" "github.com/apricote/hcloud-upload-image/hcloudimages/internal/labelutil" "github.com/apricote/hcloud-upload-image/hcloudimages/internal/randomid" - "github.com/apricote/hcloud-upload-image/hcloudimages/internal/sshkey" "github.com/apricote/hcloud-upload-image/hcloudimages/internal/sshsession" ) @@ -136,7 +136,7 @@ func (s *Client) Upload(ctx context.Context, options UploadOptions) (*hcloud.Ima // 1. Create SSH Key logger.InfoContext(ctx, "# Step 1: Generating SSH Key") - publicKey, privateKey, err := sshkey.GenerateKeyPair() + privateKey, publicKey, err := sshutils.GenerateKeyPair() if err != nil { return nil, fmt.Errorf("failed to generate temporary ssh key pair: %w", err) } diff --git a/hcloudimages/internal/sshkey/ssh_key.go b/hcloudimages/internal/sshkey/ssh_key.go deleted file mode 100644 index e5c497f..0000000 --- a/hcloudimages/internal/sshkey/ssh_key.go +++ /dev/null @@ -1,49 +0,0 @@ -// SPDX-License-Identifier: MIT -// From https://gitlab.com/hetznercloud/fleeting-plugin-hetzner/-/blob/0f60204582289c243599f8ca0f5be4822789131d/internal/utils/ssh_key.go -// Copyright (c) 2024 Hetzner Cloud GmbH - -package sshkey - -import ( - "crypto/ed25519" - "encoding/pem" - - "golang.org/x/crypto/ssh" -) - -func GenerateKeyPair() ([]byte, []byte, error) { - pub, priv, err := ed25519.GenerateKey(nil) - if err != nil { - return nil, nil, err - } - - pubBytes, err := encodePublicKey(pub) - if err != nil { - return nil, nil, err - } - - privBytes, err := encodePrivateKey(priv) - if err != nil { - return nil, nil, err - } - - return pubBytes, privBytes, nil -} - -func encodePublicKey(pub ed25519.PublicKey) ([]byte, error) { - sshPub, err := ssh.NewPublicKey(pub) - if err != nil { - return nil, err - } - - return ssh.MarshalAuthorizedKey(sshPub), nil -} - -func encodePrivateKey(priv ed25519.PrivateKey) ([]byte, error) { - privPem, err := ssh.MarshalPrivateKey(priv, "") - if err != nil { - return nil, err - } - - return pem.EncodeToMemory(privPem), nil -} diff --git a/hcloudimages/internal/sshkey/ssh_key_test.go b/hcloudimages/internal/sshkey/ssh_key_test.go deleted file mode 100644 index 926f4ed..0000000 --- a/hcloudimages/internal/sshkey/ssh_key_test.go +++ /dev/null @@ -1,29 +0,0 @@ -// SPDX-License-Identifier: MIT -// From https://gitlab.com/hetznercloud/fleeting-plugin-hetzner/-/blob/0f60204582289c243599f8ca0f5be4822789131d/internal/utils/ssh_key_test.go -// Copyright (c) 2024 Hetzner Cloud GmbH - -package sshkey - -import ( - "strings" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestGenerateSSHKeyPair(t *testing.T) { - pubBytes, privBytes, err := GenerateKeyPair() - assert.Nil(t, err) - - pub := string(pubBytes) - priv := string(privBytes) - - if !(strings.HasPrefix(priv, "-----BEGIN OPENSSH PRIVATE KEY-----\n") && - strings.HasSuffix(priv, "-----END OPENSSH PRIVATE KEY-----\n")) { - assert.Fail(t, "private key is invalid", priv) - } - - if !strings.HasPrefix(pub, "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA") { - assert.Fail(t, "public key is invalid", pub) - } -}