Skip to content

Commit 5aa0d26

Browse files
gkrizekguggero
authored andcommitted
lnrpc+macaroon: skip subserver macaroons on stateless_init
This will prevent the subservers from writing macaroons to disk when the stateless_init flag is set to true. It accomplishes this by storing the StatelessInit value in the Macaroon Service.
1 parent 0b9b7de commit 5aa0d26

File tree

5 files changed

+32
-39
lines changed

5 files changed

+32
-39
lines changed

lnrpc/chainrpc/chainnotifier_server.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,6 @@ var (
7272
"still in the process of starting")
7373
)
7474

75-
// fileExists reports whether the named file or directory exists.
76-
func fileExists(name string) bool {
77-
if _, err := os.Stat(name); err != nil {
78-
if os.IsNotExist(err) {
79-
return false
80-
}
81-
}
82-
return true
83-
}
84-
8575
// Server is a sub-server of the main RPC server: the chain notifier RPC. This
8676
// RPC sub-server allows external callers to access the full chain notifier
8777
// capabilities of lnd. This allows callers to create custom protocols, external
@@ -111,18 +101,20 @@ func New(cfg *Config) (*Server, lnrpc.MacaroonPerms, error) {
111101
}
112102

113103
// Now that we know the full path of the chain notifier macaroon, we can
114-
// check to see if we need to create it or not.
104+
// check to see if we need to create it or not. If stateless_init is set
105+
// then we don't write the macaroons.
115106
macFilePath := cfg.ChainNotifierMacPath
116-
if cfg.MacService != nil && !fileExists(macFilePath) {
107+
if cfg.MacService != nil && !cfg.MacService.StatelessInit &&
108+
!lnrpc.FileExists(macFilePath) {
109+
117110
log.Infof("Baking macaroons for ChainNotifier RPC Server at: %v",
118111
macFilePath)
119112

120113
// At this point, we know that the chain notifier macaroon
121114
// doesn't yet, exist, so we need to create it with the help of
122115
// the main macaroon service.
123116
chainNotifierMac, err := cfg.MacService.NewMacaroon(
124-
context.Background(),
125-
macaroons.DefaultRootKeyID,
117+
context.Background(), macaroons.DefaultRootKeyID,
126118
macaroonOps...,
127119
)
128120
if err != nil {
@@ -134,7 +126,7 @@ func New(cfg *Config) (*Server, lnrpc.MacaroonPerms, error) {
134126
}
135127
err = ioutil.WriteFile(macFilePath, chainNotifierMacBytes, 0644)
136128
if err != nil {
137-
os.Remove(macFilePath)
129+
_ = os.Remove(macFilePath)
138130
return nil, nil, err
139131
}
140132
}

lnrpc/invoicesrpc/invoices_server.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,11 @@ func New(cfg *Config) (*Server, lnrpc.MacaroonPerms, error) {
9292
)
9393

9494
// Now that we know the full path of the invoices macaroon, we can
95-
// check to see if we need to create it or not.
96-
if !lnrpc.FileExists(macFilePath) && cfg.MacService != nil {
95+
// check to see if we need to create it or not. If stateless_init is set
96+
// then we don't write the macaroons.
97+
if cfg.MacService != nil && !cfg.MacService.StatelessInit &&
98+
!lnrpc.FileExists(macFilePath) {
99+
97100
log.Infof("Baking macaroons for invoices RPC Server at: %v",
98101
macFilePath)
99102

@@ -113,7 +116,7 @@ func New(cfg *Config) (*Server, lnrpc.MacaroonPerms, error) {
113116
}
114117
err = ioutil.WriteFile(macFilePath, invoicesMacBytes, 0644)
115118
if err != nil {
116-
os.Remove(macFilePath)
119+
_ = os.Remove(macFilePath)
117120
return nil, nil, err
118121
}
119122
}

lnrpc/routerrpc/router_server.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,6 @@ type Server struct {
131131
// gRPC service.
132132
var _ RouterServer = (*Server)(nil)
133133

134-
// fileExists reports whether the named file or directory exists.
135-
func fileExists(name string) bool {
136-
if _, err := os.Stat(name); err != nil {
137-
if os.IsNotExist(err) {
138-
return false
139-
}
140-
}
141-
return true
142-
}
143-
144134
// New creates a new instance of the RouterServer given a configuration struct
145135
// that contains all external dependencies. If the target macaroon exists, and
146136
// we're unable to create it, then an error will be returned. We also return
@@ -156,9 +146,12 @@ func New(cfg *Config) (*Server, lnrpc.MacaroonPerms, error) {
156146
}
157147

158148
// Now that we know the full path of the router macaroon, we can check
159-
// to see if we need to create it or not.
149+
// to see if we need to create it or not. If stateless_init is set
150+
// then we don't write the macaroons.
160151
macFilePath := cfg.RouterMacPath
161-
if !fileExists(macFilePath) && cfg.MacService != nil {
152+
if cfg.MacService != nil && !cfg.MacService.StatelessInit &&
153+
!lnrpc.FileExists(macFilePath) {
154+
162155
log.Infof("Making macaroons for Router RPC Server at: %v",
163156
macFilePath)
164157

@@ -178,7 +171,7 @@ func New(cfg *Config) (*Server, lnrpc.MacaroonPerms, error) {
178171
}
179172
err = ioutil.WriteFile(macFilePath, routerMacBytes, 0644)
180173
if err != nil {
181-
os.Remove(macFilePath)
174+
_ = os.Remove(macFilePath)
182175
return nil, nil, err
183176
}
184177
}

lnrpc/signrpc/signer_server.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,12 @@ func New(cfg *Config) (*Server, lnrpc.MacaroonPerms, error) {
103103
}
104104

105105
// Now that we know the full path of the signer macaroon, we can check
106-
// to see if we need to create it or not.
106+
// to see if we need to create it or not. If stateless_init is set
107+
// then we don't write the macaroons.
107108
macFilePath := cfg.SignerMacPath
108-
if cfg.MacService != nil && !lnrpc.FileExists(macFilePath) {
109+
if cfg.MacService != nil && !cfg.MacService.StatelessInit &&
110+
!lnrpc.FileExists(macFilePath) {
111+
109112
log.Infof("Making macaroons for Signer RPC Server at: %v",
110113
macFilePath)
111114

@@ -125,7 +128,7 @@ func New(cfg *Config) (*Server, lnrpc.MacaroonPerms, error) {
125128
}
126129
err = ioutil.WriteFile(macFilePath, signerMacBytes, 0644)
127130
if err != nil {
128-
os.Remove(macFilePath)
131+
_ = os.Remove(macFilePath)
129132
return nil, nil, err
130133
}
131134
}

lnrpc/walletrpc/walletkit_server.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,20 @@ func New(cfg *Config) (*WalletKit, lnrpc.MacaroonPerms, error) {
170170
}
171171

172172
// Now that we know the full path of the wallet kit macaroon, we can
173-
// check to see if we need to create it or not.
173+
// check to see if we need to create it or not. If stateless_init is set
174+
// then we don't write the macaroons.
174175
macFilePath := cfg.WalletKitMacPath
175-
if !lnrpc.FileExists(macFilePath) && cfg.MacService != nil {
176+
if cfg.MacService != nil && !cfg.MacService.StatelessInit &&
177+
!lnrpc.FileExists(macFilePath) {
178+
176179
log.Infof("Baking macaroons for WalletKit RPC Server at: %v",
177180
macFilePath)
178181

179182
// At this point, we know that the wallet kit macaroon doesn't
180183
// yet, exist, so we need to create it with the help of the
181184
// main macaroon service.
182185
walletKitMac, err := cfg.MacService.NewMacaroon(
183-
context.Background(),
184-
macaroons.DefaultRootKeyID,
186+
context.Background(), macaroons.DefaultRootKeyID,
185187
macaroonOps...,
186188
)
187189
if err != nil {
@@ -193,7 +195,7 @@ func New(cfg *Config) (*WalletKit, lnrpc.MacaroonPerms, error) {
193195
}
194196
err = ioutil.WriteFile(macFilePath, walletKitMacBytes, 0644)
195197
if err != nil {
196-
os.Remove(macFilePath)
198+
_ = os.Remove(macFilePath)
197199
return nil, nil, err
198200
}
199201
}

0 commit comments

Comments
 (0)