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(settings): load wireguard inidivual fields as secret files #1348

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,13 @@ ENV VPN_SERVICE_PROVIDER=pia \
# Wireguard
WIREGUARD_CONF_SECRETFILE=/run/secrets/wg0.conf \
WIREGUARD_PRIVATE_KEY= \
WIREGUARD_PRIVATE_KEY_SECRETFILE=/run/secrets/wireguard_private_key \
WIREGUARD_PRESHARED_KEY= \
WIREGUARD_PRESHARED_KEY_SECRETFILE=/run/secrets/wireguard_preshared_key \
WIREGUARD_PUBLIC_KEY= \
WIREGUARD_ALLOWED_IPS= \
WIREGUARD_ADDRESSES= \
WIREGUARD_ADDRESSES_SECRETFILE=/run/secrets/wireguard_addresses \
WIREGUARD_MTU=1400 \
WIREGUARD_IMPLEMENTATION=auto \
# VPN server filtering
Expand Down
21 changes: 21 additions & 0 deletions internal/configuration/sources/secrets/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package secrets

import (
"fmt"
"net/netip"
"strings"

"github.com/qdm12/gluetun/internal/configuration/sources/files"
"github.com/qdm12/gluetun/internal/openvpn/extract"
Expand Down Expand Up @@ -35,3 +37,22 @@ func (s *Source) readPEMSecretFile(secretPathEnvKey, defaultSecretPath string) (

return &base64Data, nil
}

func parseAddresses(addressesCSV string) (addresses []netip.Prefix, err error) {
if addressesCSV == "" {
return nil, nil
}

addressStrings := strings.Split(addressesCSV, ",")
addresses = make([]netip.Prefix, len(addressStrings))
for i, addressString := range addressStrings {
addressString = strings.TrimSpace(addressString)
addresses[i], err = netip.ParsePrefix(addressString)
if err != nil {
return nil, fmt.Errorf("parsing address %d of %d: %w",
i+1, len(addressStrings), err)
}
}

return addresses, nil
}
5 changes: 5 additions & 0 deletions internal/configuration/sources/secrets/vpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@ func (s *Source) readVPN() (vpn settings.VPN, err error) {
return vpn, fmt.Errorf("reading OpenVPN settings: %w", err)
}

vpn.Wireguard, err = s.readWireguard()
if err != nil {
return vpn, fmt.Errorf("reading Wireguard settings: %w", err)
}

return vpn, nil
}
31 changes: 31 additions & 0 deletions internal/configuration/sources/secrets/wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,38 @@ func (s *Source) readWireguard() (settings settings.Wireguard, err error) {
if err != nil {
return settings, fmt.Errorf("reading Wireguard conf secret file: %w", err)
} else if wireguardConf != nil {
// Wireguard ini config file takes precedence over individual secrets
return files.ParseWireguardConf([]byte(*wireguardConf))
}

settings.PrivateKey, err = s.readSecretFileAsStringPtr(
"WIREGUARD_PRIVATE_KEY_SECRETFILE",
"/run/secrets/wireguard_private_key",
)
if err != nil {
return settings, fmt.Errorf("reading private key file: %w", err)
}

settings.PreSharedKey, err = s.readSecretFileAsStringPtr(
"WIREGUARD_PRESHARED_KEY_SECRETFILE",
"/run/secrets/wireguard_preshared_key",
)
if err != nil {
return settings, fmt.Errorf("reading preshared key file: %w", err)
}

wireguardAddressesCSV, err := s.readSecretFileAsStringPtr(
"WIREGUARD_ADDRESSES_SECRETFILE",
"/run/secrets/wireguard_addresses",
)
if err != nil {
return settings, fmt.Errorf("reading addresses file: %w", err)
} else if wireguardAddressesCSV != nil {
settings.Addresses, err = parseAddresses(*wireguardAddressesCSV)
if err != nil {
return settings, fmt.Errorf("parsing addresses: %w", err)
}
}

return settings, nil
}