mirror of
https://github.com/h44z/wg-portal.git
synced 2026-11-20 20:53:40 +01:00
feat(api): crud for interface backend settings and create default peer flag (#765)
This commit is contained in:
@@ -3,6 +3,7 @@ package backend
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"slices"
|
||||
|
||||
"github.com/h44z/wg-portal/internal/config"
|
||||
"github.com/h44z/wg-portal/internal/domain"
|
||||
@@ -79,6 +80,10 @@ func (s InterfaceService) Create(ctx context.Context, iface *domain.Interface) (
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := s.validateBackend(iface.Backend); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
createdInterface, err := s.interfaces.CreateInterface(ctx, iface)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -101,6 +106,19 @@ func (s InterfaceService) Update(ctx context.Context, id domain.InterfaceIdentif
|
||||
iface.Identifier, id, domain.ErrInvalidData)
|
||||
}
|
||||
|
||||
existingInterface, _, err := s.interfaces.GetInterfaceAndPeers(ctx, id)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if err := s.validateBackend(iface.Backend); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
// These fields are managed by the server and cannot be changed through v1.
|
||||
iface.IsDynamic = existingInterface.IsDynamic
|
||||
iface.DriverType = existingInterface.DriverType
|
||||
iface.LdapAllowedUsers = existingInterface.LdapAllowedUsers
|
||||
|
||||
updatedInterface, updatedPeers, err := s.interfaces.UpdateInterface(ctx, iface)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
@@ -109,6 +127,22 @@ func (s InterfaceService) Update(ctx context.Context, id domain.InterfaceIdentif
|
||||
return updatedInterface, updatedPeers, nil
|
||||
}
|
||||
|
||||
// validateBackend validates against configured IDs so a typo cannot silently use
|
||||
// the controller manager's fallback to the local backend.
|
||||
func (s InterfaceService) validateBackend(backend domain.InterfaceBackend) error {
|
||||
id := string(backend)
|
||||
|
||||
// Empty is the legacy representation of the local backend in domain objects.
|
||||
if id == "" || id == config.LocalBackendName ||
|
||||
slices.ContainsFunc(s.cfg.Backend.Mikrotik, func(b config.BackendMikrotik) bool { return b.Id == id }) ||
|
||||
slices.ContainsFunc(s.cfg.Backend.Pfsense, func(b config.BackendPfsense) bool { return b.Id == id }) ||
|
||||
slices.ContainsFunc(s.cfg.Backend.Opnsense, func(b config.BackendOpnsense) bool { return b.Id == id }) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("unknown interface backend %q: %w", id, domain.ErrInvalidData)
|
||||
}
|
||||
|
||||
func (s InterfaceService) Delete(ctx context.Context, id domain.InterfaceIdentifier) error {
|
||||
if err := domain.ValidateAdminAccessRights(ctx); err != nil {
|
||||
return err
|
||||
|
||||
@@ -2,6 +2,7 @@ package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-pkgz/routegroup"
|
||||
@@ -146,6 +147,7 @@ func (e InterfaceEndpoint) handlePrepareGet() http.HandlerFunc {
|
||||
// @Tags Interfaces
|
||||
// @Summary Create a new interface record.
|
||||
// @Description This endpoint creates a new interface with the provided data. All required fields must be filled (e.g. name, private key, public key, ...).
|
||||
// @Description Backend must be a configured backend ID. When omitted, the local backend is used.
|
||||
// @Param request body models.Interface true "The interface data."
|
||||
// @Produce json
|
||||
// @Success 200 {object} models.Interface
|
||||
@@ -163,7 +165,7 @@ func (e InterfaceEndpoint) handleCreatePost() http.HandlerFunc {
|
||||
respond.JSON(w, http.StatusBadRequest, models.Error{Code: http.StatusBadRequest, Message: err.Error()})
|
||||
return
|
||||
}
|
||||
if err := e.validator.Struct(iface); err != nil {
|
||||
if err := e.validateInterfaceInput(&iface); err != nil {
|
||||
respond.JSON(w, http.StatusBadRequest, models.Error{Code: http.StatusBadRequest, Message: err.Error()})
|
||||
return
|
||||
}
|
||||
@@ -185,6 +187,7 @@ func (e InterfaceEndpoint) handleCreatePost() http.HandlerFunc {
|
||||
// @Tags Interfaces
|
||||
// @Summary Update an interface record.
|
||||
// @Description This endpoint updates an existing interface with the provided data. All required fields must be filled (e.g. name, private key, public key, ...).
|
||||
// @Description Backend must be a configured backend ID. Omitted Backend and CreateDefaultPeer fields preserve their existing values. Server-managed metadata is preserved.
|
||||
// @Param id path string true "The interface identifier."
|
||||
// @Param request body models.Interface true "The interface data."
|
||||
// @Produce json
|
||||
@@ -210,7 +213,7 @@ func (e InterfaceEndpoint) handleUpdatePut() http.HandlerFunc {
|
||||
respond.JSON(w, http.StatusBadRequest, models.Error{Code: http.StatusBadRequest, Message: err.Error()})
|
||||
return
|
||||
}
|
||||
if err := e.validator.Struct(iface); err != nil {
|
||||
if err := e.validateInterfaceInput(&iface); err != nil {
|
||||
respond.JSON(w, http.StatusBadRequest, models.Error{Code: http.StatusBadRequest, Message: err.Error()})
|
||||
return
|
||||
}
|
||||
@@ -221,10 +224,26 @@ func (e InterfaceEndpoint) handleUpdatePut() http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
domainInterface := models.NewDomainInterface(&iface)
|
||||
if iface.Backend == nil || iface.CreateDefaultPeer == nil {
|
||||
existing, _, err := e.interfaces.GetById(r.Context(), domain.InterfaceIdentifier(id))
|
||||
if err != nil {
|
||||
status, model := ParseServiceError(err)
|
||||
respond.JSON(w, status, model)
|
||||
return
|
||||
}
|
||||
if iface.Backend == nil {
|
||||
domainInterface.Backend = existing.Backend
|
||||
}
|
||||
if iface.CreateDefaultPeer == nil {
|
||||
domainInterface.CreateDefaultPeer = existing.CreateDefaultPeer
|
||||
}
|
||||
}
|
||||
|
||||
updatedInterface, updatedInterfacePeers, err := e.interfaces.Update(
|
||||
r.Context(),
|
||||
domain.InterfaceIdentifier(id),
|
||||
models.NewDomainInterface(&iface),
|
||||
domainInterface,
|
||||
)
|
||||
if err != nil {
|
||||
status, model := ParseServiceError(err)
|
||||
@@ -236,6 +255,13 @@ func (e InterfaceEndpoint) handleUpdatePut() http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func (e InterfaceEndpoint) validateInterfaceInput(iface *models.Interface) error {
|
||||
if iface.Backend != nil && *iface.Backend == "" {
|
||||
return errors.New("interface backend must not be empty")
|
||||
}
|
||||
return e.validator.Struct(iface)
|
||||
}
|
||||
|
||||
// handleDelete returns a gorm handler function.
|
||||
//
|
||||
// @ID interfaces_handleDelete
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/h44z/wg-portal/internal"
|
||||
"github.com/h44z/wg-portal/internal/config"
|
||||
"github.com/h44z/wg-portal/internal/domain"
|
||||
)
|
||||
|
||||
@@ -15,6 +16,10 @@ type Interface struct {
|
||||
DisplayName string `json:"DisplayName" binding:"omitempty,max=64" example:"My Interface"`
|
||||
// Mode is the interface type, either 'server', 'client' or 'any'. The mode specifies how WireGuard Portal handles peers for this interface.
|
||||
Mode string `json:"Mode" example:"server" binding:"required,oneof=server client any"`
|
||||
// Backend is the configured backend ID used to manage this interface. Omit it on create to use the local backend, or on update to preserve the current backend.
|
||||
Backend *string `json:"Backend,omitempty" example:"local" binding:"omitempty,min=1"`
|
||||
// CreateDefaultPeer enables default peer creation for this interface. Defaults to false when omitted on create; preserves the current setting when omitted on update.
|
||||
CreateDefaultPeer *bool `json:"CreateDefaultPeer,omitempty" example:"false"`
|
||||
// PrivateKey is the private key of the interface.
|
||||
PrivateKey string `json:"PrivateKey" example:"gI6EdUSYvn8ugXOt8QQD6Yc+JyiZxIhp3GInSWRfWGE=" binding:"required,len=44"`
|
||||
// PublicKey is the public key of the server interface. The public key is used by peers to connect to the server.
|
||||
@@ -90,10 +95,16 @@ type Interface struct {
|
||||
}
|
||||
|
||||
func NewInterface(src *domain.Interface, peers []domain.Peer) *Interface {
|
||||
backend := string(src.Backend)
|
||||
if backend == "" {
|
||||
backend = config.LocalBackendName
|
||||
}
|
||||
iface := &Interface{
|
||||
Identifier: string(src.Identifier),
|
||||
DisplayName: src.DisplayName,
|
||||
Mode: string(src.Type),
|
||||
Backend: &backend,
|
||||
CreateDefaultPeer: new(src.CreateDefaultPeer),
|
||||
PrivateKey: src.PrivateKey,
|
||||
PublicKey: src.PublicKey,
|
||||
Disabled: src.IsDisabled(),
|
||||
@@ -198,6 +209,12 @@ func NewDomainInterface(src *Interface) *domain.Interface {
|
||||
if src.Disabled {
|
||||
res.Disabled = &now
|
||||
}
|
||||
if src.Backend != nil {
|
||||
res.Backend = domain.InterfaceBackend(*src.Backend)
|
||||
}
|
||||
if src.CreateDefaultPeer != nil {
|
||||
res.CreateDefaultPeer = *src.CreateDefaultPeer
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user