Skip to content

Commit b5d5682

Browse files
committed
rules: add Rule interface
1 parent 95fc0c2 commit b5d5682

File tree

4 files changed

+207
-4
lines changed

4 files changed

+207
-4
lines changed

log.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"github.com/lightninglabs/lightning-node-connect/mailbox"
77
"github.com/lightninglabs/lightning-terminal/accounts"
88
"github.com/lightninglabs/lightning-terminal/firewall"
9-
"github.com/lightninglabs/lightning-terminal/firewalldb"
109
mid "github.com/lightninglabs/lightning-terminal/rpcmiddleware"
10+
"github.com/lightninglabs/lightning-terminal/rules"
1111
"github.com/lightninglabs/lightning-terminal/session"
1212
"github.com/lightninglabs/loop/loopd"
1313
"github.com/lightninglabs/pool"
@@ -70,9 +70,7 @@ func SetupLoggers(root *build.RotatingLogWriter, intercept signal.Interceptor) {
7070
lnd.AddSubLogger(
7171
root, firewall.Subsystem, intercept, firewall.UseLogger,
7272
)
73-
lnd.AddSubLogger(
74-
root, firewalldb.Subsystem, intercept, firewalldb.UseLogger,
75-
)
73+
lnd.AddSubLogger(root, rules.Subsystem, intercept, rules.UseLogger)
7674

7775
// Add daemon loggers to lnd's root logger.
7876
faraday.SetupLoggers(root, intercept)

rules/config.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package rules
2+
3+
import (
4+
"github.com/lightninglabs/lightning-terminal/firewalldb"
5+
"github.com/lightninglabs/lndclient"
6+
"gopkg.in/macaroon-bakery.v2/bakery"
7+
)
8+
9+
// Config encompasses all the possible configuration items that could be
10+
// required by the various rules.
11+
type Config interface {
12+
// GetStores can be used to get access to methods that can be used to
13+
// perform atomic transactions on permanent and temporary local and
14+
// global kv stores.
15+
GetStores() firewalldb.KVStores
16+
17+
// GetActionsDB can be used by rules to list any past actions that were
18+
// made for the specific session or feature.
19+
GetActionsDB() firewalldb.ActionsDB
20+
21+
// GetMethodPerms returns a map that contains URIs and the permissions
22+
// required to use them.
23+
GetMethodPerms() func(string) ([]bakery.Op, bool)
24+
25+
// GetNodePubKey returns the node ID of the lnd node.
26+
GetNodePubKey() [33]byte
27+
28+
// GetRouterClient returns an lnd router client.
29+
GetRouterClient() lndclient.RouterClient
30+
31+
// GetReqID is the request ID of the call being evaluated. This can be
32+
// used to link a request with a response.
33+
GetReqID() int64
34+
35+
// GetLndClient returns an lnd client.
36+
GetLndClient() lndclient.LightningClient
37+
}
38+
39+
// ConfigImpl is an implementation of the Config interface.
40+
type ConfigImpl struct {
41+
// GetStores provides access to methods that can be used to perform
42+
// atomic transactions on permanent and temporary local and global
43+
// kv stores.
44+
Stores firewalldb.KVStores
45+
46+
// ActionsDB can be used by rules to list any past actions that were
47+
// made for the specific session or feature.
48+
ActionsDB firewalldb.ActionsDB
49+
50+
// MethodPerms is a function that can be used to fetch the permissions
51+
// required for a URI.
52+
MethodPerms func(string) ([]bakery.Op, bool)
53+
54+
// NodeID is the pub key of the lnd node.
55+
NodeID [33]byte
56+
57+
// RouterClient is an lnd router client.
58+
RouterClient lndclient.RouterClient
59+
60+
// ReqID is the request ID of the call being evaluated. This can be used
61+
// to link a request with a response.
62+
ReqID int64
63+
64+
// LndClient is a connection to the Lit node's LND node.
65+
LndClient lndclient.LightningClient
66+
}
67+
68+
func (c *ConfigImpl) GetStores() firewalldb.KVStores {
69+
return c.Stores
70+
}
71+
72+
// GetActionsDB returns the list of past actions.
73+
func (c *ConfigImpl) GetActionsDB() firewalldb.ActionsDB {
74+
return c.ActionsDB
75+
}
76+
77+
// GetMethodPerms returns a function that can be used to fetch the permissions
78+
// of a URI.
79+
func (c *ConfigImpl) GetMethodPerms() func(string) ([]bakery.Op, bool) {
80+
return c.MethodPerms
81+
}
82+
83+
// GetNodePubKey returns the node ID for the lnd node.
84+
func (c *ConfigImpl) GetNodePubKey() [33]byte {
85+
return c.NodeID
86+
}
87+
88+
// GetRouterClient returns an lnd router client.
89+
func (c *ConfigImpl) GetRouterClient() lndclient.RouterClient {
90+
return c.RouterClient
91+
}
92+
93+
// GetReqID returns the request ID of the request or response being evaluated.
94+
func (c *ConfigImpl) GetReqID() int64 {
95+
return c.ReqID
96+
}
97+
98+
// GetLndClient returns an lnd client.
99+
func (c *ConfigImpl) GetLndClient() lndclient.LightningClient {
100+
return c.LndClient
101+
}
102+
103+
// A compile-time check to ensure that ConfigImpl implements the Config
104+
// interface.
105+
var _ Config = (*ConfigImpl)(nil)

rules/interfaces.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package rules
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
7+
"github.com/lightninglabs/lightning-terminal/firewalldb"
8+
"github.com/lightninglabs/lightning-terminal/litrpc"
9+
"google.golang.org/protobuf/proto"
10+
)
11+
12+
// Manager is the interface that any firewall rule managers will need to
13+
// implement. A rule Manager is used to construct a rule Enforcer or rule
14+
// Values.
15+
type Manager interface {
16+
// NewEnforcer constructs a new rule enforcer using the passed values
17+
// and config.
18+
NewEnforcer(cfg Config, values Values) (Enforcer, error)
19+
20+
// NewValueFromProto converts the given proto value into a Value object.
21+
NewValueFromProto(p *litrpc.RuleValue) (Values, error)
22+
23+
// EmptyValue returns a new Values instance of the type that this
24+
// Manager handles.
25+
EmptyValue() Values
26+
27+
// Stop cleans up the resources held by the manager.
28+
Stop() error
29+
}
30+
31+
// Enforcer is the interface that any firewall rule enforcer must implement.
32+
// An enforcer accepts, rejects, and possible alters an RPC proto message for a
33+
// specific URI.
34+
type Enforcer interface {
35+
// HandleRequest checks the validity of a request and possibly edits it.
36+
HandleRequest(ctx context.Context, uri string,
37+
protoMsg proto.Message) (proto.Message, error)
38+
39+
// HandleResponse handles and possibly alters a response.
40+
HandleResponse(ctx context.Context, uri string,
41+
protoMsg proto.Message) (proto.Message, error)
42+
43+
// HandleErrorResponse handles and possibly alters a response error.
44+
HandleErrorResponse(ctx context.Context, uri string, err error) (error,
45+
error)
46+
}
47+
48+
// Values represents the static values that encompass the settings of the rule.
49+
type Values interface {
50+
// RuleName returns the name of the rule that these values are to be
51+
// used with.
52+
RuleName() string
53+
54+
// VerifySane checks that the rules values are valid given the allowed
55+
// minimum and maximum values.
56+
VerifySane(minVal, maxVal Values) error
57+
58+
// ToProto converts the rule Values to the litrpc counterpart.
59+
ToProto() *litrpc.RuleValue
60+
61+
// RealToPseudo converts the rule Values to a new one that uses pseudo
62+
// keys, channel IDs, channel points etc. It returns a map of real to
63+
// pseudo strings that should be persisted.
64+
RealToPseudo() (Values, map[string]string, error)
65+
66+
// PseudoToReal attempts to convert any appropriate pseudo fields in
67+
// the rule Values to their corresponding real values. It uses the
68+
// passed PrivacyMapDB to find the real values.
69+
PseudoToReal(db firewalldb.PrivacyMapDB) (Values, error)
70+
}
71+
72+
// Marshal converts the rule Values to a json byte slice.
73+
func Marshal(v Values) ([]byte, error) {
74+
return json.Marshal(v)
75+
}

rules/log.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package rules
2+
3+
import (
4+
"github.com/btcsuite/btclog"
5+
"github.com/lightningnetwork/lnd/build"
6+
)
7+
8+
const Subsystem = "RULE"
9+
10+
// log is a logger that is initialized with no output filters. This
11+
// means the package will not perform any logging by default until the caller
12+
// requests it.
13+
var log btclog.Logger // nolint:unused
14+
15+
// The default amount of logging is none.
16+
func init() {
17+
UseLogger(build.NewSubLogger(Subsystem, nil))
18+
}
19+
20+
// UseLogger uses a specified Logger to output package logging info.
21+
// This should be used in preference to SetLogWriter if the caller is also
22+
// using btclog.
23+
func UseLogger(logger btclog.Logger) {
24+
log = logger
25+
}

0 commit comments

Comments
 (0)