macaroons: add ip range checker
This commit is contained in:
2 files changed
+66
-4
No files matched your search
+1
-1
@@ -472,7 +472,7 @@ func (d *DefaultWalletImpl) BuildWalletConfig(ctx context.Context,
|
|||||||
}
|
}
|
||||||
macaroonService, err = macaroons.NewService(
|
macaroonService, err = macaroons.NewService(
|
||||||
rootKeyStore, "lnd", walletInitParams.StatelessInit,
|
rootKeyStore, "lnd", walletInitParams.StatelessInit,
|
||||||
macaroons.IPLockChecker,
|
macaroons.IPLockChecker, macaroons.IPRangeLockChecker,
|
||||||
macaroons.CustomChecker(interceptorChain),
|
macaroons.CustomChecker(interceptorChain),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package macaroons
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -21,6 +22,10 @@ const (
|
|||||||
// in the serialized macaroon. We choose a single space as the delimiter
|
// in the serialized macaroon. We choose a single space as the delimiter
|
||||||
// between the because that is also used by the macaroon bakery library.
|
// between the because that is also used by the macaroon bakery library.
|
||||||
CondLndCustom = "lnd-custom"
|
CondLndCustom = "lnd-custom"
|
||||||
|
|
||||||
|
// CondIPRange is the caveat condition name that is used for tying an IP
|
||||||
|
// range to a macaroon.
|
||||||
|
CondIPRange = "iprange"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CustomCaveatAcceptor is an interface that contains a single method for
|
// CustomCaveatAcceptor is an interface that contains a single method for
|
||||||
@@ -80,9 +85,9 @@ func TimeoutConstraint(seconds int64) func(*macaroon.Macaroon) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// IPLockConstraint locks macaroon to a specific IP address.
|
// IPLockConstraint locks a macaroon to a specific IP address. If ipAddr is an
|
||||||
// If address is an empty string, this constraint does nothing to
|
// empty string, this constraint does nothing to accommodate default value's
|
||||||
// accommodate default value's desired behavior.
|
// desired behavior.
|
||||||
func IPLockConstraint(ipAddr string) func(*macaroon.Macaroon) error {
|
func IPLockConstraint(ipAddr string) func(*macaroon.Macaroon) error {
|
||||||
return func(mac *macaroon.Macaroon) error {
|
return func(mac *macaroon.Macaroon) error {
|
||||||
if ipAddr != "" {
|
if ipAddr != "" {
|
||||||
@@ -93,8 +98,32 @@ func IPLockConstraint(ipAddr string) func(*macaroon.Macaroon) error {
|
|||||||
}
|
}
|
||||||
caveat := checkers.Condition("ipaddr",
|
caveat := checkers.Condition("ipaddr",
|
||||||
macaroonIPAddr.String())
|
macaroonIPAddr.String())
|
||||||
|
|
||||||
return mac.AddFirstPartyCaveat([]byte(caveat))
|
return mac.AddFirstPartyCaveat([]byte(caveat))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// IPRangeLockConstraint locks a macaroon to a specific IP address range. If
|
||||||
|
// ipRange is an empty string, this constraint does nothing to accommodate
|
||||||
|
// default value's desired behavior.
|
||||||
|
func IPRangeLockConstraint(ipRange string) func(*macaroon.Macaroon) error {
|
||||||
|
return func(mac *macaroon.Macaroon) error {
|
||||||
|
if ipRange != "" {
|
||||||
|
_, parsedNet, err := net.ParseCIDR(ipRange)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("incorrect macaroon IP "+
|
||||||
|
"range: %w", err)
|
||||||
|
}
|
||||||
|
caveat := checkers.Condition(
|
||||||
|
CondIPRange, parsedNet.String(),
|
||||||
|
)
|
||||||
|
|
||||||
|
return mac.AddFirstPartyCaveat([]byte(caveat))
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -122,6 +151,39 @@ func IPLockChecker() (string, checkers.Func) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IPRangeLockChecker accepts client IP range from the validation context and
|
||||||
|
// compares it with the IP range locked in the macaroon. It is of the `Checker`
|
||||||
|
// type.
|
||||||
|
func IPRangeLockChecker() (string, checkers.Func) {
|
||||||
|
return CondIPRange, func(ctx context.Context, cond, arg string) error {
|
||||||
|
// Get peer info and extract IP range from it for macaroon
|
||||||
|
// check.
|
||||||
|
pr, ok := peer.FromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
return errors.New("unable to get peer info from " +
|
||||||
|
"context")
|
||||||
|
}
|
||||||
|
peerAddr, _, err := net.SplitHostPort(pr.Addr.String())
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to parse peer address: %w",
|
||||||
|
err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, ipNet, err := net.ParseCIDR(arg)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to parse macaroon IP "+
|
||||||
|
"range: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !ipNet.Contains(net.ParseIP(peerAddr)) {
|
||||||
|
return errors.New("macaroon locked to different " +
|
||||||
|
"IP range")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// CustomConstraint returns a function that adds a custom caveat condition to
|
// CustomConstraint returns a function that adds a custom caveat condition to
|
||||||
// a macaroon.
|
// a macaroon.
|
||||||
func CustomConstraint(name, condition string) func(*macaroon.Macaroon) error {
|
func CustomConstraint(name, condition string) func(*macaroon.Macaroon) error {
|
||||||
|
|||||||
Reference in new issue
Block a user