Skip to content

Commit e806542

Browse files
committed
firewall: add WithPrivacy to RequestInfo
1 parent 5255e95 commit e806542

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

firewall/caveats.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
"github.com/lightningnetwork/lnd/macaroons"
9+
"gopkg.in/macaroon.v2"
910
)
1011

1112
const (
@@ -20,6 +21,11 @@ const (
2021
// MetaRulesValuePrefix is the static prefix a macaroon caveat value has
2122
// to mark the beginning of the rules list JSON data.
2223
MetaRulesValuePrefix = "rules"
24+
25+
// CondPrivacy is the name of the custom caveat that will
26+
// instruct lnd to send all requests with this caveat to this
27+
// interceptor.
28+
CondPrivacy = "privacy"
2329
)
2430

2531
var (
@@ -35,6 +41,15 @@ var (
3541
macaroons.CondLndCustom, RuleEnforcerCaveat,
3642
MetaRulesValuePrefix)
3743

44+
// MetaPrivacyCaveatPrefix is the caveat prefix that will be used to
45+
// identify the privacy mapper caveat.
46+
MetaPrivacyCaveatPrefix = fmt.Sprintf("%s %s", macaroons.CondLndCustom,
47+
CondPrivacy)
48+
49+
// MetaPrivacyCaveat is the caveat required to ensure that the
50+
// privacy mapper is activated as an interceptor for a request.
51+
MetaPrivacyCaveat = macaroon.Caveat{Id: []byte(MetaPrivacyCaveatPrefix)}
52+
3853
// ErrNoMetaInfoCaveat is the error that is returned if a caveat doesn't
3954
// have the prefix to be recognized as a meta information caveat.
4055
ErrNoMetaInfoCaveat = fmt.Errorf("not a meta info caveat")
@@ -150,3 +165,9 @@ func ParseRuleCaveat(caveat string) (*InterceptRules, error) {
150165

151166
return &rules, nil
152167
}
168+
169+
// IsPrivacyCaveat returns true if the given caveat string is a privacy mapper
170+
// caveat.
171+
func IsPrivacyCaveat(caveat string) bool {
172+
return strings.Contains(caveat, MetaPrivacyCaveatPrefix)
173+
}

firewall/request_info.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type RequestInfo struct {
3737
Caveats []string
3838
MetaInfo *InterceptMetaInfo
3939
Rules *InterceptRules
40+
WithPrivacy bool
4041
}
4142

4243
// NewInfoFromRequest parses the given RPC middleware interception request and
@@ -99,7 +100,8 @@ func NewInfoFromRequest(req *lnrpc.RPCMiddlewareRequest) (*RequestInfo, error) {
99100
if err == nil {
100101
ri.MetaInfo = metaInfo
101102

102-
// The same caveat can't be a meta info and rule list.
103+
// The same caveat can't be a meta info and a rule list
104+
// or a privacy caveat.
103105
continue
104106
}
105107

@@ -109,6 +111,14 @@ func NewInfoFromRequest(req *lnrpc.RPCMiddlewareRequest) (*RequestInfo, error) {
109111
rules, err := ParseRuleCaveat(ri.Caveats[idx])
110112
if err == nil {
111113
ri.Rules = rules
114+
115+
// The same caveat can't be a rule list and a privacy
116+
// caveat.
117+
continue
118+
}
119+
120+
if IsPrivacyCaveat(ri.Caveats[idx]) {
121+
ri.WithPrivacy = true
112122
}
113123
}
114124

0 commit comments

Comments
 (0)