11use 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
6367impl 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