16
16
17
17
package com .mongodb .internal .capi ;
18
18
19
+ import com .mongodb .AutoEncryptionSettings ;
19
20
import com .mongodb .AwsCredential ;
20
- import com .mongodb .Block ;
21
+ import com .mongodb .ClientEncryptionSettings ;
21
22
import com .mongodb .ConnectionString ;
22
23
import com .mongodb .MongoClientException ;
23
24
import com .mongodb .MongoClientSettings ;
24
25
import com .mongodb .MongoConfigurationException ;
25
- import com .mongodb .connection .ClusterSettings ;
26
- import com .mongodb .connection .SocketSettings ;
27
26
import com .mongodb .crypt .capi .MongoCryptOptions ;
28
27
import com .mongodb .internal .authentication .AwsCredentialHelper ;
29
28
import com .mongodb .lang .Nullable ;
41
40
import java .util .function .Supplier ;
42
41
43
42
import static java .lang .String .format ;
43
+ import static java .util .Collections .emptyList ;
44
+ import static java .util .Collections .emptyMap ;
45
+ import static java .util .Collections .singletonList ;
44
46
45
47
public final class MongoCryptHelper {
46
48
47
- public static MongoCryptOptions createMongoCryptOptions (final Map < String , Map < String , Object >> kmsProviders ) {
48
- return createMongoCryptOptions (kmsProviders , null , null , false );
49
+ public static MongoCryptOptions createMongoCryptOptions (final ClientEncryptionSettings settings ) {
50
+ return createMongoCryptOptions (settings . getKmsProviders (), false , emptyList (), emptyMap (), null , null );
49
51
}
50
52
51
- public static MongoCryptOptions createMongoCryptOptions (final Map <String , Map <String , Object >> kmsProviders ,
53
+ public static MongoCryptOptions createMongoCryptOptions (final AutoEncryptionSettings settings ) {
54
+ return createMongoCryptOptions (
55
+ settings .getKmsProviders (),
56
+ settings .isBypassQueryAnalysis (),
57
+ settings .isBypassAutoEncryption () ? emptyList () : singletonList ("$SYSTEM" ),
58
+ settings .getExtraOptions (),
59
+ settings .getSchemaMap (),
60
+ settings .getEncryptedFieldsMap ());
61
+ }
62
+
63
+ private static MongoCryptOptions createMongoCryptOptions (
64
+ final Map <String , Map <String , Object >> kmsProviders ,
65
+ final boolean bypassQueryAnalysis ,
66
+ final List <String > searchPaths ,
67
+ @ Nullable final Map <String , Object > extraOptions ,
52
68
@ Nullable final Map <String , BsonDocument > localSchemaMap ,
53
- @ Nullable final Map <String , BsonDocument > encryptedFieldsMap ,
54
- final boolean bypassQueryAnalysis ) {
69
+ @ Nullable final Map <String , BsonDocument > encryptedFieldsMap ) {
55
70
MongoCryptOptions .Builder mongoCryptOptionsBuilder = MongoCryptOptions .builder ();
56
-
57
- BsonDocument bsonKmsProviders = getKmsProvidersAsBsonDocument ( kmsProviders );
58
- mongoCryptOptionsBuilder .kmsProviderOptions ( bsonKmsProviders );
59
- mongoCryptOptionsBuilder .needsKmsCredentialsStateEnabled ( true );
71
+ mongoCryptOptionsBuilder . kmsProviderOptions ( getKmsProvidersAsBsonDocument ( kmsProviders ));
72
+ mongoCryptOptionsBuilder . bypassQueryAnalysis ( bypassQueryAnalysis );
73
+ mongoCryptOptionsBuilder .searchPaths ( searchPaths );
74
+ mongoCryptOptionsBuilder .extraOptions ( toBsonDocument ( extraOptions ) );
60
75
mongoCryptOptionsBuilder .localSchemaMap (localSchemaMap );
61
76
mongoCryptOptionsBuilder .encryptedFieldsMap (encryptedFieldsMap );
62
- mongoCryptOptionsBuilder .bypassQueryAnalysis ( bypassQueryAnalysis );
77
+ mongoCryptOptionsBuilder .needsKmsCredentialsStateEnabled ( true );
63
78
return mongoCryptOptionsBuilder .build ();
64
79
}
65
80
public static BsonDocument fetchCredentials (final Map <String , Map <String , Object >> kmsProviders ,
@@ -82,7 +97,7 @@ public static BsonDocument fetchCredentials(final Map<String, Map<String, Object
82
97
+ " The returned value is %s." ,
83
98
kmsProviderName , kmsProviderCredential == null ? "null" : "empty" ));
84
99
}
85
- addToKmsProviderDocument ( kmsProvidersDocument , kmsProviderName , kmsProviderCredential );
100
+ kmsProvidersDocument . put ( kmsProviderName , toBsonDocument ( kmsProviderCredential ) );
86
101
}
87
102
if (kmsProvidersDocument .containsKey ("aws" ) && kmsProvidersDocument .get ("aws" ).asDocument ().isEmpty ()) {
88
103
AwsCredential awsCredential = AwsCredentialHelper .obtainFromEnvironment ();
@@ -101,20 +116,20 @@ public static BsonDocument fetchCredentials(final Map<String, Map<String, Object
101
116
102
117
private static BsonDocument getKmsProvidersAsBsonDocument (final Map <String , Map <String , Object >> kmsProviders ) {
103
118
BsonDocument bsonKmsProviders = new BsonDocument ();
104
- for (Map .Entry <String , Map <String , Object >> entry : kmsProviders .entrySet ()) {
105
- addToKmsProviderDocument (bsonKmsProviders , entry .getKey (), entry .getValue ());
106
- }
119
+ kmsProviders .forEach ((k , v ) -> bsonKmsProviders .put (k , toBsonDocument (v )));
107
120
return bsonKmsProviders ;
108
121
}
109
122
110
- private static void addToKmsProviderDocument (final BsonDocument kmsProvidersDocument , final String kmsProvider ,
111
- final Map <String , Object > kmsProviderCredential ) {
112
- kmsProvidersDocument .put (kmsProvider , new BsonDocumentWrapper <>(new Document (kmsProviderCredential ), new DocumentCodec ()));
123
+ private static BsonDocument toBsonDocument (final Map <String , Object > optionsMap ) {
124
+ if (optionsMap == null ) {
125
+ return new BsonDocument ();
126
+ }
127
+ return new BsonDocumentWrapper <>(new Document (optionsMap ), new DocumentCodec ());
113
128
}
114
129
115
130
@ SuppressWarnings ("unchecked" )
116
131
public static List <String > createMongocryptdSpawnArgs (final Map <String , Object > options ) {
117
- List <String > spawnArgs = new ArrayList <String >();
132
+ List <String > spawnArgs = new ArrayList <>();
118
133
119
134
String path = options .containsKey ("mongocryptdSpawnPath" )
120
135
? (String ) options .get ("mongocryptdSpawnPath" )
@@ -135,18 +150,10 @@ public static List<String> createMongocryptdSpawnArgs(final Map<String, Object>
135
150
public static MongoClientSettings createMongocryptdClientSettings (final String connectionString ) {
136
151
137
152
return MongoClientSettings .builder ()
138
- .applyToClusterSettings (new Block <ClusterSettings .Builder >() {
139
- @ Override
140
- public void apply (final ClusterSettings .Builder builder ) {
141
- builder .serverSelectionTimeout (10 , TimeUnit .SECONDS );
142
- }
143
- })
144
- .applyToSocketSettings (new Block <SocketSettings .Builder >() {
145
- @ Override
146
- public void apply (final SocketSettings .Builder builder ) {
147
- builder .readTimeout (10 , TimeUnit .SECONDS );
148
- builder .connectTimeout (10 , TimeUnit .SECONDS );
149
- }
153
+ .applyToClusterSettings (builder -> builder .serverSelectionTimeout (10 , TimeUnit .SECONDS ))
154
+ .applyToSocketSettings (builder -> {
155
+ builder .readTimeout (10 , TimeUnit .SECONDS );
156
+ builder .connectTimeout (10 , TimeUnit .SECONDS );
150
157
})
151
158
.applyConnectionString (new ConnectionString ((connectionString != null )
152
159
? connectionString : "mongodb://localhost:27020" ))
0 commit comments