diff --git a/documentation/docs/odoo.md b/documentation/docs/odoo.md index b66a288..d5be702 100644 --- a/documentation/docs/odoo.md +++ b/documentation/docs/odoo.md @@ -58,8 +58,9 @@ letters, digits and hyphens, starting with a letter, and not `mail`. Automatic ports follow the name order from `basePort` (18069): with `blank` and `demo`, blank gets 18069/18070 and demo 18071/18072. Other machine-wide options: `baseDomain` (`odoo.localhost`), `package`, -`mail.*`, `trustCertificate`, `lan.*` and `acme.email` for exposure, and -the secret options below. +`mail.*`, `trustCertificate` (on by default: the machine's Firefox and +Chromium/Chrome trust the local CA through enterprise policies), +`lan.*` and `acme.email` for exposure, and the secret options below. ## What each instance gets @@ -211,8 +212,8 @@ example on beelink): 1. Regenerate the CA and the certificate: they now also cover `*.odoo.localhost` and the LAN domain (`odoo.beelink.home.arpa`). - Firefox on beelink picks up the new CA through its policy at the - next start; LAN clients import the new `ca.crt` + Firefox and Chromium on beelink pick up the new CA through their + policies at the next start; LAN clients import the new `ca.crt` ([Reaching Odoo from other machines](odoo-access.md)). ```sh @@ -253,3 +254,4 @@ builder): | `odoo-lifecycle` | apps from `modules`, neutralize, mail landing in Mailpit, then a package bump and an added app: `-u all` and the install keep the data | | `odoo-lan`, `odoo-public` | exposure: see [Reaching Odoo from other machines](odoo-access.md) | | `odoo-firefox` | a home-manager Firefox loads an instance without a certificate error, and does not get past TLS on an untrusted control site | +| `odoo-chromium` | the same for a Chromium in the user's packages, through each trust path alone: system trust (`security.pki`, read through NSS and p11-kit) and the `CACertificates` policy; with neither it refuses the CA | diff --git a/flake-parts/nixosModules/odoo.nix b/flake-parts/nixosModules/odoo.nix index b25a388..414e340 100644 --- a/flake-parts/nixosModules/odoo.nix +++ b/flake-parts/nixosModules/odoo.nix @@ -60,7 +60,8 @@ _: { # every instance so adding one never needs `clan vars generate`: # odoo-local-ca CA for HTTPS; name-constrained to baseDomain, # lan.domain and their subdomains, ca.key never - # deployed, ca.crt trusted system wide and by Firefox + # deployed, ca.crt trusted system wide, by Firefox + # and by Chromium/Chrome # odoo-tls one nginx certificate for baseDomain, lan.domain and # their wildcards; key root:nginx 0440 # odoo-passwords admin-password (web login of user "admin") and @@ -97,6 +98,11 @@ _: { else throw "hostSpec.services.odoo: ${settingName} must be set when provisionSecrets = false"; caFile = fileFor "caFile" "odoo-local-ca" "ca.crt"; + # ca.crt is public (secret = false), so the policy is built from it. + chromiumPolicy = pkgs.runCommand "odoo-chromium-ca-policy.json" {nativeBuildInputs = [pkgs.openssl pkgs.jq];} '' + der=$(openssl x509 -in ${caFile} -outform DER | base64 -w0) + jq -n --arg der "$der" '{CACertificates: [$der]}' > $out + ''; tlsCertFile = fileFor "tlsCertFile" "odoo-tls" "tls.crt"; # Read by nginx itself (root:nginx 0440 as a clan var), not as a systemd # credential: `systemctl reload nginx` runs `nginx -t` as the nginx @@ -645,7 +651,7 @@ _: { Instance NAME is served at https://NAME.; https:// lists the instances, https://mail. is Mailpit. Every name resolves to loopback through - networking.hosts (Firefox resolves *.localhost itself). The CA is + networking.hosts (Firefox and Chromium resolve *.localhost themselves). The CA is name-constrained to this domain, lan.domain and their subdomains. ''; }; @@ -886,9 +892,10 @@ _: { type = lib.types.bool; default = true; description = '' - Trust the CA system wide (security.pki) and in Firefox (enterprise - policy Certificates.Install), so every instance opens without a - warning. Other browsers need the CA imported by hand. + Trust the CA system wide (security.pki), in Firefox (enterprise + policy Certificates.Install) and in Chromium and Google Chrome + (enterprise policy CACertificates), so every instance opens + without a warning. Other browsers need the CA imported by hand. ''; }; @@ -1079,8 +1086,19 @@ _: { programs.firefox.policies = lib.mkIf (cfg.trustCertificate && config.programs.firefox.enable) { Certificates.Install = [caFile]; }; - environment.etc."firefox/policies/policies.json" = lib.mkIf (cfg.trustCertificate && !config.programs.firefox.enable) { - text = builtins.toJSON {policies.Certificates.Install = [caFile];}; + environment.etc = { + "firefox/policies/policies.json" = lib.mkIf (cfg.trustCertificate && !config.programs.firefox.enable) { + text = builtins.toJSON {policies.Certificates.Install = [caFile];}; + }; + # A nixpkgs Chromium already trusts security.pki: NSS's built-in + # roots are p11-kit, which reads /etc/ssl/trust-source. The + # CACertificates policy (Chromium 131+, the CA as base64 DER) does + # not depend on that NSS, so Google Chrome and other builds trust + # the CA too (the odoo-chromium VM test checks each path alone). + # Chromium merges every file in its managed policy directory, so + # this one sits beside programs.chromium's default.json/extra.json. + "chromium/policies/managed/odoo-ca.json" = lib.mkIf cfg.trustCertificate {source = chromiumPolicy;}; + "opt/chrome/policies/managed/odoo-ca.json" = lib.mkIf cfg.trustCertificate {source = chromiumPolicy;}; }; } diff --git a/flake.lock b/flake.lock index 0170ed2..a4fc1f0 100644 --- a/flake.lock +++ b/flake.lock @@ -529,11 +529,11 @@ "skills-terraform": "skills-terraform" }, "locked": { - "lastModified": 1794512107, - "narHash": "sha256-lOknKCk5wDJCXwU85K4MwQxg8LJoR7zN7zuQSu7ZnTo=", + "lastModified": 1794522907, + "narHash": "sha256-zrB0dqHyZeM/3rNvPNoW9C07qQmuC1o2fPyRC41jvQo=", "owner": "external-systems", "repo": "skills", - "rev": "bfe2d5d057f36a0d51aeed0aded47669c0f27e95", + "rev": "f0267c9a984aef1ea62a9f72a579c1c9e802ac50", "type": "github" }, "original": { diff --git a/vm-tests/odoo-chromium.nix b/vm-tests/odoo-chromium.nix new file mode 100644 index 0000000..8ee4471 --- /dev/null +++ b/vm-tests/odoo-chromium.nix @@ -0,0 +1,131 @@ +# Chromium trusts the Odoo CA: alice has Chromium in her user packages +# (as matt does on beelink) and runs it headless against +# https://demo.odoo.localhost. Chromium sends an HTTP request only after +# the TLS handshake succeeds, so a request in nginx's access log proves +# the certificate was trusted. Each trust path is checked on its own: +# system trust (security.pki, which Chromium reads through NSS, whose +# built-in roots NixOS replaces with p11-kit and /etc/ssl/trust-source) +# and the CACertificates policy each suffice, and with neither Chromium +# refuses the CA (alice's NSS database is empty). The negative control, https://untrusted.localhost +# with a self-signed certificate, must never reach the access log, +# though Chromium must reach its TLS handshake. +{inputs, ...}: let + common = import ./_odoo-common.nix {inherit inputs;}; +in { + nodes.machine = {pkgs, ...}: let + untrusted = pkgs.runCommand "untrusted-tls" {nativeBuildInputs = [pkgs.openssl];} '' + mkdir -p $out + openssl req -x509 -newkey rsa:2048 -nodes -keyout $out/tls.key -out $out/tls.crt \ + -days 30 -subj "/CN=untrusted.localhost" -addext "subjectAltName=DNS:untrusted.localhost" + ''; + in { + imports = [common.module]; + # No demo data: the database is ready sooner, and only TLS matters here. + hostSpec.services.odoo.instances.demo = { + demoData = false; + workers = 1; + }; + + services.nginx = { + # Log the host, so each request is attributable to its vhost. + commonHttpConfig = '' + log_format withhost '$host "$request" "$http_user_agent"'; + access_log /var/log/nginx/access.log withhost; + # info: handshakes a client aborts (the untrusted control). + error_log /var/log/nginx/error.log info; + ''; + virtualHosts."untrusted.localhost" = { + forceSSL = true; + listenAddresses = ["127.0.0.1"]; + sslCertificate = "${untrusted}/tls.crt"; + sslCertificateKey = "${untrusted}/tls.key"; + locations."/".return = "200 untrusted"; + }; + }; + networking.hosts."127.0.0.1" = ["untrusted.localhost"]; + + users.users.alice = { + isNormalUser = true; + packages = [pkgs.chromium]; + }; + environment = { + # jq reads the policy, trust (p11-kit) lists the system anchors. + systemPackages = [pkgs.jq pkgs.p11-kit]; + # System trust without the Odoo CA, swapped in to test the policy alone. + etc = { + "stock-ca-bundle.crt".source = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"; + "stock-trust-source".source = "${pkgs.cacert.p11kit}/etc/ssl/trust-source"; + }; + }; + + virtualisation = { + memorySize = 4096; + cores = 2; + }; + }; + + globalTimeout = 2400; + + testScript = '' + ${common.pythonHelpers} + + def chromium(url, profile): + # A fresh profile each time. --dump-dom exits on an interstitial + # too, so the exit status says nothing about trust; the timeout + # only guards against a hang. + machine.execute("su - alice -c " + shlex.quote( + f"rm -rf /tmp/cr-{profile} && " + f"timeout 120 chromium --headless --disable-gpu --no-first-run " + f"--user-data-dir=/tmp/cr-{profile} --dump-dom {url} > /tmp/cr-{profile}.html" + )) + + start_all() + wait_ready(machine, ["demo"]) + machine.succeed("test -x /etc/profiles/per-user/alice/bin/chromium") + machine.succeed( + "jq -e '.CACertificates | length == 1' /etc/chromium/policies/managed/odoo-ca.json" + ) + + with subtest("the untrusted control never gets past TLS"): + chromium("https://untrusted.localhost/control-probe", "untrusted") + machine.succeed( + "grep -E 'SSL_do_handshake\\(\\) failed|peer closed connection in SSL handshake|SSL alert' " + "/var/log/nginx/error.log >/dev/null" + ) + machine.fail("grep control-probe /var/log/nginx/access.log >/dev/null") + + def trusted(probe): + chromium(f"https://demo.{base_domain}/web/login?{probe}=1", probe) + return machine.execute( + f"grep '^demo.{base_domain} \"GET /web/login?{probe}=1 ' /var/log/nginx/access.log | grep Chrome >/dev/null" + )[0] == 0 + + policy = "/etc/chromium/policies/managed/odoo-ca.json" + bundles = "/etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-bundle.crt /etc/pki/tls/certs/ca-bundle.crt" + + with subtest("Chromium opens the instance without a certificate error"): + assert trusted("both-probe"), "Chromium rejected the Odoo CA" + + with subtest("system trust alone is enough"): + machine.succeed(f"cp -L {policy} /tmp/odoo-ca.json && rm {policy}") + machine.succeed( + "openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt /etc/odoo-test-tls/tls.crt" + ) + assert trusted("system-probe"), "Chromium ignored system trust" + + with subtest("with neither, Chromium refuses the CA"): + machine.succeed( + f"for b in {bundles}; do ln -sfn /etc/stock-ca-bundle.crt $b; done && " + "ln -sfn /etc/stock-trust-source /etc/ssl/trust-source && " + "! trust list --filter=ca-anchors | grep odoo-vm-test-ca" + ) + machine.fail( + "openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt /etc/odoo-test-tls/tls.crt" + ) + assert not trusted("neither-probe"), "Chromium trusted the CA from somewhere else" + + with subtest("the policy alone is enough"): + machine.succeed(f"cp /tmp/odoo-ca.json {policy}") + assert trusted("policy-probe"), "Chromium ignored the CACertificates policy" + ''; +}