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: Do not print sensitive information to output on POLARS_VERBOSE #20797

Merged
merged 3 commits into from
Jan 20, 2025
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
17 changes: 17 additions & 0 deletions crates/polars-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ pub fn verbose() -> bool {
std::env::var("POLARS_VERBOSE").as_deref().unwrap_or("") == "1"
}

/// Prints a log message if sensitive verbose logging has been enabled.
pub fn verbose_print_sensitive<F: Fn() -> String>(create_log_message: F) {
fn do_log(create_log_message: &dyn Fn() -> String) {
if std::env::var("POLARS_VERBOSE_SENSITIVE")
.as_deref()
.unwrap_or("")
== "1"
{
// Force the message to be a single line.
let msg = create_log_message().replace('\n', "");
eprintln!("[SENSITIVE]: {}", msg)
}
}

do_log(&create_log_message)
}

pub fn get_file_prefetch_size() -> usize {
std::env::var("POLARS_PREFETCH_SIZE")
.map(|s| s.parse::<usize>().expect("integer"))
Expand Down
48 changes: 24 additions & 24 deletions crates/polars-io/src/cloud/object_store_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;
use object_store::local::LocalFileSystem;
use object_store::ObjectStore;
use once_cell::sync::Lazy;
use polars_core::config;
use polars_core::config::{self, verbose_print_sensitive};
use polars_error::{polars_bail, to_compute_err, PolarsError, PolarsResult};
use polars_utils::aliases::PlHashMap;
use polars_utils::pl_str::PlSmallStr;
Expand Down Expand Up @@ -31,24 +31,6 @@ fn err_missing_feature(feature: &str, scheme: &str) -> PolarsResult<Arc<dyn Obje

/// Get the key of a url for object store registration.
fn url_and_creds_to_key(url: &Url, options: Option<&CloudOptions>) -> Vec<u8> {
#[derive(Clone, Debug, PartialEq, Hash, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
struct C {
max_retries: usize,
#[cfg(feature = "file_cache")]
file_cache_ttl: u64,
config: Option<CloudConfig>,
#[cfg(feature = "cloud")]
credential_provider: usize,
}

#[derive(Clone, Debug, PartialEq, Hash, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
struct S {
url_base: PlSmallStr,
cloud_options: Option<C>,
}

// We include credentials as they can expire, so users will send new credentials for the same url.
let cloud_options = options.map(
|CloudOptions {
Expand All @@ -60,7 +42,7 @@ fn url_and_creds_to_key(url: &Url, options: Option<&CloudOptions>) -> Vec<u8> {
#[cfg(feature = "cloud")]
credential_provider,
}| {
C {
CloudOptions2 {
max_retries: *max_retries,
#[cfg(feature = "file_cache")]
file_cache_ttl: *file_cache_ttl,
Expand All @@ -71,19 +53,37 @@ fn url_and_creds_to_key(url: &Url, options: Option<&CloudOptions>) -> Vec<u8> {
},
);

let cache_key = S {
let cache_key = CacheKey {
url_base: format_pl_smallstr!(
"{}",
&url[url::Position::BeforeScheme..url::Position::AfterPort]
),
cloud_options,
};

if config::verbose() {
eprintln!("object store cache key: {} {:?}", url, &cache_key);
verbose_print_sensitive(|| format!("object store cache key: {} {:?}", url, &cache_key));

return pl_serialize::serialize_to_bytes(&cache_key).unwrap();

#[derive(Clone, Debug, PartialEq, Hash, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
struct CacheKey {
url_base: PlSmallStr,
cloud_options: Option<CloudOptions2>,
}

pl_serialize::serialize_to_bytes(&cache_key).unwrap()
/// Variant of CloudOptions for serializing to a cache key. The credential
/// provider is replaced by the function address.
#[derive(Clone, Debug, PartialEq, Hash, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
struct CloudOptions2 {
max_retries: usize,
#[cfg(feature = "file_cache")]
file_cache_ttl: u64,
config: Option<CloudConfig>,
#[cfg(feature = "cloud")]
credential_provider: usize,
}
}

/// Construct an object_store `Path` from a string without any encoding/decoding.
Expand Down
Loading