diff --git a/CLAUDE.md b/CLAUDE.md index c13831c8..2fabede9 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -50,10 +50,10 @@ just add-machine NAME # Onboard + bootstrap an already-reachable machine via S `site.yml` runs roles in tagged phases. Notable wiring: - `bitwarden` resolves `BW_SESSION` (env, `/tmp/bw_session`, or interactive unlock), runs `bw sync` with a 15s timeout, and sets a `bw_unlocked` host fact. All BW-using roles gate on `when: bw_unlocked | default(false)`. -- `kube` fetches `~/.kube/config` + `~/.talos/config` from Bitwarden — desktops only. +- the `kube` unit fetches `~/.kube/config` + `~/.talos/config` from Bitwarden — desktops only. - `ssh_keys` round-trips per-machine ed25519 keys through Bitwarden, building cross-machine `authorized_keys`. -After the playbook, `just apply*` runs bash **units** (`units/`, via mise through `scripts/run-units.sh`; see [`docs/src/units.md`](docs/src/units.md)): `omp`, `chezmoi`, `atuin`, `shell-ai`. `home/` is the chezmoi source for shell, git, neovim and atuin config; `ssh_keys` remains an Ansible role. +After the playbook, `just apply*` runs bash **units** (`units/`, via mise through `scripts/run-units.sh`; see [`docs/src/units.md`](docs/src/units.md)): `chezmoi`, `fonts`, `omp`, `atuin`, `shell-ai`, `github`, `kube`, `forgejo-registry`. `home/` is the chezmoi source for shell, git, neovim and atuin config; `ssh_keys` remains an Ansible role. Roles are conditional on inventory group (`desktop`, `server`, `vps`, `test_fleet`, `termux_hosts`; `llm` is dormant/commented out) and on `skip_*` flags / `machine_profile` in `host_vars/`. See [`docs/src/roles/`](docs/src/roles/) for the full reference. diff --git a/CONTEXT.md b/CONTEXT.md index 3ff8f72c..3867b112 100644 --- a/CONTEXT.md +++ b/CONTEXT.md @@ -21,7 +21,7 @@ ## Playbook - **Role** — an Ansible role that configures one concern (e.g. `tailscale`, `proxy`, `ssh_keys`). The interface is `tasks/main.yml`; the implementation is templates, files, and handlers. -- **Unit** — an idempotent bash script in `units/` run in parallel by mise after the playbook (`omp`, `chezmoi`, `atuin`, `shell-ai`). A failing step does not stop the rest of the unit. See `docs/src/units.md`. +- **Unit** — an idempotent bash script in `units/` run in parallel by mise after the playbook (`chezmoi`, `fonts`, `omp`, `atuin`, `shell-ai`, `github`, `kube`, `forgejo-registry`). A failing step does not stop the rest of the unit. See `docs/src/units.md`. - **Phase** — a logical grouping of roles in `site.yml` (Phase 1: system + packages, Phase 2: secrets + auth, Phase 3: desktop, Phase 4: services). Phases are documented in comments, not enforced by the playbook. - **Tag** — an Ansible tag applied to roles so `just apply-tags ` can target a subset. diff --git a/docs/src/SUMMARY.md b/docs/src/SUMMARY.md index a7ea7562..21c72189 100644 --- a/docs/src/SUMMARY.md +++ b/docs/src/SUMMARY.md @@ -60,7 +60,7 @@ ## Dotfiles -- [shell_fonts](roles/shell_fonts.md) +- [shell_fonts (unit)](roles/shell_fonts.md) - [shell_dotfiles (chezmoi)](roles/shell_dotfiles.md) - [shell_atuin (chezmoi + unit)](roles/shell_atuin.md) - [shell_ai (unit)](roles/shell_ai.md) @@ -74,10 +74,10 @@ - [bitwarden](roles/bitwarden.md) - [ssh_keys](roles/ssh_keys.md) - [ssh_mesh](roles/ssh_mesh.md) -- [github](roles/github.md) +- [github (unit)](roles/github.md) - [tailscale](roles/tailscale.md) -- [kube](roles/kube.md) -- [forgejo_registry](roles/forgejo_registry.md) +- [kube (unit)](roles/kube.md) +- [forgejo_registry (unit)](roles/forgejo_registry.md) ## Desktop diff --git a/docs/src/architecture.md b/docs/src/architecture.md index 789bbeaa..d096c171 100644 --- a/docs/src/architecture.md +++ b/docs/src/architecture.md @@ -62,15 +62,11 @@ Every role in `site.yml` carries one or more tags. Tags fall into two kinds: **p | System + packages | `homebrew` | `packages`, `homebrew` | | System + packages | `termux_packages` | `system`, `packages`, `termux` | | System + packages | `bitwarden` | `secrets`, `bitwarden` | -| System + packages | `shell_fonts` | `dotfiles`, `shell`, `fonts` | | System + packages | `omp` unit | `dotfiles`, `omp` | | System + packages | `hive_ops` | `hive`, `hive_ops` | | Secrets + auth | `ssh_keys` | `secrets`, `ssh`, `ssh_keys` | | Secrets + auth | `ssh_mesh` | `ssh`, `mesh_check` | -| Secrets + auth | `github` | `secrets`, `github` | | Secrets + auth | `tailscale` | `secrets`, `tailscale` | -| Secrets + auth | `kube` | `secrets`, `kube` | -| Secrets + auth | `forgejo_registry` | `secrets`, `forgejo`, `registry` | | Desktop apps | `flatpak` | `packages`, `desktop`, `flatpak` | | Desktop apps | `bluefin_common` | `desktop`, `bluefin` | | Desktop apps | `gnome` | `desktop`, `gnome` | @@ -86,11 +82,11 @@ Every role in `site.yml` carries one or more tags. Tags fall into two kinds: **p | Services | `server_hardening` | `system`, `hardening`, `security` | | Services | `fleet_facts` | `facts`, `fleet_facts` | -Shell, git, neovim and atuin config, omp, and the shell/atuin secrets are not roles: they are [units](units.md) (`chezmoi`, `omp`, `atuin`, `shell-ai`) that `just apply*` runs after the playbook. The old tags `dotfiles`, `shell`, `git`, `neovim`, `editor`, `atuin`, `ai`, `secrets`, `omp` still select them through `scripts/run-units.sh`. +Shell, git, neovim and atuin config, fonts, omp, the shell/atuin/GitHub/Forgejo secrets and the kubeconfigs are not roles: they are [units](units.md) (`chezmoi`, `fonts`, `omp`, `atuin`, `shell-ai`, `github`, `kube`, `forgejo-registry`) that `just apply*` runs after the playbook. The old tags `dotfiles`, `shell`, `fonts`, `git`, `neovim`, `editor`, `atuin`, `ai`, `secrets`, `github`, `kube`, `forgejo`, `registry`, `omp` still select them through `scripts/run-units.sh`. Common phase-wide selections: -- `--tags secrets` — every BW-backed role only (`bitwarden`, `ssh_keys`, `github`, `tailscale`, `kube`, `forgejo_registry`) +- `--tags secrets` — every BW-backed role only (`bitwarden`, `ssh_keys`, `tailscale`; the `atuin`, `shell-ai`, `github`, `kube` and `forgejo-registry` units also run for this tag) - `--tags desktop` — every desktop-app role (`flatpak`, `bluefin_common`, `gnome`, `zen_browser`, `pipewire_audio`) - `--tags services` — every long-running service role (`syncthing`, `systemd`, `bst_dashboard`, `kirocrew`, `proxy`, `tailscale_cert`) - `--skip-tags secrets` — everything except BW-backed roles. `just apply-nosecrets`, bootstrap's first phase and `phone-apply.service` also pass `--no-secrets` to units. @@ -129,8 +125,8 @@ Every flag `site.yml` reads, all defaulting to `false` (`| default(false)`). Set |------|--------------|---------------------------------|---------------------| | `skip_sudo` | `sudo` | — | `termux` | | `skip_ssh_mesh` | `ssh_mesh` | not `vm-test` | — | -| `skip_kube` | `kube` | desktop, not `vm-test` | `mumbai`, `termux` | -| `skip_forgejo` | `forgejo_registry` | not `vm-test` | — | +| `skip_kube` | `kube` unit | desktop, not `vm-test` | `mumbai`, `termux` | +| `skip_forgejo` | `forgejo-registry` unit | not `vm-test` | — | | `skip_flatpak` | `flatpak` | desktop, not `vm-test` | `mumbai`, `termux` | | `skip_bluefin` | `bluefin_common` | desktop, not `vm-test` | `mumbai`, `termux` | | `skip_gnome` | `gnome` | desktop, not `vm-test` | `mumbai`, `termux` | diff --git a/docs/src/index.md b/docs/src/index.md index 68bae622..908bf504 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -65,9 +65,9 @@ The `llm` group is dormant (commented out in `inventory.yml`). | Phase | Roles | Main tags | |-------|-------|-----------| -| 1 — System + packages + dotfiles | sudo, sshd, apk_packages, homebrew, termux_packages, bitwarden, shell_fonts, hive_ops | `system`, `packages`, `dotfiles` | -| 2 — Secrets + auth | ssh_keys, ssh_mesh, github, tailscale, kube, forgejo_registry | `secrets` | +| 1 — System + packages + dotfiles | sudo, sshd, apk_packages, homebrew, termux_packages, bitwarden, hive_ops | `system`, `packages`, `dotfiles` | +| 2 — Secrets + auth | ssh_keys, ssh_mesh, tailscale | `secrets` | | 3 — Desktop apps | flatpak, bluefin_common, gnome, zen_browser (+ browser_fxa), pipewire_audio | `desktop` | | 4 — Services | syncthing, systemd, bst_dashboard, kirocrew, proxy, tailscale_cert, server_hardening | `services` | -After the playbook, `just apply*` runs the [bash units](units.md) (`chezmoi`, `omp`, `atuin`, `shell-ai`) in parallel through mise. The `omp` unit also uses Bitwarden for its broker token. `just apply-nosecrets` skips secret-tagged roles and passes `--no-secrets` to units. +After the playbook, `just apply*` runs the [bash units](units.md) (`chezmoi`, `fonts`, `omp`, `atuin`, `shell-ai`, `github`, `kube`, `forgejo-registry`) in parallel through mise. The `omp` unit also uses Bitwarden for its broker token. `just apply-nosecrets` skips secret-tagged roles and passes `--no-secrets` to units. diff --git a/docs/src/roles/forgejo_registry.md b/docs/src/roles/forgejo_registry.md index 6b8fc7a0..b300c8bf 100644 --- a/docs/src/roles/forgejo_registry.md +++ b/docs/src/roles/forgejo_registry.md @@ -1,29 +1,21 @@ # forgejo_registry +**Unit:** `forgejo-registry` (`units/forgejo-registry.sh`; see [Units](../units.md)) **Tags:** `secrets`, `forgejo`, `registry` -**Secrets needed:** Yes (Login item named after the registry host) -**Runs on:** All machines except `machine_profile: vm-test` (unless `skip_forgejo: true`) +**Secrets needed:** Yes (login item named after the registry host) +**Runs on:** All machines except `machine_profile: vm-test` or `skip_forgejo: true` in `host_vars` Logs podman in to the private Forgejo container registry. -## What It Does +## What it does -When `bw_unlocked` and `podman` is on `PATH`: - -1. Checks `podman login --get-login {{ forgejo_registry }}` — skips if already logged in -2. Fetches `bw get username` / `bw get password` for the item named `{{ forgejo_registry }}` -3. Runs `podman login … --password-stdin` -4. Warns if the Bitwarden item is missing +1. `SKIPPED:` unless `podman` is installed; no-op when `podman login --get-login ` already succeeds. +2. With an unlocked vault: `bw get username` / `bw get password` for the item named after the registry, then `podman login -u --password-stdin`. A missing item prints `PARTIAL:` and is not a failure. ## Configuration -```yaml -# roles/forgejo_registry/defaults/main.yml -forgejo_registry: forgejo.manatee-basking.ts.net -``` - -The Bitwarden Login item's name must equal this value; its password is a registry/package token. +The registry host is `FORGEJO_REGISTRY` in `units/forgejo-registry.sh` (default `forgejo.manatee-basking.ts.net`, overridable from the environment). The Bitwarden login item's name must equal it; its password is a registry/package token. ## Notes -- Forgejo runs on the home Talos cluster, which is currently powered down — a fresh `podman login` can't succeed until it returns. Hosts already logged in skip the step. +- Forgejo ran on the home cluster, which is powered down, so a fresh `podman login` cannot succeed until it returns. Hosts already logged in skip the step. diff --git a/docs/src/roles/github.md b/docs/src/roles/github.md index b36243e3..c8bf9686 100644 --- a/docs/src/roles/github.md +++ b/docs/src/roles/github.md @@ -1,38 +1,39 @@ # github -**Tags:** `secrets`, `github` -**Secrets needed:** Yes (only on first auth — once `gh` is authenticated, BW is not required) -**Runs on:** All machines with `gh` installed +**Unit:** `github` (`units/github.sh`; see [Units](../units.md)) +**Tags:** `secrets`, `github` +**Secrets needed:** Only on first auth; once `gh` is authenticated Bitwarden is not used +**Runs on:** All machines with `gh` installed (`SKIPPED:` otherwise) -Logs `gh` in via a BW-stored PAT (only if not already authenticated), then registers this host's SSH key with GitHub for both auth and signing. +Logs `gh` in via a Bitwarden-stored PAT (only if not already authenticated), then registers this host's SSH key with GitHub for both authentication and signing. With `--no-secrets`, only the local known-hosts step runs; the unit does not invoke `gh` or Bitwarden. -## What it does +## Steps -1. Ensures `github.com`'s ed25519 host key is in `~/.ssh/known_hosts` (prevents MITM warnings on first push). -2. `gh auth status` — if already authenticated, skip the token fetch entirely (no BW dependency for steady-state). -3. If not authenticated and BW is unlocked → `bw get password github-token` → `gh auth login --with-token`. If the BW item is missing, emits a one-line warning and continues. -4. Sets `gh config set git_protocol ssh`. -5. Lists existing keys on the GitHub account. For each key type (auth + signing), runs `gh ssh-key add` if this host's key isn't already there. +1. `github known_hosts`: ensure github.com's ed25519 host key is in `~/.ssh/known_hosts`. +2. `gh login`: skipped when `gh auth status` succeeds. Otherwise, with an unlocked vault, `bw get password github-token` → `gh auth login --with-token`. Locked vault or missing item prints a `PARTIAL:` line. +3. `register SSH keys`: needs a token with `admin:public_key` and `admin:ssh_signing_key` (otherwise a `PARTIAL:` line with the regenerate-PAT link). For each of authentication and signing, `gh ssh-key add ~/.ssh/id_ed25519.pub --title -auth|-signing` unless `gh ssh-key list` already shows the key with that type. If the list fails, the unit fails without registering either key. + +GitHub CLI configuration, including `git_protocol: ssh`, is managed by chezmoi, not this unit. ## Failure modes | Symptom | Cause | Fix | |---|---|---| -| `gh auth status` says `not logged in` after apply, vault locked | First run on this host with BW locked | Unlock BW and re-run `just apply-tags github`, or `gh auth login` manually | -| Warning: `No 'github-token' item found` | BW vault doesn't have the PAT | Create a BW *login* item named `github-token` with your PAT as the password | -| Key registration silently fails | `gh ssh-key add` returned non-zero (failed_when:false) — possibly key was just deleted on github.com side | Inspect with `gh ssh-key list` and re-run the role | -| Stale keys clutter github.com | This host's previous key was rotated; the old gh entry stays forever | Delete the old key manually at — no automated cleanup yet | +| `PARTIAL: gh not logged in; vault locked` | First run on this host with Bitwarden locked | `bw unlock`, then `just apply-tags github`, or `gh auth login` | +| `PARTIAL: no 'github-token' item` | Vault has no PAT | Create a login item `github-token` with the PAT as the password | +| `PARTIAL: GitHub token lacks admin:public_key / admin:ssh_signing_key` | PAT scopes | Regenerate the PAT with those scopes and update the item | +| `warn: failed to list GitHub SSH keys; registration skipped` | `gh ssh-key list` failed | Check GitHub access and re-run; no keys were added | +| `warn: failed to upload SSH ... key` | `gh ssh-key add` failed | Inspect with `gh ssh-key list` and re-run | -## How to verify +## Verify ```bash -gh auth status # Should say "Logged in" with this account -gh ssh-key list | grep "$(hostname)" # Should show -auth and -signing -git log --show-signature -1 # Latest commit should show "Good \"git\" signature for ..." +gh auth status +gh ssh-key list | grep "$(cat /etc/dotfiles-machine)" # -auth and -signing ``` ## Notes -- Depends on the `ssh_keys` role running first (the key must exist on disk). -- The signing key registration enables the "Verified" badge on commits — only if your local `~/.gitconfig` is also set to sign with the SSH key (the `git` role handles this). -- The PAT in BW needs `admin:public_key`, `write:gpg_key`, and `read:user` scopes for the registrations to succeed. +- Needs `~/.ssh/id_ed25519.pub`, written by the `ssh_keys` role; the units run after the playbook, so a first apply gets both in one pass. +- The "Verified" badge also needs the signing config in `~/.gitconfig` (the chezmoi-managed [git](git.md) config). +- Stale keys from rotated machine keys are not cleaned up: delete them at . diff --git a/docs/src/roles/kube.md b/docs/src/roles/kube.md index d6dd5028..baa75eb7 100644 --- a/docs/src/roles/kube.md +++ b/docs/src/roles/kube.md @@ -1,32 +1,30 @@ # kube -**Tags:** `secrets`, `kube` -**Secrets needed:** Yes -**Runs on:** Desktop group only (unless `skip_kube: true`) +**Unit:** `kube` (`units/kube.sh`; see [Units](../units.md)) +**Tags:** `secrets`, `kube` +**Secrets needed:** Yes +**Runs on:** Desktops only (`SKIPPED:` otherwise, or with `skip_kube: true` / `machine_profile: vm-test` in `host_vars`) -Pulls the kube + Talos configs for both Talos clusters from BW so any desktop in the fleet can talk to them: the home cluster (Bihar + Karnataka — powered down, expected back) and the [AWS cluster](../servers/aws-k8s/cluster.md). +Pulls the kube + Talos configs for both clusters from Bitwarden so any desktop in the fleet can talk to them: the home cluster (Bihar + Karnataka, powered down, expected back) and the [AWS cluster](../servers/aws-k8s/cluster.md) (its nodes were terminated on 2026-10-25; the configs are kept). -## What it does +## Steps -When `bw_unlocked` is true: +With an unlocked vault (a locked vault or `--no-secrets` prints `SKIPPED:` and leaves existing files alone): -1. Ensures `~/.kube/` and `~/.talos/` exist (mode 0700). -2. `bw get notes kubeconfig` → writes to `~/.kube/config` (0600). Skipped if BW returns empty. -3. `bw get notes talosconfig` → writes to `~/.talos/config` (0600). Skipped if BW returns empty. -4. Pins `tls-server-name` on every cluster in `~/.kube/config` to `kube_tls_server_name` (default: bihar's LAN address) — the home API-server cert lacks the tailnet address in its SANs. -5. Emits a one-line warning if either home-cluster note is missing in BW. -6. `bw get notes kubeconfig-aws-migration` → `~/.kube/config-aws-migration` and `bw get notes talosconfig-aws-migration` → `~/.talos/config-aws-migration` (0600, each skipped if empty). Kept as separate files so both clusters stay independently usable. - -When the vault is locked the role no-ops cleanly — existing local configs are preserved. +1. `kube dirs`: `~/.kube/` and `~/.talos/` (0700). +2. `home kubeconfig`: `bw get notes kubeconfig` → `~/.kube/config` (0600). A missing note prints `warn:` and leaves the file as it is. +3. `home talosconfig`: `bw get notes talosconfig` → `~/.talos/config` (0600). +4. `pin tls-server-name`: after writing the home kubeconfig, every cluster in it gets `--tls-server-name=$KUBE_TLS_SERVER_NAME` (`units/kube.conf`, default bihar's LAN address `192.168.0.5`): the home API-server cert lacks the tailnet address in its SANs. +5. `aws kubeconfig` / `aws talosconfig`: `kubeconfig-aws-migration` → `~/.kube/config-aws-migration`, `talosconfig-aws-migration` → `~/.talos/config-aws-migration`. Separate files keep both clusters independently usable. ## Failure modes | Symptom | Cause | Fix | |---|---|---| | `kubectl` returns `Unable to connect to the server` after onboarding | Vault was locked, configs never deployed | Unlock BW, re-run `just apply-tags kube` | -| Warning: `kubeconfig missing in BW` | Vault was never seeded | Run `just seed-kube` from a host that already has a working config | +| `warn: kubeconfig ... missing in BW` | Vault was never seeded | Run `just seed-kube` from a host that already has a working config | | Configs deployed but auth fails | Cluster PKI rotated — local configs are stale | Re-run `just seed-kube` from a healthy host, then re-apply on consumers | -| Role skipped silently on a server host that should have it | `skip_kube: true` in `host_vars/.yml`, or host not in `desktop` group | Adjust inventory or host_vars | +| Unit prints `SKIPPED: not a desktop` on a host that should have it | `skip_kube: true` in `host_vars/.yml`, or host not in the `desktop` group | Adjust inventory or host_vars | ## Seeding BW @@ -48,4 +46,4 @@ KUBECONFIG=~/.kube/config-aws-migration kubectl get nodes # AWS cluster ## Notes - Both files contain cluster PKI material; the repo's `.gitignore` excludes the cluster source files, and these caches live in `~/` only — never commit. -- The role is gated by `is_desktop | bool and not skip_kube`. Servers don't get a kubeconfig — the cluster nodes themselves are Talos and not in the inventory. +- Gated by desktop membership and `skip_kube`. Servers don't get a kubeconfig; the cluster nodes are not in the inventory. diff --git a/docs/src/roles/shell_fonts.md b/docs/src/roles/shell_fonts.md index f794d80e..ad9fe917 100644 --- a/docs/src/roles/shell_fonts.md +++ b/docs/src/roles/shell_fonts.md @@ -1,19 +1,19 @@ # shell_fonts +**Unit:** `fonts` (`units/fonts.sh`; see [Units](../units.md)) **Tags:** `dotfiles`, `shell`, `fonts` **Secrets needed:** No -**Runs on:** All machines (font install only when `is_desktop`) +**Runs on:** All machines (the font install only on desktops) Installs the [JetBrains Mono Nerd Font](https://github.com/ryanoasis/nerd-fonts) for terminals and the prompt. -## What It Does +## Steps -1. Ensures `~/.local/share/fonts/` exists (every host) -2. On desktops, if `JetBrainsMono/JetBrainsMonoNerdFont-Regular.ttf` is missing: - downloads the latest `JetBrainsMono.zip` release, unpacks it into - `~/.local/share/fonts/JetBrainsMono/`, and runs `fc-cache -f` +1. `font directory`: `~/.local/share/fonts/` (every host). +2. On desktops, if `JetBrainsMono/JetBrainsMonoNerdFont-Regular.ttf` is missing: `install JetBrains Mono Nerd Font` downloads the latest `JetBrainsMono.zip` release into a temp dir, unpacks it into `~/.local/share/fonts/JetBrainsMono/`, then `font cache` (soft) runs `fc-cache -f`. ## Notes -- Idempotent on the presence of the Regular `.ttf` — delete it to force a re-download -- The `gnome` role sets Ptyxis to `JetBrainsMono Nerd Font 12`, which depends on this +- Idempotent on the presence of the Regular `.ttf`; delete it to force a re-download. +- Desktop means: `DOTFILES_IS_DESKTOP`, else `is_desktop` in `host_vars`, else membership of the `desktop` group in `inventory.yml`. +- The `gnome` role sets Ptyxis to `JetBrainsMono Nerd Font 12`, which depends on this. diff --git a/docs/src/servers/aws-k8s/cluster.md b/docs/src/servers/aws-k8s/cluster.md index 9c5c5292..7685b278 100644 --- a/docs/src/servers/aws-k8s/cluster.md +++ b/docs/src/servers/aws-k8s/cluster.md @@ -142,7 +142,7 @@ talosctl --talosconfig ~/.talos/config-aws-migration \ ``` Configs live in Bitwarden as `kubeconfig-aws-migration` / `talosconfig-aws-migration` -and are fetched by the [`kube`](../../roles/kube.md) role into +and are fetched by the [`kube`](../../roles/kube.md) unit into `~/.kube/config-aws-migration` and `~/.talos/config-aws-migration` — kept separate from the home cluster's files so both stay independently usable. diff --git a/mise.toml b/mise.toml index 267f10f8..e180fb14 100644 --- a/mise.toml +++ b/mise.toml @@ -17,3 +17,19 @@ file = "units/atuin.sh" [tasks."unit:shell-ai"] description = "shell-ai: API secrets from Bitwarden into the shell secret files" file = "units/shell-ai.sh" + +[tasks."unit:fonts"] +description = "fonts: JetBrains Mono Nerd Font on desktops" +file = "units/fonts.sh" + +[tasks."unit:github"] +description = "github: gh login from Bitwarden and SSH key registration" +file = "units/github.sh" + +[tasks."unit:kube"] +description = "kube: kubeconfig and talosconfig for the home and AWS clusters from Bitwarden" +file = "units/kube.sh" + +[tasks."unit:forgejo-registry"] +description = "forgejo-registry: podman login to the private Forgejo registry" +file = "units/forgejo-registry.sh" diff --git a/roles/forgejo_registry/defaults/main.yml b/roles/forgejo_registry/defaults/main.yml deleted file mode 100644 index 057ae3af..00000000 --- a/roles/forgejo_registry/defaults/main.yml +++ /dev/null @@ -1,4 +0,0 @@ ---- -# Forgejo container registry on the tailnet. The matching Bitwarden login item -# (name == forgejo_registry) holds the username + a registry/package token. -forgejo_registry: forgejo.manatee-basking.ts.net diff --git a/roles/forgejo_registry/tasks/main.yml b/roles/forgejo_registry/tasks/main.yml deleted file mode 100644 index e3541c86..00000000 --- a/roles/forgejo_registry/tasks/main.yml +++ /dev/null @@ -1,60 +0,0 @@ ---- -# Automate `podman login` to the private Forgejo container registry using the -# credential stored in Bitwarden (item name == {{ forgejo_registry }}). -# Idempotent: skips if already logged in. Gated on an unlocked vault. - -- name: Set up Forgejo registry login - when: bw_unlocked | default(false) - environment: - BW_SESSION: "{{ bw_session | default('') }}" - PATH: "/home/linuxbrew/.linuxbrew/bin:{{ ansible_facts['env']['PATH'] }}" - block: - - name: Check podman is available - ansible.builtin.shell: command -v podman - register: _podman_bin - changed_when: false - failed_when: false - - - name: Check existing Forgejo registry login - ansible.builtin.command: "podman login --get-login {{ forgejo_registry }}" - register: _fj_login - changed_when: false - failed_when: false - when: _podman_bin.rc == 0 - - - name: Fetch Forgejo registry username from Bitwarden - ansible.builtin.command: "bw get username {{ forgejo_registry }}" - register: _fj_user - changed_when: false - no_log: true - failed_when: false - when: _podman_bin.rc == 0 and _fj_login.rc != 0 - - - name: Fetch Forgejo registry password from Bitwarden - ansible.builtin.command: "bw get password {{ forgejo_registry }}" - register: _fj_pass - changed_when: false - no_log: true - failed_when: false - when: _podman_bin.rc == 0 and _fj_login.rc != 0 - - - name: Podman login to Forgejo registry - ansible.builtin.command: - cmd: "podman login {{ forgejo_registry }} -u {{ _fj_user.stdout | trim }} --password-stdin" - stdin: "{{ _fj_pass.stdout }}" - register: _fj_do - changed_when: "'Login Succeeded' in _fj_do.stdout" - no_log: true - when: - - _podman_bin.rc == 0 - - _fj_login.rc != 0 - - _fj_user.rc == 0 and (_fj_user.stdout | length) > 0 - - _fj_pass.rc == 0 and (_fj_pass.stdout | length) > 0 - - - name: Warn if Forgejo credential missing in Bitwarden - ansible.builtin.debug: - msg: "Forgejo registry credential ({{ forgejo_registry }}) not found in Bitwarden — add a login item named '{{ forgejo_registry }}' with the registry token." - when: - - _podman_bin.rc == 0 - - _fj_login.rc != 0 - - (_fj_user.rc | default(1)) != 0 or (_fj_pass.rc | default(1)) != 0 diff --git a/roles/github/tasks/main.yml b/roles/github/tasks/main.yml deleted file mode 100644 index 1e1af3cc..00000000 --- a/roles/github/tasks/main.yml +++ /dev/null @@ -1,157 +0,0 @@ ---- -- name: Check if gh CLI exists - ansible.builtin.stat: - path: "{{ brew_prefix }}/bin/gh" - register: gh_binary - -- name: GitHub authentication and key registration - when: gh_binary.stat.exists - environment: - PATH: "{{ ansible_env_path }}" - BW_SESSION: "{{ bw_session | default('') }}" - block: - # ── Authentication ── - - name: Ensure GitHub host key is in known_hosts - ansible.builtin.known_hosts: - name: github.com - key: "github.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl" - state: present - - - name: Check gh auth status - ansible.builtin.command: gh auth status - register: gh_auth_status - changed_when: false - failed_when: false - - - name: Authenticate with GitHub via Bitwarden token - when: - - gh_auth_status.rc != 0 - - bw_unlocked | default(false) - block: - - name: Fetch GitHub token from Bitwarden - ansible.builtin.command: bw get password github-token - register: gh_token - changed_when: false - failed_when: false - no_log: true - - - name: Warn if GitHub token not found in Bitwarden - ansible.builtin.debug: - msg: >- - No 'github-token' item found in Bitwarden — skipping GitHub auth. - Add a login item named 'github-token' with your PAT as the password, - then re-run 'just apply'. - when: gh_token.rc != 0 or (gh_token.stdout | default('')) | length == 0 - - - name: Login to GitHub with token - ansible.builtin.command: gh auth login --with-token - args: - stdin: "{{ gh_token.stdout }}" - no_log: true - when: - - gh_token.rc == 0 - - (gh_token.stdout | default('')) | length > 0 - - - name: Set git protocol to SSH - ansible.builtin.command: gh config set git_protocol ssh - changed_when: false - - # ── SSH key registration ───────────────────────────────────── - # Checks token scopes upfront so missing permissions produce a - # clear warning instead of a silent HTTP 404 on gh ssh-key add. - - name: Check GitHub token scopes for key management - ansible.builtin.shell: | - set -o pipefail - gh auth status -t 2>&1 | grep -o "Token scopes:.*" || true - register: gh_token_scopes - changed_when: false - failed_when: false - - # Use a fact gate rather than separate when:/failed_when: on each - # downstream task so the scope check runs once and the result - # controls both the gh ssh-key list call and the two upload tasks - # uniformly — otherwise a scope-less token gets two silent 404s. - - name: Set key-scope fact - ansible.builtin.set_fact: - gh_has_key_scopes: >- - {{ ("admin:public_key" in gh_token_scopes.stdout | default("")) - and ("admin:ssh_signing_key" in gh_token_scopes.stdout | default("")) }} - - - name: Warn about missing GitHub token scopes for SSH key upload - ansible.builtin.debug: - msg: >- - ⚠ GitHub token is missing scopes required to upload SSH keys. - Current scopes: {{ gh_token_scopes.stdout | default('unknown') }} - Missing: admin:public_key, admin:ssh_signing_key - → Regenerate the PAT at https://github.com/settings/tokens - with those scopes, then update the 'github-token' Bitwarden item. - when: not gh_has_key_scopes - - - name: Read SSH public key - ansible.builtin.slurp: - src: "{{ ansible_facts['env']['HOME'] }}/.ssh/id_ed25519.pub" - register: ssh_pub_key_raw - - - name: Set SSH public key facts - ansible.builtin.set_fact: - ssh_pub_key_full: "{{ ssh_pub_key_raw.content | b64decode | trim }}" - ssh_key_body: "{{ (ssh_pub_key_raw.content | b64decode).split()[1] }}" - - # Gate on gh_has_key_scopes to avoid the HTTP 404 that gh returns - # when the token lacks admin:public_key / admin:ssh_signing_key. - # Keeps failed_when: false because a transient GitHub API glitch - # shouldn't fail the whole play — the follow-up warn task surfaces it. - - name: List existing GitHub SSH keys - ansible.builtin.command: gh ssh-key list - register: gh_existing_keys - changed_when: false - failed_when: false - when: gh_has_key_scopes | bool - - # Non-TTY `gh ssh-key list` is tab-separated: TITLE KEY ADDED ID TYPE, - # so the same key body can appear once per type; check each separately. - - name: Set existing GitHub key facts - ansible.builtin.set_fact: - gh_key_lines: "{{ gh_existing_keys.stdout_lines | default([]) | select('search', ssh_key_body | regex_escape) | list }}" - - - name: Register SSH authentication key with GitHub - ansible.builtin.command: > - gh ssh-key add "{{ ansible_facts['env']['HOME'] }}/.ssh/id_ed25519.pub" - --title "{{ inventory_hostname }}-auth" - --type authentication - when: - - gh_has_key_scopes | bool - - gh_key_lines | select('search', 'authentication$') | list | length == 0 - register: gh_auth_key_result - failed_when: false - - - name: Warn if SSH auth key upload failed - ansible.builtin.debug: - msg: >- - ⚠ Failed to upload SSH auth key for {{ inventory_hostname }} to GitHub. - stderr: {{ gh_auth_key_result.stderr | default('') }} - when: - - gh_has_key_scopes | bool - - gh_auth_key_result is defined - - gh_auth_key_result.rc | default(0) != 0 - - - name: Register SSH signing key with GitHub - ansible.builtin.command: > - gh ssh-key add "{{ ansible_facts['env']['HOME'] }}/.ssh/id_ed25519.pub" - --title "{{ inventory_hostname }}-signing" - --type signing - when: - - gh_has_key_scopes | bool - - gh_key_lines | select('search', 'signing$') | list | length == 0 - register: gh_sign_key_result - failed_when: false - - - name: Warn if SSH signing key upload failed - ansible.builtin.debug: - msg: >- - ⚠ Failed to upload SSH signing key for {{ inventory_hostname }} to GitHub. - stderr: {{ gh_sign_key_result.stderr | default('') }} - when: - - gh_has_key_scopes | bool - - gh_sign_key_result is defined - - gh_sign_key_result.rc | default(0) != 0 diff --git a/roles/kube/defaults/main.yml b/roles/kube/defaults/main.yml deleted file mode 100644 index 20aa35e7..00000000 --- a/roles/kube/defaults/main.yml +++ /dev/null @@ -1,6 +0,0 @@ ---- -# Name the Talos API-server cert is actually valid for (a SAN entry). kubeconfig -# dials the control plane over the tailnet IP but verifies TLS against this, so -# `kubectl` works from any tailnet machine without disabling cert verification. -# Control-plane (bihar) LAN IP — present in the API-server cert SANs. -kube_tls_server_name: "192.168.0.5" diff --git a/roles/kube/tasks/main.yml b/roles/kube/tasks/main.yml deleted file mode 100644 index 782c110e..00000000 --- a/roles/kube/tasks/main.yml +++ /dev/null @@ -1,110 +0,0 @@ ---- -# Fetches kubeconfig + talosconfig from Bitwarden and writes them locally. -# Seed Bitwarden with `scripts/bw-seed-kube.sh` from a machine that already -# has working configs. -# Gated on `bw_unlocked` (set by the bitwarden role). - -- name: Deploy kube + talos configs - when: bw_unlocked | default(false) - environment: - BW_SESSION: "{{ bw_session }}" - PATH: "{{ ansible_env_path }}" - block: - - name: Ensure ~/.kube and ~/.talos exist - ansible.builtin.file: - path: "{{ ansible_facts['env']['HOME'] }}/{{ item }}" - state: directory - mode: "0700" - loop: - - .kube - - .talos - - - name: Fetch kubeconfig from Bitwarden - ansible.builtin.command: bw get notes kubeconfig - register: _kubeconfig - changed_when: false - no_log: true - failed_when: false - - - name: Fetch talosconfig from Bitwarden - ansible.builtin.command: bw get notes talosconfig - register: _talosconfig - changed_when: false - no_log: true - failed_when: false - - - name: Write kubeconfig - ansible.builtin.copy: - content: "{{ _kubeconfig.stdout }}" - dest: "{{ ansible_facts['env']['HOME'] }}/.kube/config" - mode: "0600" - when: _kubeconfig.rc == 0 and (_kubeconfig.stdout | length) > 0 - no_log: true - - - name: Write talosconfig - ansible.builtin.copy: - content: "{{ _talosconfig.stdout }}" - dest: "{{ ansible_facts['env']['HOME'] }}/.talos/config" - mode: "0600" - when: _talosconfig.rc == 0 and (_talosconfig.stdout | length) > 0 - no_log: true - - # The kubeconfig connects to the control-plane over its *tailnet* IP, but the - # Talos API-server cert only lists the LAN IP / loopback in its SANs — so TLS - # verification fails ("certificate is valid for …, not 100.x.x.x"). Pin - # tls-server-name to a name the cert IS valid for: kubectl still dials the - # tailnet IP but verifies the cert against this SAN. (Alternative long-term - # fix: add the tailnet IP to machine.certSANs in the Talos config + regen.) - - name: List kubeconfig clusters - ansible.builtin.command: kubectl config get-clusters - register: _clusters - changed_when: false - when: _kubeconfig.rc == 0 and (_kubeconfig.stdout | length) > 0 - - - name: Pin kubeconfig tls-server-name to a valid cert SAN - ansible.builtin.command: - cmd: kubectl config set-cluster {{ item }} --tls-server-name={{ kube_tls_server_name }} - loop: "{{ _clusters.stdout_lines[1:] | default([]) }}" # skip the NAME header row - changed_when: false - when: _kubeconfig.rc == 0 and (_kubeconfig.stdout | length) > 0 - - - name: Warn if either config missing in Bitwarden - ansible.builtin.debug: - msg: >- - {% if _kubeconfig.rc != 0 %}kubeconfig missing in BW — seed it with scripts/bw-seed-kube.sh.{% endif %} - {% if _talosconfig.rc != 0 %}talosconfig missing in BW — seed it with scripts/bw-seed-kube.sh.{% endif %} - when: _kubeconfig.rc != 0 or _talosconfig.rc != 0 - - # AWS migration cluster (matrix/hive workloads) — kept as separate files - # rather than merged into the primary configs above, so both clusters stay - # independently usable. To merge contexts ad hoc: - # KUBECONFIG=~/.kube/config:~/.kube/config-aws-migration kubectl config view --flatten - - name: Fetch AWS migration kubeconfig from Bitwarden - ansible.builtin.command: bw get notes kubeconfig-aws-migration - register: _kubeconfig_aws - changed_when: false - no_log: true - failed_when: false - - - name: Fetch AWS migration talosconfig from Bitwarden - ansible.builtin.command: bw get notes talosconfig-aws-migration - register: _talosconfig_aws - changed_when: false - no_log: true - failed_when: false - - - name: Write AWS migration kubeconfig - ansible.builtin.copy: - content: "{{ _kubeconfig_aws.stdout }}" - dest: "{{ ansible_facts['env']['HOME'] }}/.kube/config-aws-migration" - mode: "0600" - when: _kubeconfig_aws.rc == 0 and (_kubeconfig_aws.stdout | length) > 0 - no_log: true - - - name: Write AWS migration talosconfig - ansible.builtin.copy: - content: "{{ _talosconfig_aws.stdout }}" - dest: "{{ ansible_facts['env']['HOME'] }}/.talos/config-aws-migration" - mode: "0600" - when: _talosconfig_aws.rc == 0 and (_talosconfig_aws.stdout | length) > 0 - no_log: true diff --git a/roles/shell_fonts/tasks/main.yml b/roles/shell_fonts/tasks/main.yml deleted file mode 100644 index 0648833f..00000000 --- a/roles/shell_fonts/tasks/main.yml +++ /dev/null @@ -1,36 +0,0 @@ ---- -- name: Ensure font directory exists - ansible.builtin.file: - path: "{{ ansible_facts['env']['HOME'] }}/.local/share/fonts" - state: directory - mode: "0755" - -- name: Check if JetBrains Mono Nerd Font is installed - ansible.builtin.stat: - path: "{{ ansible_facts['env']['HOME'] }}/.local/share/fonts/JetBrainsMono/JetBrainsMonoNerdFont-Regular.ttf" - register: jb_font - -- name: Install JetBrains Mono Nerd Font - when: not jb_font.stat.exists and is_desktop | bool - block: - - name: Download JetBrains Mono Nerd Font - ansible.builtin.get_url: - url: https://github.com/ryanoasis/nerd-fonts/releases/latest/download/JetBrainsMono.zip - dest: /tmp/JetBrainsMonoNerdFont.zip - mode: "0644" - - - name: Create JetBrainsMono font directory - ansible.builtin.file: - path: "{{ ansible_facts['env']['HOME'] }}/.local/share/fonts/JetBrainsMono" - state: directory - mode: "0755" - - - name: Unarchive JetBrains Mono Nerd Font - ansible.builtin.unarchive: - src: /tmp/JetBrainsMonoNerdFont.zip - dest: "{{ ansible_facts['env']['HOME'] }}/.local/share/fonts/JetBrainsMono" - remote_src: true - - - name: Update font cache - ansible.builtin.command: fc-cache -f - changed_when: false diff --git a/scripts/run-units.sh b/scripts/run-units.sh index ad101ec6..4679ea76 100755 --- a/scripts/run-units.sh +++ b/scripts/run-units.sh @@ -3,14 +3,18 @@ # Usage: scripts/run-units.sh [--no-secrets] [--tags a,b] set -uo pipefail -ALL_UNITS="chezmoi omp atuin shell-ai" # run order for the sequential fallback; keep in step with mise.toml +ALL_UNITS="chezmoi fonts omp atuin shell-ai github kube forgejo-registry" # run order for the sequential fallback; keep in step with mise.toml tasks_for_tag() { case "$1" in # Ansible tag -> units (mirrors the old role tags) - dotfiles) echo "chezmoi omp atuin shell-ai" ;; - shell) echo "chezmoi atuin shell-ai" ;; + dotfiles) echo "chezmoi fonts omp atuin shell-ai" ;; + shell) echo "chezmoi fonts atuin shell-ai" ;; + fonts) echo fonts ;; git|neovim|editor) echo chezmoi ;; atuin) echo atuin ;; ai) echo shell-ai ;; - secrets) echo "atuin shell-ai" ;; + secrets) echo "atuin shell-ai github kube forgejo-registry" ;; + github) echo github ;; + kube) echo kube ;; + forgejo|registry) echo forgejo-registry ;; omp) echo omp ;; esac; } diff --git a/site.yml b/site.yml index aff4351a..493a8760 100644 --- a/site.yml +++ b/site.yml @@ -71,8 +71,6 @@ when: inventory_hostname in groups.get('termux_hosts', []) - role: bitwarden tags: [secrets, bitwarden] - - role: shell_fonts - tags: [dotfiles, shell, fonts] - role: hive_ops tags: [hive, hive_ops] @@ -82,16 +80,8 @@ - role: ssh_mesh tags: [ssh, mesh_check] when: machine_profile != 'vm-test' and not skip_ssh_mesh | default(false) - - role: github - tags: [secrets, github] - role: tailscale tags: [secrets, tailscale] - - role: kube - tags: [secrets, kube] - when: is_desktop | bool and machine_profile != 'vm-test' and not skip_kube | default(false) - - role: forgejo_registry - tags: [secrets, forgejo, registry] - when: machine_profile != 'vm-test' and not skip_forgejo | default(false) # Phase 3: Desktop apps - role: flatpak diff --git a/tests/test_fonts_unit.py b/tests/test_fonts_unit.py new file mode 100644 index 00000000..b7c4a020 --- /dev/null +++ b/tests/test_fonts_unit.py @@ -0,0 +1,40 @@ +from pathlib import Path + +from conftest import write_stub +from unit_helpers import calls, run_unit + +CURL = 'echo "curl $*" >> "$HOME/calls"\nout=""; while [ $# -gt 0 ]; do [ "$1" = -o ] && out="$2"; shift; done\n: > "$out"\nexit "${STUB_CURL_RC:-0}"\n' +UNZIP = 'echo "unzip $*" >> "$HOME/calls"\nd=""; while [ $# -gt 0 ]; do [ "$1" = -d ] && d="$2"; shift; done\n: > "$d/JetBrainsMonoNerdFont-Regular.ttf"\n' + + +def setup(stub_env, desktop): + bindir, env = stub_env + write_stub(bindir, "curl", CURL) + write_stub(bindir, "unzip", UNZIP) + write_stub(bindir, "fc-cache", 'echo "fc-cache $*" >> "$HOME/calls"\n') + env["DOTFILES_IS_DESKTOP"] = "1" if desktop else "0" + return env + + +def test_desktop_installs_the_font_once(stub_env): + env = setup(stub_env, True) + r = run_unit("fonts", env) + assert r.returncode == 0, r.stdout + r.stderr + assert (Path(env["HOME"]) / ".local/share/fonts/JetBrainsMono/JetBrainsMonoNerdFont-Regular.ttf").exists() + assert "fc-cache -f" in calls(env) + before = calls(env) + assert run_unit("fonts", env).returncode == 0 + assert calls(env) == before # second run downloads nothing + + +def test_non_desktop_downloads_nothing(stub_env): + env = setup(stub_env, False) + r = run_unit("fonts", env) + assert r.returncode == 0 and "curl" not in calls(env) + + +def test_failed_download_fails_the_unit_without_a_partial_install(stub_env): + env = setup(stub_env, True) + r = run_unit("fonts", env, STUB_CURL_RC="22") + assert r.returncode == 1 and "FAIL install JetBrains Mono Nerd Font" in r.stdout + assert not (Path(env["HOME"]) / ".local/share/fonts/JetBrainsMono").exists() diff --git a/tests/test_forgejo_registry_unit.py b/tests/test_forgejo_registry_unit.py new file mode 100644 index 00000000..47ac73b9 --- /dev/null +++ b/tests/test_forgejo_registry_unit.py @@ -0,0 +1,48 @@ +import pytest + +from conftest import write_stub +from unit_helpers import UNLOCKED_BW, calls, run_unit + +CREDS = """\ +if [ "$1" = get ]; then + case "$2" in + username) echo " ci-user "; exit 0 ;; + password) [ -n "$STUB_NO_PASS" ] && exit 1; echo "tok123"; exit 0 ;; + esac +fi +""" + + +@pytest.fixture +def env(stub_env): + bindir, env = stub_env + write_stub(bindir, "bw", UNLOCKED_BW % CREDS) + write_stub(bindir, "podman", 'echo "podman $*" >> "$HOME/calls"\ncase "$1 $2" in "login --get-login") exit "${STUB_LOGGED_IN:-1}" ;; esac\ncat >> "$HOME/stdin" 2>/dev/null\nexit 0\n') + env["BW_SESSION"] = "x" + return env + + +def test_logs_in_with_the_bitwarden_credential_on_stdin(env): + from pathlib import Path + + r = run_unit("forgejo-registry", env) + assert r.returncode == 0, r.stdout + r.stderr + assert "podman login forgejo.manatee-basking.ts.net -u ci-user --password-stdin" in calls(env) + assert Path(env["HOME"], "stdin").read_text() == "tok123" + assert "tok123" not in calls(env) + r.stdout + r.stderr + + +def test_already_logged_in_never_touches_bitwarden(env): + r = run_unit("forgejo-registry", env, STUB_LOGGED_IN="0") + assert r.returncode == 0 and "bw " not in calls(env) and "--password-stdin" not in calls(env) + + +def test_missing_credential_is_partial_not_a_failure(env): + r = run_unit("forgejo-registry", env, STUB_NO_PASS="1") + assert r.returncode == 0 and "PARTIAL:" in r.stdout + assert "--password-stdin" not in calls(env) + + +def test_locked_vault_is_skipped(env): + r = run_unit("forgejo-registry", env, STUB_UNLOCK_RC="1") + assert r.returncode == 0 and "SKIPPED:" in r.stdout diff --git a/tests/test_github_unit.py b/tests/test_github_unit.py new file mode 100644 index 00000000..d41fb0c3 --- /dev/null +++ b/tests/test_github_unit.py @@ -0,0 +1,112 @@ +from pathlib import Path + +import pytest + +from conftest import write_stub +from unit_helpers import UNLOCKED_BW, calls, run_unit + +PUB = "ssh-ed25519 AAAAKEYBODY james@host\n" + +GH = """\ +echo "gh $*" >> "$HOME/calls" +case "$1 $2" in + "auth status") + [ "$3" = "-t" ] && echo " - Token scopes: $STUB_SCOPES" + [ "$3" = "-t" ] || exit "${STUB_AUTHED:-0}" + exit 0 ;; + "auth login") cat > "$HOME/token"; exit 0 ;; + "ssh-key list") printf '%s' "$STUB_KEYS"; exit "${STUB_LIST_RC:-0}" ;; + "config set") mkdir -p "$HOME/.config/gh"; echo 'git_protocol: ssh' > "$HOME/.config/gh/config.yml" ;; +esac +exit 0 +""" + + +@pytest.fixture +def env(stub_env): + bindir, env = stub_env + write_stub(bindir, "bw", UNLOCKED_BW % '[ "$1 $2 $3" = "get password github-token" ] && { echo ghp_secret; exit 0; }') + write_stub(bindir, "gh", GH) + ssh = Path(env["HOME"], ".ssh") + ssh.mkdir() + (ssh / "id_ed25519.pub").write_text(PUB) + env.update( + BW_SESSION="x", DOTFILES_MACHINE="testbox", UNITS_NO_SECRETS="0", + STUB_AUTHED="0", STUB_UNLOCK_RC="0", STUB_LIST_RC="0", + STUB_SCOPES="admin:public_key, admin:ssh_signing_key", STUB_KEYS="", + ) + return env + + +def test_logs_in_with_the_bitwarden_token_and_registers_both_keys(env): + r = run_unit("github", env, STUB_AUTHED="1") + assert r.returncode == 0, r.stdout + r.stderr + assert Path(env["HOME"], "token").read_text() == "ghp_secret" + c = calls(env) + assert "ssh-key add" in c and "--title testbox-auth --type authentication" in c and "--title testbox-signing --type signing" in c + assert "AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl" in Path(env["HOME"], ".ssh/known_hosts").read_text() + + +def test_registered_keys_are_not_added_again(env): + keys = "testbox-auth\tssh-ed25519 AAAAKEYBODY\t2026-01-01\t1\tauthentication\ntestbox-signing\tssh-ed25519 AAAAKEYBODY\t2026-01-01\t2\tsigning\n" + r = run_unit("github", env, STUB_KEYS=keys) + assert r.returncode == 0 and "ssh-key add" not in calls(env) + assert "auth login" not in calls(env) # already authenticated + + +def test_only_the_missing_key_type_is_added(env): + keys = "testbox-auth\tssh-ed25519 AAAAKEYBODY\t2026-01-01\t1\tauthentication\n" + run_unit("github", env, STUB_KEYS=keys) + c = calls(env) + assert "--type signing" in c and "--type authentication" not in c + + +def test_token_without_key_scopes_is_partial_and_adds_nothing(env): + r = run_unit("github", env, STUB_SCOPES="repo, read:org") + assert r.returncode == 0 and "PARTIAL:" in r.stdout and "ssh-key add" not in calls(env) + + +def test_locked_vault_does_not_log_in(env): + r = run_unit("github", env, STUB_AUTHED="1", STUB_UNLOCK_RC="1") + assert r.returncode == 0 and "PARTIAL: gh not logged in" in r.stdout + assert "auth login" not in calls(env) + + +@pytest.mark.parametrize("authed", ["0", "1"]) +def test_no_secrets_skips_all_github_and_vault_operations(env, authed): + r = run_unit("github", env, UNITS_NO_SECRETS="1", STUB_AUTHED=authed) + assert r.returncode == 0, r.stdout + r.stderr + assert "SKIPPED: GitHub authentication and SSH key registration" in r.stdout + assert calls(env) == "" + assert "github.com ssh-ed25519" in Path(env["HOME"], ".ssh/known_hosts").read_text() + + +def test_no_secrets_preserves_known_hosts_failure(env): + Path(env["HOME"], ".ssh/known_hosts").mkdir() + r = run_unit("github", env, UNITS_NO_SECRETS="1") + assert r.returncode != 0 + assert "FAIL github known_hosts" in r.stdout + assert "SKIPPED: GitHub authentication and SSH key registration" in r.stdout + assert calls(env) == "" + + +@pytest.mark.parametrize("authed", ["0", "1"]) +def test_gh_config_is_left_for_chezmoi(env, authed): + config = Path(env["HOME"], ".config/gh/config.yml") + config.parent.mkdir(parents=True) + original = "version: 1\ngit_protocol: https\nprompt: enabled\n" + config.write_text(original) + r = run_unit("github", env, STUB_AUTHED=authed) + assert r.returncode == 0, r.stdout + r.stderr + assert config.read_text() == original + assert "gh config set" not in calls(env) + + +@pytest.mark.parametrize("partial_output", ["", "testbox-auth\tssh-ed25519 AAAAKEYBODY\tauthentication\n"]) +def test_failed_key_list_never_registers_even_with_partial_output(env, partial_output): + r = run_unit("github", env, STUB_LIST_RC="1", STUB_KEYS=partial_output) + assert r.returncode != 0 + assert "failed to list GitHub SSH keys; registration skipped" in r.stdout + assert "FAIL register SSH keys" in r.stdout + assert "ssh-key list" in calls(env) + assert "ssh-key add" not in calls(env) diff --git a/tests/test_kube_unit.py b/tests/test_kube_unit.py new file mode 100644 index 00000000..ee8b50e5 --- /dev/null +++ b/tests/test_kube_unit.py @@ -0,0 +1,70 @@ +import stat +from pathlib import Path + +import pytest + +from conftest import write_stub +from unit_helpers import UNLOCKED_BW, calls, run_unit + +NOTES = """\ +if [ "$1 $2" = "get notes" ]; then + case "$3" in + kubeconfig) echo "home-kube"; exit 0 ;; + talosconfig) echo "home-talos"; exit 0 ;; + kubeconfig-aws-migration) echo "aws-kube"; exit 0 ;; + talosconfig-aws-migration) exit 1 ;; + esac +fi +""" + + +@pytest.fixture +def env(stub_env): + bindir, env = stub_env + write_stub(bindir, "bw", UNLOCKED_BW % NOTES) + write_stub(bindir, "kubectl", 'echo "kubectl $*" >> "$HOME/calls"\n[ "$2" = get-clusters ] && printf "NAME\\nhome\\n"\nexit 0\n') + env.update(BW_SESSION="x", DOTFILES_IS_DESKTOP="1") + return env + + +def test_configs_are_written_private_and_the_home_cluster_is_pinned(env): + r = run_unit("kube", env) + assert r.returncode == 0, r.stdout + r.stderr + home = Path(env["HOME"]) + assert (home / ".kube/config").read_text().strip() == "home-kube" + assert (home / ".talos/config").read_text().strip() == "home-talos" + assert (home / ".kube/config-aws-migration").read_text().strip() == "aws-kube" + for f in (".kube/config", ".talos/config", ".kube/config-aws-migration"): + assert stat.S_IMODE((home / f).stat().st_mode) == 0o600 + assert "kubectl config set-cluster home --tls-server-name=192.168.0.5" in calls(env) + + +def test_a_missing_note_warns_and_leaves_no_file(env): + r = run_unit("kube", env) + assert "AWS talosconfig missing in BW" in r.stdout + assert not (Path(env["HOME"]) / ".talos/config-aws-migration").exists() + + +def test_locked_vault_leaves_existing_configs(env): + cfg = Path(env["HOME"], ".kube/config") + cfg.parent.mkdir() + cfg.write_text("previous") + r = run_unit("kube", env, STUB_UNLOCK_RC="1") + assert r.returncode == 0 and "SKIPPED:" in r.stdout + assert cfg.read_text() == "previous" + + +@pytest.mark.parametrize("extra", [{"DOTFILES_IS_DESKTOP": "0"}]) +def test_non_desktops_do_nothing(env, extra): + r = run_unit("kube", env, **extra) + assert "SKIPPED:" in r.stdout + assert not (Path(env["HOME"]) / ".kube").exists() + + +def test_skip_kube_host_var_wins_over_desktop(env, tmp_path): + repo = tmp_path / "repo" + (repo / "host_vars").mkdir(parents=True) + (repo / "host_vars/phone.yml").write_text("machine_profile: cli-only\nskip_kube: true\n") + (repo / "units").symlink_to(Path(__file__).resolve().parent.parent / "units") + r = run_unit("kube", env, DOTFILES_DIR=str(repo), DOTFILES_MACHINE="phone") + assert "SKIPPED:" in r.stdout and not (Path(env["HOME"]) / ".kube").exists() diff --git a/tests/unit_helpers.py b/tests/unit_helpers.py new file mode 100644 index 00000000..e1b3679f --- /dev/null +++ b/tests/unit_helpers.py @@ -0,0 +1,23 @@ +"""Shared harness for the bash-unit tests: run units/.sh with stubs and a throwaway HOME.""" +import subprocess +from pathlib import Path + +from conftest import REPO + + +def run_unit(name, env, **extra): + env = {**env, "DOTFILES_DIR": str(REPO), "BREW_PREFIX": "/nonexistent", **extra} + return subprocess.run(["bash", str(REPO / "units" / f"{name}.sh")], env=env, capture_output=True, text=True) + + +def calls(env): + p = Path(env["HOME"], "calls") + return p.read_text() if p.exists() else "" + + +UNLOCKED_BW = """\ +echo "bw $*" >> "$HOME/calls" +[ "$1 $2" = "unlock --check" ] && exit "${STUB_UNLOCK_RC:-0}" +%s +exit 1 +""" diff --git a/units/chezmoi.sh b/units/chezmoi.sh index 4f5c8d91..14f72c41 100755 --- a/units/chezmoi.sh +++ b/units/chezmoi.sh @@ -18,13 +18,9 @@ if ! command -v chezmoi >/dev/null 2>&1; then exit 1 fi -is_desktop=0 -if command -v yq >/dev/null 2>&1; then - [ "$(yq ".all.children.desktop.hosts | has(\"$MACHINE\")" "$DOTFILES_DIR/inventory.yml" 2>/dev/null)" = true ] && is_desktop=1 -else - note "warn: yq missing, assuming non-desktop" -fi -export DOTFILES_MACHINE="$MACHINE" DOTFILES_IS_DESKTOP="$is_desktop" +command -v yq >/dev/null 2>&1 || note "warn: yq missing, assuming non-desktop" +if is_desktop; then desktop=1; else desktop=0; fi +export DOTFILES_MACHINE="$MACHINE" DOTFILES_IS_DESKTOP="$desktop" step "chezmoi apply" chezmoi --source "$DOTFILES_DIR" --keep-going --force --no-tty apply --init finish diff --git a/units/fonts.sh b/units/fonts.sh new file mode 100755 index 00000000..7d114bd4 --- /dev/null +++ b/units/fonts.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash +# fonts unit: JetBrains Mono Nerd Font for desktop terminals. +set -uo pipefail +# shellcheck source=units/lib.sh +. "$(dirname "${BASH_SOURCE[0]}")/lib.sh" + +dir="$HOME/.local/share/fonts" +font="$dir/JetBrainsMono/JetBrainsMonoNerdFont-Regular.ttf" + +step "font directory" mkdir -p "$dir" +if ! is_desktop; then + note "SKIPPED: not a desktop; no font install" + finish +fi +if [ -f "$font" ]; then + note "ok JetBrains Mono Nerd Font already installed" + finish +fi + +install_font() { + local tmp rc=0 + tmp="$(mktemp -d)" || return 1 + curl -fsSL https://github.com/ryanoasis/nerd-fonts/releases/latest/download/JetBrainsMono.zip -o "$tmp/font.zip" \ + && mkdir -p "$dir/JetBrainsMono" \ + && unzip -oq "$tmp/font.zip" -d "$dir/JetBrainsMono" || rc=1 + rm -rf "$tmp" + return $rc +} +step "install JetBrains Mono Nerd Font" install_font +soft "font cache" fc-cache -f +finish diff --git a/units/forgejo-registry.sh b/units/forgejo-registry.sh new file mode 100755 index 00000000..a46489bf --- /dev/null +++ b/units/forgejo-registry.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env bash +# forgejo-registry unit: `podman login` to the private Forgejo container registry from Bitwarden. +# The Bitwarden login item is named after the registry host and holds the username + a registry token. +set -uo pipefail +# shellcheck source=units/lib.sh +. "$(dirname "${BASH_SOURCE[0]}")/lib.sh" + +FORGEJO_REGISTRY="${FORGEJO_REGISTRY:-forgejo.manatee-basking.ts.net}" + +if skipped_by_profile skip_forgejo; then + note "SKIPPED: skip_forgejo or vm-test" + exit 0 +fi +if ! command -v podman >/dev/null 2>&1; then + note "SKIPPED: podman not installed" + exit 0 +fi +if podman login --get-login "$FORGEJO_REGISTRY" >/dev/null 2>&1; then + note "ok already logged in to $FORGEJO_REGISTRY" + exit 0 +fi +s="$(bw_session)" +if [ -z "$s" ]; then + note "SKIPPED: vault locked or --no-secrets; not logged in to $FORGEJO_REGISTRY" + exit 0 +fi + +registry_login() { + local user pass + user="$(BW_SESSION="$s" bw get username "$FORGEJO_REGISTRY" 2>/dev/null)" + pass="$(BW_SESSION="$s" bw get password "$FORGEJO_REGISTRY" 2>/dev/null)" + if [ -z "$user" ] || [ -z "$pass" ]; then + note "PARTIAL: Forgejo credential ($FORGEJO_REGISTRY) not found in Bitwarden — add a login item with that name holding the registry token" + return 0 + fi + printf '%s' "$pass" | podman login "$FORGEJO_REGISTRY" -u "$(printf '%s' "$user" | tr -d '[:space:]')" --password-stdin +} +step "podman login $FORGEJO_REGISTRY" registry_login +finish diff --git a/units/github.sh b/units/github.sh new file mode 100755 index 00000000..2cddf328 --- /dev/null +++ b/units/github.sh @@ -0,0 +1,75 @@ +#!/usr/bin/env bash +# github unit: gh login from a Bitwarden token, git over SSH, and this machine's SSH key +# registered with GitHub as an authentication and a signing key. +set -uo pipefail +# shellcheck source=units/lib.sh +. "$(dirname "${BASH_SOURCE[0]}")/lib.sh" + +GITHUB_HOST_KEY="github.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl" +pub="$HOME/.ssh/id_ed25519.pub" + +if ! command -v gh >/dev/null 2>&1; then + note "SKIPPED: gh not installed (just packages)" + exit 0 +fi + +known_hosts() { + install -d -m 0700 "$HOME/.ssh" || return 1 + grep -qF "$GITHUB_HOST_KEY" "$HOME/.ssh/known_hosts" 2>/dev/null && return 0 + printf '%s\n' "$GITHUB_HOST_KEY" >> "$HOME/.ssh/known_hosts" +} + +gh_login() { + gh auth status >/dev/null 2>&1 && return 0 + local s tok + s="$(bw_session)" + if [ -z "$s" ]; then + note "PARTIAL: gh not logged in; vault locked (bw unlock, then just apply-tags github)" + return 0 + fi + tok="$(BW_SESSION="$s" bw get password github-token 2>/dev/null)" + if [ -z "$tok" ]; then + note "PARTIAL: no 'github-token' item in Bitwarden; add a login item with your PAT as the password" + return 0 + fi + printf '%s' "$tok" | gh auth login --with-token +} + +# register_key TYPE: add the SSH key as TYPE unless a `... TYPE` line already lists it. +register_key() { + local type="$1" title="$MACHINE-$2" + grep -Eq "${type}\$" <<<"$key_lines" && return 0 + if ! gh ssh-key add "$pub" --title "$title" --type "$type"; then + note "warn: failed to upload SSH $type key for $MACHINE to GitHub" + return 1 + fi +} + +register_keys() { + local scopes key_body key_list + scopes="$(gh auth status -t 2>&1 | grep -o 'Token scopes:.*' || true)" + case "$scopes" in + *admin:public_key*admin:ssh_signing_key*|*admin:ssh_signing_key*admin:public_key*) ;; + *) + note "PARTIAL: GitHub token lacks admin:public_key / admin:ssh_signing_key (have: ${scopes:-unknown}); regenerate the PAT at https://github.com/settings/tokens and update the 'github-token' item" + return 0 ;; + esac + [ -f "$pub" ] || { note "warn: $pub missing; no SSH key to register"; return 1; } + key_body="$(awk '{print $2}' "$pub")" + if ! key_list="$(gh ssh-key list 2>/dev/null)"; then + note "warn: failed to list GitHub SSH keys; registration skipped" + return 1 + fi + key_lines="$(grep -F -- "$key_body" <<<"$key_list" || true)" + register_key authentication auth && register_key signing signing +} + +key_lines="" +step "github known_hosts" known_hosts +if [ "${UNITS_NO_SECRETS:-0}" = 1 ]; then + note "SKIPPED: GitHub authentication and SSH key registration (--no-secrets)" + finish +fi +step "gh login" gh_login +step "register SSH keys" register_keys +finish diff --git a/units/kube.conf b/units/kube.conf new file mode 100644 index 00000000..38f5943a --- /dev/null +++ b/units/kube.conf @@ -0,0 +1,5 @@ +# Sourced by units/kube.sh. +# Name the Talos API-server cert is actually valid for (a SAN entry). kubeconfig dials the +# control plane over the tailnet IP but verifies TLS against this, so `kubectl` works from any +# tailnet machine without disabling cert verification. Control-plane (bihar) LAN IP. +KUBE_TLS_SERVER_NAME="${KUBE_TLS_SERVER_NAME:-192.168.0.5}" diff --git a/units/kube.sh b/units/kube.sh new file mode 100755 index 00000000..3ebfa3e2 --- /dev/null +++ b/units/kube.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash +# kube unit: kubeconfig + talosconfig for the home and AWS clusters from Bitwarden notes. +# Seed the home notes with scripts/bw-seed-kube.sh. Desktops only. +set -uo pipefail +# shellcheck source=units/lib.sh +. "$(dirname "${BASH_SOURCE[0]}")/lib.sh" +# shellcheck source=units/kube.conf +. "$DOTFILES_DIR/units/kube.conf" + +if ! is_desktop || skipped_by_profile skip_kube; then + note "SKIPPED: not a desktop (or skip_kube / vm-test)" + exit 0 +fi +s="$(bw_session)" +if [ -z "$s" ]; then + note "SKIPPED: vault locked or --no-secrets; existing kube/talos configs left untouched" + exit 0 +fi + +step "kube dirs" install -d -m 0700 "$HOME/.kube" "$HOME/.talos" + +# fetch_note ITEM DEST LABEL: write the note to DEST when Bitwarden has it; prints a warning otherwise. +fetch_note() { + local item="$1" dest="$2" label="$3" val + val="$(BW_SESSION="$s" bw get notes "$item" 2>/dev/null)" + if [ -z "$val" ]; then + note "warn: $label missing in BW (item $item)" + return 0 + fi + printf '%s\n' "$val" | write_atomic "$dest" || return 1 + [ "$dest" != "$HOME/.kube/config" ] || home_written=1 +} + +# The home kubeconfig is rewritten from Bitwarden each run, so the TLS pin is re-applied each time. +pin_tls() { + command -v kubectl >/dev/null 2>&1 || { note "warn: kubectl missing; tls-server-name not pinned"; return 0; } + local c rc=0 + while read -r c; do + [ -n "$c" ] || continue + KUBECONFIG="$HOME/.kube/config" kubectl config set-cluster "$c" --tls-server-name="$KUBE_TLS_SERVER_NAME" >/dev/null || rc=1 + done < <(KUBECONFIG="$HOME/.kube/config" kubectl config get-clusters 2>/dev/null | tail -n +2) + return $rc +} + +home_written=0 +step "home kubeconfig" fetch_note kubeconfig "$HOME/.kube/config" "kubeconfig (seed with scripts/bw-seed-kube.sh)" +step "home talosconfig" fetch_note talosconfig "$HOME/.talos/config" "talosconfig (seed with scripts/bw-seed-kube.sh)" +[ "$home_written" = 0 ] || step "pin tls-server-name" pin_tls +step "aws kubeconfig" fetch_note kubeconfig-aws-migration "$HOME/.kube/config-aws-migration" "AWS kubeconfig" +step "aws talosconfig" fetch_note talosconfig-aws-migration "$HOME/.talos/config-aws-migration" "AWS talosconfig" +finish diff --git a/units/lib.sh b/units/lib.sh index 6d8a1871..82221403 100755 --- a/units/lib.sh +++ b/units/lib.sh @@ -25,4 +25,31 @@ bw_session() { [ -n "$s" ] && BW_SESSION="$s" bw unlock --check >/dev/null 2>&1 && printf '%s' "$s" return 0 } -MACHINE="$(cat /etc/dotfiles-machine 2>/dev/null || hostname)" +MACHINE="${DOTFILES_MACHINE:-$(cat /etc/dotfiles-machine 2>/dev/null || hostname)}" +# host_var KEY [DEFAULT]: KEY from host_vars/$MACHINE.yml (flat `key: value` lines only). +host_var() { + local v + v="$(sed -n "s/^$1:[[:space:]]*\([^#]*[^#[:space:]]\).*/\1/p" "$DOTFILES_DIR/host_vars/$MACHINE.yml" 2>/dev/null | head -n1)" + v="${v//[\"\']/}" + printf '%s' "${v:-${2:-}}" +} +# is_desktop: DOTFILES_IS_DESKTOP, else host_vars `is_desktop`, else membership of the inventory `desktop` group. +is_desktop() { + local o="${DOTFILES_IS_DESKTOP:-}" + [ -n "$o" ] || o="$(host_var is_desktop)" + case "$o" in 1|true) return 0 ;; 0|false) return 1 ;; esac + command -v yq >/dev/null 2>&1 || return 1 + [ "$(yq ".all.children.desktop.hosts | has(\"$MACHINE\")" "$DOTFILES_DIR/inventory.yml" 2>/dev/null)" = true ] +} +# skipped_by_profile FLAG: true when this host opted out (host_vars FLAG: true) or is a vm-test machine. +skipped_by_profile() { + [ "$(host_var "$1")" = true ] || [ "$(host_var machine_profile)" = vm-test ] +} +# write_atomic dest: stdin -> temp file in the same directory (mode 0600) -> mv +write_atomic() { + local dest="$1" tmp + mkdir -p "$(dirname "$dest")" || return 1 + tmp="$(umask 077; mktemp "$dest.XXXXXX")" || return 1 + if cat > "$tmp" && chmod 600 "$tmp" && mv -f "$tmp" "$dest"; then return 0; fi + rm -f "$tmp"; return 1 +} diff --git a/units/shell-ai.sh b/units/shell-ai.sh index 87a0949a..160ce7bb 100755 --- a/units/shell-ai.sh +++ b/units/shell-ai.sh @@ -27,15 +27,6 @@ if [ -z "$deepseek$forgejo$tavily$meta$kiro" ]; then exit 0 fi -# write_atomic dest: stdin -> temp file in the same directory (mode 0600) -> mv -write_atomic() { - local dest="$1" tmp - mkdir -p "$(dirname "$dest")" || return 1 - tmp="$(umask 077; mktemp "$dest.XXXXXX")" || return 1 - if cat > "$tmp" && chmod 600 "$tmp" && mv -f "$tmp" "$dest"; then return 0; fi - rm -f "$tmp"; return 1 -} - secrets_sh() { echo '# Managed by dotfiles playbook — DO NOT EDIT' echo 'export OMP_KIRO_CONFIG="$HOME/.config/omp/kiro.json"'