@yunuseon/kayra 0.17.2 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/agent/skills/kayra-rollout/SKILL.md +3 -2
- package/ansible/playbooks/edge.yml +5 -0
- package/ansible/playbooks/nas.yml +9 -1
- package/ansible/playbooks/platform.yml +8 -2
- package/ansible/roles/app/tasks/main.yml +11 -3
- package/ansible/roles/kayra-version/defaults/main.yml +5 -0
- package/ansible/roles/kayra-version/tasks/main.yml +18 -0
- package/ansible/roles/platform/tasks/main.yml +6 -4
- package/cli/dist/browser/assets/index-C4HCL022.js +7 -0
- package/cli/dist/browser/index.html +1 -1
- package/cli/dist/clients/api/api.js +6 -4
- package/cli/dist/clients/api/commands.js +0 -48
- package/cli/dist/clients/api/{contract.js → openapi.js} +2 -51
- package/cli/dist/clients/api/protocol.js +1 -66
- package/cli/dist/clients/api/start.js +1 -1
- package/cli/dist/clients/cli/parser.js +4 -0
- package/cli/dist/clients/cli/status.js +28 -7
- package/cli/dist/clients/mcp/run.js +69 -0
- package/cli/dist/clients/mcp/server.js +130 -0
- package/cli/dist/clients/mcp/tools.js +101 -0
- package/cli/dist/clients/wire/contract.js +53 -0
- package/cli/dist/clients/wire/schemas.js +68 -0
- package/cli/dist/clients/{api → wire}/status.js +7 -1
- package/cli/dist/clients/wire/words.js +48 -0
- package/cli/dist/core/adapters/network.js +20 -1
- package/cli/dist/core/fleet/brand.js +5 -0
- package/cli/dist/core/fleet/parse.js +10 -1
- package/cli/dist/core/fleet/settings.js +18 -3
- package/cli/dist/core/operations/dispatch.js +3 -3
- package/cli/dist/core/recipes/doctor.js +3 -3
- package/cli/dist/core/recipes/host.js +8 -3
- package/cli/dist/core/status/look.js +33 -13
- package/cli/dist/core/status/model.js +2 -0
- package/cli/dist/deployer/brand.js +5 -0
- package/cli/dist/deployer/build/builder.js +5 -0
- package/cli/dist/deployer/build/bundle.js +5 -0
- package/cli/dist/deployer/build/compose.js +21 -8
- package/cli/dist/deployer/build/routes.js +128 -0
- package/cli/dist/deployer/deployer.js +15 -13
- package/cli/dist/deployer/runs/queue.js +7 -0
- package/cli/dist/deployer/runs/run.js +14 -7
- package/cli/dist/main.js +11 -3
- package/package.json +3 -1
- package/schema/app.json +64 -8
- package/scripts/agent/guard.sh +28 -6
- package/scripts/local/app-check.sh +12 -9
- package/scripts/local/app-manifests.sh +11 -6
- package/scripts/local/verify.sh +22 -11
- package/scripts/platform/compose-policy.sh +37 -40
- package/cli/dist/browser/assets/index-hcyM6f6T.js +0 -7
- package/compose/traefik/dynamic/ratelimit.yml +0 -21
- /package/cli/dist/clients/{api → wire}/contract.gen.js +0 -0
- /package/cli/dist/clients/{api → wire}/queries.js +0 -0
- /package/cli/dist/clients/{api → wire}/validate.js +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yunuseon/kayra",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.19.0",
|
|
4
4
|
"description": "A self-hosted app platform and the CLI that runs it: Traefik, Authelia, a registry, rootless BuildKit and a small deployer per server, set up with OpenTofu and Ansible.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Yunus Öztürk",
|
|
@@ -44,6 +44,7 @@
|
|
|
44
44
|
"signatures": "rm -rf .cache/signatures && tsc -p cli/tsconfig.build.json --declaration --emitDeclarationOnly --outDir .cache/signatures"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
+
"@modelcontextprotocol/server": "2.3.1",
|
|
47
48
|
"@optique/core": "1.3.3",
|
|
48
49
|
"@optique/run": "1.3.3",
|
|
49
50
|
"yaml": "2.9.1",
|
|
@@ -51,6 +52,7 @@
|
|
|
51
52
|
},
|
|
52
53
|
"devDependencies": {
|
|
53
54
|
"@eslint/js": "10.0.1",
|
|
55
|
+
"@modelcontextprotocol/client": "2.3.1",
|
|
54
56
|
"@tsconfig/node24": "24.0.5",
|
|
55
57
|
"@tsconfig/strictest": "2.0.8",
|
|
56
58
|
"@types/node": "24.19.1",
|
package/schema/app.json
CHANGED
|
@@ -12,15 +12,72 @@
|
|
|
12
12
|
"items": {
|
|
13
13
|
"type": "string"
|
|
14
14
|
},
|
|
15
|
-
"description": "Compose files, relative to the app root, in order."
|
|
15
|
+
"description": "Compose files, relative to the app root, in order. No Traefik label, no platform network: routes say how the app is reached."
|
|
16
16
|
},
|
|
17
|
-
"
|
|
17
|
+
"routes": {
|
|
18
|
+
"description": "How the app is reached: each route a host (and path) to a service's port. Read at the commit being deployed; a new host needs kayra apps.",
|
|
18
19
|
"type": "array",
|
|
19
20
|
"items": {
|
|
20
|
-
"type": "
|
|
21
|
-
"
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
"type": "object",
|
|
22
|
+
"properties": {
|
|
23
|
+
"host": {
|
|
24
|
+
"description": "Its hostname, <host>.<domain>. The app's hosts are its routes' hosts.",
|
|
25
|
+
"type": "string",
|
|
26
|
+
"pattern": "^[a-z0-9]([a-z0-9-]*[a-z0-9])?$"
|
|
27
|
+
},
|
|
28
|
+
"path": {
|
|
29
|
+
"description": "A path prefix: the route answers below it, and beats its host's route without one.",
|
|
30
|
+
"type": "string",
|
|
31
|
+
"pattern": "^\\/[A-Za-z0-9._~%/-]*$"
|
|
32
|
+
},
|
|
33
|
+
"service": {
|
|
34
|
+
"description": "The compose service that answers it.",
|
|
35
|
+
"type": "string",
|
|
36
|
+
"pattern": "^[a-zA-Z0-9][a-zA-Z0-9_.-]*$"
|
|
37
|
+
},
|
|
38
|
+
"port": {
|
|
39
|
+
"type": "integer",
|
|
40
|
+
"minimum": 1,
|
|
41
|
+
"maximum": 65535,
|
|
42
|
+
"description": "The port that service listens on, inside its container."
|
|
43
|
+
},
|
|
44
|
+
"rate_limit": {
|
|
45
|
+
"description": "A rate limit on this route, per client address (Traefik's rateLimit).",
|
|
46
|
+
"type": "object",
|
|
47
|
+
"properties": {
|
|
48
|
+
"average": {
|
|
49
|
+
"type": "integer",
|
|
50
|
+
"exclusiveMinimum": 0,
|
|
51
|
+
"maximum": 9007199254740991,
|
|
52
|
+
"description": "Requests a client may send per period, on average."
|
|
53
|
+
},
|
|
54
|
+
"period": {
|
|
55
|
+
"description": "The period, such as 10s, 10m or 1h.",
|
|
56
|
+
"type": "string",
|
|
57
|
+
"pattern": "^[1-9][0-9]*(ms|s|m|h)$"
|
|
58
|
+
},
|
|
59
|
+
"burst": {
|
|
60
|
+
"type": "integer",
|
|
61
|
+
"exclusiveMinimum": 0,
|
|
62
|
+
"maximum": 9007199254740991,
|
|
63
|
+
"description": "Requests a client may send at once beyond the average."
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
"required": [
|
|
67
|
+
"average",
|
|
68
|
+
"period",
|
|
69
|
+
"burst"
|
|
70
|
+
],
|
|
71
|
+
"additionalProperties": false
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
"required": [
|
|
75
|
+
"host",
|
|
76
|
+
"service",
|
|
77
|
+
"port"
|
|
78
|
+
],
|
|
79
|
+
"additionalProperties": false
|
|
80
|
+
}
|
|
24
81
|
},
|
|
25
82
|
"secrets": {
|
|
26
83
|
"description": "Secret keys; their values come from the fleet's apps/<app>.secrets.yml.",
|
|
@@ -39,8 +96,7 @@
|
|
|
39
96
|
},
|
|
40
97
|
"required": [
|
|
41
98
|
"name",
|
|
42
|
-
"compose"
|
|
43
|
-
"subdomains"
|
|
99
|
+
"compose"
|
|
44
100
|
],
|
|
45
101
|
"additionalProperties": false,
|
|
46
102
|
"title": "app.yml"
|
package/scripts/agent/guard.sh
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env bash
|
|
2
2
|
# =============================================================================
|
|
3
|
-
# guard.sh — Claude Code's PreToolUse hook on Bash, in kayra and every fleet
|
|
3
|
+
# guard.sh — Claude Code's PreToolUse hook on Bash and kayra's MCP tools, in kayra and every fleet
|
|
4
4
|
# (.claude/settings.json): an AI agent may build, test and read freely, but a
|
|
5
5
|
# command that changes something real waits for the operator to approve it,
|
|
6
6
|
# in that moment.
|
|
@@ -21,6 +21,10 @@
|
|
|
21
21
|
# - git push of main or a tag, a force push, deleting a remote branch
|
|
22
22
|
# - tofu / terraform apply, destroy, import, state changes
|
|
23
23
|
# - destructive commands over SSH (rm, docker rm/stop/prune/down, systemctl…)
|
|
24
|
+
# kayra's MCP tools (mcp__kayra__<tool>, the fleet's .mcp.json naming the
|
|
25
|
+
# server kayra) the same way, read from their JSON input: a tool that changes
|
|
26
|
+
# a server asks unless dryRun is set or the server is a multipass one; one
|
|
27
|
+
# that changes nothing passes.
|
|
24
28
|
# Anything else: no output, the normal permission flow.
|
|
25
29
|
#
|
|
26
30
|
# It reads the command's text, so it stops mistakes and instructions smuggled
|
|
@@ -30,8 +34,6 @@
|
|
|
30
34
|
set -uo pipefail
|
|
31
35
|
|
|
32
36
|
input="$(cat)"
|
|
33
|
-
command="$(jq -r '.tool_input.command // empty' <<< "$input" 2>/dev/null)"
|
|
34
|
-
[ -n "$command" ] || exit 0
|
|
35
37
|
mode="$(jq -r '.permission_mode // empty' <<< "$input" 2>/dev/null)"
|
|
36
38
|
project="${CLAUDE_PROJECT_DIR:-$PWD}"
|
|
37
39
|
|
|
@@ -44,6 +46,28 @@ ask() {
|
|
|
44
46
|
'{hookSpecificOutput: {hookEventName: "PreToolUse", permissionDecision: $decision, permissionDecisionReason: $reason}}'
|
|
45
47
|
exit 0
|
|
46
48
|
}
|
|
49
|
+
# The target a server's server.yml names, if the server is one of this project's.
|
|
50
|
+
target_of() {
|
|
51
|
+
[[ "$1" =~ ^[a-z0-9][a-z0-9-]*$ ]] && [ -f "${project}/servers/$1/server.yml" ] || return 0
|
|
52
|
+
sed -n 's/^target: *\([a-z]*\).*/\1/p' "${project}/servers/$1/server.yml"
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
# ── kayra's MCP tools ───────────────────────────────────────────────────────
|
|
56
|
+
# The tools that change nothing (the catalog's effect "reads"; a test holds this list to it).
|
|
57
|
+
mcp_reads='status|validate|verify|plan|doctor|version|app_check|app_runs|app_log|app_releases'
|
|
58
|
+
tool="$(jq -r '.tool_name // empty' <<< "$input" 2>/dev/null)"
|
|
59
|
+
if [[ "$tool" == mcp__kayra__* ]]; then
|
|
60
|
+
name="${tool#mcp__kayra__}"
|
|
61
|
+
[[ "$name" =~ ^(${mcp_reads})$ ]] && exit 0
|
|
62
|
+
[ "$(jq -r '.tool_input.dryRun // false' <<< "$input" 2>/dev/null)" = true ] && exit 0
|
|
63
|
+
server="$(jq -r '.tool_input.server // empty | strings' <<< "$input" 2>/dev/null)"
|
|
64
|
+
target="$(target_of "$server")"
|
|
65
|
+
[ "$target" = multipass ] || ask "kayra (MCP) ${name} changes ${server:-this machine}${target:+ (target ${target})}"
|
|
66
|
+
exit 0
|
|
67
|
+
fi
|
|
68
|
+
|
|
69
|
+
command="$(jq -r '.tool_input.command // empty' <<< "$input" 2>/dev/null)"
|
|
70
|
+
[ -n "$command" ] || exit 0
|
|
47
71
|
has() { grep -qE -- "$1" <<< "$command"; }
|
|
48
72
|
|
|
49
73
|
# ── kayra changing a server ─────────────────────────────────────────────────
|
|
@@ -52,9 +76,7 @@ verbs='(up|provision|setup|deploy|apps|adopt|down|hosts|job|app (purge|deploy|ro
|
|
|
52
76
|
if has "${kayra} +(--[a-z-]+ +)*${verbs}( |$)" && ! has '--dry-run'; then
|
|
53
77
|
# The server: the word after the verb (after the app subcommand for app …).
|
|
54
78
|
server="$(grep -oE -- "${kayra} +(--[a-z-]+ +)*${verbs} +[a-z0-9][a-z0-9-]*" <<< "$command" | head -1 | awk '{print $NF}')"
|
|
55
|
-
target=""
|
|
56
|
-
[ -z "$server" ] || [ ! -f "${project}/servers/${server}/server.yml" ] \
|
|
57
|
-
|| target="$(sed -n 's/^target: *\([a-z]*\).*/\1/p' "${project}/servers/${server}/server.yml")"
|
|
79
|
+
target="$(target_of "$server")"
|
|
58
80
|
[ "$target" = multipass ] || ask "kayra changes ${server:-a server}${target:+ (target ${target})}"
|
|
59
81
|
fi
|
|
60
82
|
# kayra serve runs any command on any server from a browser, where this guard sees none of them.
|
|
@@ -4,13 +4,14 @@
|
|
|
4
4
|
# checked in the app's own repository, before a push: run by `kayra app
|
|
5
5
|
# check`, which has checked app.yml against its schema first.
|
|
6
6
|
#
|
|
7
|
-
# - app.yml's compose files exist, inside the app; its
|
|
8
|
-
# the platform's (hosts.conf)
|
|
7
|
+
# - app.yml's compose files exist, inside the app; its routes' hosts are
|
|
8
|
+
# not the platform's (hosts.conf)
|
|
9
9
|
# - every docker/image.json: its image in the app's namespace, a commit
|
|
10
10
|
# touching its inputs (resolve-images.sh --plan, as the builder runs it)
|
|
11
11
|
# - the compose config those files resolve to passes the compose policy
|
|
12
|
-
# (compose-policy.sh, as the deployer runs it
|
|
13
|
-
#
|
|
12
|
+
# (compose-policy.sh, as the deployer runs it: no Traefik label, no
|
|
13
|
+
# platform network, each route to one of its services), with placeholder
|
|
14
|
+
# values for the images, config and secrets, and hosts under example.test
|
|
14
15
|
#
|
|
15
16
|
# Needs git, jq, yq and the docker CLI with Compose (no daemon: `compose
|
|
16
17
|
# config` only reads files). Written for macOS's bash 3.2 too: no mapfile.
|
|
@@ -28,7 +29,8 @@ pass() { echo "✓ $*"; }
|
|
|
28
29
|
|
|
29
30
|
name="$(yq -r '.name' "$manifest")"
|
|
30
31
|
files="$(yq -r '.compose[]' "$manifest")"
|
|
31
|
-
|
|
32
|
+
routes="$(yq -o=json -I0 '.routes // []' "$manifest")"
|
|
33
|
+
subdomains="$(yq -r '.routes // [] | map(.host) | unique | .[]' "$manifest")"
|
|
32
34
|
reserved="$(awk '!/^[[:space:]]*(#|$)/ {print $1}' "${KAYRA_ROOT}/hosts.conf")"
|
|
33
35
|
|
|
34
36
|
# ── app.yml ─────────────────────────────────────────────────────────────────
|
|
@@ -39,9 +41,10 @@ while IFS= read -r file; do
|
|
|
39
41
|
esac
|
|
40
42
|
done <<< "$files"
|
|
41
43
|
while IFS= read -r sub; do
|
|
42
|
-
if grep -qxF -- "$sub" <<< "$reserved"; then fail "
|
|
44
|
+
if [ -n "$sub" ] && grep -qxF -- "$sub" <<< "$reserved"; then fail "route host ${sub} is the platform's"; fi
|
|
43
45
|
done <<< "$subdomains"
|
|
44
|
-
|
|
46
|
+
described="$(yq -r '.routes // [] | map(.host + (.path // "") + " → " + .service + ":" + (.port | tostring)) | join(", ")' "$manifest")"
|
|
47
|
+
[ "$failed" = 0 ] && pass "app.yml: compose $(paste -sd' ' - <<< "$files") · routes ${described:-none}"
|
|
45
48
|
|
|
46
49
|
# ── Images ──────────────────────────────────────────────────────────────────
|
|
47
50
|
if plan="$(bash "${KAYRA_ROOT}/scripts/platform/resolve-images.sh" "$name" "$root" --plan)"; then
|
|
@@ -71,8 +74,8 @@ while IFS= read -r file; do set -- "$@" -f "${root}/${file}"; done <<< "$files"
|
|
|
71
74
|
|| { fail "compose: docker compose config failed (above)"; exit 1; }
|
|
72
75
|
|
|
73
76
|
set --
|
|
74
|
-
while IFS= read -r sub; do set -- "$@" "${sub}.${domain}"; done <<< "$subdomains"
|
|
75
|
-
if violations="$(bash "${KAYRA_ROOT}/scripts/platform/compose-policy.sh" "$name" "$@" < "${work}/config.json")"; then
|
|
77
|
+
while IFS= read -r sub; do [ -z "$sub" ] || set -- "$@" "${sub}.${domain}"; done <<< "$subdomains"
|
|
78
|
+
if violations="$(bash "${KAYRA_ROOT}/scripts/platform/compose-policy.sh" "$name" "$domain" "$routes" "$@" < "${work}/config.json")"; then
|
|
76
79
|
pass "compose policy: admitted"
|
|
77
80
|
else
|
|
78
81
|
while IFS= read -r line; do fail "compose policy: ${line}"; done <<< "$violations"
|
|
@@ -4,14 +4,15 @@
|
|
|
4
4
|
# app role reads). A missing clone is
|
|
5
5
|
# fetched with the operator's own git credentials (kayra doctor checks them).
|
|
6
6
|
#
|
|
7
|
-
# Usage: FLEET_ROOT=<fleet> bash scripts/local/app-manifests.sh [--subdomains] <server-name>
|
|
8
|
-
# --subdomains print the apps'
|
|
7
|
+
# Usage: FLEET_ROOT=<fleet> bash scripts/local/app-manifests.sh [--subdomains|--routes] <server-name>
|
|
8
|
+
# --subdomains print the apps' hosts instead, their routes' (for certs and /etc/hosts)
|
|
9
|
+
# --routes print each route as "<subdomain> <path>", / where it has none (for verify)
|
|
9
10
|
set -euo pipefail
|
|
10
11
|
|
|
11
12
|
FLEET_ROOT="${FLEET_ROOT:?the directory of the fleet (servers/), set by kayra}"
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
name="${1:?Usage: app-manifests.sh [--subdomains] <server-name>}"
|
|
13
|
+
mode=manifests
|
|
14
|
+
case "${1:-}" in --subdomains) mode=subdomains; shift ;; --routes) mode=routes; shift ;; esac
|
|
15
|
+
name="${1:?Usage: app-manifests.sh [--subdomains|--routes] <server-name>}"
|
|
15
16
|
descriptor="${FLEET_ROOT}/servers/${name}/server.yml"
|
|
16
17
|
|
|
17
18
|
yq -o json -I0 '.apps // [] | .[]' "$descriptor" | while read -r app; do
|
|
@@ -22,5 +23,9 @@ yq -o json -I0 '.apps // [] | .[]' "$descriptor" | while read -r app; do
|
|
|
22
23
|
git clone -q --depth 1 --branch "$(jq -r '.ref // "main"' <<< "$app")" "$repo" "$clone"
|
|
23
24
|
fi
|
|
24
25
|
manifest="${clone}/${path}/app.yml"
|
|
25
|
-
|
|
26
|
+
case "$mode" in
|
|
27
|
+
subdomains) yq '.routes // [] | map(.host) | unique | .[]' "$manifest" ;;
|
|
28
|
+
routes) yq '.routes // [] | map(.host + " " + (.path // "/")) | unique | .[]' "$manifest" ;;
|
|
29
|
+
*) echo "$manifest" ;;
|
|
30
|
+
esac
|
|
26
31
|
done
|
package/scripts/local/verify.sh
CHANGED
|
@@ -9,11 +9,14 @@
|
|
|
9
9
|
# browser to the login, not a 403 from rules Authelia never loaded
|
|
10
10
|
# - every certificate is a real one (Let's Encrypt or mkcert), never
|
|
11
11
|
# Traefik's default
|
|
12
|
-
# - an unsigned webhook is refused; every app
|
|
12
|
+
# - an unsigned webhook is refused; every app route answers, not 5xx
|
|
13
13
|
# On the server (one SSH call):
|
|
14
14
|
# - every app in server.yml is registered, and nothing else
|
|
15
15
|
# - every app's containers run, none unhealthy; its last run did not fail
|
|
16
16
|
# - every platform container runs, none unhealthy; no alert is open
|
|
17
|
+
# - which kayra set it up (/opt/kayra/version): one that is not this kayra
|
|
18
|
+
# (KAYRA_VERSION, the fleet's pin) is noted, not failed — it still works,
|
|
19
|
+
# and every server runs the last release until its rollout reaches it
|
|
17
20
|
#
|
|
18
21
|
# Certificates and routes can lag a fresh deploy (ACME, DNS-01's minute), so
|
|
19
22
|
# the probes retry for VERIFY_WAIT seconds (default 150) before failing.
|
|
@@ -23,6 +26,7 @@ set -euo pipefail
|
|
|
23
26
|
|
|
24
27
|
KAYRA_ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
|
|
25
28
|
FLEET_ROOT="${FLEET_ROOT:?the directory of the fleet (servers/), set by kayra}"
|
|
29
|
+
pin="${KAYRA_VERSION:?the kayra that runs, set by kayra}"
|
|
26
30
|
name="${1:?Usage: verify.sh <server>}"
|
|
27
31
|
dir="${FLEET_ROOT}/servers/${name}"
|
|
28
32
|
wait="${VERIFY_WAIT:-150}"
|
|
@@ -30,6 +34,7 @@ failed=0
|
|
|
30
34
|
|
|
31
35
|
fail() { echo " ✗ $*"; failed=1; }
|
|
32
36
|
pass() { echo " ✓ $*"; }
|
|
37
|
+
note() { echo " ! $*"; }
|
|
33
38
|
|
|
34
39
|
domain="$(yq -r '.domain' "${dir}/server.yml")"
|
|
35
40
|
inventory="${dir}/inventory.ini"
|
|
@@ -42,12 +47,12 @@ key="$(field ansible_ssh_private_key_file)"
|
|
|
42
47
|
registered_wanted="$(yq -r '.apps // [] | .[].name' "${dir}/server.yml")"
|
|
43
48
|
|
|
44
49
|
# ── From outside ────────────────────────────────────────────────────────────
|
|
45
|
-
# "<status> <redirect> <verify result> <issuer CN>" for a host, pinned to the server.
|
|
50
|
+
# "<status> <redirect> <verify result> <issuer CN>" for a host (and path), pinned to the server.
|
|
46
51
|
probe() {
|
|
47
|
-
local host="$1" method="${2:-GET}" result issuer
|
|
52
|
+
local host="$1" method="${2:-GET}" path="${3:-/}" result issuer
|
|
48
53
|
result="$(curl -s -o /dev/null -m 10 -X "$method" -H 'Accept: text/html' \
|
|
49
54
|
--resolve "${host}:443:${address}" -w '%{http_code} %{redirect_url} %{ssl_verify_result}' \
|
|
50
|
-
"https://${host}
|
|
55
|
+
"https://${host}${path}" 2>/dev/null || echo "000 - -")"
|
|
51
56
|
issuer="$(openssl s_client -connect "${address}:443" -servername "$host" </dev/null 2>/dev/null \
|
|
52
57
|
| openssl x509 -noout -issuer 2>/dev/null | sed -n 's/.*CN *= *//p')"
|
|
53
58
|
echo "${result} ${issuer:-none}"
|
|
@@ -55,10 +60,10 @@ probe() {
|
|
|
55
60
|
|
|
56
61
|
# Probes until `test` accepts the answer or VERIFY_WAIT runs out; prints the last answer.
|
|
57
62
|
until_ok() {
|
|
58
|
-
local host="$1" method="$2" test="$3" deadline answer
|
|
63
|
+
local host="$1" method="$2" test="$3" path="${4:-/}" deadline answer
|
|
59
64
|
deadline=$(( $(date +%s) + wait ))
|
|
60
65
|
while :; do
|
|
61
|
-
answer="$(probe "$host" "$method")"
|
|
66
|
+
answer="$(probe "$host" "$method" "$path")"
|
|
62
67
|
if "$test" "$answer"; then echo "$answer"; return 0; fi
|
|
63
68
|
[ "$(date +%s)" -lt "$deadline" ] || { echo "$answer"; return 1; }
|
|
64
69
|
sleep 10
|
|
@@ -74,8 +79,8 @@ refused() { real_cert "$1" && [ "$(cut -d' ' -f1 <<< "$1")" = 401 ]; }
|
|
|
74
79
|
describe() { echo "HTTP $(cut -d' ' -f1 <<< "$1"), certificate by $(cut -d' ' -f4- <<< "$1") (verify $(cut -d' ' -f3 <<< "$1"))"; }
|
|
75
80
|
|
|
76
81
|
check() {
|
|
77
|
-
local label="$1" host="$2" method="$3" test="$4" answer
|
|
78
|
-
if answer="$(until_ok "$host" "$method" "$test")"; then pass "$label"; else fail "${label}: $(describe "$answer")"; fi
|
|
82
|
+
local label="$1" host="$2" method="$3" test="$4" path="${5:-/}" answer
|
|
83
|
+
if answer="$(until_ok "$host" "$method" "$test" "$path")"; then pass "$label"; else fail "${label}: $(describe "$answer")"; fi
|
|
79
84
|
}
|
|
80
85
|
|
|
81
86
|
check "auth.${domain} answers" "auth.${domain}" GET answers
|
|
@@ -91,9 +96,9 @@ if [ -n "$first_app" ]; then
|
|
|
91
96
|
}
|
|
92
97
|
code="$(hook)"
|
|
93
98
|
[ "$code" = 401 ] && pass "an unsigned webhook is refused" || fail "an unsigned webhook to /hook/${first_app}: HTTP ${code}, not 401"
|
|
94
|
-
while
|
|
95
|
-
[ -z "$sub" ] || check "${sub}.${domain} answers" "${sub}.${domain}" GET not_5xx
|
|
96
|
-
done < <(bash "${KAYRA_ROOT}/scripts/local/app-manifests.sh" --
|
|
99
|
+
while read -r sub path; do
|
|
100
|
+
[ -z "$sub" ] || check "${sub}.${domain}${path%/} answers" "${sub}.${domain}" GET not_5xx "$path"
|
|
101
|
+
done < <(bash "${KAYRA_ROOT}/scripts/local/app-manifests.sh" --routes "$name")
|
|
97
102
|
fi
|
|
98
103
|
|
|
99
104
|
# ── On the server ───────────────────────────────────────────────────────────
|
|
@@ -102,6 +107,7 @@ facts="$(ssh -p "${port:-22}" -i "$key" -o StrictHostKeyChecking=no -o UserKnown
|
|
|
102
107
|
cd /opt/platform
|
|
103
108
|
echo "registered $(ls apps 2>/dev/null | sed -n "s/\.json$//p" | sort | tr "\n" " ")"
|
|
104
109
|
echo "alerts $(ls alerts 2>/dev/null | tr "\n" " ")"
|
|
110
|
+
echo "kayra $(cat /opt/kayra/version 2>/dev/null || true)"
|
|
105
111
|
docker compose ps -a --format "{{.Service}}|{{.State}}|{{.Health}}" | sed "s/^/platform /"
|
|
106
112
|
for app in $(ls apps 2>/dev/null | sed -n "s/\.json$//p"); do
|
|
107
113
|
docker ps -a --filter "label=com.docker.compose.project=$app" --format "{{.Names}}|{{.State}}|{{.Status}}" | sed "s/^/container $app /"
|
|
@@ -116,6 +122,11 @@ have="$(xargs <<< "$have")"
|
|
|
116
122
|
[ "$wanted" = "$have" ] && pass "registered: ${have:-no apps}" \
|
|
117
123
|
|| fail "registered: ${have:-nothing}; server.yml says ${wanted:-no apps} (kayra apps ${name})"
|
|
118
124
|
|
|
125
|
+
running="$(sed -n 's/^kayra //p' <<< "$facts" | xargs)"
|
|
126
|
+
if [ "$running" = "$pin" ]; then pass "set up by kayra ${pin}, the fleet's pin"
|
|
127
|
+
elif [ -z "$running" ]; then note "set up by a kayra that recorded no version; the fleet pins ${pin} (kayra deploy ${name})"
|
|
128
|
+
else note "set up by kayra ${running}; the fleet pins ${pin} (kayra deploy ${name})"; fi
|
|
129
|
+
|
|
119
130
|
alerts="$(sed -n 's/^alerts //p' <<< "$facts" | xargs)"
|
|
120
131
|
[ -z "$alerts" ] && pass "no alert is open" || fail "open alerts: ${alerts}"
|
|
121
132
|
|
|
@@ -1,31 +1,41 @@
|
|
|
1
1
|
#!/usr/bin/env bash
|
|
2
2
|
# =============================================================================
|
|
3
|
-
# compose-policy.sh <app> <allowed-host>... < config.json
|
|
3
|
+
# compose-policy.sh <app> <domain> <routes> <allowed-host>... < config.json
|
|
4
4
|
#
|
|
5
5
|
# The platform's admission check, run by the deployer before every
|
|
6
6
|
# `docker compose up`. Input is `docker compose config --no-env-resolution
|
|
7
|
-
# --format json
|
|
8
|
-
# defaults cannot hide anything. Env files stay
|
|
9
|
-
# deployer would read them from its own filesystem,
|
|
10
|
-
# included. Prints one line per violation and exits 1 if
|
|
7
|
+
# --format json` of the app's own compose files: the fully resolved config, so
|
|
8
|
+
# variables, overlays and defaults cannot hide anything. Env files stay
|
|
9
|
+
# unresolved on purpose: the deployer would read them from its own filesystem,
|
|
10
|
+
# platform secrets included. Prints one line per violation and exits 1 if
|
|
11
|
+
# there is any.
|
|
11
12
|
#
|
|
12
13
|
# An allow-list: a service key the platform has not thought about is refused,
|
|
13
14
|
# rather than a new Compose feature quietly opening a hole. Modelled on
|
|
14
15
|
# Kubernetes' Pod Security "restricted" profile, adapted to what matters on a
|
|
15
16
|
# shared Docker host: an app must not reach the host, other apps, or the
|
|
16
|
-
# platform, must
|
|
17
|
-
#
|
|
18
|
-
#
|
|
19
|
-
#
|
|
17
|
+
# platform, and must declare how much memory it may take. Running as root
|
|
18
|
+
# inside the container is allowed (unlike restricted): without capabilities or
|
|
19
|
+
# host access it gets nothing a non-root user would not.
|
|
20
|
+
#
|
|
21
|
+
# Routing is the platform's: the app's routes (app.yml `routes`, as JSON) are
|
|
22
|
+
# checked here against its config, each to one of its services on one of its
|
|
23
|
+
# registered hosts, and the deployer writes the Traefik labels and the
|
|
24
|
+
# platform network from them after admission. So an app's own files carry
|
|
25
|
+
# neither: no `traefik.*` label, no `platform-*` network.
|
|
20
26
|
# =============================================================================
|
|
21
27
|
set -euo pipefail
|
|
22
28
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
29
|
+
usage="Usage: compose-policy.sh <app> <domain> <routes> <allowed-host>... < config.json"
|
|
30
|
+
app="${1:?$usage}"
|
|
31
|
+
domain="${2:?$usage}"
|
|
32
|
+
routes="${3:?$usage}"
|
|
33
|
+
shift 3
|
|
34
|
+
hosts=$(printf '%s\n' "$@" | jq -R 'select(. != "")' | jq -s .)
|
|
26
35
|
|
|
27
|
-
jq -r --arg app "$app" --argjson hosts "$hosts" '
|
|
36
|
+
jq -r --arg app "$app" --arg domain "$domain" --argjson routes "$routes" --argjson hosts "$hosts" '
|
|
28
37
|
def own($name): $name | startswith($app + "_");
|
|
38
|
+
def platform($name): $name | startswith("platform-");
|
|
29
39
|
def registry_ref: test("^(localhost|registry):5000/");
|
|
30
40
|
def allowed: ["image", "command", "entrypoint", "environment", "labels",
|
|
31
41
|
"networks", "volumes", "tmpfs", "healthcheck", "depends_on", "restart",
|
|
@@ -42,43 +52,30 @@ jq -r --arg app "$app" --argjson hosts "$hosts" '
|
|
|
42
52
|
(if ($s.mem_limit // $s.deploy.resources.limits.memory) == null
|
|
43
53
|
then "no memory limit (set mem_limit)" else empty end),
|
|
44
54
|
($s.networks // {} | to_entries[] | .key as $net | .value // {} |
|
|
45
|
-
(keys - ["aliases"] | .[] | "networks.\($net).\(.) is not allowed"),
|
|
46
|
-
(select($root.networks[$net].name == "platform-public") | .aliases // [] | .[]
|
|
47
|
-
| select(startswith($app + "-") | not)
|
|
48
|
-
| "alias \(.) on platform-public must start with \($app)-")),
|
|
55
|
+
(keys - ["aliases"] | .[] | "networks.\($net).\(.) is not allowed")),
|
|
49
56
|
($s.volumes // [] | .[] | select(.type != "volume" and .type != "tmpfs")
|
|
50
57
|
| "\(.type) mount \(.source // "") -> \(.target) (named volumes only)"),
|
|
51
58
|
(if ($s.image // "") | registry_ref and (test("^[^/]+/" + $app + "/") | not)
|
|
52
59
|
then "image \($s.image) is outside the \($app)/ namespace" else empty end),
|
|
53
|
-
($s.labels // {} |
|
|
54
|
-
|
|
55
|
-
(select(.key | test("^traefik\\.http\\.(routers|services|middlewares)\\."))
|
|
56
|
-
| (.key | split(".")[3]) as $name
|
|
57
|
-
| select($name | startswith($app + "-") | not)
|
|
58
|
-
| "\(.key): Traefik names must start with \($app)-"),
|
|
59
|
-
(select(.value | test("@internal")) | "\(.key) references an @internal service"),
|
|
60
|
-
(select(.key | test("^traefik\\.http\\.routers\\.[^.]+\\.middlewares$"))
|
|
61
|
-
| select(.value | split(",") | map(gsub("\\s"; "")) | index("authelia@file"))
|
|
62
|
-
| "\(.key): authelia@file is the platform'"'"'s login; apps bring their own"),
|
|
63
|
-
(select(.key | test("^traefik\\.http\\.routers\\.[^.]+\\.rule$")) | .key as $k | .value as $rule |
|
|
64
|
-
([$rule | scan("Host\\(`([^`]*)`\\)") | .[0]]) as $claimed |
|
|
65
|
-
if ($rule | test("HostRegexp|HostSNI")) then "\($k): only Host() rules are allowed"
|
|
66
|
-
elif ($claimed | length) == 0 then "\($k): rule must name a Host()"
|
|
67
|
-
else ($claimed[] | select(. as $h | $hosts | index($h) | not)
|
|
68
|
-
| "\($k): host \(.) is not one of this app'"'"'s")
|
|
69
|
-
end)
|
|
70
|
-
)
|
|
60
|
+
($s.labels // {} | keys[] | select(ascii_downcase | startswith("traefik."))
|
|
61
|
+
| "label \(.): Traefik labels are the platform'"'"'s; declare routes in app.yml")
|
|
71
62
|
) | "service \($svc): \(.)"
|
|
72
63
|
),
|
|
73
64
|
(.networks // {} | to_entries[] | .value as $n |
|
|
74
|
-
(
|
|
75
|
-
|
|
76
|
-
(
|
|
77
|
-
|
|
65
|
+
if platform(.key) or platform($n.name // "")
|
|
66
|
+
then "network \(.key): platform networks are the platform'"'"'s; it attaches routed services itself"
|
|
67
|
+
elif own($n.name // "") | not
|
|
68
|
+
then "network \(.key): \($n.name // "?") is not the app'"'"'s own"
|
|
69
|
+
else empty end),
|
|
78
70
|
(.volumes // {} | to_entries[] | .value as $v |
|
|
79
71
|
(select($v.external) | "volume \(.key): external volumes are not allowed"),
|
|
80
72
|
(select(($v.driver_opts // {}) | length > 0) | "volume \(.key): driver_opts are not allowed"),
|
|
81
73
|
(select(own($v.name // "") | not) | "volume \(.key): \($v.name) belongs to another project")),
|
|
82
74
|
(.secrets // {}, .configs // {} | to_entries[] | select(.value.file)
|
|
83
|
-
| "\(.key): file-backed secrets/configs read the host filesystem")
|
|
75
|
+
| "\(.key): file-backed secrets/configs read the host filesystem"),
|
|
76
|
+
($routes | to_entries[] | .value as $r | "route \($r.host)\($r.path // "")" as $route |
|
|
77
|
+
(select($root.services[$r.service] == null)
|
|
78
|
+
| "\($route): no service \($r.service) in the compose files"),
|
|
79
|
+
(select($hosts | index("\($r.host).\($domain)") | not)
|
|
80
|
+
| "\($route): \($r.host).\($domain) is not one of this app'"'"'s hosts (kayra apps registers new ones)"))
|
|
84
81
|
' | { violations=$(cat); [ -z "$violations" ] || { echo "$violations"; exit 1; }; }
|