|
| 1 | +/* |
| 2 | + * Copyright 2008-present MongoDB, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.mongodb; |
| 18 | + |
| 19 | +import com.mongodb.annotations.NotThreadSafe; |
| 20 | +import com.mongodb.lang.Nullable; |
| 21 | +import org.bson.BsonDocument; |
| 22 | + |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.Map; |
| 25 | + |
| 26 | +import static com.mongodb.assertions.Assertions.notNull; |
| 27 | + |
| 28 | +/** |
| 29 | + * The auto-encryption options. |
| 30 | + * |
| 31 | + * @since 3.11 |
| 32 | + */ |
| 33 | +public final class AutoEncryptionSettings { |
| 34 | + private final MongoClientSettings keyVaultMongoClientSettings; |
| 35 | + private final String keyVaultNamespace; |
| 36 | + private final Map<String, Map<String, Object>> kmsProviders; |
| 37 | + private final Map<String, BsonDocument> namespaceToLocalSchemaDocumentMap; |
| 38 | + private final Map<String, Object> extraOptions; |
| 39 | + private final boolean bypassAutoEncryption; |
| 40 | + |
| 41 | + /** |
| 42 | + * A builder for {@code AutoEncryptionSettings} so that {@code AutoEncryptionSettings} can be immutable, and to support easier |
| 43 | + * construction through chaining. |
| 44 | + */ |
| 45 | + @NotThreadSafe |
| 46 | + public static final class Builder { |
| 47 | + private MongoClientSettings keyVaultMongoClientSettings; |
| 48 | + private String keyVaultNamespace; |
| 49 | + private Map<String, Map<String, Object>> kmsProviders; |
| 50 | + private Map<String, BsonDocument> namespaceToLocalSchemaDocumentMap = Collections.emptyMap(); |
| 51 | + private Map<String, Object> extraOptions = Collections.emptyMap(); |
| 52 | + private boolean bypassAutoEncryption; |
| 53 | + |
| 54 | + /** |
| 55 | + * Sets the key vault settings. |
| 56 | + * |
| 57 | + * @param keyVaultMongoClientSettings the key vault mongo client settings, which may be null. |
| 58 | + * @return this |
| 59 | + */ |
| 60 | + public Builder keyVaultMongoClientSettings(final MongoClientSettings keyVaultMongoClientSettings) { |
| 61 | + this.keyVaultMongoClientSettings = keyVaultMongoClientSettings; |
| 62 | + return this; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Sets the key vault namespace |
| 67 | + * |
| 68 | + * @param keyVaultNamespace the key vault namespace, which may not be null |
| 69 | + * @return this |
| 70 | + */ |
| 71 | + public Builder keyVaultNamespace(final String keyVaultNamespace) { |
| 72 | + this.keyVaultNamespace = notNull("keyVaultNamespace", keyVaultNamespace); |
| 73 | + return this; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Sets the KMS providers map. |
| 78 | + * |
| 79 | + * @param kmsProviders the KMS providers map, which may not be null |
| 80 | + * @return this |
| 81 | + */ |
| 82 | + public Builder kmsProviders(final Map<String, Map<String, Object>> kmsProviders) { |
| 83 | + this.kmsProviders = notNull("kmsProviders", kmsProviders); |
| 84 | + return this; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Sets the map from namespace to local schema document |
| 89 | + * |
| 90 | + * @param namespaceToLocalSchemaDocumentMap the map from namespace to local schema document |
| 91 | + * @return this |
| 92 | + */ |
| 93 | + public Builder namespaceToLocalSchemaDocumentMap(final Map<String, BsonDocument> namespaceToLocalSchemaDocumentMap) { |
| 94 | + this.namespaceToLocalSchemaDocumentMap = notNull("namespaceToLocalSchemaDocumentMap", namespaceToLocalSchemaDocumentMap); |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Sets the extra options. |
| 100 | + * |
| 101 | + * @param extraOptions the extra options, which may not be null |
| 102 | + * @return this |
| 103 | + */ |
| 104 | + public Builder extraOptions(final Map<String, Object> extraOptions) { |
| 105 | + this.extraOptions = notNull("extraOptions", extraOptions); |
| 106 | + return this; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Sets whether auto-encryption should be bypassed. |
| 111 | + * |
| 112 | + * @param bypassAutoEncryption whether auto-encryption should be bypassed |
| 113 | + * @return this |
| 114 | + */ |
| 115 | + public Builder bypassAutoEncryption(final boolean bypassAutoEncryption) { |
| 116 | + this.bypassAutoEncryption = bypassAutoEncryption; |
| 117 | + return this; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Build an instance of {@code AutoEncryptionSettings}. |
| 122 | + * |
| 123 | + * @return the settings from this builder |
| 124 | + */ |
| 125 | + public AutoEncryptionSettings build() { |
| 126 | + return new AutoEncryptionSettings(this); |
| 127 | + } |
| 128 | + |
| 129 | + private Builder() { |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Convenience method to create a Builder. |
| 135 | + * |
| 136 | + * @return a builder |
| 137 | + */ |
| 138 | + public static Builder builder() { |
| 139 | + return new Builder(); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Gets the key vault settings. |
| 144 | + * |
| 145 | + * @return the key vault settings, which may be null to indicate that the same {@code MongoClient} should be used to access the key |
| 146 | + * vault collection as is used for the rest of the application. |
| 147 | + */ |
| 148 | + @Nullable |
| 149 | + public MongoClientSettings getKeyVaultMongoClientSettings() { |
| 150 | + return keyVaultMongoClientSettings; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Gets the key vault namespace. |
| 155 | + * |
| 156 | + * @return the key vault namespace, which may not be null |
| 157 | + */ |
| 158 | + public String getKeyVaultNamespace() { |
| 159 | + return keyVaultNamespace; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Gets the map of KMS provider properties |
| 164 | + * |
| 165 | + * @return map of KMS provider properties |
| 166 | + */ |
| 167 | + public Map<String, Map<String, Object>> getKmsProviders() { |
| 168 | + return kmsProviders; |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Gets the map of namespace to local JSON schema |
| 173 | + * |
| 174 | + * @return map of namespace to local JSON schema |
| 175 | + */ |
| 176 | + public Map<String, BsonDocument> getNamespaceToLocalSchemaDocumentMap() { |
| 177 | + return namespaceToLocalSchemaDocumentMap; |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Gets the extra options that control the behavior of auto-encryption components. |
| 182 | + * |
| 183 | + * @return the extra options |
| 184 | + */ |
| 185 | + public Map<String, Object> getExtraOptions() { |
| 186 | + return extraOptions; |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Gets whether auto-encryption should be bypassed. Even when this option is true, auto-decryption is still enabled. |
| 191 | + * |
| 192 | + * @return true if auto-encryption should be bypassed |
| 193 | + */ |
| 194 | + public boolean isBypassAutoEncryption() { |
| 195 | + return bypassAutoEncryption; |
| 196 | + } |
| 197 | + |
| 198 | + private AutoEncryptionSettings(final Builder builder) { |
| 199 | + this.keyVaultMongoClientSettings = builder.keyVaultMongoClientSettings; |
| 200 | + this.keyVaultNamespace = notNull("keyVaultNamespace", builder.keyVaultNamespace); |
| 201 | + this.kmsProviders = notNull("kmsProviders", builder.kmsProviders); |
| 202 | + this.namespaceToLocalSchemaDocumentMap = notNull("namespaceToLocalSchemaDocumentMap", builder.namespaceToLocalSchemaDocumentMap); |
| 203 | + this.extraOptions = notNull("extraOptions", builder.extraOptions); |
| 204 | + this.bypassAutoEncryption = builder.bypassAutoEncryption; |
| 205 | + } |
| 206 | +} |
0 commit comments