Skip to content

Commit c88ae79

Browse files
authored
PYTHON-3879 Rename SocketInfo to Connection (#1329)
1 parent c945ec6 commit c88ae79

35 files changed

+731
-735
lines changed

doc/common-issues.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ will receive the following error::
7878
File "/Library/Python/2.7/site-packages/pymongo/collection.py", line 1560, in count
7979
return self._count(cmd, collation, session)
8080
File "/Library/Python/2.7/site-packages/pymongo/collection.py", line 1504, in _count
81-
with self._socket_for_reads() as (sock_info, slave_ok):
81+
with self._socket_for_reads() as (connection, slave_ok):
8282
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 17, in __enter__
8383
return self.gen.next()
8484
File "/Library/Python/2.7/site-packages/pymongo/mongo_client.py", line 982, in _socket_for_reads

pymongo/aggregation.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from pymongo.collection import Collection
3030
from pymongo.command_cursor import CommandCursor
3131
from pymongo.database import Database
32-
from pymongo.pool import SocketInfo
32+
from pymongo.pool import Connection
3333
from pymongo.read_preferences import _ServerMode
3434
from pymongo.server import Server
3535
from pymongo.typings import _Pipeline
@@ -52,7 +52,7 @@ def __init__(
5252
explicit_session: bool,
5353
let: Optional[Mapping[str, Any]] = None,
5454
user_fields: Optional[MutableMapping[str, Any]] = None,
55-
result_processor: Optional[Callable[[Mapping[str, Any], SocketInfo], None]] = None,
55+
result_processor: Optional[Callable[[Mapping[str, Any], Connection], None]] = None,
5656
comment: Any = None,
5757
) -> None:
5858
if "explain" in options:
@@ -134,7 +134,7 @@ def get_cursor(
134134
self,
135135
session: ClientSession,
136136
server: Server,
137-
sock_info: SocketInfo,
137+
conn: Connection,
138138
read_preference: _ServerMode,
139139
) -> CommandCursor:
140140
# Serialize command.
@@ -146,7 +146,7 @@ def get_cursor(
146146
# - server version is >= 4.2 or
147147
# - server version is >= 3.2 and pipeline doesn't use $out
148148
if ("readConcern" not in cmd) and (
149-
not self._performs_write or (sock_info.max_wire_version >= 8)
149+
not self._performs_write or (conn.max_wire_version >= 8)
150150
):
151151
read_concern = self._target.read_concern
152152
else:
@@ -161,7 +161,7 @@ def get_cursor(
161161
write_concern = None
162162

163163
# Run command.
164-
result = sock_info.command(
164+
result = conn.command(
165165
self._database.name,
166166
cmd,
167167
read_preference,
@@ -176,7 +176,7 @@ def get_cursor(
176176
)
177177

178178
if self._result_processor:
179-
self._result_processor(result, sock_info)
179+
self._result_processor(result, conn)
180180

181181
# Extract cursor from result or mock/fake one if necessary.
182182
if "cursor" in result:
@@ -193,14 +193,14 @@ def get_cursor(
193193
cmd_cursor = self._cursor_class(
194194
self._cursor_collection(cursor),
195195
cursor,
196-
sock_info.address,
196+
conn.address,
197197
batch_size=self._batch_size or 0,
198198
max_await_time_ms=self._max_await_time_ms,
199199
session=session,
200200
explicit_session=self._explicit_session,
201201
comment=self._options.get("comment"),
202202
)
203-
cmd_cursor._maybe_pin_connection(sock_info)
203+
cmd_cursor._maybe_pin_connection(conn)
204204
return cmd_cursor
205205

206206

pymongo/auth.py

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
if TYPE_CHECKING:
3737
from pymongo.hello import Hello
38-
from pymongo.pool import SocketInfo
38+
from pymongo.pool import Connection
3939

4040
HAVE_KERBEROS = True
4141
_USE_PRINCIPAL = False
@@ -220,9 +220,7 @@ def _authenticate_scram_start(
220220
return nonce, first_bare, cmd
221221

222222

223-
def _authenticate_scram(
224-
credentials: MongoCredential, sock_info: SocketInfo, mechanism: str
225-
) -> None:
223+
def _authenticate_scram(credentials: MongoCredential, conn: Connection, mechanism: str) -> None:
226224
"""Authenticate using SCRAM."""
227225
username = credentials.username
228226
if mechanism == "SCRAM-SHA-256":
@@ -239,13 +237,13 @@ def _authenticate_scram(
239237
# Make local
240238
_hmac = hmac.HMAC
241239

242-
ctx = sock_info.auth_ctx
240+
ctx = conn.auth_ctx
243241
if ctx and ctx.speculate_succeeded():
244242
nonce, first_bare = ctx.scram_data
245243
res = ctx.speculative_authenticate
246244
else:
247245
nonce, first_bare, cmd = _authenticate_scram_start(credentials, mechanism)
248-
res = sock_info.command(source, cmd)
246+
res = conn.command(source, cmd)
249247

250248
server_first = res["payload"]
251249
parsed = _parse_scram_response(server_first)
@@ -285,7 +283,7 @@ def _authenticate_scram(
285283
("payload", Binary(client_final)),
286284
]
287285
)
288-
res = sock_info.command(source, cmd)
286+
res = conn.command(source, cmd)
289287

290288
parsed = _parse_scram_response(res["payload"])
291289
if not hmac.compare_digest(parsed[b"v"], server_sig):
@@ -301,7 +299,7 @@ def _authenticate_scram(
301299
("payload", Binary(b"")),
302300
]
303301
)
304-
res = sock_info.command(source, cmd)
302+
res = conn.command(source, cmd)
305303
if not res["done"]:
306304
raise OperationFailure("SASL conversation failed to complete.")
307305

@@ -345,7 +343,7 @@ def _canonicalize_hostname(hostname: str) -> str:
345343
return name[0].lower()
346344

347345

348-
def _authenticate_gssapi(credentials: MongoCredential, sock_info: SocketInfo) -> None:
346+
def _authenticate_gssapi(credentials: MongoCredential, conn: Connection) -> None:
349347
"""Authenticate using GSSAPI."""
350348
if not HAVE_KERBEROS:
351349
raise ConfigurationError(
@@ -358,7 +356,7 @@ def _authenticate_gssapi(credentials: MongoCredential, sock_info: SocketInfo) ->
358356
props = credentials.mechanism_properties
359357
# Starting here and continuing through the while loop below - establish
360358
# the security context. See RFC 4752, Section 3.1, first paragraph.
361-
host = sock_info.address[0]
359+
host = conn.address[0]
362360
if props.canonicalize_host_name:
363361
host = _canonicalize_hostname(host)
364362
service = props.service_name + "@" + host
@@ -413,7 +411,7 @@ def _authenticate_gssapi(credentials: MongoCredential, sock_info: SocketInfo) ->
413411
("autoAuthorize", 1),
414412
]
415413
)
416-
response = sock_info.command("$external", cmd)
414+
response = conn.command("$external", cmd)
417415

418416
# Limit how many times we loop to catch protocol / library issues
419417
for _ in range(10):
@@ -430,7 +428,7 @@ def _authenticate_gssapi(credentials: MongoCredential, sock_info: SocketInfo) ->
430428
("payload", payload),
431429
]
432430
)
433-
response = sock_info.command("$external", cmd)
431+
response = conn.command("$external", cmd)
434432

435433
if result == kerberos.AUTH_GSS_COMPLETE:
436434
break
@@ -453,7 +451,7 @@ def _authenticate_gssapi(credentials: MongoCredential, sock_info: SocketInfo) ->
453451
("payload", payload),
454452
]
455453
)
456-
sock_info.command("$external", cmd)
454+
conn.command("$external", cmd)
457455

458456
finally:
459457
kerberos.authGSSClientClean(ctx)
@@ -462,7 +460,7 @@ def _authenticate_gssapi(credentials: MongoCredential, sock_info: SocketInfo) ->
462460
raise OperationFailure(str(exc))
463461

464462

465-
def _authenticate_plain(credentials: MongoCredential, sock_info: SocketInfo) -> None:
463+
def _authenticate_plain(credentials: MongoCredential, conn: Connection) -> None:
466464
"""Authenticate using SASL PLAIN (RFC 4616)"""
467465
source = credentials.source
468466
username = credentials.username
@@ -476,52 +474,50 @@ def _authenticate_plain(credentials: MongoCredential, sock_info: SocketInfo) ->
476474
("autoAuthorize", 1),
477475
]
478476
)
479-
sock_info.command(source, cmd)
477+
conn.command(source, cmd)
480478

481479

482-
def _authenticate_x509(credentials: MongoCredential, sock_info: SocketInfo) -> None:
480+
def _authenticate_x509(credentials: MongoCredential, conn: Connection) -> None:
483481
"""Authenticate using MONGODB-X509."""
484-
ctx = sock_info.auth_ctx
482+
ctx = conn.auth_ctx
485483
if ctx and ctx.speculate_succeeded():
486484
# MONGODB-X509 is done after the speculative auth step.
487485
return
488486

489-
cmd = _X509Context(credentials, sock_info.address).speculate_command()
490-
sock_info.command("$external", cmd)
487+
cmd = _X509Context(credentials, conn.address).speculate_command()
488+
conn.command("$external", cmd)
491489

492490

493-
def _authenticate_mongo_cr(credentials: MongoCredential, sock_info: SocketInfo) -> None:
491+
def _authenticate_mongo_cr(credentials: MongoCredential, conn: Connection) -> None:
494492
"""Authenticate using MONGODB-CR."""
495493
source = credentials.source
496494
username = credentials.username
497495
password = credentials.password
498496
# Get a nonce
499-
response = sock_info.command(source, {"getnonce": 1})
497+
response = conn.command(source, {"getnonce": 1})
500498
nonce = response["nonce"]
501499
key = _auth_key(nonce, username, password)
502500

503501
# Actually authenticate
504502
query = SON([("authenticate", 1), ("user", username), ("nonce", nonce), ("key", key)])
505-
sock_info.command(source, query)
503+
conn.command(source, query)
506504

507505

508-
def _authenticate_default(credentials: MongoCredential, sock_info: SocketInfo) -> None:
509-
if sock_info.max_wire_version >= 7:
510-
if sock_info.negotiated_mechs:
511-
mechs = sock_info.negotiated_mechs
506+
def _authenticate_default(credentials: MongoCredential, conn: Connection) -> None:
507+
if conn.max_wire_version >= 7:
508+
if conn.negotiated_mechs:
509+
mechs = conn.negotiated_mechs
512510
else:
513511
source = credentials.source
514-
cmd = sock_info.hello_cmd()
512+
cmd = conn.hello_cmd()
515513
cmd["saslSupportedMechs"] = source + "." + credentials.username
516-
mechs = sock_info.command(source, cmd, publish_events=False).get(
517-
"saslSupportedMechs", []
518-
)
514+
mechs = conn.command(source, cmd, publish_events=False).get("saslSupportedMechs", [])
519515
if "SCRAM-SHA-256" in mechs:
520-
return _authenticate_scram(credentials, sock_info, "SCRAM-SHA-256")
516+
return _authenticate_scram(credentials, conn, "SCRAM-SHA-256")
521517
else:
522-
return _authenticate_scram(credentials, sock_info, "SCRAM-SHA-1")
518+
return _authenticate_scram(credentials, conn, "SCRAM-SHA-1")
523519
else:
524-
return _authenticate_scram(credentials, sock_info, "SCRAM-SHA-1")
520+
return _authenticate_scram(credentials, conn, "SCRAM-SHA-1")
525521

526522

527523
_AUTH_MAP: Mapping[str, Callable] = {
@@ -606,12 +602,12 @@ def speculate_command(self) -> Optional[MutableMapping[str, Any]]:
606602

607603

608604
def authenticate(
609-
credentials: MongoCredential, sock_info: SocketInfo, reauthenticate: bool = False
605+
credentials: MongoCredential, conn: Connection, reauthenticate: bool = False
610606
) -> None:
611-
"""Authenticate sock_info."""
607+
"""Authenticate connection."""
612608
mechanism = credentials.mechanism
613609
auth_func = _AUTH_MAP[mechanism]
614610
if mechanism == "MONGODB-OIDC":
615-
_authenticate_oidc(credentials, sock_info, reauthenticate)
611+
_authenticate_oidc(credentials, conn, reauthenticate)
616612
else:
617-
auth_func(credentials, sock_info)
613+
auth_func(credentials, conn)

pymongo/auth_aws.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def set_cached_credentials(creds):
4949
if TYPE_CHECKING:
5050
from bson.typings import _ReadableBuffer
5151
from pymongo.auth import MongoCredential
52-
from pymongo.pool import SocketInfo
52+
from pymongo.pool import Connection
5353

5454

5555
class _AwsSaslContext(AwsSaslContext): # type: ignore
@@ -67,15 +67,15 @@ def bson_decode(self, data: _ReadableBuffer) -> Mapping[str, Any]:
6767
return bson.decode(data)
6868

6969

70-
def _authenticate_aws(credentials: MongoCredential, sock_info: SocketInfo) -> None:
70+
def _authenticate_aws(credentials: MongoCredential, conn: Connection) -> None:
7171
"""Authenticate using MONGODB-AWS."""
7272
if not _HAVE_MONGODB_AWS:
7373
raise ConfigurationError(
7474
"MONGODB-AWS authentication requires pymongo-auth-aws: "
7575
"install with: python -m pip install 'pymongo[aws]'"
7676
)
7777

78-
if sock_info.max_wire_version < 9:
78+
if conn.max_wire_version < 9:
7979
raise ConfigurationError("MONGODB-AWS authentication requires MongoDB version 4.4 or later")
8080

8181
try:
@@ -90,7 +90,7 @@ def _authenticate_aws(credentials: MongoCredential, sock_info: SocketInfo) -> No
9090
client_first = SON(
9191
[("saslStart", 1), ("mechanism", "MONGODB-AWS"), ("payload", client_payload)]
9292
)
93-
server_first = sock_info.command("$external", client_first)
93+
server_first = conn.command("$external", client_first)
9494
res = server_first
9595
# Limit how many times we loop to catch protocol / library issues
9696
for _ in range(10):
@@ -102,7 +102,7 @@ def _authenticate_aws(credentials: MongoCredential, sock_info: SocketInfo) -> No
102102
("payload", client_payload),
103103
]
104104
)
105-
res = sock_info.command("$external", cmd)
105+
res = conn.command("$external", cmd)
106106
if res["done"]:
107107
# SASL complete.
108108
break

0 commit comments

Comments
 (0)