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
View File
@@ -1,3 +1,5 @@
config
# Logs # Logs
logs logs
*.log *.log
+1 -1
View File
@@ -26,7 +26,7 @@ ENV USERNAME=commander
RUN adduser --disabled-password --gecos '' --shell /bin/zsh ${USERNAME} RUN adduser --disabled-password --gecos '' --shell /bin/zsh ${USERNAME}
COPY ./quests/sshd_config /etc/ssh/sshd_config COPY ./quests/sshd_config /etc/ssh/sshd_config
COPY ./ssh_pubkey /home/${USERNAME}/.ssh/authorized_keys COPY ./config/ssh_key.pub /home/${USERNAME}/.ssh/authorized_keys
RUN chmod 600 /home/${USERNAME}/.ssh/authorized_keys \ RUN chmod 600 /home/${USERNAME}/.ssh/authorized_keys \
&& chmod 700 /home/${USERNAME}/.ssh \ && chmod 700 /home/${USERNAME}/.ssh \
&& chown -R ${USERNAME}:${USERNAME} /home/${USERNAME}/.ssh && chown -R ${USERNAME}:${USERNAME} /home/${USERNAME}/.ssh
+1
View File
@@ -62,6 +62,7 @@
"@types/passport-local": "^1.0.38", "@types/passport-local": "^1.0.38",
"@types/passport-oauth2": "^1.4.15", "@types/passport-oauth2": "^1.4.15",
"@types/source-map-support": "^0", "@types/source-map-support": "^0",
"@types/ssh2": "^1.15.1",
"@types/swagger-ui-express": "^4.1.6", "@types/swagger-ui-express": "^4.1.6",
"@types/validator": "^13.11.9", "@types/validator": "^13.11.9",
"concurrently": "^8.2.2", "concurrently": "^8.2.2",
+2 -2
View File
@@ -1,4 +1,3 @@
import fs from 'fs'
import { Duplex } from 'stream' import { Duplex } from 'stream'
import { Client } from 'ssh2' import { Client } from 'ssh2'
import Docker from 'dockerode' import Docker from 'dockerode'
@@ -67,6 +66,7 @@ export async function attachContainer(
config.docker.network config.docker.network
].IPAddress ].IPAddress
const conn = new Client() const conn = new Client()
console.log(config.docker.keypair.privateKey)
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
conn conn
@@ -100,7 +100,7 @@ export async function attachContainer(
host: containerIp, host: containerIp,
port: 22, port: 22,
username: 'commander', username: 'commander',
privateKey: fs.readFileSync('/home/wancat/.ssh/id_ed25519'), privateKey: config.docker.keypair.privateKey,
debug: console.log, debug: console.log,
}) })
}) })
+7 -12
View File
@@ -1,5 +1,9 @@
import fs from 'fs' import fs from 'fs'
import { generateKeyPairSync } from 'crypto' import ssh2 from 'ssh2'
const {
utils: { generateKeyPairSync },
} = ssh2
type KeyPair = { type KeyPair = {
privateKey: string privateKey: string
@@ -24,16 +28,7 @@ export function loadOrCreateKeyPair(privateKeyPath: string): KeyPair {
} }
export function generateKeyPair(): KeyPair { export function generateKeyPair(): KeyPair {
const { privateKey, publicKey } = generateKeyPairSync('rsa', { const { private: privateKey, public: publicKey } =
modulusLength: 2048, generateKeyPairSync('ed25519')
publicKeyEncoding: {
type: 'spki',
format: 'pem',
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
},
})
return { privateKey, publicKey } return { privateKey, publicKey }
} }
+7
View File
@@ -2067,6 +2067,13 @@
dependencies: dependencies:
"@types/node" "^18.11.18" "@types/node" "^18.11.18"
"@types/ssh2@^1.15.1":
version "1.15.1"
resolved "https://registry.yarnpkg.com/@types/ssh2/-/ssh2-1.15.1.tgz#4db4b6864abca09eb299fe5354fa591add412223"
integrity sha512-ZIbEqKAsi5gj35y4P4vkJYly642wIbY6PqoN0xiyQGshKUGXR9WQjF/iF9mXBQ8uBKy3ezfsCkcoHKhd0BzuDA==
dependencies:
"@types/node" "^18.11.18"
"@types/stack-utils@^2.0.0": "@types/stack-utils@^2.0.0":
version "2.0.3" version "2.0.3"
resolved "https://registry.npmjs.org/@types/stack-utils/-/stack-utils-2.0.3.tgz" resolved "https://registry.npmjs.org/@types/stack-utils/-/stack-utils-2.0.3.tgz"