(
hostAlias: string,
configPath: string,
options: { requireUser?: boolean } = {}
)
| 109 | options: { requireUser: false } |
| 110 | ): SSHConfigLookupResult | null; |
| 111 | export function parseSSHConfig( |
| 112 | hostAlias: string, |
| 113 | configPath: string, |
| 114 | options: { requireUser?: boolean } = {} |
| 115 | ): SSHConfigLookupResult | null { |
| 116 | const { requireUser = true } = options; |
| 117 | |
| 118 | // Resolve symlinks in the config path (important for Windows where .ssh may be a junction) |
| 119 | const sshConfigPath = resolveSymlink(configPath); |
| 120 | |
| 121 | // Check if SSH config file exists |
| 122 | if (!isFile(sshConfigPath)) { |
| 123 | return null; |
| 124 | } |
| 125 | |
| 126 | try { |
| 127 | // Read and parse SSH config file |
| 128 | const configContent = readFileSync(sshConfigPath, 'utf8'); |
| 129 | const config = SSHConfig.parse(configContent); |
| 130 | |
| 131 | // Find configuration for the specified host |
| 132 | const hostConfig = config.compute(hostAlias); |
| 133 | |
| 134 | // Check if we have a valid config (not just Include directives). A host counts as |
| 135 | // configured if it sets any meaningful directive — not only HostName/User — since |
| 136 | // ProxyJump aliases often define just Port/IdentityFile/ProxyJump and inherit the |
| 137 | // hostname (the alias) and username (from the target). |
| 138 | if ( |
| 139 | !hostConfig || |
| 140 | (!hostConfig.HostName && !hostConfig.User && !hostConfig.Port && !hostConfig.IdentityFile && !hostConfig.ProxyJump) |
| 141 | ) { |
| 142 | return null; |
| 143 | } |
| 144 | |
| 145 | // Extract SSH configuration parameters |
| 146 | const sshConfig: Partial<SSHConfigLookupResult> = {}; |
| 147 | |
| 148 | // Host (required) |
| 149 | if (hostConfig.HostName) { |
| 150 | sshConfig.host = hostConfig.HostName; |
| 151 | } else { |
| 152 | // If no HostName specified, use the host alias itself |
| 153 | sshConfig.host = hostAlias; |
| 154 | } |
| 155 | |
| 156 | // Port (optional, default will be 22) |
| 157 | if (hostConfig.Port) { |
| 158 | sshConfig.port = parseInt(hostConfig.Port, 10); |
| 159 | } |
| 160 | |
| 161 | // User (required) |
| 162 | if (hostConfig.User) { |
| 163 | sshConfig.username = hostConfig.User; |
| 164 | } |
| 165 | |
| 166 | // IdentityFile (private key) |
| 167 | if (hostConfig.IdentityFile) { |
| 168 | // SSH config can have multiple IdentityFile entries, take the first one |
no test coverage detected