Skip to content

Commit 85d7eed

Browse files
authored
gssapi: use hostbased_service name type (#1167)
This is used by libpq and allows us to skip canonicalization of host name, which was making a blocking DNS lookup. Similarly, don't canonicalize host name for SSPI, since this is not done by libpq.
1 parent 98aebf1 commit 85d7eed

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

asyncpg/protocol/coreproto.pxd

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ cdef class CoreProtocol:
140140
cdef _auth_password_message_sasl_continue(self, bytes server_response)
141141
cdef _auth_gss_init_gssapi(self)
142142
cdef _auth_gss_init_sspi(self, bint negotiate)
143-
cdef _auth_gss_get_spn(self)
143+
cdef _auth_gss_get_service(self)
144144
cdef _auth_gss_step(self, bytes server_response)
145145

146146
cdef _write(self, buf)

asyncpg/protocol/coreproto.pyx

+8-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77

88
import hashlib
9-
import socket
109

1110

1211
include "scram.pyx"
@@ -728,8 +727,11 @@ cdef class CoreProtocol:
728727
'use asyncpg with Kerberos/GSSAPI/SSPI authentication'
729728
) from None
730729

730+
service_name, host = self._auth_gss_get_service()
731731
self.gss_ctx = gssapi.SecurityContext(
732-
name=gssapi.Name(self._auth_gss_get_spn()), usage='initiate')
732+
name=gssapi.Name(
733+
f'{service_name}@{host}', gssapi.NameType.hostbased_service),
734+
usage='initiate')
733735

734736
cdef _auth_gss_init_sspi(self, bint negotiate):
735737
try:
@@ -740,22 +742,20 @@ cdef class CoreProtocol:
740742
'use asyncpg with Kerberos/GSSAPI/SSPI authentication'
741743
) from None
742744

745+
service_name, host = self._auth_gss_get_service()
743746
self.gss_ctx = sspilib.ClientSecurityContext(
744-
target_name=self._auth_gss_get_spn(),
747+
target_name=f'{service_name}/{host}',
745748
credential=sspilib.UserCredential(
746749
protocol='Negotiate' if negotiate else 'Kerberos'))
747750

748-
cdef _auth_gss_get_spn(self):
751+
cdef _auth_gss_get_service(self):
749752
service_name = self.con_params.krbsrvname or 'postgres'
750-
# find the canonical name of the server host
751753
if isinstance(self.address, str):
752754
raise apg_exc.InternalClientError(
753755
'GSSAPI/SSPI authentication is only supported for TCP/IP '
754756
'connections')
755757

756-
host = self.address[0]
757-
host_cname = socket.gethostbyname_ex(host)[0]
758-
return f'{service_name}/{host_cname}'
758+
return service_name, self.address[0]
759759

760760
cdef _auth_gss_step(self, bytes server_response):
761761
cdef:

0 commit comments

Comments
 (0)