@@ -177,13 +177,20 @@ def _auth_key(nonce: str, username: str, password: str) -> str:
177177 return md5hash .hexdigest ()
178178
179179
180- def _canonicalize_hostname (hostname : str ) -> str :
180+ def _canonicalize_hostname (hostname : str , option : str | bool ) -> str :
181181 """Canonicalize hostname following MIT-krb5 behavior."""
182182 # https://github.com/krb5/krb5/blob/d406afa363554097ac48646a29249c04f498c88e/src/util/k5test.py#L505-L520
183+ if option in [False , "none" ]:
184+ return hostname
185+
183186 af , socktype , proto , canonname , sockaddr = socket .getaddrinfo (
184187 hostname , None , 0 , 0 , socket .IPPROTO_TCP , socket .AI_CANONNAME
185188 )[0 ]
186189
190+ # For forward just to resolve the cname as dns.lookup() will not return it.
191+ if option == "forward" :
192+ return canonname .lower ()
193+
187194 try :
188195 name = socket .getnameinfo (sockaddr , socket .NI_NAMEREQD )
189196 except socket .gaierror :
@@ -205,9 +212,8 @@ async def _authenticate_gssapi(credentials: MongoCredential, conn: AsyncConnecti
205212 props = credentials .mechanism_properties
206213 # Starting here and continuing through the while loop below - establish
207214 # the security context. See RFC 4752, Section 3.1, first paragraph.
208- host = conn .address [0 ]
209- if props .canonicalize_host_name :
210- host = _canonicalize_hostname (host )
215+ host = props .service_host or conn .address [0 ]
216+ host = _canonicalize_hostname (host , props .canonicalize_host_name )
211217 service = props .service_name + "@" + host
212218 if props .service_realm is not None :
213219 service = service + "@" + props .service_realm
0 commit comments