chore: homogenous zone name env var management (#2232)

This commit is contained in:
Ludovic Fernandez 2024-07-21 15:06:01 +02:00 committed by GitHub
parent c759f56556
commit 5bea70766f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 65 additions and 49 deletions

View file

@ -41,6 +41,8 @@ const (
// Config is used to configure the creation of the DNSProvider. // Config is used to configure the creation of the DNSProvider.
type Config struct { type Config struct {
ZoneName string
// optional if using instance metadata service // optional if using instance metadata service
ClientID string ClientID string
ClientSecret string ClientSecret string
@ -63,6 +65,7 @@ type Config struct {
// NewDefaultConfig returns a default configuration for the DNSProvider. // NewDefaultConfig returns a default configuration for the DNSProvider.
func NewDefaultConfig() *Config { func NewDefaultConfig() *Config {
return &Config{ return &Config{
ZoneName: env.GetOrFile(EnvZoneName),
TTL: env.GetOrDefaultInt(EnvTTL, 60), TTL: env.GetOrDefaultInt(EnvTTL, 60),
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 2*time.Minute), PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 2*time.Minute),
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second), PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second),

View file

@ -11,7 +11,6 @@ import (
"github.com/Azure/go-autorest/autorest" "github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/to" "github.com/Azure/go-autorest/autorest/to"
"github.com/go-acme/lego/v4/challenge/dns01" "github.com/go-acme/lego/v4/challenge/dns01"
"github.com/go-acme/lego/v4/platform/config/env"
) )
// dnsProviderPrivate implements the challenge.Provider interface for Azure Private Zone DNS. // dnsProviderPrivate implements the challenge.Provider interface for Azure Private Zone DNS.
@ -112,8 +111,8 @@ func (d *dnsProviderPrivate) CleanUp(domain, token, keyAuth string) error {
// Checks that azure has a zone for this domain name. // Checks that azure has a zone for this domain name.
func (d *dnsProviderPrivate) getHostedZoneID(ctx context.Context, fqdn string) (string, error) { func (d *dnsProviderPrivate) getHostedZoneID(ctx context.Context, fqdn string) (string, error) {
if zone := env.GetOrFile(EnvZoneName); zone != "" { if d.config.ZoneName != "" {
return zone, nil return d.config.ZoneName, nil
} }
authZone, err := dns01.FindZoneByFqdn(fqdn) authZone, err := dns01.FindZoneByFqdn(fqdn)

View file

@ -11,7 +11,6 @@ import (
"github.com/Azure/go-autorest/autorest" "github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/to" "github.com/Azure/go-autorest/autorest/to"
"github.com/go-acme/lego/v4/challenge/dns01" "github.com/go-acme/lego/v4/challenge/dns01"
"github.com/go-acme/lego/v4/platform/config/env"
) )
// dnsProviderPublic implements the challenge.Provider interface for Azure Public Zone DNS. // dnsProviderPublic implements the challenge.Provider interface for Azure Public Zone DNS.
@ -112,8 +111,8 @@ func (d *dnsProviderPublic) CleanUp(domain, token, keyAuth string) error {
// Checks that azure has a zone for this domain name. // Checks that azure has a zone for this domain name.
func (d *dnsProviderPublic) getHostedZoneID(ctx context.Context, fqdn string) (string, error) { func (d *dnsProviderPublic) getHostedZoneID(ctx context.Context, fqdn string) (string, error) {
if zone := env.GetOrFile(EnvZoneName); zone != "" { if d.config.ZoneName != "" {
return zone, nil return d.config.ZoneName, nil
} }
authZone, err := dns01.FindZoneByFqdn(fqdn) authZone, err := dns01.FindZoneByFqdn(fqdn)

View file

@ -53,6 +53,8 @@ const (
// Config is used to configure the creation of the DNSProvider. // Config is used to configure the creation of the DNSProvider.
type Config struct { type Config struct {
ZoneName string
SubscriptionID string SubscriptionID string
ResourceGroup string ResourceGroup string
PrivateZone bool PrivateZone bool
@ -83,6 +85,7 @@ type Config struct {
// NewDefaultConfig returns a default configuration for the DNSProvider. // NewDefaultConfig returns a default configuration for the DNSProvider.
func NewDefaultConfig() *Config { func NewDefaultConfig() *Config {
return &Config{ return &Config{
ZoneName: env.GetOrFile(EnvZoneName),
TTL: env.GetOrDefaultInt(EnvTTL, 60), TTL: env.GetOrDefaultInt(EnvTTL, 60),
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 2*time.Minute), PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 2*time.Minute),
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second), PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second),
@ -257,15 +260,18 @@ func (w *timeoutTokenCredential) GetToken(ctx context.Context, opts policy.Token
return tk, err return tk, err
} }
func getAuthZone(fqdn string) (string, error) { func getZoneName(config *Config, fqdn string) (string, error) {
authZone := env.GetOrFile(EnvZoneName) if config.ZoneName != "" {
if authZone != "" { return config.ZoneName, nil
return authZone, nil
} }
authZone, err := dns01.FindZoneByFqdn(fqdn) authZone, err := dns01.FindZoneByFqdn(fqdn)
if err != nil { if err != nil {
return "", fmt.Errorf("could not find zone: %w", err) return "", fmt.Errorf("could not find zone for %s: %w", fqdn, err)
}
if authZone == "" {
return "", errors.New("empty zone name")
} }
return authZone, nil return authZone, nil

View file

@ -125,7 +125,7 @@ func (d *DNSProviderPrivate) CleanUp(domain, _, keyAuth string) error {
// Checks that azure has a zone for this domain name. // Checks that azure has a zone for this domain name.
func (d *DNSProviderPrivate) getHostedZone(fqdn string) (ServiceDiscoveryZone, error) { func (d *DNSProviderPrivate) getHostedZone(fqdn string) (ServiceDiscoveryZone, error) {
authZone, err := getAuthZone(fqdn) authZone, err := getZoneName(d.config, fqdn)
if err != nil { if err != nil {
return ServiceDiscoveryZone{}, err return ServiceDiscoveryZone{}, err
} }

View file

@ -124,7 +124,7 @@ func (d *DNSProviderPublic) CleanUp(domain, _, keyAuth string) error {
// Checks that azure has a zone for this domain name. // Checks that azure has a zone for this domain name.
func (d *DNSProviderPublic) getHostedZone(fqdn string) (ServiceDiscoveryZone, error) { func (d *DNSProviderPublic) getHostedZone(fqdn string) (ServiceDiscoveryZone, error) {
authZone, err := getAuthZone(fqdn) authZone, err := getZoneName(d.config, fqdn)
if err != nil { if err != nil {
return ServiceDiscoveryZone{}, err return ServiceDiscoveryZone{}, err
} }

View file

@ -91,7 +91,7 @@ func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
func (d *DNSProvider) Present(domain, token, keyAuth string) error { func (d *DNSProvider) Present(domain, token, keyAuth string) error {
info := dns01.GetChallengeInfo(domain, keyAuth) info := dns01.GetChallengeInfo(domain, keyAuth)
authZone, err := getZone(info.EffectiveFQDN) authZone, err := getZoneName(info.EffectiveFQDN)
if err != nil { if err != nil {
return fmt.Errorf("bunny: could not find zone for domain %q: %w", domain, err) return fmt.Errorf("bunny: could not find zone for domain %q: %w", domain, err)
} }
@ -126,7 +126,7 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error {
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
info := dns01.GetChallengeInfo(domain, keyAuth) info := dns01.GetChallengeInfo(domain, keyAuth)
authZone, err := getZone(info.EffectiveFQDN) authZone, err := getZoneName(info.EffectiveFQDN)
if err != nil { if err != nil {
return fmt.Errorf("bunny: could not find zone for domain %q: %w", domain, err) return fmt.Errorf("bunny: could not find zone for domain %q: %w", domain, err)
} }
@ -184,15 +184,13 @@ func (d *DNSProvider) findZone(ctx context.Context, authZone string) (*bunny.DNS
return zone, nil return zone, nil
} }
func getZone(fqdn string) (string, error) { func getZoneName(fqdn string) (string, error) {
authZone, err := dns01.FindZoneByFqdn(fqdn) authZone, err := dns01.FindZoneByFqdn(fqdn)
if err != nil { if err != nil {
return "", err return "", err
} }
zone := dns01.UnFqdn(authZone) return dns01.UnFqdn(authZone), nil
return zone, nil
} }
func pointer[T string | int | int32 | int64](v T) *T { return &v } func pointer[T string | int | int32 | int64](v T) *T { return &v }

View file

@ -102,7 +102,6 @@ func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
func (d *DNSProvider) Present(domain, token, keyAuth string) error { func (d *DNSProvider) Present(domain, token, keyAuth string) error {
ctx := context.Background() ctx := context.Background()
info := dns01.GetChallengeInfo(domain, keyAuth) info := dns01.GetChallengeInfo(domain, keyAuth)
quotedValue := fmt.Sprintf(`%q`, info.Value)
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN) authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
if err != nil { if err != nil {
@ -116,6 +115,8 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error {
domainName := dns01.UnFqdn(authZone) domainName := dns01.UnFqdn(authZone)
quotedValue := fmt.Sprintf(`%q`, info.Value)
rrSet, err := d.client.Records.Get(ctx, domainName, recordName, "TXT") rrSet, err := d.client.Records.Get(ctx, domainName, recordName, "TXT")
if err != nil { if err != nil {
var nf *desec.NotFoundError var nf *desec.NotFoundError

View file

@ -46,6 +46,7 @@ const (
// Config is used to configure the creation of the DNSProvider. // Config is used to configure the creation of the DNSProvider.
type Config struct { type Config struct {
ZoneName string
PropagationTimeout time.Duration PropagationTimeout time.Duration
PollingInterval time.Duration PollingInterval time.Duration
TTL int TTL int
@ -55,6 +56,7 @@ type Config struct {
// NewDefaultConfig returns a default configuration for the DNSProvider. // NewDefaultConfig returns a default configuration for the DNSProvider.
func NewDefaultConfig() *Config { func NewDefaultConfig() *Config {
return &Config{ return &Config{
ZoneName: env.GetOrFile(EnvZoneName),
TTL: env.GetOrDefaultInt(EnvTTL, 10), TTL: env.GetOrDefaultInt(EnvTTL, 10),
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 10*time.Minute), PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 10*time.Minute),
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 10*time.Second), PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 10*time.Second),
@ -129,7 +131,7 @@ func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
func (d *DNSProvider) Present(domain, token, keyAuth string) error { func (d *DNSProvider) Present(domain, token, keyAuth string) error {
info := dns01.GetChallengeInfo(domain, keyAuth) info := dns01.GetChallengeInfo(domain, keyAuth)
zone, err := getAuthZone(info.EffectiveFQDN) zone, err := d.getZoneName(info.EffectiveFQDN)
if err != nil { if err != nil {
return fmt.Errorf("designate: %w", err) return fmt.Errorf("designate: %w", err)
} }
@ -169,7 +171,7 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error {
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
info := dns01.GetChallengeInfo(domain, keyAuth) info := dns01.GetChallengeInfo(domain, keyAuth)
zone, err := getAuthZone(info.EffectiveFQDN) zone, err := d.getZoneName(info.EffectiveFQDN)
if err != nil { if err != nil {
return fmt.Errorf("designate: %w", err) return fmt.Errorf("designate: %w", err)
} }
@ -276,15 +278,18 @@ func (d *DNSProvider) getRecord(zoneID, wanted string) (*recordsets.RecordSet, e
return nil, nil return nil, nil
} }
func getAuthZone(fqdn string) (string, error) { func (d *DNSProvider) getZoneName(fqdn string) (string, error) {
authZone := env.GetOrFile(EnvZoneName) if d.config.ZoneName != "" {
if authZone != "" { return d.config.ZoneName, nil
return authZone, nil
} }
authZone, err := dns01.FindZoneByFqdn(fqdn) authZone, err := dns01.FindZoneByFqdn(fqdn)
if err != nil { if err != nil {
return "", fmt.Errorf("could not find zone: %w", err) return "", fmt.Errorf("could not find zone for %s: %w", fqdn, err)
}
if authZone == "" {
return "", errors.New("empty zone name")
} }
return authZone, nil return authZone, nil

View file

@ -112,7 +112,7 @@ func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
func (d *DNSProvider) Present(domain, token, keyAuth string) error { func (d *DNSProvider) Present(domain, token, keyAuth string) error {
info := dns01.GetChallengeInfo(domain, keyAuth) info := dns01.GetChallengeInfo(domain, keyAuth)
authZone, err := dns01.FindZoneByFqdn(dns01.ToFqdn(info.EffectiveFQDN)) authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
if err != nil { if err != nil {
return fmt.Errorf("digitalocean: could not find zone for domain %q: %w", domain, err) return fmt.Errorf("digitalocean: could not find zone for domain %q: %w", domain, err)
} }

View file

@ -32,6 +32,9 @@ type Config struct {
BaseURL string BaseURL string
Username string Username string
Password string Password string
ZoneName string
TTL int TTL int
PropagationTimeout time.Duration PropagationTimeout time.Duration
PollingInterval time.Duration PollingInterval time.Duration
@ -41,6 +44,7 @@ type Config struct {
// NewDefaultConfig returns a default configuration for the DNSProvider. // NewDefaultConfig returns a default configuration for the DNSProvider.
func NewDefaultConfig() *Config { func NewDefaultConfig() *Config {
return &Config{ return &Config{
ZoneName: env.GetOrFile(EnvZoneName),
TTL: env.GetOrDefaultInt(EnvTTL, 30), TTL: env.GetOrDefaultInt(EnvTTL, 30),
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 60*time.Second), PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 60*time.Second),
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 5*time.Second), PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 5*time.Second),
@ -95,7 +99,7 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
func (d *DNSProvider) Present(domain, token, keyAuth string) error { func (d *DNSProvider) Present(domain, token, keyAuth string) error {
info := dns01.GetChallengeInfo(domain, keyAuth) info := dns01.GetChallengeInfo(domain, keyAuth)
authZone, err := getAuthZone(info.EffectiveFQDN) authZone, err := d.getZoneName(info.EffectiveFQDN)
if err != nil { if err != nil {
return fmt.Errorf("directadmin: [domain: %q] %w", domain, err) return fmt.Errorf("directadmin: [domain: %q] %w", domain, err)
} }
@ -124,7 +128,7 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error {
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
info := dns01.GetChallengeInfo(domain, keyAuth) info := dns01.GetChallengeInfo(domain, keyAuth)
authZone, err := getAuthZone(info.EffectiveFQDN) authZone, err := d.getZoneName(info.EffectiveFQDN)
if err != nil { if err != nil {
return fmt.Errorf("directadmin: [domain: %q] %w", domain, err) return fmt.Errorf("directadmin: [domain: %q] %w", domain, err)
} }
@ -148,10 +152,9 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
return nil return nil
} }
func getAuthZone(fqdn string) (string, error) { func (d *DNSProvider) getZoneName(fqdn string) (string, error) {
authZone := env.GetOrFile(EnvZoneName) if d.config.ZoneName != "" {
if authZone != "" { return d.config.ZoneName, nil
return authZone, nil
} }
authZone, err := dns01.FindZoneByFqdn(fqdn) authZone, err := dns01.FindZoneByFqdn(fqdn)
@ -159,5 +162,9 @@ func getAuthZone(fqdn string) (string, error) {
return "", fmt.Errorf("could not find zone for %s: %w", fqdn, err) return "", fmt.Errorf("could not find zone for %s: %w", fqdn, err)
} }
if authZone == "" {
return "", errors.New("empty zone name")
}
return authZone, nil return authZone, nil
} }

View file

@ -40,6 +40,7 @@ type Config struct {
// NewDefaultConfig returns a default configuration for the DNSProvider. // NewDefaultConfig returns a default configuration for the DNSProvider.
func NewDefaultConfig() *Config { func NewDefaultConfig() *Config {
return &Config{ return &Config{
ZoneName: env.GetOrFile(EnvZoneName),
TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL), TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 2*time.Minute), PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 2*time.Minute),
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second), PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second),
@ -69,7 +70,6 @@ func NewDNSProvider() (*DNSProvider, error) {
config := NewDefaultConfig() config := NewDefaultConfig()
config.APIKey = values[EnvAPIKey] config.APIKey = values[EnvAPIKey]
config.ZoneName = env.GetOrFile(EnvZoneName)
return NewDNSProviderConfig(config) return NewDNSProviderConfig(config)
} }
@ -208,7 +208,7 @@ func (d *DNSProvider) getZoneName(fqdn string) (string, error) {
zoneName, err := dns01.FindZoneByFqdn(fqdn) zoneName, err := dns01.FindZoneByFqdn(fqdn)
if err != nil { if err != nil {
return "", fmt.Errorf("could not find zone: %w", err) return "", fmt.Errorf("could not find zone for %s: %w", fqdn, err)
} }
if zoneName == "" { if zoneName == "" {

View file

@ -41,6 +41,7 @@ type Config struct {
// NewDefaultConfig returns a default configuration for the DNSProvider. // NewDefaultConfig returns a default configuration for the DNSProvider.
func NewDefaultConfig() *Config { func NewDefaultConfig() *Config {
return &Config{ return &Config{
ZoneName: env.GetOrFile(EnvZoneName),
TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL), TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 2*time.Minute), PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 2*time.Minute),
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second), PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second),
@ -70,7 +71,6 @@ func NewDNSProvider() (*DNSProvider, error) {
config := NewDefaultConfig() config := NewDefaultConfig()
config.APIKey = values[EnvAPIKey] config.APIKey = values[EnvAPIKey]
config.ZoneName = env.GetOrFile(EnvZoneName)
return NewDNSProviderConfig(config) return NewDNSProviderConfig(config)
} }
@ -212,7 +212,7 @@ func (d *DNSProvider) getZoneName(fqdn string) (string, error) {
zoneName, err := dns01.FindZoneByFqdn(fqdn) zoneName, err := dns01.FindZoneByFqdn(fqdn)
if err != nil { if err != nil {
return "", fmt.Errorf("could not find zone: %w", err) return "", fmt.Errorf("could not find zone for %s: %w", fqdn, err)
} }
if zoneName == "" { if zoneName == "" {

View file

@ -150,7 +150,6 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
func (d *DNSProvider) Present(domain, token, keyAuth string) error { func (d *DNSProvider) Present(domain, token, keyAuth string) error {
info := dns01.GetChallengeInfo(domain, keyAuth) info := dns01.GetChallengeInfo(domain, keyAuth)
// Parse domain name
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN) authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
if err != nil { if err != nil {
return fmt.Errorf("ovh: could not find zone for domain %q: %w", domain, err) return fmt.Errorf("ovh: could not find zone for domain %q: %w", domain, err)

View file

@ -217,8 +217,7 @@ func getAuthZone(domain string) (string, error) {
return "", fmt.Errorf("could not find zone: %w", err) return "", fmt.Errorf("could not find zone: %w", err)
} }
zoneName := dns01.UnFqdn(authZone) return dns01.UnFqdn(authZone), nil
return zoneName, nil
} }
func findRecords(records []internal.Record, name string) *internal.Record { func findRecords(records []internal.Record, name string) *internal.Record {

View file

@ -119,11 +119,6 @@ func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
func (d *DNSProvider) Present(domain, token, keyAuth string) error { func (d *DNSProvider) Present(domain, token, keyAuth string) error {
info := dns01.GetChallengeInfo(domain, keyAuth) info := dns01.GetChallengeInfo(domain, keyAuth)
record := internal.TXTRecord{
Name: dns01.UnFqdn(info.EffectiveFQDN),
Destination: info.Value,
}
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN) authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
if err != nil { if err != nil {
return fmt.Errorf("zoneee: could not find zone for domain %q: %w", domain, err) return fmt.Errorf("zoneee: could not find zone for domain %q: %w", domain, err)
@ -131,6 +126,11 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error {
authZone = dns01.UnFqdn(authZone) authZone = dns01.UnFqdn(authZone)
record := internal.TXTRecord{
Name: dns01.UnFqdn(info.EffectiveFQDN),
Destination: info.Value,
}
_, err = d.client.AddTxtRecord(context.Background(), authZone, record) _, err = d.client.AddTxtRecord(context.Background(), authZone, record)
if err != nil { if err != nil {
return fmt.Errorf("zoneee: %w", err) return fmt.Errorf("zoneee: %w", err)