Debian Codegraph @

command/ca/ca.go

raw symbols SCIP
1 package ca
2
3 import (
4 "net/url"
5 "strings"
6
7 "github.com/pkg/errors"
8 "github.com/smallstep/cli/command"
9 "github.com/smallstep/cli/command/ca/provisioner"
10 "github.com/urfave/cli"
11 )
12
13 // init creates and registers the ca command
14 func init() {
15 cmd := cli.Command{
16 Name: "ca",
17 Usage: "initialize and manage a certificate authority",
18 UsageText: "step ca <subcommand> [arguments] [global-flags] [subcommand-flags]",
19 Description: `**step ca** command group provides facilities to initialize a certificate
20 authority, retrieve the root of trust, sign and renew certificates, and create
21 and manage provisioners.
22
23 ## EXAMPLES
24
25 Create the configuration for a new certificate authority:
26 '''
27 $ step ca init
28 '''
29
30 Configure the ca-url and root in the environment:
31 '''
32 $ step ca bootstrap \
33 --ca-url https://ca.smallstep.com \
34 --fingerprint 0d7d3834cf187726cf331c40a31aa7ef6b29ba4df601416c9788f6ee01058cf3
35 $ cat $STEPPATH/config/defaults.json
36 {
37 "ca-url": "https://ca.smallstep.com",
38 "fingerprint": "0d7d3834cf187726cf331c40a31aa7ef6b29ba4df601416c9788f6ee01058cf3",
39 "root": "/home/user/.step/certs/root_ca.crt"
40 }
41 '''
42
43 Download the root_ca.crt:
44 '''
45 $ step ca root root_ca.crt \
46 --ca-url https://ca.smallstep.com \
47 --fingerprint 0d7d3834cf187726cf331c40a31aa7ef6b29ba4df601416c9788f6ee01058cf3
48 '''
49
50 Get the Health status of the CA:
51 '''
52 $ step ca health --ca-url https://ca.smallstep.com --root /home/user/.step/certs/root_ca.crt
53 '''
54
55 Create a new certificate using a token:
56 '''
57 $ TOKEN=$(step ca token internal.example.com)
58 $ step ca certificate internal.example.com internal.crt internal.key \
59 --token $TOKEN --ca-url https://ca.smallstep.com --root root_ca.crt
60 '''
61
62 Renew a certificate (certificate must still be valid):
63 '''
64 $ step ca renew internal.crt internal.key \
65 --ca-url https://ca.smallstep.com --root root_ca.crt
66 '''`,
67 Subcommands: cli.Commands{
68 healthCommand(),
69 initCommand(),
70 bootstrapCommand(),
71 tokenCommand(),
72 certificateCommand(),
73 renewCertificateCommand(),
74 revokeCertificateCommand(),
75 provisioner.Command(),
76 signCertificateCommand(),
77 rootComand(),
78 rootsCommand(),
79 federationCommand(),
80 },
81 }
82
83 command.Register(cmd)
84 }
85
86 // common flags used in several commands
87 var (
88 acmeFlag = cli.StringFlag{
89 Name: "acme",
90 Usage: `ACME directory <url> to be used for requesting certificates via the ACME protocol.
91 Use this flag to define an ACME server other than the Step CA. If this flag is
92 absent and an ACME provisioner has been selected then the '--ca-url' flag must be defined.`,
93 }
94
95 acmeContactFlag = cli.StringSliceFlag{
96 Name: "contact",
97 Usage: `The <email-address> used for contact as part of the ACME protocol. These contacts
98 may be used to warn of certificate expiration or other certificate lifetime events.
99 Use the '--contact' flag multiple times to configure multiple contacts.`,
100 }
101
102 acmeHTTPListenFlag = cli.StringFlag{
103 Name: "http-listen",
104 Usage: `Use a non-standard http <address>, behind a reverse proxy or load balancer, for
105 serving ACME challenges. The default address is :80, which requires super user
106 (sudo) privileges. This flag must be used in conjunction with the '--standalone'
107 flag.`,
108 Value: ":80",
109 }
110 /*
111 TODO: Not implemented yet.
112 acmeHTTPSListenFlag = cli.StringFlag{
113 Name: "https-listen",
114 Usage: `Use a non-standard https address, behind a reverse proxy or load balancer, for
115 serving ACME challenges. The default address is :443, which requires super user
116 (sudo) privileges. This flag must be used in conjunction with the '--standalone'
117 flag.`,
118 Value: ":443",
119 }
120 */
121 acmeStandaloneFlag = cli.BoolFlag{
122 Name: "standalone",
123 Usage: `Get a certificate using the ACME protocol and standalone mode for validation.
124 Standalone is a mode in which the step process will run a server that will
125 will respond to ACME challenge validation requests. Standalone is the default
126 mode for serving challenge validation requests.`,
127 }
128
129 acmeWebrootFlag = cli.StringFlag{
130 Name: "webroot",
131 Usage: `Specify a <path> to use as a 'web root' for validation in the ACME protocol.
132 Webroot is a mode in which the step process will write a challenge file to a
133 location being served by an existing fileserver in order to respond to ACME
134 challenge validation requests.`,
135 }
136
137 consoleFlag = cli.BoolFlag{
138 Name: "console",
139 Usage: "Complete the flow while remaining inside the terminal",
140 }
141
142 fingerprintFlag = cli.StringFlag{
143 Name: "fingerprint",
144 Usage: "The <fingerprint> of the targeted root certificate.",
145 }
146
147 provisionerKidFlag = cli.StringFlag{
148 Name: "kid",
149 Usage: "The provisioner <kid> to use.",
150 }
151
152 sshHostFlag = cli.BoolFlag{
153 Name: "host",
154 Usage: `Create a host certificate instead of a user certificate.`,
155 }
156 )
157
158 // completeURL parses and validates the given URL. It supports general
159 // URLs like https://ca.smallstep.com[:port][/path], and incomplete URLs like
160 // ca.smallstep.com[:port][/path].
161 func completeURL(rawurl string) (string, error) {
162 u, err := url.Parse(rawurl)
163 if err != nil {
164 return "", errors.Wrapf(err, "error parsing url '%s'", rawurl)
165 }
166
167 // URLs are generally parsed as:
168 // [scheme:][//[userinfo@]host][/]path[?query][#fragment]
169 // But URLs that do not start with a slash after the scheme are interpreted as
170 // scheme:opaque[?query][#fragment]
171 if u.Opaque == "" {
172 if u.Scheme == "" {
173 u.Scheme = "https"
174 }
175 if u.Host == "" {
176 // rawurl looks like ca.smallstep.com or ca.smallstep.com/1.0/sign
177 if u.Path != "" {
178 parts := strings.SplitN(u.Path, "/", 2)
179 u.Host = parts[0]
180 if len(parts) == 2 {
181 u.Path = parts[1]
182 } else {
183 u.Path = ""
184 }
185 return completeURL(u.String())
186 }
187 return "", errors.Errorf("error parsing url '%s'", rawurl)
188 }
189 return u.String(), nil
190 }
191 // scheme:opaque[?query][#fragment]
192 // rawurl looks like ca.smallstep.com:443 or ca.smallstep.com:443/1.0/sign
193 return completeURL("https://" + rawurl)
194 }