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: ClientBuilder auth info drop before connect #122

Merged
merged 2 commits 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
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ jobs:
env:
LD_LIBRARY_PATH: ${{ env.JAVA_HOME }}/lib/server:${{ env.HADOOP_HOME }}/lib/native
HDRS_TEST: on
HDRS_INTEGRATED_TEST: on
HDRS_NAMENODE: hdfs://localhost:8020
HDRS_WORKDIR: /tmp/hdrs/

Expand Down
27 changes: 19 additions & 8 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::ffi::CString;
use std::io;
use std::mem::MaybeUninit;

use errno::{set_errno, Errno};
use hdfs_sys::*;
Expand Down Expand Up @@ -138,23 +139,33 @@ impl ClientBuilder {

debug!("connect name node {}", &self.name_node);

let fs = unsafe {
let builder = hdfsNewBuilder();
let fs = {
let builder = unsafe { hdfsNewBuilder() };

let name_node = CString::new(self.name_node.as_bytes())?;
hdfsBuilderSetNameNode(builder, name_node.as_ptr());
let mut user = MaybeUninit::uninit();
let mut ticket_cache_path = MaybeUninit::uninit();

unsafe { hdfsBuilderSetNameNode(builder, name_node.as_ptr()) };

if let Some(v) = self.user {
let user = CString::new(v.as_bytes())?;
hdfsBuilderSetUserName(builder, user.as_ptr());
user.write(CString::new(v)?);
unsafe {
hdfsBuilderSetUserName(builder, user.assume_init_ref().as_ptr());
}
}

if let Some(v) = self.kerberos_ticket_cache_path {
let ticket_cache_path = CString::new(v.as_bytes())?;
hdfsBuilderSetKerbTicketCachePath(builder, ticket_cache_path.as_ptr());
ticket_cache_path.write(CString::new(v)?);
unsafe {
hdfsBuilderSetKerbTicketCachePath(
builder,
ticket_cache_path.assume_init_ref().as_ptr(),
);
}
}

hdfsBuilderConnect(builder)
unsafe { hdfsBuilderConnect(builder) }
};

if fs.is_null() {
Expand Down
23 changes: 23 additions & 0 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,26 @@ async fn test_futures_file() -> Result<()> {

Ok(())
}

#[test]
fn test_client_with_user() -> Result<()> {
let _ = env_logger::try_init();

dotenv::from_filename(".env").ok();
if std::env::var("HDRS_INTEGRATED_TEST").unwrap_or_default() != "on" {
return Ok(());
}
let name_node = env::var("HDRS_NAMENODE")?;
let work_dir = env::var("HDRS_WORKDIR").unwrap_or_default();

let fs = ClientBuilder::new(&name_node)
.with_user("test_user")
.connect()?;
let test_dir = format!("{}/test_dir", work_dir);
let _ = fs.create_dir(&test_dir);
let meta = fs.metadata(&test_dir);
assert!(meta.is_ok());
assert_eq!(meta.unwrap().owner(), "test_user");

Ok(())
}