| 1 |
package ssh |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"net/http" |
| 6 |
"os" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/pkg/errors" |
| 10 |
"github.com/smallstep/certificates/ca" |
| 11 |
caErrs "github.com/smallstep/certificates/errs" |
| 12 |
"github.com/smallstep/cli/command" |
| 13 |
"github.com/smallstep/cli/errs" |
| 14 |
"github.com/smallstep/cli/flags" |
| 15 |
"github.com/smallstep/cli/jose" |
| 16 |
"github.com/smallstep/cli/token" |
| 17 |
"github.com/smallstep/cli/utils/cautils" |
| 18 |
"github.com/urfave/cli" |
| 19 |
) |
| 20 |
|
| 21 |
func checkHostCommand() cli.Command { |
| 22 |
return cli.Command{ |
| 23 |
Name: "check-host", |
| 24 |
Action: command.ActionFunc(checkHostAction), |
| 25 |
Usage: "checks if a certificate has been issued for a host", |
| 26 |
UsageText: `**step ssh check-host** <hostname> |
| 27 |
[**--ca-url**=<uri>] [**--root**=<file>] |
| 28 |
[**--offline**] [**--ca-config**=<path>] [**--verbose,-v**]`, |
| 29 |
Description: `**step ssh check-host** checks if a certificate has been issued for a host. |
| 30 |
|
| 31 |
This command returns a zero exit status if the host has a certificate. |
| 32 |
Otherwise, it returns 1. |
| 33 |
|
| 34 |
## POSITIONAL ARGUMENTS |
| 35 |
|
| 36 |
<hostname> |
| 37 |
: The hostname of the server to check. |
| 38 |
|
| 39 |
## EXAMPLES |
| 40 |
|
| 41 |
Check that internal.example.com exists: |
| 42 |
''' |
| 43 |
$ step ssh check-host internal.smallstep.com |
| 44 |
'''`, |
| 45 |
Flags: []cli.Flag{ |
| 46 |
flags.CaURL, |
| 47 |
flags.Root, |
| 48 |
flags.Offline, |
| 49 |
flags.CaConfig, |
| 50 |
cli.BoolFlag{ |
| 51 |
Name: "verbose, v", |
| 52 |
Usage: `Return "true" or "false" in the terminal.`, |
| 53 |
}, |
| 54 |
}, |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
func checkHostAction(ctx *cli.Context) error { |
| 59 |
isVerbose := ctx.Bool("verbose") |
| 60 |
|
| 61 |
if err := errs.NumberOfArguments(ctx, 1); err != nil { |
| 62 |
return err |
| 63 |
} |
| 64 |
|
| 65 |
client, err := cautils.NewClient(ctx) |
| 66 |
if err != nil { |
| 67 |
return contactAdminErr(errors.Wrap(err, "error generating ca client")) |
| 68 |
} |
| 69 |
version, err := client.Version() |
| 70 |
if err != nil { |
| 71 |
return contactAdminErr(errors.Wrap(err, "error retrieving client version info")) |
| 72 |
} |
| 73 |
|
| 74 |
var ( |
| 75 |
tok string |
| 76 |
hostname = ctx.Args().First() |
| 77 |
) |
| 78 |
if version.RequireClientAuthentication { |
| 79 |
id, err := ca.LoadDefaultIdentity() |
| 80 |
if err != nil { |
| 81 |
return sshConfigErr(errors.Wrap(err, "error loading the default x5c identity")) |
| 82 |
} |
| 83 |
|
| 84 |
if id != nil { |
| 85 |
|
| 86 |
jwk, err := jose.ParseKey(id.Key) |
| 87 |
if err != nil { |
| 88 |
return debugErr(errors.Wrap(err, "error parsing x5c key from identity file")) |
| 89 |
} |
| 90 |
tokenGen := cautils.NewTokenGenerator(jwk.KeyID, "x5c-identity", |
| 91 |
"/ssh/check-host", "", time.Time{}, time.Time{}, jwk) |
| 92 |
tok, err = tokenGen.Token(hostname, token.WithX5CInsecureFile(id.Certificate, jwk.Key)) |
| 93 |
if err != nil { |
| 94 |
return sshConfigErr(errors.Wrap(err, "error generating identity x5c token for /ssh/check-host request")) |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
resp, err := client.SSHCheckHost(hostname, tok) |
| 100 |
if err != nil { |
| 101 |
return caErrs.Wrap(http.StatusInternalServerError, err, |
| 102 |
"error checking ssh host eligibility") |
| 103 |
} |
| 104 |
|
| 105 |
if isVerbose { |
| 106 |
fmt.Println(resp.Exists) |
| 107 |
} |
| 108 |
if !resp.Exists { |
| 109 |
os.Exit(1) |
| 110 |
} |
| 111 |
return nil |
| 112 |
} |