frontend

package
v0.13.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2026 License: Apache-2.0 Imports: 24 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const Endpoint = "frontend"

Endpoint is the frontend's name in emu.Endpoints (and netplane.Addr).

View Source
const LANEndpoint = "gfe-lan"

LANEndpoint is the --gfe-bind listeners' Service endpoint name in emu.Endpoints.

View Source
const LANRelayName = "gfe-lan"

LANRelayName names the LAN front-end relay's container, gcpemu-<id>-gfe-lan.

View Source
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

func IsRegistryHost(name string) bool

IsRegistryHost reports whether name is an Artifact Registry Docker host, LOCATION-docker.pkg.dev.

func ListenDNS

func ListenDNS(addr string, h mdns.Handler) (stop func(), err error)

ListenDNS serves h on addr over UDP and TCP and returns once both are listening; stop shuts them down.

func Network

func Network(m *mdns.Msg) string

Network returns the VPC network a query carries in NetworkOption, or "".

func NewReadThrough added in v0.9.0

func NewReadThrough(resolvers []string) http.Handler

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 Normalize

func Normalize(name string) string

Normalize lower-cases a DNS name and strips the trailing dot.

func Pipe

func Pipe(client, server net.Conn)

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

func Relay(l net.Listener, target string) error

Relay accepts TCP connections on l and pipes each to target (used by the in-container 443 relays). It returns when l is closed.

func Serves

func Serves(hosts []string, name string) bool

Serves reports whether name is served by the frontend given the gateway's mounted hosts.

func SetNetwork

func SetNetwork(m *mdns.Msg, network string) *mdns.Msg

SetNetwork returns a copy of m that carries network in NetworkOption.

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.

func (*DNS) ServeDNS

func (d *DNS) ServeDNS(w mdns.ResponseWriter, req *mdns.Msg)

ServeDNS implements mdns.Handler.

type Frontend

type Frontend struct {
	// contains filtered or unexported fields
}

Frontend is the TLS-terminating front end.

func New

func New(env *emu.Env, hosts func() []string) *Frontend

New returns a frontend serving the names hosts returns (the gateway's mounted hosts) plus LOCATION-docker.pkg.dev.

func (*Frontend) Addr

func (f *Frontend) Addr() string

Addr returns the listener address ("" before Start).

func (*Frontend) Close

func (f *Frontend) Close() error

Close stops the listener and open connections, and the --gfe-bind listeners (StopLAN).

func (*Frontend) Hosts

func (f *Frontend) Hosts() []string

Hosts returns the exact hostnames served (registry hosts match by pattern; see IsRegistryHost).

func (*Frontend) Serves

func (f *Frontend) Serves(name string) bool

Serves reports whether the frontend serves name.

func (*Frontend) SetGateway added in v0.10.0

func (f *Frontend) SetGateway(serve func(context.Context, net.Conn) error)

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

func (f *Frontend) Start() error

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

func (f *Frontend) StartLAN(lan LAN)

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.

func (*Frontend) StopLAN added in v0.10.0

func (f *Frontend) StopLAN()

StopLAN stops the --gfe-bind listeners and removes the LAN front-end relay, one still starting included. The Instance calls it before it removes its containers, so that no relay starts after.

func (*Frontend) TLSConfig

func (f *Frontend) TLSConfig() *tls.Config

TLSConfig returns the server TLS configuration (exported for tests and for in-process listeners that want the same certificates).

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL