Skip to content

Commit 5bea707

Browse files
authored
chore: homogenous zone name env var management (#2232)
1 parent c759f56 commit 5bea707

File tree

16 files changed

+65
-49
lines changed

16 files changed

+65
-49
lines changed

providers/dns/azure/azure.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ const (
4141

4242
// Config is used to configure the creation of the DNSProvider.
4343
type Config struct {
44+
ZoneName string
45+
4446
// optional if using instance metadata service
4547
ClientID string
4648
ClientSecret string
@@ -63,6 +65,7 @@ type Config struct {
6365
// NewDefaultConfig returns a default configuration for the DNSProvider.
6466
func NewDefaultConfig() *Config {
6567
return &Config{
68+
ZoneName: env.GetOrFile(EnvZoneName),
6669
TTL: env.GetOrDefaultInt(EnvTTL, 60),
6770
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 2*time.Minute),
6871
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second),

providers/dns/azure/private.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/Azure/go-autorest/autorest"
1212
"github.com/Azure/go-autorest/autorest/to"
1313
"github.com/go-acme/lego/v4/challenge/dns01"
14-
"github.com/go-acme/lego/v4/platform/config/env"
1514
)
1615

1716
// dnsProviderPrivate implements the challenge.Provider interface for Azure Private Zone DNS.
@@ -112,8 +111,8 @@ func (d *dnsProviderPrivate) CleanUp(domain, token, keyAuth string) error {
112111

113112
// Checks that azure has a zone for this domain name.
114113
func (d *dnsProviderPrivate) getHostedZoneID(ctx context.Context, fqdn string) (string, error) {
115-
if zone := env.GetOrFile(EnvZoneName); zone != "" {
116-
return zone, nil
114+
if d.config.ZoneName != "" {
115+
return d.config.ZoneName, nil
117116
}
118117

119118
authZone, err := dns01.FindZoneByFqdn(fqdn)

providers/dns/azure/public.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/Azure/go-autorest/autorest"
1212
"github.com/Azure/go-autorest/autorest/to"
1313
"github.com/go-acme/lego/v4/challenge/dns01"
14-
"github.com/go-acme/lego/v4/platform/config/env"
1514
)
1615

1716
// dnsProviderPublic implements the challenge.Provider interface for Azure Public Zone DNS.
@@ -112,8 +111,8 @@ func (d *dnsProviderPublic) CleanUp(domain, token, keyAuth string) error {
112111

113112
// Checks that azure has a zone for this domain name.
114113
func (d *dnsProviderPublic) getHostedZoneID(ctx context.Context, fqdn string) (string, error) {
115-
if zone := env.GetOrFile(EnvZoneName); zone != "" {
116-
return zone, nil
114+
if d.config.ZoneName != "" {
115+
return d.config.ZoneName, nil
117116
}
118117

119118
authZone, err := dns01.FindZoneByFqdn(fqdn)

providers/dns/azuredns/azuredns.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ const (
5353

5454
// Config is used to configure the creation of the DNSProvider.
5555
type Config struct {
56+
ZoneName string
57+
5658
SubscriptionID string
5759
ResourceGroup string
5860
PrivateZone bool
@@ -83,6 +85,7 @@ type Config struct {
8385
// NewDefaultConfig returns a default configuration for the DNSProvider.
8486
func NewDefaultConfig() *Config {
8587
return &Config{
88+
ZoneName: env.GetOrFile(EnvZoneName),
8689
TTL: env.GetOrDefaultInt(EnvTTL, 60),
8790
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 2*time.Minute),
8891
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second),
@@ -257,15 +260,18 @@ func (w *timeoutTokenCredential) GetToken(ctx context.Context, opts policy.Token
257260
return tk, err
258261
}
259262

260-
func getAuthZone(fqdn string) (string, error) {
261-
authZone := env.GetOrFile(EnvZoneName)
262-
if authZone != "" {
263-
return authZone, nil
263+
func getZoneName(config *Config, fqdn string) (string, error) {
264+
if config.ZoneName != "" {
265+
return config.ZoneName, nil
264266
}
265267

266268
authZone, err := dns01.FindZoneByFqdn(fqdn)
267269
if err != nil {
268-
return "", fmt.Errorf("could not find zone: %w", err)
270+
return "", fmt.Errorf("could not find zone for %s: %w", fqdn, err)
271+
}
272+
273+
if authZone == "" {
274+
return "", errors.New("empty zone name")
269275
}
270276

271277
return authZone, nil

providers/dns/azuredns/private.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (d *DNSProviderPrivate) CleanUp(domain, _, keyAuth string) error {
125125

126126
// Checks that azure has a zone for this domain name.
127127
func (d *DNSProviderPrivate) getHostedZone(fqdn string) (ServiceDiscoveryZone, error) {
128-
authZone, err := getAuthZone(fqdn)
128+
authZone, err := getZoneName(d.config, fqdn)
129129
if err != nil {
130130
return ServiceDiscoveryZone{}, err
131131
}

providers/dns/azuredns/public.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func (d *DNSProviderPublic) CleanUp(domain, _, keyAuth string) error {
124124

125125
// Checks that azure has a zone for this domain name.
126126
func (d *DNSProviderPublic) getHostedZone(fqdn string) (ServiceDiscoveryZone, error) {
127-
authZone, err := getAuthZone(fqdn)
127+
authZone, err := getZoneName(d.config, fqdn)
128128
if err != nil {
129129
return ServiceDiscoveryZone{}, err
130130
}

providers/dns/bunny/bunny.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
9191
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
9292
info := dns01.GetChallengeInfo(domain, keyAuth)
9393

94-
authZone, err := getZone(info.EffectiveFQDN)
94+
authZone, err := getZoneName(info.EffectiveFQDN)
9595
if err != nil {
9696
return fmt.Errorf("bunny: could not find zone for domain %q: %w", domain, err)
9797
}
@@ -126,7 +126,7 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error {
126126
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
127127
info := dns01.GetChallengeInfo(domain, keyAuth)
128128

129-
authZone, err := getZone(info.EffectiveFQDN)
129+
authZone, err := getZoneName(info.EffectiveFQDN)
130130
if err != nil {
131131
return fmt.Errorf("bunny: could not find zone for domain %q: %w", domain, err)
132132
}
@@ -184,15 +184,13 @@ func (d *DNSProvider) findZone(ctx context.Context, authZone string) (*bunny.DNS
184184
return zone, nil
185185
}
186186

187-
func getZone(fqdn string) (string, error) {
187+
func getZoneName(fqdn string) (string, error) {
188188
authZone, err := dns01.FindZoneByFqdn(fqdn)
189189
if err != nil {
190190
return "", err
191191
}
192192

193-
zone := dns01.UnFqdn(authZone)
194-
195-
return zone, nil
193+
return dns01.UnFqdn(authZone), nil
196194
}
197195

198196
func pointer[T string | int | int32 | int64](v T) *T { return &v }

providers/dns/desec/desec.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
102102
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
103103
ctx := context.Background()
104104
info := dns01.GetChallengeInfo(domain, keyAuth)
105-
quotedValue := fmt.Sprintf(`%q`, info.Value)
106105

107106
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
108107
if err != nil {
@@ -116,6 +115,8 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error {
116115

117116
domainName := dns01.UnFqdn(authZone)
118117

118+
quotedValue := fmt.Sprintf(`%q`, info.Value)
119+
119120
rrSet, err := d.client.Records.Get(ctx, domainName, recordName, "TXT")
120121
if err != nil {
121122
var nf *desec.NotFoundError

providers/dns/designate/designate.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const (
4646

4747
// Config is used to configure the creation of the DNSProvider.
4848
type Config struct {
49+
ZoneName string
4950
PropagationTimeout time.Duration
5051
PollingInterval time.Duration
5152
TTL int
@@ -55,6 +56,7 @@ type Config struct {
5556
// NewDefaultConfig returns a default configuration for the DNSProvider.
5657
func NewDefaultConfig() *Config {
5758
return &Config{
59+
ZoneName: env.GetOrFile(EnvZoneName),
5860
TTL: env.GetOrDefaultInt(EnvTTL, 10),
5961
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 10*time.Minute),
6062
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 10*time.Second),
@@ -129,7 +131,7 @@ func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
129131
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
130132
info := dns01.GetChallengeInfo(domain, keyAuth)
131133

132-
zone, err := getAuthZone(info.EffectiveFQDN)
134+
zone, err := d.getZoneName(info.EffectiveFQDN)
133135
if err != nil {
134136
return fmt.Errorf("designate: %w", err)
135137
}
@@ -169,7 +171,7 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error {
169171
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
170172
info := dns01.GetChallengeInfo(domain, keyAuth)
171173

172-
zone, err := getAuthZone(info.EffectiveFQDN)
174+
zone, err := d.getZoneName(info.EffectiveFQDN)
173175
if err != nil {
174176
return fmt.Errorf("designate: %w", err)
175177
}
@@ -276,15 +278,18 @@ func (d *DNSProvider) getRecord(zoneID, wanted string) (*recordsets.RecordSet, e
276278
return nil, nil
277279
}
278280

279-
func getAuthZone(fqdn string) (string, error) {
280-
authZone := env.GetOrFile(EnvZoneName)
281-
if authZone != "" {
282-
return authZone, nil
281+
func (d *DNSProvider) getZoneName(fqdn string) (string, error) {
282+
if d.config.ZoneName != "" {
283+
return d.config.ZoneName, nil
283284
}
284285

285286
authZone, err := dns01.FindZoneByFqdn(fqdn)
286287
if err != nil {
287-
return "", fmt.Errorf("could not find zone: %w", err)
288+
return "", fmt.Errorf("could not find zone for %s: %w", fqdn, err)
289+
}
290+
291+
if authZone == "" {
292+
return "", errors.New("empty zone name")
288293
}
289294

290295
return authZone, nil

providers/dns/digitalocean/digitalocean.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
112112
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
113113
info := dns01.GetChallengeInfo(domain, keyAuth)
114114

115-
authZone, err := dns01.FindZoneByFqdn(dns01.ToFqdn(info.EffectiveFQDN))
115+
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
116116
if err != nil {
117117
return fmt.Errorf("digitalocean: could not find zone for domain %q: %w", domain, err)
118118
}

0 commit comments

Comments
 (0)