Skip to content
This repository was archived by the owner on Oct 18, 2021. It is now read-only.

Commit f547ad2

Browse files
authored
Implement SSL (#175)
* Implement SSL
1 parent 9e0b309 commit f547ad2

File tree

21 files changed

+677
-309
lines changed

21 files changed

+677
-309
lines changed

.travis.yml

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,29 @@ language: rust
33
cache: cargo
44

55
before_install:
6-
- wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.14.tgz
7-
- tar xvf mongodb-linux-x86_64-3.0.14.tgz
8-
- mv mongodb-linux-x86_64-3.0.14 3.0.14
9-
- wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.2.11.tgz
10-
- tar xvf mongodb-linux-x86_64-3.2.11.tgz
11-
- mv mongodb-linux-x86_64-3.2.11 3.2.11
6+
- wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1204-3.0.14.tgz
7+
- tar xvf mongodb-linux-x86_64-ubuntu1204-3.0.14.tgz
8+
- mv mongodb-linux-x86_64-ubuntu1204-3.0.14 3.0.14
9+
- wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1204-3.2.11.tgz
10+
- tar xvf mongodb-linux-x86_64-ubuntu1204-3.2.11.tgz
11+
- mv mongodb-linux-x86_64-ubuntu1204-3.2.11 3.2.11
1212

1313
script:
14-
- mkdir -p ./data/db ./data/db2
15-
- 3.0.14/bin/mongod --fork --nopreallocj --dbpath ./data/db --syslog --port 27017
14+
- mkdir -p ./data/db30 ./data/db30-ssl ./data/db32 ./data/db32-ssl
15+
- 3.0.14/bin/mongod --fork --dbpath ./data/db30 --syslog --port 27017
1616
- cargo build --verbose
1717
- cargo test --verbose
1818
- killall mongod
19-
- 3.2.11/bin/mongod --fork --nopreallocj --dbpath ./data/db2 --syslog --port 27017
19+
- 3.2.11/bin/mongod --fork --dbpath ./data/db32 --syslog --port 27017
2020
- cargo test --verbose
21+
- killall mongod
22+
- 3.0.14/bin/mongod --fork --dbpath ./data/db30 --syslog --port 27017
23+
- 3.0.14/bin/mongod --fork --dbpath ./data/db30-ssl --syslog --port 27018 --sslMode requireSSL --sslPEMKeyFile tests/ssl/server.pem --sslCAFile tests/ssl/ca.pem
24+
- cargo build --features ssl --verbose
25+
- cargo test --features ssl --verbose
26+
- killall mongod
27+
- 3.2.11/bin/mongod --fork --dbpath ./data/db32 --syslog --port 27017
28+
- 3.2.11/bin/mongod --fork --dbpath ./data/db32-ssl --syslog --port 27018 --sslMode requireSSL --sslPEMKeyFile tests/ssl/server.pem --sslCAFile tests/ssl/ca.pem
29+
- cargo test --features ssl --verbose
30+
31+

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@ readme = "README.md"
1212
keywords = ["mongo", "mongodb", "database", "bson", "nosql"]
1313
license = "Apache-2.0"
1414

15+
[features]
16+
default = []
17+
ssl = ["openssl"]
18+
1519
[dependencies]
1620
bitflags = "0.7.0"
1721
bson = "0.3.2"
22+
bufstream = "0.1.1"
1823
byteorder = "0.5.3"
1924
chrono = "0.2.25"
25+
openssl = { version = "0.9.3", optional = true }
2026
rand = "0.3.14"
2127
rust-crypto = "0.2.31"
2228
rustc-serialize = "0.3.19"
@@ -25,7 +31,6 @@ semver = "0.5.0"
2531
separator = "0.3.1"
2632
textnonce = { version = "0.4.1", default-features = false }
2733
time = "0.1.35"
28-
bufstream = "0.1.1"
2934

3035
[dev-dependencies]
3136
nalgebra = "0.10.1"

README.md

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,22 @@ Installation
1818

1919
#### Importing
2020

21-
The 1.0 driver is available on crates.io. To use the MongoDB driver in your code, add the bson and mongodb packages to your `Cargo.toml`:
21+
The driver is available on crates.io. To use the MongoDB driver in your code, add the bson and mongodb packages to your `Cargo.toml`:
2222

2323
```
2424
[dependencies]
25-
bson = "0.3.1"
25+
bson = "0.3.2"
2626
mongodb = "0.1.8"
2727
```
2828

29+
Alternately, you can use the MongoDB driver with SSL support. To do this, you must have OpenSSL installed on your system. Then, enable the `ssl` feature for MongoDB in your Cargo.toml:
30+
31+
```
32+
[dependencies]
33+
...
34+
mongodb = { version = "0.1.8", features = ["ssl"] }
35+
```
36+
2937
Then, import the bson and driver libraries within your code.
3038

3139
```rust
@@ -46,7 +54,7 @@ use mongodb::db::ThreadedDatabase;
4654

4755
fn main() {
4856
let client = Client::connect("localhost", 27017)
49-
.ok().expect("Failed to initialize standalone client.");
57+
.expect("Failed to initialize standalone client.");
5058

5159
let coll = client.db("test").collection("movies");
5260

@@ -74,3 +82,33 @@ fn main() {
7482
}
7583
}
7684
```
85+
86+
To connect with SSL, use `ClientOptions::with_ssl` and `Client::connect_with_options`. Afterwards, the client can be used as above (note that the server will have to be configured to accept SSL connections and that you'll have to generate your own keys and certificates):
87+
88+
```rust
89+
use bson::Bson;
90+
use mongodb::{Client, ClientOptions, ThreadedClient};
91+
use mongodb::db::ThreadedDatabase;
92+
93+
fn main() {
94+
// Path to file containing trusted server certificates.
95+
let ca_file = "path/to/ca.crt";
96+
// Path to file containing client certificate.
97+
let certificate = "path/to/client.crt";
98+
// Path to file containing the client private key.
99+
let key_file = "path/to/client.key";
100+
// Whether or not to verify that the server certificate is valid. Unless you're just testing out something locally, this should ALWAYS be true.
101+
let verify_peer = true;
102+
103+
let options = ClientOptions::with_ssl(ca_file, certificate, key_file, verify_peer);
104+
105+
let client = Client::connect_with_options("localhost", 27017, options)
106+
.expect("Failed to initialize standalone client.");
107+
108+
// Insert document into 'test.movies' collection
109+
coll.insert_one(doc.clone(), None)
110+
.ok().expect("Failed to insert document.");
111+
112+
...
113+
}
114+
```

src/coll/mod.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,7 @@ impl Collection {
415415
}
416416
}
417417

418-
vec![
419-
Batch::Insert(inserts),
420-
Batch::Delete(deletes),
421-
Batch::Update(updates),
422-
]
418+
vec![Batch::Insert(inserts), Batch::Delete(deletes), Batch::Update(updates)]
423419
}
424420

425421
pub fn get_ordered_batches(mut requests: VecDeque<WriteModel>) -> Vec<Batch> {
@@ -472,12 +468,10 @@ impl Collection {
472468
exception: &mut BulkWriteException)
473469
-> bool {
474470
let original_models = models.iter()
475-
.map(|model| {
476-
if model.multi {
477-
WriteModel::DeleteMany { filter: model.filter.clone() }
478-
} else {
479-
WriteModel::DeleteOne { filter: model.filter.clone() }
480-
}
471+
.map(|model| if model.multi {
472+
WriteModel::DeleteMany { filter: model.filter.clone() }
473+
} else {
474+
WriteModel::DeleteOne { filter: model.filter.clone() }
481475
})
482476
.collect();
483477

@@ -500,12 +494,10 @@ impl Collection {
500494
exception: &mut BulkWriteException)
501495
-> bool {
502496
let original_models = models.iter()
503-
.map(|model| {
504-
if model.multi {
505-
WriteModel::DeleteMany { filter: model.filter.clone() }
506-
} else {
507-
WriteModel::DeleteOne { filter: model.filter.clone() }
508-
}
497+
.map(|model| if model.multi {
498+
WriteModel::DeleteMany { filter: model.filter.clone() }
499+
} else {
500+
WriteModel::DeleteOne { filter: model.filter.clone() }
509501
})
510502
.collect();
511503

src/cursor.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,10 @@ impl Cursor {
170170

171171
// Extract first batch documents
172172
let map = batch.iter()
173-
.filter_map(|bdoc| {
174-
if let Bson::Document(ref doc) = *bdoc {
175-
Some(doc.clone())
176-
} else {
177-
None
178-
}
173+
.filter_map(|bdoc| if let Bson::Document(ref doc) = *bdoc {
174+
Some(doc.clone())
175+
} else {
176+
None
179177
})
180178
.collect();
181179

src/db/mod.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,9 @@ impl ThreadedDatabase for Database {
235235
options.read_preference = read_preference;
236236
let res = try!(coll.find_one_with_command_type(Some(spec.clone()), Some(options),
237237
cmd_type));
238-
res.ok_or_else(|| OperationError(
239-
format!("Failed to execute command with spec {:?}.", spec)))
238+
res.ok_or_else(|| {
239+
OperationError(format!("Failed to execute command with spec {:?}.", spec))
240+
})
240241
}
241242

242243
fn list_collections(&self, filter: Option<bson::Document>) -> Result<Cursor> {
@@ -281,15 +282,15 @@ impl ThreadedDatabase for Database {
281282

282283
fn version(&self) -> Result<Version> {
283284
let doc = doc! { "buildinfo" => 1 };
284-
let out = try!(self.command(doc,
285-
CommandType::BuildInfo,
286-
None));
285+
let out = try!(self.command(doc, CommandType::BuildInfo, None));
287286

288287
match out.get("version") {
289-
Some(&Bson::String(ref s)) => match Version::parse(s) {
290-
Ok(v) => Ok(v),
291-
Err(e) => Err(ResponseError(String::from(e.description()))),
292-
},
288+
Some(&Bson::String(ref s)) => {
289+
match Version::parse(s) {
290+
Ok(v) => Ok(v),
291+
Err(e) => Err(ResponseError(String::from(e.description()))),
292+
}
293+
}
293294
_ => Err(ResponseError(String::from("No version received from server"))),
294295
}
295296
}

0 commit comments

Comments
 (0)