Files
ssh-netbox-wrapper/internal/config/ssh.go
T
Sebastian UnterschützandClaude Opus 5.5 1660bb3098
Test / test (pull_request) Successful in 4m15s
feat: NetBox insights, host tools and configurable TUI
- info/open/whois: NetBox details (site, nested locations, rack units,
  role, platform, tenant, IPs per interface), web page, reverse IP lookup
- cache status, site, role, platform, tenant, location chain and rack;
  ask before connecting to hosts in ssh.warn_status (--force skips)
- ssh rules and command restrictions match site/role/platform/tenant/status
- tui.columns selects what the TUI shows; optional TCP reachability column;
  detail view (Ctrl+L), NetBox page (Ctrl+G), status/location filters
- resolver.fallback tries other strategies' addresses when unreachable
- copy-id, known-hosts reset and address-change notices
- export ansible (YAML/INI) and export ssh-config
- token: keyring stores NetBox tokens in the system keyring
- fix stale menu number in testenv configure test

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 12:33:59 +01:00

353 lines
11 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package config
import (
"fmt"
"path"
"regexp"
"strings"
)
// SSHOptions are connection settings that can be set globally, per NetBox
// instance, and per rule. More specific layers override less specific ones.
type SSHOptions struct {
// ProxyJump is passed as -J; "none" disables a jump host set by a broader layer.
ProxyJump string `mapstructure:"proxy_jump"`
// HostKeyChecking is strict, accept-new, or off. Empty keeps ssh's own setting.
HostKeyChecking string `mapstructure:"host_key_checking"`
// Target selects the ssh destination: ip (default), name, or hostname.
Target string `mapstructure:"target"`
// AddressFamily is any, inet, or inet6.
AddressFamily string `mapstructure:"address_family"`
// ExtraArgs are inserted verbatim before the destination.
ExtraArgs []string `mapstructure:"extra_args"`
// Options are passed as -o Key=Value.
Options map[string]string `mapstructure:"options"`
// Forwards are named port forwards, selected with --forward or netssh tunnel.
Forwards map[string]Forward `mapstructure:"forwards"`
// PreConnect is a shell command run locally before connecting; a non-zero
// exit aborts the connection. "none" disables a hook set by a broader layer.
PreConnect string `mapstructure:"pre_connect"`
PostDisconnect string `mapstructure:"post_disconnect"`
}
// PreConnectNone disables an inherited pre_connect hook.
const PreConnectNone = "none"
// PreConnectCommand returns the hook to run, or "" when none is configured.
func (o SSHOptions) PreConnectCommand() string {
command := strings.TrimSpace(o.PreConnect)
if strings.EqualFold(command, PreConnectNone) {
return ""
}
return command
}
// Forward is one port forward; exactly one of the fields is set.
type Forward struct {
Local string `mapstructure:"local"` // -L [bind:]port:host:hostport
Remote string `mapstructure:"remote"` // -R [bind:]port:host:hostport
Dynamic string `mapstructure:"dynamic"` // -D [bind:]port (SOCKS proxy)
}
// Flag returns the ssh arguments for the forward, e.g. -L 8080:localhost:80.
func (f Forward) Flag() []string {
switch {
case f.Local != "":
return []string{"-L", f.Local}
case f.Remote != "":
return []string{"-R", f.Remote}
case f.Dynamic != "":
return []string{"-D", f.Dynamic}
}
return nil
}
// String renders the forward as its ssh flag, e.g. "-L 8080:localhost:80".
func (f Forward) String() string { return strings.Join(f.Flag(), " ") }
func (f Forward) validate() error {
set := 0
for _, spec := range []string{f.Local, f.Remote, f.Dynamic} {
if spec == "" {
continue
}
set++
if strings.HasPrefix(spec, "-") || strings.ContainsAny(spec, " \t\n\r") {
return fmt.Errorf("invalid forward %q", spec)
}
}
if set != 1 {
return fmt.Errorf("set exactly one of local, remote, dynamic")
}
return nil
}
// SSHRule applies SSH options to hosts that match every non-empty field.
type SSHRule struct {
Match SSHMatch `mapstructure:"match"`
SSHOptions `mapstructure:",squash"`
}
// SSHMatch selects hosts by NetBox attributes available in the cache.
type SSHMatch struct {
Tag string `mapstructure:"tag"`
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.
type SSHHost struct {
Instance string
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]*$`)
// ValidateSSH rejects values that would be passed to ssh in an unexpected form.
func (c *Config) ValidateSSH() error {
if err := c.SSH.SSHOptions.Validate("ssh"); err != nil {
return err
}
for i, rule := range c.SSH.Rules {
if err := rule.Validate(fmt.Sprintf("ssh.rules[%d]", i)); err != nil {
return err
}
}
if err := c.NetBox.SSH.Validate("netbox.ssh"); err != nil {
return err
}
for _, source := range c.NetBoxes {
if err := source.SSH.Validate("netboxes." + source.Name + ".ssh"); err != nil {
return err
}
}
return nil
}
// Validate checks the match and options of a single rule.
func (r SSHRule) Validate(scope string) error {
if err := r.SSHOptions.Validate(scope); err != nil {
return err
}
return r.Match.Validate(scope)
}
// 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, 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)
}
return nil
}
// Validate rejects values that ssh would not accept or would misinterpret.
func (o SSHOptions) Validate(scope string) error {
switch o.HostKeyChecking {
case "", "strict", "accept-new", "off":
default:
return fmt.Errorf("%s.host_key_checking must be strict, accept-new, or off", scope)
}
switch o.Target {
case "", "ip", "name", "hostname":
default:
return fmt.Errorf("%s.target must be ip, name, or hostname", scope)
}
switch o.AddressFamily {
case "", "any", "inet", "inet6":
default:
return fmt.Errorf("%s.address_family must be any, inet, or inet6", scope)
}
if strings.HasPrefix(o.ProxyJump, "-") || strings.ContainsAny(o.ProxyJump, " \t\n") {
return fmt.Errorf("%s.proxy_jump %q is not a valid jump host", scope, o.ProxyJump)
}
for key, value := range o.Options {
if !sshOptionKey.MatchString(key) || strings.ContainsAny(value, "\n\r") {
return fmt.Errorf("%s.options: invalid option %q", scope, key)
}
}
if strings.ContainsRune(o.PostDisconnect, 0) {
return fmt.Errorf("%s.post_disconnect must not contain NUL bytes", scope)
}
if strings.ContainsRune(o.PreConnect, 0) {
return fmt.Errorf("%s.pre_connect must not contain NUL bytes", scope)
}
for name, forward := range o.Forwards {
if !instanceName.MatchString(name) {
return fmt.Errorf("%s.forwards: name %q must contain only letters, digits, underscores, or hyphens", scope, name)
}
if err := forward.validate(); err != nil {
return fmt.Errorf("%s.forwards.%s: %v", scope, name, err)
}
}
return nil
}
// SSHOptionsFor merges global, instance, and matching rule options for host.
func (c *Config) SSHOptionsFor(host SSHHost) SSHOptions {
merged := c.SSH.SSHOptions.merge(SSHOptions{})
if host.Kind == StaticKind {
if static := c.StaticHost(host.Name); static != nil {
merged = merged.merge(static.SSH)
}
} else if host.Instance == "" {
merged = merged.merge(c.NetBox.SSH)
}
for _, source := range c.NetBoxes {
if strings.EqualFold(source.Name, host.Instance) {
merged = merged.merge(source.SSH)
}
}
for _, rule := range c.SSH.Rules {
if rule.Match.matches(host) {
merged = merged.merge(rule.SSHOptions)
}
}
return merged
}
// merge returns o overridden by the non-empty fields of override.
// Extra arguments accumulate; options override per key.
func (o SSHOptions) merge(override SSHOptions) SSHOptions {
out := o
out.ExtraArgs = append(append([]string(nil), o.ExtraArgs...), override.ExtraArgs...)
out.Options = make(map[string]string, len(o.Options)+len(override.Options))
for key, value := range o.Options {
out.Options[strings.ToLower(key)] = value
}
for key, value := range override.Options {
out.Options[strings.ToLower(key)] = value
}
// Forwards override per name, so a rule can redefine a global "web" forward.
out.Forwards = make(map[string]Forward, len(o.Forwards)+len(override.Forwards))
for name, forward := range o.Forwards {
out.Forwards[strings.ToLower(name)] = forward
}
for name, forward := range override.Forwards {
out.Forwards[strings.ToLower(name)] = forward
}
if override.ProxyJump != "" {
out.ProxyJump = override.ProxyJump
}
if override.HostKeyChecking != "" {
out.HostKeyChecking = override.HostKeyChecking
}
if override.Target != "" {
out.Target = override.Target
}
if override.AddressFamily != "" {
out.AddressFamily = override.AddressFamily
}
if strings.TrimSpace(override.PostDisconnect) != "" {
out.PostDisconnect = override.PostDisconnect
}
if strings.TrimSpace(override.PreConnect) != "" {
out.PreConnect = override.PreConnect
}
return out
}
func (m SSHMatch) matches(host SSHHost) bool {
if m.Instance != "" && !strings.EqualFold(m.Instance, host.Instance) {
return false
}
if m.Kind != "" && !strings.EqualFold(m.Kind, host.Kind) {
return false
}
if m.Name != "" {
if ok, _ := path.Match(strings.ToLower(m.Name), strings.ToLower(host.Name)); !ok {
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 {
if strings.EqualFold(tag, m.Tag) || strings.EqualFold(tagSlug(tag), m.Tag) {
found = true
break
}
}
if !found {
return false
}
}
return true
}
// tagSlug approximates NetBox's slug so rules may use either the tag name or slug.
func tagSlug(name string) string {
return strings.Join(strings.Fields(strings.ToLower(name)), "-")
}