| 1 |
package provisioner |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/ecdsa" |
| 5 |
"crypto/ed25519" |
| 6 |
"crypto/rsa" |
| 7 |
"crypto/x509" |
| 8 |
"encoding/pem" |
| 9 |
"io/ioutil" |
| 10 |
"net/url" |
| 11 |
"strings" |
| 12 |
|
| 13 |
"github.com/pkg/errors" |
| 14 |
"github.com/smallstep/certificates/authority" |
| 15 |
"github.com/smallstep/certificates/authority/provisioner" |
| 16 |
"github.com/smallstep/cli/crypto/pemutil" |
| 17 |
"github.com/smallstep/cli/errs" |
| 18 |
"github.com/smallstep/cli/flags" |
| 19 |
"github.com/smallstep/cli/jose" |
| 20 |
"github.com/smallstep/cli/ui" |
| 21 |
"github.com/smallstep/cli/utils" |
| 22 |
"github.com/urfave/cli" |
| 23 |
) |
| 24 |
|
| 25 |
func addCommand() cli.Command { |
| 26 |
return cli.Command{ |
| 27 |
Name: "add", |
| 28 |
Action: cli.ActionFunc(addAction), |
| 29 |
Usage: "add one or more provisioners the CA configuration", |
| 30 |
UsageText: `**step ca provisioner add** <name> <jwk-file> [<jwk-file> ...] |
| 31 |
**--ca-config**=<file> [**--type**=JWK] [**--create**] [**--password-file**=<file>] |
| 32 |
|
| 33 |
**step ca provisioner add** <name> **--type**=OIDC **--ca-config**=<file> |
| 34 |
[**--client-id**=<id>] [**--client-secret**=<secret>] |
| 35 |
[**--configuration-endpoint**=<url>] [**--domain**=<domain>] |
| 36 |
[**--admin**=<email>]... |
| 37 |
|
| 38 |
**step ca provisioner add** <name> **--type**=x5c **--x5c-root**=<file> |
| 39 |
[**--ca-config**=<file>]... |
| 40 |
|
| 41 |
**step ca provisioner add** <name> **--type**=k8sSA |
| 42 |
[**--pem-keys=<file>**] [**--ca-config**=<file>]... |
| 43 |
|
| 44 |
**step ca provisioner add** <name> **--type**=[AWS|Azure|GCP] |
| 45 |
[**--ca-config**=<file>] [**--aws-account**=<id>] |
| 46 |
[**--gcp-service-account**=<name>] [**--gcp-project**=<name>] |
| 47 |
[**--azure-tenant**=<id>] [**--azure-resource-group**=<name>] |
| 48 |
[**--instance-age**=<duration>] [**--iid-roots**=<path>] |
| 49 |
[**--disable-custom-sans**] [**--disable-trust-on-first-use**] |
| 50 |
|
| 51 |
**step ca provisioner add** <name> **--type**=ACME **--ca-config**=<file>`, |
| 52 |
Flags: []cli.Flag{ |
| 53 |
flags.CaConfig, |
| 54 |
cli.StringFlag{ |
| 55 |
Name: "type", |
| 56 |
Value: provisioner.TypeJWK.String(), |
| 57 |
Usage: `The <type> of provisioner to create. |
| 58 |
|
| 59 |
: <type> is a case-insensitive string and must be one of: |
| 60 |
|
| 61 |
**JWK** |
| 62 |
: Uses an JWK key pair to sign provisioning tokens. (default) |
| 63 |
|
| 64 |
**OIDC** |
| 65 |
: Uses an OpenID Connect provider to sign provisioning tokens. |
| 66 |
|
| 67 |
**AWS** |
| 68 |
: Uses Amazon AWS instance identity documents. |
| 69 |
|
| 70 |
**GCP** |
| 71 |
: Use Google instance identity tokens. |
| 72 |
|
| 73 |
**Azure** |
| 74 |
: Uses Microsoft Azure identity tokens. |
| 75 |
|
| 76 |
**ACME** |
| 77 |
: Uses the ACME protocol to create certificates. |
| 78 |
|
| 79 |
**X5C** |
| 80 |
: Uses an X509 Certificate / private key pair to sign provisioning tokens. |
| 81 |
|
| 82 |
**K8sSA** |
| 83 |
: Uses Kubernetes Service Account tokens. |
| 84 |
|
| 85 |
**SSHPOP** |
| 86 |
: Uses an SSH Certificate / private key pair to sign provisioning tokens.`, |
| 87 |
}, |
| 88 |
flags.PasswordFile, |
| 89 |
cli.BoolFlag{ |
| 90 |
Name: "create", |
| 91 |
Usage: `Create a new ECDSA key pair using curve P-256 and populate a new JWK |
| 92 |
provisioner with it.`, |
| 93 |
}, |
| 94 |
cli.BoolFlag{ |
| 95 |
Name: "ssh", |
| 96 |
Usage: `Enable SSH on the new provisioners.`, |
| 97 |
}, |
| 98 |
|
| 99 |
|
| 100 |
cli.StringFlag{ |
| 101 |
Name: "client-id", |
| 102 |
Usage: `The <id> used to validate the audience in an OpenID Connect token.`, |
| 103 |
}, |
| 104 |
cli.StringFlag{ |
| 105 |
Name: "client-secret", |
| 106 |
Usage: `The <secret> used to obtain the OpenID Connect tokens.`, |
| 107 |
}, |
| 108 |
cli.StringFlag{ |
| 109 |
Name: "listen-address", |
| 110 |
Usage: `The callback <address> used in the OpenID Connect flow (e.g. \":10000\")`, |
| 111 |
}, |
| 112 |
cli.StringFlag{ |
| 113 |
Name: "configuration-endpoint", |
| 114 |
Usage: `OpenID Connect configuration <url>.`, |
| 115 |
}, |
| 116 |
cli.StringSliceFlag{ |
| 117 |
Name: "admin", |
| 118 |
Usage: `The <email> of an admin user in an OpenID Connect provisioner, this user |
| 119 |
will not have restrictions in the certificates to sign. Use the |
| 120 |
'--admin' flag multiple times to configure multiple administrators.`, |
| 121 |
}, |
| 122 |
cli.StringSliceFlag{ |
| 123 |
Name: "domain", |
| 124 |
Usage: `The <domain> used to validate the email claim in an OpenID Connect provisioner. |
| 125 |
Use the '--domain' flag multiple times to configure multiple domains.`, |
| 126 |
}, |
| 127 |
|
| 128 |
|
| 129 |
cli.StringSliceFlag{ |
| 130 |
Name: "aws-account", |
| 131 |
Usage: `The AWS account <id> used to validate the identity documents. |
| 132 |
Use the flag multiple times to configure multiple accounts.`, |
| 133 |
}, |
| 134 |
cli.StringFlag{ |
| 135 |
Name: "azure-tenant", |
| 136 |
Usage: `The Microsoft Azure tenant <id> used to validate the identity tokens.`, |
| 137 |
}, |
| 138 |
cli.StringSliceFlag{ |
| 139 |
Name: "azure-resource-group", |
| 140 |
Usage: `The Microsoft Azure resource group <name> used to validate the identity tokens. |
| 141 |
Use the flag multipl etimes to configure multiple resource groups`, |
| 142 |
}, |
| 143 |
cli.StringSliceFlag{ |
| 144 |
Name: "gcp-service-account", |
| 145 |
Usage: `The Google service account <email> or <id> used to validate the identity tokens. |
| 146 |
Use the flag multiple times to configure multiple service accounts.`, |
| 147 |
}, |
| 148 |
cli.StringSliceFlag{ |
| 149 |
Name: "gcp-project", |
| 150 |
Usage: `The Google project <id> used to validate the identity tokens. |
| 151 |
Use the flag multipl etimes to configure multiple projects`, |
| 152 |
}, |
| 153 |
cli.DurationFlag{ |
| 154 |
Name: "instance-age", |
| 155 |
Usage: `The maximum <duration> to grant a certificate in AWS and GCP provisioners. |
| 156 |
A <duration> is sequence of decimal numbers, each with optional fraction and a |
| 157 |
unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", |
| 158 |
"us" (or "µs"), "ms", "s", "m", "h".`, |
| 159 |
}, |
| 160 |
cli.StringFlag{ |
| 161 |
Name: "iid-roots", |
| 162 |
Usage: `The <path> to the file containing the certificates used to validate the |
| 163 |
instance identity documents in AWS.`, |
| 164 |
}, |
| 165 |
cli.BoolFlag{ |
| 166 |
Name: "disable-custom-sans", |
| 167 |
Usage: `On cloud provisioners, if anabled only the internal DNS and IP will be added as a SAN. |
| 168 |
By default it will accept any SAN in the CSR.`, |
| 169 |
}, |
| 170 |
cli.BoolFlag{ |
| 171 |
Name: "disable-trust-on-first-use,disable-tofu", |
| 172 |
Usage: `On cloud provisioners, if enabled multiple sign request for this provisioner |
| 173 |
with the same instance will be accepted. By default only the first request |
| 174 |
will be accepted.`, |
| 175 |
}, |
| 176 |
|
| 177 |
|
| 178 |
cli.StringFlag{ |
| 179 |
Name: "x5c-root", |
| 180 |
Usage: `Root certificate (chain) <file> used to validate the signature on X5C |
| 181 |
provisioning tokens.`, |
| 182 |
}, |
| 183 |
|
| 184 |
cli.StringFlag{ |
| 185 |
Name: "pem-keys", |
| 186 |
Usage: `Public key <file> for validating signatures on K8s Service Account Tokens. |
| 187 |
PEM formatted bundle (can have multiple PEM blocks in the same file) of public |
| 188 |
keys and x509 Certificates.`, |
| 189 |
}, |
| 190 |
}, |
| 191 |
Description: `**step ca provisioner add** adds one or more provisioners |
| 192 |
to the configuration and writes the new configuration back to the CA config. |
| 193 |
|
| 194 |
To pick up the new configuration you must SIGHUP (kill -1 <pid>) or restart the |
| 195 |
step-ca process. |
| 196 |
|
| 197 |
## POSITIONAL ARGUMENTS |
| 198 |
|
| 199 |
<name> |
| 200 |
: The name of the provisioners, if a list of JWK files are passed, this name |
| 201 |
will be linked to all the keys. |
| 202 |
|
| 203 |
<jwk-path> |
| 204 |
: List of private (or public) keys in JWK or PEM format. |
| 205 |
|
| 206 |
## EXAMPLES |
| 207 |
|
| 208 |
Add a single JWK provisioner: |
| 209 |
''' |
| 210 |
$ step ca provisioner add [email protected] ./max-laptop.jwk --ca-config ca.json |
| 211 |
''' |
| 212 |
|
| 213 |
Add a single JWK provisioner using an auto-generated asymmetric key pair: |
| 214 |
''' |
| 215 |
$ step ca provisioner add [email protected] --ca-config ca.json \ |
| 216 |
--create |
| 217 |
''' |
| 218 |
|
| 219 |
Add a single JWK provisioner with ssh enabled: |
| 220 |
''' |
| 221 |
$ step ca provisioner add [email protected] --ca-config ca.json --ssh --create |
| 222 |
''' |
| 223 |
|
| 224 |
Add a list of provisioners for a single name: |
| 225 |
''' |
| 226 |
$ step ca provisioner add [email protected] ./max-laptop.jwk ./max-phone.pem ./max-work.pem \ |
| 227 |
--ca-config ca.json |
| 228 |
''' |
| 229 |
|
| 230 |
Add a single OIDC provisioner: |
| 231 |
''' |
| 232 |
$ step ca provisioner add Google --type oidc --ca-config ca.json \ |
| 233 |
--client-id 1087160488420-8qt7bavg3qesdhs6it824mhnfgcfe8il.apps.googleusercontent.com \ |
| 234 |
--configuration-endpoint https://accounts.google.com/.well-known/openid-configuration |
| 235 |
''' |
| 236 |
|
| 237 |
Add an OIDC provisioner with two administrators: |
| 238 |
''' |
| 239 |
$ step ca provisioner add Google --type oidc --ca-config ca.json \ |
| 240 |
--client-id 1087160488420-8qt7bavg3qesdhs6it824mhnfgcfe8il.apps.googleusercontent.com \ |
| 241 |
--client-secret udTrOT3gzrO7W9fDPgZQLfYJ \ |
| 242 |
--configuration-endpoint https://accounts.google.com/.well-known/openid-configuration \ |
| 243 |
--admin [email protected] --admin [email protected] \ |
| 244 |
--domain smallstep.com |
| 245 |
''' |
| 246 |
|
| 247 |
Add an AWS provisioner on one account with a one hour of instance age: |
| 248 |
''' |
| 249 |
$ step ca provisioner add Amazon --type AWS --ca-config ca.json \ |
| 250 |
--aws-account 123456789 --instance-age 1h |
| 251 |
''' |
| 252 |
|
| 253 |
Add an GCP provisioner with two service accounts and two project ids: |
| 254 |
''' |
| 255 |
$ step ca provisioner add Google --type GCP --ca-config ca.json \ |
| 256 |
--gcp-service-account [email protected] \ |
| 257 |
--gcp-service-account [email protected] \ |
| 258 |
--gcp-project identity --gcp-project accounting |
| 259 |
''' |
| 260 |
|
| 261 |
Add an Azure provisioner with two service groups: |
| 262 |
''' |
| 263 |
$ step ca provisioner add Azure --type Azure --ca-config ca.json \ |
| 264 |
--azure-tenant bc9043e2-b645-4c1c-a87a-78f8644bfe57 \ |
| 265 |
--azure-resource-group identity --azure-resource-group accounting |
| 266 |
''' |
| 267 |
|
| 268 |
Add an GCP provisioner that will only accept the SANs provided in the identity token: |
| 269 |
''' |
| 270 |
$ step ca provisioner add Google --type GCP --ca-config ca.json \ |
| 271 |
--disable-custom-sans --gcp-project internal |
| 272 |
''' |
| 273 |
|
| 274 |
Add an AWS provisioner that will only accept the SANs provided in the identity |
| 275 |
document and will allow multiple certificates from the same instance: |
| 276 |
''' |
| 277 |
$ step ca provisioner add Amazon --type AWS --ca-config ca.json \ |
| 278 |
--aws-account 123456789 --disable-custom-sans --disable-trust-on-first-use |
| 279 |
''' |
| 280 |
|
| 281 |
Add an AWS provisioner that will use a custom certificate to validate the instance |
| 282 |
identity documents: |
| 283 |
''' |
| 284 |
$ step ca provisioner add Amazon --type AWS --ca-config ca.json \ |
| 285 |
--aws-account 123456789 --iid-roots $(step path)/certs/aws.crt |
| 286 |
''' |
| 287 |
|
| 288 |
Add an ACME provisioner. |
| 289 |
''' |
| 290 |
$ step ca provisioner add acme-smallstep --type ACME |
| 291 |
''' |
| 292 |
|
| 293 |
Add an X5C provisioner. |
| 294 |
''' |
| 295 |
$ step ca provisioner add x5c-smallstep --type X5C --x5c-root x5cRoot.crt |
| 296 |
''' |
| 297 |
|
| 298 |
Add a K8s Service Account provisioner. |
| 299 |
''' |
| 300 |
$ step ca provisioner add my-kube-provisioner --type K8sSA --pem-keys keys.pub |
| 301 |
''' |
| 302 |
|
| 303 |
Add an SSH-POP provisioner. |
| 304 |
''' |
| 305 |
$ step ca provisioner add sshpop-smallstep --type SSHPOP |
| 306 |
'''`, |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
func addAction(ctx *cli.Context) (err error) { |
| 311 |
if ctx.NArg() == 0 { |
| 312 |
return errs.TooFewArguments(ctx) |
| 313 |
} |
| 314 |
|
| 315 |
args := ctx.Args() |
| 316 |
name := args[0] |
| 317 |
|
| 318 |
config := ctx.String("ca-config") |
| 319 |
if len(config) == 0 { |
| 320 |
return errs.RequiredFlag(ctx, "ca-config") |
| 321 |
} |
| 322 |
|
| 323 |
c, err := authority.LoadConfiguration(config) |
| 324 |
if err != nil { |
| 325 |
return errors.Wrapf(err, "error loading configuration") |
| 326 |
} |
| 327 |
|
| 328 |
typ, err := parseProvisionerType(ctx) |
| 329 |
if err != nil { |
| 330 |
return err |
| 331 |
} |
| 332 |
|
| 333 |
provMap := make(map[string]bool) |
| 334 |
for _, p := range c.AuthorityConfig.Provisioners { |
| 335 |
provMap[p.GetID()] = true |
| 336 |
} |
| 337 |
|
| 338 |
var list provisioner.List |
| 339 |
switch typ { |
| 340 |
case provisioner.TypeJWK: |
| 341 |
list, err = addJWKProvisioner(ctx, name, provMap) |
| 342 |
case provisioner.TypeOIDC: |
| 343 |
list, err = addOIDCProvisioner(ctx, name, provMap) |
| 344 |
case provisioner.TypeAWS: |
| 345 |
list, err = addAWSProvisioner(ctx, name, provMap) |
| 346 |
case provisioner.TypeAzure: |
| 347 |
list, err = addAzureProvisioner(ctx, name, provMap) |
| 348 |
case provisioner.TypeGCP: |
| 349 |
list, err = addGCPProvisioner(ctx, name, provMap) |
| 350 |
case provisioner.TypeACME: |
| 351 |
list, err = addACMEProvisioner(ctx, name, provMap) |
| 352 |
case provisioner.TypeX5C: |
| 353 |
list, err = addX5CProvisioner(ctx, name, provMap) |
| 354 |
case provisioner.TypeK8sSA: |
| 355 |
list, err = addK8sSAProvisioner(ctx, name, provMap) |
| 356 |
case provisioner.TypeSSHPOP: |
| 357 |
list, err = addSSHPOPProvisioner(ctx, name, provMap) |
| 358 |
default: |
| 359 |
return errors.Errorf("unknown type %s: this should not happen", typ) |
| 360 |
} |
| 361 |
|
| 362 |
if err != nil { |
| 363 |
return err |
| 364 |
} |
| 365 |
|
| 366 |
c.AuthorityConfig.Provisioners = append(c.AuthorityConfig.Provisioners, list...) |
| 367 |
if err = c.Save(config); err != nil { |
| 368 |
return err |
| 369 |
} |
| 370 |
|
| 371 |
ui.Println("Success! Your `step-ca` config has been updated. To pick up the new configuration SIGHUP (kill -1 <pid>) or restart the step-ca process.") |
| 372 |
|
| 373 |
return nil |
| 374 |
} |
| 375 |
|
| 376 |
func addJWKProvisioner(ctx *cli.Context, name string, provMap map[string]bool) (list provisioner.List, err error) { |
| 377 |
var password string |
| 378 |
if passwordFile := ctx.String("password-file"); len(passwordFile) > 0 { |
| 379 |
password, err = utils.ReadStringPasswordFromFile(passwordFile) |
| 380 |
if err != nil { |
| 381 |
return nil, err |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
if ctx.Bool("create") { |
| 386 |
if ctx.NArg() > 1 { |
| 387 |
return nil, errs.IncompatibleFlag(ctx, "create", "<jwk-path> positional arg") |
| 388 |
} |
| 389 |
pass, err := ui.PromptPasswordGenerate("Please enter a password to encrypt the provisioner private key? [leave empty and we'll generate one]", ui.WithValue(password)) |
| 390 |
if err != nil { |
| 391 |
return nil, err |
| 392 |
} |
| 393 |
jwk, jwe, err := jose.GenerateDefaultKeyPair(pass) |
| 394 |
if err != nil { |
| 395 |
return nil, err |
| 396 |
} |
| 397 |
encryptedKey, err := jwe.CompactSerialize() |
| 398 |
if err != nil { |
| 399 |
return nil, errors.Wrap(err, "error serializing private key") |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
p := &provisioner.JWK{ |
| 404 |
Type: provisioner.TypeJWK.String(), |
| 405 |
Name: name, |
| 406 |
Key: jwk, |
| 407 |
EncryptedKey: encryptedKey, |
| 408 |
Claims: getClaims(ctx), |
| 409 |
} |
| 410 |
|
| 411 |
if _, ok := provMap[p.GetID()]; !ok { |
| 412 |
provMap[p.GetID()] = true |
| 413 |
} else { |
| 414 |
return nil, errors.Errorf("duplicated provisioner: CA config already contains a provisioner with name=%s and kid=%s", name, jwk.KeyID) |
| 415 |
} |
| 416 |
list = append(list, p) |
| 417 |
return list, nil |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
if ctx.NArg() < 2 { |
| 422 |
return nil, errs.TooFewArguments(ctx) |
| 423 |
} |
| 424 |
|
| 425 |
jwkFiles := ctx.Args()[1:] |
| 426 |
for _, filename := range jwkFiles { |
| 427 |
jwk, err := jose.ParseKey(filename) |
| 428 |
if err != nil { |
| 429 |
return nil, errs.FileError(err, filename) |
| 430 |
} |
| 431 |
|
| 432 |
if _, ok := jwk.Key.([]byte); ok { |
| 433 |
return nil, errors.New("invalid JWK: a symmetric key cannot be used as a provisioner") |
| 434 |
} |
| 435 |
|
| 436 |
if len(jwk.KeyID) == 0 { |
| 437 |
jwk.KeyID, err = jose.Thumbprint(jwk) |
| 438 |
if err != nil { |
| 439 |
return nil, err |
| 440 |
} |
| 441 |
} |
| 442 |
key := jwk.Public() |
| 443 |
|
| 444 |
|
| 445 |
p := &provisioner.JWK{ |
| 446 |
Type: provisioner.TypeJWK.String(), |
| 447 |
Name: name, |
| 448 |
Key: &key, |
| 449 |
Claims: getClaims(ctx), |
| 450 |
} |
| 451 |
if _, ok := provMap[p.GetID()]; !ok { |
| 452 |
provMap[p.GetID()] = true |
| 453 |
} else { |
| 454 |
return nil, errors.Errorf("duplicated provisioner: CA config already contains a provisioner with name=%s and kid=%s", name, jwk.KeyID) |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
if !jwk.IsPublic() { |
| 459 |
jwe, err := jose.EncryptJWK(jwk) |
| 460 |
if err != nil { |
| 461 |
return nil, err |
| 462 |
} |
| 463 |
encryptedKey, err := jwe.CompactSerialize() |
| 464 |
if err != nil { |
| 465 |
return nil, errors.Wrap(err, "error serializing private key") |
| 466 |
} |
| 467 |
p.EncryptedKey = encryptedKey |
| 468 |
} |
| 469 |
|
| 470 |
list = append(list, p) |
| 471 |
} |
| 472 |
return list, nil |
| 473 |
} |
| 474 |
|
| 475 |
func addOIDCProvisioner(ctx *cli.Context, name string, provMap map[string]bool) (list provisioner.List, err error) { |
| 476 |
clientID := ctx.String("client-id") |
| 477 |
if len(clientID) == 0 { |
| 478 |
return nil, errs.RequiredWithFlagValue(ctx, "type", ctx.String("type"), "client-id") |
| 479 |
} |
| 480 |
|
| 481 |
confURL := ctx.String("configuration-endpoint") |
| 482 |
if len(confURL) == 0 { |
| 483 |
return nil, errs.RequiredWithFlagValue(ctx, "type", ctx.String("type"), "configuration-endpoint") |
| 484 |
} |
| 485 |
u, err := url.Parse(confURL) |
| 486 |
if err != nil || (u.Scheme != "https" && u.Scheme != "http") { |
| 487 |
return nil, errs.InvalidFlagValue(ctx, "configuration-endpoint", confURL, "") |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
p := &provisioner.OIDC{ |
| 492 |
Type: provisioner.TypeOIDC.String(), |
| 493 |
Name: name, |
| 494 |
ClientID: clientID, |
| 495 |
ClientSecret: ctx.String("client-secret"), |
| 496 |
ConfigurationEndpoint: confURL, |
| 497 |
Admins: ctx.StringSlice("admin"), |
| 498 |
Domains: ctx.StringSlice("domain"), |
| 499 |
Claims: getClaims(ctx), |
| 500 |
ListenAddress: ctx.String("listen-address"), |
| 501 |
} |
| 502 |
|
| 503 |
if _, ok := provMap[p.GetID()]; !ok { |
| 504 |
provMap[p.GetID()] = true |
| 505 |
} else { |
| 506 |
return nil, errors.Errorf("duplicated provisioner: CA config already contains a provisioner with name=%s and client-id=%s", p.GetName(), p.GetID()) |
| 507 |
} |
| 508 |
list = append(list, p) |
| 509 |
return |
| 510 |
} |
| 511 |
|
| 512 |
func addAWSProvisioner(ctx *cli.Context, name string, provMap map[string]bool) (list provisioner.List, err error) { |
| 513 |
d, err := parseIntaceAge(ctx) |
| 514 |
if err != nil { |
| 515 |
return nil, err |
| 516 |
} |
| 517 |
|
| 518 |
p := &provisioner.AWS{ |
| 519 |
Type: provisioner.TypeAWS.String(), |
| 520 |
Name: name, |
| 521 |
Accounts: ctx.StringSlice("aws-account"), |
| 522 |
DisableCustomSANs: ctx.Bool("disable-custom-sans"), |
| 523 |
DisableTrustOnFirstUse: ctx.Bool("disable-trust-on-first-use"), |
| 524 |
InstanceAge: d, |
| 525 |
IIDRoots: ctx.String("iid-roots"), |
| 526 |
Claims: getClaims(ctx), |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
if _, ok := provMap[p.GetID()]; !ok { |
| 531 |
provMap[p.GetID()] = true |
| 532 |
} else { |
| 533 |
return nil, errors.Errorf("duplicated provisioner: CA config already contains a provisioner with type=AWS and name=%s", p.GetName()) |
| 534 |
} |
| 535 |
|
| 536 |
list = append(list, p) |
| 537 |
return |
| 538 |
} |
| 539 |
|
| 540 |
func addAzureProvisioner(ctx *cli.Context, name string, provMap map[string]bool) (list provisioner.List, err error) { |
| 541 |
tenantID := ctx.String("azure-tenant") |
| 542 |
if tenantID == "" { |
| 543 |
return nil, errs.RequiredWithFlagValue(ctx, "type", ctx.String("type"), "azure-tenant") |
| 544 |
} |
| 545 |
|
| 546 |
p := &provisioner.Azure{ |
| 547 |
Type: provisioner.TypeAzure.String(), |
| 548 |
Name: name, |
| 549 |
TenantID: tenantID, |
| 550 |
ResourceGroups: ctx.StringSlice("azure-resource-group"), |
| 551 |
DisableCustomSANs: ctx.Bool("disable-custom-sans"), |
| 552 |
DisableTrustOnFirstUse: ctx.Bool("disable-trust-on-first-use"), |
| 553 |
Claims: getClaims(ctx), |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
if _, ok := provMap[p.GetID()]; !ok { |
| 558 |
provMap[p.GetID()] = true |
| 559 |
} else { |
| 560 |
return nil, errors.Errorf("duplicated provisioner: CA config already contains a provisioner with type=Azure and name=%s", p.GetName()) |
| 561 |
} |
| 562 |
|
| 563 |
list = append(list, p) |
| 564 |
return |
| 565 |
} |
| 566 |
|
| 567 |
func addGCPProvisioner(ctx *cli.Context, name string, provMap map[string]bool) (list provisioner.List, err error) { |
| 568 |
d, err := parseIntaceAge(ctx) |
| 569 |
if err != nil { |
| 570 |
return nil, err |
| 571 |
} |
| 572 |
|
| 573 |
p := &provisioner.GCP{ |
| 574 |
Type: provisioner.TypeGCP.String(), |
| 575 |
Name: name, |
| 576 |
ServiceAccounts: ctx.StringSlice("gcp-service-account"), |
| 577 |
ProjectIDs: ctx.StringSlice("gcp-project"), |
| 578 |
DisableCustomSANs: ctx.Bool("disable-custom-sans"), |
| 579 |
DisableTrustOnFirstUse: ctx.Bool("disable-trust-on-first-use"), |
| 580 |
InstanceAge: d, |
| 581 |
Claims: getClaims(ctx), |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
if _, ok := provMap[p.GetID()]; !ok { |
| 586 |
provMap[p.GetID()] = true |
| 587 |
} else { |
| 588 |
return nil, errors.Errorf("duplicated provisioner: CA config already contains a provisioner with type=GCP and name=%s", p.GetName()) |
| 589 |
} |
| 590 |
|
| 591 |
list = append(list, p) |
| 592 |
return |
| 593 |
} |
| 594 |
|
| 595 |
func addACMEProvisioner(ctx *cli.Context, name string, provMap map[string]bool) (list provisioner.List, err error) { |
| 596 |
p := &provisioner.ACME{ |
| 597 |
Type: provisioner.TypeACME.String(), |
| 598 |
Name: name, |
| 599 |
Claims: getClaims(ctx), |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
if _, ok := provMap[p.GetID()]; !ok { |
| 604 |
provMap[p.GetID()] = true |
| 605 |
} else { |
| 606 |
return nil, errors.Errorf("duplicated provisioner: CA config already contains a provisioner with ID==%s", p.GetID()) |
| 607 |
} |
| 608 |
|
| 609 |
list = append(list, p) |
| 610 |
return |
| 611 |
} |
| 612 |
|
| 613 |
func addX5CProvisioner(ctx *cli.Context, name string, provMap map[string]bool) (list provisioner.List, err error) { |
| 614 |
x5cRootFile := ctx.String("x5c-root") |
| 615 |
if len(x5cRootFile) == 0 { |
| 616 |
return nil, errs.RequiredWithFlagValue(ctx, "type", "x5c", "x5c-root") |
| 617 |
} |
| 618 |
|
| 619 |
roots, err := pemutil.ReadCertificateBundle(x5cRootFile) |
| 620 |
if err != nil { |
| 621 |
return nil, errors.Wrapf(err, "error loading X5C Root certificates from %s", x5cRootFile) |
| 622 |
} |
| 623 |
var rootBytes []byte |
| 624 |
for _, r := range roots { |
| 625 |
if r.KeyUsage&x509.KeyUsageCertSign == 0 { |
| 626 |
return nil, errors.Errorf("error: certificate with common name '%s' cannot be "+ |
| 627 |
"used as an X5C root certificate.\n\n"+ |
| 628 |
"X5C provisioner root certificates must have the 'Certificate Sign' key "+ |
| 629 |
"usage extension.", r.Subject.CommonName) |
| 630 |
} |
| 631 |
rootBytes = append(rootBytes, pem.EncodeToMemory(&pem.Block{ |
| 632 |
Type: "CERTIFICATE", |
| 633 |
Bytes: r.Raw, |
| 634 |
})...) |
| 635 |
} |
| 636 |
p := &provisioner.X5C{ |
| 637 |
Type: provisioner.TypeX5C.String(), |
| 638 |
Name: name, |
| 639 |
Claims: getClaims(ctx), |
| 640 |
Roots: rootBytes, |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
if _, ok := provMap[p.GetID()]; !ok { |
| 645 |
provMap[p.GetID()] = true |
| 646 |
} else { |
| 647 |
return nil, errors.Errorf("duplicated provisioner: CA config already contains a provisioner with ID=%s", p.GetID()) |
| 648 |
} |
| 649 |
|
| 650 |
list = append(list, p) |
| 651 |
return |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
func addK8sSAProvisioner(ctx *cli.Context, name string, provMap map[string]bool) (list provisioner.List, err error) { |
| 660 |
pemKeysF := ctx.String("pem-keys") |
| 661 |
if len(pemKeysF) == 0 { |
| 662 |
return nil, errs.RequiredWithFlagValue(ctx, "type", "k8sSA", "pem-keys") |
| 663 |
} |
| 664 |
|
| 665 |
pemKeysB, err := ioutil.ReadFile(pemKeysF) |
| 666 |
if err != nil { |
| 667 |
return nil, errors.Wrap(err, "error reading pem keys") |
| 668 |
} |
| 669 |
|
| 670 |
var ( |
| 671 |
block *pem.Block |
| 672 |
rest = pemKeysB |
| 673 |
pemKeys = []interface{}{} |
| 674 |
) |
| 675 |
for rest != nil { |
| 676 |
block, rest = pem.Decode(rest) |
| 677 |
if block == nil { |
| 678 |
break |
| 679 |
} |
| 680 |
key, err := pemutil.ParseKey(pem.EncodeToMemory(block)) |
| 681 |
if err != nil { |
| 682 |
return nil, errors.Wrapf(err, "error parsing public key from %s", pemKeysF) |
| 683 |
} |
| 684 |
switch q := key.(type) { |
| 685 |
case *rsa.PublicKey, *ecdsa.PublicKey, ed25519.PublicKey: |
| 686 |
default: |
| 687 |
return nil, errors.Errorf("Unexpected public key type %T in %s", q, pemKeysF) |
| 688 |
} |
| 689 |
pemKeys = append(pemKeys, key) |
| 690 |
} |
| 691 |
|
| 692 |
var pubKeyBytes []byte |
| 693 |
for _, k := range pemKeys { |
| 694 |
blk, err := pemutil.Serialize(k) |
| 695 |
if err != nil { |
| 696 |
return nil, errors.Wrap(err, "error serializing pem key") |
| 697 |
} |
| 698 |
pubKeyBytes = append(pubKeyBytes, pem.EncodeToMemory(blk)...) |
| 699 |
} |
| 700 |
|
| 701 |
p := &provisioner.K8sSA{ |
| 702 |
Type: provisioner.TypeK8sSA.String(), |
| 703 |
Name: name, |
| 704 |
Claims: getClaims(ctx), |
| 705 |
PubKeys: pubKeyBytes, |
| 706 |
} |
| 707 |
|
| 708 |
|
| 709 |
if _, ok := provMap[p.GetID()]; !ok { |
| 710 |
provMap[p.GetID()] = true |
| 711 |
} else { |
| 712 |
return nil, errors.Errorf("duplicated provisioner: CA config already contains a provisioner with ID=%s", p.GetID()) |
| 713 |
} |
| 714 |
|
| 715 |
list = append(list, p) |
| 716 |
return |
| 717 |
} |
| 718 |
|
| 719 |
|
| 720 |
func addSSHPOPProvisioner(ctx *cli.Context, name string, provMap map[string]bool) (list provisioner.List, err error) { |
| 721 |
ctx.Set("ssh", "true") |
| 722 |
p := &provisioner.SSHPOP{ |
| 723 |
Type: provisioner.TypeSSHPOP.String(), |
| 724 |
Name: name, |
| 725 |
Claims: getClaims(ctx), |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
if _, ok := provMap[p.GetID()]; !ok { |
| 730 |
provMap[p.GetID()] = true |
| 731 |
} else { |
| 732 |
return nil, errors.Errorf("duplicated provisioner: CA config already contains a provisioner with ID=%s", p.GetID()) |
| 733 |
} |
| 734 |
|
| 735 |
list = append(list, p) |
| 736 |
return |
| 737 |
} |
| 738 |
|
| 739 |
func getClaims(ctx *cli.Context) *provisioner.Claims { |
| 740 |
if ctx.Bool("ssh") { |
| 741 |
enable := true |
| 742 |
return &provisioner.Claims{ |
| 743 |
EnableSSHCA: &enable, |
| 744 |
} |
| 745 |
} |
| 746 |
return nil |
| 747 |
} |
| 748 |
|
| 749 |
func parseIntaceAge(ctx *cli.Context) (provisioner.Duration, error) { |
| 750 |
age := ctx.Duration("instance-age") |
| 751 |
if age == 0 { |
| 752 |
return provisioner.Duration{}, nil |
| 753 |
} |
| 754 |
if age < 0 { |
| 755 |
return provisioner.Duration{}, errs.MinSizeFlag(ctx, "instance-age", "0s") |
| 756 |
} |
| 757 |
return provisioner.Duration{Duration: age}, nil |
| 758 |
} |
| 759 |
|
| 760 |
func parseProvisionerType(ctx *cli.Context) (provisioner.Type, error) { |
| 761 |
typ := ctx.String("type") |
| 762 |
switch strings.ToLower(typ) { |
| 763 |
case "", "jwk": |
| 764 |
return provisioner.TypeJWK, nil |
| 765 |
case "oidc": |
| 766 |
return provisioner.TypeOIDC, nil |
| 767 |
case "gcp": |
| 768 |
return provisioner.TypeGCP, nil |
| 769 |
case "aws": |
| 770 |
return provisioner.TypeAWS, nil |
| 771 |
case "azure": |
| 772 |
return provisioner.TypeAzure, nil |
| 773 |
case "acme": |
| 774 |
return provisioner.TypeACME, nil |
| 775 |
case "x5c": |
| 776 |
return provisioner.TypeX5C, nil |
| 777 |
case "sshpop": |
| 778 |
return provisioner.TypeSSHPOP, nil |
| 779 |
case "k8ssa": |
| 780 |
return provisioner.TypeK8sSA, nil |
| 781 |
default: |
| 782 |
return 0, errs.InvalidFlagValue(ctx, "type", typ, "JWK, OIDC, AWS, Azure, GCP, ACME, X5C, SSHPOP, K8sSA") |
| 783 |
} |
| 784 |
} |