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

Commit 7839041

Browse files
authored
Merge pull request #168 from saghm/format-and-lint
Format and lint
2 parents b0675eb + 6aaf8a3 commit 7839041

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+2860
-1923
lines changed

src/apm/event.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ pub struct CommandStarted {
1515

1616
impl Display for CommandStarted {
1717
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
18-
fmt.write_fmt(format_args!("COMMAND.{} {} STARTED: {}", self.command_name,
19-
self.connection_string, self.command))
18+
fmt.write_fmt(format_args!("COMMAND.{} {} STARTED: {}",
19+
self.command_name,
20+
self.connection_string,
21+
self.command))
2022
}
2123
}
2224

@@ -35,22 +37,32 @@ pub enum CommandResult<'a> {
3537
failure: &'a MongoError,
3638
request_id: i64,
3739
connection_string: String,
38-
}
40+
},
3941
}
4042

4143
impl<'a> Display for CommandResult<'a> {
4244
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
4345
match *self {
44-
CommandResult::Success { duration, ref reply, ref command_name, request_id: _,
45-
ref connection_string } => {
46-
fmt.write_fmt(format_args!("COMMAND.{} {} COMPLETED: {} ({} ns)", command_name,
47-
connection_string, reply,
46+
CommandResult::Success { duration,
47+
ref reply,
48+
ref command_name,
49+
ref connection_string,
50+
.. } => {
51+
fmt.write_fmt(format_args!("COMMAND.{} {} COMPLETED: {} ({} ns)",
52+
command_name,
53+
connection_string,
54+
reply,
4855
duration.separated_string()))
49-
},
50-
CommandResult::Failure { duration, ref command_name, ref failure, request_id: _,
51-
ref connection_string } => {
52-
fmt.write_fmt(format_args!("COMMAND.{} {} FAILURE: {} ({} ns)", command_name,
53-
connection_string, failure,
56+
}
57+
CommandResult::Failure { duration,
58+
ref command_name,
59+
failure,
60+
ref connection_string,
61+
.. } => {
62+
fmt.write_fmt(format_args!("COMMAND.{} {} FAILURE: {} ({} ns)",
63+
command_name,
64+
connection_string,
65+
failure,
5466
duration.separated_string()))
5567
}
5668
}

src/apm/listener.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@ pub struct Listener {
1818

1919
impl Listener {
2020
pub fn new() -> Listener {
21-
Listener { no_start_hooks: AtomicBool::new(true),
22-
no_completion_hooks: AtomicBool::new(true),
23-
start_hooks: RwLock::new(vec![]), completion_hooks: RwLock::new(vec![]) }
21+
Listener {
22+
no_start_hooks: AtomicBool::new(true),
23+
no_completion_hooks: AtomicBool::new(true),
24+
start_hooks: RwLock::new(vec![]),
25+
completion_hooks: RwLock::new(vec![]),
26+
}
2427
}
2528

2629
pub fn add_start_hook(&self, hook: StartHook) -> Result<()> {
2730
let mut guard = match self.start_hooks.write() {
2831
Ok(guard) => guard,
29-
Err(_) => return Err(Error::PoisonLockError)
32+
Err(_) => return Err(Error::PoisonLockError),
3033
};
3134

3235
self.no_start_hooks.store(false, Ordering::SeqCst);
@@ -36,7 +39,7 @@ impl Listener {
3639
pub fn add_completion_hook(&self, hook: CompletionHook) -> Result<()> {
3740
let mut guard = match self.completion_hooks.write() {
3841
Ok(guard) => guard,
39-
Err(_) => return Err(Error::PoisonLockError)
42+
Err(_) => return Err(Error::PoisonLockError),
4043
};
4144

4245
self.no_completion_hooks.store(false, Ordering::SeqCst);
@@ -50,7 +53,7 @@ impl Listener {
5053

5154
let guard = match self.start_hooks.read() {
5255
Ok(guard) => guard,
53-
Err(_) => return Err(Error::PoisonLockError)
56+
Err(_) => return Err(Error::PoisonLockError),
5457
};
5558

5659
for hook in guard.deref().iter() {
@@ -67,7 +70,7 @@ impl Listener {
6770

6871
let guard = match self.completion_hooks.read() {
6972
Ok(guard) => guard,
70-
Err(_) => return Err(Error::PoisonLockError)
73+
Err(_) => return Err(Error::PoisonLockError),
7174
};
7275

7376
for hook in guard.deref().iter() {

src/apm/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! Command Monitoring
22
//!
3-
//! The APM module provides an intuitive interface for monitoring and responding to runtime information
4-
//! about commands being executed on the server. All non-suppressed commands trigger start and completion
5-
//! hooks defined on the client. Each non-suppressed command is also logged, if a log file was specified
6-
//! during instantiation of the client.
3+
//! The APM module provides an intuitive interface for monitoring and responding to runtime
4+
//! information about commands being executed on the server. All non-suppressed commands trigger
5+
//! start and completion hooks defined on the client. Each non-suppressed command is also logged,
6+
//! if a log file was specified during instantiation of the client.
77
pub mod client;
88
mod event;
99
mod listener;

src/auth.rs

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ use error::Result;
1616
use rustc_serialize::base64::{self, FromBase64, ToBase64};
1717
use textnonce::TextNonce;
1818

19-
const B64_CONFIG : base64::Config = base64::Config { char_set: base64::CharacterSet::Standard,
20-
newline: base64::Newline::LF,
21-
pad: true, line_length: None };
19+
const B64_CONFIG: base64::Config = base64::Config {
20+
char_set: base64::CharacterSet::Standard,
21+
newline: base64::Newline::LF,
22+
pad: true,
23+
line_length: None,
24+
};
2225

2326
/// Handles SCRAM-SHA-1 authentication logic.
2427
pub struct Authenticator {
@@ -57,7 +60,7 @@ impl Authenticator {
5760
fn start(&self, user: &str) -> Result<InitialData> {
5861
let text_nonce = match TextNonce::sized(64) {
5962
Ok(text_nonce) => text_nonce,
60-
Err(string) => return Err(DefaultError(string))
63+
Err(string) => return Err(DefaultError(string)),
6164
};
6265

6366
let nonce = format!("{}", text_nonce);
@@ -76,51 +79,59 @@ impl Authenticator {
7679

7780
let data = match doc.get("payload") {
7881
Some(&Binary(_, ref payload)) => payload.to_owned(),
79-
_ => return Err(ResponseError("Invalid payload returned".to_owned()))
82+
_ => return Err(ResponseError(String::from("Invalid payload returned"))),
8083
};
8184

8285
let id = match doc.get("conversationId") {
8386
Some(bson) => bson.clone(),
84-
None => return Err(ResponseError("No conversationId returned".to_owned()))
87+
None => return Err(ResponseError(String::from("No conversationId returned"))),
8588
};
8689

8790
let response = match String::from_utf8(data) {
8891
Ok(string) => string,
89-
Err(_) => return Err(ResponseError("Invalid UTF-8 payload returned".to_owned()))
92+
Err(_) => return Err(ResponseError(String::from("Invalid UTF-8 payload returned"))),
9093
};
9194

92-
Ok(InitialData { message: message, response: response, nonce: nonce,
93-
conversation_id: id })
95+
Ok(InitialData {
96+
message: message,
97+
response: response,
98+
nonce: nonce,
99+
conversation_id: id,
100+
})
94101
}
95102

96103
fn next(&self, password: String, initial_data: InitialData) -> Result<AuthData> {
97104
// Parse out rnonce, salt, and iteration count
98-
let (rnonce_opt, salt_opt, i_opt) = scan_fmt!(&initial_data.response[..], "r={},s={},i={}", String, String, u32);
105+
let (rnonce_opt, salt_opt, i_opt) = scan_fmt!(&initial_data.response[..],
106+
"r={},s={},i={}",
107+
String,
108+
String,
109+
u32);
99110

100111
let rnonce_b64 = match rnonce_opt {
101112
Some(val) => val,
102-
None => return Err(ResponseError("Invalid rnonce returned".to_owned()))
113+
None => return Err(ResponseError(String::from("Invalid rnonce returned"))),
103114
};
104115

105116
// Validate rnonce to make sure server isn't malicious
106117
if !rnonce_b64.starts_with(&initial_data.nonce[..]) {
107-
return Err(MaliciousServerError(MaliciousServerErrorType::InvalidRnonce))
118+
return Err(MaliciousServerError(MaliciousServerErrorType::InvalidRnonce));
108119
}
109120

110121
let salt_b64 = match salt_opt {
111122
Some(val) => val,
112-
None => return Err(ResponseError("Invalid salt returned".to_owned()))
123+
None => return Err(ResponseError(String::from("Invalid salt returned"))),
113124
};
114125

115126
let salt = match salt_b64.from_base64() {
116127
Ok(val) => val,
117-
Err(_) => return Err(ResponseError("Invalid base64 salt returned".to_owned()))
128+
Err(_) => return Err(ResponseError(String::from("Invalid base64 salt returned"))),
118129
};
119130

120131

121132
let i = match i_opt {
122133
Some(val) => val,
123-
None => return Err(ResponseError("Invalid iteration count returned".to_owned()))
134+
None => return Err(ResponseError(String::from("Invalid iteration count returned"))),
124135
};
125136

126137
// Hash password
@@ -130,24 +141,27 @@ impl Authenticator {
130141

131142
// Salt password
132143
let mut hmac = Hmac::new(Sha1::new(), hashed_password.as_bytes());
133-
let mut salted_password : Vec<_> = (0..hmac.output_bytes()).map(|_| 0).collect();
144+
let mut salted_password: Vec<_> = (0..hmac.output_bytes()).map(|_| 0).collect();
134145
pbkdf2::pbkdf2(&mut hmac, &salt[..], i, &mut salted_password);
135146

136147
// Compute client key
137148
let mut client_key_hmac = Hmac::new(Sha1::new(), &salted_password[..]);
138-
let client_key_bytes = "Client Key".as_bytes();
149+
let client_key_bytes = b"Client Key";
139150
client_key_hmac.input(client_key_bytes);
140151
let client_key = client_key_hmac.result().code().to_owned();
141152

142153
// Hash into stored key
143154
let mut stored_key_sha1 = Sha1::new();
144155
stored_key_sha1.input(&client_key[..]);
145-
let mut stored_key : Vec<_> = (0..stored_key_sha1.output_bytes()).map(|_| 0).collect();
156+
let mut stored_key: Vec<_> = (0..stored_key_sha1.output_bytes()).map(|_| 0).collect();
146157
stored_key_sha1.result(&mut stored_key);
147158

148159
// Create auth message
149160
let without_proof = format!("c=biws,r={}", rnonce_b64);
150-
let auth_message = format!("{},{},{}", initial_data.message, initial_data.response, without_proof);
161+
let auth_message = format!("{},{},{}",
162+
initial_data.message,
163+
initial_data.response,
164+
without_proof);
151165

152166
// Compute client signature
153167
let mut client_signature_hmac = Hmac::new(Sha1::new(), &stored_key[..]);
@@ -156,7 +170,8 @@ impl Authenticator {
156170

157171
// Sanity check
158172
if client_key.len() != client_signature.len() {
159-
return Err(DefaultError("Generated client key and/or client signature is invalid".to_owned()));
173+
return Err(DefaultError(String::from("Generated client key and/or client signature \
174+
is invalid")));
160175
}
161176

162177
// Compute proof by xor'ing key and signature
@@ -178,8 +193,11 @@ impl Authenticator {
178193

179194
let response = try!(self.db.command(next_doc, Suppressed, None));
180195

181-
Ok(AuthData { salted_password: salted_password, message: auth_message,
182-
response: response })
196+
Ok(AuthData {
197+
salted_password: salted_password,
198+
message: auth_message,
199+
response: response,
200+
})
183201
}
184202

185203
fn finish(&self, conversation_id: Bson, auth_data: AuthData) -> Result<()> {
@@ -191,7 +209,7 @@ impl Authenticator {
191209

192210
// Compute server key
193211
let mut server_key_hmac = Hmac::new(Sha1::new(), &auth_data.salted_password[..]);
194-
let server_key_bytes = "Server Key".as_bytes();
212+
let server_key_bytes = b"Server Key";
195213
server_key_hmac.input(server_key_bytes);
196214
let server_key = server_key_hmac.result().code().to_owned();
197215

@@ -207,25 +225,29 @@ impl Authenticator {
207225
if let Some(&Binary(_, ref payload)) = doc.get("payload") {
208226
let payload_str = match String::from_utf8(payload.to_owned()) {
209227
Ok(string) => string,
210-
Err(_) => return Err(ResponseError("Invalid UTF-8 payload returned".to_owned()))
228+
Err(_) => {
229+
return Err(ResponseError(String::from("Invalid UTF-8 payload returned")))
230+
}
211231
};
212232

213233
// Check that the signature exists
214234
let verifier = match scan_fmt!(&payload_str[..], "v={}", String) {
215235
Some(string) => string,
216-
None => return Err(MaliciousServerError(MaliciousServerErrorType::NoServerSignature)),
236+
None => return Err(
237+
MaliciousServerError(MaliciousServerErrorType::NoServerSignature)),
217238
};
218239

219240
// Check that the signature is valid
220241
if verifier.ne(&server_signature.to_base64(B64_CONFIG)[..]) {
221-
return Err(MaliciousServerError(MaliciousServerErrorType::InvalidServerSignature));
242+
return Err(
243+
MaliciousServerError(MaliciousServerErrorType::InvalidServerSignature));
222244
}
223245
}
224246

225247
doc = try!(self.db.command(final_doc.clone(), Suppressed, None));
226248

227249
if let Some(&Bson::Boolean(true)) = doc.get("done") {
228-
return Ok(())
250+
return Ok(());
229251
}
230252
}
231253
}

0 commit comments

Comments
 (0)