All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Kyle Meyer" <kyle@kyleam.com>
To: tools@linux.kernel.org
Subject: [PATCH b4] Delay dns.resolver.get_default_resolver call
Date: Sun,  7 Feb 2021 23:07:22 -0500	[thread overview]
Message-ID: <20210208040722.31203-1-kyle@kyleam.com> (raw)

When b4.attestation-policy is 'off', calling `b4 am` with
--use-local-mbox is conceptually an offline operation, but it still
requires being online because get_default_resolver() is called at the
top-level of b4/__init__.py.

Support offline operation by delaying the call to
get_default_resolver() until it's needed.  Note that
get_default_resolver() already caches the result, so there's no need
for b4 to store the result as a global variable on its end.

Signed-off-by: Kyle Meyer <kyle@kyleam.com>
---
 b4/__init__.py | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/b4/__init__.py b/b4/__init__.py
index 17fae0e..49304fd 100644
--- a/b4/__init__.py
+++ b/b4/__init__.py
@@ -33,10 +33,8 @@
     import dkim
 
     can_dkim_verify = True
-    _resolver = dns.resolver.get_default_resolver()
 except ModuleNotFoundError:
     can_dkim_verify = False
-    _resolver = None
 
 __VERSION__ = '0.6.3-dev'
 
@@ -1703,7 +1701,8 @@ def __init__(self, msg):
             ddata = get_parts_from_header(dks)
             self.attestor = LoreAttestorDKIM(ddata['d'])
             # Do we have a resolve method?
-            if _resolver and hasattr(_resolver, 'resolve'):
+            resolver = get_resolver()
+            if resolver and hasattr(resolver, 'resolve'):
                 res = dkim.verify(self.msg.as_bytes(), dnsfunc=dkim_get_txt)
             else:
                 res = dkim.verify(self.msg.as_bytes())
@@ -2448,13 +2447,19 @@ def validate_gpg_signature(output, trustmodel):
     return good, valid, trusted, attestor, sigdate, errors
 
 
+def get_resolver():
+    if can_dkim_verify:
+        return dns.resolver.get_default_resolver()
+
+
 def dkim_get_txt(name: bytes, timeout: int = 5):
     global _DKIM_DNS_CACHE
     if name not in _DKIM_DNS_CACHE:
         lookup = name.decode()
         logger.debug('DNS-lookup: %s', lookup)
+        resolver = get_resolver()
         try:
-            a = _resolver.resolve(lookup, dns.rdatatype.TXT, raise_on_no_answer=False, lifetime=timeout, search=True)
+            a = resolver.resolve(lookup, dns.rdatatype.TXT, raise_on_no_answer=False, lifetime=timeout, search=True)
             for r in a.response.answer:
                 if r.rdtype == dns.rdatatype.TXT:
                     for item in r.items:

base-commit: f93bbd3e50b1fb4507aa537f4004da545af9d890
-- 
2.30.0


             reply	other threads:[~2021-02-08  4:07 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-08  4:07 Kyle Meyer [this message]
2021-04-21 23:43 ` [PATCH b4] Delay dns.resolver.get_default_resolver call Kyle Meyer
2021-04-22 18:09   ` Konstantin Ryabitsev
2021-05-14 20:37   ` Konstantin Ryabitsev
2021-05-14 21:24     ` Kyle Meyer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210208040722.31203-1-kyle@kyleam.com \
    --to=kyle@kyleam.com \
    --cc=tools@linux.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.