feat: generate key pair using ssh2 for right public key format

This commit is contained in:
Justin Lin committed 2024-10-14 11:34:53 +08:00
1 parent 03b2993b56
commit 261c6fc029
6 files changed
+20 -15

No files matched your search

+2 -2
View File
@@ -1,4 +1,3 @@
import fs from 'fs'
import { Duplex } from 'stream'
import { Client } from 'ssh2'
import Docker from 'dockerode'
@@ -67,6 +66,7 @@ export async function attachContainer(
config.docker.network
].IPAddress
const conn = new Client()
console.log(config.docker.keypair.privateKey)
return new Promise((resolve, reject) => {
conn
@@ -100,7 +100,7 @@ export async function attachContainer(
host: containerIp,
port: 22,
username: 'commander',
privateKey: fs.readFileSync('/home/wancat/.ssh/id_ed25519'),
privateKey: config.docker.keypair.privateKey,
debug: console.log,
})
})
+7 -12
View File
@@ -1,5 +1,9 @@
import fs from 'fs'
import { generateKeyPairSync } from 'crypto'
import ssh2 from 'ssh2'
const {
utils: { generateKeyPairSync },
} = ssh2
type KeyPair = {
privateKey: string
@@ -24,16 +28,7 @@ export function loadOrCreateKeyPair(privateKeyPath: string): KeyPair {
}
export function generateKeyPair(): KeyPair {
const { privateKey, publicKey } = generateKeyPairSync('rsa', {
modulusLength: 2048,
publicKeyEncoding: {
type: 'spki',
format: 'pem',
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
},
})
const { private: privateKey, public: publicKey } =
generateKeyPairSync('ed25519')
return { privateKey, publicKey }
}