Documentation
¶
Overview ¶
Package gateway is the control-plane gateway (FR-CORE-040): one listener serving every API over HTTP/1.1 REST and gRPC (h2c), routing by path prefix and Host, authenticating callers and recording the request log.
Index ¶
- Constants
- func APIHosts(api string) []string
- func EmulatorHostVar(api string) string
- func NewServer(h http.Handler, log *slog.Logger) *http.Server
- type DelegatedAPI
- type Gateway
- func (g *Gateway) CloseGate(ctx context.Context) error
- func (g *Gateway) Delegate(api, addr string) error
- func (g *Gateway) DelegateManaged(api string, dial ManagedDialer, record func(project, database string)) error
- func (g *Gateway) DelegatedAPIs() []DelegatedAPI
- func (g *Gateway) GRPCOptions(service string) []grpc.ServerOption
- func (g *Gateway) HandleAdmin(h http.Handler)
- func (g *Gateway) HandleConsole(h http.Handler)
- func (g *Gateway) Hosts() []string
- func (g *Gateway) Middleware(service string, h http.Handler) http.Handler
- func (g *Gateway) OpenGate()
- func (g *Gateway) Register(s emu.Service) error
- func (g *Gateway) Router(service string) emu.Router
- func (g *Gateway) Serve(l net.Listener) error
- func (g *Gateway) ServeFrontend(ctx context.Context, c net.Conn) error
- func (g *Gateway) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (g *Gateway) ServeLinked(w http.ResponseWriter, r *http.Request, from, principal string)
- func (g *Gateway) SetAdminToken(tok string)
- func (g *Gateway) SetLinks(l Links)
- func (g *Gateway) Shutdown(ctx context.Context) error
- type Links
- type ManagedDialer
Constants ¶
const ( AdminTokenHeader = "X-Gcpemu-Admin-Token" AdminTokenCookie = "gcpemu_admin" // AdminTokenQuery is the console URL parameter that sets the cookie. AdminTokenQuery = "token" )
Admin token transport (FR-UI-009, ADR 0006).
const PathHost = "www.googleapis.com"
PathHost is Google's legacy shared API host. Its URLs carry the API in the path (www.googleapis.com/storage/v1/..., /oauth2/v3/certs, the compute selfLink base), which is exactly the gateway's path routing, so it is always served without a host mount.
Variables ¶
This section is empty.
Functions ¶
func APIHosts ¶ added in v0.12.0
APIHosts returns the real hostnames on which Google serves the API named api, which the Google front end serves when it is delegated.
func EmulatorHostVar ¶ added in v0.12.0
EmulatorHostVar returns the variable, such as FIRESTORE_EMULATOR_HOST, that the client libraries of the API named api read for its emulator's address, or "" when they read none.
Types ¶
type DelegatedAPI ¶ added in v0.12.0
type DelegatedAPI struct {
API string `json:"api"`
// Address is "" for a Managed emulator, whose address the Instance
// knows.
Address string `json:"address"`
// Managed is whether the Instance runs the External emulator (ADR
// 0029).
Managed bool `json:"managed,omitempty"`
Hosts []string `json:"hosts"` // the hostnames the Google front end serves for it
}
DelegatedAPI is a Delegated API and its External emulator.
type Gateway ¶
type Gateway struct {
// Faults holds fault-injection rules applied to every request (FR-CORE-060).
Faults fault.Set
// contains filtered or unexported fields
}
Gateway multiplexes REST and gRPC for all services.
func (*Gateway) CloseGate ¶ added in v0.9.0
CloseGate refuses every new non-Admin request as UNAVAILABLE, cancels those in progress, streams included, and waits for their handlers to return, or returns ctx's error when it ends first. The gate stays closed until OpenGate, even on error. An Instance closes it for the length of a Reset or State snapshot restore (#302).
func (*Gateway) Delegate ¶ added in v0.12.0
Delegate forwards the API named api to the External emulator at addr (host:port). It refuses an API that a registered Service serves, by its path, one of its hostnames or a gRPC service in its google.API package, and one already delegated or whose hostname another Delegated API has. Call it after Register, before Serve.
func (*Gateway) DelegateManaged ¶ added in v0.12.0
func (g *Gateway) DelegateManaged(api string, dial ManagedDialer, record func(project, database string)) error
DelegateManaged forwards the API named api, as Delegate does, to the Managed emulator that dial connects to (ADR 0029). For Firestore and Datastore, record is told the project and database of each request it forwards (ADR 0036); it may be nil.
func (*Gateway) DelegatedAPIs ¶ added in v0.12.0
func (g *Gateway) DelegatedAPIs() []DelegatedAPI
DelegatedAPIs returns the Delegated APIs, sorted by API name.
func (*Gateway) GRPCOptions ¶
func (g *Gateway) GRPCOptions(service string) []grpc.ServerOption
GRPCOptions returns interceptors that authenticate and log gRPC calls; service names the owner for per-service servers ("" = look up by method).
func (*Gateway) HandleAdmin ¶
HandleAdmin mounts the admin API (not request-logged; only the Admin token guards it, see SetAdminToken).
func (*Gateway) HandleConsole ¶ added in v0.2.0
HandleConsole mounts the Web console at /console and /console/ (not request-logged; FR-UI-002). Only the Admin token guards it.
func (*Gateway) Hosts ¶
Hosts returns the sorted real hostnames mounted with host routing (e.g. "storage.googleapis.com") plus PathHost; the Google frontend (FR-CORE-043, FR-INT-007) serves exactly these names.
func (*Gateway) Middleware ¶
Middleware authenticates, recovers from panics and logs each HTTP request.
func (*Gateway) OpenGate ¶ added in v0.9.0
func (g *Gateway) OpenGate()
OpenGate serves requests again after CloseGate.
func (*Gateway) ServeFrontend ¶ added in v0.10.0
ServeFrontend serves c, a connection whose TLS the Google front end (internal/frontend) terminated, as plain HTTP/1.1 or prior-knowledge h2c like the API gateway's port. Requests on it arrive with X-Forwarded-Proto: https, so links services render (GCS session URIs, selfLink, mediaLink) use the scheme the client used (#440). The hand-off is in process: no listener exists that a client could reach to claim the marker, and a client-sent X-Forwarded-Proto on the API gateway's port is left as it was.
ServeFrontend returns once the API gateway has closed c, with net.ErrClosed after Shutdown, or with ctx's error if ctx ends before the API gateway accepts c.
func (*Gateway) ServeHTTP ¶
func (g *Gateway) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP answers the Instance CA's CRL, dispatches gRPC, host-mode and path-routed requests, and forwards those naming a Delegated Project.
func (*Gateway) ServeLinked ¶ added in v0.12.0
ServeLinked serves r, which a paired Instance forwarded over the link from, asserting that its caller is principal. It serves only requests naming Projects, all of which this Instance shares with that link, as its API gateway serves them otherwise, but never forwards them on, and never serves the Admin API or the Web console. A merged list it answers with the items of the Projects shared with that link.
func (*Gateway) SetAdminToken ¶ added in v0.6.0
SetAdminToken makes every Admin API and Web console request present tok (the API gateway is bound beyond loopback); "" turns the guard off. Call it before Serve.
type Links ¶ added in v0.12.0
type Links interface {
// Delegating reports whether any Project is delegated.
Delegating() bool
// DelegatedLinks returns the links any Project is delegated to, by
// name.
DelegatedLinks() []string
// DelegatedTo returns the link the Project ref, an ID or number, is
// delegated to.
DelegatedTo(ref string) (string, bool)
Shared(name, ref string) bool
// BucketOwner returns the link whose Linked Instance has the bucket,
// and its Project there.
BucketOwner(bucket string) (link, project string, ok bool)
// RefreshBuckets asks the link name for its buckets now.
RefreshBuckets(ctx context.Context, name string)
// Forward forwards r over the link name, asserting principal, or calls
// fail, having written nothing. answered, when not nil, sees the
// Linked Instance's answer before it is written.
Forward(w http.ResponseWriter, r *http.Request, name, principal string, fail func(http.ResponseWriter, error), answered func(*http.Response))
}
Links is what the API gateway needs of the Instance's links.