2018-08-27 22:21:45 +02:00
|
|
|
package serve
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/zrepl/zrepl/config"
|
|
|
|
"net"
|
2018-08-31 21:51:44 +02:00
|
|
|
"github.com/zrepl/zrepl/daemon/streamrpcconfig"
|
|
|
|
"github.com/problame/go-streamrpc"
|
2018-09-05 01:41:54 +02:00
|
|
|
"context"
|
|
|
|
"github.com/zrepl/zrepl/logger"
|
|
|
|
"github.com/zrepl/zrepl/zfs"
|
2018-08-27 22:21:45 +02:00
|
|
|
)
|
|
|
|
|
2018-09-05 01:41:54 +02:00
|
|
|
type contextKey int
|
|
|
|
|
|
|
|
const contextKeyLog contextKey = 0
|
|
|
|
|
|
|
|
type Logger = logger.Logger
|
|
|
|
|
|
|
|
func WithLogger(ctx context.Context, log Logger) context.Context {
|
|
|
|
return context.WithValue(ctx, contextKeyLog, log)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getLogger(ctx context.Context) Logger {
|
|
|
|
if log, ok := ctx.Value(contextKeyLog).(Logger); ok {
|
|
|
|
return log
|
|
|
|
}
|
|
|
|
return logger.NewNullLogger()
|
|
|
|
}
|
|
|
|
|
|
|
|
type AuthenticatedConn interface {
|
|
|
|
net.Conn
|
|
|
|
// ClientIdentity must be a string that satisfies ValidateClientIdentity
|
|
|
|
ClientIdentity() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// A client identity must be a single component in a ZFS filesystem path
|
|
|
|
func ValidateClientIdentity(in string) (err error) {
|
|
|
|
path, err := zfs.NewDatasetPath(in)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if path.Length() != 1 {
|
|
|
|
return errors.New("client identity must be a single path comonent (not empty, no '/')")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type authConn struct {
|
|
|
|
net.Conn
|
|
|
|
clientIdentity string
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ AuthenticatedConn = authConn{}
|
|
|
|
|
|
|
|
func (c authConn) ClientIdentity() string {
|
|
|
|
if err := ValidateClientIdentity(c.clientIdentity); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return c.clientIdentity
|
|
|
|
}
|
|
|
|
|
|
|
|
// like net.Listener, but with an AuthenticatedConn instead of net.Conn
|
|
|
|
type AuthenticatedListener interface {
|
|
|
|
Addr() (net.Addr)
|
|
|
|
Accept(ctx context.Context) (AuthenticatedConn, error)
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2018-08-27 22:21:45 +02:00
|
|
|
type ListenerFactory interface {
|
2018-09-05 01:41:54 +02:00
|
|
|
Listen() (AuthenticatedListener, error)
|
2018-08-27 22:21:45 +02:00
|
|
|
}
|
|
|
|
|
2018-08-31 21:51:44 +02:00
|
|
|
func FromConfig(g *config.Global, in config.ServeEnum) (lf ListenerFactory, conf *streamrpc.ConnConfig, _ error) {
|
2018-08-27 22:21:45 +02:00
|
|
|
|
2018-08-31 21:51:44 +02:00
|
|
|
var (
|
|
|
|
lfError, rpcErr error
|
|
|
|
)
|
2018-08-27 22:21:45 +02:00
|
|
|
switch v := in.Ret.(type) {
|
|
|
|
case *config.TCPServe:
|
2018-08-31 21:51:44 +02:00
|
|
|
lf, lfError = TCPListenerFactoryFromConfig(g, v)
|
|
|
|
conf, rpcErr = streamrpcconfig.FromDaemonConfig(g, v.RPC)
|
2018-08-27 22:21:45 +02:00
|
|
|
case *config.TLSServe:
|
2018-08-31 21:51:44 +02:00
|
|
|
lf, lfError = TLSListenerFactoryFromConfig(g, v)
|
|
|
|
conf, rpcErr = streamrpcconfig.FromDaemonConfig(g, v.RPC)
|
2018-08-27 22:21:45 +02:00
|
|
|
case *config.StdinserverServer:
|
2018-09-05 01:41:54 +02:00
|
|
|
lf, lfError = MultiStdinserverListenerFactoryFromConfig(g, v)
|
2018-08-31 21:51:44 +02:00
|
|
|
conf, rpcErr = streamrpcconfig.FromDaemonConfig(g, v.RPC)
|
2018-08-27 22:21:45 +02:00
|
|
|
default:
|
2018-08-31 21:51:44 +02:00
|
|
|
return nil, nil, errors.Errorf("internal error: unknown serve type %T", v)
|
2018-08-27 22:21:45 +02:00
|
|
|
}
|
|
|
|
|
2018-08-31 21:51:44 +02:00
|
|
|
if lfError != nil {
|
|
|
|
return nil, nil, lfError
|
|
|
|
}
|
|
|
|
if rpcErr != nil {
|
|
|
|
return nil, nil, rpcErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return lf, conf, nil
|
|
|
|
|
2018-08-27 22:21:45 +02:00
|
|
|
}
|
2018-08-31 21:51:44 +02:00
|
|
|
|
|
|
|
|