From 4b291aea2ea45a6bbd49662dac22915f65a66f2d Mon Sep 17 00:00:00 2001 From: fMeow Date: Wed, 20 Sep 2023 15:48:28 +0800 Subject: [PATCH 1/2] fix: `ClientBuilder` auth info drop before connect --- src/client.rs | 27 +++++++++++++++++++-------- tests/main.rs | 23 +++++++++++++++++++++++ 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/client.rs b/src/client.rs index 3c57049..c4c5936 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,5 +1,6 @@ use std::ffi::CString; use std::io; +use std::mem::MaybeUninit; use errno::{set_errno, Errno}; use hdfs_sys::*; @@ -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() { diff --git a/tests/main.rs b/tests/main.rs index d5cb708..8c9846f 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -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_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(()) +} From 20612b6c0cf8d3a49474c3788d6e7ce2b79a5f49 Mon Sep 17 00:00:00 2001 From: fMeow Date: Thu, 21 Sep 2023 09:35:32 +0800 Subject: [PATCH 2/2] ci: run `test_client_with_user` on cluster env --- .github/workflows/ci.yml | 1 + tests/main.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 211f081..b0809e3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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/ diff --git a/tests/main.rs b/tests/main.rs index 8c9846f..e4fabb8 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -370,7 +370,7 @@ fn test_client_with_user() -> Result<()> { let _ = env_logger::try_init(); dotenv::from_filename(".env").ok(); - if std::env::var("HDRS_TEST").unwrap_or_default() != "on" { + if std::env::var("HDRS_INTEGRATED_TEST").unwrap_or_default() != "on" { return Ok(()); } let name_node = env::var("HDRS_NAMENODE")?;