Search and contents

A stand-in for a target system's REST interface

An ERP Node and a Production Scheduling Node poll a target system's REST interface, and an ERP Node reports back to it. When that target system is not reachable, or when you want a poll to answer something particular at a particular moment, this command serves the routes a file names, so the Node's real connector has something to talk to.

python3 -m engine.rest_stand_in_cli serve ROUTES --key-file KEYFILE --port N
python3 -m engine.rest_stand_in_cli cue PATH BODY --port N
python3 -m engine.rest_stand_in_cli reports --port N

This command is test tooling, not part of the public API. Its options, the routes file format, the control routes and its 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.

Every value on this page and in examples/rest-stand-in-site/ is invented: the order, the lot, the quantities, the rates, the machine and the shift. The target systems are made up, and the key in the recipes is made up. The files describe the same invented order, MD-2027-0301, as the database log scenario, and they are not the first demo's data.

What the stand-in is not

  • It is not any real ERP or scheduler. It answers the paths its file names with the bodies its file holds, and nothing else. It does not know what an order, a lot or a shift is.
  • It has no paging. Each route answers one JSON object, as a Node's poll expects, and a query string on a request is ignored. A Node tracks one record per poll, and so does this.
  • It writes nothing back beyond its report sink. The sink keeps what a Node reports to it so a test can read it. No report changes what a route answers.
  • It cannot fail on cue. Only a route's body changes while it runs. It does not answer an error status, slow down or drop a connection, and it does not expire its token.
  • It does not use TLS. It speaks plain HTTP, so it is for a machine you trust.

Run it on a machine you trust

It binds only a loopback or private-network address, given as an IP address: --host is 127.0.0.1 unless you say otherwise, and a name (localhost too), a public address and 0.0.0.0 are refused. The api key is never an option. It is the one line of a file of its own, which only its owner may be able to read (chmod 600); a key file that others can read is refused. Nothing the command prints holds the key or the bearer token it hands out, and the token is new each time the server starts.

The control routes (the ones cue and reports use) answer only a client on the machine the server runs on, that names the server by an IP address, and a body change must be sent as JSON. Those checks keep another host, a web page in a browser and a name that resolves somewhere else from changing what the server says. A reverse proxy on the same machine would defeat the first of them, so do not put one in front of it.

Setup and the commands in this document

Each step below is a command that runs as written from the repository root, with the project's Python environment active (or prefixed with poetry run), once this block has been run in the same shell. It makes a work directory and two key files holding made-up keys, defines ask (a few lines that poll or report through the same RestPollingConnector a Node uses), and starts two stand-ins: the invented ERP on port 8090 and the invented scheduler on port 8091. Set ERP_PORT and SCHEDULER_PORT first if either is taken. The servers stop when the shell exits.

WORK=$(mktemp -d)
ERP_PORT=${ERP_PORT:-8090}
SCHEDULER_PORT=${SCHEDULER_PORT:-8091}
KIT=examples/rest-stand-in-site
printf '%s\n' "invented-erp-key-1" > "$WORK/erp.key"
printf '%s\n' "invented-scheduler-key-2" > "$WORK/scheduler.key"
chmod 600 "$WORK/erp.key" "$WORK/scheduler.key"
ask() {
  python3 - "$@" <<'PY'
import json, sys
import httpx
from engine.connector import RestConnectorProfile, RestPollingConnector

verb, port, key_file, path, *more = sys.argv[1:]
key = open(key_file).read().strip()
profile = RestConnectorProfile(
    endpoint=f"http://127.0.0.1:{port}", api_key=key, report_path=path
)
with RestPollingConnector(profile) as target:
    try:
        if verb == "get":
            print(json.dumps(target.poll(path=path), sort_keys=True))
        else:
            target.report(json.loads(more[1]), idempotency_key=more[0])
            print("reported")
    except httpx.HTTPStatusError as exc:
        print(f"refused: HTTP {exc.response.status_code}")
        sys.exit(1)
PY
}
python3 -m engine.rest_stand_in_cli serve "$KIT/erp-routes.json" \
  --key-file "$WORK/erp.key" --port "$ERP_PORT" > "$WORK/erp-serving.txt" 2>&1 &
python3 -m engine.rest_stand_in_cli serve "$KIT/scheduler-routes.json" \
  --key-file "$WORK/scheduler.key" --port "$SCHEDULER_PORT" > "$WORK/scheduler-serving.txt" 2>&1 &
trap 'kill $(jobs -p) 2>/dev/null' EXIT
for _ in $(seq 50); do
  grep -q Serving "$WORK/erp-serving.txt" && grep -q Serving "$WORK/scheduler-serving.txt" && break
  sleep 0.1
done
grep -q Serving "$WORK/erp-serving.txt" || echo "The ERP stand-in did not start: is port $ERP_PORT free?" >&2
grep -q Serving "$WORK/scheduler-serving.txt" || echo "The scheduler stand-in did not start: is port $SCHEDULER_PORT free?" >&2

What it says it serves

serve prints one line naming the file's name and the address, then each path it answers and how. The data comes from the file, and the key is not in it:

cat "$WORK/erp-serving.txt"
Serving 'invented ERP for order MD-2027-0301' on 127.0.0.1:8090. Every value it serves comes from the file.
  POST /auth  (trades the api key for a bearer token)
  GET  /data  (bearer token)
  GET  /materials  (bearer token)
  GET  /costing/standard-rate  (bearer token)
  POST /events  (report sink: bearer token, Idempotency-Key)

Reading a route

A route answers its body behind the bearer token, which RestPollingConnector gets by posting the api key to the auth path. This is the invented ERP's material, the one a Node's poll reads. The order holds 150 of the 180 on hand, against 400 required:

ask get "$ERP_PORT" "$WORK/erp.key" /data
{"held_for_order": {"MD-2027-0301": 150}, "lot_number": "CP-88301", "on_hand": 180, "required": 400}

A key the file's owner did not give is refused, and the poll fails the way it does against a real target system whose key was revoked:

printf '%s\n' "a-key-the-stand-in-does-not-know" > "$WORK/wrong.key" && chmod 600 "$WORK/wrong.key"
ask get "$ERP_PORT" "$WORK/wrong.key" /data
refused: HTTP 401

Changing a route on cue

cue replaces the body of a route the file names, while the server runs. The next poll answers the new body, and nothing else changes. Here the lot's on-hand quantity falls from 180 to 20, a shortage against the 150 the order holds, and the same Node polling the same route sees it:

cat > "$WORK/shortage.json" <<'JSON'
{"lot_number": "CP-88301", "on_hand": 20, "required": 400, "held_for_order": {"MD-2027-0301": 150}}
JSON
python3 -m engine.rest_stand_in_cli cue /data "$WORK/shortage.json" --port "$ERP_PORT" &&
  ask get "$ERP_PORT" "$WORK/erp.key" /data
Changed /data.
{"held_for_order": {"MD-2027-0301": 150}, "lot_number": "CP-88301", "on_hand": 20, "required": 400}

The same command moves a schedule window or a shift boundary. The second stand-in is the invented scheduler. Its shift runs 06:00 to 14:00; the cue moves it to the next one, 14:00 to 22:00, which is what a peer's current_shift question reads:

cat > "$WORK/next-shift.json" <<'JSON'
{"shift_start": "2027-03-08T14:00:00Z", "shift_end": "2027-03-08T22:00:00Z"}
JSON
ask get "$SCHEDULER_PORT" "$WORK/scheduler.key" /shift/current &&
  python3 -m engine.rest_stand_in_cli cue /shift/current "$WORK/next-shift.json" --port "$SCHEDULER_PORT" &&
  ask get "$SCHEDULER_PORT" "$WORK/scheduler.key" /shift/current
{"shift_end": "2027-03-08T14:00:00Z", "shift_start": "2027-03-08T06:00:00Z"}
Changed /shift/current.
{"shift_end": "2027-03-08T22:00:00Z", "shift_start": "2027-03-08T14:00:00Z"}

A cue can only replace a route the file already names. It cannot add a route, and it cannot touch the auth path or the report sink:

python3 -m engine.rest_stand_in_cli cue /orders "$WORK/shortage.json" --port "$ERP_PORT" 2>&1
the stand-in refused it (HTTP 404): the file names no such route

The report sink

A Node reports to the sink with an Idempotency-Key, and the sink refuses a report without one. It keeps every delivery, with its key, in the order they arrived. It does not deduplicate: a real target system decides what a repeated key means, and a test needs to see a redelivery. reports prints what was kept, one object a line:

ask post "$ERP_PORT" "$WORK/erp.key" /events release-1 '{"order_id": "MD-2027-0301", "status": "in-progress"}' &&
  ask post "$ERP_PORT" "$WORK/erp.key" /events release-1 '{"order_id": "MD-2027-0301", "status": "in-progress"}' &&
  python3 -m engine.rest_stand_in_cli reports --port "$ERP_PORT"
reported
reported
{"body": {"order_id": "MD-2027-0301", "status": "in-progress"}, "idempotency_key": "release-1"}
{"body": {"order_id": "MD-2027-0301", "status": "in-progress"}, "idempotency_key": "release-1"}

What it refuses

A request with no token gets a 401 whatever its path, so a client cannot learn from the answers which paths exist. A body must come with a Content-Length (chunked bodies are refused), must be JSON (NaN and the infinities are not), and must not nest absurdly deep.

An address that is not loopback or private is refused before anything is bound, and nothing is served:

python3 -m engine.rest_stand_in_cli serve "$KIT/erp-routes.json" --key-file "$WORK/erp.key" --port 8099 --host 0.0.0.0 2>&1
the host must be a loopback or private-network IP address

A key file that others can read is refused, and the message does not show what it held:

cp "$WORK/erp.key" "$WORK/open.key" && chmod 644 "$WORK/open.key"
python3 -m engine.rest_stand_in_cli serve "$KIT/erp-routes.json" --key-file "$WORK/open.key" --port 8099 2>&1
cannot serve: the key file is readable by others; chmod 600 it

There is no option that takes a key. An argument the command does not know is refused, and it is not repeated back:

python3 -m engine.rest_stand_in_cli serve "$KIT/erp-routes.json" --key-file "$WORK/erp.key" --port 8099 --api-key invented-erp-key-1 2>&1 | tail -n 1
python3 -m engine.rest_stand_in_cli: the arguments were not understood

The files

The routes file

One JSON object. A key this format does not know is refused, and so is a key that appears twice.

KeyMeaning
stand_in_format Required. 1.
name Required. A short printable text, shown when the server starts.
auth_path The path that trades the api key for a bearer token. Default /auth.
report_path The path of the report sink. Leave it out for a target system that is only read.
routes Required. An object from a path to the JSON object that path answers on a GET.

A path starts with /, holds no query, space, % or //, and is not /_stand-in or under it. The auth path, the report path and the routes are all different paths.

The key file

One line: the key, printable ASCII, with no padding. A file that its group or others can read, that is a link or a directory, or that holds more than a line is refused. The key is the one a Node's rest.api_key holds.

The control routes

cue and reports use them; they are listed so a test can use them without the command.

RouteDoes
PUT /_stand-in/routes/<path> Replaces the body of the route <path>, which the file must name. The body is a JSON object sent as application/json.
GET /_stand-in/reports Answers {"reports": [{"idempotency_key": ..., "body": ...}, ...]}, oldest first. The sink keeps at most 10,000 deliveries and 8 MiB of bodies; it refuses the rest with a 507.

The files in examples/rest-stand-in-site/

FileWhat it is
erp-routes.json The invented ERP: the material (/data, and /materials for the older example config), the standard cost (/costing/standard-rate) and the sink (/events), for order MD-2027-0301 and lot CP-88301.
scheduler-routes.json The invented scheduler: the order's slot (/schedule) and the current shift (/shift/current). It has no sink.

Every path the ERP and Production Scheduling example configs poll or report to is served by one of them. /materials is a second route with the same body as /data: a cue to one does not change the other.

Pointing a Node at it

A Node's rest section names the stand-in's address and the key in the key file. A Node that talks to a stand-in at 127.0.0.1 on port 8090 needs this, and the paths the shipped example configs already name:

{
  "rest": {
    "endpoint": "http://127.0.0.1:8090",
    "api_key": "invented-erp-key-1"
  }
}

The endpoint is plain HTTP, so use it with a Node that runs in development mode and not with an appliance in production. A real key never goes in a file of this kind.

The tests that run this page

tests/test_rest_stand_in_doc.py runs the setup block and every command above, in the order the page gives them, and checks that each exits with the code the page gives it and that each prints what the page shows. tests/test_rest_stand_in.py runs the server and the command against real requests, and tests/test_rest_stand_in_routes.py runs an ERP Node and a Production Scheduling Node, built as their entrypoints build them, against the two example files.

Limits

  • A route is one JSON object. A target system that pages its answers, or that answers a list, is not what this stands in for.
  • A cue changes one route at a time, and does not change the key, the token or the sink.
  • The sink holds what it was sent in memory, up to 10,000 deliveries and 8 MiB of bodies, and loses it when the server stops.
  • A server serves one file. A site with two target systems runs two, on two ports, each with its own key file.

Release: v0.11.0