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

logs with redacted password in URL #379

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
3 changes: 2 additions & 1 deletion limitador-server/sandbox/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
*.key
*.pem
*.csr
report.html
*.srl
report.html
34 changes: 33 additions & 1 deletion limitador-server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,27 @@

use crate::envoy_rls::server::RateLimitHeaders;
use limitador::storage;
use std::fmt;
use tracing::level_filters::LevelFilter;
use url::Url;

pub fn redacted_url(url: String) -> String {
return match Url::parse(url.as_str()) {
Ok(url_object) => {
if url_object.password().is_some() {
let mut owned_url = url_object.clone();
if owned_url.set_password(Some("****")).is_ok() {
String::from(owned_url)
} else {
url.clone()
}
} else {
url.clone()
}
}
Err(_) => url.clone(),
};
}

#[derive(Debug)]
pub struct Configuration {
Expand Down Expand Up @@ -164,12 +184,24 @@ pub struct DiskStorageConfiguration {
pub optimization: storage::disk::OptimizeFor,
}

#[derive(PartialEq, Eq, Debug)]
#[derive(PartialEq, Eq)]
pub struct RedisStorageConfiguration {
pub url: String,
pub cache: Option<RedisStorageCacheConfiguration>,
}

impl fmt::Debug for RedisStorageConfiguration {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Foo")
.field("cache", &self.cache)
.field(
"url",
&format_args!("{}", redacted_url(self.url.clone()).as_str()),
)
.finish()
}
}

#[derive(PartialEq, Eq, Debug)]
pub struct RedisStorageCacheConfiguration {
pub batch_size: usize,
Expand Down
8 changes: 5 additions & 3 deletions limitador-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extern crate clap;
#[cfg(feature = "distributed_storage")]
use crate::config::DistributedStorageConfiguration;
use crate::config::{
Configuration, DiskStorageConfiguration, InMemoryStorageConfiguration,
redacted_url, Configuration, DiskStorageConfiguration, InMemoryStorageConfiguration,
RedisStorageCacheConfiguration, RedisStorageConfiguration, StorageConfiguration,
};
use crate::envoy_rls::server::{run_envoy_rls_server, RateLimitHeaders};
Expand Down Expand Up @@ -121,7 +121,8 @@ impl Limiter {
AsyncRedisStorage::new(redis_url)
.await
.unwrap_or_else(|err| {
eprintln!("Failed to connect to Redis at {redis_url}: {err}");
let redacted_redis_url = redacted_url(String::from(redis_url));
eprintln!("Failed to connect to Redis at {redacted_redis_url}: {err}");
process::exit(1)
})
}
Expand All @@ -139,7 +140,8 @@ impl Limiter {
.response_timeout(Duration::from_millis(cache_cfg.response_timeout));

cached_redis_storage.build().await.unwrap_or_else(|err| {
eprintln!("Failed to connect to Redis at {redis_url}: {err}");
let redacted_redis_url = redacted_url(String::from(redis_url));
eprintln!("Failed to connect to Redis at {redacted_redis_url}: {err}");
Copy link
Member

@alexsnaps alexsnaps Oct 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The down side is that if err ever contains that password, we'd "reintroduce" the bug. Or do we know that is guarantee provided by the crate?
E.g. what if redis is restarted with a new password and the reconnect path fails? That might be worth a quick (even manual) test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added verification steps with a manual test.

I would never expect the crate to return the url of the connection serialized in the error string, but who knows maybe one day. Today, according to my tests, it is not doing so. Would you suggest to apply some regex replace to the string before writing it to stderr?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I would have possibly dealt with that error differently... but if this is fine, well... it's fine 🙌

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let me know how you would dealt with the error otherwise. Happy to consider it

process::exit(1)
})
}
Expand Down
Loading