Skip to content

Commit

Permalink
Fix check for whether crypt shared lib is available
Browse files Browse the repository at this point in the history
Also add unit tests for isMongocryptdSpawningDisabled helper method

JAVA-4739
  • Loading branch information
jyemin committed Sep 29, 2022
1 parent 7ea1f20 commit 7c1451c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private static BsonDocument toBsonDocument(final Map<String, Object> optionsMap)

public static boolean isMongocryptdSpawningDisabled(@Nullable final String cryptSharedLibVersion,
final AutoEncryptionSettings settings) {
boolean cryptSharedLibIsAvailable = cryptSharedLibVersion != null && cryptSharedLibVersion.isEmpty();
boolean cryptSharedLibIsAvailable = cryptSharedLibVersion != null && !cryptSharedLibVersion.isEmpty();
boolean cryptSharedLibRequired = (boolean) settings.getExtraOptions().getOrDefault("cryptSharedLibRequired", false);
return settings.isBypassAutoEncryption() || settings.isBypassQueryAnalysis() || cryptSharedLibRequired || cryptSharedLibIsAvailable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@
import java.util.HashMap;
import java.util.Map;

import static com.mongodb.internal.capi.MongoCryptHelper.isMongocryptdSpawningDisabled;
import static com.mongodb.internal.capi.MongoCryptHelper.validateRewrapManyDataKeyOptions;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class MongoCryptHelperTest {

Expand Down Expand Up @@ -104,6 +108,29 @@ public void validateRewrapManyDataKeyOptionsTest() {
assertThrows(MongoClientException.class, () -> validateRewrapManyDataKeyOptions(new RewrapManyDataKeyOptions().masterKey(new BsonDocument())));
}

@Test
public void isMongocryptdSpawningDisabledTest() {
assertTrue(isMongocryptdSpawningDisabled(null,
initializeAutoEncryptionSettingsBuilder().bypassAutoEncryption(true).build()));
assertTrue(isMongocryptdSpawningDisabled(null,
initializeAutoEncryptionSettingsBuilder().bypassQueryAnalysis(true).build()));
assertTrue(isMongocryptdSpawningDisabled(null,
initializeAutoEncryptionSettingsBuilder().extraOptions(singletonMap("cryptSharedLibRequired", true)).build()));
assertTrue(isMongocryptdSpawningDisabled("/path/to/shared/lib.so",
initializeAutoEncryptionSettingsBuilder().build()));
assertFalse(isMongocryptdSpawningDisabled(null,
initializeAutoEncryptionSettingsBuilder().build()));
assertFalse(isMongocryptdSpawningDisabled("",
initializeAutoEncryptionSettingsBuilder().build()));
}

private static AutoEncryptionSettings.Builder initializeAutoEncryptionSettingsBuilder() {
AutoEncryptionSettings.Builder builder = AutoEncryptionSettings.builder()
.keyVaultNamespace("test.vault")
.kmsProviders(singletonMap("local", singletonMap("key", new byte[96])));
return builder;
}

void assertMongoCryptOptions(final MongoCryptOptions expected, final MongoCryptOptions actual) {
assertEquals(expected.getAwsKmsProviderOptions(), actual.getAwsKmsProviderOptions(), "AwsKmsProviderOptions not equal");
assertEquals(expected.getEncryptedFieldsMap(), actual.getEncryptedFieldsMap(), "EncryptedFieldsMap not equal");
Expand Down

0 comments on commit 7c1451c

Please sign in to comment.