Skip to content

Commit

Permalink
Merge pull request #9 from dennisdegreef/supportCustomGnupgHome
Browse files Browse the repository at this point in the history
Respect the GNUPGHOME environment variable
  • Loading branch information
ellotheth authored Jun 18, 2016
2 parents 99e97f9 + 83f1304 commit d9eca26
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lookup/localpgp.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,27 @@ 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")

info, err := os.Stat(ringfile)
service := &LocalPGPService{}
service.buildRingfileName()

info, err := os.Stat(service.ringfile)
if err != nil || info.Size() == 0 {
return nil, err
}

return &LocalPGPService{ringfile: ringfile}, nil
return service, nil
}

func (l *LocalPGPService) buildRingfileName() {
gnupgHome := path.Join(os.Getenv("HOME"), ".gnupg")

// Check if an override for GNUPG home is set
if os.Getenv("GNUPGHOME") != "" {
gnupgHome = os.Getenv("GNUPGHOME")
}

l.ringfile = path.Join(gnupgHome, "pubring.gpg")
}

// Ring loads the local public keyring so LocalPGPService can use it later. If
Expand Down
33 changes: 33 additions & 0 deletions lookup/localpgp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ the source. If not, see http://www.gnu.org/licenses/gpl-2.0.html.
package lookup

import (
"os"
"testing"

"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -48,6 +49,38 @@ func (s *LocalPGPTest) TestIsMatchFailsWithoutMatches() {
s.False(local.isMatch("foo", user))
}

func (s *LocalPGPTest) TestGnupgHomeOverride() {
os.Setenv("GNUPGHOME", "/foo")
_, err := NewLocalPGPService()
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))
}

0 comments on commit d9eca26

Please sign in to comment.