mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
refactor: remove generating keypair
This commit is contained in:
1 parent
740cbcecb4
commit
307af61726
3 files changed
-56
No files matched your search
@@ -2,7 +2,6 @@ import validator from 'validator'
|
|||||||
import { config as dotenvConfig } from 'dotenv'
|
import { config as dotenvConfig } from 'dotenv'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import { loadOrCreateKeyPair } from './utils/crypto.js'
|
|
||||||
import { get } from './utils/env.js'
|
import { get } from './utils/env.js'
|
||||||
|
|
||||||
// Import dotenv and load ../.env
|
// Import dotenv and load ../.env
|
||||||
@@ -38,11 +37,6 @@ function createConfig() {
|
|||||||
const backendUrl = get('BACKEND_URL', `http://host.docker.internal:${port}`)
|
const backendUrl = get('BACKEND_URL', `http://host.docker.internal:${port}`)
|
||||||
const url = new URL(baseUrl)
|
const url = new URL(baseUrl)
|
||||||
const projectRoot = getProjectRoot()
|
const projectRoot = getProjectRoot()
|
||||||
const keypairPath = get(
|
|
||||||
'KEYPAIR_PATH',
|
|
||||||
path.join(projectRoot, 'config', 'ssh_key')
|
|
||||||
)
|
|
||||||
const keypair = loadOrCreateKeyPair(keypairPath)
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
host,
|
host,
|
||||||
@@ -85,8 +79,6 @@ function createConfig() {
|
|||||||
mountQuest: !isProduction && process.env.MOUNT_QUEST === 'true',
|
mountQuest: !isProduction && process.env.MOUNT_QUEST === 'true',
|
||||||
mountCLI: !isProduction && process.env.MOUNT_CLI === 'true',
|
mountCLI: !isProduction && process.env.MOUNT_CLI === 'true',
|
||||||
hostProjectRoot: get('HOST_PROJECT_ROOT', projectRoot),
|
hostProjectRoot: get('HOST_PROJECT_ROOT', projectRoot),
|
||||||
keypairPath,
|
|
||||||
keypair,
|
|
||||||
},
|
},
|
||||||
|
|
||||||
log: {
|
log: {
|
||||||
|
|||||||
@@ -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 }
|
|
||||||
}
|
|
||||||
@@ -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')
|
|
||||||
})
|
|
||||||
})
|
|
||||||
Reference in new issue
Block a user