Package com.mongodb

Class AutoEncryptionSettings


public final class AutoEncryptionSettings extends Object
The client-side automatic encryption settings. Client side encryption enables an application to specify what fields in a collection must be encrypted, and the driver automatically encrypts commands sent to MongoDB and decrypts responses.

Automatic encryption is an enterprise only feature that only applies to operations on a collection. Automatic encryption is not supported for operations on a database or view and will result in error. To bypass automatic encryption, set bypassAutoEncryption=true in AutoEncryptionSettings.

Explicit encryption/decryption and automatic decryption is a community feature, enabled with the new com.mongodb.client.vault.ClientEncryption type.

A MongoClient configured with bypassAutoEncryption=true will still automatically decrypt.

If automatic encryption fails on an operation, use a MongoClient configured with bypassAutoEncryption=true and use ClientEncryption#encrypt to manually encrypt values.

Enabling client side encryption reduces the maximum document and message size (using a maxBsonObjectSize of 2MiB and maxMessageSizeBytes of 6MB) and may have a negative performance impact.

Automatic encryption requires the authenticated user to have the listCollections privilege action.

  • Method Details

    • builder

      public static AutoEncryptionSettings.Builder builder()
      Convenience method to create a Builder.
      a builder
    • getKeyVaultMongoClientSettings

      @Nullable public MongoClientSettings getKeyVaultMongoClientSettings()
      Gets the key vault settings.

      The key vault collection is assumed to reside on the same MongoDB cluster as the encrypted collections. But the optional keyVaultMongoClientSettings can be used to route data key queries to a separate MongoDB cluster, or the same cluster but using a different credential.

      the key vault settings, which may be null to indicate that the same MongoClient should be used to access the key vault collection as is used for the rest of the application.
    • getKeyVaultNamespace

      public String getKeyVaultNamespace()
      Gets the key vault namespace.

      The key vault namespace refers to a collection that contains all data keys used for encryption and decryption (aka the key vault collection). Data keys are stored as documents in a special MongoDB collection. Data keys are protected with encryption by a KMS provider (AWS, Azure, GCP KMS or a local master key).

      the key vault namespace, which may not be null
    • getKmsProviders

      public Map<String,Map<String,Object>> getKmsProviders()
      Gets the map of KMS provider properties.

      Multiple KMS providers may be specified. The following KMS providers are supported: "aws", "azure", "gcp" and "local". The kmsProviders map values differ by provider:

      For "aws", the properties are:

      • accessKeyId: a String, the AWS access key identifier
      • secretAccessKey: a String, the AWS secret access key
      • sessionToken: an optional String, the AWS session token

      For "azure", the properties are:

      • tenantId: a String, the tenantId that identifies the organization for the account.
      • clientId: a String, the clientId to authenticate a registered application.
      • clientSecret: a String, the client secret to authenticate a registered application.
      • identityPlatformEndpoint: optional String, a host with optional port. e.g. "" or "". Generally used for private Azure instances.

      For "gcp", the properties are:

      • email: a String, the service account email to authenticate.
      • privateKey: a String or byte[], the encoded PKCS#8 encrypted key
      • endpoint: optional String, a host with optional port. e.g. "" or "".

      For "kmip", the properties are:

      • endpoint: a String, the endpoint as a host with required port. e.g. "".

      For "local", the properties are:

      • key: byte[] of length 96, the local key
      map of KMS provider properties
    • getKmsProviderSslContextMap

      public Map<String,SSLContext> getKmsProviderSslContextMap()
      Gets the KMS provider to SSLContext map.

      If a KMS provider is mapped to a non-null SSLContext, the context will be used to establish a TLS connection to the KMS. Otherwise, the default context will be used.

      the KMS provider to SSLContext map
    • getSchemaMap

      public Map<String,BsonDocument> getSchemaMap()
      Gets the map of namespace to local JSON schema.

      Automatic encryption is configured with an "encrypt" field in a collection's JSONSchema. By default, a collection's JSONSchema is periodically polled with the listCollections command. But a JSONSchema may be specified locally with the schemaMap option.

      The key into the map is the full namespace of the collection, which is &lt;database name>.&lt;collection name>. For example, if the database name is "test" and the collection name is "users", then the namesspace is "test.users".

      Supplying a schemaMap provides more security than relying on JSON Schemas obtained from the server. It protects against a malicious server advertising a false JSON Schema, which could trick the client into sending unencrypted data that should be encrypted.

      Schemas supplied in the schemaMap only apply to configuring automatic encryption for client side encryption. Other validation rules in the JSON schema will not be enforced by the driver and will result in an error.

      map of namespace to local JSON schema
    • getExtraOptions

      public Map<String,Object> getExtraOptions()
      Gets the extra options that control the behavior of auto-encryption components.

      The extraOptions currently only relate to the mongocryptd process. The following options keys are supported:

      • mongocryptdURI: a String which defaults to "mongodb://%2Fvar%2Fmongocryptd.sock" if domain sockets are available or "mongodb://localhost:27020" otherwise.
      • mongocryptdBypassSpawn: a boolean which defaults to false. If true, the driver will not attempt to automatically spawn a mongocryptd process
      • mongocryptdSpawnPath: specifies the full path to the mongocryptd executable. By default the driver spawns mongocryptd from the system path.
      • mongocryptdSpawnArgs: Used to control the behavior of mongocryptd when the driver spawns it. By default, the driver spawns mongocryptd with the single command line argument "--idleShutdownTimeoutSecs=60"
      the extra options map
    • isBypassAutoEncryption

      public boolean isBypassAutoEncryption()
      Gets whether auto-encryption should be bypassed. Even when this option is true, auto-decryption is still enabled.

      This option is useful for cases where the driver throws an exception because it is unable to prove that the command does not contain any fields that should be automatically encrypted, but the application is able to determine that it does not. For these cases, the application can construct a MongoClient with AutoEncryptionSettings with bypassAutoEncryption enabled.

      true if auto-encryption should be bypassed