Skip to content

Commit e260ef7

Browse files
committed
extract and use KERBEROS_REALM env var
1 parent 7d326d1 commit e260ef7

2 files changed

Lines changed: 32 additions & 6 deletions

File tree

rust/operator-binary/src/controller.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,14 @@ fn build_server_rolegroup_daemonset(
936936
cb_user_info_fetcher
937937
.image_from_product_image(resolved_product_image) // inherit the pull policy and pull secrets, and then...
938938
.image(user_info_fetcher_image) // ...override the image
939-
.command(vec!["stackable-opa-user-info-fetcher".to_string()])
939+
.command(vec![
940+
"/bin/bash".to_string(),
941+
"-x".to_string(),
942+
"-euo".to_string(),
943+
"pipefail".to_string(),
944+
"-c".to_string(),
945+
])
946+
.args(vec![build_user_info_fetcher_start_command()])
940947
.add_env_var("CONFIG", format!("{CONFIG_DIR}/user-info-fetcher.json"))
941948
.add_env_var("CREDENTIALS_DIR", USER_INFO_FETCHER_CREDENTIALS_DIR)
942949
.add_volume_mount(CONFIG_VOLUME_NAME, CONFIG_DIR)
@@ -1340,3 +1347,15 @@ pub fn build_recommended_labels<'a, T>(
13401347
role_group,
13411348
}
13421349
}
1350+
1351+
/// Builds the command to start the user info fetcher.
1352+
/// When using the Active Directory backend, the KERBEROS_REALM is derived from the krb5.conf file.
1353+
/// This is later used for the LDAP user search filter.
1354+
fn build_user_info_fetcher_start_command() -> String {
1355+
formatdoc! {"
1356+
if [ -f {USER_INFO_FETCHER_KERBEROS_DIR}/krb5.conf ]; then
1357+
export KERBEROS_REALM=$(grep -oP 'default_realm = \\K.*' {USER_INFO_FETCHER_KERBEROS_DIR}/krb5.conf)
1358+
fi
1359+
/sbin/stackable-opa-user-info-fetcher
1360+
"}
1361+
}

rust/user-info-fetcher/src/backend/active_directory.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::{
22
collections::{BTreeMap, HashMap},
3+
env,
34
fmt::{Display, Write},
45
io::{Cursor, Read},
56
num::ParseIntError,
@@ -58,6 +59,9 @@ pub enum Error {
5859
source: ParseSecurityIdError,
5960
user_dn: String,
6061
},
62+
63+
#[snafu(display("environment variable KERBEROS_REALM is not set"))]
64+
KerberosRealmEnvVar { source: env::VarError },
6165
}
6266

6367
impl http_error::Error for Error {
@@ -75,6 +79,7 @@ impl http_error::Error for Error {
7579
Error::InvalidPrimaryGroupRelativeId { .. } => StatusCode::INTERNAL_SERVER_ERROR,
7680
Error::UserSidHasNoSubauthorities { .. } => StatusCode::INTERNAL_SERVER_ERROR,
7781
Error::ParseUserSid { .. } => StatusCode::INTERNAL_SERVER_ERROR,
82+
Error::KerberosRealmEnvVar { .. } => StatusCode::INTERNAL_SERVER_ERROR,
7883
}
7984
}
8085
}
@@ -134,7 +139,7 @@ pub(crate) async fn get_user_info(
134139
)
135140
)
136141
}
137-
UserInfoRequest::UserInfoRequestByName(username) => user_name_filter(&username.username),
142+
UserInfoRequest::UserInfoRequestByName(username) => user_name_filter(&username.username)?,
138143
};
139144
let requested_user_attrs = [
140145
LDAP_FIELD_OBJECT_SECURITY_ID,
@@ -179,13 +184,15 @@ pub(crate) async fn get_user_info(
179184
}
180185

181186
/// Constructs a user filter that searches both the UPN as well as the sAMAccountName attributes.
187+
/// It also searches for `username@realm` in addition to just `username`.
188+
/// The realm is expected to be set in the `KERBEROS_REALM` environment variable.
182189
/// See this issue for details: <https://github.com/stackabletech/opa-operator/issues/702>
183-
fn user_name_filter(username: &str) -> String {
190+
fn user_name_filter(username: &str) -> Result<String, Error> {
184191
let escaped_username = ldap_escape(username);
185-
let realm = "SBLE.TEST"; // TODO: Replace with actual realm
186-
format!(
192+
let realm = ldap_escape(env::var("KERBEROS_REALM").context(KerberosRealmEnvVarSnafu)?);
193+
Ok(format!(
187194
"|({LDAP_FIELD_USER_NAME}={escaped_username}@{realm})({LDAP_FIELD_USER_NAME}={escaped_username})({LDAP_FIELD_SAM_ACCOUNT_NAME}={escaped_username})"
188-
)
195+
))
189196
}
190197

191198
#[tracing::instrument(

0 commit comments

Comments
 (0)