|
15 | 15 | */ |
16 | 16 | package org.springframework.data.mongodb.core.encryption; |
17 | 17 |
|
18 | | -import java.security.SecureRandom; |
19 | | -import java.util.Collections; |
20 | | -import java.util.Map; |
21 | | - |
22 | | -import org.bson.BsonBinary; |
23 | | -import org.bson.Document; |
24 | 18 | import org.junit.jupiter.api.extension.ExtendWith; |
25 | | -import org.springframework.beans.factory.annotation.Autowired; |
26 | | -import org.springframework.context.ApplicationContext; |
27 | | -import org.springframework.context.annotation.Bean; |
28 | | -import org.springframework.context.annotation.Configuration; |
29 | | -import org.springframework.data.convert.PropertyValueConverterFactory; |
30 | | -import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration; |
31 | | -import org.springframework.data.mongodb.core.convert.MongoCustomConversions.MongoConverterConfigurationAdapter; |
32 | | -import org.springframework.data.mongodb.core.convert.encryption.MongoEncryptionConverter; |
33 | | -import org.springframework.data.mongodb.core.encryption.EncryptionTests.Config; |
34 | | -import org.springframework.data.util.Lazy; |
35 | 19 | import org.springframework.test.context.ContextConfiguration; |
36 | 20 | import org.springframework.test.context.junit.jupiter.SpringExtension; |
37 | 21 |
|
38 | | -import com.mongodb.ClientEncryptionSettings; |
39 | | -import com.mongodb.ConnectionString; |
40 | | -import com.mongodb.MongoClientSettings; |
41 | | -import com.mongodb.MongoNamespace; |
42 | | -import com.mongodb.client.MongoClient; |
43 | | -import com.mongodb.client.MongoCollection; |
44 | | -import com.mongodb.client.model.Filters; |
45 | | -import com.mongodb.client.model.IndexOptions; |
46 | | -import com.mongodb.client.model.Indexes; |
47 | | -import com.mongodb.client.model.vault.DataKeyOptions; |
48 | | -import com.mongodb.client.vault.ClientEncryptions; |
49 | | - |
50 | 22 | /** |
51 | 23 | * @author Christoph Strobl |
52 | 24 | * @author Julia Lee |
53 | 25 | */ |
54 | 26 | @ExtendWith(SpringExtension.class) |
55 | | -@ContextConfiguration(classes = Config.class) |
| 27 | +@ContextConfiguration(classes = AbstractEncryptionTestBase.EncryptionConfig.class) |
56 | 28 | public class EncryptionTests extends AbstractEncryptionTestBase { |
57 | 29 |
|
58 | | - @Configuration |
59 | | - static class Config extends AbstractMongoClientConfiguration { |
60 | | - |
61 | | - @Autowired ApplicationContext applicationContext; |
62 | | - |
63 | | - @Override |
64 | | - protected String getDatabaseName() { |
65 | | - return "fle-test"; |
66 | | - } |
67 | | - |
68 | | - @Bean |
69 | | - @Override |
70 | | - public MongoClient mongoClient() { |
71 | | - return super.mongoClient(); |
72 | | - } |
73 | | - |
74 | | - @Override |
75 | | - protected void configureConverters(MongoConverterConfigurationAdapter converterConfigurationAdapter) { |
76 | | - |
77 | | - converterConfigurationAdapter |
78 | | - .registerPropertyValueConverterFactory(PropertyValueConverterFactory.beanFactoryAware(applicationContext)) |
79 | | - .useNativeDriverJavaTimeCodecs(); |
80 | | - } |
81 | | - |
82 | | - @Bean |
83 | | - MongoEncryptionConverter encryptingConverter(MongoClientEncryption mongoClientEncryption) { |
84 | | - |
85 | | - Lazy<BsonBinary> dataKey = Lazy.of(() -> mongoClientEncryption.getClientEncryption().createDataKey("local", |
86 | | - new DataKeyOptions().keyAltNames(Collections.singletonList("mySuperSecretKey")))); |
87 | | - |
88 | | - return new MongoEncryptionConverter(mongoClientEncryption, |
89 | | - EncryptionKeyResolver.annotated((ctx) -> EncryptionKey.keyId(dataKey.get()))); |
90 | | - } |
91 | | - |
92 | | - @Bean |
93 | | - CachingMongoClientEncryption clientEncryption(ClientEncryptionSettings encryptionSettings) { |
94 | | - return new CachingMongoClientEncryption(() -> ClientEncryptions.create(encryptionSettings)); |
95 | | - } |
96 | | - |
97 | | - @Bean |
98 | | - ClientEncryptionSettings encryptionSettings(MongoClient mongoClient) { |
99 | | - |
100 | | - MongoNamespace keyVaultNamespace = new MongoNamespace("encryption.testKeyVault"); |
101 | | - MongoCollection<Document> keyVaultCollection = mongoClient.getDatabase(keyVaultNamespace.getDatabaseName()) |
102 | | - .getCollection(keyVaultNamespace.getCollectionName()); |
103 | | - keyVaultCollection.drop(); |
104 | | - // Ensure that two data keys cannot share the same keyAltName. |
105 | | - keyVaultCollection.createIndex(Indexes.ascending("keyAltNames"), |
106 | | - new IndexOptions().unique(true).partialFilterExpression(Filters.exists("keyAltNames"))); |
107 | | - |
108 | | - MongoCollection<Document> collection = mongoClient.getDatabase(getDatabaseName()).getCollection("test"); |
109 | | - collection.drop(); // Clear old data |
110 | | - |
111 | | - byte[] localMasterKey = new byte[96]; |
112 | | - new SecureRandom().nextBytes(localMasterKey); |
113 | | - Map<String, Map<String, Object>> kmsProviders = Map.of("local", Map.of("key", localMasterKey)); |
114 | | - |
115 | | - // Create the ClientEncryption instance |
116 | | - return ClientEncryptionSettings.builder() |
117 | | - .keyVaultMongoClientSettings( |
118 | | - MongoClientSettings.builder().applyConnectionString(new ConnectionString("mongodb://localhost")).build()) // |
119 | | - .keyVaultNamespace(keyVaultNamespace.getFullName()) // |
120 | | - .kmsProviders(kmsProviders) // |
121 | | - .build(); |
122 | | - } |
123 | | - } |
124 | 30 | } |
0 commit comments