Skip to content

Commit

Permalink
Merge pull request #56 from vgs-samples/420neshot-patch-1
Browse files Browse the repository at this point in the history
Update inbound-integration.js
  • Loading branch information
mottersheadt authored Jul 4, 2024
2 parents 381d04d + 8a8a6e8 commit b97995d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 44 deletions.
28 changes: 16 additions & 12 deletions content-by-language/node/inbound-integration.js
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.'));
71 changes: 39 additions & 32 deletions content-by-language/node/outbound-integration.js
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.');
});

0 comments on commit b97995d

Please sign in to comment.