|
| 1 | +require 'test_helper' |
| 2 | + |
| 3 | +# Note: For testing with MongoReplicaSetClient you *MUST* use the |
| 4 | +# hostname 'server' for all members of the replica set. |
| 5 | + |
| 6 | +class ReplicaSetSSLCertValidationTest < Test::Unit::TestCase |
| 7 | + include Mongo |
| 8 | + |
| 9 | + CERT_PATH = "#{Dir.pwd}/test/fixtures/certificates/" |
| 10 | + CLIENT_CERT = "#{CERT_PATH}client.pem" |
| 11 | + CA_CERT = "#{CERT_PATH}ca.pem" |
| 12 | + SEEDS = ['server:3000','server:3001','server:3002'] |
| 13 | + BAD_SEEDS = ['localhost:3000','localhost:3001','localhost:3002'] |
| 14 | + |
| 15 | + # This test doesn't connect, no server config required |
| 16 | + def test_ssl_configuration |
| 17 | + # raises when ssl=false and ssl opts specified |
| 18 | + assert_raise MongoArgumentError do |
| 19 | + MongoReplicaSetClient.new(SEEDS, :connect => false, |
| 20 | + :ssl => false, |
| 21 | + :ssl_cert => CLIENT_CERT) |
| 22 | + end |
| 23 | + |
| 24 | + # raises when ssl=nil and ssl opts specified |
| 25 | + assert_raise MongoArgumentError do |
| 26 | + MongoReplicaSetClient.new(SEEDS, :connect => false, |
| 27 | + :ssl_key => CLIENT_CERT) |
| 28 | + end |
| 29 | + |
| 30 | + # raises when verify=true and no ca_cert |
| 31 | + assert_raise MongoArgumentError do |
| 32 | + MongoReplicaSetClient.new(SEEDS, :connect => false, |
| 33 | + :ssl => true, |
| 34 | + :ssl_key => CLIENT_CERT, |
| 35 | + :ssl_cert => CLIENT_CERT, |
| 36 | + :ssl_verify => true) |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + # Requires MongoDB built with SSL and the follow options: |
| 41 | + # |
| 42 | + # mongod --dbpath /path/to/data/directory --sslOnNormalPorts \ |
| 43 | + # --sslPEMKeyFile /path/to/server.pem \ |
| 44 | + # --sslCAFile /path/to/ca.pem \ |
| 45 | + # --sslCRLFile /path/to/crl.pem \ |
| 46 | + # --sslWeakCertificateValidation |
| 47 | + # |
| 48 | + # Make sure you have 'server' as an alias for localhost in /etc/hosts |
| 49 | + # |
| 50 | + def test_ssl_basic |
| 51 | + client = MongoReplicaSetClient.new(SEEDS, :connect => false, |
| 52 | + :ssl => true) |
| 53 | + assert client.connect |
| 54 | + end |
| 55 | + |
| 56 | + # Requires MongoDB built with SSL and the follow options: |
| 57 | + # |
| 58 | + # mongod --dbpath /path/to/data/directory --sslOnNormalPorts \ |
| 59 | + # --sslPEMKeyFile /path/to/server.pem \ |
| 60 | + # --sslCAFile /path/to/ca.pem \ |
| 61 | + # --sslCRLFile /path/to/crl.pem |
| 62 | + # |
| 63 | + # Make sure you have 'server' as an alias for localhost in /etc/hosts |
| 64 | + # |
| 65 | + def test_ssl_with_cert |
| 66 | + client = MongoReplicaSetClient.new(SEEDS, :connect => false, |
| 67 | + :ssl => true, |
| 68 | + :ssl_cert => CLIENT_CERT, |
| 69 | + :ssl_key => CLIENT_CERT) |
| 70 | + assert client.connect |
| 71 | + end |
| 72 | + |
| 73 | + def test_ssl_with_peer_cert_validation |
| 74 | + client = MongoReplicaSetClient.new(SEEDS, :connect => false, |
| 75 | + :ssl => true, |
| 76 | + :ssl_key => CLIENT_CERT, |
| 77 | + :ssl_cert => CLIENT_CERT, |
| 78 | + :ssl_verify => true, |
| 79 | + :ssl_ca_cert => CA_CERT) |
| 80 | + assert client.connect |
| 81 | + end |
| 82 | + |
| 83 | + def test_ssl_peer_cert_validation_hostname_fail |
| 84 | + client = MongoReplicaSetClient.new(BAD_SEEDS, :connect => false, |
| 85 | + :ssl => true, |
| 86 | + :ssl_key => CLIENT_CERT, |
| 87 | + :ssl_cert => CLIENT_CERT, |
| 88 | + :ssl_verify => true, |
| 89 | + :ssl_ca_cert => CA_CERT) |
| 90 | + assert_raise ConnectionFailure do |
| 91 | + client.connect |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + # Requires mongod built with SSL and the follow options: |
| 96 | + # |
| 97 | + # mongod --dbpath /path/to/data/directory --sslOnNormalPorts \ |
| 98 | + # --sslPEMKeyFile /path/to/server.pem \ |
| 99 | + # --sslCAFile /path/to/ca.pem \ |
| 100 | + # --sslCRLFile /path/to/crl_client_revoked.pem |
| 101 | + # |
| 102 | + # Make sure you have 'server' as an alias for localhost in /etc/hosts |
| 103 | + # |
| 104 | + def test_ssl_with_invalid_cert |
| 105 | + assert_raise ConnectionFailure do |
| 106 | + MongoReplicaSetClient.new(SEEDS, :ssl => true, |
| 107 | + :ssl_key => CLIENT_CERT, |
| 108 | + :ssl_cert => CLIENT_CERT, |
| 109 | + :ssl_verify => true, |
| 110 | + :ssl_ca_cert => CA_CERT) |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | +end |
0 commit comments