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.
This commit is contained in:
Oliver Gugger 2020-07-13 16:33:39 +02:00
parent 8668248d96
commit d4c4cf01f0
No known key found for this signature in database
GPG Key ID: 8E4256593F177720
3 changed files with 15 additions and 7 deletions

2
lnd.go

@ -393,7 +393,7 @@ func Main(cfg *Config, lisCfg ListenerCfg, shutdownChan <-chan struct{}) error {
if !cfg.NoMacaroons { if !cfg.NoMacaroons {
// Create the macaroon authentication/authorization service. // Create the macaroon authentication/authorization service.
macaroonService, err = macaroons.NewService( macaroonService, err = macaroons.NewService(
cfg.networkDir, macaroons.IPLockChecker, cfg.networkDir, "lnd", macaroons.IPLockChecker,
) )
if err != nil { if err != nil {
err := fmt.Errorf("unable to set up macaroon "+ err := fmt.Errorf("unable to set up macaroon "+

@ -54,7 +54,7 @@ type Service struct {
// listing the same checker more than once is not harmful. Default checkers, // listing the same checker more than once is not harmful. Default checkers,
// such as those for `allow`, `time-before`, `declared`, and `error` caveats // such as those for `allow`, `time-before`, `declared`, and `error` caveats
// are registered automatically and don't need to be added. // 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. // Ensure that the path to the directory exists.
if _, err := os.Stat(dir); os.IsNotExist(err) { if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := os.MkdirAll(dir, 0700); err != nil { if err := os.MkdirAll(dir, 0700); err != nil {
@ -77,7 +77,7 @@ func NewService(dir string, checks ...Checker) (*Service, error) {
} }
macaroonParams := bakery.BakeryParams{ macaroonParams := bakery.BakeryParams{
Location: "lnd", Location: location,
RootKeyStore: rootKeyStore, RootKeyStore: rootKeyStore,
// No third-party caveat support for now. // No third-party caveat support for now.
// TODO(aakselrod): Add third-party caveat support. // TODO(aakselrod): Add third-party caveat support.

@ -66,7 +66,9 @@ func TestNewService(t *testing.T) {
// Second, create the new service instance, unlock it and pass in a // Second, create the new service instance, unlock it and pass in a
// checker that we expect it to add to the bakery. // 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 { if err != nil {
t.Fatalf("Error creating new service: %v", err) t.Fatalf("Error creating new service: %v", err)
} }
@ -115,7 +117,9 @@ func TestValidateMacaroon(t *testing.T) {
// First, initialize the service and unlock it. // First, initialize the service and unlock it.
tempDir := setupTestRootKeyStorage(t) tempDir := setupTestRootKeyStorage(t)
defer os.RemoveAll(tempDir) defer os.RemoveAll(tempDir)
service, err := macaroons.NewService(tempDir, macaroons.IPLockChecker) service, err := macaroons.NewService(
tempDir, "lnd", macaroons.IPLockChecker,
)
if err != nil { if err != nil {
t.Fatalf("Error creating new service: %v", err) 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 // Second, create the new service instance, unlock it and pass in a
// checker that we expect it to add to the bakery. // 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") require.NoError(t, err, "Error creating new service")
defer service.Close() defer service.Close()
@ -203,7 +209,9 @@ func TestDeleteMacaroonID(t *testing.T) {
// Second, create the new service instance, unlock it and pass in a // Second, create the new service instance, unlock it and pass in a
// checker that we expect it to add to the bakery. // 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") require.NoError(t, err, "Error creating new service")
defer service.Close() defer service.Close()