Skip to content

Commit

Permalink
Update outbound-integration.js
Browse files Browse the repository at this point in the history
  • Loading branch information
420neshot authored Jul 2, 2024
1 parent 9c65904 commit 885bde3
Showing 1 changed file with 32 additions and 49 deletions.
81 changes: 32 additions & 49 deletions content-by-language/node/outbound-integration.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,41 @@
const fs = require('fs');
const tls = require('tls');
const axios = require('axios');
const { EOL } = require('os');
const https = require('https');
const tunnel = require('tunnel');

// Paths to certificate files
const certFilePath = '{CERT_LOCATION}';
const caFilePath = '/tmp/vgs-outbound-proxy-ca.pem';
const proxyOptions = {
servername: '{VAULT_HOST}',
host: '{VAULT_HOST}',
port: {SECURE_PORT},
proxyAuth: '{ACCESS_CREDENTIALS}',
};

// Read and combine system root certificates with the VGS self-signed certificate
const tlsData = tls.rootCertificates.join(EOL);
const vgsSelfSigned = fs.readFileSync(certFilePath).toString('utf-8');
const combinedCa = tlsData + EOL + vgsSelfSigned;
fs.writeFileSync(caFilePath, combinedCa);
const agent = tunnel.httpsOverHttps({
proxy: proxyOptions,
});

// Create an HTTPS agent with the combined CA certificates
const httpsAgent = new https.Agent({
ca: combinedCa,
const instance = axios.create({
baseURL: '{VGS_SAMPLE_ECHO_SERVER}',
headers: {
'Content-Type': 'application/json',
},
httpsAgent: agent,
});

async function run() {
try {
const response = await axios.post(
'{VGS_SAMPLE_ECHO_SERVER}/post',
{ account_number: '{ALIAS}' },
{
headers: { 'Content-Type': 'application/json' },
proxy: {
protocol: 'https',
host: '{VAULT_HOST}',
port: {SECURE_PORT},
auth: {
username: '{USERNAME}',
password: '{PASSWORD}',
},
},
httpsAgent: httpsAgent,
}
);

console.log(
require('util').inspect(
{
data: response.data,
statusCode: response.status,
headers: response.headers,
},
null,
4
)
);
} catch (error) {
console.error(`Something went wrong`, { error });
}
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();
run().then(() => {
console.log('Post request via proxy succeeded.');
}).catch(() => {
console.log('Post request via proxy failed.');
});

0 comments on commit 885bde3

Please sign in to comment.