Skip to content

Commit

Permalink
fix: change ssh key to uuid from machine id
Browse files Browse the repository at this point in the history
If a machine fails to start, DevPod can refuse the machine ID
which causes a conflict in Hetzner. Swap to a UUID to avoid
this.
  • Loading branch information
mrsimonemms committed Jan 12, 2025
1 parent 854d3a5 commit 16f9925
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.23.0
toolchain go1.23.4

require (
github.com/google/uuid v1.6.0
github.com/hetznercloud/hcloud-go/v2 v2.17.1
github.com/loft-sh/devpod v0.6.8
github.com/loft-sh/log v0.0.0-20240219160058-26d83ffb46ac
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hetznercloud/hcloud-go/v2 v2.17.1 h1:DPi019dv0WCiECEmtcuTgc//hBvnxESb6QlJnAb4a04=
github.com/hetznercloud/hcloud-go/v2 v2.17.1/go.mod h1:6ygmBba+FdawR2lLp/d9uJljY2k0dTYthprrI8usdLw=
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog=
Expand Down
1 change: 1 addition & 0 deletions pkg/hetzner/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package hetzner

const (
labelMachineId = "machineId"
maxServerConnectAttempts = 60
SSH_USERNAME = "devpod"
SSH_PORT = 22
Expand Down
29 changes: 24 additions & 5 deletions pkg/hetzner/hetzner.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

cryptoSsh "golang.org/x/crypto/ssh"

"github.com/google/uuid"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/loft-sh/devpod/pkg/client"
"github.com/loft-sh/devpod/pkg/ssh"
Expand Down Expand Up @@ -100,12 +101,22 @@ func (h *Hetzner) BuildServerOptions(ctx context.Context, opts *options.Options)
}

if sshKey == nil {
// Generate name
machineId := opts.MachineID
if len(machineId) >= 24 {
machineId = opts.MachineID[:24]
}
name := fmt.Sprintf("%s-%s", machineId, uuid.NewString()[:8])

log.Default.Infof("Uploading SSH key: %s", name)

// Upload the key
uploadedSSHKey, _, err := h.client.SSHKey.Create(ctx, hcloud.SSHKeyCreateOpts{
Name: opts.MachineID,
Name: name,
PublicKey: string(publicKey),
Labels: map[string]string{
"type": "devpod",
"type": "devpod",
labelMachineId: opts.MachineID,
},
})
if err != nil {
Expand Down Expand Up @@ -269,10 +280,18 @@ func (h *Hetzner) Create(ctx context.Context, req *hcloud.ServerCreateOpts, disk

func (h *Hetzner) Delete(ctx context.Context, name string) error {
// Delete SSH key
if sshKey, _, err := h.client.SSHKey.GetByName(ctx, name); err != nil {
keys, _, err := h.client.SSHKey.List(ctx, hcloud.SSHKeyListOpts{
ListOpts: hcloud.ListOpts{
LabelSelector: fmt.Sprintf("%s=%s", labelMachineId, name),
},
})
if err != nil {
return err
} else if sshKey != nil {
_, err = h.client.SSHKey.Delete(ctx, sshKey)
}

for _, k := range keys {
log.Default.Infof("Deleting SSH key: %s", k.Name)
_, err = h.client.SSHKey.Delete(ctx, k)
if err != nil {
return err
}
Expand Down

0 comments on commit 16f9925

Please sign in to comment.