Replace the service bearer tokens with [tls] ca_file and cert_dir. Caller identity comes from the verified certificate, not metadata. Without TLS, services listen and dial on loopback only. Task containers get only the CA and keep their grant.
94 lines
3.6 KiB
Go
94 lines
3.6 KiB
Go
package staterpc
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/keepalive"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/presence"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/rpctls"
|
|
)
|
|
|
|
// Dial returns a State Store client for target, attributed to the caller
|
|
// service and authenticated by id. The cleanup closes the connection.
|
|
func Dial(target, caller string, id *rpctls.Identity, options ...grpc.DialOption) (*Client, func(), error) {
|
|
target = strings.TrimSpace(target)
|
|
if target == "" {
|
|
return nil, nil, fmt.Errorf("state store target is required")
|
|
}
|
|
creds, err := id.DialOption(target, presence.StateStore)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("state store: %w", err)
|
|
}
|
|
return dial(target, creds, caller, "", options)
|
|
}
|
|
|
|
// DialTask returns the client a task container uses: it trusts caPEM and
|
|
// presents only the task grant, never a service certificate.
|
|
func DialTask(target string, caPEM []byte, grant string) (*Client, func(), error) {
|
|
creds, err := rpctls.CAOnlyDialOption(caPEM, target, presence.StateStore)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("state store: %w", err)
|
|
}
|
|
return dial(target, creds, presence.Agent, grant, nil)
|
|
}
|
|
|
|
func dial(target string, creds grpc.DialOption, caller, grant string, options []grpc.DialOption) (*Client, func(), error) {
|
|
opts := []grpc.DialOption{
|
|
creds,
|
|
// Keepalive detects a dead peer on an otherwise silent watch stream.
|
|
grpc.WithKeepaliveParams(ClientKeepaliveParams()),
|
|
// GetInstalledPackage returns the whole layer, which for an extension is
|
|
// a plugin binary well past gRPC's default 4 MiB receive limit.
|
|
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxPackageResponseBytes)),
|
|
grpc.WithUnaryInterceptor(UnaryClientCredentialInterceptor(caller, grant)),
|
|
grpc.WithStreamInterceptor(StreamClientCredentialInterceptor(caller, grant)),
|
|
}
|
|
conn, err := grpc.NewClient(target, append(opts, options...)...)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("create state store client: %w", err)
|
|
}
|
|
return NewClient(conn), func() { _ = conn.Close() }, nil
|
|
}
|
|
|
|
// maxPackageResponseBytes is the package layer cap plus headroom for the rest
|
|
// of the response.
|
|
const maxPackageResponseBytes = 80 << 20
|
|
|
|
// ClientKeepaliveParams are Dial's keepalive settings: ping after 10s idle,
|
|
// 5s timeout, no pings without streams.
|
|
func ClientKeepaliveParams() keepalive.ClientParameters {
|
|
return keepalive.ClientParameters{
|
|
Time: 10 * time.Second,
|
|
Timeout: 5 * time.Second,
|
|
}
|
|
}
|
|
|
|
// serverKeepaliveMinTime is the server half of the same agreement: the
|
|
// shortest gap between two pings a State Store server accepts before it counts
|
|
// a strike. It is half the client's ping interval rather than a copy of it;
|
|
// ServerKeepaliveOption carries the arithmetic.
|
|
const serverKeepaliveMinTime = 5 * time.Second
|
|
|
|
// ServerKeepaliveOption returns the server keepalive enforcement matching
|
|
// Dial's clients: pings at least 5s apart are allowed while a stream is open.
|
|
func ServerKeepaliveOption() grpc.ServerOption {
|
|
return grpc.KeepaliveEnforcementPolicy(ServerKeepalivePolicy())
|
|
}
|
|
|
|
// ServerKeepalivePolicy returns the policy ServerKeepaliveOption installs.
|
|
func ServerKeepalivePolicy() keepalive.EnforcementPolicy {
|
|
return keepalive.EnforcementPolicy{
|
|
MinTime: serverKeepaliveMinTime,
|
|
PermitWithoutStream: false,
|
|
}
|
|
}
|
|
|
|
// WaitForPeer makes calls to the State Store wait for it to come up
|
|
// instead of failing while it is down or restarting. A call that must not
|
|
// wait carries its own deadline.
|
|
var WaitForPeer = grpc.WithDefaultCallOptions(grpc.WaitForReady(true))
|