feat(env): clean env variable values

- Remove surrounding spaces
- Remove suffix new line characters
This commit is contained in:
Quentin McGaw
2022-05-27 17:22:50 +00:00
parent 7fd45cf17f
commit 364f9de756
18 changed files with 51 additions and 50 deletions

View File

@@ -2,14 +2,13 @@ package env
import ( import (
"fmt" "fmt"
"os"
"time" "time"
"github.com/qdm12/gluetun/internal/configuration/settings" "github.com/qdm12/gluetun/internal/configuration/settings"
) )
func (r *Reader) ReadHealth() (health settings.Health, err error) { func (r *Reader) ReadHealth() (health settings.Health, err error) {
health.ServerAddress = os.Getenv("HEALTH_SERVER_ADDRESS") health.ServerAddress = getCleanedEnv("HEALTH_SERVER_ADDRESS")
_, health.TargetAddress = r.getEnvWithRetro("HEALTH_TARGET_ADDRESS", "HEALTH_ADDRESS_TO_PING") _, health.TargetAddress = r.getEnvWithRetro("HEALTH_TARGET_ADDRESS", "HEALTH_ADDRESS_TO_PING")
health.VPN.Initial, err = r.readDurationWithRetro( health.VPN.Initial, err = r.readDurationWithRetro(

View File

@@ -12,8 +12,18 @@ import (
"github.com/qdm12/govalid/integer" "github.com/qdm12/govalid/integer"
) )
// getCleanedEnv returns an environment variable value with
// surrounding spaces and trailing new line characters removed.
func getCleanedEnv(envKey string) (value string) {
value = os.Getenv(envKey)
value = strings.TrimSpace(value)
value = strings.TrimSuffix(value, "\r\n")
value = strings.TrimSuffix(value, "\n")
return value
}
func envToCSV(envKey string) (values []string) { func envToCSV(envKey string) (values []string) {
csv := os.Getenv(envKey) csv := getCleanedEnv(envKey)
if csv == "" { if csv == "" {
return nil return nil
} }
@@ -21,7 +31,7 @@ func envToCSV(envKey string) (values []string) {
} }
func envToInt(envKey string) (n int, err error) { func envToInt(envKey string) (n int, err error) {
s := os.Getenv(envKey) s := getCleanedEnv(envKey)
if s == "" { if s == "" {
return 0, nil return 0, nil
} }
@@ -29,7 +39,7 @@ func envToInt(envKey string) (n int, err error) {
} }
func envToStringPtr(envKey string) (stringPtr *string) { func envToStringPtr(envKey string) (stringPtr *string) {
s := os.Getenv(envKey) s := getCleanedEnv(envKey)
if s == "" { if s == "" {
return nil return nil
} }
@@ -37,7 +47,7 @@ func envToStringPtr(envKey string) (stringPtr *string) {
} }
func envToBoolPtr(envKey string) (boolPtr *bool, err error) { func envToBoolPtr(envKey string) (boolPtr *bool, err error) {
s := os.Getenv(envKey) s := getCleanedEnv(envKey)
if s == "" { if s == "" {
return nil, nil //nolint:nilnil return nil, nil //nolint:nilnil
} }
@@ -49,7 +59,7 @@ func envToBoolPtr(envKey string) (boolPtr *bool, err error) {
} }
func envToIntPtr(envKey string) (intPtr *int, err error) { func envToIntPtr(envKey string) (intPtr *int, err error) {
s := os.Getenv(envKey) s := getCleanedEnv(envKey)
if s == "" { if s == "" {
return nil, nil //nolint:nilnil return nil, nil //nolint:nilnil
} }
@@ -61,7 +71,7 @@ func envToIntPtr(envKey string) (intPtr *int, err error) {
} }
func envToUint8Ptr(envKey string) (uint8Ptr *uint8, err error) { func envToUint8Ptr(envKey string) (uint8Ptr *uint8, err error) {
s := os.Getenv(envKey) s := getCleanedEnv(envKey)
if s == "" { if s == "" {
return nil, nil //nolint:nilnil return nil, nil //nolint:nilnil
} }
@@ -78,7 +88,7 @@ func envToUint8Ptr(envKey string) (uint8Ptr *uint8, err error) {
} }
func envToUint16Ptr(envKey string) (uint16Ptr *uint16, err error) { func envToUint16Ptr(envKey string) (uint16Ptr *uint16, err error) {
s := os.Getenv(envKey) s := getCleanedEnv(envKey)
if s == "" { if s == "" {
return nil, nil //nolint:nilnil return nil, nil //nolint:nilnil
} }
@@ -95,7 +105,7 @@ func envToUint16Ptr(envKey string) (uint16Ptr *uint16, err error) {
} }
func envToDurationPtr(envKey string) (durationPtr *time.Duration, err error) { func envToDurationPtr(envKey string) (durationPtr *time.Duration, err error) {
s := os.Getenv(envKey) s := getCleanedEnv(envKey)
if s == "" { if s == "" {
return nil, nil //nolint:nilnil return nil, nil //nolint:nilnil
} }

View File

@@ -20,7 +20,3 @@ func setTestEnv(t *testing.T, key, value string) {
}) })
require.NoError(t, err) require.NoError(t, err)
} }
func TestXxx(t *testing.T) {
t.Log(int(^uint32(0)))
}

View File

@@ -3,7 +3,6 @@ package env
import ( import (
"errors" "errors"
"fmt" "fmt"
"os"
"strings" "strings"
"github.com/qdm12/gluetun/internal/configuration/settings" "github.com/qdm12/gluetun/internal/configuration/settings"
@@ -20,7 +19,7 @@ func readLog() (log settings.Log, err error) {
} }
func readLogLevel() (level *log.Level, err error) { func readLogLevel() (level *log.Level, err error) {
s := os.Getenv("LOG_LEVEL") s := getCleanedEnv("LOG_LEVEL")
if s == "" { if s == "" {
return nil, nil //nolint:nilnil return nil, nil //nolint:nilnil
} }

View File

@@ -2,7 +2,6 @@ package env
import ( import (
"fmt" "fmt"
"os"
"strings" "strings"
"github.com/qdm12/gluetun/internal/configuration/settings" "github.com/qdm12/gluetun/internal/configuration/settings"
@@ -15,10 +14,10 @@ func (r *Reader) readOpenVPN() (
err = unsetEnvKeys([]string{"OPENVPN_CLIENTKEY", "OPENVPN_CLIENTCRT"}, err) err = unsetEnvKeys([]string{"OPENVPN_CLIENTKEY", "OPENVPN_CLIENTCRT"}, err)
}() }()
openVPN.Version = os.Getenv("OPENVPN_VERSION") openVPN.Version = getCleanedEnv("OPENVPN_VERSION")
openVPN.User = r.readOpenVPNUser() openVPN.User = r.readOpenVPNUser()
openVPN.Password = r.readOpenVPNPassword() openVPN.Password = r.readOpenVPNPassword()
confFile := os.Getenv("OPENVPN_CUSTOM_CONFIG") confFile := getCleanedEnv("OPENVPN_CUSTOM_CONFIG")
if confFile != "" { if confFile != "" {
openVPN.ConfFile = &confFile openVPN.ConfFile = &confFile
} }
@@ -26,7 +25,7 @@ func (r *Reader) readOpenVPN() (
ciphersKey, _ := r.getEnvWithRetro("OPENVPN_CIPHERS", "OPENVPN_CIPHER") ciphersKey, _ := r.getEnvWithRetro("OPENVPN_CIPHERS", "OPENVPN_CIPHER")
openVPN.Ciphers = envToCSV(ciphersKey) openVPN.Ciphers = envToCSV(ciphersKey)
auth := os.Getenv("OPENVPN_AUTH") auth := getCleanedEnv("OPENVPN_AUTH")
if auth != "" { if auth != "" {
openVPN.Auth = &auth openVPN.Auth = &auth
} }
@@ -65,7 +64,7 @@ func (r *Reader) readOpenVPN() (
return openVPN, fmt.Errorf("environment variable OPENVPN_VERBOSITY: %w", err) return openVPN, fmt.Errorf("environment variable OPENVPN_VERBOSITY: %w", err)
} }
flagsStr := os.Getenv("OPENVPN_FLAGS") flagsStr := getCleanedEnv("OPENVPN_FLAGS")
if flagsStr != "" { if flagsStr != "" {
openVPN.Flags = strings.Fields(flagsStr) openVPN.Flags = strings.Fields(flagsStr)
} }
@@ -85,7 +84,7 @@ func (r *Reader) readOpenVPNPassword() (password string) {
} }
func readBase64OrNil(envKey string) (valueOrNil *string, err error) { func readBase64OrNil(envKey string) (valueOrNil *string, err error) {
value := os.Getenv(envKey) value := getCleanedEnv(envKey)
if value == "" { if value == "" {
return nil, nil //nolint:nilnil return nil, nil //nolint:nilnil
} }

View File

@@ -3,7 +3,6 @@ package env
import ( import (
"errors" "errors"
"fmt" "fmt"
"os"
"strings" "strings"
"github.com/qdm12/gluetun/internal/configuration/settings" "github.com/qdm12/gluetun/internal/configuration/settings"
@@ -13,7 +12,7 @@ import (
func (r *Reader) readOpenVPNSelection() ( func (r *Reader) readOpenVPNSelection() (
selection settings.OpenVPNSelection, err error) { selection settings.OpenVPNSelection, err error) {
confFile := os.Getenv("OPENVPN_CUSTOM_CONFIG") confFile := getCleanedEnv("OPENVPN_CUSTOM_CONFIG")
if confFile != "" { if confFile != "" {
selection.ConfFile = &confFile selection.ConfFile = &confFile
} }

View File

@@ -2,7 +2,6 @@ package env
import ( import (
"fmt" "fmt"
"os"
"github.com/qdm12/gluetun/internal/pprof" "github.com/qdm12/gluetun/internal/pprof"
) )
@@ -23,7 +22,7 @@ func readPprof() (settings pprof.Settings, err error) {
return settings, fmt.Errorf("environment variable PPROF_MUTEX_PROFILE_RATE: %w", err) return settings, fmt.Errorf("environment variable PPROF_MUTEX_PROFILE_RATE: %w", err)
} }
settings.HTTPServer.Address = os.Getenv("PPROF_HTTP_SERVER_ADDRESS") settings.HTTPServer.Address = getCleanedEnv("PPROF_HTTP_SERVER_ADDRESS")
return settings, nil return settings, nil
} }

View File

@@ -2,7 +2,6 @@ package env
import ( import (
"fmt" "fmt"
"os"
"strings" "strings"
"github.com/qdm12/gluetun/internal/configuration/settings" "github.com/qdm12/gluetun/internal/configuration/settings"
@@ -35,7 +34,7 @@ func (r *Reader) readVPNServiceProvider(vpnType string) (vpnProviderPtr *string)
s = strings.ToLower(s) s = strings.ToLower(s)
switch { switch {
case vpnType != vpn.Wireguard && case vpnType != vpn.Wireguard &&
os.Getenv("OPENVPN_CUSTOM_CONFIG") != "": // retro compatibility getCleanedEnv("OPENVPN_CUSTOM_CONFIG") != "": // retro compatibility
return stringPtr(providers.Custom) return stringPtr(providers.Custom)
case s == "": case s == "":
return nil return nil

View File

@@ -2,7 +2,6 @@ package env
import ( import (
"fmt" "fmt"
"os"
"time" "time"
"github.com/qdm12/gluetun/internal/configuration/settings" "github.com/qdm12/gluetun/internal/configuration/settings"
@@ -20,7 +19,7 @@ func (r *Reader) readPublicIP() (publicIP settings.PublicIP, err error) {
} }
func readPublicIPPeriod() (period *time.Duration, err error) { func readPublicIPPeriod() (period *time.Duration, err error) {
s := os.Getenv("PUBLICIP_PERIOD") s := getCleanedEnv("PUBLICIP_PERIOD")
if s == "" { if s == "" {
return nil, nil //nolint:nilnil return nil, nil //nolint:nilnil
} }

View File

@@ -1,8 +1,6 @@
package env package env
import ( import (
"os"
"github.com/qdm12/gluetun/internal/configuration/settings" "github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/configuration/sources" "github.com/qdm12/gluetun/internal/configuration/sources"
) )
@@ -111,12 +109,12 @@ func (r *Reader) getEnvWithRetro(currentKey string,
// We check retro-compatibility keys first since // We check retro-compatibility keys first since
// the current key might be set in the Dockerfile. // the current key might be set in the Dockerfile.
for _, key = range retroKeys { for _, key = range retroKeys {
value = os.Getenv(key) value = getCleanedEnv(key)
if value != "" { if value != "" {
r.onRetroActive(key, currentKey) r.onRetroActive(key, currentKey)
return key, value return key, value
} }
} }
return currentKey, os.Getenv(currentKey) return currentKey, getCleanedEnv(currentKey)
} }

View File

@@ -2,7 +2,6 @@ package env
import ( import (
"fmt" "fmt"
"os"
"github.com/qdm12/gluetun/internal/configuration/settings" "github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/govalid/binary" "github.com/qdm12/govalid/binary"
@@ -20,7 +19,7 @@ func (r *Reader) readControlServer() (controlServer settings.ControlServer, err
} }
func readControlServerLog() (enabled *bool, err error) { func readControlServerLog() (enabled *bool, err error) {
s := os.Getenv("HTTP_CONTROL_SERVER_LOG") s := getCleanedEnv("HTTP_CONTROL_SERVER_LOG")
if s == "" { if s == "" {
return nil, nil //nolint:nilnil return nil, nil //nolint:nilnil
} }

View File

@@ -4,7 +4,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"net" "net"
"os"
"strconv" "strconv"
"strings" "strings"
@@ -49,7 +48,7 @@ func (r *Reader) readServerSelection(vpnProvider, vpnType string) (
serverNamesKey, _ := r.getEnvWithRetro("SERVER_NAMES", "SERVER_NAME") serverNamesKey, _ := r.getEnvWithRetro("SERVER_NAMES", "SERVER_NAME")
ss.Names = envToCSV(serverNamesKey) ss.Names = envToCSV(serverNamesKey)
if csv := os.Getenv("SERVER_NUMBER"); csv != "" { if csv := getCleanedEnv("SERVER_NUMBER"); csv != "" {
numbersStrings := strings.Split(csv, ",") numbersStrings := strings.Split(csv, ",")
numbers := make([]uint16, len(numbersStrings)) numbers := make([]uint16, len(numbersStrings))
for i, numberString := range numbersStrings { for i, numberString := range numbersStrings {

View File

@@ -3,7 +3,6 @@ package env
import ( import (
"errors" "errors"
"fmt" "fmt"
"os"
"strconv" "strconv"
"github.com/qdm12/gluetun/internal/configuration/settings" "github.com/qdm12/gluetun/internal/configuration/settings"
@@ -26,7 +25,7 @@ func (r *Reader) readSystem() (system settings.System, err error) {
return system, err return system, err
} }
system.Timezone = os.Getenv("TZ") system.Timezone = getCleanedEnv("TZ")
return system, nil return system, nil
} }

View File

@@ -3,7 +3,6 @@ package env
import ( import (
"fmt" "fmt"
"net" "net"
"os"
"time" "time"
"github.com/qdm12/gluetun/internal/configuration/settings" "github.com/qdm12/gluetun/internal/configuration/settings"
@@ -26,7 +25,7 @@ func readUpdater() (updater settings.Updater, err error) {
} }
func readUpdaterPeriod() (period *time.Duration, err error) { func readUpdaterPeriod() (period *time.Duration, err error) {
s := os.Getenv("UPDATER_PERIOD") s := getCleanedEnv("UPDATER_PERIOD")
if s == "" { if s == "" {
return nil, nil //nolint:nilnil return nil, nil //nolint:nilnil
} }

View File

@@ -2,7 +2,6 @@ package env
import ( import (
"fmt" "fmt"
"os"
"github.com/qdm12/gluetun/internal/configuration/settings" "github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/govalid/binary" "github.com/qdm12/govalid/binary"
@@ -18,7 +17,7 @@ func readVersion() (version settings.Version, err error) {
} }
func readVersionEnabled() (enabled *bool, err error) { func readVersionEnabled() (enabled *bool, err error) {
s := os.Getenv("VERSION_INFORMATION") s := getCleanedEnv("VERSION_INFORMATION")
if s == "" { if s == "" {
return nil, nil //nolint:nilnil return nil, nil //nolint:nilnil
} }

View File

@@ -2,14 +2,13 @@ package env
import ( import (
"fmt" "fmt"
"os"
"strings" "strings"
"github.com/qdm12/gluetun/internal/configuration/settings" "github.com/qdm12/gluetun/internal/configuration/settings"
) )
func (r *Reader) readVPN() (vpn settings.VPN, err error) { func (r *Reader) readVPN() (vpn settings.VPN, err error) {
vpn.Type = strings.ToLower(os.Getenv("VPN_TYPE")) vpn.Type = strings.ToLower(getCleanedEnv("VPN_TYPE"))
vpn.Provider, err = r.readProvider(vpn.Type) vpn.Provider, err = r.readProvider(vpn.Type)
if err != nil { if err != nil {

View File

@@ -4,7 +4,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"net" "net"
"os"
"github.com/qdm12/gluetun/internal/configuration/settings" "github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/govalid/port" "github.com/qdm12/govalid/port"
@@ -22,7 +21,7 @@ func (r *Reader) readWireguardSelection() (
return selection, err return selection, err
} }
selection.PublicKey = os.Getenv("WIREGUARD_PUBLIC_KEY") selection.PublicKey = getCleanedEnv("WIREGUARD_PUBLIC_KEY")
return selection, nil return selection, nil
} }

View File

@@ -2,13 +2,24 @@ package secrets
import ( import (
"os" "os"
"strings"
"github.com/qdm12/gluetun/internal/configuration/sources/files" "github.com/qdm12/gluetun/internal/configuration/sources/files"
) )
// getCleanedEnv returns an environment variable value with
// surrounding spaces and trailing new line characters removed.
func getCleanedEnv(envKey string) (value string) {
value = os.Getenv(envKey)
value = strings.TrimSpace(value)
value = strings.TrimSuffix(value, "\r\n")
value = strings.TrimSuffix(value, "\n")
return value
}
func readSecretFileAsStringPtr(secretPathEnvKey, defaultSecretPath string) ( func readSecretFileAsStringPtr(secretPathEnvKey, defaultSecretPath string) (
stringPtr *string, err error) { stringPtr *string, err error) {
path := os.Getenv(secretPathEnvKey) path := getCleanedEnv(secretPathEnvKey)
if path == "" { if path == "" {
path = defaultSecretPath path = defaultSecretPath
} }
@@ -17,7 +28,7 @@ func readSecretFileAsStringPtr(secretPathEnvKey, defaultSecretPath string) (
func readSecretFileAsString(secretPathEnvKey, defaultSecretPath string) ( func readSecretFileAsString(secretPathEnvKey, defaultSecretPath string) (
s string, err error) { s string, err error) {
path := os.Getenv(secretPathEnvKey) path := getCleanedEnv(secretPathEnvKey)
if path == "" { if path == "" {
path = defaultSecretPath path = defaultSecretPath
} }