Files
archie/internal/daemon/statestore_relay.go
T

168 lines
5.0 KiB
Go

package daemon
import (
"context"
"fmt"
"io"
"net"
"net/netip"
"slices"
"strings"
"sync"
"time"
)
// stateStoreDialTimeout bounds one forwarded connection's dial. The target is
// local, so a dial that has not connected by then is a dead State Store rather
// than a slow one.
const stateStoreDialTimeout = 10 * time.Second
// stateStoreRelay carries one task's State Store traffic from an address on
// the host's container bridge to the target the daemon dialed. A container has
// its own network namespace, so a loopback target there names the container
// itself; the relay address is what the container is handed instead. The State
// Store keeps listening exactly where the operator bound it.
type stateStoreRelay struct {
target string
ln net.Listener
// admitted is closed once allowed holds the task container's addresses;
// closed is closed by Close. A connection waits for one of them, so a
// container that dials before it is admitted is held rather than refused.
admitted chan struct{}
closed chan struct{}
closeOnce sync.Once
allowed []netip.Addr
}
// openStateStoreRelay listens on gateway and forwards to addr. The listener is
// per task: it forwards only connections from the addresses Admit names, and
// closing it is what ends the task's access.
func openStateStoreRelay(ctx context.Context, gateway, addr string) (*stateStoreRelay, error) {
ln, err := (&net.ListenConfig{}).Listen(ctx, "tcp", net.JoinHostPort(gateway, "0"))
if err != nil {
return nil, fmt.Errorf("state store relay: listen on %s: %w", gateway, err)
}
r := &stateStoreRelay{target: addr, ln: ln, admitted: make(chan struct{}), closed: make(chan struct{})}
go r.serve(ctx)
return r, nil
}
// Admit names the task container's addresses, the only peers the relay
// forwards. It is called once, after the container exists.
func (r *stateStoreRelay) Admit(addrs []netip.Addr) {
r.allowed = addrs
close(r.admitted)
}
// admits waits until the relay knows its container, then reports whether
// conn comes from it.
func (r *stateStoreRelay) admits(ctx context.Context, conn net.Conn) bool {
select {
case <-r.admitted:
case <-r.closed:
return false
case <-ctx.Done():
return false
}
peer, err := netip.ParseAddrPort(conn.RemoteAddr().String())
if err != nil {
return false
}
return slices.Contains(r.allowed, peer.Addr().Unmap())
}
// Addr is the address the task container is handed.
func (r *stateStoreRelay) Addr() string { return r.ln.Addr().String() }
// Close stops accepting. Streams already open end when the container that owns
// them goes away, and a restarted daemon leaves no listener behind: the
// sockets belong to its process.
func (r *stateStoreRelay) Close() {
r.closeOnce.Do(func() { close(r.closed) })
_ = r.ln.Close()
}
func (r *stateStoreRelay) serve(ctx context.Context) {
for {
downstream, err := r.ln.Accept()
if err != nil {
return
}
go r.forward(ctx, downstream)
}
}
// forward joins one container stream to the target, half-closing each side so
// a gRPC stream drains in both directions before the pair ends.
func (r *stateStoreRelay) forward(ctx context.Context, downstream net.Conn) {
if !r.admits(ctx, downstream) {
_ = downstream.Close()
return
}
ctx, cancel := context.WithTimeout(ctx, stateStoreDialTimeout)
defer cancel()
upstream, err := (&net.Dialer{}).DialContext(ctx, "tcp", r.target)
if err != nil {
_ = downstream.Close()
return
}
var wg sync.WaitGroup
wg.Add(2)
go func() { defer wg.Done(); copyAndClose(upstream, downstream) }()
go func() { defer wg.Done(); copyAndClose(downstream, upstream) }()
wg.Wait()
_ = downstream.Close()
_ = upstream.Close()
}
func copyAndClose(dst, src net.Conn) {
_, _ = io.Copy(dst, src)
if half, ok := dst.(interface{ CloseWrite() error }); ok {
_ = half.CloseWrite()
return
}
_ = dst.Close()
}
// loopbackStateStoreTarget splits target into the address to forward to, and
// reports whether it is this host's loopback: the one case a task container
// cannot reach on its own. A target already addressed for the containers'
// network needs no relay.
func loopbackStateStoreTarget(target string) (addr string, loopback bool) {
addr, host, ok := stateStoreTargetAddr(target)
if !ok || !loopbackHost(host) {
return "", false
}
return addr, true
}
// StateStoreNeedsRelay reports whether task containers can reach target only
// through a relay on the host bridge.
func StateStoreNeedsRelay(target string) bool {
_, loopback := loopbackStateStoreTarget(target)
return loopback
}
// stateStoreTargetAddr is target as host:port, accepting the gRPC scheme form
// ("dns:///host:port") the configuration allows.
func stateStoreTargetAddr(target string) (addr, host string, ok bool) {
target = strings.TrimSpace(target)
if _, rest, found := strings.Cut(target, ":///"); found {
target = rest
}
host, _, err := net.SplitHostPort(target)
if err != nil {
return "", "", false
}
return target, host, true
}
func loopbackHost(host string) bool {
if host == "localhost" {
return true
}
ip := net.ParseIP(host)
return ip != nil && ip.IsLoopback()
}