Skip to content

Commit 23bbec9

Browse files
committed
[Rust] Fix custom SecretsProvider impl requiring data to be passed back in get_data
We want to provide no value in the case of the key not having an associated value
1 parent ab6f252 commit 23bbec9

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

rust/src/secrets_provider.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::string::{BnString, IntoCStr};
88

99
pub trait SecretsProvider {
1010
fn has_data(&mut self, key: &str) -> bool;
11-
fn get_data(&mut self, key: &str) -> String;
11+
fn get_data(&mut self, key: &str) -> Option<String>;
1212
fn store_data(&mut self, key: &str, data: &str) -> bool;
1313
fn delete_data(&mut self, key: &str) -> bool;
1414
}
@@ -126,8 +126,10 @@ unsafe extern "C" fn cb_get_data<C: SecretsProvider>(
126126
key: *const c_char,
127127
) -> *mut c_char {
128128
let ctxt: &mut C = &mut *(ctxt as *mut C);
129-
let result = ctxt.get_data(&CStr::from_ptr(key).to_string_lossy());
130-
BnString::into_raw(BnString::new(result))
129+
match ctxt.get_data(&CStr::from_ptr(key).to_string_lossy()) {
130+
Some(result) => BnString::into_raw(BnString::new(result)),
131+
None => std::ptr::null_mut(),
132+
}
131133
}
132134

133135
unsafe extern "C" fn cb_store_data<C: SecretsProvider>(

rust/tests/secrets_provider.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ impl SecretsProvider for MySecretsProvider {
1717
key == "my_key"
1818
}
1919

20-
fn get_data(&mut self, key: &str) -> String {
21-
if key == "my_key" { "my_value" } else { "" }.to_string()
20+
fn get_data(&mut self, key: &str) -> Option<String> {
21+
if key == "my_key" {
22+
Some("my_value".to_string())
23+
} else {
24+
None
25+
}
2226
}
2327

2428
fn store_data(&mut self, _key: &str, _data: &str) -> bool {

0 commit comments

Comments
 (0)