Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add runtime version to user agent if present #1542

Merged
merged 13 commits into from
Nov 17, 2022
8 changes: 8 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type Command struct {
httpAddress string
httpPort string
quiet bool
runtime string

// impersonationChain is a comma separated list of one or more service
// accounts. The first entry in the chain is the impersonation target. Any
Expand Down Expand Up @@ -344,6 +345,8 @@ func NewCommand(opts ...Option) *Command {
pflags.BoolP("version", "v", false, "Print the cloud-sql-proxy version")

// Global-only flags
pflags.StringVar(&c.runtime, "runtime", "",
"(for internal use only) Runtime and version, e.g. cloud-sql-proxy-operator/0.0.1")
pflags.StringVarP(&c.conf.Token, "token", "t", "",
"Use bearer token as a source of IAM credentials.")
pflags.StringVarP(&c.conf.CredentialsFile, "credentials-file", "c", "",
Expand Down Expand Up @@ -492,6 +495,11 @@ func parseConfig(cmd *Command, conf *proxy.Config, args []string) error {
cmd.logger.Infof("Ignoring --disable-traces because --telemetry-project was not set")
}

if userHasSet("runtime") {
userAgent += " " + cmd.runtime
conf.UserAgent = userAgent
}

if userHasSet("sqladmin-api-endpoint") && conf.APIEndpointURL != "" {
_, err := url.Parse(conf.APIEndpointURL)
if err != nil {
Expand Down
37 changes: 37 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -81,6 +82,42 @@ func invokeProxyCommand(args []string) (*Command, error) {
return c, err
}

func TestUserAgentWithOperatorVersionEnvVar(t *testing.T) {
os.Setenv("CSQL_PROXY_RUNTIME", "cloud-sql-proxy-operator/0.0.1")
defer os.Unsetenv("CSQL_PROXY_RUNTIME")

cmd, err := invokeProxyCommand([]string{"proj:region:inst"})
if err != nil {
t.Fatalf("want error = nil, got = %v", err)
}

want := "cloud-sql-proxy-operator/0.0.1"
got := cmd.conf.UserAgent
if !strings.Contains(got, want) {
enocom marked this conversation as resolved.
Show resolved Hide resolved
t.Errorf("expected userAgent to contain: %v; got: %v", want, got)
}
}

func TestUserAgentWithOperatorVersionFlag(t *testing.T) {

cmd, err := invokeProxyCommand(
[]string{
"--runtime",
"cloud-sql-proxy-operator/0.0.1",
"proj:region:inst",
},
)
if err != nil {
t.Fatalf("want error = nil, got = %v", err)
}

want := "cloud-sql-proxy-operator/0.0.1"
got := cmd.conf.UserAgent
if !strings.Contains(got, want) {
t.Errorf("expected userAgent to contain: %v; got: %v", want, got)
}
}

func TestNewCommandArguments(t *testing.T) {
tcs := []struct {
desc string
Expand Down