From ffbdbde31a9c1771bafe999321941b9099e087ae Mon Sep 17 00:00:00 2001 From: Quentin Perez Date: Mon, 4 Jan 2016 11:24:35 +0100 Subject: [PATCH 1/2] Fix #151 using rfc4716hex --- pkg/commands/info.go | 4 ++-- pkg/utils/utils.go | 43 +++++++++++++++++++++++++------------------ 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/pkg/commands/info.go b/pkg/commands/info.go index 694a73b433..e3ece0fca3 100644 --- a/pkg/commands/info.go +++ b/pkg/commands/info.go @@ -55,11 +55,11 @@ func RunInfo(ctx CommandContext, args InfoArgs) error { fmt.Fprintln(ctx.Stdout, "") fmt.Fprintln(ctx.Stdout, "SSH Keys:") for id, key := range user.SSHPublicKeys { - fingerprint, err := utils.SSHGetFingerprint(key.Key) + fingerprint, err := utils.SSHGetFingerprint([]byte(key.Key)) if err != nil { return err } - fmt.Fprintf(ctx.Stdout, " [%d] %s", id, fingerprint) + fmt.Fprintf(ctx.Stdout, " [%d] %s\n", id, fingerprint) } fmt.Fprintf(ctx.Stdout, "\n") } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 6f651c09ea..67c9c9e055 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -8,19 +8,22 @@ package utils import ( + "crypto/md5" "errors" "fmt" "io" - "io/ioutil" "net" "os" "os/exec" "path" "path/filepath" + "reflect" "regexp" "strings" "time" + "golang.org/x/crypto/ssh" + "github.com/scaleway/scaleway-cli/pkg/sshcommand" "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus" log "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus" @@ -213,25 +216,29 @@ func AttachToSerial(serverID string, apiToken string) (*gottyclient.Client, chan return gottycli, done, nil } -// SSHGetFingerprint returns the fingerprint of an SSH key -func SSHGetFingerprint(key string) (string, error) { - tmp, err := ioutil.TempFile("", ".tmp") - if err != nil { - return "", fmt.Errorf("Unable to create a tempory file: %v", err) - } - defer os.Remove(tmp.Name()) - buff := []byte(key) - bytesWritten := 0 - for bytesWritten < len(buff) { - nb, err := tmp.Write(buff[bytesWritten:]) - if err != nil { - return "", fmt.Errorf("Unable to write: %v", err) +func rfc4716hex(data []byte) string { + fingerprint := "" + + for i := 0; i < len(data); i++ { + fingerprint = fmt.Sprintf("%s%0.2x", fingerprint, data[i]) + if i != len(data)-1 { + fingerprint = fingerprint + ":" } - bytesWritten += nb } - ret, err := exec.Command("ssh-keygen", "-l", "-f", tmp.Name()).Output() + return fingerprint +} + +// SSHGetFingerprint returns the fingerprint of an SSH key +func SSHGetFingerprint(key []byte) (string, error) { + publicKey, comment, _, _, err := ssh.ParseAuthorizedKey(key) if err != nil { - return "", fmt.Errorf("Unable to run ssh-keygen: %v", err) + return "", err + } + switch reflect.TypeOf(publicKey).String() { + case "*ssh.rsaPublicKey", "*ssh.dsaPublicKey", "*ssh.ecdsaPublicKey": + md5sum := md5.Sum(publicKey.Marshal()) + return publicKey.Type() + " " + rfc4716hex(md5sum[:]) + " " + comment, nil + default: + return "", errors.New("Can't handle this key") } - return string(ret), nil } From 741ada6301a632fc2b5a30b38db929e1bb446348 Mon Sep 17 00:00:00 2001 From: Quentin Perez Date: Mon, 4 Jan 2016 11:28:45 +0100 Subject: [PATCH 2/2] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0342fe535c..bce6d79fa9 100644 --- a/README.md +++ b/README.md @@ -1152,6 +1152,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address' ### master (unreleased) +* Use rfc4716 (openSSH) to generate the fingerprints ([#151](https://github.com/scaleway/scaleway-cli/issues/151)) * create-image-from-http.sh: Support HTTP proxy ([#249](https://github.com/scaleway/scaleway-cli/issues/249)) * Support of `scw run --userdata=...` ([#202](https://github.com/scaleway/scaleway-cli/issues/202)) * Refactor of `scw _security-groups` ([#197](https://github.com/scaleway/scaleway-cli/issues/197))