Skip to content

Commit

Permalink
fixes flags, flush result logic, and documentation in cli
Browse files Browse the repository at this point in the history
  • Loading branch information
luispresuelVenafi committed May 22, 2024
1 parent cac82d1 commit 96f1a86
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 46 deletions.
7 changes: 6 additions & 1 deletion cmd/vcert/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ const (
)

var (
flags commandFlags
flags commandFlags
provisionCommands = stringSlice{
subCommandCloudKeystore,
}
)

type commandFlags struct {
Expand Down Expand Up @@ -148,4 +151,6 @@ type commandFlags struct {
providerName string
keystoreName string
keystoreCertName string
provisionOutputFile string
provisionPickupID string
}
70 changes: 44 additions & 26 deletions cmd/vcert/cmdCloudProviders.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"github.com/Venafi/vcert/v5/pkg/webclient/cloudproviders"
"log"
"os"
"strings"
Expand All @@ -16,26 +17,40 @@ import (
var (
commandProvision = &cli.Command{
Before: runBeforeCommand,
Action: doCommandProvision,
Name: commandProvisionName,
Usage: "To provision a certificate",
UsageText: ` vcert provision <Required Venafi Control Plane> <Options>
vcert provision cloudkeystore -k <VCP API key>
vcert provision cloudkeystore -k <VCP API key>
vcert provision cloudkeystore -p vcp -t <VCP access token>`,
Usage: "To provision a certificate from Venafi Platform to a Cloud Keystore",
Subcommands: []*cli.Command{
{
Name: subCommandCloudKeystore,
Flags: provisionFlags,
Usage: "set Cloud Keystore for provision",
UsageText: `vcert provision cloudkeystore`,
Action: doCommandProvision,
Name: subCommandCloudKeystore,
Flags: provisionFlags,
Usage: "provision certificate from Venafi Platform to Cloud Keystore",
UsageText: `vcert provision cloudkeystore <Required Venafi Control Plane> <Options>
vcert provision cloudkeystore -k <VCP API key> --certificate-id xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx --keystore-id xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx --format json
vcert provision cloudkeystore -k <VCP API key> --pickup-id xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx --provider-name "My GCP Provider"--keystore-name "My GCP provider" --certificate-name "example-venafi-com"
vcert provision cloudkeystore -p vcp -t <VCP access token> --certificate-id xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx --provider-name "My GCP Provider"--keystore-name "My GCP provider" --file "/path/to/file.txt"`,
Action: doCommandProvisionCloudKeystore,
},
},
}
)

func doCommandProvision(c *cli.Context) error {
return fmt.Errorf("the following subcommand(s) are required: \n%s", createBulletList(provisionCommands))
}

func createBulletList(items []string) string {
var builder strings.Builder
for _, item := range items {
builder.WriteString("• ")
builder.WriteString(item)
builder.WriteString("\n")
}
return builder.String()
}

func doCommandProvisionCloudKeystore(c *cli.Context) error {
err := validateProvisionFlags(c.Command.Name)
if err != nil {
return err
Expand All @@ -47,7 +62,7 @@ func doCommandProvision(c *cli.Context) error {

cfg, err := buildConfig(c, &flags)
if err != nil {
return fmt.Errorf("Failed to build vcert config: %s", err)
return fmt.Errorf("failed to build vcert config: %s", err)
}

connector, err := vcert.NewClient(&cfg)
Expand Down Expand Up @@ -83,20 +98,23 @@ func doCommandProvision(c *cli.Context) error {
return err
}

arn := metadata.GetAWSCertificateMetadata().GetARN()
azureID := metadata.GetAzureCertificateMetadata().GetID()
azureName := metadata.GetAzureCertificateMetadata().GetName()
azureVersion := metadata.GetAzureCertificateMetadata().GetVersion()
gcpID := metadata.GetGCPCertificateMetadata().GetID()
gcpName := metadata.GetGCPCertificateMetadata().GetName()

result := &ProvisioningResult{
ARN: &arn,
AzureID: &azureID,
AzureName: &azureName,
AzureVersion: &azureVersion,
GcpID: &gcpID,
GcpName: &gcpName,
result := ProvisioningResult{}
switch cloudKeystore.Type {
case string(cloudproviders.CloudKeystoreTypeAcm):
arn := metadata.GetAWSCertificateMetadata().GetARN()
result.ARN = &arn
case string(cloudproviders.CloudKeystoreTypeAkv):
azureID := metadata.GetAzureCertificateMetadata().GetID()
azureName := metadata.GetAzureCertificateMetadata().GetName()
azureVersion := metadata.GetAzureCertificateMetadata().GetVersion()
result.AzureID = &azureID
result.AzureName = &azureName
result.AzureVersion = &azureVersion
case string(cloudproviders.CloudKeystoreTypeGcm):
gcpID := metadata.GetGCPCertificateMetadata().GetID()
gcpName := metadata.GetGCPCertificateMetadata().GetName()
result.GcpID = &gcpID
result.GcpName = &gcpName
}

err = result.Flush(flags.format)
Expand Down
31 changes: 21 additions & 10 deletions cmd/vcert/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ var (
}

flagPickupIDFile = &cli.StringFlag{
Name: "pickup-id-file",
Name: "pickup-file",
Usage: "Use to specify the file name from where to read or write the Pickup ID. " +
"Either --pickup-id or --pickup-id-file is required.",
Destination: &flags.pickupIDFile,
Expand Down Expand Up @@ -733,11 +733,25 @@ var (
}

flagKeystoreCertName = &cli.StringFlag{
Name: "cloudkeystore-certname",
Name: "certificate-name",
Usage: "Use to specify Cloud Keystore Certificate Name if it supports it",
Destination: &flags.keystoreCertName,
}

flagProvisionOutputFile = &cli.StringFlag{
Name: "file",
Usage: "Use to specify a file name and a location where the output should be written. " +
"Example: --file /path-to/provision-output",
Destination: &flags.provisionOutputFile,
TakesFile: true,
}

flagProvisionPickupID = &cli.StringFlag{
Name: "pickup-id",
Usage: "Use to specify the Pickup ID (for VCP is the Request ID) of the certificate to be provisioned.",
Destination: &flags.provisionPickupID,
}

commonFlags = []cli.Flag{flagInsecure, flagVerbose, flagNoPrompt}
keyFlags = []cli.Flag{flagKeyType, flagKeySize, flagKeyCurve, flagKeyFile, flagKeyPassword}
sansFlags = []cli.Flag{flagDNSSans, flagEmailSans, flagIPSans, flagURISans, flagUPNSans}
Expand Down Expand Up @@ -879,17 +893,14 @@ var (
provisionFlags = flagsApppend(
credentialsFlags,
flagCertificateID,
flagPickupID,
flagProvisionPickupID,
flagPickupIDFile,
flagKeystoreID,
flagKeystoreName,
flagProviderName,
flagKeystoreCertName,
flagProviderName,
flagKeystoreName,
flagKeystoreID,
flagFormat,
sortedFlags(flagsApppend(
commonFlags,
sortableCredentialsFlags,
)),
flagProvisionOutputFile,
)

commonCredFlags = []cli.Flag{flagConfig, flagProfile, flagUrl, flagToken, flagTrustBundle}
Expand Down
21 changes: 12 additions & 9 deletions cmd/vcert/result_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ type Result struct {
}

type ProvisioningResult struct {
ARN *string `json:"arn"`
AzureID *string `json:"azureId"`
AzureName *string `json:"azureName"`
AzureVersion *string `json:"azureVersion"`
GcpID *string `json:"gcpId"`
GcpName *string `json:"gcpName"`
ARN *string `json:"arn,omitempty"`
AzureID *string `json:"azureId,omitempty"`
AzureName *string `json:"azureName,omitempty"`
AzureVersion *string `json:"azureVersion,omitempty"`
GcpID *string `json:"gcpId,omitempty"`
GcpName *string `json:"gcpName,omitempty"`
}

type Output struct {
Expand Down Expand Up @@ -464,10 +464,13 @@ func (r *ProvisioningResult) Format(format string) (string, error) {
result := ""
switch strings.ToLower(format) {
case formatJson:
b, err := json.Marshal(r)
b, err := json.MarshalIndent(r, "", " ")
if err != nil {
return "", fmt.Errorf("failed to construct JSON: %s", err)
}
if err != nil {
return "", err
}
result = string(b)
default:
if r.ARN != nil {
Expand All @@ -480,8 +483,8 @@ func (r *ProvisioningResult) Format(format string) (string, error) {

}
if r.GcpID != nil {
result += fmt.Sprintf("gcpId %s:", util.StringPointerToString(r.GcpID))
result += fmt.Sprintf("gcpName %s:", util.StringPointerToString(r.GcpName))
result += fmt.Sprintf("gcpId %s\n", util.StringPointerToString(r.GcpID))
result += fmt.Sprintf("gcpName %s\n", util.StringPointerToString(r.GcpName))
}
}
return result, nil
Expand Down

0 comments on commit 96f1a86

Please sign in to comment.