Debian Codegraph @

flags/flags.go

raw symbols SCIP
1 package flags
2
3 import (
4 "encoding/json"
5 "fmt"
6 "net/url"
7 "path/filepath"
8 "strings"
9 "time"
10
11 "github.com/pkg/errors"
12 "github.com/smallstep/certificates/api"
13 "github.com/smallstep/cli/config"
14 "github.com/smallstep/cli/errs"
15 "github.com/smallstep/cli/utils"
16 "github.com/urfave/cli"
17 )
18
19 var (
20 // KTY is the flag to set the key type.
21 KTY = cli.StringFlag{
22 Name: "kty",
23 Value: "EC",
24 Usage: `The <kty> to build the certificate upon.
25 If unset, default is EC.
26
27 : <kty> is a case-sensitive string and must be one of:
28
29 **EC**
30 : Create an **elliptic curve** keypair
31
32 **OKP**
33 : Create an octet key pair (for **"Ed25519"** curve)
34
35 **RSA**
36 : Create an **RSA** keypair`,
37 }
38
39 // Size is the flag to set the key size.
40 Size = cli.IntFlag{
41 Name: "size",
42 Usage: `The <size> (in bits) of the key for RSA and oct key types. RSA keys require a
43 minimum key size of 2048 bits. If unset, default is 2048 bits for RSA keys and 128 bits for oct keys.`,
44 }
45
46 // Curve is the flag to se the key curve.
47 Curve = cli.StringFlag{
48 Name: "crv, curve",
49 Usage: `The elliptic <curve> to use for EC and OKP key types. Corresponds
50 to the **"crv"** JWK parameter. Valid curves are defined in JWA [RFC7518]. If
51 unset, default is P-256 for EC keys and Ed25519 for OKP keys.
52
53 : <curve> is a case-sensitive string and must be one of:
54
55 **P-256**
56 : NIST P-256 Curve
57
58 **P-384**
59 : NIST P-384 Curve
60
61 **P-521**
62 : NIST P-521 Curve
63
64 **Ed25519**
65 : Ed25519 Curve`,
66 }
67
68 // Subtle is the flag required for delicate operations.
69 Subtle = cli.BoolFlag{
70 Name: "subtle",
71 }
72
73 // Insecure is the flag required on insecure operations
74 Insecure = cli.BoolFlag{
75 Name: "insecure",
76 }
77
78 // K8sSATokenPathFlag is an optional flag that allows modification of the
79 // kubernetes service account token path.
80 K8sSATokenPathFlag = cli.StringFlag{
81 Name: "k8ssa-token-path",
82 Usage: `Configure the <file> from which to read the kubernetes service account token.`,
83 Value: `/var/run/secrets/kubernetes.io/serviceaccount/token`,
84 }
85
86 // Force is a cli.Flag used to overwrite files.
87 Force = cli.BoolFlag{
88 Name: "f,force",
89 Usage: "Force the overwrite of files without asking.",
90 }
91
92 // DryRun is a cli.Flag used to avoid the writing of files.
93 DryRun = cli.BoolFlag{
94 Name: "dry-run",
95 Usage: "Executes the command without changing any file.",
96 }
97
98 // PasswordFile is a cli.Flag used to pass a file to encrypt or decrypt a
99 // private key.
100 PasswordFile = cli.StringFlag{
101 Name: "password-file",
102 Usage: `The path to the <file> containing the password to encrypt or decrypt the private key.`,
103 }
104
105 // NoPassword is a cli.Flag used to avoid using a password to encrypt private
106 // keys.
107 NoPassword = cli.BoolFlag{
108 Name: "no-password",
109 Usage: `Do not ask for a password to encrypt a private key. Sensitive key material will
110 be written to disk unencrypted. This is not recommended. Requires **--insecure** flag.`,
111 }
112
113 // Token is a cli.Flag used to pass the CA token.
114 Token = cli.StringFlag{
115 Name: "token",
116 Usage: `The one-time <token> used to authenticate with the CA in order to create the
117 certificate.`,
118 }
119
120 // NotBefore is a cli.Flag used to pass the start period of the certificate
121 // validity.
122 NotBefore = cli.StringFlag{
123 Name: "not-before",
124 Usage: `The <time|duration> when the certificate validity period starts. If a <time> is
125 used it is expected to be in RFC 3339 format. If a <duration> is used, it is a
126 sequence of decimal numbers, each with optional fraction and a unit suffix, such
127 as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms",
128 "s", "m", "h".`,
129 }
130
131 // NotAfter is a cli.Flag used to pass the end period of the certificate
132 // validity.
133 NotAfter = cli.StringFlag{
134 Name: "not-after",
135 Usage: `The <time|duration> when the certificate validity period ends. If a <time> is
136 used it is expected to be in RFC 3339 format. If a <duration> is used, it is a
137 sequence of decimal numbers, each with optional fraction and a unit suffix, such
138 as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms",
139 "s", "m", "h".`,
140 }
141
142 // Provisioner is a cli.Flag used to pass the CA provisioner to use.
143 Provisioner = cli.StringFlag{
144 Name: "provisioner,issuer",
145 Usage: "The provisioner <name> to use.",
146 }
147
148 // ProvisionerPasswordFile is a cli.Flag used to pass the password file to
149 // decrypt the generating key.
150 ProvisionerPasswordFile = cli.StringFlag{
151 Name: "provisioner-password-file",
152 Usage: `The path to the <file> containing the password to decrypt the one-time token
153 generating key.`,
154 }
155
156 // ProvisionerPasswordFileWithAlias is a cli.Flag that allows multiple
157 // alias flag names for the ProvisionerPasswordFile.
158 ProvisionerPasswordFileWithAlias = cli.StringFlag{
159 Name: "provisioner-password-file,password-file",
160 Usage: `The path to the <file> containing the password to decrypt the one-time token
161 generating key.`,
162 }
163
164 // CaURL is a cli.Flag used to pass the CA url.
165 CaURL = cli.StringFlag{
166 Name: "ca-url",
167 Usage: "<URI> of the targeted Step Certificate Authority.",
168 }
169
170 // Root is a cli.Flag used to pass the path of the root certificate to use.
171 Root = cli.StringFlag{
172 Name: "root",
173 Usage: "The path to the PEM <file> used as the root certificate authority.",
174 }
175
176 // Offline is a cli.Flag used to activate the offline flow.
177 Offline = cli.BoolFlag{
178 Name: "offline",
179 Usage: `Creates a certificate without contacting the certificate authority. Offline mode
180 uses the configuration, certificates, and keys created with **step ca init**,
181 but can accept a different configuration file using **--ca-config** flag.`,
182 }
183
184 // CaConfig is a cli.Flag used to pass the CA configuration file.
185 CaConfig = cli.StringFlag{
186 Name: "ca-config",
187 Usage: `The <path> to the certificate authority configuration file. Defaults to
188 $STEPPATH/config/ca.json`,
189 Value: filepath.Join(config.StepPath(), "config", "ca.json"),
190 }
191
192 // X5cCert is a cli.Flag used to pass the x5c header certificate for a JWT.
193 X5cCert = cli.StringFlag{
194 Name: "x5c-cert",
195 Usage: "Certificate (<chain>) in PEM format to store in the 'x5c' header of a JWT.",
196 }
197
198 // X5cKey is a cli.Flag used to pass the private key (corresponding to the x5c-cert)
199 // that is used to sign the token.
200 X5cKey = cli.StringFlag{
201 Name: "x5c-key",
202 Usage: `Private key <path>, used to sign a JWT, corresponding to the certificate that will
203 be stored in the 'x5c' header.`,
204 }
205
206 // X5tCert is a cli.Flag used to pass the x5t header certificate thumbprint
207 // for a JWS or JWT.
208 X5tCert = cli.StringFlag{
209 Name: "x5t-cert",
210 Usage: "Certificate <path> in PEM format to use for the 'x5t' header of a JWS or JWT",
211 }
212
213 // X5tKey is a cli.Flag used to pass the private key (corresponding to the x5t-cert)
214 // that is used to sign the token.
215 X5tKey = cli.StringFlag{
216 Name: "x5t-key",
217 Usage: `Private key <path>, used to sign a JWT, corresponding to the certificate used for
218 the 'x5t' header.`,
219 }
220
221 // SSHPOPCert is a cli.Flag used to pass the sshpop header certificate for a JWT.
222 SSHPOPCert = cli.StringFlag{
223 Name: "sshpop-cert",
224 Usage: "Certificate (<chain>) in PEM format to store in the 'sshpop' header of a JWT.",
225 }
226
227 // SSHPOPKey is a cli.Flag used to pass the private key (corresponding to the sshpop-cert)
228 // that is used to sign the token.
229 SSHPOPKey = cli.StringFlag{
230 Name: "sshpop-key",
231 Usage: `Private key <path>, used to sign a JWT, corresponding to the certificate that will
232 be stored in the 'sshpop' header.`,
233 }
234
235 // Team is a cli.Flag used to pass the team ID.
236 Team = cli.StringFlag{
237 Name: "team",
238 Usage: "The team <ID> used to bootstrap the environment.",
239 }
240
241 // TeamURL is a cli.Flag used to pass the team URL.
242 TeamURL = cli.StringFlag{
243 Name: "team-url",
244 Usage: `The <url> step queries to retrieve initial team configuration. Only used with
245 the **--team** option. If the url contains <\<\>> placeholders, they are replaced with the team ID.`,
246 }
247
248 // RedirectURL is a cli.Flag used to pass the OAuth redirect URL.
249 RedirectURL = cli.StringFlag{
250 Name: "redirect-url",
251 Usage: "Terminal OAuth redirect <url>.",
252 }
253
254 // ServerName is a cli.Flag used to set the TLS Server Name Indication in
255 // request to a server.
256 ServerName = cli.StringFlag{
257 Name: "servername",
258 Usage: `TLS Server Name Indication that should be sent to request a specific certificate from the server.`,
259 }
260
261 // TemplateSet is a cli.Flag used to send key-value pairs to the ca.
262 TemplateSet = cli.StringSliceFlag{
263 Name: "set",
264 Usage: "The <key=value> pair with template data variables to send to the CA. Use the **--set** flag multiple times to add multiple variables.",
265 }
266
267 // TemplateSetFile is a cli.Flag used to send a JSON file to the CA.
268 TemplateSetFile = cli.StringFlag{
269 Name: "set-file",
270 Usage: "The <path> of a JSON file with the template data to send to the CA.",
271 }
272
273 // Identity is a cli.Flag used to be able to define the identity argument in
274 // defaults.json.
275 Identity = cli.StringFlag{
276 Name: "identity",
277 Usage: `The certificate identity. It is usually passed as a positional argument, but a
278 flag exists so it can be configured in $STEPPATH/config/defaults.json.`,
279 }
280 )
281
282 // ParseTimeOrDuration is a helper that returns the time or the current time
283 // with an extra duration. It's used in flags like --not-before, --not-after.
284 func ParseTimeOrDuration(s string) (time.Time, bool) {
285 if s == "" {
286 return time.Time{}, true
287 }
288
289 var t time.Time
290 if err := t.UnmarshalText([]byte(s)); err != nil {
291 d, err := time.ParseDuration(s)
292 if err != nil {
293 return time.Time{}, false
294 }
295 t = time.Now().Add(d)
296 }
297 return t, true
298 }
299
300 // ParseTimeDuration parses the not-before and not-after flags as a timeDuration
301 func ParseTimeDuration(ctx *cli.Context) (notBefore api.TimeDuration, notAfter api.TimeDuration, err error) {
302 var zero api.TimeDuration
303 notBefore, err = api.ParseTimeDuration(ctx.String("not-before"))
304 if err != nil {
305 return zero, zero, errs.InvalidFlagValue(ctx, "not-before", ctx.String("not-before"), "")
306 }
307 notAfter, err = api.ParseTimeDuration(ctx.String("not-after"))
308 if err != nil {
309 return zero, zero, errs.InvalidFlagValue(ctx, "not-after", ctx.String("not-after"), "")
310 }
311 return
312 }
313
314 // ParseTemplateData parses the set and and set-file flags and returns a json
315 // message to be used in certificate templates.
316 func ParseTemplateData(ctx *cli.Context) (json.RawMessage, error) {
317 data := make(map[string]interface{})
318 if path := ctx.String("set-file"); path != "" {
319 b, err := utils.ReadFile(path)
320 if err != nil {
321 return nil, err
322 }
323 if err := json.Unmarshal(b, &data); err != nil {
324 return nil, errors.Wrapf(err, "error unmarshaling %s", path)
325 }
326 }
327
328 keyValues := ctx.StringSlice("set")
329 for _, s := range keyValues {
330 i := strings.Index(s, "=")
331 if i == -1 {
332 return nil, errs.InvalidFlagValue(ctx, "set", s, "")
333 }
334 key, value := s[:i], s[i+1:]
335
336 // If the value is not json, use the raw string.
337 var v interface{}
338 if err := json.Unmarshal([]byte(value), &v); err == nil {
339 data[key] = v
340 } else {
341 data[key] = value
342 }
343 }
344
345 if len(data) == 0 {
346 return nil, nil
347 }
348
349 return json.Marshal(data)
350 }
351
352 // ParseCaURL gets and parses the ca-url from the command context.
353 // - Require non-empty value.
354 // - Prepend an 'https' scheme if the URL does not have a scheme.
355 // - Error if the URL scheme is not implicitly or explicitly 'https'.
356 func ParseCaURL(ctx *cli.Context) (string, error) {
357 caURL := ctx.String("ca-url")
358 if len(caURL) == 0 {
359 return "", errs.RequiredFlag(ctx, "ca-url")
360 }
361
362 return parseCaURL(ctx, caURL)
363 }
364
365 // ParseCaURLIfExists gets and parses the ca-url from the command context, if
366 // one is present.
367 // - Allow empty value.
368 // - Prepend an 'https' scheme if the URL does not have a scheme.
369 // - Error if the URL scheme is not implicitly or explicitly 'https'.
370 func ParseCaURLIfExists(ctx *cli.Context) (string, error) {
371 caURL := ctx.String("ca-url")
372 if len(caURL) == 0 {
373 return "", nil
374 }
375 return parseCaURL(ctx, caURL)
376 }
377
378 func parseCaURL(ctx *cli.Context, caURL string) (string, error) {
379 if !strings.Contains(caURL, "://") {
380 caURL = "https://" + caURL
381 }
382 u, err := url.Parse(caURL)
383 if err != nil {
384 return "", errs.InvalidFlagValueMsg(ctx, "ca-url", caURL, "invalid URL")
385 }
386 if u.Scheme != "https" {
387 return "", errs.InvalidFlagValueMsg(ctx, "ca-url", caURL, "must have https scheme")
388 }
389 return fmt.Sprintf("%s://%s", u.Scheme, u.Host), nil
390 }