Documentation
¶
Overview ¶
Package httpadapter holds request handling shared by the adapters that receive requests over net/http: the reverse proxy and forward-auth.
Index ¶
- func GRPCCode(code int) codes.Code
- func GRPCMessage(code int, id string) string
- func IsEvent(v pipeline.Verdict) bool
- func IsToken(s string) bool
- func LogEvent(ctx context.Context, logger *slog.Logger, rec *events.Recorder, ...)
- func NewRequestID() string
- func PeerAddr(remoteAddr string) netip.Addr
- func RequestID(id string) string
- func SnapshotHeaders(r *http.Request) http.Header
- func URIPath(rawURI string) (string, error)
- func WriteDenial(w http.ResponseWriter, status int, id string, reqHeaders http.Header, ...)
- func WriteGRPCDenial(w http.ResponseWriter, p GRPCProtocol, httpStatus, code int, id string)
- type Denial
- type GRPCProtocol
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GRPCCode ¶ added in v0.4.0
GRPCCode is the gRPC status of a denial with HTTP status code: gRPC's own mapping of HTTP statuses (doc/http-grpc-status-mapping.md), which clients apply to an HTTP answer, so a client sees the same code through every proxy; and RESOURCE_EXHAUSTED for an oversized body (413), which that mapping leaves UNKNOWN.
func GRPCMessage ¶ added in v0.4.0
GRPCMessage is the grpc-message of a denial with HTTP status code: the status text, as the body of an HTTP denial, and the request ID, which a gRPC client usually logs with the status. It is percent-encoded, as the gRPC spec says.
func IsEvent ¶
IsEvent reports whether a verdict is logged as a security event: blocks and detections, and every request that went uninspected unexpectedly (timeout, error, overload). Client cancellations and requests uninspected by configuration (a skipped route, no policy for the route, inspection suspended) are not events: they are counted in the metrics, and logging each would flood the logs. Neither are the requests an engine denies before it has a policy bundle (bootstrap deny): the engine logs that once.
func LogEvent ¶
func LogEvent(ctx context.Context, logger *slog.Logger, rec *events.Recorder, req *request.Request, path string, v pipeline.Verdict)
LogEvent writes one security event for req, and records it for the operator when rec is set. Lines are rate-limited per policy and rule by rec's limiter; the events for the operator are not. The line says where each rule matched (the variable's name), never what it matched.
func NewRequestID ¶
func NewRequestID() string
NewRequestID returns a random 16-hex-digit request ID.
func RequestID ¶
RequestID returns id if it is a safe correlation ID (at most 128 of [A-Za-z0-9._:-]), and "" otherwise so that one is generated. The value is client-controlled and ends up in logs and events.
func SnapshotHeaders ¶
SnapshotHeaders returns a private copy of r's headers for inspection, with Transfer-Encoding and Content-Length restored.
The copy matters because an evaluation that times out keeps running after the handler has returned, and must not read the live request. Copying costs about 0.3µs, well under 0.1% of an evaluation.
net/http moves Transfer-Encoding out of the header map, and Content-Length can be absent (HTTP/2) even when the length is known. Without them, rules see a POST with neither header and raise a false positive.
func URIPath ¶
URIPath validates a request target received from a proxy and returns its decoded path. The target must be in origin form and must not contain a fragment: rules stop reading the URI at '#' while the application may not.
func WriteDenial ¶ added in v0.5.0
WriteDenial answers a denied (or unreadable) request with status: its request ID in X-Request-Id, and DenialBody's body.
func WriteGRPCDenial ¶ added in v0.4.0
func WriteGRPCDenial(w http.ResponseWriter, p GRPCProtocol, httpStatus, code int, id string)
WriteGRPCDenial answers a denied gRPC call in the trailers-only form: the gRPC status in the headers, no message. httpStatus is the HTTP status: 200, as a gRPC server answers, unless the proxy needs another to deny (forward-auth); gRPC clients read the gRPC status either way. code is the verdict's HTTP status, which gives the gRPC status.
Types ¶
type Denial ¶ added in v0.5.0
Denial is a denied request's answer, the same from every adapter (docs/troubleshooting.md, "What a denied client sees"): the status, X-Request-Id, and a short body. A client that accepts application/json gets a JSON body instead of text when JSON denials are on (--json-denials).
type GRPCProtocol ¶ added in v0.4.0
type GRPCProtocol uint8
GRPCProtocol is the gRPC variant a request asks for by its Content-Type, and so how a denial is answered.
const ( // NotGRPC is any other request: denied with a plain HTTP answer. NotGRPC GRPCProtocol = iota // GRPC is gRPC over HTTP/2: application/grpc, with any codec. GRPC // GRPCWeb is gRPC-Web: application/grpc-web, with any codec. GRPCWeb // GRPCWebText is gRPC-Web in base64: application/grpc-web-text. GRPCWebText )
func GRPCOf ¶ added in v0.4.0
func GRPCOf(h http.Header) GRPCProtocol
GRPCOf returns the gRPC variant a request with headers h asks for: by its one Content-Type, as Envoy tells a gRPC request (application/grpc, alone or with a "+codec" suffix). Unlike the pipeline's test of a gRPC call, the body doesn't matter: this only chooses the form of an answer the client can read.
func (GRPCProtocol) ContentType ¶ added in v0.4.0
func (p GRPCProtocol) ContentType() string
ContentType is the media type of a denial's answer: the protocol's, without a codec (a trailers-only answer carries no message).
func (GRPCProtocol) String ¶ added in v0.4.0
func (p GRPCProtocol) String() string