Skip to content

Commit d9eca26

Browse files
authored
Merge pull request #9 from dennisdegreef/supportCustomGnupgHome
Respect the GNUPGHOME environment variable
2 parents 99e97f9 + 83f1304 commit d9eca26

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

lookup/localpgp.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,27 @@ type LocalPGPService struct {
2929
// NewLocalPGPService creates a new LocalPGPService if it finds a local
3030
// public keyring; otherwise it bails.
3131
func NewLocalPGPService() (*LocalPGPService, error) {
32-
ringfile := path.Join(os.Getenv("HOME"), ".gnupg", "pubring.gpg")
3332

34-
info, err := os.Stat(ringfile)
33+
service := &LocalPGPService{}
34+
service.buildRingfileName()
35+
36+
info, err := os.Stat(service.ringfile)
3537
if err != nil || info.Size() == 0 {
3638
return nil, err
3739
}
3840

39-
return &LocalPGPService{ringfile: ringfile}, nil
41+
return service, nil
42+
}
43+
44+
func (l *LocalPGPService) buildRingfileName() {
45+
gnupgHome := path.Join(os.Getenv("HOME"), ".gnupg")
46+
47+
// Check if an override for GNUPG home is set
48+
if os.Getenv("GNUPGHOME") != "" {
49+
gnupgHome = os.Getenv("GNUPGHOME")
50+
}
51+
52+
l.ringfile = path.Join(gnupgHome, "pubring.gpg")
4053
}
4154

4255
// Ring loads the local public keyring so LocalPGPService can use it later. If

lookup/localpgp_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ the source. If not, see http://www.gnu.org/licenses/gpl-2.0.html.
1010
package lookup
1111

1212
import (
13+
"os"
1314
"testing"
1415

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

52+
func (s *LocalPGPTest) TestGnupgHomeOverride() {
53+
os.Setenv("GNUPGHOME", "/foo")
54+
_, err := NewLocalPGPService()
55+
s.EqualError(err, "stat /foo/pubring.gpg: no such file or directory")
56+
os.Unsetenv("GNUPGHOME")
57+
}
58+
59+
func (s *LocalPGPTest) TestBuildRingfileName() {
60+
cases := []struct {
61+
home string
62+
gnupghome string
63+
expected string
64+
}{
65+
{"/foo/", "", "/foo/.gnupg/pubring.gpg"},
66+
{"/foo", "", "/foo/.gnupg/pubring.gpg"},
67+
{"foo", "", "foo/.gnupg/pubring.gpg"},
68+
{"foo", "/things", "/things/pubring.gpg"},
69+
{"foo", "/things/", "/things/pubring.gpg"},
70+
{"foo", "things/", "things/pubring.gpg"},
71+
{"", "/things/", "/things/pubring.gpg"},
72+
{"", "", ".gnupg/pubring.gpg"},
73+
}
74+
75+
for _, c := range cases {
76+
os.Setenv("HOME", c.home)
77+
os.Setenv("GNUPGHOME", c.gnupghome)
78+
local := LocalPGPService{}
79+
local.buildRingfileName()
80+
s.Equal(c.expected, local.ringfile)
81+
}
82+
}
83+
5184
func TestLocalPGPTest(t *testing.T) {
5285
suite.Run(t, new(LocalPGPTest))
5386
}

0 commit comments

Comments
 (0)