Skip to content

Commit e9cfa4b

Browse files
committed
server+rules: add known map to RealToPseudo
We want to add channel and peer restrictions for each feature individually. Without this change, if we would have the same peer or channel id in different feature restrictions, we would create duplicate mappings from a key to multiple values.
1 parent 9145d3c commit e9cfa4b

File tree

7 files changed

+36
-11
lines changed

7 files changed

+36
-11
lines changed

rules/chan_policy_bounds.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,8 @@ func (f *ChanPolicyBounds) PseudoToReal(_ firewalldb.PrivacyMapDB) (Values,
329329
// that should be persisted. This is a no-op for the ChanPolicyBounds rule.
330330
//
331331
// NOTE: this is part of the Values interface.
332-
func (f *ChanPolicyBounds) RealToPseudo() (Values, map[string]string, error) {
332+
func (f *ChanPolicyBounds) RealToPseudo(map[string]string) (Values,
333+
map[string]string, error) {
334+
333335
return f, nil, nil
334336
}

rules/channel_restrictions.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,17 @@ func (c *ChannelRestrict) PseudoToReal(db firewalldb.PrivacyMapDB) (Values,
363363
// RealToPseudo converts all the channel IDs into pseudo IDs.
364364
//
365365
// NOTE: this is part of the Values interface.
366-
func (c *ChannelRestrict) RealToPseudo() (Values, map[string]string, error) {
366+
func (c *ChannelRestrict) RealToPseudo(knownMappings map[string]string) (Values,
367+
map[string]string, error) {
368+
367369
pseudoIDs := make([]uint64, len(c.DenyList))
368-
privMapPairs := make(map[string]string)
370+
371+
// We unify with the already known mappings to avoid duplicates.
372+
privMapPairs := make(map[string]string, len(knownMappings))
373+
for k, v := range knownMappings {
374+
privMapPairs[k] = v
375+
}
376+
369377
for i, c := range c.DenyList {
370378
// TODO(elle): check that this channel actually exists
371379

rules/history_limit.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ func (h *HistoryLimit) PseudoToReal(_ firewalldb.PrivacyMapDB) (Values,
266266
// that should be persisted. This is a no-op for the HistoryLimit rule.
267267
//
268268
// NOTE: this is part of the Values interface.
269-
func (h *HistoryLimit) RealToPseudo() (Values, map[string]string, error) {
269+
func (h *HistoryLimit) RealToPseudo(_ map[string]string) (Values,
270+
map[string]string, error) {
271+
270272
return h, nil, nil
271273
}

rules/interfaces.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ type Values interface {
5959
ToProto() *litrpc.RuleValue
6060

6161
// 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)
62+
// keys, channel IDs, channel points etc. A map with already known
63+
// mappings can be passed. It returns a map of real to pseudo strings
64+
// that should be persisted.
65+
RealToPseudo(map[string]string) (Values, map[string]string, error)
6566

6667
// PseudoToReal attempts to convert any appropriate pseudo fields in
6768
// the rule Values to their corresponding real values. It uses the

rules/peer_restrictions.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,17 @@ func (c *PeerRestrict) PseudoToReal(db firewalldb.PrivacyMapDB) (Values,
370370
// RealToPseudo converts all the real peer IDs into pseudo IDs.
371371
//
372372
// NOTE: this is part of the Values interface.
373-
func (c *PeerRestrict) RealToPseudo() (Values, map[string]string, error) {
373+
func (c *PeerRestrict) RealToPseudo(knownMappings map[string]string) (Values,
374+
map[string]string, error) {
375+
374376
pseudoIDs := make([]string, len(c.DenyList))
375-
privMapPairs := make(map[string]string)
377+
378+
// We unify with the already known mappings to avoid duplicates.
379+
privMapPairs := make(map[string]string, len(knownMappings))
380+
for k, v := range knownMappings {
381+
privMapPairs[k] = v
382+
}
383+
376384
for i, id := range c.DenyList {
377385
// TODO(elle): check that this peer is actually one of our
378386
// channel peers.

rules/rate_limit.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ func (r *RateLimit) PseudoToReal(_ firewalldb.PrivacyMapDB) (Values,
277277
// that should be persisted. This is a no-op for the RateLimit rule.
278278
//
279279
// NOTE: this is part of the Values interface.
280-
func (r *RateLimit) RealToPseudo() (Values, map[string]string, error) {
280+
func (r *RateLimit) RealToPseudo(_ map[string]string) (Values,
281+
map[string]string, error) {
282+
281283
return r, nil, nil
282284
}

session_rpcserver.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,9 @@ func (s *sessionRpcServer) AddAutopilotSession(ctx context.Context,
850850

851851
if privacy {
852852
var privMapPairs map[string]string
853-
v, privMapPairs, err = v.RealToPseudo()
853+
v, privMapPairs, err = v.RealToPseudo(
854+
privacyMapPairs,
855+
)
854856
if err != nil {
855857
return nil, err
856858
}

0 commit comments

Comments
 (0)