Add 'key show KEY' command (#11)

This commit is contained in:
Manfred Touron 2017-12-06 00:26:58 +01:00
parent 6864b7ca10
commit 999b740df6
3 changed files with 78 additions and 1 deletions

View file

@ -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)

View file

@ -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

View file

@ -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
},
},
},
}, {