diff --git a/providers/dns/acmedns/acmedns.go b/providers/dns/acmedns/acmedns.go index 48bf4326..5dbdd2fd 100644 --- a/providers/dns/acmedns/acmedns.go +++ b/providers/dns/acmedns/acmedns.go @@ -14,12 +14,13 @@ import ( const ( // envNamespace is the prefix for ACME-DNS environment variables. envNamespace = "ACME_DNS_" - // apiBaseEnvVar is the environment variable name for the ACME-DNS API address + + // EnvAPIBase is the environment variable name for the ACME-DNS API address // (e.g. https://acmedns.your-domain.com). - apiBaseEnvVar = envNamespace + "API_BASE" - // storagePathEnvVar is the environment variable name for the ACME-DNS JSON account data file. + EnvAPIBase = envNamespace + "API_BASE" + // EnvStoragePath is the environment variable name for the ACME-DNS JSON account data file. // A per-domain account will be registered/persisted to this file and used for TXT updates. - storagePathEnvVar = envNamespace + "STORAGE_PATH" + EnvStoragePath = envNamespace + "STORAGE_PATH" ) // acmeDNSClient is an interface describing the goacmedns.Client functions the DNSProvider uses. @@ -41,15 +42,15 @@ type DNSProvider struct { } // NewDNSProvider creates an ACME-DNS provider using file based account storage. -// Its configuration is loaded from the environment by reading apiBaseEnvVar and storagePathEnvVar. +// Its configuration is loaded from the environment by reading EnvAPIBase and EnvStoragePath. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get(apiBaseEnvVar, storagePathEnvVar) + values, err := env.Get(EnvAPIBase, EnvStoragePath) if err != nil { return nil, fmt.Errorf("acme-dns: %w", err) } - client := goacmedns.NewClient(values[apiBaseEnvVar]) - storage := goacmedns.NewFileStorage(values[storagePathEnvVar], 0600) + client := goacmedns.NewClient(values[EnvAPIBase]) + storage := goacmedns.NewFileStorage(values[EnvStoragePath], 0600) return NewDNSProviderClient(client, storage) } diff --git a/providers/dns/alidns/alidns.go b/providers/dns/alidns/alidns.go index 1485e794..8274f4a8 100644 --- a/providers/dns/alidns/alidns.go +++ b/providers/dns/alidns/alidns.go @@ -17,6 +17,20 @@ import ( const defaultRegionID = "cn-hangzhou" +// Environment variables names. +const ( + envNamespace = "ALICLOUD_" + + EnvAccessKey = envNamespace + "ACCESS_KEY" + EnvSecretKey = envNamespace + "SECRET_KEY" + EnvRegionID = envNamespace + "REGION_ID" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { APIKey string @@ -31,10 +45,10 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("ALICLOUD_TTL", 600), - PropagationTimeout: env.GetOrDefaultSecond("ALICLOUD_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("ALICLOUD_POLLING_INTERVAL", dns01.DefaultPollingInterval), - HTTPTimeout: env.GetOrDefaultSecond("ALICLOUD_HTTP_TIMEOUT", 10*time.Second), + TTL: env.GetOrDefaultInt(EnvTTL, 600), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), + HTTPTimeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 10*time.Second), } } @@ -47,15 +61,15 @@ type DNSProvider struct { // NewDNSProvider returns a DNSProvider instance configured for Alibaba Cloud DNS. // Credentials must be passed in the environment variables: ALICLOUD_ACCESS_KEY and ALICLOUD_SECRET_KEY. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("ALICLOUD_ACCESS_KEY", "ALICLOUD_SECRET_KEY") + values, err := env.Get(EnvAccessKey, EnvSecretKey) if err != nil { return nil, fmt.Errorf("alicloud: %w", err) } config := NewDefaultConfig() - config.APIKey = values["ALICLOUD_ACCESS_KEY"] - config.SecretKey = values["ALICLOUD_SECRET_KEY"] - config.RegionID = env.GetOrFile("ALICLOUD_REGION_ID") + config.APIKey = values[EnvAccessKey] + config.SecretKey = values[EnvSecretKey] + config.RegionID = env.GetOrFile(EnvRegionID) return NewDNSProviderConfig(config) } diff --git a/providers/dns/alidns/alidns_test.go b/providers/dns/alidns/alidns_test.go index cde45a2b..a37e9d15 100644 --- a/providers/dns/alidns/alidns_test.go +++ b/providers/dns/alidns/alidns_test.go @@ -8,10 +8,12 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "ALICLOUD_ACCESS_KEY", - "ALICLOUD_SECRET_KEY"). - WithDomain("ALICLOUD_DOMAIN") + EnvAccessKey, + EnvSecretKey). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -22,31 +24,31 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "ALICLOUD_ACCESS_KEY": "123", - "ALICLOUD_SECRET_KEY": "456", + EnvAccessKey: "123", + EnvSecretKey: "456", }, }, { desc: "missing credentials", envVars: map[string]string{ - "ALICLOUD_ACCESS_KEY": "", - "ALICLOUD_SECRET_KEY": "", + EnvAccessKey: "", + EnvSecretKey: "", }, expected: "alicloud: some credentials information are missing: ALICLOUD_ACCESS_KEY,ALICLOUD_SECRET_KEY", }, { desc: "missing access key", envVars: map[string]string{ - "ALICLOUD_ACCESS_KEY": "", - "ALICLOUD_SECRET_KEY": "456", + EnvAccessKey: "", + EnvSecretKey: "456", }, expected: "alicloud: some credentials information are missing: ALICLOUD_ACCESS_KEY", }, { desc: "missing secret key", envVars: map[string]string{ - "ALICLOUD_ACCESS_KEY": "123", - "ALICLOUD_SECRET_KEY": "", + EnvAccessKey: "123", + EnvSecretKey: "", }, expected: "alicloud: some credentials information are missing: ALICLOUD_SECRET_KEY", }, diff --git a/providers/dns/auroradns/auroradns.go b/providers/dns/auroradns/auroradns.go index 05c6b163..acd00e61 100644 --- a/providers/dns/auroradns/auroradns.go +++ b/providers/dns/auroradns/auroradns.go @@ -14,6 +14,19 @@ import ( const defaultBaseURL = "https://api.auroradns.eu" +// Environment variables names. +const ( + envNamespace = "AURORA_" + + EnvUserID = envNamespace + "USER_ID" + EnvKey = envNamespace + "KEY" + EnvEndpoint = envNamespace + "ENDPOINT" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" +) + // Config is used to configure the creation of the DNSProvider type Config struct { BaseURL string @@ -27,9 +40,9 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("AURORA_TTL", 300), - PropagationTimeout: env.GetOrDefaultSecond("AURORA_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("AURORA_POLLING_INTERVAL", dns01.DefaultPollingInterval), + TTL: env.GetOrDefaultInt(EnvTTL, 300), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), } } @@ -45,15 +58,15 @@ type DNSProvider struct { // Credentials must be passed in the environment variables: // AURORA_USER_ID and AURORA_KEY. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("AURORA_USER_ID", "AURORA_KEY") + values, err := env.Get(EnvUserID, EnvKey) if err != nil { return nil, fmt.Errorf("aurora: %w", err) } config := NewDefaultConfig() - config.BaseURL = env.GetOrFile("AURORA_ENDPOINT") - config.UserID = values["AURORA_USER_ID"] - config.Key = values["AURORA_KEY"] + config.BaseURL = env.GetOrFile(EnvEndpoint) + config.UserID = values[EnvUserID] + config.Key = values[EnvKey] return NewDNSProviderConfig(config) } diff --git a/providers/dns/auroradns/auroradns_test.go b/providers/dns/auroradns/auroradns_test.go index 44016063..53012393 100644 --- a/providers/dns/auroradns/auroradns_test.go +++ b/providers/dns/auroradns/auroradns_test.go @@ -13,9 +13,8 @@ import ( ) var envTest = tester.NewEnvTest( - "AURORA_USER_ID", - "AURORA_KEY", -) + EnvUserID, + EnvKey) func setupTest() (*DNSProvider, *http.ServeMux, func()) { handler := http.NewServeMux() @@ -43,31 +42,31 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "AURORA_USER_ID": "123", - "AURORA_KEY": "456", + EnvUserID: "123", + EnvKey: "456", }, }, { desc: "missing credentials", envVars: map[string]string{ - "AURORA_USER_ID": "", - "AURORA_KEY": "", + EnvUserID: "", + EnvKey: "", }, expected: "aurora: some credentials information are missing: AURORA_USER_ID,AURORA_KEY", }, { desc: "missing user id", envVars: map[string]string{ - "AURORA_USER_ID": "", - "AURORA_KEY": "456", + EnvUserID: "", + EnvKey: "456", }, expected: "aurora: some credentials information are missing: AURORA_USER_ID", }, { desc: "missing key", envVars: map[string]string{ - "AURORA_USER_ID": "123", - "AURORA_KEY": "", + EnvUserID: "123", + EnvKey: "", }, expected: "aurora: some credentials information are missing: AURORA_KEY", }, diff --git a/providers/dns/autodns/autodns.go b/providers/dns/autodns/autodns.go index ea93bd9d..02c6b33c 100644 --- a/providers/dns/autodns/autodns.go +++ b/providers/dns/autodns/autodns.go @@ -11,15 +11,19 @@ import ( "github.com/go-acme/lego/v3/platform/config/env" ) +// Environment variables names. const ( - envAPIUser = "AUTODNS_API_USER" - envAPIPassword = "AUTODNS_API_PASSWORD" - envAPIEndpoint = "AUTODNS_ENDPOINT" - envAPIEndpointContext = "AUTODNS_CONTEXT" - envTTL = "AUTODNS_TTL" - envPropagationTimeout = "AUTODNS_PROPAGATION_TIMEOUT" - envPollingInterval = "AUTODNS_POLLING_INTERVAL" - envHTTPTimeout = "AUTODNS_HTTP_TIMEOUT" + envNamespace = "AUTODNS_" + + EnvAPIUser = envNamespace + "API_USER" + EnvAPIPassword = envNamespace + "API_PASSWORD" + EnvAPIEndpoint = envNamespace + "ENDPOINT" + EnvAPIEndpointContext = envNamespace + "CONTEXT" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" ) const ( @@ -39,16 +43,16 @@ type Config struct { } func NewDefaultConfig() *Config { - endpoint, _ := url.Parse(env.GetOrDefaultString(envAPIEndpoint, defaultEndpoint)) + endpoint, _ := url.Parse(env.GetOrDefaultString(EnvAPIEndpoint, defaultEndpoint)) return &Config{ Endpoint: endpoint, - Context: env.GetOrDefaultInt(envAPIEndpointContext, defaultEndpointContext), - TTL: env.GetOrDefaultInt(envTTL, defaultTTL), - PropagationTimeout: env.GetOrDefaultSecond(envPropagationTimeout, 2*time.Minute), - PollingInterval: env.GetOrDefaultSecond(envPollingInterval, 2*time.Second), + Context: env.GetOrDefaultInt(EnvAPIEndpointContext, defaultEndpointContext), + TTL: env.GetOrDefaultInt(EnvTTL, defaultTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 2*time.Minute), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond(envHTTPTimeout, 30*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), }, } } @@ -62,14 +66,14 @@ func (d *DNSProvider) Timeout() (timeout, interval time.Duration) { } func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get(envAPIUser, envAPIPassword) + values, err := env.Get(EnvAPIUser, EnvAPIPassword) if err != nil { return nil, fmt.Errorf("autodns: %w", err) } config := NewDefaultConfig() - config.Username = values[envAPIUser] - config.Password = values[envAPIPassword] + config.Username = values[EnvAPIUser] + config.Password = values[EnvAPIPassword] return NewDNSProviderConfig(config) } diff --git a/providers/dns/autodns/autodns_test.go b/providers/dns/autodns/autodns_test.go index 676d4bd2..f7f0c708 100644 --- a/providers/dns/autodns/autodns_test.go +++ b/providers/dns/autodns/autodns_test.go @@ -8,7 +8,13 @@ import ( "github.com/stretchr/testify/require" ) -var envTest = tester.NewEnvTest(envAPIEndpoint, envAPIUser, envAPIPassword) +const envDomain = envNamespace + "DOMAIN" + +var envTest = tester.NewEnvTest( + EnvAPIEndpoint, + EnvAPIUser, + EnvAPIPassword). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -19,31 +25,31 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - envAPIUser: "123", - envAPIPassword: "456", + EnvAPIUser: "123", + EnvAPIPassword: "456", }, }, { desc: "missing credentials", envVars: map[string]string{ - envAPIUser: "", - envAPIPassword: "", + EnvAPIUser: "", + EnvAPIPassword: "", }, expected: "autodns: some credentials information are missing: AUTODNS_API_USER,AUTODNS_API_PASSWORD", }, { desc: "missing user id", envVars: map[string]string{ - envAPIUser: "", - envAPIPassword: "456", + EnvAPIUser: "", + EnvAPIPassword: "456", }, expected: "autodns: some credentials information are missing: AUTODNS_API_USER", }, { desc: "missing key", envVars: map[string]string{ - envAPIUser: "123", - envAPIPassword: "", + EnvAPIUser: "123", + EnvAPIPassword: "", }, expected: "autodns: some credentials information are missing: AUTODNS_API_PASSWORD", }, diff --git a/providers/dns/azure/azure.go b/providers/dns/azure/azure.go index d2b5355f..852c2933 100644 --- a/providers/dns/azure/azure.go +++ b/providers/dns/azure/azure.go @@ -23,6 +23,22 @@ import ( const defaultMetadataEndpoint = "http://169.254.169.254" +// Environment variables names. +const ( + envNamespace = "AZURE_" + + EnvMetadataEndpoint = envNamespace + "METADATA_ENDPOINT" + EnvSubscriptionID = envNamespace + "SUBSCRIPTION_ID" + EnvResourceGroup = envNamespace + "RESOURCE_GROUP" + EnvTenantID = envNamespace + "TENANT_ID" + EnvClientID = envNamespace + "CLIENT_ID" + EnvClientSecret = envNamespace + "CLIENT_SECRET" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" +) + // Config is used to configure the creation of the DNSProvider type Config struct { // optional if using instance metadata service @@ -44,10 +60,10 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("AZURE_TTL", 60), - PropagationTimeout: env.GetOrDefaultSecond("AZURE_PROPAGATION_TIMEOUT", 2*time.Minute), - PollingInterval: env.GetOrDefaultSecond("AZURE_POLLING_INTERVAL", 2*time.Second), - MetadataEndpoint: env.GetOrFile("AZURE_METADATA_ENDPOINT"), + TTL: env.GetOrDefaultInt(EnvTTL, 60), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 2*time.Minute), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second), + MetadataEndpoint: env.GetOrFile(EnvMetadataEndpoint), } } @@ -65,8 +81,8 @@ type DNSProvider struct { // see: https://github.com/Azure/go-autorest/blob/v10.14.0/autorest/azure/auth/auth.go#L38-L42 func NewDNSProvider() (*DNSProvider, error) { config := NewDefaultConfig() - config.SubscriptionID = env.GetOrFile("AZURE_SUBSCRIPTION_ID") - config.ResourceGroup = env.GetOrFile("AZURE_RESOURCE_GROUP") + config.SubscriptionID = env.GetOrFile(EnvSubscriptionID) + config.ResourceGroup = env.GetOrFile(EnvResourceGroup) return NewDNSProviderConfig(config) } diff --git a/providers/dns/azure/azure_test.go b/providers/dns/azure/azure_test.go index 28defdc0..38cc9371 100644 --- a/providers/dns/azure/azure_test.go +++ b/providers/dns/azure/azure_test.go @@ -10,13 +10,15 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "AZURE_CLIENT_ID", - "AZURE_CLIENT_SECRET", - "AZURE_SUBSCRIPTION_ID", - "AZURE_TENANT_ID", - "AZURE_RESOURCE_GROUP"). - WithDomain("AZURE_DOMAIN") + EnvClientID, + EnvClientSecret, + EnvSubscriptionID, + EnvTenantID, + EnvResourceGroup). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -27,21 +29,21 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "AZURE_CLIENT_ID": "A", - "AZURE_CLIENT_SECRET": "B", - "AZURE_TENANT_ID": "C", - "AZURE_SUBSCRIPTION_ID": "D", - "AZURE_RESOURCE_GROUP": "E", + EnvClientID: "A", + EnvClientSecret: "B", + EnvTenantID: "C", + EnvSubscriptionID: "D", + EnvResourceGroup: "E", }, }, { desc: "missing client ID", envVars: map[string]string{ - "AZURE_CLIENT_ID": "", - "AZURE_CLIENT_SECRET": "B", - "AZURE_TENANT_ID": "C", - "AZURE_SUBSCRIPTION_ID": "D", - "AZURE_RESOURCE_GROUP": "E", + EnvClientID: "", + EnvClientSecret: "B", + EnvTenantID: "C", + EnvSubscriptionID: "D", + EnvResourceGroup: "E", }, expected: "failed to get oauth token from client credentials: parameter 'clientID' cannot be empty", }, diff --git a/providers/dns/bindman/bindman.go b/providers/dns/bindman/bindman.go index 3fecc8b2..cb139f24 100644 --- a/providers/dns/bindman/bindman.go +++ b/providers/dns/bindman/bindman.go @@ -12,6 +12,17 @@ import ( "github.com/labbsr0x/bindman-dns-webhook/src/client" ) +// Environment variables names. +const ( + envNamespace = "BINDMAN_" + + EnvManagerAddress = envNamespace + "MANAGER_ADDRESS" + + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { PropagationTimeout time.Duration @@ -23,10 +34,10 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - PropagationTimeout: env.GetOrDefaultSecond("BINDMAN_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("BINDMAN_POLLING_INTERVAL", dns01.DefaultPollingInterval), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("BINDMAN_HTTP_TIMEOUT", time.Minute), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, time.Minute), }, } } @@ -41,13 +52,13 @@ type DNSProvider struct { // NewDNSProvider returns a DNSProvider instance configured for Bindman. // BINDMAN_MANAGER_ADDRESS should have the scheme, hostname, and port (if required) of the authoritative Bindman Manager server. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("BINDMAN_MANAGER_ADDRESS") + values, err := env.Get(EnvManagerAddress) if err != nil { return nil, fmt.Errorf("bindman: %w", err) } config := NewDefaultConfig() - config.BaseURL = values["BINDMAN_MANAGER_ADDRESS"] + config.BaseURL = values[EnvManagerAddress] return NewDNSProviderConfig(config) } diff --git a/providers/dns/bindman/bindman_test.go b/providers/dns/bindman/bindman_test.go index 52671d6a..dea5a427 100644 --- a/providers/dns/bindman/bindman_test.go +++ b/providers/dns/bindman/bindman_test.go @@ -12,7 +12,9 @@ import ( "github.com/stretchr/testify/require" ) -var envTest = tester.NewEnvTest("BINDMAN_MANAGER_ADDRESS").WithDomain("BINDMAN_DOMAIN") +const envDomain = envNamespace + "DOMAIN" + +var envTest = tester.NewEnvTest(EnvManagerAddress).WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -23,20 +25,20 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "BINDMAN_MANAGER_ADDRESS": "http://localhost", + EnvManagerAddress: "http://localhost", }, }, { desc: "missing bindman manager address", envVars: map[string]string{ - "BINDMAN_MANAGER_ADDRESS": "", + EnvManagerAddress: "", }, expected: "bindman: some credentials information are missing: BINDMAN_MANAGER_ADDRESS", }, { desc: "empty bindman manager address", envVars: map[string]string{ - "BINDMAN_MANAGER_ADDRESS": " ", + EnvManagerAddress: " ", }, expected: "bindman: managerAddress parameter must be a non-empty string", }, diff --git a/providers/dns/bluecat/bluecat.go b/providers/dns/bluecat/bluecat.go index 9eadaed3..7e3fdcaa 100644 --- a/providers/dns/bluecat/bluecat.go +++ b/providers/dns/bluecat/bluecat.go @@ -21,6 +21,22 @@ const ( txtType = "TXTRecord" ) +// Environment variables names. +const ( + envNamespace = "BLUECAT_" + + EnvServerURL = envNamespace + "SERVER_URL" + EnvUserName = envNamespace + "USER_NAME" + EnvPassword = envNamespace + "PASSWORD" + EnvConfigName = envNamespace + "CONFIG_NAME" + EnvDNSView = envNamespace + "DNS_VIEW" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { BaseURL string @@ -37,11 +53,11 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("BLUECAT_TTL", dns01.DefaultTTL), - PropagationTimeout: env.GetOrDefaultSecond("BLUECAT_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("BLUECAT_POLLING_INTERVAL", dns01.DefaultPollingInterval), + TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("BLUECAT_HTTP_TIMEOUT", 30*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), }, } } @@ -59,17 +75,17 @@ type DNSProvider struct { // The REST endpoint will be appended. // In addition, the Configuration name and external DNS View Name must be passed in BLUECAT_CONFIG_NAME and BLUECAT_DNS_VIEW func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("BLUECAT_SERVER_URL", "BLUECAT_USER_NAME", "BLUECAT_PASSWORD", "BLUECAT_CONFIG_NAME", "BLUECAT_DNS_VIEW") + values, err := env.Get(EnvServerURL, EnvUserName, EnvPassword, EnvConfigName, EnvDNSView) if err != nil { return nil, fmt.Errorf("bluecat: %w", err) } config := NewDefaultConfig() - config.BaseURL = values["BLUECAT_SERVER_URL"] - config.UserName = values["BLUECAT_USER_NAME"] - config.Password = values["BLUECAT_PASSWORD"] - config.ConfigName = values["BLUECAT_CONFIG_NAME"] - config.DNSView = values["BLUECAT_DNS_VIEW"] + config.BaseURL = values[EnvServerURL] + config.UserName = values[EnvUserName] + config.Password = values[EnvPassword] + config.ConfigName = values[EnvConfigName] + config.DNSView = values[EnvDNSView] return NewDNSProviderConfig(config) } diff --git a/providers/dns/bluecat/bluecat_test.go b/providers/dns/bluecat/bluecat_test.go index 8fecbdd4..2a19de34 100644 --- a/providers/dns/bluecat/bluecat_test.go +++ b/providers/dns/bluecat/bluecat_test.go @@ -8,13 +8,15 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "BLUECAT_SERVER_URL", - "BLUECAT_USER_NAME", - "BLUECAT_PASSWORD", - "BLUECAT_CONFIG_NAME", - "BLUECAT_DNS_VIEW"). - WithDomain("BLUECAT_DOMAIN") + EnvServerURL, + EnvUserName, + EnvPassword, + EnvConfigName, + EnvDNSView). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -25,76 +27,76 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "BLUECAT_SERVER_URL": "http://localhost", - "BLUECAT_USER_NAME": "A", - "BLUECAT_PASSWORD": "B", - "BLUECAT_CONFIG_NAME": "C", - "BLUECAT_DNS_VIEW": "D", + EnvServerURL: "http://localhost", + EnvUserName: "A", + EnvPassword: "B", + EnvConfigName: "C", + EnvDNSView: "D", }, }, { desc: "missing credentials", envVars: map[string]string{ - "BLUECAT_SERVER_URL": "", - "BLUECAT_USER_NAME": "", - "BLUECAT_PASSWORD": "", - "BLUECAT_CONFIG_NAME": "", - "BLUECAT_DNS_VIEW": "", + EnvServerURL: "", + EnvUserName: "", + EnvPassword: "", + EnvConfigName: "", + EnvDNSView: "", }, expected: "bluecat: some credentials information are missing: BLUECAT_SERVER_URL,BLUECAT_USER_NAME,BLUECAT_PASSWORD,BLUECAT_CONFIG_NAME,BLUECAT_DNS_VIEW", }, { desc: "missing server url", envVars: map[string]string{ - "BLUECAT_SERVER_URL": "", - "BLUECAT_USER_NAME": "A", - "BLUECAT_PASSWORD": "B", - "BLUECAT_CONFIG_NAME": "C", - "BLUECAT_DNS_VIEW": "D", + EnvServerURL: "", + EnvUserName: "A", + EnvPassword: "B", + EnvConfigName: "C", + EnvDNSView: "D", }, expected: "bluecat: some credentials information are missing: BLUECAT_SERVER_URL", }, { desc: "missing username", envVars: map[string]string{ - "BLUECAT_SERVER_URL": "http://localhost", - "BLUECAT_USER_NAME": "", - "BLUECAT_PASSWORD": "B", - "BLUECAT_CONFIG_NAME": "C", - "BLUECAT_DNS_VIEW": "D", + EnvServerURL: "http://localhost", + EnvUserName: "", + EnvPassword: "B", + EnvConfigName: "C", + EnvDNSView: "D", }, expected: "bluecat: some credentials information are missing: BLUECAT_USER_NAME", }, { desc: "missing password", envVars: map[string]string{ - "BLUECAT_SERVER_URL": "http://localhost", - "BLUECAT_USER_NAME": "A", - "BLUECAT_PASSWORD": "", - "BLUECAT_CONFIG_NAME": "C", - "BLUECAT_DNS_VIEW": "D", + EnvServerURL: "http://localhost", + EnvUserName: "A", + EnvPassword: "", + EnvConfigName: "C", + EnvDNSView: "D", }, expected: "bluecat: some credentials information are missing: BLUECAT_PASSWORD", }, { desc: "missing config name", envVars: map[string]string{ - "BLUECAT_SERVER_URL": "http://localhost", - "BLUECAT_USER_NAME": "A", - "BLUECAT_PASSWORD": "B", - "BLUECAT_CONFIG_NAME": "", - "BLUECAT_DNS_VIEW": "D", + EnvServerURL: "http://localhost", + EnvUserName: "A", + EnvPassword: "B", + EnvConfigName: "", + EnvDNSView: "D", }, expected: "bluecat: some credentials information are missing: BLUECAT_CONFIG_NAME", }, { desc: "missing DNS view", envVars: map[string]string{ - "BLUECAT_SERVER_URL": "http://localhost", - "BLUECAT_USER_NAME": "A", - "BLUECAT_PASSWORD": "B", - "BLUECAT_CONFIG_NAME": "C", - "BLUECAT_DNS_VIEW": "", + EnvServerURL: "http://localhost", + EnvUserName: "A", + EnvPassword: "B", + EnvConfigName: "C", + EnvDNSView: "", }, expected: "bluecat: some credentials information are missing: BLUECAT_DNS_VIEW", }, diff --git a/providers/dns/checkdomain/checkdomain.go b/providers/dns/checkdomain/checkdomain.go index 3f4f73db..399031c6 100644 --- a/providers/dns/checkdomain/checkdomain.go +++ b/providers/dns/checkdomain/checkdomain.go @@ -12,13 +12,17 @@ import ( "github.com/go-acme/lego/v3/platform/config/env" ) +// Environment variables names. const ( - envEndpoint = "CHECKDOMAIN_ENDPOINT" - envToken = "CHECKDOMAIN_TOKEN" - envTTL = "CHECKDOMAIN_TTL" - envHTTPTimeout = "CHECKDOMAIN_HTTP_TIMEOUT" - envPropagationTimeout = "CHECKDOMAIN_PROPAGATION_TIMEOUT" - envPollingInterval = "CHECKDOMAIN_POLLING_INTERVAL" + envNamespace = "CHECKDOMAIN_" + + EnvEndpoint = envNamespace + "ENDPOINT" + EnvToken = envNamespace + "TOKEN" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" ) const ( @@ -39,11 +43,11 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt(envTTL, defaultTTL), - PropagationTimeout: env.GetOrDefaultSecond(envPropagationTimeout, 5*time.Minute), - PollingInterval: env.GetOrDefaultSecond(envPollingInterval, 7*time.Second), + TTL: env.GetOrDefaultInt(EnvTTL, defaultTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 5*time.Minute), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 7*time.Second), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond(envHTTPTimeout, 30*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), }, } } @@ -58,17 +62,17 @@ type DNSProvider struct { } func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get(envToken) + values, err := env.Get(EnvToken) if err != nil { return nil, fmt.Errorf("checkdomain: %w", err) } config := NewDefaultConfig() - config.Token = values[envToken] + config.Token = values[EnvToken] - endpoint, err := url.Parse(env.GetOrDefaultString(envEndpoint, defaultEndpoint)) + endpoint, err := url.Parse(env.GetOrDefaultString(EnvEndpoint, defaultEndpoint)) if err != nil { - return nil, fmt.Errorf("checkdomain: invalid %s: %w", envEndpoint, err) + return nil, fmt.Errorf("checkdomain: invalid %s: %w", EnvEndpoint, err) } config.Endpoint = endpoint diff --git a/providers/dns/checkdomain/checkdomain_test.go b/providers/dns/checkdomain/checkdomain_test.go index ad57e57c..697334f8 100644 --- a/providers/dns/checkdomain/checkdomain_test.go +++ b/providers/dns/checkdomain/checkdomain_test.go @@ -9,7 +9,12 @@ import ( "github.com/stretchr/testify/require" ) -var envTest = tester.NewEnvTest(envEndpoint, envToken) +const envDomain = envNamespace + "DOMAIN" + +var envTest = tester.NewEnvTest( + EnvEndpoint, + EnvToken). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -20,7 +25,7 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - envToken: "dummy", + EnvToken: "dummy", }, }, { @@ -31,8 +36,8 @@ func TestNewDNSProvider(t *testing.T) { { desc: "invalid endpoint", envVars: map[string]string{ - envToken: "dummy", - envEndpoint: ":", + EnvToken: "dummy", + EnvEndpoint: ":", }, expected: `checkdomain: invalid CHECKDOMAIN_ENDPOINT: parse ":": missing protocol scheme`, }, diff --git a/providers/dns/cloudns/cloudns.go b/providers/dns/cloudns/cloudns.go index 0ceac63c..e8483044 100644 --- a/providers/dns/cloudns/cloudns.go +++ b/providers/dns/cloudns/cloudns.go @@ -12,6 +12,19 @@ import ( "github.com/go-acme/lego/v3/providers/dns/cloudns/internal" ) +// Environment variables names. +const ( + envNamespace = "CLOUDNS_" + + EnvAuthID = envNamespace + "AUTH_ID" + EnvAuthPassword = envNamespace + "AUTH_PASSWORD" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { AuthID string @@ -25,11 +38,11 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - PropagationTimeout: env.GetOrDefaultSecond("CLOUDNS_PROPAGATION_TIMEOUT", 120*time.Second), - PollingInterval: env.GetOrDefaultSecond("CLOUDNS_POLLING_INTERVAL", 4*time.Second), - TTL: env.GetOrDefaultInt("CLOUDNS_TTL", 60), + TTL: env.GetOrDefaultInt(EnvTTL, 60), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 120*time.Second), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 4*time.Second), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("CLOUDNS_HTTP_TIMEOUT", 30*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), }, } } @@ -44,14 +57,14 @@ type DNSProvider struct { // Credentials must be passed in the environment variables: // CLOUDNS_AUTH_ID and CLOUDNS_AUTH_PASSWORD. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("CLOUDNS_AUTH_ID", "CLOUDNS_AUTH_PASSWORD") + values, err := env.Get(EnvAuthID, EnvAuthPassword) if err != nil { return nil, fmt.Errorf("ClouDNS: %w", err) } config := NewDefaultConfig() - config.AuthID = values["CLOUDNS_AUTH_ID"] - config.AuthPassword = values["CLOUDNS_AUTH_PASSWORD"] + config.AuthID = values[EnvAuthID] + config.AuthPassword = values[EnvAuthPassword] return NewDNSProviderConfig(config) } diff --git a/providers/dns/cloudns/cloudns_test.go b/providers/dns/cloudns/cloudns_test.go index 98915146..e138f62d 100644 --- a/providers/dns/cloudns/cloudns_test.go +++ b/providers/dns/cloudns/cloudns_test.go @@ -8,10 +8,12 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "CLOUDNS_AUTH_ID", - "CLOUDNS_AUTH_PASSWORD"). - WithDomain("CLOUDNS_DOMAIN") + EnvAuthID, + EnvAuthPassword). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -22,31 +24,31 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "CLOUDNS_AUTH_ID": "123", - "CLOUDNS_AUTH_PASSWORD": "456", + EnvAuthID: "123", + EnvAuthPassword: "456", }, }, { desc: "missing credentials", envVars: map[string]string{ - "CLOUDNS_AUTH_ID": "", - "CLOUDNS_AUTH_PASSWORD": "", + EnvAuthID: "", + EnvAuthPassword: "", }, expected: "ClouDNS: some credentials information are missing: CLOUDNS_AUTH_ID,CLOUDNS_AUTH_PASSWORD", }, { desc: "missing auth-id", envVars: map[string]string{ - "CLOUDNS_AUTH_ID": "", - "CLOUDNS_AUTH_PASSWORD": "456", + EnvAuthID: "", + EnvAuthPassword: "456", }, expected: "ClouDNS: some credentials information are missing: CLOUDNS_AUTH_ID", }, { desc: "missing auth-password", envVars: map[string]string{ - "CLOUDNS_AUTH_ID": "123", - "CLOUDNS_AUTH_PASSWORD": "", + EnvAuthID: "123", + EnvAuthPassword: "", }, expected: "ClouDNS: some credentials information are missing: CLOUDNS_AUTH_PASSWORD", }, diff --git a/providers/dns/cloudxns/cloudxns.go b/providers/dns/cloudxns/cloudxns.go index 88e24dae..f4ccd7bd 100644 --- a/providers/dns/cloudxns/cloudxns.go +++ b/providers/dns/cloudxns/cloudxns.go @@ -12,6 +12,19 @@ import ( "github.com/go-acme/lego/v3/providers/dns/cloudxns/internal" ) +// Environment variables names. +const ( + envNamespace = "CLOUDXNS_" + + EnvAPIKey = envNamespace + "API_KEY" + EnvSecretKey = envNamespace + "SECRET_KEY" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { APIKey string @@ -25,11 +38,11 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - PropagationTimeout: env.GetOrDefaultSecond("CLOUDXNS_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("CLOUDXNS_POLLING_INTERVAL", dns01.DefaultPollingInterval), - TTL: env.GetOrDefaultInt("CLOUDXNS_TTL", dns01.DefaultTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), + TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("CLOUDXNS_HTTP_TIMEOUT", 30*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), }, } } @@ -44,14 +57,14 @@ type DNSProvider struct { // Credentials must be passed in the environment variables: // CLOUDXNS_API_KEY and CLOUDXNS_SECRET_KEY. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("CLOUDXNS_API_KEY", "CLOUDXNS_SECRET_KEY") + values, err := env.Get(EnvAPIKey, EnvSecretKey) if err != nil { return nil, fmt.Errorf("CloudXNS: %w", err) } config := NewDefaultConfig() - config.APIKey = values["CLOUDXNS_API_KEY"] - config.SecretKey = values["CLOUDXNS_SECRET_KEY"] + config.APIKey = values[EnvAPIKey] + config.SecretKey = values[EnvSecretKey] return NewDNSProviderConfig(config) } diff --git a/providers/dns/cloudxns/cloudxns_test.go b/providers/dns/cloudxns/cloudxns_test.go index 19170de9..1addc6aa 100644 --- a/providers/dns/cloudxns/cloudxns_test.go +++ b/providers/dns/cloudxns/cloudxns_test.go @@ -8,10 +8,12 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "CLOUDXNS_API_KEY", - "CLOUDXNS_SECRET_KEY"). - WithDomain("CLOUDXNS_DOMAIN") + EnvAPIKey, + EnvSecretKey). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -22,31 +24,31 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "CLOUDXNS_API_KEY": "123", - "CLOUDXNS_SECRET_KEY": "456", + EnvAPIKey: "123", + EnvSecretKey: "456", }, }, { desc: "missing credentials", envVars: map[string]string{ - "CLOUDXNS_API_KEY": "", - "CLOUDXNS_SECRET_KEY": "", + EnvAPIKey: "", + EnvSecretKey: "", }, expected: "CloudXNS: some credentials information are missing: CLOUDXNS_API_KEY,CLOUDXNS_SECRET_KEY", }, { desc: "missing API key", envVars: map[string]string{ - "CLOUDXNS_API_KEY": "", - "CLOUDXNS_SECRET_KEY": "456", + EnvAPIKey: "", + EnvSecretKey: "456", }, expected: "CloudXNS: some credentials information are missing: CLOUDXNS_API_KEY", }, { desc: "missing secret key", envVars: map[string]string{ - "CLOUDXNS_API_KEY": "123", - "CLOUDXNS_SECRET_KEY": "", + EnvAPIKey: "123", + EnvSecretKey: "", }, expected: "CloudXNS: some credentials information are missing: CLOUDXNS_SECRET_KEY", }, diff --git a/providers/dns/conoha/conoha.go b/providers/dns/conoha/conoha.go index db5941b2..3fed330f 100644 --- a/providers/dns/conoha/conoha.go +++ b/providers/dns/conoha/conoha.go @@ -12,6 +12,21 @@ import ( "github.com/go-acme/lego/v3/providers/dns/conoha/internal" ) +// Environment variables names. +const ( + envNamespace = "CONOHA_" + + EnvRegion = envNamespace + "REGION" + EnvTenantID = envNamespace + "TENANT_ID" + EnvAPIUsername = envNamespace + "API_USERNAME" + EnvAPIPassword = envNamespace + "API_PASSWORD" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { Region string @@ -27,12 +42,12 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - Region: env.GetOrDefaultString("CONOHA_REGION", "tyo1"), - TTL: env.GetOrDefaultInt("CONOHA_TTL", 60), - PropagationTimeout: env.GetOrDefaultSecond("CONOHA_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("CONOHA_POLLING_INTERVAL", dns01.DefaultPollingInterval), + Region: env.GetOrDefaultString(EnvRegion, "tyo1"), + TTL: env.GetOrDefaultInt(EnvTTL, 60), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("CONOHA_HTTP_TIMEOUT", 30*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), }, } } @@ -46,15 +61,15 @@ type DNSProvider struct { // NewDNSProvider returns a DNSProvider instance configured for ConoHa DNS. // Credentials must be passed in the environment variables: CONOHA_TENANT_ID, CONOHA_API_USERNAME, CONOHA_API_PASSWORD func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("CONOHA_TENANT_ID", "CONOHA_API_USERNAME", "CONOHA_API_PASSWORD") + values, err := env.Get(EnvTenantID, EnvAPIUsername, EnvAPIPassword) if err != nil { return nil, fmt.Errorf("conoha: %w", err) } config := NewDefaultConfig() - config.TenantID = values["CONOHA_TENANT_ID"] - config.Username = values["CONOHA_API_USERNAME"] - config.Password = values["CONOHA_API_PASSWORD"] + config.TenantID = values[EnvTenantID] + config.Username = values[EnvAPIUsername] + config.Password = values[EnvAPIPassword] return NewDNSProviderConfig(config) } diff --git a/providers/dns/conoha/conoha_test.go b/providers/dns/conoha/conoha_test.go index 9168f194..0acaa8b3 100644 --- a/providers/dns/conoha/conoha_test.go +++ b/providers/dns/conoha/conoha_test.go @@ -8,11 +8,13 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "CONOHA_TENANT_ID", - "CONOHA_API_USERNAME", - "CONOHA_API_PASSWORD"). - WithDomain("CONOHA_DOMAIN") + EnvTenantID, + EnvAPIUsername, + EnvAPIPassword). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -23,45 +25,45 @@ func TestNewDNSProvider(t *testing.T) { { desc: "complete credentials, but login failed", envVars: map[string]string{ - "CONOHA_TENANT_ID": "tenant_id", - "CONOHA_API_USERNAME": "api_username", - "CONOHA_API_PASSWORD": "api_password", + EnvTenantID: "tenant_id", + EnvAPIUsername: "api_username", + EnvAPIPassword: "api_password", }, expected: `conoha: failed to create client: failed to login: HTTP request failed with status code 401: {"unauthorized":{"message":"Invalid user: api_username","code":401}}`, }, { desc: "missing credentials", envVars: map[string]string{ - "CONOHA_TENANT_ID": "", - "CONOHA_API_USERNAME": "", - "CONOHA_API_PASSWORD": "", + EnvTenantID: "", + EnvAPIUsername: "", + EnvAPIPassword: "", }, expected: "conoha: some credentials information are missing: CONOHA_TENANT_ID,CONOHA_API_USERNAME,CONOHA_API_PASSWORD", }, { desc: "missing tenant id", envVars: map[string]string{ - "CONOHA_TENANT_ID": "", - "CONOHA_API_USERNAME": "api_username", - "CONOHA_API_PASSWORD": "api_password", + EnvTenantID: "", + EnvAPIUsername: "api_username", + EnvAPIPassword: "api_password", }, expected: "conoha: some credentials information are missing: CONOHA_TENANT_ID", }, { desc: "missing api username", envVars: map[string]string{ - "CONOHA_TENANT_ID": "tenant_id", - "CONOHA_API_USERNAME": "", - "CONOHA_API_PASSWORD": "api_password", + EnvTenantID: "tenant_id", + EnvAPIUsername: "", + EnvAPIPassword: "api_password", }, expected: "conoha: some credentials information are missing: CONOHA_API_USERNAME", }, { desc: "missing api password", envVars: map[string]string{ - "CONOHA_TENANT_ID": "tenant_id", - "CONOHA_API_USERNAME": "api_username", - "CONOHA_API_PASSWORD": "", + EnvTenantID: "tenant_id", + EnvAPIUsername: "api_username", + EnvAPIPassword: "", }, expected: "conoha: some credentials information are missing: CONOHA_API_PASSWORD", }, diff --git a/providers/dns/constellix/constellix.go b/providers/dns/constellix/constellix.go index 356572c2..fd1d4f7a 100644 --- a/providers/dns/constellix/constellix.go +++ b/providers/dns/constellix/constellix.go @@ -12,6 +12,19 @@ import ( "github.com/go-acme/lego/v3/providers/dns/constellix/internal" ) +// Environment variables names. +const ( + envNamespace = "CONSTELLIX_" + + EnvAPIKey = envNamespace + "API_KEY" + EnvSecretKey = envNamespace + "SECRET_KEY" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { APIKey string @@ -25,11 +38,11 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("CONSTELLIX_TTL", 300), - PropagationTimeout: env.GetOrDefaultSecond("CONSTELLIX_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("CONSTELLIX_POLLING_INTERVAL", dns01.DefaultPollingInterval), + TTL: env.GetOrDefaultInt(EnvTTL, 300), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("CONSTELLIX_HTTP_TIMEOUT", 30*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), }, } } @@ -44,14 +57,14 @@ type DNSProvider struct { // Credentials must be passed in the environment variables: // CONSTELLIX_API_KEY and CONSTELLIX_SECRET_KEY. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("CONSTELLIX_API_KEY", "CONSTELLIX_SECRET_KEY") + values, err := env.Get(EnvAPIKey, EnvSecretKey) if err != nil { return nil, fmt.Errorf("constellix: %w", err) } config := NewDefaultConfig() - config.APIKey = values["CONSTELLIX_API_KEY"] - config.SecretKey = values["CONSTELLIX_SECRET_KEY"] + config.APIKey = values[EnvAPIKey] + config.SecretKey = values[EnvSecretKey] return NewDNSProviderConfig(config) } diff --git a/providers/dns/constellix/constellix_test.go b/providers/dns/constellix/constellix_test.go index a94f9586..ac6a86e7 100644 --- a/providers/dns/constellix/constellix_test.go +++ b/providers/dns/constellix/constellix_test.go @@ -8,10 +8,12 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "CONSTELLIX_API_KEY", - "CONSTELLIX_SECRET_KEY"). - WithDomain("CONSTELLIX_DOMAIN") + EnvAPIKey, + EnvSecretKey). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -22,31 +24,31 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "CONSTELLIX_API_KEY": "123", - "CONSTELLIX_SECRET_KEY": "456", + EnvAPIKey: "123", + EnvSecretKey: "456", }, }, { desc: "missing credentials", envVars: map[string]string{ - "CONSTELLIX_API_KEY": "", - "CONSTELLIX_SECRET_KEY": "", + EnvAPIKey: "", + EnvSecretKey: "", }, expected: "constellix: some credentials information are missing: CONSTELLIX_API_KEY,CONSTELLIX_SECRET_KEY", }, { desc: "missing api key", envVars: map[string]string{ - "CONSTELLIX_API_KEY": "", - "CONSTELLIX_SECRET_KEY": "api_password", + EnvAPIKey: "", + EnvSecretKey: "api_password", }, expected: "constellix: some credentials information are missing: CONSTELLIX_API_KEY", }, { desc: "missing secret key", envVars: map[string]string{ - "CONSTELLIX_API_KEY": "api_username", - "CONSTELLIX_SECRET_KEY": "", + EnvAPIKey: "api_username", + EnvSecretKey: "", }, expected: "constellix: some credentials information are missing: CONSTELLIX_SECRET_KEY", }, diff --git a/providers/dns/designate/designate.go b/providers/dns/designate/designate.go index 00778ceb..6fa197ae 100644 --- a/providers/dns/designate/designate.go +++ b/providers/dns/designate/designate.go @@ -17,6 +17,24 @@ import ( "github.com/gophercloud/gophercloud/openstack/dns/v2/zones" ) +// Environment variables names. +const ( + envNamespace = "DESIGNATE_" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + + envNamespaceClient = "OS_" + + EnvAuthURL = envNamespaceClient + "AUTH_URL" + EnvUsername = envNamespaceClient + "USERNAME" + EnvPassword = envNamespaceClient + "PASSWORD" + EnvTenantName = envNamespaceClient + "TENANT_NAME" + EnvRegionName = envNamespaceClient + "REGION_NAME" + EnvProjectID = envNamespaceClient + "PROJECT_ID" +) + // Config is used to configure the creation of the DNSProvider type Config struct { PropagationTimeout time.Duration @@ -28,9 +46,9 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("DESIGNATE_TTL", 10), - PropagationTimeout: env.GetOrDefaultSecond("DESIGNATE_PROPAGATION_TIMEOUT", 10*time.Minute), - PollingInterval: env.GetOrDefaultSecond("DESIGNATE_POLLING_INTERVAL", 10*time.Second), + TTL: env.GetOrDefaultInt(EnvTTL, 10), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 10*time.Minute), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 10*time.Second), } } @@ -45,7 +63,7 @@ type DNSProvider struct { // Credentials must be passed in the environment variables: // OS_AUTH_URL, OS_USERNAME, OS_PASSWORD, OS_TENANT_NAME, OS_REGION_NAME. func NewDNSProvider() (*DNSProvider, error) { - _, err := env.Get("OS_AUTH_URL", "OS_USERNAME", "OS_PASSWORD", "OS_TENANT_NAME", "OS_REGION_NAME") + _, err := env.Get(EnvAuthURL, EnvUsername, EnvPassword, EnvTenantName, EnvRegionName) if err != nil { return nil, fmt.Errorf("designate: %w", err) } diff --git a/providers/dns/designate/designate_test.go b/providers/dns/designate/designate_test.go index 4cd422db..7783b711 100644 --- a/providers/dns/designate/designate_test.go +++ b/providers/dns/designate/designate_test.go @@ -10,14 +10,16 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "OS_AUTH_URL", - "OS_USERNAME", - "OS_PASSWORD", - "OS_TENANT_NAME", - "OS_REGION_NAME", - "OS_PROJECT_ID"). - WithDomain("DESIGNATE_DOMAIN") + EnvAuthURL, + EnvUsername, + EnvPassword, + EnvTenantName, + EnvRegionName, + EnvProjectID). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { server := getServer() @@ -31,77 +33,77 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "OS_AUTH_URL": server.URL + "/v2.0/", - "OS_USERNAME": "B", - "OS_PASSWORD": "C", - "OS_REGION_NAME": "D", - "OS_TENANT_NAME": "E", - "OS_PROJECT_ID": "F", + EnvAuthURL: server.URL + "/v2.0/", + EnvUsername: "B", + EnvPassword: "C", + EnvRegionName: "D", + EnvTenantName: "E", + EnvProjectID: "F", }, }, { desc: "missing credentials", envVars: map[string]string{ - "OS_AUTH_URL": "", - "OS_USERNAME": "", - "OS_PASSWORD": "", - "OS_REGION_NAME": "", - "OS_TENANT_NAME": "", + EnvAuthURL: "", + EnvUsername: "", + EnvPassword: "", + EnvRegionName: "", + EnvTenantName: "", }, expected: "designate: some credentials information are missing: OS_AUTH_URL,OS_USERNAME,OS_PASSWORD,OS_TENANT_NAME,OS_REGION_NAME", }, { desc: "missing auth url", envVars: map[string]string{ - "OS_AUTH_URL": "", - "OS_USERNAME": "B", - "OS_PASSWORD": "C", - "OS_REGION_NAME": "D", - "OS_TENANT_NAME": "E", + EnvAuthURL: "", + EnvUsername: "B", + EnvPassword: "C", + EnvRegionName: "D", + EnvTenantName: "E", }, expected: "designate: some credentials information are missing: OS_AUTH_URL", }, { desc: "missing username", envVars: map[string]string{ - "OS_AUTH_URL": server.URL + "/v2.0/", - "OS_USERNAME": "", - "OS_PASSWORD": "C", - "OS_REGION_NAME": "D", - "OS_TENANT_NAME": "E", + EnvAuthURL: server.URL + "/v2.0/", + EnvUsername: "", + EnvPassword: "C", + EnvRegionName: "D", + EnvTenantName: "E", }, expected: "designate: some credentials information are missing: OS_USERNAME", }, { desc: "missing password", envVars: map[string]string{ - "OS_AUTH_URL": server.URL + "/v2.0/", - "OS_USERNAME": "B", - "OS_PASSWORD": "", - "OS_REGION_NAME": "D", - "OS_TENANT_NAME": "E", + EnvAuthURL: server.URL + "/v2.0/", + EnvUsername: "B", + EnvPassword: "", + EnvRegionName: "D", + EnvTenantName: "E", }, expected: "designate: some credentials information are missing: OS_PASSWORD", }, { desc: "missing region name", envVars: map[string]string{ - "OS_AUTH_URL": server.URL + "/v2.0/", - "OS_USERNAME": "B", - "OS_PASSWORD": "C", - "OS_REGION_NAME": "", - "OS_TENANT_NAME": "E", + EnvAuthURL: server.URL + "/v2.0/", + EnvUsername: "B", + EnvPassword: "C", + EnvRegionName: "", + EnvTenantName: "E", }, expected: "designate: some credentials information are missing: OS_REGION_NAME", }, { desc: "missing tenant name", envVars: map[string]string{ - "OS_AUTH_URL": server.URL + "/v2.0/", - "OS_USERNAME": "B", - "OS_PASSWORD": "C", - "OS_REGION_NAME": "D", - "OS_TENANT_NAME": "", + EnvAuthURL: server.URL + "/v2.0/", + EnvUsername: "B", + EnvPassword: "C", + EnvRegionName: "D", + EnvTenantName: "", }, expected: "designate: some credentials information are missing: OS_TENANT_NAME", }, diff --git a/providers/dns/digitalocean/digitalocean.go b/providers/dns/digitalocean/digitalocean.go index db82b2f3..1e668424 100644 --- a/providers/dns/digitalocean/digitalocean.go +++ b/providers/dns/digitalocean/digitalocean.go @@ -12,6 +12,18 @@ import ( "github.com/go-acme/lego/v3/platform/config/env" ) +// Environment variables names. +const ( + envNamespace = "DO_" + + EnvAuthToken = envNamespace + "AUTH_TOKEN" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { BaseURL string @@ -26,11 +38,11 @@ type Config struct { func NewDefaultConfig() *Config { return &Config{ BaseURL: defaultBaseURL, - TTL: env.GetOrDefaultInt("DO_TTL", 30), - PropagationTimeout: env.GetOrDefaultSecond("DO_PROPAGATION_TIMEOUT", 60*time.Second), - PollingInterval: env.GetOrDefaultSecond("DO_POLLING_INTERVAL", 5*time.Second), + TTL: env.GetOrDefaultInt(EnvTTL, 30), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 60*time.Second), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 5*time.Second), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("DO_HTTP_TIMEOUT", 30*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), }, } } @@ -47,13 +59,13 @@ type DNSProvider struct { // Ocean. Credentials must be passed in the environment variable: // DO_AUTH_TOKEN. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("DO_AUTH_TOKEN") + values, err := env.Get(EnvAuthToken) if err != nil { return nil, fmt.Errorf("digitalocean: %w", err) } config := NewDefaultConfig() - config.AuthToken = values["DO_AUTH_TOKEN"] + config.AuthToken = values[EnvAuthToken] return NewDNSProviderConfig(config) } diff --git a/providers/dns/digitalocean/digitalocean_test.go b/providers/dns/digitalocean/digitalocean_test.go index 0342a535..c076edbc 100644 --- a/providers/dns/digitalocean/digitalocean_test.go +++ b/providers/dns/digitalocean/digitalocean_test.go @@ -12,7 +12,7 @@ import ( "github.com/stretchr/testify/require" ) -var envTest = tester.NewEnvTest("DO_AUTH_TOKEN") +var envTest = tester.NewEnvTest(EnvAuthToken) func setupTest() (*DNSProvider, *http.ServeMux, func()) { handler := http.NewServeMux() @@ -39,13 +39,13 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "DO_AUTH_TOKEN": "123", + EnvAuthToken: "123", }, }, { desc: "missing credentials", envVars: map[string]string{ - "DO_AUTH_TOKEN": "", + EnvAuthToken: "", }, expected: "digitalocean: some credentials information are missing: DO_AUTH_TOKEN", }, diff --git a/providers/dns/dnsimple/dnsimple.go b/providers/dns/dnsimple/dnsimple.go index 141bbf5d..b729b4a8 100644 --- a/providers/dns/dnsimple/dnsimple.go +++ b/providers/dns/dnsimple/dnsimple.go @@ -15,6 +15,18 @@ import ( "golang.org/x/oauth2" ) +// Environment variables names. +const ( + envNamespace = "DNSIMPLE_" + + EnvOAuthToken = envNamespace + "OAUTH_TOKEN" + EnvBaseURL = envNamespace + "BASE_URL" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" +) + // Config is used to configure the creation of the DNSProvider type Config struct { AccessToken string @@ -27,9 +39,9 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("DNSIMPLE_TTL", dns01.DefaultTTL), - PropagationTimeout: env.GetOrDefaultSecond("DNSIMPLE_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("DNSIMPLE_POLLING_INTERVAL", dns01.DefaultPollingInterval), + TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), } } @@ -45,8 +57,8 @@ type DNSProvider struct { // See: https://developer.dnsimple.com/v2/#authentication func NewDNSProvider() (*DNSProvider, error) { config := NewDefaultConfig() - config.AccessToken = env.GetOrFile("DNSIMPLE_OAUTH_TOKEN") - config.BaseURL = env.GetOrFile("DNSIMPLE_BASE_URL") + config.AccessToken = env.GetOrFile(EnvOAuthToken) + config.BaseURL = env.GetOrFile(EnvBaseURL) return NewDNSProviderConfig(config) } diff --git a/providers/dns/dnsimple/dnsimple_test.go b/providers/dns/dnsimple/dnsimple_test.go index 0d52e1f6..7631e48c 100644 --- a/providers/dns/dnsimple/dnsimple_test.go +++ b/providers/dns/dnsimple/dnsimple_test.go @@ -12,11 +12,13 @@ import ( const sandboxURL = "https://api.sandbox.fake.com" +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "DNSIMPLE_OAUTH_TOKEN", - "DNSIMPLE_BASE_URL"). - WithDomain("DNSIMPLE_DOMAIN"). - WithLiveTestRequirements("DNSIMPLE_OAUTH_TOKEN", "DNSIMPLE_DOMAIN") + EnvOAuthToken, + EnvBaseURL). + WithDomain(envDomain). + WithLiveTestRequirements(EnvOAuthToken, envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -27,20 +29,20 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "DNSIMPLE_OAUTH_TOKEN": "my_token", + EnvOAuthToken: "my_token", }, }, { desc: "success: base url", envVars: map[string]string{ - "DNSIMPLE_OAUTH_TOKEN": "my_token", - "DNSIMPLE_BASE_URL": "https://api.dnsimple.test", + EnvOAuthToken: "my_token", + EnvBaseURL: "https://api.dnsimple.test", }, }, { desc: "missing oauth token", envVars: map[string]string{ - "DNSIMPLE_OAUTH_TOKEN": "", + EnvOAuthToken: "", }, expected: "dnsimple: OAuth token is missing", }, @@ -61,7 +63,7 @@ func TestNewDNSProvider(t *testing.T) { require.NotNil(t, p.config) require.NotNil(t, p.client) - baseURL := os.Getenv("DNSIMPLE_BASE_URL") + baseURL := os.Getenv(EnvBaseURL) if baseURL != "" { assert.Equal(t, baseURL, p.client.BaseURL) } @@ -126,8 +128,8 @@ func TestLivePresent(t *testing.T) { envTest.RestoreEnv() - if len(os.Getenv("DNSIMPLE_BASE_URL")) == 0 { - os.Setenv("DNSIMPLE_BASE_URL", sandboxURL) + if len(os.Getenv(EnvBaseURL)) == 0 { + os.Setenv(EnvBaseURL, sandboxURL) } provider, err := NewDNSProvider() @@ -144,8 +146,8 @@ func TestLiveCleanUp(t *testing.T) { envTest.RestoreEnv() - if len(os.Getenv("DNSIMPLE_BASE_URL")) == 0 { - os.Setenv("DNSIMPLE_BASE_URL", sandboxURL) + if len(os.Getenv(EnvBaseURL)) == 0 { + os.Setenv(EnvBaseURL, sandboxURL) } provider, err := NewDNSProvider() diff --git a/providers/dns/dnsmadeeasy/dnsmadeeasy.go b/providers/dns/dnsmadeeasy/dnsmadeeasy.go index fdb9cbbf..6a7acb64 100644 --- a/providers/dns/dnsmadeeasy/dnsmadeeasy.go +++ b/providers/dns/dnsmadeeasy/dnsmadeeasy.go @@ -14,6 +14,20 @@ import ( "github.com/go-acme/lego/v3/providers/dns/dnsmadeeasy/internal" ) +// Environment variables names. +const ( + envNamespace = "DNSMADEEASY_" + + EnvAPIKey = envNamespace + "API_KEY" + EnvAPISecret = envNamespace + "API_SECRET" + EnvSandbox = envNamespace + "SANDBOX" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { BaseURL string @@ -29,11 +43,11 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("DNSMADEEASY_TTL", dns01.DefaultTTL), - PropagationTimeout: env.GetOrDefaultSecond("DNSMADEEASY_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("DNSMADEEASY_POLLING_INTERVAL", dns01.DefaultPollingInterval), + TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("DNSMADEEASY_HTTP_TIMEOUT", 10*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 10*time.Second), Transport: &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, }, @@ -52,15 +66,15 @@ type DNSProvider struct { // Credentials must be passed in the environment variables: // DNSMADEEASY_API_KEY and DNSMADEEASY_API_SECRET. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("DNSMADEEASY_API_KEY", "DNSMADEEASY_API_SECRET") + values, err := env.Get(EnvAPIKey, EnvAPISecret) if err != nil { return nil, fmt.Errorf("dnsmadeeasy: %w", err) } config := NewDefaultConfig() - config.Sandbox = env.GetOrDefaultBool("DNSMADEEASY_SANDBOX", false) - config.APIKey = values["DNSMADEEASY_API_KEY"] - config.APISecret = values["DNSMADEEASY_API_SECRET"] + config.Sandbox = env.GetOrDefaultBool(EnvSandbox, false) + config.APIKey = values[EnvAPIKey] + config.APISecret = values[EnvAPISecret] return NewDNSProviderConfig(config) } diff --git a/providers/dns/dnsmadeeasy/dnsmadeeasy_test.go b/providers/dns/dnsmadeeasy/dnsmadeeasy_test.go index 54534936..fb37b20f 100644 --- a/providers/dns/dnsmadeeasy/dnsmadeeasy_test.go +++ b/providers/dns/dnsmadeeasy/dnsmadeeasy_test.go @@ -8,13 +8,15 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "DNSMADEEASY_API_KEY", - "DNSMADEEASY_API_SECRET"). - WithDomain("DNSMADEEASY_DOMAIN") + EnvAPIKey, + EnvAPISecret). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { - os.Setenv("DNSMADEEASY_SANDBOX", "true") + os.Setenv(EnvSandbox, "true") testCases := []struct { desc string @@ -24,31 +26,31 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "DNSMADEEASY_API_KEY": "123", - "DNSMADEEASY_API_SECRET": "456", + EnvAPIKey: "123", + EnvAPISecret: "456", }, }, { desc: "missing credentials", envVars: map[string]string{ - "DNSMADEEASY_API_KEY": "", - "DNSMADEEASY_API_SECRET": "", + EnvAPIKey: "", + EnvAPISecret: "", }, expected: "dnsmadeeasy: some credentials information are missing: DNSMADEEASY_API_KEY,DNSMADEEASY_API_SECRET", }, { desc: "missing access key", envVars: map[string]string{ - "DNSMADEEASY_API_KEY": "", - "DNSMADEEASY_API_SECRET": "456", + EnvAPIKey: "", + EnvAPISecret: "456", }, expected: "dnsmadeeasy: some credentials information are missing: DNSMADEEASY_API_KEY", }, { desc: "missing secret key", envVars: map[string]string{ - "DNSMADEEASY_API_KEY": "123", - "DNSMADEEASY_API_SECRET": "", + EnvAPIKey: "123", + EnvAPISecret: "", }, expected: "dnsmadeeasy: some credentials information are missing: DNSMADEEASY_API_SECRET", }, @@ -76,7 +78,7 @@ func TestNewDNSProvider(t *testing.T) { } func TestNewDNSProviderConfig(t *testing.T) { - os.Setenv("DNSMADEEASY_SANDBOX", "true") + os.Setenv(EnvSandbox, "true") testCases := []struct { desc string @@ -130,7 +132,7 @@ func TestLivePresentAndCleanup(t *testing.T) { t.Skip("skipping live test") } - os.Setenv("DNSMADEEASY_SANDBOX", "true") + os.Setenv(EnvSandbox, "true") envTest.RestoreEnv() provider, err := NewDNSProvider() diff --git a/providers/dns/dnspod/dnspod.go b/providers/dns/dnspod/dnspod.go index a3e1eb73..af1078a0 100644 --- a/providers/dns/dnspod/dnspod.go +++ b/providers/dns/dnspod/dnspod.go @@ -14,6 +14,18 @@ import ( "github.com/nrdcg/dnspod-go" ) +// Environment variables names. +const ( + envNamespace = "DNSPOD_" + + EnvAPIKey = envNamespace + "API_KEY" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { LoginToken string @@ -26,11 +38,11 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("DNSPOD_TTL", 600), - PropagationTimeout: env.GetOrDefaultSecond("DNSPOD_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("DNSPOD_POLLING_INTERVAL", dns01.DefaultPollingInterval), + TTL: env.GetOrDefaultInt(EnvTTL, 600), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("DNSPOD_HTTP_TIMEOUT", 0), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30), }, } } @@ -44,13 +56,13 @@ type DNSProvider struct { // NewDNSProvider returns a DNSProvider instance configured for dnspod. // Credentials must be passed in the environment variables: DNSPOD_API_KEY. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("DNSPOD_API_KEY") + values, err := env.Get(EnvAPIKey) if err != nil { return nil, fmt.Errorf("dnspod: %w", err) } config := NewDefaultConfig() - config.LoginToken = values["DNSPOD_API_KEY"] + config.LoginToken = values[EnvAPIKey] return NewDNSProviderConfig(config) } diff --git a/providers/dns/dnspod/dnspod_test.go b/providers/dns/dnspod/dnspod_test.go index 79f3d066..cb5a5f22 100644 --- a/providers/dns/dnspod/dnspod_test.go +++ b/providers/dns/dnspod/dnspod_test.go @@ -8,8 +8,10 @@ import ( "github.com/stretchr/testify/require" ) -var envTest = tester.NewEnvTest("DNSPOD_API_KEY"). - WithDomain("DNSPOD_DOMAIN") +const envDomain = envNamespace + "DOMAIN" + +var envTest = tester.NewEnvTest(EnvAPIKey). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -20,13 +22,13 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "DNSPOD_API_KEY": "123", + EnvAPIKey: "123", }, }, { desc: "missing api key", envVars: map[string]string{ - "DNSPOD_API_KEY": "", + EnvAPIKey: "", }, expected: "dnspod: some credentials information are missing: DNSPOD_API_KEY", }, diff --git a/providers/dns/dode/dode.go b/providers/dns/dode/dode.go index cff34867..c531c830 100644 --- a/providers/dns/dode/dode.go +++ b/providers/dns/dode/dode.go @@ -11,6 +11,18 @@ import ( "github.com/go-acme/lego/v3/platform/config/env" ) +// Environment variables names. +const ( + envNamespace = "DODE_" + + EnvToken = envNamespace + "TOKEN" + + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" + EnvSequenceInterval = envNamespace + "SEQUENCE_INTERVAL" +) + // Config is used to configure the creation of the DNSProvider type Config struct { Token string @@ -23,11 +35,11 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - PropagationTimeout: env.GetOrDefaultSecond("DODE_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("DODE_POLLING_INTERVAL", dns01.DefaultPollingInterval), - SequenceInterval: env.GetOrDefaultSecond("DODE_SEQUENCE_INTERVAL", dns01.DefaultPropagationTimeout), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), + SequenceInterval: env.GetOrDefaultSecond(EnvSequenceInterval, dns01.DefaultPropagationTimeout), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("DODE_HTTP_TIMEOUT", 30*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), }, } } @@ -40,13 +52,13 @@ type DNSProvider struct { // NewDNSProvider returns a new DNS provider using // environment variable DODE_TOKEN for adding and removing the DNS record. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("DODE_TOKEN") + values, err := env.Get(EnvToken) if err != nil { return nil, fmt.Errorf("do.de: %w", err) } config := NewDefaultConfig() - config.Token = values["DODE_TOKEN"] + config.Token = values[EnvToken] return NewDNSProviderConfig(config) } diff --git a/providers/dns/dode/dode_test.go b/providers/dns/dode/dode_test.go index 83e72746..942983cd 100644 --- a/providers/dns/dode/dode_test.go +++ b/providers/dns/dode/dode_test.go @@ -8,8 +8,10 @@ import ( "github.com/stretchr/testify/require" ) -var envTest = tester.NewEnvTest("DODE_TOKEN"). - WithDomain("DODE_DOMAIN") +const envDomain = envNamespace + "DOMAIN" + +var envTest = tester.NewEnvTest(EnvToken). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -20,13 +22,13 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "DODE_TOKEN": "123", + EnvToken: "123", }, }, { desc: "missing api key", envVars: map[string]string{ - "DODE_TOKEN": "", + EnvToken: "", }, expected: "do.de: some credentials information are missing: DODE_TOKEN", }, diff --git a/providers/dns/dreamhost/dreamhost.go b/providers/dns/dreamhost/dreamhost.go index 637e84c8..822ca41e 100644 --- a/providers/dns/dreamhost/dreamhost.go +++ b/providers/dns/dreamhost/dreamhost.go @@ -13,6 +13,17 @@ import ( "github.com/go-acme/lego/v3/platform/config/env" ) +// Environment variables names. +const ( + envNamespace = "DREAMHOST_" + + EnvAPIKey = envNamespace + "API_KEY" + + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { BaseURL string @@ -26,10 +37,10 @@ type Config struct { func NewDefaultConfig() *Config { return &Config{ BaseURL: defaultBaseURL, - PropagationTimeout: env.GetOrDefaultSecond("DREAMHOST_PROPAGATION_TIMEOUT", 60*time.Minute), - PollingInterval: env.GetOrDefaultSecond("DREAMHOST_POLLING_INTERVAL", 1*time.Minute), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 60*time.Minute), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 1*time.Minute), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("DREAMHOST_HTTP_TIMEOUT", 30*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), }, } } @@ -42,13 +53,13 @@ type DNSProvider struct { // NewDNSProvider returns a new DNS provider using // environment variable DREAMHOST_API_KEY for adding and removing the DNS record. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("DREAMHOST_API_KEY") + values, err := env.Get(EnvAPIKey) if err != nil { return nil, fmt.Errorf("dreamhost: %w", err) } config := NewDefaultConfig() - config.APIKey = values["DREAMHOST_API_KEY"] + config.APIKey = values[EnvAPIKey] return NewDNSProviderConfig(config) } diff --git a/providers/dns/dreamhost/dreamhost_test.go b/providers/dns/dreamhost/dreamhost_test.go index bb67e2ff..ab40ca6a 100644 --- a/providers/dns/dreamhost/dreamhost_test.go +++ b/providers/dns/dreamhost/dreamhost_test.go @@ -12,8 +12,10 @@ import ( "github.com/stretchr/testify/require" ) -var envTest = tester.NewEnvTest("DREAMHOST_API_KEY"). - WithDomain("DREAMHOST_TEST_DOMAIN") +const envDomain = envNamespace + "DOMAIN" + +var envTest = tester.NewEnvTest(EnvAPIKey). + WithDomain(envDomain) const ( fakeAPIKey = "asdf1234" @@ -46,13 +48,13 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "DREAMHOST_API_KEY": "123", + EnvAPIKey: "123", }, }, { desc: "missing API key", envVars: map[string]string{ - "DREAMHOST_API_KEY": "", + EnvAPIKey: "", }, expected: "dreamhost: some credentials information are missing: DREAMHOST_API_KEY", }, diff --git a/providers/dns/duckdns/duckdns.go b/providers/dns/duckdns/duckdns.go index ba8997dd..e58fee7d 100644 --- a/providers/dns/duckdns/duckdns.go +++ b/providers/dns/duckdns/duckdns.go @@ -12,6 +12,18 @@ import ( "github.com/go-acme/lego/v3/platform/config/env" ) +// Environment variables names. +const ( + envNamespace = "DUCKDNS_" + + EnvToken = envNamespace + "TOKEN" + + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" + EnvSequenceInterval = envNamespace + "SEQUENCE_INTERVAL" +) + // Config is used to configure the creation of the DNSProvider type Config struct { Token string @@ -24,11 +36,11 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - PropagationTimeout: env.GetOrDefaultSecond("DUCKDNS_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("DUCKDNS_POLLING_INTERVAL", dns01.DefaultPollingInterval), - SequenceInterval: env.GetOrDefaultSecond("DUCKDNS_SEQUENCE_INTERVAL", dns01.DefaultPropagationTimeout), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), + SequenceInterval: env.GetOrDefaultSecond(EnvSequenceInterval, dns01.DefaultPropagationTimeout), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("DUCKDNS_HTTP_TIMEOUT", 30*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), }, } } @@ -41,13 +53,13 @@ type DNSProvider struct { // NewDNSProvider returns a new DNS provider using // environment variable DUCKDNS_TOKEN for adding and removing the DNS record. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("DUCKDNS_TOKEN") + values, err := env.Get(EnvToken) if err != nil { return nil, fmt.Errorf("duckdns: %w", err) } config := NewDefaultConfig() - config.Token = values["DUCKDNS_TOKEN"] + config.Token = values[EnvToken] return NewDNSProviderConfig(config) } diff --git a/providers/dns/duckdns/duckdns_test.go b/providers/dns/duckdns/duckdns_test.go index 44821c15..ea8dfe73 100644 --- a/providers/dns/duckdns/duckdns_test.go +++ b/providers/dns/duckdns/duckdns_test.go @@ -9,8 +9,10 @@ import ( "github.com/stretchr/testify/require" ) -var envTest = tester.NewEnvTest("DUCKDNS_TOKEN"). - WithDomain("DUCKDNS_DOMAIN") +const envDomain = envNamespace + "DOMAIN" + +var envTest = tester.NewEnvTest(EnvToken). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -21,13 +23,13 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "DUCKDNS_TOKEN": "123", + EnvToken: "123", }, }, { desc: "missing api key", envVars: map[string]string{ - "DUCKDNS_TOKEN": "", + EnvToken: "", }, expected: "duckdns: some credentials information are missing: DUCKDNS_TOKEN", }, diff --git a/providers/dns/dyn/dyn.go b/providers/dns/dyn/dyn.go index ecc3b503..77a3dc33 100644 --- a/providers/dns/dyn/dyn.go +++ b/providers/dns/dyn/dyn.go @@ -12,6 +12,20 @@ import ( "github.com/go-acme/lego/v3/platform/config/env" ) +// Environment variables names. +const ( + envNamespace = "DYN_" + + EnvCustomerName = envNamespace + "CUSTOMER_NAME" + EnvUserName = envNamespace + "USER_NAME" + EnvPassword = envNamespace + "PASSWORD" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { CustomerName string @@ -26,11 +40,11 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("DYN_TTL", dns01.DefaultTTL), - PropagationTimeout: env.GetOrDefaultSecond("DYN_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("DYN_POLLING_INTERVAL", dns01.DefaultPollingInterval), + TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("DYN_HTTP_TIMEOUT", 10*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 10*time.Second), }, } } @@ -46,15 +60,15 @@ type DNSProvider struct { // Credentials must be passed in the environment variables: // DYN_CUSTOMER_NAME, DYN_USER_NAME and DYN_PASSWORD. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("DYN_CUSTOMER_NAME", "DYN_USER_NAME", "DYN_PASSWORD") + values, err := env.Get(EnvCustomerName, EnvUserName, EnvPassword) if err != nil { return nil, fmt.Errorf("dyn: %w", err) } config := NewDefaultConfig() - config.CustomerName = values["DYN_CUSTOMER_NAME"] - config.UserName = values["DYN_USER_NAME"] - config.Password = values["DYN_PASSWORD"] + config.CustomerName = values[EnvCustomerName] + config.UserName = values[EnvUserName] + config.Password = values[EnvPassword] return NewDNSProviderConfig(config) } diff --git a/providers/dns/dyn/dyn_test.go b/providers/dns/dyn/dyn_test.go index 56537bba..fcb1f048 100644 --- a/providers/dns/dyn/dyn_test.go +++ b/providers/dns/dyn/dyn_test.go @@ -8,11 +8,13 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "DYN_CUSTOMER_NAME", - "DYN_USER_NAME", - "DYN_PASSWORD"). - WithDomain("DYN_DOMAIN") + EnvCustomerName, + EnvUserName, + EnvPassword). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -23,44 +25,44 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "DYN_CUSTOMER_NAME": "A", - "DYN_USER_NAME": "B", - "DYN_PASSWORD": "C", + EnvCustomerName: "A", + EnvUserName: "B", + EnvPassword: "C", }, }, { desc: "missing credentials", envVars: map[string]string{ - "DYN_CUSTOMER_NAME": "", - "DYN_USER_NAME": "", - "DYN_PASSWORD": "", + EnvCustomerName: "", + EnvUserName: "", + EnvPassword: "", }, expected: "dyn: some credentials information are missing: DYN_CUSTOMER_NAME,DYN_USER_NAME,DYN_PASSWORD", }, { desc: "missing customer name", envVars: map[string]string{ - "DYN_CUSTOMER_NAME": "", - "DYN_USER_NAME": "B", - "DYN_PASSWORD": "C", + EnvCustomerName: "", + EnvUserName: "B", + EnvPassword: "C", }, expected: "dyn: some credentials information are missing: DYN_CUSTOMER_NAME", }, { desc: "missing password", envVars: map[string]string{ - "DYN_CUSTOMER_NAME": "A", - "DYN_USER_NAME": "", - "DYN_PASSWORD": "C", + EnvCustomerName: "A", + EnvUserName: "", + EnvPassword: "C", }, expected: "dyn: some credentials information are missing: DYN_USER_NAME", }, { desc: "missing username", envVars: map[string]string{ - "DYN_CUSTOMER_NAME": "A", - "DYN_USER_NAME": "B", - "DYN_PASSWORD": "", + EnvCustomerName: "A", + EnvUserName: "B", + EnvPassword: "", }, expected: "dyn: some credentials information are missing: DYN_PASSWORD", }, diff --git a/providers/dns/dynu/dynu.go b/providers/dns/dynu/dynu.go index 63e4c752..3158e132 100644 --- a/providers/dns/dynu/dynu.go +++ b/providers/dns/dynu/dynu.go @@ -123,7 +123,7 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error { NodeName: dns01.UnFqdn(strings.TrimSuffix(fqdn, dns.Fqdn(domain))), TextData: value, State: true, - TTL: 300, + TTL: d.config.TTL, } err = d.client.AddNewRecord(rootDomain.ID, record) diff --git a/providers/dns/dynu/dynu_test.go b/providers/dns/dynu/dynu_test.go index 94c10aaf..d6ad7e41 100644 --- a/providers/dns/dynu/dynu_test.go +++ b/providers/dns/dynu/dynu_test.go @@ -8,7 +8,7 @@ import ( "github.com/stretchr/testify/require" ) -const envDomain = envNamespace + "_DOMAIN" +const envDomain = envNamespace + "DOMAIN" var envTest = tester.NewEnvTest( EnvAPIKey). diff --git a/providers/dns/easydns/easydns.go b/providers/dns/easydns/easydns.go index 38a2baaf..ec96fa7b 100644 --- a/providers/dns/easydns/easydns.go +++ b/providers/dns/easydns/easydns.go @@ -16,6 +16,21 @@ import ( "github.com/miekg/dns" ) +// Environment variables names. +const ( + envNamespace = "EASYDNS_" + + EnvEndpoint = envNamespace + "ENDPOINT" + EnvToken = envNamespace + "TOKEN" + EnvKey = envNamespace + "KEY" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" + EnvSequenceInterval = envNamespace + "SEQUENCE_INTERVAL" +) + // Config is used to configure the creation of the DNSProvider type Config struct { Endpoint *url.URL @@ -31,12 +46,12 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - PropagationTimeout: env.GetOrDefaultSecond("EASYDNS_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - SequenceInterval: env.GetOrDefaultSecond("EASYDNS_SEQUENCE_INTERVAL", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("EASYDNS_POLLING_INTERVAL", dns01.DefaultPollingInterval), - TTL: env.GetOrDefaultInt("EASYDNS_TTL", dns01.DefaultTTL), + TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), + SequenceInterval: env.GetOrDefaultSecond(EnvSequenceInterval, dns01.DefaultPropagationTimeout), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("EASYDNS_HTTP_TIMEOUT", 30*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), }, } } @@ -52,19 +67,19 @@ type DNSProvider struct { func NewDNSProvider() (*DNSProvider, error) { config := NewDefaultConfig() - endpoint, err := url.Parse(env.GetOrDefaultString("EASYDNS_ENDPOINT", defaultEndpoint)) + endpoint, err := url.Parse(env.GetOrDefaultString(EnvEndpoint, defaultEndpoint)) if err != nil { return nil, fmt.Errorf("easydns: %w", err) } config.Endpoint = endpoint - values, err := env.Get("EASYDNS_TOKEN", "EASYDNS_KEY") + values, err := env.Get(EnvToken, EnvKey) if err != nil { return nil, fmt.Errorf("easydns: %w", err) } - config.Token = values["EASYDNS_TOKEN"] - config.Key = values["EASYDNS_KEY"] + config.Token = values[EnvToken] + config.Key = values[EnvKey] return NewDNSProviderConfig(config) } diff --git a/providers/dns/easydns/easydns_test.go b/providers/dns/easydns/easydns_test.go index a22c22fd..dd8751ae 100644 --- a/providers/dns/easydns/easydns_test.go +++ b/providers/dns/easydns/easydns_test.go @@ -14,11 +14,13 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "EASYDNS_ENDPOINT", - "EASYDNS_TOKEN", - "EASYDNS_KEY"). - WithDomain("EASYDNS_DOMAIN") + EnvEndpoint, + EnvToken, + EnvKey). + WithDomain(envDomain) func setup() (*DNSProvider, *http.ServeMux, func()) { handler := http.NewServeMux() @@ -51,21 +53,21 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "EASYDNS_TOKEN": "TOKEN", - "EASYDNS_KEY": "SECRET", + EnvToken: "TOKEN", + EnvKey: "SECRET", }, }, { desc: "missing token", envVars: map[string]string{ - "EASYDNS_KEY": "SECRET", + EnvKey: "SECRET", }, expected: "easydns: some credentials information are missing: EASYDNS_TOKEN", }, { desc: "missing key", envVars: map[string]string{ - "EASYDNS_TOKEN": "TOKEN", + EnvToken: "TOKEN", }, expected: "easydns: some credentials information are missing: EASYDNS_KEY", }, diff --git a/providers/dns/exec/exec.go b/providers/dns/exec/exec.go index f66125be..93f02357 100644 --- a/providers/dns/exec/exec.go +++ b/providers/dns/exec/exec.go @@ -13,6 +13,17 @@ import ( "github.com/go-acme/lego/v3/platform/config/env" ) +// Environment variables names. +const ( + envNamespace = "EXEC_" + + EnvPath = envNamespace + "PATH" + EnvMode = envNamespace + "MODE" + + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" +) + // Config Provider configuration. type Config struct { Program string @@ -24,8 +35,8 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - PropagationTimeout: env.GetOrDefaultSecond("EXEC_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("EXEC_POLLING_INTERVAL", dns01.DefaultPollingInterval), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), } } @@ -38,14 +49,14 @@ type DNSProvider struct { // NewDNSProvider returns a new DNS provider which runs the program in the // environment variable EXEC_PATH for adding and removing the DNS record. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("EXEC_PATH") + values, err := env.Get(EnvPath) if err != nil { return nil, fmt.Errorf("exec: %w", err) } config := NewDefaultConfig() - config.Program = values["EXEC_PATH"] - config.Mode = os.Getenv("EXEC_MODE") + config.Program = values[EnvPath] + config.Mode = os.Getenv(EnvMode) return NewDNSProviderConfig(config) } diff --git a/providers/dns/exoscale/exoscale.go b/providers/dns/exoscale/exoscale.go index 0fa8a690..21be7f37 100644 --- a/providers/dns/exoscale/exoscale.go +++ b/providers/dns/exoscale/exoscale.go @@ -15,6 +15,20 @@ import ( const defaultBaseURL = "https://api.exoscale.com/dns" +// Environment variables names. +const ( + envNamespace = "EXOSCALE_" + + EnvAPISecret = envNamespace + "API_SECRET" + EnvAPIKey = envNamespace + "API_KEY" + EnvEndpoint = envNamespace + "ENDPOINT" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { APIKey string @@ -29,11 +43,11 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("EXOSCALE_TTL", dns01.DefaultTTL), - PropagationTimeout: env.GetOrDefaultSecond("EXOSCALE_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("EXOSCALE_POLLING_INTERVAL", dns01.DefaultPollingInterval), + TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("EXOSCALE_HTTP_TIMEOUT", 0), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30), }, } } @@ -47,15 +61,15 @@ type DNSProvider struct { // NewDNSProvider Credentials must be passed in the environment variables: // EXOSCALE_API_KEY, EXOSCALE_API_SECRET, EXOSCALE_ENDPOINT. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("EXOSCALE_API_KEY", "EXOSCALE_API_SECRET") + values, err := env.Get(EnvAPIKey, EnvAPISecret) if err != nil { return nil, fmt.Errorf("exoscale: %w", err) } config := NewDefaultConfig() - config.APIKey = values["EXOSCALE_API_KEY"] - config.APISecret = values["EXOSCALE_API_SECRET"] - config.Endpoint = env.GetOrFile("EXOSCALE_ENDPOINT") + config.APIKey = values[EnvAPIKey] + config.APISecret = values[EnvAPISecret] + config.Endpoint = env.GetOrFile(EnvEndpoint) return NewDNSProviderConfig(config) } diff --git a/providers/dns/exoscale/exoscale_test.go b/providers/dns/exoscale/exoscale_test.go index 52b887bd..965ff42e 100644 --- a/providers/dns/exoscale/exoscale_test.go +++ b/providers/dns/exoscale/exoscale_test.go @@ -9,10 +9,12 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "EXOSCALE_API_SECRET", - "EXOSCALE_API_KEY"). - WithDomain("EXOSCALE_DOMAIN") + EnvAPISecret, + EnvAPIKey). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -23,31 +25,31 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "EXOSCALE_API_KEY": "123", - "EXOSCALE_API_SECRET": "456", + EnvAPIKey: "123", + EnvAPISecret: "456", }, }, { desc: "missing credentials", envVars: map[string]string{ - "EXOSCALE_API_KEY": "", - "EXOSCALE_API_SECRET": "", + EnvAPIKey: "", + EnvAPISecret: "", }, expected: "exoscale: some credentials information are missing: EXOSCALE_API_KEY,EXOSCALE_API_SECRET", }, { desc: "missing access key", envVars: map[string]string{ - "EXOSCALE_API_KEY": "", - "EXOSCALE_API_SECRET": "456", + EnvAPIKey: "", + EnvAPISecret: "456", }, expected: "exoscale: some credentials information are missing: EXOSCALE_API_KEY", }, { desc: "missing secret key", envVars: map[string]string{ - "EXOSCALE_API_KEY": "123", - "EXOSCALE_API_SECRET": "", + EnvAPIKey: "123", + EnvAPISecret: "", }, expected: "exoscale: some credentials information are missing: EXOSCALE_API_SECRET", }, diff --git a/providers/dns/fastdns/fastdns.go b/providers/dns/fastdns/fastdns.go index 5b4d2242..17f34053 100644 --- a/providers/dns/fastdns/fastdns.go +++ b/providers/dns/fastdns/fastdns.go @@ -13,6 +13,20 @@ import ( "github.com/go-acme/lego/v3/platform/config/env" ) +// Environment variables names. +const ( + envNamespace = "AKAMAI_" + + EnvHost = envNamespace + "HOST" + EnvClientToken = envNamespace + "CLIENT_TOKEN" + EnvClientSecret = envNamespace + "CLIENT_SECRET" + EnvAccessToken = envNamespace + "ACCESS_TOKEN" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" +) + // Config is used to configure the creation of the DNSProvider type Config struct { edgegrid.Config @@ -24,9 +38,9 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - PropagationTimeout: env.GetOrDefaultSecond("AKAMAI_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("AKAMAI_POLLING_INTERVAL", dns01.DefaultPollingInterval), - TTL: env.GetOrDefaultInt("AKAMAI_TTL", dns01.DefaultTTL), + TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), } } @@ -38,17 +52,17 @@ type DNSProvider struct { // NewDNSProvider uses the supplied environment variables to return a DNSProvider instance: // AKAMAI_HOST, AKAMAI_CLIENT_TOKEN, AKAMAI_CLIENT_SECRET, AKAMAI_ACCESS_TOKEN func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("AKAMAI_HOST", "AKAMAI_CLIENT_TOKEN", "AKAMAI_CLIENT_SECRET", "AKAMAI_ACCESS_TOKEN") + values, err := env.Get(EnvHost, EnvClientToken, EnvClientSecret, EnvAccessToken) if err != nil { return nil, fmt.Errorf("fastdns: %w", err) } config := NewDefaultConfig() config.Config = edgegrid.Config{ - Host: values["AKAMAI_HOST"], - ClientToken: values["AKAMAI_CLIENT_TOKEN"], - ClientSecret: values["AKAMAI_CLIENT_SECRET"], - AccessToken: values["AKAMAI_ACCESS_TOKEN"], + Host: values[EnvHost], + ClientToken: values[EnvClientToken], + ClientSecret: values[EnvClientSecret], + AccessToken: values[EnvAccessToken], MaxBody: 131072, } diff --git a/providers/dns/fastdns/fastdns_test.go b/providers/dns/fastdns/fastdns_test.go index 85bf59e6..907a8919 100644 --- a/providers/dns/fastdns/fastdns_test.go +++ b/providers/dns/fastdns/fastdns_test.go @@ -9,12 +9,14 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "TEST_DOMAIN" + var envTest = tester.NewEnvTest( - "AKAMAI_HOST", - "AKAMAI_CLIENT_TOKEN", - "AKAMAI_CLIENT_SECRET", - "AKAMAI_ACCESS_TOKEN"). - WithDomain("AKAMAI_TEST_DOMAIN") + EnvHost, + EnvClientToken, + EnvClientSecret, + EnvAccessToken). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -25,59 +27,59 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "AKAMAI_HOST": "A", - "AKAMAI_CLIENT_TOKEN": "B", - "AKAMAI_CLIENT_SECRET": "C", - "AKAMAI_ACCESS_TOKEN": "D", + EnvHost: "A", + EnvClientToken: "B", + EnvClientSecret: "C", + EnvAccessToken: "D", }, }, { desc: "missing credentials", envVars: map[string]string{ - "AKAMAI_HOST": "", - "AKAMAI_CLIENT_TOKEN": "", - "AKAMAI_CLIENT_SECRET": "", - "AKAMAI_ACCESS_TOKEN": "", + EnvHost: "", + EnvClientToken: "", + EnvClientSecret: "", + EnvAccessToken: "", }, expected: "fastdns: some credentials information are missing: AKAMAI_HOST,AKAMAI_CLIENT_TOKEN,AKAMAI_CLIENT_SECRET,AKAMAI_ACCESS_TOKEN", }, { desc: "missing host", envVars: map[string]string{ - "AKAMAI_HOST": "", - "AKAMAI_CLIENT_TOKEN": "B", - "AKAMAI_CLIENT_SECRET": "C", - "AKAMAI_ACCESS_TOKEN": "D", + EnvHost: "", + EnvClientToken: "B", + EnvClientSecret: "C", + EnvAccessToken: "D", }, expected: "fastdns: some credentials information are missing: AKAMAI_HOST", }, { desc: "missing client token", envVars: map[string]string{ - "AKAMAI_HOST": "A", - "AKAMAI_CLIENT_TOKEN": "", - "AKAMAI_CLIENT_SECRET": "C", - "AKAMAI_ACCESS_TOKEN": "D", + EnvHost: "A", + EnvClientToken: "", + EnvClientSecret: "C", + EnvAccessToken: "D", }, expected: "fastdns: some credentials information are missing: AKAMAI_CLIENT_TOKEN", }, { desc: "missing client secret", envVars: map[string]string{ - "AKAMAI_HOST": "A", - "AKAMAI_CLIENT_TOKEN": "B", - "AKAMAI_CLIENT_SECRET": "", - "AKAMAI_ACCESS_TOKEN": "D", + EnvHost: "A", + EnvClientToken: "B", + EnvClientSecret: "", + EnvAccessToken: "D", }, expected: "fastdns: some credentials information are missing: AKAMAI_CLIENT_SECRET", }, { desc: "missing access token", envVars: map[string]string{ - "AKAMAI_HOST": "A", - "AKAMAI_CLIENT_TOKEN": "B", - "AKAMAI_CLIENT_SECRET": "C", - "AKAMAI_ACCESS_TOKEN": "", + EnvHost: "A", + EnvClientToken: "B", + EnvClientSecret: "C", + EnvAccessToken: "", }, expected: "fastdns: some credentials information are missing: AKAMAI_ACCESS_TOKEN", }, diff --git a/providers/dns/gandi/gandi.go b/providers/dns/gandi/gandi.go index 2c84e6af..ae5ec10f 100644 --- a/providers/dns/gandi/gandi.go +++ b/providers/dns/gandi/gandi.go @@ -22,6 +22,18 @@ const ( minTTL = 300 ) +// Environment variables names. +const ( + envNamespace = "GANDI_" + + EnvAPIKey = envNamespace + "API_KEY" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { BaseURL string @@ -35,11 +47,11 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("GANDI_TTL", minTTL), - PropagationTimeout: env.GetOrDefaultSecond("GANDI_PROPAGATION_TIMEOUT", 40*time.Minute), - PollingInterval: env.GetOrDefaultSecond("GANDI_POLLING_INTERVAL", 60*time.Second), + TTL: env.GetOrDefaultInt(EnvTTL, minTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 40*time.Minute), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 60*time.Second), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("GANDI_HTTP_TIMEOUT", 60*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 60*time.Second), }, } } @@ -66,13 +78,13 @@ type DNSProvider struct { // NewDNSProvider returns a DNSProvider instance configured for Gandi. // Credentials must be passed in the environment variable: GANDI_API_KEY. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("GANDI_API_KEY") + values, err := env.Get(EnvAPIKey) if err != nil { return nil, fmt.Errorf("gandi: %w", err) } config := NewDefaultConfig() - config.APIKey = values["GANDI_API_KEY"] + config.APIKey = values[EnvAPIKey] return NewDNSProviderConfig(config) } diff --git a/providers/dns/gandi/gandi_test.go b/providers/dns/gandi/gandi_test.go index 4085784b..60985286 100644 --- a/providers/dns/gandi/gandi_test.go +++ b/providers/dns/gandi/gandi_test.go @@ -13,7 +13,7 @@ import ( "github.com/stretchr/testify/require" ) -var envTest = tester.NewEnvTest("GANDI_API_KEY") +var envTest = tester.NewEnvTest(EnvAPIKey) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -24,13 +24,13 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "GANDI_API_KEY": "123", + EnvAPIKey: "123", }, }, { desc: "missing api key", envVars: map[string]string{ - "GANDI_API_KEY": "", + EnvAPIKey: "", }, expected: "gandi: some credentials information are missing: GANDI_API_KEY", }, diff --git a/providers/dns/gandiv5/gandiv5.go b/providers/dns/gandiv5/gandiv5.go index 4b83361e..0ad79e7f 100644 --- a/providers/dns/gandiv5/gandiv5.go +++ b/providers/dns/gandiv5/gandiv5.go @@ -21,6 +21,18 @@ const ( minTTL = 300 ) +// Environment variables names. +const ( + envNamespace = "GANDIV5_" + + EnvAPIKey = envNamespace + "API_KEY" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // inProgressInfo contains information about an in-progress challenge type inProgressInfo struct { fieldName string @@ -40,11 +52,11 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("GANDIV5_TTL", minTTL), - PropagationTimeout: env.GetOrDefaultSecond("GANDIV5_PROPAGATION_TIMEOUT", 20*time.Minute), - PollingInterval: env.GetOrDefaultSecond("GANDIV5_POLLING_INTERVAL", 20*time.Second), + TTL: env.GetOrDefaultInt(EnvTTL, minTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 20*time.Minute), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 20*time.Second), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("GANDIV5_HTTP_TIMEOUT", 10*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 10*time.Second), }, } } @@ -63,13 +75,13 @@ type DNSProvider struct { // NewDNSProvider returns a DNSProvider instance configured for Gandi. // Credentials must be passed in the environment variable: GANDIV5_API_KEY. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("GANDIV5_API_KEY") + values, err := env.Get(EnvAPIKey) if err != nil { return nil, fmt.Errorf("gandi: %w", err) } config := NewDefaultConfig() - config.APIKey = values["GANDIV5_API_KEY"] + config.APIKey = values[EnvAPIKey] return NewDNSProviderConfig(config) } diff --git a/providers/dns/gandiv5/gandiv5_test.go b/providers/dns/gandiv5/gandiv5_test.go index 11e5438e..b076cee0 100644 --- a/providers/dns/gandiv5/gandiv5_test.go +++ b/providers/dns/gandiv5/gandiv5_test.go @@ -13,7 +13,7 @@ import ( "github.com/stretchr/testify/require" ) -var envTest = tester.NewEnvTest("GANDIV5_API_KEY") +var envTest = tester.NewEnvTest(EnvAPIKey) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -24,13 +24,13 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "GANDIV5_API_KEY": "123", + EnvAPIKey: "123", }, }, { desc: "missing api key", envVars: map[string]string{ - "GANDIV5_API_KEY": "", + EnvAPIKey: "", }, expected: "gandi: some credentials information are missing: GANDIV5_API_KEY", }, diff --git a/providers/dns/gcloud/googlecloud.go b/providers/dns/gcloud/googlecloud.go index 36d6bb4b..7a39d03f 100644 --- a/providers/dns/gcloud/googlecloud.go +++ b/providers/dns/gcloud/googlecloud.go @@ -26,6 +26,19 @@ const ( changeStatusDone = "done" ) +// Environment variables names. +const ( + envNamespace = "GCE_" + + EnvServiceAccount = envNamespace + "SERVICE_ACCOUNT" + EnvProject = envNamespace + "PROJECT" + EnvDebug = envNamespace + "DEBUG" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" +) + // Config is used to configure the creation of the DNSProvider type Config struct { Debug bool @@ -39,10 +52,10 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - Debug: env.GetOrDefaultBool("GCE_DEBUG", false), - TTL: env.GetOrDefaultInt("GCE_TTL", dns01.DefaultTTL), - PropagationTimeout: env.GetOrDefaultSecond("GCE_PROPAGATION_TIMEOUT", 180*time.Second), - PollingInterval: env.GetOrDefaultSecond("GCE_POLLING_INTERVAL", 5*time.Second), + Debug: env.GetOrDefaultBool(EnvDebug, false), + TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 180*time.Second), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 5*time.Second), } } @@ -59,12 +72,12 @@ type DNSProvider struct { // or by specifying the keyfile location: GCE_SERVICE_ACCOUNT_FILE func NewDNSProvider() (*DNSProvider, error) { // Use a service account file if specified via environment variable. - if saKey := env.GetOrFile("GCE_SERVICE_ACCOUNT"); len(saKey) > 0 { + if saKey := env.GetOrFile(EnvServiceAccount); len(saKey) > 0 { return NewDNSProviderServiceAccountKey([]byte(saKey)) } // Use default credentials. - project := env.GetOrDefaultString("GCE_PROJECT", autodetectProjectID()) + project := env.GetOrDefaultString(EnvProject, autodetectProjectID()) return NewDNSProviderCredentials(project) } @@ -96,7 +109,7 @@ func NewDNSProviderServiceAccountKey(saKey []byte) (*DNSProvider, error) { // If GCE_PROJECT is non-empty it overrides the project in the service // account file. - project := env.GetOrDefaultString("GCE_PROJECT", "") + project := env.GetOrDefaultString(EnvProject, "") if project == "" { // read project id from service account file var datJSON struct { diff --git a/providers/dns/gcloud/googlecloud_test.go b/providers/dns/gcloud/googlecloud_test.go index 3b991cfd..b14ce6db 100644 --- a/providers/dns/gcloud/googlecloud_test.go +++ b/providers/dns/gcloud/googlecloud_test.go @@ -16,13 +16,22 @@ import ( "google.golang.org/api/dns/v1" ) +const ( + envDomain = envNamespace + "DOMAIN" + + envServiceAccountFile = envNamespace + "SERVICE_ACCOUNT_FILE" + envMetadataHost = envNamespace + "METADATA_HOST" + + envGoogleApplicationCredentials = "GOOGLE_APPLICATION_CREDENTIALS" +) + var envTest = tester.NewEnvTest( - "GCE_PROJECT", - "GCE_SERVICE_ACCOUNT_FILE", - "GOOGLE_APPLICATION_CREDENTIALS", - "GCE_METADATA_HOST", - "GCE_SERVICE_ACCOUNT"). - WithDomain("GCE_DOMAIN"). + EnvProject, + envServiceAccountFile, + envGoogleApplicationCredentials, + envMetadataHost, + EnvServiceAccount). + WithDomain(envDomain). WithLiveTestExtra(func() bool { _, err := google.DefaultClient(context.Background(), dns.NdevClouddnsReadwriteScope) return err == nil @@ -37,36 +46,36 @@ func TestNewDNSProvider(t *testing.T) { { desc: "invalid credentials", envVars: map[string]string{ - "GCE_PROJECT": "123", - "GCE_SERVICE_ACCOUNT_FILE": "", + EnvProject: "123", + envServiceAccountFile: "", // as Travis run on GCE, we have to alter env - "GOOGLE_APPLICATION_CREDENTIALS": "not-a-secret-file", - "GCE_METADATA_HOST": "http://lego.wtf", // defined here to avoid the client cache. + envGoogleApplicationCredentials: "not-a-secret-file", + envMetadataHost: "http://lego.wtf", // defined here to avoid the client cache. }, expected: "googlecloud: unable to get Google Cloud client: google: error getting credentials using GOOGLE_APPLICATION_CREDENTIALS environment variable: open not-a-secret-file: no such file or directory", }, { desc: "missing project", envVars: map[string]string{ - "GCE_PROJECT": "", - "GCE_SERVICE_ACCOUNT_FILE": "", + EnvProject: "", + envServiceAccountFile: "", // as Travis run on GCE, we have to alter env - "GCE_METADATA_HOST": "http://lego.wtf", + envMetadataHost: "http://lego.wtf", }, expected: "googlecloud: project name missing", }, { desc: "success key file", envVars: map[string]string{ - "GCE_PROJECT": "", - "GCE_SERVICE_ACCOUNT_FILE": "fixtures/gce_account_service_file.json", + EnvProject: "", + envServiceAccountFile: "fixtures/gce_account_service_file.json", }, }, { desc: "success key", envVars: map[string]string{ - "GCE_PROJECT": "", - "GCE_SERVICE_ACCOUNT": `{"project_id": "A","type": "service_account","client_email": "foo@bar.com","private_key_id": "pki","private_key": "pk","token_uri": "/token","client_secret": "secret","client_id": "C","refresh_token": "D"}`, + EnvProject: "", + EnvServiceAccount: `{"project_id": "A","type": "service_account","client_email": "foo@bar.com","private_key_id": "pki","private_key": "pk","token_uri": "/token","client_secret": "secret","client_id": "C","refresh_token": "D"}`, }, }, } @@ -381,7 +390,7 @@ func TestLivePresent(t *testing.T) { envTest.RestoreEnv() - provider, err := NewDNSProviderCredentials(envTest.GetValue("GCE_PROJECT")) + provider, err := NewDNSProviderCredentials(envTest.GetValue(EnvProject)) require.NoError(t, err) err = provider.Present(envTest.GetDomain(), "", "123d==") @@ -395,7 +404,7 @@ func TestLivePresentMultiple(t *testing.T) { envTest.RestoreEnv() - provider, err := NewDNSProviderCredentials(envTest.GetValue("GCE_PROJECT")) + provider, err := NewDNSProviderCredentials(envTest.GetValue(EnvProject)) require.NoError(t, err) // Check that we're able to create multiple entries @@ -413,7 +422,7 @@ func TestLiveCleanUp(t *testing.T) { envTest.RestoreEnv() - provider, err := NewDNSProviderCredentials(envTest.GetValue("GCE_PROJECT")) + provider, err := NewDNSProviderCredentials(envTest.GetValue(EnvProject)) require.NoError(t, err) time.Sleep(1 * time.Second) diff --git a/providers/dns/glesys/glesys.go b/providers/dns/glesys/glesys.go index 2270fc44..b3da1783 100644 --- a/providers/dns/glesys/glesys.go +++ b/providers/dns/glesys/glesys.go @@ -19,6 +19,19 @@ const ( minTTL = 60 ) +// Environment variables names. +const ( + envNamespace = "GLESYS_" + + EnvAPIUser = envNamespace + "API_USER" + EnvAPIKey = envNamespace + "API_KEY" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { APIUser string @@ -32,11 +45,11 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("GLESYS_TTL", minTTL), - PropagationTimeout: env.GetOrDefaultSecond("GLESYS_PROPAGATION_TIMEOUT", 20*time.Minute), - PollingInterval: env.GetOrDefaultSecond("GLESYS_POLLING_INTERVAL", 20*time.Second), + TTL: env.GetOrDefaultInt(EnvTTL, minTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 20*time.Minute), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 20*time.Second), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("GLESYS_HTTP_TIMEOUT", 10*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 10*time.Second), }, } } @@ -54,14 +67,14 @@ type DNSProvider struct { // Credentials must be passed in the environment variables: // GLESYS_API_USER and GLESYS_API_KEY. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("GLESYS_API_USER", "GLESYS_API_KEY") + values, err := env.Get(EnvAPIUser, EnvAPIKey) if err != nil { return nil, fmt.Errorf("glesys: %w", err) } config := NewDefaultConfig() - config.APIUser = values["GLESYS_API_USER"] - config.APIKey = values["GLESYS_API_KEY"] + config.APIUser = values[EnvAPIUser] + config.APIKey = values[EnvAPIKey] return NewDNSProviderConfig(config) } diff --git a/providers/dns/glesys/glesys_test.go b/providers/dns/glesys/glesys_test.go index ec2d635a..2de6a11c 100644 --- a/providers/dns/glesys/glesys_test.go +++ b/providers/dns/glesys/glesys_test.go @@ -7,10 +7,12 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "GLESYS_API_USER", - "GLESYS_API_KEY"). - WithDomain("GLESYS_DOMAIN") + EnvAPIUser, + EnvAPIKey). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -21,31 +23,31 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "GLESYS_API_USER": "A", - "GLESYS_API_KEY": "B", + EnvAPIUser: "A", + EnvAPIKey: "B", }, }, { desc: "missing credentials", envVars: map[string]string{ - "GLESYS_API_USER": "", - "GLESYS_API_KEY": "", + EnvAPIUser: "", + EnvAPIKey: "", }, expected: "glesys: some credentials information are missing: GLESYS_API_USER,GLESYS_API_KEY", }, { desc: "missing api user", envVars: map[string]string{ - "GLESYS_API_USER": "", - "GLESYS_API_KEY": "B", + EnvAPIUser: "", + EnvAPIKey: "B", }, expected: "glesys: some credentials information are missing: GLESYS_API_USER", }, { desc: "missing api key", envVars: map[string]string{ - "GLESYS_API_USER": "A", - "GLESYS_API_KEY": "", + EnvAPIUser: "A", + EnvAPIKey: "", }, expected: "glesys: some credentials information are missing: GLESYS_API_KEY", }, diff --git a/providers/dns/godaddy/godaddy.go b/providers/dns/godaddy/godaddy.go index ee019904..071e9807 100644 --- a/providers/dns/godaddy/godaddy.go +++ b/providers/dns/godaddy/godaddy.go @@ -18,6 +18,20 @@ const ( minTTL = 600 ) +// Environment variables names. +const ( + envNamespace = "GODADDY_" + + EnvAPIKey = envNamespace + "API_KEY" + EnvAPISecret = envNamespace + "API_SECRET" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" + EnvSequenceInterval = envNamespace + "SEQUENCE_INTERVAL" +) + // Config is used to configure the creation of the DNSProvider type Config struct { APIKey string @@ -32,12 +46,12 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("GODADDY_TTL", minTTL), - PropagationTimeout: env.GetOrDefaultSecond("GODADDY_PROPAGATION_TIMEOUT", 120*time.Second), - PollingInterval: env.GetOrDefaultSecond("GODADDY_POLLING_INTERVAL", 2*time.Second), - SequenceInterval: env.GetOrDefaultSecond("GODADDY_SEQUENCE_INTERVAL", dns01.DefaultPropagationTimeout), + TTL: env.GetOrDefaultInt(EnvTTL, minTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 120*time.Second), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second), + SequenceInterval: env.GetOrDefaultSecond(EnvSequenceInterval, dns01.DefaultPropagationTimeout), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("GODADDY_HTTP_TIMEOUT", 30*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), }, } } @@ -51,14 +65,14 @@ type DNSProvider struct { // Credentials must be passed in the environment variables: // GODADDY_API_KEY and GODADDY_API_SECRET. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("GODADDY_API_KEY", "GODADDY_API_SECRET") + values, err := env.Get(EnvAPIKey, EnvAPISecret) if err != nil { return nil, fmt.Errorf("godaddy: %w", err) } config := NewDefaultConfig() - config.APIKey = values["GODADDY_API_KEY"] - config.APISecret = values["GODADDY_API_SECRET"] + config.APIKey = values[EnvAPIKey] + config.APISecret = values[EnvAPISecret] return NewDNSProviderConfig(config) } diff --git a/providers/dns/godaddy/godaddy_test.go b/providers/dns/godaddy/godaddy_test.go index ce968d0a..2f41124f 100644 --- a/providers/dns/godaddy/godaddy_test.go +++ b/providers/dns/godaddy/godaddy_test.go @@ -7,10 +7,12 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "GODADDY_API_KEY", - "GODADDY_API_SECRET"). - WithDomain("GODADDY_DOMAIN") + EnvAPIKey, + EnvAPISecret). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -21,31 +23,31 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "GODADDY_API_KEY": "123", - "GODADDY_API_SECRET": "456", + EnvAPIKey: "123", + EnvAPISecret: "456", }, }, { desc: "missing credentials", envVars: map[string]string{ - "GODADDY_API_KEY": "", - "GODADDY_API_SECRET": "", + EnvAPIKey: "", + EnvAPISecret: "", }, expected: "godaddy: some credentials information are missing: GODADDY_API_KEY,GODADDY_API_SECRET", }, { desc: "missing access key", envVars: map[string]string{ - "GODADDY_API_KEY": "", - "GODADDY_API_SECRET": "456", + EnvAPIKey: "", + EnvAPISecret: "456", }, expected: "godaddy: some credentials information are missing: GODADDY_API_KEY", }, { desc: "missing secret key", envVars: map[string]string{ - "GODADDY_API_KEY": "123", - "GODADDY_API_SECRET": "", + EnvAPIKey: "123", + EnvAPISecret: "", }, expected: "godaddy: some credentials information are missing: GODADDY_API_SECRET", }, diff --git a/providers/dns/hostingde/hostingde.go b/providers/dns/hostingde/hostingde.go index 18044947..bfee68d7 100644 --- a/providers/dns/hostingde/hostingde.go +++ b/providers/dns/hostingde/hostingde.go @@ -12,6 +12,19 @@ import ( "github.com/go-acme/lego/v3/platform/config/env" ) +// Environment variables names. +const ( + envNamespace = "HOSTINGDE_" + + EnvAPIKey = envNamespace + "API_KEY" + EnvZoneName = envNamespace + "ZONE_NAME" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { APIKey string @@ -25,11 +38,11 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("HOSTINGDE_TTL", dns01.DefaultTTL), - PropagationTimeout: env.GetOrDefaultSecond("HOSTINGDE_PROPAGATION_TIMEOUT", 2*time.Minute), - PollingInterval: env.GetOrDefaultSecond("HOSTINGDE_POLLING_INTERVAL", 2*time.Second), + TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 2*time.Minute), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("HOSTINGDE_HTTP_TIMEOUT", 30*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), }, } } @@ -45,14 +58,14 @@ type DNSProvider struct { // Credentials must be passed in the environment variables: // HOSTINGDE_ZONE_NAME and HOSTINGDE_API_KEY func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("HOSTINGDE_API_KEY", "HOSTINGDE_ZONE_NAME") + values, err := env.Get(EnvAPIKey, EnvZoneName) if err != nil { return nil, fmt.Errorf("hostingde: %w", err) } config := NewDefaultConfig() - config.APIKey = values["HOSTINGDE_API_KEY"] - config.ZoneName = values["HOSTINGDE_ZONE_NAME"] + config.APIKey = values[EnvAPIKey] + config.ZoneName = values[EnvZoneName] return NewDNSProviderConfig(config) } diff --git a/providers/dns/hostingde/hostingde_test.go b/providers/dns/hostingde/hostingde_test.go index fabb3b92..f48af811 100644 --- a/providers/dns/hostingde/hostingde_test.go +++ b/providers/dns/hostingde/hostingde_test.go @@ -8,10 +8,12 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "HOSTINGDE_API_KEY", - "HOSTINGDE_ZONE_NAME"). - WithDomain("HOSTINGDE_DOMAIN") + EnvAPIKey, + EnvZoneName). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -22,31 +24,31 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "HOSTINGDE_API_KEY": "123", - "HOSTINGDE_ZONE_NAME": "456", + EnvAPIKey: "123", + EnvZoneName: "456", }, }, { desc: "missing credentials", envVars: map[string]string{ - "HOSTINGDE_API_KEY": "", - "HOSTINGDE_ZONE_NAME": "", + EnvAPIKey: "", + EnvZoneName: "", }, expected: "hostingde: some credentials information are missing: HOSTINGDE_API_KEY,HOSTINGDE_ZONE_NAME", }, { desc: "missing access key", envVars: map[string]string{ - "HOSTINGDE_API_KEY": "", - "HOSTINGDE_ZONE_NAME": "456", + EnvAPIKey: "", + EnvZoneName: "456", }, expected: "hostingde: some credentials information are missing: HOSTINGDE_API_KEY", }, { desc: "missing zone name", envVars: map[string]string{ - "HOSTINGDE_API_KEY": "123", - "HOSTINGDE_ZONE_NAME": "", + EnvAPIKey: "123", + EnvZoneName: "", }, expected: "hostingde: some credentials information are missing: HOSTINGDE_ZONE_NAME", }, diff --git a/providers/dns/httpreq/httpreq.go b/providers/dns/httpreq/httpreq.go index 49f5b5bc..cec00984 100644 --- a/providers/dns/httpreq/httpreq.go +++ b/providers/dns/httpreq/httpreq.go @@ -16,6 +16,20 @@ import ( "github.com/go-acme/lego/v3/platform/config/env" ) +// Environment variables names. +const ( + envNamespace = "HTTPREQ_" + + EnvEndpoint = envNamespace + "ENDPOINT" + EnvMode = envNamespace + "MODE" + EnvUsername = envNamespace + "USERNAME" + EnvPassword = envNamespace + "PASSWORD" + + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + type message struct { FQDN string `json:"fqdn"` Value string `json:"value"` @@ -41,10 +55,10 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - PropagationTimeout: env.GetOrDefaultSecond("HTTPREQ_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("HTTPREQ_POLLING_INTERVAL", dns01.DefaultPollingInterval), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("HTTPREQ_HTTP_TIMEOUT", 30*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), }, } } @@ -56,20 +70,20 @@ type DNSProvider struct { // NewDNSProvider returns a DNSProvider instance. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("HTTPREQ_ENDPOINT") + values, err := env.Get(EnvEndpoint) if err != nil { return nil, fmt.Errorf("httpreq: %w", err) } - endpoint, err := url.Parse(values["HTTPREQ_ENDPOINT"]) + endpoint, err := url.Parse(values[EnvEndpoint]) if err != nil { return nil, fmt.Errorf("httpreq: %w", err) } config := NewDefaultConfig() - config.Mode = env.GetOrFile("HTTPREQ_MODE") - config.Username = env.GetOrFile("HTTPREQ_USERNAME") - config.Password = env.GetOrFile("HTTPREQ_PASSWORD") + config.Mode = env.GetOrFile(EnvMode) + config.Username = env.GetOrFile(EnvUsername) + config.Password = env.GetOrFile(EnvPassword) config.Endpoint = endpoint return NewDNSProviderConfig(config) } diff --git a/providers/dns/httpreq/httpreq_test.go b/providers/dns/httpreq/httpreq_test.go index 662a0ffc..11ae5dcf 100644 --- a/providers/dns/httpreq/httpreq_test.go +++ b/providers/dns/httpreq/httpreq_test.go @@ -13,7 +13,7 @@ import ( "github.com/stretchr/testify/require" ) -var envTest = tester.NewEnvTest("HTTPREQ_ENDPOINT", "HTTPREQ_MODE", "HTTPREQ_USERNAME", "HTTPREQ_PASSWORD") +var envTest = tester.NewEnvTest(EnvEndpoint, EnvMode, EnvUsername, EnvPassword) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -24,20 +24,20 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "HTTPREQ_ENDPOINT": "http://localhost:8090", + EnvEndpoint: "http://localhost:8090", }, }, { desc: "invalid URL", envVars: map[string]string{ - "HTTPREQ_ENDPOINT": ":", + EnvEndpoint: ":", }, expected: `httpreq: parse ":": missing protocol scheme`, }, { desc: "missing endpoint", envVars: map[string]string{ - "HTTPREQ_ENDPOINT": "", + EnvEndpoint: "", }, expected: "httpreq: some credentials information are missing: HTTPREQ_ENDPOINT", }, diff --git a/providers/dns/iij/iij.go b/providers/dns/iij/iij.go index ec8bbb0d..8d8ff18f 100644 --- a/providers/dns/iij/iij.go +++ b/providers/dns/iij/iij.go @@ -14,6 +14,19 @@ import ( "github.com/iij/doapi/protocol" ) +// Environment variables names. +const ( + envNamespace = "IIJ_" + + EnvAPIAccessKey = envNamespace + "API_ACCESS_KEY" + EnvAPISecretKey = envNamespace + "API_SECRET_KEY" + EnvDoServiceCode = envNamespace + "DO_SERVICE_CODE" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" +) + // Config is used to configure the creation of the DNSProvider type Config struct { AccessKey string @@ -27,9 +40,9 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("IIJ_TTL", 300), - PropagationTimeout: env.GetOrDefaultSecond("IIJ_PROPAGATION_TIMEOUT", 2*time.Minute), - PollingInterval: env.GetOrDefaultSecond("IIJ_POLLING_INTERVAL", 4*time.Second), + TTL: env.GetOrDefaultInt(EnvTTL, 300), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 2*time.Minute), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 4*time.Second), } } @@ -41,15 +54,15 @@ type DNSProvider struct { // NewDNSProvider returns a DNSProvider instance configured for IIJ DO func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("IIJ_API_ACCESS_KEY", "IIJ_API_SECRET_KEY", "IIJ_DO_SERVICE_CODE") + values, err := env.Get(EnvAPIAccessKey, EnvAPISecretKey, EnvDoServiceCode) if err != nil { return nil, fmt.Errorf("iij: %w", err) } config := NewDefaultConfig() - config.AccessKey = values["IIJ_API_ACCESS_KEY"] - config.SecretKey = values["IIJ_API_SECRET_KEY"] - config.DoServiceCode = values["IIJ_DO_SERVICE_CODE"] + config.AccessKey = values[EnvAPIAccessKey] + config.SecretKey = values[EnvAPISecretKey] + config.DoServiceCode = values[EnvDoServiceCode] return NewDNSProviderConfig(config) } diff --git a/providers/dns/iij/iij_test.go b/providers/dns/iij/iij_test.go index 4f25ce79..13d46997 100644 --- a/providers/dns/iij/iij_test.go +++ b/providers/dns/iij/iij_test.go @@ -8,11 +8,13 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "TESTDOMAIN" + var envTest = tester.NewEnvTest( - "IIJ_API_ACCESS_KEY", - "IIJ_API_SECRET_KEY", - "IIJ_DO_SERVICE_CODE"). - WithDomain("IIJ_API_TESTDOMAIN") + EnvAPIAccessKey, + EnvAPISecretKey, + EnvDoServiceCode). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -23,44 +25,44 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "IIJ_API_ACCESS_KEY": "A", - "IIJ_API_SECRET_KEY": "B", - "IIJ_DO_SERVICE_CODE": "C", + EnvAPIAccessKey: "A", + EnvAPISecretKey: "B", + EnvDoServiceCode: "C", }, }, { desc: "missing credentials", envVars: map[string]string{ - "IIJ_API_ACCESS_KEY": "", - "IIJ_API_SECRET_KEY": "", - "IIJ_DO_SERVICE_CODE": "", + EnvAPIAccessKey: "", + EnvAPISecretKey: "", + EnvDoServiceCode: "", }, expected: "iij: some credentials information are missing: IIJ_API_ACCESS_KEY,IIJ_API_SECRET_KEY,IIJ_DO_SERVICE_CODE", }, { desc: "missing api access key", envVars: map[string]string{ - "IIJ_API_ACCESS_KEY": "", - "IIJ_API_SECRET_KEY": "B", - "IIJ_DO_SERVICE_CODE": "C", + EnvAPIAccessKey: "", + EnvAPISecretKey: "B", + EnvDoServiceCode: "C", }, expected: "iij: some credentials information are missing: IIJ_API_ACCESS_KEY", }, { desc: "missing secret key", envVars: map[string]string{ - "IIJ_API_ACCESS_KEY": "A", - "IIJ_API_SECRET_KEY": "", - "IIJ_DO_SERVICE_CODE": "C", + EnvAPIAccessKey: "A", + EnvAPISecretKey: "", + EnvDoServiceCode: "C", }, expected: "iij: some credentials information are missing: IIJ_API_SECRET_KEY", }, { desc: "missing do service code", envVars: map[string]string{ - "IIJ_API_ACCESS_KEY": "A", - "IIJ_API_SECRET_KEY": "B", - "IIJ_DO_SERVICE_CODE": "", + EnvAPIAccessKey: "A", + EnvAPISecretKey: "B", + EnvDoServiceCode: "", }, expected: "iij: some credentials information are missing: IIJ_DO_SERVICE_CODE", }, diff --git a/providers/dns/inwx/inwx.go b/providers/dns/inwx/inwx.go index 49391a4f..01a3d6b5 100644 --- a/providers/dns/inwx/inwx.go +++ b/providers/dns/inwx/inwx.go @@ -12,6 +12,19 @@ import ( "github.com/nrdcg/goinwx" ) +// Environment variables names. +const ( + envNamespace = "INWX_" + + EnvUsername = envNamespace + "USERNAME" + EnvPassword = envNamespace + "PASSWORD" + EnvSandbox = envNamespace + "SANDBOX" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" +) + // Config is used to configure the creation of the DNSProvider type Config struct { Username string @@ -25,10 +38,10 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - PropagationTimeout: env.GetOrDefaultSecond("INWX_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("INWX_POLLING_INTERVAL", dns01.DefaultPollingInterval), - TTL: env.GetOrDefaultInt("INWX_TTL", 300), - Sandbox: env.GetOrDefaultBool("INWX_SANDBOX", false), + TTL: env.GetOrDefaultInt(EnvTTL, 300), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), + Sandbox: env.GetOrDefaultBool(EnvSandbox, false), } } @@ -42,14 +55,14 @@ type DNSProvider struct { // Credentials must be passed in the environment variables: // INWX_USERNAME and INWX_PASSWORD. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("INWX_USERNAME", "INWX_PASSWORD") + values, err := env.Get(EnvUsername, EnvPassword) if err != nil { return nil, fmt.Errorf("inwx: %w", err) } config := NewDefaultConfig() - config.Username = values["INWX_USERNAME"] - config.Password = values["INWX_PASSWORD"] + config.Username = values[EnvUsername] + config.Password = values[EnvPassword] return NewDNSProviderConfig(config) } diff --git a/providers/dns/inwx/inwx_test.go b/providers/dns/inwx/inwx_test.go index 7004e29a..9ea1d2d7 100644 --- a/providers/dns/inwx/inwx_test.go +++ b/providers/dns/inwx/inwx_test.go @@ -7,13 +7,15 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "INWX_USERNAME", - "INWX_PASSWORD", - "INWX_SANDBOX", - "INWX_TTL"). - WithDomain("INWX_DOMAIN"). - WithLiveTestRequirements("INWX_USERNAME", "INWX_PASSWORD", "INWX_DOMAIN") + EnvUsername, + EnvPassword, + EnvSandbox, + EnvTTL). + WithDomain(envDomain). + WithLiveTestRequirements(EnvUsername, EnvPassword, envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -24,31 +26,31 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "INWX_USERNAME": "123", - "INWX_PASSWORD": "456", + EnvUsername: "123", + EnvPassword: "456", }, }, { desc: "missing credentials", envVars: map[string]string{ - "INWX_USERNAME": "", - "INWX_PASSWORD": "", + EnvUsername: "", + EnvPassword: "", }, expected: "inwx: some credentials information are missing: INWX_USERNAME,INWX_PASSWORD", }, { desc: "missing username", envVars: map[string]string{ - "INWX_USERNAME": "", - "INWX_PASSWORD": "456", + EnvUsername: "", + EnvPassword: "456", }, expected: "inwx: some credentials information are missing: INWX_USERNAME", }, { desc: "missing password", envVars: map[string]string{ - "INWX_USERNAME": "123", - "INWX_PASSWORD": "", + EnvUsername: "123", + EnvPassword: "", }, expected: "inwx: some credentials information are missing: INWX_PASSWORD", }, @@ -120,8 +122,8 @@ func TestLivePresentAndCleanup(t *testing.T) { envTest.RestoreEnv() envTest.Apply(map[string]string{ - "INWX_SANDBOX": "true", - "INWX_TTL": "3600", // In sandbox mode, the minimum allowed TTL is 3600 + EnvSandbox: "true", + EnvTTL: "3600", // In sandbox mode, the minimum allowed TTL is 3600 }) defer envTest.RestoreEnv() diff --git a/providers/dns/joker/joker.go b/providers/dns/joker/joker.go index 2448b3cc..cef5d399 100644 --- a/providers/dns/joker/joker.go +++ b/providers/dns/joker/joker.go @@ -13,6 +13,21 @@ import ( "github.com/go-acme/lego/v3/platform/config/env" ) +// Environment variables names. +const ( + envNamespace = "JOKER_" + + EnvAPIKey = envNamespace + "API_KEY" + EnvUsername = envNamespace + "USERNAME" + EnvPassword = envNamespace + "PASSWORD" + EnvDebug = envNamespace + "DEBUG" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider. type Config struct { Debug bool @@ -31,12 +46,12 @@ type Config struct { func NewDefaultConfig() *Config { return &Config{ BaseURL: defaultBaseURL, - Debug: env.GetOrDefaultBool("JOKER_DEBUG", false), - TTL: env.GetOrDefaultInt("JOKER_TTL", dns01.DefaultTTL), - PropagationTimeout: env.GetOrDefaultSecond("JOKER_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("JOKER_POLLING_INTERVAL", dns01.DefaultPollingInterval), + Debug: env.GetOrDefaultBool(EnvDebug, false), + TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("JOKER_HTTP_TIMEOUT", 60*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 60*time.Second), }, } } @@ -50,19 +65,19 @@ type DNSProvider struct { // NewDNSProvider returns a DNSProvider instance configured for Joker DMAPI. // Credentials must be passed in the environment variable JOKER_API_KEY. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("JOKER_API_KEY") + values, err := env.Get(EnvAPIKey) if err != nil { var errU error - values, errU = env.Get("JOKER_USERNAME", "JOKER_PASSWORD") + values, errU = env.Get(EnvUsername, EnvPassword) if errU != nil { return nil, fmt.Errorf("joker: %v or %v", errU, err) } } config := NewDefaultConfig() - config.APIKey = values["JOKER_API_KEY"] - config.Username = values["JOKER_USERNAME"] - config.Password = values["JOKER_PASSWORD"] + config.APIKey = values[EnvAPIKey] + config.Username = values[EnvUsername] + config.Password = values[EnvPassword] return NewDNSProviderConfig(config) } diff --git a/providers/dns/joker/joker_test.go b/providers/dns/joker/joker_test.go index 327c36e2..2e4c505b 100644 --- a/providers/dns/joker/joker_test.go +++ b/providers/dns/joker/joker_test.go @@ -9,8 +9,10 @@ import ( "github.com/stretchr/testify/require" ) -var envTest = tester.NewEnvTest("JOKER_API_KEY", "JOKER_USERNAME", "JOKER_PASSWORD"). - WithDomain("JOKER_DOMAIN") +const envDomain = envNamespace + "DOMAIN" + +var envTest = tester.NewEnvTest(EnvAPIKey, EnvUsername, EnvPassword). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -21,40 +23,40 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success API key", envVars: map[string]string{ - "JOKER_API_KEY": "123", + EnvAPIKey: "123", }, }, { desc: "success username password", envVars: map[string]string{ - "JOKER_USERNAME": "123", - "JOKER_PASSWORD": "123", + EnvUsername: "123", + EnvPassword: "123", }, }, { desc: "missing credentials", envVars: map[string]string{ - "JOKER_API_KEY": "", - "JOKER_USERNAME": "", - "JOKER_PASSWORD": "", + EnvAPIKey: "", + EnvUsername: "", + EnvPassword: "", }, expected: "joker: some credentials information are missing: JOKER_USERNAME,JOKER_PASSWORD or some credentials information are missing: JOKER_API_KEY", }, { desc: "missing password", envVars: map[string]string{ - "JOKER_API_KEY": "", - "JOKER_USERNAME": "123", - "JOKER_PASSWORD": "", + EnvAPIKey: "", + EnvUsername: "123", + EnvPassword: "", }, expected: "joker: some credentials information are missing: JOKER_PASSWORD or some credentials information are missing: JOKER_API_KEY", }, { desc: "missing username", envVars: map[string]string{ - "JOKER_API_KEY": "", - "JOKER_USERNAME": "", - "JOKER_PASSWORD": "123", + EnvAPIKey: "", + EnvUsername: "", + EnvPassword: "123", }, expected: "joker: some credentials information are missing: JOKER_USERNAME or some credentials information are missing: JOKER_API_KEY", }, diff --git a/providers/dns/lightsail/lightsail.go b/providers/dns/lightsail/lightsail.go index 78bf3e9e..0397c951 100644 --- a/providers/dns/lightsail/lightsail.go +++ b/providers/dns/lightsail/lightsail.go @@ -20,6 +20,17 @@ const ( maxRetries = 5 ) +// Environment variables names. +const ( + envNamespace = "LIGHTSAIL_" + + EnvRegion = envNamespace + "LIGHTSAIL_REGION" + EnvDNSZone = "DNS_ZONE" + + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" +) + // customRetryer implements the client.Retryer interface by composing the DefaultRetryer. // It controls the logic for retrying recoverable request errors (e.g. when rate limits are exceeded). type customRetryer struct { @@ -52,10 +63,10 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - DNSZone: env.GetOrFile("DNS_ZONE"), - PropagationTimeout: env.GetOrDefaultSecond("LIGHTSAIL_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("LIGHTSAIL_POLLING_INTERVAL", dns01.DefaultPollingInterval), - Region: env.GetOrDefaultString("LIGHTSAIL_REGION", "us-east-1"), + DNSZone: env.GetOrFile(EnvDNSZone), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), + Region: env.GetOrDefaultString(EnvRegion, "us-east-1"), } } diff --git a/providers/dns/lightsail/lightsail_test.go b/providers/dns/lightsail/lightsail_test.go index 20008a62..00fc0d57 100644 --- a/providers/dns/lightsail/lightsail_test.go +++ b/providers/dns/lightsail/lightsail_test.go @@ -13,13 +13,22 @@ import ( "github.com/stretchr/testify/require" ) +const ( + envAwsNamespace = "AWS_" + + envAwsAccessKeyID = envAwsNamespace + "ACCESS_KEY_ID" + envAwsSecretAccessKey = envAwsNamespace + "SECRET_ACCESS_KEY" + envAwsRegion = envAwsNamespace + "REGION" + envAwsHostedZoneID = envAwsNamespace + "HOSTED_ZONE_ID" +) + var envTest = tester.NewEnvTest( - "AWS_ACCESS_KEY_ID", - "AWS_SECRET_ACCESS_KEY", - "AWS_REGION", - "AWS_HOSTED_ZONE_ID"). - WithDomain("DNS_ZONE"). - WithLiveTestRequirements("AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "DNS_ZONE") + envAwsAccessKeyID, + envAwsSecretAccessKey, + envAwsRegion, + envAwsHostedZoneID). + WithDomain(EnvDNSZone). + WithLiveTestRequirements(envAwsAccessKeyID, envAwsSecretAccessKey, EnvDNSZone) func makeProvider(ts *httptest.Server) (*DNSProvider, error) { config := &aws.Config{ @@ -44,9 +53,9 @@ func TestCredentialsFromEnv(t *testing.T) { defer envTest.RestoreEnv() envTest.ClearEnv() - os.Setenv("AWS_ACCESS_KEY_ID", "123") - os.Setenv("AWS_SECRET_ACCESS_KEY", "123") - os.Setenv("AWS_REGION", "us-east-1") + os.Setenv(envAwsAccessKeyID, "123") + os.Setenv(envAwsSecretAccessKey, "123") + os.Setenv(envAwsRegion, "us-east-1") config := &aws.Config{ CredentialsChainVerboseErrors: aws.Bool(true), diff --git a/providers/dns/linode/linode.go b/providers/dns/linode/linode.go index d00b2de7..f6d7d8d8 100644 --- a/providers/dns/linode/linode.go +++ b/providers/dns/linode/linode.go @@ -18,6 +18,16 @@ const ( dnsUpdateFudgeSecs = 120 ) +// Environment variables names. +const ( + envNamespace = "LINODE_" + + EnvAPIKey = envNamespace + "API_KEY" + + EnvTTL = envNamespace + "TTL" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" +) + // Config is used to configure the creation of the DNSProvider type Config struct { APIKey string @@ -28,8 +38,8 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - PollingInterval: env.GetOrDefaultSecond("LINODE_POLLING_INTERVAL", 15*time.Second), - TTL: env.GetOrDefaultInt("LINODE_TTL", minTTL), + TTL: env.GetOrDefaultInt(EnvTTL, minTTL), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 15*time.Second), } } @@ -47,13 +57,13 @@ type DNSProvider struct { // NewDNSProvider returns a DNSProvider instance configured for Linode. // Credentials must be passed in the environment variable: LINODE_API_KEY. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("LINODE_API_KEY") + values, err := env.Get(EnvAPIKey) if err != nil { return nil, fmt.Errorf("linode: %w", err) } config := NewDefaultConfig() - config.APIKey = values["LINODE_API_KEY"] + config.APIKey = values[EnvAPIKey] return NewDNSProviderConfig(config) } diff --git a/providers/dns/linode/linode_test.go b/providers/dns/linode/linode_test.go index f30e9f46..7323551a 100644 --- a/providers/dns/linode/linode_test.go +++ b/providers/dns/linode/linode_test.go @@ -29,7 +29,7 @@ type ( MockResponseMap map[string]MockResponse ) -var envTest = tester.NewEnvTest("LINODE_API_KEY") +var envTest = tester.NewEnvTest(EnvAPIKey) func newMockServer(responses MockResponseMap) *httptest.Server { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -77,13 +77,13 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "LINODE_API_KEY": "123", + EnvAPIKey: "123", }, }, { desc: "missing api key", envVars: map[string]string{ - "LINODE_API_KEY": "", + EnvAPIKey: "", }, expected: "linode: some credentials information are missing: LINODE_API_KEY", }, @@ -147,7 +147,7 @@ func TestNewDNSProviderConfig(t *testing.T) { func TestDNSProvider_Present(t *testing.T) { defer envTest.RestoreEnv() - os.Setenv("LINODE_API_KEY", "testing") + os.Setenv(EnvAPIKey, "testing") p, err := NewDNSProvider() require.NoError(t, err) @@ -234,7 +234,7 @@ func TestDNSProvider_Present(t *testing.T) { func TestDNSProvider_CleanUp(t *testing.T) { defer envTest.RestoreEnv() - os.Setenv("LINODE_API_KEY", "testing") + os.Setenv(EnvAPIKey, "testing") p, err := NewDNSProvider() require.NoError(t, err) diff --git a/providers/dns/linodev4/linodev4.go b/providers/dns/linodev4/linodev4.go index 229807d4..01b08eb9 100644 --- a/providers/dns/linodev4/linodev4.go +++ b/providers/dns/linodev4/linodev4.go @@ -22,6 +22,18 @@ const ( dnsUpdateFudgeSecs = 120 ) +// Environment variables names. +const ( + envNamespace = "LINODE_" + + EnvToken = envNamespace + "TOKEN" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { Token string @@ -34,10 +46,10 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - PropagationTimeout: env.GetOrDefaultSecond("LINODE_PROPAGATION_TIMEOUT", 0), - PollingInterval: env.GetOrDefaultSecond("LINODE_POLLING_INTERVAL", 15*time.Second), - TTL: env.GetOrDefaultInt("LINODE_TTL", minTTL), - HTTPTimeout: env.GetOrDefaultSecond("LINODE_HTTP_TIMEOUT", 0), + TTL: env.GetOrDefaultInt(EnvTTL, minTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 0), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 15*time.Second), + HTTPTimeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 0), } } @@ -55,13 +67,13 @@ type DNSProvider struct { // NewDNSProvider returns a DNSProvider instance configured for Linode. // Credentials must be passed in the environment variable: LINODE_TOKEN. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("LINODE_TOKEN") + values, err := env.Get(EnvToken) if err != nil { return nil, fmt.Errorf("linodev4: %w", err) } config := NewDefaultConfig() - config.Token = values["LINODE_TOKEN"] + config.Token = values[EnvToken] return NewDNSProviderConfig(config) } diff --git a/providers/dns/linodev4/linodev4_test.go b/providers/dns/linodev4/linodev4_test.go index 77800d33..4193b319 100644 --- a/providers/dns/linodev4/linodev4_test.go +++ b/providers/dns/linodev4/linodev4_test.go @@ -15,11 +15,9 @@ import ( "github.com/stretchr/testify/require" ) -type ( - MockResponseMap map[string]interface{} -) +type MockResponseMap map[string]interface{} -var envTest = tester.NewEnvTest("LINODE_TOKEN") +var envTest = tester.NewEnvTest(EnvToken) func newMockServer(responses MockResponseMap) *httptest.Server { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -68,13 +66,13 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "LINODE_TOKEN": "123", + EnvToken: "123", }, }, { desc: "missing api key", envVars: map[string]string{ - "LINODE_TOKEN": "", + EnvToken: "", }, expected: "linodev4: some credentials information are missing: LINODE_TOKEN", }, @@ -138,7 +136,7 @@ func TestNewDNSProviderConfig(t *testing.T) { func TestDNSProvider_Present(t *testing.T) { defer envTest.RestoreEnv() - os.Setenv("LINODE_TOKEN", "testing") + os.Setenv(EnvToken, "testing") p, err := NewDNSProvider() require.NoError(t, err) @@ -227,7 +225,7 @@ func TestDNSProvider_Present(t *testing.T) { func TestDNSProvider_CleanUp(t *testing.T) { defer envTest.RestoreEnv() - os.Setenv("LINODE_TOKEN", "testing") + os.Setenv(EnvToken, "testing") p, err := NewDNSProvider() require.NoError(t, err) diff --git a/providers/dns/liquidweb/liquidweb.go b/providers/dns/liquidweb/liquidweb.go index aacfc5bf..e3ed9635 100644 --- a/providers/dns/liquidweb/liquidweb.go +++ b/providers/dns/liquidweb/liquidweb.go @@ -16,6 +16,21 @@ import ( const defaultBaseURL = "https://api.stormondemand.com" +// Environment variables names. +const ( + envNamespace = "LIQUID_WEB_" + + EnvURL = envNamespace + "URL" + EnvUsername = envNamespace + "USERNAME" + EnvPassword = envNamespace + "PASSWORD" + EnvZone = envNamespace + "ZONE" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { BaseURL string @@ -32,10 +47,10 @@ type Config struct { func NewDefaultConfig() *Config { config := &Config{ BaseURL: defaultBaseURL, - TTL: env.GetOrDefaultInt("LIQUID_WEB_TTL", 300), - PollingInterval: env.GetOrDefaultSecond("LIQUID_WEB_POLLING_INTERVAL", 2*time.Second), - PropagationTimeout: env.GetOrDefaultSecond("LIQUID_WEB_PROPAGATION_TIMEOUT", 2*time.Minute), - HTTPTimeout: env.GetOrDefaultSecond("LIQUID_WEB_HTTP_TIMEOUT", 1*time.Minute), + TTL: env.GetOrDefaultInt(EnvTTL, 300), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 2*time.Minute), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second), + HTTPTimeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 1*time.Minute), } return config @@ -52,16 +67,16 @@ type DNSProvider struct { // NewDNSProvider returns a DNSProvider instance configured for Liquid Web. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("LIQUID_WEB_USERNAME", "LIQUID_WEB_PASSWORD", "LIQUID_WEB_ZONE") + values, err := env.Get(EnvUsername, EnvPassword, EnvZone) if err != nil { return nil, fmt.Errorf("liquidweb: %w", err) } config := NewDefaultConfig() - config.BaseURL = env.GetOrFile("LIQUID_WEB_URL") - config.Username = values["LIQUID_WEB_USERNAME"] - config.Password = values["LIQUID_WEB_PASSWORD"] - config.Zone = values["LIQUID_WEB_ZONE"] + config.BaseURL = env.GetOrFile(EnvURL) + config.Username = values[EnvUsername] + config.Password = values[EnvPassword] + config.Zone = values[EnvZone] return NewDNSProviderConfig(config) } diff --git a/providers/dns/liquidweb/liquidweb_test.go b/providers/dns/liquidweb/liquidweb_test.go index a887b138..3db669dd 100644 --- a/providers/dns/liquidweb/liquidweb_test.go +++ b/providers/dns/liquidweb/liquidweb_test.go @@ -13,12 +13,14 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "LIQUID_WEB_URL", - "LIQUID_WEB_USERNAME", - "LIQUID_WEB_PASSWORD", - "LIQUID_WEB_ZONE"). - WithDomain("LIQUID_WEB_DOMAIN") + EnvURL, + EnvUsername, + EnvPassword, + EnvZone). + WithDomain(envDomain) func setupTest() (*DNSProvider, *http.ServeMux, func()) { handler := http.NewServeMux() @@ -47,10 +49,10 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "LIQUID_WEB_URL": "https://storm.com", - "LIQUID_WEB_USERNAME": "blars", - "LIQUID_WEB_PASSWORD": "tacoman", - "LIQUID_WEB_ZONE": "blars.com", + EnvURL: "https://storm.com", + EnvUsername: "blars", + EnvPassword: "tacoman", + EnvZone: "blars.com", }, }, { @@ -61,23 +63,23 @@ func TestNewDNSProvider(t *testing.T) { { desc: "missing username", envVars: map[string]string{ - "LIQUID_WEB_PASSWORD": "tacoman", - "LIQUID_WEB_ZONE": "blars.com", + EnvPassword: "tacoman", + EnvZone: "blars.com", }, expected: "liquidweb: some credentials information are missing: LIQUID_WEB_USERNAME", }, { desc: "missing password", envVars: map[string]string{ - "LIQUID_WEB_USERNAME": "blars", - "LIQUID_WEB_ZONE": "blars.com", + EnvUsername: "blars", + EnvZone: "blars.com", }, expected: "liquidweb: some credentials information are missing: LIQUID_WEB_PASSWORD", }, { desc: "missing zone", envVars: map[string]string{ - "LIQUID_WEB_USERNAME": "blars", - "LIQUID_WEB_PASSWORD": "tacoman", + EnvUsername: "blars", + EnvPassword: "tacoman", }, expected: "liquidweb: some credentials information are missing: LIQUID_WEB_ZONE", }, } diff --git a/providers/dns/mydnsjp/mydnsjp.go b/providers/dns/mydnsjp/mydnsjp.go index 2068c1a3..fd3d449e 100644 --- a/providers/dns/mydnsjp/mydnsjp.go +++ b/providers/dns/mydnsjp/mydnsjp.go @@ -13,6 +13,18 @@ import ( const defaultBaseURL = "https://www.mydns.jp/directedit.html" +// Environment variables names. +const ( + envNamespace = "MYDNSJP_" + + EnvMasterID = envNamespace + "MASTER_ID" + EnvPassword = envNamespace + "PASSWORD" + + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { MasterID string @@ -25,10 +37,10 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - PropagationTimeout: env.GetOrDefaultSecond("MYDNSJP_PROPAGATION_TIMEOUT", 2*time.Minute), - PollingInterval: env.GetOrDefaultSecond("MYDNSJP_POLLING_INTERVAL", 2*time.Second), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 2*time.Minute), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("MYDNSJP_HTTP_TIMEOUT", 30*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), }, } } @@ -41,14 +53,14 @@ type DNSProvider struct { // NewDNSProvider returns a DNSProvider instance configured for MyDNS.jp. // Credentials must be passed in the environment variables: MYDNSJP_MASTER_ID and MYDNSJP_PASSWORD. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("MYDNSJP_MASTER_ID", "MYDNSJP_PASSWORD") + values, err := env.Get(EnvMasterID, EnvPassword) if err != nil { return nil, fmt.Errorf("mydnsjp: %w", err) } config := NewDefaultConfig() - config.MasterID = values["MYDNSJP_MASTER_ID"] - config.Password = values["MYDNSJP_PASSWORD"] + config.MasterID = values[EnvMasterID] + config.Password = values[EnvPassword] return NewDNSProviderConfig(config) } diff --git a/providers/dns/mydnsjp/mydnsjp_test.go b/providers/dns/mydnsjp/mydnsjp_test.go index 30e79af6..c27b7d89 100644 --- a/providers/dns/mydnsjp/mydnsjp_test.go +++ b/providers/dns/mydnsjp/mydnsjp_test.go @@ -9,8 +9,10 @@ import ( "github.com/stretchr/testify/require" ) -var envTest = tester.NewEnvTest("MYDNSJP_MASTER_ID", "MYDNSJP_PASSWORD"). - WithDomain("MYDNSJP_DOMAIN") +const envDomain = envNamespace + "DOMAIN" + +var envTest = tester.NewEnvTest(EnvMasterID, EnvPassword). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -21,31 +23,31 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "MYDNSJP_MASTER_ID": "test@example.com", - "MYDNSJP_PASSWORD": "123", + EnvMasterID: "test@example.com", + EnvPassword: "123", }, }, { desc: "missing credentials", envVars: map[string]string{ - "MYDNSJP_MASTER_ID": "", - "MYDNSJP_PASSWORD": "", + EnvMasterID: "", + EnvPassword: "", }, expected: "mydnsjp: some credentials information are missing: MYDNSJP_MASTER_ID,MYDNSJP_PASSWORD", }, { desc: "missing email", envVars: map[string]string{ - "MYDNSJP_MASTER_ID": "", - "MYDNSJP_PASSWORD": "key", + EnvMasterID: "", + EnvPassword: "key", }, expected: "mydnsjp: some credentials information are missing: MYDNSJP_MASTER_ID", }, { desc: "missing api key", envVars: map[string]string{ - "MYDNSJP_MASTER_ID": "awesome@possum.com", - "MYDNSJP_PASSWORD": "", + EnvMasterID: "awesome@possum.com", + EnvPassword: "", }, expected: "mydnsjp: some credentials information are missing: MYDNSJP_PASSWORD", }, diff --git a/providers/dns/namecheap/namecheap.go b/providers/dns/namecheap/namecheap.go index ecec33d1..91bcd2cd 100644 --- a/providers/dns/namecheap/namecheap.go +++ b/providers/dns/namecheap/namecheap.go @@ -35,6 +35,22 @@ const ( getIPURL = "https://dynamicdns.park-your-domain.com/getip" ) +// Environment variables names. +const ( + envNamespace = "NAMECHEAP_" + + EnvAPIUser = envNamespace + "API_USER" + EnvAPIKey = envNamespace + "API_KEY" + + EnvSandbox = envNamespace + "SANDBOX" + EnvDebug = envNamespace + "DEBUG" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // A challenge represents all the data needed to specify a dns-01 challenge // to lets-encrypt. type challenge struct { @@ -63,18 +79,18 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { baseURL := defaultBaseURL - if env.GetOrDefaultBool("NAMECHEAP_SANDBOX", false) { + if env.GetOrDefaultBool(EnvSandbox, false) { baseURL = sandboxBaseURL } return &Config{ BaseURL: baseURL, - Debug: env.GetOrDefaultBool("NAMECHEAP_DEBUG", false), - TTL: env.GetOrDefaultInt("NAMECHEAP_TTL", dns01.DefaultTTL), - PropagationTimeout: env.GetOrDefaultSecond("NAMECHEAP_PROPAGATION_TIMEOUT", 60*time.Minute), - PollingInterval: env.GetOrDefaultSecond("NAMECHEAP_POLLING_INTERVAL", 15*time.Second), + Debug: env.GetOrDefaultBool(EnvDebug, false), + TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 60*time.Minute), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 15*time.Second), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("NAMECHEAP_HTTP_TIMEOUT", 60*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 60*time.Second), }, } } @@ -89,14 +105,14 @@ type DNSProvider struct { // Credentials must be passed in the environment variables: // NAMECHEAP_API_USER and NAMECHEAP_API_KEY. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("NAMECHEAP_API_USER", "NAMECHEAP_API_KEY") + values, err := env.Get(EnvAPIUser, EnvAPIKey) if err != nil { return nil, fmt.Errorf("namecheap: %w", err) } config := NewDefaultConfig() - config.APIUser = values["NAMECHEAP_API_USER"] - config.APIKey = values["NAMECHEAP_API_KEY"] + config.APIUser = values[EnvAPIUser] + config.APIKey = values[EnvAPIKey] return NewDNSProviderConfig(config) } diff --git a/providers/dns/namedotcom/namedotcom.go b/providers/dns/namedotcom/namedotcom.go index c5cb9769..574d0abf 100644 --- a/providers/dns/namedotcom/namedotcom.go +++ b/providers/dns/namedotcom/namedotcom.go @@ -16,6 +16,20 @@ import ( // according to https://www.name.com/api-docs/DNS#CreateRecord const minTTL = 300 +// Environment variables names. +const ( + envNamespace = "NAMECOM_" + + EnvUsername = envNamespace + "USERNAME" + EnvAPIToken = envNamespace + "API_TOKEN" + EnvServer = envNamespace + "SERVER" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { Username string @@ -30,11 +44,11 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("NAMECOM_TTL", minTTL), - PropagationTimeout: env.GetOrDefaultSecond("NAMECOM_PROPAGATION_TIMEOUT", 15*time.Minute), - PollingInterval: env.GetOrDefaultSecond("NAMECOM_POLLING_INTERVAL", 20*time.Second), + TTL: env.GetOrDefaultInt(EnvTTL, minTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 15*time.Minute), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 20*time.Second), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("NAMECOM_HTTP_TIMEOUT", 10*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 10*time.Second), }, } } @@ -49,15 +63,15 @@ type DNSProvider struct { // Credentials must be passed in the environment variables: // NAMECOM_USERNAME and NAMECOM_API_TOKEN func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("NAMECOM_USERNAME", "NAMECOM_API_TOKEN") + values, err := env.Get(EnvUsername, EnvAPIToken) if err != nil { return nil, fmt.Errorf("namedotcom: %w", err) } config := NewDefaultConfig() - config.Username = values["NAMECOM_USERNAME"] - config.APIToken = values["NAMECOM_API_TOKEN"] - config.Server = env.GetOrFile("NAMECOM_SERVER") + config.Username = values[EnvUsername] + config.APIToken = values[EnvAPIToken] + config.Server = env.GetOrFile(EnvServer) return NewDNSProviderConfig(config) } diff --git a/providers/dns/namedotcom/namedotcom_test.go b/providers/dns/namedotcom/namedotcom_test.go index 82bbbb1d..efda4e91 100644 --- a/providers/dns/namedotcom/namedotcom_test.go +++ b/providers/dns/namedotcom/namedotcom_test.go @@ -8,10 +8,12 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "NAMECOM_USERNAME", - "NAMECOM_API_TOKEN"). - WithDomain("NAMEDOTCOM_DOMAIN") + EnvUsername, + EnvAPIToken). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -22,31 +24,31 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "NAMECOM_USERNAME": "A", - "NAMECOM_API_TOKEN": "B", + EnvUsername: "A", + EnvAPIToken: "B", }, }, { desc: "missing credentials", envVars: map[string]string{ - "NAMECOM_USERNAME": "", - "NAMECOM_API_TOKEN": "", + EnvUsername: "", + EnvAPIToken: "", }, expected: "namedotcom: some credentials information are missing: NAMECOM_USERNAME,NAMECOM_API_TOKEN", }, { desc: "missing username", envVars: map[string]string{ - "NAMECOM_USERNAME": "", - "NAMECOM_API_TOKEN": "B", + EnvUsername: "", + EnvAPIToken: "B", }, expected: "namedotcom: some credentials information are missing: NAMECOM_USERNAME", }, { desc: "missing api token", envVars: map[string]string{ - "NAMECOM_USERNAME": "A", - "NAMECOM_API_TOKEN": "", + EnvUsername: "A", + EnvAPIToken: "", }, expected: "namedotcom: some credentials information are missing: NAMECOM_API_TOKEN", }, diff --git a/providers/dns/namesilo/namesilo.go b/providers/dns/namesilo/namesilo.go index 347aea8b..315b4795 100644 --- a/providers/dns/namesilo/namesilo.go +++ b/providers/dns/namesilo/namesilo.go @@ -17,6 +17,17 @@ const ( maxTTL = 2592000 ) +// Environment variables names. +const ( + envNamespace = "NAMESILO_" + + EnvAPIKey = envNamespace + "API_KEY" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" +) + // Config is used to configure the creation of the DNSProvider type Config struct { APIKey string @@ -28,9 +39,9 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - PropagationTimeout: env.GetOrDefaultSecond("NAMESILO_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("NAMESILO_POLLING_INTERVAL", dns01.DefaultPollingInterval), - TTL: env.GetOrDefaultInt("NAMESILO_TTL", defaultTTL), + TTL: env.GetOrDefaultInt(EnvTTL, defaultTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), } } @@ -45,13 +56,13 @@ type DNSProvider struct { // // See: https://www.namesilo.com/api_reference.php func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("NAMESILO_API_KEY") + values, err := env.Get(EnvAPIKey) if err != nil { return nil, fmt.Errorf("namesilo: %w", err) } config := NewDefaultConfig() - config.APIKey = values["NAMESILO_API_KEY"] + config.APIKey = values[EnvAPIKey] return NewDNSProviderConfig(config) } diff --git a/providers/dns/namesilo/namesilo_test.go b/providers/dns/namesilo/namesilo_test.go index 87b14c80..70467985 100644 --- a/providers/dns/namesilo/namesilo_test.go +++ b/providers/dns/namesilo/namesilo_test.go @@ -8,10 +8,12 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "NAMESILO_TTL", - "NAMESILO_API_KEY"). - WithDomain("NAMESILO_DOMAIN") + EnvTTL, + EnvAPIKey). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -22,7 +24,7 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "NAMESILO_API_KEY": "A", + EnvAPIKey: "A", }, }, { @@ -33,8 +35,8 @@ func TestNewDNSProvider(t *testing.T) { { desc: "unsupported TTL", envVars: map[string]string{ - "NAMESILO_API_KEY": "A", - "NAMESILO_TTL": "180", + EnvAPIKey: "A", + EnvTTL: "180", }, expected: "namesilo: TTL should be in [3600, 2592000]", }, diff --git a/providers/dns/netcup/netcup.go b/providers/dns/netcup/netcup.go index a1786924..a5fc4802 100644 --- a/providers/dns/netcup/netcup.go +++ b/providers/dns/netcup/netcup.go @@ -15,6 +15,20 @@ import ( "github.com/go-acme/lego/v3/platform/config/env" ) +// Environment variables names. +const ( + envNamespace = "NETCUP_" + + EnvCustomerNumber = envNamespace + "CUSTOMER_NUMBER" + EnvAPIKey = envNamespace + "API_KEY" + EnvAPIPassword = envNamespace + "API_PASSWORD" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { Key string @@ -29,11 +43,11 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("NETCUP_TTL", dns01.DefaultTTL), - PropagationTimeout: env.GetOrDefaultSecond("NETCUP_PROPAGATION_TIMEOUT", 120*time.Second), - PollingInterval: env.GetOrDefaultSecond("NETCUP_POLLING_INTERVAL", 5*time.Second), + TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 120*time.Second), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 5*time.Second), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("NETCUP_HTTP_TIMEOUT", 10*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 10*time.Second), }, } } @@ -48,15 +62,15 @@ type DNSProvider struct { // Credentials must be passed in the environment variables: // NETCUP_CUSTOMER_NUMBER, NETCUP_API_KEY, NETCUP_API_PASSWORD func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("NETCUP_CUSTOMER_NUMBER", "NETCUP_API_KEY", "NETCUP_API_PASSWORD") + values, err := env.Get(EnvCustomerNumber, EnvAPIKey, EnvAPIPassword) if err != nil { return nil, fmt.Errorf("netcup: %w", err) } config := NewDefaultConfig() - config.Customer = values["NETCUP_CUSTOMER_NUMBER"] - config.Key = values["NETCUP_API_KEY"] - config.Password = values["NETCUP_API_PASSWORD"] + config.Customer = values[EnvCustomerNumber] + config.Key = values[EnvAPIKey] + config.Password = values[EnvAPIPassword] return NewDNSProviderConfig(config) } diff --git a/providers/dns/netcup/netcup_test.go b/providers/dns/netcup/netcup_test.go index e60cbc41..8265ef9c 100644 --- a/providers/dns/netcup/netcup_test.go +++ b/providers/dns/netcup/netcup_test.go @@ -9,11 +9,13 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "NETCUP_CUSTOMER_NUMBER", - "NETCUP_API_KEY", - "NETCUP_API_PASSWORD"). - WithDomain("NETCUP_DOMAIN") + EnvCustomerNumber, + EnvAPIKey, + EnvAPIPassword). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -24,44 +26,44 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "NETCUP_CUSTOMER_NUMBER": "A", - "NETCUP_API_KEY": "B", - "NETCUP_API_PASSWORD": "C", + EnvCustomerNumber: "A", + EnvAPIKey: "B", + EnvAPIPassword: "C", }, }, { desc: "missing credentials", envVars: map[string]string{ - "NETCUP_CUSTOMER_NUMBER": "", - "NETCUP_API_KEY": "", - "NETCUP_API_PASSWORD": "", + EnvCustomerNumber: "", + EnvAPIKey: "", + EnvAPIPassword: "", }, expected: "netcup: some credentials information are missing: NETCUP_CUSTOMER_NUMBER,NETCUP_API_KEY,NETCUP_API_PASSWORD", }, { desc: "missing customer number", envVars: map[string]string{ - "NETCUP_CUSTOMER_NUMBER": "", - "NETCUP_API_KEY": "B", - "NETCUP_API_PASSWORD": "C", + EnvCustomerNumber: "", + EnvAPIKey: "B", + EnvAPIPassword: "C", }, expected: "netcup: some credentials information are missing: NETCUP_CUSTOMER_NUMBER", }, { desc: "missing API key", envVars: map[string]string{ - "NETCUP_CUSTOMER_NUMBER": "A", - "NETCUP_API_KEY": "", - "NETCUP_API_PASSWORD": "C", + EnvCustomerNumber: "A", + EnvAPIKey: "", + EnvAPIPassword: "C", }, expected: "netcup: some credentials information are missing: NETCUP_API_KEY", }, { desc: "missing api password", envVars: map[string]string{ - "NETCUP_CUSTOMER_NUMBER": "A", - "NETCUP_API_KEY": "B", - "NETCUP_API_PASSWORD": "", + EnvCustomerNumber: "A", + EnvAPIKey: "B", + EnvAPIPassword: "", }, expected: "netcup: some credentials information are missing: NETCUP_API_PASSWORD", }, diff --git a/providers/dns/nifcloud/nifcloud.go b/providers/dns/nifcloud/nifcloud.go index d7043ad6..38840b41 100644 --- a/providers/dns/nifcloud/nifcloud.go +++ b/providers/dns/nifcloud/nifcloud.go @@ -14,6 +14,20 @@ import ( "github.com/go-acme/lego/v3/platform/wait" ) +// Environment variables names. +const ( + envNamespace = "NIFCLOUD_" + + EnvAccessKeyID = envNamespace + "ACCESS_KEY_ID" + EnvSecretAccessKey = envNamespace + "SECRET_ACCESS_KEY" + EnvDNSEndpoint = envNamespace + "DNS_ENDPOINT" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { BaseURL string @@ -28,11 +42,11 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("NIFCLOUD_TTL", dns01.DefaultTTL), - PropagationTimeout: env.GetOrDefaultSecond("NIFCLOUD_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("NIFCLOUD_POLLING_INTERVAL", dns01.DefaultPollingInterval), + TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("NIFCLOUD_HTTP_TIMEOUT", 30*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), }, } } @@ -47,15 +61,15 @@ type DNSProvider struct { // Credentials must be passed in the environment variables: // NIFCLOUD_ACCESS_KEY_ID and NIFCLOUD_SECRET_ACCESS_KEY. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("NIFCLOUD_ACCESS_KEY_ID", "NIFCLOUD_SECRET_ACCESS_KEY") + values, err := env.Get(EnvAccessKeyID, EnvSecretAccessKey) if err != nil { return nil, fmt.Errorf("nifcloud: %w", err) } config := NewDefaultConfig() - config.BaseURL = env.GetOrFile("NIFCLOUD_DNS_ENDPOINT") - config.AccessKey = values["NIFCLOUD_ACCESS_KEY_ID"] - config.SecretKey = values["NIFCLOUD_SECRET_ACCESS_KEY"] + config.BaseURL = env.GetOrFile(EnvDNSEndpoint) + config.AccessKey = values[EnvAccessKeyID] + config.SecretKey = values[EnvSecretAccessKey] return NewDNSProviderConfig(config) } diff --git a/providers/dns/nifcloud/nifcloud_test.go b/providers/dns/nifcloud/nifcloud_test.go index 5971e6bd..e5d746f6 100644 --- a/providers/dns/nifcloud/nifcloud_test.go +++ b/providers/dns/nifcloud/nifcloud_test.go @@ -8,10 +8,12 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "NIFCLOUD_ACCESS_KEY_ID", - "NIFCLOUD_SECRET_ACCESS_KEY"). - WithDomain("NIFCLOUD_DOMAIN") + EnvAccessKeyID, + EnvSecretAccessKey). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -22,31 +24,31 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "NIFCLOUD_ACCESS_KEY_ID": "123", - "NIFCLOUD_SECRET_ACCESS_KEY": "456", + EnvAccessKeyID: "123", + EnvSecretAccessKey: "456", }, }, { desc: "missing credentials", envVars: map[string]string{ - "NIFCLOUD_ACCESS_KEY_ID": "", - "NIFCLOUD_SECRET_ACCESS_KEY": "", + EnvAccessKeyID: "", + EnvSecretAccessKey: "", }, expected: "nifcloud: some credentials information are missing: NIFCLOUD_ACCESS_KEY_ID,NIFCLOUD_SECRET_ACCESS_KEY", }, { desc: "missing access key", envVars: map[string]string{ - "NIFCLOUD_ACCESS_KEY_ID": "", - "NIFCLOUD_SECRET_ACCESS_KEY": "456", + EnvAccessKeyID: "", + EnvSecretAccessKey: "456", }, expected: "nifcloud: some credentials information are missing: NIFCLOUD_ACCESS_KEY_ID", }, { desc: "missing secret key", envVars: map[string]string{ - "NIFCLOUD_ACCESS_KEY_ID": "123", - "NIFCLOUD_SECRET_ACCESS_KEY": "", + EnvAccessKeyID: "123", + EnvSecretAccessKey: "", }, expected: "nifcloud: some credentials information are missing: NIFCLOUD_SECRET_ACCESS_KEY", }, diff --git a/providers/dns/ns1/ns1.go b/providers/dns/ns1/ns1.go index 6282ab65..e13ee92e 100644 --- a/providers/dns/ns1/ns1.go +++ b/providers/dns/ns1/ns1.go @@ -15,6 +15,18 @@ import ( "gopkg.in/ns1/ns1-go.v2/rest/model/dns" ) +// Environment variables names. +const ( + envNamespace = "NS1_" + + EnvAPIKey = envNamespace + "API_KEY" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { APIKey string @@ -27,11 +39,11 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("NS1_TTL", dns01.DefaultTTL), - PropagationTimeout: env.GetOrDefaultSecond("NS1_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("NS1_POLLING_INTERVAL", dns01.DefaultPollingInterval), + TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("NS1_HTTP_TIMEOUT", 10*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 10*time.Second), }, } } @@ -45,13 +57,13 @@ type DNSProvider struct { // NewDNSProvider returns a DNSProvider instance configured for NS1. // Credentials must be passed in the environment variables: NS1_API_KEY. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("NS1_API_KEY") + values, err := env.Get(EnvAPIKey) if err != nil { return nil, fmt.Errorf("ns1: %w", err) } config := NewDefaultConfig() - config.APIKey = values["NS1_API_KEY"] + config.APIKey = values[EnvAPIKey] return NewDNSProviderConfig(config) } diff --git a/providers/dns/ns1/ns1_test.go b/providers/dns/ns1/ns1_test.go index e06a5ca4..e183b2a0 100644 --- a/providers/dns/ns1/ns1_test.go +++ b/providers/dns/ns1/ns1_test.go @@ -9,8 +9,10 @@ import ( "github.com/stretchr/testify/require" ) -var envTest = tester.NewEnvTest("NS1_API_KEY"). - WithDomain("NS1_DOMAIN") +const envDomain = envNamespace + "DOMAIN" + +var envTest = tester.NewEnvTest(EnvAPIKey). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -21,13 +23,13 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "NS1_API_KEY": "123", + EnvAPIKey: "123", }, }, { desc: "missing api key", envVars: map[string]string{ - "NS1_API_KEY": "", + EnvAPIKey: "", }, expected: "ns1: some credentials information are missing: NS1_API_KEY", }, diff --git a/providers/dns/oraclecloud/configprovider.go b/providers/dns/oraclecloud/configprovider.go index 6f748948..f107ee18 100644 --- a/providers/dns/oraclecloud/configprovider.go +++ b/providers/dns/oraclecloud/configprovider.go @@ -11,15 +11,6 @@ import ( "github.com/oracle/oci-go-sdk/common" ) -const ( - ociPrivkey = "OCI_PRIVKEY" - ociPrivkeyPass = "OCI_PRIVKEY_PASS" - ociTenancyOCID = "OCI_TENANCY_OCID" - ociUserOCID = "OCI_USER_OCID" - ociPubkeyFingerprint = "OCI_PUBKEY_FINGERPRINT" - ociRegion = "OCI_REGION" -) - type configProvider struct { values map[string]string privateKeyPassphrase string @@ -28,12 +19,12 @@ type configProvider struct { func newConfigProvider(values map[string]string) *configProvider { return &configProvider{ values: values, - privateKeyPassphrase: env.GetOrFile(ociPrivkeyPass), + privateKeyPassphrase: env.GetOrFile(EnvPrivKeyPass), } } func (p *configProvider) PrivateRSAKey() (*rsa.PrivateKey, error) { - privateKey, err := getPrivateKey(ociPrivkey) + privateKey, err := getPrivateKey(envPrivKey) if err != nil { return nil, err } @@ -61,19 +52,19 @@ func (p *configProvider) KeyID() (string, error) { } func (p *configProvider) TenancyOCID() (value string, err error) { - return p.values[ociTenancyOCID], nil + return p.values[EnvTenancyOCID], nil } func (p *configProvider) UserOCID() (string, error) { - return p.values[ociUserOCID], nil + return p.values[EnvUserOCID], nil } func (p *configProvider) KeyFingerprint() (string, error) { - return p.values[ociPubkeyFingerprint], nil + return p.values[EnvPubKeyFingerprint], nil } func (p *configProvider) Region() (string, error) { - return p.values[ociRegion], nil + return p.values[EnvRegion], nil } func getPrivateKey(envVar string) ([]byte, error) { diff --git a/providers/dns/oraclecloud/oraclecloud.go b/providers/dns/oraclecloud/oraclecloud.go index c48a6291..b2b9e25d 100644 --- a/providers/dns/oraclecloud/oraclecloud.go +++ b/providers/dns/oraclecloud/oraclecloud.go @@ -13,6 +13,25 @@ import ( "github.com/oracle/oci-go-sdk/dns" ) +// Environment variables names. +const ( + envNamespace = "OCI_" + + EnvCompartmentOCID = envNamespace + "COMPARTMENT_OCID" + envPrivKey = envNamespace + "PRIVKEY" + EnvPrivKeyFile = envPrivKey + "_FILE" + EnvPrivKeyPass = envPrivKey + "_PASS" + EnvTenancyOCID = envNamespace + "TENANCY_OCID" + EnvUserOCID = envNamespace + "USER_OCID" + EnvPubKeyFingerprint = envNamespace + "PUBKEY_FINGERPRINT" + EnvRegion = envNamespace + "REGION" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { CompartmentID string @@ -26,11 +45,11 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("OCI_TTL", dns01.DefaultTTL), - PropagationTimeout: env.GetOrDefaultSecond("OCI_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("OCI_POLLING_INTERVAL", dns01.DefaultPollingInterval), + TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("OCI_HTTP_TIMEOUT", 60*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 60*time.Second), }, } } @@ -43,13 +62,13 @@ type DNSProvider struct { // NewDNSProvider returns a DNSProvider instance configured for OracleCloud. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get(ociPrivkey, ociTenancyOCID, ociUserOCID, ociPubkeyFingerprint, ociRegion, "OCI_COMPARTMENT_OCID") + values, err := env.Get(envPrivKey, EnvTenancyOCID, EnvUserOCID, EnvPubKeyFingerprint, EnvRegion, EnvCompartmentOCID) if err != nil { return nil, fmt.Errorf("oraclecloud: %w", err) } config := NewDefaultConfig() - config.CompartmentID = values["OCI_COMPARTMENT_OCID"] + config.CompartmentID = values[EnvCompartmentOCID] config.OCIConfigProvider = newConfigProvider(values) return NewDNSProviderConfig(config) diff --git a/providers/dns/oraclecloud/oraclecloud_test.go b/providers/dns/oraclecloud/oraclecloud_test.go index 2f5ef41f..494465c9 100644 --- a/providers/dns/oraclecloud/oraclecloud_test.go +++ b/providers/dns/oraclecloud/oraclecloud_test.go @@ -16,16 +16,18 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - ociPrivkey, - ociPrivkey+"_FILE", - ociPrivkeyPass, - ociTenancyOCID, - ociUserOCID, - ociPubkeyFingerprint, - ociRegion, - "OCI_COMPARTMENT_OCID"). - WithDomain("ORACLECLOUD_DOMAIN") + envPrivKey, + EnvPrivKeyFile, + EnvPrivKeyPass, + EnvTenancyOCID, + EnvUserOCID, + EnvPubKeyFingerprint, + EnvRegion, + EnvCompartmentOCID). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -36,25 +38,25 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - ociPrivkey: mustGeneratePrivateKey("secret1"), - ociPrivkeyPass: "secret1", - ociTenancyOCID: "ocid1.tenancy.oc1..secret", - ociUserOCID: "ocid1.user.oc1..secret", - ociPubkeyFingerprint: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00", - ociRegion: "us-phoenix-1", - "OCI_COMPARTMENT_OCID": "123", + envPrivKey: mustGeneratePrivateKey("secret1"), + EnvPrivKeyPass: "secret1", + EnvTenancyOCID: "ocid1.tenancy.oc1..secret", + EnvUserOCID: "ocid1.user.oc1..secret", + EnvPubKeyFingerprint: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00", + EnvRegion: "us-phoenix-1", + EnvCompartmentOCID: "123", }, }, { desc: "success file", envVars: map[string]string{ - ociPrivkey + "_FILE": mustGeneratePrivateKeyFile("secret1"), - ociPrivkeyPass: "secret1", - ociTenancyOCID: "ocid1.tenancy.oc1..secret", - ociUserOCID: "ocid1.user.oc1..secret", - ociPubkeyFingerprint: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00", - ociRegion: "us-phoenix-1", - "OCI_COMPARTMENT_OCID": "123", + EnvPrivKeyFile: mustGeneratePrivateKeyFile("secret1"), + EnvPrivKeyPass: "secret1", + EnvTenancyOCID: "ocid1.tenancy.oc1..secret", + EnvUserOCID: "ocid1.user.oc1..secret", + EnvPubKeyFingerprint: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00", + EnvRegion: "us-phoenix-1", + EnvCompartmentOCID: "123", }, }, { @@ -65,104 +67,104 @@ func TestNewDNSProvider(t *testing.T) { { desc: "missing CompartmentID", envVars: map[string]string{ - ociPrivkey: mustGeneratePrivateKey("secret"), - ociPrivkeyPass: "secret", - ociTenancyOCID: "ocid1.tenancy.oc1..secret", - ociUserOCID: "ocid1.user.oc1..secret", - ociPubkeyFingerprint: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00", - ociRegion: "us-phoenix-1", - "OCI_COMPARTMENT_OCID": "", + envPrivKey: mustGeneratePrivateKey("secret"), + EnvPrivKeyPass: "secret", + EnvTenancyOCID: "ocid1.tenancy.oc1..secret", + EnvUserOCID: "ocid1.user.oc1..secret", + EnvPubKeyFingerprint: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00", + EnvRegion: "us-phoenix-1", + EnvCompartmentOCID: "", }, expected: "oraclecloud: some credentials information are missing: OCI_COMPARTMENT_OCID", }, { desc: "missing OCI_PRIVKEY", envVars: map[string]string{ - ociPrivkey: "", - ociPrivkeyPass: "secret", - ociTenancyOCID: "ocid1.tenancy.oc1..secret", - ociUserOCID: "ocid1.user.oc1..secret", - ociPubkeyFingerprint: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00", - ociRegion: "us-phoenix-1", - "OCI_COMPARTMENT_OCID": "123", + envPrivKey: "", + EnvPrivKeyPass: "secret", + EnvTenancyOCID: "ocid1.tenancy.oc1..secret", + EnvUserOCID: "ocid1.user.oc1..secret", + EnvPubKeyFingerprint: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00", + EnvRegion: "us-phoenix-1", + EnvCompartmentOCID: "123", }, expected: "oraclecloud: some credentials information are missing: OCI_PRIVKEY", }, { desc: "missing OCI_PRIVKEY_PASS", envVars: map[string]string{ - ociPrivkey: mustGeneratePrivateKey("secret"), - ociPrivkeyPass: "", - ociTenancyOCID: "ocid1.tenancy.oc1..secret", - ociUserOCID: "ocid1.user.oc1..secret", - ociPubkeyFingerprint: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00", - ociRegion: "us-phoenix-1", - "OCI_COMPARTMENT_OCID": "123", + envPrivKey: mustGeneratePrivateKey("secret"), + EnvPrivKeyPass: "", + EnvTenancyOCID: "ocid1.tenancy.oc1..secret", + EnvUserOCID: "ocid1.user.oc1..secret", + EnvPubKeyFingerprint: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00", + EnvRegion: "us-phoenix-1", + EnvCompartmentOCID: "123", }, expected: "oraclecloud: can not create client, bad configuration: x509: decryption password incorrect", }, { desc: "missing OCI_TENANCY_OCID", envVars: map[string]string{ - ociPrivkey: mustGeneratePrivateKey("secret"), - ociPrivkeyPass: "secret", - ociTenancyOCID: "", - ociUserOCID: "ocid1.user.oc1..secret", - ociPubkeyFingerprint: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00", - ociRegion: "us-phoenix-1", - "OCI_COMPARTMENT_OCID": "123", + envPrivKey: mustGeneratePrivateKey("secret"), + EnvPrivKeyPass: "secret", + EnvTenancyOCID: "", + EnvUserOCID: "ocid1.user.oc1..secret", + EnvPubKeyFingerprint: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00", + EnvRegion: "us-phoenix-1", + EnvCompartmentOCID: "123", }, expected: "oraclecloud: some credentials information are missing: OCI_TENANCY_OCID", }, { desc: "missing OCI_USER_OCID", envVars: map[string]string{ - ociPrivkey: mustGeneratePrivateKey("secret"), - ociPrivkeyPass: "secret", - ociTenancyOCID: "ocid1.tenancy.oc1..secret", - ociUserOCID: "", - ociPubkeyFingerprint: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00", - ociRegion: "us-phoenix-1", - "OCI_COMPARTMENT_OCID": "123", + envPrivKey: mustGeneratePrivateKey("secret"), + EnvPrivKeyPass: "secret", + EnvTenancyOCID: "ocid1.tenancy.oc1..secret", + EnvUserOCID: "", + EnvPubKeyFingerprint: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00", + EnvRegion: "us-phoenix-1", + EnvCompartmentOCID: "123", }, expected: "oraclecloud: some credentials information are missing: OCI_USER_OCID", }, { desc: "missing OCI_PUBKEY_FINGERPRINT", envVars: map[string]string{ - ociPrivkey: mustGeneratePrivateKey("secret"), - ociPrivkeyPass: "secret", - ociTenancyOCID: "ocid1.tenancy.oc1..secret", - ociUserOCID: "ocid1.user.oc1..secret", - ociPubkeyFingerprint: "", - ociRegion: "us-phoenix-1", - "OCI_COMPARTMENT_OCID": "123", + envPrivKey: mustGeneratePrivateKey("secret"), + EnvPrivKeyPass: "secret", + EnvTenancyOCID: "ocid1.tenancy.oc1..secret", + EnvUserOCID: "ocid1.user.oc1..secret", + EnvPubKeyFingerprint: "", + EnvRegion: "us-phoenix-1", + EnvCompartmentOCID: "123", }, expected: "oraclecloud: some credentials information are missing: OCI_PUBKEY_FINGERPRINT", }, { desc: "missing OCI_REGION", envVars: map[string]string{ - ociPrivkey: mustGeneratePrivateKey("secret"), - ociPrivkeyPass: "secret", - ociTenancyOCID: "ocid1.tenancy.oc1..secret", - ociUserOCID: "ocid1.user.oc1..secret", - ociPubkeyFingerprint: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00", - ociRegion: "", - "OCI_COMPARTMENT_OCID": "123", + envPrivKey: mustGeneratePrivateKey("secret"), + EnvPrivKeyPass: "secret", + EnvTenancyOCID: "ocid1.tenancy.oc1..secret", + EnvUserOCID: "ocid1.user.oc1..secret", + EnvPubKeyFingerprint: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00", + EnvRegion: "", + EnvCompartmentOCID: "123", }, expected: "oraclecloud: some credentials information are missing: OCI_REGION", }, { desc: "missing OCI_REGION", envVars: map[string]string{ - ociPrivkey: mustGeneratePrivateKey("secret"), - ociPrivkeyPass: "secret", - ociTenancyOCID: "ocid1.tenancy.oc1..secret", - ociUserOCID: "ocid1.user.oc1..secret", - ociPubkeyFingerprint: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00", - ociRegion: "", - "OCI_COMPARTMENT_OCID": "123", + envPrivKey: mustGeneratePrivateKey("secret"), + EnvPrivKeyPass: "secret", + EnvTenancyOCID: "ocid1.tenancy.oc1..secret", + EnvUserOCID: "ocid1.user.oc1..secret", + EnvPubKeyFingerprint: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00", + EnvRegion: "", + EnvCompartmentOCID: "123", }, expected: "oraclecloud: some credentials information are missing: OCI_REGION", }, @@ -171,7 +173,7 @@ func TestNewDNSProvider(t *testing.T) { for _, test := range testCases { t.Run(test.desc, func(t *testing.T) { defer func() { - privKeyFile := os.Getenv(ociPrivkey + "_FILE") + privKeyFile := os.Getenv(EnvPrivKeyFile) if privKeyFile != "" { _ = os.Remove(privKeyFile) } diff --git a/providers/dns/otc/otc.go b/providers/dns/otc/otc.go index 9d28d208..a82c4497 100644 --- a/providers/dns/otc/otc.go +++ b/providers/dns/otc/otc.go @@ -17,6 +17,22 @@ const defaultIdentityEndpoint = "https://iam.eu-de.otc.t-systems.com:443/v3/auth // minTTL 300 is otc minimum value for ttl const minTTL = 300 +// Environment variables names. +const ( + envNamespace = "OTC_" + + EnvDomainName = envNamespace + "DOMAIN_NAME" + EnvUserName = envNamespace + "USER_NAME" + EnvPassword = envNamespace + "PASSWORD" + EnvProjectName = envNamespace + "PROJECT_NAME" + EnvIdentityEndpoint = envNamespace + "IDENTITY_ENDPOINT" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { IdentityEndpoint string @@ -33,12 +49,12 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - IdentityEndpoint: env.GetOrDefaultString("OTC_IDENTITY_ENDPOINT", defaultIdentityEndpoint), - PropagationTimeout: env.GetOrDefaultSecond("OTC_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("OTC_POLLING_INTERVAL", dns01.DefaultPollingInterval), - TTL: env.GetOrDefaultInt("OTC_TTL", minTTL), + TTL: env.GetOrDefaultInt(EnvTTL, minTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), + IdentityEndpoint: env.GetOrDefaultString(EnvIdentityEndpoint, defaultIdentityEndpoint), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("OTC_HTTP_TIMEOUT", 10*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 10*time.Second), Transport: &http.Transport{ Proxy: http.ProxyFromEnvironment, DialContext: (&net.Dialer{ @@ -70,16 +86,16 @@ type DNSProvider struct { // Credentials must be passed in the environment variables: OTC_USER_NAME, // OTC_DOMAIN_NAME, OTC_PASSWORD OTC_PROJECT_NAME and OTC_IDENTITY_ENDPOINT. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("OTC_DOMAIN_NAME", "OTC_USER_NAME", "OTC_PASSWORD", "OTC_PROJECT_NAME") + values, err := env.Get(EnvDomainName, EnvUserName, EnvPassword, EnvProjectName) if err != nil { return nil, fmt.Errorf("otc: %w", err) } config := NewDefaultConfig() - config.DomainName = values["OTC_DOMAIN_NAME"] - config.UserName = values["OTC_USER_NAME"] - config.Password = values["OTC_PASSWORD"] - config.ProjectName = values["OTC_PROJECT_NAME"] + config.DomainName = values[EnvDomainName] + config.UserName = values[EnvUserName] + config.Password = values[EnvPassword] + config.ProjectName = values[EnvProjectName] return NewDNSProviderConfig(config) } diff --git a/providers/dns/otc/otc_test.go b/providers/dns/otc/otc_test.go index ebcdb69e..9540abb3 100644 --- a/providers/dns/otc/otc_test.go +++ b/providers/dns/otc/otc_test.go @@ -19,11 +19,11 @@ func (s *OTCSuite) SetupTest() { s.Mock = NewDNSServerMock(s.T()) s.Mock.HandleAuthSuccessfully() s.envTest = tester.NewEnvTest( - "OTC_DOMAIN_NAME", - "OTC_USER_NAME", - "OTC_PASSWORD", - "OTC_PROJECT_NAME", - "OTC_IDENTITY_ENDPOINT", + EnvDomainName, + EnvUserName, + EnvPassword, + EnvProjectName, + EnvIdentityEndpoint, ) } @@ -62,11 +62,11 @@ func (s *OTCSuite) TestLoginEnv() { s.envTest.ClearEnv() s.envTest.Apply(map[string]string{ - "OTC_DOMAIN_NAME": "unittest1", - "OTC_USER_NAME": "unittest2", - "OTC_PASSWORD": "unittest3", - "OTC_PROJECT_NAME": "unittest4", - "OTC_IDENTITY_ENDPOINT": "unittest5", + EnvDomainName: "unittest1", + EnvUserName: "unittest2", + EnvPassword: "unittest3", + EnvProjectName: "unittest4", + EnvIdentityEndpoint: "unittest5", }) provider, err := NewDNSProvider() @@ -78,7 +78,7 @@ func (s *OTCSuite) TestLoginEnv() { s.Equal(provider.config.ProjectName, "unittest4") s.Equal(provider.config.IdentityEndpoint, "unittest5") - os.Setenv("OTC_IDENTITY_ENDPOINT", "") + os.Setenv(EnvIdentityEndpoint, "") provider, err = NewDNSProvider() s.Require().NoError(err) diff --git a/providers/dns/ovh/ovh.go b/providers/dns/ovh/ovh.go index af46ae8d..a3e6bf8a 100644 --- a/providers/dns/ovh/ovh.go +++ b/providers/dns/ovh/ovh.go @@ -17,6 +17,21 @@ import ( // OVH API reference: https://eu.api.ovh.com/ // Create a Token: https://eu.api.ovh.com/createToken/ +// Environment variables names. +const ( + envNamespace = "OVH_" + + EnvEndpoint = envNamespace + "ENDPOINT" + EnvApplicationKey = envNamespace + "APPLICATION_KEY" + EnvApplicationSecret = envNamespace + "APPLICATION_SECRET" + EnvConsumerKey = envNamespace + "CONSUMER_KEY" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Record a DNS record type Record struct { ID int64 `json:"id,omitempty"` @@ -42,11 +57,11 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("OVH_TTL", dns01.DefaultTTL), - PropagationTimeout: env.GetOrDefaultSecond("OVH_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("OVH_POLLING_INTERVAL", dns01.DefaultPollingInterval), + TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("OVH_HTTP_TIMEOUT", ovh.DefaultTimeout), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, ovh.DefaultTimeout), }, } } @@ -67,16 +82,16 @@ type DNSProvider struct { // OVH_APPLICATION_SECRET // OVH_CONSUMER_KEY func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("OVH_ENDPOINT", "OVH_APPLICATION_KEY", "OVH_APPLICATION_SECRET", "OVH_CONSUMER_KEY") + values, err := env.Get(EnvEndpoint, EnvApplicationKey, EnvApplicationSecret, EnvConsumerKey) if err != nil { return nil, fmt.Errorf("ovh: %w", err) } config := NewDefaultConfig() - config.APIEndpoint = values["OVH_ENDPOINT"] - config.ApplicationKey = values["OVH_APPLICATION_KEY"] - config.ApplicationSecret = values["OVH_APPLICATION_SECRET"] - config.ConsumerKey = values["OVH_CONSUMER_KEY"] + config.APIEndpoint = values[EnvEndpoint] + config.ApplicationKey = values[EnvApplicationKey] + config.ApplicationSecret = values[EnvApplicationSecret] + config.ConsumerKey = values[EnvConsumerKey] return NewDNSProviderConfig(config) } diff --git a/providers/dns/ovh/ovh_test.go b/providers/dns/ovh/ovh_test.go index 31f86b3f..ba74a317 100644 --- a/providers/dns/ovh/ovh_test.go +++ b/providers/dns/ovh/ovh_test.go @@ -8,12 +8,14 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "OVH_ENDPOINT", - "OVH_APPLICATION_KEY", - "OVH_APPLICATION_SECRET", - "OVH_CONSUMER_KEY"). - WithDomain("OVH_DOMAIN") + EnvEndpoint, + EnvApplicationKey, + EnvApplicationSecret, + EnvConsumerKey). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -24,69 +26,69 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "OVH_ENDPOINT": "ovh-eu", - "OVH_APPLICATION_KEY": "B", - "OVH_APPLICATION_SECRET": "C", - "OVH_CONSUMER_KEY": "D", + EnvEndpoint: "ovh-eu", + EnvApplicationKey: "B", + EnvApplicationSecret: "C", + EnvConsumerKey: "D", }, }, { desc: "missing credentials", envVars: map[string]string{ - "OVH_ENDPOINT": "", - "OVH_APPLICATION_KEY": "", - "OVH_APPLICATION_SECRET": "", - "OVH_CONSUMER_KEY": "", + EnvEndpoint: "", + EnvApplicationKey: "", + EnvApplicationSecret: "", + EnvConsumerKey: "", }, expected: "ovh: some credentials information are missing: OVH_ENDPOINT,OVH_APPLICATION_KEY,OVH_APPLICATION_SECRET,OVH_CONSUMER_KEY", }, { desc: "missing endpoint", envVars: map[string]string{ - "OVH_ENDPOINT": "", - "OVH_APPLICATION_KEY": "B", - "OVH_APPLICATION_SECRET": "C", - "OVH_CONSUMER_KEY": "D", + EnvEndpoint: "", + EnvApplicationKey: "B", + EnvApplicationSecret: "C", + EnvConsumerKey: "D", }, expected: "ovh: some credentials information are missing: OVH_ENDPOINT", }, { desc: "missing invalid endpoint", envVars: map[string]string{ - "OVH_ENDPOINT": "foobar", - "OVH_APPLICATION_KEY": "B", - "OVH_APPLICATION_SECRET": "C", - "OVH_CONSUMER_KEY": "D", + EnvEndpoint: "foobar", + EnvApplicationKey: "B", + EnvApplicationSecret: "C", + EnvConsumerKey: "D", }, expected: "ovh: unknown endpoint 'foobar', consider checking 'Endpoints' list of using an URL", }, { desc: "missing application key", envVars: map[string]string{ - "OVH_ENDPOINT": "ovh-eu", - "OVH_APPLICATION_KEY": "", - "OVH_APPLICATION_SECRET": "C", - "OVH_CONSUMER_KEY": "D", + EnvEndpoint: "ovh-eu", + EnvApplicationKey: "", + EnvApplicationSecret: "C", + EnvConsumerKey: "D", }, expected: "ovh: some credentials information are missing: OVH_APPLICATION_KEY", }, { desc: "missing application secret", envVars: map[string]string{ - "OVH_ENDPOINT": "ovh-eu", - "OVH_APPLICATION_KEY": "B", - "OVH_APPLICATION_SECRET": "", - "OVH_CONSUMER_KEY": "D", + EnvEndpoint: "ovh-eu", + EnvApplicationKey: "B", + EnvApplicationSecret: "", + EnvConsumerKey: "D", }, expected: "ovh: some credentials information are missing: OVH_APPLICATION_SECRET", }, { desc: "missing consumer key", envVars: map[string]string{ - "OVH_ENDPOINT": "ovh-eu", - "OVH_APPLICATION_KEY": "B", - "OVH_APPLICATION_SECRET": "C", - "OVH_CONSUMER_KEY": "", + EnvEndpoint: "ovh-eu", + EnvApplicationKey: "B", + EnvApplicationSecret: "C", + EnvConsumerKey: "", }, expected: "ovh: some credentials information are missing: OVH_CONSUMER_KEY", }, diff --git a/providers/dns/pdns/pdns.go b/providers/dns/pdns/pdns.go index 7627ed4d..d3e97ecd 100644 --- a/providers/dns/pdns/pdns.go +++ b/providers/dns/pdns/pdns.go @@ -15,6 +15,19 @@ import ( "github.com/go-acme/lego/v3/platform/config/env" ) +// Environment variables names. +const ( + envNamespace = "PDNS_" + + EnvAPIKey = envNamespace + "API_KEY" + EnvAPIURL = envNamespace + "API_URL" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { APIKey string @@ -28,11 +41,11 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("PDNS_TTL", dns01.DefaultTTL), - PropagationTimeout: env.GetOrDefaultSecond("PDNS_PROPAGATION_TIMEOUT", 120*time.Second), - PollingInterval: env.GetOrDefaultSecond("PDNS_POLLING_INTERVAL", 2*time.Second), + TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 120*time.Second), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("PDNS_HTTP_TIMEOUT", 30*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), }, } } @@ -47,19 +60,19 @@ type DNSProvider struct { // Credentials must be passed in the environment variable: // PDNS_API_URL and PDNS_API_KEY. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("PDNS_API_KEY", "PDNS_API_URL") + values, err := env.Get(EnvAPIKey, EnvAPIURL) if err != nil { return nil, fmt.Errorf("pdns: %w", err) } - hostURL, err := url.Parse(values["PDNS_API_URL"]) + hostURL, err := url.Parse(values[EnvAPIURL]) if err != nil { return nil, fmt.Errorf("pdns: %w", err) } config := NewDefaultConfig() config.Host = hostURL - config.APIKey = values["PDNS_API_KEY"] + config.APIKey = values[EnvAPIKey] return NewDNSProviderConfig(config) } diff --git a/providers/dns/pdns/pdns_test.go b/providers/dns/pdns/pdns_test.go index 6dd0fdd9..6d0356fa 100644 --- a/providers/dns/pdns/pdns_test.go +++ b/providers/dns/pdns/pdns_test.go @@ -8,10 +8,12 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "PDNS_API_URL", - "PDNS_API_KEY"). - WithDomain("PDNS_DOMAIN") + EnvAPIURL, + EnvAPIKey). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -22,31 +24,31 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "PDNS_API_KEY": "123", - "PDNS_API_URL": "http://example.com", + EnvAPIKey: "123", + EnvAPIURL: "http://example.com", }, }, { desc: "missing credentials", envVars: map[string]string{ - "PDNS_API_KEY": "", - "PDNS_API_URL": "", + EnvAPIKey: "", + EnvAPIURL: "", }, expected: "pdns: some credentials information are missing: PDNS_API_KEY,PDNS_API_URL", }, { desc: "missing api key", envVars: map[string]string{ - "PDNS_API_KEY": "", - "PDNS_API_URL": "http://example.com", + EnvAPIKey: "", + EnvAPIURL: "http://example.com", }, expected: "pdns: some credentials information are missing: PDNS_API_KEY", }, { desc: "missing API URL", envVars: map[string]string{ - "PDNS_API_KEY": "123", - "PDNS_API_URL": "", + EnvAPIKey: "123", + EnvAPIURL: "", }, expected: "pdns: some credentials information are missing: PDNS_API_URL", }, diff --git a/providers/dns/rackspace/rackspace.go b/providers/dns/rackspace/rackspace.go index 6966315d..dcae2eee 100644 --- a/providers/dns/rackspace/rackspace.go +++ b/providers/dns/rackspace/rackspace.go @@ -16,6 +16,19 @@ import ( // defaultBaseURL represents the Identity API endpoint to call const defaultBaseURL = "https://identity.api.rackspacecloud.com/v2.0/tokens" +// Environment variables names. +const ( + envNamespace = "RACKSPACE_" + + EnvUser = envNamespace + "USER" + EnvAPIKey = envNamespace + "API_KEY" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { BaseURL string @@ -31,11 +44,11 @@ type Config struct { func NewDefaultConfig() *Config { return &Config{ BaseURL: defaultBaseURL, - TTL: env.GetOrDefaultInt("RACKSPACE_TTL", 300), - PropagationTimeout: env.GetOrDefaultSecond("RACKSPACE_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("RACKSPACE_POLLING_INTERVAL", dns01.DefaultPollingInterval), + TTL: env.GetOrDefaultInt(EnvTTL, 300), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("RACKSPACE_HTTP_TIMEOUT", 30*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), }, } } @@ -52,14 +65,14 @@ type DNSProvider struct { // Credentials must be passed in the environment variables: // RACKSPACE_USER and RACKSPACE_API_KEY. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("RACKSPACE_USER", "RACKSPACE_API_KEY") + values, err := env.Get(EnvUser, EnvAPIKey) if err != nil { return nil, fmt.Errorf("rackspace: %w", err) } config := NewDefaultConfig() - config.APIUser = values["RACKSPACE_USER"] - config.APIKey = values["RACKSPACE_API_KEY"] + config.APIUser = values[EnvUser] + config.APIKey = values[EnvAPIKey] return NewDNSProviderConfig(config) } diff --git a/providers/dns/rackspace/rackspace_test.go b/providers/dns/rackspace/rackspace_test.go index 8c875ee7..d804caad 100644 --- a/providers/dns/rackspace/rackspace_test.go +++ b/providers/dns/rackspace/rackspace_test.go @@ -14,10 +14,12 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "RACKSPACE_USER", - "RACKSPACE_API_KEY"). - WithDomain("RACKSPACE_DOMAIN") + EnvUser, + EnvAPIKey). + WithDomain(envDomain) func TestNewDNSProviderConfig(t *testing.T) { config, tearDown := setupTest() diff --git a/providers/dns/regru/regru_test.go b/providers/dns/regru/regru_test.go index 94a0d2f7..3f486796 100644 --- a/providers/dns/regru/regru_test.go +++ b/providers/dns/regru/regru_test.go @@ -8,7 +8,7 @@ import ( "github.com/stretchr/testify/require" ) -const envDomain = envNamespace + "_DOMAIN" +const envDomain = envNamespace + "DOMAIN" var envTest = tester.NewEnvTest( EnvUsername, diff --git a/providers/dns/rfc2136/rfc2136.go b/providers/dns/rfc2136/rfc2136.go index e0833042..494288a0 100644 --- a/providers/dns/rfc2136/rfc2136.go +++ b/providers/dns/rfc2136/rfc2136.go @@ -13,6 +13,22 @@ import ( "github.com/miekg/dns" ) +// Environment variables names. +const ( + envNamespace = "RFC2136_" + + EnvTSIGKey = envNamespace + "TSIG_KEY" + EnvTSIGSecret = envNamespace + "TSIG_SECRET" + EnvTSIGAlgorithm = envNamespace + "TSIG_ALGORITHM" + EnvNameserver = envNamespace + "NAMESERVER" + EnvDNSTimeout = envNamespace + "DNS_TIMEOUT" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvSequenceInterval = envNamespace + "SEQUENCE_INTERVAL" +) + // Config is used to configure the creation of the DNSProvider type Config struct { Nameserver string @@ -29,12 +45,12 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TSIGAlgorithm: env.GetOrDefaultString("RFC2136_TSIG_ALGORITHM", dns.HmacMD5), - TTL: env.GetOrDefaultInt("RFC2136_TTL", dns01.DefaultTTL), - PropagationTimeout: env.GetOrDefaultSecond("RFC2136_PROPAGATION_TIMEOUT", env.GetOrDefaultSecond("RFC2136_TIMEOUT", 60*time.Second)), - PollingInterval: env.GetOrDefaultSecond("RFC2136_POLLING_INTERVAL", 2*time.Second), - SequenceInterval: env.GetOrDefaultSecond("RFC2136_SEQUENCE_INTERVAL", dns01.DefaultPropagationTimeout), - DNSTimeout: env.GetOrDefaultSecond("RFC2136_DNS_TIMEOUT", 10*time.Second), + TSIGAlgorithm: env.GetOrDefaultString(EnvTSIGAlgorithm, dns.HmacMD5), + TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, env.GetOrDefaultSecond("RFC2136_TIMEOUT", 60*time.Second)), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second), + SequenceInterval: env.GetOrDefaultSecond(EnvSequenceInterval, dns01.DefaultPropagationTimeout), + DNSTimeout: env.GetOrDefaultSecond(EnvDNSTimeout, 10*time.Second), } } @@ -54,15 +70,15 @@ type DNSProvider struct { // RFC2136_PROPAGATION_TIMEOUT: DNS propagation timeout in time.ParseDuration format. (60s) // To disable TSIG authentication, leave the RFC2136_TSIG* variables unset. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("RFC2136_NAMESERVER") + values, err := env.Get(EnvNameserver) if err != nil { return nil, fmt.Errorf("rfc2136: %w", err) } config := NewDefaultConfig() - config.Nameserver = values["RFC2136_NAMESERVER"] - config.TSIGKey = env.GetOrFile("RFC2136_TSIG_KEY") - config.TSIGSecret = env.GetOrFile("RFC2136_TSIG_SECRET") + config.Nameserver = values[EnvNameserver] + config.TSIGKey = env.GetOrFile(EnvTSIGKey) + config.TSIGSecret = env.GetOrFile(EnvTSIGSecret) return NewDNSProviderConfig(config) } diff --git a/providers/dns/rfc2136/rfc2136_test.go b/providers/dns/rfc2136/rfc2136_test.go index 3d056931..bbefbe57 100644 --- a/providers/dns/rfc2136/rfc2136_test.go +++ b/providers/dns/rfc2136/rfc2136_test.go @@ -16,14 +16,14 @@ import ( ) const ( - envTestDomain = "123456789.www.example.com" - envTestKeyAuth = "123d==" - envTestValue = "Now36o-3BmlB623-0c1qCIUmgWVVmDJb88KGl24pqpo" - envTestFqdn = "_acme-challenge.123456789.www.example.com." - envTestZone = "example.com." - envTestTTL = 120 - envTestTsigKey = "example.com." - envTestTsigSecret = "IwBTJx9wrDp4Y1RyC3H0gA==" + fakeDomain = "123456789.www.example.com" + fakeKeyAuth = "123d==" + fakeValue = "Now36o-3BmlB623-0c1qCIUmgWVVmDJb88KGl24pqpo" + fakeFqdn = "_acme-challenge.123456789.www.example.com." + fakeZone = "example.com." + fakeTTL = 120 + fakeTsigKey = "example.com." + fakeTsigSecret = "IwBTJx9wrDp4Y1RyC3H0gA==" ) func TestCanaryLocalTestServer(t *testing.T) { @@ -50,8 +50,8 @@ func TestCanaryLocalTestServer(t *testing.T) { func TestServerSuccess(t *testing.T) { dns01.ClearFqdnCache() - dns.HandleFunc(envTestZone, serverHandlerReturnSuccess) - defer dns.HandleRemove(envTestZone) + dns.HandleFunc(fakeZone, serverHandlerReturnSuccess) + defer dns.HandleRemove(fakeZone) server, addr, err := runLocalDNSTestServer(false) require.NoError(t, err, "Failed to start test server") @@ -63,14 +63,14 @@ func TestServerSuccess(t *testing.T) { provider, err := NewDNSProviderConfig(config) require.NoError(t, err) - err = provider.Present(envTestDomain, "", envTestKeyAuth) + err = provider.Present(fakeDomain, "", fakeKeyAuth) require.NoError(t, err) } func TestServerError(t *testing.T) { dns01.ClearFqdnCache() - dns.HandleFunc(envTestZone, serverHandlerReturnErr) - defer dns.HandleRemove(envTestZone) + dns.HandleFunc(fakeZone, serverHandlerReturnErr) + defer dns.HandleRemove(fakeZone) server, addr, err := runLocalDNSTestServer(false) require.NoError(t, err, "Failed to start test server") @@ -82,7 +82,7 @@ func TestServerError(t *testing.T) { provider, err := NewDNSProviderConfig(config) require.NoError(t, err) - err = provider.Present(envTestDomain, "", envTestKeyAuth) + err = provider.Present(fakeDomain, "", fakeKeyAuth) require.Error(t, err) if !strings.Contains(err.Error(), "NOTZONE") { t.Errorf("Expected Present() to return an error with the 'NOTZONE' rcode string but it did not: %v", err) @@ -91,8 +91,8 @@ func TestServerError(t *testing.T) { func TestTsigClient(t *testing.T) { dns01.ClearFqdnCache() - dns.HandleFunc(envTestZone, serverHandlerReturnSuccess) - defer dns.HandleRemove(envTestZone) + dns.HandleFunc(fakeZone, serverHandlerReturnSuccess) + defer dns.HandleRemove(fakeZone) server, addr, err := runLocalDNSTestServer(true) require.NoError(t, err, "Failed to start test server") @@ -100,13 +100,13 @@ func TestTsigClient(t *testing.T) { config := NewDefaultConfig() config.Nameserver = addr - config.TSIGKey = envTestTsigKey - config.TSIGSecret = envTestTsigSecret + config.TSIGKey = fakeTsigKey + config.TSIGSecret = fakeTsigSecret provider, err := NewDNSProviderConfig(config) require.NoError(t, err) - err = provider.Present(envTestDomain, "", envTestKeyAuth) + err = provider.Present(fakeDomain, "", fakeKeyAuth) require.NoError(t, err) } @@ -114,17 +114,17 @@ func TestValidUpdatePacket(t *testing.T) { var reqChan = make(chan *dns.Msg, 10) dns01.ClearFqdnCache() - dns.HandleFunc(envTestZone, serverHandlerPassBackRequest(reqChan)) - defer dns.HandleRemove(envTestZone) + dns.HandleFunc(fakeZone, serverHandlerPassBackRequest(reqChan)) + defer dns.HandleRemove(fakeZone) server, addr, err := runLocalDNSTestServer(false) require.NoError(t, err, "Failed to start test server") defer func() { _ = server.Shutdown() }() - txtRR, _ := dns.NewRR(fmt.Sprintf("%s %d IN TXT %s", envTestFqdn, envTestTTL, envTestValue)) + txtRR, _ := dns.NewRR(fmt.Sprintf("%s %d IN TXT %s", fakeFqdn, fakeTTL, fakeValue)) rrs := []dns.RR{txtRR} m := new(dns.Msg) - m.SetUpdate(envTestZone) + m.SetUpdate(fakeZone) m.RemoveRRset(rrs) m.Insert(rrs) expectStr := m.String() @@ -138,7 +138,7 @@ func TestValidUpdatePacket(t *testing.T) { provider, err := NewDNSProviderConfig(config) require.NoError(t, err) - err = provider.Present(envTestDomain, "", "1234d==") + err = provider.Present(fakeDomain, "", "1234d==") require.NoError(t, err) rcvMsg := <-reqChan @@ -173,7 +173,7 @@ func runLocalDNSTestServer(tsig bool) (*dns.Server, string, error) { }} if tsig { - server.TsigSecret = map[string]string{envTestTsigKey: envTestTsigSecret} + server.TsigSecret = map[string]string{fakeTsigKey: fakeTsigSecret} } waitLock := sync.Mutex{} @@ -205,14 +205,14 @@ func serverHandlerReturnSuccess(w dns.ResponseWriter, req *dns.Msg) { m.SetReply(req) if req.Opcode == dns.OpcodeQuery && req.Question[0].Qtype == dns.TypeSOA && req.Question[0].Qclass == dns.ClassINET { // Return SOA to appease findZoneByFqdn() - soaRR, _ := dns.NewRR(fmt.Sprintf("%s %d IN SOA ns1.%s admin.%s 2016022801 28800 7200 2419200 1200", envTestZone, envTestTTL, envTestZone, envTestZone)) + soaRR, _ := dns.NewRR(fmt.Sprintf("%s %d IN SOA ns1.%s admin.%s 2016022801 28800 7200 2419200 1200", fakeZone, fakeTTL, fakeZone, fakeZone)) m.Answer = []dns.RR{soaRR} } if t := req.IsTsig(); t != nil { if w.TsigStatus() == nil { // Validated - m.SetTsig(envTestZone, dns.HmacMD5, 300, time.Now().Unix()) + m.SetTsig(fakeZone, dns.HmacMD5, 300, time.Now().Unix()) } } @@ -231,14 +231,14 @@ func serverHandlerPassBackRequest(reqChan chan *dns.Msg) func(w dns.ResponseWrit m.SetReply(req) if req.Opcode == dns.OpcodeQuery && req.Question[0].Qtype == dns.TypeSOA && req.Question[0].Qclass == dns.ClassINET { // Return SOA to appease findZoneByFqdn() - soaRR, _ := dns.NewRR(fmt.Sprintf("%s %d IN SOA ns1.%s admin.%s 2016022801 28800 7200 2419200 1200", envTestZone, envTestTTL, envTestZone, envTestZone)) + soaRR, _ := dns.NewRR(fmt.Sprintf("%s %d IN SOA ns1.%s admin.%s 2016022801 28800 7200 2419200 1200", fakeZone, fakeTTL, fakeZone, fakeZone)) m.Answer = []dns.RR{soaRR} } if t := req.IsTsig(); t != nil { if w.TsigStatus() == nil { // Validated - m.SetTsig(envTestZone, dns.HmacMD5, 300, time.Now().Unix()) + m.SetTsig(fakeZone, dns.HmacMD5, 300, time.Now().Unix()) } } diff --git a/providers/dns/route53/route53.go b/providers/dns/route53/route53.go index 3c7f97ee..19ae4667 100644 --- a/providers/dns/route53/route53.go +++ b/providers/dns/route53/route53.go @@ -18,6 +18,21 @@ import ( "github.com/go-acme/lego/v3/platform/wait" ) +// Environment variables names. +const ( + envNamespace = "AWS_" + + EnvAccessKeyID = envNamespace + "ACCESS_KEY_ID" + EnvSecretAccessKey = envNamespace + "SECRET_ACCESS_KEY" + EnvRegion = envNamespace + "REGION" + EnvHostedZoneID = envNamespace + "HOSTED_ZONE_ID" + EnvMaxRetries = envNamespace + "MAX_RETRIES" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" +) + // Config is used to configure the creation of the DNSProvider type Config struct { MaxRetries int @@ -31,11 +46,11 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - MaxRetries: env.GetOrDefaultInt("AWS_MAX_RETRIES", 5), - TTL: env.GetOrDefaultInt("AWS_TTL", 10), - PropagationTimeout: env.GetOrDefaultSecond("AWS_PROPAGATION_TIMEOUT", 2*time.Minute), - PollingInterval: env.GetOrDefaultSecond("AWS_POLLING_INTERVAL", 4*time.Second), - HostedZoneID: env.GetOrFile("AWS_HOSTED_ZONE_ID"), + MaxRetries: env.GetOrDefaultInt(EnvMaxRetries, 5), + TTL: env.GetOrDefaultInt(EnvTTL, 10), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 2*time.Minute), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 4*time.Second), + HostedZoneID: env.GetOrFile(EnvHostedZoneID), } } diff --git a/providers/dns/route53/route53_test.go b/providers/dns/route53/route53_test.go index c5b2a42e..c315fb3c 100644 --- a/providers/dns/route53/route53_test.go +++ b/providers/dns/route53/route53_test.go @@ -15,17 +15,19 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = "R53_DOMAIN" + var envTest = tester.NewEnvTest( - "AWS_ACCESS_KEY_ID", - "AWS_SECRET_ACCESS_KEY", - "AWS_REGION", - "AWS_HOSTED_ZONE_ID", - "AWS_MAX_RETRIES", - "AWS_TTL", - "AWS_PROPAGATION_TIMEOUT", - "AWS_POLLING_INTERVAL"). - WithDomain("R53_DOMAIN"). - WithLiveTestRequirements("AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_REGION", "R53_DOMAIN") + EnvAccessKeyID, + EnvSecretAccessKey, + EnvRegion, + EnvHostedZoneID, + EnvMaxRetries, + EnvTTL, + EnvPropagationTimeout, + EnvPollingInterval). + WithDomain(envDomain). + WithLiveTestRequirements(EnvAccessKeyID, EnvSecretAccessKey, EnvRegion, envDomain) func makeTestProvider(ts *httptest.Server) *DNSProvider { config := &aws.Config{ @@ -48,9 +50,9 @@ func Test_loadCredentials_FromEnv(t *testing.T) { defer envTest.RestoreEnv() envTest.ClearEnv() - os.Setenv("AWS_ACCESS_KEY_ID", "123") - os.Setenv("AWS_SECRET_ACCESS_KEY", "456") - os.Setenv("AWS_REGION", "us-east-1") + os.Setenv(EnvAccessKeyID, "123") + os.Setenv(EnvSecretAccessKey, "456") + os.Setenv(EnvRegion, "us-east-1") config := &aws.Config{ CredentialsChainVerboseErrors: aws.Bool(true), @@ -75,7 +77,7 @@ func Test_loadRegion_FromEnv(t *testing.T) { defer envTest.RestoreEnv() envTest.ClearEnv() - os.Setenv("AWS_REGION", route53.CloudWatchRegionUsEast1) + os.Setenv(EnvRegion, route53.CloudWatchRegionUsEast1) sess, err := session.NewSession(aws.NewConfig()) require.NoError(t, err) @@ -90,7 +92,7 @@ func Test_getHostedZoneID_FromEnv(t *testing.T) { expectedZoneID := "zoneID" - os.Setenv("AWS_HOSTED_ZONE_ID", expectedZoneID) + os.Setenv(EnvHostedZoneID, expectedZoneID) provider, err := NewDNSProvider() require.NoError(t, err) @@ -121,11 +123,11 @@ func TestNewDefaultConfig(t *testing.T) { { desc: "", envVars: map[string]string{ - "AWS_MAX_RETRIES": "10", - "AWS_TTL": "99", - "AWS_PROPAGATION_TIMEOUT": "60", - "AWS_POLLING_INTERVAL": "60", - "AWS_HOSTED_ZONE_ID": "abc123", + EnvMaxRetries: "10", + EnvTTL: "99", + EnvPropagationTimeout: "60", + EnvPollingInterval: "60", + EnvHostedZoneID: "abc123", }, expected: &Config{ MaxRetries: 10, diff --git a/providers/dns/sakuracloud/sakuracloud.go b/providers/dns/sakuracloud/sakuracloud.go index e51c55ee..54f9dced 100644 --- a/providers/dns/sakuracloud/sakuracloud.go +++ b/providers/dns/sakuracloud/sakuracloud.go @@ -12,6 +12,19 @@ import ( "github.com/sacloud/libsacloud/api" ) +// Environment variables names. +const ( + envNamespace = "SAKURACLOUD_" + + EnvAccessToken = envNamespace + "ACCESS_TOKEN" + EnvAccessTokenSecret = envNamespace + "ACCESS_TOKEN_SECRET" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { Token string @@ -25,11 +38,11 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("SAKURACLOUD_TTL", dns01.DefaultTTL), - PropagationTimeout: env.GetOrDefaultSecond("SAKURACLOUD_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("SAKURACLOUD_POLLING_INTERVAL", dns01.DefaultPollingInterval), + TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("SAKURACLOUD_HTTP_TIMEOUT", 10*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 10*time.Second), }, } } @@ -43,14 +56,14 @@ type DNSProvider struct { // NewDNSProvider returns a DNSProvider instance configured for SakuraCloud. // Credentials must be passed in the environment variables: SAKURACLOUD_ACCESS_TOKEN & SAKURACLOUD_ACCESS_TOKEN_SECRET func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("SAKURACLOUD_ACCESS_TOKEN", "SAKURACLOUD_ACCESS_TOKEN_SECRET") + values, err := env.Get(EnvAccessToken, EnvAccessTokenSecret) if err != nil { return nil, fmt.Errorf("sakuracloud: %w", err) } config := NewDefaultConfig() - config.Token = values["SAKURACLOUD_ACCESS_TOKEN"] - config.Secret = values["SAKURACLOUD_ACCESS_TOKEN_SECRET"] + config.Token = values[EnvAccessToken] + config.Secret = values[EnvAccessTokenSecret] return NewDNSProviderConfig(config) } diff --git a/providers/dns/sakuracloud/sakuracloud_test.go b/providers/dns/sakuracloud/sakuracloud_test.go index 4a784c19..fe32876c 100644 --- a/providers/dns/sakuracloud/sakuracloud_test.go +++ b/providers/dns/sakuracloud/sakuracloud_test.go @@ -8,10 +8,12 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "SAKURACLOUD_ACCESS_TOKEN", - "SAKURACLOUD_ACCESS_TOKEN_SECRET"). - WithDomain("SAKURACLOUD_DOMAIN") + EnvAccessToken, + EnvAccessTokenSecret). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -22,31 +24,31 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "SAKURACLOUD_ACCESS_TOKEN": "123", - "SAKURACLOUD_ACCESS_TOKEN_SECRET": "456", + EnvAccessToken: "123", + EnvAccessTokenSecret: "456", }, }, { desc: "missing credentials", envVars: map[string]string{ - "SAKURACLOUD_ACCESS_TOKEN": "", - "SAKURACLOUD_ACCESS_TOKEN_SECRET": "", + EnvAccessToken: "", + EnvAccessTokenSecret: "", }, expected: "sakuracloud: some credentials information are missing: SAKURACLOUD_ACCESS_TOKEN,SAKURACLOUD_ACCESS_TOKEN_SECRET", }, { desc: "missing access token", envVars: map[string]string{ - "SAKURACLOUD_ACCESS_TOKEN": "", - "SAKURACLOUD_ACCESS_TOKEN_SECRET": "456", + EnvAccessToken: "", + EnvAccessTokenSecret: "456", }, expected: "sakuracloud: some credentials information are missing: SAKURACLOUD_ACCESS_TOKEN", }, { desc: "missing token secret", envVars: map[string]string{ - "SAKURACLOUD_ACCESS_TOKEN": "123", - "SAKURACLOUD_ACCESS_TOKEN_SECRET": "", + EnvAccessToken: "123", + EnvAccessTokenSecret: "", }, expected: "sakuracloud: some credentials information are missing: SAKURACLOUD_ACCESS_TOKEN_SECRET", }, diff --git a/providers/dns/scaleway/scaleway.go b/providers/dns/scaleway/scaleway.go index 3107db5a..1be072c3 100644 --- a/providers/dns/scaleway/scaleway.go +++ b/providers/dns/scaleway/scaleway.go @@ -22,15 +22,18 @@ const ( defaultPropagationTimeout = 120 * time.Second ) +// Environment variables names. const ( - envNamespace = "SCALEWAY_" - baseURLEnvVar = envNamespace + "BASE_URL" - apiTokenEnvVar = envNamespace + "API_TOKEN" - apiVersionEnvVar = envNamespace + "API_VERSION" - ttlEnvVar = envNamespace + "TTL" - propagationTimeoutEnvVar = envNamespace + "PROPAGATION_TIMEOUT" - pollingIntervalEnvVar = envNamespace + "POLLING_INTERVAL" - httpTimeoutEnvVar = envNamespace + "HTTP_TIMEOUT" + envNamespace = "SCALEWAY_" + + EnvBaseURL = envNamespace + "BASE_URL" + EnvAPIToken = envNamespace + "API_TOKEN" + EnvAPIVersion = envNamespace + "API_VERSION" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" ) // Config is used to configure the creation of the DNSProvider. @@ -47,13 +50,13 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider. func NewDefaultConfig() *Config { return &Config{ - BaseURL: env.GetOrDefaultString(baseURLEnvVar, defaultBaseURL), - Version: env.GetOrDefaultString(apiVersionEnvVar, defaultVersion), - TTL: env.GetOrDefaultInt(ttlEnvVar, minTTL), - PropagationTimeout: env.GetOrDefaultSecond(propagationTimeoutEnvVar, defaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond(pollingIntervalEnvVar, defaultPollingInterval), + BaseURL: env.GetOrDefaultString(EnvBaseURL, defaultBaseURL), + Version: env.GetOrDefaultString(EnvAPIVersion, defaultVersion), + TTL: env.GetOrDefaultInt(EnvTTL, minTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, defaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, defaultPollingInterval), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond(httpTimeoutEnvVar, 30*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), }, } } @@ -67,13 +70,13 @@ type DNSProvider struct { // NewDNSProvider returns a DNSProvider instance configured for Scaleway Domains API. // API token must be passed in the environment variable SCALEWAY_API_TOKEN. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get(apiTokenEnvVar) + values, err := env.Get(EnvAPIToken) if err != nil { return nil, fmt.Errorf("scaleway: %w", err) } config := NewDefaultConfig() - config.Token = values[apiTokenEnvVar] + config.Token = values[EnvAPIToken] return NewDNSProviderConfig(config) } diff --git a/providers/dns/scaleway/scaleway_test.go b/providers/dns/scaleway/scaleway_test.go index f3fe9b5c..87a6a9a1 100644 --- a/providers/dns/scaleway/scaleway_test.go +++ b/providers/dns/scaleway/scaleway_test.go @@ -10,12 +10,10 @@ import ( "github.com/stretchr/testify/require" ) -const ( - cleanUpDelay = 2 * time.Second -) +const cleanUpDelay = 2 * time.Second func TestNewDNSProvider(t *testing.T) { - var envTest = tester.NewEnvTest(apiTokenEnvVar, ttlEnvVar) + var envTest = tester.NewEnvTest(EnvAPIToken, EnvTTL) testCases := []struct { desc string @@ -25,15 +23,15 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - apiTokenEnvVar: "123", + EnvAPIToken: "123", }, }, { desc: "missing api key", envVars: map[string]string{ - apiTokenEnvVar: "", + EnvAPIToken: "", }, - expected: fmt.Sprintf("scaleway: some credentials information are missing: %s", apiTokenEnvVar), + expected: fmt.Sprintf("scaleway: some credentials information are missing: %s", EnvAPIToken), }, } @@ -99,7 +97,7 @@ func TestNewDNSProviderConfig(t *testing.T) { } func TestLivePresent(t *testing.T) { - var envTest = tester.NewEnvTest(apiTokenEnvVar, ttlEnvVar) + var envTest = tester.NewEnvTest(EnvAPIToken, EnvTTL) if !envTest.IsLiveTest() { t.Skip("skipping live test") @@ -114,7 +112,7 @@ func TestLivePresent(t *testing.T) { } func TestLiveCleanUp(t *testing.T) { - var envTest = tester.NewEnvTest(apiTokenEnvVar, ttlEnvVar) + var envTest = tester.NewEnvTest(EnvAPIToken, EnvTTL) if !envTest.IsLiveTest() { t.Skip("skipping live test") diff --git a/providers/dns/selectel/selectel.go b/providers/dns/selectel/selectel.go index 66dfeac5..3810d9e0 100644 --- a/providers/dns/selectel/selectel.go +++ b/providers/dns/selectel/selectel.go @@ -19,14 +19,17 @@ const ( minTTL = 60 ) +// Environment variables names. const ( - envNamespace = "SELECTEL_" - baseURLEnvVar = envNamespace + "BASE_URL" - apiTokenEnvVar = envNamespace + "API_TOKEN" - ttlEnvVar = envNamespace + "TTL" - propagationTimeoutEnvVar = envNamespace + "PROPAGATION_TIMEOUT" - pollingIntervalEnvVar = envNamespace + "POLLING_INTERVAL" - httpTimeoutEnvVar = envNamespace + "HTTP_TIMEOUT" + envNamespace = "SELECTEL_" + + EnvBaseURL = envNamespace + "BASE_URL" + EnvAPIToken = envNamespace + "API_TOKEN" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" ) // Config is used to configure the creation of the DNSProvider. @@ -42,12 +45,12 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider. func NewDefaultConfig() *Config { return &Config{ - BaseURL: env.GetOrDefaultString(baseURLEnvVar, defaultBaseURL), - TTL: env.GetOrDefaultInt(ttlEnvVar, minTTL), - PropagationTimeout: env.GetOrDefaultSecond(propagationTimeoutEnvVar, 120*time.Second), - PollingInterval: env.GetOrDefaultSecond(pollingIntervalEnvVar, 2*time.Second), + BaseURL: env.GetOrDefaultString(EnvBaseURL, defaultBaseURL), + TTL: env.GetOrDefaultInt(EnvTTL, minTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 120*time.Second), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond(httpTimeoutEnvVar, 30*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), }, } } @@ -61,13 +64,13 @@ type DNSProvider struct { // NewDNSProvider returns a DNSProvider instance configured for Selectel Domains API. // API token must be passed in the environment variable SELECTEL_API_TOKEN. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get(apiTokenEnvVar) + values, err := env.Get(EnvAPIToken) if err != nil { return nil, fmt.Errorf("selectel: %w", err) } config := NewDefaultConfig() - config.Token = values[apiTokenEnvVar] + config.Token = values[EnvAPIToken] return NewDNSProviderConfig(config) } diff --git a/providers/dns/selectel/selectel_test.go b/providers/dns/selectel/selectel_test.go index 03d752ac..50d3ed40 100644 --- a/providers/dns/selectel/selectel_test.go +++ b/providers/dns/selectel/selectel_test.go @@ -10,7 +10,7 @@ import ( "github.com/stretchr/testify/require" ) -var envTest = tester.NewEnvTest(apiTokenEnvVar, ttlEnvVar) +var envTest = tester.NewEnvTest(EnvAPIToken, EnvTTL) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -21,15 +21,15 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - apiTokenEnvVar: "123", + EnvAPIToken: "123", }, }, { desc: "missing api key", envVars: map[string]string{ - apiTokenEnvVar: "", + EnvAPIToken: "", }, - expected: fmt.Sprintf("selectel: some credentials information are missing: %s", apiTokenEnvVar), + expected: fmt.Sprintf("selectel: some credentials information are missing: %s", EnvAPIToken), }, } diff --git a/providers/dns/servercow/servercow.go b/providers/dns/servercow/servercow.go index a4755caf..a8634aa7 100644 --- a/providers/dns/servercow/servercow.go +++ b/providers/dns/servercow/servercow.go @@ -14,6 +14,19 @@ import ( const defaultTTL = 120 +// Environment variables names. +const ( + envNamespace = "SERVERCOW_" + + EnvUsername = envNamespace + "USERNAME" + EnvPassword = envNamespace + "PASSWORD" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider. type Config struct { Username string @@ -28,11 +41,11 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("SERVERCOW_TTL", defaultTTL), - PropagationTimeout: env.GetOrDefaultSecond("SERVERCOW_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("SERVERCOW_POLLING_INTERVAL", dns01.DefaultPollingInterval), + TTL: env.GetOrDefaultInt(EnvTTL, defaultTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("SERVERCOW_HTTP_TIMEOUT", 30*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), }, } } @@ -45,14 +58,14 @@ type DNSProvider struct { // NewDNSProvider returns a DNSProvider instance. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("SERVERCOW_USERNAME", "SERVERCOW_PASSWORD") + values, err := env.Get(EnvUsername, EnvPassword) if err != nil { return nil, fmt.Errorf("servercow: %w", err) } config := NewDefaultConfig() - config.Username = values["SERVERCOW_USERNAME"] - config.Password = values["SERVERCOW_PASSWORD"] + config.Username = values[EnvUsername] + config.Password = values[EnvPassword] return NewDNSProviderConfig(config) } diff --git a/providers/dns/servercow/servercow_test.go b/providers/dns/servercow/servercow_test.go index 08746890..b4f00647 100644 --- a/providers/dns/servercow/servercow_test.go +++ b/providers/dns/servercow/servercow_test.go @@ -8,10 +8,12 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "SERVERCOW_USERNAME", - "SERVERCOW_PASSWORD"). - WithDomain("SERVERCOW_DOMAIN") + EnvUsername, + EnvPassword). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -22,31 +24,31 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "SERVERCOW_USERNAME": "123", - "SERVERCOW_PASSWORD": "456", + EnvUsername: "123", + EnvPassword: "456", }, }, { desc: "missing credentials", envVars: map[string]string{ - "SERVERCOW_USERNAME": "", - "SERVERCOW_PASSWORD": "", + EnvUsername: "", + EnvPassword: "", }, expected: "servercow: some credentials information are missing: SERVERCOW_USERNAME,SERVERCOW_PASSWORD", }, { desc: "missing username", envVars: map[string]string{ - "SERVERCOW_USERNAME": "", - "SERVERCOW_PASSWORD": "api_password", + EnvUsername: "", + EnvPassword: "api_password", }, expected: "servercow: some credentials information are missing: SERVERCOW_USERNAME", }, { desc: "missing password", envVars: map[string]string{ - "SERVERCOW_USERNAME": "api_username", - "SERVERCOW_PASSWORD": "", + EnvUsername: "api_username", + EnvPassword: "", }, expected: "servercow: some credentials information are missing: SERVERCOW_PASSWORD", }, diff --git a/providers/dns/stackpath/stackpath.go b/providers/dns/stackpath/stackpath.go index 3898e784..6ff54cdf 100644 --- a/providers/dns/stackpath/stackpath.go +++ b/providers/dns/stackpath/stackpath.go @@ -22,6 +22,19 @@ const ( defaultAuthURL = "https://gateway.stackpath.com/identity/v1/oauth2/token" ) +// Environment variables names. +const ( + envNamespace = "STACKPATH_" + + EnvClientID = envNamespace + "CLIENT_ID" + EnvClientSecret = envNamespace + "CLIENT_SECRET" + EnvStackID = envNamespace + "STACK_ID" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" +) + // Config is used to configure the creation of the DNSProvider type Config struct { ClientID string @@ -35,9 +48,9 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("STACKPATH_TTL", 120), - PropagationTimeout: env.GetOrDefaultSecond("STACKPATH_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("STACKPATH_POLLING_INTERVAL", dns01.DefaultPollingInterval), + TTL: env.GetOrDefaultInt(EnvTTL, 120), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), } } @@ -52,15 +65,15 @@ type DNSProvider struct { // Credentials must be passed in the environment variables: // STACKPATH_CLIENT_ID, STACKPATH_CLIENT_SECRET, and STACKPATH_STACK_ID. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("STACKPATH_CLIENT_ID", "STACKPATH_CLIENT_SECRET", "STACKPATH_STACK_ID") + values, err := env.Get(EnvClientID, EnvClientSecret, EnvStackID) if err != nil { return nil, fmt.Errorf("stackpath: %w", err) } config := NewDefaultConfig() - config.ClientID = values["STACKPATH_CLIENT_ID"] - config.ClientSecret = values["STACKPATH_CLIENT_SECRET"] - config.StackID = values["STACKPATH_STACK_ID"] + config.ClientID = values[EnvClientID] + config.ClientSecret = values[EnvClientSecret] + config.StackID = values[EnvStackID] return NewDNSProviderConfig(config) } diff --git a/providers/dns/stackpath/stackpath_test.go b/providers/dns/stackpath/stackpath_test.go index 819ccb11..e1562483 100644 --- a/providers/dns/stackpath/stackpath_test.go +++ b/providers/dns/stackpath/stackpath_test.go @@ -12,11 +12,13 @@ import ( "github.com/stretchr/testify/require" ) +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "STACKPATH_CLIENT_ID", - "STACKPATH_CLIENT_SECRET", - "STACKPATH_STACK_ID"). - WithDomain("STACKPATH_DOMAIN") + EnvClientID, + EnvClientSecret, + EnvStackID). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -27,44 +29,44 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "STACKPATH_CLIENT_ID": "test@example.com", - "STACKPATH_CLIENT_SECRET": "123", - "STACKPATH_STACK_ID": "ID", + EnvClientID: "test@example.com", + EnvClientSecret: "123", + EnvStackID: "ID", }, }, { desc: "missing credentials", envVars: map[string]string{ - "STACKPATH_CLIENT_ID": "", - "STACKPATH_CLIENT_SECRET": "", - "STACKPATH_STACK_ID": "", + EnvClientID: "", + EnvClientSecret: "", + EnvStackID: "", }, expected: "stackpath: some credentials information are missing: STACKPATH_CLIENT_ID,STACKPATH_CLIENT_SECRET,STACKPATH_STACK_ID", }, { desc: "missing client id", envVars: map[string]string{ - "STACKPATH_CLIENT_ID": "", - "STACKPATH_CLIENT_SECRET": "123", - "STACKPATH_STACK_ID": "ID", + EnvClientID: "", + EnvClientSecret: "123", + EnvStackID: "ID", }, expected: "stackpath: some credentials information are missing: STACKPATH_CLIENT_ID", }, { desc: "missing client secret", envVars: map[string]string{ - "STACKPATH_CLIENT_ID": "test@example.com", - "STACKPATH_CLIENT_SECRET": "", - "STACKPATH_STACK_ID": "ID", + EnvClientID: "test@example.com", + EnvClientSecret: "", + EnvStackID: "ID", }, expected: "stackpath: some credentials information are missing: STACKPATH_CLIENT_SECRET", }, { desc: "missing stack id", envVars: map[string]string{ - "STACKPATH_CLIENT_ID": "test@example.com", - "STACKPATH_CLIENT_SECRET": "123", - "STACKPATH_STACK_ID": "", + EnvClientID: "test@example.com", + EnvClientSecret: "123", + EnvStackID: "", }, expected: "stackpath: some credentials information are missing: STACKPATH_STACK_ID", }, diff --git a/providers/dns/transip/transip.go b/providers/dns/transip/transip.go index e1215556..6de7972c 100644 --- a/providers/dns/transip/transip.go +++ b/providers/dns/transip/transip.go @@ -14,6 +14,18 @@ import ( transipdomain "github.com/transip/gotransip/domain" ) +// Environment variables names. +const ( + envNamespace = "TRANSIP_" + + EnvAccountName = envNamespace + "ACCOUNT_NAME" + EnvPrivateKeyPath = envNamespace + "PRIVATE_KEY_PATH" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" +) + // Config is used to configure the creation of the DNSProvider type Config struct { AccountName string @@ -26,9 +38,9 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: int64(env.GetOrDefaultInt("TRANSIP_TTL", 10)), - PropagationTimeout: env.GetOrDefaultSecond("TRANSIP_PROPAGATION_TIMEOUT", 10*time.Minute), - PollingInterval: env.GetOrDefaultSecond("TRANSIP_POLLING_INTERVAL", 10*time.Second), + TTL: int64(env.GetOrDefaultInt(EnvTTL, 10)), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 10*time.Minute), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 10*time.Second), } } @@ -43,14 +55,14 @@ type DNSProvider struct { // Credentials must be passed in the environment variables: // TRANSIP_ACCOUNTNAME, TRANSIP_PRIVATEKEYPATH. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("TRANSIP_ACCOUNT_NAME", "TRANSIP_PRIVATE_KEY_PATH") + values, err := env.Get(EnvAccountName, EnvPrivateKeyPath) if err != nil { return nil, fmt.Errorf("transip: %w", err) } config := NewDefaultConfig() - config.AccountName = values["TRANSIP_ACCOUNT_NAME"] - config.PrivateKeyPath = values["TRANSIP_PRIVATE_KEY_PATH"] + config.AccountName = values[EnvAccountName] + config.PrivateKeyPath = values[EnvPrivateKeyPath] return NewDNSProviderConfig(config) } diff --git a/providers/dns/transip/transip_test.go b/providers/dns/transip/transip_test.go index 9ab44b98..a4cb6062 100644 --- a/providers/dns/transip/transip_test.go +++ b/providers/dns/transip/transip_test.go @@ -77,10 +77,12 @@ func (f *fakeClient) Call(r gotransip.SoapRequest, b interface{}) error { } } +const envDomain = envNamespace + "DOMAIN" + var envTest = tester.NewEnvTest( - "TRANSIP_ACCOUNT_NAME", - "TRANSIP_PRIVATE_KEY_PATH"). - WithDomain("TRANSIP_DOMAIN") + EnvAccountName, + EnvPrivateKeyPath). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -91,39 +93,39 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "TRANSIP_ACCOUNT_NAME": "johndoe", - "TRANSIP_PRIVATE_KEY_PATH": "./fixtures/private.key", + EnvAccountName: "johndoe", + EnvPrivateKeyPath: "./fixtures/private.key", }, }, { desc: "missing all credentials", envVars: map[string]string{ - "TRANSIP_ACCOUNT_NAME": "", - "TRANSIP_PRIVATE_KEY_PATH": "", + EnvAccountName: "", + EnvPrivateKeyPath: "", }, expected: "transip: some credentials information are missing: TRANSIP_ACCOUNT_NAME,TRANSIP_PRIVATE_KEY_PATH", }, { desc: "missing account name", envVars: map[string]string{ - "TRANSIP_ACCOUNT_NAME": "", - "TRANSIP_PRIVATE_KEY_PATH": "./fixtures/private.key", + EnvAccountName: "", + EnvPrivateKeyPath: "./fixtures/private.key", }, expected: "transip: some credentials information are missing: TRANSIP_ACCOUNT_NAME", }, { desc: "missing private key path", envVars: map[string]string{ - "TRANSIP_ACCOUNT_NAME": "johndoe", - "TRANSIP_PRIVATE_KEY_PATH": "", + EnvAccountName: "johndoe", + EnvPrivateKeyPath: "", }, expected: "transip: some credentials information are missing: TRANSIP_PRIVATE_KEY_PATH", }, { desc: "could not open private key path", envVars: map[string]string{ - "TRANSIP_ACCOUNT_NAME": "johndoe", - "TRANSIP_PRIVATE_KEY_PATH": "./fixtures/non/existent/private.key", + EnvAccountName: "johndoe", + EnvPrivateKeyPath: "./fixtures/non/existent/private.key", }, expected: "transip: could not open private key: stat ./fixtures/non/existent/private.key: no such file or directory", }, diff --git a/providers/dns/vegadns/vegadns.go b/providers/dns/vegadns/vegadns.go index e312933d..aeae3dfc 100644 --- a/providers/dns/vegadns/vegadns.go +++ b/providers/dns/vegadns/vegadns.go @@ -12,6 +12,19 @@ import ( "github.com/go-acme/lego/v3/platform/config/env" ) +// Environment variables names. +const ( + envNamespace = "VEGADNS_" + + EnvKey = "SECRET_VEGADNS_KEY" + EnvSecret = "SECRET_VEGADNS_SECRET" + EnvURL = envNamespace + "URL" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" +) + // Config is used to configure the creation of the DNSProvider type Config struct { BaseURL string @@ -25,9 +38,9 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("VEGADNS_TTL", 10), - PropagationTimeout: env.GetOrDefaultSecond("VEGADNS_PROPAGATION_TIMEOUT", 12*time.Minute), - PollingInterval: env.GetOrDefaultSecond("VEGADNS_POLLING_INTERVAL", 1*time.Minute), + TTL: env.GetOrDefaultInt(EnvTTL, 10), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 12*time.Minute), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 1*time.Minute), } } @@ -41,15 +54,15 @@ type DNSProvider struct { // Credentials must be passed in the environment variables: // VEGADNS_URL, SECRET_VEGADNS_KEY, SECRET_VEGADNS_SECRET. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("VEGADNS_URL") + values, err := env.Get(EnvURL) if err != nil { return nil, fmt.Errorf("vegadns: %w", err) } config := NewDefaultConfig() - config.BaseURL = values["VEGADNS_URL"] - config.APIKey = env.GetOrFile("SECRET_VEGADNS_KEY") - config.APISecret = env.GetOrFile("SECRET_VEGADNS_SECRET") + config.BaseURL = values[EnvURL] + config.APIKey = env.GetOrFile(EnvKey) + config.APISecret = env.GetOrFile(EnvSecret) return NewDNSProviderConfig(config) } diff --git a/providers/dns/vegadns/vegadns_test.go b/providers/dns/vegadns/vegadns_test.go index 998b2bfb..f22c0afd 100644 --- a/providers/dns/vegadns/vegadns_test.go +++ b/providers/dns/vegadns/vegadns_test.go @@ -14,7 +14,7 @@ import ( const testDomain = "example.com" -var envTest = tester.NewEnvTest("SECRET_VEGADNS_KEY", "SECRET_VEGADNS_SECRET", "VEGADNS_URL") +var envTest = tester.NewEnvTest(EnvKey, EnvSecret, EnvURL) func TestNewDNSProvider_Fail(t *testing.T) { defer envTest.RestoreEnv() @@ -277,9 +277,9 @@ func startTestServer(handler http.Handler) func() { ts := httptest.NewServer(handler) envTest.Apply(map[string]string{ - "SECRET_VEGADNS_KEY": "key", - "SECRET_VEGADNS_SECRET": "secret", - "VEGADNS_URL": ts.URL, + EnvKey: "key", + EnvSecret: "secret", + EnvURL: ts.URL, }) return func() { diff --git a/providers/dns/versio/versio.go b/providers/dns/versio/versio.go index fc4781ea..12cde1ed 100644 --- a/providers/dns/versio/versio.go +++ b/providers/dns/versio/versio.go @@ -13,6 +13,21 @@ import ( "github.com/go-acme/lego/v3/platform/config/env" ) +// Environment variables names. +const ( + envNamespace = "VERSIO_" + + EnvUsername = envNamespace + "USERNAME" + EnvPassword = envNamespace + "PASSWORD" + EnvEndpoint = envNamespace + "ENDPOINT" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvSequenceInterval = envNamespace + "SEQUENCE_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { BaseURL *url.URL @@ -27,19 +42,19 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { - baseURL, err := url.Parse(env.GetOrDefaultString("VERSIO_ENDPOINT", defaultBaseURL)) + baseURL, err := url.Parse(env.GetOrDefaultString(EnvEndpoint, defaultBaseURL)) if err != nil { baseURL, _ = url.Parse(defaultBaseURL) } return &Config{ BaseURL: baseURL, - TTL: env.GetOrDefaultInt("VERSIO_TTL", 300), - PropagationTimeout: env.GetOrDefaultSecond("VERSIO_PROPAGATION_TIMEOUT", 60*time.Second), - PollingInterval: env.GetOrDefaultSecond("VERSIO_POLLING_INTERVAL", 5*time.Second), - SequenceInterval: env.GetOrDefaultSecond("VERSIO_SEQUENCE_INTERVAL", dns01.DefaultPropagationTimeout), + TTL: env.GetOrDefaultInt(EnvTTL, 300), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 60*time.Second), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 5*time.Second), + SequenceInterval: env.GetOrDefaultSecond(EnvSequenceInterval, dns01.DefaultPropagationTimeout), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("VERSIO_HTTP_TIMEOUT", 30*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), }, } } @@ -52,14 +67,14 @@ type DNSProvider struct { // NewDNSProvider returns a DNSProvider instance. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("VERSIO_USERNAME", "VERSIO_PASSWORD") + values, err := env.Get(EnvUsername, EnvPassword) if err != nil { return nil, fmt.Errorf("versio: %w", err) } config := NewDefaultConfig() - config.Username = values["VERSIO_USERNAME"] - config.Password = values["VERSIO_PASSWORD"] + config.Username = values[EnvUsername] + config.Password = values[EnvPassword] return NewDNSProviderConfig(config) } diff --git a/providers/dns/versio/versio_test.go b/providers/dns/versio/versio_test.go index eedd2b3a..6d238d8f 100644 --- a/providers/dns/versio/versio_test.go +++ b/providers/dns/versio/versio_test.go @@ -14,7 +14,9 @@ import ( const testDomain = "example.com" -var envTest = tester.NewEnvTest("VERSIO_USERNAME", "VERSIO_PASSWORD", "VERSIO_ENDPOINT").WithDomain("VERSIO_DOMAIN") +const envDomain = envNamespace + "DOMAIN" + +var envTest = tester.NewEnvTest(EnvUsername, EnvPassword, EnvEndpoint).WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -25,21 +27,21 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "VERSIO_USERNAME": "me@example.com", - "VERSIO_PASSWORD": "SECRET", + EnvUsername: "me@example.com", + EnvPassword: "SECRET", }, }, { desc: "missing token", envVars: map[string]string{ - "VERSIO_PASSWORD": "me@example.com", + EnvPassword: "me@example.com", }, expected: "versio: some credentials information are missing: VERSIO_USERNAME", }, { desc: "missing key", envVars: map[string]string{ - "VERSIO_USERNAME": "TOKEN", + EnvUsername: "TOKEN", }, expected: "versio: some credentials information are missing: VERSIO_PASSWORD", }, @@ -150,9 +152,9 @@ func TestDNSProvider_Present(t *testing.T) { defer tearDown() envTest.Apply(map[string]string{ - "VERSIO_USERNAME": "me@example.com", - "VERSIO_PASSWORD": "secret", - "VERSIO_ENDPOINT": baseURL, + EnvUsername: "me@example.com", + EnvPassword: "secret", + EnvEndpoint: baseURL, }) provider, err := NewDNSProvider() require.NoError(t, err) @@ -193,9 +195,9 @@ func TestDNSProvider_CleanUp(t *testing.T) { defer tearDown() envTest.Apply(map[string]string{ - "VERSIO_USERNAME": "me@example.com", - "VERSIO_PASSWORD": "secret", - "VERSIO_ENDPOINT": baseURL, + EnvUsername: "me@example.com", + EnvPassword: "secret", + EnvEndpoint: baseURL, }) provider, err := NewDNSProvider() diff --git a/providers/dns/vscale/vscale.go b/providers/dns/vscale/vscale.go index 37ea05dc..6fbec551 100644 --- a/providers/dns/vscale/vscale.go +++ b/providers/dns/vscale/vscale.go @@ -20,14 +20,17 @@ const ( minTTL = 60 ) +// Environment variables names. const ( - envNamespace = "VSCALE_" - baseURLEnvVar = envNamespace + "BASE_URL" - apiTokenEnvVar = envNamespace + "API_TOKEN" - ttlEnvVar = envNamespace + "TTL" - propagationTimeoutEnvVar = envNamespace + "PROPAGATION_TIMEOUT" - pollingIntervalEnvVar = envNamespace + "POLLING_INTERVAL" - httpTimeoutEnvVar = envNamespace + "HTTP_TIMEOUT" + envNamespace = "VSCALE_" + + EnvBaseURL = envNamespace + "BASE_URL" + EnvAPIToken = envNamespace + "API_TOKEN" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" ) // Config is used to configure the creation of the DNSProvider. @@ -43,12 +46,12 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider. func NewDefaultConfig() *Config { return &Config{ - BaseURL: env.GetOrDefaultString(baseURLEnvVar, defaultBaseURL), - TTL: env.GetOrDefaultInt(ttlEnvVar, minTTL), - PropagationTimeout: env.GetOrDefaultSecond(propagationTimeoutEnvVar, 120*time.Second), - PollingInterval: env.GetOrDefaultSecond(pollingIntervalEnvVar, 2*time.Second), + BaseURL: env.GetOrDefaultString(EnvBaseURL, defaultBaseURL), + TTL: env.GetOrDefaultInt(EnvTTL, minTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 120*time.Second), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond(httpTimeoutEnvVar, 30*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), }, } } @@ -62,13 +65,13 @@ type DNSProvider struct { // NewDNSProvider returns a DNSProvider instance configured for Vscale Domains API. // API token must be passed in the environment variable VSCALE_API_TOKEN. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get(apiTokenEnvVar) + values, err := env.Get(EnvAPIToken) if err != nil { return nil, fmt.Errorf("vscale: %w", err) } config := NewDefaultConfig() - config.Token = values[apiTokenEnvVar] + config.Token = values[EnvAPIToken] return NewDNSProviderConfig(config) } diff --git a/providers/dns/vscale/vscale_test.go b/providers/dns/vscale/vscale_test.go index ecbb4915..801cbe0d 100644 --- a/providers/dns/vscale/vscale_test.go +++ b/providers/dns/vscale/vscale_test.go @@ -10,7 +10,7 @@ import ( "github.com/stretchr/testify/require" ) -var envTest = tester.NewEnvTest(apiTokenEnvVar, ttlEnvVar) +var envTest = tester.NewEnvTest(EnvAPIToken, EnvTTL) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -21,15 +21,15 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - apiTokenEnvVar: "123", + EnvAPIToken: "123", }, }, { desc: "missing api key", envVars: map[string]string{ - apiTokenEnvVar: "", + EnvAPIToken: "", }, - expected: fmt.Sprintf("vscale: some credentials information are missing: %s", apiTokenEnvVar), + expected: fmt.Sprintf("vscale: some credentials information are missing: %s", EnvAPIToken), }, } diff --git a/providers/dns/vultr/vultr.go b/providers/dns/vultr/vultr.go index 50acba06..fca5222f 100644 --- a/providers/dns/vultr/vultr.go +++ b/providers/dns/vultr/vultr.go @@ -17,6 +17,18 @@ import ( "github.com/vultr/govultr" ) +// Environment variables names. +const ( + envNamespace = "VULTR_" + + EnvAPIKey = envNamespace + "API_KEY" + + EnvTTL = envNamespace + "TTL" + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { APIKey string @@ -29,11 +41,11 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider func NewDefaultConfig() *Config { return &Config{ - TTL: env.GetOrDefaultInt("VULTR_TTL", dns01.DefaultTTL), - PropagationTimeout: env.GetOrDefaultSecond("VULTR_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout), - PollingInterval: env.GetOrDefaultSecond("VULTR_POLLING_INTERVAL", dns01.DefaultPollingInterval), + TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("VULTR_HTTP_TIMEOUT", 0), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30), // from Vultr Client Transport: &http.Transport{ TLSNextProto: make(map[string]func(string, *tls.Conn) http.RoundTripper), @@ -51,13 +63,13 @@ type DNSProvider struct { // NewDNSProvider returns a DNSProvider instance with a configured Vultr client. // Authentication uses the VULTR_API_KEY environment variable. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("VULTR_API_KEY") + values, err := env.Get(EnvAPIKey) if err != nil { return nil, fmt.Errorf("vultr: %w", err) } config := NewDefaultConfig() - config.APIKey = values["VULTR_API_KEY"] + config.APIKey = values[EnvAPIKey] return NewDNSProviderConfig(config) } diff --git a/providers/dns/vultr/vultr_test.go b/providers/dns/vultr/vultr_test.go index 4136699f..7104d129 100644 --- a/providers/dns/vultr/vultr_test.go +++ b/providers/dns/vultr/vultr_test.go @@ -8,8 +8,10 @@ import ( "github.com/stretchr/testify/require" ) -var envTest = tester.NewEnvTest("VULTR_API_KEY"). - WithDomain("VULTR_TEST_DOMAIN") +const envDomain = envNamespace + "TEST_DOMAIN" + +var envTest = tester.NewEnvTest(EnvAPIKey). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -20,13 +22,13 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "VULTR_API_KEY": "123", + EnvAPIKey: "123", }, }, { desc: "missing api key", envVars: map[string]string{ - "VULTR_API_KEY": "", + EnvAPIKey: "", }, expected: "vultr: some credentials information are missing: VULTR_API_KEY", }, diff --git a/providers/dns/zoneee/zoneee.go b/providers/dns/zoneee/zoneee.go index 1f2e3700..d568078e 100644 --- a/providers/dns/zoneee/zoneee.go +++ b/providers/dns/zoneee/zoneee.go @@ -12,6 +12,19 @@ import ( "github.com/go-acme/lego/v3/platform/config/env" ) +// Environment variables names. +const ( + envNamespace = "ZONEEE_" + + EnvEndpoint = envNamespace + "ENDPOINT" + EnvAPIUser = envNamespace + "API_USER" + EnvAPIKey = envNamespace + "API_KEY" + + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" +) + // Config is used to configure the creation of the DNSProvider type Config struct { Endpoint *url.URL @@ -29,10 +42,10 @@ func NewDefaultConfig() *Config { return &Config{ Endpoint: endpoint, // zone.ee can take up to 5min to propagate according to the support - PropagationTimeout: env.GetOrDefaultSecond("ZONEEE_PROPAGATION_TIMEOUT", 5*time.Minute), - PollingInterval: env.GetOrDefaultSecond("ZONEEE_POLLING_INTERVAL", 5*time.Second), + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 5*time.Minute), + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 5*time.Second), HTTPClient: &http.Client{ - Timeout: env.GetOrDefaultSecond("ZONEEE_HTTP_TIMEOUT", 30*time.Second), + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), }, } } @@ -44,20 +57,20 @@ type DNSProvider struct { // NewDNSProvider returns a DNSProvider instance. func NewDNSProvider() (*DNSProvider, error) { - values, err := env.Get("ZONEEE_API_USER", "ZONEEE_API_KEY") + values, err := env.Get(EnvAPIUser, EnvAPIKey) if err != nil { return nil, fmt.Errorf("zoneee: %w", err) } - rawEndpoint := env.GetOrDefaultString("ZONEEE_ENDPOINT", defaultEndpoint) + rawEndpoint := env.GetOrDefaultString(EnvEndpoint, defaultEndpoint) endpoint, err := url.Parse(rawEndpoint) if err != nil { return nil, fmt.Errorf("zoneee: %w", err) } config := NewDefaultConfig() - config.Username = values["ZONEEE_API_USER"] - config.APIKey = values["ZONEEE_API_KEY"] + config.Username = values[EnvAPIUser] + config.APIKey = values[EnvAPIKey] config.Endpoint = endpoint return NewDNSProviderConfig(config) diff --git a/providers/dns/zoneee/zoneee_test.go b/providers/dns/zoneee/zoneee_test.go index 1001a782..6cb06461 100644 --- a/providers/dns/zoneee/zoneee_test.go +++ b/providers/dns/zoneee/zoneee_test.go @@ -13,9 +13,11 @@ import ( "github.com/stretchr/testify/require" ) -var envTest = tester.NewEnvTest("ZONEEE_ENDPOINT", "ZONEEE_API_USER", "ZONEEE_API_KEY"). - WithLiveTestRequirements("ZONEEE_API_USER", "ZONEEE_API_KEY"). - WithDomain("ZONEE_DOMAIN") +const envDomain = envNamespace + "DOMAIN" + +var envTest = tester.NewEnvTest(EnvEndpoint, EnvAPIUser, EnvAPIKey). + WithLiveTestRequirements(EnvAPIUser, EnvAPIKey). + WithDomain(envDomain) func TestNewDNSProvider(t *testing.T) { testCases := []struct { @@ -26,40 +28,40 @@ func TestNewDNSProvider(t *testing.T) { { desc: "success", envVars: map[string]string{ - "ZONEEE_API_USER": "123", - "ZONEEE_API_KEY": "456", + EnvAPIUser: "123", + EnvAPIKey: "456", }, }, { desc: "missing credentials", envVars: map[string]string{ - "ZONEEE_API_USER": "", - "ZONEEE_API_KEY": "", + EnvAPIUser: "", + EnvAPIKey: "", }, expected: "zoneee: some credentials information are missing: ZONEEE_API_USER,ZONEEE_API_KEY", }, { desc: "missing username", envVars: map[string]string{ - "ZONEEE_API_USER": "", - "ZONEEE_API_KEY": "456", + EnvAPIUser: "", + EnvAPIKey: "456", }, expected: "zoneee: some credentials information are missing: ZONEEE_API_USER", }, { desc: "missing API key", envVars: map[string]string{ - "ZONEEE_API_USER": "123", - "ZONEEE_API_KEY": "", + EnvAPIUser: "123", + EnvAPIKey: "", }, expected: "zoneee: some credentials information are missing: ZONEEE_API_KEY", }, { desc: "invalid URL", envVars: map[string]string{ - "ZONEEE_API_USER": "123", - "ZONEEE_API_KEY": "456", - "ZONEEE_ENDPOINT": ":", + EnvAPIUser: "123", + EnvAPIKey: "456", + EnvEndpoint: ":", }, expected: `zoneee: parse ":": missing protocol scheme`, },