MCPcopy Create free account
hub / github.com/bytebase/dbhub / loadPrivateKey

Method loadPrivateKey

src/utils/ssh-tunnel.ts:64–84  ·  view source on GitHub ↗

* Load an SSH private key, supporting both a file path (with symlink resolution) * and base64-encoded key content.

(key: string)

Source from the content-addressed store, hash-verified

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.

Callers 2

establishMethod · 0.95
establishChainMethod · 0.95

Calls 1

resolveSymlinkFunction · 0.85

Tested by

no test coverage detected