@yunuseon/kayra 0.20.1 → 0.21.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.
Files changed (50) hide show
  1. package/README.md +2 -2
  2. package/agent/skills/kayra-rollout/SKILL.md +1 -1
  3. package/ansible/.ansible-lint +2 -2
  4. package/ansible/playbooks/app.yml +1 -1
  5. package/ansible/playbooks/edge.yml +1 -1
  6. package/ansible/playbooks/nas.yml +1 -1
  7. package/ansible/playbooks/platform.yml +2 -2
  8. package/ansible/playbooks/restore-test.yml +24 -0
  9. package/ansible/roles/app/tasks/main.yml +20 -28
  10. package/ansible/roles/forgejo-config/tasks/main.yml +1 -1
  11. package/ansible/roles/platform/defaults/main.yml +1 -1
  12. package/ansible/roles/platform/tasks/main.yml +1 -1
  13. package/cli/dist/app-hook.js +6 -13
  14. package/cli/dist/app-launch.js +1 -1
  15. package/cli/dist/app-values.js +1 -1
  16. package/cli/dist/clients/mcp/run.js +1 -1
  17. package/cli/dist/clients/mcp/server.js +1 -1
  18. package/cli/dist/core/adapters/sops.js +19 -0
  19. package/cli/dist/core/app-values/store.js +46 -57
  20. package/cli/dist/core/fleet/brand.js +2 -2
  21. package/cli/dist/core/fleet/docs.js +1 -1
  22. package/cli/dist/core/fleet/parse.js +19 -29
  23. package/cli/dist/core/fleet/secrets.js +59 -0
  24. package/cli/dist/core/fleet/settings.js +5 -6
  25. package/cli/dist/core/interpreter/loop.js +8 -3
  26. package/cli/dist/core/interpreter/needs.js +54 -26
  27. package/cli/dist/core/interpreter/process.js +2 -2
  28. package/cli/dist/core/operations/catalog.js +1 -1
  29. package/cli/dist/core/operations/dispatch.js +1 -1
  30. package/cli/dist/core/plan/describe.js +2 -2
  31. package/cli/dist/core/recipes/app-hook.js +2 -2
  32. package/cli/dist/core/recipes/app-values.js +1 -2
  33. package/cli/dist/core/recipes/apps.js +5 -6
  34. package/cli/dist/core/recipes/backup.js +2 -15
  35. package/cli/dist/core/recipes/doctor.js +8 -11
  36. package/cli/dist/core/recipes/host.js +11 -11
  37. package/cli/dist/core/recipes/launch.js +3 -3
  38. package/cli/dist/core/recipes/steps.js +7 -20
  39. package/cli/dist/core/validation/facts.js +37 -27
  40. package/cli/dist/core/validation/findings.js +23 -4
  41. package/compose/authelia/configuration.yml +1 -1
  42. package/package.json +1 -1
  43. package/schema/app-values.json +1 -1
  44. package/schema/app.json +1 -1
  45. package/schema/server.json +2 -7
  46. package/scripts/local/authelia-dev-init.sh +2 -2
  47. package/scripts/server/app-restore +39 -0
  48. package/terraform/modules/platform-dns/tests/fleet/servers/nested/server.yml +0 -1
  49. package/terraform/modules/platform-dns/tests/fleet/servers/shop/server.yml +0 -1
  50. package/terraform/modules/platform-dns/tests/fleet/servers/uncloned/server.yml +0 -1
package/README.md CHANGED
@@ -14,7 +14,7 @@ kayra is the engine that turns that description into running machines.
14
14
  ┌──────────────────────────────┐ ┌───────────────────────┐ ┌──────────────────┐
15
15
  │ CLI: kayra up/setup/apps/… │◀─────│ servers/<name>/ │ │ app.yml │
16
16
  │ Ansible, OpenTofu, compose │ pins │ server.yml, vars, │ │ Dockerfiles │
17
- │ the deployer: build, deploy │ │ vault secrets │ └────────┬─────────┘
17
+ │ the deployer: build, deploy │ │ secrets (SOPS) │ └────────┬─────────┘
18
18
  │ compose admission policy │ │ package.json → kayra │ │ built & deployed
19
19
  └──────────────────────────────┘ └───────────────────────┘ ▼ by the platform
20
20
  ```
@@ -29,7 +29,7 @@ kayra is the engine that turns that description into running machines.
29
29
  pipeline, no Ansible. It runs without the platform locally and can leave
30
30
  with its customer at any time.
31
31
  - **One server per customer, reproducible.** Everything about a server lives in
32
- its folder of the fleet; secrets per server in Ansible Vault; machines from
32
+ its folder of the fleet; secrets per server in SOPS, reviewable and checked without the key; machines from
33
33
  OpenTofu; every version pinned and kept current by Renovate.
34
34
  - **Small and self-hosted.** No cloud provider beyond a VPS; it runs on a
35
35
  single Hetzner box or at home.
@@ -44,7 +44,7 @@ In a worktree of the fleet:
44
44
 
45
45
  ```bash
46
46
  npm install --prefix "$W" --silent -D -E @yunuseon/kayra@<version>
47
- # …the fleet changes the release asks for (vault edits: decrypt in memory, never print a value)
47
+ # …the fleet changes the release asks for (secrets.yml edits: sops set, never print a value)
48
48
  cd "$W" && kayra validate
49
49
  ```
50
50
 
@@ -1,7 +1,7 @@
1
1
  # ansible-lint, run by the platform checks (ci/checks).
2
2
  skip_list:
3
- # Variable names are the contract with every server's vars.yml (several
4
- # vaulted) and are shared across roles (domain, apps_dir); prefixing them
3
+ # Variable names are the contract with every server's vars.yml and
4
+ # secrets.yml, and are shared across roles (domain, apps_dir); prefixing them
5
5
  # per role would rename that contract for nothing.
6
6
  - var-naming[no-role-prefix]
7
7
  # edge-proxy predates the check; renaming a role is churn, not safety.
@@ -15,7 +15,7 @@
15
15
  vars_files:
16
16
  - "{{ server_dir }}/../defaults.yml"
17
17
  - "{{ server_dir }}/vars.yml"
18
- - "{{ server_dir }}/secrets.yml"
18
+ - "{{ lookup('ansible.builtin.env', 'KAYRA_SECRETS', default=undef('kayra opens secrets.yml: run this playbook through kayra')) }}"
19
19
 
20
20
  roles:
21
21
  - app
@@ -16,7 +16,7 @@
16
16
  vars_files:
17
17
  - "{{ server_dir }}/../defaults.yml"
18
18
  - "{{ server_dir }}/vars.yml"
19
- - "{{ server_dir }}/secrets.yml"
19
+ - "{{ lookup('ansible.builtin.env', 'KAYRA_SECRETS', default=undef('kayra opens secrets.yml: run this playbook through kayra')) }}"
20
20
 
21
21
  roles:
22
22
  - common
@@ -16,7 +16,7 @@
16
16
  vars_files:
17
17
  - "{{ server_dir }}/../defaults.yml"
18
18
  - "{{ server_dir }}/vars.yml"
19
- - "{{ server_dir }}/secrets.yml"
19
+ - "{{ lookup('ansible.builtin.env', 'KAYRA_SECRETS', default=undef('kayra opens secrets.yml: run this playbook through kayra')) }}"
20
20
 
21
21
  vars:
22
22
  kayra_dir: "{{ nas_root }}/.kayra"
@@ -24,7 +24,7 @@
24
24
  vars_files:
25
25
  - "{{ server_dir }}/../defaults.yml"
26
26
  - "{{ server_dir }}/vars.yml"
27
- - "{{ server_dir }}/secrets.yml"
27
+ - "{{ lookup('ansible.builtin.env', 'KAYRA_SECRETS', default=undef('kayra opens secrets.yml: run this playbook through kayra')) }}"
28
28
 
29
29
  roles:
30
30
  - common
@@ -41,7 +41,7 @@
41
41
  vars_files:
42
42
  - "{{ server_dir }}/../defaults.yml"
43
43
  - "{{ server_dir }}/vars.yml"
44
- - "{{ server_dir }}/secrets.yml"
44
+ - "{{ lookup('ansible.builtin.env', 'KAYRA_SECRETS', default=undef('kayra opens secrets.yml: run this playbook through kayra')) }}"
45
45
 
46
46
  roles:
47
47
  - platform
@@ -0,0 +1,24 @@
1
+ ---
2
+ # =============================================================================
3
+ # Restore test — starts the restic-backup role's restore test on the server,
4
+ # detached: the latest backup, restored to a scratch dir and checked. kayra
5
+ # follows its log over SSH afterwards.
6
+ #
7
+ # Usage: kayra backup test <server>
8
+ # =============================================================================
9
+
10
+ - name: Start the restore test
11
+ hosts: servers
12
+ become: true
13
+ gather_facts: false
14
+
15
+ # ansible_become_password, where the server has one (a NAS).
16
+ vars_files:
17
+ - "{{ server_dir }}/vars.yml"
18
+ - "{{ lookup('ansible.builtin.env', 'KAYRA_SECRETS', default=undef('kayra opens secrets.yml: run this playbook through kayra')) }}"
19
+
20
+ tasks:
21
+ - name: Start the restore test
22
+ ansible.builtin.command:
23
+ cmd: docker compose -f {{ restic_backup.dir }}/docker-compose.yml exec -d -T backup /etc/backup/restore-test.sh
24
+ changed_when: true
@@ -2,10 +2,12 @@
2
2
  # =============================================================================
3
3
  # App role — the same for every app. Reads the app's app.yml and the
4
4
  # operator's values for it, servers/<server>/apps/<app>.yml (config,
5
- # plaintext) and <app>.secrets.yml (secrets, vault on real servers), and
5
+ # plaintext) and app_secrets.<app> in its secrets.yml (SOPS), and
6
6
  # registers the app:
7
7
  # {{ apps_dir }}/<app>/.env operator config for compose
8
8
  # app-apply secrets → app-secrets/<app>/
9
+ # app-restore a deployed app that runs
10
+ # nothing, deployed again
9
11
  # {{ platform_dir }}/apps/<app>.json the registration the deployer
10
12
  # and the builder read
11
13
  #
@@ -43,16 +45,17 @@
43
45
  file: "{{ app_root }}/app.yml"
44
46
  name: manifest
45
47
 
46
- # Either file may be missing: an app without config or without secrets.
47
- - name: Read the operator's values for this app
48
+ # Either may be missing: an app without config or without secrets.
49
+ - name: Read the operator's config for this app
48
50
  ansible.builtin.include_vars:
49
- file: "{{ server_dir }}/apps/{{ app.name }}{{ item.suffix }}"
50
- name: "{{ item.name }}"
51
- when: (server_dir ~ '/apps/' ~ app.name ~ item.suffix) is file
52
- loop:
53
- - {name: app_config, suffix: .yml}
54
- - {name: app_secrets, suffix: .secrets.yml}
55
- loop_control: {label: "{{ app.name }}{{ item.suffix }}"}
51
+ file: "{{ server_dir }}/apps/{{ app.name }}.yml"
52
+ name: app_config
53
+ when: (server_dir ~ '/apps/' ~ app.name ~ '.yml') is file
54
+
55
+ - name: Take this app's secrets from secrets.yml
56
+ ansible.builtin.set_fact:
57
+ app_secret_values: "{{ (app_secrets | default({}))[app.name] | default({}) }}"
58
+ no_log: true
56
59
 
57
60
  # An app's hosts are its routes' hosts; the routes themselves are read at
58
61
  # each commit, by the builder.
@@ -72,11 +75,11 @@
72
75
  {{ app.name }}: app.yml name={{ manifest.name }};
73
76
  {{ 'subdomains is gone: declare routes instead (the app contract);' if manifest.subdomains is defined else '' }}
74
77
  missing config {{ missing_config }} in servers/{{ server }}/apps/{{ app.name }}.yml,
75
- missing secrets {{ missing_secrets }} in servers/{{ server }}/apps/{{ app.name }}.secrets.yml;
78
+ missing secrets {{ missing_secrets }} in servers/{{ server }}/secrets.yml under app_secrets.{{ app.name }};
76
79
  hosts reserved by the platform {{ reserved_subdomains }}
77
80
  quiet: true
78
81
  vars:
79
- missing_secrets: "{{ manifest.secrets | default([]) | difference(app_secrets | default({}) | list) }}"
82
+ missing_secrets: "{{ manifest.secrets | default([]) | difference(app_secret_values | list) }}"
80
83
  missing_config: "{{ manifest.config | default([]) | difference(app_config | default({}) | list) }}"
81
84
  reserved_subdomains: "{{ app_subdomains | intersect(lookup('file', kayra_root ~ '/hosts.conf').splitlines() | reject('match', '^\\s*(#|$)') | list) }}"
82
85
 
@@ -102,7 +105,7 @@
102
105
  - name: Apply secrets
103
106
  ansible.builtin.command:
104
107
  cmd: "{{ platform_dir }}/bin/app-apply {{ app.name }}"
105
- stdin: "{% for key in manifest.secrets | default([]) %}{{ key }}={{ app_secrets[key] }}\n{% endfor %}"
108
+ stdin: "{% for key in manifest.secrets | default([]) %}{{ key }}={{ app_secret_values[key] }}\n{% endfor %}"
106
109
  no_log: true
107
110
  become: true
108
111
  changed_when: true
@@ -127,22 +130,11 @@
127
130
  become: true
128
131
 
129
132
  # A Docker update or a reboot stops every container, and one deployed before
130
- # the platform gave each service a restart policy stays down: its live
131
- # release, deployed again. ctl follows the run to its end; the retries wait
132
- # out a deployer the host layer has just restarted.
133
+ # the platform gave each service a restart policy stays down (bin/app-restore).
134
+ # The retries wait out a deployer the host layer has just restarted.
133
135
  - name: Restore the app if it was deployed but runs nothing
134
- ansible.builtin.shell:
135
- cmd: |
136
- set -euo pipefail
137
- releases={{ platform_dir }}/releases/{{ app.name }}
138
- if [ ! -d "$releases" ]; then echo "never deployed"; exit 0; fi
139
- live="$(find "$releases" -maxdepth 1 -name '*.env' -printf '%f\n' | sort | tail -1 | sed -n 's/^[0-9]\{8\}T[0-9]\{6\}Z-\([0-9a-f]\{40\}\)\.env$/\1/p')"
140
- if [ -z "$live" ]; then echo "never deployed"; exit 0; fi
141
- if [ -n "$(docker ps -q --filter label=com.docker.compose.project={{ app.name }} --filter status=running --filter status=restarting)" ]; then echo "runs"; exit 0; fi
142
- cd {{ platform_dir }}
143
- docker compose exec -T deployer kayra-deployer ctl deploy {{ app.name }} "$live"
144
- echo "restored ${live}"
145
- executable: /bin/bash
136
+ ansible.builtin.command:
137
+ cmd: "{{ platform_dir }}/bin/app-restore {{ app.name }}"
146
138
  register: app_restored
147
139
  retries: 5
148
140
  delay: 3
@@ -320,7 +320,7 @@
320
320
  no_log: true
321
321
 
322
322
  # Tokens for the platform's services on this server (builder and deployer, Renovate), into
323
- # the files the platform role would otherwise fill from the vault. Nobody ever
323
+ # the files the platform role would otherwise fill from secrets.yml. Nobody ever
324
324
  # sees one: kept only in that file, minted again when it is missing, wrong or
325
325
  # revoked. Unique names, since the admin token cannot delete a user's tokens.
326
326
  - name: Read service tokens
@@ -62,7 +62,7 @@ renovate_host_rules: []
62
62
  renovate_github_com_token: ""
63
63
 
64
64
  # --- Authelia ---------------------------------------------------------------
65
- # Secrets are per server and belong in servers/<server>/vars.yml (vault for prod).
65
+ # Secrets are per server and belong in servers/<server>/secrets.yml.
66
66
  authelia_session_secret: ""
67
67
  authelia_storage_encryption_key: ""
68
68
  # Leave the address empty to fall the notifier back to a file, which is what
@@ -148,7 +148,7 @@
148
148
  become: true
149
149
  register: traefik_static_config
150
150
 
151
- # Vault values rendered to files rather than .env lines, which Authelia reads
151
+ # Secrets rendered to files rather than .env lines, which Authelia reads
152
152
  # via *_FILE. Authelia never writes the user database back — password change
153
153
  # and reset are disabled — so rendering it every run is safe.
154
154
  - name: Render Authelia user database
@@ -1,25 +1,18 @@
1
- import { spawnSync } from "node:child_process";
2
1
  import { readFileSync } from "node:fs";
3
- import { join } from "node:path";
4
2
  import { z } from "zod";
5
3
  import { forgeRepo } from "./lib/forge-repo.js";
6
4
  import { ensureHook, hookApi, hookUrl } from "./core/app-hook/hooks.js";
7
5
  import { operatorToken, tokenVariable } from "./core/app-hook/token.js";
8
6
  import { absolutePath, appNameSchema, fleetRoot, serverNameSchema } from "./core/fleet/brand.js";
9
7
  import { loadFleet } from "./core/fleet/load.js";
10
- import { isVaulted } from "./core/fleet/parse.js";
11
8
  import { takesWebhooks } from "./core/recipes/app-hook.js";
12
9
  import { err, flatMap } from "./lib/result.js";
13
10
  import { fields } from "./lib/yaml.js";
14
11
  const input = z.strictObject({ root: z.string(), server: serverNameSchema, app: appNameSchema });
15
- /** secrets.yml, opened with the password the step was given (ANSIBLE_VAULT_PASSWORD_FILE) where it is a vault. */
16
- const secretsOf = (dir) => {
17
- const file = join(dir, "secrets.yml");
18
- const text = readFileSync(file, "utf8");
19
- if (!isVaulted(text))
20
- return fields(text);
21
- const opened = spawnSync("ansible-vault", ["view", file], { encoding: "utf8" });
22
- return opened.status === 0 ? fields(opened.stdout) : err("cannot open secrets.yml; configure vault_password_file or give the vault password");
12
+ /** secrets.yml as kayra opened it for this step (KAYRA_SECRETS). */
13
+ const secretsOf = () => {
14
+ const file = process.env["KAYRA_SECRETS"];
15
+ return file === undefined ? err("kayra opened no secrets.yml for this step") : fields(readFileSync(file, "utf8"));
23
16
  };
24
17
  const parsedJson = (text) => {
25
18
  try {
@@ -55,7 +48,7 @@ const run = async () => {
55
48
  const operator = operatorToken(repo);
56
49
  if (operator === undefined)
57
50
  return err(`no token for ${new URL(repo.origin).host}: set ${tokenVariable(new URL(repo.origin).host)} to one with admin rights on ${repo.owner}/${repo.name}`);
58
- const secrets = secretsOf(server.dir);
51
+ const secrets = secretsOf();
59
52
  if (!secrets.ok)
60
53
  return secrets;
61
54
  const platform = secrets.value["git_token"];
@@ -65,7 +58,7 @@ const run = async () => {
65
58
  const done = await ensureHook(hookApi(repo, http), hookUrl(server.domain, app.name), { platform, operator: operator.token }, webhook);
66
59
  return done.ok ? { ok: true, value: [...done.value, ` (as you, with ${operator.from})`] } : done;
67
60
  };
68
- const result = await run().catch(() => err("the hook could not be set up; check the forge's address, the tokens and the vault"));
61
+ const result = await run().catch(() => err("the hook could not be set up; check the forge's address, the tokens and secrets.yml"));
69
62
  if (result.ok)
70
63
  process.stdout.write(`${result.value.join("\n")}\n`);
71
64
  else {
@@ -79,7 +79,7 @@ const run = async () => {
79
79
  return err(`${lines.join("\n")}\n${app} needs ${String(missing.length)} value(s) before it can run; set them, then run the launch again:\n${missing.map((line) => ` ${line}`).join("\n")}`);
80
80
  return ok([...lines, `✓ every value ${app} declares is set (${String(values.value.config.length)} config, ${String(values.value.secrets.length)} secret)`]);
81
81
  };
82
- const result = await run().catch(() => err("the launch could not prepare the app; check file access and vault configuration"));
82
+ const result = await run().catch(() => err("the launch could not prepare the app; check file access and secrets.yml"));
83
83
  if (result.ok)
84
84
  process.stdout.write(`${result.value.join("\n")}\n`);
85
85
  else {
@@ -24,7 +24,7 @@ const run = async () => {
24
24
  }
25
25
  catch {
26
26
  // Errors may contain YAML or imported secret text. None reaches a run record.
27
- return err("app values could not be updated; check file access and vault configuration");
27
+ return err("app values could not be updated; check file access and secrets.yml");
28
28
  }
29
29
  };
30
30
  const result = await run();
@@ -8,7 +8,7 @@ import { typed } from "../../lib/typed.js";
8
8
  export const confirmations = (plan) => Object.freeze(plan.flatMap((step, index) => (step.kind === "confirm" ? [{ key: `confirm-${String(index)}`, warning: step.warning, expect: step.expect }] : [])));
9
9
  const secretNote = (question, server) => [
10
10
  `kayra mcp passes no password, and this step needed one: ${flat(question).trim().replace(/^\?\s*/, "").replace(/:$/, "")}.`,
11
- `Name a vault_password_file in ${server === undefined ? "the server's" : `servers/${server}/`}server.yml, have sudo's cached (sudo -v), or run the command in a terminal.`,
11
+ `Have sudo's cached (sudo -v)${server === undefined ? "" : ` for ${server}`}, or run the command in a terminal.`,
12
12
  ].join(" ");
13
13
  /** The run as text: the heading, each step with its output and how it went, the footer. */
14
14
  const report = (told, about, bare) => {
@@ -25,7 +25,7 @@ const instructions = (fleet) => [
25
25
  "",
26
26
  "- dryRun: true on a plan's tool lists its steps and runs nothing: use it before a change.",
27
27
  "- A change to a server whose target is not multipass is approved by the operator first; down and app_purge every time, with the server's name typed.",
28
- "- No password passes through here: a vaulted server needs vault_password_file in its server.yml.",
28
+ "- No password passes through here: SOPS opens secrets.yml with the operator's age key, which may ask for Touch ID on their Mac.",
29
29
  "- verify, plan, status and validate change nothing.",
30
30
  ].join("\n").slice(0, 4000);
31
31
  /** What the operator typed for each confirmation, or the request to ask them first. */
@@ -0,0 +1,19 @@
1
+ import { spawned } from "../interpreter/process.js";
2
+ import { err, ok } from "../../lib/result.js";
3
+ import { typed } from "../../lib/typed.js";
4
+ /** Where SOPS finds the age identity: the server's age_key_file, else its own defaults (SOPS_AGE_KEY_FILE, ~/.config/sops/age/keys.txt). */
5
+ export const keyEnv = (key) => typed(key === undefined ? {} : { SOPS_AGE_KEY_FILE: key });
6
+ /** SOPS's own error, its first line; the plugin's when the Secure Enclave or Touch ID refused. */
7
+ const why = (stderr) => stderr.split("\n").map((line) => line.trim()).find((line) => line !== "") ?? "sops failed";
8
+ export const sops = {
9
+ decrypt: async (file, key, plumbing) => {
10
+ const opened = await spawned(["sops", "decrypt", "--input-type", "yaml", "--output-type", "yaml", file], { stdout: "capture", stderr: "capture", stdin: { kind: "none" } }, keyEnv(key), plumbing);
11
+ return opened.code === 0 ? ok(opened.stdout) : err(opened.started ? `cannot open ${file}: ${why(opened.stderr)}` : "sops is not installed (brew install sops)");
12
+ },
13
+ set: async (file, path, value, key) => {
14
+ const index = path.map((segment) => `[${JSON.stringify(segment)}]`).join("");
15
+ // The value on stdin, as JSON: never in an argument a process listing shows.
16
+ const changed = await spawned(["sops", "set", "--idempotent", "--value-stdin", file, index], { stdout: "discard", stderr: "capture", stdin: { kind: "given", text: JSON.stringify(value) } }, keyEnv(key));
17
+ return changed.code === 0 ? ok(undefined) : err(changed.started ? `cannot change ${file}: ${why(changed.stderr)}` : "sops is not installed (brew install sops)");
18
+ },
19
+ };
@@ -3,12 +3,11 @@ import { existsSync, lstatSync, mkdirSync, readFileSync, renameSync, rmSync, wri
3
3
  import { dirname, join } from "node:path";
4
4
  import { parseDocument } from "yaml";
5
5
  import { appValuesSchema } from "../fleet/settings.js";
6
- import { isVaulted } from "../fleet/parse.js";
6
+ import { appSecretsIn, isEncrypted, secretsShape } from "../fleet/secrets.js";
7
7
  import { within } from "../fleet/brand.js";
8
8
  import { textAt } from "../adapters/fs.js";
9
- import { spawned } from "../interpreter/process.js";
10
- import { vault } from "../recipes/steps.js";
11
- import { attempt, err, ok } from "../../lib/result.js";
9
+ import { sops } from "../adapters/sops.js";
10
+ import { attempt, err, flatMap, map, ok } from "../../lib/result.js";
12
11
  import { fields } from "../../lib/yaml.js";
13
12
  import { requirements } from "./model.js";
14
13
  const parsedValues = (text) => {
@@ -19,39 +18,23 @@ const parsedValues = (text) => {
19
18
  return checked.success ? ok(checked.data) : err("values must be strings, numbers, or booleans");
20
19
  };
21
20
  const present = (values, key) => Object.hasOwn(values, key) && values[key] !== "";
22
- const fileOf = (server, name, secret) => within(server.dir, "apps", `${name}${secret ? ".secrets" : ""}.yml`);
23
- const opened = async (server, file, source) => {
24
- if (source === undefined)
25
- return ok("");
26
- if (!isVaulted(source)) {
27
- if (server.vault.kind !== "none")
28
- return err("secrets must be Ansible Vault encrypted for this server");
29
- return ok(source);
30
- }
31
- const result = await spawned(["ansible-vault", "view", file], { stdout: "capture", stderr: "capture", stdin: { kind: "none" } }, vault(server).env);
32
- return result.code === 0 ? ok(result.stdout) : err("cannot open app secrets; configure vault_password_file or provide the vault password through the client");
33
- };
21
+ const configFile = (server, name) => within(server.dir, "apps", `${name}.yml`);
22
+ /** An app's secrets as secrets.yml holds them: without its key, each value a stand-in. */
23
+ const secretsShapeOf = (server, name) => map(secretsShape(textAt(server.secrets.file) ?? ""), (secrets) => appSecretsIn(secrets, name));
34
24
  export const inspectValues = async (root, server, name, dir) => {
35
25
  const required = requirements(root, server, name, textAt, dir);
36
26
  if (!required.ok)
37
27
  return required;
38
- const config = parsedValues(textAt(fileOf(server, name, false)) ?? "");
28
+ const config = parsedValues(textAt(configFile(server, name)) ?? "");
39
29
  if (!config.ok)
40
30
  return config;
41
- const file = fileOf(server, name, true);
42
- const source = textAt(file);
43
- const secret = source !== undefined && isVaulted(source) && server.vault.kind === "ask"
44
- ? err("configure vault_password_file to inspect encrypted secret presence without prompting")
45
- : await opened(server, file, source);
46
- const values = secret.ok ? parsedValues(secret.value) : secret;
47
- if (secret.ok && !values.ok)
48
- return values;
49
- return ok({
31
+ const secrets = secretsShapeOf(server, name);
32
+ return Promise.resolve(ok({
50
33
  server: server.name, app: name, manifest: required.value.manifest,
51
34
  config: required.value.config.map((key) => ({ key, state: present(config.value, key) ? "set" : "missing" })),
52
- secrets: required.value.secrets.map((key) => ({ key, state: values.ok ? (present(values.value, key) ? "set" : "missing") : "locked" })),
53
- hint: values.ok ? null : values.error,
54
- });
35
+ secrets: required.value.secrets.map((key) => ({ key, state: secrets.ok ? (present(secrets.value, key) ? "set" : "missing") : "locked" })),
36
+ hint: secrets.ok ? null : `secrets.yml ${secrets.error}`,
37
+ }));
55
38
  };
56
39
  const store = (file, original, content) => {
57
40
  const temporary = `${file}.${randomUUID()}.tmp`;
@@ -66,39 +49,45 @@ const store = (file, original, content) => {
66
49
  rmSync(temporary, { force: true });
67
50
  }
68
51
  };
69
- const editFile = async (server, name, edit) => {
70
- const file = fileOf(server, name, edit.kind !== "config");
52
+ const saved = (key, server) => ok(`${key} saved in the fleet; apply with kayra apps ${server.name} after reviewing the change`);
53
+ const editConfig = (server, name, key, value) => {
54
+ const file = configFile(server, name);
71
55
  if (existsSync(file) && lstatSync(file).isSymbolicLink())
72
56
  return err("refusing to replace a symlinked values file");
73
57
  const original = textAt(file);
74
- if (edit.kind === "config" && original !== undefined && isVaulted(original))
75
- return err("configuration belongs in plaintext, not a vault");
76
- const source = edit.kind === "config" ? ok(original ?? "") : await opened(server, file, original);
77
- if (!source.ok)
78
- return source;
79
- const existing = parsedValues(source.value);
58
+ if (original !== undefined && isEncrypted(original))
59
+ return err("configuration belongs in plaintext, not encrypted");
60
+ const existing = parsedValues(original ?? "");
80
61
  if (!existing.ok)
81
62
  return existing;
63
+ if (existing.value[key] === value)
64
+ return ok(`${key} is unchanged`);
65
+ const document = parseDocument(original ?? "{}\n");
66
+ document.set(key, value);
67
+ return flatMap(store(file, original, document.toString()), () => saved(key, server));
68
+ };
69
+ /** A secret into secrets.yml's app_secrets.<app>: SOPS sets it in place (one Touch ID where the key asks), a multipass server's plaintext is written. */
70
+ const editSecret = async (server, name, edit) => {
71
+ const { file } = server.secrets;
72
+ if (existsSync(file) && lstatSync(file).isSymbolicLink())
73
+ return err("refusing to replace a symlinked secrets.yml");
74
+ const existing = secretsShapeOf(server, name);
75
+ if (!existing.ok)
76
+ return err(`secrets.yml ${existing.error}`);
82
77
  if (edit.kind === "generate" && present(existing.value, edit.key))
83
78
  return ok(`${edit.key} already exists; kept it`);
84
- const value = edit.kind === "config" ? edit.value : edit.kind === "generate" ? randomBytes(32).toString("hex") : readFileSync(edit.file, "utf8").replace(/\r?\n$/, "");
85
- if (value.includes("\0") || (edit.kind !== "config" && value === ""))
79
+ const value = edit.kind === "generate" ? randomBytes(32).toString("hex") : readFileSync(edit.file, "utf8").replace(/\r?\n$/, "");
80
+ if (value.includes("\0") || value === "")
86
81
  return err("secret input must be nonempty text without NUL bytes");
87
- if (existing.value[edit.key] === value)
82
+ const path = ["app_secrets", name, edit.key];
83
+ if (server.secrets.encrypted)
84
+ return flatMap(await sops.set(file, path, value, server.secrets.key), () => saved(edit.key, server));
85
+ const original = textAt(file);
86
+ const document = parseDocument(original ?? "{}\n");
87
+ if (document.getIn(path) === value)
88
88
  return ok(`${edit.key} is unchanged`);
89
- const document = parseDocument(source.value || "{}\n");
90
- document.set(edit.key, value);
91
- const plaintext = document.toString();
92
- if (edit.kind === "config" || (server.vault.kind === "none" && (original === undefined || !isVaulted(original)))) {
93
- const saved = store(file, original, plaintext);
94
- return saved.ok ? ok(`${edit.key} saved in the fleet; apply with kayra apps ${server.name} after reviewing the change`) : saved;
95
- }
96
- // Vault receives plaintext over stdin; only ciphertext reaches a temporary file.
97
- const encrypted = await spawned(["ansible-vault", "encrypt", "--output", "-"], { stdout: "capture", stderr: "capture", stdin: { kind: "given", text: plaintext } }, vault(server).env);
98
- if (encrypted.code !== 0 || !isVaulted(encrypted.stdout))
99
- return err("could not encrypt app secrets; the existing file was not changed");
100
- const saved = store(file, original, encrypted.stdout);
101
- return saved.ok ? ok(`${edit.key} saved in the fleet; apply with kayra apps ${server.name} after reviewing the change`) : saved;
89
+ document.setIn(path, value);
90
+ return flatMap(store(file, original, document.toString()), () => saved(edit.key, server));
102
91
  };
103
92
  export const editValues = async (root, server, name, edit, dir) => {
104
93
  const required = requirements(root, server, name, textAt, dir);
@@ -107,16 +96,16 @@ export const editValues = async (root, server, name, edit, dir) => {
107
96
  const keys = edit.kind === "config" ? required.value.config : required.value.secrets;
108
97
  if (!keys.includes(edit.key))
109
98
  return err(`${edit.key} is not a declared ${edit.kind === "config" ? "configuration" : "secret"} key`);
110
- const directory = dirname(fileOf(server, name, edit.kind !== "config"));
99
+ const directory = edit.kind === "config" ? dirname(configFile(server, name)) : server.dir;
111
100
  mkdirSync(directory, { recursive: true, mode: 0o700 });
112
101
  if (lstatSync(directory).isSymbolicLink())
113
- return err("refusing a symlinked apps directory");
102
+ return err(`refusing a symlinked ${edit.kind === "config" ? "apps directory" : "server directory"}`);
114
103
  const lock = join(directory, ".kayra-values.lock");
115
- const acquired = attempt(() => { mkdirSync(lock, { mode: 0o700 }); return ok(true); }, () => "app values are being edited; retry, or remove apps/.kayra-values.lock if its writer was interrupted");
104
+ const acquired = attempt(() => { mkdirSync(lock, { mode: 0o700 }); return ok(true); }, () => `app values are being edited; retry, or remove ${lock} if its writer was interrupted`);
116
105
  if (!acquired.ok)
117
106
  return acquired;
118
107
  try {
119
- return await editFile(server, name, edit);
108
+ return edit.kind === "config" ? editConfig(server, name, edit.key, edit.value) : await editSecret(server, name, edit);
120
109
  }
121
110
  finally {
122
111
  rmSync(lock, { recursive: true, force: true });
@@ -26,7 +26,7 @@ export const serverName = minting(serverNameSchema);
26
26
  export const appName = minting(appNameSchema);
27
27
  export const subdomain = minting(subdomainSchema);
28
28
  export const domain = minting(domainSchema);
29
- /** `~/…` is the operator's home, as Ansible reads it in vault_password_file. */
29
+ /** `~/…` is the operator's home, as a shell reads it. */
30
30
  export const absolutePath = (value) => {
31
31
  const expanded = value === "~" || value.startsWith("~/") ? join(homedir(), value.slice(1)) : value;
32
32
  return isAbsolute(expanded) ? ok(normalize(expanded)) : err(`'${value}' is not an absolute path`);
@@ -34,7 +34,7 @@ export const absolutePath = (value) => {
34
34
  export const kayraRoot = (path) => path;
35
35
  export const fleetRoot = (path) => path;
36
36
  export const serverDir = (root, name) => join(root, "servers", name);
37
- export const vaultPasswordFile = (path) => path;
37
+ export const ageKeyFile = (path) => path;
38
38
  export const within = (base, ...segments) => join(base, ...segments);
39
39
  /** One of each, as help shows a value: what a command-line parser offers before the operator types one. */
40
40
  export const examples = {
@@ -13,7 +13,7 @@ export const jsonSchemas = () => typed({
13
13
  "defaults.json": jsonSchema("defaults.yml", defaultsSchema),
14
14
  ...Object.fromEntries(serverKinds.map((kind) => [`vars-${kind}.json`, jsonSchema(`vars.yml (kind ${kind})`, varsSchema(kind))])),
15
15
  "app.json": jsonSchema("app.yml", appManifestSchema),
16
- "app-values.json": jsonSchema("apps/<app>.yml and apps/<app>.secrets.yml", appValuesSchema),
16
+ "app-values.json": jsonSchema("apps/<app>.yml", appValuesSchema),
17
17
  });
18
18
  const kindsOf = (each) => each.kinds.join(", ");
19
19
  /** MDX reads <…> as a component and {…} as an expression; a table cell's | ends it. */
@@ -1,18 +1,11 @@
1
- import { absolutePath, host, sshDefaultPort, keyFile, port, remotePath, sshUser, vaultPasswordFile } from "./brand.js";
1
+ import { absolutePath, ageKeyFile, host, sshDefaultPort, keyFile, port, remotePath, sshUser, within } from "./brand.js";
2
2
  import { err, flatMap, map, ok } from "../../lib/result.js";
3
3
  import { serverSchema, varsSchema } from "./settings.js";
4
4
  import { checkSettings, knownSettings } from "./shape.js";
5
+ import { isEncrypted } from "./secrets.js";
5
6
  import { typed } from "../../lib/typed.js";
6
7
  import { fields, mapping } from "../../lib/yaml.js";
7
- export const isVaulted = (text) => text.startsWith("$ANSIBLE_VAULT");
8
- const vault = (descriptor) => {
9
- const file = descriptor.vault_password_file;
10
- if (!descriptor.vault)
11
- return ok({ kind: "none" });
12
- if (file === undefined)
13
- return ok({ kind: "ask" });
14
- return map(absolutePath(file), (path) => typed({ kind: "file", file: vaultPasswordFile(path) }));
15
- };
8
+ const ageKey = (descriptor) => descriptor.age_key_file === undefined ? ok(undefined) : map(absolutePath(descriptor.age_key_file), ageKeyFile);
16
9
  /** What Zod's expected types are called in a fleet's YAML. */
17
10
  const expected = { string: "a string", boolean: "true or false", array: "a list", object: "a mapping" };
18
11
  /** What the file says at a path Zod reports: Zod's issues leave the input out. */
@@ -69,14 +62,12 @@ const inventoryOf = (text) => {
69
62
  const stackHosts = (stacks, serverDomain) => Object.freeze(stacks
70
63
  .flatMap((compose) => Object.freeze([...compose.matchAll(/Host\(`([^`]*)`\)/g)].map((match) => match[1] ?? "")))
71
64
  .map((host) => (host.endsWith(`.${serverDomain}`) ? host.slice(0, -serverDomain.length - 1) : host)));
72
- /** An app's config is plaintext, to be read and diffed; its secrets are vaulted wherever secrets.yml is. */
73
- const appValuesProblem = (appValues, secretsMustBeVaulted) => appValues
65
+ /** An app's config is plaintext, to be read and diffed; its secrets are in secrets.yml, under app_secrets. */
66
+ const appValuesProblem = (appValues) => appValues
74
67
  .map(({ file, text }) => file.endsWith(".secrets.yml")
75
- ? secretsMustBeVaulted && !isVaulted(text)
76
- ? `apps/${file} must be vault-encrypted`
77
- : undefined
78
- : isVaulted(text)
79
- ? `apps/${file} is vault-encrypted; it holds config only — secrets go in apps/${file.replace(/\.yml$/, ".secrets.yml")}`
68
+ ? `apps/${file}: an app's secrets go in secrets.yml, under app_secrets.${file.replace(/\.secrets\.yml$/, "")}`
69
+ : isEncrypted(text)
70
+ ? `apps/${file} is encrypted; it holds config only — secrets go in secrets.yml, under app_secrets.${file.replace(/\.yml$/, "")}`
80
71
  : undefined)
81
72
  .find((problem) => problem !== undefined);
82
73
  /** What every command may rely on, checked for every server whenever the fleet loads. */
@@ -88,9 +79,9 @@ export const parseServer = (files) => {
88
79
  if (!parsed.success)
89
80
  return err(serverProblem(parsed.error, descriptor.value));
90
81
  const { kind, target, domain, apps } = parsed.data;
91
- const serverVault = vault(parsed.data);
92
- if (!serverVault.ok)
93
- return serverVault;
82
+ const key = ageKey(parsed.data);
83
+ if (!key.ok)
84
+ return err(`age_key_file ${key.error}`);
94
85
  if (target === "home" && files.inventory === undefined)
95
86
  return err("target home needs a hand-written inventory.ini next to it");
96
87
  if (target !== "home" && !files.hasTerraform)
@@ -99,13 +90,13 @@ export const parseServer = (files) => {
99
90
  return err("no vars.yml next to it");
100
91
  if (files.secrets === undefined)
101
92
  return err("no secrets.yml next to it");
102
- if (isVaulted(files.vars))
103
- return err("vars.yml is vault-encrypted; it holds settings only — secrets go in secrets.yml");
104
- if (target !== "multipass" && serverVault.value.kind === "none")
105
- return err(`target ${target} requires vault: true`);
106
- if (target !== "multipass" && !isVaulted(files.secrets))
107
- return err(`target ${target} requires a vault-encrypted secrets.yml`);
108
- const appValues = appValuesProblem(files.appValues, target !== "multipass");
93
+ if (isEncrypted(files.vars))
94
+ return err("vars.yml is encrypted; it holds settings only — secrets go in secrets.yml");
95
+ if (files.secrets.startsWith("$ANSIBLE_VAULT"))
96
+ return err("secrets.yml is an Ansible Vault; kayra reads SOPS now (see Upgrading to 0.21 in kayra's docs)");
97
+ if (target !== "multipass" && !isEncrypted(files.secrets))
98
+ return err(`target ${target} requires a SOPS-encrypted secrets.yml`);
99
+ const appValues = appValuesProblem(files.appValues);
109
100
  if (appValues !== undefined)
110
101
  return err(appValues);
111
102
  const vars = fields(files.vars);
@@ -137,8 +128,7 @@ export const parseServer = (files) => {
137
128
  kind,
138
129
  target,
139
130
  domain,
140
- vault: serverVault.value,
141
- hasVaultedSecrets: isVaulted(files.secrets),
131
+ secrets: { file: within(files.dir, "secrets.yml"), encrypted: isEncrypted(files.secrets), key: key.value },
142
132
  apps: apps.map((app, index) => typed({ name: app.name, repo: app.repo, path: app.path, entry: mapping(entries[index]) ?? {} })),
143
133
  hasTerraform: files.hasTerraform,
144
134
  buildsVm: target === "home" && vars.value["vm"] !== undefined,