[tor-commits] [stem/master] Drop 'sign' argument from base descriptor methods

atagar at torproject.org atagar at torproject.org
Mon Feb 10 03:14:50 UTC 2020


commit 3e597d10f73db029ec733c26eaa9f76ab65b6bdb
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Feb 9 18:00:50 2020 -0800

    Drop 'sign' argument from base descriptor methods
    
    Just a few classes implemented a sign argument. Subclasses already customize
    their signature when necessary so we can limit it to just the classes that use
    it.
---
 stem/descriptor/__init__.py             | 10 +++-------
 stem/descriptor/bandwidth_file.py       |  5 +----
 stem/descriptor/extrainfo_descriptor.py | 10 ++--------
 stem/descriptor/hidden_service.py       | 15 ++++++---------
 stem/descriptor/microdescriptor.py      |  5 +----
 stem/descriptor/networkstatus.py        | 33 +++++++++------------------------
 stem/descriptor/router_status_entry.py  | 15 +++------------
 stem/descriptor/server_descriptor.py    | 10 ++--------
 8 files changed, 27 insertions(+), 76 deletions(-)

diff --git a/stem/descriptor/__init__.py b/stem/descriptor/__init__.py
index 0b9e2056..556970c3 100644
--- a/stem/descriptor/__init__.py
+++ b/stem/descriptor/__init__.py
@@ -865,7 +865,7 @@ class Descriptor(object):
       raise ValueError("Descriptor.from_str() expected a single descriptor, but had %i instead. Please include 'multiple = True' if you want a list of results instead." % len(results))
 
   @classmethod
-  def content(cls, attr = None, exclude = (), sign = False):
+  def content(cls, attr = None, exclude = ()):
     """
     Creates descriptor content with the given attributes. Mandatory fields are
     filled with dummy information unless data is supplied. This doesn't yet
@@ -876,7 +876,6 @@ class Descriptor(object):
     :param dict attr: keyword/value mappings to be included in the descriptor
     :param list exclude: mandatory keywords to exclude from the descriptor, this
       results in an invalid descriptor
-    :param bool sign: includes cryptographic signatures and digests if True
 
     :returns: **str** with the content of a descriptor
 
@@ -885,12 +884,10 @@ class Descriptor(object):
       * **NotImplementedError** if not implemented for this descriptor type
     """
 
-    # TODO: drop the 'sign' argument in stem 2.x (only a few subclasses use this)
-
     raise NotImplementedError("The create and content methods haven't been implemented for %s" % cls.__name__)
 
   @classmethod
-  def create(cls, attr = None, exclude = (), validate = True, sign = False):
+  def create(cls, attr = None, exclude = (), validate = True):
     """
     Creates a descriptor with the given attributes. Mandatory fields are filled
     with dummy information unless data is supplied. This doesn't yet create a
@@ -903,7 +900,6 @@ class Descriptor(object):
       results in an invalid descriptor
     :param bool validate: checks the validity of the descriptor's content if
       **True**, skips these checks otherwise
-    :param bool sign: includes cryptographic signatures and digests if True
 
     :returns: :class:`~stem.descriptor.Descriptor` subclass
 
@@ -913,7 +909,7 @@ class Descriptor(object):
       * **NotImplementedError** if not implemented for this descriptor type
     """
 
-    return cls(cls.content(attr, exclude, sign), validate = validate)
+    return cls(cls.content(attr, exclude), validate = validate)
 
   def type_annotation(self):
     """
diff --git a/stem/descriptor/bandwidth_file.py b/stem/descriptor/bandwidth_file.py
index 43c43aff..3cf20595 100644
--- a/stem/descriptor/bandwidth_file.py
+++ b/stem/descriptor/bandwidth_file.py
@@ -301,7 +301,7 @@ class BandwidthFile(Descriptor):
   ATTRIBUTES.update(dict([(k, (None, _parse_header)) for k in HEADER_ATTR.keys()]))
 
   @classmethod
-  def content(cls, attr = None, exclude = (), sign = False):
+  def content(cls, attr = None, exclude = ()):
     """
     Creates descriptor content with the given attributes. This descriptor type
     differs somewhat from others and treats our attr/exclude attributes as
@@ -324,9 +324,6 @@ class BandwidthFile(Descriptor):
       })
     """
 
-    if sign:
-      raise NotImplementedError('Signing of %s not implemented' % cls.__name__)
-
     header = collections.OrderedDict(attr) if attr is not None else collections.OrderedDict()
     timestamp = header.pop('timestamp', str(int(time.time())))
     content = header.pop('content', [])
diff --git a/stem/descriptor/extrainfo_descriptor.py b/stem/descriptor/extrainfo_descriptor.py
index f53d9502..c9125efa 100644
--- a/stem/descriptor/extrainfo_descriptor.py
+++ b/stem/descriptor/extrainfo_descriptor.py
@@ -924,10 +924,7 @@ class RelayExtraInfoDescriptor(ExtraInfoDescriptor):
       ('published', _random_date()),
     )
 
-    if signing_key:
-      sign = True
-
-    if sign:
+    if sign or signing_key:
       if attr and 'router-signature' in attr:
         raise ValueError('Cannot sign the descriptor if a router-signature has been provided')
 
@@ -990,10 +987,7 @@ class BridgeExtraInfoDescriptor(ExtraInfoDescriptor):
   })
 
   @classmethod
-  def content(cls, attr = None, exclude = (), sign = False):
-    if sign:
-      raise NotImplementedError('Signing of %s not implemented' % cls.__name__)
-
+  def content(cls, attr = None, exclude = ()):
     return _descriptor_content(attr, exclude, (
       ('extra-info', 'ec2bridgereaac65a3 %s' % _random_fingerprint()),
       ('published', _random_date()),
diff --git a/stem/descriptor/hidden_service.py b/stem/descriptor/hidden_service.py
index ed455fd1..8d6fa813 100644
--- a/stem/descriptor/hidden_service.py
+++ b/stem/descriptor/hidden_service.py
@@ -688,10 +688,7 @@ class HiddenServiceDescriptorV2(BaseHiddenServiceDescriptor):
   }
 
   @classmethod
-  def content(cls, attr = None, exclude = (), sign = False):
-    if sign:
-      raise NotImplementedError('Signing of %s not implemented' % cls.__name__)
-
+  def content(cls, attr = None, exclude = ()):
     return _descriptor_content(attr, exclude, (
       ('rendezvous-service-descriptor', 'y3olqqblqw2gbh6phimfuiroechjjafa'),
       ('version', '2'),
@@ -705,8 +702,8 @@ class HiddenServiceDescriptorV2(BaseHiddenServiceDescriptor):
     ))
 
   @classmethod
-  def create(cls, attr = None, exclude = (), validate = True, sign = False):
-    return cls(cls.content(attr, exclude, sign), validate = validate, skip_crypto_validation = not sign)
+  def create(cls, attr = None, exclude = (), validate = True):
+    return cls(cls.content(attr, exclude), validate = validate)
 
   def __init__(self, raw_contents, validate = False, skip_crypto_validation = False):
     super(HiddenServiceDescriptorV2, self).__init__(raw_contents, lazy_load = not validate)
@@ -1302,7 +1299,7 @@ class InnerLayer(Descriptor):
     return _encrypt_layer(self.get_bytes(), b'hsdir-encrypted-data', revision_counter, subcredential, blinded_key)
 
   @classmethod
-  def content(cls, attr = None, exclude = (), sign = False, introduction_points = None):
+  def content(cls, attr = None, exclude = (), introduction_points = None):
     if introduction_points:
       suffix = '\n' + '\n'.join(map(IntroductionPointV3.encode, introduction_points))
     else:
@@ -1313,8 +1310,8 @@ class InnerLayer(Descriptor):
     )) + stem.util.str_tools._to_bytes(suffix)
 
   @classmethod
-  def create(cls, attr = None, exclude = (), validate = True, sign = False, introduction_points = None):
-    return cls(cls.content(attr, exclude, sign, introduction_points), validate = validate)
+  def create(cls, attr = None, exclude = (), validate = True, introduction_points = None):
+    return cls(cls.content(attr, exclude, introduction_points), validate = validate)
 
   def __init__(self, content, validate = False, outer_layer = None):
     super(InnerLayer, self).__init__(content, lazy_load = not validate)
diff --git a/stem/descriptor/microdescriptor.py b/stem/descriptor/microdescriptor.py
index f8ee58e8..42d115d6 100644
--- a/stem/descriptor/microdescriptor.py
+++ b/stem/descriptor/microdescriptor.py
@@ -245,10 +245,7 @@ class Microdescriptor(Descriptor):
   }
 
   @classmethod
-  def content(cls, attr = None, exclude = (), sign = False):
-    if sign:
-      raise NotImplementedError('Signing of %s not implemented' % cls.__name__)
-
+  def content(cls, attr = None, exclude = ()):
     return _descriptor_content(attr, exclude, (
       ('onion-key', _random_crypto_blob('RSA PUBLIC KEY')),
     ))
diff --git a/stem/descriptor/networkstatus.py b/stem/descriptor/networkstatus.py
index 444ec0c2..77c6d612 100644
--- a/stem/descriptor/networkstatus.py
+++ b/stem/descriptor/networkstatus.py
@@ -598,10 +598,7 @@ class NetworkStatusDocumentV2(NetworkStatusDocument):
   }
 
   @classmethod
-  def content(cls, attr = None, exclude = (), sign = False):
-    if sign:
-      raise NotImplementedError('Signing of %s not implemented' % cls.__name__)
-
+  def content(cls, attr = None, exclude = ()):
     return _descriptor_content(attr, exclude, (
       ('network-status-version', '2'),
       ('dir-source', '%s %s 80' % (_random_ipv4_address(), _random_ipv4_address())),
@@ -1099,10 +1096,7 @@ class NetworkStatusDocumentV3(NetworkStatusDocument):
   }
 
   @classmethod
-  def content(cls, attr = None, exclude = (), sign = False, authorities = None, routers = None):
-    if sign:
-      raise NotImplementedError('Signing of %s not implemented' % cls.__name__)
-
+  def content(cls, attr = None, exclude = (), authorities = None, routers = None):
     attr = {} if attr is None else dict(attr)
     is_vote = attr.get('vote-status') == 'vote'
 
@@ -1174,8 +1168,8 @@ class NetworkStatusDocumentV3(NetworkStatusDocument):
     return desc_content
 
   @classmethod
-  def create(cls, attr = None, exclude = (), validate = True, sign = False, authorities = None, routers = None):
-    return cls(cls.content(attr, exclude, sign, authorities, routers), validate = validate)
+  def create(cls, attr = None, exclude = (), validate = True, authorities = None, routers = None):
+    return cls(cls.content(attr, exclude, authorities, routers), validate = validate)
 
   def __init__(self, raw_content, validate = False, default_params = True):
     """
@@ -1586,10 +1580,7 @@ class DirectoryAuthority(Descriptor):
   }
 
   @classmethod
-  def content(cls, attr = None, exclude = (), sign = False, is_vote = False):
-    if sign:
-      raise NotImplementedError('Signing of %s not implemented' % cls.__name__)
-
+  def content(cls, attr = None, exclude = (), is_vote = False):
     attr = {} if attr is None else dict(attr)
 
     # include mandatory 'vote-digest' if a consensus
@@ -1608,8 +1599,8 @@ class DirectoryAuthority(Descriptor):
     return content
 
   @classmethod
-  def create(cls, attr = None, exclude = (), validate = True, sign = False, is_vote = False):
-    return cls(cls.content(attr, exclude, sign, is_vote), validate = validate, is_vote = is_vote)
+  def create(cls, attr = None, exclude = (), validate = True, is_vote = False):
+    return cls(cls.content(attr, exclude, is_vote), validate = validate, is_vote = is_vote)
 
   def __init__(self, raw_content, validate = False, is_vote = False):
     """
@@ -1761,10 +1752,7 @@ class KeyCertificate(Descriptor):
   }
 
   @classmethod
-  def content(cls, attr = None, exclude = (), sign = False):
-    if sign:
-      raise NotImplementedError('Signing of %s not implemented' % cls.__name__)
-
+  def content(cls, attr = None, exclude = ()):
     return _descriptor_content(attr, exclude, (
       ('dir-key-certificate-version', '3'),
       ('fingerprint', _random_fingerprint()),
@@ -1910,10 +1898,7 @@ class DetachedSignature(Descriptor):
   }
 
   @classmethod
-  def content(cls, attr = None, exclude = (), sign = False):
-    if sign:
-      raise NotImplementedError('Signing of %s not implemented' % cls.__name__)
-
+  def content(cls, attr = None, exclude = ()):
     return _descriptor_content(attr, exclude, (
       ('consensus-digest', '6D3CC0EFA408F228410A4A8145E1B0BB0670E442'),
       ('valid-after', _random_date()),
diff --git a/stem/descriptor/router_status_entry.py b/stem/descriptor/router_status_entry.py
index f84997ac..0486d78d 100644
--- a/stem/descriptor/router_status_entry.py
+++ b/stem/descriptor/router_status_entry.py
@@ -513,10 +513,7 @@ class RouterStatusEntryV2(RouterStatusEntry):
   })
 
   @classmethod
-  def content(cls, attr = None, exclude = (), sign = False):
-    if sign:
-      raise NotImplementedError('Signing of %s not implemented' % cls.__name__)
-
+  def content(cls, attr = None, exclude = ()):
     return _descriptor_content(attr, exclude, (
       ('r', '%s p1aag7VwarGxqctS7/fS0y5FU+s oQZFLYe9e4A7bOkWKR7TaNxb0JE %s %s 9001 0' % (_random_nickname(), _random_date(), _random_ipv4_address())),
     ))
@@ -607,10 +604,7 @@ class RouterStatusEntryV3(RouterStatusEntry):
   })
 
   @classmethod
-  def content(cls, attr = None, exclude = (), sign = False):
-    if sign:
-      raise NotImplementedError('Signing of %s not implemented' % cls.__name__)
-
+  def content(cls, attr = None, exclude = ()):
     return _descriptor_content(attr, exclude, (
       ('r', '%s p1aag7VwarGxqctS7/fS0y5FU+s oQZFLYe9e4A7bOkWKR7TaNxb0JE %s %s 9001 0' % (_random_nickname(), _random_date(), _random_ipv4_address())),
       ('s', 'Fast Named Running Stable Valid'),
@@ -675,10 +669,7 @@ class RouterStatusEntryMicroV3(RouterStatusEntry):
   })
 
   @classmethod
-  def content(cls, attr = None, exclude = (), sign = False):
-    if sign:
-      raise NotImplementedError('Signing of %s not implemented' % cls.__name__)
-
+  def content(cls, attr = None, exclude = ()):
     return _descriptor_content(attr, exclude, (
       ('r', '%s ARIJF2zbqirB9IwsW0mQznccWww %s %s 9001 9030' % (_random_nickname(), _random_date(), _random_ipv4_address())),
       ('m', 'aiUklwBrua82obG5AsTX+iEpkjQA2+AQHxZ7GwMfY70'),
diff --git a/stem/descriptor/server_descriptor.py b/stem/descriptor/server_descriptor.py
index 36ddb624..d38bac9d 100644
--- a/stem/descriptor/server_descriptor.py
+++ b/stem/descriptor/server_descriptor.py
@@ -781,9 +781,6 @@ class RelayDescriptor(ServerDescriptor):
 
   @classmethod
   def content(cls, attr = None, exclude = (), sign = False, signing_key = None, exit_policy = None):
-    if signing_key:
-      sign = True
-
     if attr is None:
       attr = {}
 
@@ -801,7 +798,7 @@ class RelayDescriptor(ServerDescriptor):
       ('signing-key', _random_crypto_blob('RSA PUBLIC KEY')),
     ]
 
-    if sign:
+    if sign or signing_key:
       if attr and 'signing-key' in attr:
         raise ValueError('Cannot sign the descriptor if a signing-key has been provided')
       elif attr and 'router-signature' in attr:
@@ -939,10 +936,7 @@ class BridgeDescriptor(ServerDescriptor):
   })
 
   @classmethod
-  def content(cls, attr = None, exclude = (), sign = False):
-    if sign:
-      raise NotImplementedError('Signing of %s not implemented' % cls.__name__)
-
+  def content(cls, attr = None, exclude = ()):
     return _descriptor_content(attr, exclude, (
       ('router', '%s %s 9001 0 0' % (_random_nickname(), _random_ipv4_address())),
       ('router-digest', '006FD96BA35E7785A6A3B8B75FE2E2435A13BDB4'),





More information about the tor-commits mailing list