From a854ae0f46d3c51dc24d0c7674e89385bdaa1f25 Mon Sep 17 00:00:00 2001 From: Quentin Perez Date: Wed, 2 Sep 2015 11:01:23 +0200 Subject: [PATCH] fix 130 --- pkg/commands/info.go | 19 +++++++++++++++++++ pkg/utils/utils.go | 23 +++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/pkg/commands/info.go b/pkg/commands/info.go index f349c56467..501cf2ef97 100644 --- a/pkg/commands/info.go +++ b/pkg/commands/info.go @@ -12,6 +12,7 @@ import ( "github.com/scaleway/scaleway-cli/vendor/github.com/kardianos/osext" "github.com/scaleway/scaleway-cli/pkg/config" + "github.com/scaleway/scaleway-cli/pkg/utils" ) // InfoArgs are flags for the `RunInfo` function @@ -41,5 +42,23 @@ func RunInfo(ctx CommandContext, args InfoArgs) error { fmt.Fprintf(ctx.Stdout, " Snapshots: %d\n", ctx.API.Cache.GetNbSnapshots()) fmt.Fprintf(ctx.Stdout, " Volumes: %d\n", ctx.API.Cache.GetNbVolumes()) fmt.Fprintf(ctx.Stdout, " Bootscripts: %d\n", ctx.API.Cache.GetNbBootscripts()) + user, err := ctx.API.GetUser() + if err != nil { + return fmt.Errorf("Unable to get your SSH Keys") + } else { + if len(user.SSHPublicKeys) == 0 { + fmt.Fprintln(ctx.Stdout, "You have no ssh keys") + } else { + fmt.Fprintln(ctx.Stdout, "SSH Keys:") + for id, key := range user.SSHPublicKeys { + fingerprint, err := utils.SSHGetFingerprint(key.Key) + if err != nil { + return err + } else { + fmt.Fprintf(ctx.Stdout, " [%d] %s", id, fingerprint) + } + } + } + } return nil } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 127d81dd39..6964969c72 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -11,6 +11,7 @@ import ( "errors" "fmt" "io" + "io/ioutil" "net" "os" "os/exec" @@ -211,3 +212,25 @@ However, you can access your serial using a web browser: } return nil } + +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) + } + bytesWritten += nb + } + ret, err := exec.Command("ssh-keygen", "-l", "-f", tmp.Name()).Output() + if err != nil { + return "", fmt.Errorf("Unable to run ssh-keygen: %v", err) + } + return string(ret), nil +}