forked from lightninglabs/pool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
macaroons.go
308 lines (289 loc) · 7.98 KB
/
macaroons.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package pool
import (
"context"
"fmt"
"io/ioutil"
"os"
"github.com/lightninglabs/pool/clientdb"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/macaroons"
"github.com/lightningnetwork/lnd/rpcperms"
"go.etcd.io/bbolt"
"google.golang.org/grpc"
"gopkg.in/macaroon-bakery.v2/bakery"
)
const (
// poolMacaroonLocation is the value we use for the pool macaroons'
// "Location" field when baking them.
poolMacaroonLocation = "pool"
)
var (
// RequiredPermissions is a map of all pool RPC methods and their
// required macaroon permissions to access poold.
RequiredPermissions = map[string][]bakery.Op{
"/poolrpc.Trader/GetInfo": {{
Entity: "account",
Action: "read",
}, {
Entity: "order",
Action: "read",
}, {
Entity: "auction",
Action: "read",
}, {
Entity: "auth",
Action: "read",
}},
"/poolrpc.Trader/StopDaemon": {{
Entity: "account",
Action: "write",
}},
"/poolrpc.Trader/QuoteAccount": {{
Entity: "account",
Action: "read",
}},
"/poolrpc.Trader/InitAccount": {{
Entity: "account",
Action: "write",
}},
"/poolrpc.Trader/ListAccounts": {{
Entity: "account",
Action: "read",
}},
"/poolrpc.Trader/CloseAccount": {{
Entity: "account",
Action: "write",
}},
"/poolrpc.Trader/WithdrawAccount": {{
Entity: "account",
Action: "write",
}},
"/poolrpc.Trader/DepositAccount": {{
Entity: "account",
Action: "write",
}},
"/poolrpc.Trader/RenewAccount": {{
Entity: "account",
Action: "write",
}},
"/poolrpc.Trader/BumpAccountFee": {{
Entity: "account",
Action: "write",
}},
"/poolrpc.Trader/RecoverAccounts": {{
Entity: "account",
Action: "write",
}},
"/poolrpc.Trader/SubmitOrder": {{
Entity: "order",
Action: "write",
}},
"/poolrpc.Trader/ListOrders": {{
Entity: "order",
Action: "read",
}},
"/poolrpc.Trader/CancelOrder": {{
Entity: "order",
Action: "write",
}},
"/poolrpc.Trader/QuoteOrder": {{
Entity: "order",
Action: "read",
}},
"/poolrpc.Trader/AuctionFee": {{
Entity: "auction",
Action: "read",
}},
"/poolrpc.Trader/Leases": {{
Entity: "auction",
Action: "read",
}},
"/poolrpc.Trader/BatchSnapshot": {{
Entity: "auction",
Action: "read",
}},
"/poolrpc.Trader/GetLsatTokens": {{
Entity: "auth",
Action: "read",
}},
"/poolrpc.Trader/LeaseDurations": {{
Entity: "auction",
Action: "read",
}},
"/poolrpc.Trader/NextBatchInfo": {{
Entity: "auction",
Action: "read",
}},
"/poolrpc.Trader/NodeRatings": {{
Entity: "auction",
Action: "read",
}},
"/poolrpc.Trader/BatchSnapshots": {{
Entity: "auction",
Action: "read",
}},
"/poolrpc.Trader/OfferSidecar": {{
Entity: "order",
Action: "write",
}},
"/poolrpc.Trader/RegisterSidecar": {{
Entity: "order",
Action: "write",
}},
"/poolrpc.Trader/ExpectSidecarChannel": {{
Entity: "order",
Action: "write",
}},
"/poolrpc.Trader/DecodeSidecarTicket": {{
Entity: "order",
Action: "read",
}},
"/poolrpc.Trader/ListSidecars": {{
Entity: "order",
Action: "read",
}},
"/poolrpc.Trader/CancelSidecar": {{
Entity: "order",
Action: "write",
}},
}
// allPermissions is the list of all existing permissions that exist
// for poold's RPC. The default macaroon that is created on startup
// contains all these permissions and is therefore equivalent to lnd's
// admin.macaroon but for pool.
allPermissions = []bakery.Op{{
Entity: "account",
Action: "read",
}, {
Entity: "account",
Action: "write",
}, {
Entity: "order",
Action: "read",
}, {
Entity: "order",
Action: "write",
}, {
Entity: "auction",
Action: "read",
}, {
Entity: "auth",
Action: "read",
}}
// macDbDefaultPw is the default encryption password used to encrypt the
// pool macaroon database. The macaroon service requires us to set a
// non-nil password so we set it to an empty string. This will cause the
// keys to be encrypted on disk but won't provide any security at all as
// the password is known to anyone.
//
// TODO(guggero): Allow the password to be specified by the user. Needs
// create/unlock calls in the RPC. Using a password should be optional
// though.
macDbDefaultPw = []byte("")
)
// startMacaroonService starts the macaroon validation service, creates or
// unlocks the macaroon database and creates the default macaroon if it doesn't
// exist yet. If macaroons are disabled in general in the configuration, none of
// these actions are taken.
func (s *Server) startMacaroonService(createDefaultMacaroonFile bool) error {
var err error
s.macaroonDB, err = kvdb.GetBoltBackend(&kvdb.BoltBackendConfig{
DBPath: s.cfg.BaseDir,
DBFileName: "macaroons.db",
DBTimeout: clientdb.DefaultPoolDBTimeout,
})
if err == bbolt.ErrTimeout {
return fmt.Errorf("error while trying to open %s/%s: "+
"timed out after %v when trying to obtain exclusive "+
"lock - make sure no other pool daemon process "+
"(standalone or embedded in lightning-terminal) is "+
"running", s.cfg.BaseDir, "macaroons.db",
clientdb.DefaultPoolDBTimeout)
}
if err != nil {
return fmt.Errorf("unable to load macaroon db: %v", err)
}
// Create the macaroon authentication/authorization service.
s.macaroonService, err = macaroons.NewService(
s.macaroonDB, poolMacaroonLocation, false,
macaroons.IPLockChecker,
)
if err != nil {
return fmt.Errorf("unable to set up macaroon service: %v", err)
}
// Try to unlock the macaroon store with the private password.
err = s.macaroonService.CreateUnlock(&macDbDefaultPw)
if err != nil {
return fmt.Errorf("unable to unlock macaroon DB: %v", err)
}
// There are situations in which we don't want a macaroon to be created
// on disk (for example when running inside LiT stateless integrated
// mode). For any other cases, we create macaroon files for the pool CLI
// in the default directory.
if createDefaultMacaroonFile && !lnrpc.FileExists(s.cfg.MacaroonPath) {
// We don't offer the ability to rotate macaroon root keys yet,
// so just use the default one since the service expects some
// value to be set.
idCtx := macaroons.ContextWithRootKeyID(
context.Background(), macaroons.DefaultRootKeyID,
)
// We only generate one default macaroon that contains all
// existing permissions (equivalent to the admin.macaroon in
// lnd). Custom macaroons can be created through the bakery
// RPC.
poolMac, err := s.macaroonService.Oven.NewMacaroon(
idCtx, bakery.LatestVersion, nil, allPermissions...,
)
if err != nil {
return err
}
poolMacBytes, err := poolMac.M().MarshalBinary()
if err != nil {
return err
}
err = ioutil.WriteFile(s.cfg.MacaroonPath, poolMacBytes, 0644)
if err != nil {
if err := os.Remove(s.cfg.MacaroonPath); err != nil {
log.Errorf("Unable to remove %s: %v",
s.cfg.MacaroonPath, err)
}
return err
}
}
return nil
}
// stopMacaroonService closes the macaroon database.
func (s *Server) stopMacaroonService() error {
var shutdownErr error
if err := s.macaroonService.Close(); err != nil {
log.Errorf("Error closing macaroon service: %v", err)
shutdownErr = err
}
if err := s.macaroonDB.Close(); err != nil {
log.Errorf("Error closing macaroon DB: %v", err)
shutdownErr = err
}
return shutdownErr
}
// macaroonInterceptor creates gRPC server options with the macaroon security
// interceptors.
func (s *Server) macaroonInterceptor() (grpc.UnaryServerInterceptor,
grpc.StreamServerInterceptor, error) {
interceptor := rpcperms.NewInterceptorChain(log, false, nil)
err := interceptor.Start()
if err != nil {
return nil, nil, err
}
interceptor.SetWalletUnlocked()
interceptor.AddMacaroonService(s.macaroonService)
for method, permissions := range RequiredPermissions {
err := interceptor.AddPermission(method, permissions)
if err != nil {
return nil, nil, err
}
}
unaryInterceptor := interceptor.MacaroonUnaryServerInterceptor()
streamInterceptor := interceptor.MacaroonStreamServerInterceptor()
return unaryInterceptor, streamInterceptor, nil
}