Skip to content

Architecture

This page is a map of the code for contributors: where things live and which rules hold everywhere. It describes only what changes rarely; the details are in each package’s documentation.

Overview

Lemano runs on an operator’s workstation. For each target server it opens one SSH connection, sends a POSIX shell script on standard input, and reads back a line-based stream of records. The engine turns those records into a discovery document, compares it with the latest generation in the local store, and writes a new generation only when something that counts has changed. Every generation is canonicalised and hashed, and each one includes its parent’s hash, so the whole history can be verified.

What a pass reads is defined by kinds (Package, File, Sysctl, …). A kind is a folder under kinds/: a declaration (kind.yaml, checked against schemas/kind.json), a JSON Schema of its fields (schema.json) and a read step in POSIX sh (read.sh). The kinds are embedded in the binary and checked when the engine starts. Kinds lists the fourteen that exist and what each reads.

A pass runs three scripts on the same connection, and two more when it needs them:

  1. Package files. The files the installed packages own (dpkg’s file lists, rpm’s database) with their size, times, inode, mode and owner, never their content. The engine compares them with the table the previous pass left in the store (package_files, sealed, the latest only) and passes the File kind the files to check against the package database. The package manager’s full check, which reads every package file, runs instead on a target’s first pass, once per spec.discovery.packageCheckInterval and on --full-package-check, never in a pass around another tool’s run or a revert (internal/engine/pkgfiles.go).
  2. Inventory. The facts step, then each kind’s read step in a fixed order, each in a shell of its own under the time limit per step, with the helper library (kinds/helpers.sh) in front. Files are reported with their metadata and a plain SHA-256, never their content.
  3. Data, on the RHEL family only: dnf’s history database and its write-ahead log, which the engine reads in memory for the manual field of Package and never stores.
  4. Content. The engine compares each file’s hash with the content the store already holds and fetches only the rest, in 48 KiB chunks. A host that did not change sends no content.
  5. Evidence, only when the pass found changes: the host’s package logs, sudo’s and sshd’s journal messages and auditd’s records (when its rules are already in place) since the previous pass, from which the engine attributes each change (internal/attribution). Command lines are kept only as keyed hashes.

The engine then checks each object against its kind’s schema, replaces the fields the kind marks sensitive by their keyed hashes, stores file content as encrypted blobs, and writes one object record per resource, without the fields the kind marks volatile. A generation maps each Kind/identity to the hash of its record.

The command-line tool and the engine are separate parts. The CLI builds requests, sends them through a client, and prints the responses. Today the client calls the engine in the same process; a network client can replace it later without changing the CLI.

Codemap

PathWhat it holds
cmd/lemanoThe main package. It only starts the CLI.
cmd/lemano-ansibleThe main package of the Ansible integration, lemano-ansible.
cmd/gendocsWrites the command reference pages under docs/reference/cli/ from the command tree; make docs runs it and CI runs it with -check.
internal/cliThe commands, one file per command, built with cobra; output formats and exit codes. Imports only api/v1alpha1 and client.
api/v1alpha1Request and response types and typed errors: the boundary between the CLI and the engine.
clientThe client the CLI uses to reach the engine, today in the same process. It finds the store and the key, and loads target files.
internal/engineOrchestration: discovery passes (lock, connect, inventory, data, content and evidence scripts, discovery document, object records, decision, attribution, record), targets, history and blind windows (measured against each target’s interval), diff (objects and their fields), baselines, verify, summaries of a period (generations, changes grouped by cause, why a target was not read), and the objects of a generation with their conditions and notes; revert (the plan against a fresh pass, the write step’s records and answers, the trial’s fresh login, confirmation and outcome).
internal/storeThe SQLite store: schema and migrations, generations and the hash chain, objects, encrypted blobs, passes, baselines, the locks table, encrypted secrets.
internal/lockThe lock per target: one writer at a time, stale-lock takeover, forced unlock. Backed by the store’s locks table.
internal/canonicalCanonical JSON (RFC 8785) and SHA-256 digests.
internal/cryptoKey providers, sealed store keys, blob encryption, keyed hashes.
internal/connectorThe connector contract: how the engine reaches a target.
internal/connector/sshThe SSH connector: sessions, host key pinning, jump hosts, proxy commands, identities, timeouts, sudo.
internal/clidocWrites one Markdown reference page per command from the command tree.
internal/exitcodeThe exit codes of lemano and of the programs installed beside it, and their mapping to the reasons of the API.
internal/writestepThe scripts that write on a host for a revert: writing a file, validating it, the answers it records, and the trial’s undo. Sent only with a revert’s write step, never with a pass.
internal/termReads a password on the terminal without echo, and tells whether a file is a terminal.
internal/connector/sshconfigResolves the settings a target file leaves out from the user’s ssh configuration with ssh -G.
internal/connector/fakeAn in-memory connector that replays recorded output, for tests.
internal/wireThe parser for the record stream that discovery scripts emit.
internal/discoveryThe scripts a pass sends (the frame, the facts step, the evidence step, the assembly of the inventory, data, content and evidence scripts) and the builder of the discovery document.
internal/kindsLoading and checking the kinds: declarations, schemas, reading order; typing, validating and splitting an object’s fields; evaluating the conditions and notes a kind declares.
internal/condThe expression language of conditions and notes in kind.yaml: compiling and checking an expression, evaluating it on an object’s status with a cost limit, and rendering the text of a note. The only package that imports the CEL library.
internal/dnfhistoryReading dnf’s history database from a copy of its bytes: replaying its write-ahead log, opening it in memory, each package’s reason and the transactions of a period.
internal/attributionParsing the host’s logs (apt, dpkg, dnf, the journal’s sudo and sshd messages, auditd) and attributing a pass’s changes from them, with a confidence.
internal/filesThe rules for File objects: the shipped exclusion list, a target’s exclusions and content exclusions, parked package copies, the content size limit.
internal/linediffComparing two texts line by line and writing the differences as unified hunks, with a bound on the search and on the length of the output.
kindsThe built-in kinds, one folder each, and the shell helper library, embedded for the engine, under Apache-2.0.
internal/targetsLoading and validating target files; derived labels and selectors; the jump host syntax and the first line of generated files.
internal/siblingFinding and running the programs installed beside lemano (lemano-NAME, only from the folder of the running executable), and the credential helper protocol that asks them for passwords.
internal/encodeWriting a value as JSON or YAML for -o, shared by lemano and the programs installed beside it.
internal/integrationsOne folder for each integration, a program of its own that the core never imports (a lint rule enforces it). ansible imports an Ansible inventory as target files and answers the credential helper protocol for ansible:// password references, and records playbook runs for the callback plugin of integrations/ansible/collection (the Ansible collection lemano.lemano, GPL-3.0-or-later); lemano ansible discover, revert and end-trial build target files from an inventory in a private folder and run the core’s own command on them; lemano ansible check scans a playbook with Ansible’s own loader (scan.py, embedded, run with the Python of ansible-playbook), runs it in check mode with the callback plugin and compares its tasks’ results with the hosts’ latest generations, through the client API only; lemano ansible export writes the hosts’ latest generations as an Ansible project, reading file content through the client’s neutral GetContents; lemano ansible codify runs the check, then edits the user’s roles in place with a YAML editor that changes only the text of a value (yamledit.go) and writes a patch or the files; its command line is in ansible/command, built as cmd/lemano-ansible.
internal/testutilHelpers shared by tests: recorded identity streams and a connector that replays them. Never imported by production code.
schemasJSON Schemas for the file formats, embedded for validation, under Apache-2.0.
labThe test hosts: container images and a Compose file, scripts that start and stop them and write their target files, cloud-init data for a VM, and the script that reads the kinds that need a VM (needsVM in their kind.yaml) on it.
test/integrationIntegration tests of the engine’s packages against the lab, behind the integration build tag.
test/e2eEnd-to-end tests of the built lemano binary against the lab: exit codes, -o json output and the files it writes. Behind the e2e build tag.
skillsThe agent skill (skills/lemano), under Apache-2.0.
scriptsRepository scripts, such as the check of the licence headers (make headers).
docsUser and contributor documentation.
siteThe website, built with Hugo: the landing, install, community, security and blog pages, and the pages of docs/, read from docs/ at build time. A module of its own (site/go.mod) that holds only the theme.

Core and integrations

The core, the lemano binary, knows no other tool by name. A tool that lemano works with is an integration: a program of its own, built and released with lemano, installed in the same folder and reached through two small protocols that any program can implement.

                       lemano (core)
   +----------------------------------------------------+
   |  cli -> client -> engine -> connector / store      |
   |                                                    |
   |  internal/sibling                                  |
   |    lemano NAME ...   runs lemano-NAME   (1)        |
   |    ref SCHEME://...  asks lemano-SCHEME (2)        |
   +----------------------------+-----------------------+
                                |  only the folder of the
                                |  running lemano, never PATH
   +----------------------------v-----------------------+
   |  lemano-NAME  (integrations/NAME)                  |
   |  in the same module, it may use the loader of      |
   |  target files; nothing in the core uses it         |
   +----------------------------------------------------+

 (1) the command line: arguments, standard streams and the exit code
     pass through unchanged
 (2) the credential helper protocol: JSON on standard input and output,
     see docs/reference/lemano/credential-helpers.md

The rules that keep it so:

  • No core package imports internal/integrations (the depguard rule integrations-boundary in .golangci.yml). The core has no list of integration names: whether lemano NAME exists is looked at only when it is asked for.
  • A program is found only in the folder of the running lemano, with symbolic links resolved, and never on PATH: a program on PATH would run with the user’s rights whoever put it there.
  • A password reference with a scheme the core does not know (SCHEME://...) is stored and passed on as written. The core checks that it is well formed and nothing more; the helper lemano-SCHEME reads it.
  • A generated target file starts with # Generated by "lemano NAME COMMAND"., and labels of the form NAME.lemano.X are reserved for such files. The core knows the shape, not the names.
  • An integration that records a run of its tool (a playbook run, say) uses the client API: DiscoverRequest.Run names an outside run, a pass before it and a pass after it with hints of what it changed. The core stores the tool’s name, the run’s ID, title and metadata as given, and never interprets them, and the run names the noun of its units of work (unit, “task” for Ansible), so the core’s wording never names a tool’s concepts. The generation after the run has trigger run, and its changes are attributed to the run when the pass before it was the run’s own and read their kind whole; a pass after a run that changed nothing writes no generation but is listed in history and the summary (runs). There is no command-line flag for it: the integration’s program calls the client directly (lemano ansible record), with target files it builds for the length of the command from the run’s own inventory.

Invariants

These hold across the whole code base. A change that breaks one is a bug, whatever else it fixes.

  • The CLI never imports engine internals. cmd/ and internal/cli depend only on api/v1alpha1 and client. golangci-lint’s depguard rules in .golangci.yml enforce it.
  • Secrets are never in plaintext in the store, in logs, in error messages or in test fixtures. Target files hold references to credentials, not the credentials themselves. Logs carry target names, kinds, paths and errors only. Free text that can hold a secret typed by an administrator (a cron command, a sudo command, a forced SSH command, sshd’s command settings, a CIFS password in fstab) is a sensitive field: the discovery document holds the keyed hash of its value, never the value, so no record, output or fixture made from it can hold the text, and a change still shows. A name derived from such text holds a short keyed hash, never a plain one, and errors name fields, never values. A command line found in a host’s logs for attribution (sudo’s, apt’s, dnf’s) is kept as its keyed hash beside the program’s name, and no output shows the hash. The text of a sudo command is also kept, encrypted apart from the record, and shown unless it looks like it holds a secret. A proxy command is never logged, shown or put in an error; its failures carry its exit status and the start of its standard error.
  • Host keys are pinned. An unknown host key is refused unless the operator accepts it explicitly; a changed host key is always refused. Every jump host on the way to a target is pinned the same way, under its own address. Nothing in the user’s ssh configuration changes this: the ssh client is only asked to print its settings (ssh -G), and the settings about host keys are never read.
  • History is append-only. Generations are numbered and chained by hash when they are written, and a stored generation is never changed. The canonical JSON of a generation is the record; any other column is a copy that verification checks against it.
  • One writer per target. A command that can write a generation for a target takes the target’s lock first and never waits for it. A holder also keeps an operating system lock on a file beside the store for as long as it holds the row. A lock is taken over only when its holder ran on this host and no longer holds that lock (or, for a row without one, its process is gone); any other lock is freed only by an explicit force-unlock.
  • The store schema only moves forward. A schema change is a new numbered migration in internal/store/migrations; a released migration is never edited. A store written by a newer version is refused.
  • Raw machine and boot IDs are never stored. A host is identified by an application-specific value derived from its machine ID, never by the ID itself.
  • Nothing is installed or written on target hosts, except by a revert. Scripts travel over the SSH session and the commands of a pass only read: the connector writes no file on the host. The host still logs the login, and some tools update their own metadata as they read; on Red Hat family hosts the package read leaves dnf’s own cache folder in /var/tmp. Only lemano revert writes, and only the files and kernel parameters of its plan, plus a trial’s root-only folder, timer and boot unit (lemano target end-trial runs or removes a trial’s folder that a restart left behind). The file helpers that write live in internal/writestep/writefile.sh and travel with the write step only, never with a pass.
  • A revert plans against a fresh pass under the lock, and the host refuses stale files. The engine sends, with each file to write, the SHA-256 the fresh pass read; the write step compares it with the live file right before the rename and skips a file that changed. The revert is recorded as a new generation (trigger revert or trial, with its source generation and a result per change): history only moves forward.
  • A kind never stops a pass. A step that fails, runs out of time or lacks its tool gives error records for its kind; the other kinds are still read. The objects of a kind that failed are carried from the previous generation, never recorded as removed. So are the objects of a file or directory that a kind which was read could not read for lack of privilege: the kind declares in readFrom which field holds the path each object is read from, and the generation lists these sources as not read, with the objects kept for each. A file that was listed but not read keeps its own record from the pass, with the content fields of the earlier one, and is listed as not read with kept: content.
  • A kind’s version is not a change on the server. A record names the version of the kind that read it, so a new version rewrites every record of its kind. When two records of an object differ in that version alone, the engine counts the object as a kind update, once per kind, not as a changed object; the generation is still written. Only the engine knows the difference: it compares the two stored records, not their hashes.
  • A baseline is a pointer. lemano baseline records which generation a target is compared with; it copies nothing and never changes history.
  • Diffs never show a sensitive value. A field the kind declares sensitive and a sealed setting of the ssh.effective fact are listed as changed with no value, not even their keyed hash. The command line of an attribution is shown only when it does not look like it holds a secret.
  • No gap is silent. Everything a pass could not read is listed with the pass, by kind and scope, and the pass is partial. The one exception is a target declared with spec.connector.privilege: user, run without sudo on purpose: there a privilege gap (an error whose message contains “permission denied”) is still listed, marked expected, but the pass stays complete. A kind that read nothing for that reason is skipped (it needs root), and its objects are absent from the generation rather than carried over.
  • A kind that cannot apply on a host says so. A host that lacks what a kind is about (Package with neither dpkg nor rpm) is not a failure and not a privilege gap: the kind prints one record, with an empty scope and the fixed message “not supported on this host” (lemano_emit_unsupported), and the engine records the kind as unsupported in the generation’s coverage. The pass stays complete and the kind’s objects are absent. Coverage then decides what is compared: a kind skipped or unsupported in one generation and read in the other is named by diff, history and the pass as a change of the kind’s state, and its objects are not listed as added or removed.
  • Content is stored only encrypted. Object records hold keyed hashes of file content and of sensitive fields, never a plain hash or the text, and volatile fields are never stored, so they never cause a generation.