refactor: remove generating keypair

This commit is contained in:
Justin Lin committed 2025-05-05 19:55:41 +08:00
1 parent 740cbcecb4
commit 307af61726
3 files changed
-56

No files matched your search

-8
View File
@@ -2,7 +2,6 @@ import validator from 'validator'
import { config as dotenvConfig } from 'dotenv'
import path from 'path'
import fs from 'fs'
import { loadOrCreateKeyPair } from './utils/crypto.js'
import { get } from './utils/env.js'
// Import dotenv and load ../.env
@@ -38,11 +37,6 @@ function createConfig() {
const backendUrl = get('BACKEND_URL', `http://host.docker.internal:${port}`)
const url = new URL(baseUrl)
const projectRoot = getProjectRoot()
const keypairPath = get(
'KEYPAIR_PATH',
path.join(projectRoot, 'config', 'ssh_key')
)
const keypair = loadOrCreateKeyPair(keypairPath)
return {
host,
@@ -85,8 +79,6 @@ function createConfig() {
mountQuest: !isProduction && process.env.MOUNT_QUEST === 'true',
mountCLI: !isProduction && process.env.MOUNT_CLI === 'true',
hostProjectRoot: get('HOST_PROJECT_ROOT', projectRoot),
keypairPath,
keypair,
},
log: {
-39
View File
@@ -1,39 +0,0 @@
import fs from 'fs'
import path from 'path'
import ssh2 from 'ssh2'
const {
utils: { generateKeyPairSync },
} = ssh2
type KeyPair = {
privateKey: string
publicKey: string
}
export function loadOrCreateKeyPair(privateKeyPath: string): KeyPair {
const publicKeyPath = `${privateKeyPath}.pub`
if (fs.existsSync(privateKeyPath) && fs.existsSync(publicKeyPath)) {
const privateKey = fs.readFileSync(privateKeyPath, 'utf8')
const publicKey = fs.readFileSync(publicKeyPath, 'utf8')
return { privateKey, publicKey }
}
const dir = path.dirname(privateKeyPath)
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true })
}
const keyPair = generateKeyPair()
fs.writeFileSync(privateKeyPath, keyPair.privateKey, {
mode: 0o600,
})
fs.writeFileSync(publicKeyPath, keyPair.publicKey, {
mode: 0o600,
})
return keyPair
}
export function generateKeyPair(): KeyPair {
const { private: privateKey, public: publicKey } =
generateKeyPairSync('ed25519')
return { privateKey, publicKey }
}
-9
View File
@@ -1,9 +0,0 @@
import { generateKeyPair } from '../../src/utils/crypto'
describe('generateKeyPair', () => {
it('should generate a key pair', () => {
const keyPair = generateKeyPair()
expect(keyPair).toHaveProperty('privateKey')
expect(keyPair).toHaveProperty('publicKey')
})
})