Skip to content

Commit

Permalink
limit BufConnListenerDialer to dialing functions
Browse files Browse the repository at this point in the history
Uses a local transportDialer interface in config.Cache{}. Adds
DialContext() to BufConnListenerDialer.
  • Loading branch information
tvoran committed Oct 13, 2021
1 parent 4bf6c99 commit 8e26033
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 47 deletions.
5 changes: 3 additions & 2 deletions command/agent.go
Expand Up @@ -51,6 +51,7 @@ import (
"github.com/mitchellh/cli"
"github.com/oklog/run"
"github.com/posener/complete"
"google.golang.org/grpc/test/bufconn"
)

var (
Expand Down Expand Up @@ -679,8 +680,8 @@ func (c *AgentCommand) Run(args []string) int {
var tlsConf *tls.Config

if lnConfig.Type == "bufconn" {
inProcListener := listenerutil.NewBufConnListenerDialer()
config.Cache.InProcDialer = inProcListener
inProcListener := bufconn.Listen(1024 * 1024)
config.Cache.InProcDialer = listenerutil.NewBufConnListenerDialer(inProcListener)
ln = inProcListener
} else {
ln, tlsConf, err = cache.StartListener(lnConfig)
Expand Down
26 changes: 19 additions & 7 deletions command/agent/config/config.go
@@ -1,9 +1,11 @@
package config

import (
"context"
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"strings"
"time"
Expand Down Expand Up @@ -63,15 +65,25 @@ type Vault struct {
Retry *Retry `hcl:"retry"`
}

// transportDialer is an interface that allows passing a custom dialer function
// to an HTTP client's transport config
type transportDialer interface {
// Dial is intended to match https://pkg.go.dev/net#Dialer.Dial
Dial(network, address string) (net.Conn, error)

// DialContext is intended to match https://pkg.go.dev/net#Dialer.DialContext
DialContext(ctx context.Context, network, address string) (net.Conn, error)
}

// Cache contains any configuration needed for Cache mode
type Cache struct {
UseAutoAuthTokenRaw interface{} `hcl:"use_auto_auth_token"`
UseAutoAuthToken bool `hcl:"-"`
ForceAutoAuthToken bool `hcl:"-"`
EnforceConsistency string `hcl:"enforce_consistency"`
WhenInconsistent string `hcl:"when_inconsistent"`
Persist *Persist `hcl:"persist"`
InProcDialer ctconfig.TransportDialer `hcl:"-"`
UseAutoAuthTokenRaw interface{} `hcl:"use_auto_auth_token"`
UseAutoAuthToken bool `hcl:"-"`
ForceAutoAuthToken bool `hcl:"-"`
EnforceConsistency string `hcl:"enforce_consistency"`
WhenInconsistent string `hcl:"when_inconsistent"`
Persist *Persist `hcl:"persist"`
InProcDialer transportDialer `hcl:"-"`
}

// Persist contains configuration needed for persistent caching
Expand Down
9 changes: 5 additions & 4 deletions command/agent/template/template_test.go
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/hashicorp/vault/sdk/helper/pointerutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/test/bufconn"
)

// TestNewServer is a simple test to make sure NewServer returns a Server and
Expand Down Expand Up @@ -144,9 +145,9 @@ func TestCacheConfig(t *testing.T) {
t.Run(name, func(t *testing.T) {
agentConfig := newAgentConfig(listeners, tc.cacheEnabled, tc.persistentCacheEnabled)
if tc.setDialer && tc.cacheEnabled {
bListener := listenerutil.NewBufConnListenerDialer()
bListener := bufconn.Listen(1024 * 1024)
defer bListener.Close()
agentConfig.Cache.InProcDialer = bListener
agentConfig.Cache.InProcDialer = listenerutil.NewBufConnListenerDialer(bListener)
}
serverConfig := ServerConfig{AgentConfig: agentConfig}

Expand All @@ -173,9 +174,9 @@ func TestCacheConfigNoListener(t *testing.T) {
listeners := []*configutil.Listener{}

agentConfig := newAgentConfig(listeners, true, true)
bListener := listenerutil.NewBufConnListenerDialer()
bListener := bufconn.Listen(1024 * 1024)
defer bListener.Close()
agentConfig.Cache.InProcDialer = bListener
agentConfig.Cache.InProcDialer = listenerutil.NewBufConnListenerDialer(bListener)
serverConfig := ServerConfig{AgentConfig: agentConfig}

ctConfig, err := newRunnerConfig(&serverConfig, ctconfig.TemplateConfigs{})
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -8,7 +8,7 @@ replace github.com/hashicorp/vault/sdk => ./sdk

replace go.etcd.io/etcd/client/pkg/v3 v3.5.0 => go.etcd.io/etcd/client/pkg/v3 v3.0.0-20210928084031-3df272774672

replace github.com/hashicorp/consul-template => github.com/tvoran/consul-template v0.27.2-0.20211012192322-e61ddbd05d86
replace github.com/hashicorp/consul-template => github.com/tvoran/consul-template v0.27.2-0.20211013180249-9860065fbe7f

require (
cloud.google.com/go v0.56.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -1186,8 +1186,8 @@ github.com/tklauser/numcpus v0.2.2/go.mod h1:x3qojaO3uyYt0i56EW/VUYs7uBvdl2fkfZF
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926 h1:G3dpKMzFDjgEh2q1Z7zUUtKa8ViPtH+ocF0bE0g00O8=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
github.com/tvoran/consul-template v0.27.2-0.20211012192322-e61ddbd05d86 h1:dx1eIlbal5MzOcDvOAz+CMgIVV/eLld4VLPNJQ+IhD0=
github.com/tvoran/consul-template v0.27.2-0.20211012192322-e61ddbd05d86/go.mod h1:cAi5bOqno7Ao5sFHu7O80wMOPnqcF5ADrTApWU4Lqx4=
github.com/tvoran/consul-template v0.27.2-0.20211013180249-9860065fbe7f h1:wsbCGJyOn+1kg9TH2qOqIqPez8utzBnE5TxfSDEbJnA=
github.com/tvoran/consul-template v0.27.2-0.20211013180249-9860065fbe7f/go.mod h1:cAi5bOqno7Ao5sFHu7O80wMOPnqcF5ADrTApWU4Lqx4=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/ulikunitz/xz v0.5.6/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
Expand Down
48 changes: 17 additions & 31 deletions internalshared/listenerutil/bufconn.go
@@ -1,50 +1,36 @@
package listenerutil

import (
"context"
"net"

ctconfig "github.com/hashicorp/consul-template/config"
"google.golang.org/grpc/test/bufconn"
)

var (
_ net.Listener = (*BufConnListenerDialer)(nil)
_ ctconfig.TransportDialer = (*BufConnListenerDialer)(nil)
)

// BufConnListenerDialer implements both a net.Listener and consul
// TransportDialer, to serve both ends of an in-process connection (Dial and
// Accept).
// BufConnListenerDialer implements consul-template's TransportDialer using a
// bufconn listener, to provide a way to Dial the in-memory listener
type BufConnListenerDialer struct {
listener *bufconn.Listener
}

// NewBufConnListenerDialer returns a new BufConnListenerDialer
func NewBufConnListenerDialer() *BufConnListenerDialer {
buffer := 1024 * 1024
listener := bufconn.Listen(buffer)

return &BufConnListenerDialer{listener: listener}
}

// Accept incoming connections to the bufconn listener
func (bcl *BufConnListenerDialer) Accept() (net.Conn, error) {
return bcl.listener.Accept()
}

// Close the bufconn
func (bcl *BufConnListenerDialer) Close() error {
return bcl.listener.Close()
// NewBufConnListenerDialer returns a new BufConnListenerDialer using an
// existing bufconn.Listener
func NewBufConnListenerDialer(bcl *bufconn.Listener) *BufConnListenerDialer {
return &BufConnListenerDialer{
listener: bcl,
}
}

// Addr returns the net.Addr of the bufconn listener
func (bcl *BufConnListenerDialer) Addr() net.Addr {
return bcl.listener.Addr()
}

// Dial and connect to the listening end of the bufconn (satisfies
// Dial connects to the listening end of the bufconn (satisfies
// consul-template's TransportDialer interface). This is essentially the client
// side of the bufconn connection.
func (bcl *BufConnListenerDialer) Dial(network, addr string) (net.Conn, error) {
return bcl.listener.Dial()
}

// DialContext connects to the listening end of the bufconn (satisfies
// consul-template's TransportDialer interface). This is essentially the client
// side of the bufconn connection.
func (bcl *BufConnListenerDialer) DialContext(_ context.Context, network, addr string) (net.Conn, error) {
return bcl.Dial(network, addr)
}

0 comments on commit 8e26033

Please sign in to comment.