2017-08-18 04:50:15 +03:00
|
|
|
package macaroons
|
|
|
|
|
|
|
|
import (
|
2019-09-29 02:10:57 +03:00
|
|
|
"context"
|
2018-02-01 03:04:56 +03:00
|
|
|
"encoding/hex"
|
2018-01-16 19:18:41 +03:00
|
|
|
"fmt"
|
2018-08-22 22:25:03 +03:00
|
|
|
"os"
|
2017-08-18 04:50:15 +03:00
|
|
|
"path"
|
|
|
|
|
2020-01-10 05:45:26 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb/kvdb"
|
2018-01-16 19:18:41 +03:00
|
|
|
"google.golang.org/grpc"
|
2018-02-01 03:04:56 +03:00
|
|
|
"google.golang.org/grpc/metadata"
|
2018-01-16 19:18:41 +03:00
|
|
|
|
|
|
|
"gopkg.in/macaroon-bakery.v2/bakery"
|
|
|
|
"gopkg.in/macaroon-bakery.v2/bakery/checkers"
|
2018-02-01 03:04:56 +03:00
|
|
|
macaroon "gopkg.in/macaroon.v2"
|
2017-08-18 04:50:15 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-04-20 10:02:49 +03:00
|
|
|
// DBFilename is the filename within the data directory which contains
|
2017-08-18 04:50:15 +03:00
|
|
|
// the macaroon stores.
|
2018-04-20 10:02:49 +03:00
|
|
|
DBFilename = "macaroons.db"
|
2020-07-23 19:26:59 +03:00
|
|
|
|
|
|
|
// ErrMissingRootKeyID specifies the root key ID is missing.
|
|
|
|
ErrMissingRootKeyID = fmt.Errorf("missing root key ID")
|
|
|
|
|
|
|
|
// ErrDeletionForbidden is used when attempting to delete the
|
|
|
|
// DefaultRootKeyID or the encryptedKeyID.
|
|
|
|
ErrDeletionForbidden = fmt.Errorf("the specified ID cannot be deleted")
|
2020-09-04 10:22:35 +03:00
|
|
|
|
|
|
|
// PermissionEntityCustomURI is a special entity name for a permission
|
|
|
|
// that does not describe an entity:action pair but instead specifies a
|
|
|
|
// specific URI that needs to be granted access to. This can be used for
|
|
|
|
// more fine-grained permissions where a macaroon only grants access to
|
|
|
|
// certain methods instead of a whole list of methods that define the
|
|
|
|
// same entity:action pairs. For example: uri:/lnrpc.Lightning/GetInfo
|
|
|
|
// only gives access to the GetInfo call.
|
|
|
|
PermissionEntityCustomURI = "uri"
|
2017-08-18 04:50:15 +03:00
|
|
|
)
|
|
|
|
|
2020-07-13 17:53:06 +03:00
|
|
|
// MacaroonValidator is an interface type that can check if macaroons are valid.
|
|
|
|
type MacaroonValidator interface {
|
|
|
|
// ValidateMacaroon extracts the macaroon from the context's gRPC
|
|
|
|
// metadata, checks its signature, makes sure all specified permissions
|
|
|
|
// for the called method are contained within and finally ensures all
|
|
|
|
// caveat conditions are met. A non-nil error is returned if any of the
|
|
|
|
// checks fail.
|
|
|
|
ValidateMacaroon(ctx context.Context,
|
|
|
|
requiredPermissions []bakery.Op, fullMethod string) error
|
|
|
|
}
|
|
|
|
|
2018-02-01 03:04:56 +03:00
|
|
|
// Service encapsulates bakery.Bakery and adds a Close() method that zeroes the
|
|
|
|
// root key service encryption keys, as well as utility methods to validate a
|
|
|
|
// macaroon against the bakery and gRPC middleware for macaroon-based auth.
|
|
|
|
type Service struct {
|
|
|
|
bakery.Bakery
|
|
|
|
|
|
|
|
rks *RootKeyStorage
|
2020-07-13 17:53:06 +03:00
|
|
|
|
|
|
|
// externalValidators is a map between an absolute gRPC URIs and the
|
|
|
|
// corresponding external macaroon validator to be used for that URI.
|
|
|
|
// If no external validator for an URI is specified, the service will
|
|
|
|
// use the internal validator.
|
|
|
|
externalValidators map[string]MacaroonValidator
|
2020-10-06 18:23:31 +03:00
|
|
|
|
|
|
|
// StatelessInit denotes if the service was initialized in the stateless
|
|
|
|
// mode where no macaroon files should be created on disk.
|
|
|
|
StatelessInit bool
|
2018-02-01 03:04:56 +03:00
|
|
|
}
|
|
|
|
|
2017-08-18 04:50:15 +03:00
|
|
|
// NewService returns a service backed by the macaroon Bolt DB stored in the
|
2018-01-16 19:18:41 +03:00
|
|
|
// passed directory. The `checks` argument can be any of the `Checker` type
|
|
|
|
// functions defined in this package, or a custom checker if desired. This
|
|
|
|
// constructor prevents double-registration of checkers to prevent panics, so
|
|
|
|
// listing the same checker more than once is not harmful. Default checkers,
|
|
|
|
// such as those for `allow`, `time-before`, `declared`, and `error` caveats
|
|
|
|
// are registered automatically and don't need to be added.
|
2020-10-06 18:23:31 +03:00
|
|
|
func NewService(dir, location string, statelessInit bool,
|
|
|
|
checks ...Checker) (*Service, error) {
|
|
|
|
|
2018-08-22 22:25:03 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 09:18:19 +03:00
|
|
|
// Open the database that we'll use to store the primary macaroon key,
|
|
|
|
// and all generated macaroons+caveats.
|
2020-01-10 05:45:26 +03:00
|
|
|
macaroonDB, err := kvdb.Create(
|
|
|
|
kvdb.BoltBackendName, path.Join(dir, DBFilename), true,
|
2018-08-22 22:25:03 +03:00
|
|
|
)
|
2017-08-18 04:50:15 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-22 09:18:19 +03:00
|
|
|
|
2017-08-18 04:50:15 +03:00
|
|
|
rootKeyStore, err := NewRootKeyStorage(macaroonDB)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-22 09:18:19 +03:00
|
|
|
|
2018-01-16 19:18:41 +03:00
|
|
|
macaroonParams := bakery.BakeryParams{
|
2020-07-13 17:33:39 +03:00
|
|
|
Location: location,
|
2017-08-18 04:50:15 +03:00
|
|
|
RootKeyStore: rootKeyStore,
|
|
|
|
// No third-party caveat support for now.
|
|
|
|
// TODO(aakselrod): Add third-party caveat support.
|
|
|
|
Locator: nil,
|
|
|
|
Key: nil,
|
|
|
|
}
|
2018-01-16 19:18:41 +03:00
|
|
|
|
|
|
|
svc := bakery.New(macaroonParams)
|
|
|
|
|
|
|
|
// Register all custom caveat checkers with the bakery's checker.
|
|
|
|
// TODO(aakselrod): Add more checks as required.
|
|
|
|
checker := svc.Checker.FirstPartyCaveatChecker.(*checkers.Checker)
|
|
|
|
for _, check := range checks {
|
|
|
|
cond, fun := check()
|
|
|
|
if !isRegistered(checker, cond) {
|
|
|
|
checker.Register(cond, "std", fun)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-13 17:53:06 +03:00
|
|
|
return &Service{
|
|
|
|
Bakery: *svc,
|
|
|
|
rks: rootKeyStore,
|
|
|
|
externalValidators: make(map[string]MacaroonValidator),
|
2020-10-06 18:23:31 +03:00
|
|
|
StatelessInit: statelessInit,
|
2020-07-13 17:53:06 +03:00
|
|
|
}, nil
|
2018-01-16 19:18:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// isRegistered checks to see if the required checker has already been
|
|
|
|
// registered in order to avoid a panic caused by double registration.
|
|
|
|
func isRegistered(c *checkers.Checker, name string) bool {
|
|
|
|
if c == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, info := range c.Info() {
|
2018-04-27 00:06:43 +03:00
|
|
|
if info.Name == name &&
|
|
|
|
info.Prefix == "" &&
|
|
|
|
info.Namespace == "std" {
|
2018-01-16 19:18:41 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-13 17:53:06 +03:00
|
|
|
// RegisterExternalValidator registers a custom, external macaroon validator for
|
|
|
|
// the specified absolute gRPC URI. That validator is then fully responsible to
|
|
|
|
// make sure any macaroon passed for a request to that URI is valid and
|
|
|
|
// satisfies all conditions.
|
|
|
|
func (svc *Service) RegisterExternalValidator(fullMethod string,
|
|
|
|
validator MacaroonValidator) error {
|
|
|
|
|
|
|
|
if validator == nil {
|
|
|
|
return fmt.Errorf("validator cannot be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := svc.externalValidators[fullMethod]
|
|
|
|
if ok {
|
|
|
|
return fmt.Errorf("external validator for method %s already "+
|
|
|
|
"registered", fullMethod)
|
|
|
|
}
|
|
|
|
|
|
|
|
svc.externalValidators[fullMethod] = validator
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-16 19:18:41 +03:00
|
|
|
// UnaryServerInterceptor is a GRPC interceptor that checks whether the
|
|
|
|
// request is authorized by the included macaroons.
|
2018-02-01 03:04:56 +03:00
|
|
|
func (svc *Service) UnaryServerInterceptor(
|
2018-01-16 19:18:41 +03:00
|
|
|
permissionMap map[string][]bakery.Op) grpc.UnaryServerInterceptor {
|
|
|
|
|
|
|
|
return func(ctx context.Context, req interface{},
|
|
|
|
info *grpc.UnaryServerInfo,
|
|
|
|
handler grpc.UnaryHandler) (interface{}, error) {
|
|
|
|
|
2020-09-04 10:22:35 +03:00
|
|
|
uriPermissions, ok := permissionMap[info.FullMethod]
|
|
|
|
if !ok {
|
2018-01-16 19:18:41 +03:00
|
|
|
return nil, fmt.Errorf("%s: unknown permissions "+
|
|
|
|
"required for method", info.FullMethod)
|
|
|
|
}
|
|
|
|
|
2020-07-13 17:53:06 +03:00
|
|
|
// Find out if there is an external validator registered for
|
|
|
|
// this method. Fall back to the internal one if there isn't.
|
|
|
|
validator, ok := svc.externalValidators[info.FullMethod]
|
|
|
|
if !ok {
|
|
|
|
validator = svc
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we know what validator to use, let it do its work.
|
|
|
|
err := validator.ValidateMacaroon(
|
2020-09-04 10:22:35 +03:00
|
|
|
ctx, uriPermissions, info.FullMethod,
|
|
|
|
)
|
2018-01-16 19:18:41 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return handler(ctx, req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StreamServerInterceptor is a GRPC interceptor that checks whether the
|
|
|
|
// request is authorized by the included macaroons.
|
2018-02-01 03:04:56 +03:00
|
|
|
func (svc *Service) StreamServerInterceptor(
|
2018-01-16 19:18:41 +03:00
|
|
|
permissionMap map[string][]bakery.Op) grpc.StreamServerInterceptor {
|
|
|
|
|
|
|
|
return func(srv interface{}, ss grpc.ServerStream,
|
|
|
|
info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
|
|
|
|
|
2020-09-04 10:22:35 +03:00
|
|
|
uriPermissions, ok := permissionMap[info.FullMethod]
|
|
|
|
if !ok {
|
2018-01-16 19:18:41 +03:00
|
|
|
return fmt.Errorf("%s: unknown permissions required "+
|
|
|
|
"for method", info.FullMethod)
|
|
|
|
}
|
|
|
|
|
2020-07-13 17:53:06 +03:00
|
|
|
// Find out if there is an external validator registered for
|
|
|
|
// this method. Fall back to the internal one if there isn't.
|
|
|
|
validator, ok := svc.externalValidators[info.FullMethod]
|
|
|
|
if !ok {
|
|
|
|
validator = svc
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we know what validator to use, let it do its work.
|
|
|
|
err := validator.ValidateMacaroon(
|
2020-09-04 10:22:35 +03:00
|
|
|
ss.Context(), uriPermissions, info.FullMethod,
|
2018-10-23 04:05:37 +03:00
|
|
|
)
|
2018-01-16 19:18:41 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return handler(srv, ss)
|
|
|
|
}
|
2017-08-18 04:50:15 +03:00
|
|
|
}
|
2018-02-01 03:04:56 +03:00
|
|
|
|
|
|
|
// ValidateMacaroon validates the capabilities of a given request given a
|
|
|
|
// bakery service, context, and uri. Within the passed context.Context, we
|
|
|
|
// expect a macaroon to be encoded as request metadata using the key
|
|
|
|
// "macaroon".
|
|
|
|
func (svc *Service) ValidateMacaroon(ctx context.Context,
|
2020-09-04 10:22:35 +03:00
|
|
|
requiredPermissions []bakery.Op, fullMethod string) error {
|
2018-02-01 03:04:56 +03:00
|
|
|
|
|
|
|
// Get macaroon bytes from context and unmarshal into macaroon.
|
|
|
|
md, ok := metadata.FromIncomingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unable to get metadata from context")
|
|
|
|
}
|
|
|
|
if len(md["macaroon"]) != 1 {
|
|
|
|
return fmt.Errorf("expected 1 macaroon, got %d",
|
|
|
|
len(md["macaroon"]))
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the macaroon obtained, we'll now decode the hex-string
|
|
|
|
// encoding, then unmarshal it from binary into its concrete struct
|
|
|
|
// representation.
|
|
|
|
macBytes, err := hex.DecodeString(md["macaroon"][0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mac := &macaroon.Macaroon{}
|
|
|
|
err = mac.UnmarshalBinary(macBytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:23:30 +03:00
|
|
|
// Check the method being called against the permitted operation, the
|
|
|
|
// expiration time and IP address and return the result.
|
2018-02-01 03:04:56 +03:00
|
|
|
authChecker := svc.Checker.Auth(macaroon.Slice{mac})
|
|
|
|
_, err = authChecker.Allow(ctx, requiredPermissions...)
|
2020-09-04 10:22:35 +03:00
|
|
|
|
|
|
|
// If the macaroon contains broad permissions and checks out, we're
|
|
|
|
// done.
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// To also allow the special permission of "uri:<FullMethod>" to be a
|
|
|
|
// valid permission, we need to check it manually in case there is no
|
|
|
|
// broader scope permission defined.
|
|
|
|
_, err = authChecker.Allow(ctx, bakery.Op{
|
|
|
|
Entity: PermissionEntityCustomURI,
|
|
|
|
Action: fullMethod,
|
|
|
|
})
|
2018-02-01 03:04:56 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the database that underlies the RootKeyStore and zeroes the
|
|
|
|
// encryption keys.
|
|
|
|
func (svc *Service) Close() error {
|
|
|
|
return svc.rks.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateUnlock calls the underlying root key store's CreateUnlock and returns
|
|
|
|
// the result.
|
|
|
|
func (svc *Service) CreateUnlock(password *[]byte) error {
|
|
|
|
return svc.rks.CreateUnlock(password)
|
|
|
|
}
|
2020-07-23 19:26:59 +03:00
|
|
|
|
|
|
|
// NewMacaroon wraps around the function Oven.NewMacaroon with the defaults,
|
|
|
|
// - version is always bakery.LatestVersion;
|
|
|
|
// - caveats is always nil.
|
|
|
|
// In addition, it takes a rootKeyID parameter, and puts it into the context.
|
|
|
|
// The context is passed through Oven.NewMacaroon(), in which calls the function
|
|
|
|
// RootKey(), that reads the context for rootKeyID.
|
|
|
|
func (svc *Service) NewMacaroon(
|
|
|
|
ctx context.Context, rootKeyID []byte,
|
|
|
|
ops ...bakery.Op) (*bakery.Macaroon, error) {
|
|
|
|
|
|
|
|
// Check rootKeyID is not called with nil or empty bytes. We want the
|
|
|
|
// caller to be aware the value of root key ID used, so we won't replace
|
|
|
|
// it with the DefaultRootKeyID if not specified.
|
|
|
|
if len(rootKeyID) == 0 {
|
|
|
|
return nil, ErrMissingRootKeyID
|
|
|
|
}
|
|
|
|
|
|
|
|
// // Pass the root key ID to context.
|
|
|
|
ctx = ContextWithRootKeyID(ctx, rootKeyID)
|
|
|
|
|
|
|
|
return svc.Oven.NewMacaroon(ctx, bakery.LatestVersion, nil, ops...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListMacaroonIDs returns all the root key ID values except the value of
|
|
|
|
// encryptedKeyID.
|
|
|
|
func (svc *Service) ListMacaroonIDs(ctxt context.Context) ([][]byte, error) {
|
|
|
|
return svc.rks.ListMacaroonIDs(ctxt)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteMacaroonID removes one specific root key ID. If the root key ID is
|
|
|
|
// found and deleted, it will be returned.
|
|
|
|
func (svc *Service) DeleteMacaroonID(ctxt context.Context,
|
|
|
|
rootKeyID []byte) ([]byte, error) {
|
|
|
|
return svc.rks.DeleteMacaroonID(ctxt, rootKeyID)
|
|
|
|
}
|
2020-10-06 18:23:30 +03:00
|
|
|
|
|
|
|
// GenerateNewRootKey calls the underlying root key store's GenerateNewRootKey
|
|
|
|
// and returns the result.
|
|
|
|
func (svc *Service) GenerateNewRootKey() error {
|
|
|
|
return svc.rks.GenerateNewRootKey()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChangePassword calls the underlying root key store's ChangePassword and
|
|
|
|
// returns the result.
|
|
|
|
func (svc *Service) ChangePassword(oldPw, newPw []byte) error {
|
|
|
|
return svc.rks.ChangePassword(oldPw, newPw)
|
|
|
|
}
|