Search and contents

Checking a configuration repository in CI

A site's Node configs live in a Git repository of the integrator's own. This page is a pipeline job for that repository: every config is run through the offline check on each merge request, so an invalid config is stopped before it reaches a Node's management API. The check reaches no target and changes nothing in the repository.

The job is one script, deployment/check_config_repo.sh, which needs nothing else from a Kindred checkout. Copy that one file into the configuration repository (the examples use ci/check_config_repo.sh) and call it from the pipeline.

What the script does

  1. Clones the public Kindred repository at the tag you name. The installable package carries the engine only; the check also needs the Node Type definitions under node-types/, which are in the repository.
  2. Installs the engine from that clone into a throwaway virtual environment, or, with --python, puts the clone on the path of a Python you already prepared.
  3. Runs python -m engine.offline_check_cli over each config against the clone's node-types/, and exits by the codes below. Every file is checked, whatever the files before it gave. With --template it also runs python -m engine.starting_point_cli over the starting points (see below).
check_config_repo.sh --tag TAG [--repo-url URL] [--python PATH] [--shared-dir DIR]
                     [--template PATH]... [PATH...]

PATH is a config file or a directory; a directory means every *.json file under it (not under .git). Name the directories that hold configs, not the repository root, so a package.json or a tool's settings are not read as configs. At least one config or one --template is required, and a directory with none is an error: a check that found nothing to check does not pass. --shared-dir names the shared fragments directory, if your configs use any. The tag may also come from KINDRED_TAG, and the repository from KINDRED_REPO_URL or --repo-url (the default is the public repository).

The runner needs git, python3 with venv and network access to the repository and to the Python package index (not with --python, which needs only the repository), and sops on the PATH if any config is encrypted. The engine's dependencies are installed at the versions the tag allows, not frozen, so a run is reproducible only as far as the tag's own bounds are; use --python with an image you control to freeze them.

The tag must match the Node Type version the site runs

A config names the Node Type and its version (node_type_version), and the check compares that with the version in the Node Type definition exactly: a config for any other version is invalid (the offline check reports it as a node_type_version finding). The definitions come from the tag you pin, so the pinned tag must carry the Node Type version your sites run. In practice: pin the tag of the Kindred checkout your Nodes run (git describe --tags on a Node shows it), and move the pin in the same merge request that moves the configs to a newer Node Type version. There is no default tag, because only you know which one is right; the script refuses to run without one. It also refuses a tag that has no offline check, which is any tag older than the one that introduced it.

Exit codes

The script gives the offline check's own codes, the most serious across all the files checked.

CodeMeaningPipeline
0 Every config is valid. Passes.
1 At least one config is invalid. The reasons are printed, each a rule and a message with no config value in it. Fails.
2 A check could not run: a usage error, a config that cannot be read or decrypted, the clone or install failed, or no config was found. Outranks 1 and 5. Fails.
5 No verdict: at least one config is encrypted and no age key is available, so it was neither validated nor checked. Fails.

Exit 5 fails the pipeline on purpose. A config that could not be read has not passed, and a job that quietly passes over every encrypted file would be green for exactly the files that carry credentials. The script has no switch to turn this off. If a pipeline must run without the key, point it only at configs that are not encrypted.

The pipeline job (GitLab)

The image needs git, python3 with venv and, for encrypted configs, sops (the python:3.12 image has the first two; add a sops release binary to the image or in a before_script).

check-configs:
  stage: test
  image: python:3.12
  variables:
    KINDRED_TAG: v0.11.0
  script:
    - ci/check_config_repo.sh configs/
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"

v0.11.0 stands for the tag your sites run; substitute it. With SOPS_AGE_KEY set (next section) the same job also judges the encrypted configs. Without it, a repository with an encrypted config fails this job with exit 5, which is the default described above.

Giving the job the age key

To judge an encrypted config the job must decrypt it, which needs an age private key. The way to hand a CI job a secret is a CI/CD variable; set SOPS_AGE_KEY to the key's contents, masked (so it is hidden from job logs) and protected (so only pipelines on protected branches and tags receive it). The script never prints it.

The trade-off is real. Anyone who can change what the pipeline runs on a protected branch, or who has maintainer rights on the project, can read the variable, and a key that decrypts the configs decrypts every credential in them. A job on an unprotected branch or from a fork does not receive a protected variable, which means merge requests from such branches get exit 5, not a verdict. Choices that limit the damage:

  • Give CI a key of its own, with the configs encrypted to it as well as to the Node's key (several recipients in .sops.yaml), so it can be revoked without touching the Nodes.
  • Keep the key out of the checks that matter less: check unencrypted configs (or ones without credentials) in the merge request job, and the encrypted ones in a job on the protected branch only.
  • Do not use the key to do anything else. The job should check; it does not need to deploy.

If GitLab will not accept the key as a masked variable (it applies rules about which characters a masked value may hold), keep it protected and file-typed instead and set SOPS_AGE_KEY_FILE to the file's path; a file-type variable is not masked, so keep the job from printing the file.

Checking the starting points

If the repository also holds starting points, the templates a Node's configuration begins from, the same script checks them with --template. A starting point is checked against the Node Type it names, at the exact version, and then filled in with its examples and run through the offline check, so a Node Type that has moved on fails the merge request that was meant to keep up with it, and not a site's first config. --template takes a file or a directory (every *.json file under it) and may be given more than once; a run may check configs and starting points together, or either alone.

check-templates:
  stage: test
  image: python:3.12
  variables:
    KINDRED_TAG: v0.11.0
  script:
    - ci/check_config_repo.sh --template templates/
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"

Keep the templates in their own directory, apart from the configs: a template is not a config and the offline check would refuse it, and a config is not a template and the starting-point check would refuse that. The exit codes are the table above. A starting point has no credential in it, so the job needs no age key. Needs a tag that carries the starting-point check; an older one is refused by name.

An optional extra layer

The offline check applies the same validation a Node does, plus the Node Type's own rules (the exactly-one-of sections, the mapping consistency, the version match). A generic JSON schema validator such as check-jsonschema can sit in front of it as a cheap first pass for the shape of the file, against a schema you maintain yourself. It cannot replace the offline check, which knows the Node Types; this template depends on no such tool and does not install one.

What the check does not do

  • It does not reach a target. Whether a connector can connect is --connect on a workstation that can see the plant network, as the offline check describes; a CI runner usually cannot.
  • It does not stage anything. A passing job means the config is sound, not that a Node has it: the walkthrough covers the sync.

Release: v0.11.0