-
Notifications
You must be signed in to change notification settings - Fork 255
/
health.go
87 lines (73 loc) · 1.92 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
83
84
85
86
87
package ca
import (
"context"
"fmt"
"os"
"github.com/smallstep/certificates/ca"
"github.com/smallstep/certificates/pki"
"github.com/smallstep/cli/flags"
"github.com/urfave/cli"
"go.step.sm/cli-utils/errs"
)
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>] [**--context**=<name>]`,
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{
flags.CaURL,
flags.Root,
flags.Context,
},
}
}
func healthAction(ctx *cli.Context) error {
if err := errs.NumberOfArguments(ctx, 0); err != nil {
return err
}
caURL, err := flags.ParseCaURL(ctx)
if err != nil {
return err
}
root := ctx.String("root")
// Prepare client for bootstrap or provisioning tokens
if root == "" {
root = pki.GetRootCAPath()
if _, err := os.Stat(root); err != nil {
return errs.RequiredFlag(ctx, "root")
}
}
var options []ca.ClientOption
options = append(options, ca.WithRootFile(root))
caClient, err := ca.NewClient(caURL, options...)
if err != nil {
return err
}
r, err := caClient.HealthWithContext(context.Background())
if err != nil {
return err
}
fmt.Printf("%v\n", r.Status)
return nil
}