|
| 1 | +// Copyright 2020 Contributors to the Parsec project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +//! Peer credentials authenticator |
| 4 | +//! |
| 5 | +//! The `PeerCredentialsAuthenticator` uses peer credentials (i.e. such as those that can be |
| 6 | +//! acquired from Unix domain sockets) to perform authentication. As such, it uses the UID (and |
| 7 | +//! possibly GID in the future) to authenticate the connecting process. Currently, the UID is used |
| 8 | +//! as the application name. |
| 9 | +
|
| 10 | +use super::ApplicationName; |
| 11 | +use super::Authenticate; |
| 12 | +use crate::front::listener::ConnectionMetadata; |
| 13 | +use log::error; |
| 14 | +use parsec_interface::requests::request::RequestAuth; |
| 15 | +use parsec_interface::requests::{ResponseStatus, Result}; |
| 16 | +use parsec_interface::secrecy::ExposeSecret; |
| 17 | +use std::str; |
| 18 | + |
| 19 | +#[derive(Copy, Clone, Debug)] |
| 20 | +pub struct PeerCredentialsAuthenticator; |
| 21 | + |
| 22 | +impl Authenticate for PeerCredentialsAuthenticator { |
| 23 | + fn authenticate( |
| 24 | + &self, |
| 25 | + auth: &RequestAuth, |
| 26 | + meta: Option<ConnectionMetadata>, |
| 27 | + ) -> Result<ApplicationName> { |
| 28 | + // Parse authentication request. |
| 29 | + let expected_uid = auth.buffer.expose_secret(); |
| 30 | + if expected_uid.is_empty() { |
| 31 | + error!("Expected UID in authentication request, but it is empty."); |
| 32 | + return Err(ResponseStatus::AuthenticationError); |
| 33 | + } |
| 34 | + |
| 35 | + let expected_uid = match str::from_utf8(expected_uid) { |
| 36 | + Ok(expected_uid) => expected_uid, |
| 37 | + Err(_) => { |
| 38 | + error!("Error parsing the authentication value as a UTF-8 string."); |
| 39 | + return Err(ResponseStatus::AuthenticationError); |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + // Get request metadata. |
| 44 | + let meta = match meta { |
| 45 | + Some(meta) => meta, |
| 46 | + None => { |
| 47 | + error!("Authenticator did not receive any metadata; cannot authenticate."); |
| 48 | + return Err(ResponseStatus::AuthenticationError); |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + let (uid, _gid) = match meta { |
| 53 | + ConnectionMetadata::PeerCredentials { uid, gid } => (uid, gid), |
| 54 | + // TODO: add wildcard pattern when `ConnectionMetadata` has more possibilities. |
| 55 | + }; |
| 56 | + |
| 57 | + let uid = uid.to_string(); |
| 58 | + |
| 59 | + // Authentication is successful if the _actual_ UID from the peer credentials equals the |
| 60 | + // self-declared UID in the authentication request. |
| 61 | + if uid == expected_uid { |
| 62 | + Ok(ApplicationName(uid)) |
| 63 | + } else { |
| 64 | + error!("Declared UID in authentication request does not match the process's UID."); |
| 65 | + Err(ResponseStatus::AuthenticationError) |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +#[cfg(test)] |
| 71 | +mod test { |
| 72 | + use super::super::Authenticate; |
| 73 | + use super::PeerCredentialsAuthenticator; |
| 74 | + use crate::front::listener::ConnectionMetadata; |
| 75 | + use parsec_interface::requests::request::RequestAuth; |
| 76 | + use parsec_interface::requests::ResponseStatus; |
| 77 | + use rand::Rng; |
| 78 | + use std::os::unix::net::UnixStream; |
| 79 | + use users::get_current_uid; |
| 80 | + |
| 81 | + #[test] |
| 82 | + fn successful_authentication() { |
| 83 | + // This test should PASS; we are verifying that our username gets set as the application |
| 84 | + // secret when using peer credentials authentication with Unix domain sockets. |
| 85 | + |
| 86 | + // Create two connected sockets. |
| 87 | + let (sock_a, _sock_b) = UnixStream::pair().unwrap(); |
| 88 | + let (cred_a, _cred_b) = (sock_a.peer_cred().unwrap(), _sock_b.peer_cred().unwrap()); |
| 89 | + |
| 90 | + let authenticator = PeerCredentialsAuthenticator {}; |
| 91 | + |
| 92 | + let req_auth_data = cred_a.uid.to_string().as_bytes().to_vec(); |
| 93 | + let req_auth = RequestAuth::new(req_auth_data); |
| 94 | + let conn_metadata = Some(ConnectionMetadata::PeerCredentials { |
| 95 | + uid: cred_a.uid, |
| 96 | + gid: cred_a.gid, |
| 97 | + }); |
| 98 | + |
| 99 | + let auth_name = authenticator |
| 100 | + .authenticate(&req_auth, conn_metadata) |
| 101 | + .expect("Failed to authenticate"); |
| 102 | + |
| 103 | + assert_eq!(auth_name.get_name(), get_current_uid().to_string()); |
| 104 | + } |
| 105 | + |
| 106 | + #[test] |
| 107 | + fn unsuccessful_authentication_wrong_declared_uid() { |
| 108 | + // This test should FAIL; we are trying to authenticate, but we are declaring the wrong |
| 109 | + // UID. |
| 110 | + |
| 111 | + // Create two connected sockets. |
| 112 | + let (sock_a, _sock_b) = UnixStream::pair().unwrap(); |
| 113 | + let (cred_a, _cred_b) = (sock_a.peer_cred().unwrap(), _sock_b.peer_cred().unwrap()); |
| 114 | + |
| 115 | + let authenticator = PeerCredentialsAuthenticator {}; |
| 116 | + |
| 117 | + let wrong_uid = cred_a.uid + 1; |
| 118 | + let wrong_req_auth_data = wrong_uid.to_string().as_bytes().to_vec(); |
| 119 | + let req_auth = RequestAuth::new(wrong_req_auth_data); |
| 120 | + let conn_metadata = Some(ConnectionMetadata::PeerCredentials { |
| 121 | + uid: cred_a.uid, |
| 122 | + gid: cred_a.gid, |
| 123 | + }); |
| 124 | + |
| 125 | + let auth_result = authenticator.authenticate(&req_auth, conn_metadata); |
| 126 | + assert_eq!(auth_result, Err(ResponseStatus::AuthenticationError)); |
| 127 | + } |
| 128 | + |
| 129 | + #[test] |
| 130 | + fn unsuccessful_authentication_garbage_data() { |
| 131 | + // This test should FAIL; we are sending garbage (random) data in the request. |
| 132 | + |
| 133 | + // Create two connected sockets. |
| 134 | + let (sock_a, _sock_b) = UnixStream::pair().unwrap(); |
| 135 | + let (cred_a, _cred_b) = (sock_a.peer_cred().unwrap(), _sock_b.peer_cred().unwrap()); |
| 136 | + |
| 137 | + let authenticator = PeerCredentialsAuthenticator {}; |
| 138 | + |
| 139 | + let garbage_data = rand::thread_rng().gen::<[u8; 32]>().to_vec(); |
| 140 | + let req_auth = RequestAuth::new(garbage_data); |
| 141 | + let conn_metadata = Some(ConnectionMetadata::PeerCredentials { |
| 142 | + uid: cred_a.uid, |
| 143 | + gid: cred_a.gid, |
| 144 | + }); |
| 145 | + |
| 146 | + let auth_result = authenticator.authenticate(&req_auth, conn_metadata); |
| 147 | + assert_eq!(auth_result, Err(ResponseStatus::AuthenticationError)); |
| 148 | + } |
| 149 | + |
| 150 | + #[test] |
| 151 | + fn unsuccessful_authentication_no_metadata() { |
| 152 | + let authenticator = PeerCredentialsAuthenticator {}; |
| 153 | + let req_auth = RequestAuth::new("secret".into()); |
| 154 | + |
| 155 | + let conn_metadata = None; |
| 156 | + let auth_result = authenticator.authenticate(&req_auth, conn_metadata); |
| 157 | + assert_eq!(auth_result, Err(ResponseStatus::AuthenticationError)); |
| 158 | + } |
| 159 | + |
| 160 | + #[test] |
| 161 | + fn unsuccessful_authentication_wrong_metadata() { |
| 162 | + // TODO: this test needs implementing when we have more than one metadata type in |
| 163 | + // `PeerCredentials::ConnectionMetadata`. At the moment, the compiler just complains with |
| 164 | + // an 'unreachable branch' message. |
| 165 | + } |
| 166 | +} |
0 commit comments