Skip to content

Commit

Permalink
readyset-data: Integrate 'url' crate and refine authentication code
Browse files Browse the repository at this point in the history
This commit addresses the following issues:
- Included the 'url' crate in Cargo.toml
- Adjusted the approach for processing user credentials from the
  upstream DB URL by using the 'url' crate to parse the URL and extract
  the username and password components.

Change-Id: Ic59d96884571accd19aaf13b8b8f51e20f97299b
  • Loading branch information
tbjuhasz committed May 7, 2024
1 parent d5c5daf commit 8cf9485
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions readyset/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ readyset-tracing = { path = "../readyset-tracing" }
readyset-version = { path = "../readyset-version" }
replicators = { path = "../replicators" }
serde_json = "1.0.89"
url = "2.2.2"


[dev-dependencies]
Expand Down
57 changes: 34 additions & 23 deletions readyset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
pub mod mysql;
pub mod psql;
mod query_logger;

use std::borrow::Cow;
use std::collections::HashMap;
use std::fs::remove_dir_all;
use std::io;
Expand Down Expand Up @@ -64,6 +66,8 @@ use tokio::time::{sleep, timeout};
use tokio_stream::wrappers::TcpListenerStream;
use tracing::{debug, debug_span, error, info, info_span, span, warn, Level};
use tracing_futures::Instrument;
use url::form_urlencoded::{byte_serialize, parse};


// readyset_alloc initializes the global allocator
extern crate readyset_alloc;
Expand Down Expand Up @@ -555,6 +559,7 @@ where
out
}


impl<H> NoriaAdapter<H>
where
H: ConnectionHandler + Clone + Send + Sync + 'static,
Expand Down Expand Up @@ -602,37 +607,43 @@ where
.as_ref()
.and_then(|s| s.parse::<DatabaseURL>().ok());

let decoded_username = upstream_url.as_ref().and_then(|url|
parse(url.user()?.as_bytes())
.map(|(key, _)| key.to_string())
.next()
);

let decoded_password = upstream_url.as_ref().and_then(|url| url.password()).and_then(|password|
parse(password.as_bytes())
.map(|(key, _)| key.to_string())
.next()
);

match (
(options.username, options.password),
(
upstream_url.as_ref().and_then(|url| url.user()),
upstream_url.as_ref().and_then(|url| url.password()),
),
) {
// --username and --password
(decoded_username, decoded_password)
) {
((Some(user), Some(pass)), _) => (user, pass.0),
// --password, username from url
((None, Some(pass)), (Some(user), _)) => (user.to_owned(), pass.0),
// username and password from url
(_, (Some(user), Some(pass))) => (user.to_owned(), pass.to_owned()),
((None, Some(pass)), (Some(user), _)) => (user, pass.0),
(_, (Some(user), Some(pass))) => (user, pass),
_ => {
if upstream_url.is_some() {
bail!(
"Failed to infer ReadySet username and password from \
upstream DB URL. Please ensure they are present and \
correctly formatted as follows: \
<protocol>://<username>:<password>@<address>[:<port>][/<database>] \
You can also configure ReadySet to accept credentials \
different from those of your upstream database via \
--username/-u and --password/-p, or use \
--allow-unauthenticated-connections."
)
"Failed to infer ReadySet username and password from \
upstream DB URL. Please ensure they are present and \
correctly formatted as follows: \
<protocol>://<username>:<password>@<address>[:<port>][/<database>] \
You can also configure ReadySet to accept credentials \
different from those of your upstream database via \
--username/-u and --password/-p, or use \
--allow-unauthenticated-connections."
)
} else {
bail!(
"Must specify --username/-u and --password/-p if one of \
--allow-unauthenticated-connections or --upstream-db-url is not \
passed"
)
"Must specify --username/-u and --password/-p if one of \
--allow-unauthenticated-connections or --upstream-db-url is not \
passed"
)
}
}
}
Expand Down

0 comments on commit 8cf9485

Please sign in to comment.