Skip to content

Commit ac1b0f3

Browse files
committed
feat: add support for token-based auth (#1149)
1 parent c336544 commit ac1b0f3

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

cmd/root.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ any client SSL certificates.`,
7676
},
7777
}
7878

79+
// Global-only flags
80+
cmd.PersistentFlags().StringVarP(&c.conf.Token, "token", "t", "",
81+
"Bearer token used for authorization.")
82+
83+
// Global and per instance flags
7984
cmd.PersistentFlags().StringVarP(&c.conf.Addr, "address", "a", "127.0.0.1",
8085
"Address on which to bind Cloud SQL instance listeners.")
8186
cmd.PersistentFlags().IntVarP(&c.conf.Port, "port", "p", 0,
@@ -173,7 +178,7 @@ func runSignalWrapper(cmd *Command) error {
173178
startCh := make(chan *proxy.Client)
174179
go func() {
175180
defer close(startCh)
176-
d, err := cloudsqlconn.NewDialer(ctx)
181+
d, err := cloudsqlconn.NewDialer(ctx, cmd.conf.DialerOpts()...)
177182
if err != nil {
178183
shutdownCh <- fmt.Errorf("error initializing dialer: %v", err)
179184
return

cmd/root_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ func TestNewCommandArguments(t *testing.T) {
9898
}},
9999
}),
100100
},
101+
{
102+
desc: "using the token flag",
103+
args: []string{"--token", "MYCOOLTOKEN", "proj:region:inst"},
104+
want: withDefaults(&proxy.Config{
105+
Token: "MYCOOLTOKEN",
106+
}),
107+
},
108+
{
109+
desc: "using the token (short) flag",
110+
args: []string{"-t", "MYCOOLTOKEN", "proj:region:inst"},
111+
want: withDefaults(&proxy.Config{
112+
Token: "MYCOOLTOKEN",
113+
}),
114+
},
101115
}
102116

103117
for _, tc := range tcs {

internal/proxy/proxy.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"cloud.google.com/go/cloudsqlconn"
2727
"github.com/spf13/cobra"
28+
"golang.org/x/oauth2"
2829
)
2930

3031
// InstanceConnConfig holds the configuration for an individual instance
@@ -40,6 +41,9 @@ type InstanceConnConfig struct {
4041

4142
// Config contains all the configuration provided by the caller.
4243
type Config struct {
44+
// Token is the Bearer token used for authorization.
45+
Token string
46+
4347
// Addr is the address on which to bind all instances.
4448
Addr string
4549

@@ -52,6 +56,16 @@ type Config struct {
5256
Instances []InstanceConnConfig
5357
}
5458

59+
func (c Config) DialerOpts() []cloudsqlconn.Option {
60+
var opts []cloudsqlconn.Option
61+
if c.Token != "" {
62+
opts = append(opts, cloudsqlconn.WithTokenSource(
63+
oauth2.StaticTokenSource(&oauth2.Token{AccessToken: c.Token}),
64+
))
65+
}
66+
return opts
67+
}
68+
5569
// Client represents the state of the current instantiation of the proxy.
5670
type Client struct {
5771
cmd *cobra.Command

internal/proxy/proxy_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,30 @@ func TestClientInitialization(t *testing.T) {
179179
})
180180
}
181181
}
182+
183+
func TestConfigDialerOpts(t *testing.T) {
184+
tcs := []struct {
185+
desc string
186+
config proxy.Config
187+
wantLen int
188+
}{
189+
{
190+
desc: "when there are no options",
191+
config: proxy.Config{},
192+
wantLen: 0,
193+
},
194+
{
195+
desc: "when a token is present",
196+
config: proxy.Config{Token: "my-token"},
197+
wantLen: 1,
198+
},
199+
}
200+
201+
for _, tc := range tcs {
202+
t.Run(tc.desc, func(t *testing.T) {
203+
if got := tc.config.DialerOpts(); tc.wantLen != len(got) {
204+
t.Errorf("want len = %v, got = %v", tc.wantLen, len(got))
205+
}
206+
})
207+
}
208+
}

0 commit comments

Comments
 (0)