| 1 |
package ca |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"os" |
| 6 |
|
| 7 |
"github.com/smallstep/certificates/ca" |
| 8 |
"github.com/smallstep/certificates/pki" |
| 9 |
"github.com/smallstep/cli/errs" |
| 10 |
"github.com/smallstep/cli/flags" |
| 11 |
"github.com/urfave/cli" |
| 12 |
) |
| 13 |
|
| 14 |
func healthCommand() cli.Command { |
| 15 |
return cli.Command{ |
| 16 |
Name: "health", |
| 17 |
Action: healthAction, |
| 18 |
Usage: "get the status of the CA", |
| 19 |
UsageText: `**step ca health** |
| 20 |
[**--ca-url**=<URI>] [**--root**=<file>]`, |
| 21 |
Description: `**step ca health** makes an API request to the /health |
| 22 |
endpoint of the Step CA to check if it is running. If the CA is healthy, the |
| 23 |
response will be 'ok'. |
| 24 |
|
| 25 |
## EXAMPLES |
| 26 |
|
| 27 |
Using the required flags: |
| 28 |
''' |
| 29 |
$ step ca health --ca-url https://ca.smallstep.com:8080 --root path/to/root_ca.crt |
| 30 |
ok |
| 31 |
''' |
| 32 |
|
| 33 |
With the required flags preconfigured: |
| 34 |
|
| 35 |
**--ca-url** is set using environment variables (as STEP_CA_URL) or the default |
| 36 |
configuration file in <$STEPPATH/config/defaults.json>. |
| 37 |
|
| 38 |
**--root** is set using environment variables (as STEP_ROOT), the default |
| 39 |
configuration file in <$STEPPATH/config/defaults.json> or the default root |
| 40 |
certificate located in <$STEPPATH/certs/root_ca.crt> |
| 41 |
|
| 42 |
''' |
| 43 |
$ step ca health |
| 44 |
ok |
| 45 |
'''`, |
| 46 |
Flags: []cli.Flag{ |
| 47 |
flags.CaURL, |
| 48 |
flags.Root, |
| 49 |
}, |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
func healthAction(ctx *cli.Context) error { |
| 54 |
if err := errs.NumberOfArguments(ctx, 0); err != nil { |
| 55 |
return err |
| 56 |
} |
| 57 |
|
| 58 |
caURL, err := flags.ParseCaURL(ctx) |
| 59 |
if err != nil { |
| 60 |
return err |
| 61 |
} |
| 62 |
|
| 63 |
root := ctx.String("root") |
| 64 |
|
| 65 |
if len(root) == 0 { |
| 66 |
root = pki.GetRootCAPath() |
| 67 |
if _, err := os.Stat(root); err != nil { |
| 68 |
return errs.RequiredFlag(ctx, "root") |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
var options []ca.ClientOption |
| 73 |
options = append(options, ca.WithRootFile(root)) |
| 74 |
|
| 75 |
client, err := ca.NewClient(caURL, options...) |
| 76 |
if err != nil { |
| 77 |
return err |
| 78 |
} |
| 79 |
r, err := client.Health() |
| 80 |
if err != nil { |
| 81 |
return err |
| 82 |
} |
| 83 |
fmt.Printf("%v\n", r.Status) |
| 84 |
return nil |
| 85 |
} |