Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions pkg/commands/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
43 changes: 25 additions & 18 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}