Skip to content

Commit

Permalink
chore: avoid confusing TryFrom (#3565)
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun authored Mar 22, 2024
1 parent 3420a01 commit 8345f17
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/auth/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn user_provider_from_option(opt: &String) -> Result<UserProviderRef> {
match name {
STATIC_USER_PROVIDER => {
let provider =
StaticUserProvider::try_from(content).map(|p| Arc::new(p) as UserProviderRef)?;
StaticUserProvider::new(content).map(|p| Arc::new(p) as UserProviderRef)?;
Ok(provider)
}
_ => InvalidConfigSnafu {
Expand Down
22 changes: 10 additions & 12 deletions src/auth/src/user_provider/static_user_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@ use secrecy::ExposeSecret;
use snafu::{ensure, OptionExt, ResultExt};

use crate::error::{
Error, IllegalParamSnafu, InvalidConfigSnafu, IoSnafu, Result, UnsupportedPasswordTypeSnafu,
IllegalParamSnafu, InvalidConfigSnafu, IoSnafu, Result, UnsupportedPasswordTypeSnafu,
UserNotFoundSnafu, UserPasswordMismatchSnafu,
};
use crate::user_info::DefaultUserInfo;
use crate::{auth_mysql, Identity, Password, UserInfoRef, UserProvider};

pub(crate) const STATIC_USER_PROVIDER: &str = "static_user_provider";

impl TryFrom<&str> for StaticUserProvider {
type Error = Error;
pub(crate) struct StaticUserProvider {
users: HashMap<String, Vec<u8>>,
}

fn try_from(value: &str) -> Result<Self> {
impl StaticUserProvider {
pub(crate) fn new(value: &str) -> Result<Self> {
let (mode, content) = value.split_once(':').context(InvalidConfigSnafu {
value: value.to_string(),
msg: "StaticUserProviderOption must be in format `<option>:<value>`",
Expand Down Expand Up @@ -83,15 +85,11 @@ impl TryFrom<&str> for StaticUserProvider {
value: mode.to_string(),
msg: "StaticUserProviderOption must be in format `file:<path>` or `cmd:<values>`",
}
.fail(),
.fail(),
};
}
}

pub(crate) struct StaticUserProvider {
users: HashMap<String, Vec<u8>>,
}

#[async_trait]
impl UserProvider for StaticUserProvider {
fn name(&self) -> &str {
Expand Down Expand Up @@ -181,7 +179,7 @@ pub mod test {
#[tokio::test]
async fn test_authorize() {
let user_info = DefaultUserInfo::with_name("root");
let provider = StaticUserProvider::try_from("cmd:root=123456,admin=654321").unwrap();
let provider = StaticUserProvider::new("cmd:root=123456,admin=654321").unwrap();
provider
.authorize("catalog", "schema", &user_info)
.await
Expand All @@ -190,7 +188,7 @@ pub mod test {

#[tokio::test]
async fn test_inline_provider() {
let provider = StaticUserProvider::try_from("cmd:root=123456,admin=654321").unwrap();
let provider = StaticUserProvider::new("cmd:root=123456,admin=654321").unwrap();
test_authenticate(&provider, "root", "123456").await;
test_authenticate(&provider, "admin", "654321").await;
}
Expand All @@ -214,7 +212,7 @@ admin=654321",
}

let param = format!("file:{file_path}");
let provider = StaticUserProvider::try_from(param.as_str()).unwrap();
let provider = StaticUserProvider::new(param.as_str()).unwrap();
test_authenticate(&provider, "root", "123456").await;
test_authenticate(&provider, "admin", "654321").await;
}
Expand Down

0 comments on commit 8345f17

Please sign in to comment.