Skip to content

Commit 1b471ab

Browse files
Merge pull request #490 from Venafi/VC-32826-provision-vcert-cli-4
Adds certificate ID File flags for provisioning in VCert CLI
2 parents 78ecbb6 + 65cf690 commit 1b471ab

File tree

5 files changed

+59
-24
lines changed

5 files changed

+59
-24
lines changed

README-CLI-CLOUD.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -229,17 +229,18 @@ vcert provisioning cloudkeystore -p vcp -t <access token> [--certificate-id <cer
229229
```
230230
Options:
231231

232-
| Command | Description |
233-
|----------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
234-
| `--certificate-id` | The id of the certificate to be provisioned to a cloud keystore. |
235-
| `--pickup-id` | Use to specify the unique identifier of the certificate returned by the enroll or renew actions if `--no-pickup` was used or a timeout occurred. Required when `--pickup-id-file` is not specified. |
236-
| `--pickup-id-file` | Use to specify a file name that contains the unique identifier of the certificate returned by the enroll or renew actions if --no-pickup was used or a timeout occurred. Required when `--pickup-id` is not specified. |
237-
| `--certificate-name` | Use to specify Cloud Keystore Certificate Name if it supports it |
238-
| `--keystore-id` | The id of the cloud keystore where the certificate will be provisioned. |
239-
| `--provider-name` | The name of the cloud provider which owns the cloud keystore where the certificate will be provisioned. Must be set along with keystore-name flag. |
240-
| `--keystore-name` | The name of the cloud keystore where the certificate will be provisioned. Must be set along with provider-name flag. |
241-
| `--file` | Use to specify a file name and a location where the output should be written. Example: --file /path-to/provision-output |
242-
| `--format` | The format of the operation output: text or JSON. Defaults to text. |
232+
| Command | Description |
233+
|-------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
234+
| `--certificate-id` | The id of the certificate to be provisioned to a cloud keystore. |
235+
| `--certificate-id-file` | Use to specify a file name that contains the unique identifier of the certificate. Required when `--certificate-id` is not specified. |
236+
| `--certificate-name` | Use to specify Cloud Keystore Certificate Name if it supports it |
237+
| `--file` | Use to specify a file name and a location where the output should be written. Example: --file /path-to/provision-output |
238+
| `--format` | The format of the operation output: text or JSON. Defaults to text. |
239+
| `--keystore-id` | The id of the cloud keystore where the certificate will be provisioned. |
240+
| `--keystore-name` | The name of the cloud keystore where the certificate will be provisioned. Must be set along with provider-name flag. |
241+
| `--pickup-id-file` | Use to specify a file name that contains the unique identifier of the certificate returned by the enroll or renew actions if --no-pickup was used or a timeout occurred. Required when `--pickup-id` is not specified. |
242+
| `--pickup-id` | Use to specify the unique identifier of the certificate returned by the enroll or renew actions. Required when `--pickup-id-file` is not specified. |
243+
| `--provider-name` | The name of the cloud provider which owns the cloud keystore where the certificate will be provisioned. Must be set along with keystore-name flag. |
243244

244245
## Parameters for Applying Certificate Policy
245246
API key:

cmd/vcert/args.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ type commandFlags struct {
147147
sshFileCertEnroll string
148148
sshFileGetConfig string
149149
certificateID string
150+
certificateIDFile string
150151
keystoreID string
151152
providerName string
152153
keystoreName string

cmd/vcert/cmdCloudKeystores.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,18 @@ func doCommandProvisionCloudKeystore(c *cli.Context) error {
3232
if err != nil {
3333
return err
3434
}
35+
var flagsP *commandFlags
36+
flagsP, err = gettingIDsFromFiles(&flags)
37+
if err != nil {
38+
return err
39+
}
40+
3541
err = setTLSConfig()
3642
if err != nil {
3743
return err
3844
}
3945

40-
cfg, err := buildConfig(c, &flags)
46+
cfg, err := buildConfig(c, flagsP)
4147
if err != nil {
4248
return fmt.Errorf("failed to build vcert config: %s", err)
4349
}
@@ -53,22 +59,14 @@ func doCommandProvisionCloudKeystore(c *cli.Context) error {
5359
var options *endpoint.ProvisioningOptions
5460

5561
log.Printf("fetching keystore information for provided keystore information from flags. KeystoreID: %s, KeystoreName: %s, ProviderName: %s", flags.keystoreID, flags.keystoreName, flags.providerName)
56-
getKeystoreReq := buildGetCloudKeystoreRequest(&flags)
62+
getKeystoreReq := buildGetCloudKeystoreRequest(flagsP)
5763
cloudKeystore, err := connector.(*cloud.Connector).GetCloudKeystore(getKeystoreReq)
5864
if err != nil {
5965
return err
6066
}
6167
log.Printf("successfully fetched keystore")
6268

63-
if flags.pickupIDFile != "" {
64-
bytes, err := os.ReadFile(flags.pickupIDFile)
65-
if err != nil {
66-
return fmt.Errorf("failed to read Pickup ID value: %s", err)
67-
}
68-
flags.pickupID = strings.TrimSpace(string(bytes))
69-
}
70-
71-
req, options = fillProvisioningRequest(req, *cloudKeystore, &flags)
69+
req, options = fillProvisioningRequest(req, *cloudKeystore, flagsP)
7270

7371
metadata, err := connector.ProvisionCertificate(req, options)
7472
if err != nil {
@@ -94,3 +92,21 @@ func doCommandProvisionCloudKeystore(c *cli.Context) error {
9492
}
9593
return nil
9694
}
95+
96+
func gettingIDsFromFiles(flags *commandFlags) (*commandFlags, error) {
97+
if flags.pickupIDFile != "" {
98+
bytes, err := os.ReadFile(flags.pickupIDFile)
99+
if err != nil {
100+
return nil, fmt.Errorf("failed to read Pickup ID value: %s", err)
101+
}
102+
flags.pickupID = strings.TrimSpace(string(bytes))
103+
}
104+
if flags.certificateIDFile != "" {
105+
bytes, err := os.ReadFile(flags.certificateIDFile)
106+
if err != nil {
107+
return nil, fmt.Errorf("failed to read Certificate ID value: %s", err)
108+
}
109+
flags.certificateID = strings.TrimSpace(string(bytes))
110+
}
111+
return flags, nil
112+
}

cmd/vcert/flags.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,13 @@ var (
714714
Destination: &flags.certificateID,
715715
}
716716

717+
flagCertificateIDFile = &cli.StringFlag{
718+
Name: "certificate-id-file",
719+
Usage: "Use to specify the file name from where to read or write the Certificate ID. " +
720+
"Either --certificate-id or --certificate-id-file is required.",
721+
Destination: &flags.certificateIDFile,
722+
}
723+
717724
flagKeystoreID = &cli.StringFlag{
718725
Name: "keystore-id",
719726
Usage: "The id of the cloud keystore where the certificate will be provisioned.",
@@ -900,6 +907,7 @@ var (
900907
credentialsFlags,
901908
flagPlatform,
902909
flagCertificateID,
910+
flagCertificateIDFile,
903911
flagProvisionPickupID,
904912
flagPickupIDFile,
905913
flagKeystoreCertName,

cmd/vcert/validators.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,8 +732,8 @@ func validateProvisionFlags(commandName string) error {
732732
return fmt.Errorf("unexpected output format: %s", flags.format)
733733
}
734734

735-
if flags.certificateID == "" && flags.provisionPickupID == "" && flags.pickupIDFile == "" {
736-
return fmt.Errorf("please, provide any of --certificate-id or --pickup-id or --pickup-id-file")
735+
if flags.certificateID == "" && flags.provisionPickupID == "" && flags.pickupIDFile == "" && flags.certificateIDFile == "" {
736+
return fmt.Errorf("please, provide any of --certificate-id or --certificate-id-file or --pickup-id or --pickup-id-file")
737737
}
738738

739739
if flags.pickupIDFile != "" {
@@ -745,6 +745,15 @@ func validateProvisionFlags(commandName string) error {
745745
}
746746
}
747747

748+
if flags.certificateIDFile != "" {
749+
if flags.pickupID != "" {
750+
return fmt.Errorf("both --certificate-id and --pickup-id-file options cannot be specified at the same time")
751+
}
752+
if flags.certificateID != "" {
753+
return fmt.Errorf("both --certificate-id and --certificate-id-file options cannot be specified at the same time")
754+
}
755+
}
756+
748757
if flags.keystoreID == "" {
749758
if flags.keystoreName == "" || flags.providerName == "" {
750759
return fmt.Errorf("any of keystore ID or both Provider Name and Keystore Name must be provided for provisioning")

0 commit comments

Comments
 (0)