-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f50577
commit d27c7cf
Showing
5 changed files
with
106 additions
and
76 deletions.
There are no files selected for viewing
This file contains 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 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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,94 @@ | ||
package suricata | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os/exec" | ||
|
||
"github.com/metal-stack/metal-networker/pkg/netconf" | ||
|
||
"github.com/ks2211/go-suricata/client" | ||
) | ||
|
||
const ( | ||
suricataService = "suricata.service" | ||
systemctlBin = "/bin/systemctl" | ||
|
||
// defaultSocket to communicate with suricata | ||
defaultSocket = "/run/suricata-command.socket" | ||
) | ||
|
||
type Suricata struct { | ||
socket string | ||
EnableIDS bool | ||
} | ||
|
||
type InterfaceStats map[string]InterFaceStat | ||
|
||
type InterFaceStat struct { | ||
Drop int | ||
InvalidChecksums int | ||
Pkts int | ||
} | ||
|
||
func New(enableIDS bool) *Suricata { | ||
return &Suricata{ | ||
socket: defaultSocket, | ||
EnableIDS: enableIDS, | ||
} | ||
} | ||
|
||
func (s *Suricata) InterfaceStats() (*InterfaceStats, error) { | ||
suricata, err := client.CreateSocket(s.socket) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer suricata.Close() | ||
|
||
ifaces, err := suricata.IFaceListCommand(context.Background()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
result := InterfaceStats{} | ||
for _, iface := range ifaces.Ifaces { | ||
stat, err := suricata.IFaceStatCommand(context.Background(), client.IFaceStatRequest{IFace: iface}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
result[iface] = InterFaceStat{ | ||
Drop: stat.Drop, | ||
InvalidChecksums: stat.InvalidChecksums, | ||
Pkts: stat.Pkts, | ||
} | ||
} | ||
|
||
return &result, nil | ||
} | ||
|
||
func (s *Suricata) ReconcileSuricata(kb netconf.KnowledgeBase, enableIDS bool) error { | ||
if enableIDS != s.EnableIDS { | ||
configurator := netconf.FirewallConfigurator{ | ||
CommonConfigurator: netconf.CommonConfigurator{ | ||
Kb: kb, | ||
}, | ||
EnableIDS: enableIDS, | ||
} | ||
configurator.ConfigureSuricata() | ||
|
||
if err := s.restart(); err != nil { | ||
return fmt.Errorf("failed to restart suricata: %w", err) | ||
} | ||
s.EnableIDS = enableIDS | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *Suricata) restart() error { | ||
c := exec.Command(systemctlBin, "restart", suricataService) | ||
err := c.Run() | ||
if err != nil { | ||
return fmt.Errorf("could not reload suricata service, err: %w", err) | ||
} | ||
return nil | ||
} |