| 1 | package ssh |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 6 | "github.com/pkg/errors" |
| 7 | "github.com/smallstep/cli/command" |
| 8 | "github.com/smallstep/cli/crypto/sshutil" |
| 9 | "github.com/smallstep/cli/errs" |
| 10 | "github.com/smallstep/cli/utils" |
| 11 | "github.com/urfave/cli" |
| 12 | "golang.org/x/crypto/ssh" |
| 13 | ) |
| 14 | |
| 15 | func inspectCommand() cli.Command { |
| 16 | return cli.Command{ |
| 17 | Name: "inspect", |
| 18 | Action: command.ActionFunc(inspectAction), |
| 19 | Usage: "print the contents of an ssh certificate", |
| 20 | UsageText: `**step ssh inspect** <crt-file>`, |
| 21 | Description: `**step ssh inspect** command prints ssh certificate details in human readable |
| 22 | format. |
| 23 | |
| 24 | ## POSITIONAL ARGUMENTS |
| 25 | |
| 26 | <crt-file> |
| 27 | : The path to an ssh certificate. |
| 28 | |
| 29 | ## EXAMPLES |
| 30 | |
| 31 | Prints the contents of id_ecdsa-cert.pub: |
| 32 | ''' |
| 33 | $ step ssh inspect id_ecdsa-cert.pub |
| 34 | '''`, |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func inspectAction(ctx *cli.Context) error { |
| 39 | if err := errs.MinMaxNumberOfArguments(ctx, 0, 1); err != nil { |
| 40 | return err |
| 41 | } |
| 42 | |
| 43 | var name string |
| 44 | switch ctx.NArg() { |
| 45 | case 0: |
| 46 | name = "-" |
| 47 | case 1: |
| 48 | name = ctx.Args().First() |
| 49 | default: |
| 50 | return errs.TooManyArguments(ctx) |
| 51 | } |
| 52 | |
| 53 | b, err := utils.ReadFile(name) |
| 54 | if err != nil { |
| 55 | return err |
| 56 | } |
| 57 | |
| 58 | pub, _, _, _, err := ssh.ParseAuthorizedKey(b) |
| 59 | if err != nil { |
| 60 | return errors.Wrap(err, "error parsing ssh certificate") |
| 61 | } |
| 62 | cert, ok := pub.(*ssh.Certificate) |
| 63 | if !ok { |
| 64 | return errors.Errorf("error decoding ssh certificate: %T is not an *ssh.Certificate", pub) |
| 65 | } |
| 66 | inspect, err := sshutil.InspectCertificate(cert) |
| 67 | if err != nil { |
| 68 | return err |
| 69 | } |
| 70 | |
| 71 | space := "" |
| 72 | fmt.Println(name + ":") |
| 73 | fmt.Printf("%8sType: %s %s certificate\n", space, inspect.KeyName, inspect.Type) |
| 74 | fmt.Printf("%8sPublic key: %s-CERT %s\n", space, inspect.KeyAlgo, inspect.KeyFingerprint) |
| 75 | fmt.Printf("%8sSigning CA: %s %s\n", space, inspect.SigningKeyAlgo, inspect.SigningKeyFingerprint) |
| 76 | fmt.Printf("%8sKey ID: \"%s\"\n", space, inspect.KeyID) |
| 77 | fmt.Printf("%8sSerial: %d\n", space, inspect.Serial) |
| 78 | fmt.Printf("%8sValid: %s\n", space, inspect.Validity()) |
| 79 | fmt.Printf("%8sPrincipals: ", space) |
| 80 | if len(inspect.Principals) == 0 { |
| 81 | fmt.Println("(none)") |
| 82 | } else { |
| 83 | fmt.Println() |
| 84 | for _, p := range inspect.Principals { |
| 85 | fmt.Printf("%16s%s\n", space, p) |
| 86 | } |
| 87 | } |
| 88 | fmt.Printf("%8sCritical Options: ", space) |
| 89 | if len(inspect.CriticalOptions) == 0 { |
| 90 | fmt.Println("(none)") |
| 91 | } else { |
| 92 | fmt.Println() |
| 93 | for k, v := range inspect.CriticalOptions { |
| 94 | fmt.Printf("%16s%s %v\n", space, k, v) |
| 95 | } |
| 96 | } |
| 97 | fmt.Printf("%8sExtensions: ", space) |
| 98 | if len(inspect.Extensions) == 0 { |
| 99 | fmt.Println("(none)") |
| 100 | } else { |
| 101 | fmt.Println() |
| 102 | for k, v := range inspect.Extensions { |
| 103 | fmt.Printf("%16s%s %v\n", space, k, v) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return nil |
| 108 | } |