| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 69 |
Subtle = cli.BoolFlag{ |
| 70 |
Name: "subtle", |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
Insecure = cli.BoolFlag{ |
| 75 |
Name: "insecure", |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 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 |
|
| 87 |
Force = cli.BoolFlag{ |
| 88 |
Name: "f,force", |
| 89 |
Usage: "Force the overwrite of files without asking.", |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
DryRun = cli.BoolFlag{ |
| 94 |
Name: "dry-run", |
| 95 |
Usage: "Executes the command without changing any file.", |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 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 |
|
| 106 |
|
| 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 |
|
| 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 |
|
| 121 |
|
| 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 |
|
| 132 |
|
| 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 |
|
| 143 |
Provisioner = cli.StringFlag{ |
| 144 |
Name: "provisioner,issuer", |
| 145 |
Usage: "The provisioner <name> to use.", |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 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 |
|
| 157 |
|
| 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 |
|
| 165 |
CaURL = cli.StringFlag{ |
| 166 |
Name: "ca-url", |
| 167 |
Usage: "<URI> of the targeted Step Certificate Authority.", |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
Root = cli.StringFlag{ |
| 172 |
Name: "root", |
| 173 |
Usage: "The path to the PEM <file> used as the root certificate authority.", |
| 174 |
} |
| 175 |
|
| 176 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 199 |
|
| 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 |
|
| 207 |
|
| 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 |
|
| 214 |
|
| 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 |
|
| 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 |
|
| 228 |
|
| 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 |
|
| 236 |
Team = cli.StringFlag{ |
| 237 |
Name: "team", |
| 238 |
Usage: "The team <ID> used to bootstrap the environment.", |
| 239 |
} |
| 240 |
|
| 241 |
|
| 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 |
|
| 249 |
RedirectURL = cli.StringFlag{ |
| 250 |
Name: "redirect-url", |
| 251 |
Usage: "Terminal OAuth redirect <url>.", |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 274 |
|
| 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 |
|
| 283 |
|
| 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 |
|
| 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 |
|
| 315 |
|
| 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 |
|
| 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 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 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 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 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 |
} |