Skip to content

Commit

Permalink
Rename public key lists
Browse files Browse the repository at this point in the history
  • Loading branch information
aftermath2 committed Dec 27, 2023
1 parent db70684 commit c3dffb0
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 91 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ A policy would only be enforced if its conditions are satisfied, or if it has no
| -- | -- | -- |
| **conditions** | [Conditions](#conditions) | Set of conditions that must be met to enforce the policies |
| **reject_all** | boolean | Reject all channel requests |
| **whitelist** | []string | List of nodes public keys whose requests will be accepted |
| **blacklist** | []string | List of nodes public keys whose requests will be rejected |
| **allow_list** | []string | List of nodes public keys whose requests will be accepted |
| **block_list** | []string | List of nodes public keys whose requests will be rejected |
| **accept_zero_conf_channels** | boolean | Whether to accept zero confirmation channels |
| **zero_conf_list** | []string | List of nodes public keys whose zero conf requests will be accepted. Requires `accept_zero_conf_channels` to be `true` |
| **reject_private_channels** | boolean | Whether private channels should be rejected |
Expand Down Expand Up @@ -112,8 +112,8 @@ They are defined in the configuration exactly the same way policies are, only a
| Name | Type | Description |
| -- | -- | -- |
| **whitelist** | []string | List of nodes public keys to which policies should be applied |
| **blacklist** | []string | List of nodes public keys to which policies should not be applied |
| **is** | []string | List of nodes public keys to which policies should be applied |
| **is_not** | []string | List of nodes public keys to which policies should not be applied |
| **is_private** | boolean | Match private channels |
| **wants_zero_conf** | boolean | Match zero confirmation channels |
| **request** | [Request](#request) | Parameters related to the channel opening request |
Expand Down
7 changes: 7 additions & 0 deletions examples/conditional.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
policies:
- # Enforce policies depending on the node public key
conditions:
is:
- public_key_1
- public_key_2
request:
max_value_in_flight: 5_000_000
- # Enforce policies on nodes with a capacity of 1 BTC or less
conditions:
node:
Expand Down
2 changes: 1 addition & 1 deletion examples/reject.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ policies:

policies:
-
blacklist:
block_list:
- public_key_1
- public_key_2
- public_key_3
20 changes: 10 additions & 10 deletions policy/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
type Conditions struct {
IsPrivate *bool `yaml:"is_private,omitempty"`
WantsZeroConf *bool `yaml:"wants_zero_conf,omitempty"`
Whitelist *[]string `yaml:"whitelist,omitempty"`
Blacklist *[]string `yaml:"blacklist,omitempty"`
Is *[]string `yaml:"is,omitempty"`
IsNot *[]string `yaml:"is_not,omitempty"`
Request *Request `yaml:"request,omitempty"`
Node *Node `yaml:"node,omitempty"`
}
Expand All @@ -25,11 +25,11 @@ func (c *Conditions) Match(
return true
}

if c.checkWhitelist(peer.Node.PubKey) {
if c.checkIs(peer.Node.PubKey) {
return true
}

if !c.checkBlacklist(peer.Node.PubKey) {
if !c.checkIsNot(peer.Node.PubKey) {
return false
}

Expand All @@ -52,25 +52,25 @@ func (c *Conditions) Match(
return true
}

func (c *Conditions) checkWhitelist(publicKey string) bool {
if c.Whitelist == nil {
func (c *Conditions) checkIs(publicKey string) bool {
if c.Is == nil {
return false
}

for _, pubKey := range *c.Whitelist {
for _, pubKey := range *c.Is {
if publicKey == pubKey {
return true
}
}
return false
}

func (c *Conditions) checkBlacklist(publicKey string) bool {
if c.Blacklist == nil {
func (c *Conditions) checkIsNot(publicKey string) bool {
if c.IsNot == nil {
return true
}

for _, pubKey := range *c.Blacklist {
for _, pubKey := range *c.IsNot {
if publicKey == pubKey {
return false
}
Expand Down
72 changes: 36 additions & 36 deletions policy/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ func TestMatch(t *testing.T) {
expected: true,
},
{
desc: "Whitelist",
desc: "Is",
conditions: &Conditions{
Whitelist: &[]string{peerPublicKey},
Is: &[]string{peerPublicKey},
},
req: defaultReq,
peer: defaultPeer,
expected: true,
},
{
desc: "Blacklist",
desc: "Is not",
conditions: &Conditions{
Blacklist: &[]string{peerPublicKey},
IsNot: &[]string{peerPublicKey},
},
req: defaultReq,
peer: defaultPeer,
Expand Down Expand Up @@ -124,94 +124,94 @@ func TestMatch(t *testing.T) {
}
}

func TestConditionsCheckWhitelist(t *testing.T) {
func TestConditionsCheckIs(t *testing.T) {
publicKey := "key"

cases := []struct {
list *[]string
desc string
publicKey string
whitelist []string
expected bool
}{
{
desc: "Whitelisted",
desc: "Is",
publicKey: publicKey,
whitelist: []string{publicKey},
list: &[]string{publicKey},
expected: true,
},
{
desc: "Not whitelisted",
desc: "Isn't",
publicKey: "not key",
whitelist: []string{publicKey},
list: &[]string{publicKey},
expected: false,
},
{
desc: "Empty whitelist",
whitelist: []string{},
expected: false,
desc: "Empty list",
list: &[]string{},
expected: false,
},
{
desc: "Nil list",
list: nil,
expected: false,
},
}

for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
conditions := Conditions{
Whitelist: &tc.whitelist,
Is: tc.list,
}

actual := conditions.checkWhitelist(tc.publicKey)
actual := conditions.checkIs(tc.publicKey)
assert.Equal(t, tc.expected, actual)
})
}

t.Run("Nil", func(t *testing.T) {
conditions := Conditions{}
assert.False(t, conditions.checkWhitelist(""))
})
}

func TestConditionsCheckBlacklist(t *testing.T) {
func TestConditionsCheckIsNot(t *testing.T) {
publicKey := "key"

cases := []struct {
list *[]string
desc string
publicKey string
blacklist []string
expected bool
}{
{
desc: "Blacklisted",
desc: "In list",
publicKey: publicKey,
blacklist: []string{publicKey},
list: &[]string{publicKey},
expected: false,
},
{
desc: "Not blacklisted",
desc: "Not in list",
publicKey: "not key",
blacklist: []string{publicKey},
list: &[]string{publicKey},
expected: true,
},
{
desc: "Empty blacklist",
blacklist: []string{},
expected: true,
desc: "Empty list",
list: &[]string{},
expected: true,
},
{
desc: "Nil list",
list: nil,
expected: true,
},
}

for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
conditions := Conditions{
Blacklist: &tc.blacklist,
IsNot: tc.list,
}

actual := conditions.checkBlacklist(tc.publicKey)
actual := conditions.checkIsNot(tc.publicKey)
assert.Equal(t, tc.expected, actual)
})
}

t.Run("Nil", func(t *testing.T) {
conditions := Conditions{}
assert.True(t, conditions.checkBlacklist(""))
})
}

func TestConditionsCheckIsPrivate(t *testing.T) {
Expand Down
24 changes: 12 additions & 12 deletions policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ type Policy struct {
Conditions *Conditions `yaml:"conditions,omitempty"`
Request *Request `yaml:"request,omitempty"`
Node *Node `yaml:"node,omitempty"`
Whitelist *[]string `yaml:"whitelist,omitempty"`
Blacklist *[]string `yaml:"blacklist,omitempty"`
AllowList *[]string `yaml:"allow_list,omitempty"`
BlockList *[]string `yaml:"block_list,omitempty"`
ZeroConfList *[]string `yaml:"zero_conf_list,omitempty"`
RejectAll *bool `yaml:"reject_all,omitempty"`
RejectPrivateChannels *bool `yaml:"reject_private_channels,omitempty"`
Expand All @@ -38,12 +38,12 @@ func (p *Policy) Evaluate(
return errors.New("No new channels are accepted")
}

if !p.checkWhitelist(peer.Node.PubKey) {
return errors.New("Node is not whitelisted")
if !p.checkAllowList(peer.Node.PubKey) {
return errors.New("Node is not allowed")
}

if !p.checkBlacklist(peer.Node.PubKey) {
return errors.New("Node is blacklisted")
if !p.checkBlockList(peer.Node.PubKey) {
return errors.New("Node is blocked")
}

if !p.checkPrivate(req.ChannelFlags != uint32(lnwire.FFAnnounceChannel)) {
Expand All @@ -68,25 +68,25 @@ func (p *Policy) checkRejectAll() bool {
return !*p.RejectAll
}

func (p *Policy) checkWhitelist(publicKey string) bool {
if p.Whitelist == nil {
func (p *Policy) checkAllowList(publicKey string) bool {
if p.AllowList == nil {
return true
}

for _, pubKey := range *p.Whitelist {
for _, pubKey := range *p.AllowList {
if publicKey == pubKey {
return true
}
}
return false
}

func (p *Policy) checkBlacklist(publicKey string) bool {
if p.Blacklist == nil {
func (p *Policy) checkBlockList(publicKey string) bool {
if p.BlockList == nil {
return true
}

for _, pubKey := range *p.Blacklist {
for _, pubKey := range *p.BlockList {
if publicKey == pubKey {
return false
}
Expand Down
Loading

0 comments on commit c3dffb0

Please sign in to comment.