Skip to content
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

feat(systemd): systemd improvements #133

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
15 changes: 10 additions & 5 deletions contrib/coraza-spoa.service
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
[Unit]
Description=Coraza WAF SPOA Daemon
Documentation=https://www.coraza.io
After=network.target

[Service]
ExecStart=/usr/bin/coraza-spoa -config=/etc/coraza-spoa/config.yaml
WorkingDirectory=/
ExecReload=/bin/kill -HUP $MAINPID
WorkingDirectory=/etc/coraza-spoa/
Restart=always
Type=exec
Type=notify
# notify-reload is pretty new and isn't available on debian 12
#Type=notify-reload
NotifyAccess=all
User=coraza-spoa
Group=coraza-spoa

Expand Down Expand Up @@ -45,7 +50,7 @@ InaccessiblePaths=-/opt
InaccessiblePaths=-/srv
#InaccessiblePaths=-/bin
InaccessiblePaths=-/bin/bash
inaccessiblepaths=-/bin/find
InaccessiblePaths=-/bin/find
DavidProdinger marked this conversation as resolved.
Show resolved Hide resolved
InaccessiblePaths=-/bin/less
InaccessiblePaths=-/bin/zcat
InaccessiblePaths=-/bin/rm
Expand Down Expand Up @@ -105,7 +110,7 @@ InaccessiblePaths=-/usr/bin/htop
InaccessiblePaths=-/usr/bin/ipcmk
InaccessiblePaths=-/usr/bin/journalctl
InaccessiblePaths=-/usr/bin/keyctl
InaccessiblePaths=-/usr/bin/kill
# InaccessiblePaths=-/usr/bin/kill
InaccessiblePaths=-/usr/bin/killall
InaccessiblePaths=-/usr/bin/ksh
InaccessiblePaths=-/usr/bin/last
Expand Down Expand Up @@ -293,7 +298,7 @@ PrivateTmp=true

RemoveIPC=true

RestrictAddressFamilies=AF_INET AF_INET6
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
#RestrictNamespaces=uts ipc pid user cgroup

SystemCallArchitectures=native
Expand Down
44 changes: 44 additions & 0 deletions internal/daemon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package internal

DavidProdinger marked this conversation as resolved.
Show resolved Hide resolved
import (
"net"
"os"
"strconv"
"time"
)

func SdNotify(message string) error {
socketAddr := &net.UnixAddr{
Name: os.Getenv("NOTIFY_SOCKET"),
Net: "unixgram",
}

if socketAddr.Name == "" {
return nil
}

conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr)
if err != nil {
return err
}
defer conn.Close()

if _, err = conn.Write([]byte(message)); err != nil {
return err
}

return nil
}

func SdNotifyReady() error {
return SdNotify("READY=1")
}

func SdNotifyReloading() error {
microseconds := time.Now().UnixMicro()
return SdNotify("RELOADING=1\nMONOTONIC_USEC=" + strconv.FormatInt(microseconds, 10))
DavidProdinger marked this conversation as resolved.
Show resolved Hide resolved
}

func SdNotifyStopping() error {
return SdNotify("STOPPING=1")
}
23 changes: 23 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ func main() {
defer cancelFunc()

globalLogger.Info().Msg("Starting coraza-spoa")

DavidProdinger marked this conversation as resolved.
Show resolved Hide resolved
err := internal.SdNotifyReady()
if err != nil {
globalLogger.Error().Err(err).Msg("Failed notifying daemon")
}

if err := a.Serve(l); err != nil {
globalLogger.Fatal().Err(err).Msg("listener closed")
}
Expand Down Expand Up @@ -126,11 +132,28 @@ outer:
continue
}

err = internal.SdNotifyReloading()
if err != nil {
globalLogger.Error().Err(err).Msg("Failed notifying daemon")
}

a.ReplaceApplications(apps)
cfg = newCfg

err = internal.SdNotifyReady()
if err != nil {
globalLogger.Error().Err(err).Msg("Failed notifying daemon")
}
}
}

globalLogger.Info().Msg("Stopping coraza-spoa")

err = internal.SdNotifyStopping()
if err != nil {
globalLogger.Error().Err(err).Msg("Failed notifying daemon")
}

if memProfile != "" {
f, err := os.Create(memProfile)
if err != nil {
Expand Down