fix: autosave wireguard conf files (#303)

* fix: autosave wireguard conf files

- Fix subscription to Interface and Peer updates topics
- Remove admin permissions validation
- Update file on peer deletion
- Change save condition to configured storage path only, as initialized interface is not nil

* Added  comment to peer config for prometheus exporter
This commit is contained in:
Dmytro Bondar 2024-09-22 11:53:42 +02:00 committed by GitHub
parent 605841f2a0
commit 2428dedc42
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 27 additions and 34 deletions

View File

@ -5,15 +5,16 @@ import (
"bytes"
"context"
"fmt"
"io"
"os"
"strings"
"github.com/h44z/wg-portal/internal/app"
"github.com/h44z/wg-portal/internal/config"
"github.com/h44z/wg-portal/internal/domain"
"github.com/sirupsen/logrus"
evbus "github.com/vardius/message-bus"
"github.com/yeqown/go-qrcode/v2"
"io"
"os"
"strings"
)
type Manager struct {
@ -21,7 +22,7 @@ type Manager struct {
bus evbus.MessageBus
tplHandler *TemplateHandler
fsRepo FileSystemRepo // can be nil if storing the configuration is disabled
fsRepo FileSystemRepo
users UserDatabaseRepo
wg WireguardDatabaseRepo
}
@ -42,18 +43,18 @@ func NewConfigFileManager(cfg *config.Config, bus evbus.MessageBus, users UserDa
wg: wg,
}
if m.cfg.Advanced.ConfigStoragePath != "" {
if err := m.createStorageDirectory(); err != nil {
return nil, err
}
m.connectToMessageBus()
}
return m, nil
}
func (m Manager) createStorageDirectory() error {
if m.cfg.Advanced.ConfigStoragePath == "" {
return nil // no storage path configured, skip initialization step
}
err := os.MkdirAll(m.cfg.Advanced.ConfigStoragePath, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create configuration storage path %s: %w",
@ -64,21 +65,17 @@ func (m Manager) createStorageDirectory() error {
}
func (m Manager) connectToMessageBus() {
if m.fsRepo == nil {
return // skip subscription
}
_ = m.bus.Subscribe(app.TopicInterfaceUpdated, m.handleInterfaceUpdatedEvent)
_ = m.bus.Subscribe(app.TopicPeerInterfaceUpdated, m.handleInterfaceUpdatedEvent)
_ = m.bus.Subscribe(app.TopicPeerInterfaceUpdated, m.handlePeerInterfaceUpdatedEvent)
}
func (m Manager) handleInterfaceUpdatedEvent(iface *domain.Interface) {
logrus.Errorf("handling interface updated event for %s", iface.Identifier)
if !iface.SaveConfig || m.fsRepo == nil {
if !iface.SaveConfig {
return
}
logrus.Debugf("handling interface updated event for %s", iface.Identifier)
err := m.PersistInterfaceConfig(context.Background(), iface.Identifier)
if err != nil {
logrus.Errorf("failed to automatically persist interface config for %s: %v", iface.Identifier, err)
@ -86,12 +83,6 @@ func (m Manager) handleInterfaceUpdatedEvent(iface *domain.Interface) {
}
func (m Manager) handlePeerInterfaceUpdatedEvent(id domain.InterfaceIdentifier) {
logrus.Errorf("handling interface updated event for %s", id)
if m.fsRepo == nil {
return
}
peerInterface, err := m.wg.GetInterface(context.Background(), id)
if err != nil {
logrus.Errorf("failed to load interface %s: %v", id, err)
@ -102,6 +93,8 @@ func (m Manager) handlePeerInterfaceUpdatedEvent(id domain.InterfaceIdentifier)
return
}
logrus.Debugf("handling peer interface updated event for %s", id)
err = m.PersistInterfaceConfig(context.Background(), peerInterface.Identifier)
if err != nil {
logrus.Errorf("failed to automatically persist interface config for %s: %v", peerInterface.Identifier, err)
@ -184,14 +177,6 @@ func (m Manager) GetPeerConfigQrCode(ctx context.Context, id domain.PeerIdentifi
}
func (m Manager) PersistInterfaceConfig(ctx context.Context, id domain.InterfaceIdentifier) error {
if err := domain.ValidateAdminAccessRights(ctx); err != nil {
return err
}
if m.fsRepo == nil {
return fmt.Errorf("peristing configuration is not supported")
}
iface, peers, err := m.wg.GetInterfaceAndPeers(ctx, id)
if err != nil {
return fmt.Errorf("failed to fetch interface %s: %w", id, err)

View File

@ -60,6 +60,8 @@ PostDown = {{ .Interface.PostDown }}
{{range .Peers}}
{{- if not .IsDisabled}}
[Peer]
{{/* `friendly_name` used by https://github.com/MindFlavor/prometheus_wireguard_exporter */ -}}
# friendly_name = {{ .DisplayName }}
# -WGP- Peer: {{.Identifier}}
# -WGP- Created: {{.CreatedAt}}
# -WGP- Updated: {{.UpdatedAt}}

View File

@ -4,11 +4,12 @@ import (
"context"
"errors"
"fmt"
"time"
"github.com/h44z/wg-portal/internal"
"github.com/h44z/wg-portal/internal/app"
"github.com/h44z/wg-portal/internal/domain"
"github.com/sirupsen/logrus"
"time"
)
func (m Manager) CreateDefaultPeer(ctx context.Context, userId domain.UserIdentifier) error {
@ -253,6 +254,11 @@ func (m Manager) DeletePeer(ctx context.Context, id domain.PeerIdentifier) error
return fmt.Errorf("failed to delete peer %s: %w", id, err)
}
// Update routes after peers have changed
m.bus.Publish(app.TopicRouteUpdate, "peers updated")
// Update interface after peers have changed
m.bus.Publish(app.TopicPeerInterfaceUpdated, peer.InterfaceIdentifier)
return nil
}