Skip Menu |

This queue is for tickets about the Mail-GnuPG CPAN distribution.

Report information
The Basics
Id: 77070
Status: resolved
Priority: 0/
Queue: Mail-GnuPG

People
Owner: Nobody in particular
Requestors: TWILDE [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: 0.17
Fixed in: (no value)



Subject: Issue with has_public_key
Greetings! I have come across an issue with the has_public_key method; when using a GPG key directory other than the default/expected directory by the gpg binary (ie: Mail::GnuPG->new(keydir => '/foo/.gnupg');), the behavior of has_public_key is inconsistent, because it is simply calling gpg manually, outside of the arguments passed to the module. In the situation I'm in, this means gpg is using the wrong key directory, and thus this method returns incorrect results. Would you accept a patch that modified has_public_key to use the get_public_keys method of GnuPG::Interface instead? If so, I will attempt to craft one; is there a git or other SCM repo I can use for the latest sources, or is working from the CPAN distribution sufficient? Best regards, Tim Wilde
Subject: Re: [rt.cpan.org #77070] Issue with has_public_key
Date: Tue, 08 May 2012 21:22:13 -0300
To: bug-Mail-GnuPG [...] rt.cpan.org
From: David Bremner <david [...] tethera.net>
Tim Wilde via RT <bug-Mail-GnuPG@rt.cpan.org> writes: Show quoted text
> Would you accept a patch > that modified has_public_key to use the get_public_keys method of > GnuPG::Interface instead? If so, I will attempt to craft one; is there > a git or other SCM repo I can use for the latest sources, or is working > from the CPAN distribution sufficient?
Hi Tim; Without thinking about it too deeply, what you propose sounds sensible. So I'd say go ahead with a patch You can get the latest sources from git://pivot.cs.unb.ca/mail-gnupg d
CC: Tim Wilde <github [...] krellis.org>
Subject: [rt.cpan.org #77070] [PATCH] Use GnuPG::Interface for has_public_key * Now works with non-standard keydir * Regression test added in t/35-has-public-key.t
Date: Wed, 9 May 2012 13:25:44 +0000
To: bug-Mail-GnuPG [...] rt.cpan.org
From: Tim Wilde <twilde [...] cpan.org>
From: Tim Wilde <github@krellis.org> --- MANIFEST | 1 + lib/Mail/GnuPG.pm | 17 +++++++++-------- t/35.has-public-key.t | 21 +++++++++++++++++++++ 3 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 t/35.has-public-key.t diff --git a/MANIFEST b/MANIFEST index 6750e81..1cb636a 100644 --- a/MANIFEST +++ b/MANIFEST @@ -8,6 +8,7 @@ t/05.load.t t/20.inline-verify.t t/25.multipart-verify.t t/30.inline-decrypt.t +t/35.has-public-key.t t/99.pod.t t/agent.t t/base.t diff --git a/lib/Mail/GnuPG.pm b/lib/Mail/GnuPG.pm index ef12594..baff93e 100644 --- a/lib/Mail/GnuPG.pm +++ b/lib/Mail/GnuPG.pm @@ -476,14 +476,15 @@ sub _rebuild_key_cache { my $self = shift; local $_; %key_cache = (); - # sometimes the best tool for the job... is not perl - open(my $fh, "$self->{gpg_path} --list-public-keys --with-colons | cut -d: -f10|") - or die $!; - while(<$fh>) { - next unless $_; - # M::A may not parse the gpg stuff properly. Cross fingers - my ($a) = Mail::Address->parse($_); # list context, please - $key_cache{$a->address}=1 if ref $a; + my $gnupg = GnuPG::Interface->new(); + $self->_set_options($gnupg); + my @keys = $gnupg->get_public_keys(); + foreach my $key (@keys) { + foreach my $uid ($key->user_ids) { + # M::A may not parse the gpg stuff properly. Cross fingers + my ($a) = Mail::Address->parse($uid->as_string); # list context, please + $key_cache{$a->address}=1 if ref $a; + } } } diff --git a/t/35.has-public-key.t b/t/35.has-public-key.t new file mode 100644 index 0000000..2fbb9ab --- /dev/null +++ b/t/35.has-public-key.t @@ -0,0 +1,21 @@ +# -*- perl -*- + +use Test::More; +use Mail::GnuPG; +use strict; + +require('t/import_keys.pl'); +my $gpghome=import_keys('t/test-key.pgp'); +unless (defined($gpghome)){ + plan skip_all => "failed to import GPG keys for testing"; + goto end; +} + +plan tests => 2; + +my $mg = new Mail::GnuPG( keydir => $gpghome ); +isa_ok($mg, 'Mail::GnuPG'); + +ok($mg->has_public_key('mail@gnupg.dom'), 'has_public_key works'); + +end: -- 1.7.9.6
I have an updated version of this patch with an additional test coming, sorry for the extra noise, bear with me. Tim On Wed May 09 09:26:04 2012, TWILDE wrote: Show quoted text
> From: Tim Wilde <github@krellis.org> > > --- > MANIFEST | 1 + > lib/Mail/GnuPG.pm | 17 +++++++++-------- > t/35.has-public-key.t | 21 +++++++++++++++++++++ > 3 files changed, 31 insertions(+), 8 deletions(-) > create mode 100644 t/35.has-public-key.t > > diff --git a/MANIFEST b/MANIFEST > index 6750e81..1cb636a 100644 > --- a/MANIFEST > +++ b/MANIFEST > @@ -8,6 +8,7 @@ t/05.load.t > t/20.inline-verify.t > t/25.multipart-verify.t > t/30.inline-decrypt.t > +t/35.has-public-key.t > t/99.pod.t > t/agent.t > t/base.t > diff --git a/lib/Mail/GnuPG.pm b/lib/Mail/GnuPG.pm > index ef12594..baff93e 100644 > --- a/lib/Mail/GnuPG.pm > +++ b/lib/Mail/GnuPG.pm > @@ -476,14 +476,15 @@ sub _rebuild_key_cache { > my $self = shift; > local $_; > %key_cache = (); > - # sometimes the best tool for the job... is not perl > - open(my $fh, "$self->{gpg_path} --list-public-keys --with-colons | > cut -d: -f10|") > - or die $!; > - while(<$fh>) { > - next unless $_; > - # M::A may not parse the gpg stuff properly. Cross fingers > - my ($a) = Mail::Address->parse($_); # list context, please > - $key_cache{$a->address}=1 if ref $a; > + my $gnupg = GnuPG::Interface->new(); > + $self->_set_options($gnupg); > + my @keys = $gnupg->get_public_keys(); > + foreach my $key (@keys) { > + foreach my $uid ($key->user_ids) { > + # M::A may not parse the gpg stuff properly. Cross fingers > + my ($a) = Mail::Address->parse($uid->as_string); # list > context, please > + $key_cache{$a->address}=1 if ref $a; > + } > } > } > > diff --git a/t/35.has-public-key.t b/t/35.has-public-key.t > new file mode 100644 > index 0000000..2fbb9ab > --- /dev/null > +++ b/t/35.has-public-key.t > @@ -0,0 +1,21 @@ > +# -*- perl -*- > + > +use Test::More; > +use Mail::GnuPG; > +use strict; > + > +require('t/import_keys.pl'); > +my $gpghome=import_keys('t/test-key.pgp'); > +unless (defined($gpghome)){ > + plan skip_all => "failed to import GPG keys for testing"; > + goto end; > +} > + > +plan tests => 2; > + > +my $mg = new Mail::GnuPG( keydir => $gpghome ); > +isa_ok($mg, 'Mail::GnuPG'); > + > +ok($mg->has_public_key('mail@gnupg.dom'), 'has_public_key works'); > + > +end:
CC: Tim Wilde <github [...] krellis.org>
Subject: [rt.cpan.org #77070] [PATCH] Use GnuPG::Interface for has_public_key
Date: Wed, 9 May 2012 13:31:41 +0000
To: bug-mail-gnupg [...] rt.cpan.org
From: Tim Wilde <twilde [...] cpan.org>
From: Tim Wilde <github@krellis.org> * Now works with non-standard keydir * Regression test added in t/35-has-public-key.t --- MANIFEST | 1 + lib/Mail/GnuPG.pm | 17 +++++++++-------- t/35.has-public-key.t | 22 ++++++++++++++++++++++ 3 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 t/35.has-public-key.t diff --git a/MANIFEST b/MANIFEST index 6750e81..1cb636a 100644 --- a/MANIFEST +++ b/MANIFEST @@ -8,6 +8,7 @@ t/05.load.t t/20.inline-verify.t t/25.multipart-verify.t t/30.inline-decrypt.t +t/35.has-public-key.t t/99.pod.t t/agent.t t/base.t diff --git a/lib/Mail/GnuPG.pm b/lib/Mail/GnuPG.pm index ef12594..baff93e 100644 --- a/lib/Mail/GnuPG.pm +++ b/lib/Mail/GnuPG.pm @@ -476,14 +476,15 @@ sub _rebuild_key_cache { my $self = shift; local $_; %key_cache = (); - # sometimes the best tool for the job... is not perl - open(my $fh, "$self->{gpg_path} --list-public-keys --with-colons | cut -d: -f10|") - or die $!; - while(<$fh>) { - next unless $_; - # M::A may not parse the gpg stuff properly. Cross fingers - my ($a) = Mail::Address->parse($_); # list context, please - $key_cache{$a->address}=1 if ref $a; + my $gnupg = GnuPG::Interface->new(); + $self->_set_options($gnupg); + my @keys = $gnupg->get_public_keys(); + foreach my $key (@keys) { + foreach my $uid ($key->user_ids) { + # M::A may not parse the gpg stuff properly. Cross fingers + my ($a) = Mail::Address->parse($uid->as_string); # list context, please + $key_cache{$a->address}=1 if ref $a; + } } } diff --git a/t/35.has-public-key.t b/t/35.has-public-key.t new file mode 100644 index 0000000..b8716b6 --- /dev/null +++ b/t/35.has-public-key.t @@ -0,0 +1,22 @@ +# -*- perl -*- + +use Test::More; +use Mail::GnuPG; +use strict; + +require('t/import_keys.pl'); +my $gpghome=import_keys('t/test-key.pgp'); +unless (defined($gpghome)){ + plan skip_all => "failed to import GPG keys for testing"; + goto end; +} + +plan tests => 3; + +my $mg = new Mail::GnuPG( keydir => $gpghome ); +isa_ok($mg, 'Mail::GnuPG'); + +is($mg->has_public_key('mail@gnupg.dom'), 1, 'public key exists'); +is($mg->has_public_key('bogus@email.example.com'), 0, "bogus key doesn't exist"); + +end: -- 1.7.9.6
Hi Tim; Sorry for the delay, I have just uploaded 0.18 with your patch. d