From d4c4cf01f0536c2e14589169557fed610f0e449d Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Mon, 13 Jul 2020 16:33:39 +0200 Subject: [PATCH] lnd+macaroons: specify location when creating macaroon service To allow the macaroon service to be used in other projects, we want the location to be passed in as a parameter instead of being hard coded. --- lnd.go | 2 +- macaroons/service.go | 4 ++-- macaroons/service_test.go | 16 ++++++++++++---- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lnd.go b/lnd.go index 2ed6f30d..b4fde604 100644 --- a/lnd.go +++ b/lnd.go @@ -393,7 +393,7 @@ func Main(cfg *Config, lisCfg ListenerCfg, shutdownChan <-chan struct{}) error { if !cfg.NoMacaroons { // Create the macaroon authentication/authorization service. macaroonService, err = macaroons.NewService( - cfg.networkDir, macaroons.IPLockChecker, + cfg.networkDir, "lnd", macaroons.IPLockChecker, ) if err != nil { err := fmt.Errorf("unable to set up macaroon "+ diff --git a/macaroons/service.go b/macaroons/service.go index 0e84ddeb..a1f750ac 100644 --- a/macaroons/service.go +++ b/macaroons/service.go @@ -54,7 +54,7 @@ type Service struct { // listing the same checker more than once is not harmful. Default checkers, // such as those for `allow`, `time-before`, `declared`, and `error` caveats // are registered automatically and don't need to be added. -func NewService(dir string, checks ...Checker) (*Service, error) { +func NewService(dir, location string, checks ...Checker) (*Service, error) { // Ensure that the path to the directory exists. if _, err := os.Stat(dir); os.IsNotExist(err) { if err := os.MkdirAll(dir, 0700); err != nil { @@ -77,7 +77,7 @@ func NewService(dir string, checks ...Checker) (*Service, error) { } macaroonParams := bakery.BakeryParams{ - Location: "lnd", + Location: location, RootKeyStore: rootKeyStore, // No third-party caveat support for now. // TODO(aakselrod): Add third-party caveat support. diff --git a/macaroons/service_test.go b/macaroons/service_test.go index 90c9b573..5f584240 100644 --- a/macaroons/service_test.go +++ b/macaroons/service_test.go @@ -66,7 +66,9 @@ func TestNewService(t *testing.T) { // Second, create the new service instance, unlock it and pass in a // checker that we expect it to add to the bakery. - service, err := macaroons.NewService(tempDir, macaroons.IPLockChecker) + service, err := macaroons.NewService( + tempDir, "lnd", macaroons.IPLockChecker, + ) if err != nil { t.Fatalf("Error creating new service: %v", err) } @@ -115,7 +117,9 @@ func TestValidateMacaroon(t *testing.T) { // First, initialize the service and unlock it. tempDir := setupTestRootKeyStorage(t) defer os.RemoveAll(tempDir) - service, err := macaroons.NewService(tempDir, macaroons.IPLockChecker) + service, err := macaroons.NewService( + tempDir, "lnd", macaroons.IPLockChecker, + ) if err != nil { t.Fatalf("Error creating new service: %v", err) } @@ -173,7 +177,9 @@ func TestListMacaroonIDs(t *testing.T) { // Second, create the new service instance, unlock it and pass in a // checker that we expect it to add to the bakery. - service, err := macaroons.NewService(tempDir, macaroons.IPLockChecker) + service, err := macaroons.NewService( + tempDir, "lnd", macaroons.IPLockChecker, + ) require.NoError(t, err, "Error creating new service") defer service.Close() @@ -203,7 +209,9 @@ func TestDeleteMacaroonID(t *testing.T) { // Second, create the new service instance, unlock it and pass in a // checker that we expect it to add to the bakery. - service, err := macaroons.NewService(tempDir, macaroons.IPLockChecker) + service, err := macaroons.NewService( + tempDir, "lnd", macaroons.IPLockChecker, + ) require.NoError(t, err, "Error creating new service") defer service.Close()