lnrpc+rpcserver: support list/delete on marcaroon IDs

This commit is contained in:
yyforyongyu committed 2020-08-14 12:33:13 +08:00
1 parent f362f7670b
commit c0e2513350
13 files changed
+1383 -756

No files matched your search

+87 -2
View File
@@ -12,6 +12,7 @@ import (
"net/http"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
@@ -107,6 +108,10 @@ var (
Entity: "signer",
Action: "read",
},
{
Entity: "macaroon",
Action: "read",
},
}
// writePermissions is a slice of all entities that allow write
@@ -148,6 +153,10 @@ var (
Entity: "macaroon",
Action: "generate",
},
{
Entity: "macaroon",
Action: "write",
},
}
// invoicePermissions is a slice of all the entities that allows a user
@@ -429,6 +438,14 @@ func mainRPCServerPermissions() map[string][]bakery.Op {
Entity: "macaroon",
Action: "generate",
}},
"/lnrpc.Lightning/ListMacaroonIDs": {{
Entity: "macaroon",
Action: "read",
}},
"/lnrpc.Lightning/DeleteMacaroonID": {{
Entity: "macaroon",
Action: "write",
}},
"/lnrpc.Lightning/SubscribePeerEvents": {{
Entity: "peers",
Action: "read",
@@ -6399,10 +6416,16 @@ func (r *rpcServer) BakeMacaroon(ctx context.Context,
}
}
// Convert root key id from uint64 to bytes. Because the DefaultRootKeyID is
// a digit 0 expressed in a byte slice of a string "0", we will keep the IDs
// in the same format - all must be numeric, and must be a byte slice of
// string value of the digit, e.g., uint64(123) to string(123).
rootKeyID := []byte(strconv.FormatUint(req.RootKeyId, 10))
// Bake new macaroon with the given permissions and send it binary
// serialized and hex encoded to the client.
newMac, err := r.macService.Oven.NewMacaroon(
ctx, bakery.LatestVersion, nil, requestedPermissions...,
newMac, err := r.macService.NewMacaroon(
ctx, rootKeyID, requestedPermissions...,
)
if err != nil {
return nil, err
@@ -6417,6 +6440,68 @@ func (r *rpcServer) BakeMacaroon(ctx context.Context,
return resp, nil
}
// ListMacaroonIDs returns a list of macaroon root key IDs in use.
func (r *rpcServer) ListMacaroonIDs(ctx context.Context,
req *lnrpc.ListMacaroonIDsRequest) (*lnrpc.ListMacaroonIDsResponse, error) {
rpcsLog.Debugf("[listmacaroonids]")
// If the --no-macaroons flag is used to start lnd, the macaroon service
// is not initialized. Therefore we can't show any IDs.
if r.macService == nil {
return nil, fmt.Errorf("macaroon authentication disabled, " +
"remove --no-macaroons flag to enable")
}
rootKeyIDByteSlice, err := r.macService.ListMacaroonIDs(ctx)
if err != nil {
return nil, err
}
var rootKeyIDs []uint64
for _, value := range rootKeyIDByteSlice {
// Convert bytes into uint64.
id, err := strconv.ParseUint(string(value), 10, 64)
if err != nil {
return nil, err
}
rootKeyIDs = append(rootKeyIDs, id)
}
return &lnrpc.ListMacaroonIDsResponse{RootKeyIds: rootKeyIDs}, nil
}
// DeleteMacaroonID removes a specific macaroon ID.
func (r *rpcServer) DeleteMacaroonID(ctx context.Context,
req *lnrpc.DeleteMacaroonIDRequest) (*lnrpc.DeleteMacaroonIDResponse, error) {
rpcsLog.Debugf("[deletemacaroonid]")
// If the --no-macaroons flag is used to start lnd, the macaroon service
// is not initialized. Therefore we can't show any IDs.
if r.macService == nil {
return nil, fmt.Errorf("macaroon authentication disabled, " +
"remove --no-macaroons flag to enable")
}
// Convert root key id from uint64 to bytes. Because the DefaultRootKeyID is
// a digit 0 expressed in a byte slice of a string "0", we will keep the IDs
// in the same format - all must be digit, and must be a byte slice of
// string value of the digit.
rootKeyID := []byte(strconv.FormatUint(req.RootKeyId, 10))
deletedIDBytes, err := r.macService.DeleteMacaroonID(ctx, rootKeyID)
if err != nil {
return nil, err
}
return &lnrpc.DeleteMacaroonIDResponse{
// If the root key ID doesn't exist, it won't be deleted. We will return
// a response with deleted = false, otherwise true.
Deleted: deletedIDBytes != nil,
}, nil
}
// FundingStateStep is an advanced funding related call that allows the caller
// to either execute some preparatory steps for a funding workflow, or manually
// progress a funding workflow. The primary way a funding flow is identified is