From 856500dc433c16368345ae08a86bea6b62147a2f Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Wed, 22 Aug 2018 15:25:03 -0400 Subject: [PATCH] macaroons: ensure path for macaroon DB exists Co-Authored-By: Karl Ranna --- macaroons/service.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/macaroons/service.go b/macaroons/service.go index 91480ead..3b3ea7fe 100644 --- a/macaroons/service.go +++ b/macaroons/service.go @@ -3,6 +3,7 @@ package macaroons import ( "encoding/hex" "fmt" + "os" "path" "google.golang.org/grpc" @@ -40,10 +41,18 @@ type Service struct { // 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) { + // Ensure that the path to the directory exists. + if _, err := os.Stat(dir); os.IsNotExist(err) { + if err := os.MkdirAll(dir, 0700); err != nil { + return nil, err + } + } + // Open the database that we'll use to store the primary macaroon key, // and all generated macaroons+caveats. - macaroonDB, err := bolt.Open(path.Join(dir, DBFilename), 0600, - bolt.DefaultOptions) + macaroonDB, err := bolt.Open( + path.Join(dir, DBFilename), 0600, bolt.DefaultOptions, + ) if err != nil { return nil, err }