@yunuseon/kayra 0.22.4 → 0.23.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.
@@ -45,6 +45,10 @@ renovate_token: ""
45
45
  # people's releases; a server whose repos carry the operator's own images
46
46
  # runs it more often, so a new build reaches a PR soon after it is pushed.
47
47
  renovate_schedule: "0 4 * * 1"
48
+ # The host the bare domain redirects to, permanently, path and query kept:
49
+ # www sends https://<domain>/x?y to https://www.<domain>/x?y. Empty, the bare
50
+ # domain is not served.
51
+ apex_redirect: ""
48
52
  # Push webhooks start a deploy at once; without them, the poll below finds the push.
49
53
  webhooks: true
50
54
  deploy_poll: "17 * * * *"
@@ -81,6 +81,25 @@
81
81
  become: true
82
82
  register: platform_files
83
83
 
84
+ # Rendered beside the copied dynamic/ files, which the copy never deletes:
85
+ # gone again when apex_redirect is unset.
86
+ - name: Render the bare domain's redirect
87
+ ansible.builtin.template:
88
+ src: apex.yml.j2
89
+ dest: "{{ platform_dir }}/traefik/dynamic/apex.yml"
90
+ owner: deploy
91
+ group: deploy
92
+ mode: "0644"
93
+ when: apex_redirect | length > 0
94
+ become: true
95
+
96
+ - name: Remove the bare domain's redirect
97
+ ansible.builtin.file:
98
+ path: "{{ platform_dir }}/traefik/dynamic/apex.yml"
99
+ state: absent
100
+ when: apex_redirect | length == 0
101
+ become: true
102
+
84
103
  # The deployer's code and the scripts it and the builder run, into the build
85
104
  # context of both images: a checkout's TypeScript, or the published package's
86
105
  # JavaScript (Node strips types only outside node_modules).
@@ -0,0 +1,19 @@
1
+ # The bare domain, {{ domain }}: a permanent redirect to {{ apex_redirect }}.{{ domain }},
2
+ # path and query kept (apex_redirect in vars.yml). Its certificate comes like
3
+ # every other host's; plain HTTP reaches it through the web entrypoint's
4
+ # redirect to HTTPS first, as HSTS preloading wants.
5
+ http:
6
+ routers:
7
+ apex:
8
+ rule: "Host(`{{ domain }}`)"
9
+ entryPoints: [websecure]
10
+ tls:
11
+ certResolver: le
12
+ middlewares: [apex-redirect]
13
+ service: noop@internal
14
+ middlewares:
15
+ apex-redirect:
16
+ redirectRegex:
17
+ regex: "^https?://[^/]+(.*)$"
18
+ replacement: "https://{{ apex_redirect }}.{{ domain }}${1}"
19
+ permanent: true
@@ -24,6 +24,7 @@ export const settings = [
24
24
  setting("authelia_smtp_username", platform, text, "SMTP user for Authelia."),
25
25
  setting("authelia_smtp_sender", platform, text, "Sender address of Authelia's mails."),
26
26
  setting("acme_dns", platform, z.enum(["", "hetzner"]), "Certificates by DNS-01 instead of HTTP-01, for a server the internet never reaches: the DNS provider (hetzner)."),
27
+ setting("apex_redirect", platform, subdomainSchema, "The host the bare domain redirects to, permanently, path and query kept: www sends https://<domain>/x?y to https://www.<domain>/x?y. Unset, the bare domain is not served."),
27
28
  setting("webhooks", platform, z.boolean(), "Whether a push webhook may start a deploy at once: true (default) serves deploy.<domain>/hook/<app>; false serves no such endpoint, and pushes deploy by deploy_poll."),
28
29
  setting("deploy_poll", platform, text, "When the deployer looks for pushes no webhook brought, as a five-field cron spec in the server's time (default hourly: 17 * * * *)."),
29
30
  setting("renovate_repositories", platform, texts, "owner/repo of the repos this server's Renovate job keeps up to date; the job runs only where the list is not empty."),
@@ -85,6 +85,24 @@ const routeFindings = (facts) => {
85
85
  ]);
86
86
  }), `edge_routes: ${count(routes.length, "route")}, each with subdomains and a host:port upstream`);
87
87
  };
88
+ /** apex_redirect: the bare domain redirects to a host one of the server's apps serves. */
89
+ const apexFindings = (facts) => {
90
+ const target = facts.vars["apex_redirect"];
91
+ if (typeof target !== "string" || target === "")
92
+ return typed([]);
93
+ const hosts = facts.server.apps.flatMap((app) => {
94
+ const parsed = appManifestSchema.safeParse(facts.manifests[app.name]);
95
+ return typed(parsed.success ? (parsed.data.routes ?? []).map((route) => String(route.host)) : []);
96
+ });
97
+ const domain = facts.server.domain;
98
+ if (hosts.includes(target))
99
+ return typed([passed(`apex_redirect: ${domain} redirects to ${target}.${domain}, which app routes serve`)]);
100
+ if (facts.server.apps.some((app) => facts.manifests[app.name] === undefined))
101
+ return typed([unchecked(`apex_redirect not checked: not every app is cloned yet`, `kayra apps ${facts.server.name} clones them`)]);
102
+ return typed([
103
+ problem(`apex_redirect: no app routes ${target}.${domain}, so ${domain} would redirect to nothing`, "name a route host of one of the server's apps"),
104
+ ]);
105
+ };
88
106
  /** Each app's contract against the fleet's values for it. */
89
107
  const appFindings = (facts) => Object.freeze(facts.server.apps.flatMap((app) => {
90
108
  const manifest = facts.manifests[app.name];
@@ -116,7 +134,7 @@ const appFindings = (facts) => Object.freeze(facts.server.apps.flatMap((app) =>
116
134
  }));
117
135
  const rank = { problem: 0, unchecked: 1, passed: 2 };
118
136
  /** Everything validate finds for one server: problems first, then what it could not check, then what holds. */
119
- export const findings = (facts) => Object.freeze([...secretsFindings(facts), ...recipientFindings(facts), ...terraformFindings(facts), ...stackFindings(facts), ...routeFindings(facts), ...appFindings(facts)].toSorted((a, b) => rank[a.result] - rank[b.result]));
137
+ export const findings = (facts) => Object.freeze([...secretsFindings(facts), ...recipientFindings(facts), ...terraformFindings(facts), ...stackFindings(facts), ...routeFindings(facts), ...apexFindings(facts), ...appFindings(facts)].toSorted((a, b) => rank[a.result] - rank[b.result]));
120
138
  /** How a set of findings went as a whole: its worst. */
121
139
  export const overall = (found) => verdicts.find((verdict) => found.some((each) => each.result === verdict)) ?? "passed";
122
140
  /** How many problems, and how many checks could not look, across reports. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yunuseon/kayra",
3
- "version": "0.22.4",
3
+ "version": "0.23.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",
@@ -38,6 +38,11 @@
38
38
  ],
39
39
  "description": "Certificates by DNS-01 instead of HTTP-01, for a server the internet never reaches: the DNS provider (hetzner)."
40
40
  },
41
+ "apex_redirect": {
42
+ "description": "The host the bare domain redirects to, permanently, path and query kept: www sends https://<domain>/x?y to https://www.<domain>/x?y. Unset, the bare domain is not served.",
43
+ "type": "string",
44
+ "pattern": "^[a-z0-9]([a-z0-9-]*[a-z0-9])?$"
45
+ },
41
46
  "webhooks": {
42
47
  "type": "boolean",
43
48
  "description": "Whether a push webhook may start a deploy at once: true (default) serves deploy.<domain>/hook/<app>; false serves no such endpoint, and pushes deploy by deploy_poll."
@@ -38,6 +38,11 @@
38
38
  ],
39
39
  "description": "Certificates by DNS-01 instead of HTTP-01, for a server the internet never reaches: the DNS provider (hetzner)."
40
40
  },
41
+ "apex_redirect": {
42
+ "description": "The host the bare domain redirects to, permanently, path and query kept: www sends https://<domain>/x?y to https://www.<domain>/x?y. Unset, the bare domain is not served.",
43
+ "type": "string",
44
+ "pattern": "^[a-z0-9]([a-z0-9-]*[a-z0-9])?$"
45
+ },
41
46
  "webhooks": {
42
47
  "type": "boolean",
43
48
  "description": "Whether a push webhook may start a deploy at once: true (default) serves deploy.<domain>/hook/<app>; false serves no such endpoint, and pushes deploy by deploy_poll."
@@ -5,7 +5,8 @@
5
5
  # its vars.yml's monitor.watch,
6
6
  # platform: auth.<domain> answers; the platform's pages (hosts.conf, status
7
7
  # where it runs a monitor) send a browser to the login; every app route answers, not 5xx;
8
- # deploy.<domain>/health is 200 (monitor_health_tokens.<server>)
8
+ # deploy.<domain>/health is 200 (monitor_health_tokens.<server>);
9
+ # with apex_redirect, the bare domain redirects (301)
9
10
  # edge: every edge_routes name answers, not 5xx
10
11
  # nas: nothing of its own: its stacks are monitor.extra
11
12
  # plus monitor.extra as given. Every certificate must have 10 days left. Each
@@ -56,6 +57,8 @@ watched() {
56
57
  [ "$sub" = auth ] || endpoint "${sub}.${domain}" "$server" "https://${sub}.${domain}/" false false '[STATUS] == 302' "$cert"
57
58
  done
58
59
  endpoint "deploy.${domain}/health" "$server" "https://deploy.${domain}/health" false true '[STATUS] == 200'
60
+ apex="$( [ -f "${dir}/vars.yml" ] && yq -r '.apex_redirect // ""' "${dir}/vars.yml" || true)"
61
+ [ -z "$apex" ] || endpoint "$domain" "$server" "https://${domain}/" false false '[STATUS] == 301' "$cert"
59
62
  bash "${KAYRA_ROOT}/scripts/local/app-manifests.sh" --routes "$server" | while read -r sub path; do
60
63
  [ -z "$sub" ] || endpoint "${sub}.${domain}${path%/}" "$server" "https://${sub}.${domain}${path}" true false '[STATUS] < 500' "$cert"
61
64
  done
@@ -8,6 +8,8 @@
8
8
  # - auth.<domain> answers; the platform's own pages (hosts.conf; status only
9
9
  # where a monitor runs) send a browser to the login, not a 403 from rules
10
10
  # Authelia never loaded
11
+ # - with apex_redirect, the bare domain redirects permanently to that host,
12
+ # path and query kept
11
13
  # - every certificate is a real one (Let's Encrypt or mkcert), never
12
14
  # Traefik's default
13
15
  # - an unsigned webhook is refused (with webhooks: false, /hook is not
@@ -41,6 +43,7 @@ note() { echo " ! $*"; }
41
43
  domain="$(yq -r '.domain' "${dir}/server.yml")"
42
44
  # Not yq's `//`: it takes false for missing too.
43
45
  webhooks="$( { [ -f "${dir}/vars.yml" ] && yq -r '.webhooks' "${dir}/vars.yml"; } || echo true)"
46
+ apex="$( { [ -f "${dir}/vars.yml" ] && yq -r '.apex_redirect // ""' "${dir}/vars.yml"; } || true)"
44
47
  inventory="${dir}/inventory.ini"
45
48
  [ -f "$inventory" ] || { fail "no inventory for ${name}: provision it first"; exit 1; }
46
49
  field() { awk -v key="$1" '{for (i = 1; i <= NF; i++) if (index($i, key "=") == 1) {print substr($i, length(key) + 2); exit}}' "$inventory"; }
@@ -79,6 +82,7 @@ answers() { real_cert "$1" && [ "$(cut -d' ' -f1 <<< "$1")" = 200 ]; }
79
82
  to_login() { real_cert "$1" && [ "$(cut -d' ' -f1 <<< "$1")" = 302 ] && case "$(cut -d' ' -f2 <<< "$1")" in "https://auth.${domain}/"*) return 0 ;; esac; return 1; }
80
83
  not_5xx() { real_cert "$1" && case "$(cut -d' ' -f1 <<< "$1")" in 000|5*) return 1 ;; esac; }
81
84
  refused() { real_cert "$1" && [ "$(cut -d' ' -f1 <<< "$1")" = 401 ]; }
85
+ to_apex() { real_cert "$1" && [ "$(cut -d' ' -f1 <<< "$1")" = 301 ] && [ "$(cut -d' ' -f2 <<< "$1")" = "https://${apex}.${domain}/x?y" ]; }
82
86
 
83
87
  describe() { echo "HTTP $(cut -d' ' -f1 <<< "$1"), certificate by $(cut -d' ' -f4- <<< "$1") (verify $(cut -d' ' -f3 <<< "$1"))"; }
84
88
 
@@ -96,6 +100,7 @@ while IFS= read -r sub; do
96
100
  *) check "${sub}.${domain} sends a browser to the login" "${sub}.${domain}" GET to_login ;;
97
101
  esac
98
102
  done < <(awk '!/^[[:space:]]*(#|$)/ {print $1}' "${KAYRA_ROOT}/hosts.conf")
103
+ [ -z "$apex" ] || check "${domain} redirects to ${apex}.${domain}" "$domain" GET to_apex "/x?y"
99
104
 
100
105
  first_app="$(head -1 <<< "$registered_wanted")"
101
106
  if [ -n "$first_app" ]; then
@@ -1,9 +1,10 @@
1
1
  # =============================================================================
2
2
  # A platform server's DNS: an A record for each host it serves, derived from
3
3
  # the fleet, never listed by hand. The platform's own subdomains (kayra's
4
- # hosts.conf) and every route host of every app in server.yml, as the app's
5
- # app.yml in the fleet's clone (.cache/apps, which kayra doctor fills) says.
6
- # A new route host in an app is a new record on the next apply.
4
+ # hosts.conf), every route host of every app in server.yml, as the app's
5
+ # app.yml in the fleet's clone (.cache/apps, which kayra doctor fills) says,
6
+ # and the bare domain where the server redirects it (apex_redirect in
7
+ # vars.yml). A new route host in an app is a new record on the next apply.
7
8
  # =============================================================================
8
9
 
9
10
  terraform {
@@ -28,9 +29,11 @@ locals {
28
29
  }
29
30
  uncloned = [for name, manifest in local.manifests : name if !fileexists(manifest)]
30
31
 
31
- # status only where the server runs a monitor, which serves it.
32
+ # status only where the server runs a monitor, which serves it; the bare
33
+ # domain only where it redirects (apex_redirect).
32
34
  vars_file = "${var.fleet_root}/servers/${var.server}/vars.yml"
33
35
  monitored = fileexists(local.vars_file) ? try(yamldecode(file(local.vars_file)).monitor, null) != null : false
36
+ apex = fileexists(local.vars_file) ? try(yamldecode(file(local.vars_file)).apex_redirect, "") != "" : false
34
37
  platform_subdomains = [
35
38
  for sub in [
36
39
  for line in split("\n", file("${path.module}/../../../hosts.conf")) :
@@ -44,9 +47,13 @@ locals {
44
47
  subdomains = toset(concat(local.platform_subdomains, local.app_subdomains))
45
48
 
46
49
  # A host's name in the zone: www under ippo-club.net in ippo-club.net is www;
47
- # under shop.example.com in example.com it is www.shop.
50
+ # under shop.example.com in example.com it is www.shop. The bare domain is
51
+ # "@" in its own zone, else the rest of it (shop).
48
52
  suffix = local.domain == var.zone ? "" : trimsuffix(local.domain, ".${var.zone}")
49
- names = { for sub in local.subdomains : sub => local.suffix == "" ? sub : "${sub}.${local.suffix}" }
53
+ names = merge(
54
+ { for sub in local.subdomains : sub => local.suffix == "" ? sub : "${sub}.${local.suffix}" },
55
+ local.apex ? { "@" = local.suffix == "" ? "@" : local.suffix } : {},
56
+ )
50
57
  }
51
58
 
52
59
  resource "hcloud_zone_rrset" "a" {
@@ -5,5 +5,5 @@ output "records" {
5
5
 
6
6
  output "hosts" {
7
7
  description = "Every host this server serves, fully qualified"
8
- value = sort([for sub in local.subdomains : "${sub}.${local.domain}"])
8
+ value = sort([for sub in keys(local.names) : sub == "@" ? local.domain : "${sub}.${local.domain}"])
9
9
  }
@@ -0,0 +1,6 @@
1
+ kind: platform
2
+ target: hetzner
3
+ domain: example.com
4
+ apps:
5
+ - {name: shop, repo: https://github.com/acme/shop.git}
6
+ - {name: blog, repo: https://github.com/acme/blog.git, path: site}
@@ -0,0 +1,6 @@
1
+ kind: platform
2
+ target: hetzner
3
+ domain: shop.example.com
4
+ apps:
5
+ - {name: shop, repo: https://github.com/acme/shop.git}
6
+ - {name: blog, repo: https://github.com/acme/blog.git, path: site}
@@ -77,3 +77,33 @@ run "monitor_page" {
77
77
  error_message = "status should be a host where the server runs a monitor"
78
78
  }
79
79
  }
80
+
81
+ # A server redirecting its bare domain (apex_redirect) serves that too: "@" in its own zone.
82
+ run "bare_domain" {
83
+ command = plan
84
+
85
+ variables {
86
+ server = "redirecting"
87
+ zone = "example.com"
88
+ }
89
+
90
+ assert {
91
+ condition = contains(output.hosts, "example.com") && hcloud_zone_rrset.a["@"].name == "@"
92
+ error_message = "The bare domain should be an A record named @: ${jsonencode(output.hosts)}"
93
+ }
94
+ }
95
+
96
+ # Under the zone, the bare domain's name is the rest of it.
97
+ run "bare_domain_under_the_zone" {
98
+ command = plan
99
+
100
+ variables {
101
+ server = "redirecting-nested"
102
+ zone = "example.com"
103
+ }
104
+
105
+ assert {
106
+ condition = contains(output.hosts, "shop.example.com") && hcloud_zone_rrset.a["@"].name == "shop"
107
+ error_message = "shop.example.com in example.com should be named shop"
108
+ }
109
+ }