From 999b740df6e925ea0ee5b4b8aea7b3df895bb9b4 Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Wed, 6 Dec 2017 00:26:58 +0100 Subject: [PATCH] Add 'key show KEY' command (#11) --- CHANGELOG.md | 1 + README.md | 1 + shell.go | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b48f3dd..7556c17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Add `--latest` and `--quiet` options to `ls` commands * Add `healthcheck` user +* Add `key show KEY` command ## v1.5.0 (2017-12-02) diff --git a/README.md b/README.md index dd49f8c..939ea14 100644 --- a/README.md +++ b/README.md @@ -181,6 +181,7 @@ key inspect [-h] [--decrypt] KEY... key ls [-h] [--latest] [--quiet] key rm [-h] KEY... key setup [-h] KEY +key show [-h] KEY # session management session help diff --git a/shell.go b/shell.go index d8212f7..a29b95d 100644 --- a/shell.go +++ b/shell.go @@ -15,6 +15,7 @@ import ( humanize "github.com/dustin/go-humanize" "github.com/gliderlabs/ssh" "github.com/jinzhu/gorm" + "github.com/mgutz/ansi" "github.com/moby/moby/pkg/namesgenerator" "github.com/olekukonko/tablewriter" "github.com/urfave/cli" @@ -1112,7 +1113,7 @@ GLOBAL OPTIONS: } var keys []*SSHKey - if err := SSHKeysByIdentifiers(db, c.Args()).Find(&keys).Error; err != nil { + if err := SSHKeysByIdentifiers(SSHKeysPreload(db), c.Args()).Find(&keys).Error; err != nil { return err } @@ -1212,6 +1213,80 @@ GLOBAL OPTIONS: fmt.Fprintf(s, "umask 077; mkdir -p .ssh; echo %s sshportal >> .ssh/authorized_keys\n", key.PubKey) return nil }, + }, { + Name: "show", + Usage: "Shows standard information on a `KEY`", + ArgsUsage: "KEY", + Action: func(c *cli.Context) error { + if c.NArg() != 1 { + return cli.ShowSubcommandHelp(c) + } + + // not checking roles, everyone with an account can see how to enroll new hosts + + var key SSHKey + if err := SSHKeysByIdentifiers(SSHKeysPreload(db), c.Args()).First(&key).Error; err != nil { + return err + } + SSHKeyDecrypt(globalContext.String("aes-key"), &key) + + type line struct { + key string + value string + } + type section struct { + name string + lines []line + } + var hosts []string + for _, host := range key.Hosts { + hosts = append(hosts, host.Name) + } + sections := []section{ + { + name: "General", + lines: []line{ + {"Name", key.Name}, + {"Type", key.Type}, + {"Length", fmt.Sprintf("%d", key.Length)}, + {"Comment", key.Comment}, + }, + }, { + name: "Relationships", + lines: []line{ + {"Linked hosts", fmt.Sprintf("%s (%d)", strings.Join(hosts, ", "), len(hosts))}, + }, + }, { + name: "Crypto", + lines: []line{ + {"authorized_key format", key.PubKey}, + {"Private Key", key.PrivKey}, + }, + }, { + name: "Help", + lines: []line{ + {"inspect", fmt.Sprintf("ssh sshportal key inspect %s", key.Name)}, + {"setup", fmt.Sprintf(`ssh user@example.com "$(ssh sshportal key setup %s)"`, key.Name)}, + }, + }, + } + + valueColor := ansi.ColorFunc("white") + titleColor := ansi.ColorFunc("magenta+bh") + keyColor := ansi.ColorFunc("red+bh") + for _, section := range sections { + fmt.Fprintf(s, "%s\n%s\n", titleColor(section.name), strings.Repeat("=", len(section.name))) + for _, line := range section.lines { + if strings.Contains(line.value, "\n") { + fmt.Fprintf(s, "%s:\n%s\n", keyColor(line.key), valueColor(line.value)) + } else { + fmt.Fprintf(s, "%s: %s\n", keyColor(line.key), valueColor(line.value)) + } + } + fmt.Fprintf(s, "\n") + } + return nil + }, }, }, }, {