Skip to content

Commit f2d8d2e

Browse files
committed
use krb5-rs to read realm name
1 parent 1ccd2df commit f2d8d2e

File tree

5 files changed

+86
-26
lines changed

5 files changed

+86
-26
lines changed

Cargo.lock

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

Cargo.nix

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

rust/operator-binary/src/controller.rs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -936,14 +936,7 @@ 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![
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()])
939+
.command(vec!["stackable-opa-user-info-fetcher".to_string()])
947940
.add_env_var("CONFIG", format!("{CONFIG_DIR}/user-info-fetcher.json"))
948941
.add_env_var("CREDENTIALS_DIR", USER_INFO_FETCHER_CREDENTIALS_DIR)
949942
.add_volume_mount(CONFIG_VOLUME_NAME, CONFIG_DIR)
@@ -1347,15 +1340,3 @@ pub fn build_recommended_labels<'a, T>(
13471340
role_group,
13481341
}
13491342
}
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/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ publish = false
1111
[dependencies]
1212
stackable-opa-operator = { path = "../operator-binary" }
1313
stackable-operator.workspace = true
14+
krb5 = { path = "../../../krb5-rs/rust/krb5"}
1415

1516
axum.workspace = true
1617
base64.workspace = true

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::{
22
collections::{BTreeMap, HashMap},
3-
env,
43
fmt::{Display, Write},
54
io::{Cursor, Read},
65
num::ParseIntError,
@@ -9,6 +8,7 @@ use std::{
98

109
use byteorder::{BigEndian, LittleEndian, ReadBytesExt};
1110
use hyper::StatusCode;
11+
use krb5::KrbContext;
1212
use ldap3::{Ldap, LdapConnAsync, LdapConnSettings, LdapError, Scope, SearchEntry, ldap_escape};
1313
use snafu::{OptionExt, ResultExt, Snafu};
1414
use stackable_operator::commons::tls_verification::TlsClientDetails;
@@ -60,8 +60,14 @@ pub enum Error {
6060
user_dn: String,
6161
},
6262

63-
#[snafu(display("environment variable KERBEROS_REALM is not set"))]
64-
KerberosRealmEnvVar { source: env::VarError },
63+
#[snafu(display("failed to create Kerberos context"))]
64+
KerberosContext { source: krb5::Error },
65+
66+
#[snafu(display("failed to get Kerberos realm"))]
67+
KerberosRealm { source: krb5::Error },
68+
69+
#[snafu(display("failed to get Kerberos realm name"))]
70+
KerberosRealmName { source: std::str::Utf8Error },
6571
}
6672

6773
impl http_error::Error for Error {
@@ -79,7 +85,9 @@ impl http_error::Error for Error {
7985
Error::InvalidPrimaryGroupRelativeId { .. } => StatusCode::INTERNAL_SERVER_ERROR,
8086
Error::UserSidHasNoSubauthorities { .. } => StatusCode::INTERNAL_SERVER_ERROR,
8187
Error::ParseUserSid { .. } => StatusCode::INTERNAL_SERVER_ERROR,
82-
Error::KerberosRealmEnvVar { .. } => StatusCode::INTERNAL_SERVER_ERROR,
88+
Error::KerberosContext { .. } => StatusCode::INTERNAL_SERVER_ERROR,
89+
Error::KerberosRealm { .. } => StatusCode::INTERNAL_SERVER_ERROR,
90+
Error::KerberosRealmName { .. } => StatusCode::INTERNAL_SERVER_ERROR,
8391
}
8492
}
8593
}
@@ -185,16 +193,24 @@ pub(crate) async fn get_user_info(
185193

186194
/// Constructs a user filter that searches both the UPN as well as the sAMAccountName attributes.
187195
/// 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.
189196
/// See this issue for details: <https://github.com/stackabletech/opa-operator/issues/702>
190197
fn user_name_filter(username: &str) -> Result<String, Error> {
191198
let escaped_username = ldap_escape(username);
192-
let realm = ldap_escape(env::var("KERBEROS_REALM").context(KerberosRealmEnvVarSnafu)?);
199+
let realm = ldap_escape(default_realm_name()?);
193200
Ok(format!(
194201
"|({LDAP_FIELD_USER_NAME}={escaped_username}@{realm})({LDAP_FIELD_USER_NAME}={escaped_username})({LDAP_FIELD_SAM_ACCOUNT_NAME}={escaped_username})"
195202
))
196203
}
197204

205+
fn default_realm_name() -> Result<String, Error> {
206+
let krb_context = KrbContext::new().context(KerberosContextSnafu)?;
207+
let krb_realm = krb_context.default_realm().context(KerberosRealmSnafu)?;
208+
Ok(krb_realm
209+
.to_str()
210+
.context(KerberosRealmNameSnafu)?
211+
.to_string())
212+
}
213+
198214
#[tracing::instrument(
199215
skip(
200216
ldap,

0 commit comments

Comments
 (0)