Skip to content

Commit d01bf53

Browse files
committed
Merge pull request #252 from QuentinPerez/fix_151
Fix 151
2 parents 27cb668 + 741ada6 commit d01bf53

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address'
11521152

11531153
### master (unreleased)
11541154

1155+
* Use rfc4716 (openSSH) to generate the fingerprints ([#151](https://github.com/scaleway/scaleway-cli/issues/151))
11551156
* create-image-from-http.sh: Support HTTP proxy ([#249](https://github.com/scaleway/scaleway-cli/issues/249))
11561157
* Support of `scw run --userdata=...` ([#202](https://github.com/scaleway/scaleway-cli/issues/202))
11571158
* Refactor of `scw _security-groups` ([#197](https://github.com/scaleway/scaleway-cli/issues/197))

pkg/commands/info.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ func RunInfo(ctx CommandContext, args InfoArgs) error {
5555
fmt.Fprintln(ctx.Stdout, "")
5656
fmt.Fprintln(ctx.Stdout, "SSH Keys:")
5757
for id, key := range user.SSHPublicKeys {
58-
fingerprint, err := utils.SSHGetFingerprint(key.Key)
58+
fingerprint, err := utils.SSHGetFingerprint([]byte(key.Key))
5959
if err != nil {
6060
return err
6161
}
62-
fmt.Fprintf(ctx.Stdout, " [%d] %s", id, fingerprint)
62+
fmt.Fprintf(ctx.Stdout, " [%d] %s\n", id, fingerprint)
6363
}
6464
fmt.Fprintf(ctx.Stdout, "\n")
6565
}

pkg/utils/utils.go

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,22 @@
88
package utils
99

1010
import (
11+
"crypto/md5"
1112
"errors"
1213
"fmt"
1314
"io"
14-
"io/ioutil"
1515
"net"
1616
"os"
1717
"os/exec"
1818
"path"
1919
"path/filepath"
20+
"reflect"
2021
"regexp"
2122
"strings"
2223
"time"
2324

25+
"golang.org/x/crypto/ssh"
26+
2427
"github.com/scaleway/scaleway-cli/pkg/sshcommand"
2528
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
2629
log "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
@@ -213,25 +216,29 @@ func AttachToSerial(serverID string, apiToken string) (*gottyclient.Client, chan
213216
return gottycli, done, nil
214217
}
215218

216-
// SSHGetFingerprint returns the fingerprint of an SSH key
217-
func SSHGetFingerprint(key string) (string, error) {
218-
tmp, err := ioutil.TempFile("", ".tmp")
219-
if err != nil {
220-
return "", fmt.Errorf("Unable to create a tempory file: %v", err)
221-
}
222-
defer os.Remove(tmp.Name())
223-
buff := []byte(key)
224-
bytesWritten := 0
225-
for bytesWritten < len(buff) {
226-
nb, err := tmp.Write(buff[bytesWritten:])
227-
if err != nil {
228-
return "", fmt.Errorf("Unable to write: %v", err)
219+
func rfc4716hex(data []byte) string {
220+
fingerprint := ""
221+
222+
for i := 0; i < len(data); i++ {
223+
fingerprint = fmt.Sprintf("%s%0.2x", fingerprint, data[i])
224+
if i != len(data)-1 {
225+
fingerprint = fingerprint + ":"
229226
}
230-
bytesWritten += nb
231227
}
232-
ret, err := exec.Command("ssh-keygen", "-l", "-f", tmp.Name()).Output()
228+
return fingerprint
229+
}
230+
231+
// SSHGetFingerprint returns the fingerprint of an SSH key
232+
func SSHGetFingerprint(key []byte) (string, error) {
233+
publicKey, comment, _, _, err := ssh.ParseAuthorizedKey(key)
233234
if err != nil {
234-
return "", fmt.Errorf("Unable to run ssh-keygen: %v", err)
235+
return "", err
236+
}
237+
switch reflect.TypeOf(publicKey).String() {
238+
case "*ssh.rsaPublicKey", "*ssh.dsaPublicKey", "*ssh.ecdsaPublicKey":
239+
md5sum := md5.Sum(publicKey.Marshal())
240+
return publicKey.Type() + " " + rfc4716hex(md5sum[:]) + " " + comment, nil
241+
default:
242+
return "", errors.New("Can't handle this key")
235243
}
236-
return string(ret), nil
237244
}

0 commit comments

Comments
 (0)