|
| 1 | +use crate::errors::Error; |
| 2 | +use crate::server::Server; |
| 3 | +use log::debug; |
| 4 | + |
| 5 | +#[derive(Clone, Debug)] |
| 6 | +pub struct AuthPassthrough { |
| 7 | + password: String, |
| 8 | + query: String, |
| 9 | + user: String, |
| 10 | +} |
| 11 | + |
| 12 | +impl AuthPassthrough { |
| 13 | + /// Initializes an AuthPassthrough. |
| 14 | + pub fn new(query: &str, user: &str, password: &str) -> Self { |
| 15 | + AuthPassthrough { |
| 16 | + password: password.to_string(), |
| 17 | + query: query.to_string(), |
| 18 | + user: user.to_string(), |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + /// Returns an AuthPassthrough given the pool configuration. |
| 23 | + /// If any of required values is not set, None is returned. |
| 24 | + pub fn from_pool_config(pool_config: &crate::config::Pool) -> Option<Self> { |
| 25 | + if pool_config.is_auth_query_configured() { |
| 26 | + return Some(AuthPassthrough::new( |
| 27 | + pool_config.auth_query.as_ref().unwrap(), |
| 28 | + pool_config.auth_query_user.as_ref().unwrap(), |
| 29 | + pool_config.auth_query_password.as_ref().unwrap(), |
| 30 | + )); |
| 31 | + } |
| 32 | + |
| 33 | + None |
| 34 | + } |
| 35 | + |
| 36 | + /// Returns an AuthPassthrough given the pool settings. |
| 37 | + /// If any of required values is not set, None is returned. |
| 38 | + pub fn from_pool_settings(pool_settings: &crate::pool::PoolSettings) -> Option<Self> { |
| 39 | + let pool_config = crate::config::Pool { |
| 40 | + auth_query: pool_settings.auth_query.clone(), |
| 41 | + auth_query_password: pool_settings.auth_query_password.clone(), |
| 42 | + auth_query_user: pool_settings.auth_query_user.clone(), |
| 43 | + ..Default::default() |
| 44 | + }; |
| 45 | + |
| 46 | + AuthPassthrough::from_pool_config(&pool_config) |
| 47 | + } |
| 48 | + |
| 49 | + /// Connects to server and executes auth_query for the specified address. |
| 50 | + /// If the response is a row with two columns containing the username set in the address. |
| 51 | + /// and its MD5 hash, the MD5 hash returned. |
| 52 | + /// |
| 53 | + /// Note that the query is executed, changing $1 with the name of the user |
| 54 | + /// this is so we only hold in memory (and transfer) the least amount of 'sensitive' data. |
| 55 | + /// Also, it is compatible with pgbouncer. |
| 56 | + /// |
| 57 | + /// # Arguments |
| 58 | + /// |
| 59 | + /// * `address` - An Address of the server we want to connect to. The username for the hash will be obtained from this value. |
| 60 | + /// |
| 61 | + /// # Examples |
| 62 | + /// |
| 63 | + /// ``` |
| 64 | + /// use pgcat::auth_passthrough::AuthPassthrough; |
| 65 | + /// use pgcat::config::Address; |
| 66 | + /// let auth_passthrough = AuthPassthrough::new("SELECT * FROM public.user_lookup('$1');", "postgres", "postgres"); |
| 67 | + /// auth_passthrough.fetch_hash(&Address::default()); |
| 68 | + /// ``` |
| 69 | + /// |
| 70 | + pub async fn fetch_hash(&self, address: &crate::config::Address) -> Result<String, Error> { |
| 71 | + let auth_user = crate::config::User { |
| 72 | + username: self.user.clone(), |
| 73 | + password: Some(self.password.clone()), |
| 74 | + pool_size: 1, |
| 75 | + statement_timeout: 0, |
| 76 | + }; |
| 77 | + |
| 78 | + let user = &address.username; |
| 79 | + |
| 80 | + debug!("Connecting to server to obtain auth hashes."); |
| 81 | + let auth_query = self.query.replace("$1", user); |
| 82 | + match Server::exec_simple_query(address, &auth_user, &auth_query).await { |
| 83 | + Ok(password_data) => { |
| 84 | + if password_data.len() == 2 && password_data.first().unwrap() == user { |
| 85 | + if let Some(stripped_hash) = password_data.last().unwrap().to_string().strip_prefix("md5") { |
| 86 | + Ok(stripped_hash.to_string()) |
| 87 | + } |
| 88 | + else { |
| 89 | + Err(Error::AuthPassthroughError( |
| 90 | + "Obtained hash from auth_query does not seem to be in md5 format.".to_string(), |
| 91 | + )) |
| 92 | + } |
| 93 | + } else { |
| 94 | + Err(Error::AuthPassthroughError( |
| 95 | + "Data obtained from query does not follow the scheme 'user','hash'." |
| 96 | + .to_string(), |
| 97 | + )) |
| 98 | + } |
| 99 | + } |
| 100 | + Err(err) => { |
| 101 | + Err(Error::AuthPassthroughError( |
| 102 | + format!("Error trying to obtain password from auth_query, ignoring hash for user '{}'. Error: {:?}", |
| 103 | + user, err))) |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments