2017-09-02 02:30:57 +03:00
|
|
|
package macaroons
|
|
|
|
|
|
|
|
import (
|
2019-09-29 02:10:57 +03:00
|
|
|
"context"
|
2017-09-02 02:30:57 +03:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2018-01-16 19:18:41 +03:00
|
|
|
"google.golang.org/grpc/peer"
|
|
|
|
|
|
|
|
"gopkg.in/macaroon-bakery.v2/bakery/checkers"
|
|
|
|
macaroon "gopkg.in/macaroon.v2"
|
2017-09-02 02:30:57 +03:00
|
|
|
)
|
|
|
|
|
2018-01-16 19:18:41 +03:00
|
|
|
// Constraint type adds a layer of indirection over macaroon caveats.
|
2017-09-13 23:44:05 +03:00
|
|
|
type Constraint func(*macaroon.Macaroon) error
|
2017-09-02 02:30:57 +03:00
|
|
|
|
2018-01-16 19:18:41 +03:00
|
|
|
// Checker type adds a layer of indirection over macaroon checkers. A Checker
|
|
|
|
// returns the name of the checker and the checker function; these are used to
|
|
|
|
// register the function with the bakery service's compound checker.
|
|
|
|
type Checker func() (string, checkers.Func)
|
|
|
|
|
2017-09-02 02:30:57 +03:00
|
|
|
// AddConstraints returns new derived macaroon by applying every passed
|
|
|
|
// constraint and tightening its restrictions.
|
2017-09-13 23:44:05 +03:00
|
|
|
func AddConstraints(mac *macaroon.Macaroon, cs ...Constraint) (*macaroon.Macaroon, error) {
|
2017-09-02 02:30:57 +03:00
|
|
|
newMac := mac.Clone()
|
|
|
|
for _, constraint := range cs {
|
2017-09-13 23:44:05 +03:00
|
|
|
if err := constraint(newMac); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-02 02:30:57 +03:00
|
|
|
}
|
2017-09-13 23:44:05 +03:00
|
|
|
return newMac, nil
|
2017-09-02 02:30:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Each *Constraint function is a functional option, which takes a pointer
|
|
|
|
// to the macaroon and adds another restriction to it. For each *Constraint,
|
2018-01-16 19:18:41 +03:00
|
|
|
// the corresponding *Checker is provided if not provided by default.
|
2017-09-02 02:30:57 +03:00
|
|
|
|
|
|
|
// TimeoutConstraint restricts the lifetime of the macaroon
|
|
|
|
// to the amount of seconds given.
|
2017-09-13 23:44:05 +03:00
|
|
|
func TimeoutConstraint(seconds int64) func(*macaroon.Macaroon) error {
|
|
|
|
return func(mac *macaroon.Macaroon) error {
|
2017-09-02 02:30:57 +03:00
|
|
|
macaroonTimeout := time.Duration(seconds)
|
|
|
|
requestTimeout := time.Now().Add(time.Second * macaroonTimeout)
|
|
|
|
caveat := checkers.TimeBeforeCaveat(requestTimeout)
|
2018-01-16 19:18:41 +03:00
|
|
|
return mac.AddFirstPartyCaveat([]byte(caveat.Condition))
|
2017-09-02 02:30:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IPLockConstraint locks macaroon to a specific IP address.
|
|
|
|
// If address is an empty string, this constraint does nothing to
|
|
|
|
// accommodate default value's desired behavior.
|
2017-09-13 23:44:05 +03:00
|
|
|
func IPLockConstraint(ipAddr string) func(*macaroon.Macaroon) error {
|
|
|
|
return func(mac *macaroon.Macaroon) error {
|
2017-09-02 02:30:57 +03:00
|
|
|
if ipAddr != "" {
|
|
|
|
macaroonIPAddr := net.ParseIP(ipAddr)
|
2017-09-13 23:44:05 +03:00
|
|
|
if macaroonIPAddr == nil {
|
|
|
|
return fmt.Errorf("incorrect macaroon IP-lock address")
|
|
|
|
}
|
2018-01-16 19:18:41 +03:00
|
|
|
caveat := checkers.Condition("ipaddr",
|
|
|
|
macaroonIPAddr.String())
|
|
|
|
return mac.AddFirstPartyCaveat([]byte(caveat))
|
2017-09-02 02:30:57 +03:00
|
|
|
}
|
2017-09-13 23:44:05 +03:00
|
|
|
return nil
|
2017-09-02 02:30:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IPLockChecker accepts client IP from the validation context and compares it
|
2018-01-16 19:18:41 +03:00
|
|
|
// with IP locked in the macaroon. It is of the `Checker` type.
|
|
|
|
func IPLockChecker() (string, checkers.Func) {
|
|
|
|
return "ipaddr", func(ctx context.Context, cond, arg string) error {
|
|
|
|
// Get peer info and extract IP address from it for macaroon
|
|
|
|
// check.
|
|
|
|
pr, ok := peer.FromContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("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")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !net.ParseIP(arg).Equal(net.ParseIP(peerAddr)) {
|
|
|
|
msg := "macaroon locked to different IP address"
|
|
|
|
return fmt.Errorf(msg)
|
|
|
|
}
|
|
|
|
return nil
|
2017-09-02 02:30:57 +03:00
|
|
|
}
|
|
|
|
}
|