Skip to content

Commit

Permalink
remove futures' dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
uslon committed Feb 26, 2024
1 parent bbfe5bd commit b2dbf51
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 41 deletions.
1 change: 0 additions & 1 deletion ydb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ force-exhaustive-all=[] # The feature disable all non_exhaustive attributes in y
async-trait="0.1"
derivative="2"
derive_builder="0.12.0"
futures = "0.3"
futures-util = "0.3"
http = "0.2"
itertools="0.10"
Expand Down
66 changes: 40 additions & 26 deletions ydb/src/auth_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ use tracing::trace;
use tracing_test::traced_test;

use crate::{
test_helpers::CONNECTION_STRING,
test_integration_helper::create_password_client,
Query,
YdbResult,
Transaction,
credentials::StaticCredentialsAuth
credentials::StaticCredentialsAuth, pub_traits::Credentials, test_helpers::CONNECTION_STRING,
test_integration_helper::create_password_client, Query, Transaction, YdbResult,
};

#[test]
Expand All @@ -17,13 +13,32 @@ fn auth_success_test() -> YdbResult<()> {
let uri = http::uri::Uri::from_static(&(CONNECTION_STRING));

let database = uri.path().to_string();
let up_auth = StaticCredentialsAuth::new(
"root".to_string(),
"1234".to_string(),
uri, database);
let up_auth = StaticCredentialsAuth::new("root".to_string(), "1234".to_string(), uri, database);

let token_str = up_auth.create_token()?.token;

trace!("got token: `{}'", token_str);
if token_str.is_empty() {
panic!("got the empty token on the presumably successful auth request");
}

Ok(())
}

#[tokio::test]
#[traced_test]
#[ignore] // YDB access is necessary
async fn auth_async_success_test() -> YdbResult<()> {
let uri = http::uri::Uri::from_static(&(CONNECTION_STRING));

let database = uri.path().to_string();
let up_auth = StaticCredentialsAuth::new("root".to_string(), "1234".to_string(), uri, database);

let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
let token_str = rt.block_on(up_auth.acquire_token())?;
let token_str = std::thread::spawn(move || up_auth.create_token())
.join()
.unwrap()
.unwrap()
.token;

trace!("got token: `{}'", token_str);
if token_str.is_empty() {
Expand All @@ -43,7 +58,9 @@ async fn wrong_username_test() {
let up_auth = StaticCredentialsAuth::new(
"wr0n9_u$ern@me".to_string(),
"1234".to_string(),
uri, database);
uri,
database,
);

up_auth.acquire_token().await.unwrap();
}
Expand All @@ -58,7 +75,9 @@ async fn wrong_password_test() {
let up_auth = StaticCredentialsAuth::new(
"root".to_string(),
"wr0n9_p@$$w0rd".to_string(),
uri, database);
uri,
database,
);

up_auth.acquire_token().await.unwrap();
}
Expand All @@ -69,18 +88,13 @@ async fn wrong_password_test() {
async fn password_client_test() -> YdbResult<()> {
let client = create_password_client().await?;
let two: i32 = client
.table_client() // create table client
.retry_transaction(|mut t: Box<dyn Transaction>| async move {
// send the query to the database
let res = t.query(Query::from("SELECT 2")).await?;

// read exactly one result from the db
let field_val: i32 = res.into_only_row()?.remove_field(0)?.try_into()?;

// return result
Ok(field_val)
})
.await?;
.table_client() // create table client
.retry_transaction(|mut t: Box<dyn Transaction>| async move {
let res = t.query(Query::from("SELECT 2")).await?;
let field_val: i32 = res.into_only_row()?.remove_field(0)?.try_into()?;
Ok(field_val)
})
.await?;

assert_eq!(two, 2);
Ok(())
Expand Down
19 changes: 14 additions & 5 deletions ydb/src/client_builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::client_common::{DBCredentials, TokenCache};
use crate::credentials::{credencials_ref, CredentialsRef, GCEMetadata, StaticToken, StaticCredentialsAuth};
use crate::credentials::{
credencials_ref, CredentialsRef, GCEMetadata, StaticCredentialsAuth, StaticToken,
};
use crate::dicovery_pessimization_interceptor::DiscoveryPessimizationInterceptor;
use crate::discovery::{Discovery, TimerDiscovery};
use crate::errors::{YdbError, YdbResult};
Expand Down Expand Up @@ -120,10 +122,14 @@ fn token_static_password(uri: &str, mut client_builder: ClientBuilder) -> YdbRes
}
}
if username.is_none() {
return Err(YdbError::Custom("username was not provided for password authentication".to_string()));
return Err(YdbError::Custom(
"username was not provided for password authentication".to_string(),
));
}
if password.is_none() {
return Err(YdbError::Custom("password was not provided for password authentication".to_string()));
return Err(YdbError::Custom(
"password was not provided for password authentication".to_string(),
));
}
let username = username.unwrap();
let password = password.unwrap();
Expand Down Expand Up @@ -295,11 +301,14 @@ mod test {
#[test]
fn password_without_username() -> YdbResult<()> {
let builder = ClientBuilder::new_from_connection_string(
"http://asd:222/qwe1?token_static_password=hello");
"http://asd:222/qwe1?token_static_password=hello",
);

match builder {
Err(YdbError::Custom(_)) => Ok(()),
_ => Err(YdbError::Custom("expected connection string parsing failure".to_string())),
_ => Err(YdbError::Custom(
"expected connection string parsing failure".to_string(),
)),
}
}
}
16 changes: 10 additions & 6 deletions ydb/src/credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,13 @@ impl StaticCredentialsAuth {
MultiInterceptor::new(),
);

let mut auth_client = futures::executor::block_on(
empty_connection_manager.get_auth_service(RawAuthClient::new)).unwrap();
let mut auth_client = empty_connection_manager
.get_auth_service(RawAuthClient::new)
.await
.unwrap();

// TODO: add configurable authorization request timeout
let raw_request = RawLoginRequest{
let raw_request = RawLoginRequest {
operation_params: TimeoutSettings::default().operation_params(),
user: self.username.clone(),
password: self.password.clone(),
Expand All @@ -267,7 +269,8 @@ impl StaticCredentialsAuth {
}

pub fn new(username: String, password: String, endpoint: Uri, database: String) -> Self {
Self {username,
Self {
username,
password,
database,
endpoint,
Expand All @@ -276,7 +279,8 @@ impl StaticCredentialsAuth {
}

impl Credentials for StaticCredentialsAuth {
fn create_token(&self) -> YdbResult<TokenInfo> {
Ok(TokenInfo::token(futures::executor::block_on(self.acquire_token())?))
#[tokio::main(flavor = "current_thread")]
async fn create_token(&self) -> YdbResult<TokenInfo> {
Ok(TokenInfo::token(self.acquire_token().await?))
}
}
9 changes: 7 additions & 2 deletions ydb/src/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ pub(crate) fn test_client_builder() -> ClientBuilder {
}

pub(crate) fn get_passworded_connection_string() -> String {
Url::parse_with_params(&CONNECTION_STRING,
&[("password", "1234"), ("username", "root")]).unwrap().as_str().to_string()
Url::parse_with_params(
&CONNECTION_STRING,
&[("password", "1234"), ("username", "root")],
)
.unwrap()
.as_str()
.to_string()
}

pub(crate) fn test_with_password_builder() -> ClientBuilder {
Expand Down
2 changes: 1 addition & 1 deletion ydb/src/test_integration_helper.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::client::Client;
use crate::client::TimeoutSettings;
use crate::errors::YdbResult;
use crate::test_helpers::{test_with_password_builder, test_client_builder};
use crate::test_helpers::{test_client_builder, test_with_password_builder};
use async_once::AsyncOnce;
use lazy_static::lazy_static;
use std::sync::Arc;
Expand Down

0 comments on commit b2dbf51

Please sign in to comment.