|
| 1 | +--TEST-- |
| 2 | +Connect to MongoDB with using X509 retrieving username from certificate #002 |
| 3 | +--SKIPIF-- |
| 4 | +<?php require "tests/utils/basic-skipif.inc"?> |
| 5 | +--FILE-- |
| 6 | +<?php |
| 7 | +require_once "tests/utils/basic.inc"; |
| 8 | + |
| 9 | +$SSL_DIR = realpath(__DIR__ . "/" . "./../../scripts/ssl/"); |
| 10 | + |
| 11 | +$opts = array( |
| 12 | + "ssl" => array( |
| 13 | + "peer_name" => "MongoDB", |
| 14 | + "verify_peer" => true, |
| 15 | + "verify_peer_name" => true, |
| 16 | + "allow_self_signed" => false, |
| 17 | + "cafile" => $SSL_DIR . "/ca.pem", /* Defaults to openssl.cafile */ |
| 18 | + "capath" => $SSL_DIR, /* Defaults to openssl.capath */ |
| 19 | + "local_cert" => $SSL_DIR . "/client.pem", |
| 20 | + "passphrase" => "Very secretive client.pem passphrase", |
| 21 | + "CN_match" => "server", |
| 22 | + "verify_depth" => 5, |
| 23 | + "ciphers" => "HIGH:!EXPORT:!aNULL@STRENGTH", |
| 24 | + "capture_peer_cert" => true, |
| 25 | + "capture_peer_cert_chain" => true, |
| 26 | + "SNI_enabled" => true, |
| 27 | + "disable_compression" => false, |
| 28 | + "peer_fingerprint" => "0d6dbd95", |
| 29 | + ), |
| 30 | +); |
| 31 | +$context = stream_context_create($opts); |
| 32 | + |
| 33 | +$parsed = parse_url(MONGODB_STANDALONE_X509_URI); |
| 34 | +$adminuser = "root"; |
| 35 | +$adminpass = "toor"; |
| 36 | +$dsn = sprintf("mongodb://%s:%s@%s:%d/admin?ssl=true", $adminuser, $adminpass, $parsed["host"], $parsed["port"]); |
| 37 | +$adminmanager = new MongoDB\Manager($dsn, array(), array("context" => $context, "debug" => STDERR)); |
| 38 | + |
| 39 | +$certusername = "C=US,ST=New York,L=New York City,O=MongoDB,OU=KernelUser,CN=client"; |
| 40 | + |
| 41 | + |
| 42 | +$cmd = array( |
| 43 | + "createUser" => $certusername, |
| 44 | + "roles" => [["role" => "readWrite", "db" => DATABASE_NAME]], |
| 45 | +); |
| 46 | + |
| 47 | +try { |
| 48 | + echo "User Created\n"; |
| 49 | + $command = new MongoDB\Command($cmd); |
| 50 | + $result = $adminmanager->executeCommand(DATABASE_NAME, $command); |
| 51 | + echo "User Created\n"; |
| 52 | +} catch(Exception $e) { |
| 53 | + echo get_class($e), ": ", $e->getMessage(), "\n"; |
| 54 | +} |
| 55 | + |
| 56 | +try { |
| 57 | + /* mongoc will pull the username of the certificate */ |
| 58 | + $parsed = parse_url(MONGODB_STANDALONE_X509_URI); |
| 59 | + $dsn = sprintf("mongodb://%s:%d/%s?ssl=true&authMechanism=MONGODB-X509", $parsed["host"], $parsed["port"], DATABASE_NAME); |
| 60 | + |
| 61 | + $manager = new MongoDB\Manager($dsn, array(), array("context" => $context, "debug" => STDERR)); |
| 62 | + |
| 63 | + $batch = new MongoDB\WriteBatch(); |
| 64 | + $batch->insert(array("very" => "important")); |
| 65 | + $manager->executeWriteBatch(NS, $batch); |
| 66 | + $query = new MongoDB\Query(array("very" => "important")); |
| 67 | + $cursor = $manager->executeQuery(NS, $query); |
| 68 | + foreach($cursor as $document) { |
| 69 | + var_dump($document["very"]); |
| 70 | + } |
| 71 | +} catch(Exception $e) { |
| 72 | + echo get_class($e), ": ", $e->getMessage(), "\n"; |
| 73 | +} |
| 74 | + |
| 75 | +try { |
| 76 | + echo "User dropped\n"; |
| 77 | + $command = new MongoDB\Command(array("drop" => COLLECTION_NAME)); |
| 78 | + $result = $adminmanager->executeCommand(DATABASE_NAME, $command); |
| 79 | + echo "User dropped\n"; |
| 80 | +} catch(Exception $e) { |
| 81 | + echo get_class($e), ": ", $e->getMessage(), "\n"; |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | +?> |
| 86 | +===DONE=== |
| 87 | +<?php exit(0); ?> |
| 88 | +--EXPECTF-- |
| 89 | +User Created |
| 90 | +string(9) "important" |
| 91 | +User dropped |
| 92 | +===DONE=== |
0 commit comments