Skip to content

Commit

Permalink
feat: add '--version' flag (#1161)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtisvg authored and enocom committed Apr 5, 2022
1 parent eb0c9f1 commit d4f06c6
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .github/release-please.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
# limitations under the License.

handleGHRelease: true
releaseType: simple
packageName: cloud-sql-proxy
releaseType: simple
versionFile: 'cmd/version.txt'
5 changes: 0 additions & 5 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,3 @@ jobs:
- run: go mod tidy
- name: Verify no changes from goimports and go mod tidy. If you're reading this and the check has failed, run `goimports -w . && go mod tidy`.
run: git diff --exit-code
- name: check for mismatch
shell: bash
run: |
VERSION=$(cat version.txt)
grep -w \"$VERSION\" cmd/cloud_sql_proxy/cloud_sql_proxy.go || exit 1
21 changes: 18 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cmd

import (
"context"
_ "embed"
"errors"
"fmt"
"net"
Expand All @@ -31,6 +32,18 @@ import (
"github.com/spf13/cobra"
)

var (
// versionString indicates the version of this library.
//go:embed version.txt
versionString string
userAgent string
)

func init() {
versionString = strings.TrimSpace(versionString)
userAgent = "cloud-sql-auth-proxy/" + versionString
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
Expand All @@ -56,8 +69,9 @@ func NewCommand() *Command {
}

cmd := &cobra.Command{
Use: "cloud_sql_proxy instance_connection_name...",
Short: "cloud_sql_proxy provides a secure way to authorize connections to Cloud SQL.",
Use: "cloud_sql_proxy instance_connection_name...",
Version: versionString,
Short: "cloud_sql_proxy provides a secure way to authorize connections to Cloud SQL.",
Long: `The Cloud SQL Auth proxy provides IAM-based authorization and encryption when
connecting to Cloud SQL instances. It listens on a local port and forwards connections
to your instance's IP address, providing a secure connection without having to manage
Expand Down Expand Up @@ -178,7 +192,8 @@ func runSignalWrapper(cmd *Command) error {
startCh := make(chan *proxy.Client)
go func() {
defer close(startCh)
d, err := cloudsqlconn.NewDialer(ctx, cmd.conf.DialerOpts()...)
opts := append(cmd.conf.DialerOpts(), cloudsqlconn.WithUserAgent(userAgent))
d, err := cloudsqlconn.NewDialer(ctx, opts...)
if err != nil {
shutdownCh <- fmt.Errorf("error initializing dialer: %v", err)
return
Expand Down
1 change: 1 addition & 0 deletions cmd/version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.0.0-dev
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/GoogleCloudPlatform/cloudsql-proxy/v2
go 1.17

require (
cloud.google.com/go/cloudsqlconn v0.2.1-0.20220317212813-c79136972083
cloud.google.com/go/cloudsqlconn v0.2.1-0.20220401153611-87e713b37755
cloud.google.com/go/compute v1.5.0
github.com/GoogleCloudPlatform/cloudsql-proxy v1.30.0
github.com/coreos/go-systemd/v22 v22.3.2
Expand Down Expand Up @@ -44,7 +44,7 @@ require (
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220324131243-acbaeb5b85eb // indirect
google.golang.org/genproto v0.0.0-20220329172620-7be39ac1afc7 // indirect
google.golang.org/grpc v1.45.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
google.golang.org/protobuf v1.28.0 // indirect
)
20 changes: 17 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ cloud.google.com/go/bigquery v1.4.0/go.mod h1:S8dzgnTigyfTmLBfrtrhyYhwRxG72rYxvf
cloud.google.com/go/bigquery v1.5.0/go.mod h1:snEHRnqQbz117VIFhE8bmtwIDY80NLUZUMb4Nv6dBIg=
cloud.google.com/go/bigquery v1.7.0/go.mod h1://okPTzCYNXSlb24MZs83e2Do+h+VXtc4gLoIoXIAPc=
cloud.google.com/go/bigquery v1.8.0/go.mod h1:J5hqkt3O0uAFnINi6JXValWIb1v0goeZM77hZzJN/fQ=
cloud.google.com/go/cloudsqlconn v0.2.1-0.20220317212813-c79136972083 h1:wD6r6mYz97+/iS4OuX1tf+8WIRFGQN/nPqFl8/gPQzI=
cloud.google.com/go/cloudsqlconn v0.2.1-0.20220317212813-c79136972083/go.mod h1:7JeNiyYmQKqNFJ+HMceyAiYra2KRVowsLZqPmu1IMe0=
cloud.google.com/go/cloudsqlconn v0.2.1-0.20220401153611-87e713b37755 h1:74ZeQyxqrRSBoytWhFCDqScGNuF4PpCh7fe+BR9eX/I=
cloud.google.com/go/cloudsqlconn v0.2.1-0.20220401153611-87e713b37755/go.mod h1:y/ogms5BQof7JguwhcHsTPfskl7BFxXI8lvyXCQu/5E=
cloud.google.com/go/compute v0.1.0/go.mod h1:GAesmwr110a34z04OlxYkATPBEfVhkymfTBXtfbBFow=
cloud.google.com/go/compute v1.3.0/go.mod h1:cCZiE1NHEtai4wiufUhW8I8S1JKkAnhnQJWM7YD99wM=
cloud.google.com/go/compute v1.5.0 h1:b1zWmYuuHz7gO9kDcM/EpHGr06UgsYNRpNJzI2kFiLM=
Expand Down Expand Up @@ -600,7 +600,11 @@ golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
<<<<<<< HEAD
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
=======
golang.org/x/sys v0.0.0-20220328115105-d36c6a25d886 h1:eJv7u3ksNXoLbGSKuv2s/SIO4tJVxc/A+MTpzxDgz/Q=
>>>>>>> c8797b7 (feat: add '--version' flag (#1161))
golang.org/x/sys v0.0.0-20220328115105-d36c6a25d886/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220403205710-6acee93ad0eb h1:PVGECzEo9Y3uOidtkHGdd347NjLtITfJFO9BxFpmRoo=
golang.org/x/sys v0.0.0-20220403205710-6acee93ad0eb/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down Expand Up @@ -724,7 +728,10 @@ google.golang.org/api v0.63.0/go.mod h1:gs4ij2ffTRXwuzzgJl/56BdwJaA194ijkfn++9tD
google.golang.org/api v0.67.0/go.mod h1:ShHKP8E60yPsKNw/w8w+VYaj9H6buA5UqDp8dhbQZ6g=
google.golang.org/api v0.70.0/go.mod h1:Bs4ZM2HGifEvXwd50TtW70ovgJffJYw2oRCOFU/SkfA=
google.golang.org/api v0.71.0/go.mod h1:4PyU6e6JogV1f9eA4voyrTY2batOLdgZ5qZ5HOCc4j8=
<<<<<<< HEAD
google.golang.org/api v0.73.0/go.mod h1:lbd/q6BRFJbdpV6OUCXstVeiI5mL/d3/WifG7iNKnjI=
=======
>>>>>>> c8797b7 (feat: add '--version' flag (#1161))
google.golang.org/api v0.74.0 h1:ExR2D+5TYIrMphWgs5JCgwRhEDlPDXXrLwHHMgPHTXE=
google.golang.org/api v0.74.0/go.mod h1:ZpfMZOVRMywNyvJFeqL9HRWBgAuRfSjJFpe9QtRRyDs=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
Expand Down Expand Up @@ -801,8 +808,14 @@ google.golang.org/genproto v0.0.0-20220218161850-94dd64e39d7c/go.mod h1:kGP+zUP2
google.golang.org/genproto v0.0.0-20220222213610-43724f9ea8cf/go.mod h1:kGP+zUP2Ddo0ayMi4YuN7C3WZyJvGLZRh8Z5wnAqvEI=
google.golang.org/genproto v0.0.0-20220304144024-325a89244dc8/go.mod h1:kGP+zUP2Ddo0ayMi4YuN7C3WZyJvGLZRh8Z5wnAqvEI=
google.golang.org/genproto v0.0.0-20220310185008-1973136f34c6/go.mod h1:kGP+zUP2Ddo0ayMi4YuN7C3WZyJvGLZRh8Z5wnAqvEI=
<<<<<<< HEAD
google.golang.org/genproto v0.0.0-20220324131243-acbaeb5b85eb h1:0m9wktIpOxGw+SSKmydXWB3Z3GTfcPP6+q75HCQa6HI=
google.golang.org/genproto v0.0.0-20220324131243-acbaeb5b85eb/go.mod h1:hAL49I2IFola2sVEjAn7MEwsja0xp51I0tlGAf9hz4E=
=======
google.golang.org/genproto v0.0.0-20220324131243-acbaeb5b85eb/go.mod h1:hAL49I2IFola2sVEjAn7MEwsja0xp51I0tlGAf9hz4E=
google.golang.org/genproto v0.0.0-20220329172620-7be39ac1afc7 h1:HOL66YCI20JvN2hVk6o2YIp9i/3RvzVUz82PqNr7fXw=
google.golang.org/genproto v0.0.0-20220329172620-7be39ac1afc7/go.mod h1:8w6bsBMX6yCPbAVTeqQHvzxW0EIFigd5lZyahWgyfDo=
>>>>>>> c8797b7 (feat: add '--version' flag (#1161))
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
Expand Down Expand Up @@ -845,8 +858,9 @@ google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGj
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
Expand Down
2 changes: 1 addition & 1 deletion testsV2/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (p *proxyExec) Wait(ctx context.Context) error {
}
}

// Stop sends the pskill signal to the proxy and returns.
// Done returns true if the proxy has exited.
func (p *proxyExec) Done() bool {
select {
case <-p.done:
Expand Down
57 changes: 57 additions & 0 deletions testsV2/other_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2022 Google LLC

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// https://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// other_test runs various tests that are database agnostic.
package tests

import (
"bufio"
"context"
"os"
"strings"
"testing"
"time"
)

func TestVersion(t *testing.T) {
ctx := context.Background()

data, err := os.ReadFile("../cmd/version.txt")
if err != nil {
t.Fatalf("failed to read version.txt: %v", err)
}
want := strings.TrimSpace(string(data))

// Start the proxy
p, err := StartProxy(ctx, "--version")
if err != nil {
t.Fatalf("proxy start failed: %v", err)
}
defer p.Close()

// Assume the proxy should be able to print "version" relatively quickly
ctx, cancel := context.WithTimeout(ctx, 50*time.Millisecond)
defer cancel()
err = p.Wait(ctx)
if err != nil {
t.Fatalf("proxy exited unexpectedly: %v", err)
}
output, err := bufio.NewReader(p.Out).ReadString('\n')
if err != nil {
t.Fatalf("failed to read output from proxy: %v", err)
}
if !strings.Contains(output, want) {
t.Errorf("proxy did not return correct version: want %q, got %q", want, output)
}
}

0 comments on commit d4f06c6

Please sign in to comment.