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