mongoc_client_encryption_opts_set_kms_connect_callback ()

Added in version 2.6.0.

Synopsis

void
mongoc_client_encryption_opts_set_kms_connect_callback (
  mongoc_client_encryption_opts_t *opts,
  const mongoc_kms_connect_callback_t *callback);

Set a callback that is invoked whenever the driver needs to open a network connection to a KMS server. The callback is responsible for establishing the transport-layer connection (plain TCP or one tunnelled through an HTTP proxy) and returning the raw socket as a mongoc_stream_t. After the callback returns, the driver wraps that stream with TLS before sending any KMS requests.

This is the primary extension point for routing KMS traffic through an HTTP CONNECT proxy.

Parameters

  • opts - The options object to update.

  • callback - The connect callback to set. A copy is stored in opts, so callback may be destroyed immediately after this call. May be NULL to clear a previously set callback. Refer to: mongoc_kms_connect_callback_t.

Example: Routing KMS Traffic Through an HTTP CONNECT Proxy

Registers a mongoc_kms_connect_callback_t on a mongoc_client_encryption_opts_t so that all KMS requests made by the resulting mongoc_client_encryption_t are tunnelled through an HTTP CONNECT proxy:

Excerpt from examples/client-side-encryption-kms-http-proxy.c
// Configure the HTTP CONNECT proxy that KMS traffic is routed through:
kms_proxy_config_t proxy_config = {
   .proxy_host = getenv_or("MONGOC_KMS_PROXY_HOST", "127.0.0.1"),
   .proxy_port = (uint16_t)atoi(getenv_or("MONGOC_KMS_PROXY_PORT", "3128")),
};
mongoc_kms_connect_callback_t *connect_callback =
   mongoc_kms_connect_callback_new_with_user_data(kms_connect_via_http_proxy, &proxy_config);

// Create ClientEncryption object:
mongoc_client_encryption_t *client_encryption;
{
   mongoc_client_encryption_opts_t *ce_opts = mongoc_client_encryption_opts_new();
   mongoc_client_encryption_opts_set_kms_providers(ce_opts, &kms_providers);
   mongoc_client_encryption_opts_set_keyvault_namespace(ce_opts, keyvault_db_name, keyvault_coll_name);
   mongoc_client_encryption_opts_set_keyvault_client(ce_opts, client);
   // Route all KMS network traffic through the HTTP CONNECT proxy:
   mongoc_client_encryption_opts_set_kms_connect_callback(ce_opts, connect_callback);
   client_encryption = mongoc_client_encryption_new(ce_opts, &error);
   if (!client_encryption) {
      FAIL("Failed to create ClientEncryption: %s", error.message);
   }
   mongoc_client_encryption_opts_destroy(ce_opts);
}

// The callback object is copied by `mongoc_client_encryption_opts_set_kms_connect_callback`, so it may be
// destroyed as soon as it has been set on every options object that needs it. `proxy_config` must remain valid
// for as long as `client_encryption` may invoke the callback, since only a pointer to it was stored.
mongoc_kms_connect_callback_destroy(connect_callback);

See the full example, including the implementation of kms_connect_via_http_proxy that performs the CONNECT handshake, in examples/client-side-encryption-kms-http-proxy.c.