-
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.
- Loading branch information
Showing
1 changed file
with
32 additions
and
49 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,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.'); | ||
}); |