Search and contents

Checking a config before it is staged

Before a config goes to a site, two questions are worth answering on the integrator's own workstation: is the file sound, and can its connectors reach their targets. One command answers both, and the same checks are available from Python as check_candidate.

  • The offline check validates the config the way a Node does when it loads one, and adds the check that every source key a connector declares is projected. For a database_log section it also checks:

    • the rules the Node applies when it starts: a password needs a ca_bundle_path, the host must be a private-network address or a host name, and source_timezone must be a zone name the system knows;
    • that every name the mapping reads is one of the section's declared columns, which a Node does not check when it loads.

    The offline check reaches no target and looks up no host name, stages nothing and writes nothing.

  • The connection check (--connect) runs only for a config that passes the offline check. It tests each connector the config declares against its target, and closes every connector it opened.

Both run from the repository root, with the project's Python environment active (or prefixed with poetry run). The commands below use paths relative to the repository root, such as node-types.

Setup and the commands in this document

Every command below runs as written once this block has been run in the same shell. It makes a scratch directory with a small Node Type definition, a valid config, an invalid one and an encrypted one (a file is treated as encrypted when its name ends in .enc.json), plus a copy of the MES example config pointed at a port where nothing listens.

WORK=$(mktemp -d)
mkdir "$WORK/types"
echo '{"identity": {"name": "Demo Sensor", "version": "1.0.0"}, "config_schema": {"type": "object", "required": ["endpoint"]}}' > "$WORK/types/node_type.json"
echo '{"node_type": "Demo Sensor", "node_type_version": "1.0.0", "config_version": 1, "config": {"endpoint": "http://sensor.local"}}' > "$WORK/valid.json"
echo '{"node_type": "Demo Sensor", "node_type_version": "1.0.0", "config_version": 1, "config": {}}' > "$WORK/invalid.json"
echo '{"sops": {}, "node_type": "ENC[ciphertext]"}' > "$WORK/secret.enc.json"
sed 's/127.0.0.1:4840/127.0.0.1:1/' node-types/mes/example_node_config.json > "$WORK/unreachable.json"

Running the check

The command takes the config file and a directory of Node Type definitions. --shared-dir DIR names the shared fragments directory, if the config uses any.

python -m engine.offline_check_cli "$WORK/valid.json" "$WORK/types"

With --connect, a config that passes also has each connector tested against its target, so the targets must be reachable from this machine. This one fails, because nothing listens on the port:

python -m engine.offline_check_cli "$WORK/unreachable.json" node-types --connect --timeout 2

Four options belong to --connect, and each is refused without it (exit 2):

OptionMeaning
--timeout SECONDS How long to wait for a connector to connect. The default is 5 s.
--mqtt-wait SECONDS How long an MQTT connector waits for one message on its subscriptions. The default is 5 s, 0 is not to wait, and the most is 60 s. A quiet topic is a result ("no message within N s"), not a failure; a retained message arrives at once.
--db-sample Also read one row of a database log connector's table, the one with the highest watermark, and print it (it implies --show-sample). Off by default: a plain --connect only asks the database whether the table and its columns are there, and reads no row. The row is production data. --timeout bounds the row read as well as the connection, so raise it for a table whose watermark has no index.
--show-sample Print what each passing connector returned. Off by default, because a sample is whatever the target returned. It has the config's own secrets removed and is cut to a size, but it can still hold plant data that is not a secret of the config.

A value that is not a positive, finite number of seconds, or an --mqtt-wait above 60, is a usage error too:

python -m engine.offline_check_cli "$WORK/valid.json" "$WORK/types" --connect --mqtt-wait 61

Exit codes

A script should act on the exit code alone.

CodeMeaningWhat a script should do
0 The config is valid, and every connector connected if --connect was given. Go on.
1 The config is invalid: not JSON, a schema or rule failure, or an unknown Node Type. Fix the config. The reasons are printed, each as a rule and a message with no config value in it.
2 The check could not run: a usage error, a config that cannot be read or decrypted, or a Node Type directory that cannot be used. Fix the command or the environment. Nothing was judged about the config.
3 The config is valid, but a connector failed its connection check. Look at the connector named in the output: a wrong address, credential or node, or a target that is not reachable from this machine.
4 The config is valid, but the connection check was skipped: a connector type with no builder, or a Node Type with no known connectors. Treat the connections as unchecked, not as passed. The reason is printed.
5 No verdict. The config is encrypted and no age key is available, so it was neither validated nor checked. Make the key available and run again.

A connector failure outranks a skip, so a run with both exits 3. A config that is invalid, cannot be read or has no verdict keeps 1, 2 or 5 even with --connect, and its connections are never tried.

These are the documented codes. An invalid config:

python -m engine.offline_check_cli "$WORK/invalid.json" "$WORK/types"

A config that cannot be read:

python -m engine.offline_check_cli "$WORK/absent.json" "$WORK/types"

A valid config for which the connection check could not be done, because this Node Type has no known connectors:

python -m engine.offline_check_cli "$WORK/valid.json" "$WORK/types" --connect

An encrypted config on a workstation with no age key. The env prefix makes this the case whatever the host has; on a real workstation without a key, the plain command is enough:

env -u SOPS_AGE_KEY -u SOPS_AGE_KEY_CMD -u SOPS_AGE_KEY_FILE XDG_CONFIG_HOME="$WORK/no-keys" \
  python -m engine.offline_check_cli "$WORK/secret.enc.json" "$WORK/types"

An encrypted config is never read without a key, and the file is not guessed at. With sops's default settings every value is ciphertext, the Node Type name included, so nothing can be judged. The check says so instead of reporting a pass.

When a key is available but does not open the file (or sops is not installed), the config cannot be decrypted, which is exit 2, not 5.

From Python

check_candidate does the same and returns a CandidateCheck, with one ConnectionOutcome per connector. These names are in engine.public.

import json
import tempfile
from pathlib import Path

from engine.public import check_candidate

with tempfile.TemporaryDirectory() as tmp:
    types = Path(tmp) / "types"
    types.mkdir()
    (types / "node_type.json").write_text(
        json.dumps(
            {
                "identity": {"name": "Demo Sensor", "version": "1.0.0"},
                "config_schema": {"type": "object", "required": ["endpoint"]},
            }
        )
    )
    config = Path(tmp) / "config.json"
    config.write_text(
        json.dumps(
            {
                "node_type": "Demo Sensor",
                "node_type_version": "1.0.0",
                "config_version": 1,
                "config": {},
            }
        )
    )
    check = check_candidate(config, types, connect=True)

print(check.state)
for rule, message in check.findings:
    print(rule, message)
print(check.connection_state, check.connection_reason)

That config is missing a required key, so the check fails and the connections are skipped with a reason; connect=True never tests a connector for a config that did not pass. For a config that passes, check.connections holds a ConnectionOutcome per connector, with its state ("passed", "failed" or "skipped"), a message, the target it connected to and, for a passing connector, a scrubbed and size-limited sample.

To test a connector of your own (one built on SystemConnector), pass:

  • extra_builders: a mapping from your connector type to a function that takes the config section and returns a BuiltConnector.
  • connector_sections: which sections of your Node Type use that type.

Both apply only with connect=True, and the check closes every connector it built. The BuiltConnector names the target the connector connects to (no credential in it) and the secrets a result must never show. The secrets are removed from every message and sample the check reports.

Other parameters and results:

  • db_sample=True returns the same row as the command's --db-sample, as the connector's sample. It is off by default and must be a real bool.
  • timeout_seconds, mqtt_wait_seconds and db_sample are checked whether or not connect is set. A value out of range raises ValueError.
  • KindredError is raised when the Node Type directory is missing or holds a definition that does not load.
  • The result's state is "skipped" (no verdict) for an encrypted config with no age key, where the command exits 5.

Checking a whole configuration repository

config-repo-ci.md runs this check over every config of a repository in a CI pipeline, with the exit codes above deciding the job.

Previewing a mapping over a sample

Once the config is valid, the mapping preview runs a captured file of source records through its mapping, offline, and reports where the mapping leaves gaps.

What to know before relying on it

  • A connection check opens a session of its own on the target. A Node that is running may hold one already. An MQTT check uses a clean session under a random client id, so it does not displace the Node's; an OPC UA or REST target sees an extra client for the length of the check. A database log check is a read-only session that asks about the table and reads no row (unless --db-sample is given), and it never touches the Node's watermark table, so it cannot move a Node's place in the log (see database-log-scanning.md).
  • A sample can hold plant data. The config's secrets are removed from it and it is cut to a size, but a value the target returned is shown as it came. This is why --show-sample is off by default and why ConnectionOutcome.sample is documented as possibly holding plant data.
  • A passing connection check is not a proof of the right target. A REST path or an OPC UA node the credential can read but that was mistyped passes, with that target's data as the sample.
  • No secret of the config is printed. Without --connect, nothing printed carries a value from the config. With it, each connector's target (its host and port, or its URL without credentials) is printed beside its message, and the message and any sample are scrubbed. An OPC UA endpoint that carries a user name and password in its URL is invalid (exit 1): the Node refuses it too, because the OPC UA library writes such a URL to its log, and the check never prints it. Keep credentials in the config's own credential fields.
  • A connection check runs from where you run it. The verdict on the config is the same from any directory, but a relative path the config names (a certificate, a CA bundle, a key, a trust list) is read from the directory the command runs in, as a Node reads it from its own. Run the check from the directory the Node runs in, or use absolute paths. An OPC UA check also needs the kindred-opcua-sidecar program on the PATH (or the config's sidecar_path); without it that connector fails and says it could not start the sidecar. An encrypted config needs sops on the PATH.
  • The text printed is not stable. Only the exit codes and the names and meanings of the options are covered by the compatibility promise, as the stability policy describes. Do not parse the output; use the exit code, or check_candidate.

Release: v0.11.0