|
| 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.client.auth; |
| 18 | + |
| 19 | +import com.mongodb.MongoClientSettings; |
| 20 | +import com.mongodb.MongoCommandException; |
| 21 | +import com.mongodb.MongoSecurityException; |
| 22 | +import com.mongodb.client.Fixture; |
| 23 | +import com.mongodb.client.MongoClient; |
| 24 | +import com.mongodb.connection.NettyTransportSettings; |
| 25 | +import io.netty.handler.ssl.SslContextBuilder; |
| 26 | +import io.netty.handler.ssl.SslProvider; |
| 27 | +import org.junit.jupiter.api.extension.ConditionEvaluationResult; |
| 28 | +import org.junit.jupiter.api.extension.ExecutionCondition; |
| 29 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 30 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 31 | +import org.junit.jupiter.params.ParameterizedTest; |
| 32 | +import org.junit.jupiter.params.provider.Arguments; |
| 33 | +import org.junit.jupiter.params.provider.MethodSource; |
| 34 | + |
| 35 | +import javax.net.ssl.KeyManagerFactory; |
| 36 | +import javax.net.ssl.SSLContext; |
| 37 | +import java.io.File; |
| 38 | +import java.io.FileInputStream; |
| 39 | +import java.io.IOException; |
| 40 | +import java.security.KeyStore; |
| 41 | +import java.security.KeyStoreException; |
| 42 | +import java.security.NoSuchAlgorithmException; |
| 43 | +import java.security.UnrecoverableKeyException; |
| 44 | +import java.security.cert.CertificateException; |
| 45 | +import java.util.stream.Stream; |
| 46 | + |
| 47 | +import static com.mongodb.AuthenticationMechanism.MONGODB_X509; |
| 48 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 49 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 50 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 51 | + |
| 52 | +@ExtendWith(AbstractX509AuthenticationTest.X509AuthenticationPropertyCondition.class) |
| 53 | +public abstract class AbstractX509AuthenticationTest { |
| 54 | + |
| 55 | + private static final String KEYSTORE_PASSWORD = "test"; |
| 56 | + protected abstract MongoClient createMongoClient(MongoClientSettings mongoClientSettings); |
| 57 | + |
| 58 | + private static Stream<Arguments> shouldAuthenticateWithClientCertificate() throws Exception { |
| 59 | + String keystoreFileName = "existing_user.p12"; |
| 60 | + return getArgumentForKeystore(keystoreFileName); |
| 61 | + } |
| 62 | + |
| 63 | + @ParameterizedTest(name = "should authenticate with client certificate. MongoClientSettings: {0}") |
| 64 | + @MethodSource |
| 65 | + public void shouldAuthenticateWithClientCertificate(final MongoClientSettings mongoClientSettings) { |
| 66 | + //given |
| 67 | + try (MongoClient client = createMongoClient(mongoClientSettings)) { |
| 68 | + |
| 69 | + //when & then command completes successfully with x509 authentication |
| 70 | + client.getDatabase("test").getCollection("test").estimatedDocumentCount(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private static Stream<Arguments> shouldPassMutualTLSWithClientCertificateAndFailAuthenticateWithAbsentUser() throws Exception { |
| 75 | + String keystoreFileName = "non_existing_user.p12"; |
| 76 | + return getArgumentForKeystore(keystoreFileName); |
| 77 | + } |
| 78 | + |
| 79 | + @ParameterizedTest(name = "should pass mutual TLS with client certificate and fail authenticate with absent user. " |
| 80 | + + "MongoClientSettings: {0}") |
| 81 | + @MethodSource |
| 82 | + public void shouldPassMutualTLSWithClientCertificateAndFailAuthenticateWithAbsentUser(final MongoClientSettings mongoClientSettings) { |
| 83 | + // given |
| 84 | + try (MongoClient client = createMongoClient(mongoClientSettings)) { |
| 85 | + |
| 86 | + // when & then |
| 87 | + MongoSecurityException mongoSecurityException = assertThrows(MongoSecurityException.class, |
| 88 | + () -> client.getDatabase("test").getCollection("test").estimatedDocumentCount()); |
| 89 | + |
| 90 | + assertTrue(mongoSecurityException.getMessage().contains("Exception authenticating")); |
| 91 | + MongoCommandException mongoCommandException = (MongoCommandException) mongoSecurityException.getCause(); |
| 92 | + |
| 93 | + assertTrue(mongoCommandException.getMessage().contains("Could not find user")); |
| 94 | + assertEquals(11, mongoCommandException.getCode()); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private static Stream<Arguments> getArgumentForKeystore(final String keystoreFileName) throws Exception { |
| 99 | + SSLContext context = buildSslContextFromKeyStore(keystoreFileName); |
| 100 | + MongoClientSettings.Builder mongoClientSettingsBuilder = Fixture.getMongoClientSettingsBuilder(); |
| 101 | + verifyX509AuthenticationIsRequired(mongoClientSettingsBuilder); |
| 102 | + |
| 103 | + return Stream.of( |
| 104 | + Arguments.of(mongoClientSettingsBuilder |
| 105 | + .applyToSslSettings(builder -> builder.context(context)) |
| 106 | + .build()), |
| 107 | + |
| 108 | + Arguments.of(mongoClientSettingsBuilder |
| 109 | + .applyToSslSettings(builder -> builder.context(context)) |
| 110 | + .transportSettings(NettyTransportSettings.nettyBuilder() |
| 111 | + .sslContext(SslContextBuilder.forClient() |
| 112 | + .sslProvider(SslProvider.JDK) |
| 113 | + .keyManager(getKeyManagerFactory(keystoreFileName)) |
| 114 | + .build()) |
| 115 | + .build()) |
| 116 | + .build()), |
| 117 | + |
| 118 | + Arguments.of(mongoClientSettingsBuilder |
| 119 | + .applyToSslSettings(builder -> builder.context(context)) |
| 120 | + .transportSettings(NettyTransportSettings.nettyBuilder() |
| 121 | + .sslContext(SslContextBuilder.forClient() |
| 122 | + .sslProvider(SslProvider.OPENSSL) |
| 123 | + .keyManager(getKeyManagerFactory(keystoreFileName)) |
| 124 | + .build()) |
| 125 | + .build()) |
| 126 | + .build()) |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + private static SSLContext buildSslContextFromKeyStore(final String keystoreFileName) throws Exception { |
| 131 | + KeyManagerFactory keyManagerFactory = getKeyManagerFactory(keystoreFileName); |
| 132 | + SSLContext sslContext = SSLContext.getInstance("TLS"); |
| 133 | + sslContext.init(keyManagerFactory.getKeyManagers(), null, null); |
| 134 | + return sslContext; |
| 135 | + } |
| 136 | + |
| 137 | + private static KeyManagerFactory getKeyManagerFactory(final String keystoreFileName) |
| 138 | + throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException { |
| 139 | + KeyStore ks = KeyStore.getInstance("PKCS12"); |
| 140 | + try (FileInputStream fis = new FileInputStream(getKeystoreLocation() + File.separator + keystoreFileName)) { |
| 141 | + ks.load(fis, KEYSTORE_PASSWORD.toCharArray()); |
| 142 | + } |
| 143 | + KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance( |
| 144 | + KeyManagerFactory.getDefaultAlgorithm()); |
| 145 | + keyManagerFactory.init(ks, KEYSTORE_PASSWORD.toCharArray()); |
| 146 | + return keyManagerFactory; |
| 147 | + } |
| 148 | + |
| 149 | + private static String getKeystoreLocation() { |
| 150 | + return System.getProperty("org.mongodb.test.x509.auth.keystore.location"); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * The connection string is sourced from an environment variable populated from Secret Storage. |
| 155 | + * We verify it still requires X.509 authentication before running these tests to ensure test invariants. |
| 156 | + */ |
| 157 | + private static void verifyX509AuthenticationIsRequired(final MongoClientSettings.Builder mongoClientSettingsBuilder) { |
| 158 | + com.mongodb.assertions.Assertions.assertTrue( |
| 159 | + com.mongodb.assertions.Assertions.assertNotNull(mongoClientSettingsBuilder.build().getCredential()) |
| 160 | + .getAuthenticationMechanism() == MONGODB_X509); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + This condition allows to skip initialization of method sources and test execution. |
| 165 | + - @EnableIf on the class, assumeTrue in the constructor - do not block method source initialization. |
| 166 | + - assumeTrue in the static block - fails the test. |
| 167 | + **/ |
| 168 | + public static class X509AuthenticationPropertyCondition implements ExecutionCondition { |
| 169 | + @Override |
| 170 | + public ConditionEvaluationResult evaluateExecutionCondition(final ExtensionContext context) { |
| 171 | + if (isX509TestsEnabled()) { |
| 172 | + return ConditionEvaluationResult.enabled("Test is enabled because x509 auth configuration exists"); |
| 173 | + } else { |
| 174 | + return ConditionEvaluationResult.disabled("Test is disabled because x509 auth configuration is missing"); |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + private static boolean isX509TestsEnabled() { |
| 180 | + return Boolean.parseBoolean(System.getProperty("org.mongodb.test.x509.auth.enabled")); |
| 181 | + } |
| 182 | +} |
0 commit comments