Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Port crypto/ to Python 3 #3822

Merged
merged 3 commits into from
Sep 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/3822.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
crypto/ is now ported to Python 3.
2 changes: 1 addition & 1 deletion synapse/crypto/context_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,6 @@ def __init__(self, config):

def get_options(self, host):
return ClientTLSOptions(
host.decode('utf-8'),
host,
CertificateOptions(verify=False).getContext()
)
8 changes: 7 additions & 1 deletion synapse/crypto/keyclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def fetch_server_key(server_name, tls_client_options_factory, path=KEY_API_V1):
defer.returnValue((server_response, server_certificate))
except SynapseKeyClientError as e:
logger.warn("Error getting key for %r: %s", server_name, e)
if e.status.startswith("4"):
if e.status.startswith(b"4"):
# Don't retry for 4xx responses.
raise IOError("Cannot get key for %r" % server_name)
except (ConnectError, DomainError) as e:
Expand Down Expand Up @@ -82,6 +82,12 @@ def connectionMade(self):
self._peer = self.transport.getPeer()
logger.debug("Connected to %s", self._peer)

if not isinstance(self.path, bytes):
self.path = self.path.encode('ascii')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can paths and hosts not be unicode?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, must be ascii-encoded (maybe punycode if it's a hostname or url-encoded if it's a path), because http


if not isinstance(self.host, bytes):
self.host = self.host.encode('ascii')

self.sendCommand(b"GET", self.path)
if self.host:
self.sendHeader(b"Host", self.host)
Expand Down
9 changes: 5 additions & 4 deletions synapse/crypto/keyring.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

import hashlib
import logging
import urllib
from collections import namedtuple

from six.moves import urllib

from signedjson.key import (
decode_verify_key_bytes,
encode_verify_key_base64,
Expand Down Expand Up @@ -432,7 +433,7 @@ def get_server_verify_key_v2_indirect(self, server_names_and_key_ids,
# an incoming request.
query_response = yield self.client.post_json(
destination=perspective_name,
path=b"/_matrix/key/v2/query",
path="/_matrix/key/v2/query",
data={
u"server_keys": {
server_name: {
Expand Down Expand Up @@ -513,8 +514,8 @@ def get_server_verify_key_v2_direct(self, server_name, key_ids):

(response, tls_certificate) = yield fetch_server_key(
server_name, self.hs.tls_client_options_factory,
path=(b"/_matrix/key/v2/server/%s" % (
urllib.quote(requested_key_id),
path=("/_matrix/key/v2/server/%s" % (
urllib.parse.quote(requested_key_id),
)).encode("ascii"),
)

Expand Down