Skip to content

openssl service refactoring #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions openssl-service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
const openssl = require('async-openssl')
const fs = require('fs/promises')

module.exports = function (command) {
return openssl(command)
module.exports = {
exec (command) {
return openssl(command)
},
async getAndDeleteKeyPair (filePrivKey, filePubKey) {
const [publicKey, privateKey] = await Promise.all([
fs.readFile(filePubKey, 'utf8'),
fs.readFile(filePrivKey, 'utf8')
])
await Promise.all([
fs.unlink(filePubKey),
fs.unlink(filePrivKey)
])

return { publicKey, privateKey }
}
}
37 changes: 12 additions & 25 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
const restana = require('restana')
const opensslService = require('./openssl-service')
const fs = require('fs/promises')
const { v4: uuidv4 } = require('uuid')
const assert = require('assert')
const { AssertionError } = require('assert')

const ALLOWED_BITS = [1024, 2048, 3072, 4096]

let OPENSSL_VERSION

async function getAndDeleteKeyPair (filePrivKey, filePubKey) {
const [publicKey, privateKey] = await Promise.all([
fs.readFile(filePubKey, 'utf8'),
fs.readFile(filePrivKey, 'utf8')
])
await Promise.all([
fs.unlink(filePubKey),
fs.unlink(filePrivKey)
])

return { publicKey, privateKey }
}

const app = restana({})

app.get(['/api/openssl-version', '/api/health/status'], async (req, res) => {
Expand All @@ -48,10 +33,10 @@ app.get('/api/generate/:algorithm', async (req, res) => {
case 'PS256':
case 'PS384':
case 'PS512': {
await opensslService(`genrsa -out ${filePrivKey} ${bits}`)
await opensslService(`rsa -in ${filePrivKey} -pubout -out ${filePubKey}`)
await opensslService.exec(`genrsa -out ${filePrivKey} ${bits}`)
await opensslService.exec(`rsa -in ${filePrivKey} -pubout -out ${filePubKey}`)

const { privateKey, publicKey } = await getAndDeleteKeyPair(filePrivKey, filePubKey)
const { privateKey, publicKey } = await opensslService.getAndDeleteKeyPair(filePrivKey, filePubKey)

res.send({
privateKey,
Expand All @@ -65,10 +50,10 @@ app.get('/api/generate/:algorithm', async (req, res) => {
case 'HS256':
case 'HS384':
case 'HS512': {
const secret = (await opensslService(`rand -base64 ${bytes}`)).toString().trim()
const secret = await opensslService.exec(`rand -base64 ${bytes}`)

res.send({
secret,
secret: secret.toString().trim(),
algorithm,
bytes,
openssl: OPENSSL_VERSION
Expand All @@ -85,10 +70,10 @@ app.get('/api/generate/:algorithm', async (req, res) => {
curve = 'secp384r1'
}

await opensslService(`ecparam -genkey -name ${curve} -noout -out ${filePrivKey}`)
await opensslService(`ec -in ${filePrivKey} -pubout -out ${filePubKey}`)
await opensslService.exec(`ecparam -genkey -name ${curve} -noout -out ${filePrivKey}`)
await opensslService.exec(`ec -in ${filePrivKey} -pubout -out ${filePubKey}`)

const { privateKey, publicKey } = await getAndDeleteKeyPair(filePrivKey, filePubKey)
const { privateKey, publicKey } = await opensslService.getAndDeleteKeyPair(filePrivKey, filePubKey)

res.send({
privateKey,
Expand All @@ -111,10 +96,12 @@ app.get('/api/generate/:algorithm', async (req, res) => {
}
})

opensslService('version').then(version => {
opensslService.exec('version').then(version => {
OPENSSL_VERSION = version.toString().trim()

app.start(process.env.PORT || 3000)
const PORT = process.env.PORT || 3000
app.start(PORT)
console.log('API successfully running on port: ' + PORT)
}).catch(err => {
console.error('OpenSSL integration failed: ' + err.message)
})