SaltDns.Proxy.Routes (Salt v1.0.0)

Validates and matches the host routes of a proxy node. Pure.

A route sends one host name to one backend. Hosts are lower-case ASCII host names (A-labels): a wildcard is the form *.example.org, where the * is the whole first label. validate_host/1 also normalises: surrounding whitespace is trimmed, the name is lower-cased and one trailing dot is removed. Unicode names are refused with :not_ascii; the caller converts them to A-labels first.

split/1 turns routes into the two HAProxy map files. Exact names keep their host as the key. A wildcard route becomes the key .example.org (the * replaced by an empty label), which HAProxy's map_end looks up by suffix:

a.example.org      matches the key .example.org
a.b.example.org    matches the key .example.org
example.org        does NOT match the key .example.org

Because map_end is a plain string-suffix match, a deeper name matches the wildcard too; match/2 reproduces that, so a console preview shows what HAProxy will actually do.

Summary

Types

A backend name that HAProxy can use in its configuration.

A normalised host name, either exact or starting with *..

Functions

Renders map-file text: key value per line, sorted, with a trailing newline. An empty list gives an empty string.

The backend HAProxy would choose for host, or nil.

Splits routes into exact and wildcard entries, both sorted, and refuses a host that points at two different backends.

Validates one backend name: ^[a-z][a-z0-9_-]{0,62}$.

Validates and normalises one host name.

Types

backend()

@type backend() :: String.t()

A backend name that HAProxy can use in its configuration.

host()

@type host() :: String.t()

A normalised host name, either exact or starting with *..

route()

@type route() :: %{host: String.t(), backend: String.t()}

split_result()

@type split_result() :: {[{host(), backend()}], [{String.t(), backend()}]}

Functions

map_file(entries)

@spec map_file([{String.t(), String.t()}]) :: String.t()

Renders map-file text: key value per line, sorted, with a trailing newline. An empty list gives an empty string.

match(host, routes)

@spec match(term(), term()) :: backend() | nil

The backend HAProxy would choose for host, or nil.

Matching follows the generated configuration: exact names first, then the longest wildcard key that is a suffix of the host (map_end). So a.example.org and a.b.example.org both match *.example.org, while example.org does not.

The host is lower-cased and one :port part is dropped before matching, as req.hdr(host),field(1,:),lower does in HAProxy. Unlike validate_host/1, a trailing dot is not removed: HAProxy does not remove it either, so example.org. matches neither form. An invalid input or invalid route list returns nil.

split(routes)

@spec split(term()) :: {:ok, split_result()} | {:error, term()}

Splits routes into exact and wildcard entries, both sorted, and refuses a host that points at two different backends.

Each route is a map with :host and :backend (string keys are accepted too). Exact entries are {host, backend}; wildcard routes become {".example.org", backend} for map_end. A duplicate host with the same backend is collapsed; a duplicate with a different backend returns {:error, {:conflict, host}}.

Hosts and backend names are validated here as well, so an invalid route never reaches a map file. Errors are {:error, {:conflict, host}} or the validation error from validate_host/1 / validate_backend_name/1.

validate_backend_name(name)

@spec validate_backend_name(term()) :: {:ok, backend()} | {:error, atom()}

Validates one backend name: ^[a-z][a-z0-9_-]{0,62}$.

No normalisation is applied, so a name with a space, a newline, a quote or upper-case letters is refused rather than repaired.

validate_host(host)

@spec validate_host(term()) :: {:ok, host()} | {:error, atom()}

Validates and normalises one host name.

Returns {:ok, host} with the trimmed, lower-cased name and at most one trailing dot removed, or {:error, reason} where reason is one of :empty, :not_ascii, :too_long, :invalid_label, :invalid_wildcard or :invalid_host (the input was not a binary).