Migrate a bunch of stuff around to prep for worker command (#2)

Right now it's essentially a copy; it needs some updating, integration
into dev, and so on.
This commit is contained in:
Jeff Mitchell authored and GitHub committed 2020-03-19 15:17:40 -04:00
1 parent c2d385d851
commit 1645c3106e
18 files changed
+1200 -157

No files matched your search

+159
View File
@@ -0,0 +1,159 @@
package base
import (
"errors"
"fmt"
"io"
"net"
"strings"
"time"
// We must import sha512 so that it registers with the runtime so that
// certificates that use it can be parsed.
_ "crypto/sha512"
"github.com/hashicorp/go-alpnmux"
"github.com/hashicorp/vault/internalshared/configutil"
"github.com/hashicorp/vault/internalshared/listenerutil"
"github.com/hashicorp/vault/internalshared/reloadutil"
"github.com/mitchellh/cli"
"github.com/pires/go-proxyproto"
)
// Factory is the factory function to create a listener.
type ListenerFactory func(*configutil.Listener, io.Writer, cli.Ui) (*alpnmux.ALPNMux, map[string]string, reloadutil.ReloadFunc, error)
// BuiltinListeners is the list of built-in listener types.
var BuiltinListeners = map[string]ListenerFactory{
"tcp": tcpListenerFactory,
}
// New creates a new listener of the given type with the given
// configuration. The type is looked up in the BuiltinListeners map.
func NewListener(l *configutil.Listener, w io.Writer, ui cli.Ui) (*alpnmux.ALPNMux, map[string]string, reloadutil.ReloadFunc, error) {
f, ok := BuiltinListeners[l.Type]
if !ok {
return nil, nil, nil, fmt.Errorf("unknown listener type: %q", l.Type)
}
return f(l, w, ui)
}
func tcpListenerFactory(l *configutil.Listener, _ io.Writer, ui cli.Ui) (*alpnmux.ALPNMux, map[string]string, reloadutil.ReloadFunc, error) {
if l.Address == "" {
l.Address = "127.0.0.1:9200"
}
bindProto := "tcp"
// If they've passed 0.0.0.0, we only want to bind on IPv4
// rather than golang's dual stack default
if strings.HasPrefix(l.Address, "0.0.0.0:") {
bindProto = "tcp4"
}
ln, err := net.Listen(bindProto, l.Address)
if err != nil {
return nil, nil, nil, err
}
ln = TCPKeepAliveListener{ln.(*net.TCPListener)}
ln, err = listenerWrapProxy(ln, l)
if err != nil {
return nil, nil, nil, err
}
props := map[string]string{
"addr": l.Address,
}
alpnMux := alpnmux.New(ln, nil)
if l.TLSDisable {
if _, err = alpnMux.RegisterProto(alpnmux.NoProto, nil); err != nil {
return nil, nil, nil, err
}
return alpnMux, props, nil, nil
}
tlsConfig, reloadFunc, err := listenerutil.GetTLSConfig(ln, l, props, ui)
if err != nil {
return nil, nil, nil, err
}
// Register no proto, "http/1.1", and "h2", with same TLS config
if _, err = alpnMux.RegisterProto("", tlsConfig); err != nil {
return nil, nil, nil, err
}
if _, err = alpnMux.RegisterProto("http/1.1", tlsConfig); err != nil {
return nil, nil, nil, err
}
if _, err = alpnMux.RegisterProto("h2", tlsConfig); err != nil {
return nil, nil, nil, err
}
return alpnMux, props, reloadFunc, nil
}
func listenerWrapProxy(ln net.Listener, l *configutil.Listener) (net.Listener, error) {
behavior := l.ProxyProtocolBehavior
if behavior == "" {
return ln, nil
}
authorizedAddrs := make([]string, 0, len(l.ProxyProtocolAuthorizedAddrs))
for _, v := range l.ProxyProtocolAuthorizedAddrs {
authorizedAddrs = append(authorizedAddrs, v.String())
}
var policyFunc proxyproto.PolicyFunc
switch behavior {
case "use_always":
policyFunc = func(upstream net.Addr) (proxyproto.Policy, error) {
return proxyproto.USE, nil
}
case "allow_authorized":
if len(authorizedAddrs) == 0 {
return nil, errors.New("proxy_protocol_behavior set but no proxy_protocol_authorized_addrs value")
}
policyFunc = proxyproto.MustLaxWhiteListPolicy(authorizedAddrs)
case "deny_unauthorized":
if len(authorizedAddrs) == 0 {
return nil, errors.New("proxy_protocol_behavior set but no proxy_protocol_authorized_addrs value")
}
policyFunc = proxyproto.MustStrictWhiteListPolicy(authorizedAddrs)
default:
return nil, fmt.Errorf("unknown %q value: %q", "proxy_protocol_behavior", behavior)
}
proxyListener := &proxyproto.Listener{
Listener: ln,
Policy: policyFunc,
}
return proxyListener, nil
}
// TCPKeepAliveListener sets TCP keep-alive timeouts on accepted
// connections. It's used by ListenAndServe and ListenAndServeTLS so
// dead TCP connections (e.g. closing laptop mid-download) eventually
// go away.
//
// This is copied directly from the Go source code.
type TCPKeepAliveListener struct {
*net.TCPListener
}
func (ln TCPKeepAliveListener) Accept() (c net.Conn, err error) {
tc, err := ln.AcceptTCP()
if err != nil {
return
}
tc.SetKeepAlive(true)
tc.SetKeepAlivePeriod(3 * time.Minute)
return tc, nil
}
+8
View File
@@ -0,0 +1,8 @@
// +build !memprofiler
package base
import "github.com/hashicorp/go-hclog"
func StartMemProfiler(_ hclog.Logger) {
}
+42
View File
@@ -0,0 +1,42 @@
// +build memprofiler
package base
import (
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"time"
"github.com/hashicorp/go-hclog"
)
func init() {
memProfilerEnabled = true
}
func StartMemProfiler(logger hclog.Logger) {
profileDir := filepath.Join(os.TempDir(), "watchtowerprof")
if err := os.MkdirAll(profileDir, 0700); err != nil {
logger.Debug("could not create profile directory", "error", err)
return
}
go func() {
for {
filename := filepath.Join(profileDir, time.Now().UTC().Format("20060102_150405")) + ".pprof"
f, err := os.Create(filename)
if err != nil {
logger.Debug("could not create memory profile", "error", err)
}
runtime.GC()
if err := pprof.WriteHeapProfile(f); err != nil {
logger.Debug("could not write memory profile", "error", err)
}
f.Close()
logger.Debug("wrote memory profile", "filename", filename)
time.Sleep(5 * time.Minute)
}
}()
}
+1 -2
View File
@@ -28,7 +28,6 @@ import (
"github.com/hashicorp/vault/sdk/helper/mlock"
"github.com/hashicorp/vault/sdk/logical"
"github.com/hashicorp/watchtower/globals"
"github.com/hashicorp/watchtower/internal/cmd/commands/controller/listener"
"github.com/hashicorp/watchtower/version"
"github.com/mitchellh/cli"
"github.com/ory/dockertest/v3"
@@ -250,7 +249,7 @@ func (b *Server) SetupListeners(ui cli.Ui, config *configutil.SharedConfig) erro
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
}
lnMux, props, reloadFunc, err := listener.New(lnConfig, b.GatedWriter, ui)
lnMux, props, reloadFunc, err := NewListener(lnConfig, b.GatedWriter, ui)
if err != nil {
return fmt.Errorf("Error initializing listener of type %s: %w", lnConfig.Type, err)
}