Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion controllers/clusterwidenetworkpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (r *ClusterwideNetworkPolicyReconciler) Reconcile(ctx context.Context, _ ct
return ctrl.Result{}, err
}

updated, err := nft.Apply()
updated, err := nft.Apply(ctx)
if err != nil {
return ctrl.Result{}, err
}
Expand Down
34 changes: 9 additions & 25 deletions controllers/droptailer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"os"
"os/exec"
"path"
"time"

Expand All @@ -15,6 +14,7 @@ import (

"github.com/go-logr/logr"
firewallv1 "github.com/metal-stack/firewall-controller/v2/api/v1"
"github.com/metal-stack/firewall-controller/v2/pkg/systemd"

"github.com/txn2/txeh"
corev1 "k8s.io/api/core/v1"
Expand All @@ -37,35 +37,19 @@ type DroptailerReconciler struct {

Log logr.Logger

HostsFile string
// Hosts is the hosts file editor, it must be shared with other reconcilers manipulating the same hosts file.
Hosts *txeh.Hosts

certificateBase string
oldPodIP string
hosts *txeh.Hosts
}

// SetupWithManager configure this controller with required defaults
func (r *DroptailerReconciler) SetupWithManager(mgr ctrl.Manager) error {
hc := &txeh.HostsConfig{
ReadFilePath: r.HostsFile,
WriteFilePath: r.HostsFile,
if r.Hosts == nil {
return fmt.Errorf("hosts editor must be specified")
}

_, err := os.Stat(r.HostsFile)
if os.IsNotExist(err) {
empty, err := os.Create(r.HostsFile)
if err != nil {
return err
}
_ = empty.Close()
}

hosts, err := txeh.NewHosts(hc)
if err != nil {
return fmt.Errorf("unable to create hosts editor:%w", err)
}

r.hosts = hosts
certificateBase := os.Getenv("DROPTAILER_CLIENT_CERTIFICATE_BASE")

if certificateBase != "" {
Expand Down Expand Up @@ -163,17 +147,17 @@ func (r *DroptailerReconciler) Reconcile(ctx context.Context, req ctrl.Request)
podIP := pod.Status.PodIP
if podIP != "" && r.oldPodIP != podIP {
r.Log.Info("podIP changed, update /etc/hosts", "old", r.oldPodIP, "new", podIP)
r.hosts.RemoveHost("droptailer")
r.hosts.AddHost(podIP, "droptailer")
err := r.hosts.Save()
r.Hosts.RemoveHost("droptailer")
r.Hosts.AddHost(podIP, "droptailer")
err := r.Hosts.Save()
if err != nil {
r.Log.Error(err, "could not write droptailer hosts entry")
return ctrl.Result{}, fmt.Errorf("could not write droptailer hosts entry:%w", err)
}
r.oldPodIP = podIP
}

err = exec.Command("systemctl", "restart", "droptailer.service").Run()
err = systemd.Restart(ctx, "droptailer.service")
if err != nil {
return ctrl.Result{}, err
}
Expand Down
104 changes: 104 additions & 0 deletions controllers/evebox_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package controllers

import (
"context"
"fmt"

"github.com/go-logr/logr"
"github.com/txn2/txeh"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/predicate"

firewallv1 "github.com/metal-stack/firewall-controller/v2/api/v1"
"github.com/metal-stack/firewall-controller/v2/pkg/systemd"
)

const (
// eveboxHostname is the hostname under which the evebox server in the shoot is reachable from the firewall.
// the evebox server url in the firewall spec is expected to reference this hostname.
eveboxHostname = "evebox"
eveboxAppLabel = "evebox"
eveboxAgentService = "evebox.service"
)

// EveboxReconciler keeps a hosts entry pointing to the pod ip of the evebox server running in the shoot
// such that the evebox agent on the firewall can reach it under a stable name.
type EveboxReconciler struct {
ShootClient client.Client
Log logr.Logger
Hosts *txeh.Hosts

oldPodIP string
}

// SetupWithManager configures this controller with required defaults
func (r *EveboxReconciler) SetupWithManager(mgr ctrl.Manager) error {
if r.Hosts == nil {
return fmt.Errorf("hosts editor must be specified")
}

eveboxPredicate := predicate.NewPredicateFuncs(func(obj client.Object) bool {
if obj.GetNamespace() != firewallv1.ClusterwideNetworkPolicyNamespace {
return false
}

return obj.GetLabels()["app"] == eveboxAppLabel
})

return ctrl.NewControllerManagedBy(mgr).
Named("evebox").
For(&corev1.Pod{}, builder.WithPredicates(eveboxPredicate)).
Complete(r)
}

// Reconcile updates the evebox hosts entry with the evebox server pod ip and restarts the evebox agent on change.
func (r *EveboxReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
pod := &corev1.Pod{}
if err := r.ShootClient.Get(ctx, req.NamespacedName, pod); err != nil {
if apierrors.IsNotFound(err) {
return ctrl.Result{}, nil
}

return ctrl.Result{}, fmt.Errorf("error retrieving evebox pod: %w", err)
}

if !pod.GetDeletionTimestamp().IsZero() {
r.Log.Info("evebox pod is being deleted")
return ctrl.Result{}, nil
}

podIP := pod.Status.PodIP
if podIP == "" || podIP == r.oldPodIP {
return ctrl.Result{}, nil
}

r.Log.Info("evebox pod ip changed, updating hosts file", "old", r.oldPodIP, "new", podIP)

r.Hosts.RemoveHost(eveboxHostname)
r.Hosts.AddHost(podIP, eveboxHostname)
if err := r.Hosts.Save(); err != nil {
return ctrl.Result{}, fmt.Errorf("could not write evebox hosts entry: %w", err)
}

r.oldPodIP = podIP

// only restart the agent when it is enabled, the agent is deployed by the firewall reconciler
enabled, err := systemd.AnyUnitFileEnabled(ctx, eveboxAgentService)
if err != nil {
return ctrl.Result{}, fmt.Errorf("unable to check if evebox agent is enabled: %w", err)
}

if enabled {
if err := systemd.Restart(ctx, eveboxAgentService); err != nil {
return ctrl.Result{}, fmt.Errorf("unable to restart evebox agent: %w", err)
}
}

r.Log.Info("evebox hosts entry successfully reconciled")

return ctrl.Result{}, nil
}
19 changes: 18 additions & 1 deletion controllers/firewall_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import (
"github.com/metal-stack/firewall-controller-manager/api/v2/helper"
firewallv1 "github.com/metal-stack/firewall-controller/v2/api/v1"
"github.com/metal-stack/firewall-controller/v2/pkg/network"
"github.com/metal-stack/firewall-controller/v2/pkg/suricata"
"github.com/metal-stack/firewall-controller/v2/pkg/updater"
osnet "github.com/metal-stack/os-installer/pkg/network"
)

// FirewallReconciler reconciles a Firewall object
Expand Down Expand Up @@ -63,6 +65,9 @@ const (
nftablesExporterService = "nftables-exporter"
nftablesExporterNamedPort = "nftexporter"
nftablesExporterPort = 9630
suricataExporterService = "suricata-exporter"
suricataExporterNamedPort = "suricataexporter"
suricataExporterPort = 9917
exporterLabelKey = "app"
)

Expand Down Expand Up @@ -94,6 +99,8 @@ func (r *FirewallReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
return ctrl.Result{}, err
}

firewallNetwork := osnet.New(allocation)

if r.Updater != nil {
r.Log.Info("running binary updater")

Expand All @@ -110,7 +117,7 @@ func (r *FirewallReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c

var errs []error

changed, err := network.ReconcileNetwork(ctx, r.Log, f, allocation, r.FrrVersion)
changed, err := network.ReconcileNetwork(ctx, r.Log, f, firewallNetwork, r.FrrVersion)
if changed && err == nil {
r.recordFirewallEvent(f, corev1.EventTypeNormal, "Network settings", "reconciliation succeeded (frr.conf)")
} else if changed && err != nil {
Expand All @@ -125,6 +132,11 @@ func (r *FirewallReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
errs = append(errs, err)
}

r.Log.Info("reconciling suricata")
if err = suricata.New(r.Log).Reconcile(ctx, r.Log, f, firewallNetwork); err != nil {
errs = append(errs, err)
}

r.Log.Info("reconciling ssh keys")
if err := r.reconcileSSHKeys(f); err != nil {
errs = append(errs, err)
Expand Down Expand Up @@ -165,6 +177,11 @@ func (r *FirewallReconciler) reconcileFirewallServices(ctx context.Context, f *f
port: nodeExporterPort,
namedPort: nodeExporterNamedPort,
},
{
name: suricataExporterNamedPort,
port: suricataExporterPort,
namedPort: suricataExporterNamedPort,
},
}

var errs []error
Expand Down
48 changes: 44 additions & 4 deletions controllers/firewall_monitor_annotation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package controllers
import (
"context"
"fmt"
"os/exec"
"slices"
"strings"

"github.com/go-logr/logr"
firewallv2 "github.com/metal-stack/firewall-controller-manager/api/v2"
"github.com/metal-stack/firewall-controller/v2/pkg/updater"
"github.com/metal-stack/firewall-controller/v2/pkg/systemd"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/tools/record"
Expand Down Expand Up @@ -89,7 +90,7 @@ func (r *FirewallMonitorAnnotationController) Reconcile(ctx context.Context, req

services, ok := fwmon.Annotations[firewallv2.FirewallRestartSystemdServicesAnnotation]
if !ok {
return reconcile.Result{}, nil
return r.reconcileSuricataUpdateSources(ctx, fwmon)
}

var (
Expand Down Expand Up @@ -126,7 +127,7 @@ func (r *FirewallMonitorAnnotationController) Reconcile(ctx context.Context, req

r.Log.Info("restart service", "service-name", serviceName)

if err := updater.Restart(ctx, serviceName); err != nil {
if err := systemd.Restart(ctx, serviceName); err != nil {
r.Recorder.Event(
fwmon,
corev1.EventTypeWarning,
Expand Down Expand Up @@ -156,7 +157,7 @@ func (r *FirewallMonitorAnnotationController) Reconcile(ctx context.Context, req
if restartFirewallController {
r.Log.Info("restart firewall-controller")

if err := updater.Restart(ctx, firewallControllerService); err != nil {
if err := systemd.Restart(ctx, firewallControllerService); err != nil {
r.Recorder.Event(
fwmon,
corev1.EventTypeWarning,
Expand All @@ -175,5 +176,44 @@ func (r *FirewallMonitorAnnotationController) Reconcile(ctx context.Context, req
}
}

return r.reconcileSuricataUpdateSources(ctx, fwmon)
}

func (r *FirewallMonitorAnnotationController) reconcileSuricataUpdateSources(ctx context.Context, fwmon *firewallv2.FirewallMonitor) (ctrl.Result, error) {
if !firewallv2.IsAnnotationTrue(fwmon, firewallv2.FirewallSuricataUpdateSourcesAnnotation) {
return ctrl.Result{}, nil
}

r.Log.Info("updating suricata sources")

cmd := exec.CommandContext(ctx, "suricata-update", "update-sources")
out, err := cmd.CombinedOutput()
if err != nil {
r.Recorder.Event(
fwmon,
corev1.EventTypeWarning,
"SuricataUpdateSources",
fmt.Sprintf("suricata source update failed: %s: %s", err, strings.TrimSpace(string(out))),
)

r.Log.Error(err, "error updating suricata sources", "output", string(out))
return ctrl.Result{}, fmt.Errorf("unable to update suricata sources: %w", err)
}

r.Recorder.Event(
fwmon,
corev1.EventTypeNormal,
"SuricataUpdateSources",
"suricata sources were updated through monitor annotation",
)

r.Log.Info("Removing annotation from firewall monitor", "annotation", firewallv2.FirewallSuricataUpdateSourcesAnnotation)

patch := client.MergeFrom(fwmon.DeepCopy())
delete(fwmon.Annotations, firewallv2.FirewallSuricataUpdateSourcesAnnotation)
if err := r.ShootClient.Patch(ctx, fwmon, patch); err != nil {
return ctrl.Result{}, err
}

return ctrl.Result{}, nil
}
21 changes: 17 additions & 4 deletions controllers/firewall_monitor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,23 @@ func (r *FirewallMonitorReconciler) Reconcile(ctx context.Context, req ctrl.Requ
return ctrl.Result{}, err
}

idsStats := firewallv2.IDSStatsByDevice{}
var (
idsStats = firewallv2.IDSStatsByDevice{}
s = suricata.New(r.Log)
)

suricataStatus, err := s.Status(ctx)
if err != nil {
r.Log.Error(err, "unable to figure out suricata status")
}

if r.IDSEnabled {
s := suricata.New()
ss, err := s.InterfaceStats(ctx)
interfaceStats, err := s.InterfaceStats(ctx)
if err != nil {
return ctrl.Result{}, err
}
for iface, stat := range *ss {

for iface, stat := range interfaceStats {
idsStats[iface] = firewallv2.InterfaceStat{
Drop: stat.Drop,
InvalidChecksums: stat.InvalidChecksums,
Expand Down Expand Up @@ -121,6 +130,10 @@ func (r *FirewallMonitorReconciler) Reconcile(ctx context.Context, req ctrl.Requ
DistanceSupported: false,
}

if suricataStatus != nil {
mon.ControllerStatus.Suricata = *suricataStatus
}

if !r.seedUpdated.IsZero() {
mon.ControllerStatus.SeedUpdated = r.seedUpdated
}
Expand Down
Loading