diff --git a/group_vars/mac/main.yml b/group_vars/mac/main.yml index a32739e..ff0920d 100644 --- a/group_vars/mac/main.yml +++ b/group_vars/mac/main.yml @@ -18,3 +18,9 @@ mac_pmset: disksleep: 0 # never spin down disks autorestart: 1 # power back on after a power cut womp: 1 # wake on network access + +# Hermes's web dashboard: a launchd service listening on 127.0.0.1 only, +# published to the tailnet over HTTPS by Tailscale Serve. +hermes_dashboard_port: 9119 +hermes_dashboard_url: "https://{{ ansible_host }}" +hermes_dashboard_username: admin diff --git a/roles/mac/README.md b/roles/mac/README.md index e2a7b32..c39cabe 100644 --- a/roles/mac/README.md +++ b/roles/mac/README.md @@ -11,15 +11,23 @@ Manages cogitator-02, an Apple Silicon Mac on the tailnet (`mac.yml`, sleep, power back on after a power cut, wake on network. - **Firewall** (tag `firewall`): macOS's application firewall on, with signed software allowed automatically (SSH, BlueBubbles, AirPlay, Tailscale), no - stealth mode and no block-all. Hermes's own Python isn't properly signed, so - it's allowed explicitly (its web dashboard listens on port 9119). Any other - unsigned program that needs incoming connections must be added the same way, - or the firewall silently blocks it. + stealth mode and no block-all. Any unsigned program that needs incoming + connections has to be allowed explicitly, or the firewall silently blocks + it. (Nothing needs that today.) +- **Hermes dashboard** (tag `hermes-dashboard`): a launchd service + (`local.hermes-dashboard`) running `hermes dashboard` on 127.0.0.1:9119, and + Tailscale Serve publishing it at `https://cogitator-02.porgy-porgy.ts.net`, + reachable from the tailnet only. Login is username `admin` with the password + hash and signing secret from `vault_cogitator02`, written into + `~/.hermes/.env` (a plaintext password line there is removed, since it would + override the hash). After setting up Serve, the play checks the dashboard + demands a login through it; if not, it turns Serve off and fails. Ansible logs in as `phil`. sudo needs phil's password, which is in the vault as `vault_cogitator02.become_password`; only tasks with `become: true` use it. phil deliberately doesn't get passwordless sudo, because Hermes Agent runs as phil and takes commands over Telegram. -Not managed here: Hermes Agent (`~/.hermes`, self-updating git install, -launchd agent `ai.hermes.gateway`) and Tailscale. +Not managed here: Hermes Agent itself (`~/.hermes`, a self-updating git +install on the stable channel, launchd agent `ai.hermes.gateway`), its other +settings, and the Tailscale app. diff --git a/roles/mac/handlers/main.yml b/roles/mac/handlers/main.yml new file mode 100644 index 0000000..f75ec39 --- /dev/null +++ b/roles/mac/handlers/main.yml @@ -0,0 +1,6 @@ +- name: Restart Hermes dashboard + community.general.launchd: + name: "{{ hermes_dashboard_label }}" + state: restarted + # Nothing to restart in a dry run (on the first deploy the plist isn't there). + when: not ansible_check_mode diff --git a/roles/mac/tasks/firewall.yml b/roles/mac/tasks/firewall.yml index 6dc29dc..5831aec 100644 --- a/roles/mac/tasks/firewall.yml +++ b/roles/mac/tasks/firewall.yml @@ -26,28 +26,6 @@ when: item.current not in firewall_current become: true -# Hermes's own Python (ad hoc signed) serves its web dashboard on port 9119. -# Its path has a version in it, so every Python Hermes has installed is allowed. -- name: Find Hermes's Python - ansible.builtin.find: - paths: "{{ ansible_user_dir }}/.hermes/tools" - patterns: '^python3\.\d+$' - use_regex: true - recurse: true - depth: 3 - file_type: file - register: hermes_python - -- name: Allow Hermes's Python - ansible.builtin.shell: >- - {{ socketfilterfw }} --add {{ item.path | quote }} && - {{ socketfilterfw }} --unblockapp {{ item.path | quote }} - loop: "{{ hermes_python.files }}" - loop_control: - label: "{{ item.path }}" - when: firewall_current is not search((item.path | regex_escape) ~ '\s*\n\s*\(Allow incoming') - become: true - - name: Turn the firewall on ansible.builtin.command: "{{ socketfilterfw }} --setglobalstate on" when: "'Firewall is enabled' not in firewall_current" diff --git a/roles/mac/tasks/hermes-dashboard.yml b/roles/mac/tasks/hermes-dashboard.yml new file mode 100644 index 0000000..54ec6d5 --- /dev/null +++ b/roles/mac/tasks/hermes-dashboard.yml @@ -0,0 +1,105 @@ +# Hermes never installs a service for its dashboard; this is the launchd +# version of the systemd unit in its docs (user-guide/features/web-dashboard). +# Login: username + scrypt password hash + a fixed signing secret, so sessions +# survive restarts. public_url makes it accept the Tailscale Serve origin. +- name: Dashboard login and public URL in Hermes's .env + ansible.builtin.lineinfile: + path: "{{ hermes_env }}" + regexp: "^{{ item.key }}=" + # Single quotes: Hermes reads .env without interpolation, and the hash has $ in it. + line: "{{ item.key }}='{{ item.value }}'" + mode: "0600" + loop: + - { key: HERMES_DASHBOARD_BASIC_AUTH_USERNAME, value: "{{ hermes_dashboard_username }}" } + - { key: HERMES_DASHBOARD_BASIC_AUTH_PASSWORD_HASH, value: "{{ vault_cogitator02.dashboard_password_hash }}" } + - { key: HERMES_DASHBOARD_BASIC_AUTH_SECRET, value: "{{ vault_cogitator02.dashboard_secret }}" } + - { key: HERMES_DASHBOARD_PUBLIC_URL, value: "{{ hermes_dashboard_url }}" } + no_log: true + diff: false + notify: Restart Hermes dashboard + +# A plaintext password overrides the hash, so it must not be there. +- name: No plaintext dashboard password in .env + ansible.builtin.lineinfile: + path: "{{ hermes_env }}" + regexp: "^HERMES_DASHBOARD_BASIC_AUTH_PASSWORD=" + state: absent + no_log: true + diff: false + notify: Restart Hermes dashboard + +- name: Dashboard launchd service + ansible.builtin.template: + src: hermes-dashboard.plist.j2 + dest: "{{ ansible_user_dir }}/Library/LaunchAgents/{{ hermes_dashboard_label }}.plist" + mode: "0644" + register: dashboard_plist + notify: Restart Hermes dashboard + +- name: Dashboard running + community.general.launchd: + name: "{{ hermes_dashboard_label }}" + state: started + # In a dry run of the first deploy the plist doesn't exist yet. + when: not (ansible_check_mode and dashboard_plist is changed) + +# Restart now on changes, so the login check below tests the new settings. +- name: Apply dashboard changes + ansible.builtin.meta: flush_handlers + +# Tailscale Serve: tailnet-only HTTPS on https://.ts.net, proxied to +# the loopback dashboard. No Ansible module manages it, so read the current +# config and set it only when it differs. --bg keeps it across reboots. +# TAILSCALE_BE_CLI makes the app binary act as the CLI without a terminal. +- name: Read Tailscale Serve config + ansible.builtin.command: + argv: ["{{ tailscale_cli }}", serve, status, --json] + environment: + TAILSCALE_BE_CLI: "1" + register: tailscale_serve + changed_when: false + check_mode: false + +- name: Serve the dashboard on the tailnet over HTTPS + ansible.builtin.command: + argv: ["{{ tailscale_cli }}", serve, --bg, --https=443, "http://127.0.0.1:{{ hermes_dashboard_port }}"] + environment: + TAILSCALE_BE_CLI: "1" + when: >- + (tailscale_serve.stdout | from_json).get('Web', {}).get(ansible_host ~ ':443', {}) + .get('Handlers', {}).get('/', {}).get('Proxy', '') + != 'http://127.0.0.1:' ~ hermes_dashboard_port + +# The dashboard must ask for a login through Serve. If it doesn't, take it +# off the tailnet and stop. +- name: Check the dashboard login is on + when: not ansible_check_mode + block: + - name: Wait for the dashboard to answer through Tailscale Serve + ansible.builtin.uri: + url: "{{ hermes_dashboard_url }}/api/status" + return_content: true + register: dashboard_status + until: dashboard_status.status == 200 + retries: 12 + delay: 5 + + - name: Dashboard requires the username/password login + ansible.builtin.assert: + that: + - dashboard_status.json.auth_required | bool + - "'basic' in dashboard_status.json.auth_providers" + quiet: true + rescue: + - name: Take the dashboard off the tailnet + ansible.builtin.command: + argv: ["{{ tailscale_cli }}", serve, --https=443, "off"] + environment: + TAILSCALE_BE_CLI: "1" + + - name: Stop here + ansible.builtin.fail: + msg: >- + The dashboard didn't answer or doesn't require a login through + {{ hermes_dashboard_url }}, so Tailscale Serve was turned off. + Check ~/.hermes/logs/dashboard.error.log on the Mac. diff --git a/roles/mac/tasks/main.yml b/roles/mac/tasks/main.yml index 072ce56..8018b5f 100644 --- a/roles/mac/tasks/main.yml +++ b/roles/mac/tasks/main.yml @@ -29,3 +29,7 @@ - name: Firewall ansible.builtin.import_tasks: firewall.yml tags: firewall + +- name: Hermes dashboard + ansible.builtin.import_tasks: hermes-dashboard.yml + tags: hermes-dashboard diff --git a/roles/mac/templates/hermes-dashboard.plist.j2 b/roles/mac/templates/hermes-dashboard.plist.j2 new file mode 100644 index 0000000..770f549 --- /dev/null +++ b/roles/mac/templates/hermes-dashboard.plist.j2 @@ -0,0 +1,38 @@ + + + + + + Label + {{ hermes_dashboard_label }} + ProgramArguments + + {{ ansible_user_dir }}/.local/bin/hermes + dashboard + --host + 127.0.0.1 + --port + {{ hermes_dashboard_port }} + --no-open + + --skip-build + + EnvironmentVariables + + HERMES_HOME + {{ ansible_user_dir }}/.hermes + PATH + {{ ansible_user_dir }}/.local/bin:/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin + + RunAtLoad + + KeepAlive + + ThrottleInterval + 30 + StandardOutPath + {{ ansible_user_dir }}/.hermes/logs/dashboard.log + StandardErrorPath + {{ ansible_user_dir }}/.hermes/logs/dashboard.error.log + + diff --git a/roles/mac/vars/main.yml b/roles/mac/vars/main.yml index 458a0f3..ec5870d 100644 --- a/roles/mac/vars/main.yml +++ b/roles/mac/vars/main.yml @@ -1 +1,4 @@ socketfilterfw: /usr/libexec/ApplicationFirewall/socketfilterfw +hermes_env: "{{ ansible_user_dir }}/.hermes/.env" +hermes_dashboard_label: local.hermes-dashboard +tailscale_cli: /Applications/Tailscale.app/Contents/MacOS/Tailscale