tools.linux.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH b4] Delay dns.resolver.get_default_resolver call
@ 2021-02-08  4:07 Kyle Meyer
  2021-04-21 23:43 ` Kyle Meyer
  0 siblings, 1 reply; 5+ messages in thread
From: Kyle Meyer @ 2021-02-08  4:07 UTC (permalink / raw)
  To: tools

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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH b4] Delay dns.resolver.get_default_resolver call
  2021-02-08  4:07 [PATCH b4] Delay dns.resolver.get_default_resolver call Kyle Meyer
@ 2021-04-21 23:43 ` Kyle Meyer
  2021-04-22 18:09   ` Konstantin Ryabitsev
  2021-05-14 20:37   ` Konstantin Ryabitsev
  0 siblings, 2 replies; 5+ messages in thread
From: Kyle Meyer @ 2021-04-21 23:43 UTC (permalink / raw)
  To: tools

Kyle Meyer writes:

> 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.

Any thoughts on this patch?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH b4] Delay dns.resolver.get_default_resolver call
  2021-04-21 23:43 ` Kyle Meyer
@ 2021-04-22 18:09   ` Konstantin Ryabitsev
  2021-05-14 20:37   ` Konstantin Ryabitsev
  1 sibling, 0 replies; 5+ messages in thread
From: Konstantin Ryabitsev @ 2021-04-22 18:09 UTC (permalink / raw)
  To: Kyle Meyer; +Cc: tools

On Wed, Apr 21, 2021 at 07:43:44PM -0400, Kyle Meyer wrote:
> Kyle Meyer writes:
> 
> > 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.
> 
> Any thoughts on this patch?

Sorry, Kyle, it wasn't my intent to delay for long. I had to refocus my
efforts on something else for a while, so b4 took a bit of a backseat (plus,
I'm waiting on public-inbox 1.7 to be out so I can drop a bunch of redundant
features from b4 before putting in any work on new stuff).

I'll deal with all outstanding patches either tomorrow or early next week.

-K

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH b4] Delay dns.resolver.get_default_resolver call
  2021-04-21 23:43 ` Kyle Meyer
  2021-04-22 18:09   ` Konstantin Ryabitsev
@ 2021-05-14 20:37   ` Konstantin Ryabitsev
  2021-05-14 21:24     ` Kyle Meyer
  1 sibling, 1 reply; 5+ messages in thread
From: Konstantin Ryabitsev @ 2021-05-14 20:37 UTC (permalink / raw)
  To: Kyle Meyer; +Cc: tools

On Wed, Apr 21, 2021 at 07:43:44PM -0400, Kyle Meyer wrote:
> Kyle Meyer writes:
> 
> > 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.
> 
> Any thoughts on this patch?

Kyle:

I reworked the current 0.7-dev version to remove the little hacks for caching
that I added. I'm comfortable with how the latest dns-python module is doing
it by default, so we just defer to it for doing whatever caching necessary.

This should allow it to be a fully offline operation again.

Thanks and best regards,
-K

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH b4] Delay dns.resolver.get_default_resolver call
  2021-05-14 20:37   ` Konstantin Ryabitsev
@ 2021-05-14 21:24     ` Kyle Meyer
  0 siblings, 0 replies; 5+ messages in thread
From: Kyle Meyer @ 2021-05-14 21:24 UTC (permalink / raw)
  To: Konstantin Ryabitsev; +Cc: tools

Konstantin Ryabitsev writes:

> Kyle:
>
> I reworked the current 0.7-dev version to remove the little hacks for caching
> that I added. I'm comfortable with how the latest dns-python module is doing
> it by default, so we just defer to it for doing whatever caching necessary.

Sounds good.

> This should allow it to be a fully offline operation again.

Confirmed.  Thanks.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-05-14 21:34 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-08  4:07 [PATCH b4] Delay dns.resolver.get_default_resolver call Kyle Meyer
2021-04-21 23:43 ` Kyle Meyer
2021-04-22 18:09   ` Konstantin Ryabitsev
2021-05-14 20:37   ` Konstantin Ryabitsev
2021-05-14 21:24     ` Kyle Meyer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).