diff --git a/group_vars/mac/main.yml b/group_vars/mac/main.yml index 2902faa..a32739e 100644 --- a/group_vars/mac/main.yml +++ b/group_vars/mac/main.yml @@ -10,3 +10,11 @@ homebrew_formulae: homebrew_casks: - beeper - claude-code + +# Power settings (pmset, AC power). The Mac had `sleep 1` and only stayed up +# because BlueBubbles runs caffeinate; this keeps it up on its own. +mac_pmset: + sleep: 0 # never sleep + disksleep: 0 # never spin down disks + autorestart: 1 # power back on after a power cut + womp: 1 # wake on network access diff --git a/roles/mac/README.md b/roles/mac/README.md index 4d8fa86..e2a7b32 100644 --- a/roles/mac/README.md +++ b/roles/mac/README.md @@ -3,8 +3,18 @@ Manages cogitator-02, an Apple Silicon Mac on the tailnet (`mac.yml`, `PLAYBOOK=mac.yml bin/deploy`). -- **Homebrew**: taps, formulae and casks listed in `group_vars/mac/main.yml`. - Installs what's missing; never upgrades or removes. +- **Homebrew** (tag `homebrew`): taps, formulae and casks listed in + `group_vars/mac/main.yml`. Installs what's missing; never upgrades or removes. +- **SSH key** (tag `ssh`): your local `~/.ssh/id_ed25519.pub` in phil's + `authorized_keys`. Other keys there are left alone. +- **Power** (tag `power`): `mac_pmset` in `group_vars/mac/main.yml`: never + 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. 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. diff --git a/roles/mac/tasks/firewall.yml b/roles/mac/tasks/firewall.yml new file mode 100644 index 0000000..6dc29dc --- /dev/null +++ b/roles/mac/tasks/firewall.yml @@ -0,0 +1,54 @@ +# macOS's application firewall. It filters incoming connections per program: +# Apple's and other properly signed apps are allowed automatically (SSH, +# BlueBubbles, AirPlay, Tailscale), unsigned ones are blocked. Nothing is +# asked on screen because nobody sits at this Mac, so an unsigned program +# that listens has to be allowed here. +- name: Read firewall settings + ansible.builtin.command: "{{ socketfilterfw }} --{{ item }}" + loop: [getglobalstate, getallowsigned, getstealthmode, getblockall, listapps] + register: firewall_read + changed_when: false + check_mode: false + +- name: Note the current firewall settings + ansible.builtin.set_fact: + firewall_current: "{{ firewall_read.results | map(attribute='stdout') | join('\n') }}" + +- name: Allow signed software, no stealth mode, don't block everything + ansible.builtin.command: "{{ socketfilterfw }} {{ item.set }}" + loop: + - { set: --setallowsigned on, current: built-in signed software ENABLED } + - { set: --setallowsignedapp on, current: downloaded signed software ENABLED } + - { set: --setstealthmode off, current: stealth mode is off } + - { set: --setblockall off, current: block all state set to disabled } + loop_control: + label: "{{ item.set }}" + 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" + become: true diff --git a/roles/mac/tasks/main.yml b/roles/mac/tasks/main.yml index 718764d..072ce56 100644 --- a/roles/mac/tasks/main.yml +++ b/roles/mac/tasks/main.yml @@ -15,3 +15,17 @@ name: "{{ homebrew_casks }}" state: present tags: homebrew + +- name: SSH key + ansible.posix.authorized_key: + user: "{{ ansible_user }}" + key: "{{ lookup('file', lookup('env','HOME') + '/.ssh/id_ed25519.pub') }}" + tags: ssh + +- name: Power settings + ansible.builtin.import_tasks: power.yml + tags: power + +- name: Firewall + ansible.builtin.import_tasks: firewall.yml + tags: firewall diff --git a/roles/mac/tasks/power.yml b/roles/mac/tasks/power.yml new file mode 100644 index 0000000..b542b92 --- /dev/null +++ b/roles/mac/tasks/power.yml @@ -0,0 +1,13 @@ +- name: Read power settings + ansible.builtin.command: pmset -g custom + register: pmset_current + changed_when: false + check_mode: false + +- name: Set power settings + ansible.builtin.command: pmset -a {{ item.key }} {{ item.value }} + loop: "{{ mac_pmset | dict2items }}" + when: >- + (pmset_current.stdout | regex_search('(?m)^\\s*' ~ item.key ~ '\\s+(\\S+)', '\\1') + | default([''], true) | first) != (item.value | string) + become: true diff --git a/roles/mac/vars/main.yml b/roles/mac/vars/main.yml new file mode 100644 index 0000000..458a0f3 --- /dev/null +++ b/roles/mac/vars/main.yml @@ -0,0 +1 @@ +socketfilterfw: /usr/libexec/ApplicationFirewall/socketfilterfw