From d1c64412cfaab24189036874f88cde8ba84b5019 Mon Sep 17 00:00:00 2001 From: Russel Vela Date: Tue, 2 Apr 2024 18:49:25 -0600 Subject: [PATCH 1/2] feat(user-agent): Adds specific user agents for VCert Adds specific User-Agent for VCert SDK, CLI and Playbook --- cmd/vcert/config.go | 4 ++++ cmd/vcert/main.go | 2 +- config.go | 1 + pkg/endpoint/endpoint.go | 2 +- pkg/playbook/app/vcertutil/helper.go | 6 ++++++ pkg/playbook/app/vcertutil/vcertutil.go | 1 + pkg/util/constants.go | 13 ++++++++++--- pkg/venafi/cloud/cloud.go | 2 +- vcert.go | 11 +++++++++-- 9 files changed, 34 insertions(+), 8 deletions(-) diff --git a/cmd/vcert/config.go b/cmd/vcert/config.go index 96202b27..c4aae646 100644 --- a/cmd/vcert/config.go +++ b/cmd/vcert/config.go @@ -130,6 +130,7 @@ func buildConfigFake(_ *commandFlags) (*vcert.Config, error) { return &vcert.Config{ ConnectorType: endpoint.ConnectorTypeFake, Credentials: &endpoint.Authentication{}, + UserAgent: vcert.GetUserAgentCLI(), }, nil } @@ -145,6 +146,7 @@ func buildConfigTPP(commandName string, flags *commandFlags) (*vcert.Config, err ConnectionTrust: "", LogVerbose: false, Client: nil, + UserAgent: vcert.GetUserAgentCLI(), } if commandName == commandGetCredName { @@ -168,6 +170,7 @@ func buildConfigVaaS(flags *commandFlags) (*vcert.Config, error) { TenantID: flags.vaasTenantID, ExternalIdPJWT: flags.externalJWT, }, + UserAgent: vcert.GetUserAgentCLI(), }, nil } @@ -188,5 +191,6 @@ func buildConfigFirefly(flags *commandFlags) (*vcert.Config, error) { Audience: flags.audience, }, }, + UserAgent: vcert.GetUserAgentCLI(), }, nil } diff --git a/cmd/vcert/main.go b/cmd/vcert/main.go index 6f8e9c2e..fb29e6b0 100644 --- a/cmd/vcert/main.go +++ b/cmd/vcert/main.go @@ -136,7 +136,7 @@ COPYRIGHT: SUPPORT: opensource@venafi.com {{end}} -`, vcert.GetFormattedVersionString(), vcert.GetFormatedBuildTimeStamp()) +`, vcert.GetFormattedVersionString(), vcert.GetFormattedBuildTimeStamp()) cli.CommandHelpTemplate = `NAME: {{.HelpName}} - {{.Usage}} diff --git a/config.go b/config.go index 6788c429..68249ff5 100644 --- a/config.go +++ b/config.go @@ -182,6 +182,7 @@ func LoadConfigFromFile(path, section string) (cfg Config, err error) { cfg.ConnectorType = connectorType cfg.Credentials = auth cfg.BaseUrl = baseUrl + cfg.UserAgent = GetUserAgentCLI() return } diff --git a/pkg/endpoint/endpoint.go b/pkg/endpoint/endpoint.go index e5f954d4..d9326429 100644 --- a/pkg/endpoint/endpoint.go +++ b/pkg/endpoint/endpoint.go @@ -367,7 +367,7 @@ func (p *Policy) ValidateCertificateRequest(request *certificate.Request) error return nil } -// SimpleValidateCertificateRequest functions just check Common Name and SANs mathching with policies +// SimpleValidateCertificateRequest functions just check Common Name and SANs matching with policies func (p *Policy) SimpleValidateCertificateRequest(request certificate.Request) error { csr := request.GetCSR() const ( diff --git a/pkg/playbook/app/vcertutil/helper.go b/pkg/playbook/app/vcertutil/helper.go index 24a30fce..c3e0cc8b 100644 --- a/pkg/playbook/app/vcertutil/helper.go +++ b/pkg/playbook/app/vcertutil/helper.go @@ -26,6 +26,7 @@ import ( "strings" "time" + "github.com/Venafi/vcert/v5" "go.uber.org/zap" "github.com/Venafi/vcert/v5/pkg/certificate" @@ -239,3 +240,8 @@ func readFile(fileName string) ([]byte, error) { } return bytes, nil } + +func getUserAgent() *string { + userAgent := fmt.Sprintf("vcert-playbook/%s", vcert.GetFormattedVersionString()[1:]) + return &userAgent +} diff --git a/pkg/playbook/app/vcertutil/vcertutil.go b/pkg/playbook/app/vcertutil/vcertutil.go index 4a4f7a35..e2da0201 100644 --- a/pkg/playbook/app/vcertutil/vcertutil.go +++ b/pkg/playbook/app/vcertutil/vcertutil.go @@ -89,6 +89,7 @@ func buildClient(config domain.Config, zone string) (endpoint.Connector, error) Zone: zone, ConnectionTrust: loadTrustBundle(config.Connection.TrustBundlePath), LogVerbose: false, + UserAgent: getUserAgent(), } // build Authentication object diff --git a/pkg/util/constants.go b/pkg/util/constants.go index c151fda9..9122f9e8 100644 --- a/pkg/util/constants.go +++ b/pkg/util/constants.go @@ -1,9 +1,16 @@ package util +import ( + "fmt" + + "github.com/Venafi/vcert/v5" +) + const ( PathSeparator = "\\" ApplicationServerTypeID = "784938d1-ef0d-11eb-9461-7bb533ba575b" - // DefaultUserAgent is the default value of the UserAgent header in HTTP - // requests to Venafi API endpoints. - DefaultUserAgent = "vcert/v5" ) + +// DefaultUserAgent is the default value of the UserAgent header in HTTP +// requests to Venafi API endpoints. +var DefaultUserAgent = fmt.Sprintf("vcert-sdk/%s", vcert.GetFormattedVersionString()[1:]) diff --git a/pkg/venafi/cloud/cloud.go b/pkg/venafi/cloud/cloud.go index c2593fd2..4f494e3c 100644 --- a/pkg/venafi/cloud/cloud.go +++ b/pkg/venafi/cloud/cloud.go @@ -665,7 +665,7 @@ type cloudZone struct { templateAlias string } -func (z cloudZone) String() string { +func (z *cloudZone) String() string { return z.zone } diff --git a/vcert.go b/vcert.go index 2589cb3e..7f1227d3 100644 --- a/vcert.go +++ b/vcert.go @@ -14,10 +14,12 @@ * limitations under the License. */ /* -VCert is a Go library, SDK, and command line utility designed to simplify key generation and enrollment of machine identities (also known as SSL/TLS certificates and keys) that comply with enterprise security policy by using the Venafi Platform or Venafi Cloud. +Package vcert is a Go library, SDK, and command line utility designed to simplify key generation and enrollment of machine identities (also known as SSL/TLS certificates and keys) that comply with enterprise security policy by using the Venafi Platform or Venafi Cloud. */ package vcert +import "fmt" + var ( versionBuildTimeStamp string versionString string @@ -31,9 +33,14 @@ func GetFormattedVersionString() string { return versionString } -func GetFormatedBuildTimeStamp() string { +func GetFormattedBuildTimeStamp() string { if versionBuildTimeStamp == "" { versionBuildTimeStamp = "Unknown" } return versionBuildTimeStamp } + +func GetUserAgentCLI() *string { + userAgent := fmt.Sprintf("vcert-cli/%s", GetFormattedVersionString()[1:]) + return &userAgent +} From 5442981f59e9f3fe8ffca7e10a9d5756c727909e Mon Sep 17 00:00:00 2001 From: Russel Vela Date: Tue, 2 Apr 2024 19:30:42 -0600 Subject: [PATCH 2/2] feat(user-agent): Fixes circular dependencies Fixes a circular dependency when trying to reuse the versionString from vcert root package --- Makefile | 2 +- pkg/playbook/app/vcertutil/helper.go | 2 +- pkg/util/constants.go | 19 ++++++++++++++----- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index a7dd1015..0b0912a6 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ VERSION=$(RELEASE_VERSION) endif endif -GO_LDFLAGS=-ldflags "-X github.com/Venafi/vcert/v5.versionString=$(VERSION) -X github.com/Venafi/vcert/v5.versionBuildTimeStamp=`date -u +%Y%m%d.%H%M%S` -s -w" +GO_LDFLAGS=-ldflags "-X github.com/Venafi/vcert/v5.versionString=$(VERSION) -X github.com/Venafi/vcert/v5.versionBuildTimeStamp=`date -u +%Y%m%d.%H%M%S` -X github.com/Venafi/vcert/v5/pkg/util.versionString=$(VERSION) -s -w" version: echo "$(VERSION)" diff --git a/pkg/playbook/app/vcertutil/helper.go b/pkg/playbook/app/vcertutil/helper.go index c3e0cc8b..91c9d67a 100644 --- a/pkg/playbook/app/vcertutil/helper.go +++ b/pkg/playbook/app/vcertutil/helper.go @@ -26,9 +26,9 @@ import ( "strings" "time" - "github.com/Venafi/vcert/v5" "go.uber.org/zap" + "github.com/Venafi/vcert/v5" "github.com/Venafi/vcert/v5/pkg/certificate" "github.com/Venafi/vcert/v5/pkg/playbook/app/domain" "github.com/Venafi/vcert/v5/pkg/util" diff --git a/pkg/util/constants.go b/pkg/util/constants.go index 9122f9e8..52e00dd6 100644 --- a/pkg/util/constants.go +++ b/pkg/util/constants.go @@ -2,8 +2,6 @@ package util import ( "fmt" - - "github.com/Venafi/vcert/v5" ) const ( @@ -11,6 +9,17 @@ const ( ApplicationServerTypeID = "784938d1-ef0d-11eb-9461-7bb533ba575b" ) -// DefaultUserAgent is the default value of the UserAgent header in HTTP -// requests to Venafi API endpoints. -var DefaultUserAgent = fmt.Sprintf("vcert-sdk/%s", vcert.GetFormattedVersionString()[1:]) +var ( + // We load this variable from build command instead of reusing the vcert one to avoid circular dependencies + versionString string + // DefaultUserAgent is the default value of the UserAgent header in HTTP + // requests to Venafi API endpoints. + DefaultUserAgent = fmt.Sprintf("vcert-sdk/%s", getVersionString()[1:]) +) + +func getVersionString() string { + if versionString == "" { + return "Unknown" + } + return versionString +}