Skip to content

Commit 99f2005

Browse files
author
Andrés Maldonado
committed
WIP: Serach for an existing key by fingerprint before attempting upload
fixes #49
1 parent eecb2b0 commit 99f2005

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

driver.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,29 @@ func (d *Driver) Create() error {
241241
return errors.Wrap(err, "could not read ssh public key")
242242
}
243243

244-
keyopts := hcloud.SSHKeyCreateOpts{
245-
Name: d.GetMachineName(),
246-
PublicKey: string(buf),
244+
// Try to find a key with the same Fingerprint
245+
pubkey, _, _, _, err := ssh.ParseAuthorizedKey(buf)
246+
if err != nil {
247+
return errors.Wrap(err, "could not parse ssh public key")
247248
}
248249

249-
key, _, err := d.getClient().SSHKey.Create(context.Background(), keyopts)
250+
fp := ssh.FingerprintLegacyMD5(pubkey)
251+
252+
key, _, err := d.getClient().SSHKey.GetByFingerprint(context.Background(), fp)
250253
if err != nil {
251-
return errors.Wrap(err, "could not create ssh key")
254+
log.Debugf("SSH key not found in Hetzner. Uploading...")
255+
256+
keyopts := hcloud.SSHKeyCreateOpts{
257+
Name: d.GetMachineName(),
258+
PublicKey: string(buf),
259+
}
260+
261+
key, _, err = d.getClient().SSHKey.Create(context.Background(), keyopts)
262+
if err != nil {
263+
return errors.Wrap(err, "could not create ssh key")
264+
}
265+
} else {
266+
log.Debugf("SSH key found in Hetzner. ID: %d", key.ID)
252267
}
253268

254269
d.KeyID = key.ID

0 commit comments

Comments
 (0)