Skip to content

Commit

Permalink
chore: move dialer options to proxy.Config (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
enocom authored Jul 8, 2022
1 parent b928921 commit bca543c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 50 deletions.
44 changes: 4 additions & 40 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,12 @@ import (
"syscall"
"time"

"cloud.google.com/go/alloydbconn"
"contrib.go.opencensus.io/exporter/prometheus"
"contrib.go.opencensus.io/exporter/stackdriver"
"github.com/GoogleCloudPlatform/alloydb-auth-proxy/alloydb"
"github.com/GoogleCloudPlatform/alloydb-auth-proxy/internal/gcloud"
"github.com/GoogleCloudPlatform/alloydb-auth-proxy/internal/proxy"
"github.com/spf13/cobra"
"go.opencensus.io/trace"
"golang.org/x/oauth2"
)

var (
Expand Down Expand Up @@ -171,6 +168,9 @@ func parseConfig(cmd *cobra.Command, conf *proxy.Config, args []string) error {
if len(args) == 0 {
return newBadCommandError("missing instance uri (e.g., /projects/$PROJECTS/locations/$LOCTION/clusters/$CLUSTER/instances/$INSTANCES)")
}

conf.UserAgent = userAgent

userHasSet := func(f string) bool {
return cmd.PersistentFlags().Lookup(f).Changed
}
Expand All @@ -195,31 +195,6 @@ func parseConfig(cmd *cobra.Command, conf *proxy.Config, args []string) error {
if conf.CredentialsFile != "" && conf.GcloudAuth {
return newBadCommandError("cannot specify --credentials-file and --gcloud-auth flags at the same time")
}
opts := []alloydbconn.Option{
alloydbconn.WithUserAgent(userAgent),
}
switch {
case conf.Token != "":
cmd.Printf("Authorizing with the -token flag\n")
opts = append(opts, alloydbconn.WithTokenSource(
oauth2.StaticTokenSource(&oauth2.Token{AccessToken: conf.Token}),
))
case conf.CredentialsFile != "":
cmd.Printf("Authorizing with the credentials file at %q\n", conf.CredentialsFile)
opts = append(opts, alloydbconn.WithCredentialsFile(
conf.CredentialsFile,
))
case conf.GcloudAuth:
cmd.Println("Authorizing with gcloud user credentials")
ts, err := gcloud.TokenSource()
if err != nil {
return err
}
opts = append(opts, alloydbconn.WithTokenSource(ts))
default:
cmd.Println("Authorizing with Application Default Credentials")
}
conf.DialerOpts = opts

if userHasSet("http-port") && !userHasSet("prometheus-namespace") {
return newBadCommandError("cannot specify --http-port without --prometheus-namespace")
Expand Down Expand Up @@ -394,18 +369,7 @@ func runSignalWrapper(cmd *Command) error {
startCh := make(chan *proxy.Client)
go func() {
defer close(startCh)
// Check if the caller has configured a dialer.
// Otherwise, initialize a new one.
d := cmd.conf.Dialer
if d == nil {
var err error
d, err = alloydbconn.NewDialer(ctx, cmd.conf.DialerOpts...)
if err != nil {
shutdownCh <- fmt.Errorf("error initializing dialer: %v", err)
return
}
}
p, err := proxy.NewClient(ctx, d, cmd.Command, cmd.conf)
p, err := proxy.NewClient(ctx, cmd.Command, cmd.conf)
if err != nil {
shutdownCh <- fmt.Errorf("unable to start: %v", err)
return
Expand Down
7 changes: 4 additions & 3 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ import (
"cloud.google.com/go/alloydbconn"
"github.com/GoogleCloudPlatform/alloydb-auth-proxy/internal/proxy"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/spf13/cobra"
)

func TestNewCommandArguments(t *testing.T) {
withDefaults := func(c *proxy.Config) *proxy.Config {
if c.UserAgent == "" {
c.UserAgent = userAgent
}
if c.Addr == "" {
c.Addr = "127.0.0.1"
}
Expand Down Expand Up @@ -180,8 +182,7 @@ func TestNewCommandArguments(t *testing.T) {
t.Fatalf("want error = nil, got = %v", err)
}

opts := cmpopts.IgnoreFields(proxy.Config{}, "DialerOpts")
if got := c.conf; !cmp.Equal(tc.want, got, opts) {
if got := c.conf; !cmp.Equal(tc.want, got) {
t.Fatalf("want = %#v\ngot = %#v\ndiff = %v", tc.want, got, cmp.Diff(tc.want, got))
}
})
Expand Down
52 changes: 48 additions & 4 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import (

"cloud.google.com/go/alloydbconn"
"github.com/GoogleCloudPlatform/alloydb-auth-proxy/alloydb"
"github.com/GoogleCloudPlatform/alloydb-auth-proxy/internal/gcloud"
"github.com/spf13/cobra"
"golang.org/x/oauth2"
)

// InstanceConnConfig holds the configuration for an individual instance
Expand All @@ -48,6 +50,10 @@ type InstanceConnConfig struct {

// Config contains all the configuration provided by the caller.
type Config struct {
// UserAgent is the user agent to use when sending requests to the Admin
// API.
UserAgent string

// Token is the Bearer token used for authorization.
Token string

Expand Down Expand Up @@ -76,10 +82,33 @@ type Config struct {
// Dialer specifies the dialer to use when connecting to AlloyDB
// instances.
Dialer alloydb.Dialer
}

// DialerOptions builds appropriate list of options from the Config
// values for use by alloydbconn.NewClient()
func (c *Config) DialerOptions() ([]alloydbconn.Option, error) {
opts := []alloydbconn.Option{
alloydbconn.WithUserAgent(c.UserAgent),
}
switch {
case c.Token != "":
opts = append(opts, alloydbconn.WithTokenSource(
oauth2.StaticTokenSource(&oauth2.Token{AccessToken: c.Token}),
))
case c.CredentialsFile != "":
opts = append(opts, alloydbconn.WithCredentialsFile(
c.CredentialsFile,
))
case c.GcloudAuth:
ts, err := gcloud.TokenSource()
if err != nil {
return nil, err
}
opts = append(opts, alloydbconn.WithTokenSource(ts))
default:
}

// DialerOpts specifies the opts to use when creating a new dialer. This
// value is ignored when a Dialer has been set.
DialerOpts []alloydbconn.Option
return opts, nil
}

type portConfig struct {
Expand Down Expand Up @@ -131,7 +160,22 @@ type Client struct {
}

// NewClient completes the initial setup required to get the proxy to a "steady" state.
func NewClient(ctx context.Context, d alloydb.Dialer, cmd *cobra.Command, conf *Config) (*Client, error) {
func NewClient(ctx context.Context, cmd *cobra.Command, conf *Config) (*Client, error) {
// Check if the caller has configured a dialer.
// Otherwise, initialize a new one.
d := conf.Dialer
if d == nil {
var err error
dialerOpts, err := conf.DialerOptions()
if err != nil {
return nil, fmt.Errorf("error initializing dialer: %v", err)
}
d, err = alloydbconn.NewDialer(ctx, dialerOpts...)
if err != nil {
return nil, fmt.Errorf("error initializing dialer: %v", err)
}
}

pc := newPortConfig(conf.Port)
var mnts []*socketMount
for _, inst := range conf.Instances {
Expand Down
8 changes: 5 additions & 3 deletions internal/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ func TestClientInitialization(t *testing.T) {

for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
c, err := proxy.NewClient(ctx, fakeDialer{}, &cobra.Command{}, tc.in)
tc.in.Dialer = fakeDialer{}
c, err := proxy.NewClient(ctx, &cobra.Command{}, tc.in)
if err != nil {
t.Fatalf("want error = nil, got = %v", err)
}
Expand Down Expand Up @@ -228,14 +229,15 @@ func TestClientInitializationWorksRepeatedly(t *testing.T) {
Instances: []proxy.InstanceConnConfig{
{Name: "/projects/proj/locations/region/clusters/clust/instances/inst1"},
},
Dialer: fakeDialer{},
}
c, err := proxy.NewClient(ctx, fakeDialer{}, &cobra.Command{}, in)
c, err := proxy.NewClient(ctx, &cobra.Command{}, in)
if err != nil {
t.Fatalf("want error = nil, got = %v", err)
}
c.Close()

c, err = proxy.NewClient(ctx, fakeDialer{}, &cobra.Command{}, in)
c, err = proxy.NewClient(ctx, &cobra.Command{}, in)
if err != nil {
t.Fatalf("want error = nil, got = %v", err)
}
Expand Down

0 comments on commit bca543c

Please sign in to comment.