From 53adfcb2814619d90018ef389d0d84324a1fd4fe Mon Sep 17 00:00:00 2001 From: Dennis de Greef Date: Sat, 30 Apr 2016 12:10:23 +0200 Subject: [PATCH 1/3] Support GNUPGHOME environment variable --- lookup/localpgp.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lookup/localpgp.go b/lookup/localpgp.go index 4d400b9..5b79647 100644 --- a/lookup/localpgp.go +++ b/lookup/localpgp.go @@ -29,7 +29,15 @@ type LocalPGPService struct { // NewLocalPGPService creates a new LocalPGPService if it finds a local // public keyring; otherwise it bails. func NewLocalPGPService() (*LocalPGPService, error) { - ringfile := path.Join(os.Getenv("HOME"), ".gnupg", "pubring.gpg") + + home := os.Getenv("HOME") + + // Check if an override for GNUPG home is set + if os.Getenv("GNUPGHOME") != "" { + home = os.Getenv("GNUPGHOME") + } + + ringfile := path.Join(home, ".gnupg", "pubring.gpg") info, err := os.Stat(ringfile) if err != nil || info.Size() == 0 { From 17909529c35c3d83eae343286b647658072c8273 Mon Sep 17 00:00:00 2001 From: Dennis de Greef Date: Sat, 14 May 2016 23:23:21 +0200 Subject: [PATCH 2/3] Change path to proper GNUPGHOME directory and add test --- lookup/localpgp.go | 6 +++--- lookup/localpgp_test.go | 11 +++++++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lookup/localpgp.go b/lookup/localpgp.go index 5b79647..60bf476 100644 --- a/lookup/localpgp.go +++ b/lookup/localpgp.go @@ -30,14 +30,14 @@ type LocalPGPService struct { // public keyring; otherwise it bails. func NewLocalPGPService() (*LocalPGPService, error) { - home := os.Getenv("HOME") + gnupgHome := path.Join(os.Getenv("HOME"), ".gnupg") // Check if an override for GNUPG home is set if os.Getenv("GNUPGHOME") != "" { - home = os.Getenv("GNUPGHOME") + gnupgHome = os.Getenv("GNUPGHOME") } - ringfile := path.Join(home, ".gnupg", "pubring.gpg") + ringfile := path.Join(gnupgHome, "pubring.gpg") info, err := os.Stat(ringfile) if err != nil || info.Size() == 0 { diff --git a/lookup/localpgp_test.go b/lookup/localpgp_test.go index d640b2a..33473a7 100644 --- a/lookup/localpgp_test.go +++ b/lookup/localpgp_test.go @@ -10,9 +10,9 @@ the source. If not, see http://www.gnu.org/licenses/gpl-2.0.html. package lookup import ( - "testing" - "github.com/stretchr/testify/suite" + "os" + "testing" ) type LocalPGPTest struct { @@ -48,6 +48,13 @@ func (s *LocalPGPTest) TestIsMatchFailsWithoutMatches() { s.False(local.isMatch("foo", user)) } +func (s *LocalPGPTest) TestGnupgHomeOverride() { + os.Setenv("GNUPGHOME", "/foo") + _, err := NewLocalPGPService() + s.True(err.Error() == "stat /foo/pubring.gpg: no such file or directory") + os.Unsetenv("GNUPGHOME") +} + func TestLocalPGPTest(t *testing.T) { suite.Run(t, new(LocalPGPTest)) } From 83f13048ae9eae3d29b57ffebce2763fb309447e Mon Sep 17 00:00:00 2001 From: Gemma Lynn Date: Sun, 12 Jun 2016 07:16:49 -0600 Subject: [PATCH 3/3] extract ringfile path building logic Alternate implementation for #9 --- lookup/localpgp.go | 21 +++++++++++++-------- lookup/localpgp_test.go | 30 ++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/lookup/localpgp.go b/lookup/localpgp.go index 60bf476..bc9d4de 100644 --- a/lookup/localpgp.go +++ b/lookup/localpgp.go @@ -30,6 +30,18 @@ type LocalPGPService struct { // public keyring; otherwise it bails. func NewLocalPGPService() (*LocalPGPService, error) { + service := &LocalPGPService{} + service.buildRingfileName() + + info, err := os.Stat(service.ringfile) + if err != nil || info.Size() == 0 { + return nil, err + } + + return service, nil +} + +func (l *LocalPGPService) buildRingfileName() { gnupgHome := path.Join(os.Getenv("HOME"), ".gnupg") // Check if an override for GNUPG home is set @@ -37,14 +49,7 @@ func NewLocalPGPService() (*LocalPGPService, error) { gnupgHome = os.Getenv("GNUPGHOME") } - ringfile := path.Join(gnupgHome, "pubring.gpg") - - info, err := os.Stat(ringfile) - if err != nil || info.Size() == 0 { - return nil, err - } - - return &LocalPGPService{ringfile: ringfile}, nil + l.ringfile = path.Join(gnupgHome, "pubring.gpg") } // Ring loads the local public keyring so LocalPGPService can use it later. If diff --git a/lookup/localpgp_test.go b/lookup/localpgp_test.go index 33473a7..744698d 100644 --- a/lookup/localpgp_test.go +++ b/lookup/localpgp_test.go @@ -10,9 +10,10 @@ the source. If not, see http://www.gnu.org/licenses/gpl-2.0.html. package lookup import ( - "github.com/stretchr/testify/suite" "os" "testing" + + "github.com/stretchr/testify/suite" ) type LocalPGPTest struct { @@ -51,10 +52,35 @@ func (s *LocalPGPTest) TestIsMatchFailsWithoutMatches() { func (s *LocalPGPTest) TestGnupgHomeOverride() { os.Setenv("GNUPGHOME", "/foo") _, err := NewLocalPGPService() - s.True(err.Error() == "stat /foo/pubring.gpg: no such file or directory") + s.EqualError(err, "stat /foo/pubring.gpg: no such file or directory") os.Unsetenv("GNUPGHOME") } +func (s *LocalPGPTest) TestBuildRingfileName() { + cases := []struct { + home string + gnupghome string + expected string + }{ + {"/foo/", "", "/foo/.gnupg/pubring.gpg"}, + {"/foo", "", "/foo/.gnupg/pubring.gpg"}, + {"foo", "", "foo/.gnupg/pubring.gpg"}, + {"foo", "/things", "/things/pubring.gpg"}, + {"foo", "/things/", "/things/pubring.gpg"}, + {"foo", "things/", "things/pubring.gpg"}, + {"", "/things/", "/things/pubring.gpg"}, + {"", "", ".gnupg/pubring.gpg"}, + } + + for _, c := range cases { + os.Setenv("HOME", c.home) + os.Setenv("GNUPGHOME", c.gnupghome) + local := LocalPGPService{} + local.buildRingfileName() + s.Equal(c.expected, local.ringfile) + } +} + func TestLocalPGPTest(t *testing.T) { suite.Run(t, new(LocalPGPTest)) }