Skip to content

Commit

Permalink
fix: replace the hardcoded default version string (#1836) (#1900)
Browse files Browse the repository at this point in the history
  • Loading branch information
annafang-google authored Aug 15, 2023
1 parent 52d5fa0 commit f945798
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .build/release_artifacts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 2 additions & 24 deletions cmd/cloud_sql_proxy/cloud_sql_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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,
Expand Down
14 changes: 0 additions & 14 deletions cmd/cloud_sql_proxy/cloud_sql_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion proxy/certs/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 27 additions & 1 deletion proxy/util/cloudsqlutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
30 changes: 29 additions & 1 deletion proxy/util/cloudsqlutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }{
Expand Down
File renamed without changes.

0 comments on commit f945798

Please sign in to comment.