diff --git a/changelog.d/345.misc b/changelog.d/345.misc new file mode 100644 index 00000000..40c0330d --- /dev/null +++ b/changelog.d/345.misc @@ -0,0 +1 @@ +Run flake8 lints on sydent/ and update files accordingly. \ No newline at end of file diff --git a/sydent/db/accounts.py b/sydent/db/accounts.py index 140e170d..4900fac2 100644 --- a/sydent/db/accounts.py +++ b/sydent/db/accounts.py @@ -34,8 +34,8 @@ def getAccountByToken(self, token): """ cur = self.sydent.db.cursor() res = cur.execute( - "select a.user_id, a.created_ts, a.consent_version from accounts a, tokens t " - "where t.user_id = a.user_id and t.token = ?", + "select a.user_id, a.created_ts, a.consent_version from accounts a, " + "tokens t where t.user_id = a.user_id and t.token = ?", (token,), ) @@ -58,7 +58,7 @@ def storeAccount(self, user_id, creation_ts, consent_version): :type consent_version: str or None """ cur = self.sydent.db.cursor() - res = cur.execute( + cur.execute( "insert or ignore into accounts (user_id, created_ts, consent_version) " "values (?, ?, ?)", (user_id, creation_ts, consent_version), @@ -76,7 +76,7 @@ def setConsentVersion(self, user_id, consent_version): :type consent_version: unicode or None """ cur = self.sydent.db.cursor() - res = cur.execute( + cur.execute( "update accounts set consent_version = ? where user_id = ?", (consent_version, user_id), ) @@ -92,7 +92,7 @@ def addToken(self, user_id, token): :type token: unicode """ cur = self.sydent.db.cursor() - res = cur.execute( + cur.execute( "insert into tokens (user_id, token) values (?, ?)", (user_id, token), ) @@ -106,7 +106,7 @@ def delToken(self, token): :type token: unicode """ cur = self.sydent.db.cursor() - res = cur.execute( + cur.execute( "delete from tokens where token = ?", (token,), ) diff --git a/sydent/db/hashing_metadata.py b/sydent/db/hashing_metadata.py index 111224f4..97b6de0e 100644 --- a/sydent/db/hashing_metadata.py +++ b/sydent/db/hashing_metadata.py @@ -71,15 +71,16 @@ def store_lookup_pepper(self, hashing_function, pepper): cur, hashing_function, pepper, "global_threepid_associations" ) - # Commit the queued db transactions so that adding a new pepper and hashing is atomic + # Commit the queued db transactions so that adding a new pepper and hashing + # is atomic self.sydent.db.commit() def _rehash_threepids(self, cur, hashing_function, pepper, table): """Rehash 3PIDs of a given table using a given hashing_function and pepper - A database cursor `cur` must be passed to this function. After this function completes, - the calling function should make sure to call self`self.sydent.db.commit()` to commit - the made changes to the database. + A database cursor `cur` must be passed to this function. After this function + completes, the calling function should make sure to call + self`self.sydent.db.commit()` to commit the made changes to the database. :param cur: Database cursor :type cur: @@ -87,7 +88,8 @@ def _rehash_threepids(self, cur, hashing_function, pepper, table): :param hashing_function: A function with single input and output strings :type hashing_function func(str) -> str - :param pepper: A pepper to append to the end of the 3PID (after a space) before hashing + :param pepper: A pepper to append to the end of the 3PID (after a space) + before hashing :type pepper: str :param table: The database table to perform the rehashing on @@ -121,7 +123,8 @@ def _rehash_threepids(self, cur, hashing_function, pepper, table): # Combine the medium, address and pepper together in the # following form: "address medium pepper" - # According to MSC2134: https://github.com/matrix-org/matrix-doc/pull/2134 + # According to MSC2134: + # https://github.com/matrix-org/matrix-doc/pull/2134 combo = "%s %s %s" % (address, medium, pepper) # Hash the resulting string diff --git a/sydent/db/peers.py b/sydent/db/peers.py index 3c796ad6..38ff2747 100644 --- a/sydent/db/peers.py +++ b/sydent/db/peers.py @@ -34,7 +34,8 @@ def getPeerByName(self, name): """ cur = self.sydent.db.cursor() res = cur.execute( - "select p.name, p.port, p.lastSentVersion, pk.alg, pk.key from peers p, peer_pubkeys pk " + "select p.name, p.port, p.lastSentVersion, pk.alg, pk.key " + "from peers p, peer_pubkeys pk " "where p.name = ? and pk.peername = p.name and p.active = 1", (name,), ) @@ -66,7 +67,8 @@ def getAllPeers(self): """ cur = self.sydent.db.cursor() res = cur.execute( - "select p.name, p.port, p.lastSentVersion, pk.alg, pk.key from peers p, peer_pubkeys pk " + "select p.name, p.port, p.lastSentVersion, pk.alg, pk.key " + "from peers p, peer_pubkeys pk " "where pk.peername = p.name and p.active = 1" ) diff --git a/sydent/db/sqlitedb.py b/sydent/db/sqlitedb.py index fb685742..3d08a4a6 100644 --- a/sydent/db/sqlitedb.py +++ b/sydent/db/sqlitedb.py @@ -54,7 +54,7 @@ def _createSchema(self): try: logger.info("Importing %s", scriptPath) c.executescript(fp.read()) - except: + except: # noqa: E722 logger.error("Error importing %s", scriptPath) raise fp.close() @@ -68,18 +68,21 @@ def _upgradeSchema(self): if curVer < 1: cur = self.db.cursor() - # add auto_increment to the primary key of local_threepid_associations to ensure ids are never re-used, - # allow the mxid column to be null to represent the deletion of a binding - # and remove not null constraints on ts, notBefore and notAfter (again for when a binding has been deleted + # add auto_increment to the primary key of local_threepid_associations to + # ensure ids are never re-used, allow the mxid column to be null to + # represent the deletion of a binding and remove not null constraints on + # ts, notBefore and notAfter (again for when a binding has been deleted # and these wouldn't be very meaningful) logger.info("Migrating schema from v0 to v1") cur.execute("DROP INDEX IF EXISTS medium_address") cur.execute("DROP INDEX IF EXISTS local_threepid_medium_address") cur.execute( - "ALTER TABLE local_threepid_associations RENAME TO old_local_threepid_associations" + "ALTER TABLE local_threepid_associations " + "RENAME TO old_local_threepid_associations" ) cur.execute( - "CREATE TABLE local_threepid_associations (id integer primary key autoincrement, " + "CREATE TABLE local_threepid_associations ( " + "id integer primary key autoincrement, " "medium varchar(16) not null, " "address varchar(256) not null, " "mxid varchar(256), " @@ -88,23 +91,28 @@ def _upgradeSchema(self): "notAfter bigint)" ) cur.execute( - "INSERT INTO local_threepid_associations (medium, address, mxid, ts, notBefore, notAfter) " - "SELECT medium, address, mxid, ts, notBefore, notAfter FROM old_local_threepid_associations" + "INSERT INTO local_threepid_associations ( " + "medium, address, mxid, ts, notBefore, notAfter) " + "SELECT medium, address, mxid, ts, notBefore, notAfter " + "FROM old_local_threepid_associations" ) cur.execute( - "CREATE UNIQUE INDEX local_threepid_medium_address on local_threepid_associations(medium, address)" + "CREATE UNIQUE INDEX local_threepid_medium_address " + "on local_threepid_associations(medium, address)" ) cur.execute("DROP TABLE old_local_threepid_associations") - # same autoincrement for global_threepid_associations (fields stay non-nullable because we don't need - # entries in this table for deletions, we can just delete the rows) + # same autoincrement for global_threepid_associations (fields stay + # non-nullable because we don't need entries in this table for deletions, + # we can just delete the rows) cur.execute("DROP INDEX IF EXISTS global_threepid_medium_address") cur.execute("DROP INDEX IF EXISTS global_threepid_medium_lower_address") cur.execute("DROP INDEX IF EXISTS global_threepid_originServer_originId") cur.execute("DROP INDEX IF EXISTS medium_lower_address") cur.execute("DROP INDEX IF EXISTS threepid_originServer_originId") cur.execute( - "ALTER TABLE global_threepid_associations RENAME TO old_global_threepid_associations" + "ALTER TABLE global_threepid_associations " + "RENAME TO old_global_threepid_associations" ) cur.execute( "CREATE TABLE IF NOT EXISTS global_threepid_associations " @@ -121,12 +129,15 @@ def _upgradeSchema(self): ) cur.execute( "INSERT INTO global_threepid_associations " - "(medium, address, mxid, ts, notBefore, notAfter, originServer, originId, sgAssoc) " - "SELECT medium, address, mxid, ts, notBefore, notAfter, originServer, originId, sgAssoc " + "(medium, address, mxid, ts, notBefore, notAfter, " + "originServer, originId, sgAssoc) " + "SELECT medium, address, mxid, ts, notBefore, notAfter, " + "originServer, originId, sgAssoc " "FROM old_global_threepid_associations" ) cur.execute( - "CREATE INDEX global_threepid_medium_address on global_threepid_associations (medium, address)" + "CREATE INDEX global_threepid_medium_address on " + "global_threepid_associations (medium, address)" ) cur.execute( "CREATE INDEX global_threepid_medium_lower_address on " @@ -145,7 +156,8 @@ def _upgradeSchema(self): logger.info("Migrating schema from v1 to v2") cur = self.db.cursor() cur.execute( - "CREATE INDEX threepid_validation_sessions_mtime ON threepid_validation_sessions(mtime)" + "CREATE INDEX threepid_validation_sessions_mtime ON " + "threepid_validation_sessions(mtime)" ) self.db.commit() logger.info("v1 -> v2 schema migration complete") @@ -184,16 +196,20 @@ def _upgradeSchema(self): if curVer < 4: cur = self.db.cursor() cur.execute( - "CREATE TABLE accounts(user_id TEXT NOT NULL PRIMARY KEY, created_ts BIGINT NOT NULL, consent_version TEXT)" + "CREATE TABLE accounts(user_id TEXT NOT NULL PRIMARY KEY, " + "created_ts BIGINT NOT NULL, consent_version TEXT)" ) cur.execute( - "CREATE TABLE tokens(token TEXT NOT NULL PRIMARY KEY, user_id TEXT NOT NULL)" + "CREATE TABLE tokens(token TEXT NOT NULL PRIMARY KEY, " + "user_id TEXT NOT NULL)" ) cur.execute( - "CREATE TABLE accepted_terms_urls(user_id TEXT NOT NULL, url TEXT NOT NULL)" + "CREATE TABLE accepted_terms_urls(user_id TEXT NOT NULL, " + "url TEXT NOT NULL)" ) cur.execute( - "CREATE UNIQUE INDEX accepted_terms_urls_idx ON accepted_terms_urls (user_id, url)" + "CREATE UNIQUE INDEX accepted_terms_urls_idx ON " + "accepted_terms_urls (user_id, url)" ) self.db.commit() logger.info("v3 -> v4 schema migration complete") @@ -204,7 +220,8 @@ def _upgradeSchema(self): cur = self.db.cursor() cur.execute("DROP INDEX IF EXISTS lookup_hash_medium") cur.execute( - "CREATE INDEX global_threepid_lookup_hash ON global_threepid_associations(lookup_hash)" + "CREATE INDEX global_threepid_lookup_hash ON " + "global_threepid_associations(lookup_hash)" ) self.db.commit() logger.info("v4 -> v5 schema migration complete") @@ -212,7 +229,7 @@ def _upgradeSchema(self): def _getSchemaVersion(self): cur = self.db.cursor() - res = cur.execute("PRAGMA user_version") + cur.execute("PRAGMA user_version") row = cur.fetchone() return row[0] @@ -220,4 +237,4 @@ def _setSchemaVersion(self, ver): cur = self.db.cursor() # NB. pragma doesn't support variable substitution so we # do it in python (as a decimal so we don't risk SQL injection) - res = cur.execute("PRAGMA user_version = %d" % (ver,)) + cur.execute("PRAGMA user_version = %d" % (ver,)) diff --git a/sydent/db/terms.py b/sydent/db/terms.py index bb8d1942..6e27715a 100644 --- a/sydent/db/terms.py +++ b/sydent/db/terms.py @@ -55,7 +55,7 @@ def addAgreedUrls(self, user_id, urls): :type urls: list[unicode] """ cur = self.sydent.db.cursor() - res = cur.executemany( + cur.executemany( "insert or ignore into accepted_terms_urls (user_id, url) values (?, ?)", ((user_id, u) for u in urls), ) diff --git a/sydent/db/threepid_associations.py b/sydent/db/threepid_associations.py index 6c0fcc57..847196d9 100644 --- a/sydent/db/threepid_associations.py +++ b/sydent/db/threepid_associations.py @@ -42,7 +42,8 @@ def addOrUpdateAssociation(self, assoc): # sqlite's support for upserts is atrocious cur.execute( "insert or replace into local_threepid_associations " - "('medium', 'address', 'lookup_hash', 'mxid', 'ts', 'notBefore', 'notAfter')" + "('medium', 'address', 'lookup_hash', 'mxid', 'ts', " + "'notBefore', 'notAfter')" " values (?, ?, ?, ?, ?, ?, ?)", ( assoc.medium, @@ -76,8 +77,8 @@ def getAssociationsAfterId(self, afterId, limit=None): afterId = -1 q = ( - "select id, medium, address, lookup_hash, mxid, ts, notBefore, notAfter from " - "local_threepid_associations " + "select id, medium, address, lookup_hash, mxid, ts, " + "notBefore, notAfter from local_threepid_associations " "where id > ? order by id asc" ) if limit is not None: @@ -201,9 +202,9 @@ def signedAssociationStringForThreepid(self, medium, address): # case insensitive which is technically incorrect). If we someday get a # case-sensitive threepid, this can change. res = cur.execute( - "select sgAssoc from global_threepid_associations where " - "medium = ? and lower(address) = lower(?) and notBefore < ? and notAfter > ? " - "order by ts desc limit 1", + "select sgAssoc from global_threepid_associations " + "where medium = ? and lower(address) = lower(?) " + "and notBefore < ? and notAfter > ? order by ts desc limit 1", (medium, address, time_msec(), time_msec()), ) @@ -230,9 +231,9 @@ def getMxid(self, medium, address): """ cur = self.sydent.db.cursor() res = cur.execute( - "select mxid from global_threepid_associations where " - "medium = ? and lower(address) = lower(?) and notBefore < ? and notAfter > ? " - "order by ts desc limit 1", + "select mxid from global_threepid_associations " + "where medium = ? and lower(address) = lower(?) and notBefore < ? " + "and notAfter > ? order by ts desc limit 1", (medium, address, time_msec(), time_msec()), ) @@ -257,10 +258,12 @@ def getMxids(self, threepid_tuples): cur = self.sydent.db.cursor() cur.execute( - "CREATE TEMPORARY TABLE tmp_getmxids (medium VARCHAR(16), address VARCHAR(256))" + "CREATE TEMPORARY TABLE tmp_getmxids (medium VARCHAR(16), " + "address VARCHAR(256))" ) cur.execute( - "CREATE INDEX tmp_getmxids_medium_lower_address ON tmp_getmxids (medium, lower(address))" + "CREATE INDEX tmp_getmxids_medium_lower_address " + "ON tmp_getmxids (medium, lower(address))" ) try: @@ -273,10 +276,13 @@ def getMxids(self, threepid_tuples): inserted_cap += 500 res = cur.execute( - # 'notBefore' is the time the association starts being valid, 'notAfter' the the time at which - # it ceases to be valid, so the ts must be greater than 'notBefore' and less than 'notAfter'. - "SELECT gte.medium, gte.address, gte.ts, gte.mxid FROM global_threepid_associations gte " - "JOIN tmp_getmxids ON gte.medium = tmp_getmxids.medium AND lower(gte.address) = lower(tmp_getmxids.address) " + # 'notBefore' is the time the association starts being valid, 'notAfter' + # the the time at which it ceases to be valid, so the ts must be greater + # than 'notBefore' and less than 'notAfter'. + "SELECT gte.medium, gte.address, gte.ts, gte.mxid " + "FROM global_threepid_associations gte " + "JOIN tmp_getmxids ON gte.medium = tmp_getmxids.medium " + "AND lower(gte.address) = lower(tmp_getmxids.address) " "WHERE gte.notBefore < ? AND gte.notAfter > ? " "ORDER BY gte.medium, gte.address, gte.ts DESC", (time_msec(), time_msec()), @@ -317,7 +323,8 @@ def addAssociation(self, assoc, rawSgAssoc, originServer, originId, commit=True) cur = self.sydent.db.cursor() cur.execute( "insert or ignore into global_threepid_associations " - "(medium, address, lookup_hash, mxid, ts, notBefore, notAfter, originServer, originId, sgAssoc) values " + "(medium, address, lookup_hash, mxid, ts, notBefore, notAfter, " + "originServer, originId, sgAssoc) values " "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ( assoc.medium, @@ -417,9 +424,11 @@ def retrieveMxidsForHashes(self, addresses): inserted_cap += 500 res = cur.execute( - # 'notBefore' is the time the association starts being valid, 'notAfter' the the time at which - # it ceases to be valid, so the ts must be greater than 'notBefore' and less than 'notAfter'. - "SELECT gta.lookup_hash, gta.mxid FROM global_threepid_associations gta " + # 'notBefore' is the time the association starts being valid, 'notAfter' + # the the time at which it ceases to be valid, so the ts must be greater + # than 'notBefore' and less than 'notAfter'. + "SELECT gta.lookup_hash, gta.mxid " + "FROM global_threepid_associations gta " "JOIN tmp_retrieve_mxids_for_hashes " "ON gta.lookup_hash = tmp_retrieve_mxids_for_hashes.lookup_hash " "WHERE gta.notBefore < ? AND gta.notAfter > ? " @@ -428,8 +437,8 @@ def retrieveMxidsForHashes(self, addresses): ) # Place the results from the query into a dictionary - # Results are sorted from oldest to newest, so if there are multiple mxid's for - # the same lookup hash, only the newest mapping will be returned + # Results are sorted from oldest to newest, so if there are multiple mxid's + # for the same lookup hash, only the newest mapping will be returned for lookup_hash, mxid in res.fetchall(): results[lookup_hash] = mxid diff --git a/sydent/db/valsession.py b/sydent/db/valsession.py index e71bd50b..1e8e2384 100644 --- a/sydent/db/valsession.py +++ b/sydent/db/valsession.py @@ -54,8 +54,10 @@ def getOrCreateTokenSession(self, medium, address, clientSecret): cur.execute( "select s.id, s.medium, s.address, s.clientSecret, s.validated, s.mtime, " - "t.token, t.sendAttemptNumber from threepid_validation_sessions s,threepid_token_auths t " - "where s.medium = ? and s.address = ? and s.clientSecret = ? and t.validationSession = s.id", + "t.token, t.sendAttemptNumber " + "from threepid_validation_sessions s,threepid_token_auths t " + "where s.medium = ? and s.address = ? and s.clientSecret = ? " + "and t.validationSession = s.id", (medium, address, clientSecret), ) row = cur.fetchone() @@ -73,7 +75,8 @@ def getOrCreateTokenSession(self, medium, address, clientSecret): tokenString = sydent.util.tokenutils.generateTokenForMedium(medium) cur.execute( - "insert into threepid_token_auths (validationSession, token, sendAttemptNumber) values (?, ?, ?)", + "insert into threepid_token_auths " + "(validationSession, token, sendAttemptNumber) values (?, ?, ?)", (sid, tokenString, -1), ) self.sydent.db.commit() @@ -110,7 +113,8 @@ def addValSession(self, medium, address, clientSecret, mtime, commit=True): sid = self.random.randint(0, 2 ** 31) cur.execute( - "insert into threepid_validation_sessions ('id', 'medium', 'address', 'clientSecret', 'mtime')" + "insert into threepid_validation_sessions " + "('id', 'medium', 'address', 'clientSecret', 'mtime')" + " values (?, ?, ?, ?, ?)", (sid, medium, address, clientSecret, mtime), ) @@ -210,7 +214,8 @@ def getTokenSessionById(self, sid): cur.execute( "select s.id, s.medium, s.address, s.clientSecret, s.validated, s.mtime, " - "t.token, t.sendAttemptNumber from threepid_validation_sessions s,threepid_token_auths t " + "t.token, t.sendAttemptNumber " + "from threepid_validation_sessions s,threepid_token_auths t " "where s.id = ? and t.validationSession = s.id", (sid,), ) diff --git a/sydent/hs_federation/verifier.py b/sydent/hs_federation/verifier.py index aa540578..5401de4f 100644 --- a/sydent/hs_federation/verifier.py +++ b/sydent/hs_federation/verifier.py @@ -41,7 +41,8 @@ class NoAuthenticationError(Exception): class InvalidServerName(Exception): """ - Raised when the provided origin parameter is not a valid hostname (plus optional port). + Raised when the provided origin parameter is not a valid hostname (plus optional + port). """ pass @@ -156,7 +157,8 @@ def verifyServerSignedJson(self, signed_json, acceptable_server_names=None): server_keys, ) logger.warn( - "Unable to verify any signatures from block %r. Acceptable server names: %r", + "Unable to verify any signatures from block %r. " + "Acceptable server names: %r", signed_json["signatures"], acceptable_server_names, ) diff --git a/sydent/http/auth.py b/sydent/http/auth.py index c69db4a9..fb03c58a 100644 --- a/sydent/http/auth.py +++ b/sydent/http/auth.py @@ -59,13 +59,13 @@ def authV2(sydent, request, requireTermsAgreed=True): :type sydent: sydent.sydent.Sydent :param request: The request to look for an access token in. :type request: twisted.web.server.Request - :param requireTermsAgreed: Whether to deny authentication if the user hasn't accepted - the terms of service. + :param requireTermsAgreed: Whether to deny authentication if the user hasn't + accepted the terms of service. :returns Account|None: The account object if there is correct auth, or None for v1 APIs. - :raises MatrixRestError: If the request is v2 but could not be authed or the user has - not accepted terms. + :raises MatrixRestError: If the request is v2 but could not be authed or the user + has not accepted terms. """ token = tokenFromRequest(request) diff --git a/sydent/http/federation_tls_options.py b/sydent/http/federation_tls_options.py index a830ed0e..3e493563 100644 --- a/sydent/http/federation_tls_options.py +++ b/sydent/http/federation_tls_options.py @@ -34,8 +34,8 @@ def infoCallback(connection, where, ret): try: return wrapped(connection, where, ret) except: # noqa: E722, taken from the twisted implementation - f = Failure() - logger.exception("Error during info_callback") + f = Failure() # noqa: F821 + logger.exception("Error during info_callback") # noqa: F821 connection.get_app_data().failVerification(f) return infoCallback @@ -56,7 +56,7 @@ def _idnaBytes(text): @implementer(IOpenSSLClientConnectionCreator) -class ClientTLSOptions(object): +class ClientTLSOptions(object): # noqa: F811 """ Client creator for TLS without certificate identity verification. This is a copy of twisted.internet._sslverify.ClientTLSOptions with the identity diff --git a/sydent/http/httpclient.py b/sydent/http/httpclient.py index 7366d707..50eb97ae 100644 --- a/sydent/http/httpclient.py +++ b/sydent/http/httpclient.py @@ -20,7 +20,7 @@ from io import BytesIO from twisted.internet import defer -from twisted.web.client import FileBodyProducer, Agent, readBody +from twisted.web.client import FileBodyProducer, Agent from twisted.web.http_headers import Headers from sydent.http.blacklisting_reactor import BlacklistingReactorWrapper @@ -60,7 +60,7 @@ def get_json(self, uri, max_size=None): try: # json.loads doesn't allow bytes in Python 3.5 json_body = json_decoder.decode(body.decode("UTF-8")) - except Exception as e: + except Exception as e: # noqa: F841 logger.exception("Error parsing JSON from %s", uri) raise defer.returnValue(json_body) @@ -123,8 +123,8 @@ class SimpleHttpClient(HTTPClient): def __init__(self, sydent): self.sydent = sydent # The default endpoint factory in Twisted 14.0.0 (which we require) uses the - # BrowserLikePolicyForHTTPS context factory which will do regular cert validation - # 'like a browser' + # BrowserLikePolicyForHTTPS context factory which will do regular cert + # validation 'like a browser' self.agent = Agent( BlacklistingReactorWrapper( reactor=self.sydent.reactor, diff --git a/sydent/http/httpcommon.py b/sydent/http/httpcommon.py index e50ec2c4..959434d8 100644 --- a/sydent/http/httpcommon.py +++ b/sydent/http/httpcommon.py @@ -54,7 +54,8 @@ def makeMyCertificate(self): fp = open(privKeyAndCertFilename) except IOError: logger.warn( - "Unable to read private key / cert file from %s: not starting the replication HTTPS server " + "Unable to read private key / cert file from %s: " + "not starting the replication HTTPS server " "or doing replication pushes.", privKeyAndCertFilename, ) @@ -65,8 +66,9 @@ def makeMyCertificate(self): return twisted.internet.ssl.PrivateCertificate.loadPEM(authData) def makeTrustRoot(self): - # If this option is specified, use a specific root CA cert. This is useful for testing when it's not - # practical to get the client cert signed by a real root CA but should never be used on a production server. + # If this option is specified, use a specific root CA cert. This is useful for + # testing when it's not practical to get the client cert signed by a real root + # CA but should never be used on a production server. caCertFilename = self.sydent.cfg.get("http", "replication.https.cacert") if len(caCertFilename) > 0: try: @@ -112,7 +114,8 @@ def connectionLost(self, reason) -> None: class _ReadBodyWithMaxSizeProtocol(protocol.Protocol): - """A protocol which reads body to a stream, erroring if the body exceeds a maximum size.""" + """A protocol which reads body to a stream, erroring if the body exceeds a maximum + size.""" def __init__(self, deferred, max_size): self.stream = BytesIO() @@ -153,7 +156,8 @@ def connectionLost(self, reason=connectionDone) -> None: def read_body_with_max_size(response, max_size): """ - Read a HTTP response body to a file-object. Optionally enforcing a maximum file size. + Read a HTTP response body to a file-object. Optionally enforcing a maximum file + size. If the maximum file size is reached, the returned Deferred will resolve to a Failure with a BodyExceededMaxSize exception. diff --git a/sydent/http/httpsclient.py b/sydent/http/httpsclient.py index d6918aa8..f55e06e3 100644 --- a/sydent/http/httpsclient.py +++ b/sydent/http/httpsclient.py @@ -31,9 +31,9 @@ class ReplicationHttpsClient: """ - An HTTPS client specifically for talking replication to other Matrix Identity Servers - (ie. presents our replication SSL certificate and validates peer SSL certificates as we would in the - replication HTTPS server) + An HTTPS client specifically for talking replication to other Matrix Identity + Servers (ie. presents our replication SSL certificate and validates peer SSL + certificates as we would in the replication HTTPS server) """ def __init__(self, sydent): @@ -43,9 +43,12 @@ def __init__(self, sydent): if self.sydent.sslComponents.myPrivateCertificate: # We will already have logged a warn if this is absent, so don't do it again # cert = self.sydent.sslComponents.myPrivateCertificate - # self.certOptions = twisted.internet.ssl.CertificateOptions(privateKey=cert.privateKey.original, - # certificate=cert.original, - # trustRoot=self.sydent.sslComponents.trustRoot) + # self.certOptions = ( + # twisted.internet.ssl.CertificateOptions( + # privateKey=cert.privateKey.original, + # certificate=cert.original, + # trustRoot=self.sydent.sslComponents.trustRoot) + # ) self.agent = Agent(self.sydent.reactor, SydentPolicyForHTTPS(self.sydent)) def postJson(self, uri, jsonObject): diff --git a/sydent/http/matrixfederationagent.py b/sydent/http/matrixfederationagent.py index 91c2ee53..ebe6ca25 100644 --- a/sydent/http/matrixfederationagent.py +++ b/sydent/http/matrixfederationagent.py @@ -30,7 +30,7 @@ from twisted.web.http_headers import Headers from twisted.web.iweb import IAgent -from sydent.http.httpcommon import BodyExceededMaxSize, read_body_with_max_size +from sydent.http.httpcommon import read_body_with_max_size from sydent.http.srvresolver import SrvResolver, pick_server_from_list from sydent.util import json_decoder from sydent.util.ttlcache import TTLCache @@ -450,8 +450,8 @@ class _RoutingResult(object): target_host = attr.ib() """ - The hostname (or IP literal) we should route the TCP connection to (the target of the - SRV record, or the hostname from the URL/.well-known) + The hostname (or IP literal) we should route the TCP connection to (the target of + the SRV record, or the hostname from the URL/.well-known) :type: bytes """ diff --git a/sydent/http/servlets/blindlysignstuffservlet.py b/sydent/http/servlets/blindlysignstuffservlet.py index b79b8bf1..dbe6d8a7 100644 --- a/sydent/http/servlets/blindlysignstuffservlet.py +++ b/sydent/http/servlets/blindlysignstuffservlet.py @@ -63,7 +63,7 @@ def render_POST(self, request): "ed25519", "0", private_key_base64 ) signed = signedjson.sign.sign_json(to_sign, self.server_name, private_key) - except: + except: # noqa: E722 logger.exception("signing failed") raise MatrixRestError(500, "M_UNKNOWN", "Internal Server Error") diff --git a/sydent/http/servlets/bulklookupservlet.py b/sydent/http/servlets/bulklookupservlet.py index deab3254..0ff06743 100644 --- a/sydent/http/servlets/bulklookupservlet.py +++ b/sydent/http/servlets/bulklookupservlet.py @@ -36,9 +36,10 @@ def __init__(self, syd): def render_POST(self, request): """ Bulk-lookup for threepids. - Params: 'threepids': list of threepids, each of which is a list of medium, address - Returns: Object with key 'threepids', which is a list of results where each result - is a 3 item list of medium, address, mxid + Params: 'threepids': list of threepids, each of which is a list of + medium, address + Returns: Object with key 'threepids', which is a list of results where + each result is a 3 item list of medium, address, mxid Note that results are not streamed to the client. Threepids for which no mapping is found are omitted. """ diff --git a/sydent/http/servlets/emailservlet.py b/sydent/http/servlets/emailservlet.py index fe380c2e..d148c467 100644 --- a/sydent/http/servlets/emailservlet.py +++ b/sydent/http/servlets/emailservlet.py @@ -106,17 +106,23 @@ def render_GET(self, request): resp = None try: resp = self.do_validate_request(request) - except: + except: # noqa: E722 pass if resp and "success" in resp and resp["success"]: - msg = "Verification successful! Please return to your Matrix client to continue." + msg = ( + "Verification successful! " + "Please return to your Matrix client to continue." + ) if "nextLink" in args: next_link = args["nextLink"] if not next_link.startswith("file:///"): request.setResponseCode(302) request.setHeader("Location", next_link) else: - msg = "Verification failed: you may need to request another verification email" + msg = ( + "Verification failed: " + "you may need to request another verification email" + ) brand = self.sydent.brand_from_request(request) templateFile = self.sydent.get_branded_template( @@ -172,7 +178,8 @@ def do_validate_request(self, request): return { "success": False, "errcode": "M_INVALID_PARAM", - "error": "Client secret does not match the one given when requesting the token", + "error": "Client secret does not match the one given " + "when requesting the token", } except SessionExpiredException: return { diff --git a/sydent/http/servlets/lookupservlet.py b/sydent/http/servlets/lookupservlet.py index 03465fcf..6525026b 100644 --- a/sydent/http/servlets/lookupservlet.py +++ b/sydent/http/servlets/lookupservlet.py @@ -22,7 +22,7 @@ import logging import signedjson.sign -from sydent.http.servlets import get_args, jsonwrap, send_cors, MatrixRestError +from sydent.http.servlets import get_args, jsonwrap, send_cors from sydent.util import json_decoder @@ -44,7 +44,8 @@ def render_GET(self, request): Params: 'medium': the medium of the threepid 'address': the address of the threepid - Returns: A signed association if the threepid has a corresponding mxid, otherwise the empty object. + Returns: A signed association if the threepid has a corresponding mxid, + otherwise the empty object. """ send_cors(request) @@ -61,7 +62,7 @@ def render_GET(self, request): return {} sgassoc = json_decoder.decode(sgassoc) - if not self.sydent.server_name in sgassoc["signatures"]: + if self.sydent.server_name not in sgassoc["signatures"]: # We have not yet worked out what the proper trust model should be. # # Maybe clients implicitly trust a server they talk to (and so we diff --git a/sydent/http/servlets/msisdnservlet.py b/sydent/http/servlets/msisdnservlet.py index 7364824e..5f02d552 100644 --- a/sydent/http/servlets/msisdnservlet.py +++ b/sydent/http/servlets/msisdnservlet.py @@ -132,14 +132,20 @@ def render_GET(self, request): else: resp = self.do_validate_request(args) if "success" in resp and resp["success"]: - msg = "Verification successful! Please return to your Matrix client to continue." + msg = ( + "Verification successful! " + "Please return to your Matrix client to continue." + ) if "next_link" in args: next_link = args["next_link"] request.setResponseCode(302) request.setHeader("Location", next_link) else: request.setResponseCode(400) - msg = "Verification failed: you may need to request another verification text" + msg = ( + "Verification failed: " + "you may need to request another verification text." + ) brand = self.sydent.brand_from_request(request) templateFile = self.sydent.get_branded_template( @@ -196,7 +202,8 @@ def do_validate_request(self, request): return { "success": False, "errcode": "M_INVALID_PARAM", - "error": "Client secret does not match the one given when requesting the token", + "error": "Client secret does not match the one given " + "when requesting the token", } except SessionExpiredException: request.setResponseCode(400) diff --git a/sydent/http/servlets/registerservlet.py b/sydent/http/servlets/registerservlet.py index b39799fb..14b95c76 100644 --- a/sydent/http/servlets/registerservlet.py +++ b/sydent/http/servlets/registerservlet.py @@ -19,10 +19,9 @@ from twisted.internet import defer import logging -import json from six.moves import urllib -from sydent.http.servlets import get_args, jsonwrap, deferjsonwrap, send_cors +from sydent.http.servlets import get_args, deferjsonwrap, send_cors from sydent.http.httpclient import FederationHttpClient from sydent.users.tokens import issueToken from sydent.util.stringutils import is_valid_matrix_server_name @@ -53,7 +52,8 @@ def render_POST(self, request): request.setResponseCode(400) return { "errcode": "M_INVALID_PARAM", - "error": "matrix_server_name must be a valid Matrix server name (IP address or hostname)", + "error": "matrix_server_name must be a valid Matrix server name " + "(IP address or hostname)", } result = yield self.client.get_json( @@ -100,7 +100,8 @@ def render_POST(self, request): request.setResponseCode(500) return { "errcode": "M_UNKNOWN", - "error": "The Matrix homeserver returned a MXID belonging to another homeserver", + "error": "The Matrix homeserver returned a MXID belonging to " + "another homeserver", } tok = yield issueToken(self.sydent, user_id) diff --git a/sydent/http/servlets/replication.py b/sydent/http/servlets/replication.py index 7081a1f4..dc2ab74d 100644 --- a/sydent/http/servlets/replication.py +++ b/sydent/http/servlets/replication.py @@ -102,10 +102,10 @@ def render_POST(self, request): originId, ) - # Don't bother adding if one has already failed: we add all of them or none so - # we're only going to roll back the transaction anyway (but we continue to try - # & verify the rest so we can give a complete list of the ones that don't - # verify) + # Don't bother adding if one has already failed: we add all of them or + # none so we're only going to roll back the transaction anyway (but we + # continue to try & verify the rest so we can give a complete list of + # the ones that don't verify) if len(failedIds) > 0: continue @@ -142,7 +142,7 @@ def render_POST(self, request): logger.info( "Stored association origin ID %s from %s", originId, peer.servername ) - except: + except: # noqa: E722 failedIds.append(originId) logger.warn( "Failed to verify signed association from %s with origin ID %s", diff --git a/sydent/http/servlets/threepidunbindservlet.py b/sydent/http/servlets/threepidunbindservlet.py index a72b1850..447537c7 100644 --- a/sydent/http/servlets/threepidunbindservlet.py +++ b/sydent/http/servlets/threepidunbindservlet.py @@ -16,7 +16,6 @@ # limitations under the License. from __future__ import absolute_import -import json import logging from sydent.hs_federation.verifier import NoAuthenticationError, InvalidServerName @@ -130,7 +129,8 @@ def _async_render_POST(self, request): dict_to_json_bytes( { "errcode": "M_NO_VALID_SESSION", - "error": "No valid session was found matching that sid and client secret", + "error": "No valid session was found matching that " + "sid and client secret", } ) ) @@ -142,7 +142,8 @@ def _async_render_POST(self, request): dict_to_json_bytes( { "errcode": "M_SESSION_NOT_VALIDATED", - "error": "This validation session has not yet been completed", + "error": "This validation session has not yet " + "been completed", } ) ) @@ -154,7 +155,8 @@ def _async_render_POST(self, request): dict_to_json_bytes( { "errcode": "M_FORBIDDEN", - "error": "Provided session information does not match medium/address combo", + "error": "Provided session information does not match " + "medium/address combo", } ) ) diff --git a/sydent/http/srvresolver.py b/sydent/http/srvresolver.py index 3db2d610..92cff620 100644 --- a/sydent/http/srvresolver.py +++ b/sydent/http/srvresolver.py @@ -84,8 +84,9 @@ def pick_server_from_list(server_list): class SrvResolver(object): """Interface to the dns client to do SRV lookups, with result caching. - The default resolver in twisted.names doesn't do any caching (it has a CacheResolver, - but the cache never gets populated), so we add our own caching layer here. + The default resolver in twisted.names doesn't do any caching (it has a + CacheResolver, but the cache never gets populated), so we add our own + caching layer here. :param dns_client: Twisted resolver impl :type dns_client: twisted.internet.interfaces.IResolver diff --git a/sydent/replication/__init__.py b/sydent/replication/__init__.py index 8b137891..e69de29b 100644 --- a/sydent/replication/__init__.py +++ b/sydent/replication/__init__.py @@ -1 +0,0 @@ - diff --git a/sydent/replication/peer.py b/sydent/replication/peer.py index f4ab39e7..d1610b78 100644 --- a/sydent/replication/peer.py +++ b/sydent/replication/peer.py @@ -49,8 +49,9 @@ def __init__(self, servername, pubkeys): def pushUpdates(self, sgAssocs): """ - :param sgAssocs: Sequence of (originId, sgAssoc) tuples where originId is the id on the creating server and - sgAssoc is the json object of the signed association + :param sgAssocs: Sequence of (originId, sgAssoc) tuples where originId is + the id on the creating server and sgAssoc is the json + object of the signed association :return a deferred """ pass @@ -58,7 +59,8 @@ def pushUpdates(self, sgAssocs): class LocalPeer(Peer): """ - The local peer (ourselves: essentially copying from the local associations table to the global one) + The local peer (ourselves: essentially copying from the local associations + table to the global one) """ def __init__(self, sydent): @@ -98,8 +100,8 @@ def pushUpdates(self, sgAssocs): ) assocObj.lookup_hash = sha256_and_url_safe_base64(str_to_hash) - # We can probably skip verification for the local peer (although it could - # be good as a sanity check) + # We can probably skip verification for the local peer + # (although it could be good as a sanity check) globalAssocStore.addAssociation( assocObj, json.dumps(sgAssocs[localId]), @@ -163,7 +165,8 @@ def __init__(self, sydent, server_name, port, pubkeys, lastSentVersion): pubkey_decoded = binascii.unhexlify(pubkey) logger.warn( - "Peer public key of %s is hex encoded. Please update to base64 encoding", + "Peer public key of %s is hex encoded. " + "Please update to base64 encoding", server_name, ) except ValueError: @@ -190,7 +193,7 @@ def verifySignedAssociation(self, assoc): :param assoc: A signed association. :type assoc: dict[any, any] """ - if not "signatures" in assoc: + if "signatures" not in assoc: raise NoSignaturesException() key_ids = signedjson.sign.signature_ids(assoc, self.servername) diff --git a/sydent/replication/pusher.py b/sydent/replication/pusher.py index eb387197..4dff52ca 100644 --- a/sydent/replication/pusher.py +++ b/sydent/replication/pusher.py @@ -47,11 +47,11 @@ def setup(self): def doLocalPush(self): """ - Synchronously push local associations to this server (ie. copy them to globals table) - The local server is essentially treated the same as any other peer except we don't do - the network round-trip and this function can be used so the association goes into the - global table before the http call returns (so clients know it will be available on at - least the same ID server they used) + Synchronously push local associations to this server (ie. copy them to globals + table) The local server is essentially treated the same as any other peer except + we don't do the network round-trip and this function can be used so the + association goes into the global table before the http call returns (so + clients know it will be available on at least the same ID server they used) """ localPeer = LocalPeer(self.sydent) @@ -64,8 +64,9 @@ def doLocalPush(self): def scheduledPush(self): """Push pending updates to all known remote peers. To be called regularly. - :returns a deferred.DeferredList of defers, one per peer we're pushing to that will - resolve when pushing to that peer has completed, successfully or otherwise + :returns a deferred.DeferredList of defers, one per peer we're pushing to + that will resolve when pushing to that peer has completed, successfully + or otherwise :rtype deferred.DeferredList """ peers = self.peerStore.getAllPeers() @@ -124,5 +125,6 @@ def _push_to_peer(self, p): except Exception: logger.exception("Error pushing updates to %s:%d", p.servername, p.port) finally: - # Whether pushing completed or an error occurred, signal that pushing has finished + # Whether pushing completed or an error occurred, signal that pushing + # has finished p.is_being_pushed_to = False diff --git a/sydent/sydent.py b/sydent/sydent.py index ee8ab4af..533a4637 100644 --- a/sydent/sydent.py +++ b/sydent/sydent.py @@ -95,7 +95,8 @@ "log.level": "INFO", "pidfile.path": os.environ.get("SYDENT_PID_FILE", "sydent.pid"), "terms.path": "", - "address_lookup_limit": "10000", # Maximum amount of addresses in a single /lookup request + "address_lookup_limit": "10000", # Maximum amount of addresses + # in a single /lookup request # The root path to use for load templates. This should contain branded # directories. Each directory should contain the following templates: # @@ -109,10 +110,13 @@ # The following can be added to your local config file to enable prometheus # support. # 'prometheus_port': '8080', # The port to serve metrics on - # 'prometheus_addr': '', # The address to bind to. Empty string means bind to all. + # 'prometheus_addr': '', # The address to bind to. Empty string means bind + # to all. # The following can be added to your local config file to enable sentry support. - # 'sentry_dsn': 'https://...' # The DSN has configured in the sentry instance project. - # Whether clients and homeservers can register an association using v1 endpoints. + # 'sentry_dsn': 'https://...' # The DSN has configured in the sentry instance + # project. + # Whether clients and homeservers can register an association using v1 + # endpoints. "enable_v1_associations": "true", "delete_tokens_on_bind": "true", # Prevent outgoing requests from being sent to the following blacklisted @@ -180,11 +184,13 @@ # address. These config options determine how much of the email address to # obfuscate. Note that the '@' sign is always included. # - # If the string is longer than a configured limit below, it is truncated to that limit - # with '...' added. Otherwise: + # If the string is longer than a configured limit below, it is truncated to + # that limit with '...' added. Otherwise: # - # * If the string is longer than 5 characters, it is truncated to 3 characters + '...' - # * If the string is longer than 1 character, it is truncated to 1 character + '...' + # * If the string is longer than 5 characters, it is truncated to 3 + # characters + '...' + # * If the string is longer than 1 character, it is truncated to 1 + # character + '...' # * If the string is 1 character long, it is converted to '...' # # This ensures that a full email address is never shown, even if it is extremely @@ -229,9 +235,10 @@ def __init__( self.server_name = os.uname()[1] logger.warn( ( - "You had not specified a server name. I have guessed that this server is called '%s' " - + "and saved this in the config file. If this is incorrect, you should edit server.name in " - + "the config file." + "You had not specified a server name. I have guessed that this " + "server is called '%s' and saved this in the config file. " + "If this is incorrect, you should edit server.name in the config " + "file." ) % (self.server_name,) ) @@ -385,7 +392,8 @@ def __init__( self.pusher = Pusher(self) - # A dedicated validation session store just to clean up old sessions every N minutes + # A dedicated validation session store just to clean up + # old sessions every N minutes self.cleanupValSession = ThreePidValSessionStore(self) cb = task.LoopingCall(self.cleanupValSession.deleteOldSessions) cb.clock = self.reactor @@ -432,7 +440,8 @@ def ip_from_request(self, request): def brand_from_request(self, request): """ - If the brand GET parameter is passed, returns that as a string, otherwise returns None. + If the brand GET parameter is passed, returns that as a string, + otherwise returns None. :param request: The incoming request. :type request: twisted.web.http.Request @@ -474,7 +483,8 @@ def get_branded_template(self, brand, template_name, deprecated_template_name): if brand not in self.valid_brands: brand = None - # If the brand hint is not valid, or not provided, fallback to the default brand. + # If the brand hint is not valid, or not provided, fallback to the default + # brand. if not brand: brand = self.cfg.get("general", "brand.default") diff --git a/sydent/threepid/__init__.py b/sydent/threepid/__init__.py index bcf210e8..89d3b313 100644 --- a/sydent/threepid/__init__.py +++ b/sydent/threepid/__init__.py @@ -46,7 +46,8 @@ def __init__(self, medium, address, lookup_hash, mxid, ts, not_before, not_after :param mxid: The matrix ID the 3pid is associated with :param ts: The creation timestamp of this association, ms :param not_before: The timestamp, in ms, at which this association becomes valid - :param not_after: The timestamp, in ms, at which this association ceases to be valid + :param not_after: The timestamp, in ms, at which this association ceases to be + valid """ self.medium = medium self.address = address diff --git a/sydent/util/emailutils.py b/sydent/util/emailutils.py index 24d20783..ec5c69c3 100644 --- a/sydent/util/emailutils.py +++ b/sydent/util/emailutils.py @@ -31,10 +31,8 @@ else: from html import escape -import email.utils - -from sydent.util import time_msec -from sydent.util.tokenutils import generateAlphanumericTokenOfLength +from sydent.util import time_msec # noqa: E402 +from sydent.util.tokenutils import generateAlphanumericTokenOfLength # noqa: E402 logger = logging.getLogger(__name__) @@ -43,11 +41,11 @@ def sendEmail(sydent, templateFile, mailTo, substitutions): """ Sends an email with the given parameters. - :param sydent: The Sydent instance to use when building the configuration to send the - email with. + :param sydent: The Sydent instance to use when building the configuration + to send the email with. :type sydent: sydent.sydent.Sydent - :param templateFile: The filename of the template to use when building the body of the - email. + :param templateFile: The filename of the template to use when building the + body of the email. :type templateFile: str :param mailTo: The email address to send the email to. :type mailTo: unicode diff --git a/sydent/util/stringutils.py b/sydent/util/stringutils.py index 3b9f66fe..d42cbb45 100644 --- a/sydent/util/stringutils.py +++ b/sydent/util/stringutils.py @@ -23,7 +23,8 @@ # hostname/domain name # https://regex101.com/r/OyN1lg/2 hostname_regex = re.compile( - r"^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*$", + r"^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)" + r"(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*$", flags=re.IGNORECASE, ) diff --git a/sydent/validators/emailvalidator.py b/sydent/validators/emailvalidator.py index fc979d45..f9b04e98 100644 --- a/sydent/validators/emailvalidator.py +++ b/sydent/validators/emailvalidator.py @@ -41,8 +41,8 @@ def requestToken( brand=None, ): """ - Creates or retrieves a validation session and sends an email to the corresponding - email address with a token to use to verify the association. + Creates or retrieves a validation session and sends an email to the + corresponding email address with a token to use to verify the association. :param emailAddress: The email address to send the email to. :type emailAddress: unicode @@ -77,7 +77,8 @@ def requestToken( if int(valSession.sendAttemptNumber) >= int(sendAttempt): logger.info( - "Not mailing code because current send attempt (%d) is not less than given send attempt (%s)", + "Not mailing code because current send attempt " + "(%d) is not less than given send attempt (%s)", int(sendAttempt), int(valSession.sendAttemptNumber), ) @@ -118,11 +119,14 @@ def makeValidateLink(self, valSession, clientSecret, nextLink): :rtype: unicode """ base = self.sydent.cfg.get("http", "client_http_base") - link = "%s/_matrix/identity/api/v1/validate/email/submitToken?token=%s&client_secret=%s&sid=%d" % ( - base, - urllib.parse.quote(valSession.token), - urllib.parse.quote(clientSecret), - valSession.id, + link = ( + "%s/_matrix/identity/api/v1/validate/email/submitToken?" + "token=%s&client_secret=%s&sid=%d" % ( + base, + urllib.parse.quote(valSession.token), + urllib.parse.quote(clientSecret), + valSession.id, + ) ) if nextLink: # manipulate the nextLink to add the sid, because diff --git a/sydent/validators/msisdnvalidator.py b/sydent/validators/msisdnvalidator.py index 0b54bd9f..0f13ba01 100644 --- a/sydent/validators/msisdnvalidator.py +++ b/sydent/validators/msisdnvalidator.py @@ -49,11 +49,13 @@ def __init__(self, sydent): parts = origString.split(":") if len(parts) != 2: raise Exception( - "Originators must be in form: long:, short: or alpha:, separated by commas" + "Originators must be in form: long:, " + "short: or alpha:, separated by commas" ) if parts[0] not in ["long", "short", "alpha"]: raise Exception( - "Invalid originator type: valid types are long, short and alpha" + "Invalid originator type: " + "valid types are long, short and alpha" ) self.originators[country].append( { @@ -76,7 +78,8 @@ def __init__(self, sydent): def requestToken(self, phoneNumber, clientSecret, sendAttempt, brand=None): """ Creates or retrieves a validation session and sends an text message to the - corresponding phone number address with a token to use to verify the association. + corresponding phone number address with a token to use to verify the + association. :param phoneNumber: The phone number to send the email to. :type phoneNumber: phonenumbers.PhoneNumber @@ -109,7 +112,8 @@ def requestToken(self, phoneNumber, clientSecret, sendAttempt, brand=None): if int(valSession.sendAttemptNumber) >= int(sendAttempt): logger.info( - "Not texting code because current send attempt (%d) is not less than given send attempt (%s)", + "Not texting code because current send attempt " + "(%d) is not less than given send attempt (%s)", int(sendAttempt), int(valSession.sendAttemptNumber), ) diff --git a/tests/test_blacklisting.py b/tests/test_blacklisting.py index e91c5d2a..9f8032a5 100644 --- a/tests/test_blacklisting.py +++ b/tests/test_blacklisting.py @@ -14,7 +14,6 @@ from mock import patch -from netaddr import IPSet from twisted.internet import defer from twisted.internet.error import DNSLookupError from twisted.test.proto_helpers import StringTransport diff --git a/tests/test_email.py b/tests/test_email.py index a1d06212..58929737 100644 --- a/tests/test_email.py +++ b/tests/test_email.py @@ -13,14 +13,10 @@ # limitations under the License. import os.path -from mock import Mock, patch +from mock import patch -from twisted.web.client import Response from twisted.trial import unittest -from sydent.db.invite_tokens import JoinTokenStore -from sydent.http.httpclient import FederationHttpClient -from sydent.http.servlets.store_invite_servlet import StoreInviteServlet from tests.utils import make_request, make_sydent diff --git a/tests/test_register.py b/tests/test_register.py index 9ad8fd4a..4fd5e937 100644 --- a/tests/test_register.py +++ b/tests/test_register.py @@ -27,7 +27,8 @@ def setUp(self): self.sydent = make_sydent() def test_sydent_rejects_invalid_hostname(self): - """Tests that the /register endpoint rejects an invalid hostname passed as matrix_server_name""" + """Tests that the /register endpoint rejects an invalid hostname passed as + matrix_server_name""" self.sydent.run() bad_hostname = "example.com#" diff --git a/tests/test_replication.py b/tests/test_replication.py index 9e10ac93..07c8da63 100644 --- a/tests/test_replication.py +++ b/tests/test_replication.py @@ -16,7 +16,7 @@ def setUp(self): # Create a new sydent config = { "crypto": { - "ed25519.signingkey": "ed25519 0 FJi1Rnpj3/otydngacrwddFvwz/dTDsBv62uZDN2fZM" + "ed25519.signingkey": "ed25519 0 FJi1Rnpj3/otydngacrwddFvwz/dTDsBv62uZDN2fZM" # noqa: E501 } } self.sydent = make_sydent(test_config=config) @@ -27,7 +27,8 @@ def setUp(self): # Inject our fake peer into the database. cur = self.sydent.db.cursor() cur.execute( - "INSERT INTO peers (name, port, lastSentVersion, active) VALUES (?, ?, ?, ?)", + "INSERT INTO peers (name, port, lastSentVersion, active) VALUES " + "(?, ?, ?, ?)", ("fake.server", 1234, 0, 1), ) cur.execute( @@ -60,13 +61,13 @@ def test_incoming_replication(self): # Configure the Sydent to impersonate. We need to use "fake.server" as the # server's name because that's the name the recipient Sydent has for it. On top - # of that, the replication servlet expects a TLS certificate in the request so it - # can extract a common name and figure out which peer sent it from its common + # of that, the replication servlet expects a TLS certificate in the request so + # it can extract a common name and figure out which peer sent it from its common # name. The common name of the certificate we use for tests is fake.server. config = { "general": {"server.name": "fake.server"}, "crypto": { - "ed25519.signingkey": "ed25519 0 b29eXMMAYCFvFEtq9mLI42aivMtcg4Hl0wK89a+Vb6c" + "ed25519.signingkey": "ed25519 0 b29eXMMAYCFvFEtq9mLI42aivMtcg4Hl0wK89a+Vb6c" # noqa: E501 }, } @@ -105,7 +106,8 @@ def test_incoming_replication(self): self.assertDictEqual(signed_assoc, res_assocs[assoc_id]) def test_outgoing_replication(self): - """Make a fake peer and associations and make sure Sydent tries to push to it.""" + """Make a fake peer and associations and make sure Sydent tries to push to + it.""" cur = self.sydent.db.cursor() # Insert the fake associations into the database. @@ -185,8 +187,8 @@ def request(method, uri, headers, body): self.assertEqual(len(self.assocs), len(sent_assocs)) for assoc_id, assoc in sent_assocs.items(): # Replication payloads use a specific format that causes the JSON encoder to - # convert the numeric indexes to string, so we need to convert them back when - # looking up in signed_assocs. Also, the ID of the first association Sydent - # will push will be 1, so we need to subtract 1 when figuring out which index - # to lookup. + # convert the numeric indexes to string, so we need to convert them back + # when looking up in signed_assocs. Also, the ID of the first association + # Sydent will push will be 1, so we need to subtract 1 when figuring out + # which index to lookup. self.assertDictEqual(assoc, signed_assocs[int(assoc_id) - 1]) diff --git a/tests/utils.py b/tests/utils.py index c2ccccff..0ed0a7fa 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -57,8 +57,8 @@ def make_sydent(test_config={}): """Create a new sydent Args: - test_config (dict): any configuration variables for overriding the default sydent - config + test_config (dict): any configuration variables for overriding the default + sydent config """ # Use an in-memory SQLite database. Note that the database isn't cleaned up between # tests, so by default the same database will be used for each test if changed to be