Skip to content

Commit 3696d22

Browse files
committed
Use 'secrecy' to avoid logging passwords
1 parent c19a902 commit 3696d22

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

Cargo.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ ldap3 = "0.11.1"
4848
lru_time_cache = "0.11.11"
4949
regex = "1.7.1"
5050
rust-ini = "0.18.0"
51+
secrecy = "0.8.0"
5152
sha2 = "0.10.6"
5253
tokio = { version = "1.24.2", features = ["full"] }
5354
tracing = "0.1.37"

src/config.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use ini::Ini;
77
use anyhow::Error;
88
use anyhow::Result;
99
use regex::Regex;
10+
use secrecy::SecretString;
1011

1112
use crate::SubQueryJoin;
1213

@@ -92,7 +93,7 @@ config_options! {
9293
ldap_server_url: String = None; "URL of the LDAP server (e.g. 'ldaps://ldap.example.com:636')",
9394
ldap_conn_timeout: f32 = Some("10.0"); "LDAP connection timeout in seconds",
9495
ldap_bind_dn: String = None; "DN of the LDAP user to bind as (e.g. 'CN=proxyuser,OU=users,DC=example,DC=com')",
95-
ldap_bind_password: String = None; "Password of the LDAP user to bind as",
96+
ldap_bind_password: SecretString = None; "Password of the LDAP user to bind as",
9697
ldap_search_base: String = None; "LDAP base DN to search in (e.g. 'OU=users,DC=example,DC=com')",
9798
ldap_scope: ldap3::Scope = Some("subtree"); "LDAP search scope. Must be 'subtree', 'onelevel' or 'base')",
9899
ldap_query: String = None; "LDAP query to use. May contain '%USERNAME%', which will be quoted and replaced.\nExample: '(&(objectClass=person)(sAMAccountName=%USERNAME%))",
@@ -250,7 +251,7 @@ pub(crate) fn parse_config(config_file: &str) -> Result<Vec<ConfigSection>, Erro
250251
ldap_server_url: get("ldap_server_url"),
251252
ldap_conn_timeout: get("ldap_conn_timeout").parse().or_else(|_| Err(parse_err("ldap_conn_timeout")))?,
252253
ldap_bind_dn: get("ldap_bind_dn"),
253-
ldap_bind_password: get("ldap_bind_password"),
254+
ldap_bind_password: get("ldap_bind_password").into(),
254255
ldap_search_base: get("ldap_search_base"),
255256
ldap_scope: match get("ldap_scope").as_str() {
256257
"subtree" => ldap3::Scope::Subtree,

src/main.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use ldap3::{LdapConnAsync, SearchEntry};
1616
use sha2::{Sha256, Digest};
1717
use lru_time_cache::LruCache;
1818
use tokio::sync::{Mutex, RwLock};
19+
use secrecy::ExposeSecret;
1920

2021
mod config;
2122

@@ -118,7 +119,7 @@ async fn ldap_query(
118119
ldap3::drive!(conn);
119120

120121
let bind_dn = conf.ldap_bind_dn.as_str();
121-
let bind_pw = conf.ldap_bind_password.as_str();
122+
let bind_pw = conf.ldap_bind_password.expose_secret().as_str();
122123
ldap.simple_bind(bind_dn, bind_pw).await?.success()?;
123124

124125
let (rs, _res) = match ldap.search(
@@ -247,8 +248,14 @@ async fn http_handler(req: Request<Body>, ctx: Arc<ReqContext>) -> Result<Respon
247248

248249
// Check LDAP (and cache)
249250
let cache = ctx.cache.get(conf.section.as_str()).unwrap().clone();
250-
match ldap_query(
251-
conf.section.clone(), username.into(), ctx.config.clone(), cache, Arc::new(RwLock::new(HashSet::new()))).await {
251+
let ldap_res = span.in_scope(|| async { ldap_query(
252+
conf.section.clone(),
253+
username.into(),
254+
ctx.config.clone(),
255+
cache,
256+
Arc::new(RwLock::new(HashSet::new()))).await }).await;
257+
258+
match ldap_res {
252259
Err(e) => {
253260
span.in_scope(|| { tracing::error!("LDAP error: {:?}", e); });
254261
return Response::builder().status(StatusCode::BAD_GATEWAY).body(Body::from("LDAP error"))

0 commit comments

Comments
 (0)