| 1 |
package ca |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"os" |
| 6 |
|
| 7 |
"github.com/smallstep/certificates/api" |
| 8 |
"github.com/smallstep/certificates/pki" |
| 9 |
"github.com/smallstep/cli/command" |
| 10 |
"github.com/smallstep/cli/errs" |
| 11 |
"github.com/smallstep/cli/flags" |
| 12 |
"github.com/smallstep/cli/utils" |
| 13 |
"github.com/smallstep/cli/utils/cautils" |
| 14 |
"github.com/urfave/cli" |
| 15 |
) |
| 16 |
|
| 17 |
func tokenCommand() cli.Command { |
| 18 |
|
| 19 |
certNotBeforeFlag := flags.NotBefore |
| 20 |
certNotAfterFlag := flags.NotAfter |
| 21 |
certNotBeforeFlag.Name = "cert-not-before" |
| 22 |
certNotAfterFlag.Name = "cert-not-after" |
| 23 |
|
| 24 |
return cli.Command{ |
| 25 |
Name: "token", |
| 26 |
Action: command.ActionFunc(tokenAction), |
| 27 |
Usage: "generate an OTT granting access to the CA", |
| 28 |
UsageText: `**step ca token** <subject> |
| 29 |
[--**kid**=<kid>] [--**issuer**=<name>] [**--ca-url**=<uri>] [**--root**=<path>] |
| 30 |
[**--cert-not-before**=<time|duration>] [**--cert-not-after**=<time|duration>] |
| 31 |
[**--not-before**=<time|duration>] [**--not-after**=<time|duration>] |
| 32 |
[**--password-file**=<path>] [**--output-file**=<path>] [**--key**=<path>] |
| 33 |
[**--san**=<SAN>] [**--offline**] [**--revoke**] |
| 34 |
[**--x5c-cert**=<path>] [**--x5c-key**=<path>] |
| 35 |
[**--sshpop-cert**=<path>] [**--sshpop-key**=<path>] |
| 36 |
[**--ssh**] [**--host**] [**--principal**=<string>] |
| 37 |
[**--k8ssa-token-path**=<path>`, |
| 38 |
Description: `**step ca token** command generates a one-time token granting access to the |
| 39 |
certificates authority. |
| 40 |
|
| 41 |
## POSITIONAL ARGUMENTS |
| 42 |
|
| 43 |
<subject> |
| 44 |
: The Common Name, DNS Name, or IP address that will be set by the certificate authority. |
| 45 |
When there are no additional Subject Alternative Names configured (via the |
| 46 |
--san flag), the subject will be added as the only element of the 'sans' claim |
| 47 |
on the token. |
| 48 |
|
| 49 |
## EXAMPLES |
| 50 |
|
| 51 |
Most of the following examples assumes that **--ca-url** and **--root** are |
| 52 |
set using environment variables or the default configuration file in |
| 53 |
<$STEPPATH/config/defaults.json>. |
| 54 |
|
| 55 |
Get a new token for a DNS. Because there are no Subject Alternative Names |
| 56 |
configured (via the '--san' flag), the 'sans' claim of the token will have a |
| 57 |
default value of ['internal.example.com']: |
| 58 |
''' |
| 59 |
$ step ca token internal.example.com |
| 60 |
''' |
| 61 |
|
| 62 |
Get a new token for a 'Revoke' request: |
| 63 |
''' |
| 64 |
$ step ca token --revoke 146103349666685108195655980390445292315 |
| 65 |
''' |
| 66 |
|
| 67 |
Get a new token for an IP address. Because there are no Subject Alternative Names |
| 68 |
configured (via the '--san' flag), the 'sans' claim of the token will have a |
| 69 |
default value of ['192.168.10.10']: |
| 70 |
''' |
| 71 |
$ step ca token 192.168.10.10 |
| 72 |
''' |
| 73 |
|
| 74 |
Get a new token with custom Subject Alternative Names. The value of the 'sans' |
| 75 |
claim of the token will be ['1.1.1.1', 'hello.example.com'] - 'foobar' will not |
| 76 |
be in the 'sans' claim unless explicitly configured via the '--san' flag: |
| 77 |
''' |
| 78 |
$ step ca token foobar --san 1.1.1.1 --san hello.example.com |
| 79 |
''' |
| 80 |
|
| 81 |
Get a new token that expires in 30 minutes: |
| 82 |
''' |
| 83 |
$ step ca token --not-after 30m internal.example.com |
| 84 |
''' |
| 85 |
|
| 86 |
Get a new token that becomes valid in 30 minutes and expires 5 minutes after that: |
| 87 |
''' |
| 88 |
$ step ca token --not-before 30m --not-after 35m internal.example.com |
| 89 |
''' |
| 90 |
|
| 91 |
Get a new token signed with the given private key, the public key must be |
| 92 |
configured in the certificate authority: |
| 93 |
''' |
| 94 |
$ step ca token internal.smallstep.com --key token.key |
| 95 |
''' |
| 96 |
|
| 97 |
Get a new token for a specific provisioner kid, ca-url and root: |
| 98 |
''' |
| 99 |
$ step ca token internal.example.com \ |
| 100 |
--kid 4vn46fbZT68Uxfs9LBwHkTvrjEvxQqx-W8nnE-qDjts \ |
| 101 |
--ca-url https://ca.example.com \ |
| 102 |
--root /path/to/root_ca.crt |
| 103 |
''' |
| 104 |
|
| 105 |
Get a new token using the simple offline mode, requires the configuration |
| 106 |
files, certificates, and keys created with **step ca init**: |
| 107 |
''' |
| 108 |
$ step ca token internal.example.com --offline |
| 109 |
''' |
| 110 |
|
| 111 |
Get a new token using the offline mode with all the parameters: |
| 112 |
''' |
| 113 |
$ step ca token internal.example.com \ |
| 114 |
--offline \ |
| 115 |
--kid 4vn46fbZT68Uxfs9LBwHkTvrjEvxQqx-W8nnE-qDjts \ |
| 116 |
--issuer [email protected] \ |
| 117 |
--key provisioner.key \ |
| 118 |
--ca-url https://ca.example.com \ |
| 119 |
--root /path/to/root_ca.crt |
| 120 |
''' |
| 121 |
|
| 122 |
Get a new token for a 'Revoke' request: |
| 123 |
''' |
| 124 |
$ step ca token --revoke 146103349666685108195655980390445292315 |
| 125 |
''' |
| 126 |
|
| 127 |
Get a new token in offline mode for a 'Revoke' request: |
| 128 |
''' |
| 129 |
$ step ca token --offline --revoke 146103349666685108195655980390445292315 |
| 130 |
''' |
| 131 |
|
| 132 |
Get a new token for an SSH user certificate: |
| 133 |
''' |
| 134 |
$ step ca token [email protected] --ssh |
| 135 |
''' |
| 136 |
|
| 137 |
Get a new token for an SSH host certificate: |
| 138 |
''' |
| 139 |
$ step ca token my-remote.hostname --ssh --host |
| 140 |
'''`, |
| 141 |
Flags: []cli.Flag{ |
| 142 |
certNotAfterFlag, |
| 143 |
certNotBeforeFlag, |
| 144 |
provisionerKidFlag, |
| 145 |
cli.StringSliceFlag{ |
| 146 |
Name: "san", |
| 147 |
Usage: `Add <dns|ip|email|uri> Subject Alternative Name(s) (SANs) |
| 148 |
that should be authorized. A certificate signing request using this token must |
| 149 |
match the complete set of SANs in the token 1:1. Use the '--san' flag multiple |
| 150 |
times to configure multiple SANs.`, |
| 151 |
}, |
| 152 |
cli.StringSliceFlag{ |
| 153 |
Name: "principal,n", |
| 154 |
Usage: `Add the principals (user or host <name>s) that the token is authorized to |
| 155 |
request. The signing request using this token won't be able to add |
| 156 |
extra names. Use the '--principal' flag multiple times to configure |
| 157 |
multiple principals.`, |
| 158 |
}, |
| 159 |
sshHostFlag, |
| 160 |
flags.CaURL, |
| 161 |
flags.CaConfig, |
| 162 |
flags.Force, |
| 163 |
flags.NotAfter, |
| 164 |
flags.NotBefore, |
| 165 |
flags.Offline, |
| 166 |
flags.Root, |
| 167 |
flags.Provisioner, |
| 168 |
flags.ProvisionerPasswordFileWithAlias, |
| 169 |
flags.X5cCert, |
| 170 |
flags.X5cKey, |
| 171 |
flags.SSHPOPCert, |
| 172 |
flags.SSHPOPKey, |
| 173 |
cli.StringFlag{ |
| 174 |
Name: "key", |
| 175 |
Usage: `The private key <path> used to sign the JWT. This is usually downloaded from |
| 176 |
the certificate authority.`, |
| 177 |
}, |
| 178 |
cli.StringFlag{ |
| 179 |
Name: "output-file", |
| 180 |
Usage: "The destination <file> of the generated one-time token.", |
| 181 |
}, |
| 182 |
cli.BoolFlag{ |
| 183 |
Name: "revoke", |
| 184 |
Usage: `Create a token for authorizing 'Revoke' requests. The audience will |
| 185 |
be invalid for any other API request.`, |
| 186 |
}, |
| 187 |
cli.BoolFlag{ |
| 188 |
Name: "renew", |
| 189 |
Usage: `Create a token for authorizing 'renew' requests. The audience will |
| 190 |
be invalid for any other API request.`, |
| 191 |
}, |
| 192 |
cli.BoolFlag{ |
| 193 |
Name: "rekey", |
| 194 |
Usage: `Create a token for authorizing 'rekey' requests. The audience will |
| 195 |
be invalid for any other API request.`, |
| 196 |
}, |
| 197 |
cli.BoolFlag{ |
| 198 |
Name: "ssh", |
| 199 |
Usage: `Create a token for authorizing an SSH certificate signing request.`, |
| 200 |
}, |
| 201 |
flags.K8sSATokenPathFlag, |
| 202 |
}, |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
func tokenAction(ctx *cli.Context) error { |
| 207 |
if err := errs.NumberOfArguments(ctx, 1); err != nil { |
| 208 |
return err |
| 209 |
} |
| 210 |
|
| 211 |
subject := ctx.Args().Get(0) |
| 212 |
outputFile := ctx.String("output-file") |
| 213 |
offline := ctx.Bool("offline") |
| 214 |
|
| 215 |
sans := ctx.StringSlice("san") |
| 216 |
isRevoke := ctx.Bool("revoke") |
| 217 |
isRenew := ctx.Bool("renew") |
| 218 |
isRekey := ctx.Bool("rekey") |
| 219 |
|
| 220 |
isSSH := ctx.Bool("ssh") |
| 221 |
isHost := ctx.Bool("host") |
| 222 |
principals := ctx.StringSlice("principal") |
| 223 |
|
| 224 |
switch { |
| 225 |
case isSSH && len(sans) > 0: |
| 226 |
return errs.IncompatibleFlagWithFlag(ctx, "ssh", "san") |
| 227 |
case isHost && len(sans) > 0: |
| 228 |
return errs.IncompatibleFlagWithFlag(ctx, "host", "san") |
| 229 |
case len(principals) > 0 && len(sans) > 0: |
| 230 |
return errs.IncompatibleFlagWithFlag(ctx, "principal", "san") |
| 231 |
case !isSSH && isHost: |
| 232 |
return errs.RequiredWithFlag(ctx, "host", "ssh") |
| 233 |
case !isSSH && len(principals) > 0: |
| 234 |
return errs.RequiredWithFlag(ctx, "principal", "ssh") |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
var typ int |
| 239 |
if isSSH { |
| 240 |
switch { |
| 241 |
case isRevoke: |
| 242 |
typ = cautils.SSHRevokeType |
| 243 |
case isRenew: |
| 244 |
typ = cautils.SSHRenewType |
| 245 |
case isRekey: |
| 246 |
typ = cautils.SSHRekeyType |
| 247 |
case isHost: |
| 248 |
typ = cautils.SSHHostSignType |
| 249 |
sans = principals |
| 250 |
default: |
| 251 |
typ = cautils.SSHUserSignType |
| 252 |
sans = principals |
| 253 |
} |
| 254 |
} else { |
| 255 |
switch { |
| 256 |
case isRevoke: |
| 257 |
typ = cautils.RevokeType |
| 258 |
default: |
| 259 |
typ = cautils.SignType |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
caURL, err := flags.ParseCaURL(ctx) |
| 264 |
if err != nil { |
| 265 |
return err |
| 266 |
} |
| 267 |
|
| 268 |
root := ctx.String("root") |
| 269 |
if len(root) == 0 { |
| 270 |
root = pki.GetRootCAPath() |
| 271 |
if _, err := os.Stat(root); err != nil { |
| 272 |
return errs.RequiredFlag(ctx, "root") |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
if typ == cautils.RevokeType && len(sans) > 0 { |
| 278 |
return errs.IncompatibleFlagWithFlag(ctx, "san", "revoke") |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
notBefore, ok := flags.ParseTimeOrDuration(ctx.String("not-before")) |
| 283 |
if !ok { |
| 284 |
return errs.InvalidFlagValue(ctx, "not-before", ctx.String("not-before"), "") |
| 285 |
} |
| 286 |
notAfter, ok := flags.ParseTimeOrDuration(ctx.String("not-after")) |
| 287 |
if !ok { |
| 288 |
return errs.InvalidFlagValue(ctx, "not-after", ctx.String("not-after"), "") |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
certNotBefore, err := api.ParseTimeDuration(ctx.String("cert-not-before")) |
| 293 |
if err != nil { |
| 294 |
return errs.InvalidFlagValue(ctx, "cert-not-before", ctx.String("cert-not-before"), "") |
| 295 |
} |
| 296 |
certNotAfter, err := api.ParseTimeDuration(ctx.String("cert-not-after")) |
| 297 |
if err != nil { |
| 298 |
return errs.InvalidFlagValue(ctx, "cert-not-after", ctx.String("cert-not-after"), "") |
| 299 |
} |
| 300 |
|
| 301 |
var token string |
| 302 |
if offline { |
| 303 |
token, err = cautils.OfflineTokenFlow(ctx, typ, subject, sans, notBefore, notAfter, certNotBefore, certNotAfter) |
| 304 |
if err != nil { |
| 305 |
return err |
| 306 |
} |
| 307 |
} else { |
| 308 |
token, err = cautils.NewTokenFlow(ctx, typ, subject, sans, caURL, root, notBefore, notAfter, certNotBefore, certNotAfter) |
| 309 |
if err != nil { |
| 310 |
return err |
| 311 |
} |
| 312 |
} |
| 313 |
if len(outputFile) > 0 { |
| 314 |
return utils.WriteFile(outputFile, []byte(token), 0600) |
| 315 |
} |
| 316 |
fmt.Println(token) |
| 317 |
return nil |
| 318 |
} |