format

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: Apache-2.0 Imports: 12 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 format holds the one description of each input file and checks a file's tree against it.

Index

Constants

View Source
const (
	AgentExpect    = "an IPv4 address and a port from 1 to 65535, such as 127.0.0.1:2000"
	EndpointExpect = "an IP address or a DNS name, and a port, such as 127.0.0.1:4317 or [fd00::40]:4317"
)

AgentExpect and EndpointExpect are exported because the reader's message for an agent or an endpoint its pattern admits but ParseAgent or ParseEndpoint refuses must be the check's, and the reader's tooLarge cannot walk the provider's Either to read it from the description.

Variables

View Source
var (
	MatchKinds   = []string{"prefix", "path", "pattern"}
	ServicePorts = []Field{withRule(port("http", "The service's HTTP port.", false), "Spoken as HTTP/1.1."),
		withRule(port("grpc", "The service's gRPC port.", false), "Spoken as HTTP/2 without TLS (h2c).")}
)

MatchKinds are the keys of which a route writes exactly one, and ServicePorts the port keys of a service, each named by its protocol. The decoder reads a route's match and a service's ports through these two lists, so a key added to either is read without a second list to keep in step.

View Source
var (
	CheckRequired = []string{"timeout", "enabled", "when_unavailable"}
	CheckKeys     = slices.Concat(CheckRequired, []string{"only_with_header", "send", "accept", "send_request"})
)

CheckRequired are the keys of auth that ask needs beside it, and CheckKeys every key of auth that sets the check, which a file without ask cannot use. The schema writes the rule from them (authNeeds) and the reader refuses by them (internal/read/decode.go, the decoder's auth method), so the two cannot differ.

View Source
var Health = Field{Name: "health", Doc: "Active checks of each backend: one that fails unhealthy checks in a row leaves the pool until it passes healthy checks in a row.",
	Kind: Map, Rule: "At least one of `path` and `grpc`.", Fields: []Field{
		withRule(Field{Name: "path", Doc: "The path an HTTP GET must answer with a 2xx, on the service's http port.", Kind: Text,
			Pattern: `^/[^\t\n\f\r ]*$`, Expect: "a path starting with /, with no space"}, "Needs an `http` port."),
		{Name: "grpc", Doc: "true checks the service's grpc port with the gRPC health protocol, grpc.health.v1.Health/Check.", Kind: True,
			Rule: "Needs a `grpc` port."},
		{Name: "interval", Doc: "How often each backend is checked; 5s when not written.", Kind: Text, Pattern: model.TimePattern, Expect: "a time such as 5s",
			Rule: "At least `1s`."},
		{Name: "timeout", Doc: "How long one check may take; 2s when not written.", Kind: Text, Pattern: model.TimePattern, Expect: "a time such as 2s",
			Rule: "Shorter than `interval`."},
		{Name: "healthy", Doc: "Checks in a row a backend must pass to join the pool; 2 when not written.", Kind: Int, Min: 1, Max: 255},
		{Name: "unhealthy", Doc: "Checks in a row a backend must fail to leave the pool; 3 when not written.", Kind: Int, Min: 1, Max: 255,
			Rule: "With `grpc`, the Envoy generator's check removes a backend that answers it is not serving at once."},
	}}

Health is a service's health key. It is exported because the reader quotes its Expect for a time the parser refuses, and cannot walk the services' Dict to reach it.

View Source
var Methods = Field{
	Doc:  "The RPC methods the gateway routes, each with what the check is told about it.",
	Kind: List,
	Item: &methodField,
}

Methods describes the RPC methods file: one entry per method. Which service a method goes to is decided by the rpc entry of the proxyfile, not here.

View Source
var Origins = Field{
	Doc:  "The origins file: the CORS settings and, for each environment, its hosts and the browser origins it admits.",
	Kind: Map,
	Fields: []Field{
		corsField,
		{Name: "environments", Doc: "The environments, each with its hosts and the browser origins it admits.", Kind: List, Required: true,
			Rule: "At least one environment.",
			Item: &Field{Doc: "One environment.", Kind: Map, Fields: slices.Concat([]Field{
				{Name: "name", Doc: "The environment's name.", Kind: Text, Required: true,
					Rule: "It names the environment's virtual host. Not empty, and not used by another environment."},
			}, environmentLists)}},
	},
}

The rules for hosts, origins and patterns belong to the origins reader, which names the environment in its messages; this description checks only the keys and the kinds.

View Source
var Proxyfile = Field{
	Doc:   "A proxyfile: one gateway, the services behind it and the routes to them, from which proxyfile writes the proxy's configuration.",
	Kind:  Map,
	Moved: map[string]string{"namespace": "envoy.namespace"},
	Fields: []Field{
		port("listen", "The port of the public listener, on 0.0.0.0.", true),
		withRule(port("admin", "The port of the admin listener, on 127.0.0.1; no admin listener when not written.", false),
			"Not the same as `listen`. It answers only `/ready`, `/stats` and `/server_info`."),
		{Name: "tls", Doc: "The public listener serves HTTPS with this certificate and key; without it, plain HTTP.", Kind: Map,
			Rule: "The proxy reads the files when it starts.", Fields: []Field{
				absolutePath("certificate", "The certificate chain, PEM, a file on the proxy's host.", true),
				absolutePath("key", "The private key, PEM, a file on the proxy's host.", true),
				{Name: "clients", Doc: "Require a client certificate on every connection (mutual TLS).", Kind: Map, Fields: []Field{
					absolutePath("ca", "The CA bundle, PEM, that a client's certificate must chain to.", true),
				}},
			}},
		{Name: "client_address", Doc: "Which address counts as the client's: forwarded, the last address a proxy in front appended to X-Forwarded-For, or connection, the peer of the TCP connection.",
			Kind: Enum, Values: []string{"forwarded", "connection"},
			Rule: "When not written: `connection` in a file that writes `tls`, `forwarded` otherwise. A rate limit by client and `hash.client` read it."},
		{Name: "limits", Doc: "Limits on connections and request headers.", Kind: Map, Moved: map[string]string{"heap": "envoy.heap"}, Fields: []Field{
			{Name: "connections", Doc: "The most connections at once, from clients and to each service.", Kind: Int, Min: 1, Max: largestUint32},
			{Name: "request_headers", Doc: "The largest header block and the most header fields a request may carry.", Kind: Map, Fields: []Field{

				{Name: "size", Doc: "The largest header block of a request.", Kind: Text, Required: true, Pattern: model.SizePattern,
					Expect: "a size such as 96KiB", Rule: "At most `8MiB`."},
				{Name: "count", Doc: "The most header fields in a request.", Kind: Int, Required: true, Min: 1, Max: largestUint32},
			}},
		}},
		{Name: "timeouts", Doc: "How long a request, an idle request and an idle connection to a service may last.", Kind: Map, Fields: []Field{
			withRule(timeOrNone("request", "The longest a whole request may take."),
				"Without it, the Envoy configuration keeps Envoy's default of 15 seconds, counted from the end of the request. `none` removes the limit."),
			timeOrNone("idle", "The longest a request may go without sending or receiving data."),
			withRule(timeOrNone("upstream_idle", "The longest a connection to a service is kept open with no request on it."),
				"Without it, the Envoy configuration keeps Envoy's default of 1 hour."),
		}},
		{Name: "services", Doc: "The services behind the gateway, by name.", Kind: Dict, Required: true, Rule: "At least one service.",
			Key: &Field{Name: "service name", Kind: Text, Pattern: serviceNamePattern, Expect: serviceNameExpect},
			Item: &Field{Doc: "One service: a port per protocol, an optional Host header, its backends, how requests are spread over them and how they are checked.", Kind: Map, Rule: "It has at least one port.", Fields: slices.Concat(ServicePorts, []Field{
				{Name: "host_header", Doc: "The Host header a request carries when a route sends it to this service.",
					Kind: Text, Pattern: `^[A-Za-z0-9.:\[\]-]+$`, Expect: "a host, with an optional port", Rule: "Without it a request keeps the client's Host."},
				{Name: "address", Doc: "The service's IP address, or a DNS name the proxy looks up again while it runs; 127.0.0.1 when not written.",
					Kind: Text, Pattern: `^[^\t\n\f\r ]+$`, Expect: "an IP address or a DNS name",
					Rule: "A name has no port and is looked up every 5 seconds; every address it gives is a backend. Not with `addresses`."},
				{Name: "addresses", Doc: "The service's backends, by IP address, each on the service's ports.", Kind: List,
					Item: &Field{Kind: Text, Pattern: `^[^\t\n\f\r ]+$`, Expect: "an IP address"},
					Rule: "Two or more, none written twice. Not with `address`."},
				{Name: "balance", Doc: "How requests are spread over the backends; round_robin when not written.", Kind: Enum,
					Values: []string{"round_robin", "least_requests", "random", "hash"}},
				{Name: "hash", Doc: "What balance hash hashes, so that one client or one header value always reaches the same backend.", Kind: Map,
					OneOf: []string{"header", "client"}, Rule: "Required with `balance: hash`, and refused with any other balance.", Fields: []Field{
						{Name: "header", Doc: "The request header whose value is hashed; a request without it goes to a backend chosen at random.",
							Kind: Text, Pattern: lowerHeaderPattern, Expect: lowerHeaderExpect},
						{Name: "client", Doc: "true hashes the client's address, the one client_address names.", Kind: True},
					}},
				Health,
			})}},
		{Name: "dns", Doc: "The name lookups of every DNS name the gateway reaches: a service's address, the tracing endpoint and the keys URL's host.", Kind: Map, Fields: []Field{
			{Name: "resolver", Doc: "The name server the proxy asks; the host's own resolver configuration when not written.", Kind: Text,
				Pattern: `^[^\t\n\f\r ]+$`, Expect: "an IP address with an optional port, such as 10.0.0.2:53", Rule: "Port 53 when not written. An IPv6 address with a port is written in brackets, such as [fd00::53]:5353; no zone."},
		}},

		{Name: "auth", Doc: "The checks every request goes through before it reaches a service: a service of yours asked about each request, " +
			"a signed token the proxy verifies, or both.", Kind: Map, Rule: "At least one of `ask` and `jwt`. Without `auth` there is no check.", AtLeastOneOf: []string{"ask", "jwt"}, Needs: authNeeds, Fields: []Field{
			{Name: "ask", Doc: "The service and the path the gateway asks.", Kind: Text,
				Pattern: `^[a-z][a-z0-9-]* /[^\t\n\f\r ]*$`, Expect: "a service and a path, such as accounts /check", Rule: "The service needs an `http` port."},
			{Name: "timeout", Doc: "How long the gateway waits for the answer.", Kind: Text,
				Pattern: model.TimePattern, Expect: "a time such as 2s", Rule: "Required with `ask`, and refused without it. At most `4294967295s`. `none` is not accepted."},
			{Name: "enabled", Doc: "true runs the check on every request it applies to, false on none.", Kind: Bool,
				Rule: "Required with `ask`, and refused without it. It does not turn `jwt` off."},
			{Name: "when_unavailable", Doc: "What happens to a request when the check cannot be asked: allow lets it through, deny refuses it.",
				Kind: Enum, Values: []string{"allow", "deny"}, Rule: "Required with `ask`, and refused without it."},

			{Name: "only_with_header", Doc: "The check runs only on requests that carry this header.",
				Kind: Text, Pattern: lowerHeaderPattern, Expect: lowerHeaderExpect, Rule: "A request without it, or with it empty, goes through with no check. Needs `ask`."},
			withRule(headers("send", "The request headers the check is sent; a name ending in * is a prefix."), "Needs `ask`."),
			withRule(headers("accept", "The headers of the check's answer that reach the service; a name ending in * is a prefix."), "Needs `ask`."),
			{Name: "send_request", Doc: "true also tells the check which request it is asked about: its path and query in x-forwarded-uri, " +
				"its method in x-forwarded-method, its scheme in x-forwarded-proto and its host in x-forwarded-host; false when not written.",
				Kind: Bool, Rule: "Needs `ask`."},
			jwtField,
		}},
		withRule(headers("remove_from_clients", "Headers removed from every client request before routing and before any filter; a name ending in * is a prefix."),
			"No route, filter or service sees a client's copy. Envoy holds header names in lower case, so a name with a capital would never match."),
		withRule(corsField, "Not written when `origins` names a file, which holds its own `cors`."),
		{Name: "origins", Doc: "The browser origins each environment admits, by environment name; or, as in 1.0, the name of an origins file, relative to this file.",
			Kind: Either, Required: true, Expect: "a mapping of environments by name, or a file name", Rule: "At least one environment.", Choices: []Field{
				{Kind: Text, Pattern: `^[^\t\n\f\r ]+$`, Expect: "a file name"},
				{Kind: Dict, Key: &Field{Name: "environment name", Kind: Text, Pattern: `[\s\S]`, Expect: "one character or more"},
					Item: &Field{Doc: "One environment: its hosts and the browser origins it admits.", Kind: Map, Fields: environmentLists}},
			}},
		{Name: "routes", Doc: "The routes, tried in the order written; the rpc entry marks where the RPC routes go.", Kind: List, Required: true,
			Rule: "Not empty. The rpc entry is `- rpc:` in YAML and `{ rpc { ... } }` in HOCON.",
			Item: &Field{Kind: Either, Expect: "a mapping: a route or an rpc entry", Choices: []Field{
				{Doc: "The place in the order where the RPC routes go.", Kind: Map, Select: "rpc", Fields: []Field{
					{Name: "rpc", Doc: "The RPC routes: one exact-path route for each RPC method.", Kind: Map, Required: true,
						Rule: "At most one rpc entry in a file. It needs methods from exactly one of `rpc.methods`, `rpc.proto` and `--rpc-methods`.", Fields: []Field{
							retries("How many times an RPC call that never reached its service is tried again."),
							withRule(RateLimit, "One limit for every RPC route of the entry, in every environment."),
							{Name: "to", Doc: "The service the RPC methods of each package go to.", Kind: Dict, Required: true, Rule: "Not empty.",
								Key: &Field{Name: "package pattern", Kind: Text,
									Doc: "Which services' methods go to the value: * matches every service, a package name ending in .* every service " +
										"whose full name starts with that package and a dot, and a full service name that service alone.",
									Pattern: `^(\*|[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)*(\.\*)?)$`,
									Expect:  "*, a package name ending in .*, or a full service name"},
								Item: &Field{Kind: Either, Expect: "a service name, or a mapping with service and name", Rule: "The service needs a `grpc` port.", Choices: []Field{
									serviceName("", "The service the methods go to.", false),
									{Doc: "The service the methods go to, and the name of their routes.", Kind: Map, Fields: []Field{
										serviceName("service", "The service the methods go to.", true),
										{Name: "name", Doc: "The name of the routes; the service's name followed by -rpc when not written.",
											Kind: Text, Pattern: routeNamePattern, Expect: routeNameExpect},
									}},
								}}},
						}},
				}},
				{Doc: "A route: a name, what it matches and the service it goes to.", Kind: Map, OneOf: MatchKinds, Fields: []Field{
					{Name: "name", Doc: "The route's name.", Kind: Text, Required: true, Pattern: routeNamePattern, Expect: routeNameExpect,
						Rule: "It appears in the request log. Not used twice, and not `rpc`."},
					path("prefix", "Matches paths that start with this text."),
					path("path", "Matches this path exactly."),

					{Name: "pattern", Doc: "Matches paths by this regular expression, in RE2 syntax.", Kind: Text,
						Pattern: `[\s\S]`, Expect: "a regular expression that is not empty", Rule: "It must compile."},
					{Name: "method", Doc: "Matches only requests with this HTTP method.", Kind: Text, Pattern: `^[A-Z]+$`, Expect: "an HTTP method in capitals, such as POST"},
					withRule(serviceName("to", "The service the route goes to.", true), "A declared service with an `http` port."),
					withRule(path("rewrite", "Replaces the matched prefix or path before the request goes to the service."), "Not allowed with `pattern`."),
					{Name: "auth", Doc: "off sends the route's requests to the service without auth's checks: no service is asked and no token is verified.", Kind: Enum, Values: []string{"off"},
						Rule: "Needs an `auth` key."},
					retries("How many times a request that never reached the service is tried again."),
					withRule(RateLimit, "One limit for the route's copy in every environment."),
				}},
			}}},
		{Name: "rpc", Doc: "The RPC methods the gateway routes; the routes' rpc entry maps them to services.", Kind: Map,
			Rule:  "Written here or read from `.proto` files, instead of in a methods file. Not given together with `--rpc-methods`.",
			OneOf: []string{"methods", "proto"}, Fields: []Field{
				{Name: "methods", Doc: "One entry for each RPC method the gateway routes, as a build writes them from the gRPC descriptors.",
					Kind: List, Item: &methodField, Rule: "It may be empty. Which service a method goes to is decided by the rpc entry of `routes`."},
				protoField,
			}},
		{Name: "log", Doc: "Which requests the request log writes.", Kind: Map, Rule: "Without `log`, every request is logged.", Fields: []Field{
			{Name: "status_from", Doc: "The lowest status that is always logged.", Kind: Int, Required: true, Min: 100, Max: 599},
			{Name: "sample", Doc: "The share of the other requests that is logged.", Kind: Text, Required: true,
				Pattern: model.PercentPattern, Expect: "a whole percentage such as 1%"},
		}},
		tracingField, envoyField, reserved("nginx", "nginx"), reserved("haproxy", "HAProxy"),
	},
}

Proxyfile describes the file proxyfile reads: every key, its kind and its rule. The meaning rules that need more than one key, such as a route naming a declared service, belong to the reader.

View Source
var RateLimit = Field{Name: "rate_limit", Doc: "How many requests the route admits in a period; a request over the limit is answered 429 and never reaches the service.",
	Kind: Map, Fields: []Field{
		{Name: "requests", Doc: "Requests admitted per period.", Kind: Int, Required: true, Min: 1, Max: largestUint32},
		{Name: "per", Doc: "The period.", Kind: Text, Required: true, Pattern: model.TimePattern, Expect: "a time such as 1s or 1m", Rule: "At least `1s`."},
		{Name: "burst", Doc: "Extra requests admitted above requests after a quiet spell; 0 when not written.", Kind: Int, Min: 0, Max: largestUint32,
			Rule: "`requests` and `burst` together at most 4294967295."},
		{Name: "by", Doc: "client counts each client address on its own, all every client together; client when not written.", Kind: Enum,
			Values: []string{"client", "all"}, Rule: "The client's address is the one `client_address` names."},
	}}

RateLimit is the rate_limit key of a route and of the rpc entry. It is exported because the reader quotes its Expect for a time the parser refuses, and cannot walk the routes' Either to reach it.

Functions

func Check

func Check(spec Field, n *tree.Node) []model.Problem

Check returns every problem of n against spec, sorted, so one run names them all.

func NoGenerator

func NoGenerator(backend string) string

NoGenerator says why a target and a key of its block are refused: proxyfile has no generator for that proxy yet.

func Reference

func Reference(f Field, path, class string) []byte

Reference writes, as a Markdown table, the key f at path and every key under it, depth first in the description's order, so the documentation lists the keys, kinds and rules the checker reads. With path "", f is the root of a file, which has no row. A row gives the key's path, its Doc, its rule: Required when it is, what the description says the value must be, then the key's own Rule; and its class: the Class it names, else its holder's, and class for f, which is the class of the key that holds f. One description always gives the same bytes.

func Schema

func Schema(spec Field) ([]byte, error)

Schema writes spec as a JSON Schema (draft 2020-12), so an editor checks a file with the keys, kinds and patterns the checker uses. Every schema object is a Go map, which encoding/json writes with its keys sorted, and every list keeps the description's order, so one description always gives the same bytes.

Types

type Field

type Field struct {
	Name string // the key; "" for the root, list items and dictionary values
	Doc  string // one sentence, the schema's description; on a Key, what a key means, which the schema has no place for
	// Rule completes the key's row in the documentation's key tables (Reference) with what the other fields cannot
	// say, such as a rule the reader checks across keys or a default of Envoy's. It is Markdown, and the schema
	// leaves it out.
	Rule     string
	Kind     Kind
	Required bool
	Fields   []Field  // Map
	OneOf    []string // Map: exactly one of these keys
	Item     *Field   // List items; Dict values
	Key      *Field   // Dict: the keys the writer chooses (Name says what they are, Pattern and Expect their rule)
	// AtLeastOneOf (Map) names keys of which one or more must be written, and Needs (Map) names, by key, the keys that
	// must be written beside it. The schema writes them as anyOf and dependentRequired, so an editor refuses what the
	// reader refuses. The checker does not read them, because the reader checks the same rules with messages of its
	// own: for auth, in internal/read/decode.go (the decoder's auth method).
	AtLeastOneOf []string
	Needs        map[string][]string
	// Pattern (Text and Key) runs in two engines: Go's RE2 in the checker, and an editor's ECMAScript engine through
	// the JSON Schema, which writes it unchanged. So it keeps to what both read alike. The package fails to load on a
	// form Go refuses, such as a lookahead, an atomic group or a possessive repeat. Of the forms Go accepts,
	// ECMAScript refuses a flag set for the rest of the pattern, such as (?i), and (?P<name>; it reads \A and \z as
	// letters or refuses them; and \s, \S and a . outside brackets match other characters in it: write Go's \S out as
	// [^\t\n\f\r ]. [\s\S], every character, is the same in both.
	Pattern  string
	Expect   string   // Text, Key, Either: completes "must be ..."
	Min, Max int64    // Int; Max 0 is no maximum. int64, not int, so a uint32 limit fits on a 32-bit build too
	Values   []string // Enum
	Choices  []Field  // Either: a Text choice, and Map, Dict or List choices
	Select   string   // a Map choice of an Either: chosen when the node has this key; the Map or Dict choice without Select is the default
	// Moved names, by key, the keys of a Map that proxyfile 1.x read here and 2.0 reads at another place, with that place,
	// so the check names it instead of calling the key unknown.
	Moved map[string]string
	// Reserved marks the block of a proxy proxyfile has no generator for yet (Map): it is accepted empty, and every key
	// written in it is refused with NoGenerator.
	Reserved bool
	// Class is the proxy whose generator alone reads the key and every key under it that names no class of its own:
	// envoy, nginx or haproxy. A key that names none, held by none that does, is core: every proxy can carry it
	// (docs/portability.md).
	Class string
}

Field describes one key of a file format. The checker reads it, and `proxyfile schema` prints it as JSON Schema, so the keys an editor offers are the keys the program accepts.

func (*Field) Child

func (f *Field) Child(name string) *Field

Child returns the description of the key name of a Map, or nil. A later stage that refuses a value the check accepted quotes the child's Expect, so both stages describe a value in the same words.

type Kind

type Kind int

Kind is what a key's value must be. Map has the keys its Fields name and Dict the keys its writer chooses; Either is whichever of its Choices fits the value.

const (
	Map Kind = iota + 1
	Dict
	List
	Text
	Int
	Bool
	True // a key that takes only true; the schema says const true, so an editor offers no false
	Enum
	Either
)

Jump to

Keyboard shortcuts

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