ValidateHostConfig validates a host configuration
(cfg *HostConfig)
| 66 | |
| 67 | // ValidateHostConfig validates a host configuration |
| 68 | func ValidateHostConfig(cfg *HostConfig) error { |
| 69 | var errs []string |
| 70 | |
| 71 | // Validate required fields |
| 72 | if cfg.Name == "" { |
| 73 | errs = append(errs, "host name is required") |
| 74 | } |
| 75 | if strings.ContainsAny(cfg.Name, " \t\n") { |
| 76 | errs = append(errs, "host name cannot contain whitespace") |
| 77 | } |
| 78 | |
| 79 | // Special handling for Host * |
| 80 | if cfg.Name == "*" || cfg.IsGlobal { |
| 81 | cfg.IsGlobal = true |
| 82 | // For global configs, Hostname is optional |
| 83 | } else { |
| 84 | // For regular hosts, Hostname is required |
| 85 | if cfg.Hostname == "" { |
| 86 | errs = append(errs, "hostname is required") |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // Validate port |
| 91 | if cfg.Port != "" { |
| 92 | port, err := strconv.Atoi(cfg.Port) |
| 93 | if err != nil || port < 1 || port > 65535 { |
| 94 | errs = append(errs, "port must be between 1 and 65535") |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // Validate proxy configuration (mutually exclusive) |
| 99 | if cfg.ProxyCommand != "" && cfg.ProxyJump != "" { |
| 100 | errs = append(errs, "cannot specify both ProxyCommand and ProxyJump") |
| 101 | } |
| 102 | |
| 103 | // Validate ForwardAgent |
| 104 | if cfg.ForwardAgent != "" { |
| 105 | fa := strings.ToLower(cfg.ForwardAgent) |
| 106 | if fa != "yes" && fa != "no" { |
| 107 | errs = append(errs, "ForwardAgent must be 'yes' or 'no'") |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // Validate ServerAliveCountMax |
| 112 | if cfg.ServerAliveCountMax != "" { |
| 113 | count, err := strconv.Atoi(cfg.ServerAliveCountMax) |
| 114 | if err != nil || count < 0 { |
| 115 | errs = append(errs, "ServerAliveCountMax must be a non-negative integer") |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // Validate AddKeysToAgent |
| 120 | if cfg.AddKeysToAgent != "" { |
| 121 | val := strings.ToLower(cfg.AddKeysToAgent) |
| 122 | if val != "yes" && val != "no" && val != "ask" && val != "confirm" { |
| 123 | errs = append(errs, "AddKeysToAgent must be 'yes', 'no', 'ask', or 'confirm'") |
| 124 | } |
| 125 | } |
no outgoing calls
no test coverage detected