SaltDns.Proxy.HaproxyConfig (Salt v1.0.0)

Renders the HAProxy 3.4 configuration for one proxy node. Pure.

The generated file is the whole haproxy.cfg written by Shaker: a global section with the admin socket, a named defaults salt_http section, the plain-HTTP redirect frontend, the TLS frontend with its two map lookups and one backend per state backend. The names are the fixed names of the Salt/Shaker contract so runtime socket commands and a restart agree.

One name deviates from the contract deliberately: the plain-HTTP frontend is salt_http_redirect, not salt_http. HAProxy 3.3 and later refuse a proxy whose name equals a defaults section ("This is no longer supported as of 3.3"), and the defaults section must keep the name salt_http because the runtime command add backend <name> from salt_http refers to it.

Every value placed in the file is validated first. Names must match the route/backend grammar, addresses must be IPv4 literals, IPv6 literals or host names, ports must be 1..65535, paths must be absolute and contain only safe characters, and timeouts must be a positive number with an optional time unit. A newline, space, # or quote in a name is therefore impossible: nothing from the input is interpolated unchecked.

The frontends listen on IPv4 and IPv6 with one bind :::<port> v4v6 line each. v4v6 makes the IPv6 wildcard socket accept IPv4 too, and HAProxy falls back to plain IPv4 when the host has no IPv6 socket support, so the same file starts on hosts with and without IPv6.

With http01_thumbprint the plain-HTTP frontend answers the HTTP-01 challenges of one ACME account itself, before the redirect: see docs/shaker.md. Without it the file is exactly what it always was.

A request whose host is in neither map goes to the fixed backend salt_not_found (404) unless the state names a default backend. That name cannot be used for a state backend: {:reserved_backend_name, name}.

Rendering is deterministic: backends and servers are sorted by name, and the same input always produces byte-identical content.

Summary

Types

One backend in the generated configuration.

The rendered file and the hash Shaker reports as config_sha256.

One backend server in the generated configuration.

Functions

Renders the configuration.

Types

backend()

@type backend() :: %{
  name: String.t(),
  mode: String.t(),
  servers: [server()],
  local: boolean()
}

One backend in the generated configuration.

rendered()

@type rendered() :: %{content: String.t(), sha256: String.t()}

The rendered file and the hash Shaker reports as config_sha256.

server()

@type server() :: %{
  name: String.t(),
  address: String.t(),
  port: 1..65535,
  ipv6: boolean(),
  check: boolean()
}

One backend server in the generated configuration.

Functions

render(input)

@spec render(term()) :: {:ok, rendered()} | {:error, term()}

Renders the configuration.

Accepted input keys (atom or string keys):

  • version_line - only "3.4" (default "3.4")
  • paths - admin_socket, crt_list, hosts_map, hosts_wildcard_map
  • socket_group - the group of the admin socket (default "haproxy-certs"). A name renders group <name>; a number (an integer or its decimal string, as a node reports a gid) renders gid <n>
  • drop_privileges - true adds user and group to global so HAProxy drops root (default false, for a container that already runs as a non-root user); run_user and run_group name them (default "haproxy")
  • backends - %{name, mode: "http", servers: [%{name, address, port, check}]}; with local: true the backend is defined in the operator's own file, has no servers and is not rendered (routes and default_backend may name it)
  • default_backend - a backend name or nil
  • maxconn - a positive integer (default 4096)
  • http01_thumbprint - the RFC 7638 thumbprint of the ACME account key (43 base64url characters) or nil (default). A thumbprint adds one http-request return rule to the plain-HTTP frontend, ahead of the redirect
  • timeouts - connect, client, server, http_request (defaults 5s, 10m, 10m, 10s: the long client and server timeouts keep a large upload alive while the backend is slow to answer, the short connect and request-header timeouts still cut slow openers; Proxy.State.build/1 passes its own timeouts key through)

Returns {:ok, %{content: content, sha256: lowercase_hex}} or the first validation error. Errors include {:unsupported_version, value}, {:invalid_path, key}, {:invalid_backend_name, value}, {:invalid_server_name, value}, {:invalid_address, value}, {:invalid_port, value}, {:duplicate_backend, name}, {:duplicate_server, name}, {:local_backend_with_servers, name}, {:reserved_backend_name, name} and {:unknown_backend, name} (for an unknown default_backend).