mirror of https://github.com/h44z/wg-portal.git
				
				
				
			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:
		
							parent
							
								
									605841f2a0
								
							
						
					
					
						commit
						2428dedc42
					
				|  | @ -5,15 +5,16 @@ import ( | ||||||
| 	"bytes" | 	"bytes" | ||||||
| 	"context" | 	"context" | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	"io" | ||||||
|  | 	"os" | ||||||
|  | 	"strings" | ||||||
|  | 
 | ||||||
| 	"github.com/h44z/wg-portal/internal/app" | 	"github.com/h44z/wg-portal/internal/app" | ||||||
| 	"github.com/h44z/wg-portal/internal/config" | 	"github.com/h44z/wg-portal/internal/config" | ||||||
| 	"github.com/h44z/wg-portal/internal/domain" | 	"github.com/h44z/wg-portal/internal/domain" | ||||||
| 	"github.com/sirupsen/logrus" | 	"github.com/sirupsen/logrus" | ||||||
| 	evbus "github.com/vardius/message-bus" | 	evbus "github.com/vardius/message-bus" | ||||||
| 	"github.com/yeqown/go-qrcode/v2" | 	"github.com/yeqown/go-qrcode/v2" | ||||||
| 	"io" |  | ||||||
| 	"os" |  | ||||||
| 	"strings" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| type Manager struct { | type Manager struct { | ||||||
|  | @ -21,7 +22,7 @@ type Manager struct { | ||||||
| 	bus        evbus.MessageBus | 	bus        evbus.MessageBus | ||||||
| 	tplHandler *TemplateHandler | 	tplHandler *TemplateHandler | ||||||
| 
 | 
 | ||||||
| 	fsRepo FileSystemRepo // can be nil if storing the configuration is disabled
 | 	fsRepo FileSystemRepo | ||||||
| 	users  UserDatabaseRepo | 	users  UserDatabaseRepo | ||||||
| 	wg     WireguardDatabaseRepo | 	wg     WireguardDatabaseRepo | ||||||
| } | } | ||||||
|  | @ -42,18 +43,18 @@ func NewConfigFileManager(cfg *config.Config, bus evbus.MessageBus, users UserDa | ||||||
| 		wg:     wg, | 		wg:     wg, | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if err := m.createStorageDirectory(); err != nil { | 	if m.cfg.Advanced.ConfigStoragePath != "" { | ||||||
| 		return nil, err | 		if err := m.createStorageDirectory(); err != nil { | ||||||
|  | 			return nil, err | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		m.connectToMessageBus() | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	return m, nil | 	return m, nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (m Manager) createStorageDirectory() error { | 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) | 	err := os.MkdirAll(m.cfg.Advanced.ConfigStoragePath, os.ModePerm) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return fmt.Errorf("failed to create configuration storage path %s: %w", | 		return fmt.Errorf("failed to create configuration storage path %s: %w", | ||||||
|  | @ -64,21 +65,17 @@ func (m Manager) createStorageDirectory() error { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (m Manager) connectToMessageBus() { | func (m Manager) connectToMessageBus() { | ||||||
| 	if m.fsRepo == nil { |  | ||||||
| 		return // skip subscription
 |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	_ = m.bus.Subscribe(app.TopicInterfaceUpdated, m.handleInterfaceUpdatedEvent) | 	_ = 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) { | func (m Manager) handleInterfaceUpdatedEvent(iface *domain.Interface) { | ||||||
| 	logrus.Errorf("handling interface updated event for %s", iface.Identifier) | 	if !iface.SaveConfig { | ||||||
| 
 |  | ||||||
| 	if !iface.SaveConfig || m.fsRepo == nil { |  | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	logrus.Debugf("handling interface updated event for %s", iface.Identifier) | ||||||
|  | 
 | ||||||
| 	err := m.PersistInterfaceConfig(context.Background(), iface.Identifier) | 	err := m.PersistInterfaceConfig(context.Background(), iface.Identifier) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		logrus.Errorf("failed to automatically persist interface config for %s: %v", iface.Identifier, err) | 		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) { | 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) | 	peerInterface, err := m.wg.GetInterface(context.Background(), id) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		logrus.Errorf("failed to load interface %s: %v", id, err) | 		logrus.Errorf("failed to load interface %s: %v", id, err) | ||||||
|  | @ -102,6 +93,8 @@ func (m Manager) handlePeerInterfaceUpdatedEvent(id domain.InterfaceIdentifier) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	logrus.Debugf("handling peer interface updated event for %s", id) | ||||||
|  | 
 | ||||||
| 	err = m.PersistInterfaceConfig(context.Background(), peerInterface.Identifier) | 	err = m.PersistInterfaceConfig(context.Background(), peerInterface.Identifier) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		logrus.Errorf("failed to automatically persist interface config for %s: %v", peerInterface.Identifier, err) | 		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 { | 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) | 	iface, peers, err := m.wg.GetInterfaceAndPeers(ctx, id) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return fmt.Errorf("failed to fetch interface %s: %w", id, err) | 		return fmt.Errorf("failed to fetch interface %s: %w", id, err) | ||||||
|  |  | ||||||
|  | @ -60,6 +60,8 @@ PostDown = {{ .Interface.PostDown }} | ||||||
| {{range .Peers}} | {{range .Peers}} | ||||||
| {{- if not .IsDisabled}} | {{- if not .IsDisabled}} | ||||||
| [Peer] | [Peer] | ||||||
|  | {{/* `friendly_name` used by https://github.com/MindFlavor/prometheus_wireguard_exporter */ -}} | ||||||
|  | # friendly_name = {{ .DisplayName }} | ||||||
| # -WGP- Peer: {{.Identifier}} | # -WGP- Peer: {{.Identifier}} | ||||||
| # -WGP- Created: {{.CreatedAt}} | # -WGP- Created: {{.CreatedAt}} | ||||||
| # -WGP- Updated: {{.UpdatedAt}} | # -WGP- Updated: {{.UpdatedAt}} | ||||||
|  | @ -86,4 +88,4 @@ Endpoint = {{ .Endpoint.GetValue }} | ||||||
| PersistentKeepalive = {{ .PersistentKeepalive.GetValue  }} | PersistentKeepalive = {{ .PersistentKeepalive.GetValue  }} | ||||||
| {{- end}} | {{- end}} | ||||||
| {{- end}} | {{- end}} | ||||||
| {{end}} | {{end}} | ||||||
|  |  | ||||||
|  | @ -4,11 +4,12 @@ import ( | ||||||
| 	"context" | 	"context" | ||||||
| 	"errors" | 	"errors" | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	"time" | ||||||
|  | 
 | ||||||
| 	"github.com/h44z/wg-portal/internal" | 	"github.com/h44z/wg-portal/internal" | ||||||
| 	"github.com/h44z/wg-portal/internal/app" | 	"github.com/h44z/wg-portal/internal/app" | ||||||
| 	"github.com/h44z/wg-portal/internal/domain" | 	"github.com/h44z/wg-portal/internal/domain" | ||||||
| 	"github.com/sirupsen/logrus" | 	"github.com/sirupsen/logrus" | ||||||
| 	"time" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func (m Manager) CreateDefaultPeer(ctx context.Context, userId domain.UserIdentifier) error { | 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) | 		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 | 	return nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue