mirror of
https://github.com/netbirdio/netbird.git
synced 2026-08-24 08:31:28 +02:00
Compare commits
3 Commits
feature/ne
...
wasmbuild-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
485a38a4a8 | ||
|
|
05cbead39b | ||
|
|
60f4d5f9b0 |
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"errors"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"time"
|
||||
@@ -12,7 +11,6 @@ import (
|
||||
"github.com/cenkalti/backoff/v4"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/connectivity"
|
||||
"google.golang.org/grpc/credentials"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"google.golang.org/grpc/keepalive"
|
||||
@@ -20,9 +18,6 @@ import (
|
||||
"github.com/netbirdio/netbird/util/embeddedroots"
|
||||
)
|
||||
|
||||
// ErrConnectionShutdown indicates that the connection entered shutdown state before becoming ready
|
||||
var ErrConnectionShutdown = errors.New("connection shutdown before ready")
|
||||
|
||||
// Backoff returns a backoff configuration for gRPC calls
|
||||
func Backoff(ctx context.Context) backoff.BackOff {
|
||||
b := backoff.NewExponentialBackOff()
|
||||
@@ -31,26 +26,6 @@ func Backoff(ctx context.Context) backoff.BackOff {
|
||||
return backoff.WithContext(b, ctx)
|
||||
}
|
||||
|
||||
// waitForConnectionReady blocks until the connection becomes ready or fails.
|
||||
// Returns an error if the connection times out, is cancelled, or enters shutdown state.
|
||||
func waitForConnectionReady(ctx context.Context, conn *grpc.ClientConn) error {
|
||||
conn.Connect()
|
||||
|
||||
state := conn.GetState()
|
||||
for state != connectivity.Ready && state != connectivity.Shutdown {
|
||||
if !conn.WaitForStateChange(ctx, state) {
|
||||
return fmt.Errorf("wait state change from %s: %w", state, ctx.Err())
|
||||
}
|
||||
state = conn.GetState()
|
||||
}
|
||||
|
||||
if state == connectivity.Shutdown {
|
||||
return ErrConnectionShutdown
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateConnection creates a gRPC client connection with the appropriate transport options.
|
||||
// The component parameter specifies the WebSocket proxy component path (e.g., "/management", "/signal").
|
||||
func CreateConnection(ctx context.Context, addr string, tlsEnabled bool, component string) (*grpc.ClientConn, error) {
|
||||
@@ -68,25 +43,22 @@ func CreateConnection(ctx context.Context, addr string, tlsEnabled bool, compone
|
||||
}))
|
||||
}
|
||||
|
||||
conn, err := grpc.NewClient(
|
||||
connCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
conn, err := grpc.DialContext(
|
||||
connCtx,
|
||||
addr,
|
||||
transportOption,
|
||||
WithCustomDialer(tlsEnabled, component),
|
||||
grpc.WithBlock(),
|
||||
grpc.WithKeepaliveParams(keepalive.ClientParameters{
|
||||
Time: 30 * time.Second,
|
||||
Timeout: 10 * time.Second,
|
||||
}),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("new client: %w", err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := waitForConnectionReady(ctx, conn); err != nil {
|
||||
_ = conn.Close()
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("dial context: %w", err)
|
||||
}
|
||||
|
||||
return conn, nil
|
||||
|
||||
@@ -1242,6 +1242,13 @@ func (a *Account) getRulePeers(rule *PolicyRule, postureChecks []string, peerID
|
||||
}
|
||||
}
|
||||
}
|
||||
if rule.SourceResource.Type == ResourceTypePeer && rule.SourceResource.ID != "" {
|
||||
_, distPeer := distributionPeers[rule.SourceResource.ID]
|
||||
_, valid := validatedPeersMap[rule.SourceResource.ID]
|
||||
if distPeer && valid && a.validatePostureChecksOnPeer(context.Background(), postureChecks, rule.SourceResource.ID) {
|
||||
distPeersWithPolicy[rule.SourceResource.ID] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
distributionGroupPeers := make([]*nbpeer.Peer, 0, len(distPeersWithPolicy))
|
||||
for pID := range distPeersWithPolicy {
|
||||
@@ -1587,6 +1594,10 @@ func getPoliciesSourcePeers(policies []*Policy, groups map[string]*Group) map[st
|
||||
sourcePeers[peer] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
if rule.SourceResource.Type == ResourceTypePeer && rule.SourceResource.ID != "" {
|
||||
sourcePeers[rule.SourceResource.ID] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -858,6 +858,14 @@ func (b *NetworkMapBuilder) getRulePeers(
|
||||
}
|
||||
}
|
||||
|
||||
if rule.SourceResource.Type == ResourceTypePeer && rule.SourceResource.ID != "" {
|
||||
_, distPeer := distributionPeers[rule.SourceResource.ID]
|
||||
_, valid := validatedPeersMap[rule.SourceResource.ID]
|
||||
if distPeer && valid && account.validatePostureChecksOnPeer(context.Background(), postureChecks, rule.SourceResource.ID) {
|
||||
distPeersWithPolicy[rule.SourceResource.ID] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
distributionGroupPeers := make([]*nbpeer.Peer, 0, len(distPeersWithPolicy))
|
||||
for pID := range distPeersWithPolicy {
|
||||
peer := b.cache.globalPeers[pID]
|
||||
@@ -1287,24 +1295,54 @@ func (b *NetworkMapBuilder) calculateIncrementalUpdates(account *Account, newPee
|
||||
if !rule.Enabled {
|
||||
continue
|
||||
}
|
||||
var peerInSources, peerInDestinations bool
|
||||
|
||||
peerInSources := b.isPeerInGroups(rule.Sources, peerGroups)
|
||||
peerInDestinations := b.isPeerInGroups(rule.Destinations, peerGroups)
|
||||
if rule.SourceResource.Type == ResourceTypePeer && rule.SourceResource.ID == newPeerID {
|
||||
peerInSources = true
|
||||
} else {
|
||||
peerInSources = b.isPeerInGroups(rule.Sources, peerGroups)
|
||||
}
|
||||
|
||||
if rule.DestinationResource.Type == ResourceTypePeer && rule.DestinationResource.ID == newPeerID {
|
||||
peerInDestinations = true
|
||||
} else {
|
||||
peerInDestinations = b.isPeerInGroups(rule.Destinations, peerGroups)
|
||||
}
|
||||
|
||||
if peerInSources {
|
||||
b.addUpdateForPeersInGroups(updates, rule.Destinations, newPeerID, rule, FirewallRuleDirectionIN, groupAllLn)
|
||||
if len(rule.Destinations) > 0 {
|
||||
b.addUpdateForPeersInGroups(updates, rule.Destinations, newPeerID, rule, FirewallRuleDirectionIN, groupAllLn)
|
||||
}
|
||||
if rule.DestinationResource.Type == ResourceTypePeer && rule.DestinationResource.ID != "" {
|
||||
b.addUpdateForDirectPeerResource(updates, rule.DestinationResource.ID, newPeerID, rule, FirewallRuleDirectionIN)
|
||||
}
|
||||
}
|
||||
|
||||
if peerInDestinations {
|
||||
b.addUpdateForPeersInGroups(updates, rule.Sources, newPeerID, rule, FirewallRuleDirectionOUT, groupAllLn)
|
||||
if len(rule.Sources) > 0 {
|
||||
b.addUpdateForPeersInGroups(updates, rule.Sources, newPeerID, rule, FirewallRuleDirectionOUT, groupAllLn)
|
||||
}
|
||||
if rule.SourceResource.Type == ResourceTypePeer && rule.SourceResource.ID != "" {
|
||||
b.addUpdateForDirectPeerResource(updates, rule.SourceResource.ID, newPeerID, rule, FirewallRuleDirectionOUT)
|
||||
}
|
||||
}
|
||||
|
||||
if rule.Bidirectional {
|
||||
if peerInSources {
|
||||
b.addUpdateForPeersInGroups(updates, rule.Destinations, newPeerID, rule, FirewallRuleDirectionOUT, groupAllLn)
|
||||
if len(rule.Destinations) > 0 {
|
||||
b.addUpdateForPeersInGroups(updates, rule.Destinations, newPeerID, rule, FirewallRuleDirectionOUT, groupAllLn)
|
||||
}
|
||||
if rule.DestinationResource.Type == ResourceTypePeer && rule.DestinationResource.ID != "" {
|
||||
b.addUpdateForDirectPeerResource(updates, rule.DestinationResource.ID, newPeerID, rule, FirewallRuleDirectionOUT)
|
||||
}
|
||||
}
|
||||
if peerInDestinations {
|
||||
b.addUpdateForPeersInGroups(updates, rule.Sources, newPeerID, rule, FirewallRuleDirectionIN, groupAllLn)
|
||||
if len(rule.Sources) > 0 {
|
||||
b.addUpdateForPeersInGroups(updates, rule.Sources, newPeerID, rule, FirewallRuleDirectionIN, groupAllLn)
|
||||
}
|
||||
if rule.SourceResource.Type == ResourceTypePeer && rule.SourceResource.ID != "" {
|
||||
b.addUpdateForDirectPeerResource(updates, rule.SourceResource.ID, newPeerID, rule, FirewallRuleDirectionIN)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1566,42 +1604,90 @@ func (b *NetworkMapBuilder) addUpdateForPeersInGroups(
|
||||
if _, ok := b.validatedPeers[peerID]; !ok {
|
||||
continue
|
||||
}
|
||||
delta := updates[peerID]
|
||||
if delta == nil {
|
||||
delta = &PeerUpdateDelta{
|
||||
PeerID: peerID,
|
||||
AddConnectedPeer: newPeerID,
|
||||
AddFirewallRules: make([]*FirewallRuleDelta, 0),
|
||||
}
|
||||
updates[peerID] = delta
|
||||
targetPeer := b.cache.globalPeers[peerID]
|
||||
if targetPeer == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
peerIPForRule := fr.PeerIP
|
||||
if all {
|
||||
fr.PeerIP = allPeers
|
||||
peerIPForRule = allPeers
|
||||
}
|
||||
|
||||
if len(rule.Ports) > 0 || len(rule.PortRanges) > 0 {
|
||||
expandedRules := expandPortsAndRanges(*fr, rule, b.cache.globalPeers[peerID])
|
||||
for _, expandedRule := range expandedRules {
|
||||
ruleID := b.generateFirewallRuleID(expandedRule)
|
||||
delta.AddFirewallRules = append(delta.AddFirewallRules, &FirewallRuleDelta{
|
||||
Rule: expandedRule,
|
||||
RuleID: ruleID,
|
||||
Direction: direction,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
ruleID := b.generateFirewallRuleID(fr)
|
||||
delta.AddFirewallRules = append(delta.AddFirewallRules, &FirewallRuleDelta{
|
||||
Rule: fr,
|
||||
RuleID: ruleID,
|
||||
Direction: direction,
|
||||
})
|
||||
}
|
||||
b.addOrUpdateFirewallRuleInDelta(updates, peerID, newPeerID, rule, direction, fr, peerIPForRule, targetPeer)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (b *NetworkMapBuilder) addUpdateForDirectPeerResource(
|
||||
updates map[string]*PeerUpdateDelta, targetPeerID string, newPeerID string,
|
||||
rule *PolicyRule, direction int,
|
||||
) {
|
||||
if targetPeerID == newPeerID {
|
||||
return
|
||||
}
|
||||
|
||||
if _, ok := b.validatedPeers[targetPeerID]; !ok {
|
||||
return
|
||||
}
|
||||
|
||||
newPeer := b.cache.globalPeers[newPeerID]
|
||||
if newPeer == nil {
|
||||
return
|
||||
}
|
||||
|
||||
targetPeer := b.cache.globalPeers[targetPeerID]
|
||||
if targetPeer == nil {
|
||||
return
|
||||
}
|
||||
|
||||
fr := &FirewallRule{
|
||||
PolicyID: rule.ID,
|
||||
PeerIP: newPeer.IP.String(),
|
||||
Direction: direction,
|
||||
Action: string(rule.Action),
|
||||
Protocol: string(rule.Protocol),
|
||||
}
|
||||
|
||||
b.addOrUpdateFirewallRuleInDelta(updates, targetPeerID, newPeerID, rule, direction, fr, fr.PeerIP, targetPeer)
|
||||
}
|
||||
|
||||
func (b *NetworkMapBuilder) addOrUpdateFirewallRuleInDelta(
|
||||
updates map[string]*PeerUpdateDelta, targetPeerID string, newPeerID string,
|
||||
rule *PolicyRule, direction int, baseRule *FirewallRule, peerIP string, targetPeer *nbpeer.Peer,
|
||||
) {
|
||||
delta := updates[targetPeerID]
|
||||
if delta == nil {
|
||||
delta = &PeerUpdateDelta{
|
||||
PeerID: targetPeerID,
|
||||
AddConnectedPeer: newPeerID,
|
||||
AddFirewallRules: make([]*FirewallRuleDelta, 0),
|
||||
}
|
||||
updates[targetPeerID] = delta
|
||||
}
|
||||
|
||||
baseRule.PeerIP = peerIP
|
||||
|
||||
if len(rule.Ports) > 0 || len(rule.PortRanges) > 0 {
|
||||
expandedRules := expandPortsAndRanges(*baseRule, rule, targetPeer)
|
||||
for _, expandedRule := range expandedRules {
|
||||
ruleID := b.generateFirewallRuleID(expandedRule)
|
||||
delta.AddFirewallRules = append(delta.AddFirewallRules, &FirewallRuleDelta{
|
||||
Rule: expandedRule,
|
||||
RuleID: ruleID,
|
||||
Direction: direction,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
ruleID := b.generateFirewallRuleID(baseRule)
|
||||
delta.AddFirewallRules = append(delta.AddFirewallRules, &FirewallRuleDelta{
|
||||
Rule: baseRule,
|
||||
RuleID: ruleID,
|
||||
Direction: direction,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (b *NetworkMapBuilder) applyDeltaToPeer(account *Account, peerID string, delta *PeerUpdateDelta) {
|
||||
if delta.AddConnectedPeer != "" || len(delta.AddFirewallRules) > 0 {
|
||||
if aclView := b.cache.peerACLs[peerID]; aclView != nil {
|
||||
|
||||
Reference in New Issue
Block a user