mongoc_kms_connect_callback_fn_t

Synopsis

typedef mongoc_stream_t *(*mongoc_kms_connect_callback_fn_t) (
   mongoc_kms_connect_callback_params_t *params);

The type of the function pointer stored by mongoc_kms_connect_callback_t. It opens a transport connection to a KMS endpoint.

The driver calls this function instead of opening a direct TCP connection to the host and port described by params. After the callback returns a connected stream, the driver wraps it with TLS before sending any KMS request.

The primary use case is routing KMS traffic through an HTTP CONNECT proxy: the callback opens a connection to the proxy, performs the CONNECT handshake to establish a tunnel to the KMS endpoint, then returns the tunnel socket.

The callback chooses its own deadline for establishing the connection.

Parameters

Returns

A connected mongoc_stream_t on success, or NULL on failure. Must be an owning pointer as it will be cleaned up with mongoc_stream_destroy().

When returning NULL, call mongoc_kms_connect_callback_params_set_error() and return the NULL it returns.

Example

The following implementation of mongoc_kms_connect_callback_fn_t opens a connection to an HTTP CONNECT proxy, then performs the CONNECT handshake to establish a tunnel to the KMS endpoint:

Excerpt from examples/client-side-encryption-kms-http-proxy.c
// The callback chooses its own deadline for reaching the proxy and completing the CONNECT handshake.
#define PROXY_CONNECT_TIMEOUT_MS 10000

// A mongoc_kms_connect_callback_fn_t implementation. Connects to an HTTP CONNECT proxy and asks it to open a
// tunnel to the KMS endpoint described by `params`. The driver applies TLS to the returned stream itself, so this
// callback only needs to hand back a connected, un-encrypted tunnel.
static mongoc_stream_t *
kms_connect_via_http_proxy(mongoc_kms_connect_callback_params_t *params)
{
   const char *host = mongoc_kms_connect_callback_params_get_host(params);
   const uint16_t port = mongoc_kms_connect_callback_params_get_port(params);
   const kms_proxy_config_t *config = (kms_proxy_config_t *)mongoc_kms_connect_callback_params_get_user_data(params);

   bson_error_t error;
   mongoc_stream_t *proxy_stream =
      tcp_connect(config->proxy_host, config->proxy_port, PROXY_CONNECT_TIMEOUT_MS, &error);
   if (!proxy_stream) {
      return mongoc_kms_connect_callback_params_set_error(params, error.message);
   }

   // Ask the proxy to open a tunnel to the KMS endpoint:
   char req[512];
   int req_len =
      bson_snprintf(req, sizeof(req), "CONNECT %s:%hu HTTP/1.1\r\nHost: %s:%hu\r\n\r\n", host, port, host, port);
   if (mongoc_stream_write(proxy_stream, req, (size_t)req_len, PROXY_CONNECT_TIMEOUT_MS) != req_len) {
      mongoc_stream_destroy(proxy_stream);
      return mongoc_kms_connect_callback_params_set_error(params, "Failed to send CONNECT request to proxy");
   }

   // Read the proxy's response headers, one byte at a time, until the blank line that ends them:
   char resp[1024] = {0};
   size_t resp_len = 0;
   while (resp_len + 1 < sizeof(resp)) {
      ssize_t r = mongoc_stream_read(proxy_stream, resp + resp_len, 1, 1 /* min_bytes */, PROXY_CONNECT_TIMEOUT_MS);
      if (r <= 0) {
         mongoc_stream_destroy(proxy_stream);
         return mongoc_kms_connect_callback_params_set_error(params, "Failed to read CONNECT response from proxy");
      }
      resp_len += (size_t)r;
      if (resp_len >= 4 && 0 == memcmp(resp + resp_len - 4, "\r\n\r\n", 4)) {
         break;
      }
   }

   if (!strstr(resp, " 200 ")) {
      char msg[1024 + 32];
      bson_snprintf(msg, sizeof(msg), "Proxy CONNECT failed: %s", resp);
      mongoc_stream_destroy(proxy_stream);
      return mongoc_kms_connect_callback_params_set_error(params, msg);
   }

   // `proxy_stream` now delivers raw bytes to `host`:`port`. The driver wraps it with TLS before sending any KMS
   // requests.
   return proxy_stream;
}

See the full example, which registers this callback on a mongoc_client_encryption_opts_t, in examples/client-side-encryption-kms-http-proxy.c.