* Load an SSH private key, supporting both a file path (with symlink resolution) * and base64-encoded key content.
(key: string)
| 62 | * and base64-encoded key content. |
| 63 | */ |
| 64 | private loadPrivateKey(key: string): Buffer { |
| 65 | try { |
| 66 | const resolvedKeyPath = resolveSymlink(key); |
| 67 | return readFileSync(resolvedKeyPath); |
| 68 | } catch { |
| 69 | // Not a readable file — try base64 decode |
| 70 | try { |
| 71 | const decoded = Buffer.from(key, 'base64'); |
| 72 | const text = decoded.toString('utf8'); |
| 73 | if (text.includes('PRIVATE KEY')) { |
| 74 | return decoded; |
| 75 | } |
| 76 | throw new Error('SSH key is neither a valid file path nor a base64-encoded private key'); |
| 77 | } catch (decodeError) { |
| 78 | if (decodeError instanceof Error && decodeError.message.includes('neither a valid file path')) { |
| 79 | throw decodeError; |
| 80 | } |
| 81 | throw new Error('SSH key is neither a valid file path nor a base64-encoded private key'); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Establish a chain of SSH connections through jump hosts. |
no test coverage detected