github.com/Venafi/vcert/v5@v5.10.2/examples/provisionWithCertificateRequest/main.go (about)

     1  package main
     2  
     3  import (
     4  	"crypto/x509/pkix"
     5  	"log"
     6  	"os"
     7  
     8  	"github.com/Venafi/vcert/v5"
     9  	"github.com/Venafi/vcert/v5/pkg/certificate"
    10  	"github.com/Venafi/vcert/v5/pkg/domain"
    11  	"github.com/Venafi/vcert/v5/pkg/endpoint"
    12  )
    13  
    14  const (
    15  	vcpURL       = "VCP_URL"
    16  	vcpZone      = "VCP_ZONE"
    17  	vcpApiKey    = "CLOUD_APIKEY"
    18  	envVarNotSet = "environment variable not set: %s"
    19  
    20  	name = "example-provisioning"
    21  )
    22  
    23  func main() {
    24  
    25  	// URL can be nil if using production TLSPC
    26  	url := os.Getenv(vcpURL)
    27  
    28  	zone, found := os.LookupEnv(vcpZone)
    29  	if !found {
    30  		log.Fatalf(envVarNotSet, vcpZone)
    31  	}
    32  
    33  	config := &vcert.Config{
    34  		ConnectorType: endpoint.ConnectorTypeCloud,
    35  		BaseUrl:       url,
    36  		Zone:          zone,
    37  		Credentials:   &endpoint.Authentication{APIKey: os.Getenv(vcpApiKey)},
    38  	}
    39  
    40  	connector, err := vcert.NewClient(config)
    41  	if err != nil {
    42  		log.Fatalf("error creating client: %s", err.Error())
    43  	}
    44  
    45  	request := &certificate.Request{
    46  		Subject: pkix.Name{
    47  			CommonName:         "common.name.venafi.example.com",
    48  			Organization:       []string{"Venafi.com"},
    49  			OrganizationalUnit: []string{"Integration Team"},
    50  			Locality:           []string{"Salt Lake"},
    51  			Province:           []string{"Salt Lake"},
    52  			Country:            []string{"US"},
    53  		},
    54  		DNSNames:  []string{"www.client.venafi.example.com", "ww1.client.venafi.example.com"},
    55  		CsrOrigin: certificate.ServiceGeneratedCSR,
    56  		KeyType:   certificate.KeyTypeRSA,
    57  		KeyLength: certificate.DefaultRSAlength,
    58  	}
    59  
    60  	err = connector.GenerateRequest(nil, request)
    61  	if err != nil {
    62  		log.Fatalf("could not generate certificate request: %s", err)
    63  	}
    64  
    65  	requestID, err := connector.RequestCertificate(request)
    66  	if err != nil {
    67  		log.Fatalf("could not submit certificate request: %s", err)
    68  	}
    69  	log.Printf("Successfully submitted certificate request. Will pickup certificate by ID %s", requestID)
    70  
    71  	keystoreName := "<insert Keystore Name here>"
    72  	providerName := "<insert Provider Name here>"
    73  	certName := "<insert cert name>" // e.g. test2-venafi-com
    74  
    75  	optionsInput := domain.ProvisioningOptions{
    76  		CloudCertificateName: certName,
    77  	}
    78  
    79  	req := &domain.ProvisioningRequest{
    80  		KeystoreName: &keystoreName,
    81  		ProviderName: &providerName,
    82  		PickupID:     &requestID,
    83  	}
    84  
    85  	certMetaData, err := connector.ProvisionCertificate(req, &optionsInput)
    86  	if err != nil {
    87  		log.Fatalf("error provisioning: %s", err.Error())
    88  	}
    89  
    90  	// Example to get values from other keystores machine identities metadata
    91  	if certMetaData.CloudKeystoreType == domain.CloudKeystoreTypeACM {
    92  		log.Printf("Certificate AWS Metadata ARN:\n%v", certMetaData.CertificateID)
    93  	}
    94  	if certMetaData.CloudKeystoreType == domain.CloudKeystoreTypeAKV {
    95  		log.Printf("Certificate Azure Metadata ID:\n%v", certMetaData.CertificateID)
    96  		log.Printf("Certificate Azure Metadata Name:\n%v", certMetaData.CertificateName)
    97  		log.Printf("Certificate Azure Metadata Version:\n%v", certMetaData.CertificateVersion)
    98  	}
    99  	if certMetaData.CloudKeystoreType == domain.CloudKeystoreTypeGCM {
   100  		log.Printf("Certificate GCP Metadata ID:\n%v", certMetaData.CertificateID)
   101  		log.Printf("Certificate GCP Metadata Name:\n%v", certMetaData.CertificateName)
   102  	}
   103  }