Skip to content

Commit a19ff39

Browse files
authored
openssl service refactoring
* refactoring openssl service * updating openssl service usage + startup logs
1 parent 9865014 commit a19ff39

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

openssl-service.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
const openssl = require('async-openssl')
2+
const fs = require('fs/promises')
23

3-
module.exports = function (command) {
4-
return openssl(command)
4+
module.exports = {
5+
exec (command) {
6+
return openssl(command)
7+
},
8+
async getAndDeleteKeyPair (filePrivKey, filePubKey) {
9+
const [publicKey, privateKey] = await Promise.all([
10+
fs.readFile(filePubKey, 'utf8'),
11+
fs.readFile(filePrivKey, 'utf8')
12+
])
13+
await Promise.all([
14+
fs.unlink(filePubKey),
15+
fs.unlink(filePrivKey)
16+
])
17+
18+
return { publicKey, privateKey }
19+
}
520
}

server.js

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,12 @@
11
const restana = require('restana')
22
const opensslService = require('./openssl-service')
3-
const fs = require('fs/promises')
43
const { v4: uuidv4 } = require('uuid')
54
const assert = require('assert')
65
const { AssertionError } = require('assert')
76

87
const ALLOWED_BITS = [1024, 2048, 3072, 4096]
9-
108
let OPENSSL_VERSION
119

12-
async function getAndDeleteKeyPair (filePrivKey, filePubKey) {
13-
const [publicKey, privateKey] = await Promise.all([
14-
fs.readFile(filePubKey, 'utf8'),
15-
fs.readFile(filePrivKey, 'utf8')
16-
])
17-
await Promise.all([
18-
fs.unlink(filePubKey),
19-
fs.unlink(filePrivKey)
20-
])
21-
22-
return { publicKey, privateKey }
23-
}
24-
2510
const app = restana({})
2611

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

54-
const { privateKey, publicKey } = await getAndDeleteKeyPair(filePrivKey, filePubKey)
39+
const { privateKey, publicKey } = await opensslService.getAndDeleteKeyPair(filePrivKey, filePubKey)
5540

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

7055
res.send({
71-
secret,
56+
secret: secret.toString().trim(),
7257
algorithm,
7358
bytes,
7459
openssl: OPENSSL_VERSION
@@ -85,10 +70,10 @@ app.get('/api/generate/:algorithm', async (req, res) => {
8570
curve = 'secp384r1'
8671
}
8772

88-
await opensslService(`ecparam -genkey -name ${curve} -noout -out ${filePrivKey}`)
89-
await opensslService(`ec -in ${filePrivKey} -pubout -out ${filePubKey}`)
73+
await opensslService.exec(`ecparam -genkey -name ${curve} -noout -out ${filePrivKey}`)
74+
await opensslService.exec(`ec -in ${filePrivKey} -pubout -out ${filePubKey}`)
9075

91-
const { privateKey, publicKey } = await getAndDeleteKeyPair(filePrivKey, filePubKey)
76+
const { privateKey, publicKey } = await opensslService.getAndDeleteKeyPair(filePrivKey, filePubKey)
9277

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

114-
opensslService('version').then(version => {
99+
opensslService.exec('version').then(version => {
115100
OPENSSL_VERSION = version.toString().trim()
116101

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

0 commit comments

Comments
 (0)