import * as net from 'net';
const kmsConnectCallback: KMSConnectCallback = ({ host, port, signal }) =>
new Promise((resolve, reject) => {
// Open a plain connection to the proxy, not to the KMS host.
const socket = net.connect({ host: 'proxy.example.com', port: 8080, signal });
socket.once('error', reject);
socket.once('connect', () => {
// Ask the proxy to tunnel to the KMS host, then hand the socket back for the driver's TLS.
socket.write(`CONNECT ${host}:${port} HTTP/1.1\r\nHost: ${host}:${port}\r\n\r\n`);
socket.once('data', chunk => {
if (chunk.toString('utf8').startsWith('HTTP/1.1 200')) resolve(socket);
else reject(new Error('Proxy refused the CONNECT request'));
});
});
});
const clientEncryption = new ClientEncryption(keyVaultClient, {
keyVaultNamespace,
kmsProviders,
kmsConnectCallback
});
A callback that establishes the connection to a KMS host.
When provided on
AutoEncryptionOptionsorClientEncryptionOptions, the driver invokes this callback instead of connecting to the KMS host itself, passing the targethostandport. The callback MUST return aDuplexstream connected to the KMS host, either directly or tunneled through a proxy; anet.Socketsatisfies this, as does any otherDuplex. The returned stream is passed to Node.js'tls.connect()as itssocket, and the driver performs the KMS host's TLS handshake over it using the KMS provider's configured TLS options. The callback therefore MUST NOT perform the KMS host's TLS handshake itself, though it MAY use TLS for its own transport, e.g. when connecting to an HTTPS proxy. This enables routing KMS requests through an HTTP proxy via the HTTP CONNECT method.When the operation has a client-side operation timeout (CSOT) configured,
timeoutMSis the remaining time budget in milliseconds; it isundefinedotherwise. Thesignalaborts when the connection attempt exceeds that budget; the callback should stop connecting and reject when it fires.