Skip to content
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

fix: check the password on leader #435

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 7 additions & 42 deletions xline/src/server/auth_server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::marker::PhantomData;
use std::sync::Arc;

use curp::{client::Client, cmd::generate_propose_id};
Expand Down Expand Up @@ -27,7 +28,7 @@ use crate::{
AuthUserRevokeRoleRequest, AuthUserRevokeRoleResponse, AuthenticateRequest,
AuthenticateResponse, RequestWrapper, ResponseWrapper,
},
storage::{storage_api::StorageApi, AuthStore},
storage::storage_api::StorageApi,
};

/// Auth Server
Expand All @@ -36,12 +37,12 @@ pub(crate) struct AuthServer<S>
where
S: StorageApi,
{
/// Auth storage
storage: Arc<AuthStore<S>>,
/// Consensus client
client: Arc<Client<Command>>,
/// Server name
name: String,
/// Phantom
phantom: PhantomData<S>,
}

/// Get token from metadata
Expand All @@ -57,15 +58,11 @@ where
S: StorageApi,
{
/// New `AuthServer`
pub(crate) fn new(
storage: Arc<AuthStore<S>>,
client: Arc<Client<Command>>,
name: String,
) -> Self {
pub(crate) fn new(client: Arc<Client<Command>>, name: String) -> Self {
Self {
storage,
client,
name,
phantom: PhantomData,
}
}

Expand Down Expand Up @@ -97,17 +94,6 @@ where
hashed_password.to_string()
}

/// Check password in storage
pub(crate) fn check_password(
&self,
username: &str,
password: &str,
) -> Result<i64, tonic::Status> {
self.storage
.check_password(username, password)
.map_err(Into::into)
}

/// Propose request and make a response
async fn handle_req<Req, Res>(
&self,
Expand Down Expand Up @@ -162,28 +148,7 @@ where
request: tonic::Request<AuthenticateRequest>,
) -> Result<tonic::Response<AuthenticateResponse>, tonic::Status> {
debug!("Receive AuthenticateRequest {:?}", request);
loop {
let checked_revision =
self.check_password(&request.get_ref().name, &request.get_ref().password)?;
let mut authenticate_req = request.get_ref().clone();
authenticate_req.password = String::new();

let (res, sync_res) = self
.propose(tonic::Request::new(authenticate_req), false)
.await?;

if checked_revision == self.storage.revision() {
if let Some(sync_res) = sync_res {
let revision = sync_res.revision();
debug!("Get revision {:?} for AuthDisableResponse", revision);
let mut res: AuthenticateResponse = res.into_inner().into();
if let Some(mut header) = res.header.as_mut() {
header.revision = revision;
}
return Ok(tonic::Response::new(res));
}
}
}
self.handle_req(request, false).await
}

async fn user_add(
Expand Down
2 changes: 1 addition & 1 deletion xline/src/server/lease_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ where
let token = get_token(request.metadata());
let wrapper = RequestWithToken::new_with_token(request.into_inner().into(), token);
let cmd = command_from_request_wrapper(
generate_propose_id(self.cluster_info.self_name()),
generate_propose_id(self.cluster_info.self_name().as_str()),
wrapper,
Some(self.lease_storage.as_ref()),
);
Expand Down
4 changes: 2 additions & 2 deletions xline/src/server/xline_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,12 +376,12 @@ impl XlineServer {
),
LeaseServer::new(
lease_storage,
Arc::clone(&auth_storage),
auth_storage,
Arc::clone(&client),
id_gen,
Arc::clone(&self.cluster_info),
),
AuthServer::new(auth_storage, client, self.cluster_info.self_name()),
AuthServer::new(client, self.cluster_info.self_name()),
WatchServer::new(
watcher,
Arc::clone(&header_gen),
Expand Down
5 changes: 3 additions & 2 deletions xline/src/storage/auth_store/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ where
if !self.is_enabled() {
return Err(ExecuteError::AuthNotEnabled);
}
self.check_password(&req.name, &req.password)?;
let token = self.assign(&req.name)?;
Ok(AuthenticateResponse {
header: Some(self.header_gen.gen_auth_header()),
Expand Down Expand Up @@ -868,7 +869,7 @@ where
&self,
username: &str,
password: &str,
) -> Result<i64, ExecuteError> {
Phoenix500526 marked this conversation as resolved.
Show resolved Hide resolved
) -> Result<(), ExecuteError> {
if !self.is_enabled() {
return Err(ExecuteError::AuthNotEnabled);
}
Expand All @@ -885,7 +886,7 @@ where
.verify_password(password.as_bytes(), &hash)
.map_err(|_ignore| ExecuteError::AuthFailed)?;

Ok(self.revision())
Ok(())
}

/// Check if the request need admin permission
Expand Down
Loading