Documentation
¶
Overview ¶
Package frontend is the emulator's "Google front end": a TLS terminator that lets unmodified clients reach emulated services by their real hostnames (storage.googleapis.com, pubsub.googleapis.com, LOCATION-docker.pkg.dev, ...) over HTTPS, HTTP/2 and gRPC (FR-INT-007, FR-CORE-043).
Design ¶
The frontend is one TCP listener in the emulator process (endpoint "frontend", dynamic port unless --port frontend=N). For each connection it completes a TLS handshake with a certificate issued by the instance's local CA (env.CA) for the requested SNI, offering ALPN "h2" and "http/1.1", then passes the decrypted byte stream unchanged to:
- the Artifact Registry listener for LOCATION-docker.pkg.dev, or
- the API gateway for every other name, in process (SetGateway), so the API gateway knows the client used HTTPS and services render https links (#440).
Both backends speak HTTP/1.1 and prior-knowledge h2c, so whichever protocol the client negotiated works end to end, including gRPC, streaming and trailers, and the Host / :authority header arrives untouched; the gateway's host routing (emu.Router Mount hosts) then picks the service. Only names the emulator serves get a certificate: the gateway's mounted hosts and LOCATION-docker.pkg.dev. A connection without SNI (a client dialing an IP address, such as kube-apiserver calling an admission webhook) gets a certificate for "localhost" and every IP address of the host, which includes the container networks' gateway addresses.
Port 443 is reached from containers through small TCP relays rather than by binding it on the host (which needs root): the GKE node agent listens on 169.254.169.254:443 in every node, and the host-mode container (internal/hostmode) listens on its own address. Both resolve the served names to themselves with the DNS Relay in this package.
Index ¶
- Constants
- func IsRegistryHost(name string) bool
- func ListenDNS(addr string, h mdns.Handler) (stop func(), err error)
- func Network(m *mdns.Msg) string
- func NewReadThrough(resolvers []string) http.Handler
- func Normalize(name string) string
- func Pipe(client, server net.Conn)
- func Relay(l net.Listener, target string) error
- func Serves(hosts []string, name string) bool
- func SetNetwork(m *mdns.Msg, network string) *mdns.Msg
- type DNS
- type Frontend
- func (f *Frontend) Addr() string
- func (f *Frontend) Close() error
- func (f *Frontend) Hosts() []string
- func (f *Frontend) Serves(name string) bool
- func (f *Frontend) SetGateway(serve func(context.Context, net.Conn) error)
- func (f *Frontend) Start() error
- func (f *Frontend) StartLAN(lan LAN)
- func (f *Frontend) StopLAN()
- func (f *Frontend) TLSConfig() *tls.Config
- type LAN
- type Storage
Constants ¶
const Endpoint = "frontend"
Endpoint is the frontend's name in emu.Endpoints (and netplane.Addr).
const LANEndpoint = "gfe-lan"
LANEndpoint is the --gfe-bind listeners' Service endpoint name in emu.Endpoints.
const LANRelayName = "gfe-lan"
LANRelayName names the LAN front-end relay's container, gcpemu-<id>-gfe-lan.
const NetworkOption = 65400
NetworkOption is the EDNS0 local option (RFC 6891 private range) with which a VPC's DNS relay tells the emulated Cloud DNS the VPC network a query comes from ("projects/P/global/networks/N"), so that private zones are answered only on the networks they are bound to (FR-DNS-004).
Variables ¶
This section is empty.
Functions ¶
func IsRegistryHost ¶
IsRegistryHost reports whether name is an Artifact Registry Docker host, LOCATION-docker.pkg.dev.
func ListenDNS ¶
ListenDNS serves h on addr over UDP and TCP and returns once both are listening; stop shuts them down.
func NewReadThrough ¶ added in v0.9.0
NewReadThrough returns the handler that sends requests on to real GCS, resolving storage.googleapis.com with resolvers (host:port), the host's real DNS servers, since the host's own resolver may send it back here in Host mode. It honours HTTPS_PROXY. With no resolvers it returns nil.
func Pipe ¶
Pipe copies between client and server in both directions. When the server side ends, the client gets a half-close and the connection is torn down shortly after; a client half-close is passed on and the server may keep answering.
func Relay ¶
Relay accepts TCP connections on l and pipes each to target (used by the in-container 443 relays). It returns when l is closed.
Types ¶
type DNS ¶
type DNS struct {
// Answer returns the IPv4 address for name (normalized, no trailing
// dot), or nil when the name is relayed.
Answer func(name string) net.IP
// Upstreams returns the servers (host:port) a name is relayed to;
// none yields SERVFAIL.
Upstreams func(name string) []string
// TTL of synthesized answers (default 60 s).
TTL uint32
// Network, when set, tags relayed queries with the VPC network they
// come from (NetworkOption).
Network string
}
DNS is a small resolver that answers the names a relay serves with its own address and relays everything else upstream. The GKE node agent (cluster DNS upstream) and the host-mode container use it so that *.googleapis.com names the emulator serves resolve to their 443 relay.
type Frontend ¶
type Frontend struct {
// contains filtered or unexported fields
}
Frontend is the TLS-terminating front end.
func New ¶
New returns a frontend serving the names hosts returns (the gateway's mounted hosts) plus LOCATION-docker.pkg.dev.
func (*Frontend) Close ¶
Close stops the listener and open connections, and the --gfe-bind listeners (StopLAN).
func (*Frontend) Hosts ¶
Hosts returns the exact hostnames served (registry hosts match by pattern; see IsRegistryHost).
func (*Frontend) SetGateway ¶ added in v0.10.0
SetGateway makes the frontend hand API gateway connections to serve (Gateway.ServeFrontend) instead of dialing the "gateway" endpoint, where they look like direct plain-HTTP clients. Without it (tests building a Frontend alone) the frontend dials the endpoint. Call it before Start.
func (*Frontend) Start ¶
Start opens the listener on the configured bind address and records the "frontend" endpoint. A dynamic port comes from --port-range, except in a Containerized Instance, whose published range ports forwarding rules and LoadBalancer Services need (ADR 0012). The port is settled before the endpoint is recorded, so it never moves.
func (*Frontend) StartLAN ¶ added in v0.9.0
StartLAN serves lan on every --gfe-bind address. An address whose port 443 the emulator may not bind is relayed by the LAN front-end relay when it can be (lanrelay.go); any other failed address is logged and skipped.
type LAN ¶ added in v0.9.0
type LAN struct {
// Gateway serves every real hostname but the registry's.
Gateway http.Handler
// Registry returns Artifact Registry's authenticating handler, or nil
// while it isn't running.
Registry func() http.Handler
// Storage classifies storage.googleapis.com requests; nil without
// Cloud Storage.
Storage Storage
// ReadThrough serves reads of buckets the Instance doesn't have
// (NewReadThrough); nil sends them through the credentials check like
// any other.
ReadThrough http.Handler
}
LAN is where the --gfe-bind listeners send the requests they admit.
type Storage ¶ added in v0.9.0
type Storage interface {
// ForeignBucket reports whether r reads a bucket the Instance doesn't have.
ForeignBucket(r *http.Request) bool
// Signed reports whether r carries a signature Cloud Storage verifies.
Signed(r *http.Request) bool
}
Storage is what the LAN listeners ask Cloud Storage.