11use 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
109use byteorder:: { BigEndian , LittleEndian , ReadBytesExt } ;
1110use hyper:: StatusCode ;
11+ use krb5:: KrbContext ;
1212use ldap3:: { Ldap , LdapConnAsync , LdapConnSettings , LdapError , Scope , SearchEntry , ldap_escape} ;
1313use snafu:: { OptionExt , ResultExt , Snafu } ;
1414use 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
6773impl 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>
190197fn 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