-
Notifications
You must be signed in to change notification settings - Fork 310
Feature/netflow udp proxy #8909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fdurand
wants to merge
13
commits into
devel
Choose a base branch
from
feature/netflow_udp_proxy
base: devel
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,968
−37
Open
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c7296cc
pfudpproxy to forward netflow sflow to one of the fingerbank-collector
fdurand 8d87b3a
Fixed spec file
fdurand 6b4207a
Missing import
fdurand 1b55169
Fixes backend healthcheck and added iptables rules for pfupdproxy
fdurand 2002954
Set the log level and get the probe port from pfconfig
fdurand d1dcee8
Fix for PR
fdurand 4495639
pfudpproxy to forward netflow sflow to one of the fingerbank-collector
fdurand ef62897
Missing import
fdurand a37f541
Fixes backend healthcheck and added iptables rules for pfupdproxy
fdurand 8aa8066
Set the log level and get the probe port from pfconfig
fdurand 6c10222
Fixes for PR
fdurand b6d92f7
Fixes for PR
fdurand 31f312e
Fixes for PR
fdurand File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,4 +24,3 @@ vrrp_script haproxy_portal { | |
|
|
||
| %%vrrp%% | ||
|
|
||
| %%lvs%% | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Copyright (C) Inverse inc. | ||
| [Unit] | ||
| Description=PacketFence UDP Reverse Proxy for Cluster Mode | ||
| Wants=packetfence-keepalived.service packetfence-base.target packetfence-config.service | ||
| After=packetfence-keepalived.service packetfence-base.target packetfence-config.service | ||
|
|
||
| [Service] | ||
| Type=notify | ||
| WatchdogSec=30s | ||
| NotifyAccess=all | ||
| Environment=LOG_LEVEL=INFO | ||
| ExecStart=/usr/local/pf/sbin/pfudpproxy | ||
| Restart=on-failure | ||
| StartLimitBurst=3 | ||
| StartLimitInterval=60 | ||
| Slice=packetfence.slice | ||
|
|
||
| [Install] | ||
| WantedBy=packetfence-cluster.target |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../conf/systemd/packetfence-pfudpproxy.service |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/inverse-inc/go-utils/log" | ||
| "github.com/inverse-inc/packetfence/go/pfconfigdriver" | ||
| ) | ||
|
|
||
| // Default configuration values | ||
| const ( | ||
| DefaultHealthCheckPort = 4723 | ||
| DefaultHealthCheckPath = "/" | ||
| DefaultHealthCheckInterval = 5 * time.Second | ||
| DefaultHealthCheckTimeout = 10 * time.Second | ||
| DefaultExpectedStatusCode = 404 | ||
|
|
||
| PortNetFlow = 2055 | ||
| PortSFlow = 6343 | ||
| PortIPFIX = 4739 | ||
| ) | ||
|
|
||
| // ProxyConfig holds the configuration for the UDP proxy | ||
| type ProxyConfig struct { | ||
| VIPAddress string | ||
| Ports []int | ||
| Backends []*Backend | ||
| HealthCheckPort int | ||
| HealthCheckPath string | ||
| HealthCheckInterval time.Duration | ||
| HealthCheckTimeout time.Duration | ||
| ExpectedStatusCode int | ||
| } | ||
|
|
||
| // Backend represents a cluster member that can receive forwarded packets | ||
| type Backend struct { | ||
| Host string | ||
| ManagementIP string | ||
| Healthy bool | ||
| LastCheck time.Time | ||
| } | ||
|
|
||
| // getHealthCheckPort returns the health check port from FingerbankSettingsCollector or default | ||
| func getHealthCheckPort(ctx context.Context) int { | ||
| collector := pfconfigdriver.GetType[pfconfigdriver.FingerbankSettingsCollector](ctx) | ||
| if port, err := collector.Port.Int64(); err == nil && port > 0 && port < 65536 { | ||
| return int(port) | ||
| } | ||
| return DefaultHealthCheckPort | ||
| } | ||
|
|
||
| // LoadConfig loads the proxy configuration from pfconfig | ||
| func LoadConfig(ctx context.Context) (*ProxyConfig, error) { | ||
| config := &ProxyConfig{ | ||
| Ports: []int{PortNetFlow, PortSFlow, PortIPFIX}, | ||
| HealthCheckPort: getHealthCheckPort(ctx), | ||
| HealthCheckPath: DefaultHealthCheckPath, | ||
| HealthCheckInterval: DefaultHealthCheckInterval, | ||
| HealthCheckTimeout: DefaultHealthCheckTimeout, | ||
| ExpectedStatusCode: DefaultExpectedStatusCode, | ||
| } | ||
|
|
||
| // Load VIP address from management network configuration | ||
| vip, err := loadVIPAddress(ctx) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to load VIP address: %w", err) | ||
| } | ||
| config.VIPAddress = vip | ||
|
|
||
| // Load cluster members as backends | ||
| backends, err := loadBackends(ctx) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to load backends: %w", err) | ||
| } | ||
| config.Backends = backends | ||
|
|
||
| return config, nil | ||
| } | ||
|
|
||
| // loadVIPAddress loads the VIP address from the cluster configuration | ||
| func loadVIPAddress(ctx context.Context) (string, error) { | ||
| var mgmtNet pfconfigdriver.ManagementNetwork | ||
| pfconfigdriver.FetchDecodeSocketCache(ctx, &mgmtNet) | ||
|
|
||
| var keyConfCluster pfconfigdriver.NetInterface | ||
| keyConfCluster.PfconfigNS = "config::Pf(CLUSTER," + pfconfigdriver.FindClusterName(ctx) + ")" | ||
| keyConfCluster.PfconfigHashNS = "interface " + mgmtNet.Int | ||
| pfconfigdriver.FetchDecodeSocket(ctx, &keyConfCluster) | ||
|
|
||
| if keyConfCluster.Ip == "" { | ||
| log.LoggerWContext(ctx).Warn("No VIP configured in cluster config for interface " + mgmtNet.Int) | ||
| return "", nil | ||
| } | ||
|
|
||
| log.LoggerWContext(ctx).Debug("Loaded VIP address from cluster config: " + keyConfCluster.Ip) | ||
| return keyConfCluster.Ip, nil | ||
| } | ||
|
|
||
| // loadBackends loads cluster members from pfconfig | ||
| func loadBackends(ctx context.Context) ([]*Backend, error) { | ||
| var clusterServers pfconfigdriver.AllClusterServers | ||
| pfconfigdriver.FetchDecodeSocketCache(ctx, &clusterServers) | ||
|
|
||
| backends := make([]*Backend, 0, len(clusterServers.Element)) | ||
| for _, server := range clusterServers.Element { | ||
| if server.ManagementIp == "" { | ||
| log.LoggerWContext(ctx).Warn("Cluster server " + server.Host + " has no management IP, skipping") | ||
| continue | ||
| } | ||
|
|
||
| backend := &Backend{ | ||
| Host: server.Host, | ||
| ManagementIP: server.ManagementIp, | ||
| Healthy: false, // Will be updated by health checker | ||
| LastCheck: time.Time{}, | ||
| } | ||
| backends = append(backends, backend) | ||
| log.LoggerWContext(ctx).Debug("Added backend: " + server.Host + " (" + server.ManagementIp + ")") | ||
| } | ||
|
|
||
| if len(backends) == 0 { | ||
| return nil, fmt.Errorf("no valid cluster servers found") | ||
| } | ||
|
|
||
| return backends, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/tls" | ||
| "fmt" | ||
| "net/http" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/inverse-inc/go-utils/log" | ||
| ) | ||
|
|
||
| // HealthChecker periodically checks the health of backends by making | ||
| // HTTPS requests to fingerbank-collector. | ||
| type HealthChecker struct { | ||
| config *ProxyConfig | ||
| lb *LoadBalancer | ||
| httpClient *http.Client | ||
| } | ||
|
|
||
| // NewHealthChecker creates a new health checker. | ||
| func NewHealthChecker(config *ProxyConfig, lb *LoadBalancer) *HealthChecker { | ||
| // Create HTTP client with TLS config that skips certificate verification | ||
| // (fingerbank-collector uses self-signed certificates) | ||
| transport := &http.Transport{ | ||
| TLSClientConfig: &tls.Config{ | ||
| InsecureSkipVerify: true, | ||
| }, | ||
| TLSHandshakeTimeout: 5 * time.Second, | ||
| } | ||
|
|
||
| httpClient := &http.Client{ | ||
| Transport: transport, | ||
| Timeout: config.HealthCheckTimeout, | ||
| // Don't follow redirects | ||
| CheckRedirect: func(req *http.Request, via []*http.Request) error { | ||
| return http.ErrUseLastResponse | ||
| }, | ||
| } | ||
|
|
||
| return &HealthChecker{ | ||
| config: config, | ||
| lb: lb, | ||
| httpClient: httpClient, | ||
| } | ||
| } | ||
|
|
||
| // Start begins the health checking loop. | ||
| func (hc *HealthChecker) Start(ctx context.Context) { | ||
| log.LoggerWContext(ctx).Info("Starting health checker") | ||
|
|
||
| // Do an initial health check immediately | ||
| hc.checkAllBackends(ctx) | ||
|
|
||
| ticker := time.NewTicker(hc.config.HealthCheckInterval) | ||
| defer ticker.Stop() | ||
|
|
||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| log.LoggerWContext(ctx).Info("Health checker stopped") | ||
| return | ||
| case <-ticker.C: | ||
| hc.checkAllBackends(ctx) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // checkAllBackends checks the health of all backends in parallel. | ||
| func (hc *HealthChecker) checkAllBackends(ctx context.Context) { | ||
| backends := hc.lb.GetAllBackends() | ||
| if len(backends) == 0 { | ||
| log.LoggerWContext(ctx).Warn("No backends to check") | ||
| return | ||
| } | ||
|
|
||
| log.LoggerWContext(ctx).Debug(fmt.Sprintf("Running health checks for %d backends", len(backends))) | ||
|
|
||
| var wg sync.WaitGroup | ||
| for i := range backends { | ||
| wg.Add(1) | ||
| go func(b Backend) { | ||
| defer wg.Done() | ||
| hc.checkBackend(ctx, b) | ||
| }(backends[i]) | ||
| } | ||
| wg.Wait() | ||
|
|
||
| // Log current primary after health checks | ||
| primary := hc.lb.GetPrimary() | ||
| if primary != nil { | ||
| log.LoggerWContext(ctx).Debug(fmt.Sprintf("Current primary backend: %s (%s)", primary.Host, primary.ManagementIP)) | ||
| } else { | ||
| log.LoggerWContext(ctx).Warn("No healthy backend available after health checks") | ||
| } | ||
| } | ||
|
|
||
| // checkBackend checks the health of a single backend. | ||
| // backend is received by value (a snapshot from GetAllBackends) so reads | ||
| // of its fields are safe without holding the load balancer lock. | ||
| func (hc *HealthChecker) checkBackend(ctx context.Context, backend Backend) { | ||
| url := fmt.Sprintf("https://%s:%d%s", | ||
| backend.ManagementIP, | ||
| hc.config.HealthCheckPort, | ||
| hc.config.HealthCheckPath, | ||
| ) | ||
|
|
||
| req, err := http.NewRequestWithContext(ctx, "GET", url, nil) | ||
| if err != nil { | ||
| log.LoggerWContext(ctx).Error(fmt.Sprintf("Failed to create health check request for %s: %s", | ||
| backend.Host, err.Error())) | ||
| hc.lb.SetHealth(backend.Host, false) | ||
| return | ||
| } | ||
|
|
||
| resp, err := hc.httpClient.Do(req) | ||
| if err != nil { | ||
| log.LoggerWContext(ctx).Debug(fmt.Sprintf("Health check failed for %s: %s", | ||
| backend.Host, err.Error())) | ||
| hc.lb.SetHealth(backend.Host, false) | ||
| return | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| // fingerbank-collector returns 404 on "/" when healthy | ||
| healthy := resp.StatusCode == hc.config.ExpectedStatusCode | ||
|
|
||
| log.LoggerWContext(ctx).Debug(fmt.Sprintf("Health check result for %s (%s): status=%d, healthy=%v, wasHealthy=%v", | ||
| backend.Host, backend.ManagementIP, resp.StatusCode, healthy, backend.Healthy)) | ||
|
|
||
| if healthy && !backend.Healthy { | ||
| log.LoggerWContext(ctx).Info(fmt.Sprintf("Backend %s (%s) is now healthy", | ||
| backend.Host, backend.ManagementIP)) | ||
| } else if !healthy && backend.Healthy { | ||
| log.LoggerWContext(ctx).Warn(fmt.Sprintf("Backend %s (%s) is now unhealthy (status: %d, expected: %d)", | ||
| backend.Host, backend.ManagementIP, resp.StatusCode, hc.config.ExpectedStatusCode)) | ||
| } | ||
|
|
||
fdurand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| hc.lb.SetHealth(backend.Host, healthy) | ||
| } | ||
|
|
||
| // UpdateConfig updates the health checker configuration. | ||
| func (hc *HealthChecker) UpdateConfig(config *ProxyConfig) { | ||
| hc.config = config | ||
| hc.httpClient.Timeout = config.HealthCheckTimeout | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Health checks unconditionally set
InsecureSkipVerify: true. Even if fingerbank-collector commonly uses self-signed certs, this removes TLS authenticity entirely; consider wiring this to an explicit config toggle (or trusting a configured CA bundle) so deployments that can validate certs are not forced into insecure mode.