Skip to main content
Version: 4.1

Lua Cookbook

The Lua Cookbook is a set of small, complete Nauthilus installations. Each recipe solves one everyday task of a mail or identity administrator with a few lines of Lua: a user list in a file, a maintenance switch, an IP blocklist, a login journal, and more. Every recipe

  • starts with one docker compose up,
  • contains every file it needs, with comments in the files themselves,
  • ships a test.sh that proves the recipe works, and
  • is explained file by file on its page.

The recipes are tested against the released Nauthilus image and, by the Nauthilus maintainers, against every new build with the Lua JIT switched on and off. When a recipe page says "this works", a test has checked it.

What you need​

  • Docker with the Compose plugin (docker compose version)
  • curl and bash for the test scripts
  • openssl for the recipes that create key material

Nothing is installed on your machine. Every stack listens only on 127.0.0.1.

The recipes​

RecipeEveryday taskLua interfacesPort
Flat-file usersKeep accounts in a text file instead of a directory serverinit script, Lua backend, cache-flush callback28081
SQL usersAuthenticate against an existing MariaDB/MySQL account table, including TOTP enrolmentLua backend with account listing and MFA callbacks28082
Admin toolkitSwitch on a maintenance mode and block networks through a small HTTP APIcustom hooks, environment sources, policy registry28083
Account rulesAllow protocols per account, lock accounts during offboardingsubject sources, policy registry28084
Login journalAnswer "when did this user last log in, and from where?" and mail the admin on brute-force blocksPOST-action, brute-force action28085
Country rules per accountLet some accounts log in only from the countries of their regionnative Go plugin (GeoIP) together with a Lua environment source28087
Learning IP reputationLearn which addresses misbehave, protect privileged accounts from them, and override per address or networknative Go plugin (reputation) together with a Lua environment source28088
Policy APILet a VPN gateway ask "may this user connect now?"generic policy provider (kind: lua)28086

Every recipe page has the same structure: what the recipe does, how to start and test it, the configuration, every Lua script with an explanation, and ideas for your own changes. Read Flat-file users first if Nauthilus is new to you; it explains the parts that all recipes share.

Lua interfaces at a glance​

Nauthilus calls Lua at many points. Each point expects a script with a fixed entry function:

InterfaceEntry functionRunsShown in
Init scriptnauthilus_run_hook(request)once when the process startsFlat-file users
Lua backendnauthilus_backend_verify_password(request), nauthilus_backend_list_accounts()for every password check that is not answered from the cacheFlat-file users, SQL users
Backend MFA callbacksnauthilus_backend_add_totp(request) and friendswhen TOTP or recovery codes are enrolled or removedSQL users
Cache-flush callbacknauthilus_cache_flush(request)for DELETE /api/v1/cache/flushFlat-file users
Environment sourcenauthilus_call_environment(request)before the password is checkedAdmin toolkit
Subject sourcenauthilus_call_subject(request)after the password is checkedAccount rules
Actionnauthilus_call_action(request)when a policy rule selects it, while the request waitsLogin journal
POST-actionnauthilus_call_action(request)after the answer was sentLogin journal
Custom hooknauthilus_run_hook(request)for /api/v1/custom/<location>Admin toolkit
Policy registry scripttop-level nauthilus_policy.register_attribute(...)when the policy snapshot is builtAdmin toolkit, Account rules
Generic policy provider_G["policy.facts.collect"], _G["policy.effects.execute"]for the Policy decision APIPolicy API

Native Go plugins are the other way to extend Nauthilus. They deliver facts and effects to the same policy as Lua scripts; Country rules per account combines the bundled GeoIP plugin with a Lua environment source, and Learning IP reputation does the same with the bundled reputation plugin.

The Lua API reference describes every module. Lua runtimes and capabilities explains which modules each interface can use.

Run a recipe​

Download a recipe as an archive from its page, or take the files from the website repository. An archive unpacks into cookbook/<recipe>/ together with the shared test helper cookbook/lib/cookbook.sh:

curl -fsSLO https://nauthilus.org/cookbook/flatfile-users.tar.gz
tar -xzf flatfile-users.tar.gz
cd cookbook/flatfile-users
docker compose up -d --wait
./test.sh

test.sh prints one line per check and a summary:

Lua backend: password checks
ok alice logs in (bcrypt hash)
ok bob logs in (SHA512-crypt hash)
...
16 checks passed, 0 failed, 0 known issues

A line that starts with known marks a check that is held back by a reported Nauthilus bug. It does not fail the run, and it turns into an ok line as soon as a release fixes the bug.

Stop and remove a stack, including its data, with:

docker compose down -v

Choose the Nauthilus image​

The compose files use the released image ghcr.io/croessner/nauthilus:v4.2.0. The recipes need Nauthilus 4.1.3 or later. Two environment variables change the image:

VariableDefaultMeaning
NAUTHILUS_IMAGEghcr.io/croessner/nauthilus:v4.2.0Image to start, for example a local build
NAUTHILUS_LUA_JITtrueSwitches the Lua JIT compiler of Nauthilus 4.2 and later on or off; earlier releases ignore it
NAUTHILUS_IMAGE=nauthilus:dev NAUTHILUS_LUA_JIT=false docker compose up -d --wait

run-all.sh in the cookbook directory starts, tests, and removes every recipe one after the other. The Nauthilus maintainers run it for every release with the JIT on and off:

NAUTHILUS_IMAGE=nauthilus:dev ./run-all.sh
NAUTHILUS_IMAGE=nauthilus:dev NAUTHILUS_LUA_JIT=false ./run-all.sh

Rules the recipes follow​

  • Secrets are demo values. Passwords, nonces, and keys in the files are public. Replace every one before you copy a recipe into a real installation.
  • Callers get scoped credentials. The mail server credential may only check logins; the administrator credential holds only the scopes it needs. See Backchannel Basic credentials.
  • Lua emits facts, the policy decides. Scripts that should stop a login emit a registered policy attribute, and a rule in nauthilus.yml turns it into deny or tempfail. The decision stays visible in the configuration.
  • Shared code lives in modules. When two scripts need the same logic, it lives in one module that both load with require. package_path in nauthilus.yml tells Nauthilus where to find it.

Problems these recipes found​

Building the cookbook found several Nauthilus defects. All of them are fixed in Nauthilus 4.1.3 and 4.2.0, which is why the recipes need 4.1.3 or later. See the 4.1.3 release notes:

  • A Lua environment source that returned ENVIRONMENT_TRIGGER_YES did not reject the login (security fix).
  • A tempfail rule answered with the generic temporary-failure text instead of its response_message.
  • A rule whose response_message used from: attribute_detail stopped Nauthilus from starting, although --config-check accepted the configuration.
  • Configured rules at subject_analysis did not see the attributes of subject sources.
  • A custom hook that set the HTTP status and returned a Lua table sent an empty body.
  • nauthilus_backend_add_totp did not receive request.totp_secret.
  • mode=list-accounts with Accept: text/plain returned only one account.

When a future release breaks a recipe, its test prints a failing check, and a check that waits for a reported fix is printed as known.

  • mode=list-accounts with Accept: text/plain returns only one account; use application/json.