Skip to content
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

added tls options #84

Merged
merged 4 commits into from
Mar 3, 2021
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
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ declare namespace customPlugin {
returnAs?: 'JSON' | 'BUFFER' | 'STREAM'
allowedStatusCodes?: number[]
isMiaHeaderInjected?: boolean
cert?: string | Buffer
key?: string | Buffer
ca?: string | Buffer
}
interface BaseServiceResponse extends http.ServerResponse {
headers: object
Expand Down
9 changes: 9 additions & 0 deletions lib/serviceBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ function makeCallAsStream(endUrl, method, body, options) {
headers: options.headers,
timeout: options.timeout,
agent: options.agent,
cert: options.cert,
chiararelandini marked this conversation as resolved.
Show resolved Hide resolved
key: options.key,
ca: options.ca,
}, function onResult(error, res) {
if (error) {
return reject(error)
Expand All @@ -88,6 +91,9 @@ function makeCallAsBuffer(endUrl, method, body, options) {
headers: options.headers,
timeout: options.timeout,
agent: options.agent,
cert: options.cert,
key: options.key,
ca: options.ca,
}, function onResult(error, res, data) {
if (error) {
return reject(error)
Expand All @@ -111,6 +117,9 @@ function makeCallAsJSON(endUrl, method, body, options) {
headers: options.headers,
timeout: options.timeout,
agent: options.agent,
cert: options.cert,
key: options.key,
ca: options.ca,
}, function onResult(error, res, data) {
if (error) {
return reject(error)
Expand Down
22 changes: 22 additions & 0 deletions tests/fixtures/keys/ca.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-----BEGIN CERTIFICATE-----
MIIDuTCCAqGgAwIBAgIUM0TqbZ4hXbGUBgRA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P4Wgk259BuULyvKyv
4PxCIYeYzIJxUFeDn6WNk1F5LsDyMDAJd66sqPXRd4pYOU8gl1lPaqK2o5idV4eq
tDeNN4utwtYt7lklsO2+aMSfI9H+MXcy0o2Kyrmk1aCBWgnN4bkIJkStZfOEV7Fm
O8/tJIni/UU69jpE1/pIjhoHb/6MXG2PWLvzUccSfcZTgDxziofnNgdeI4OC
-----END CERTIFICATE-----
18 changes: 18 additions & 0 deletions tests/fixtures/keys/client.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-----BEGIN CERTIFICATE-----
MIIC8jCCAdoCFCyd0WYjPRpPR/WtmrywgZ1detlbMA0GCSqGSIb3DQEBCwUAMGwx
CzAJBgNVBAYTAlVTMQswCQYDVQQIDAJPSDEPMA0GA1UEBwwGRGF5dG9uMQ8wDQYD
VQQKDAZNeSBJbmMxDzANBgNVBAsMBkRldk9wczEdMBsGCSqGSIb3DQEJARYOcm9v
dEBsb2NhbGhvc3QwIBcNMjEwMzAxMTU0MDQxWhgPMzAwMTA1MDMxNTQwNDFaMIGA
MQswCQYDVQQGEwJVUzELMAkGA1UECAwCT0gxDzANBgNVBAcMBkRheXRvbjEPMA0G
A1UECgwGTXkgSW5jMQ8wDQYDVQQLDAZEZXZPcHMxEjAQBgNVBAMMCWxvY2FsaG9z
dDEdMBsGCSqGSIb3DQEJARYOcm9vdEBsb2NhbGhvc3QwgZ8wDQYJKoZIhvcNAQEB
BQADgY0AMIGJAoGBANYhtrPmxQ1EWBW77m/bpqONDFVhsdac0V7oOJuPybQL9+1u
70vPsdYfEt6hTpCZCP+ob9uIieUlxRl9qhiGibd61/yXaW0HSXiBokIMshf/8otg
IRfbsJJGDN4quGGLUUVLGASweTFiUZno8BHXy3cgNSY1jtWsgluWuiYYuiGxAgMB
AAEwDQYJKoZIhvcNAQELBQADggEBAEDN4jHQyb4AK9FILAEFljKYBdm5B5qfrkqK
vIbxY+Gpy/cTRcpq8UwFHVS6HQ9OkdPYMPlIFq5OmPRqMBk4hJza7WeEvsj7R5fq
Tv82I+UohoIAN0iReWMQOe9jr0nUuEQAbljyDx1OtrCqsa3+sbTk+ddvvmiw+cMF
WD1Koavi4EwRmZ0TIy/462nkzdBIe3Lk2ckTRrAApa6VQGmIf5ZgJSQRJrcqlJz3
teIA+kEyt3D6oHw9qACBOZLxZT5vy6TjhbgKfxaBpgWCCSeKgkoCbvGBZW9POVMB
0361MFfdEGgfV9Rbml6U1c9J2HDzot1/JvLKPqYCMaz1YIDsFfc=
-----END CERTIFICATE-----
16 changes: 16 additions & 0 deletions tests/fixtures/keys/client.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-----BEGIN PRIVATE KEY-----
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBANYhtrPmxQ1EWBW7
7m/bpqONDFVhsdac0V7oOJuPybQL9+1u70vPsdYfEt6hTpCZCP+ob9uIieUlxRl9
qhiGibd61/yXaW0HSXiBokIMshf/8otgIRfbsJJGDN4quGGLUUVLGASweTFiUZno
8BHXy3cgNSY1jtWsgluWuiYYuiGxAgMBAAECgYBfsaFctHzLeQeEbwAXrSnDXX1t
isNXDa3jpBPoMVIKzG54BWObla376tcb9yqNJFcJKdUTXumBJmf3BFnHrBhOvaba
l2tie+QmALMMjALEFxuJqqxsD1LSRqJsXtPqR5I7onEgOL/RezypXR8JHOk48YZO
nNbqhOAmrXhRdz1SAQJBAO0VQI1jSdEQ24J/VkHEwfDR8D27oTFWVyG/7SXyPD/X
4FMhaXBBP+AfPIyVfbsUDkA3CwoibUpXdQq15PK7wwkCQQDnN6Yq1kqWLAKOnapr
e9hQuLBWcCxe+1O+PLE8JzGSRSGooBWa+4HzEu12kprcS0Cks6IrcfAB2xVrFxLu
V8tpAkB8xOR2BDzC0/ZR1SXV8vCGBlZChs4sIt40Gsd5YuQmTTv3Q+wzcYNmNcot
yjajD8j4Ln+W08tkhmZIPrXAsOPRAkEAykfGKN3W3PBdwFUxPd1YDq61Hrn+J45q
4hiI2d5UIOo4GyUKIc99DaH2e3Cg6K8xO6Yn7yg71/PKFilWl4vZWQJAfWwO2SnG
Hh95cZ3PgIiWn7s5JI0qywJawU1ti7+kaig6uCpc6+937zwlDAT1WuYtQImNFyo+
HrDjpCQKlOL5QQ==
-----END PRIVATE KEY-----
18 changes: 18 additions & 0 deletions tests/fixtures/keys/server.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-----BEGIN CERTIFICATE-----
MIIC8jCCAdoCFCyd0WYjPRpPR/WtmrywgZ1detlaMA0GCSqGSIb3DQEBCwUAMGwx
CzAJBgNVBAYTAlVTMQswCQYDVQQIDAJPSDEPMA0GA1UEBwwGRGF5dG9uMQ8wDQYD
VQQKDAZNeSBJbmMxDzANBgNVBAsMBkRldk9wczEdMBsGCSqGSIb3DQEJARYOcm9v
dEBsb2NhbGhvc3QwIBcNMjEwMzAxMTU0MDAyWhgPMzAwMTA1MDMxNTQwMDJaMIGA
MQswCQYDVQQGEwJVUzELMAkGA1UECAwCT0gxDzANBgNVBAcMBkRheXRvbjEPMA0G
A1UECgwGTXkgSW5jMQ8wDQYDVQQLDAZEZXZPcHMxEjAQBgNVBAMMCWxvY2FsaG9z
dDEdMBsGCSqGSIb3DQEJARYOcm9vdEBsb2NhbGhvc3QwgZ8wDQYJKoZIhvcNAQEB
BQADgY0AMIGJAoGBAKMDDLfJalttnIaNWsuBeJ/EQwUcOyHGe/rw9Ofj3mg/xJhJ
9/EnlyMeRsQRPJG51w323Apk7lBEzubLbfnQy1blR8jllpbrBJ+BSXy0Ubif1uNk
PY8fm87Q5VsBigBdS52y/zD7d8kghOauXBWLWkI+Y9BpNdOhnCHzZRvwwPKBAgMB
AAEwDQYJKoZIhvcNAQELBQADggEBAFlWLokSayq+IO/Axx4FaGr9YHdxpmC4RiGR
JuDCI3YJxlBhaPvIByTYncdEQuzpg4hn+atyzQ5pWGEBuoJwCIRIxoXZX1/asHKY
RPrE1VJGmz3V9eGtmzSwYdivEL+oWvk+jrZvnB88cNu27DVQ0IfmhhZGkpYliDEJ
F4hTshPk0AoB1oOGxZbXTIGzSFAXPlSc8DXNIBW4ygEQex1+33wCRoljJ7cD5irc
z9PwBannvt2xxmvwwNLum+Gcxa/2e8PAMWO4olIyXuA18T6+EAG3oSyqUN1E7Uhr
DsrdacknU/GRD/528xXHYTj/e/ofNtCl5xdhDF4MRuZk0hsO4M8=
-----END CERTIFICATE-----
16 changes: 16 additions & 0 deletions tests/fixtures/keys/server.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-----BEGIN PRIVATE KEY-----
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKMDDLfJalttnIaN
WsuBeJ/EQwUcOyHGe/rw9Ofj3mg/xJhJ9/EnlyMeRsQRPJG51w323Apk7lBEzubL
bfnQy1blR8jllpbrBJ+BSXy0Ubif1uNkPY8fm87Q5VsBigBdS52y/zD7d8kghOau
XBWLWkI+Y9BpNdOhnCHzZRvwwPKBAgMBAAECgYBJO1uBTWkMfeetwt8hQZ7xF3iM
WSxvdf3ItGsxqfsGlm85gzFyEzkgu79K4ft19CkayuX7fXBaey8AHC/h5N0UCufc
zj5yG/YXkv6dU+/47XbRP8/tKpfUmE3G86oyj3o7QoShGEGe81SaElGryV2XEpiW
DtvPAxnDdg5tXHIoCQJBANSQ25ZaKVSzXvG4OTpRg8jMuG/dT5RiE/2Fz7AaGDuI
DRXwsqcjpSa4ptm1pq2+4x8KJz+kybEF5QkhChXn4+8CQQDEUhEY81oJggAwxbEm
ucBiHFoGMMypAZEWlTL/n766lNk+2vPYGtRfduLss/G8FYz++QkbxwBifEUFPSvC
MWCPAkB3VuSRowj1TscNOnqWrVEpmRYrZ7rX+cBzY37Yxa25Lob6PGpwv+hmtuCl
9gAgJ71nndGvP5sk0cRmva/l4IztAkBLU5nP0v/HCiR8kj4It9kDTBGWsn60ki9T
VOf+gRKzkI1Surw5PQIjpwAnBlxIaJRy+KbwC4kwQLJS8kep+TwtAkEAhQCNjWGd
F1wB9KzVy4GI035HAEFQ4FoANlrS7eWvxdhd7tqZ/uuS1N/cTwYrM/BAMhw4l3kT
e5d4dyPOWj+dIg==
-----END PRIVATE KEY-----
183 changes: 183 additions & 0 deletions tests/serviceBuilder.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-sync */
/*
* Copyright 2018 Mia srl
*
Expand All @@ -24,6 +25,8 @@ const { Readable } = require('stream')
const http = require('http')
const proxy = require('proxy')
const { HttpProxyAgent } = require('hpagent')
const fs = require('fs')
const https = require('https')

function wait(time) {
return new Promise(resolve => setTimeout(resolve, time))
Expand Down Expand Up @@ -1384,5 +1387,185 @@ tap.test('serviceBuilder', test => {
assert.end()
})

test.test('tls options', async assert => {
nock.enableNetConnect('localhost:3200')
assert.tearDown(() => {
nock.disableNetConnect()
})

const serverCa = fs.readFileSync('tests/fixtures/keys/ca.crt')
const serverKey = fs.readFileSync('tests/fixtures/keys/server.key')
const serverCert = fs.readFileSync('tests/fixtures/keys/server.crt')

const clientKey = fs.readFileSync('tests/fixtures/keys/client.key')
const clientCert = fs.readFileSync('tests/fixtures/keys/client.crt')

async function createServer() {
return new Promise((resolve) => {
const server = https.createServer({
requestCert: true,
rejectUnauthorized: false,
ca: serverCa,
key: serverKey,
cert: serverCert,
})
server.listen(3200, 'localhost', () => {
resolve(server)
})
})
}

assert.test('returnAs: JSON', async assert => {
const server = await createServer()

assert.tearDown(() => {
server.close()
})

server.on('request', (req, res) => {
if (!req.client.authorized) {
res.writeHead(401)
return res.end('{"status": "nok"}')
}

res.end('{"status": "ok"}')
})

const service = serviceBuilder('localhost', {}, {
protocol: 'https',
port: 3200,
chiararelandini marked this conversation as resolved.
Show resolved Hide resolved
})

const response = await service.get('/', {}, {
returnAs: 'JSON',
cert: clientCert,
key: clientKey,
ca: serverCa,
})

assert.equal(response.statusCode, 200)
assert.strictSame(response.payload, { status: 'ok' })

assert.end()
})


assert.test('returnAs: BUFFER', async assert => {
const server = await createServer()

assert.tearDown(() => {
server.close()
})

server.on('request', (req, res) => {
if (!req.client.authorized) {
res.writeHead(401)
res.end('NOK')
}

res.end('OK')
})

const service = serviceBuilder('localhost', {}, {
protocol: 'https',
port: 3200,
})

const response = await service.get('/', {}, {
returnAs: 'BUFFER',
cert: clientCert,
key: clientKey,
ca: serverCa,
})

assert.equal(response.statusCode, 200)
assert.strictSame(response.payload.toString('utf-8'), 'OK')

assert.end()
})


assert.test('returnAs: STREAM', async assert => {
const server = await createServer()

assert.tearDown(() => {
server.close()
})

server.on('request', (req, res) => {
if (!req.client.authorized) {
res.writeHead(401)
res.end(JSON.stringify({ the: 'nok' }))
}

res.end(JSON.stringify({ the: 'response' }))
})

const service = serviceBuilder('localhost', {}, {
protocol: 'https',
port: 3200,
})

const response = await service.get('/', {}, {
returnAs: 'STREAM',
cert: clientCert,
key: clientKey,
ca: serverCa,
})

assert.equal(response.statusCode, 200)
assert.ok(response.headers['content-length'])

await wait(200)

const body = await new Promise(resolve => {
let acc = ''
response.on('data', data => {
acc += data.toString()
})
response.on('end', () => resolve(acc))
})

assert.strictSame(body, JSON.stringify({ the: 'response' }))
assert.end()
})

assert.test('returnAs: JSON - passing options to service initialization', async assert => {
const server = await createServer()

assert.tearDown(() => {
server.close()
})

server.on('request', (req, res) => {
if (!req.client.authorized) {
res.writeHead(401)
return res.end('{"status": "nok"}')
}

res.end('{"status": "ok"}')
})

const service = serviceBuilder('localhost', {}, {
protocol: 'https',
port: 3200,
ca: serverCa,
cert: clientCert,
key: clientKey,
})

const response = await service.get('/', {}, {
returnAs: 'JSON',
})

assert.equal(response.statusCode, 200)
assert.strictSame(response.payload, { status: 'ok' })

assert.end()
})

assert.end()
})

test.end()
})