Search and contents

Making a stand-in for a target system's OPC UA server with one command

When a target system's OPC UA server cannot be reached while a config is written, a stand-in lets the config be checked now and run against the real thing later. This command makes one from the target's own listing and serves it: it lists the nodes the config's server offers, writes the node file and a config that points at the stand-in, and starts the file-driven node server on them.

python3 -m engine.stand_in_cli CONFIG NODE_TYPE_DIR [options]

It reads the listing as structured data from the connector the Node itself uses, not from the text the browse command prints, so nothing parses printed text. It reads no values, and what it serves is the target's node ids, types and browse names, each at its type's zero unless you give it a value.

This command is test tooling, not part of the public API. Its options and exit codes may change in any release, and the stability policy (public-api-stability.md) does not cover them. It is not installed or provisioned on an appliance. The text it prints is for a person to read; do not parse it.

Run it on a machine and network you trust

The node server it starts speaks Security Policy None, anonymous, on every interface: anyone who can reach the port can read every node it serves. Nothing here asks for a credential, and nothing about the stand-in is secured. Run it on a machine and a network you trust, for as long as you need it, and stop it afterwards. The command's own --port picks the port and not the interface, and the node server has no option to narrow either.

The data is synthetic. A stand-in holds a target's ids and names and values of zero or ones you give it; it is not a copy of the target system's data.

Before you start

The command runs from the repository root with the project's Python environment active (or prefixed with poetry run), and needs two programs built on the same machine:

  • The kindred-opcua-sidecar program on PATH, as a Node needs it. The command connects to the target through it.
  • The node server, at sidecar/node_server/build/kindred-opcua-node-server unless --server names another. It is needed even with --write-only, because its own --check judges the node file.

deployment/install_integrator_toolchain.sh builds both. It runs four setup scripts, in this order, and stops at the first one that fails, with that script's exit code. It asks for no more privilege than they do (each uses sudo itself, or none as root). --print lists what it would run and runs none of it; an absolute directory after it is the prefix the scripts that take one install under (default /usr/local, so kindred-opcua-sidecar lands in its bin, which must be on PATH). The script can be started from any directory; the commands it lists are written from the repository root, where this page's commands run too:

deployment/install_integrator_toolchain.sh --print
bash deployment/install_open62541_build_tools.sh
bash deployment/install_open62541.sh
bash deployment/build_opcua_sidecar.sh
bash sidecar/node_server/build.sh

Run without --print, it builds open62541 from source, which takes a few minutes the first time and is skipped when an install that is current is there. It installs no Python packages: poetry install is a step of its own, run as the user whose checkout it is.

Setup and the commands in this document

Every command below runs as written once this block has been run in the same shell. It starts a first server, which plays the target system so there is something to list: on a real project you leave it out, because your config's endpoint names the target system. It also makes a copy of the MES example config that points at that server. The nodes, their types and the order are made up, and the data is synthetic. Servers started here stop when the shell exits. They use ports 4840 and 4841; set TARGET_PORT and STANDIN_PORT first if either is taken.

WORK=$(mktemp -d)
TARGET_PORT=${TARGET_PORT:-4840}
STANDIN_PORT=${STANDIN_PORT:-4841}
SERVER=sidecar/node_server/build/kindred-opcua-node-server
cat > "$WORK/target-nodes.txt" <<'NODES'
ns=1;i=5000             string  ro      Order         MD-2027-0142
ns=2;s=Line1.State      string  ro      State         RUN
ns=2;s=Line1.Setpoint   double  rw      Setpoint      72.5
ns=2;s=Line1.Enabled    boolean ro      Enabled       true
NODES
"$SERVER" "$WORK/target-nodes.txt" --port "$TARGET_PORT" > "$WORK/target-out.txt" &
trap 'kill $(jobs -p) 2>/dev/null' EXIT
for _ in $(seq 50); do grep -q listening "$WORK/target-out.txt" && break; sleep 0.1; done
grep -q listening "$WORK/target-out.txt" || echo "The target server did not start: is port $TARGET_PORT free?" >&2
sed "s/127.0.0.1:4840/127.0.0.1:$TARGET_PORT/" node-types/mes/example_node_config.json > "$WORK/target.json"

Writing the files

The command takes the config and a directory of Node Type definitions (node-types in this repository). With --write-only it checks the config offline, lists the target, writes two files to --out-dir and serves nothing. --max-depth 1 lists only what is directly under the Objects folder, which is all this server has; the default, 2, would also list what is inside the target's own Server object, nodes of the server's own in namespace 0, and report its Variables as left out (see what it keeps). The first line says how many nodes the listing held and how many the stand-in serves; one node is not served here, the server's own entry, because only Variables are candidates:

{
  python3 -m engine.stand_in_cli "$WORK/target.json" node-types \
    --max-depth 1 --port "$STANDIN_PORT" --write-only --out-dir "$WORK/out" &&
  cat "$WORK/out/nodes.txt"
}
Browsed opc.tcp://127.0.0.1:4840 from the Objects folder, to a depth of 1: 5 nodes listed, 4 to serve.
Stand-in: wrote nodes.txt (4 nodes) and stand-in.json in /tmp/tmp.q1w2e3r4t5/out; nothing is served.
ns=1;i=5000 string ro Order
ns=2;s=Line1.State string ro State
ns=2;s=Line1.Setpoint double ro Setpoint
ns=2;s=Line1.Enabled boolean ro Enabled

nodes.txt is the node file: each node read-only, at its type's zero. stand-in.json is the config you gave, with the endpoint of each OPC UA section that held the browsed endpoint replaced by opc.tcp://127.0.0.1:PORT. Here that is the only line that differs; diff exits 1 when files differ, which is what is wanted:

diff "$WORK/target.json" "$WORK/out/stand-in.json"
7c7
<       "endpoint": "opc.tcp://127.0.0.1:4840",
---
>       "endpoint": "opc.tcp://127.0.0.1:4841",

Other sections' addresses (a broker, a database, an inference endpoint) are not touched. Both files are readable by their owner only. A config that sets credentials or security settings differs in more than the endpoint; see what it leaves out of the config.

Giving a node a value

--set ID=VALUE gives a served node a value instead of its type's zero. It is for a site that already knows what a node reads, and it can be repeated. The node server's own check judges each value (a value outside its type is refused), and an id that is not among the nodes to be served is refused, here with exit 2:

{
  python3 -m engine.stand_in_cli "$WORK/target.json" node-types \
    --max-depth 1 --port "$STANDIN_PORT" --write-only --out-dir "$WORK/valued" \
    --set 'ns=1;i=5000=MD-2027-0142' --set 'ns=2;s=Line1.Setpoint=72.5' &&
  cat "$WORK/valued/nodes.txt"
}
Browsed opc.tcp://127.0.0.1:4840 from the Objects folder, to a depth of 1: 5 nodes listed, 4 to serve.
Stand-in: wrote nodes.txt (4 nodes) and stand-in.json in /tmp/tmp.q1w2e3r4t5/valued; nothing is served.
ns=1;i=5000 string ro Order MD-2027-0142
ns=2;s=Line1.State string ro State
ns=2;s=Line1.Setpoint double ro Setpoint 72.5
ns=2;s=Line1.Enabled boolean ro Enabled
python3 -m engine.stand_in_cli "$WORK/target.json" node-types \
  --write-only --out-dir "$WORK/refused" --set 'ns=9;i=1=5' 2>&1
Stand-in: cannot run: --set number 1 names no served node, repeats one, or holds a control character

A refusal names the argument's position and never the value. A --set value is on the command line, so the machine's other users can see it in the process list, and it lands in the shell's history. It is a node's value and never a credential: do not put a secret there.

Serving it

Without --write-only the command goes on to start the node server on --port (default 4841, the target's usual 4840 plus one), waits up to ten seconds for the server to say it is listening, prints one line and runs until SIGINT (Ctrl-C) or SIGTERM, when it stops the server and exits 0. Normally it runs in a shell of its own and the config is tried from another; here it runs in the background of this one:

python3 -m engine.stand_in_cli "$WORK/target.json" node-types \
  --max-depth 1 --port "$STANDIN_PORT" --out-dir "$WORK/served" > "$WORK/stand-in-out.txt" &
STAND_IN=$!
for _ in $(seq 100); do grep -q serving "$WORK/stand-in-out.txt" && break; sleep 0.1; done
cat "$WORK/stand-in-out.txt"
Browsed opc.tcp://127.0.0.1:4840 from the Objects folder, to a depth of 1: 5 nodes listed, 4 to serve.
Stand-in: serving 4 nodes of opc.tcp://127.0.0.1:4840 at opc.tcp://127.0.0.1:4841, files in /tmp/tmp.q1w2e3r4t5/served. Stop with Ctrl-C.

The config it wrote reads the stand-in. The offline check with --connect tests the connection, so a config for a target can be tried before the target is reached:

python3 -m engine.offline_check_cli "$WORK/served/stand-in.json" node-types --connect
Offline check: the config is valid (Node Type MES).
Connection check: passed.
  opcua (opcua) -> opc.tcp://127.0.0.1:4841: passed: connected

A browse of the stand-in lists the target's nodes. The header line names the endpoint, so it is the one line left out of the comparison:

python3 -m engine.opcua_browse_cli "$WORK/target.json" node-types --max-depth 1 > "$WORK/target-listing.txt" &&
python3 -m engine.opcua_browse_cli "$WORK/served/stand-in.json" node-types --max-depth 1 > "$WORK/stand-in-listing.txt" &&
diff <(tail -n +2 "$WORK/target-listing.txt") <(tail -n +2 "$WORK/stand-in-listing.txt") &&
echo "The two servers list the same nodes."
The two servers list the same nodes.

Stopping it with SIGTERM stops the node server too, and the exit code is 0:

kill -TERM "$STAND_IN"
wait "$STAND_IN"

The files' directory

With no --out-dir the files go in a new temporary directory, which is printed, made readable by its owner only, and removed when the stand-in stops. --write-only keeps it, since the files are what it is for, and a directory you name with --out-dir is kept too. Files already called nodes.txt or stand-in.json in a directory you name are replaced. Here the temporary location is an empty directory of the run's own, so the files can be counted:

mkdir "$WORK/tmp"
TMPDIR="$WORK/tmp" python3 -m engine.stand_in_cli "$WORK/target.json" node-types \
  --max-depth 1 --port "$STANDIN_PORT" > "$WORK/default-out.txt" &
DEFAULT=$!
for _ in $(seq 100); do grep -q serving "$WORK/default-out.txt" && break; sleep 0.1; done
echo "while serving: $(ls "$WORK/tmp" | wc -l) directory"
kill -TERM "$DEFAULT"
wait "$DEFAULT"
echo "after stopping: $(ls "$WORK/tmp" | wc -l) directories"
while serving: 1 directory
after stopping: 0 directories

When it cannot make one

A target that cannot be browsed exits 3, and nothing is written or served. Here the config names the port the stand-in would use, where nothing is listening now:

sed "s/127.0.0.1:$TARGET_PORT/127.0.0.1:$STANDIN_PORT/" "$WORK/target.json" > "$WORK/unreachable.json"
python3 -m engine.stand_in_cli "$WORK/unreachable.json" node-types --write-only --out-dir "$WORK/none" 2>&1
Stand-in: the target could not be browsed: BadConnectionRejected

Exit codes

Not covered by the stability policy; they follow the browse command's pattern.

CodeMeaning
0 Stopped by a signal (one that arrives before the server is listening included), or --write-only finished.
1 The config is invalid (not JSON, a schema or rule failure, an unknown Node Type).
2 The command could not run: a usage error, a config that cannot be read, a Node Type directory that cannot be used, a section that is not one of the Node Type's OPC UA sections, a --set the command or the node server refused, a node server that is not built or could not start (a taken port included), an output directory that cannot be used, or a sidecar program that could not be started.
3 The target could not be browsed: it could not be reached, refused the start node, did not answer in time, or answered in a way the command could not read.
4 The listing is partial, or nothing in it can be served, or the node server will not serve what it holds. Nothing was written and nothing was served.
5 No verdict: the config is encrypted. The command reads plain configs only, and writes nothing.

A partial listing is refused

A stand-in built from part of a listing would mislead, so exit 4 and nothing served when the listing is partial in the browse command's sense: the node limit was reached (the node server's own limit is 1000 nodes, the browse command's default), nodes were left out because their ids cannot be shown, or the server refused to list the children of a node. A listing that holds nothing the node server can serve, or that it would refuse (a namespace above 100, say), exits 4 as well, with the server's reason.

What it keeps and what it leaves out

It keeps Variables at any depth that are in namespace 1 or above and have a type the node server serves. --max-depth (1 to 8, default 2) says how many levels below the start node are listed, and --node-id starts the listing at a folder. A config with more than one OPC UA section needs --section to say which one. The stand-in is flat: the node server serves every node directly under its Objects folder, so a folder in the target's tree is not reproduced and the ids a config reads are the same. A Variable inside an Object at the depth limit is not served, and the command says how many such Objects it did not open.

What it leaves out is counted by reason, with the first 20 ids named and then "and N more". It never prints a value.

Reason, as the command reports itWhy
in namespace 0, which is the server's own The node server makes its own; a node file cannot hold them.
a Variable with no data type The node file needs a type.
a data type the node server does not serve The node server serves ten types (opcua-node-server.md).
an id or browse name holding a space or a control character A node file is one node to a line, split at spaces, so a line break or a space inside a name or an id would change what the file says.
a browse name that is empty or not in its id's namespace The node server would serve it under another name than the listing's.
listed more than once One node serves it.

Each is reported as left out, REASON: COUNT (IDS).

A node left out for one of the other four reasons is one a config cannot read from the stand-in, and a check that reads it fails with exit 3. The first and the last are the exceptions: the node server makes the server's own namespace 0 nodes itself, and a node listed twice is served once. The only server a public page can show is the node server, which serves none of the cases, so no recipe here lists one; the command's own tests do.

What it leaves out of the config

The node server speaks Security Policy None and is anonymous, so the stand-in's config leaves out username, password, certificate_path, private_key_path, trusted_certificates_path, security_mode and security_policy in each OPC UA section it rewrites. It names them in a line (left out of stand-in.json: ...) and never shows a value. The reason is that a target's credentials should not be copied to a new place, and that a certificate left behind would also bring back the encrypted mode as the connection's default. A config that sets any of them therefore differs from the one you gave in those lines as well as the endpoint.

The command takes no credential as an option and prints none. It refuses an encrypted config (exit 5) instead of decrypting it, so no key is read. Other sections are copied as they are, secrets of theirs included, to the same owner-only file: give it a config you are willing to have copied, and keep the output directory somewhere you trust. A recipe cannot show this against the node server, because the node server is anonymous and a config that names a user cannot browse it; the command's own tests run it against a scripted target.

Limits

  • It is not the target system. It serves single values, directly under the Objects folder, with no folders, methods, history or security, so a config that depends on any of them is not exercised. A passing check shows that a node the config names can be read from the stand-in, not that the target behaves that way.
  • Values are zero unless you give them. A listing holds none, and every node is read-only.
  • A config that uses shared fragments is not read. The browse command takes --shared-dir for one; this command has no such option, and a config whose OPC UA section is a $shared reference exits 1 before anything is browsed. Put the section's own settings in the config you give it.
  • A listing is a walk, and it has the browse command's limits (opcua-browse.md).
  • The node server's startup check covers IPv4 only, and it wakes every 5 ms whether or not anyone is connected (fixture-kit.md).

Where next

Release: v0.11.0