diff --git a/.build/release_artifacts.sh b/.build/release_artifacts.sh index fbf3aec9c..7fc0a524a 100755 --- a/.build/release_artifacts.sh +++ b/.build/release_artifacts.sh @@ -21,7 +21,7 @@ PROJ_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )"/.. >/dev/null 2>&1 && pwd )" cd $PROJ_ROOT # get the current version -export VERSION=$(cat cmd/cloud_sql_proxy/version.txt) +export VERSION=$(cat proxy/util/version.txt) if [ -z "$VERSION" ]; then echo "error: No version.txt found in $PROJ_ROOT" exit 1 diff --git a/cmd/cloud_sql_proxy/cloud_sql_proxy.go b/cmd/cloud_sql_proxy/cloud_sql_proxy.go index fb8a9c43e..1de80ff5f 100644 --- a/cmd/cloud_sql_proxy/cloud_sql_proxy.go +++ b/cmd/cloud_sql_proxy/cloud_sql_proxy.go @@ -274,28 +274,6 @@ Information for all flags: var defaultTmp = filepath.Join(os.TempDir(), "cloudsql-proxy-tmp") -// versionString indiciates the version of the proxy currently in use. -// -//go:embed version.txt -var versionString string - -// metadataString indiciates additional build or distribution metadata. -var metadataString = "" - -// semanticVersion returns the version of the proxy in a semver format. -func semanticVersion() string { - v := strings.TrimSpace(versionString) - if metadataString != "" { - v += "+" + metadataString - } - return v -} - -// userAgentFromVersionString returns an appropriate user agent string for identifying this proxy process. -func userAgentFromVersionString() string { - return "cloud_sql_proxy/" + semanticVersion() -} - const accountErrorSuffix = `Please create a new VM with Cloud SQL access (scope) enabled under "Identity and API access". Alternatively, create a new "service account key" and specify it using the -credential_file parameter` type stringListValue []string @@ -548,7 +526,7 @@ use the value from the flag, Not compatible with -fuse.`, flag.Parse() if *version { - fmt.Println("Cloud SQL Auth proxy:", semanticVersion()) + fmt.Println("Cloud SQL Auth proxy:", util.SemanticVersion()) return 0 } @@ -664,7 +642,7 @@ use the value from the flag, Not compatible with -fuse.`, Certs: certs.NewCertSourceOpts(client, certs.RemoteOpts{ APIBasePath: *host, IgnoreRegion: !*checkRegion, - UserAgent: userAgentFromVersionString(), + UserAgent: util.UserAgentFromVersionString(), IPAddrTypeOpts: ipAddrTypeOptsInput, EnableIAMLogin: *enableIAMLogin, TokenSource: tokSrc, diff --git a/cmd/cloud_sql_proxy/cloud_sql_proxy_test.go b/cmd/cloud_sql_proxy/cloud_sql_proxy_test.go index f9cde6b76..4d3df8ca9 100644 --- a/cmd/cloud_sql_proxy/cloud_sql_proxy_test.go +++ b/cmd/cloud_sql_proxy/cloud_sql_proxy_test.go @@ -15,25 +15,11 @@ package main import ( - "os" - "strings" "testing" "golang.org/x/net/context" ) -func TestVersionStripsNewline(t *testing.T) { - v, err := os.ReadFile("version.txt") - if err != nil { - t.Fatalf("failed to read verion.txt: %v", err) - } - want := strings.TrimSpace(string(v)) - - if got := semanticVersion(); got != want { - t.Fatalf("want = %q, got = %q", want, got) - } -} - func TestAuthenticatedClient(t *testing.T) { tcs := []struct { desc string diff --git a/proxy/certs/certs.go b/proxy/certs/certs.go index e26aa572e..9257e0c7a 100644 --- a/proxy/certs/certs.go +++ b/proxy/certs/certs.go @@ -37,7 +37,7 @@ import ( sqladmin "google.golang.org/api/sqladmin/v1beta4" ) -const defaultUserAgent = "custom cloud_sql_proxy version >= 1.10" +var defaultUserAgent = util.UserAgentFromVersionString() // NewCertSource returns a CertSource which can be used to authenticate using // the provided client, which must not be nil. diff --git a/proxy/util/cloudsqlutil.go b/proxy/util/cloudsqlutil.go index c4a033784..8af8100db 100644 --- a/proxy/util/cloudsqlutil.go +++ b/proxy/util/cloudsqlutil.go @@ -15,7 +15,10 @@ // Package util contains utility functions for use throughout the Cloud SQL Auth proxy. package util -import "strings" +import ( + _ "embed" + "strings" +) // SplitName splits a fully qualified instance into its project, region, and // instance name components. While we make the transition to regionalized @@ -44,3 +47,26 @@ func SplitName(instance string) (project, region, name string) { return spl[0], spl[1], spl[2] } } + +// versionString indicates the version of the proxy currently in use. +// +//go:embed version.txt +var versionString string + +// metadataString indicates additional build or distribution metadata. +var metadataString = "" + +// semanticVersion returns the version of the proxy in a semver format. +func SemanticVersion() string { + v := strings.TrimSpace(versionString) + if metadataString != "" { + v += "+" + metadataString + } + return v +} + +// userAgentFromVersionString returns an appropriate user agent string +// for identifying this proxy process. +func UserAgentFromVersionString() string { + return "cloud_sql_proxy/" + SemanticVersion() +} diff --git a/proxy/util/cloudsqlutil_test.go b/proxy/util/cloudsqlutil_test.go index d614f33db..d61fdd566 100644 --- a/proxy/util/cloudsqlutil_test.go +++ b/proxy/util/cloudsqlutil_test.go @@ -14,7 +14,35 @@ package util -import "testing" +import ( + "os" + "strings" + "testing" +) + +func TestSemanticVersion(t *testing.T) { + v, err := os.ReadFile("version.txt") + if err != nil { + t.Fatalf("failed to read version.txt: %v", err) + } + want := strings.TrimSpace(string(v)) + + if got := SemanticVersion(); got != want { + t.Fatalf("want = %q, got = %q", want, got) + } +} + +func TestUserAgentFromVersionString(t *testing.T) { + v, err := os.ReadFile("version.txt") + if err != nil { + t.Fatalf("failed to read version.txt: %v", err) + } + want := "cloud_sql_proxy/" + strings.TrimSpace(string(v)) + + if got := UserAgentFromVersionString(); got != want { + t.Fatalf("want = %q, got = %q", want, got) + } +} func TestSplitName(t *testing.T) { table := []struct{ in, wantProj, wantRegion, wantInstance string }{ diff --git a/cmd/cloud_sql_proxy/version.txt b/proxy/util/version.txt similarity index 100% rename from cmd/cloud_sql_proxy/version.txt rename to proxy/util/version.txt