Configuration loading and validation
Configuration loading turns a YAML policy file into the typed settings that Anubis uses for request rules, thresholds, data stores, logs, metrics, GeoIP data, and honeypots. It reads local or built-in policy sources, applies safe defaults, expands imported rules, and rejects invalid settings before the server starts.
Loading lifecycle
The loading lifecycle separates file parsing from system setup. lib/…/config.go first decodes the file into a file-shaped model, validates its values, then returns a typed Config; lib/…/policy.go converts that model into live stores, checkers, loggers, GeoIP data, and threshold programs.
The main stages are:
lib/config.Loadreads YAML from the supplied reader. An explicit policy path is used when present; otherwiselib.LoadPoliciesOrDefaultopens the built-in policy.- File-level validation checks required rule data and each configured subsystem. Errors are collected so one load can report several bad settings.
- Imports are opened and flattened into ordinary bot rules. Imported files can come from the local file system or the embedded
(data)/policy tree. - Missing thresholds use the default threshold that challenges requests with a positive weight.
policy.ParseConfigcreates the runtime resources and performs checks that need registered challenge methods, extensions, stores, or external data.
This diagram shows how configuration moves from YAML to the running policy.
flowchart LR
A[YAML policy] --> B[Decode and apply defaults]
B --> C[Validate typed settings]
C --> D[Expand imports]
D --> E[Build runtime policy]
E --> F[Create store, logger, GeoIP, metrics, and checkers]
F --> G[Serve requests]
References: lib/…/config.go• L308-481 lib/…/import.go• L86-155 lib/config.go• L64-113 lib/…/policy.go• L80-148
Rule and threshold configuration
Bot rules describe request signals and the action or weight change that follows. A rule needs a name, one or more supported match inputs, and a known action; its regular expressions, CIDRs, remote address URL, challenge settings, and expressions must also be valid.
Rules can use a direct expression string or an all or any list. The loader turns a list into a combined expression, while the policy layer compiles it for request checks. ALLOW, DENY, CHALLENGE, WEIGH, and DEBUG_BENCHMARK are the supported rule actions.
Thresholds convert the accumulated request weight into a final decision. Each threshold needs a name and expression. A CHALLENGE threshold must include challenge settings, and thresholds cannot use WEIGH; when the file has no thresholds, the loader supplies the legacy positive-weight challenge behavior.
References: lib/…/config.go• L35-53 lib/…/config.go• L57-201 lib/…/expressionorlist.go• L17-129 lib/…/threshold.go• L10-78 lib/…/thresholds.go• L9-31 lib/…/policy.go• L150-289
Import resolution
Imports let a policy reuse rule sets without copying them into the main file. An import points to a local path or to an embedded (data)/ path; a glob can select several files, and matches are sorted in natural order before their rules are combined.
Imported files use the same bot-rule shape as the parent file and can contain further imports. The loader rejects missing files, empty glob matches, unsupported recursive or brace glob patterns, parse errors, and invalid rules. A rule entry cannot define both a bot and an import.
References: lib/…/import.go• L20-31 lib/…/import.go• L33-83 lib/…/import.go• L86-155 data/…/default-config.yaml• L1-33
Typed operational settings
The typed configuration model gives each operational area a clear contract. Optional blocks are validated only when present, while the loader supplies defaults for the in-memory store, standard logging, honeypot behavior, status codes, and DNS cache TTLs.
| Area | Purpose | Important validation or default |
|---|---|---|
| Store | Keeps policy-related runtime state | The backend must be registered, and its parameter object must pass backend validation. The default backend is memory. |
| Logging | Selects standard output or file logging | File logging needs a file name and non-negative rotation limits. A log level in the file takes precedence over the flag level. |
| Metrics | Exposes operational metrics | The bind address and network are required. TCP networks or Unix sockets are supported; Unix sockets also need an octal mode. |
| Honeypot | Enables an extra trap flow for suspicious clients | The only accepted enabled implementation is naive. Disabled honeypots do not need an implementation. |
| DNS | Controls forward and reverse lookup cache time | Forward and reverse TTLs cannot be negative. The default for each is 300 seconds. |
| Status codes | Sets responses for challenge and deny actions | Both values must be HTTP status codes from 100 through 599. |
References: lib/…/config.go• L308-425 lib/…/store.go• L12-43 lib/…/logging.go• L16-57 lib/…/logging.go• L59-124 lib/…/metrics.go• L30-80 lib/…/honeypot.go• L10-44 lib/config.go• L484-520
Metrics access controls
Metrics can use TLS and basic authentication in addition to a network listener. TLS validation checks that the certificate and key files can be read, that the key pair is valid, and that an optional CA file contains a certificate; basic authentication requires both a user name and a password.
References: lib/…/metrics.go• L83-169
GeoIP settings
GeoIP settings connect bot rules to MaxMind ASN and country data. The top-level GeoIP block must define at least one database, and each database needs a path; country databases may select country or registered_country, while ASN databases cannot set a country field.
Automatic updates use a MaxMind account ID and the name of an environment variable that holds the license key. The update interval defaults to 24 hours and cannot be shorter than one hour. An override endpoint must be an absolute HTTP or HTTPS URL.
Country codes in bot rules must contain two letters and are normalized to lower case. ASN rules reject private-use ASNs. If a rule needs GeoIP data that is not configured, policy loading warns and does not add that check; if the configured database itself cannot load, policy parsing fails.
References: lib/…/geoip.go• L10-35 lib/…/asn.go• L8-43 lib/…/geoip_databases.go• L11-95 lib/…/geoip_databases.go• L107-218 lib/…/policy.go• L116-145 lib/…/policy.go• L217-233
Configuration failure boundaries
Configuration errors appear at two boundaries. File-level errors cover YAML shape, required values, ranges, regular expressions, paths, and relationships between fields; runtime validation covers resources that are only known after parsing, such as registered stores, challenge algorithms, extensions, GeoIP databases, and compiled expressions.
The loader returns an error instead of a partial policy when validation fails. Some missing optional runtime data produces a warning and removes only the dependent rule check, so operators must treat warnings about absent GeoIP data or ASN logging as behavior changes rather than parse failures.
References: lib/…/config.go• L323-384 lib/…/config.go• L434-481 lib/config.go• L88-110 lib/…/policy.go• L116-145 lib/…/policy.go• L264-298
Change checklist
When these files or sections are changed, remember to consider these:
- Keep file-level defaults aligned with the runtime consumers that read the typed
Config. - When adding a challenge algorithm or extension, update the final validation path so valid names are also registered at runtime.
- When changing an import path or glob rule, preserve support for both local files and embedded
(data)/policies. - When changing GeoIP fields, check both database validation and bot-rule behavior when the matching database is absent.
- When changing store parameters, keep the config validator and the selected store factory's parameter contract aligned.
- When changing logging or metrics file options, preserve the early readability and format checks that prevent startup failures.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| An import path or glob matches no file | The policy cannot load | Use an existing local path or embedded (data)/ path. Do not use recursive ** or brace alternation. |
A file rule sets both user_agent_regex and path_regex | The rule is rejected | Choose one of these two primary match forms. |
| File logging lacks parameters or a file name | Logging setup cannot start | Set a valid file target and rotation settings when sink is file. |
| Metrics TLS files are missing or do not form a key pair | Metrics setup is rejected | Check certificate, key, and optional CA files before startup. |
| GeoIP auto-update names an unset license-key variable | GeoIP validation fails | Set the named environment variable before starting the service. |
| A rule uses GeoIP or ASN matching without its database | That match check is skipped and a warning is logged | Configure the matching GeoIP database or remove the dependent rule. |
| A configured challenge algorithm or extension is not registered | Final policy validation fails | Use names provided by the active challenge and extension registries. |
Key files
| File | Why to read it |
|---|---|
| lib/…/config.go | Defines the file schema, load order, defaults, and top-level validation. |
| lib/…/import.go | Explains local, embedded, and glob-based rule imports. |
| lib/…/policy.go | Shows the boundary between typed settings and live runtime resources. |
| lib/…/store.go | Defines store backend selection and parameter validation. |
| lib/…/logging.go | Defines logging sinks and file rotation settings. |
| lib/…/metrics.go | Defines metrics listeners, TLS, and basic authentication checks. |
| lib/…/geoip_databases.go | Defines GeoIP database and automatic update requirements. |
| lib/…/threshold.go | Defines threshold defaults and final decision rules. |
| data/…/default-config.yaml | Shows how the built-in policy combines imports with direct rules. |