Skip to content

Commit 39c0838

Browse files
author
Andrés Maldonado
committed
Move logic to a separate function
Also, fix condition ('if err != nil' -> 'if key == nil') and use 'log.Infof' instead of 'log.Debugf' to inform users that SSH key is being uploaded.
1 parent 99f2005 commit 39c0838

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

driver.go

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

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")
248-
}
249-
250-
fp := ssh.FingerprintLegacyMD5(pubkey)
251-
252-
key, _, err := d.getClient().SSHKey.GetByFingerprint(context.Background(), fp)
253-
if err != nil {
254-
log.Debugf("SSH key not found in Hetzner. Uploading...")
244+
key, err := d.getRemoteKeyWithSameFingerprint(buf)
245+
if key == nil {
246+
log.Infof("SSH key not found in Hetzner. Uploading...")
255247

256248
keyopts := hcloud.SSHKeyCreateOpts{
257249
Name: d.GetMachineName(),
@@ -621,6 +613,21 @@ func (d *Driver) getKey() (*hcloud.SSHKey, error) {
621613
return stype, nil
622614
}
623615

616+
func (d *Driver) getRemoteKeyWithSameFingerprint(pubkey_byte []byte) (*hcloud.SSHKey, error) {
617+
pubkey, _, _, _, err := ssh.ParseAuthorizedKey(pubkey_byte)
618+
if err != nil {
619+
return nil, errors.Wrap(err, "could not parse ssh public key")
620+
}
621+
622+
fp := ssh.FingerprintLegacyMD5(pubkey)
623+
624+
remotekey, _, err := d.getClient().SSHKey.GetByFingerprint(context.Background(), fp)
625+
if err != nil {
626+
return remotekey, errors.Wrap(err, "could not get sshkey by fingerprint")
627+
}
628+
return remotekey, nil
629+
}
630+
624631
func (d *Driver) getServerHandle() (*hcloud.Server, error) {
625632
if d.cachedServer != nil {
626633
return d.cachedServer, nil

0 commit comments

Comments
 (0)