mirror of
https://github.com/apricote/hcloud-upload-image.git
synced 2026-01-13 21:31:03 +00:00
refactor: drop custom package in favor of hcloud-go (#39)
This commit is contained in:
parent
a07856317d
commit
99d5396435
3 changed files with 2 additions and 80 deletions
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hetznercloud/hcloud-go/v2/hcloud"
|
"github.com/hetznercloud/hcloud-go/v2/hcloud"
|
||||||
|
"github.com/hetznercloud/hcloud-go/v2/hcloud/exp/kit/sshutils"
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
|
|
||||||
"github.com/apricote/hcloud-upload-image/hcloudimages/contextlogger"
|
"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/control"
|
||||||
"github.com/apricote/hcloud-upload-image/hcloudimages/internal/labelutil"
|
"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/randomid"
|
||||||
"github.com/apricote/hcloud-upload-image/hcloudimages/internal/sshkey"
|
|
||||||
"github.com/apricote/hcloud-upload-image/hcloudimages/internal/sshsession"
|
"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
|
// 1. Create SSH Key
|
||||||
logger.InfoContext(ctx, "# Step 1: Generating SSH Key")
|
logger.InfoContext(ctx, "# Step 1: Generating SSH Key")
|
||||||
publicKey, privateKey, err := sshkey.GenerateKeyPair()
|
privateKey, publicKey, err := sshutils.GenerateKeyPair()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to generate temporary ssh key pair: %w", err)
|
return nil, fmt.Errorf("failed to generate temporary ssh key pair: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
|
|
||||||
}
|
|
||||||
|
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue