diff --git a/CHANGELOG.md b/CHANGELOG.md index 61e9676..39a11ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,21 @@ # Changelog +## v1.2.0 + +- Add `netssh info HOST` and `Ctrl+L` in the TUI: site, rack/U, role, platform, tenant, device type, serial, VM resources and all IP addresses per interface from NetBox; `--json` for scripts. +- Add `netssh open HOST` and `Ctrl+G` in the TUI to open the host's NetBox page (`--print` prints the URL). +- Add `netssh whois ADDRESS|HOSTNAME`: the device or VM, interface, DNS name and containing prefixes of an address across all NetBox instances, plus matching cached hosts. +- Cache NetBox ID, status, site, role, platform and tenant. Ask before connecting to hosts whose status is in `ssh.warn_status` (default: offline, planned, staged, failed, inventory, decommissioning); `--force` skips the question, non-interactive callers get a warning. The TUI shows a status badge and filters by `status:`, `site:`, `role:`, `platform:` and `tenant:`. +- `ssh.rules` and command restrictions match `site`, `role`, `platform`, `tenant` and `status` (name or slug), e.g. legacy key exchange for `platform: junos`. The wizard edits the new selectors. +- Show where a device is: site › all nested NetBox locations › rack › rack unit. `netssh info` and the TUI detail view add `Location`, `Rack` (units, height, face) and `Path`; the TUI gets a `location` column and `location:`/`rack:` filters; Ansible exports add `netbox_location`, `netbox_rack`, `netbox_position` and `--group-by location,rack`. Each location is fetched once per run. +- Add `tui.columns` to choose what the TUI shows next to each host and in which order: `alias`, `ip`, `kind`, `source`, `status`, `site`, `location`, `role`, `platform`, `tenant`, `cluster`, `tags`, `reachability` (default `[alias, ip, source, status]`). Also in `netssh configure`. +- Optional TUI reachability check (`reachability` column, off by default): listed hosts are probed on their SSH port and marked ✓/✗ (`tui.reachability_timeout`); hosts behind a jump host are skipped. +- Add `resolver.fallback`: when the chosen address does not answer, connect to the first answering address of the other strategies (or the other primary IP) for this connection. +- Add `netssh copy-id` to run ssh-copy-id with netssh's address, user, port, jump host and options, for single hosts, `tag:`, `cluster:` and `@group`. +- Report changed host addresses on connect and in `netssh cache refresh`, offer to remove the old known_hosts entry, and add `netssh known-hosts reset HOST...`. +- Add `netssh export ansible` (YAML or INI inventory with groups by tag/site/role/platform/… and `netbox_*` host variables) and `netssh export ssh-config`. +- Add keyring token storage: `token: keyring` reads the NetBox token from the system keyring; `netssh token set|migrate|delete|status` manage it, `netssh doctor` checks it. + ## v1.1.0 - Add local post-disconnect/post-command scripts, execution timeouts, cancellation, per-command confirmation and host restrictions. diff --git a/README.md b/README.md index 6e9f9d9..87c6dbf 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ The same lookup drives `netssh sftp`, `netssh scp`, and `netssh tunnel`, the int - [Configuration](#configuration) — [wizard](#interactive-wizard), [manual config](#manual-config), [reference](#configuration-reference), [environment variables](#environment-variables), [files](#files-and-directories), [multiple instances](#multiple-netbox-instances) - [Usage](#usage) — [command overview](#command-overview), [SSH options](#ssh-options), [pre-connect hook](#pre-connect-hook), [SFTP & SCP](#sftp-and-scp), [port forwarding](#port-forwarding), [static hosts](#static-hosts), [multiple servers](#multiple-servers-in-one-window), [shortcuts](#host-shortcuts), [TUI](#interactive-tui), [notes](#server-notes), [cache](#cache-management) - [Operations](#operations) — post scripts, favorites, history, diagnostics, batch commands, configuration sharing +- [NetBox insights and host tools](#netbox-insights-and-host-tools) — `info`, `open`, `whois`, status warnings, TUI columns and reachability, address fallback, `copy-id`, `known-hosts`, `export`, keyring tokens - [Custom host commands](#custom-host-commands) — automatic aliases for mtr, ping, and other programs - [Hotkey cheat sheet](#hotkey-cheat-sheet) — search, editors, tmux and zellij - [Recipes](#recipes) @@ -56,6 +57,13 @@ The same lookup drives `netssh sftp`, `netssh scp`, and `netssh tunnel`, the int - **Several servers in one window** — `netssh multi` opens hosts, a tag, a NetBox cluster, or a saved group side by side in tmux or zellij, with optional typing into all of them - **Port forwarding** — named forwards in the config (`netssh tunnel host web`, `netssh --forward web host`), ad hoc specs, and `Ctrl+T` in the TUI - **Dry run** — `netssh --dry-run host` prints the final `ssh` command instead of connecting +- **Host details** — `netssh info host` (and `Ctrl+L` in the TUI) shows site, the nested locations down to rack and rack units, role, platform, tenant, status and all IP addresses per interface; `netssh open host` (`Ctrl+G`) opens the NetBox page +- **Reverse lookup** — `netssh whois 10.1.2.3` names the device or VM, interface, DNS name and prefixes of an address +- **Status warning** — asks before connecting to hosts that are `offline`, `planned`, `decommissioning` …; TUI badge and `status:` filter +- **Configurable TUI** — choose what is shown next to each host (`tui.columns`: IP, source, status, site, location path, role, platform, tenant, cluster, tags, kind, alias); the optional `reachability` column marks hosts ✓/✗ after a quick TCP check of their SSH port; `resolver.fallback` connects to another known address when the chosen one does not answer +- **Rules by NetBox attributes** — `ssh.rules` and command restrictions match `site`, `role`, `platform`, `tenant` and `status`, e.g. legacy ciphers for `platform: junos` +- **ssh-copy-id, known_hosts cleanup, exports** — `netssh copy-id tag:lab`, `netssh known-hosts reset host`, `netssh export ansible` (YAML or INI) and `netssh export ssh-config` +- **Keyring tokens** — `netssh token migrate` moves API tokens from the config into the system keyring - **Clean uninstall** — `netssh uninstall` removes the binary, profile entries of all versions, completions, cache, and config ## Installation @@ -212,17 +220,20 @@ Every key, its default, and the section that explains it. Keys marked *layered* | Key | Type | Default | Description | |---|---|---|---| | `netbox.url` | string | — | NetBox base URL (single instance). Use either `netbox` or `netboxes`. | -| `netbox.token` | string | — | API token; `nbt_…` is v2, anything else v1. | +| `netbox.token` | string | — | API token; `nbt_…` is v2, anything else v1. `keyring` reads it from the system keyring — see [Keyring tokens](#keyring-tokens). | | `netbox.token_version` | 1 \| 2 | auto | Detected from the token prefix. | | `netboxes[]` | list | — | Named instances with `name`, `url`, `token`, `token_version`, `disable_prefix`, `ssh` — see [Multiple NetBox instances](#multiple-netbox-instances). | | `resolver.strategies` | list | `[management_subnet, primary_ip]` | Tried in order, first IP wins — see [IP Resolution Strategies](#ip-resolution-strategies). The wizard suggests `primary_ip`. | | `resolver.management_subnets` | list of CIDR | `[]` | Used by `management_subnet`. | | `resolver.interface_name` | string | — | Used by `interface_name`, e.g. `mgmt0`. | | `resolver.hostname_domain` | string | — | Used by `hostname`: appended to NetBox names without a dot. | +| `resolver.fallback` | bool | `false` | When the chosen address does not answer on the SSH port, connect to the first address of another strategy that does — see [Address fallback](#address-fallback). | +| `resolver.fallback_timeout` | milliseconds | `1000` | Connect timeout per address for the fallback check. | | `cache.ttl` | seconds | `3600` | How long a cached IP is used without asking NetBox; `0` always asks NetBox when connecting. | | `cache.path` | path | user cache dir `/netssh/hosts.json` | Cache file; named instances use a subdirectory next to it. | | `ssh.default_user` | string | — | Login user when none is given (`-l` for ssh, `-o User=` for sftp/scp). | | `ssh.default_port` | int | — | Port when none is given (`-p` for ssh, `-P` for sftp/scp). | +| `ssh.warn_status` | list | `[offline, planned, staged, failed, inventory, decommissioning]` | NetBox status values that ask before connecting; `[]` disables the question — see [Status warning](#status-warning). | | `ssh.proxy_jump` | string | — | *Layered.* Jump host (`-J`); `none` disables an inherited one. | | `ssh.host_key_checking` | `strict` \| `accept-new` \| `off` | ssh's own | *Layered.* See [SSH options](#ssh-options). | | `ssh.target` | `ip` \| `name` \| `hostname` | `ip` | *Layered.* What is passed to ssh as destination. | @@ -232,10 +243,12 @@ Every key, its default, and the section that explains it. Keys marked *layered* | `ssh.forwards` | map | `{}` | *Layered.* Named port forwards (`local`, `remote`, `dynamic`) — see [Port forwarding](#port-forwarding). | | `ssh.post_disconnect` | string | — | *Layered.* Local shell command after the client exits; `none` disables inheritance. | | `tui.history_limit` | integer | `10` | Recent hosts shown; `0` means all, favorites are additional. | +| `tui.columns` | list | `[alias, ip, source, status]` | What the TUI shows next to each hostname (also `location`), in order; `[]` shows only names. Adding `reachability` enables the TCP check — see [TUI columns](#tui-columns-and-reachability). | +| `tui.reachability_timeout` | milliseconds | `300` | Timeout of the TCP check of the `reachability` column. | | `activity.enabled` | boolean | `false` | Enable local execution metadata history. | | `activity.retention_days` / `activity.max_entries` | integer | `30` / `1000` | History retention limits; `0` disables the respective limit. | | `ssh.pre_connect` | string | — | *Layered.* Shell command run locally before connecting; a non-zero exit aborts; `none` disables an inherited one — see [Pre-connect hook](#pre-connect-hook). | -| `ssh.rules[]` | list | `[]` | `match` (`tag`, `instance`, `kind`, `name`) plus any layered key. | +| `ssh.rules[]` | list | `[]` | `match` (`tag`, `instance`, `kind`, `name`, `site`, `role`, `platform`, `tenant`, `status`) plus any layered key. | | `shortcuts.domains` | list | `[]` | Suffixes stripped for shortcuts and tried when expanding a short name. | | `shortcuts.strip_hyphens` | bool | `false` | Ignore hyphens when matching shortcuts. | | `shortcuts.rewrites[]` | list | `[]` | `pattern` (Go regex) and `replace` — see [Host shortcuts](#host-shortcuts). | @@ -259,6 +272,9 @@ These variables override the file. Lists are comma-separated (`NETSSH_SHORTCUTS_ | `NETSSH_RESOLVER_STRATEGIES`, `NETSSH_RESOLVER_MANAGEMENT_SUBNETS`, `NETSSH_RESOLVER_INTERFACE_NAME`, `NETSSH_RESOLVER_HOSTNAME_DOMAIN` | `resolver.*` | | `NETSSH_CACHE_TTL`, `NETSSH_CACHE_PATH` | `cache.*` | | `NETSSH_SSH_DEFAULT_USER`, `NETSSH_SSH_DEFAULT_PORT` | `ssh.default_*` | +| `NETSSH_SSH_WARN_STATUS` | `ssh.warn_status` | +| `NETSSH_TUI_COLUMNS`, `NETSSH_TUI_REACHABILITY_TIMEOUT` | `tui.columns`, `tui.reachability_timeout` | +| `NETSSH_RESOLVER_FALLBACK`, `NETSSH_RESOLVER_FALLBACK_TIMEOUT` | `resolver.fallback*` | | `NETSSH_SSH_PROXY_JUMP`, `NETSSH_SSH_HOST_KEY_CHECKING`, `NETSSH_SSH_TARGET`, `NETSSH_SSH_ADDRESS_FAMILY`, `NETSSH_SSH_PRE_CONNECT` | global `ssh.*` options | | `NETSSH_SHORTCUTS_DOMAINS`, `NETSSH_SHORTCUTS_STRIP_HYPHENS`, `NETSSH_SHORTCUTS_SHOW` | `shortcuts.*` | | `NETSSH_COMPLETION_MAX_RESULTS` | `completion.max_results` | @@ -398,6 +414,9 @@ NetBox supports two token formats: `netssh` auto-detects the version from the token prefix and stores it as `token_version` in the config. A hint is shown during `netssh configure` if a legacy v1 token is entered. +To keep tokens out of the config file, store them in the system keyring with +`netssh token set` or `netssh token migrate` — see [Keyring tokens](#keyring-tokens). + ## Usage ### Command overview @@ -409,6 +428,12 @@ A hint is shown during `netssh configure` if a legacy v1 token is entered. | `netssh sftp [netssh flags] [sftp options] HOST[:PATH]` | SFTP session — see [SFTP and SCP](#sftp-and-scp). | | `netssh scp [netssh flags] [scp options] SOURCE... TARGET` | Copy files — see [SFTP and SCP](#sftp-and-scp). | | `netssh doctor [--online]` / `netssh explain HOST` | Diagnose configuration and explain effective settings. | +| `netssh info [--json] HOST` / `netssh open [--print] HOST` | Show NetBox details of a host / open its NetBox page — see [Host details](#host-details-and-netbox-page). | +| `netssh whois [--json] ADDRESS\|HOSTNAME` | Find the host, interface and prefix of an address — see [Reverse lookup](#reverse-lookup). | +| `netssh copy-id [-i KEY] [-l U] [--dry-run] HOST\|tag:T\|cluster:C\|@GROUP...` | Install your public key with ssh-copy-id — see [ssh-copy-id](#ssh-copy-id). | +| `netssh known-hosts reset [--yes] [--dry-run] HOST...` | Remove stored host keys of re-deployed hosts — see [known_hosts cleanup](#known_hosts-cleanup). | +| `netssh export ansible [--format yaml\|ini] [--group-by …]` / `netssh export ssh-config` | Export cached hosts — see [Exports](#exports-ansible-inventory-and-ssh_config). | +| `netssh token set\|migrate\|delete\|status` | Keep NetBox tokens in the system keyring — see [Keyring tokens](#keyring-tokens). | | `netssh favorites` / `netssh history` | Manage saved hosts and optional action records. | | `netssh batch COMMAND HOST...` | Run a configured command with bounded parallelism and retry. | | `netssh config export\|import FILE` | Share host configuration without secrets or executable settings. | @@ -427,7 +452,7 @@ A hint is shown during `netssh configure` if a legacy v1 token is entered. | `netssh uninstall [--dry-run] [--keep-config] [--yes]` | Remove netssh. | | `netssh version` / `netssh --version` | Print the version. | -The netssh flags `--dry-run`, `--jump`, `--no-jump`, and `--forward` must come before the host. Everything after the host is passed to ssh as the remote command (for `tunnel`: forward names). +The netssh flags `--dry-run`, `--force`, `--jump`, `--no-jump`, and `--forward` must come before the host. `--force` connects without asking about the NetBox status. Everything after the host is passed to ssh as the remote command (for `tunnel`: forward names). ### SSH wrapper mode @@ -940,13 +965,15 @@ The TUI opens with your most-recently-connected hosts (10 by default, configurab See the [hotkey cheat sheet](#hotkey-cheat-sheet) for all search and editor keys. -**Tag and kind filter** — press `Ctrl+F` to open a second input line: +**Filter** — press `Ctrl+F` to open a second input line: ``` -Filter: tag:prod kind:vm +Filter: tag:prod kind:vm status:active site:fsn1 location:building-a role:router platform:junos tenant:acme ``` -Multiple `tag:` values are AND-combined. The filter is applied locally against the cache; when doing a live NetBox search the first tag is also forwarded as a query parameter. +`location:` matches any level of the location chain (name or slug), `rack:` the rack name. + +Multiple `tag:` values are AND-combined. `site:`, `role:`, `platform:` and `tenant:` accept the NetBox name or slug; they need a cache written by this version (`netssh cache refresh`). The filter is applied locally against the cache; when doing a live NetBox search the first tag is also forwarded as a query parameter. **User/port override** — press `Ctrl+E` on any highlighted host: @@ -956,6 +983,10 @@ Connect as: admin@my-router:22 Edit the pre-filled value and press `Enter` to connect. `Esc` cancels. The configured default port is pre-filled. An explicit port 22 overrides a different configured default. +**Details and NetBox page** — `Ctrl+L` shows the host's NetBox details (site, rack, role, platform, status, IP addresses) without leaving the TUI; `Enter` connects from there. `Ctrl+G` opens the host's NetBox page in the browser. + +**Columns** — by default each host shows its shortcut alias, IP, source and a badge such as `[offline]` when its NetBox status is not `active`. Choose other information, its order, and the optional `✓`/`✗` reachability check with `tui.columns` — see [TUI columns](#tui-columns-and-reachability). + ### Server notes Select a server in the TUI and press `Ctrl+N` for local notes, or `Ctrl+B` for @@ -1055,7 +1086,9 @@ in the search, while tmux uses `Ctrl+b` as its default prefix. | `Ctrl+E` | open inline editor to override user/port before connecting | | `Ctrl+N` | edit local notes for the selected server | | `Ctrl+B` | edit NetBox comments for the selected server | -| `Ctrl+F` | open/close tag and kind filter (`tag:prod kind:vm`) | +| `Ctrl+F` | open/close the filter (`tag:prod kind:vm status:active site:fsn1 platform:junos`) | +| `Ctrl+L` | show NetBox details of the selected host (`Enter` connects, `Ctrl+G` opens NetBox, `Esc` back) | +| `Ctrl+G` | open the NetBox page of the selected host in the browser | | `Esc` | quit search | | `Ctrl+C` | quit from any TUI mode, including editors | @@ -1307,6 +1340,296 @@ the previous configuration as `netssh.yaml.before-import` with mode `0600`. The preview does not modify files. Export/import is intended for shared host configuration, not a complete backup of local executable settings or secrets. +## NetBox insights and host tools + +These commands and settings use more of what NetBox knows about a host. Site, +role, platform, tenant, status and the NetBox ID are stored in the cache by +`netssh cache refresh` (and by every live lookup); caches written by older +versions get them with the next refresh. + +### Host details and NetBox page + +```sh +netssh info prod/sw1 # site, locations, rack/U, role, platform, tenant, type, serial, IPs per interface +netssh info --json web01 # the same as JSON +netssh open web01 # NetBox page in the browser ($BROWSER, open, or xdg-open) +netssh open --print web01 # only print the URL +``` + +``` +sw1 (device, NetBox prod) + Status Active + Site FSN1 + Location Building A › Floor 2 › Room 2.04 + Rack R12 U20-21 (2U, front) + Path FSN1 › Building A › Floor 2 › Room 2.04 › R12 › U20 + Role Core Switch + Platform Junos + Type Juniper QFX5120 + Connect 10.0.0.7 (management_subnet, cache) + URL https://netbox.example.com/dcim/devices/7/ + +IP addresses + * 10.0.0.7/24 em0 sw1.example.com active + 192.0.2.7/24 xe-0/0/1 active +``` + +`Location` lists every nested NetBox location from the one directly below the +site down to the device's own location (NetBox nests them through `parent`). +`Rack` shows the occupied units with the device height and mounting face. +`*` marks primary addresses. In the TUI, `Ctrl+L` shows the same view and +`Ctrl+G` opens the page. Static hosts have no NetBox record; `info` shows their +configured address, user and port. + +### Reverse lookup + +```sh +netssh whois 10.0.0.7 +netssh whois mail.example.com # resolved through DNS first +netssh whois --json 10.0.0.7 +``` + +``` +10.0.0.7 + IP 10.0.0.7/24 prod/sw1 (device) em0 active dns sw1.example.com + Prefix 10.0.0.0/8 (prod) "internal" + Prefix 10.0.0.0/24 (prod) active site FSN1 vlan 100 (mgmt) + Cache prod/sw1 +``` + +All NetBox instances are queried; prefixes are listed from the widest to the +most specific. The exit code is 1 when nothing is found. A failing instance is +reported as a warning without hiding the answers of the others. + +### Status warning + +Connecting to a host whose NetBox status is listed in `ssh.warn_status` asks first: + +``` +prod/web03 has NetBox status "decommissioning" — connect anyway? [y/N] +``` + +```yaml +ssh: + warn_status: [offline, planned, staged, failed, inventory, decommissioning] # default + # warn_status: [] # never ask +``` + +`--force` skips the question (`netssh --force web03`). Without a terminal +(scripts, pipes) netssh only prints a warning and connects, so automation keeps +working. `--dry-run` prints the warning as a comment. `copy-id` asks the same +question; `--ignore-status` skips it. + +### TUI columns and reachability + +`tui.columns` chooses what the TUI shows next to each hostname, in this order: + +```yaml +tui: + columns: [reachability, ip, status, site, platform, tags] + reachability_timeout: 300 # ms, only used with the reachability column +``` + +``` + ✓ ★ prod/sw1 10.0.0.7 site:fsn1 platform:junos #core + ✗ prod/web03 10.0.2.3 [decommissioning] site:nbg1 platform:ubuntu-24-04 +``` + +| Column | Shows | +|---|---| +| `alias` | shortcut alias (`→ fsnbx1`), with `shortcuts.show` | +| `ip` | the address from the cache or NetBox | +| `kind` | `[device]`, `[vm]` or `[static]` | +| `source` | `[live]`, `[static]`, `[cache 5m]`, `stale` | +| `status` | `[offline]` etc. — only when the NetBox status is not `active` | +| `site`, `role`, `platform`, `tenant`, `cluster` | `site:fsn1` — the slug, like in the `Ctrl+F` filter | +| `location` | `⌂ FSN1 › Building A › Room 2.04 › R12 › U20` — site, every nested location, rack and lowest rack unit | +| `tags` | `#prod #web` | +| `reachability` | `✓` / `✗` before the name: TCP check of the SSH port | + +Without `tui.columns` the TUI shows `[alias, ip, source, status]`; `columns: []` +shows only the names. Empty values are left out. `netssh configure` → +**History & activity logging** offers the same choice. + +The **reachability check is optional** and off by default, because it opens a +TCP connection to every listed host. With the `reachability` column, every host +in the result list is probed on its SSH port (default port, or the static host's +port), in parallel and with `tui.reachability_timeout`. `✓` means the port +answers, `✗` that it does not. Hosts behind a jump host (`proxy_jump`, +`ProxyCommand`) cannot be checked from your machine and get no mark. Each host +is probed once per TUI session. + +### Address fallback + +```yaml +resolver: + strategies: [management_subnet, primary_ip] + fallback: true + fallback_timeout: 1000 # ms per address +``` + +With `resolver.fallback`, netssh checks the chosen address before connecting +(ssh, sftp, scp, tunnel). If it does not answer on the SSH port, netssh asks +NetBox for the addresses of all strategies (plus the primary IPv4 and IPv6) and +connects to the first one that answers: + +``` +netssh: 10.10.0.7 does not answer on port 22, using 192.0.2.7 (primary_ip) +``` + +The fallback address is used for this connection only; the cache keeps the +address of the configured order. Static hosts, hosts behind a jump host and +`--dry-run` are not checked. When no address answers, netssh still tries the +original one, so ssh reports the actual error. + +### Rules by site, role, platform, tenant and status + +`ssh.rules[].match` and `commands.*.match` also select by NetBox attributes +(name or slug, case-insensitive; status by value): + +```yaml +ssh: + rules: + - match: {platform: junos} + host_key_checking: accept-new + options: + KexAlgorithms: +diffie-hellman-group14-sha1 + HostKeyAlgorithms: +ssh-rsa + - match: {site: fsn1, role: core-switch} + proxy_jump: jump-fsn1.example.com + - match: {tenant: customer-a} + options: {User: customer-a} # any layered key +commands: + junos-cli: + args: [ssh, -t, "{address}", cli] + match: {platform: junos, status: active} +``` + +All selectors of a match must apply. `netssh explain HOST` lists the matching +rule numbers and the host's attributes. + +### ssh-copy-id + +```sh +netssh copy-id web01 +netssh copy-id -i ~/.ssh/id_ed25519.pub tag:lab +netssh copy-id -l root cluster:pve-fsn1 --kind vm +netssh copy-id --dry-run @k8s # print the ssh-copy-id commands +``` + +Runs `ssh-copy-id` for each host with the same address, user, port, jump host +and `-o` options as `netssh HOST` (a jump host is passed as `-o ProxyJump=`, +since ssh-copy-id has no `-J`). Selectors work like for `netssh multi`. Hosts +run one after another so password prompts stay readable; a failing host does +not stop the others, and the exit code is 1 if any failed. `-f` and `-n` are +passed on to ssh-copy-id. + +### known_hosts cleanup + +When a live lookup or `netssh cache refresh` finds a new address for a host, +netssh says so: + +``` +netssh: address of web01 changed from 10.0.0.5 to 10.0.0.9 +Remove the known_hosts entry of the old address 10.0.0.5? [y/N] +``` + +After a host was re-installed (new host key, same address), remove its keys with: + +```sh +netssh known-hosts reset web01 # asks first; --yes for scripts +netssh known-hosts reset --dry-run web01 # print the ssh-keygen -R commands +``` + +Removed are the hostname, the current address, the previously cached address, +and `[name]:port` / `[address]:port` for a non-standard port, using +`ssh-keygen -R` (which keeps `known_hosts.old`). A `UserKnownHostsFile` from +`ssh.options` is honoured. + +### Exports: Ansible inventory and ssh_config + +```sh +netssh export ansible > inventory.yml # YAML (default) +netssh export ansible --format ini -o hosts.ini # INI +netssh export ansible --group-by site,role,platform # choose groups +netssh export ansible --tag prod --status active --no-static +netssh export ssh-config -o ~/.ssh/netssh.conf # Host blocks for other tools +``` + +Exports read the cache (run `netssh cache refresh` first) including static hosts. +Filters: `--tag`, `--kind`, `--instance`, `--status`, `--no-static`. A hostname +that exists in several instances is exported as `instance-name`. + +**Ansible.** Each host gets `ansible_host` (the address netssh connects to), +`ansible_user`, `ansible_port` (if not 22) and, with a jump host, +`ansible_ssh_common_args: -o ProxyJump=…`. NetBox hosts additionally get +`netbox_instance`, `netbox_kind`, `netbox_status`, `netbox_site`, `netbox_role`, +`netbox_platform`, `netbox_tenant`, `netbox_cluster`, `netbox_location` +(`Building A/Room 2.04`), `netbox_rack`, `netbox_position`, `netbox_id` and +`netbox_tags` (`--no-vars` leaves them out). Groups are named +`_` such as `tag_prod`, `site_fsn1`, `platform_junos`; +`--group-by` accepts `tag, site, location, rack, role, platform, tenant, kind, +instance, cluster, status` (default `tag,site,role,platform,kind`) or `none`. +`location` puts a host into a group for every level, e.g. `location_building_a` +and `location_room_2_04`. + +```yaml +all: + hosts: + sw1: + ansible_host: 10.0.0.7 + ansible_user: admin + ansible_ssh_common_args: -o ProxyJump=jump.example.com + netbox_site: FSN1 + netbox_platform: Junos + children: + platform_junos: + hosts: + sw1: {} +``` + +```ini +[all] +sw1 ansible_host=10.0.0.7 ansible_user=admin ansible_ssh_common_args="-o ProxyJump=jump.example.com" netbox_site=FSN1 + +[platform_junos] +sw1 +``` + +In INI, `netbox_tags` is a comma-separated string, since INI has no lists. + +**ssh_config.** One `Host` block per host with `HostName`, `User`, `Port`, +`ProxyJump`, host key checking, `AddressFamily` and `ssh.options`; shortcut +aliases become extra Host names (`--no-aliases` to skip). Include the file once +from `~/.ssh/config` with `Include ~/.ssh/netssh.conf`. `extra_args`, forwards +and hooks have no ssh_config equivalent and are not exported. + +### Keyring tokens + +```sh +netssh token migrate # move all tokens from netssh.yaml into the keyring +netssh token set prod # store a token (asked without echo) +echo "$TOKEN" | netssh token set prod +netssh token status # where each instance's token comes from +netssh token delete prod +``` + +Tokens are stored in the system keyring — Secret Service (GNOME Keyring, +KWallet) on Linux, Keychain on macOS — under the service `netssh` with the +instance name as account (`default` for the unnamed `netbox:` instance). The +config then contains `token: keyring`: + +```yaml +netboxes: + - name: prod + url: https://netbox.example.com + token: keyring +``` + +`netssh doctor` checks that every keyring token can be read. On machines without +a keyring (servers, containers) keep the token in the file or use +`NETSSH_NETBOX_TOKEN`. + ## Recipes **Everything through a bastion, except the bastion itself** diff --git a/cmd/netssh/connect_checks.go b/cmd/netssh/connect_checks.go new file mode 100644 index 0000000..76dc6e6 --- /dev/null +++ b/cmd/netssh/connect_checks.go @@ -0,0 +1,217 @@ +package main + +import ( + "context" + "fmt" + "net" + "os" + "os/exec" + "path/filepath" + "strconv" + "strings" + "time" + + "github.com/charmbracelet/x/term" + + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config" + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/netbox" + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/resolver" + internalssh "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/ssh" +) + +// stdinIsTerminal is replaced in tests. +var stdinIsTerminal = func() bool { return term.IsTerminal(os.Stdin.Fd()) } + +// confirmStatus asks before connecting to a host whose NetBox status is listed +// in ssh.warn_status. Without a terminal it only warns, so scripts keep working. +func confirmStatus(cfg *config.Config, host resolvedHost, force bool) error { + if force || !cfg.SSH.WarnsStatus(host.Status) { + return nil + } + message := fmt.Sprintf("%s has NetBox status %q", host.Name, host.Status) + if !stdinIsTerminal() { + fmt.Fprintf(os.Stderr, "netssh: warning: %s\n", message) + return nil + } + if err := confirmAction(message+" — connect anyway", false); err != nil { + return fmt.Errorf("%s: %w (skip this question with --force)", host.Name, err) + } + return nil +} + +// connectPort returns the port given in args (-p for ssh, -P for sftp and scp, +// or -o Port=), else 22. The default port is already in args at this point. +func connectPort(client internalssh.Client, args []string, dests []int) int { + end := len(args) + if len(dests) > 0 && dests[0] <= len(args) { + end = dests[0] + } + flag := client.PortArgs("")[0] + for i := 0; i < end; i++ { + arg := args[i] + value := "" + switch { + case arg == flag && i+1 < end: + value = args[i+1] + case strings.HasPrefix(arg, flag) && len(arg) > len(flag): + value = arg[len(flag):] + case arg == "-o" && i+1 < end: + if key, v, ok := strings.Cut(args[i+1], "="); ok && strings.EqualFold(strings.TrimSpace(key), "port") { + value = v + } + } + if port, err := strconv.Atoi(strings.TrimSpace(value)); err == nil && port > 0 && port <= 65535 { + return port + } + } + return 22 +} + +// reachable reports whether address accepts TCP connections on port. +func reachable(address string, port int, timeout time.Duration) bool { + conn, err := net.DialTimeout("tcp", net.JoinHostPort(address, strconv.Itoa(port)), timeout) + if err != nil { + return false + } + _ = conn.Close() + return true +} + +// usesJumpHost reports whether the connection goes through a jump host, so +// the target address cannot be probed from this machine. +func usesJumpHost(opts config.SSHOptions, flags internalssh.WrapperFlags) bool { + if flags.NoJump { + return false + } + return flags.Jump != "" || (opts.ProxyJump != "" && !strings.EqualFold(opts.ProxyJump, "none")) || opts.Options["proxycommand"] != "" +} + +// fallbackProbe and fallbackCandidates are replaced in tests. +var ( + fallbackProbe = reachable + fallbackCandidates = netboxCandidates +) + +// applyFallback keeps host.IP when it answers on the SSH port. Otherwise it +// tries the addresses the other resolver strategies find and connects to the +// first one that answers, with resolver.fallback enabled. +func applyFallback(cfg *config.Config, host resolvedHost, opts config.SSHOptions, flags internalssh.WrapperFlags, port int) resolvedHost { + if !cfg.Resolver.Fallback || host.Kind == config.StaticKind || usesJumpHost(opts, flags) { + return host + } + timeout := cfg.Resolver.FallbackTimeoutDuration() + if fallbackProbe(host.IP, port, timeout) { + return host + } + candidates, err := fallbackCandidates(cfg, host) + if err != nil { + fmt.Fprintf(os.Stderr, "netssh: %s does not answer on port %d and no other address is known: %v\n", host.IP, port, err) + return host + } + for _, candidate := range candidates { + if candidate.Address == host.IP || !internalssh.ValidAddress(candidate.Address) { + continue + } + if fallbackProbe(candidate.Address, port, timeout) { + fmt.Fprintf(os.Stderr, "netssh: %s does not answer on port %d, using %s (%s)\n", host.IP, port, candidate.Address, candidate.Strategy) + host.IP = candidate.Address + return host + } + } + fmt.Fprintf(os.Stderr, "netssh: no address of %s answers on port %d; trying %s anyway\n", host.Name, port, host.IP) + return host +} + +// netboxCandidates looks the host up again and lists every strategy's address. +func netboxCandidates(cfg *config.Config, host resolvedHost) ([]resolver.Candidate, error) { + if !cfg.HasNetBox() { + return nil, fmt.Errorf("no NetBox configured") + } + client := newNetBoxClient(cfg) + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + target, err := client.FindHost(ctx, host.Name, host.Kind) + if err != nil { + return nil, err + } + chain, err := resolver.New(cfg.Resolver) + if err != nil { + return nil, err + } + return chain.Candidates(ctx, &target, client) +} + +// knownHostsKeys lists the known_hosts names ssh may have stored for a host. +func knownHostsKeys(name string, addresses []string, port int) []string { + seen := map[string]bool{} + var keys []string + add := func(key string) { + if key == "" || seen[strings.ToLower(key)] { + return + } + seen[strings.ToLower(key)] = true + keys = append(keys, key) + if port != 22 { + keys = append(keys, "["+key+"]:"+strconv.Itoa(port)) + } + } + _, bare := netbox.SplitTarget(name) + add(bare) + for _, address := range addresses { + add(address) + } + return keys +} + +// knownHostsFile returns the first UserKnownHostsFile configured for the +// host, or "" for ssh-keygen's default (~/.ssh/known_hosts). +func knownHostsFile(opts config.SSHOptions) string { + fields := strings.Fields(opts.Options["userknownhostsfile"]) + if len(fields) == 0 || fields[0] == "/dev/null" { + return "" + } + file := fields[0] + if rest, ok := strings.CutPrefix(file, "~/"); ok { + if home, err := os.UserHomeDir(); err == nil { + file = filepath.Join(home, rest) + } + } + return file +} + +// removeKnownHosts runs ssh-keygen -R for every key. ssh-keygen keeps a +// known_hosts.old backup of the previous file. +var removeKnownHosts = func(keys []string, file string) error { + for _, key := range keys { + args := []string{"-R", key} + if file != "" { + args = append(args, "-f", file) + } + cmd := exec.Command("ssh-keygen", args...) + cmd.Stdout, cmd.Stderr = os.Stderr, os.Stderr + if err := cmd.Run(); err != nil { + return fmt.Errorf("ssh-keygen -R %s: %w", key, err) + } + } + return nil +} + +// offerKnownHostsCleanup reports a changed NetBox address and, in a terminal, +// offers to remove the host key stored for the old address. +func offerKnownHostsCleanup(opts config.SSHOptions, host resolvedHost, port int) { + fmt.Fprintf(os.Stderr, "netssh: address of %s changed from %s to %s\n", host.Name, host.PreviousIP, host.IP) + if opts.HostKeyChecking == "off" { + return + } + keys := knownHostsKeys("", []string{host.PreviousIP}, port) + if !stdinIsTerminal() { + fmt.Fprintf(os.Stderr, "netssh: remove the old host key with: netssh known-hosts reset %s\n", host.Name) + return + } + if confirmAction("Remove the known_hosts entry of the old address "+host.PreviousIP, false) != nil { + return + } + if err := removeKnownHosts(keys, knownHostsFile(opts)); err != nil { + fmt.Fprintf(os.Stderr, "netssh: %v\n", err) + } +} diff --git a/cmd/netssh/copyid.go b/cmd/netssh/copyid.go new file mode 100644 index 0000000..3813605 --- /dev/null +++ b/cmd/netssh/copyid.go @@ -0,0 +1,186 @@ +package main + +import ( + "errors" + "fmt" + "os" + "os/exec" + "strconv" + "strings" + + "github.com/spf13/cobra" + + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config" + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/netbox" + internalssh "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/ssh" +) + +type copyIDFlags struct { + identity, user, kind, jump string + force, check, dryRun, yes bool + noJump, ignoreStatus bool +} + +func copyIDCmd() *cobra.Command { + var f copyIDFlags + cmd := &cobra.Command{ + Use: "copy-id [flags] HOST|tag:TAG|cluster:NAME|@GROUP...", + Short: "Install your public key on hosts with ssh-copy-id", + Long: `Run ssh-copy-id for each host with the address, user, port, jump host and +SSH options netssh would use to connect. Hosts are given like for netssh multi: +names, tag:TAG, cluster:NAME (devices; --kind vm|all for VMs) or @GROUP. + +Hosts run one after another, so password prompts stay readable. A failing +host does not stop the others; the exit code is 1 if any host failed.`, + Example: ` netssh copy-id web01 + netssh copy-id -i ~/.ssh/id_ed25519.pub tag:lab + netssh copy-id --dry-run @k8s`, + Args: cobra.MinimumNArgs(1), + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) { + cfg, err := config.Load() + if err != nil { + return nil, cobra.ShellCompDirectiveError + } + return multiCompletions(cfg, toComplete) + }, + RunE: func(cmd *cobra.Command, args []string) error { + return runCopyID(loadConfigOrSetup(), args, f) + }, + } + cmd.Flags().StringVarP(&f.identity, "identity", "i", "", "Public key file to install (ssh-copy-id -i)") + cmd.Flags().StringVarP(&f.user, "user", "l", "", "Login user for every host") + cmd.Flags().StringVar(&f.kind, "kind", "", "Host kind for tag: and cluster: — device, vm, or all") + cmd.Flags().StringVar(&f.jump, "jump", "", "Jump host for every host") + cmd.Flags().BoolVar(&f.noJump, "no-jump", false, "Disable jump hosts") + cmd.Flags().BoolVarP(&f.force, "force", "f", false, "Install the key even if it seems present (ssh-copy-id -f)") + cmd.Flags().BoolVarP(&f.check, "check", "n", false, "Only show which keys would be installed (ssh-copy-id -n)") + cmd.Flags().BoolVar(&f.dryRun, "dry-run", false, "Print the ssh-copy-id commands without running them") + cmd.Flags().BoolVarP(&f.yes, "yes", "y", false, fmt.Sprintf("Run for more than %d hosts without asking", maxHostsWithoutConfirm)) + cmd.Flags().BoolVar(&f.ignoreStatus, "ignore-status", false, "Do not ask about hosts with a NetBox status in ssh.warn_status") + _ = cmd.RegisterFlagCompletionFunc("kind", cobra.FixedCompletions([]cobra.Completion{"device", "vm", "all"}, cobra.ShellCompDirectiveNoFileComp)) + return cmd +} + +func runCopyID(cfg *config.Config, selectors []string, f copyIDFlags) error { + if f.jump != "" && f.noJump { + return errors.New("--jump and --no-jump are mutually exclusive") + } + switch f.kind { + case "", "device", "vm", "all": + default: + return fmt.Errorf("--kind must be device, vm, or all") + } + if strings.HasPrefix(f.user, "-") || strings.ContainsAny(f.user, " \t\n@") { + return fmt.Errorf("invalid user %q", f.user) + } + if strings.HasPrefix(f.jump, "-") { + return fmt.Errorf("--jump: invalid host %q", f.jump) + } + c := newCache(cfg) + _ = c.Load() + targets, warnings, err := expandSelectors(cfg, c, selectors, f.kind) + for _, warning := range warnings { + fmt.Fprintln(os.Stderr, "netssh: warning: "+warning) + } + if err != nil { + return err + } + if len(targets) > maxHostsWithoutConfirm && !f.yes && !f.dryRun { + if err := confirmAction(fmt.Sprintf("Run ssh-copy-id for %d hosts", len(targets)), false); err != nil { + return err + } + } + var failed []string + for _, target := range targets { + h, err := resolveTargetE(cfg, target, false) + if err != nil { + fmt.Fprintf(os.Stderr, "netssh: %s: %v\n", target, err) + failed = append(failed, target) + continue + } + if !f.dryRun { + if err := confirmStatus(cfg, h, f.ignoreStatus); err != nil { + fmt.Fprintf(os.Stderr, "netssh: skipping %v\n", err) + failed = append(failed, target) + continue + } + } + argv, err := copyIDArgv(cfg, h, f) + if err != nil { + fmt.Fprintf(os.Stderr, "netssh: %s: %v\n", target, err) + failed = append(failed, target) + continue + } + if f.dryRun { + fmt.Println(internalssh.Quote(argv)) + continue + } + fmt.Fprintf(os.Stderr, "==> %s (%s)\n", h.Name, h.IP) + if err := runCopyIDCommand(argv); err != nil { + fmt.Fprintf(os.Stderr, "netssh: %s: %v\n", h.Name, err) + failed = append(failed, h.Name) + } + } + if len(failed) > 0 { + return fmt.Errorf("ssh-copy-id failed for %d of %d hosts: %s", len(failed), len(targets), strings.Join(failed, " ")) + } + return nil +} + +// runCopyIDCommand is replaced in tests. +var runCopyIDCommand = func(argv []string) error { + path, err := exec.LookPath(argv[0]) + if err != nil { + return fmt.Errorf("%s not found in PATH (it ships with OpenSSH): %w", argv[0], err) + } + cmd := exec.Command(path, argv[1:]...) + cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr + return cmd.Run() +} + +// copyIDArgv builds the ssh-copy-id command for a resolved host. The +// connection options come from the same code that builds ssh commands, so +// the key lands where netssh HOST connects to. ssh-copy-id passes -o and -p +// on to ssh but has no -J, so a jump host becomes -o ProxyJump=. +func copyIDArgv(cfg *config.Config, h resolvedHost, f copyIDFlags) ([]string, error) { + if !internalssh.ValidAddress(h.IP) { + return nil, fmt.Errorf("refusing address %q", h.IP) + } + user, port := hostDefaults(cfg, h) + if f.user != "" { + user = f.user + } + _, name := netbox.SplitTarget(h.Name) + dest := name + if user != "" { + dest = user + "@" + name + } + var args []string + if port > 0 && port != 22 { + args = append(args, "-p", strconv.Itoa(port)) + } + args = append(args, dest) + opts := hostOptions(cfg, h) + opts.ExtraArgs, opts.Forwards = nil, nil + result := internalssh.SSH.Apply(args, []int{len(args) - 1}, internalssh.Host{Name: name, IP: h.IP}, opts, internalssh.WrapperFlags{Jump: f.jump, NoJump: f.noJump}) + + argv := []string{"ssh-copy-id"} + if f.identity != "" { + argv = append(argv, "-i", f.identity) + } + if f.force { + argv = append(argv, "-f") + } + if f.check { + argv = append(argv, "-n") + } + for i := 0; i < len(result.Args); i++ { + if result.Args[i] == "-J" && i+1 < len(result.Args) { + argv = append(argv, "-o", "ProxyJump="+result.Args[i+1]) + i++ + continue + } + argv = append(argv, result.Args[i]) + } + return argv, nil +} diff --git a/cmd/netssh/export.go b/cmd/netssh/export.go new file mode 100644 index 0000000..0c17236 --- /dev/null +++ b/cmd/netssh/export.go @@ -0,0 +1,523 @@ +package main + +import ( + "fmt" + "io" + "os" + "regexp" + "sort" + "strconv" + "strings" + "time" + + "github.com/spf13/cobra" + "go.yaml.in/yaml/v3" + + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/cache" + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config" + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/netbox" + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/storage" +) + +// exportFilter selects the cached hosts to export. +type exportFilter struct { + tag, kind, instance, status string + noStatic bool + output string +} + +func (f *exportFilter) register(cmd *cobra.Command) { + cmd.Flags().StringVar(&f.tag, "tag", "", "Only hosts with this tag") + cmd.Flags().StringVar(&f.kind, "kind", "", "Only this kind: device, vm, or static") + cmd.Flags().StringVar(&f.instance, "instance", "", "Only hosts of this NetBox instance") + cmd.Flags().StringVar(&f.status, "status", "", "Only hosts with this NetBox status, e.g. active") + cmd.Flags().BoolVar(&f.noStatic, "no-static", false, "Leave out static hosts (hosts:)") + cmd.Flags().StringVarP(&f.output, "output", "o", "", "Write to this file instead of stdout") + _ = cmd.RegisterFlagCompletionFunc("kind", cobra.FixedCompletions([]cobra.Completion{"device", "vm", "static"}, cobra.ShellCompDirectiveNoFileComp)) +} + +func (f exportFilter) matches(e cache.Entry) bool { + instance, _ := netbox.SplitTarget(e.Name) + switch { + case f.noStatic && e.Static, + f.kind != "" && !strings.EqualFold(e.Kind, f.kind), + f.instance != "" && !strings.EqualFold(instance, f.instance), + f.status != "" && !strings.EqualFold(e.Status, f.status), + f.tag != "" && !hasTag(e.Tags, f.tag): + return false + } + return true +} + +// exportHost is one host with the connection settings netssh would use. +type exportHost struct { + entry cache.Entry + name string // unique inventory / Host name + address string + user string + port int + opts config.SSHOptions +} + +// exportHosts loads the filtered cache entries. Hostnames that exist in +// several instances get the instance as prefix ("prod-web01"), others stay bare. +func exportHosts(cfg *config.Config, f exportFilter) ([]exportHost, error) { + c := newCache(cfg) + if err := c.Load(); err != nil { + return nil, err + } + var entries []cache.Entry + counts := map[string]int{} + for _, e := range c.All() { + if !f.matches(e) { + continue + } + entries = append(entries, e) + _, name := netbox.SplitTarget(e.Name) + counts[strings.ToLower(name)]++ + } + sort.Slice(entries, func(i, j int) bool { return entries[i].Name < entries[j].Name }) + out := make([]exportHost, 0, len(entries)) + for _, e := range entries { + instance, name := netbox.SplitTarget(e.Name) + if counts[strings.ToLower(name)] > 1 && instance != "" { + name = instance + "-" + name + } + h := hostFromEntry(e, "cache") + user, port := hostDefaults(cfg, h) + out = append(out, exportHost{entry: e, name: name, address: e.IP, user: user, port: port, opts: hostOptions(cfg, h)}) + } + if len(out) == 0 { + return nil, fmt.Errorf("no cached hosts match; run netssh cache refresh first") + } + return out, nil +} + +// writeExport writes data to the --output file, or stdout. +func writeExport(f exportFilter, data []byte) error { + if f.output == "" || f.output == "-" { + _, err := os.Stdout.Write(data) + return err + } + if err := storage.WriteFile(f.output, data, 0o600); err != nil { + return err + } + fmt.Fprintf(os.Stderr, "Written → %s\n", f.output) + return nil +} + +func exportCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "export", + Short: "Export cached hosts as Ansible inventory or ssh_config", + } + cmd.AddCommand(exportAnsibleCmd(), exportSSHConfigCmd()) + return cmd +} + +// ansibleGroupKeys are the attributes --group-by accepts. +var ansibleGroupKeys = []string{"tag", "site", "location", "rack", "role", "platform", "tenant", "kind", "instance", "cluster", "status"} + +func exportAnsibleCmd() *cobra.Command { + var f exportFilter + var format, groupBy string + var noVars bool + cmd := &cobra.Command{ + Use: "ansible", + Short: "Print an Ansible inventory (YAML or INI) of the cached hosts", + Long: `Print an Ansible inventory of the cached hosts. Every host gets ansible_host +(the address netssh connects to), ansible_user, ansible_port and, with a jump +host, ansible_ssh_common_args. NetBox attributes are added as netbox_* host +variables (disable with --no-vars). + +Groups are formed from host attributes, named like tag_prod, site_fsn1, +role_router or platform_junos. Choose them with --group-by (comma-separated: +` + strings.Join(ansibleGroupKeys, ", ") + `, or none).`, + Example: ` netssh export ansible > inventory.yml + netssh export ansible --format ini --group-by site,role -o hosts.ini + netssh export ansible --tag prod --status active`, + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + cfg, err := config.Load() + if err != nil { + return err + } + keys, err := parseGroupBy(groupBy) + if err != nil { + return err + } + hosts, err := exportHosts(cfg, f) + if err != nil { + return err + } + inv := buildInventory(hosts, keys, !noVars) + var data []byte + switch strings.ToLower(format) { + case "yaml", "yml": + data, err = inv.yaml() + case "ini": + data = inv.ini() + default: + return fmt.Errorf("--format must be yaml or ini") + } + if err != nil { + return err + } + return writeExport(f, data) + }, + } + f.register(cmd) + cmd.Flags().StringVar(&format, "format", "yaml", "Inventory format: yaml or ini") + cmd.Flags().StringVar(&groupBy, "group-by", "tag,site,role,platform,kind", "Attributes to group hosts by, or none") + cmd.Flags().BoolVar(&noVars, "no-vars", false, "Leave out the netbox_* host variables") + _ = cmd.RegisterFlagCompletionFunc("format", cobra.FixedCompletions([]cobra.Completion{"yaml", "ini"}, cobra.ShellCompDirectiveNoFileComp)) + return cmd +} + +func parseGroupBy(value string) ([]string, error) { + if strings.EqualFold(strings.TrimSpace(value), "none") || strings.TrimSpace(value) == "" { + return nil, nil + } + var keys []string + for _, key := range strings.Split(value, ",") { + key = strings.ToLower(strings.TrimSpace(key)) + valid := false + for _, known := range ansibleGroupKeys { + valid = valid || key == known + } + if !valid { + return nil, fmt.Errorf("--group-by: unknown attribute %q; use %s, or none", key, strings.Join(ansibleGroupKeys, ", ")) + } + keys = append(keys, key) + } + return keys, nil +} + +// inventoryVar is a host variable; list values stay lists in YAML. +type inventoryVar struct { + key string + value any // string, int, or []string +} + +type inventory struct { + hosts []string + vars map[string][]inventoryVar + groups map[string][]string +} + +var groupInvalid = regexp.MustCompile(`[^a-z0-9_]+`) + +// groupName builds a valid Ansible group name such as site_fsn1. +func groupName(prefix, value string) string { + name := strings.Trim(groupInvalid.ReplaceAllString(strings.ToLower(value), "_"), "_") + if name == "" { + return "" + } + return prefix + "_" + name +} + +func buildInventory(hosts []exportHost, groupBy []string, netboxVars bool) inventory { + inv := inventory{vars: map[string][]inventoryVar{}, groups: map[string][]string{}} + for _, h := range hosts { + e := h.entry + inv.hosts = append(inv.hosts, h.name) + vars := []inventoryVar{{"ansible_host", h.address}} + if h.user != "" { + vars = append(vars, inventoryVar{"ansible_user", h.user}) + } + if h.port > 0 && h.port != 22 { + vars = append(vars, inventoryVar{"ansible_port", h.port}) + } + if jump := h.opts.ProxyJump; jump != "" && !strings.EqualFold(jump, "none") { + vars = append(vars, inventoryVar{"ansible_ssh_common_args", "-o ProxyJump=" + jump}) + } + instance, _ := netbox.SplitTarget(e.Name) + if netboxVars && !e.Static { + for _, pair := range [][2]string{ + {"netbox_instance", instance}, {"netbox_kind", e.Kind}, {"netbox_status", e.Status}, + {"netbox_site", cache.RefOf(e.Site).Name}, {"netbox_role", cache.RefOf(e.Role).Name}, + {"netbox_platform", cache.RefOf(e.Platform).Name}, {"netbox_tenant", cache.RefOf(e.Tenant).Name}, + {"netbox_cluster", e.Cluster}, {"netbox_location", locationNames(e.Location)}, + {"netbox_rack", e.Rack}, {"netbox_position", e.Position}, + } { + if pair[1] != "" { + vars = append(vars, inventoryVar{pair[0], pair[1]}) + } + } + if e.ID > 0 { + vars = append(vars, inventoryVar{"netbox_id", e.ID}) + } + if len(e.Tags) > 0 { + vars = append(vars, inventoryVar{"netbox_tags", append([]string(nil), e.Tags...)}) + } + } + inv.vars[h.name] = vars + for _, key := range groupBy { + var values []string + switch key { + case "tag": + values = e.Tags + case "site": + values = []string{cache.RefOf(e.Site).Key()} + case "location": + // Every level, so a play can target a building or a single room. + for _, location := range e.Location { + values = append(values, location.Key()) + } + case "rack": + values = []string{e.Rack} + case "role": + values = []string{cache.RefOf(e.Role).Key()} + case "platform": + values = []string{cache.RefOf(e.Platform).Key()} + case "tenant": + values = []string{cache.RefOf(e.Tenant).Key()} + case "kind": + values = []string{e.Kind} + case "instance": + values = []string{instance} + case "cluster": + values = []string{e.Cluster} + case "status": + values = []string{e.Status} + } + for _, value := range values { + if group := groupName(key, value); group != "" { + inv.groups[group] = append(inv.groups[group], h.name) + } + } + } + } + return inv +} + +func (inv inventory) groupNames() []string { + names := make([]string, 0, len(inv.groups)) + for name := range inv.groups { + names = append(names, name) + } + sort.Strings(names) + return names +} + +func exportHeader(comment string) string { + return fmt.Sprintf("%s Generated by netssh export on %s — changes are overwritten on the next export.\n", comment, time.Now().Format(time.RFC3339)) +} + +// yaml renders the inventory with host variables under all.hosts and the +// groups as children of all, listing their member hosts. +func (inv inventory) yaml() ([]byte, error) { + node := func(kind yaml.Kind, value string) *yaml.Node { + n := &yaml.Node{Kind: kind, Value: value} + if kind == yaml.ScalarNode { + n.Tag = "!!str" + } + return n + } + mapping := func() *yaml.Node { return &yaml.Node{Kind: yaml.MappingNode} } + add := func(m *yaml.Node, key string, value *yaml.Node) { + m.Content = append(m.Content, node(yaml.ScalarNode, key), value) + } + hosts := mapping() + for _, name := range inv.hosts { + vars := mapping() + for _, v := range inv.vars[name] { + var value *yaml.Node + switch typed := v.value.(type) { + case int: + value = &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!int", Value: strconv.Itoa(typed)} + case []string: + value = &yaml.Node{Kind: yaml.SequenceNode} + for _, item := range typed { + value.Content = append(value.Content, node(yaml.ScalarNode, item)) + } + default: + value = node(yaml.ScalarNode, fmt.Sprint(typed)) + } + add(vars, v.key, value) + } + add(hosts, name, vars) + } + all := mapping() + add(all, "hosts", hosts) + if len(inv.groups) > 0 { + children := mapping() + for _, group := range inv.groupNames() { + members := mapping() + for _, host := range inv.groups[group] { + add(members, host, &yaml.Node{Kind: yaml.MappingNode, Style: yaml.FlowStyle}) + } + g := mapping() + add(g, "hosts", members) + add(children, group, g) + } + add(all, "children", children) + } + root := mapping() + add(root, "all", all) + var b strings.Builder + b.WriteString(exportHeader("#")) + enc := yaml.NewEncoder(&b) + enc.SetIndent(2) + if err := enc.Encode(&yaml.Node{Kind: yaml.DocumentNode, Content: []*yaml.Node{root}}); err != nil { + return nil, err + } + if err := enc.Close(); err != nil { + return nil, err + } + return []byte(b.String()), nil +} + +// ini renders host variables in [all] and one section per group. Lists such +// as netbox_tags become comma-separated strings, since INI has no lists. +func (inv inventory) ini() []byte { + var b strings.Builder + b.WriteString(exportHeader(";")) + b.WriteString("[all]\n") + for _, name := range inv.hosts { + b.WriteString(name) + for _, v := range inv.vars[name] { + value := fmt.Sprint(v.value) + if list, ok := v.value.([]string); ok { + value = strings.Join(list, ",") + } + fmt.Fprintf(&b, " %s=%s", v.key, iniQuote(value)) + } + b.WriteString("\n") + } + for _, group := range inv.groupNames() { + fmt.Fprintf(&b, "\n[%s]\n", group) + for _, host := range inv.groups[group] { + b.WriteString(host + "\n") + } + } + return []byte(b.String()) +} + +// iniQuote quotes values that Ansible's INI parser (shlex) would split. +func iniQuote(value string) string { + if value != "" && !strings.ContainsAny(value, " \t\"'\\#;=") { + return value + } + return `"` + strings.NewReplacer(`\`, `\\`, `"`, `\"`).Replace(value) + `"` +} + +func exportSSHConfigCmd() *cobra.Command { + var f exportFilter + var noAliases bool + cmd := &cobra.Command{ + Use: "ssh-config", + Short: "Print ssh_config Host blocks for the cached hosts", + Long: `Print one ssh_config Host block per cached host with the address, user, port, +jump host and SSH options netssh would use. Tools that run ssh themselves +(VS Code Remote, rsync, git, Ansible) can then reach the hosts by name. + +Include the file from ~/.ssh/config: + + netssh export ssh-config -o ~/.ssh/netssh.conf + echo 'Include ~/.ssh/netssh.conf' >> ~/.ssh/config # once + +Shortcut aliases are added as extra Host names (disable with --no-aliases). +extra_args, forwards and hooks have no ssh_config equivalent and are skipped.`, + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + cfg, err := config.Load() + if err != nil { + return err + } + hosts, err := exportHosts(cfg, f) + if err != nil { + return err + } + aliases := map[string]string{} + if !noAliases { + c := newCache(cfg) + _ = c.Load() + aliases = shortcutAliases(c.All(), *cfg) + } + var b strings.Builder + writeSSHConfig(&b, hosts, aliases) + return writeExport(f, []byte(b.String())) + }, + } + f.register(cmd) + cmd.Flags().BoolVar(&noAliases, "no-aliases", false, "Do not add shortcut aliases as Host names") + return cmd +} + +var sshConfigUnsafe = regexp.MustCompile(`[\s"#*?!,]`) + +func writeSSHConfig(w io.Writer, hosts []exportHost, aliases map[string]string) { + fmt.Fprint(w, exportHeader("#")) + used := map[string]bool{} + for _, h := range hosts { + used[strings.ToLower(h.name)] = true + } + for _, h := range hosts { + if sshConfigUnsafe.MatchString(h.name) { + fmt.Fprintf(w, "\n# skipped %q: not usable as a Host name\n", h.name) + continue + } + names := []string{h.name} + if alias := aliases[strings.ToLower(h.entry.Name)]; alias != "" && !used[strings.ToLower(alias)] && !sshConfigUnsafe.MatchString(alias) { + used[strings.ToLower(alias)] = true + names = append(names, alias) + } + fmt.Fprintf(w, "\nHost %s\n", strings.Join(names, " ")) + _, bare := netbox.SplitTarget(h.entry.Name) + hostName := h.address + if h.opts.Target == "hostname" && plainName(bare) { + hostName = bare + } + line := func(key, value string) { + if value != "" { + fmt.Fprintf(w, " %s %s\n", key, value) + } + } + line("HostName", hostName) + if h.opts.Target == "name" && plainName(bare) { + line("HostKeyAlias", bare) + } + line("User", h.user) + if h.port > 0 && h.port != 22 { + line("Port", strconv.Itoa(h.port)) + } + if jump := h.opts.ProxyJump; jump != "" { + line("ProxyJump", jump) + } + switch h.opts.HostKeyChecking { + case "strict": + line("StrictHostKeyChecking", "yes") + case "accept-new": + line("StrictHostKeyChecking", "accept-new") + case "off": + line("StrictHostKeyChecking", "no") + line("UserKnownHostsFile", "/dev/null") + } + line("AddressFamily", h.opts.AddressFamily) + keys := make([]string, 0, len(h.opts.Options)) + for key := range h.opts.Options { + keys = append(keys, key) + } + sort.Strings(keys) + for _, key := range keys { + line(key, h.opts.Options[key]) + } + if len(h.opts.ExtraArgs) > 0 { + fmt.Fprintf(w, " # extra_args not exported: %s\n", strings.Join(h.opts.ExtraArgs, " ")) + } + } +} + +var plainNamePattern = regexp.MustCompile(`^[A-Za-z0-9]([A-Za-z0-9._-]*[A-Za-z0-9])?$`) + +func plainName(name string) bool { return plainNamePattern.MatchString(name) } + +// locationNames joins a location chain as "Building A/Room 2.04". +func locationNames(chain []config.Ref) string { + names := make([]string, 0, len(chain)) + for _, location := range chain { + names = append(names, location.Name) + } + return strings.Join(names, "/") +} diff --git a/cmd/netssh/insights.go b/cmd/netssh/insights.go new file mode 100644 index 0000000..6fc1e69 --- /dev/null +++ b/cmd/netssh/insights.go @@ -0,0 +1,422 @@ +package main + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "io" + "net" + "os" + "os/exec" + "runtime" + "sort" + "strings" + "text/tabwriter" + "time" + + "github.com/spf13/cobra" + + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config" + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/netbox" +) + +// hostCompletion completes cached hostnames for commands taking hosts. +func hostCompletion(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) { + return completeHosts(toComplete), cobra.ShellCompDirectiveNoFileComp +} + +// netboxHost finds the NetBox record of a host given like on the command line +// (shortcut, instance/host). The cache supplies the ID when it has one. +func netboxHost(ctx context.Context, cfg *config.Config, target string) (netbox.HostEntry, *netbox.Client, resolvedHost, error) { + h, err := resolveTargetE(cfg, target, false) + if err != nil { + return netbox.HostEntry{}, nil, h, err + } + if h.Kind == config.StaticKind { + return netbox.HostEntry{}, nil, h, fmt.Errorf("%s is a static host (hosts:) and has no NetBox record", h.Name) + } + client := newNetBoxClient(cfg) + if client == nil { + return netbox.HostEntry{}, nil, h, fmt.Errorf("no NetBox is configured (netssh configure)") + } + return netboxEntry(ctx, client, h.Name, h.Kind, h.ID, h) +} + +// netboxEntry completes a cached host with its NetBox ID, looking it up if needed. +func netboxEntry(ctx context.Context, client *netbox.Client, name, kind string, id int, h resolvedHost) (netbox.HostEntry, *netbox.Client, resolvedHost, error) { + source, bare := netbox.SplitTarget(name) + entry := netbox.HostEntry{Source: source, Name: bare, Kind: kind, ID: id} + if id <= 0 { + found, err := client.FindHost(ctx, name, kind) + if err != nil { + return entry, client, h, fmt.Errorf("NetBox: %w", err) + } + entry = found + } + return entry, client, h, nil +} + +// openBrowser opens url with $BROWSER, open (macOS), or xdg-open. +var openBrowser = func(url string) error { + var cmd *exec.Cmd + switch browser := os.Getenv("BROWSER"); { + case browser != "": + cmd = exec.Command(browser, url) + case runtime.GOOS == "darwin": + cmd = exec.Command("open", url) + default: + cmd = exec.Command("xdg-open", url) + } + cmd.Stdout, cmd.Stderr = nil, nil + if err := cmd.Start(); err != nil { + return fmt.Errorf("opening browser: %w (print the URL with --print)", err) + } + return cmd.Process.Release() +} + +func openCmd() *cobra.Command { + var printOnly bool + cmd := &cobra.Command{ + Use: "open HOST", + Short: "Open the NetBox page of a host in the web browser", + Long: "Open the device or virtual machine page in the NetBox web interface.\nThe browser is $BROWSER, else open (macOS) or xdg-open.", + Args: cobra.ExactArgs(1), + ValidArgsFunction: hostCompletion, + RunE: func(cmd *cobra.Command, args []string) error { + cfg, err := config.Load() + if err != nil { + return err + } + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) + defer cancel() + entry, client, _, err := netboxHost(ctx, cfg, args[0]) + if err != nil { + return err + } + url, err := client.WebURL(entry) + if err != nil { + return err + } + if printOnly { + fmt.Fprintln(cmd.OutOrStdout(), url) + return nil + } + fmt.Fprintf(os.Stderr, "Opening %s\n", url) + return openBrowser(url) + }, + } + cmd.Flags().BoolVar(&printOnly, "print", false, "Print the URL instead of opening it") + return cmd +} + +func infoCmd() *cobra.Command { + var asJSON bool + cmd := &cobra.Command{ + Use: "info HOST", + Short: "Show site, rack, role, platform, status and IP addresses of a host", + Args: cobra.ExactArgs(1), + ValidArgsFunction: hostCompletion, + RunE: func(cmd *cobra.Command, args []string) error { + cfg, err := config.Load() + if err != nil { + return err + } + h, err := resolveTargetE(cfg, args[0], false) + if err != nil { + return err + } + out := cmd.OutOrStdout() + if h.Kind == config.StaticKind { + return printStaticInfo(out, cfg, h, asJSON) + } + client := newNetBoxClient(cfg) + if client == nil { + return fmt.Errorf("no NetBox is configured (netssh configure)") + } + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + defer cancel() + entry, _, _, err := netboxEntry(ctx, client, h.Name, h.Kind, h.ID, h) + if err != nil { + return err + } + details, err := client.GetDetails(ctx, entry) + if err != nil && details.Name == "" { + return fmt.Errorf("NetBox: %w", err) + } + if asJSON { + return json.NewEncoder(out).Encode(map[string]any{"host": details, "connect": map[string]any{"address": h.IP, "source": h.Origin, "resolver": h.Resolver}}) + } + writeDetails(out, details, h) + if err != nil { + fmt.Fprintf(os.Stderr, "warning: %v\n", err) + } + return nil + }, + } + cmd.Flags().BoolVar(&asJSON, "json", false, "Print JSON") + return cmd +} + +func printStaticInfo(w io.Writer, cfg *config.Config, h resolvedHost, asJSON bool) error { + user, port := hostDefaults(cfg, h) + if asJSON { + return json.NewEncoder(w).Encode(map[string]any{"host": map[string]any{"name": h.Name, "kind": h.Kind, "address": h.IP, "user": user, "port": port, "tags": h.Tags}}) + } + tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0) + fmt.Fprintf(tw, "%s\t(static host from hosts:)\n", h.Name) + row(tw, "Address", h.IP) + row(tw, "User", user) + if port > 0 { + row(tw, "Port", fmt.Sprint(port)) + } + row(tw, "Tags", strings.Join(h.Tags, ", ")) + return tw.Flush() +} + +func row(w io.Writer, label, value string) { + if strings.TrimSpace(value) != "" { + fmt.Fprintf(w, " %s\t%s\n", label, value) + } +} + +// formatDetails renders netssh info output; the TUI detail view uses it too. +func formatDetails(d netbox.HostDetails, h resolvedHost) string { + var b strings.Builder + writeDetails(&b, d, h) + return b.String() +} + +func writeDetails(w io.Writer, d netbox.HostDetails, h resolvedHost) { + tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0) + header := d.Target() + "\t(" + d.Kind + if d.Source != "" { + header += ", NetBox " + d.Source + } + fmt.Fprintln(tw, header+")") + status := d.StatusLabel + if status == "" { + status = d.Status + } + row(tw, "Status", status) + row(tw, "Site", d.Site.Name) + var locations []string + for _, location := range d.Location { + locations = append(locations, location.Name) + } + row(tw, "Location", strings.Join(locations, " › ")) + row(tw, "Rack", strings.TrimSpace(d.Rack+" "+d.RackUnits())) + if len(d.Location) > 0 || d.Rack != "" { + row(tw, "Path", d.LocationPath()) + } + row(tw, "Role", d.Role.Name) + row(tw, "Platform", d.Platform.Name) + row(tw, "Tenant", d.Tenant.Name) + row(tw, "Type", d.DeviceType) + row(tw, "Serial", d.Serial) + row(tw, "Asset tag", d.AssetTag) + row(tw, "Cluster", d.Cluster) + var resources []string + if d.VCPUs != "" { + resources = append(resources, d.VCPUs+" vCPU") + } + if d.MemoryMB > 0 { + resources = append(resources, fmt.Sprintf("%d MB RAM", d.MemoryMB)) + } + if d.DiskGB > 0 { + resources = append(resources, fmt.Sprintf("%d GB disk", d.DiskGB)) + } + row(tw, "Resources", strings.Join(resources, ", ")) + row(tw, "Tags", strings.Join(d.Tags, ", ")) + if h.IP != "" { + via := h.Resolver + if h.Origin != "" { + via = strings.Trim(via+", "+h.Origin, ", ") + } + connect := h.IP + if via != "" { + connect += " (" + via + ")" + } + row(tw, "Connect", connect) + } + row(tw, "URL", d.URL) + row(tw, "Description", d.Description) + _ = tw.Flush() + if len(d.IPs) == 0 { + return + } + fmt.Fprintln(w, "\nIP addresses") + tw = tabwriter.NewWriter(w, 0, 0, 2, ' ', 0) + for _, ip := range d.IPs { + mark := " " + if ip.Primary { + mark = "*" + } + extra := ip.Status + if ip.Role != "" { + extra = strings.Trim(extra+", "+ip.Role, ", ") + } + if ip.VRF != "" { + extra = strings.Trim(extra+", vrf "+ip.VRF, ", ") + } + fmt.Fprintf(tw, " %s %s\t%s\t%s\t%s\n", mark, ip.Address, orDash(ip.Interface), ip.DNSName, extra) + } + _ = tw.Flush() +} + +// whoisResult is what netssh whois found for one address. +type whoisResult struct { + Address string `json:"address"` + NetBox []netbox.IPOwner `json:"netbox"` + Prefixes []netbox.PrefixInfo `json:"prefixes"` + Cached []string `json:"cached_hosts"` +} + +func whoisCmd() *cobra.Command { + var asJSON bool + cmd := &cobra.Command{ + Use: "whois ADDRESS|HOSTNAME", + Short: "Find the host, interface and prefix an IP address belongs to", + Long: `Look an IP address up in every NetBox instance and in the local cache: +the device or VM and interface it is assigned to, its DNS name, and the +prefixes containing it. A hostname is resolved through DNS first.`, + Example: " netssh whois 10.20.0.15\n netssh whois mail.example.com", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + cfg, err := config.Load() + if err != nil { + return err + } + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + defer cancel() + addresses, err := whoisAddresses(ctx, args[0]) + if err != nil { + return err + } + c := newCache(cfg) + _ = c.Load() + var client *netbox.Client + if cfg.HasNetBox() { + client = newNetBoxClient(cfg) + } + var results []whoisResult + var warnings []error + found := false + for _, address := range addresses { + r := whoisResult{Address: address} + for _, e := range c.All() { + if e.IP == address { + r.Cached = append(r.Cached, e.Name) + } + } + sort.Strings(r.Cached) + if client != nil { + owners, err := client.LookupIP(ctx, address) + if err != nil { + warnings = append(warnings, err) + } + r.NetBox = owners + prefixes, err := client.PrefixesContaining(ctx, address) + if err != nil { + warnings = append(warnings, err) + } + r.Prefixes = prefixes + } + found = found || len(r.NetBox) > 0 || len(r.Cached) > 0 || len(r.Prefixes) > 0 + results = append(results, r) + } + for _, warning := range warnings { + fmt.Fprintf(os.Stderr, "warning: %v\n", warning) + } + out := cmd.OutOrStdout() + if asJSON { + return json.NewEncoder(out).Encode(results) + } + writeWhois(out, results) + if !found { + cmd.SilenceErrors, cmd.SilenceUsage = true, true + return &commandExit{code: 1} + } + return nil + }, + } + cmd.Flags().BoolVar(&asJSON, "json", false, "Print JSON") + return cmd +} + +// lookupHost is replaced in tests. +var lookupHost = net.DefaultResolver.LookupHost + +func whoisAddresses(ctx context.Context, query string) ([]string, error) { + query = strings.TrimSpace(query) + if ip := net.ParseIP(query); ip != nil { + return []string{ip.String()}, nil + } + if _, network, err := net.ParseCIDR(query); err == nil { + return []string{network.IP.String()}, nil + } + addresses, err := lookupHost(ctx, query) + if err != nil { + return nil, fmt.Errorf("%q is no IP address and cannot be resolved: %w", query, err) + } + if len(addresses) == 0 { + return nil, errors.New("no addresses found") + } + return addresses, nil +} + +func writeWhois(w io.Writer, results []whoisResult) { + for i, r := range results { + if i > 0 { + fmt.Fprintln(w) + } + fmt.Fprintln(w, r.Address) + tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0) + if len(r.NetBox) == 0 && len(r.Cached) == 0 && len(r.Prefixes) == 0 { + fmt.Fprintln(tw, " not found in NetBox or the cache") + } + for _, o := range r.NetBox { + owner := "unassigned" + if o.Target() != "" { + owner = o.Target() + " (" + o.HostKind + ")" + if o.Interface != "" { + owner += " " + o.Interface + } + } + var extra []string + if o.Status != "" { + extra = append(extra, o.Status) + } + if o.VRF != "" { + extra = append(extra, "vrf "+o.VRF) + } + if o.DNSName != "" { + extra = append(extra, "dns "+o.DNSName) + } + if o.Description != "" { + extra = append(extra, fmt.Sprintf("%q", o.Description)) + } + fmt.Fprintf(tw, " IP\t%s\t%s\t%s\n", o.Address, owner, strings.Join(extra, " ")) + } + for _, p := range r.Prefixes { + var extra []string + for _, pair := range [][2]string{{"", p.Status}, {"site ", p.Site}, {"role ", p.Role}, {"vlan ", p.VLAN}, {"vrf ", p.VRF}} { + if pair[1] != "" { + extra = append(extra, pair[0]+pair[1]) + } + } + if p.Description != "" { + extra = append(extra, fmt.Sprintf("%q", p.Description)) + } + prefix := p.Prefix + if p.Source != "" { + prefix += " (" + p.Source + ")" + } + fmt.Fprintf(tw, " Prefix\t%s\t%s\n", prefix, strings.Join(extra, " ")) + } + for _, name := range r.Cached { + fmt.Fprintf(tw, " Cache\t%s\n", name) + } + _ = tw.Flush() + } +} diff --git a/cmd/netssh/insights_test.go b/cmd/netssh/insights_test.go new file mode 100644 index 0000000..5e1cc56 --- /dev/null +++ b/cmd/netssh/insights_test.go @@ -0,0 +1,559 @@ +package main + +import ( + "bytes" + "context" + "errors" + "fmt" + "net" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "testing" + "time" + + "go.yaml.in/yaml/v3" + + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/cache" + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config" + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/resolver" + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/secret" + internalssh "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/ssh" +) + +// insightEnv writes a config and a cache with two NetBox hosts and a static host. +func insightEnv(t *testing.T, yamlConfig string) string { + t.Helper() + configDir := t.TempDir() + t.Setenv("HOME", t.TempDir()) + t.Setenv("XDG_CONFIG_HOME", configDir) + t.Setenv("XDG_CACHE_HOME", t.TempDir()) + path := filepath.Join(t.TempDir(), "hosts.json") + t.Setenv("NETSSH_CACHE_PATH", path) + t.Setenv("NETSSH_SHELL_ALIAS", "") + if err := os.WriteFile(filepath.Join(configDir, "netssh.yaml"), []byte(yamlConfig), 0o600); err != nil { + t.Fatal(err) + } + cfg, err := config.Load() + if err != nil { + t.Fatal(err) + } + c := cache.New(cfg.CachePath(), 3600) + c.Upsert(cache.Entry{Name: "prod/web01", IP: "10.0.0.1", Kind: "vm", Tags: []string{"prod", "Web Server"}, ID: 11, Status: "active", + Site: &config.Ref{Name: "FSN 1", Slug: "fsn1"}, Role: &config.Ref{Name: "Web", Slug: "web"}, Platform: &config.Ref{Name: "Ubuntu 24.04", Slug: "ubuntu-24-04"}}) + c.Upsert(cache.Entry{Name: "lab/web01", IP: "10.9.0.1", Kind: "vm", ID: 12, Status: "planned"}) + c.Upsert(cache.Entry{Name: "prod/sw1", IP: "10.0.0.2", Kind: "device", ID: 13, Status: "active", Platform: &config.Ref{Name: "Junos", Slug: "junos"}, + Location: []config.Ref{{Name: "Building A", Slug: "building-a"}, {Name: "Room 2.04", Slug: "room-2-04"}}, Rack: "R12", Position: "20"}) + if err := c.Save(); err != nil { + t.Fatal(err) + } + return path +} + +const insightConfig = `netboxes: + - name: prod + url: https://nb.example + token: t + - name: lab + url: https://lab.example + token: t +ssh: + default_user: admin + rules: + - match: {platform: junos} + proxy_jump: jump.example + options: {KexAlgorithms: +diffie-hellman-group14-sha1} +hosts: + - name: router + address: 192.168.1.1 + port: 2222 + tags: [home] +` + +func TestExportAnsibleYAML(t *testing.T) { + insightEnv(t, insightConfig) + cfg, err := config.Load() + if err != nil { + t.Fatal(err) + } + keys, _ := parseGroupBy("tag,site,platform,status") + hosts, err := exportHosts(cfg, exportFilter{}) + if err != nil { + t.Fatal(err) + } + data, err := buildInventory(hosts, keys, true).yaml() + if err != nil { + t.Fatal(err) + } + var inv struct { + All struct { + Hosts map[string]map[string]any `yaml:"hosts"` + Children map[string]struct { + Hosts map[string]any `yaml:"hosts"` + } `yaml:"children"` + } `yaml:"all"` + } + if err := yaml.Unmarshal(data, &inv); err != nil { + t.Fatalf("invalid YAML: %v\n%s", err, data) + } + web := inv.All.Hosts["prod-web01"] + if web["ansible_host"] != "10.0.0.1" || web["ansible_user"] != "admin" || web["netbox_site"] != "FSN 1" || web["netbox_id"] != 11 { + t.Fatalf("prod-web01 vars wrong: %+v\n%s", web, data) + } + if tags, _ := web["netbox_tags"].([]any); len(tags) != 2 { + t.Fatalf("tags should stay a list: %#v", web["netbox_tags"]) + } + if _, ok := inv.All.Hosts["lab-web01"]; !ok { + t.Fatalf("duplicate hostnames must be qualified: %v", inv.All.Hosts) + } + sw := inv.All.Hosts["sw1"] + if sw["ansible_ssh_common_args"] != "-o ProxyJump=jump.example" { + t.Fatalf("jump host missing for platform rule: %+v", sw) + } + router := inv.All.Hosts["router"] + if router["ansible_port"] != 2222 || router["ansible_host"] != "192.168.1.1" { + t.Fatalf("static host wrong: %+v", router) + } + for _, group := range []string{"tag_prod", "tag_web_server", "site_fsn1", "platform_junos", "status_planned", "tag_home"} { + if _, ok := inv.All.Children[group]; !ok { + t.Errorf("group %s missing in\n%s", group, data) + } + } + if _, ok := inv.All.Children["platform_junos"].Hosts["sw1"]; !ok { + t.Fatal("sw1 not in platform_junos") + } +} + +func TestExportAnsibleINI(t *testing.T) { + insightEnv(t, insightConfig) + cfg, _ := config.Load() + hosts, err := exportHosts(cfg, exportFilter{instance: "prod"}) + if err != nil { + t.Fatal(err) + } + keys, _ := parseGroupBy("tag,role") + out := string(buildInventory(hosts, keys, true).ini()) + for _, want := range []string{ + "[all]\nsw1 ansible_host=10.0.0.2 ansible_user=admin ansible_ssh_common_args=\"-o ProxyJump=jump.example\"", + `web01 ansible_host=10.0.0.1`, + `netbox_site="FSN 1"`, + `netbox_tags="prod,Web Server"`, + "\n[role_web]\nweb01\n", + "\n[tag_prod]\nweb01\n", + } { + if !strings.Contains(out, want) { + t.Errorf("missing %q in\n%s", want, out) + } + } + if strings.Contains(out, "lab") || strings.Contains(out, "router") { + t.Fatalf("--instance prod not applied:\n%s", out) + } +} + +func TestExportFiltersAndGroupByValidation(t *testing.T) { + insightEnv(t, insightConfig) + cfg, _ := config.Load() + hosts, err := exportHosts(cfg, exportFilter{status: "active", noStatic: true}) + if err != nil { + t.Fatal(err) + } + if len(hosts) != 2 { + t.Fatalf("status filter: %+v", hosts) + } + if _, err := parseGroupBy("site,color"); err == nil { + t.Fatal("unknown group-by accepted") + } + if keys, err := parseGroupBy("none"); err != nil || keys != nil { + t.Fatal("none should disable groups") + } + if _, err := exportHosts(cfg, exportFilter{tag: "missing"}); err == nil { + t.Fatal("empty export should fail") + } +} + +func TestExportSSHConfig(t *testing.T) { + insightEnv(t, insightConfig) + cfg, _ := config.Load() + hosts, err := exportHosts(cfg, exportFilter{}) + if err != nil { + t.Fatal(err) + } + var b bytes.Buffer + writeSSHConfig(&b, hosts, map[string]string{"prod/sw1": "sw"}) + out := b.String() + for _, want := range []string{ + "\nHost sw1 sw\n HostName 10.0.0.2\n User admin\n ProxyJump jump.example\n kexalgorithms +diffie-hellman-group14-sha1\n", + "\nHost router\n HostName 192.168.1.1\n User admin\n Port 2222\n", + "\nHost prod-web01\n HostName 10.0.0.1\n", + } { + if !strings.Contains(out, want) { + t.Errorf("missing %q in\n%s", want, out) + } + } +} + +func TestCopyIDArgv(t *testing.T) { + insightEnv(t, insightConfig) + cfg, _ := config.Load() + h, err := resolveTargetE(cfg, "prod/sw1", false) + if err != nil { + t.Fatal(err) + } + argv, err := copyIDArgv(cfg, h, copyIDFlags{identity: "/k.pub"}) + if err != nil { + t.Fatal(err) + } + got := internalssh.Quote(argv) + for _, want := range []string{"ssh-copy-id -i /k.pub", "-o ProxyJump=jump.example", "-o kexalgorithms=+diffie-hellman-group14-sha1", "admin@10.0.0.2"} { + if !strings.Contains(got, want) { + t.Errorf("missing %q in %s", want, got) + } + } + if strings.Contains(got, " -J ") { + t.Fatalf("ssh-copy-id has no -J: %s", got) + } + router, _ := resolveTargetE(cfg, "router", false) + argv, _ = copyIDArgv(cfg, router, copyIDFlags{user: "root", noJump: true, check: true}) + if got := internalssh.Quote(argv); !strings.Contains(got, "-n") || !strings.Contains(got, "-p 2222") || !strings.HasSuffix(got, "root@192.168.1.1") { + t.Fatalf("static host argv wrong: %s", got) + } +} + +func TestRunCopyIDContinuesAfterFailure(t *testing.T) { + insightEnv(t, insightConfig) + cfg, _ := config.Load() + var ran []string + old := runCopyIDCommand + runCopyIDCommand = func(argv []string) error { + ran = append(ran, argv[len(argv)-1]) + if strings.Contains(argv[len(argv)-1], "10.0.0.2") { + return errors.New("permission denied") + } + return nil + } + t.Cleanup(func() { runCopyIDCommand = old }) + stubTerminal(t, false) + err := runCopyID(cfg, []string{"prod/sw1", "router"}, copyIDFlags{}) + if err == nil || !strings.Contains(err.Error(), "1 of 2 hosts") || len(ran) != 2 { + t.Fatalf("err %v, ran %v", err, ran) + } +} + +func stubTerminal(t *testing.T, tty bool) { + t.Helper() + old := stdinIsTerminal + stdinIsTerminal = func() bool { return tty } + t.Cleanup(func() { stdinIsTerminal = old }) +} + +func TestConfirmStatusWarnsWithoutTerminal(t *testing.T) { + stubTerminal(t, false) + cfg := &config.Config{} + if err := confirmStatus(cfg, resolvedHost{Name: "web", Status: "offline"}, false); err != nil { + t.Fatalf("non-interactive status check must not fail: %v", err) + } + if err := confirmStatus(cfg, resolvedHost{Name: "web", Status: "active"}, false); err != nil { + t.Fatal(err) + } +} + +func TestKnownHostsKeysAndFile(t *testing.T) { + keys := knownHostsKeys("prod/web01", []string{"10.0.0.1", "10.0.0.1", "10.0.0.9"}, 2222) + want := []string{"web01", "[web01]:2222", "10.0.0.1", "[10.0.0.1]:2222", "10.0.0.9", "[10.0.0.9]:2222"} + if strings.Join(keys, " ") != strings.Join(want, " ") { + t.Fatalf("got %v", keys) + } + if keys := knownHostsKeys("web", []string{"10.0.0.1"}, 22); strings.Join(keys, " ") != "web 10.0.0.1" { + t.Fatalf("got %v", keys) + } + t.Setenv("HOME", "/home/u") + if f := knownHostsFile(config.SSHOptions{Options: map[string]string{"userknownhostsfile": "~/.ssh/lab_hosts ~/.ssh/other"}}); f != "/home/u/.ssh/lab_hosts" { + t.Fatalf("file %q", f) + } + if f := knownHostsFile(config.SSHOptions{Options: map[string]string{"userknownhostsfile": "/dev/null"}}); f != "" { + t.Fatalf("file %q", f) + } +} + +func TestKnownHostsResetRemovesNameAndAddresses(t *testing.T) { + insightEnv(t, insightConfig) + var removed []string + old := removeKnownHosts + removeKnownHosts = func(keys []string, file string) error { removed = append(removed, keys...); return nil } + t.Cleanup(func() { removeKnownHosts = old }) + cmd := rootCmd() + cmd.SetArgs([]string{"known-hosts", "reset", "--yes", "router"}) + if err := cmd.Execute(); err != nil { + t.Fatal(err) + } + if strings.Join(removed, " ") != "router [router]:2222 192.168.1.1 [192.168.1.1]:2222" { + t.Fatalf("removed %v", removed) + } +} + +func TestConnectPort(t *testing.T) { + for _, tc := range []struct { + client internalssh.Client + args []string + want int + }{ + {internalssh.SSH, []string{"-p", "2222", "web"}, 2222}, + {internalssh.SSH, []string{"-p2200", "web"}, 2200}, + {internalssh.SSH, []string{"-o", "Port=2022", "web"}, 2022}, + {internalssh.SFTP, []string{"-P", "23", "web"}, 23}, + {internalssh.SSH, []string{"web", "-p", "1"}, 22}, + } { + dest := 0 + for i, a := range tc.args { + if a == "web" { + dest = i + } + } + if got := connectPort(tc.client, tc.args, []int{dest}); got != tc.want { + t.Errorf("%v: got %d, want %d", tc.args, got, tc.want) + } + } +} + +func TestApplyFallback(t *testing.T) { + up := map[string]bool{"10.0.0.2": true} + oldProbe, oldCandidates := fallbackProbe, fallbackCandidates + fallbackProbe = func(address string, port int, _ time.Duration) bool { return up[address] && port == 22 } + fallbackCandidates = func(*config.Config, resolvedHost) ([]resolver.Candidate, error) { + return []resolver.Candidate{{Address: "10.0.0.1", Strategy: "management_subnet"}, {Address: "10.0.0.2", Strategy: "primary_ip"}}, nil + } + t.Cleanup(func() { fallbackProbe, fallbackCandidates = oldProbe, oldCandidates }) + host := resolvedHost{Name: "web", Kind: "vm", IP: "10.0.0.1"} + + cfg := &config.Config{} + if got := applyFallback(cfg, host, config.SSHOptions{}, internalssh.WrapperFlags{}, 22); got.IP != "10.0.0.1" { + t.Fatal("fallback ran while disabled") + } + cfg.Resolver.Fallback = true + if got := applyFallback(cfg, host, config.SSHOptions{}, internalssh.WrapperFlags{}, 22); got.IP != "10.0.0.2" { + t.Fatalf("fallback did not switch: %s", got.IP) + } + if got := applyFallback(cfg, host, config.SSHOptions{ProxyJump: "jump"}, internalssh.WrapperFlags{}, 22); got.IP != "10.0.0.1" { + t.Fatal("hosts behind a jump host must not be probed") + } + if got := applyFallback(cfg, host, config.SSHOptions{ProxyJump: "jump"}, internalssh.WrapperFlags{NoJump: true}, 22); got.IP != "10.0.0.2" { + t.Fatal("--no-jump should allow the fallback") + } + up = map[string]bool{} + if got := applyFallback(cfg, host, config.SSHOptions{}, internalssh.WrapperFlags{}, 22); got.IP != "10.0.0.1" { + t.Fatal("must keep the chosen address when nothing answers") + } +} + +func TestReachableProbe(t *testing.T) { + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Skip(err) + } + port := ln.Addr().(*net.TCPAddr).Port + if !reachable("127.0.0.1", port, time.Second) { + t.Fatal("listening port reported unreachable") + } + ln.Close() + if reachable("127.0.0.1", port, 200*time.Millisecond) { + t.Fatal("closed port reported reachable") + } +} + +func TestWhoisAddresses(t *testing.T) { + old := lookupHost + lookupHost = func(_ context.Context, host string) ([]string, error) { + if host == "mail.example.com" { + return []string{"192.0.2.25"}, nil + } + return nil, errors.New("no such host") + } + t.Cleanup(func() { lookupHost = old }) + for in, want := range map[string]string{"10.0.0.1": "10.0.0.1", "10.0.0.0/24": "10.0.0.0", "mail.example.com": "192.0.2.25"} { + got, err := whoisAddresses(context.Background(), in) + if err != nil || len(got) != 1 || got[0] != want { + t.Errorf("%s: %v %v", in, got, err) + } + } + if _, err := whoisAddresses(context.Background(), "nope.invalid"); err == nil { + t.Fatal("unresolvable name accepted") + } +} + +func TestWhoisCommandUsesNetBoxAndCache(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/api/ipam/ip-addresses/": + fmt.Fprint(w, `{"count":1,"results":[{"address":"10.0.0.1/24","dns_name":"web01.example.com","assigned_object":{"name":"eth0","virtual_machine":{"id":11,"name":"web01"}}}]}`) + case "/api/ipam/prefixes/": + fmt.Fprint(w, `{"count":1,"results":[{"prefix":"10.0.0.0/24","description":"servers"}]}`) + default: + http.NotFound(w, r) + } + })) + defer srv.Close() + insightEnv(t, fmt.Sprintf("netboxes:\n - name: prod\n url: %s\n token: t\n", srv.URL)) + var out bytes.Buffer + cmd := rootCmd() + cmd.SetOut(&out) + cmd.SetArgs([]string{"whois", "10.0.0.1"}) + if err := cmd.Execute(); err != nil { + t.Fatal(err) + } + for _, want := range []string{"prod/web01 (vm) eth0", "dns web01.example.com", "10.0.0.0/24 (prod)", `"servers"`, "Cache prod/web01"} { + if !strings.Contains(out.String(), want) { + t.Errorf("missing %q in\n%s", want, out.String()) + } + } +} + +func TestOpenPrintsNetBoxURL(t *testing.T) { + insightEnv(t, insightConfig) + var out bytes.Buffer + cmd := rootCmd() + cmd.SetOut(&out) + cmd.SetArgs([]string{"open", "--print", "prod/sw1"}) + if err := cmd.Execute(); err != nil { + t.Fatal(err) + } + if strings.TrimSpace(out.String()) != "https://nb.example/dcim/devices/13/" { + t.Fatalf("got %q", out.String()) + } + cmd = rootCmd() + cmd.SetArgs([]string{"open", "--print", "router"}) + if err := cmd.Execute(); err == nil || !strings.Contains(err.Error(), "static host") { + t.Fatalf("static host: %v", err) + } +} + +func TestTokenMigrateAndResolve(t *testing.T) { + secret.MockInit() + insightEnv(t, insightConfig) + var saved config.Config + old := saveConfig + saveConfig = func(cfg config.Config) error { saved = cfg; return nil } + t.Cleanup(func() { saveConfig = old }) + tokenMu.Lock() + tokenCache = map[string]string{} + tokenMu.Unlock() + + var out bytes.Buffer + cmd := rootCmd() + cmd.SetOut(&out) + cmd.SetArgs([]string{"token", "migrate"}) + if err := cmd.Execute(); err != nil { + t.Fatal(err) + } + if len(saved.NetBoxes) != 2 || !config.IsKeyringToken(saved.NetBoxes[0].Token) || !config.IsKeyringToken(saved.NetBoxes[1].Token) { + t.Fatalf("tokens not replaced: %+v", saved.NetBoxes) + } + token, err := resolveToken(saved.NetBoxes[0]) + if err != nil || token != "t" { + t.Fatalf("token %q, %v", token, err) + } + + cmd = rootCmd() + cmd.SetIn(strings.NewReader("nbt_secret\n")) + cmd.SetOut(&out) + cmd.SetArgs([]string{"token", "set", "lab"}) + if err := cmd.Execute(); err != nil { + t.Fatal(err) + } + stored, err := secret.Get("lab") + if err != nil || stored != "nbt_secret" { + t.Fatalf("stored %q, %v", stored, err) + } + for _, n := range saved.NetBoxes { + if n.Name == "lab" && n.TokenVersion != 2 { + t.Fatalf("token version not detected: %+v", n) + } + } + cmd = rootCmd() + cmd.SetArgs([]string{"token", "set"}) + if err := cmd.Execute(); err == nil { + t.Fatal("ambiguous instance accepted") + } +} + +func TestCacheRefreshStoresAttributesAndReportsChangedAddresses(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/api/dcim/devices/": + fmt.Fprint(w, `{"count":1,"results":[{"id":5,"name":"sw1","primary_ip4":{"address":"192.0.2.5/24"}, + "status":{"value":"offline"},"site":{"name":"FSN1","slug":"fsn1"},"role":{"name":"Switch","slug":"switch"},"platform":{"name":"Junos","slug":"junos"}}]}`) + case "/api/virtualization/virtual-machines/", "/api/ipam/ip-addresses/": + fmt.Fprint(w, `{"count":0,"results":[]}`) + default: + http.NotFound(w, r) + } + })) + defer srv.Close() + path := prepareRefreshTest(t, srv.URL, "[primary_ip]") + c := cache.New(path, 3600) + c.Upsert(cache.Entry{Name: "sw1", IP: "192.0.2.99", Kind: "device"}) + if err := c.Save(); err != nil { + t.Fatal(err) + } + stdout := captureStdout(t, func() { + cmd := rootCmd() + cmd.SetArgs([]string{"cache", "refresh"}) + if err := cmd.Execute(); err != nil { + t.Fatal(err) + } + }) + if !strings.Contains(stdout, "sw1: 192.0.2.99 → 192.0.2.5") || !strings.Contains(stdout, "known-hosts reset") { + t.Fatalf("address change not reported:\n%s", stdout) + } + if err := c.Load(); err != nil { + t.Fatal(err) + } + got, _ := c.Get("sw1") + if got.ID != 5 || got.Status != "offline" || cache.RefOf(got.Site).Slug != "fsn1" || cache.RefOf(got.Platform).Slug != "junos" || cache.RefOf(got.Role).Name != "Switch" { + t.Fatalf("attributes not cached: %+v", got) + } + cfg, _ := config.Load() + h, _ := resolveTargetE(cfg, "sw1", false) + if h.Status != "offline" || h.Platform.Slug != "junos" || h.ID != 5 { + t.Fatalf("resolved host lacks attributes: %+v", h) + } +} + +func captureStdout(t *testing.T, fn func()) string { + t.Helper() + r, w, err := os.Pipe() + if err != nil { + t.Fatal(err) + } + old := os.Stdout + os.Stdout = w + done := make(chan string) + go func() { + var b bytes.Buffer + _, _ = b.ReadFrom(r) + done <- b.String() + }() + defer func() { os.Stdout = old }() + fn() + w.Close() + os.Stdout = old + return <-done +} + +func TestExportLocationVarsAndGroups(t *testing.T) { + insightEnv(t, insightConfig) + cfg, _ := config.Load() + hosts, err := exportHosts(cfg, exportFilter{instance: "prod", kind: "device"}) + if err != nil { + t.Fatal(err) + } + keys, _ := parseGroupBy("location,rack") + out := string(buildInventory(hosts, keys, true).ini()) + for _, want := range []string{`netbox_location="Building A/Room 2.04"`, "netbox_rack=R12", "netbox_position=20", + "\n[location_building_a]\nsw1\n", "\n[location_room_2_04]\nsw1\n", "\n[rack_r12]\nsw1\n"} { + if !strings.Contains(out, want) { + t.Errorf("missing %q in\n%s", want, out) + } + } +} diff --git a/cmd/netssh/instances.go b/cmd/netssh/instances.go index 3cbd791..20b2a08 100644 --- a/cmd/netssh/instances.go +++ b/cmd/netssh/instances.go @@ -20,14 +20,16 @@ func newNetBoxClient(cfg *config.Config) *netbox.Client { if len(cfg.NetBoxes) > 0 { sources := make([]netbox.Source, 0, len(cfg.NetBoxes)) for _, source := range cfg.NetBoxes { - sources = append(sources, netbox.Source{Name: source.Name, URL: source.URL, Token: source.Token, TokenVersion: source.TokenVersion}) + token, version := clientToken(source) + sources = append(sources, netbox.Source{Name: source.Name, URL: source.URL, Token: token, TokenVersion: version}) } return netbox.NewMultiClient(sources) } if cfg.NetBox.URL == "" { return nil } - return netbox.NewClient(cfg.NetBox.URL, cfg.NetBox.Token, cfg.NetBox.TokenVersion) + token, version := clientToken(cfg.NetBox) + return netbox.NewClient(cfg.NetBox.URL, token, version) } // shortcutAliases maps each target (lowercased) to the shell alias generated for diff --git a/cmd/netssh/knownhosts.go b/cmd/netssh/knownhosts.go new file mode 100644 index 0000000..4f3e408 --- /dev/null +++ b/cmd/netssh/knownhosts.go @@ -0,0 +1,98 @@ +package main + +import ( + "fmt" + "os" + "strings" + + "github.com/spf13/cobra" + + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config" + internalssh "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/ssh" +) + +func knownHostsCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "known-hosts", + Short: "Clean up host keys of re-deployed hosts", + } + cmd.AddCommand(knownHostsResetCmd()) + return cmd +} + +func knownHostsResetCmd() *cobra.Command { + var yes, dryRun bool + cmd := &cobra.Command{ + Use: "reset HOST...", + Short: "Remove the known_hosts entries of hosts (name, current and previous address)", + Long: `Remove the stored host keys of hosts with ssh-keygen -R, after a host was +re-installed or got a new address. Removed are the hostname, the address +netssh connects to, the previously cached address if NetBox changed it, and +[name]:port / [address]:port for a non-standard port. ssh-keygen keeps the +previous file as known_hosts.old.`, + Example: " netssh known-hosts reset web01\n netssh known-hosts reset --dry-run web01 web02", + Args: cobra.MinimumNArgs(1), + ValidArgsFunction: hostCompletion, + RunE: func(cmd *cobra.Command, args []string) error { + cfg, err := config.Load() + if err != nil { + return err + } + c := newCache(cfg) + _ = c.Load() + type plan struct { + keys []string + file string + } + var plans []plan + var all []string + for _, target := range args { + previous, _ := c.Get(target) + h, err := resolveTargetE(cfg, target, false) + if err != nil { + return err + } + addresses := []string{h.IP} + if h.PreviousIP != "" { + addresses = append(addresses, h.PreviousIP) + } + if previous.IP != "" && previous.IP != h.IP { + addresses = append(addresses, previous.IP) + } + _, port := hostDefaults(cfg, h) + if port <= 0 { + port = 22 + } + opts := hostOptions(cfg, h) + p := plan{keys: knownHostsKeys(h.Name, addresses, port), file: knownHostsFile(opts)} + plans = append(plans, p) + all = append(all, p.keys...) + } + if dryRun { + for _, p := range plans { + for _, key := range p.keys { + argv := []string{"ssh-keygen", "-R", key} + if p.file != "" { + argv = append(argv, "-f", p.file) + } + fmt.Fprintln(cmd.OutOrStdout(), internalssh.Quote(argv)) + } + } + return nil + } + if err := confirmAction("Remove known_hosts entries for "+strings.Join(all, ", "), yes); err != nil { + return err + } + for _, p := range plans { + if err := removeKnownHosts(p.keys, p.file); err != nil { + return err + } + } + fmt.Fprintln(os.Stderr, "Done. The next connection asks to confirm the new host key.") + return nil + }, + } + cmd.Flags().BoolVarP(&yes, "yes", "y", false, "Do not ask for confirmation") + cmd.Flags().BoolVar(&dryRun, "dry-run", false, "Print the ssh-keygen commands without running them") + return cmd +} diff --git a/cmd/netssh/main.go b/cmd/netssh/main.go index 4bfbcac..7c763e8 100644 --- a/cmd/netssh/main.go +++ b/cmd/netssh/main.go @@ -32,25 +32,32 @@ import ( // managedSubcommands are dispatched to cobra; everything else is treated as SSH wrapper mode. var managedSubcommands = map[string]bool{ "doctor": true, "explain": true, "favorites": true, "history": true, "batch": true, "config": true, - "configure": true, - "notes": true, - "search": true, - "cache": true, - "alias": true, - "hook": true, - "shell-init": true, - "completion": true, - "__complete": true, - "help": true, - "version": true, - "uninstall": true, - "sftp": true, - "scp": true, - "tunnel": true, - "hosts": true, - "multi": true, - "sync": true, - "run": true, + "configure": true, + "notes": true, + "search": true, + "cache": true, + "alias": true, + "hook": true, + "shell-init": true, + "completion": true, + "__complete": true, + "help": true, + "version": true, + "uninstall": true, + "sftp": true, + "scp": true, + "tunnel": true, + "hosts": true, + "multi": true, + "sync": true, + "run": true, + "open": true, + "info": true, + "whois": true, + "copy-id": true, + "export": true, + "token": true, + "known-hosts": true, } func main() { @@ -221,6 +228,49 @@ type resolvedHost struct { Kind string IP string Tags []string + ID int // NetBox ID; 0 for static hosts and old cache entries + Status string + Site config.Ref + Role config.Ref + Platform config.Ref + Tenant config.Ref + Location []config.Ref + Rack string + Position string + // PreviousIP is the cached address a live lookup replaced, if it differs. + PreviousIP string +} + +// hostFromEntry converts a cache entry into a resolved host. +func hostFromEntry(entry cache.Entry, origin string) resolvedHost { + return resolvedHost{ + Name: entry.Name, Kind: entry.Kind, IP: entry.IP, Tags: entry.Tags, Origin: origin, Resolver: entry.Resolver, CachedAt: entry.CachedAt, + ID: entry.ID, Status: entry.Status, Site: cache.RefOf(entry.Site), Role: cache.RefOf(entry.Role), Platform: cache.RefOf(entry.Platform), Tenant: cache.RefOf(entry.Tenant), + Location: entry.Location, Rack: entry.Rack, Position: entry.Position, + } +} + +// LocationPath renders site › locations › rack › U position. +func (h resolvedHost) LocationPath() string { + return config.LocationPath(h.Site, h.Location, h.Rack, h.Position) +} + +// cacheEntry records a NetBox host and its resolved address in the cache. +func cacheEntry(target netbox.HostEntry, ip, strategy string) cache.Entry { + return cache.Entry{ + Name: target.Target(), IP: ip, Kind: target.Kind, Tags: target.Tags, Cluster: target.Cluster, Resolver: strategy, + ID: target.ID, Status: target.Status, Site: cache.RefPtr(target.Site), Role: cache.RefPtr(target.Role), Platform: cache.RefPtr(target.Platform), Tenant: cache.RefPtr(target.Tenant), + Location: target.Location, Rack: target.Rack, Position: target.Position, + } +} + +// upsertResolved caches a live result and reports the address it replaced. +func upsertResolved(c *cache.Cache, entry cache.Entry) (previous string) { + if old, _ := c.Get(entry.Name); old.Name != "" && !old.Static && old.IP != "" && old.IP != entry.IP { + previous = old.IP + } + c.Upsert(entry) + return previous } // resolveTarget finds host in the cache (also by shortcut) or NetBox and @@ -250,7 +300,7 @@ func resolveTargetE(cfg *config.Config, host string, record bool) (resolvedHost, if entry.Static { origin = "static" } - return resolvedHost{Name: entry.Name, Kind: entry.Kind, IP: entry.IP, Tags: entry.Tags, Origin: origin, Resolver: entry.Resolver, CachedAt: entry.CachedAt} + return hostFromEntry(entry, origin) } // Static hosts from the config win and need no NetBox. @@ -328,10 +378,11 @@ func resolveTargetE(cfg *config.Config, host string, record bool) (resolvedHost, return resolvedHost{}, fmt.Errorf("IP resolution for %q: %w", host, err) } - entry := cache.Entry{Name: target.Target(), IP: ip, Kind: target.Kind, Tags: target.Tags, Cluster: target.Cluster, Resolver: strategy} - c.Upsert(entry) + entry := cacheEntry(target, ip, strategy) + previous := upsertResolved(c, entry) result := used(entry) result.Origin = "live" + result.PreviousIP = previous return result, nil } @@ -375,6 +426,20 @@ func connectHost(cfg *config.Config, client internalssh.Client, flags internalss } fatalf("name a forward or a spec such as 8080:localhost:80; forwards for %s: %s", host.Name, available) } + if flags.DryRun { + if cfg.SSH.WarnsStatus(host.Status) { + fmt.Fprintf(os.Stderr, "# warning: %s has NetBox status %q\n", host.Name, host.Status) + } + } else { + if err := confirmStatus(cfg, host, flags.Force); err != nil { + fatalf("%v", err) + } + port := connectPort(client, args, dests) + host = applyFallback(cfg, host, opts, flags, port) + if host.PreviousIP != "" { + offerKnownHostsCleanup(opts, host, port) + } + } if err := runPreConnect(opts.PreConnectCommand(), host, client.Name, flags.DryRun); err != nil { fatalf("%v", err) } @@ -401,8 +466,7 @@ func connectHost(cfg *config.Config, client internalssh.Client, flags internalss // hostOptions merges the global, instance, and rule SSH options for host. func hostOptions(cfg *config.Config, host resolvedHost) config.SSHOptions { - instance, name := netbox.SplitTarget(host.Name) - return cfg.SSHOptionsFor(config.SSHHost{Instance: instance, Name: name, Kind: host.Kind, Tags: host.Tags}) + return cfg.SSHOptionsFor(commandHost(host)) } // execClient replaces the process with the client, or prints the command for --dry-run. @@ -466,9 +530,15 @@ func runTUI() { } return err }) + known := func(name, kind string, tags []string) resolvedHost { + if e, _ := c.Get(name); e.Name != "" && e.Kind == kind { + return hostFromEntry(e, "cache") + } + return resolvedHost{Name: name, Kind: kind, Tags: tags} + } m.SetCommands(func(name, kind string, tags []string) []string { var names []string - h := commandHost(resolvedHost{Name: name, Kind: kind, Tags: tags}) + h := commandHost(known(name, kind, tags)) for name, c := range cfg.Commands { if c.Allowed(h) { names = append(names, name) @@ -483,8 +553,9 @@ func runTUI() { } m.SetNotes(notes.New(cfg.NetBox.URL, nbClient)) m.SetForwards(func(name, kind string, tags []string) []string { - return forwardNames(hostOptions(cfg, resolvedHost{Name: name, Kind: kind, Tags: tags})) + return forwardNames(hostOptions(cfg, known(name, kind, tags))) }) + configureTUIExtras(m, cfg, nbClient, known) p := tea.NewProgram(m) final, err := p.Run() if err != nil { @@ -523,10 +594,9 @@ func runTUI() { } // Revalidate stale or live TUI results through the configured resolver chain. - var tags []string + var resolved resolvedHost if cached, fresh := c.Get(host.Name); fresh && cached.Kind == host.Kind { - host.IP = cached.IP - tags = cached.Tags + resolved = hostFromEntry(cached, "cache") } else { if nbClient == nil { fatalf("cannot resolve %q: NetBox is not configured", host.Name) @@ -546,16 +616,19 @@ func runTUI() { if err != nil { fatalf("IP resolution: %v", err) } - c.Upsert(cache.Entry{Name: target.Target(), IP: host.IP, Kind: target.Kind, Tags: target.Tags, Cluster: target.Cluster, Resolver: strategy}) - tags = target.Tags + entry := cacheEntry(target, host.IP, strategy) + previous := upsertResolved(c, entry) + resolved = hostFromEntry(entry, "live") + resolved.PreviousIP = previous } + host.IP = resolved.IP fmt.Fprintf(os.Stderr, "Connecting to %s (%s)…\n", host.Name, host.IP) c.MarkUsed(host.Name) _ = c.Save() client, flags, args := tuiCommand(cfg, host) - connectHost(cfg, client, flags, resolvedHost{Name: host.Name, Kind: host.Kind, IP: host.IP, Tags: tags}, []int{len(args) - 1}, args) + connectHost(cfg, client, flags, resolved, []int{len(args) - 1}, args) } // tuiCommand builds the client and arguments for the action chosen in the TUI. @@ -632,7 +705,8 @@ func rootCmd() *cobra.Command { }, } - root.AddCommand(doctorCmd(), explainCmd(), favoritesCmd(), historyCmd(), batchCmd(), configCmd(), runCmd(), notesCmd(), configureCmd(), searchCmd(), cacheCmd(), aliasCmd(), hookCmd(), shellInitCmd(), versionCmd(), uninstallCmd(), sftpCmd(), scpCmd(), tunnelCmd(), hostsCmd(), multiCmd(), syncCmd()) + root.AddCommand(doctorCmd(), explainCmd(), favoritesCmd(), historyCmd(), batchCmd(), configCmd(), runCmd(), notesCmd(), configureCmd(), searchCmd(), cacheCmd(), aliasCmd(), hookCmd(), shellInitCmd(), versionCmd(), uninstallCmd(), sftpCmd(), scpCmd(), tunnelCmd(), hostsCmd(), multiCmd(), syncCmd(), + openCmd(), infoCmd(), whoisCmd(), copyIDCmd(), exportCmd(), tokenCmd(), knownHostsCmd()) // cobra builds the "completion" command lazily; force init so we can extend it. root.InitDefaultCompletionCmd() @@ -883,6 +957,7 @@ func cacheRefreshCmd() *cobra.Command { return fmt.Errorf("resolver: %w", err) } written := 0 + var changed []string nameCounts := make(map[string]int) for _, e := range entries { nameCounts[strings.ToLower(e.Target())]++ @@ -906,7 +981,9 @@ func cacheRefreshCmd() *cobra.Command { fmt.Fprintf(os.Stderr, "warning: skipping %s: %v\n", e.Name, err) continue } - c.Upsert(cache.Entry{Name: e.Target(), IP: ip, Kind: e.Kind, Tags: e.Tags, Cluster: e.Cluster, Resolver: strategy}) + if previous := upsertResolved(c, cacheEntry(*e, ip, strategy)); previous != "" { + changed = append(changed, fmt.Sprintf("%s: %s → %s", e.Target(), previous, ip)) + } written++ } @@ -924,6 +1001,14 @@ func cacheRefreshCmd() *cobra.Command { } } fmt.Printf("%d entries written to cache.\n", written) + if len(changed) > 0 { + sort.Strings(changed) + fmt.Printf("%d hosts changed their address:\n", len(changed)) + for _, line := range changed { + fmt.Printf(" %s\n", line) + } + fmt.Println("Remove outdated host keys with: netssh known-hosts reset HOST") + } return nil }, } diff --git a/cmd/netssh/operations.go b/cmd/netssh/operations.go index ca891ad..da700f2 100644 --- a/cmd/netssh/operations.go +++ b/cmd/netssh/operations.go @@ -60,6 +60,12 @@ func doctorCmd() *cobra.Command { report("program "+p, err) } report("cache", newCache(cfg).Load()) + for _, instance := range instanceConfigs(cfg) { + if config.IsKeyringToken(instance.Token) { + _, err := resolveToken(*instance) + report("keyring token "+instance.KeyringAccount(), err) + } + } if online { sources := cfg.NetBoxes if len(sources) == 0 && cfg.NetBox.URL != "" { @@ -67,7 +73,8 @@ func doctorCmd() *cobra.Command { } for _, s := range sources { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - _, err := netbox.NewClient(s.URL, s.Token, s.TokenVersion).Search(ctx, "", netbox.SearchOptions{}) + token, version := clientToken(s) + _, err := netbox.NewClient(s.URL, token, version).Search(ctx, "", netbox.SearchOptions{}) cancel() report("NetBox "+s.Name, err) } @@ -109,6 +116,7 @@ func explainCmd() *cobra.Command { } return json.NewEncoder(cmd.OutOrStdout()).Encode(map[string]any{ "target": h.Name, "instance": selectors.Instance, "address": h.IP, "source": h.Origin, "cached_at": h.CachedAt, + "status": h.Status, "site": h.Site.Name, "role": h.Role.Name, "platform": h.Platform.Name, "tenant": h.Tenant.Name, "netbox_id": h.ID, "location": h.LocationPath(), "resolver_order": cfg.Resolver.Strategies, "resolver_used": strategy, "matching_rules": rules, "user": user, "port": port, "ssh": opts, }) }} diff --git a/cmd/netssh/supervise.go b/cmd/netssh/supervise.go index 15a65e7..ff14182 100644 --- a/cmd/netssh/supervise.go +++ b/cmd/netssh/supervise.go @@ -19,7 +19,7 @@ import ( func commandHost(h resolvedHost) config.SSHHost { instance, name := netbox.SplitTarget(h.Name) - return config.SSHHost{Instance: instance, Name: name, Kind: h.Kind, Tags: h.Tags} + return config.SSHHost{Instance: instance, Name: name, Kind: h.Kind, Tags: h.Tags, Status: h.Status, Site: h.Site, Role: h.Role, Platform: h.Platform, Tenant: h.Tenant} } func confirmAction(message string, yes bool) error { if yes { diff --git a/cmd/netssh/token.go b/cmd/netssh/token.go new file mode 100644 index 0000000..a563dc4 --- /dev/null +++ b/cmd/netssh/token.go @@ -0,0 +1,260 @@ +package main + +import ( + "bufio" + "errors" + "fmt" + "io" + "os" + "strings" + "sync" + "text/tabwriter" + + "github.com/charmbracelet/x/term" + "github.com/spf13/cobra" + + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config" + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/netbox" + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/secret" + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/setup" +) + +var ( + tokenMu sync.Mutex + tokenCache = map[string]string{} +) + +// resolveToken returns the API token of an instance: the configured value, +// or the keyring entry when the value is "keyring". Lookups are cached per +// process, since each one can be a D-Bus round trip. +func resolveToken(n config.NetBoxConfig) (string, error) { + if !config.IsKeyringToken(n.Token) { + return n.Token, nil + } + account := n.KeyringAccount() + tokenMu.Lock() + defer tokenMu.Unlock() + if token, ok := tokenCache[account]; ok { + return token, nil + } + token, err := secret.Get(account) + if err != nil { + return "", err + } + tokenCache[account] = token + return token, nil +} + +// clientToken resolves a token for a client; a failure is reported once and +// leaves the token empty, so NetBox answers with a clear authentication error. +func clientToken(n config.NetBoxConfig) (string, int) { + token, err := resolveToken(n) + if err != nil { + label := n.Name + if label == "" { + label = "netbox" + } + fmt.Fprintf(os.Stderr, "netssh: %s: %v\n", label, err) + return "", n.TokenVersion + } + version := n.TokenVersion + if config.IsKeyringToken(n.Token) { + version = netbox.TokenVersion(token) + } + return token, version +} + +// instanceConfigs returns the configured NetBox instances, including the +// legacy unnamed one. +func instanceConfigs(cfg *config.Config) []*config.NetBoxConfig { + var out []*config.NetBoxConfig + for i := range cfg.NetBoxes { + out = append(out, &cfg.NetBoxes[i]) + } + if len(out) == 0 && cfg.NetBox.URL != "" { + out = append(out, &cfg.NetBox) + } + return out +} + +// selectInstance finds the instance named in args, or the only one. +func selectInstance(cfg *config.Config, args []string) (*config.NetBoxConfig, error) { + instances := instanceConfigs(cfg) + if len(instances) == 0 { + return nil, errors.New("no NetBox is configured (netssh configure)") + } + if len(args) == 0 { + if len(instances) == 1 { + return instances[0], nil + } + return nil, fmt.Errorf("name the NetBox instance: %s", strings.Join(instanceNames(cfg), ", ")) + } + for _, instance := range instances { + if strings.EqualFold(instance.Name, args[0]) || (instance.Name == "" && strings.EqualFold(args[0], "default")) { + return instance, nil + } + } + return nil, fmt.Errorf("unknown NetBox instance %q", args[0]) +} + +// readToken reads a token without echo from a terminal, or a line from stdin. +func readToken(label string, stdin io.Reader) (string, error) { + if f, ok := stdin.(*os.File); ok && term.IsTerminal(f.Fd()) { + fmt.Fprintf(os.Stderr, "NetBox API token for %s: ", label) + data, err := term.ReadPassword(f.Fd()) + fmt.Fprintln(os.Stderr) + if err != nil { + return "", err + } + return strings.TrimSpace(string(data)), nil + } + line, err := bufio.NewReader(stdin).ReadString('\n') + if err != nil && err != io.EOF { + return "", err + } + return strings.TrimSpace(line), nil +} + +// saveConfig is replaced in tests. +var saveConfig = setup.Save + +func tokenCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "token", + Short: "Keep NetBox API tokens in the system keyring", + Long: `Store NetBox API tokens in the system keyring (Secret Service / GNOME Keyring / +KWallet on Linux, Keychain on macOS) instead of the config file. The config +then contains token: keyring, and netssh reads the token from the keyring. + +NETSSH_NETBOX_TOKEN still overrides the token of the unnamed instance.`, + } + cmd.AddCommand(tokenSetCmd(), tokenMigrateCmd(), tokenDeleteCmd(), tokenStatusCmd()) + return cmd +} + +func tokenSetCmd() *cobra.Command { + return &cobra.Command{ + Use: "set [INSTANCE]", + Short: "Store a token in the keyring and switch the config to token: keyring", + Long: "Read the token without echo (or from stdin) and store it in the keyring.\nExample for scripts: echo \"$TOKEN\" | netssh token set prod", + Args: cobra.MaximumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + cfg, err := config.Load() + if err != nil { + return err + } + instance, err := selectInstance(cfg, args) + if err != nil { + return err + } + token, err := readToken(instance.KeyringAccount(), cmd.InOrStdin()) + if err != nil { + return err + } + if token == "" || config.IsKeyringToken(token) { + return errors.New("no token given") + } + if err := secret.Set(instance.KeyringAccount(), token); err != nil { + return err + } + instance.Token, instance.TokenVersion = config.KeyringToken, netbox.TokenVersion(token) + if err := saveConfig(*cfg); err != nil { + return err + } + fmt.Fprintf(cmd.OutOrStdout(), "Token for %s stored in the system keyring.\n", instance.KeyringAccount()) + return nil + }, + } +} + +func tokenMigrateCmd() *cobra.Command { + return &cobra.Command{ + Use: "migrate", + Short: "Move all tokens from the config file into the keyring", + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + cfg, err := config.Load() + if err != nil { + return err + } + moved := 0 + for _, instance := range instanceConfigs(cfg) { + if instance.Token == "" || config.IsKeyringToken(instance.Token) { + continue + } + if err := secret.Set(instance.KeyringAccount(), instance.Token); err != nil { + return err + } + instance.TokenVersion = netbox.TokenVersion(instance.Token) + instance.Token = config.KeyringToken + moved++ + fmt.Fprintf(cmd.OutOrStdout(), "%s: moved to the keyring\n", instance.KeyringAccount()) + } + if moved == 0 { + fmt.Fprintln(cmd.OutOrStdout(), "No tokens in the config file.") + return nil + } + return saveConfig(*cfg) + }, + } +} + +func tokenDeleteCmd() *cobra.Command { + return &cobra.Command{ + Use: "delete [INSTANCE]", + Short: "Remove a token from the keyring", + Args: cobra.MaximumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + cfg, err := config.Load() + if err != nil { + return err + } + instance, err := selectInstance(cfg, args) + if err != nil { + return err + } + if err := secret.Delete(instance.KeyringAccount()); err != nil { + return err + } + fmt.Fprintf(cmd.OutOrStdout(), "Token for %s removed from the keyring.\n", instance.KeyringAccount()) + if config.IsKeyringToken(instance.Token) { + fmt.Fprintln(cmd.OutOrStdout(), "The config still says token: keyring; store a new one with netssh token set.") + } + return nil + }, + } +} + +func tokenStatusCmd() *cobra.Command { + return &cobra.Command{ + Use: "status", + Short: "Show where each instance's token comes from", + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + cfg, err := config.Load() + if err != nil { + return err + } + w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 2, ' ', 0) + fmt.Fprintln(w, "INSTANCE\tTOKEN") + for _, instance := range instanceConfigs(cfg) { + fmt.Fprintf(w, "%s\t%s\n", instance.KeyringAccount(), tokenSource(*instance)) + } + return w.Flush() + }, + } +} + +func tokenSource(n config.NetBoxConfig) string { + switch { + case n.Token == "": + return "missing" + case config.IsKeyringToken(n.Token): + if _, err := resolveToken(n); err != nil { + return "keyring (not readable: " + err.Error() + ")" + } + return "keyring" + default: + return "config file (plain text; move it with netssh token migrate)" + } +} diff --git a/cmd/netssh/tui_extras.go b/cmd/netssh/tui_extras.go new file mode 100644 index 0000000..aca988f --- /dev/null +++ b/cmd/netssh/tui_extras.go @@ -0,0 +1,87 @@ +package main + +import ( + "context" + "fmt" + "time" + + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config" + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/netbox" + internalssh "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/ssh" + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/tui" +) + +// configureTUIExtras wires reachability, the detail view and opening NetBox +// pages into the TUI. known enriches a listed host from the cache. +func configureTUIExtras(m *tui.Model, cfg *config.Config, client *netbox.Client, known func(name, kind string, tags []string) resolvedHost) { + m.SetColumns(cfg.TUI.EffectiveColumns()) + if cfg.TUI.ReachabilityEnabled() { + m.SetReachability(cfg.TUI.ReachabilityTimeoutDuration(), func(name, kind string, tags []string) (int, bool) { + h := known(name, kind, tags) + if usesJumpHost(hostOptions(cfg, h), internalssh.WrapperFlags{}) { + return 0, false + } + _, port := hostDefaults(cfg, h) + if port <= 0 { + port = 22 + } + return port, true + }) + } + lookup := func(ctx context.Context, name, kind string, id int) (netbox.HostEntry, resolvedHost, error) { + h := known(name, kind, nil) + if kind == config.StaticKind { + return netbox.HostEntry{}, h, fmt.Errorf("%s is a static host and has no NetBox record", name) + } + if client == nil { + return netbox.HostEntry{}, h, fmt.Errorf("no NetBox is configured") + } + if id <= 0 { + id = h.ID + } + entry, _, h, err := netboxEntry(ctx, client, name, kind, id, h) + return entry, h, err + } + m.SetOpener(func(name, kind string, id int) error { + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) + defer cancel() + entry, _, err := lookup(ctx, name, kind, id) + if err != nil { + return err + } + url, err := client.WebURL(entry) + if err != nil { + return err + } + return openBrowser(url) + }) + m.SetDetails(func(name, kind string, id int) (string, error) { + if kind == config.StaticKind { + h := known(name, kind, nil) + user, port := hostDefaults(cfg, h) + text := fmt.Sprintf("%s (static host from hosts:)\n Address %s\n", name, h.IP) + if user != "" { + text += " User " + user + "\n" + } + if port > 0 { + text += fmt.Sprintf(" Port %d\n", port) + } + return text, nil + } + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + defer cancel() + entry, h, err := lookup(ctx, name, kind, id) + if err != nil { + return "", err + } + details, err := client.GetDetails(ctx, entry) + if err != nil && details.Name == "" { + return "", err + } + text := formatDetails(details, h) + if err != nil { + text += "\nwarning: " + err.Error() + "\n" + } + return text, nil + }) +} diff --git a/go.mod b/go.mod index 2de1aba..690e119 100644 --- a/go.mod +++ b/go.mod @@ -7,12 +7,16 @@ require ( charm.land/bubbletea/v2 v2.1.1 charm.land/huh/v2 v2.0.3 charm.land/lipgloss/v2 v2.1.1 + github.com/charmbracelet/x/term v0.2.2 github.com/spf13/cobra v1.10.2 github.com/spf13/viper v1.21.0 + github.com/zalando/go-keyring v0.2.6 + go.yaml.in/yaml/v3 v3.0.6 golang.org/x/sys v0.50.0 ) require ( + al.essio.dev/pkg/shellescape v1.5.1 // indirect github.com/atotto/clipboard v0.1.4 // indirect github.com/catppuccin/go v0.3.0 // indirect github.com/charmbracelet/colorprofile v0.4.3 // indirect @@ -21,15 +25,16 @@ require ( github.com/charmbracelet/x/exp/golden v0.1.0 // indirect github.com/charmbracelet/x/exp/ordered v0.1.0 // indirect github.com/charmbracelet/x/exp/strings v0.1.0 // indirect - github.com/charmbracelet/x/term v0.2.2 // indirect github.com/charmbracelet/x/termios v0.1.1 // indirect github.com/charmbracelet/x/windows v0.2.2 // indirect github.com/charmbracelet/x/xpty v0.1.4 // indirect github.com/clipperhouse/displaywidth v0.11.0 // indirect github.com/clipperhouse/uax29/v2 v2.7.0 // indirect + github.com/danieljoos/wincred v1.2.2 // indirect github.com/dustin/go-humanize v1.1.0 // indirect github.com/fsnotify/fsnotify v1.10.1 // indirect github.com/go-viper/mapstructure/v2 v2.5.0 // indirect + github.com/godbus/dbus/v5 v5.1.0 // indirect github.com/google/go-cmp v0.7.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/lucasb-eyer/go-colorful v1.4.1 // indirect @@ -47,7 +52,6 @@ require ( github.com/stretchr/testify v1.12.1 // indirect github.com/subosito/gotenv v1.6.0 // indirect github.com/xo/terminfo v1.3.1 // indirect - go.yaml.in/yaml/v3 v3.0.6 // indirect golang.org/x/sync v0.25.0 // indirect golang.org/x/text v0.44.0 // indirect ) diff --git a/go.sum b/go.sum index 5a9b607..866d079 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +al.essio.dev/pkg/shellescape v1.5.1 h1:86HrALUujYS/h+GtqoB26SBEdkWfmMI6FubjXlsXyho= +al.essio.dev/pkg/shellescape v1.5.1/go.mod h1:6sIqp7X2P6mThCQ7twERpZTuigpr6KbZWtls1U8I890= charm.land/bubbles/v2 v2.2.1 h1:Fq1+qm5hV6GkvzLQDhCBpXXE5tLgvh1PRriCLwSvIQU= charm.land/bubbles/v2 v2.2.1/go.mod h1:wdMgn+sje1KNXdwFizIWjbf328fIUBxqEmJ/vYPo8yc= charm.land/bubbletea/v2 v2.1.1 h1:S1S9l37pCZGrj1JexBcr9x4Nr3MVeM42InqCYM/tDpU= @@ -43,6 +45,8 @@ github.com/clipperhouse/uax29/v2 v2.7.0/go.mod h1:EFJ2TJMRUaplDxHKj1qAEhCtQPW2tJ github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s= github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE= +github.com/danieljoos/wincred v1.2.2 h1:774zMFJrqaeYCK2W57BgAem/MLi6mtSE47MB6BOJ0i0= +github.com/danieljoos/wincred v1.2.2/go.mod h1:w7w4Utbrz8lqeMbDAK0lkNJUv5sAOkFi7nd/ogr0Uh8= github.com/dustin/go-humanize v1.1.0 h1:dbKTrvD0klcbBV/h4AWJdMuZogJACoMlvWIWZ5b2xWg= github.com/dustin/go-humanize v1.1.0/go.mod h1:hc1CvRkJMsgxqjmjMQF3QNRAZBwY8AXBAzKYoSX9sFI= github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= @@ -51,8 +55,12 @@ github.com/fsnotify/fsnotify v1.10.1 h1:b0/UzAf9yR5rhf3RPm9gf3ehBPpf0oZKIjtpKrx5 github.com/fsnotify/fsnotify v1.10.1/go.mod h1:TLheqan6HD6GBK6PrDWyDPBaEV8LspOxvPSjC+bVfgo= github.com/go-viper/mapstructure/v2 v2.5.0 h1:vM5IJoUAy3d7zRSVtIwQgBj7BiWtMPfmPEgAXnvj1Ro= github.com/go-viper/mapstructure/v2 v2.5.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= +github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk= +github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= @@ -91,12 +99,16 @@ github.com/spf13/pflag v1.1.0 h1:6W6MbVUrnMb8PEhjkqwsVLY7ryuzRfFHF4mQd7L94ag= github.com/spf13/pflag v1.1.0/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/viper v1.21.0 h1:x5S+0EU27Lbphp4UKm1C+1oQO+rKx36vfCoaVebLFSU= github.com/spf13/viper v1.21.0/go.mod h1:P0lhsswPGWD/1lZJ9ny3fYnVqxiegrlNrEmgLjbTCAY= +github.com/stretchr/objx v0.5.3 h1:jmXUvGomnU1o3W/V5h2VEradbpJDwGrzugQQvL0POH4= +github.com/stretchr/objx v0.5.3/go.mod h1:rDQraq+vQZU7Fde9LOZLr8Tax6zZvy4kuNKF+QYS+U0= github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE= github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg= github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= github.com/xo/terminfo v1.3.1 h1:4ZwPCuu4PEFw0PkNp3Hv/x3gqvgLCzYhurszsj101cM= github.com/xo/terminfo v1.3.1/go.mod h1:lGzkSo8Fe7IRh/w+Gqz7n5mDog4FVXCj3gy/DYTBqio= +github.com/zalando/go-keyring v0.2.6 h1:r7Yc3+H+Ux0+M72zacZoItR3UDxeWfKTcabvkI8ua9s= +github.com/zalando/go-keyring v0.2.6/go.mod h1:2TCrxYrbUNYfNS/Kgy/LSrkSQzZ5UPVH85RwfczwvcI= go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= go.yaml.in/yaml/v3 v3.0.6 h1:zcDfvjXEqTQmpjniUBLc91xYJMB2c4QOFxEF/LCEckM= go.yaml.in/yaml/v3 v3.0.6/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg= diff --git a/internal/cache/cache.go b/internal/cache/cache.go index 5d40839..4beb5ee 100644 --- a/internal/cache/cache.go +++ b/internal/cache/cache.go @@ -17,14 +17,26 @@ import ( ) type Entry struct { - Resolver string `json:"resolver,omitempty"` - Name string `json:"name"` - IP string `json:"ip"` - Kind string `json:"kind"` - Tags []string `json:"tags,omitempty"` - Cluster string `json:"cluster,omitempty"` - CachedAt time.Time `json:"cached_at"` - LastUsed time.Time `json:"last_used,omitempty"` + Resolver string `json:"resolver,omitempty"` + Name string `json:"name"` + IP string `json:"ip"` + Kind string `json:"kind"` + Tags []string `json:"tags,omitempty"` + Cluster string `json:"cluster,omitempty"` + // ID, Status, Site, Role, Platform and Tenant come from NetBox; entries + // written by older versions lack them until the next refresh. + ID int `json:"id,omitempty"` + Status string `json:"status,omitempty"` + Site *config.Ref `json:"site,omitempty"` + Role *config.Ref `json:"role,omitempty"` + Platform *config.Ref `json:"platform,omitempty"` + Tenant *config.Ref `json:"tenant,omitempty"` + // Location is the chain of nested locations below the site, outermost first. + Location []config.Ref `json:"location,omitempty"` + Rack string `json:"rack,omitempty"` + Position string `json:"position,omitempty"` + CachedAt time.Time `json:"cached_at"` + LastUsed time.Time `json:"last_used,omitempty"` // Static marks a host from the config's hosts: list. It never expires; // it is stored only to remember LastUsed for the recently-used list. Static bool `json:"static,omitempty"` @@ -427,3 +439,19 @@ func (c *Cache) All() []Entry { } return out } + +// RefPtr returns nil for an empty reference, keeping the cache file small. +func RefPtr(r config.Ref) *config.Ref { + if r == (config.Ref{}) { + return nil + } + return &r +} + +// RefOf returns the referenced value, or an empty Ref. +func RefOf(r *config.Ref) config.Ref { + if r == nil { + return config.Ref{} + } + return *r +} diff --git a/internal/config/config.go b/internal/config/config.go index ba830eb..f5c8da9 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -6,6 +6,7 @@ import ( "path/filepath" "regexp" "strings" + "time" "github.com/spf13/viper" ) @@ -42,6 +43,11 @@ type NetBoxConfig struct { } type ResolverConfig struct { + // Fallback tries the addresses of the other strategies when the chosen + // address does not accept TCP connections on the SSH port. + Fallback bool `mapstructure:"fallback"` + // FallbackTimeout is the per-address connect timeout in milliseconds; 0 = 1000. + FallbackTimeout int `mapstructure:"fallback_timeout"` Strategies []string `mapstructure:"strategies"` ManagementSubnets []string `mapstructure:"management_subnets"` InterfaceName string `mapstructure:"interface_name"` @@ -57,8 +63,11 @@ type CacheConfig struct { type SSHConfig struct { DefaultUser string `mapstructure:"default_user"` DefaultPort int `mapstructure:"default_port"` - SSHOptions `mapstructure:",squash"` - Rules []SSHRule `mapstructure:"rules"` + // WarnStatus lists NetBox status values that ask before connecting; + // nil means DefaultWarnStatus, an empty list disables the question. + WarnStatus []string `mapstructure:"warn_status"` + SSHOptions `mapstructure:",squash"` + Rules []SSHRule `mapstructure:"rules"` } type CompletionConfig struct { @@ -100,6 +109,51 @@ func (s ShortcutsConfig) Validate() error { return nil } +// DefaultWarnStatus are the NetBox status values that ask before connecting. +var DefaultWarnStatus = []string{"offline", "planned", "staged", "failed", "inventory", "decommissioning"} + +// KeyringToken as a token value reads the token from the system keyring. +const KeyringToken = "keyring" + +// IsKeyringToken reports whether token refers to the system keyring. +func IsKeyringToken(token string) bool { + return strings.EqualFold(strings.TrimSpace(token), KeyringToken) +} + +// KeyringAccount is the keyring entry of a NetBox instance; the unnamed +// single-instance configuration uses "default". +func (n NetBoxConfig) KeyringAccount() string { + if n.Name == "" { + return "default" + } + return strings.ToLower(n.Name) +} + +// WarnsStatus reports whether status is configured to ask before connecting. +func (s SSHConfig) WarnsStatus(status string) bool { + if status == "" { + return false + } + list := s.WarnStatus + if list == nil { + list = DefaultWarnStatus + } + for _, warn := range list { + if strings.EqualFold(strings.TrimSpace(warn), status) { + return true + } + } + return false +} + +// FallbackTimeoutDuration returns resolver.fallback_timeout, 1s by default. +func (r ResolverConfig) FallbackTimeoutDuration() time.Duration { + if r.FallbackTimeout <= 0 { + return time.Second + } + return time.Duration(r.FallbackTimeout) * time.Millisecond +} + // Path returns the canonical config file path. func Path() string { configDir, err := os.UserConfigDir() @@ -131,7 +185,7 @@ func Load() (*Config, error) { v.SetEnvPrefix("NETSSH") v.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) v.AutomaticEnv() - for _, key := range []string{"netbox.url", "netbox.token", "netbox.token_version", "resolver.strategies", "resolver.management_subnets", "resolver.interface_name", "resolver.hostname_domain", "cache.ttl", "cache.path", "ssh.default_user", "ssh.default_port", "shortcuts.domains", "shortcuts.strip_hyphens", "shortcuts.show", "shell.alias", "ssh.proxy_jump", "ssh.pre_connect", "ssh.post_disconnect", "tui.history_limit", "activity.enabled", "activity.retention_days", "activity.max_entries", "ssh.host_key_checking", "ssh.target", "ssh.address_family", "completion.max_results", "multi.multiplexer", "multi.panes_per_window"} { + for _, key := range []string{"netbox.url", "netbox.token", "netbox.token_version", "resolver.strategies", "resolver.management_subnets", "resolver.interface_name", "resolver.hostname_domain", "cache.ttl", "cache.path", "ssh.default_user", "ssh.default_port", "shortcuts.domains", "shortcuts.strip_hyphens", "shortcuts.show", "shell.alias", "ssh.proxy_jump", "ssh.pre_connect", "ssh.post_disconnect", "tui.history_limit", "activity.enabled", "activity.retention_days", "activity.max_entries", "ssh.host_key_checking", "ssh.target", "ssh.address_family", "completion.max_results", "multi.multiplexer", "multi.panes_per_window", "ssh.warn_status", "tui.columns", "tui.reachability_timeout", "resolver.fallback", "resolver.fallback_timeout"} { if err := v.BindEnv(key); err != nil { return nil, err } @@ -147,6 +201,14 @@ func Load() (*Config, error) { if err := v.Unmarshal(&cfg); err != nil { return nil, fmt.Errorf("parsing config: %w", err) } + // "columns: []" shows only the hostname; only a missing key means the default. + if cfg.TUI.Columns == nil && v.IsSet("tui.columns") { + cfg.TUI.Columns = []string{} + } + // "warn_status: []" disables the question; only a missing key means the default. + if cfg.SSH.WarnStatus == nil && v.IsSet("ssh.warn_status") { + cfg.SSH.WarnStatus = []string{} + } // Legacy environment variables must not invent an additional unnamed source // when a file explicitly configures named instances (including wizard migration). if len(cfg.NetBoxes) > 0 && !v.InConfig("netbox.url") { @@ -157,7 +219,7 @@ func Load() (*Config, error) { return nil, err } - if cfg.NetBox.TokenVersion == 0 && cfg.NetBox.Token != "" { + if cfg.NetBox.TokenVersion == 0 && cfg.NetBox.Token != "" && !IsKeyringToken(cfg.NetBox.Token) { if strings.HasPrefix(cfg.NetBox.Token, "nbt_") { cfg.NetBox.TokenVersion = 2 } else { @@ -189,7 +251,7 @@ func Load() (*Config, error) { for i := range cfg.NetBoxes { source := &cfg.NetBoxes[i] source.Name = strings.ToLower(source.Name) - if source.TokenVersion == 0 && source.Token != "" { + if source.TokenVersion == 0 && source.Token != "" && !IsKeyringToken(source.Token) { source.TokenVersion = 1 if strings.HasPrefix(source.Token, "nbt_") { source.TokenVersion = 2 diff --git a/internal/config/insights_test.go b/internal/config/insights_test.go new file mode 100644 index 0000000..541c6a9 --- /dev/null +++ b/internal/config/insights_test.go @@ -0,0 +1,108 @@ +package config + +import ( + "os" + "strings" + "testing" +) + +func TestSSHRuleMatchesNetBoxAttributes(t *testing.T) { + host := SSHHost{Name: "sw1", Kind: "device", Status: "active", + Site: Ref{Name: "Falkenstein 1", Slug: "fsn1"}, Role: Ref{Name: "Core Switch", Slug: "core-switch"}, + Platform: Ref{Name: "Juniper Junos", Slug: "junos"}, Tenant: Ref{Name: "ACME"}} + for _, tc := range []struct { + match SSHMatch + want bool + }{ + {SSHMatch{Platform: "junos"}, true}, + {SSHMatch{Platform: "juniper junos"}, true}, + {SSHMatch{Platform: "eos"}, false}, + {SSHMatch{Site: "fsn1", Role: "core-switch"}, true}, + {SSHMatch{Role: "Core Switch"}, true}, + {SSHMatch{Site: "falkenstein-1"}, true}, + {SSHMatch{Tenant: "acme"}, true}, + {SSHMatch{Status: "ACTIVE"}, true}, + {SSHMatch{Status: "offline"}, false}, + {SSHMatch{Platform: "junos", Kind: "vm"}, false}, + } { + if got := tc.match.matches(host); got != tc.want { + t.Errorf("%+v: got %v, want %v", tc.match, got, tc.want) + } + } + if err := (SSHMatch{Platform: "junos"}).Validate("rule"); err != nil { + t.Fatalf("platform-only match rejected: %v", err) + } +} + +func TestSSHOptionsForPlatformRule(t *testing.T) { + cfg := &Config{SSH: SSHConfig{Rules: []SSHRule{{Match: SSHMatch{Platform: "junos"}, SSHOptions: SSHOptions{Options: map[string]string{"KexAlgorithms": "+diffie-hellman-group14-sha1"}}}}}} + opts := cfg.SSHOptionsFor(SSHHost{Name: "sw1", Kind: "device", Platform: Ref{Slug: "junos"}}) + if opts.Options["kexalgorithms"] != "+diffie-hellman-group14-sha1" { + t.Fatalf("platform rule not applied: %+v", opts.Options) + } + if opts := cfg.SSHOptionsFor(SSHHost{Name: "web", Kind: "vm"}); len(opts.Options) != 0 { + t.Fatalf("rule applied to other platform: %+v", opts.Options) + } +} + +func TestWarnStatusDefaultsAndOverrides(t *testing.T) { + cfg := loadFromDir(t, writeConfig(t, "netbox:\n url: https://nb\n")) + if cfg.SSH.WarnStatus != nil || !cfg.SSH.WarnsStatus("offline") || cfg.SSH.WarnsStatus("active") || cfg.SSH.WarnsStatus("") { + t.Fatalf("default warn_status wrong: %#v", cfg.SSH.WarnStatus) + } + cfg = loadFromDir(t, writeConfig(t, "netbox:\n url: https://nb\nssh:\n warn_status: []\n")) + if cfg.SSH.WarnStatus == nil || cfg.SSH.WarnsStatus("offline") { + t.Fatalf("empty warn_status should disable the question: %#v", cfg.SSH.WarnStatus) + } + cfg = loadFromDir(t, writeConfig(t, "netbox:\n url: https://nb\nssh:\n warn_status: [planned]\n")) + if !cfg.SSH.WarnsStatus("Planned") || cfg.SSH.WarnsStatus("offline") { + t.Fatalf("custom warn_status wrong: %#v", cfg.SSH.WarnStatus) + } +} + +func TestReachabilityAndFallbackDefaults(t *testing.T) { + cfg := loadFromDir(t, writeConfig(t, "netbox:\n url: https://nb\n")) + if cfg.TUI.ReachabilityEnabled() || cfg.TUI.ReachabilityTimeoutDuration().Milliseconds() != 300 { + t.Fatalf("reachability must be opt-in: %+v", cfg.TUI) + } + if strings.Join(cfg.TUI.EffectiveColumns(), ",") != "alias,ip,source,status" { + t.Fatalf("default columns: %v", cfg.TUI.EffectiveColumns()) + } + if cfg.Resolver.Fallback || cfg.Resolver.FallbackTimeoutDuration().Milliseconds() != 1000 { + t.Fatalf("fallback defaults wrong: %+v", cfg.Resolver) + } + cfg = loadFromDir(t, writeConfig(t, "netbox:\n url: https://nb\ntui:\n columns: [Reachability, ip, platform]\n reachability_timeout: 800\nresolver:\n fallback: true\n fallback_timeout: 250\n")) + if !cfg.TUI.ReachabilityEnabled() || cfg.TUI.ReachabilityTimeoutDuration().Milliseconds() != 800 || strings.Join(cfg.TUI.Columns, ",") != "reachability,ip,platform" { + t.Fatalf("reachability settings ignored: %+v", cfg.TUI) + } + if !cfg.Resolver.Fallback || cfg.Resolver.FallbackTimeoutDuration().Milliseconds() != 250 { + t.Fatalf("fallback settings ignored: %+v", cfg.Resolver) + } +} + +func TestKeyringTokenKeepsVersionUndetected(t *testing.T) { + cfg := loadFromDir(t, writeConfig(t, "netbox:\n url: https://nb\n token: keyring\nnetboxes: []\n")) + if !IsKeyringToken(cfg.NetBox.Token) || cfg.NetBox.TokenVersion != 0 { + t.Fatalf("keyring token treated as literal: %+v", cfg.NetBox) + } + if cfg.NetBox.KeyringAccount() != "default" || (NetBoxConfig{Name: "Prod"}).KeyringAccount() != "prod" { + t.Fatal("unexpected keyring account names") + } +} + +func TestTUIColumnsValidation(t *testing.T) { + cfg := loadFromDir(t, writeConfig(t, "netbox:\n url: https://nb\ntui:\n columns: []\n")) + if cfg.TUI.Columns == nil || len(cfg.TUI.EffectiveColumns()) != 0 { + t.Fatalf("empty columns should show only the hostname: %#v", cfg.TUI.Columns) + } + for _, bad := range []string{"[ip, colour]", "[ip, ip]"} { + dir := writeConfig(t, "netbox:\n url: https://nb\ntui:\n columns: "+bad+"\n") + orig := os.Getenv("XDG_CONFIG_HOME") + os.Setenv("XDG_CONFIG_HOME", dir) + _, err := Load() + os.Setenv("XDG_CONFIG_HOME", orig) + if err == nil || !strings.Contains(err.Error(), "tui.columns") { + t.Errorf("%s accepted: %v", bad, err) + } + } +} diff --git a/internal/config/operations.go b/internal/config/operations.go index c2e1462..1449ea9 100644 --- a/internal/config/operations.go +++ b/internal/config/operations.go @@ -3,11 +3,51 @@ package config import ( "fmt" "strings" + "time" ) type TUIConfig struct { HistoryLimit int `mapstructure:"history_limit"` + // Columns selects what the TUI shows next to each hostname, in order; + // nil means DefaultTUIColumns. "reachability" enables the TCP check. + Columns []string `mapstructure:"columns"` + // ReachabilityTimeout is the probe timeout in milliseconds; 0 = 300. + ReachabilityTimeout int `mapstructure:"reachability_timeout"` } + +// TUIColumns are the values tui.columns accepts. +var TUIColumns = []string{"alias", "ip", "kind", "source", "status", "site", "location", "role", "platform", "tenant", "cluster", "tags", "reachability"} + +// DefaultTUIColumns is the layout without tui.columns. The TCP reachability +// check is opt-in, since it opens connections to every listed host. +var DefaultTUIColumns = []string{"alias", "ip", "source", "status"} + +// EffectiveColumns returns tui.columns, or the default layout. +func (t TUIConfig) EffectiveColumns() []string { + if t.Columns == nil { + return DefaultTUIColumns + } + return t.Columns +} + +// ReachabilityEnabled reports whether the TUI probes listed hosts. +func (t TUIConfig) ReachabilityEnabled() bool { + for _, column := range t.EffectiveColumns() { + if column == "reachability" { + return true + } + } + return false +} + +// ReachabilityTimeoutDuration returns tui.reachability_timeout, 300ms by default. +func (t TUIConfig) ReachabilityTimeoutDuration() time.Duration { + if t.ReachabilityTimeout <= 0 { + return 300 * time.Millisecond + } + return time.Duration(t.ReachabilityTimeout) * time.Millisecond +} + type ActivityConfig struct { Enabled bool `mapstructure:"enabled"` RetentionDays int `mapstructure:"retention_days"` @@ -21,6 +61,33 @@ func (c *Config) ValidateOperations() error { if c.Activity.RetentionDays < 0 || c.Activity.MaxEntries < 0 { return fmt.Errorf("activity retention_days and max_entries must be >= 0") } + seen := map[string]bool{} + for i, column := range c.TUI.Columns { + column = strings.ToLower(strings.TrimSpace(column)) + c.TUI.Columns[i] = column + known := false + for _, valid := range TUIColumns { + known = known || column == valid + } + if !known { + return fmt.Errorf("tui.columns: unknown column %q; use %s", column, strings.Join(TUIColumns, ", ")) + } + if seen[column] { + return fmt.Errorf("tui.columns: %q is listed twice", column) + } + seen[column] = true + } + if c.TUI.ReachabilityTimeout < 0 || c.TUI.ReachabilityTimeout > 10000 { + return fmt.Errorf("tui.reachability_timeout must be between 0 and 10000 milliseconds") + } + if c.Resolver.FallbackTimeout < 0 || c.Resolver.FallbackTimeout > 30000 { + return fmt.Errorf("resolver.fallback_timeout must be between 0 and 30000 milliseconds") + } + for _, status := range c.SSH.WarnStatus { + if strings.TrimSpace(status) == "" || strings.ContainsAny(status, " \t\n") { + return fmt.Errorf("ssh.warn_status: invalid status %q", status) + } + } return nil } func (m SSHMatch) Matches(host SSHHost) bool { return m == (SSHMatch{}) || m.matches(host) } diff --git a/internal/config/ssh.go b/internal/config/ssh.go index c0fdded..8286096 100644 --- a/internal/config/ssh.go +++ b/internal/config/ssh.go @@ -94,6 +94,57 @@ type SSHMatch struct { Instance string `mapstructure:"instance"` Kind string `mapstructure:"kind"` Name string `mapstructure:"name"` // shell-style glob, e.g. "*.dmz.example.com" + // Site, Role, Platform and Tenant match the NetBox name or slug. + Site string `mapstructure:"site"` + Role string `mapstructure:"role"` + Platform string `mapstructure:"platform"` + Tenant string `mapstructure:"tenant"` + // Status matches the NetBox status value, e.g. active or offline. + Status string `mapstructure:"status"` +} + +// Ref is a NetBox object referenced by a host, such as its site or platform. +type Ref struct { + Name string `json:"name,omitempty"` + Slug string `json:"slug,omitempty"` +} + +// Matches compares want with the name or slug, ignoring case. +func (r Ref) Matches(want string) bool { + return want != "" && (strings.EqualFold(r.Name, want) || strings.EqualFold(r.Slug, want) || strings.EqualFold(tagSlug(r.Name), want)) +} + +// Key returns the slug, or the name when NetBox sent no slug. +func (r Ref) Key() string { + if r.Slug != "" { + return r.Slug + } + return r.Name +} + +// LocationPath renders a host's place as "site › location › … › rack › U20", +// leaving out levels that are not set. +func LocationPath(site Ref, locations []Ref, rack, position string) string { + var parts []string + if site.Name != "" { + parts = append(parts, site.Name) + } else if site.Slug != "" { + parts = append(parts, site.Slug) + } + for _, location := range locations { + if location.Name != "" { + parts = append(parts, location.Name) + } else if location.Slug != "" { + parts = append(parts, location.Slug) + } + } + if rack != "" { + parts = append(parts, rack) + } + if position != "" { + parts = append(parts, "U"+position) + } + return strings.Join(parts, " › ") } // SSHHost identifies the resolved host that SSH options are computed for. @@ -102,6 +153,11 @@ type SSHHost struct { Name string // NetBox name without the instance prefix Kind string Tags []string + Status string + Site Ref + Role Ref + Platform Ref + Tenant Ref } var sshOptionKey = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9]*$`) @@ -138,7 +194,7 @@ func (r SSHRule) Validate(scope string) error { // Validate requires at least one selector and a well-formed name pattern. func (m SSHMatch) Validate(scope string) error { if m == (SSHMatch{}) { - return fmt.Errorf("%s: match must set at least one of tag, instance, kind, name", scope) + return fmt.Errorf("%s: match must set at least one of tag, instance, kind, name, site, role, platform, tenant, status", scope) } if _, err := path.Match(m.Name, ""); err != nil { return fmt.Errorf("%s: invalid name pattern %q", scope, m.Name) @@ -264,6 +320,17 @@ func (m SSHMatch) matches(host SSHHost) bool { return false } } + for _, ref := range []struct { + want string + have Ref + }{{m.Site, host.Site}, {m.Role, host.Role}, {m.Platform, host.Platform}, {m.Tenant, host.Tenant}} { + if ref.want != "" && !ref.have.Matches(ref.want) { + return false + } + } + if m.Status != "" && !strings.EqualFold(m.Status, host.Status) { + return false + } if m.Tag != "" { found := false for _, tag := range host.Tags { diff --git a/internal/netbox/client.go b/internal/netbox/client.go index e5e151a..da0e879 100644 --- a/internal/netbox/client.go +++ b/internal/netbox/client.go @@ -12,6 +12,8 @@ import ( "strings" "sync" "time" + + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config" ) type Client struct { @@ -20,6 +22,9 @@ type Client struct { token string tokenVersion int httpClient *http.Client + + locMu sync.Mutex + locations map[int]locationNode // location ID → name and parent, per client } // NewClient creates a NetBox API client. Pass tokenVersion=0 to auto-detect @@ -227,6 +232,7 @@ func (c *Client) searchDevices(ctx context.Context, query, tag string) ([]HostEn for _, d := range resp.Results { entries = append(entries, deviceToEntry(d)) } + c.fillLocations(ctx, entries) return entries, nil } @@ -278,6 +284,7 @@ func (c *Client) fetchAllDevices(ctx context.Context, query, filters string) ([] break } } + c.fillLocations(ctx, all) return all, nil } @@ -370,6 +377,20 @@ func deviceToEntry(d netboxDevice) HostEntry { if d.Cluster != nil { e.Cluster = d.Cluster.Name } + e.Status = d.Status.value() + e.Site, e.Platform, e.Tenant = d.Site.ref(), d.Platform.ref(), d.Tenant.ref() + e.Role = d.Role.ref() + if d.Role == nil { + e.Role = d.DeviceRole.ref() + } + if d.Location != nil { + e.locationID = d.Location.ID + e.Location = []config.Ref{{Name: d.Location.Name, Slug: d.Location.Slug}} + } + if d.Rack != nil { + e.Rack = d.Rack.Name + } + e.Position = rackUnit(d.Position) return e } @@ -387,6 +408,20 @@ func vmToEntry(v netboxVM) HostEntry { if v.Cluster != nil { e.Cluster = v.Cluster.Name } + e.Status = v.Status.value() + e.Site, e.Platform, e.Tenant = v.Site.ref(), v.Platform.ref(), v.Tenant.ref() + e.Role = v.Role.ref() + if v.Role == nil { + e.Role = v.DeviceRole.ref() + } + if v.Location != nil { + e.locationID = v.Location.ID + e.Location = []config.Ref{{Name: v.Location.Name, Slug: v.Location.Slug}} + } + if v.Rack != nil { + e.Rack = v.Rack.Name + } + e.Position = rackUnit(v.Position) return e } diff --git a/internal/netbox/details.go b/internal/netbox/details.go new file mode 100644 index 0000000..23c23a9 --- /dev/null +++ b/internal/netbox/details.go @@ -0,0 +1,365 @@ +package netbox + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "net/url" + "sort" + "strconv" + "strings" +) + +// HostDetails is everything netssh info shows about a device or VM. +type HostDetails struct { + HostEntry + URL string `json:"url"` + StatusLabel string `json:"status_label,omitempty"` + HeightU string `json:"height_u,omitempty"` // rack units the device occupies + Face string `json:"face,omitempty"` // front | rear + DeviceType string `json:"device_type,omitempty"` + Serial string `json:"serial,omitempty"` + AssetTag string `json:"asset_tag,omitempty"` + Description string `json:"description,omitempty"` + VCPUs string `json:"vcpus,omitempty"` + MemoryMB int `json:"memory_mb,omitempty"` + DiskGB int `json:"disk_gb,omitempty"` + IPs []IPAssignment `json:"ips"` +} + +// IPAssignment is one IP address of a host with the interface it is on. +type IPAssignment struct { + Address string `json:"address"` // CIDR notation + Interface string `json:"interface,omitempty"` + DNSName string `json:"dns_name,omitempty"` + Status string `json:"status,omitempty"` + Role string `json:"role,omitempty"` + VRF string `json:"vrf,omitempty"` + Primary bool `json:"primary,omitempty"` +} + +// IPOwner is a NetBox IP address found by LookupIP, with the host and +// interface it is assigned to. +type IPOwner struct { + Source string `json:"instance,omitempty"` + Address string `json:"address"` + Status string `json:"status,omitempty"` + DNSName string `json:"dns_name,omitempty"` + Description string `json:"description,omitempty"` + VRF string `json:"vrf,omitempty"` + Interface string `json:"interface,omitempty"` + HostKind string `json:"kind,omitempty"` // device | vm; empty when unassigned + HostName string `json:"host,omitempty"` + HostID int `json:"host_id,omitempty"` +} + +// Target returns the selector of the owning host, or "" when unassigned. +func (o IPOwner) Target() string { + if o.HostName == "" { + return "" + } + return HostEntry{Source: o.Source, Name: o.HostName}.Target() +} + +// PrefixInfo is a NetBox prefix that contains an address. +type PrefixInfo struct { + Source string `json:"instance,omitempty"` + Prefix string `json:"prefix"` + Status string `json:"status,omitempty"` + Role string `json:"role,omitempty"` + Site string `json:"site,omitempty"` + VLAN string `json:"vlan,omitempty"` + VRF string `json:"vrf,omitempty"` + Description string `json:"description,omitempty"` +} + +type netboxHostDetail struct { + netboxDevice + Face *netboxStatus `json:"face"` + Serial string `json:"serial"` + AssetTag *string `json:"asset_tag"` + DeviceType *struct { + ID int `json:"id"` + Model string `json:"model"` + Manufacturer *netboxRef `json:"manufacturer"` + } `json:"device_type"` + Description string `json:"description"` + VCPUs json.Number `json:"vcpus"` + Memory *int `json:"memory"` + Disk *int `json:"disk"` +} + +type netboxIPAddress struct { + Address string `json:"address"` + Status *netboxStatus `json:"status"` + Role *netboxStatus `json:"role"` + DNSName string `json:"dns_name"` + Description string `json:"description"` + VRF *netboxRef `json:"vrf"` + ObjectType string `json:"assigned_object_type"` + Object *struct { + Name string `json:"name"` + Device *struct { + ID int `json:"id"` + Name string `json:"name"` + } `json:"device"` + VirtualMachine *struct { + ID int `json:"id"` + Name string `json:"name"` + } `json:"virtual_machine"` + } `json:"assigned_object"` +} + +type netboxPrefix struct { + Prefix string `json:"prefix"` + Status *netboxStatus `json:"status"` + Role *netboxRef `json:"role"` + Site *netboxRef `json:"site"` + Scope *netboxRef `json:"scope"` + VRF *netboxRef `json:"vrf"` + Description string `json:"description"` + VLAN *struct { + VID int `json:"vid"` + Name string `json:"name"` + } `json:"vlan"` +} + +func hostPath(kind string) (string, error) { + switch kind { + case "device": + return "dcim/devices", nil + case "vm": + return "virtualization/virtual-machines", nil + } + return "", fmt.Errorf("invalid host kind %q", kind) +} + +// WebURL returns the page of a host in the NetBox web interface. +func (c *Client) WebURL(host HostEntry) (string, error) { + source, host, err := c.sourceForHost(host) + if err != nil { + return "", err + } + path, err := hostPath(host.Kind) + if err != nil { + return "", err + } + if host.ID <= 0 { + return "", fmt.Errorf("NetBox ID of %s is unknown", host.Target()) + } + return fmt.Sprintf("%s/%s/%d/", source.baseURL, path, host.ID), nil +} + +// GetDetails fetches the full record and all IP addresses of a host. +func (c *Client) GetDetails(ctx context.Context, host HostEntry) (HostDetails, error) { + source, host, err := c.sourceForHost(host) + if err != nil { + return HostDetails{}, err + } + apiURL, err := source.hostURL(host) + if err != nil { + return HostDetails{}, err + } + var raw netboxHostDetail + if err := source.get(ctx, apiURL, &raw); err != nil { + return HostDetails{}, err + } + entry := deviceToEntry(raw.netboxDevice) + entry.Kind, entry.Source = host.Kind, host.Source + entries := []HostEntry{entry} + source.fillLocations(ctx, entries) + entry = entries[0] + d := HostDetails{HostEntry: entry, Serial: raw.Serial, Description: raw.Description} + d.URL, _ = c.WebURL(entry) + if raw.Status != nil { + d.StatusLabel = raw.Status.Label + } + if raw.Face != nil { + d.Face = raw.Face.Value + } + if raw.AssetTag != nil { + d.AssetTag = *raw.AssetTag + } + if raw.DeviceType != nil && entry.Position != "" && raw.DeviceType.ID > 0 { + var deviceType struct { + UHeight json.Number `json:"u_height"` + } + if err := source.get(ctx, fmt.Sprintf("%s/api/dcim/device-types/%d/", source.baseURL, raw.DeviceType.ID), &deviceType); err == nil { + d.HeightU = rackUnit(deviceType.UHeight) + } + } + if raw.DeviceType != nil { + d.DeviceType = raw.DeviceType.Model + if raw.DeviceType.Manufacturer != nil && raw.DeviceType.Manufacturer.Name != "" { + d.DeviceType = raw.DeviceType.Manufacturer.Name + " " + d.DeviceType + } + } + if raw.VCPUs != "" { + d.VCPUs = strings.TrimSuffix(strings.TrimSuffix(raw.VCPUs.String(), ".00"), ".0") + } + if raw.Memory != nil { + d.MemoryMB = *raw.Memory + } + if raw.Disk != nil { + d.DiskGB = *raw.Disk + } + + filter := fmt.Sprintf("device_id=%d", host.ID) + if host.Kind == "vm" { + filter = fmt.Sprintf("virtual_machine_id=%d", host.ID) + } + addresses, err := source.fetchIPAddresses(ctx, filter) + if err != nil { + return d, fmt.Errorf("IP addresses: %w", err) + } + for _, a := range addresses { + ip := IPAssignment{Address: a.Address, DNSName: a.DNSName, Status: a.Status.value(), Role: a.Role.value()} + if a.VRF != nil { + ip.VRF = a.VRF.Name + } + if a.Object != nil { + ip.Interface = a.Object.Name + } + addr := stripPrefix(a.Address) + ip.Primary = addr == entry.PrimaryIP4 || addr == entry.PrimaryIP6 + d.IPs = append(d.IPs, ip) + } + sort.SliceStable(d.IPs, func(i, j int) bool { + if d.IPs[i].Primary != d.IPs[j].Primary { + return d.IPs[i].Primary + } + return d.IPs[i].Interface < d.IPs[j].Interface + }) + return d, nil +} + +func (c *Client) fetchIPAddresses(ctx context.Context, filter string) ([]netboxIPAddress, error) { + const pageSize = 100 + var out []netboxIPAddress + for offset := 0; ; offset += pageSize { + apiURL := fmt.Sprintf("%s/api/ipam/ip-addresses/?%s&limit=%d&offset=%d", c.baseURL, filter, pageSize, offset) + var resp netboxListResponse[netboxIPAddress] + if err := c.get(ctx, apiURL, &resp); err != nil { + return nil, err + } + out = append(out, resp.Results...) + if len(resp.Results) < pageSize || len(out) >= resp.Count { + return out, nil + } + } +} + +// eachSource runs fn for every configured instance in name order, or once +// for a single-instance client. It keeps going after an error, so one +// unreachable instance does not hide answers from the others. +func (c *Client) eachSource(fn func(name string, source *Client) error) error { + if c.sources == nil { + return fn("", c) + } + names := make([]string, 0, len(c.sources)) + for name := range c.sources { + names = append(names, name) + } + sort.Strings(names) + var errs []error + for _, name := range names { + if err := fn(name, c.sources[name]); err != nil { + errs = append(errs, fmt.Errorf("NetBox %s: %w", name, err)) + } + } + return errors.Join(errs...) +} + +// LookupIP finds the NetBox IP address records of ip in every instance. +func (c *Client) LookupIP(ctx context.Context, ip string) ([]IPOwner, error) { + var out []IPOwner + err := c.eachSource(func(name string, source *Client) error { + found, err := source.fetchIPAddresses(ctx, "address="+url.QueryEscape(ip)) + if err != nil { + return err + } + for _, a := range found { + owner := IPOwner{Source: name, Address: a.Address, Status: a.Status.value(), DNSName: a.DNSName, Description: a.Description} + if a.VRF != nil { + owner.VRF = a.VRF.Name + } + if a.Object != nil { + owner.Interface = a.Object.Name + switch { + case a.Object.Device != nil: + owner.HostKind, owner.HostName, owner.HostID = "device", a.Object.Device.Name, a.Object.Device.ID + case a.Object.VirtualMachine != nil: + owner.HostKind, owner.HostName, owner.HostID = "vm", a.Object.VirtualMachine.Name, a.Object.VirtualMachine.ID + } + } + out = append(out, owner) + } + return nil + }) + return out, err +} + +// PrefixesContaining returns the prefixes containing ip, most specific last. +func (c *Client) PrefixesContaining(ctx context.Context, ip string) ([]PrefixInfo, error) { + var out []PrefixInfo + err := c.eachSource(func(name string, source *Client) error { + apiURL := fmt.Sprintf("%s/api/ipam/prefixes/?contains=%s&limit=100", source.baseURL, url.QueryEscape(ip)) + var resp netboxListResponse[netboxPrefix] + if err := source.get(ctx, apiURL, &resp); err != nil { + return err + } + for _, p := range resp.Results { + info := PrefixInfo{Source: name, Prefix: p.Prefix, Status: p.Status.value(), Description: p.Description} + if p.Role != nil { + info.Role = p.Role.Name + } + if p.Site != nil { + info.Site = p.Site.Name + } else if p.Scope != nil { + info.Site = p.Scope.Name + } + if p.VRF != nil { + info.VRF = p.VRF.Name + } + if p.VLAN != nil { + info.VLAN = strconv.Itoa(p.VLAN.VID) + if p.VLAN.Name != "" { + info.VLAN += " (" + p.VLAN.Name + ")" + } + } + out = append(out, info) + } + return nil + }) + sort.SliceStable(out, func(i, j int) bool { return prefixLen(out[i].Prefix) < prefixLen(out[j].Prefix) }) + return out, err +} + +func prefixLen(prefix string) int { + _, bits, _ := strings.Cut(prefix, "/") + n, _ := strconv.Atoi(bits) + return n +} + +// RackUnits renders the occupied units, e.g. "U20-21 (2U, front)". +func (d HostDetails) RackUnits() string { + if d.Position == "" { + return "" + } + units := "U" + d.Position + var extra []string + if height, err := strconv.ParseFloat(d.HeightU, 64); err == nil && height > 0 { + if start, err := strconv.ParseFloat(d.Position, 64); err == nil && height > 1 { + units += "-" + strconv.FormatFloat(start+height-1, 'f', -1, 64) + } + extra = append(extra, d.HeightU+"U") + } + if d.Face != "" { + extra = append(extra, d.Face) + } + if len(extra) > 0 { + units += " (" + strings.Join(extra, ", ") + ")" + } + return units +} diff --git a/internal/netbox/details_test.go b/internal/netbox/details_test.go new file mode 100644 index 0000000..22a6a14 --- /dev/null +++ b/internal/netbox/details_test.go @@ -0,0 +1,101 @@ +package netbox + +import ( + "context" + "fmt" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +func detailServer(t *testing.T) *httptest.Server { + t.Helper() + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch { + case r.URL.Path == "/api/dcim/devices/7/": + fmt.Fprint(w, `{"id":7,"name":"sw1","status":{"value":"offline","label":"Offline"}, + "site":{"name":"Falkenstein 1","slug":"fsn1"},"rack":{"name":"R12"},"position":20.0, + "device_role":{"name":"Core Switch","slug":"core-switch"},"platform":{"name":"Junos","slug":"junos"}, + "tenant":null,"serial":"ABC","asset_tag":null,"device_type":{"model":"QFX5120","manufacturer":{"name":"Juniper"}}, + "primary_ip4":{"address":"10.0.0.7/24"},"tags":[{"name":"core"}]}`) + case r.URL.Path == "/api/ipam/ip-addresses/" && r.URL.Query().Get("device_id") == "7": + fmt.Fprint(w, `{"count":2,"results":[ + {"address":"192.0.2.7/24","assigned_object":{"name":"xe-0/0/1"},"status":{"value":"active"}}, + {"address":"10.0.0.7/24","assigned_object":{"name":"em0"},"dns_name":"sw1.example.com","status":{"value":"active"}}]}`) + case r.URL.Path == "/api/ipam/ip-addresses/" && r.URL.Query().Get("address") != "": + fmt.Fprint(w, `{"count":1,"results":[{"address":"10.0.0.7/24","status":{"value":"active"}, + "assigned_object_type":"dcim.interface","assigned_object":{"name":"em0","device":{"id":7,"name":"sw1"}}}]}`) + case r.URL.Path == "/api/ipam/prefixes/": + if r.URL.Query().Get("contains") != "10.0.0.7" { + t.Errorf("unexpected contains %q", r.URL.Query().Get("contains")) + } + fmt.Fprint(w, `{"count":2,"results":[ + {"prefix":"10.0.0.0/24","status":{"value":"active"},"vlan":{"vid":100,"name":"mgmt"},"site":{"name":"FSN1"}}, + {"prefix":"10.0.0.0/8","description":"internal"}]}`) + default: + http.NotFound(w, r) + } + })) + t.Cleanup(srv.Close) + return srv +} + +func TestDeviceAttributesAndDetails(t *testing.T) { + srv := detailServer(t) + c := NewClient(srv.URL, "token", 0) + d, err := c.GetDetails(context.Background(), HostEntry{ID: 7, Name: "sw1", Kind: "device"}) + if err != nil { + t.Fatal(err) + } + if d.Status != "offline" || d.StatusLabel != "Offline" || d.Site.Slug != "fsn1" || d.Role.Slug != "core-switch" || d.Platform.Slug != "junos" { + t.Fatalf("attributes not parsed: %+v", d.HostEntry) + } + if d.Rack != "R12" || d.Position != "20" || d.DeviceType != "Juniper QFX5120" || d.Serial != "ABC" { + t.Fatalf("details not parsed: %+v", d) + } + if d.URL != srv.URL+"/dcim/devices/7/" { + t.Fatalf("web URL %q", d.URL) + } + if len(d.IPs) != 2 || !d.IPs[0].Primary || d.IPs[0].Interface != "em0" || d.IPs[0].DNSName != "sw1.example.com" { + t.Fatalf("IPs not sorted primary first: %+v", d.IPs) + } +} + +func TestWebURLForInstances(t *testing.T) { + c := NewMultiClient([]Source{{Name: "prod", URL: "https://nb.example/", Token: "t"}}) + got, err := c.WebURL(HostEntry{Source: "prod", Name: "web", Kind: "vm", ID: 3}) + if err != nil || got != "https://nb.example/virtualization/virtual-machines/3/" { + t.Fatalf("got %q, %v", got, err) + } + if _, err := c.WebURL(HostEntry{Source: "prod", Name: "web", Kind: "vm"}); err == nil { + t.Fatal("missing ID accepted") + } +} + +func TestLookupIPAndPrefixes(t *testing.T) { + srv := detailServer(t) + c := NewMultiClient([]Source{{Name: "prod", URL: srv.URL, Token: "t"}}) + owners, err := c.LookupIP(context.Background(), "10.0.0.7") + if err != nil || len(owners) != 1 || owners[0].Target() != "prod/sw1" || owners[0].Interface != "em0" || owners[0].HostKind != "device" { + t.Fatalf("owners %+v, %v", owners, err) + } + prefixes, err := c.PrefixesContaining(context.Background(), "10.0.0.7") + if err != nil || len(prefixes) != 2 || prefixes[0].Prefix != "10.0.0.0/8" || prefixes[1].VLAN != "100 (mgmt)" || prefixes[1].Source != "prod" { + t.Fatalf("prefixes %+v, %v", prefixes, err) + } +} + +func TestLookupIPReportsFailingInstanceButKeepsOthers(t *testing.T) { + good := detailServer(t) + bad := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + http.Error(w, "down", http.StatusBadGateway) + })) + defer bad.Close() + c := NewMultiClient([]Source{{Name: "a", URL: bad.URL, Token: "t"}, {Name: "b", URL: good.URL, Token: "t"}}) + owners, err := c.LookupIP(context.Background(), "10.0.0.7") + if err == nil || !strings.Contains(err.Error(), "NetBox a") || len(owners) != 1 { + t.Fatalf("owners %+v, err %v", owners, err) + } +} diff --git a/internal/netbox/locations.go b/internal/netbox/locations.go new file mode 100644 index 0000000..b196b35 --- /dev/null +++ b/internal/netbox/locations.go @@ -0,0 +1,92 @@ +package netbox + +import ( + "context" + "encoding/json" + "fmt" + "strings" + + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config" +) + +// maxLocationDepth stops walking up a broken (cyclic) location tree. +const maxLocationDepth = 16 + +type locationNode struct { + ref config.Ref + parent int +} + +type netboxLocation struct { + ID int `json:"id"` + Name string `json:"name"` + Slug string `json:"slug"` + Parent *struct { + ID int `json:"id"` + } `json:"parent"` +} + +// rackUnit formats a NetBox rack position (a decimal such as 20.0 or 20.5). +func rackUnit(n json.Number) string { + if n == "" { + return "" + } + s := n.String() + if strings.Contains(s, ".") { + s = strings.TrimRight(strings.TrimRight(s, "0"), ".") + } + return s +} + +// locationChain returns the locations from the outermost to id. NetBox nests +// locations through "parent"; each location is fetched once per client. +func (c *Client) locationChain(ctx context.Context, id int) ([]config.Ref, error) { + var chain []config.Ref + for depth := 0; id > 0 && depth < maxLocationDepth; depth++ { + node, err := c.location(ctx, id) + if err != nil { + return nil, err + } + chain = append([]config.Ref{node.ref}, chain...) + id = node.parent + } + return chain, nil +} + +func (c *Client) location(ctx context.Context, id int) (locationNode, error) { + c.locMu.Lock() + node, ok := c.locations[id] + c.locMu.Unlock() + if ok { + return node, nil + } + var raw netboxLocation + if err := c.get(ctx, fmt.Sprintf("%s/api/dcim/locations/%d/", c.baseURL, id), &raw); err != nil { + return locationNode{}, err + } + node = locationNode{ref: config.Ref{Name: raw.Name, Slug: raw.Slug}} + if raw.Parent != nil { + node.parent = raw.Parent.ID + } + c.locMu.Lock() + if c.locations == nil { + c.locations = map[int]locationNode{} + } + c.locations[id] = node + c.locMu.Unlock() + return node, nil +} + +// fillLocations replaces each device's own location with the full chain of +// parent locations. A failed lookup keeps the device's own location: the +// location is informational and must not break a search or a connection. +func (c *Client) fillLocations(ctx context.Context, entries []HostEntry) { + for i := range entries { + if entries[i].locationID <= 0 { + continue + } + if chain, err := c.locationChain(ctx, entries[i].locationID); err == nil && len(chain) > 0 { + entries[i].Location = chain + } + } +} diff --git a/internal/netbox/locations_test.go b/internal/netbox/locations_test.go new file mode 100644 index 0000000..9a857d8 --- /dev/null +++ b/internal/netbox/locations_test.go @@ -0,0 +1,101 @@ +package netbox + +import ( + "context" + "fmt" + "net/http" + "net/http/httptest" + "strings" + "sync/atomic" + "testing" +) + +// locationServer serves a site with Building A › Floor 2 › Room 2.04 and two +// racked devices in Room 2.04, plus a location loop 90 ↔ 91. +func locationServer(t *testing.T, calls *int32) *httptest.Server { + t.Helper() + locations := map[string]string{ + "1": `{"id":1,"name":"Building A","slug":"building-a","parent":null}`, + "2": `{"id":2,"name":"Floor 2","slug":"floor-2","parent":{"id":1}}`, + "3": `{"id":3,"name":"Room 2.04","slug":"room-2-04","parent":{"id":2}}`, + "90": `{"id":90,"name":"Loop A","slug":"loop-a","parent":{"id":91}}`, + "91": `{"id":91,"name":"Loop B","slug":"loop-b","parent":{"id":90}}`, + } + device := func(id int, name string, location int, position string) string { + return fmt.Sprintf(`{"id":%d,"name":%q,"site":{"name":"FSN1","slug":"fsn1"},"location":{"id":%d,"name":"x","slug":"x"}, + "rack":{"name":"R12"},"position":%s,"face":{"value":"front"},"device_type":{"id":9,"model":"R640"}}`, id, name, location, position) + } + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case strings.HasPrefix(r.URL.Path, "/api/dcim/locations/"): + atomic.AddInt32(calls, 1) + id := strings.Trim(strings.TrimPrefix(r.URL.Path, "/api/dcim/locations/"), "/") + if body, ok := locations[id]; ok { + fmt.Fprint(w, body) + return + } + http.NotFound(w, r) + case r.URL.Path == "/api/dcim/devices/": + fmt.Fprintf(w, `{"count":3,"results":[%s,%s,%s]}`, device(1, "srv1", 3, "20.0"), device(2, "srv2", 3, "22"), device(3, "loopy", 90, "null")) + case r.URL.Path == "/api/dcim/devices/1/": + fmt.Fprint(w, device(1, "srv1", 3, "20.0")) + case r.URL.Path == "/api/dcim/device-types/9/": + fmt.Fprint(w, `{"id":9,"u_height":2.0}`) + case r.URL.Path == "/api/virtualization/virtual-machines/", r.URL.Path == "/api/ipam/ip-addresses/": + fmt.Fprint(w, `{"count":0,"results":[]}`) + default: + http.NotFound(w, r) + } + })) + t.Cleanup(srv.Close) + return srv +} + +func TestSearchAllResolvesNestedLocations(t *testing.T) { + var calls int32 + srv := locationServer(t, &calls) + entries, err := NewClient(srv.URL, "t", 0).SearchAll(context.Background(), "", SearchOptions{Kind: "device"}) + if err != nil { + t.Fatal(err) + } + byName := map[string]HostEntry{} + for _, e := range entries { + byName[e.Name] = e + } + srv1 := byName["srv1"] + if got := srv1.LocationPath(); got != "FSN1 › Building A › Floor 2 › Room 2.04 › R12 › U20" { + t.Fatalf("path %q", got) + } + if srv1.Location[0].Slug != "building-a" || srv1.Location[2].Slug != "room-2-04" { + t.Fatalf("chain order: %+v", srv1.Location) + } + // Room, floor and building are fetched once for both devices, the loop + // stops at the depth limit instead of hanging. + if n := atomic.LoadInt32(&calls); n != 5 { + t.Fatalf("location requests: %d", n) + } + if len(byName["loopy"].Location) != maxLocationDepth || byName["loopy"].Position != "" { + t.Fatalf("loop not bounded: %d", len(byName["loopy"].Location)) + } +} + +func TestDetailsShowRackUnits(t *testing.T) { + var calls int32 + srv := locationServer(t, &calls) + d, err := NewClient(srv.URL, "t", 0).GetDetails(context.Background(), HostEntry{ID: 1, Name: "srv1", Kind: "device"}) + if err != nil { + t.Fatal(err) + } + if d.Rack != "R12" || d.Position != "20" || d.HeightU != "2" || d.RackUnits() != "U20-21 (2U, front)" { + t.Fatalf("rack: %q %q %q %q", d.Rack, d.Position, d.HeightU, d.RackUnits()) + } + if len(d.Location) != 3 { + t.Fatalf("details lack location chain: %+v", d.Location) + } + if (HostDetails{HostEntry: HostEntry{Position: "7"}, HeightU: "1"}).RackUnits() != "U7 (1U)" { + t.Fatal("single unit") + } + if (HostDetails{}).RackUnits() != "" { + t.Fatal("unracked device should have no units") + } +} diff --git a/internal/netbox/models.go b/internal/netbox/models.go index df655a3..fb0d370 100644 --- a/internal/netbox/models.go +++ b/internal/netbox/models.go @@ -1,8 +1,11 @@ package netbox import ( + "encoding/json" "strings" "time" + + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config" ) // SearchOptions filters a Search or SearchAll query. @@ -27,6 +30,49 @@ type HostEntry struct { PrimaryIP6 string Tags []string Cluster string // virtualization cluster name, if assigned + Status string // NetBox status value, e.g. "active" + Site config.Ref + Role config.Ref + Platform config.Ref + Tenant config.Ref + // Location is the chain of nested NetBox locations from the outermost + // (directly below the site) to the device's own location. + Location []config.Ref + Rack string + Position string // lowest rack unit, e.g. "20"; empty when not racked + + locationID int // the device's own location, before the chain is resolved +} + +// LocationPath renders site › locations › rack › U position. +func (h HostEntry) LocationPath() string { + return config.LocationPath(h.Site, h.Location, h.Rack, h.Position) +} + +// netboxRef is a nested NetBox object with a name and slug. +type netboxRef struct { + Name string `json:"name"` + Slug string `json:"slug"` +} + +func (r *netboxRef) ref() config.Ref { + if r == nil { + return config.Ref{} + } + return config.Ref{Name: r.Name, Slug: r.Slug} +} + +// netboxStatus is a NetBox choice field such as status. +type netboxStatus struct { + Value string `json:"value"` + Label string `json:"label"` +} + +func (s *netboxStatus) value() string { + if s == nil { + return "" + } + return s.Value } // netboxCluster is the nested cluster of a device or virtual machine. @@ -50,6 +96,21 @@ type netboxDevice struct { PrimaryIP4 *netboxIP `json:"primary_ip4"` PrimaryIP6 *netboxIP `json:"primary_ip6"` Cluster *netboxCluster `json:"cluster"` + Status *netboxStatus `json:"status"` + Site *netboxRef `json:"site"` + Role *netboxRef `json:"role"` + DeviceRole *netboxRef `json:"device_role"` // NetBox < 3.6 + Platform *netboxRef `json:"platform"` + Tenant *netboxRef `json:"tenant"` + Location *struct { + ID int `json:"id"` + Name string `json:"name"` + Slug string `json:"slug"` + } `json:"location"` + Rack *struct { + Name string `json:"name"` + } `json:"rack"` + Position json.Number `json:"position"` } // netboxVM matches the relevant fields of the NetBox /virtualization/virtual-machines/ response. @@ -62,6 +123,21 @@ type netboxVM struct { PrimaryIP4 *netboxIP `json:"primary_ip4"` PrimaryIP6 *netboxIP `json:"primary_ip6"` Cluster *netboxCluster `json:"cluster"` + Status *netboxStatus `json:"status"` + Site *netboxRef `json:"site"` + Role *netboxRef `json:"role"` + DeviceRole *netboxRef `json:"device_role"` // NetBox < 3.6 + Platform *netboxRef `json:"platform"` + Tenant *netboxRef `json:"tenant"` + Location *struct { + ID int `json:"id"` + Name string `json:"name"` + Slug string `json:"slug"` + } `json:"location"` + Rack *struct { + Name string `json:"name"` + } `json:"rack"` + Position json.Number `json:"position"` } type netboxListResponse[T any] struct { diff --git a/internal/resolver/candidates_test.go b/internal/resolver/candidates_test.go new file mode 100644 index 0000000..6f89580 --- /dev/null +++ b/internal/resolver/candidates_test.go @@ -0,0 +1,30 @@ +package resolver + +import ( + "context" + "testing" + + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config" + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/netbox" +) + +func TestCandidatesListsEveryStrategyOnce(t *testing.T) { + chain, err := New(config.ResolverConfig{Strategies: []string{"primary_ip", "hostname"}, HostnameDomain: "example.com"}) + if err != nil { + t.Fatal(err) + } + entry := &netbox.HostEntry{Name: "web", Kind: "vm", PrimaryIP4: "10.0.0.1", PrimaryIP6: "2001:db8::1"} + got, err := chain.Candidates(context.Background(), entry, nil) + if err != nil { + t.Fatal(err) + } + want := []Candidate{{"10.0.0.1", "primary_ip"}, {"web.example.com", "hostname"}, {"2001:db8::1", "primary_ip"}} + if len(got) != len(want) { + t.Fatalf("got %+v", got) + } + for i := range want { + if got[i] != want[i] { + t.Fatalf("got %+v, want %+v", got, want) + } + } +} diff --git a/internal/resolver/chain.go b/internal/resolver/chain.go index 1d08092..7c7492a 100644 --- a/internal/resolver/chain.go +++ b/internal/resolver/chain.go @@ -69,3 +69,43 @@ func newStrategy(name string, cfg config.ResolverConfig) (Strategy, error) { return nil, fmt.Errorf("unknown strategy %q", name) } } + +// Candidate is an address one strategy resolved for a host. +type Candidate struct { + Address string + Strategy string +} + +// Candidates runs every strategy and returns their distinct addresses in the +// configured order. Unlike Resolve, an API failure of one strategy does not +// stop the others: candidates are only used as fallbacks for an address that +// was already chosen by Resolve and turned out to be unreachable. +func (c *Chain) Candidates(ctx context.Context, entry *netbox.HostEntry, client *netbox.Client) ([]Candidate, error) { + var out []Candidate + var errs []error + seen := map[string]bool{} + for _, s := range c.strategies { + ip, err := s.Resolve(ctx, entry, client) + if err != nil { + if !errors.Is(err, ErrNoIP) { + errs = append(errs, fmt.Errorf("%s: %w", s.Name(), err)) + } + continue + } + if !seen[ip] { + seen[ip] = true + out = append(out, Candidate{Address: ip, Strategy: s.Name()}) + } + } + // Both addresses of a dual-stack host are worth trying. + for _, ip := range []string{entry.PrimaryIP4, entry.PrimaryIP6} { + if ip != "" && !seen[ip] { + seen[ip] = true + out = append(out, Candidate{Address: ip, Strategy: "primary_ip"}) + } + } + if len(out) == 0 { + return nil, errors.Join(errs...) + } + return out, nil +} diff --git a/internal/secret/secret.go b/internal/secret/secret.go new file mode 100644 index 0000000..c9384b4 --- /dev/null +++ b/internal/secret/secret.go @@ -0,0 +1,48 @@ +// Package secret keeps NetBox API tokens in the operating system's keyring +// (Secret Service on Linux, Keychain on macOS) instead of the config file. +package secret + +import ( + "errors" + "fmt" + + "github.com/zalando/go-keyring" +) + +// Service is the keyring service name of all netssh entries. +const Service = "netssh" + +// ErrNotFound means the keyring has no token for the account. +var ErrNotFound = errors.New("no token in the system keyring") + +// Get returns the token stored for account. +func Get(account string) (string, error) { + token, err := keyring.Get(Service, account) + if errors.Is(err, keyring.ErrNotFound) { + return "", fmt.Errorf("%w for %q (store one with: netssh token set %s)", ErrNotFound, account, account) + } + if err != nil { + return "", fmt.Errorf("system keyring: %w", err) + } + return token, nil +} + +// Set stores token for account, replacing an existing one. +func Set(account, token string) error { + if err := keyring.Set(Service, account, token); err != nil { + return fmt.Errorf("system keyring: %w", err) + } + return nil +} + +// Delete removes the token of account; a missing entry is not an error. +func Delete(account string) error { + err := keyring.Delete(Service, account) + if err != nil && !errors.Is(err, keyring.ErrNotFound) { + return fmt.Errorf("system keyring: %w", err) + } + return nil +} + +// MockInit replaces the system keyring with an in-memory one, for tests. +func MockInit() { keyring.MockInit() } diff --git a/internal/setup/commands.go b/internal/setup/commands.go index e96501c..0ca0056 100644 --- a/internal/setup/commands.go +++ b/internal/setup/commands.go @@ -71,10 +71,15 @@ func editCommands(cfg *config.Config) error { huh.NewInput().Title("Only for instance").Value(&command.Match.Instance), huh.NewInput().Title("Only for kind").Description("device, vm or static; empty means all.").Value(&command.Match.Kind), huh.NewInput().Title("Only for hostname pattern").Description("For example router*; all specified filters must match.").Value(&command.Match.Name), + huh.NewInput().Title("Only for site").Description("NetBox site name or slug.").Value(&command.Match.Site), + huh.NewInput().Title("Only for role").Description("NetBox device/VM role name or slug.").Value(&command.Match.Role), + huh.NewInput().Title("Only for platform").Description("NetBox platform name or slug, e.g. junos.").Value(&command.Match.Platform), + huh.NewInput().Title("Only for status").Description("NetBox status value, e.g. active.").Value(&command.Match.Status), ).Title("Local command")).Run() if err != nil { return err } + command.Match = trimMatch(command.Match) command.Args = []string{binary} if arguments != "" { command.Args = append(command.Args, strings.Split(arguments, "\n")...) diff --git a/internal/setup/operations.go b/internal/setup/operations.go index 494f879..1aac6b8 100644 --- a/internal/setup/operations.go +++ b/internal/setup/operations.go @@ -11,6 +11,12 @@ import ( func editOperations(cfg *config.Config) error { history, days, entries := strconv.Itoa(cfg.TUI.HistoryLimit), strconv.Itoa(cfg.Activity.RetentionDays), strconv.Itoa(cfg.Activity.MaxEntries) enabled := cfg.Activity.Enabled + columns := append([]string(nil), cfg.TUI.EffectiveColumns()...) + timeout := strconv.Itoa(int(cfg.TUI.ReachabilityTimeoutDuration().Milliseconds())) + columnOptions := make([]huh.Option[string], 0, len(config.TUIColumns)) + for _, column := range config.TUIColumns { + columnOptions = append(columnOptions, huh.NewOption(columnLabels[column], column).Selected(contains(columns, column))) + } valid := func(s string) error { n, err := strconv.Atoi(s) if err != nil || n < 0 { @@ -23,13 +29,56 @@ func editOperations(cfg *config.Config) error { huh.NewConfirm().Title("Record local action history?").Description("Stores host, command name, duration and exit code; no arguments or output.").Value(&enabled), huh.NewInput().Title("Retention in days").Description("0 = no age limit").Value(&days).Validate(valid), huh.NewInput().Title("Maximum recorded actions").Description("0 = no count limit").Value(&entries).Validate(valid), - ).Title("History & activity")).Run() + ).Title("History & activity"), huh.NewGroup( + huh.NewMultiSelect[string]().Title("Shown in the TUI next to each host"). + Description("Reachability opens a TCP connection to every listed host's SSH port."). + Options(columnOptions...).Value(&columns), + huh.NewInput().Title("Reachability timeout (ms)").Description("300 by default").Value(&timeout).Validate(valid), + ).Title("TUI display")).Run() if err != nil { return err } + cfg.TUI.Columns = orderColumns(cfg.TUI.EffectiveColumns(), columns) + if ms, _ := strconv.Atoi(timeout); ms != 300 { + cfg.TUI.ReachabilityTimeout = ms + } else { + cfg.TUI.ReachabilityTimeout = 0 + } cfg.TUI.HistoryLimit, _ = strconv.Atoi(history) cfg.Activity.RetentionDays, _ = strconv.Atoi(days) cfg.Activity.MaxEntries, _ = strconv.Atoi(entries) cfg.Activity.Enabled = enabled return nil } + +var columnLabels = map[string]string{ + "alias": "Shortcut alias", "ip": "IP address", "kind": "Kind (device/vm/static)", "source": "Source (live/cache/static)", + "status": "NetBox status (if not active)", "site": "Site", "location": "Location (site › locations › rack › U)", "role": "Role", "platform": "Platform", "tenant": "Tenant", + "cluster": "Cluster", "tags": "Tags", "reachability": "Reachability (TCP check ✓/✗)", +} + +func contains(list []string, value string) bool { + for _, item := range list { + if item == value { + return true + } + } + return false +} + +// orderColumns keeps an existing custom order and appends new selections in +// the canonical order; the multi-select itself does not preserve order. +func orderColumns(previous, selected []string) []string { + out := []string{} + for _, column := range previous { + if contains(selected, column) { + out = append(out, column) + } + } + for _, column := range config.TUIColumns { + if contains(selected, column) && !contains(out, column) { + out = append(out, column) + } + } + return out +} diff --git a/internal/setup/ssh.go b/internal/setup/ssh.go index 64b9ebf..b1cfd99 100644 --- a/internal/setup/ssh.go +++ b/internal/setup/ssh.go @@ -262,22 +262,36 @@ func editRule(rule config.SSHRule, sources []config.NetBoxConfig) (config.SSHRul Title("Kind"). Options(huh.NewOption("(any)", ""), huh.NewOption("device", "device"), huh.NewOption("vm", "vm")). Value(&match.Kind), + huh.NewInput(). + Title("Site"). + Description("NetBox site name or slug, e.g. fsn1"). + Value(&match.Site), + huh.NewInput(). + Title("Role"). + Description("NetBox device/VM role name or slug, e.g. core-switch"). + Value(&match.Role), + huh.NewInput(). + Title("Platform"). + Description("NetBox platform name or slug, e.g. junos"). + Value(&match.Platform), + huh.NewInput(). + Title("Status"). + Description("NetBox status value, e.g. active"). + Value(&match.Status), huh.NewInput(). Title("Name pattern"). Description("Shell-style glob without instance prefix, e.g. *.dmz.example.com"). Value(&match.Name). Validate(func(s string) error { candidate := match - candidate.Tag = strings.TrimSpace(candidate.Tag) - candidate.Name = strings.TrimSpace(s) - return candidate.Validate("rule") + candidate.Name = s + return trimMatch(candidate).Validate("rule") }), ).Title("Rule match")).Run() if err != nil { return rule, err } - match.Tag = strings.TrimSpace(match.Tag) - match.Name = strings.TrimSpace(match.Name) + match = trimMatch(match) options, err := editSSHOptions("SSH options for rule "+describeMatch(match), rule.SSHOptions) if err != nil { @@ -288,10 +302,18 @@ func editRule(rule config.SSHRule, sources []config.NetBoxConfig) (config.SSHRul func describeMatch(m config.SSHMatch) string { var parts []string - for _, field := range [][2]string{{"tag", m.Tag}, {"instance", m.Instance}, {"kind", m.Kind}, {"name", m.Name}} { + for _, field := range matchFields(m) { if field[1] != "" { parts = append(parts, field[0]+"="+field[1]) } } return "[" + strings.Join(parts, " ") + "]" } + +// trimMatch removes surrounding spaces from every selector. +func trimMatch(m config.SSHMatch) config.SSHMatch { + for _, field := range []*string{&m.Tag, &m.Instance, &m.Kind, &m.Name, &m.Site, &m.Role, &m.Platform, &m.Tenant, &m.Status} { + *field = strings.TrimSpace(*field) + } + return m +} diff --git a/internal/setup/wizard.go b/internal/setup/wizard.go index 2defa3d..69232db 100644 --- a/internal/setup/wizard.go +++ b/internal/setup/wizard.go @@ -426,6 +426,12 @@ func save(cfg config.Config) error { for _, s := range cfg.Resolver.Strategies { fmt.Fprintf(&b, " - %s\n", s) } + if cfg.Resolver.Fallback { + b.WriteString(" fallback: true\n") + } + if cfg.Resolver.FallbackTimeout > 0 { + fmt.Fprintf(&b, " fallback_timeout: %d\n", cfg.Resolver.FallbackTimeout) + } if len(cfg.Resolver.ManagementSubnets) > 0 { b.WriteString(" management_subnets:\n") for _, s := range cfg.Resolver.ManagementSubnets { @@ -452,12 +458,19 @@ func save(cfg config.Config) error { if cfg.SSH.DefaultPort > 0 { fmt.Fprintf(&b, " default_port: %d\n", cfg.SSH.DefaultPort) } + if cfg.SSH.WarnStatus != nil && !sameStrings(cfg.SSH.WarnStatus, config.DefaultWarnStatus) { + quoted := make([]string, 0, len(cfg.SSH.WarnStatus)) + for _, status := range cfg.SSH.WarnStatus { + quoted = append(quoted, strconv.Quote(status)) + } + fmt.Fprintf(&b, " warn_status: [%s]\n", strings.Join(quoted, ", ")) + } writeSSHOptions(&b, " ", cfg.SSH.SSHOptions) if len(cfg.SSH.Rules) > 0 { b.WriteString(" rules:\n") for _, rule := range cfg.SSH.Rules { b.WriteString(" - match:\n") - for _, field := range [][2]string{{"tag", rule.Match.Tag}, {"instance", rule.Match.Instance}, {"kind", rule.Match.Kind}, {"name", rule.Match.Name}} { + for _, field := range matchFields(rule.Match) { if field[1] != "" { fmt.Fprintf(&b, " %s: %q\n", field[0], field[1]) } @@ -561,7 +574,7 @@ func save(cfg config.Config) error { fmt.Fprintf(&b, " confirm: %t\n timeout: %q\n post_run: %q\n", command.Confirm, command.Timeout, command.PostRun) if command.Match != (config.SSHMatch{}) { b.WriteString(" match:\n") - for _, pair := range [][2]string{{"tag", command.Match.Tag}, {"instance", command.Match.Instance}, {"kind", command.Match.Kind}, {"name", command.Match.Name}} { + for _, pair := range matchFields(command.Match) { if pair[1] != "" { fmt.Fprintf(&b, " %s: %q\n", pair[0], pair[1]) } @@ -570,7 +583,14 @@ func save(cfg config.Config) error { } } - fmt.Fprintf(&b, "\ntui:\n history_limit: %d\nactivity:\n enabled: %t\n retention_days: %d\n max_entries: %d\n", cfg.TUI.HistoryLimit, cfg.Activity.Enabled, cfg.Activity.RetentionDays, cfg.Activity.MaxEntries) + fmt.Fprintf(&b, "\ntui:\n history_limit: %d\n", cfg.TUI.HistoryLimit) + if cfg.TUI.Columns != nil && !sameStrings(cfg.TUI.Columns, config.DefaultTUIColumns) { + fmt.Fprintf(&b, " columns: [%s]\n", strings.Join(cfg.TUI.Columns, ", ")) + } + if cfg.TUI.ReachabilityTimeout > 0 { + fmt.Fprintf(&b, " reachability_timeout: %d\n", cfg.TUI.ReachabilityTimeout) + } + fmt.Fprintf(&b, "activity:\n enabled: %t\n retention_days: %d\n max_entries: %d\n", cfg.Activity.Enabled, cfg.Activity.RetentionDays, cfg.Activity.MaxEntries) if cfg.Shell.Alias != "" { fmt.Fprintf(&b, "\nshell:\n alias: %q\n", cfg.Shell.Alias) } @@ -628,3 +648,21 @@ func writeSSHOptions(b *strings.Builder, indent string, o config.SSHOptions) { } } } + +// matchFields lists the selectors of a rule or command match in config order. +func matchFields(m config.SSHMatch) [][2]string { + return [][2]string{{"tag", m.Tag}, {"instance", m.Instance}, {"kind", m.Kind}, {"name", m.Name}, + {"site", m.Site}, {"role", m.Role}, {"platform", m.Platform}, {"tenant", m.Tenant}, {"status", m.Status}} +} + +func sameStrings(a, b []string) bool { + if len(a) != len(b) { + return false + } + for i := range a { + if !strings.EqualFold(a[i], b[i]) { + return false + } + } + return true +} diff --git a/internal/setup/wizard_test.go b/internal/setup/wizard_test.go index 36d319f..c63e2e4 100644 --- a/internal/setup/wizard_test.go +++ b/internal/setup/wizard_test.go @@ -715,7 +715,59 @@ func TestOperationsRoundTrip(t *testing.T) { if err != nil { t.Fatal(err) } - if got.TUI != cfg.TUI || got.Activity != cfg.Activity || !reflect.DeepEqual(got.Commands, cfg.Commands) || !reflect.DeepEqual(got.SSH, cfg.SSH) { + if !reflect.DeepEqual(got.TUI, cfg.TUI) || got.Activity != cfg.Activity || !reflect.DeepEqual(got.Commands, cfg.Commands) || !reflect.DeepEqual(got.SSH, cfg.SSH) { t.Fatalf("round trip lost operations: %+v", got) } } + +func TestSaveRoundtripNetBoxInsightSettings(t *testing.T) { + dir := t.TempDir() + t.Setenv("XDG_CONFIG_HOME", dir) + cfg := config.Config{ + NetBox: config.NetBoxConfig{URL: "https://nb.example", Token: "keyring"}, + Resolver: config.ResolverConfig{Strategies: []string{"primary_ip"}, Fallback: true, FallbackTimeout: 400}, + TUI: config.TUIConfig{HistoryLimit: 10, Columns: []string{"ip", "platform", "reachability"}, ReachabilityTimeout: 900}, + SSH: config.SSHConfig{WarnStatus: []string{"planned"}, Rules: []config.SSHRule{{ + Match: config.SSHMatch{Platform: "junos", Site: "fsn1", Role: "core", Tenant: "acme", Status: "active"}, + SSHOptions: config.SSHOptions{ProxyJump: "jump"}, + }}}, + Commands: map[string]config.HostCommand{"cli": {Args: []string{"cli", "{address}"}, Match: config.SSHMatch{Platform: "eos"}}}, + } + if err := Save(cfg); err != nil { + t.Fatal(err) + } + loaded, err := config.Load() + if err != nil { + t.Fatal(err) + } + if !loaded.Resolver.Fallback || loaded.Resolver.FallbackTimeout != 400 { + t.Fatalf("resolver lost: %+v", loaded.Resolver) + } + if !reflect.DeepEqual(loaded.TUI.Columns, []string{"ip", "platform", "reachability"}) || !loaded.TUI.ReachabilityEnabled() || loaded.TUI.ReachabilityTimeout != 900 { + t.Fatalf("tui lost: %+v", loaded.TUI) + } + if !loaded.SSH.WarnsStatus("planned") || loaded.SSH.WarnsStatus("offline") { + t.Fatalf("warn_status lost: %v", loaded.SSH.WarnStatus) + } + if got := loaded.SSH.Rules[0].Match; got != cfg.SSH.Rules[0].Match { + t.Fatalf("rule match lost: %+v", got) + } + if loaded.Commands["cli"].Match.Platform != "eos" { + t.Fatalf("command match lost: %+v", loaded.Commands["cli"].Match) + } + if !config.IsKeyringToken(loaded.NetBox.Token) || loaded.NetBox.TokenVersion != 0 { + t.Fatalf("keyring token changed: %+v", loaded.NetBox) + } + + // Defaults stay implicit, so later default changes reach existing configs. + cfg = config.Config{NetBox: config.NetBoxConfig{URL: "https://nb.example"}, Resolver: config.ResolverConfig{Strategies: []string{"primary_ip"}}} + if err := Save(cfg); err != nil { + t.Fatal(err) + } + data, _ := os.ReadFile(filepath.Join(dir, "netssh.yaml")) + for _, key := range []string{"warn_status", "columns", "reachability", "fallback"} { + if strings.Contains(string(data), key) { + t.Errorf("default %s written:\n%s", key, data) + } + } +} diff --git a/internal/ssh/options.go b/internal/ssh/options.go index f4a3db6..68c24cd 100644 --- a/internal/ssh/options.go +++ b/internal/ssh/options.go @@ -21,6 +21,8 @@ type WrapperFlags struct { ForwardArgs []string // Tunnel runs ssh without a remote command (-N), for netssh tunnel. Tunnel bool + // Force connects without asking about a NetBox status in ssh.warn_status. + Force bool } // ExtractWrapperFlags removes netssh's long options that appear before the @@ -40,6 +42,8 @@ func (c Client) ExtractWrapperFlags(args []string) (WrapperFlags, []string, erro flags.DryRun = true case arg == "--no-jump": flags.NoJump = true + case arg == "--force": + flags.Force = true case arg == "--jump": if i+1 >= len(args) { return flags, nil, fmt.Errorf("--jump requires a host") diff --git a/internal/tui/insights_test.go b/internal/tui/insights_test.go new file mode 100644 index 0000000..69e850f --- /dev/null +++ b/internal/tui/insights_test.go @@ -0,0 +1,233 @@ +package tui + +import ( + "errors" + "net" + "strconv" + "strings" + "testing" + "time" + + tea "charm.land/bubbletea/v2" + + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config" + "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/netbox" +) + +func TestFilterByNetBoxAttributes(t *testing.T) { + entries := []netbox.HostEntry{ + {Name: "sw1", Kind: "device", Status: "active", Site: config.Ref{Name: "FSN 1", Slug: "fsn1"}, Platform: config.Ref{Slug: "junos"}, Role: config.Ref{Slug: "core"}}, + {Name: "sw2", Kind: "device", Status: "offline", Site: config.Ref{Slug: "nbg1"}, Platform: config.Ref{Slug: "junos"}}, + {Name: "web", Kind: "vm", Status: "active", Site: config.Ref{Slug: "fsn1"}, Tenant: config.Ref{Name: "ACME"}}, + } + for filter, want := range map[string]string{ + "status:active": "sw1 web", + "site:fsn1": "sw1 web", + "site:FSN-1": "sw1", + "platform:junos status:active": "sw1", + "role:core": "sw1", + "tenant:acme kind:vm": "web", + "bogus": "sw1 sw2 web", + } { + var names []string + for _, e := range applyFilter(entries, parseFilter(filter)) { + names = append(names, e.Name) + } + if got := strings.Join(names, " "); got != want { + t.Errorf("%q: got %q, want %q", filter, got, want) + } + } +} + +func TestStatusBadgeShownForInactiveHosts(t *testing.T) { + m := New(nil, nil, "") + m.updateListItems([]netbox.HostEntry{{Name: "a", Status: "active"}, {Name: "b", Status: "offline"}}) + var out strings.Builder + for i, it := range m.list.Items() { + compactDelegate{marked: map[string]bool{}}.Render(&out, m.list, i, it) + out.WriteString("\n") + } + if strings.Contains(out.String(), "[active]") || !strings.Contains(out.String(), "[offline]") { + t.Fatalf("unexpected badges:\n%s", out.String()) + } +} + +func TestReachabilityProbesListedHosts(t *testing.T) { + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Skip(err) + } + defer ln.Close() + port := ln.Addr().(*net.TCPAddr).Port + m := New(nil, nil, "") + m.SetReachability(time.Second, func(name, kind string, tags []string) (int, bool) { + return port, name != "behind-jump" + }) + _, cmd := m.Update(searchResultMsg{query: "", recent: true, entries: []netbox.HostEntry{ + {Name: "up", PrimaryIP4: "127.0.0.1"}, + {Name: "behind-jump", PrimaryIP4: "127.0.0.1"}, + }}) + if cmd == nil { + t.Fatal("no probe started") + } + for _, msg := range runBatch(cmd) { + m.Update(msg) + } + up := hostItem{name: "up"}.key() + skipped := hostItem{name: "behind-jump"}.key() + if m.reach[up] != reachUp || m.reach[skipped] != reachSkipped { + t.Fatalf("states: %v", m.reach) + } + var out strings.Builder + m.list.Select(0) + compactDelegate{marked: m.marked, reach: m.reach}.Render(&out, m.list, 0, m.list.Items()[0]) + if !strings.Contains(out.String(), "✓") { + t.Fatalf("reachable mark missing: %q", out.String()) + } + // A second result list does not probe hosts that already have a state. + _, cmd = m.Update(searchResultMsg{query: "", recent: true, entries: []netbox.HostEntry{{Name: "up", PrimaryIP4: "127.0.0.1"}}}) + if len(runBatch(cmd)) != 0 { + t.Fatal("host probed twice") + } +} + +func TestUnreachableHostMarked(t *testing.T) { + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Skip(err) + } + port := ln.Addr().(*net.TCPAddr).Port + ln.Close() + m := New(nil, nil, "") + m.SetReachability(200*time.Millisecond, func(string, string, []string) (int, bool) { return port, true }) + _, cmd := m.Update(searchResultMsg{recent: true, entries: []netbox.HostEntry{{Name: "down", PrimaryIP4: "127.0.0.1"}}}) + for _, msg := range runBatch(cmd) { + m.Update(msg) + } + if m.reach[hostItem{name: "down"}.key()] != reachDown { + t.Fatalf("states: %v", m.reach) + } +} + +// runBatch executes a command and the commands of a batch it returns. +func runBatch(cmd tea.Cmd) []tea.Msg { + if cmd == nil { + return nil + } + msg := cmd() + if batch, ok := msg.(tea.BatchMsg); ok { + var out []tea.Msg + for _, c := range batch { + out = append(out, runBatch(c)...) + } + return out + } + if msg == nil { + return nil + } + return []tea.Msg{msg} +} + +func TestDetailViewLoadsAndConnects(t *testing.T) { + m := New(nil, nil, "") + var asked string + m.SetDetails(func(name, kind string, id int) (string, error) { + asked = name + "/" + strconv.Itoa(id) + return "sw1 (device)\n Site FSN1\n", nil + }) + m.updateListItems([]netbox.HostEntry{{Name: "sw1", ID: 7, Kind: "device", PrimaryIP4: "10.0.0.7"}}) + _, cmd := m.Update(tea.KeyPressMsg{Code: 'l', Mod: tea.ModCtrl}) + if m.detailHost == nil || !strings.Contains(m.viewContent(), "Loading") { + t.Fatal("detail view not opened") + } + m.Update(cmd()) + if asked != "sw1/7" || !strings.Contains(m.viewContent(), "Site FSN1") { + t.Fatalf("details not shown (asked %q):\n%s", asked, m.viewContent()) + } + m.Update(tea.KeyPressMsg{Code: tea.KeyEnter}) + if m.selected == nil || m.selected.Name != "sw1" { + t.Fatalf("enter in detail view should connect: %+v", m.selected) + } +} + +func TestDetailViewShowsErrorAndEscCloses(t *testing.T) { + m := New(nil, nil, "") + m.SetDetails(func(string, string, int) (string, error) { return "", errors.New("boom") }) + m.updateListItems([]netbox.HostEntry{{Name: "sw1"}}) + _, cmd := m.Update(tea.KeyPressMsg{Code: 'l', Mod: tea.ModCtrl}) + m.Update(cmd()) + if !strings.Contains(m.viewContent(), "Error: boom") { + t.Fatalf("error not shown:\n%s", m.viewContent()) + } + m.Update(tea.KeyPressMsg{Code: tea.KeyEscape}) + if m.detailHost != nil || m.selected != nil { + t.Fatal("esc should close the detail view without selecting") + } +} + +func TestCtrlGOpensNetBoxPage(t *testing.T) { + m := New(nil, nil, "") + var opened string + m.SetOpener(func(name, kind string, id int) error { opened = name; return nil }) + m.updateListItems([]netbox.HostEntry{{Name: "web", Kind: "vm"}}) + _, cmd := m.Update(tea.KeyPressMsg{Code: 'g', Mod: tea.ModCtrl}) + if cmd == nil || m.selected != nil { + t.Fatal("ctrl+g must not quit the TUI") + } + m.Update(cmd()) + if opened != "web" || !strings.Contains(m.viewContent(), "opened web in NetBox") { + t.Fatalf("opened %q:\n%s", opened, m.viewContent()) + } +} + +func TestConfiguredColumns(t *testing.T) { + m := New(nil, nil, "") + m.SetColumns([]string{"platform", "site", "tags", "kind"}) + m.updateListItems([]netbox.HostEntry{{Name: "sw1", Kind: "device", PrimaryIP4: "10.0.0.7", Status: "offline", Tags: []string{"core", "dc"}, + Site: config.Ref{Name: "FSN 1", Slug: "fsn1"}, Platform: config.Ref{Slug: "junos"}}}) + var out strings.Builder + m.delegate().Render(&out, m.list, 1, m.list.Items()[0]) + got := out.String() + for _, want := range []string{"platform:junos", "site:fsn1", "#core #dc", "[device]"} { + if !strings.Contains(got, want) { + t.Errorf("missing %q in %q", want, got) + } + } + if strings.Contains(got, "10.0.0.7") || strings.Contains(got, "[offline]") || strings.Contains(got, "[live]") { + t.Fatalf("columns not configured must be hidden: %q", got) + } + if strings.Index(got, "platform:") > strings.Index(got, "site:") { + t.Fatalf("configured order not kept: %q", got) + } + + m.SetColumns(nil) + out.Reset() + m.delegate().Render(&out, m.list, 1, m.list.Items()[0]) + if got := out.String(); !strings.HasSuffix(strings.TrimSpace(got), "sw1") { + t.Fatalf("empty column list should show only the name: %q", got) + } +} + +func TestLocationColumnAndFilter(t *testing.T) { + entries := []netbox.HostEntry{ + {Name: "srv1", Site: config.Ref{Name: "FSN1"}, Location: []config.Ref{{Name: "Building A", Slug: "building-a"}, {Name: "Room 2.04", Slug: "room-2-04"}}, Rack: "R12", Position: "20"}, + {Name: "srv2", Site: config.Ref{Name: "NBG1"}, Location: []config.Ref{{Name: "Hall 1", Slug: "hall-1"}}, Rack: "B03"}, + } + for filter, want := range map[string]string{"location:building-a": "srv1", "location:room-2-04": "srv1", "rack:b03": "srv2", "location:hall-1": "srv2"} { + var names []string + for _, e := range applyFilter(entries, parseFilter(filter)) { + names = append(names, e.Name) + } + if got := strings.Join(names, " "); got != want { + t.Errorf("%q: got %q, want %q", filter, got, want) + } + } + m := New(nil, nil, "") + m.SetColumns([]string{"location"}) + m.updateListItems(entries[:1]) + var out strings.Builder + m.delegate().Render(&out, m.list, 1, m.list.Items()[0]) + if !strings.Contains(out.String(), "FSN1 › Building A › Room 2.04 › R12 › U20") { + t.Fatalf("location column: %q", out.String()) + } +} diff --git a/internal/tui/model.go b/internal/tui/model.go index c6eb641..47e6879 100644 --- a/internal/tui/model.go +++ b/internal/tui/model.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "io" + "net" "sort" "strconv" "strings" @@ -53,6 +54,25 @@ type noteResultMsg struct { saved bool } +// reachMsg is the result of probing a listed host's SSH port. +type reachMsg struct { + key string + state reachState +} + +// detailMsg carries the text of the detail view (ctrl+l). +type detailMsg struct { + key string + text string + err error +} + +// flashMsg is a short notice in the footer, e.g. after opening NetBox. +type flashMsg struct { + text string + err error +} + type searchResultMsg struct { query string seq int @@ -73,6 +93,11 @@ type hostItem struct { ip string kind string tags []string + status string + site, role string + platform, tenant string + cluster string + location string // site › locations › rack › U } func (h hostItem) Title() string { @@ -86,8 +111,85 @@ func (h hostItem) FilterValue() string { return h.name } // --- compact list delegate --- +// reachState is the result of the reachability probe of a host. +type reachState uint8 + +const ( + reachUnknown reachState = iota + reachPending + reachUp + reachDown + reachSkipped // behind a jump host: not probed +) + // compactDelegate shares the model's marks so marked hosts show a dot. -type compactDelegate struct{ marked map[string]bool } +// With reach set, a column shows whether the SSH port answers. +type compactDelegate struct { + marked map[string]bool + reach map[string]reachState + columns []string // nil = defaultColumns +} + +// defaultColumns matches config.DefaultTUIColumns. +var defaultColumns = []string{"alias", "ip", "source", "status"} + +var ( + dimStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("240")) + attrStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("244")) + aliasStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("109")) + statusStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("214")) +) + +// column renders one configured column of a host, or "" when it is empty. +func (h hostItem) column(name string) string { + attr := func(key, value string) string { + if value == "" { + return "" + } + return attrStyle.Render(key + ":" + value) + } + switch name { + case "alias": + if h.alias != "" { + return aliasStyle.Render("→ " + h.alias) + } + case "ip": + if h.ip != "" { + return dimStyle.Render(h.ip) + } + case "kind": + if h.kind != "" { + return "[" + h.kind + "]" + } + case "source": + if h.provenance != "" { + return "[" + h.provenance + "]" + } + case "status": + if h.status != "" && !strings.EqualFold(h.status, "active") { + return statusStyle.Render("[" + h.status + "]") + } + case "site": + return attr("site", h.site) + case "location": + if h.location != "" { + return attrStyle.Render("⌂ " + h.location) + } + case "role": + return attr("role", h.role) + case "platform": + return attr("platform", h.platform) + case "tenant": + return attr("tenant", h.tenant) + case "cluster": + return attr("cluster", h.cluster) + case "tags": + if len(h.tags) > 0 { + return attrStyle.Render("#" + strings.Join(h.tags, " #")) + } + } + return "" +} func (d compactDelegate) Height() int { return 1 } func (d compactDelegate) Spacing() int { return 0 } @@ -101,19 +203,35 @@ func (d compactDelegate) Render(w io.Writer, m list.Model, index int, item list. if d.marked[h.key()] { mark = "● " } - title := h.Title() - if h.alias != "" { - title += " " + lipgloss.NewStyle().Foreground(lipgloss.Color("109")).Render("→ "+h.alias) - } badge := "" if h.pinned { badge = "◆ " } else if h.favorite { badge = "★ " } - line := fmt.Sprintf(" %s%s %s", mark+badge, title, lipgloss.NewStyle().Foreground(lipgloss.Color("240")).Render(h.ip)) - if h.provenance != "" { - line += " [" + h.provenance + "]" + columns := d.columns + if columns == nil { + columns = defaultColumns + } + // The reachability mark is a fixed-width prefix so names stay aligned. + reach := "" + if d.reach != nil { + switch d.reach[h.key()] { + case reachUp: + reach = lipgloss.NewStyle().Foreground(lipgloss.Color("2")).Render("✓") + " " + case reachDown: + reach = lipgloss.NewStyle().Foreground(lipgloss.Color("9")).Render("✗") + " " + case reachPending: + reach = dimStyle.Render("·") + " " + default: + reach = " " + } + } + line := " " + mark + reach + badge + h.Title() + for _, name := range columns { + if text := h.column(name); text != "" { + line += " " + text + } } if index == m.Index() { line = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("86")).Render("> " + strings.TrimPrefix(line, " ")) @@ -124,24 +242,47 @@ func (d compactDelegate) Render(w io.Writer, m list.Model, index int, item list. // --- filter --- type filterOpts struct { - tags []string - kind string + tags []string + kind string + status, site, role, platform, tenant string + location string +} + +func (f filterOpts) empty() bool { + return len(f.tags) == 0 && f.kind == "" && f.status == "" && f.site == "" && f.role == "" && f.platform == "" && f.tenant == "" && f.location == "" } func parseFilter(s string) filterOpts { var f filterOpts for _, part := range strings.Fields(s) { - if after, ok := strings.CutPrefix(part, "tag:"); ok { - f.tags = append(f.tags, after) - } else if after, ok := strings.CutPrefix(part, "kind:"); ok { - f.kind = after + key, value, ok := strings.Cut(part, ":") + if !ok { + continue + } + switch strings.ToLower(key) { + case "tag": + f.tags = append(f.tags, value) + case "kind": + f.kind = value + case "status": + f.status = value + case "site": + f.site = value + case "role": + f.role = value + case "platform": + f.platform = value + case "tenant": + f.tenant = value + case "location", "rack": + f.location = value } } return f } func applyFilter(entries []netbox.HostEntry, f filterOpts) []netbox.HostEntry { - if len(f.tags) == 0 && f.kind == "" { + if f.empty() { return entries } out := make([]netbox.HostEntry, 0, len(entries)) @@ -149,6 +290,14 @@ func applyFilter(entries []netbox.HostEntry, f filterOpts) []netbox.HostEntry { if f.kind != "" && e.Kind != f.kind { continue } + if f.status != "" && !strings.EqualFold(e.Status, f.status) { + continue + } + if f.site != "" && !e.Site.Matches(f.site) || f.role != "" && !e.Role.Matches(f.role) || + f.platform != "" && !e.Platform.Matches(f.platform) || f.tenant != "" && !e.Tenant.Matches(f.tenant) || + f.location != "" && !inLocation(e, f.location) { + continue + } if len(f.tags) > 0 { tagSet := make(map[string]bool, len(e.Tags)) for _, t := range e.Tags { @@ -215,6 +364,17 @@ type Model struct { hostLabel func(string) string aliasFor func(string) string // shortcut alias per target, "" for none markOrder []string // targets in the order they were marked + + columns []string // nil = defaultColumns + reach map[string]reachState // probe results by hostItem.key() + reachTimeout time.Duration + reachPort func(name, kind string, tags []string) (port int, probe bool) + opener func(name, kind string, id int) error + details func(name, kind string, id int) (string, error) + detailHost *hostItem + detailText string + detailErr error + flash string } func New(client *netbox.Client, c *cache.Cache, defaultUser string, defaultPorts ...int) *Model { @@ -223,7 +383,7 @@ func New(client *netbox.Client, c *cache.Cache, defaultUser string, defaultPorts ti.Focus() fi := textinput.New() - fi.Placeholder = "tag:prod kind:vm" + fi.Placeholder = "tag:prod kind:vm status:active site:fsn1 location:room-2 role:router platform:junos" fi.Prompt = "Filter: " ei := textinput.New() @@ -304,7 +464,7 @@ func (m *Model) loadRecent() tea.Cmd { recent = favorites entries := make([]netbox.HostEntry, len(recent)) for i, e := range recent { - entries[i] = netbox.HostEntry{Name: e.Name, PrimaryIP4: e.IP, Kind: e.Kind, Tags: e.Tags, Cached: true, CachedAt: e.CachedAt, Static: e.Static} + entries[i] = entryFromCache(e, false) } return searchResultMsg{query: "", entries: entries, recent: true} } @@ -328,6 +488,25 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } return m, nil + case reachMsg: + if m.reach != nil { + m.reach[msg.key] = msg.state + } + return m, nil + + case detailMsg: + if m.detailHost != nil && m.detailHost.key() == msg.key { + m.detailText, m.detailErr = msg.text, msg.err + } + return m, nil + + case flashMsg: + m.flash = msg.text + if msg.err != nil { + m.flash = "error: " + msg.err.Error() + } + return m, nil + case tea.WindowSizeMsg: m.width = msg.Width m.height = msg.Height @@ -342,6 +521,23 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, tea.Quit } + if m.flash != "" { + m.flash = "" + } + if m.detailHost != nil { + switch msg.String() { + case "esc", "ctrl+l", "q": + m.detailHost = nil + m.input.Focus() + case "enter": + h := m.detailHost + m.selected = &SelectedHost{Name: h.name, IP: h.ip, Kind: h.kind} + return m, tea.Quit + case "ctrl+g": + return m, m.openCmd(*m.detailHost) + } + return m, nil + } if m.actionHost != nil { switch msg.String() { case "esc": @@ -554,6 +750,25 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, tea.Quit } + case "ctrl+g": + if item, ok := m.list.SelectedItem().(hostItem); ok { + return m, m.openCmd(item) + } + return m, nil + + case "ctrl+l": + if item, ok := m.list.SelectedItem().(hostItem); ok && m.details != nil { + m.detailHost = &item + m.detailText, m.detailErr = "", nil + m.input.Blur() + details, key := m.details, item.key() + return m, func() tea.Msg { + text, err := details(item.name, item.kind, item.id) + return detailMsg{key: key, text: text, err: err} + } + } + return m, nil + case "ctrl+t": if item, ok := m.list.SelectedItem().(hostItem); ok { m.forwardMode = true @@ -617,7 +832,7 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.recentMode = true m.lastResults = msg.entries m.updateListItems(msg.entries) - return m, nil + return m, m.probeCmd() } if msg.seq != m.seq || msg.query != m.input.Value() { return m, nil // discard stale result @@ -626,7 +841,7 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.err = msg.err m.lastResults = msg.entries m.updateListItems(msg.entries) - return m, nil + return m, m.probeCmd() } if m.noteMode { @@ -675,6 +890,32 @@ func (m *Model) View() tea.View { } func (m *Model) viewContent() string { + if m.detailHost != nil { + var out strings.Builder + title := lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("86")).Render("Details — " + m.detailHost.Title()) + out.WriteString(title + "\n\n") + switch { + case m.detailErr != nil: + out.WriteString(lipgloss.NewStyle().Foreground(lipgloss.Color("9")).Render("Error: "+m.detailErr.Error()) + "\n") + case m.detailText == "": + out.WriteString(lipgloss.NewStyle().Foreground(lipgloss.Color("240")).Render("Loading…") + "\n") + default: + text := m.detailText + if m.height > 4 { + lines := strings.Split(strings.TrimRight(text, "\n"), "\n") + if len(lines) > m.height-4 { + lines = append(lines[:m.height-5], "…") + } + text = strings.Join(lines, "\n") + "\n" + } + out.WriteString(text) + } + if m.flash != "" { + out.WriteString("\n" + m.flash + "\n") + } + out.WriteString("\n" + lipgloss.NewStyle().Foreground(lipgloss.Color("240")).Render("enter connect ctrl+g open in NetBox esc back") + "\n") + return out.String() + } if m.actionHost != nil { var out strings.Builder fmt.Fprintf(&out, "Action for %s", m.actionHost.Title()) @@ -753,7 +994,10 @@ func (m *Model) viewContent() string { sb.WriteString(m.editInput.View() + "\n") sb.WriteString(lipgloss.NewStyle().Foreground(lipgloss.Color("240")).Render(" enter connect esc cancel") + "\n") } else { - hint := "ctrl+r actions ctrl+d favorite ctrl+p pin ctrl+c quit enter connect space mark ctrl+o sftp ctrl+t forward ctrl+e edit ctrl+f filter ctrl+n notes ctrl+b NetBox" + hint := "ctrl+r actions ctrl+d favorite ctrl+p pin ctrl+c quit enter connect space mark ctrl+o sftp ctrl+t forward ctrl+e edit ctrl+f filter ctrl+n notes ctrl+b NetBox ctrl+l details ctrl+g NetBox page" + if m.flash != "" { + hint = m.flash + } if n := len(m.markOrder); n > 0 { hint = fmt.Sprintf("%d marked — enter open side by side space unmark ctrl+a all esc quit", n) } @@ -781,8 +1025,7 @@ func (m *Model) doSearch(query string, seq int) tea.Cmd { var cached []netbox.HostEntry if m.cache != nil { for _, entry := range m.cache.Search(query) { - source, name := netbox.SplitTarget(entry.Name) - cached = append(cached, netbox.HostEntry{Source: source, Name: name, PrimaryIP4: entry.IP, Kind: entry.Kind, Tags: entry.Tags, Cached: true, CachedAt: entry.CachedAt, Static: entry.Static}) + cached = append(cached, entryFromCache(entry, true)) } } if m.client == nil || (len(cached) > 0 && !m.client.MultipleSources()) { @@ -816,6 +1059,18 @@ func (m *Model) netboxSearchOpts() netbox.SearchOptions { return netbox.SearchOptions{Tag: tag, Kind: f.kind} } +// entryFromCache converts a cache entry for the list. With split, the +// instance prefix becomes Source, like in live NetBox results. +func entryFromCache(e cache.Entry, split bool) netbox.HostEntry { + h := netbox.HostEntry{Name: e.Name, ID: e.ID, PrimaryIP4: e.IP, Kind: e.Kind, Tags: e.Tags, Cluster: e.Cluster, Cached: true, CachedAt: e.CachedAt, Static: e.Static, + Status: e.Status, Site: cache.RefOf(e.Site), Role: cache.RefOf(e.Role), Platform: cache.RefOf(e.Platform), Tenant: cache.RefOf(e.Tenant), + Location: e.Location, Rack: e.Rack, Position: e.Position} + if split { + h.Source, h.Name = netbox.SplitTarget(e.Name) + } + return h +} + // updateListItems applies the active filter and sets the list items. func (m *Model) updateListItems(entries []netbox.HostEntry) { f := parseFilter(m.filterInput.Value()) @@ -876,7 +1131,8 @@ func (m *Model) updateListItems(entries []netbox.HostEntry) { } } } - items[i] = hostItem{provenance: provenance, favorite: m.favorites[e.Target()], pinned: m.pinned[e.Target()], label: label, alias: alias, id: e.ID, name: e.Target(), ip: ip, kind: e.Kind, tags: e.Tags} + items[i] = hostItem{provenance: provenance, favorite: m.favorites[e.Target()], pinned: m.pinned[e.Target()], label: label, alias: alias, id: e.ID, name: e.Target(), ip: ip, kind: e.Kind, tags: e.Tags, status: e.Status, + site: e.Site.Key(), role: e.Role.Key(), platform: e.Platform.Key(), tenant: e.Tenant.Key(), cluster: e.Cluster, location: e.LocationPath()} } m.list.SetItems(items) m.resizeList() @@ -1040,3 +1296,94 @@ func (m *Model) SetPreferences(favorites, pinned map[string]bool, save func(map[ func (m *Model) SetCommands(commands func(string, string, []string) []string) { m.commandsFor = commands } + +// SetReachability enables the reachability column: listed hosts are probed +// with a TCP connect to the port returned by portFor, unless it returns +// probe=false (for hosts behind a jump host). Configure it before the program starts. +func (m *Model) SetReachability(timeout time.Duration, portFor func(name, kind string, tags []string) (port int, probe bool)) { + if timeout <= 0 { + timeout = 300 * time.Millisecond + } + m.reach = map[string]reachState{} + m.reachTimeout = timeout + m.reachPort = portFor + m.list.SetDelegate(m.delegate()) +} + +// SetColumns selects what is shown next to each hostname, in order (see +// config.TUIColumns). "reachability" is ignored here: SetReachability +// enables the check. Configure it before the program starts. +func (m *Model) SetColumns(columns []string) { + m.columns = append([]string{}, columns...) + m.list.SetDelegate(m.delegate()) +} + +func (m *Model) delegate() compactDelegate { + return compactDelegate{marked: m.marked, reach: m.reach, columns: m.columns} +} + +// probeCmd starts probes for listed hosts that have no result yet. +func (m *Model) probeCmd() tea.Cmd { + if m.reach == nil { + return nil + } + var cmds []tea.Cmd + for _, it := range m.list.Items() { + h, ok := it.(hostItem) + if !ok || m.reach[h.key()] != reachUnknown { + continue + } + key := h.key() + port, probe := 22, true + if m.reachPort != nil { + port, probe = m.reachPort(h.name, h.kind, h.tags) + } + if !probe || h.ip == "" { + m.reach[key] = reachSkipped + continue + } + m.reach[key] = reachPending + address, timeout := h.ip, m.reachTimeout + cmds = append(cmds, func() tea.Msg { + conn, err := net.DialTimeout("tcp", net.JoinHostPort(address, strconv.Itoa(port)), timeout) + if err != nil { + return reachMsg{key: key, state: reachDown} + } + _ = conn.Close() + return reachMsg{key: key, state: reachUp} + }) + } + return tea.Batch(cmds...) +} + +// SetOpener opens a host's NetBox page (ctrl+g). Configure it before the program starts. +func (m *Model) SetOpener(open func(name, kind string, id int) error) { m.opener = open } + +// SetDetails provides the text of the detail view (ctrl+l). Configure it before the program starts. +func (m *Model) SetDetails(details func(name, kind string, id int) (string, error)) { + m.details = details +} + +func (m *Model) openCmd(h hostItem) tea.Cmd { + if m.opener == nil { + return nil + } + open := m.opener + return func() tea.Msg { + if err := open(h.name, h.kind, h.id); err != nil { + return flashMsg{err: err} + } + return flashMsg{text: "opened " + h.name + " in NetBox"} + } +} + +// inLocation reports whether a host is in the location want at any level of +// its location chain, or in the rack named want. +func inLocation(e netbox.HostEntry, want string) bool { + for _, location := range e.Location { + if location.Matches(want) { + return true + } + } + return want != "" && strings.EqualFold(e.Rack, want) +} diff --git a/testenv/tests/13-configure.sh b/testenv/tests/13-configure.sh index 7a2ab02..0c58108 100644 --- a/testenv/tests/13-configure.sh +++ b/testenv/tests/13-configure.sh @@ -3,7 +3,7 @@ config_only