Skip to content

Commit 29b1126

Browse files
committed
multi: implement BakeSuperMacaroon method
1 parent 7f38895 commit 29b1126

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

perms/permissions.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ var (
6868
Entity: "proxy",
6969
Action: "read",
7070
}},
71+
"/litrpc.Proxy/BakeSuperMacaroon": {{
72+
Entity: "supermacaroon",
73+
Action: "write",
74+
}},
7175
}
7276

7377
// whiteListedLNDMethods is a map of all lnd RPC methods that don't

rpc_proxy.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ type rpcProxy struct {
157157
permsMgr *perms.Manager
158158
subServerMgr *subservers.Manager
159159

160+
bakeSuperMac bakeSuperMac
161+
160162
macValidator macaroons.MacaroonValidator
161163
superMacValidator session.SuperMacaroonValidator
162164

@@ -168,9 +170,15 @@ type rpcProxy struct {
168170
grpcWebProxy *grpcweb.WrappedGrpcServer
169171
}
170172

173+
// bakeSuperMac can be used to bake a new super macaroon.
174+
type bakeSuperMac func(ctx context.Context, rootKeyID uint32) (string, error)
175+
171176
// Start creates initial connection to lnd.
172-
func (p *rpcProxy) Start(lndConn *grpc.ClientConn) error {
177+
func (p *rpcProxy) Start(lndConn *grpc.ClientConn,
178+
bakeSuperMac bakeSuperMac) error {
179+
173180
p.lndConn = lndConn
181+
p.bakeSuperMac = bakeSuperMac
174182

175183
atomic.CompareAndSwapInt32(&p.started, 0, 1)
176184

@@ -215,6 +223,28 @@ func (p *rpcProxy) GetInfo(_ context.Context, _ *litrpc.GetInfoRequest) (
215223
}, nil
216224
}
217225

226+
// BakeSuperMacaroon bakes a new macaroon that includes permissions for
227+
// all the active daemons that LiT is connected to.
228+
//
229+
// NOTE: this is part of the litrpc.ProxyServiceServer interface.
230+
func (p *rpcProxy) BakeSuperMacaroon(ctx context.Context,
231+
req *litrpc.BakeSuperMacaroonRequest) (
232+
*litrpc.BakeSuperMacaroonResponse, error) {
233+
234+
if !p.hasStarted() {
235+
return nil, ErrWaitingToStart
236+
}
237+
238+
superMac, err := p.bakeSuperMac(ctx, req.RootKeyIdSuffix)
239+
if err != nil {
240+
return nil, err
241+
}
242+
243+
return &litrpc.BakeSuperMacaroonResponse{
244+
Macaroon: superMac,
245+
}, nil
246+
}
247+
218248
// isHandling checks if the specified request is something to be handled by lnd
219249
// or any of the attached sub daemons. If true is returned, the call was handled
220250
// by the RPC proxy and the caller MUST NOT handle it again. If false is

terminal.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"crypto/tls"
66
"embed"
7+
"encoding/binary"
78
"encoding/hex"
89
"errors"
910
"fmt"
@@ -496,9 +497,25 @@ func (g *LightningTerminal) start() error {
496497
err)
497498
}
498499

500+
// bakeSuperMac is a closure that can be used to bake a new super
501+
// macaroon that contains all active permissions.
502+
bakeSuperMac := func(ctx context.Context, rootKeyIDSuffix uint32) (
503+
string, error) {
504+
505+
var suffixBytes [4]byte
506+
binary.BigEndian.PutUint32(suffixBytes[:], rootKeyIDSuffix)
507+
508+
rootKeyID := session.NewSuperMacaroonRootKeyID(suffixBytes)
509+
510+
return BakeSuperMacaroon(
511+
ctx, g.basicClient, rootKeyID,
512+
g.permsMgr.ActivePermissions(false), nil,
513+
)
514+
}
515+
499516
// Now start the RPC proxy that will handle all incoming gRPC, grpc-web
500517
// and REST requests.
501-
if err := g.rpcProxy.Start(g.lndConn); err != nil {
518+
if err := g.rpcProxy.Start(g.lndConn, bakeSuperMac); err != nil {
502519
return fmt.Errorf("error starting lnd gRPC proxy server: %v",
503520
err)
504521
}

0 commit comments

Comments
 (0)