Skip to content

Commit 896ba1c

Browse files
authored
feat: use alloydb-go-connector (GoogleCloudPlatform#2)
1 parent 90ba6c6 commit 896ba1c

File tree

14 files changed

+198
-247
lines changed

14 files changed

+198
-247
lines changed

.github/workflows/tests.yaml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
name: tests
16+
on:
17+
pull_request:
18+
push:
19+
branches:
20+
- main
21+
22+
jobs:
23+
build:
24+
name: "unit tests"
25+
runs-on: ubuntu-latest
26+
environment: "Private Repos"
27+
steps:
28+
- name: Setup Go
29+
uses: actions/setup-go@v3
30+
with:
31+
go-version: "1.18"
32+
- name: Checkout code
33+
uses: actions/checkout@v3
34+
# TODO remove this step and the following when the repo is public
35+
- name: Checkout AlloyDB connector
36+
uses: actions/checkout@v3
37+
with:
38+
repository: "googlecloudplatform/alloydb-go-connector"
39+
path: "alloydb-go-connector"
40+
token: ${{ secrets.GH_PAT }}
41+
- name: Point to local connector
42+
run: |
43+
go mod edit -replace=cloud.google.com/go/alloydbconn=./alloydb-go-connector
44+
- name: Run tests
45+
run: |
46+
go test -v -race -cover -short ./...

.gitignore

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
.vscode/
77

88
# Compiled binary
9-
/cmd/cloud_sql_proxy/cloud_sql_proxy
10-
/cloud_sql_proxy
11-
# v2 binary
12-
/cloudsql-proxy
9+
/alloydb-auth-proxy
1310

1411
/key.json

cloudsql/cloudsql.go renamed to alloydb/alloydb.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,20 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package cloudsql
15+
package alloydb
1616

1717
import (
1818
"context"
1919
"io"
2020
"net"
2121

22-
"cloud.google.com/go/cloudsqlconn"
22+
"cloud.google.com/go/alloydbconn"
2323
)
2424

25-
// Dialer dials a Cloud SQL instance and returns its database engine version.
25+
// Dialer dials an AlloyDB instance.
2626
type Dialer interface {
2727
// Dial returns a connection to the specified instance.
28-
Dial(ctx context.Context, inst string, opts ...cloudsqlconn.DialOption) (net.Conn, error)
29-
// EngineVersion retrieves the provided instance's database version (e.g.,
30-
// POSTGRES_14)
31-
EngineVersion(ctx context.Context, inst string) (string, error)
28+
Dial(ctx context.Context, inst string, opts ...alloydbconn.DialOption) (net.Conn, error)
3229

3330
io.Closer
3431
}

cmd/root.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ import (
2727
"strings"
2828
"syscall"
2929

30-
"cloud.google.com/go/cloudsqlconn"
31-
"github.com/GoogleCloudPlatform/cloudsql-proxy/v2/cloudsql"
32-
"github.com/GoogleCloudPlatform/cloudsql-proxy/v2/internal/proxy"
30+
"cloud.google.com/go/alloydbconn"
31+
"github.com/GoogleCloudPlatform/alloydb-auth-proxy/alloydb"
32+
"github.com/GoogleCloudPlatform/alloydb-auth-proxy/internal/proxy"
3333
"github.com/spf13/cobra"
3434
)
3535

@@ -42,7 +42,7 @@ var (
4242

4343
func init() {
4444
versionString = strings.TrimSpace(versionString)
45-
userAgent = "cloud-sql-auth-proxy/" + versionString
45+
userAgent = "alloy-db-auth-proxy/" + versionString
4646
}
4747

4848
// Execute adds all child commands to the root command and sets flags appropriately.
@@ -57,7 +57,7 @@ func Execute() {
5757
}
5858
}
5959

60-
// Command represents an invocation of the Cloud SQL Auth Proxy.
60+
// Command represents an invocation of the AlloyDB Auth Proxy.
6161
type Command struct {
6262
*cobra.Command
6363
conf *proxy.Config
@@ -67,8 +67,8 @@ type Command struct {
6767
type Option func(*proxy.Config)
6868

6969
// WithDialer configures the Command to use the provided dialer to connect to
70-
// Cloud SQL instances.
71-
func WithDialer(d cloudsql.Dialer) Option {
70+
// AlloyDB instances.
71+
func WithDialer(d alloydb.Dialer) Option {
7272
return func(c *proxy.Config) {
7373
c.Dialer = d
7474
}
@@ -84,13 +84,13 @@ func NewCommand(opts ...Option) *Command {
8484
}
8585

8686
cmd := &cobra.Command{
87-
Use: "cloud_sql_proxy instance_connection_name...",
87+
Use: "alloydb-auth-proxy instance_connection_name...",
8888
Version: versionString,
89-
Short: "cloud_sql_proxy provides a secure way to authorize connections to Cloud SQL.",
90-
Long: `The Cloud SQL Auth proxy provides IAM-based authorization and encryption when
91-
connecting to Cloud SQL instances. It listens on a local port and forwards connections
92-
to your instance's IP address, providing a secure connection without having to manage
93-
any client SSL certificates.`,
89+
Short: "alloydb-auth-proxy provides a secure way to authorize connections to AlloyDB.",
90+
Long: `The AlloyDB Auth proxy provides IAM-based authorization and encryption when
91+
connecting to AlloyDB instances. It listens on a local port and forwards
92+
connections to your instance's IP address, providing a secure connection
93+
without having to manage any client SSL certificates.`,
9494
Args: func(cmd *cobra.Command, args []string) error {
9595
err := parseConfig(cmd, c.conf, args)
9696
if err != nil {
@@ -113,8 +113,8 @@ any client SSL certificates.`,
113113

114114
// Global and per instance flags
115115
cmd.PersistentFlags().StringVarP(&c.conf.Addr, "address", "a", "127.0.0.1",
116-
"Address on which to bind Cloud SQL instance listeners.")
117-
cmd.PersistentFlags().IntVarP(&c.conf.Port, "port", "p", 0,
116+
"Address on which to bind AlloyDB instance listeners.")
117+
cmd.PersistentFlags().IntVarP(&c.conf.Port, "port", "p", 5432,
118118
"Initial port to use for listeners. Subsequent listeners increment from this value.")
119119

120120
c.Command = cmd
@@ -142,7 +142,7 @@ func parseConfig(cmd *cobra.Command, conf *proxy.Config, args []string) error {
142142
case conf.CredentialsFile != "":
143143
cmd.Printf("Authorizing with the credentials file at %q\n", conf.CredentialsFile)
144144
default:
145-
cmd.Printf("Authorizing with Application Default Credentials")
145+
cmd.Println("Authorizing with Application Default Credentials")
146146
}
147147

148148
var ics []proxy.InstanceConnConfig
@@ -227,9 +227,9 @@ func runSignalWrapper(cmd *Command) error {
227227
// Otherwise, initialize a new one.
228228
d := cmd.conf.Dialer
229229
if d == nil {
230-
opts := append(cmd.conf.DialerOpts(), cloudsqlconn.WithUserAgent(userAgent))
230+
opts := append(cmd.conf.DialerOpts(), alloydbconn.WithUserAgent(userAgent))
231231
var err error
232-
d, err = cloudsqlconn.NewDialer(ctx, opts...)
232+
d, err = alloydbconn.NewDialer(ctx, opts...)
233233
if err != nil {
234234
shutdownCh <- fmt.Errorf("error initializing dialer: %v", err)
235235
return

cmd/root_test.go

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ package cmd
1717
import (
1818
"context"
1919
"errors"
20+
"fmt"
2021
"net"
2122
"sync"
2223
"testing"
2324
"time"
2425

25-
"cloud.google.com/go/cloudsqlconn"
26-
"github.com/GoogleCloudPlatform/cloudsql-proxy/v2/internal/proxy"
26+
"cloud.google.com/go/alloydbconn"
27+
"github.com/GoogleCloudPlatform/alloydb-auth-proxy/internal/proxy"
2728
"github.com/google/go-cmp/cmp"
2829
"github.com/google/go-cmp/cmp/cmpopts"
2930
"github.com/spf13/cobra"
@@ -34,6 +35,9 @@ func TestNewCommandArguments(t *testing.T) {
3435
if c.Addr == "" {
3536
c.Addr = "127.0.0.1"
3637
}
38+
if c.Port == 0 {
39+
c.Port = 5432
40+
}
3741
if c.Instances == nil {
3842
c.Instances = []proxy.InstanceConnConfig{{}}
3943
}
@@ -240,38 +244,70 @@ func (s *spyDialer) instance() string {
240244
return i
241245
}
242246

243-
func (*spyDialer) Dial(_ context.Context, inst string, _ ...cloudsqlconn.DialOption) (net.Conn, error) {
244-
return nil, errors.New("spy dialer does not dial")
245-
}
246-
247-
func (s *spyDialer) EngineVersion(ctx context.Context, inst string) (string, error) {
247+
func (s *spyDialer) Dial(_ context.Context, inst string, _ ...alloydbconn.DialOption) (net.Conn, error) {
248248
s.mu.Lock()
249249
defer s.mu.Unlock()
250250
s.got = inst
251-
return "", nil
251+
return nil, errors.New("spy dialer does not dial")
252252
}
253253

254254
func (*spyDialer) Close() error {
255255
return nil
256256
}
257257

258258
func TestCommandWithCustomDialer(t *testing.T) {
259-
want := "my-project:my-region:my-instance"
259+
want := "my-project:my-region:my-cluster:my-instance"
260260
s := &spyDialer{}
261261
c := NewCommand(WithDialer(s))
262262
// Keep the test output quiet
263263
c.SilenceUsage = true
264264
c.SilenceErrors = true
265-
c.SetArgs([]string{want})
265+
c.SetArgs([]string{"--port", "10000", want})
266266

267-
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
267+
ctx, cancel := context.WithCancel(context.Background())
268268
defer cancel()
269269

270-
if err := c.ExecuteContext(ctx); !errors.As(err, &errSigInt) {
271-
t.Fatalf("want errSigInt, got = %v", err)
272-
}
270+
go func() {
271+
if err := c.ExecuteContext(ctx); !errors.As(err, &errSigInt) {
272+
t.Fatalf("want errSigInt, got = %v", err)
273+
}
274+
}()
273275

274-
if got := s.instance(); got != want {
275-
t.Fatalf("want = %v, got = %v", want, got)
276+
// try will run f count times, returning early if f succeeds, or failing
277+
// when count has been exceeded.
278+
try := func(f func() error, count int) {
279+
var (
280+
attempts int
281+
err error
282+
)
283+
for {
284+
if attempts == count {
285+
t.Fatal(err)
286+
}
287+
err = f()
288+
if err != nil {
289+
attempts++
290+
time.Sleep(time.Millisecond)
291+
continue
292+
}
293+
return
294+
}
276295
}
296+
// give the listener some time to start
297+
try(func() error {
298+
conn, err := net.Dial("tcp", "127.0.0.1:10000")
299+
if err != nil {
300+
return err
301+
}
302+
defer conn.Close()
303+
return nil
304+
}, 10)
305+
306+
// give the proxy some time to run
307+
try(func() error {
308+
if got := s.instance(); got != want {
309+
return fmt.Errorf("want = %v, got = %v", want, got)
310+
}
311+
return nil
312+
}, 10)
277313
}

go.mod

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
1-
module github.com/GoogleCloudPlatform/cloudsql-proxy/v2
1+
module github.com/GoogleCloudPlatform/alloydb-auth-proxy
22

33
go 1.16
44

55
require (
6-
cloud.google.com/go/cloudsqlconn v0.2.1-0.20220401153611-87e713b37755
7-
cloud.google.com/go/compute v1.5.0
8-
github.com/GoogleCloudPlatform/cloudsql-proxy v1.29.0
9-
github.com/coreos/go-systemd/v22 v22.3.2
10-
github.com/denisenkom/go-mssqldb v0.12.0
11-
github.com/go-sql-driver/mysql v1.6.0
6+
cloud.google.com/go/alloydbconn v0.0.0-0.20220401153611-87e713b37755
127
github.com/google/go-cmp v0.5.7
13-
github.com/hanwen/go-fuse/v2 v2.1.0
14-
github.com/jackc/pgx/v4 v4.15.0
15-
github.com/lib/pq v1.10.5
8+
github.com/lib/pq v1.10.5 // indirect
169
github.com/spf13/cobra v1.2.1
17-
go.uber.org/zap v1.21.0
18-
golang.org/x/net v0.0.0-20220325170049-de3da57026de
1910
golang.org/x/oauth2 v0.0.0-20220309155454-6242fa91716a
20-
golang.org/x/sys v0.0.0-20220406163625-3f8b81556e12
21-
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65
22-
google.golang.org/api v0.74.0
11+
golang.org/x/sys v0.0.0-20220406163625-3f8b81556e12 // indirect
12+
google.golang.org/api v0.74.0 // indirect
13+
google.golang.org/genproto v0.0.0-20220401170504-314d38edb7de // indirect
2314
)
15+
16+
replace cloud.google.com/go/alloydbconn => ../alloydb-go-connector

0 commit comments

Comments
 (0)