|
| 1 | +/* |
| 2 | + * Copyright 2016 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 | + |
| 18 | +package com.mongodb.connection; |
| 19 | + |
| 20 | +import com.mongodb.MongoCredential; |
| 21 | +import com.mongodb.MongoSecurityException; |
| 22 | +import com.mongodb.ServerAddress; |
| 23 | +import com.mongodb.async.FutureResultCallback; |
| 24 | +import org.bson.io.BsonInput; |
| 25 | +import org.junit.Before; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +import java.util.List; |
| 29 | +import java.util.concurrent.ExecutionException; |
| 30 | + |
| 31 | +import static com.mongodb.connection.MessageHelper.buildSuccessfulReply; |
| 32 | +import static org.junit.Assert.assertEquals; |
| 33 | +import static org.junit.Assert.fail; |
| 34 | + |
| 35 | +public class X509AuthenticatorNoUserNameTest { |
| 36 | + private TestInternalConnection connection; |
| 37 | + private ConnectionDescription connectionDescriptionThreeTwo; |
| 38 | + private ConnectionDescription connectionDescriptionThreeFour; |
| 39 | + private MongoCredential credential; |
| 40 | + private X509Authenticator subject; |
| 41 | + |
| 42 | + @Before |
| 43 | + public void before() { |
| 44 | + connection = new TestInternalConnection(new ServerId(new ClusterId(), new ServerAddress("localhost", 27017))); |
| 45 | + connectionDescriptionThreeTwo = new ConnectionDescription(new ConnectionId(new ServerId(new ClusterId(), new ServerAddress())), |
| 46 | + new ServerVersion(3, 2), ServerType.STANDALONE, 1000, 16000, |
| 47 | + 48000); |
| 48 | + connectionDescriptionThreeFour = new ConnectionDescription(new ConnectionId(new ServerId(new ClusterId(), new ServerAddress())), |
| 49 | + new ServerVersion(3, 4), ServerType.STANDALONE, 1000, 16000, |
| 50 | + 48000); |
| 51 | + credential = MongoCredential.createMongoX509Credential( |
| 52 | + "CN=client,OU=kerneluser,O=10Gen,L=New York City,ST=New York,C=US"); |
| 53 | + subject = new X509Authenticator(this.credential); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testSuccessfulAuthentication() { |
| 58 | + enqueueSuccessfulAuthenticationReply(); |
| 59 | + |
| 60 | + new X509Authenticator(MongoCredential.createMongoX509Credential()).authenticate(connection, connectionDescriptionThreeFour); |
| 61 | + |
| 62 | + validateMessages(); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testUnsuccessfulAuthenticationWhenServerVersionLessThanThreeFour() { |
| 67 | + try { |
| 68 | + new X509Authenticator(MongoCredential.createMongoX509Credential()).authenticate(connection, |
| 69 | + connectionDescriptionThreeTwo); |
| 70 | + fail(); |
| 71 | + } catch (MongoSecurityException e) { |
| 72 | + assertEquals("User name is required for the MONGODB-X509 authentication mechanism on server versions less than 3.4", |
| 73 | + e.getMessage()); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testSuccessfulAuthenticationAsync() throws ExecutionException, InterruptedException { |
| 79 | + enqueueSuccessfulAuthenticationReply(); |
| 80 | + |
| 81 | + FutureResultCallback<Void> futureCallback = new FutureResultCallback<Void>(); |
| 82 | + new X509Authenticator(MongoCredential.createMongoX509Credential()) |
| 83 | + .authenticateAsync(connection, connectionDescriptionThreeFour, futureCallback); |
| 84 | + |
| 85 | + futureCallback.get(); |
| 86 | + |
| 87 | + validateMessages(); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testUnsuccessfulAuthenticationWhenServerVersionLessThanThreeFourAsync() throws ExecutionException, InterruptedException { |
| 92 | + FutureResultCallback<Void> futureCallback = new FutureResultCallback<Void>(); |
| 93 | + new X509Authenticator(MongoCredential.createMongoX509Credential()).authenticateAsync(connection, |
| 94 | + connectionDescriptionThreeTwo, futureCallback); |
| 95 | + |
| 96 | + try { |
| 97 | + futureCallback.get(); |
| 98 | + } catch (MongoSecurityException e) { |
| 99 | + assertEquals("User name is required for the MONGODB-X509 authentication mechanism on server versions less than 3.4", |
| 100 | + e.getMessage()); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private void enqueueSuccessfulAuthenticationReply() { |
| 105 | + connection.enqueueReply(buildSuccessfulReply("{ok: 1}")); |
| 106 | + } |
| 107 | + |
| 108 | + private void validateMessages() { |
| 109 | + List<BsonInput> sent = connection.getSent(); |
| 110 | + String command = MessageHelper.decodeCommandAsJson(sent.get(0)); |
| 111 | + assertEquals("{ \"authenticate\" : 1, \"mechanism\" : \"MONGODB-X509\" }", command); |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments