-
Notifications
You must be signed in to change notification settings - Fork 227
Auth passthrough (auth_query) #266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+1,026
−31
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
edition = "2021" | ||
hard_tabs = false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
use crate::errors::Error; | ||
use crate::server::Server; | ||
use log::debug; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct AuthPassthrough { | ||
password: String, | ||
query: String, | ||
user: String, | ||
} | ||
|
||
impl AuthPassthrough { | ||
/// Initializes an AuthPassthrough. | ||
pub fn new(query: &str, user: &str, password: &str) -> Self { | ||
AuthPassthrough { | ||
password: password.to_string(), | ||
query: query.to_string(), | ||
user: user.to_string(), | ||
} | ||
} | ||
|
||
/// Returns an AuthPassthrough given the pool configuration. | ||
/// If any of required values is not set, None is returned. | ||
pub fn from_pool_config(pool_config: &crate::config::Pool) -> Option<Self> { | ||
if pool_config.is_auth_query_configured() { | ||
return Some(AuthPassthrough::new( | ||
pool_config.auth_query.as_ref().unwrap(), | ||
pool_config.auth_query_user.as_ref().unwrap(), | ||
pool_config.auth_query_password.as_ref().unwrap(), | ||
)); | ||
} | ||
|
||
None | ||
} | ||
|
||
/// Returns an AuthPassthrough given the pool settings. | ||
/// If any of required values is not set, None is returned. | ||
pub fn from_pool_settings(pool_settings: &crate::pool::PoolSettings) -> Option<Self> { | ||
let pool_config = crate::config::Pool { | ||
auth_query: pool_settings.auth_query.clone(), | ||
auth_query_password: pool_settings.auth_query_password.clone(), | ||
auth_query_user: pool_settings.auth_query_user.clone(), | ||
..Default::default() | ||
}; | ||
|
||
AuthPassthrough::from_pool_config(&pool_config) | ||
} | ||
|
||
/// Connects to server and executes auth_query for the specified address. | ||
/// If the response is a row with two columns containing the username set in the address. | ||
/// and its MD5 hash, the MD5 hash returned. | ||
/// | ||
/// Note that the query is executed, changing $1 with the name of the user | ||
/// this is so we only hold in memory (and transfer) the least amount of 'sensitive' data. | ||
/// Also, it is compatible with pgbouncer. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `address` - An Address of the server we want to connect to. The username for the hash will be obtained from this value. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use pgcat::auth_passthrough::AuthPassthrough; | ||
/// use pgcat::config::Address; | ||
/// let auth_passthrough = AuthPassthrough::new("SELECT * FROM public.user_lookup('$1');", "postgres", "postgres"); | ||
/// auth_passthrough.fetch_hash(&Address::default()); | ||
/// ``` | ||
/// | ||
pub async fn fetch_hash(&self, address: &crate::config::Address) -> Result<String, Error> { | ||
let auth_user = crate::config::User { | ||
username: self.user.clone(), | ||
password: Some(self.password.clone()), | ||
pool_size: 1, | ||
statement_timeout: 0, | ||
}; | ||
|
||
let user = &address.username; | ||
|
||
debug!("Connecting to server to obtain auth hashes."); | ||
let auth_query = self.query.replace("$1", user); | ||
match Server::exec_simple_query(address, &auth_user, &auth_query).await { | ||
Ok(password_data) => { | ||
if password_data.len() == 2 && password_data.first().unwrap() == user { | ||
if let Some(stripped_hash) = password_data.last().unwrap().to_string().strip_prefix("md5") { | ||
Ok(stripped_hash.to_string()) | ||
} | ||
else { | ||
Err(Error::AuthPassthroughError( | ||
"Obtained hash from auth_query does not seem to be in md5 format.".to_string(), | ||
)) | ||
} | ||
} else { | ||
Err(Error::AuthPassthroughError( | ||
"Data obtained from query does not follow the scheme 'user','hash'." | ||
.to_string(), | ||
)) | ||
} | ||
} | ||
Err(err) => { | ||
Err(Error::AuthPassthroughError( | ||
format!("Error trying to obtain password from auth_query, ignoring hash for user '{}'. Error: {:?}", | ||
user, err))) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great find!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@magec I created this library a while ago with intention to create a PR to use this in PgCat. Wondering if you'd take a look, https://github.com/zain-kabani/postgres-proto-rs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took this one because was the one used in postgres crate. Also, I found it pretty easy to use and is widely imused. Havent seen yours yet. Will take a look.