2023-03-13 15:14:18 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2023-03-13 15:14:18 +01:00
|
|
|
_ "embed"
|
2024-09-27 16:10:50 +02:00
|
|
|
"slices"
|
2023-07-18 11:12:50 +02:00
|
|
|
"strconv"
|
2023-03-13 15:14:18 +01:00
|
|
|
"strings"
|
|
|
|
|
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"
|
2024-03-27 18:48:48 +01:00
|
|
|
nbgroup "github.com/netbirdio/netbird/management/server/group"
|
2023-11-28 13:45:26 +01:00
|
|
|
nbpeer "github.com/netbirdio/netbird/management/server/peer"
|
2024-02-20 09:59:56 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/posture"
|
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")
|
|
|
|
)
|
|
|
|
|
2024-03-14 21:31:21 +01:00
|
|
|
const (
|
|
|
|
// DefaultRuleName is a name for the Default rule that is created for every account
|
|
|
|
DefaultRuleName = "Default"
|
|
|
|
// DefaultRuleDescription is a description for the Default rule that is created for every account
|
|
|
|
DefaultRuleDescription = "This is a default rule that allows connections between all the resources"
|
|
|
|
// DefaultPolicyName is a name for the Default policy that is created for every account
|
|
|
|
DefaultPolicyName = "Default"
|
|
|
|
// DefaultPolicyDescription is a description for the Default policy that is created for every account
|
|
|
|
DefaultPolicyDescription = "This is a default policy that allows connections between all the resources"
|
|
|
|
)
|
|
|
|
|
2023-05-29 16:00:18 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-10-02 13:41:00 +02:00
|
|
|
// RulePortRange represents a range of ports for a firewall rule.
|
|
|
|
type RulePortRange struct {
|
|
|
|
Start uint16
|
|
|
|
End uint16
|
|
|
|
}
|
|
|
|
|
2023-03-13 15:14:18 +01:00
|
|
|
// 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"`
|
2024-10-02 13:41:00 +02:00
|
|
|
|
|
|
|
// PortRanges a list of port ranges.
|
|
|
|
PortRanges []RulePortRange `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)),
|
2024-10-02 13:41:00 +02:00
|
|
|
PortRanges: make([]RulePortRange, len(pm.PortRanges)),
|
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)
|
2024-10-02 13:41:00 +02:00
|
|
|
copy(rule.PortRanges, pm.PortRanges)
|
2023-08-22 17:56:39 +02:00
|
|
|
return rule
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2024-05-16 18:28:37 +02:00
|
|
|
Rules []*PolicyRule `gorm:"foreignKey:PolicyID;references:id;constraint:OnDelete:CASCADE;"`
|
2024-02-20 09:59:56 +01:00
|
|
|
|
|
|
|
// SourcePostureChecks are ID references to Posture checks for policy source groups
|
|
|
|
SourcePostureChecks []string `gorm:"serializer:json"`
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy returns a copy of the policy.
|
|
|
|
func (p *Policy) Copy() *Policy {
|
|
|
|
c := &Policy{
|
2024-02-20 09:59:56 +01:00
|
|
|
ID: p.ID,
|
|
|
|
Name: p.Name,
|
|
|
|
Description: p.Description,
|
|
|
|
Enabled: p.Enabled,
|
|
|
|
Rules: make([]*PolicyRule, len(p.Rules)),
|
|
|
|
SourcePostureChecks: make([]string, len(p.SourcePostureChecks)),
|
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
|
|
|
}
|
2024-02-20 09:59:56 +01:00
|
|
|
copy(c.SourcePostureChecks, p.SourcePostureChecks)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 19:34:50 +02:00
|
|
|
// ruleGroups returns a list of all groups referenced in the policy's rules,
|
|
|
|
// including sources and destinations.
|
|
|
|
func (p *Policy) ruleGroups() []string {
|
|
|
|
groups := make([]string, 0)
|
|
|
|
for _, rule := range p.Rules {
|
|
|
|
groups = append(groups, rule.Sources...)
|
|
|
|
groups = append(groups, rule.Destinations...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return groups
|
|
|
|
}
|
|
|
|
|
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.
|
2024-07-03 11:33:02 +02:00
|
|
|
func (a *Account) getPeerConnectionResources(ctx context.Context, peerID string, validatedPeersMap map[string]struct{}) ([]*nbpeer.Peer, []*FirewallRule) {
|
|
|
|
generateResources, getAccumulatedResources := a.connResourcesGenerator(ctx)
|
2023-05-29 16:00:18 +02:00
|
|
|
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
|
|
|
|
2024-08-07 10:52:31 +02:00
|
|
|
sourcePeers, peerInSources := a.getAllPeersFromGroups(ctx, rule.Sources, peerID, policy.SourcePostureChecks, validatedPeersMap)
|
|
|
|
destinationPeers, peerInDestinations := a.getAllPeersFromGroups(ctx, rule.Destinations, peerID, nil, validatedPeersMap)
|
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.
|
2024-07-03 11:33:02 +02:00
|
|
|
func (a *Account) connResourcesGenerator(ctx context.Context) (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 {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Errorf("failed to get group all: %v", err)
|
2024-03-27 18:48:48 +01:00
|
|
|
all = &nbgroup.Group{}
|
2023-07-18 11:12:50 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2024-02-20 09:59:56 +01:00
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2024-08-07 10:52:31 +02:00
|
|
|
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
|
2024-07-03 11:33:02 +02:00
|
|
|
func (am *DefaultAccountManager) GetPolicy(ctx context.Context, accountID, policyID, userID string) (*Policy, error) {
|
2024-09-27 16:10:50 +02:00
|
|
|
user, err := am.Store.GetUserByUserID(ctx, LockingStrengthShare, userID)
|
2023-03-13 15:14:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
if !user.IsAdminOrServiceUser() || user.AccountID != accountID {
|
2023-12-01 17:24:57 +01:00
|
|
|
return nil, status.Errorf(status.PermissionDenied, "only users with admin power are allowed to view policies")
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
return am.Store.GetPolicyByID(ctx, LockingStrengthShare, policyID, accountID)
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SavePolicy in the store
|
2024-09-27 16:10:50 +02:00
|
|
|
func (am *DefaultAccountManager) SavePolicy(ctx context.Context, accountID, userID string, policy *Policy, isUpdate bool) error {
|
2024-07-31 14:53:32 +02:00
|
|
|
unlock := am.Store.AcquireWriteLockByUID(ctx, accountID)
|
2023-03-13 15:14:18 +01:00
|
|
|
defer unlock()
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
account, err := am.Store.GetAccount(ctx, accountID)
|
2023-03-13 15:14:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-10-06 19:34:50 +02:00
|
|
|
updateAccountPeers, err := am.savePolicy(account, policy, isUpdate)
|
|
|
|
if err != nil {
|
2024-09-27 16:10:50 +02:00
|
|
|
return err
|
|
|
|
}
|
2023-03-13 15:14:18 +01:00
|
|
|
|
|
|
|
account.Network.IncSerial()
|
2024-07-03 11:33:02 +02:00
|
|
|
if err = am.Store.SaveAccount(ctx, account); err != nil {
|
2023-03-13 15:14:18 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
action := activity.PolicyAdded
|
2024-09-27 16:10:50 +02:00
|
|
|
if isUpdate {
|
2023-03-13 15:14:18 +01:00
|
|
|
action = activity.PolicyUpdated
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
am.StoreEvent(ctx, userID, policy.ID, accountID, action, policy.EventMeta())
|
2023-03-13 15:14:18 +01:00
|
|
|
|
2024-10-06 19:34:50 +02:00
|
|
|
if updateAccountPeers {
|
|
|
|
am.updateAccountPeers(ctx, account)
|
2024-10-21 11:09:48 +02:00
|
|
|
} else {
|
|
|
|
log.WithContext(ctx).Tracef("Skipping account peers update for policy: %s", policy.ID)
|
2024-10-06 19:34:50 +02:00
|
|
|
}
|
2023-10-04 15:08:50 +02:00
|
|
|
|
|
|
|
return nil
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeletePolicy from the store
|
2024-07-03 11:33:02 +02:00
|
|
|
func (am *DefaultAccountManager) DeletePolicy(ctx context.Context, accountID, policyID, userID string) error {
|
2024-07-31 14:53:32 +02:00
|
|
|
unlock := am.Store.AcquireWriteLockByUID(ctx, accountID)
|
2023-03-13 15:14:18 +01:00
|
|
|
defer unlock()
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
account, err := am.Store.GetAccount(ctx, accountID)
|
2023-03-13 15:14:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
policy, err := am.deletePolicy(account, policyID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
account.Network.IncSerial()
|
2024-07-03 11:33:02 +02:00
|
|
|
if err = am.Store.SaveAccount(ctx, account); err != nil {
|
2023-03-13 15:14:18 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
am.StoreEvent(ctx, userID, policy.ID, accountID, activity.PolicyRemoved, policy.EventMeta())
|
2023-03-13 15:14:18 +01:00
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
am.updateAccountPeers(ctx, account)
|
2023-10-04 15:08:50 +02:00
|
|
|
|
|
|
|
return nil
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListPolicies from the store
|
2024-07-03 11:33:02 +02:00
|
|
|
func (am *DefaultAccountManager) ListPolicies(ctx context.Context, accountID, userID string) ([]*Policy, error) {
|
2024-09-27 16:10:50 +02:00
|
|
|
user, err := am.Store.GetUserByUserID(ctx, LockingStrengthShare, userID)
|
2023-03-13 15:14:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
if !user.IsAdminOrServiceUser() || user.AccountID != accountID {
|
|
|
|
return nil, status.Errorf(status.PermissionDenied, "only users with admin power are allowed to view policies")
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
return am.Store.GetAccountPolicies(ctx, LockingStrengthShare, accountID)
|
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
|
|
|
|
}
|
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
// savePolicy saves or updates a policy in the given account.
|
|
|
|
// If isUpdate is true, the function updates the existing policy; otherwise, it adds a new policy.
|
2024-10-06 19:34:50 +02:00
|
|
|
func (am *DefaultAccountManager) savePolicy(account *Account, policyToSave *Policy, isUpdate bool) (bool, error) {
|
2024-09-27 16:10:50 +02:00
|
|
|
for index, rule := range policyToSave.Rules {
|
|
|
|
rule.Sources = filterValidGroupIDs(account, rule.Sources)
|
|
|
|
rule.Destinations = filterValidGroupIDs(account, rule.Destinations)
|
|
|
|
policyToSave.Rules[index] = rule
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
2024-09-27 16:10:50 +02:00
|
|
|
|
|
|
|
if policyToSave.SourcePostureChecks != nil {
|
|
|
|
policyToSave.SourcePostureChecks = filterValidPostureChecks(account, policyToSave.SourcePostureChecks)
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
2024-09-27 16:10:50 +02:00
|
|
|
|
|
|
|
if isUpdate {
|
|
|
|
policyIdx := slices.IndexFunc(account.Policies, func(policy *Policy) bool { return policy.ID == policyToSave.ID })
|
|
|
|
if policyIdx < 0 {
|
2024-10-06 19:34:50 +02:00
|
|
|
return false, status.Errorf(status.NotFound, "couldn't find policy id %s", policyToSave.ID)
|
2024-09-27 16:10:50 +02:00
|
|
|
}
|
|
|
|
|
2024-10-06 19:34:50 +02:00
|
|
|
oldPolicy := account.Policies[policyIdx]
|
2024-10-21 16:30:31 +02:00
|
|
|
// Update the existing policy
|
|
|
|
account.Policies[policyIdx] = policyToSave
|
|
|
|
|
2024-10-21 16:12:19 +02:00
|
|
|
if !policyToSave.Enabled && !oldPolicy.Enabled {
|
|
|
|
return false, nil
|
|
|
|
}
|
2024-10-06 19:34:50 +02:00
|
|
|
updateAccountPeers := anyGroupHasPeers(account, oldPolicy.ruleGroups()) || anyGroupHasPeers(account, policyToSave.ruleGroups())
|
|
|
|
|
|
|
|
return updateAccountPeers, nil
|
2024-09-27 16:10:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the new policy to the account
|
|
|
|
account.Policies = append(account.Policies, policyToSave)
|
|
|
|
|
2024-10-06 19:34:50 +02:00
|
|
|
return anyGroupHasPeers(account, policyToSave.ruleGroups()), nil
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
2023-05-29 16:00:18 +02:00
|
|
|
|
2024-10-02 13:41:00 +02:00
|
|
|
func toProtocolFirewallRules(rules []*FirewallRule) []*proto.FirewallRule {
|
|
|
|
result := make([]*proto.FirewallRule, len(rules))
|
|
|
|
for i := range rules {
|
|
|
|
rule := rules[i]
|
2023-05-29 16:00:18 +02:00
|
|
|
|
|
|
|
result[i] = &proto.FirewallRule{
|
2024-10-02 13:41:00 +02:00
|
|
|
PeerIP: rule.PeerIP,
|
|
|
|
Direction: getProtoDirection(rule.Direction),
|
|
|
|
Action: getProtoAction(rule.Action),
|
|
|
|
Protocol: getProtoProtocol(rule.Protocol),
|
|
|
|
Port: rule.Port,
|
2023-05-29 16:00:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// getAllPeersFromGroups for given peer ID and list of groups
|
|
|
|
//
|
2024-02-20 09:59:56 +01:00
|
|
|
// Returns a list of peers from specified groups that pass specified posture checks
|
|
|
|
// and a boolean indicating if the supplied peer ID exists within these groups.
|
|
|
|
//
|
|
|
|
// Important: Posture checks are applicable only to source group peers,
|
|
|
|
// for destination group peers, call this method with an empty list of sourcePostureChecksIDs
|
2024-08-07 10:52:31 +02:00
|
|
|
func (a *Account) getAllPeersFromGroups(ctx context.Context, groups []string, peerID string, sourcePostureChecksIDs []string, validatedPeersMap map[string]struct{}) ([]*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 {
|
2024-08-07 10:52:31 +02:00
|
|
|
group, ok := a.Groups[g]
|
2023-05-29 16:00:18 +02:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range group.Peers {
|
2024-08-07 10:52:31 +02:00
|
|
|
peer, ok := a.Peers[p]
|
2023-12-12 18:17:00 +01:00
|
|
|
if !ok || peer == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-02-20 09:59:56 +01:00
|
|
|
// validate the peer based on policy posture checks applied
|
2024-08-07 10:52:31 +02:00
|
|
|
isValid := a.validatePostureChecksOnPeer(ctx, sourcePostureChecksIDs, peer.ID)
|
2024-02-20 09:59:56 +01:00
|
|
|
if !isValid {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
if _, ok := validatedPeersMap[peer.ID]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-12-12 18:17:00 +01:00
|
|
|
if peer.ID == peerID {
|
2023-05-29 16:00:18 +02:00
|
|
|
peerInGroups = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
filteredPeers = append(filteredPeers, peer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filteredPeers, peerInGroups
|
|
|
|
}
|
2024-02-20 09:59:56 +01:00
|
|
|
|
|
|
|
// validatePostureChecksOnPeer validates the posture checks on a peer
|
2024-07-03 11:33:02 +02:00
|
|
|
func (a *Account) validatePostureChecksOnPeer(ctx context.Context, sourcePostureChecksID []string, peerID string) bool {
|
2024-02-20 09:59:56 +01:00
|
|
|
peer, ok := a.Peers[peerID]
|
|
|
|
if !ok && peer == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, postureChecksID := range sourcePostureChecksID {
|
2024-08-07 10:52:31 +02:00
|
|
|
postureChecks := a.getPostureChecks(postureChecksID)
|
2024-02-20 09:59:56 +01:00
|
|
|
if postureChecks == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, check := range postureChecks.GetChecks() {
|
2024-07-03 11:33:02 +02:00
|
|
|
isValid, err := check.Check(ctx, *peer)
|
2024-02-20 09:59:56 +01:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Debugf("an error occurred check %s: on peer: %s :%s", check.Name(), peer.ID, err.Error())
|
2024-02-20 09:59:56 +01:00
|
|
|
}
|
|
|
|
if !isValid {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-08-07 10:52:31 +02:00
|
|
|
func (a *Account) getPostureChecks(postureChecksID string) *posture.Checks {
|
|
|
|
for _, postureChecks := range a.PostureChecks {
|
2024-02-20 09:59:56 +01:00
|
|
|
if postureChecks.ID == postureChecksID {
|
|
|
|
return postureChecks
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-09-27 16:10:50 +02:00
|
|
|
|
|
|
|
// filterValidPostureChecks filters and returns the posture check IDs from the given list
|
|
|
|
// that are valid within the provided account.
|
|
|
|
func filterValidPostureChecks(account *Account, postureChecksIds []string) []string {
|
|
|
|
result := make([]string, 0, len(postureChecksIds))
|
|
|
|
for _, id := range postureChecksIds {
|
|
|
|
for _, postureCheck := range account.PostureChecks {
|
|
|
|
if id == postureCheck.ID {
|
|
|
|
result = append(result, id)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// filterValidGroupIDs filters a list of group IDs and returns only the ones present in the account's group map.
|
|
|
|
func filterValidGroupIDs(account *Account, groupIDs []string) []string {
|
|
|
|
result := make([]string, 0, len(groupIDs))
|
|
|
|
for _, groupID := range groupIDs {
|
|
|
|
if _, exists := account.Groups[groupID]; exists {
|
|
|
|
result = append(result, groupID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|