-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from vgs-samples/420neshot-patch-1
Update inbound-integration.js
- Loading branch information
Showing
2 changed files
with
55 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,25 @@ | ||
const fetch = require('node-fetch'); | ||
const axios = require('axios'); | ||
|
||
const instance = axios.create({ | ||
baseURL: '{VAULT_URL}', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
async function getData() { | ||
let result; | ||
|
||
try { | ||
result = await fetch('{VAULT_URL}/post', { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ | ||
const response = await instance.post('/post', { | ||
account_number: 'ACC00000000000000000', | ||
}), | ||
}); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
|
||
return await result.text(); | ||
console.log('Response data:', response.data); | ||
return response.data; | ||
} catch (error) { | ||
console.error('Error caught during request:', error.message); | ||
throw error; | ||
} | ||
} | ||
|
||
getData().then(response => console.log(response)); | ||
getData().then(response => console.log('Post request via proxy succeeded.')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,44 @@ | ||
const fs = require('fs') | ||
const tls = require('tls') | ||
const { curly } = require('node-libcurl') | ||
const {EOL} = require('os'); | ||
// The NODE_EXTRA_CA_CERTS environment variable needs to be set before running the code | ||
// NODE_EXTRA_CA_CERTS=<path_to_sandbox.pem> node outbound.js | ||
|
||
const certFilePath = '{CERT_LOCATION}' | ||
const ca = '/tmp/vgs-outbound-proxy-ca.pem' | ||
const tlsData = tls.rootCertificates.join(EOL) | ||
const vgsSelfSigned = fs.readFileSync(certFilePath).toString('utf-8') | ||
const systemCaAndVgsCa = tlsData + EOL + vgsSelfSigned; | ||
fs.writeFileSync(ca, systemCaAndVgsCa) | ||
const axios = require('axios'); | ||
const tunnel = require('tunnel'); | ||
|
||
const proxyOptions = { | ||
servername: '{VAULT_HOST}', | ||
host: '{VAULT_HOST}', | ||
port: {SECURE_PORT}, | ||
proxyAuth: '{ACCESS_CREDENTIALS}', | ||
}; | ||
|
||
const agent = tunnel.httpsOverHttps({ | ||
proxy: proxyOptions, | ||
}); | ||
|
||
const instance = axios.create({ | ||
baseURL: '{VGS_SAMPLE_ECHO_SERVER}', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
httpsAgent: agent, | ||
}); | ||
|
||
async function run() { | ||
return curly.post('{VGS_SAMPLE_ECHO_SERVER}/post', { | ||
postFields: JSON.stringify({ account_number: '{ALIAS}' }), | ||
httpHeader: ['Content-type: application/json'], | ||
caInfo: ca, | ||
proxy: 'https://{ACCESS_CREDENTIALS}@{VAULT_HOST}:{SECURE_PORT}', | ||
verbose: true, | ||
}) | ||
console.log('Sending POST request via proxy...'); | ||
try { | ||
const response = await instance.post('/post', { | ||
account_number: '{ALIAS}', | ||
}); | ||
console.log('Response data:', response.data); | ||
return response.data; | ||
} catch (error) { | ||
console.error('Error caught during request:', error.message); | ||
throw error; | ||
} | ||
} | ||
|
||
run() | ||
.then(({ data, statusCode, headers }) => | ||
console.log( | ||
require('util').inspect( | ||
{ | ||
data: JSON.parse(data.data), | ||
statusCode, | ||
headers, | ||
}, | ||
null, | ||
4, | ||
), | ||
), | ||
) | ||
.catch((error) => console.error(`Something went wrong`, { error })) | ||
run().then(() => { | ||
console.log('Post request via proxy succeeded.'); | ||
}).catch(() => { | ||
console.log('Post request via proxy failed.'); | ||
}); |