2023-03-13 15:14:18 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "embed"
|
2023-07-18 11:12:50 +02:00
|
|
|
"strconv"
|
2023-03-13 15:14:18 +01:00
|
|
|
"strings"
|
|
|
|
|
2023-12-04 14:53:38 +01:00
|
|
|
"github.com/netbirdio/management-integrations/additions"
|
2023-07-18 11:12:50 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2023-05-29 16:00:18 +02:00
|
|
|
"github.com/netbirdio/netbird/management/proto"
|
2023-03-13 15:14:18 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/activity"
|
2023-11-28 13:45:26 +01:00
|
|
|
nbpeer "github.com/netbirdio/netbird/management/server/peer"
|
2023-03-13 15:14:18 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/status"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PolicyUpdateOperationType operation type
|
|
|
|
type PolicyUpdateOperationType int
|
|
|
|
|
|
|
|
// PolicyTrafficActionType action type for the firewall
|
|
|
|
type PolicyTrafficActionType string
|
|
|
|
|
2023-05-29 16:00:18 +02:00
|
|
|
// PolicyRuleProtocolType type of traffic
|
|
|
|
type PolicyRuleProtocolType string
|
|
|
|
|
|
|
|
// PolicyRuleDirection direction of traffic
|
|
|
|
type PolicyRuleDirection string
|
|
|
|
|
2023-03-13 15:14:18 +01:00
|
|
|
const (
|
|
|
|
// PolicyTrafficActionAccept indicates that the traffic is accepted
|
|
|
|
PolicyTrafficActionAccept = PolicyTrafficActionType("accept")
|
|
|
|
// PolicyTrafficActionDrop indicates that the traffic is dropped
|
|
|
|
PolicyTrafficActionDrop = PolicyTrafficActionType("drop")
|
|
|
|
)
|
|
|
|
|
2023-05-29 16:00:18 +02:00
|
|
|
const (
|
|
|
|
// PolicyRuleProtocolALL type of traffic
|
|
|
|
PolicyRuleProtocolALL = PolicyRuleProtocolType("all")
|
|
|
|
// PolicyRuleProtocolTCP type of traffic
|
|
|
|
PolicyRuleProtocolTCP = PolicyRuleProtocolType("tcp")
|
|
|
|
// PolicyRuleProtocolUDP type of traffic
|
|
|
|
PolicyRuleProtocolUDP = PolicyRuleProtocolType("udp")
|
|
|
|
// PolicyRuleProtocolICMP type of traffic
|
|
|
|
PolicyRuleProtocolICMP = PolicyRuleProtocolType("icmp")
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-11-01 17:11:16 +01:00
|
|
|
// PolicyRuleFlowDirect allows traffic from source to destination
|
2023-05-29 16:00:18 +02:00
|
|
|
PolicyRuleFlowDirect = PolicyRuleDirection("direct")
|
|
|
|
// PolicyRuleFlowBidirect allows traffic to both directions
|
|
|
|
PolicyRuleFlowBidirect = PolicyRuleDirection("bidirect")
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
firewallRuleDirectionIN = 0
|
|
|
|
firewallRuleDirectionOUT = 1
|
|
|
|
)
|
|
|
|
|
2023-03-13 15:14:18 +01:00
|
|
|
// PolicyUpdateOperation operation object with type and values to be applied
|
|
|
|
type PolicyUpdateOperation struct {
|
|
|
|
Type PolicyUpdateOperationType
|
|
|
|
Values []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// PolicyRule is the metadata of the policy
|
|
|
|
type PolicyRule struct {
|
|
|
|
// ID of the policy rule
|
2023-10-12 15:42:36 +02:00
|
|
|
ID string `gorm:"primaryKey"`
|
|
|
|
|
|
|
|
// PolicyID is a reference to Policy that this object belongs
|
|
|
|
PolicyID string `json:"-" gorm:"index"`
|
2023-03-13 15:14:18 +01:00
|
|
|
|
|
|
|
// Name of the rule visible in the UI
|
|
|
|
Name string
|
|
|
|
|
|
|
|
// Description of the rule visible in the UI
|
|
|
|
Description string
|
|
|
|
|
|
|
|
// Enabled status of rule in the system
|
|
|
|
Enabled bool
|
|
|
|
|
|
|
|
// Action policy accept or drops packets
|
|
|
|
Action PolicyTrafficActionType
|
|
|
|
|
|
|
|
// Destinations policy destination groups
|
2023-10-12 15:42:36 +02:00
|
|
|
Destinations []string `gorm:"serializer:json"`
|
2023-03-13 15:14:18 +01:00
|
|
|
|
|
|
|
// Sources policy source groups
|
2023-10-12 15:42:36 +02:00
|
|
|
Sources []string `gorm:"serializer:json"`
|
2023-05-29 16:00:18 +02:00
|
|
|
|
|
|
|
// Bidirectional define if the rule is applicable in both directions, sources, and destinations
|
|
|
|
Bidirectional bool
|
|
|
|
|
|
|
|
// Protocol type of the traffic
|
|
|
|
Protocol PolicyRuleProtocolType
|
|
|
|
|
|
|
|
// Ports or it ranges list
|
2023-10-12 15:42:36 +02:00
|
|
|
Ports []string `gorm:"serializer:json"`
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy returns a copy of a policy rule
|
|
|
|
func (pm *PolicyRule) Copy() *PolicyRule {
|
2023-08-22 17:56:39 +02:00
|
|
|
rule := &PolicyRule{
|
2023-05-29 16:00:18 +02:00
|
|
|
ID: pm.ID,
|
|
|
|
Name: pm.Name,
|
|
|
|
Description: pm.Description,
|
|
|
|
Enabled: pm.Enabled,
|
|
|
|
Action: pm.Action,
|
2023-08-22 17:56:39 +02:00
|
|
|
Destinations: make([]string, len(pm.Destinations)),
|
|
|
|
Sources: make([]string, len(pm.Sources)),
|
2023-05-29 16:00:18 +02:00
|
|
|
Bidirectional: pm.Bidirectional,
|
|
|
|
Protocol: pm.Protocol,
|
2023-08-22 17:56:39 +02:00
|
|
|
Ports: make([]string, len(pm.Ports)),
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
2023-08-22 17:56:39 +02:00
|
|
|
copy(rule.Destinations, pm.Destinations)
|
|
|
|
copy(rule.Sources, pm.Sources)
|
|
|
|
copy(rule.Ports, pm.Ports)
|
|
|
|
return rule
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToRule converts the PolicyRule to a legacy representation of the Rule (for backwards compatibility)
|
|
|
|
func (pm *PolicyRule) ToRule() *Rule {
|
|
|
|
return &Rule{
|
|
|
|
ID: pm.ID,
|
|
|
|
Name: pm.Name,
|
|
|
|
Description: pm.Description,
|
|
|
|
Disabled: !pm.Enabled,
|
|
|
|
Flow: TrafficFlowBidirect,
|
|
|
|
Destination: pm.Destinations,
|
|
|
|
Source: pm.Sources,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Policy of the Rego query
|
|
|
|
type Policy struct {
|
2023-10-12 15:42:36 +02:00
|
|
|
// ID of the policy'
|
|
|
|
ID string `gorm:"primaryKey"`
|
|
|
|
|
|
|
|
// AccountID is a reference to Account that this object belongs
|
|
|
|
AccountID string `json:"-" gorm:"index"`
|
2023-03-13 15:14:18 +01:00
|
|
|
|
|
|
|
// Name of the Policy
|
|
|
|
Name string
|
|
|
|
|
|
|
|
// Description of the policy visible in the UI
|
|
|
|
Description string
|
|
|
|
|
|
|
|
// Enabled status of the policy
|
|
|
|
Enabled bool
|
|
|
|
|
|
|
|
// Rules of the policy
|
2023-10-12 15:42:36 +02:00
|
|
|
Rules []*PolicyRule `gorm:"foreignKey:PolicyID;references:id"`
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy returns a copy of the policy.
|
|
|
|
func (p *Policy) Copy() *Policy {
|
|
|
|
c := &Policy{
|
|
|
|
ID: p.ID,
|
|
|
|
Name: p.Name,
|
|
|
|
Description: p.Description,
|
|
|
|
Enabled: p.Enabled,
|
2023-08-22 17:56:39 +02:00
|
|
|
Rules: make([]*PolicyRule, len(p.Rules)),
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
2023-08-22 17:56:39 +02:00
|
|
|
for i, r := range p.Rules {
|
|
|
|
c.Rules[i] = r.Copy()
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventMeta returns activity event meta related to this policy
|
|
|
|
func (p *Policy) EventMeta() map[string]any {
|
|
|
|
return map[string]any{"name": p.Name}
|
|
|
|
}
|
|
|
|
|
2023-05-29 16:00:18 +02:00
|
|
|
// UpgradeAndFix different version of policies to latest version
|
|
|
|
func (p *Policy) UpgradeAndFix() {
|
2023-03-13 15:14:18 +01:00
|
|
|
for _, r := range p.Rules {
|
2023-05-29 16:00:18 +02:00
|
|
|
// start migrate from version v0.20.3
|
|
|
|
if r.Protocol == "" {
|
|
|
|
r.Protocol = PolicyRuleProtocolALL
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
2023-05-29 16:00:18 +02:00
|
|
|
if r.Protocol == PolicyRuleProtocolALL && !r.Bidirectional {
|
|
|
|
r.Bidirectional = true
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
2023-05-29 16:00:18 +02:00
|
|
|
// -- v0.20.4
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FirewallRule is a rule of the firewall.
|
|
|
|
type FirewallRule struct {
|
|
|
|
// PeerIP of the peer
|
|
|
|
PeerIP string
|
|
|
|
|
|
|
|
// Direction of the traffic
|
2023-05-29 16:00:18 +02:00
|
|
|
Direction int
|
2023-03-13 15:14:18 +01:00
|
|
|
|
|
|
|
// Action of the traffic
|
|
|
|
Action string
|
|
|
|
|
2023-05-29 16:00:18 +02:00
|
|
|
// Protocol of the traffic
|
|
|
|
Protocol string
|
|
|
|
|
2023-03-13 15:14:18 +01:00
|
|
|
// Port of the traffic
|
|
|
|
Port string
|
|
|
|
}
|
|
|
|
|
2023-05-29 16:00:18 +02:00
|
|
|
// getPeerConnectionResources for a given peer
|
|
|
|
//
|
|
|
|
// This function returns the list of peers and firewall rules that are applicable to a given peer.
|
2023-11-28 13:45:26 +01:00
|
|
|
func (a *Account) getPeerConnectionResources(peerID string) ([]*nbpeer.Peer, []*FirewallRule) {
|
2023-05-29 16:00:18 +02:00
|
|
|
generateResources, getAccumulatedResources := a.connResourcesGenerator()
|
|
|
|
for _, policy := range a.Policies {
|
|
|
|
if !policy.Enabled {
|
2023-03-13 15:14:18 +01:00
|
|
|
continue
|
|
|
|
}
|
2023-05-29 16:00:18 +02:00
|
|
|
|
|
|
|
for _, rule := range policy.Rules {
|
|
|
|
if !rule.Enabled {
|
2023-03-13 15:14:18 +01:00
|
|
|
continue
|
|
|
|
}
|
2023-05-29 16:00:18 +02:00
|
|
|
|
|
|
|
sourcePeers, peerInSources := getAllPeersFromGroups(a, rule.Sources, peerID)
|
|
|
|
destinationPeers, peerInDestinations := getAllPeersFromGroups(a, rule.Destinations, peerID)
|
2023-12-04 14:53:38 +01:00
|
|
|
sourcePeers = additions.ValidatePeers(sourcePeers)
|
|
|
|
destinationPeers = additions.ValidatePeers(destinationPeers)
|
2023-05-29 16:00:18 +02:00
|
|
|
|
|
|
|
if rule.Bidirectional {
|
|
|
|
if peerInSources {
|
|
|
|
generateResources(rule, destinationPeers, firewallRuleDirectionIN)
|
|
|
|
}
|
|
|
|
if peerInDestinations {
|
|
|
|
generateResources(rule, sourcePeers, firewallRuleDirectionOUT)
|
|
|
|
}
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
|
|
|
|
2023-05-29 16:00:18 +02:00
|
|
|
if peerInSources {
|
|
|
|
generateResources(rule, destinationPeers, firewallRuleDirectionOUT)
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
2023-05-29 16:00:18 +02:00
|
|
|
|
|
|
|
if peerInDestinations {
|
|
|
|
generateResources(rule, sourcePeers, firewallRuleDirectionIN)
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-29 16:00:18 +02:00
|
|
|
return getAccumulatedResources()
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
|
|
|
|
2023-05-29 16:00:18 +02:00
|
|
|
// connResourcesGenerator returns generator and accumulator function which returns the result of generator calls
|
|
|
|
//
|
|
|
|
// The generator function is used to generate the list of peers and firewall rules that are applicable to a given peer.
|
|
|
|
// It safe to call the generator function multiple times for same peer and different rules no duplicates will be
|
|
|
|
// generated. The accumulator function returns the result of all the generator calls.
|
2023-11-28 13:45:26 +01:00
|
|
|
func (a *Account) connResourcesGenerator() (func(*PolicyRule, []*nbpeer.Peer, int), func() ([]*nbpeer.Peer, []*FirewallRule)) {
|
2023-05-29 16:00:18 +02:00
|
|
|
rulesExists := make(map[string]struct{})
|
|
|
|
peersExists := make(map[string]struct{})
|
|
|
|
rules := make([]*FirewallRule, 0)
|
2023-11-28 13:45:26 +01:00
|
|
|
peers := make([]*nbpeer.Peer, 0)
|
2023-07-18 11:12:50 +02:00
|
|
|
|
|
|
|
all, err := a.GetGroupAll()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to get group all: %v", err)
|
|
|
|
all = &Group{}
|
|
|
|
}
|
|
|
|
|
2023-11-28 13:45:26 +01:00
|
|
|
return func(rule *PolicyRule, groupPeers []*nbpeer.Peer, direction int) {
|
2023-07-18 11:12:50 +02:00
|
|
|
isAll := (len(all.Peers) - 1) == len(groupPeers)
|
2023-05-29 16:00:18 +02:00
|
|
|
for _, peer := range groupPeers {
|
2023-05-31 19:04:38 +02:00
|
|
|
if peer == nil {
|
|
|
|
continue
|
|
|
|
}
|
2023-05-29 16:00:18 +02:00
|
|
|
if _, ok := peersExists[peer.ID]; !ok {
|
|
|
|
peers = append(peers, peer)
|
|
|
|
peersExists[peer.ID] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2023-07-18 11:12:50 +02:00
|
|
|
fr := FirewallRule{
|
2023-05-29 16:00:18 +02:00
|
|
|
PeerIP: peer.IP.String(),
|
|
|
|
Direction: direction,
|
|
|
|
Action: string(rule.Action),
|
|
|
|
Protocol: string(rule.Protocol),
|
|
|
|
}
|
|
|
|
|
2023-07-18 11:12:50 +02:00
|
|
|
if isAll {
|
|
|
|
fr.PeerIP = "0.0.0.0"
|
|
|
|
}
|
|
|
|
|
|
|
|
ruleID := (rule.ID + fr.PeerIP + strconv.Itoa(direction) +
|
|
|
|
fr.Protocol + fr.Action + strings.Join(rule.Ports, ","))
|
2023-05-29 16:00:18 +02:00
|
|
|
if _, ok := rulesExists[ruleID]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rulesExists[ruleID] = struct{}{}
|
|
|
|
|
|
|
|
if len(rule.Ports) == 0 {
|
2023-07-18 11:12:50 +02:00
|
|
|
rules = append(rules, &fr)
|
2023-05-29 16:00:18 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, port := range rule.Ports {
|
2023-07-18 11:12:50 +02:00
|
|
|
pr := fr // clone rule and add set new port
|
|
|
|
pr.Port = port
|
|
|
|
rules = append(rules, &pr)
|
2023-05-29 16:00:18 +02:00
|
|
|
}
|
2023-04-01 12:02:08 +02:00
|
|
|
}
|
2023-11-28 13:45:26 +01:00
|
|
|
}, func() ([]*nbpeer.Peer, []*FirewallRule) {
|
2023-05-29 16:00:18 +02:00
|
|
|
return peers, rules
|
2023-04-01 12:02:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-13 15:14:18 +01:00
|
|
|
// GetPolicy from the store
|
|
|
|
func (am *DefaultAccountManager) GetPolicy(accountID, policyID, userID string) (*Policy, error) {
|
|
|
|
unlock := am.Store.AcquireAccountLock(accountID)
|
|
|
|
defer unlock()
|
|
|
|
|
|
|
|
account, err := am.Store.GetAccount(accountID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := account.FindUser(userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-01 17:24:57 +01:00
|
|
|
if !user.HasAdminPower() {
|
|
|
|
return nil, status.Errorf(status.PermissionDenied, "only users with admin power are allowed to view policies")
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, policy := range account.Policies {
|
|
|
|
if policy.ID == policyID {
|
|
|
|
return policy, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, status.Errorf(status.NotFound, "policy with ID %s not found", policyID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SavePolicy in the store
|
|
|
|
func (am *DefaultAccountManager) SavePolicy(accountID, userID string, policy *Policy) error {
|
|
|
|
unlock := am.Store.AcquireAccountLock(accountID)
|
|
|
|
defer unlock()
|
|
|
|
|
|
|
|
account, err := am.Store.GetAccount(accountID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
exists := am.savePolicy(account, policy)
|
|
|
|
|
|
|
|
account.Network.IncSerial()
|
|
|
|
if err = am.Store.SaveAccount(account); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
action := activity.PolicyAdded
|
|
|
|
if exists {
|
|
|
|
action = activity.PolicyUpdated
|
|
|
|
}
|
2023-11-08 11:35:37 +01:00
|
|
|
am.StoreEvent(userID, policy.ID, accountID, action, policy.EventMeta())
|
2023-03-13 15:14:18 +01:00
|
|
|
|
2023-10-04 15:08:50 +02:00
|
|
|
am.updateAccountPeers(account)
|
|
|
|
|
|
|
|
return nil
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeletePolicy from the store
|
|
|
|
func (am *DefaultAccountManager) DeletePolicy(accountID, policyID, userID string) error {
|
|
|
|
unlock := am.Store.AcquireAccountLock(accountID)
|
|
|
|
defer unlock()
|
|
|
|
|
|
|
|
account, err := am.Store.GetAccount(accountID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
policy, err := am.deletePolicy(account, policyID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
account.Network.IncSerial()
|
|
|
|
if err = am.Store.SaveAccount(account); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-08 11:35:37 +01:00
|
|
|
am.StoreEvent(userID, policy.ID, accountID, activity.PolicyRemoved, policy.EventMeta())
|
2023-03-13 15:14:18 +01:00
|
|
|
|
2023-10-04 15:08:50 +02:00
|
|
|
am.updateAccountPeers(account)
|
|
|
|
|
|
|
|
return nil
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListPolicies from the store
|
|
|
|
func (am *DefaultAccountManager) ListPolicies(accountID, userID string) ([]*Policy, error) {
|
|
|
|
unlock := am.Store.AcquireAccountLock(accountID)
|
|
|
|
defer unlock()
|
|
|
|
|
|
|
|
account, err := am.Store.GetAccount(accountID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := account.FindUser(userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-01 17:24:57 +01:00
|
|
|
if !user.HasAdminPower() {
|
|
|
|
return nil, status.Errorf(status.PermissionDenied, "only users with admin power can view policies")
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
|
|
|
|
2023-11-27 16:40:02 +01:00
|
|
|
return account.Policies, nil
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (am *DefaultAccountManager) deletePolicy(account *Account, policyID string) (*Policy, error) {
|
|
|
|
policyIdx := -1
|
|
|
|
for i, policy := range account.Policies {
|
|
|
|
if policy.ID == policyID {
|
|
|
|
policyIdx = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if policyIdx < 0 {
|
|
|
|
return nil, status.Errorf(status.NotFound, "rule with ID %s doesn't exist", policyID)
|
|
|
|
}
|
|
|
|
|
|
|
|
policy := account.Policies[policyIdx]
|
|
|
|
account.Policies = append(account.Policies[:policyIdx], account.Policies[policyIdx+1:]...)
|
|
|
|
return policy, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *DefaultAccountManager) savePolicy(account *Account, policy *Policy) (exists bool) {
|
|
|
|
for i, p := range account.Policies {
|
|
|
|
if p.ID == policy.ID {
|
|
|
|
account.Policies[i] = policy
|
|
|
|
exists = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
account.Policies = append(account.Policies, policy)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2023-05-29 16:00:18 +02:00
|
|
|
|
|
|
|
func toProtocolFirewallRules(update []*FirewallRule) []*proto.FirewallRule {
|
|
|
|
result := make([]*proto.FirewallRule, len(update))
|
|
|
|
for i := range update {
|
|
|
|
direction := proto.FirewallRule_IN
|
|
|
|
if update[i].Direction == firewallRuleDirectionOUT {
|
|
|
|
direction = proto.FirewallRule_OUT
|
|
|
|
}
|
|
|
|
action := proto.FirewallRule_ACCEPT
|
|
|
|
if update[i].Action == string(PolicyTrafficActionDrop) {
|
|
|
|
action = proto.FirewallRule_DROP
|
|
|
|
}
|
|
|
|
|
|
|
|
protocol := proto.FirewallRule_UNKNOWN
|
|
|
|
switch PolicyRuleProtocolType(update[i].Protocol) {
|
|
|
|
case PolicyRuleProtocolALL:
|
|
|
|
protocol = proto.FirewallRule_ALL
|
|
|
|
case PolicyRuleProtocolTCP:
|
|
|
|
protocol = proto.FirewallRule_TCP
|
|
|
|
case PolicyRuleProtocolUDP:
|
|
|
|
protocol = proto.FirewallRule_UDP
|
|
|
|
case PolicyRuleProtocolICMP:
|
|
|
|
protocol = proto.FirewallRule_ICMP
|
|
|
|
}
|
|
|
|
|
|
|
|
result[i] = &proto.FirewallRule{
|
|
|
|
PeerIP: update[i].PeerIP,
|
|
|
|
Direction: direction,
|
|
|
|
Action: action,
|
|
|
|
Protocol: protocol,
|
|
|
|
Port: update[i].Port,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// getAllPeersFromGroups for given peer ID and list of groups
|
|
|
|
//
|
|
|
|
// Returns list of peers and boolean indicating if peer is in any of the groups
|
2023-11-28 13:45:26 +01:00
|
|
|
func getAllPeersFromGroups(account *Account, groups []string, peerID string) ([]*nbpeer.Peer, bool) {
|
2023-05-29 16:00:18 +02:00
|
|
|
peerInGroups := false
|
2023-11-28 13:45:26 +01:00
|
|
|
filteredPeers := make([]*nbpeer.Peer, 0, len(groups))
|
2023-05-29 16:00:18 +02:00
|
|
|
for _, g := range groups {
|
|
|
|
group, ok := account.Groups[g]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range group.Peers {
|
2023-05-31 19:04:38 +02:00
|
|
|
peer, ok := account.Peers[p]
|
|
|
|
if ok && peer != nil && peer.ID == peerID {
|
2023-05-29 16:00:18 +02:00
|
|
|
peerInGroups = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
filteredPeers = append(filteredPeers, peer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filteredPeers, peerInGroups
|
|
|
|
}
|