Debian Codegraph @

command/ca/federation.go

raw symbols SCIP
1 package ca
2
3 import (
4 "encoding/pem"
5 "fmt"
6 "os"
7
8 "github.com/pkg/errors"
9 "github.com/smallstep/certificates/api"
10 "github.com/smallstep/certificates/ca"
11 "github.com/smallstep/certificates/pki"
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/smallstep/cli/utils"
18 "github.com/urfave/cli"
19 )
20
21 type flowType int
22
23 const (
24 rootsFlow flowType = iota
25 federationFlow
26 )
27
28 func rootsCommand() cli.Command {
29 return cli.Command{
30 Name: "roots",
31 Action: command.ActionFunc(rootsAction),
32 Usage: "download all the root certificates",
33 UsageText: `**step ca roots** [<roots-file>]
34 [**--ca-url**=<uri>] [**--root**=<file>]`,
35 Description: `**step ca roots** downloads a certificate bundle with all the root
36 certificates.
37
38 ## POSITIONAL ARGUMENTS
39
40 <roots-file>
41 : File to write all the root certificates (PEM format)
42
43 ## EXAMPLES
44
45 Download the roots with flags set by <step ca bootstrap>:
46 '''
47 $ step ca roots roots.pem
48 '''
49
50 Download the roots with custom flags:
51 '''
52 $ step ca roots roots.pem \
53 --ca-url https://ca.example.com \
54 --root /path/to/root_ca.crt
55 '''
56
57 Print the roots using flags set by <step ca bootstrap>:
58 '''
59 $ step ca roots
60 '''`,
61 Flags: []cli.Flag{
62 flags.CaURL,
63 flags.Force,
64 flags.Root,
65 },
66 }
67 }
68
69 func federationCommand() cli.Command {
70 return cli.Command{
71 Name: "federation",
72 Action: command.ActionFunc(federationAction),
73 Usage: "download all the federated certificates",
74 UsageText: `**step ca federation** [<federation-file>]
75 [**--ca-url**=<uri>] [**--root**=<file>]`,
76 Description: `**step ca federation** downloads a certificate bundle with all the root
77 certificates in the federation.
78
79 ## POSITIONAL ARGUMENTS
80
81 <federation-file>
82 : File to write federation certificates (PEM format)
83
84 ## EXAMPLES
85
86 Download the federated roots with flags set by <step ca bootstrap>:
87 '''
88 $ step ca federation federation.pem
89 '''
90
91 Download the federated roots with custom flags:
92 '''
93 $ step ca federation federation.pem \
94 --ca-url https://ca.example.com \
95 --root /path/to/root_ca.crt
96 '''
97
98 Print the federated roots using flags set by <step ca bootstrap>:
99 '''
100 $ step ca federation
101 '''`,
102 Flags: []cli.Flag{
103 flags.CaURL,
104 flags.Force,
105 flags.Root,
106 },
107 }
108 }
109
110 func rootsAction(ctx *cli.Context) error {
111 return rootsAndFederationFlow(ctx, rootsFlow)
112 }
113
114 func federationAction(ctx *cli.Context) error {
115 return rootsAndFederationFlow(ctx, federationFlow)
116 }
117
118 func rootsAndFederationFlow(ctx *cli.Context, typ flowType) error {
119 if err := errs.MinMaxNumberOfArguments(ctx, 0, 1); err != nil {
120 return err
121 }
122
123 caURL, err := flags.ParseCaURL(ctx)
124 if err != nil {
125 return err
126 }
127
128 root := ctx.String("root")
129 if len(root) == 0 {
130 root = pki.GetRootCAPath()
131 if _, err := os.Stat(root); err != nil {
132 return errs.RequiredFlag(ctx, "root")
133 }
134 }
135
136 client, err := ca.NewClient(caURL, ca.WithRootFile(root))
137 if err != nil {
138 return err
139 }
140
141 var certs []api.Certificate
142 switch typ {
143 case rootsFlow:
144 roots, err := client.Roots()
145 if err != nil {
146 return err
147 }
148 certs = roots.Certificates
149 case federationFlow:
150 federation, err := client.Federation()
151 if err != nil {
152 return err
153 }
154 certs = federation.Certificates
155 default:
156 return errors.New("unknown flow type: this should not happen")
157 }
158
159 var data []byte
160 for _, cert := range certs {
161 block, err := pemutil.Serialize(cert.Certificate)
162 if err != nil {
163 return err
164 }
165 data = append(data, pem.EncodeToMemory(block)...)
166 }
167
168 if outFile := ctx.Args().Get(0); outFile != "" {
169 if err := utils.WriteFile(outFile, data, 0600); err != nil {
170 return err
171 }
172
173 switch typ {
174 case rootsFlow:
175 ui.Printf("The root certificate bundle has been saved in %s.\n", outFile)
176 case federationFlow:
177 ui.Printf("The federation certificate bundle has been saved in %s.\n", outFile)
178 default:
179 return errors.New("unknown flow type: this should not happen")
180 }
181 } else {
182 fmt.Print(string(data))
183 }
184 return nil
185 }