Debian Codegraph @

command/ca/root.go

raw symbols SCIP
1 package ca
2
3 import (
4 "crypto/tls"
5 "encoding/pem"
6 "fmt"
7 "net/http"
8
9 "github.com/pkg/errors"
10
11 "github.com/smallstep/certificates/ca"
12 "github.com/smallstep/cli/command"
13 "github.com/smallstep/cli/crypto/pemutil"
14 "github.com/smallstep/cli/errs"
15 "github.com/smallstep/cli/flags"
16 "github.com/smallstep/cli/ui"
17 "github.com/urfave/cli"
18 )
19
20 func rootComand() cli.Command {
21 return cli.Command{
22 Name: "root",
23 Action: command.ActionFunc(rootAction),
24 Usage: "download and validate the root certificate",
25 UsageText: `**step ca root** [<root-file>]
26 [**--ca-url**=<uri>] [**--fingerprint**=<fingerprint>]`,
27 Description: `**step ca root** downloads and validates the root certificate from the
28 certificate authority.
29
30 ## POSITIONAL ARGUMENTS
31
32 <root-file>
33 : File to write root certificate (PEM format)
34
35 ## EXAMPLES
36
37 Get the root fingerprint in the CA:
38 '''
39 $ step certificate fingerprint /path/to/root_ca.crt
40 0d7d3834cf187726cf331c40a31aa7ef6b29ba4df601416c9788f6ee01058cf3
41 '''
42
43 Download the root certificate from the configured certificate authority:
44 '''
45 $ step ca root root_ca.crt \
46 --fingerprint 0d7d3834cf187726cf331c40a31aa7ef6b29ba4df601416c9788f6ee01058cf3
47 '''
48
49 Download the root certificate using a given certificate authority:
50 '''
51 $ step ca root root_ca.crt \
52 --ca-url https://ca.smallstep.com:9000 \
53 --fingerprint 0d7d3834cf187726cf331c40a31aa7ef6b29ba4df601416c9788f6ee01058cf3
54 '''
55
56 Print the root certificate using the flags set by <step ca bootstrap>:
57 '''
58 $ step ca root
59 '''`,
60 Flags: []cli.Flag{
61 flags.CaURL,
62 flags.Force,
63 fingerprintFlag,
64 },
65 }
66 }
67
68 func rootAction(ctx *cli.Context) error {
69 if err := errs.MinMaxNumberOfArguments(ctx, 0, 1); err != nil {
70 return err
71 }
72
73 caURL, err := flags.ParseCaURL(ctx)
74 if err != nil {
75 return err
76 }
77
78 fingerprint := ctx.String("fingerprint")
79 if len(fingerprint) == 0 {
80 return errs.RequiredFlag(ctx, "fingerprint")
81 }
82
83 tr := getInsecureTransport()
84 client, err := ca.NewClient(caURL, ca.WithTransport(tr))
85 if err != nil {
86 return err
87 }
88
89 // Root already validates the certificate
90 resp, err := client.Root(fingerprint)
91 if err != nil {
92 return errors.Wrap(err, "error downloading root certificate")
93 }
94
95 if rootFile := ctx.Args().Get(0); rootFile != "" {
96 if _, err := pemutil.Serialize(resp.RootPEM.Certificate, pemutil.ToFile(rootFile, 0600)); err != nil {
97 return err
98 }
99 ui.Printf("The root certificate has been saved in %s.\n", rootFile)
100 } else {
101 block, err := pemutil.Serialize(resp.RootPEM.Certificate)
102 if err != nil {
103 return err
104 }
105 fmt.Print(string(pem.EncodeToMemory(block)))
106 }
107 return nil
108 }
109
110 func getInsecureTransport() *http.Transport {
111 return &http.Transport{
112 Proxy: http.ProxyFromEnvironment,
113 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
114 }
115 }