1
0
mirror of https://github.com/go-acme/lego.git synced 2024-12-22 16:53:17 +02:00

chore: shared deref and pointer functions (#2376)

This commit is contained in:
Ludovic Fernandez 2024-12-05 19:49:13 +01:00 committed by GitHub
parent 1a62bbab40
commit 0bbf5ab59c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 107 additions and 148 deletions

View File

@ -278,12 +278,3 @@ func getZoneName(config *Config, fqdn string) (string, error) {
return authZone, nil return authZone, nil
} }
func deref[T any](v *T) T {
if v == nil {
var zero T
return zero
}
return *v
}

View File

@ -14,6 +14,7 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/privatedns/armprivatedns" "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/privatedns/armprivatedns"
"github.com/go-acme/lego/v4/challenge" "github.com/go-acme/lego/v4/challenge"
"github.com/go-acme/lego/v4/challenge/dns01" "github.com/go-acme/lego/v4/challenge/dns01"
"github.com/go-acme/lego/v4/providers/dns/internal/ptr"
) )
var _ challenge.ProviderTimeout = (*DNSProviderPrivate)(nil) var _ challenge.ProviderTimeout = (*DNSProviderPrivate)(nil)
@ -184,7 +185,7 @@ func privateUniqueRecords(recordSet armprivatedns.RecordSet, value string) map[s
for _, txtRecord := range recordSet.Properties.TxtRecords { for _, txtRecord := range recordSet.Properties.TxtRecords {
// Assume Value doesn't contain multiple strings // Assume Value doesn't contain multiple strings
if len(txtRecord.Value) > 0 { if len(txtRecord.Value) > 0 {
uniqRecords[deref(txtRecord.Value[0])] = struct{}{} uniqRecords[ptr.Deref(txtRecord.Value[0])] = struct{}{}
} }
} }
} }

View File

@ -14,6 +14,7 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/dns/armdns" "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/dns/armdns"
"github.com/go-acme/lego/v4/challenge" "github.com/go-acme/lego/v4/challenge"
"github.com/go-acme/lego/v4/challenge/dns01" "github.com/go-acme/lego/v4/challenge/dns01"
"github.com/go-acme/lego/v4/providers/dns/internal/ptr"
) )
var _ challenge.ProviderTimeout = (*DNSProviderPublic)(nil) var _ challenge.ProviderTimeout = (*DNSProviderPublic)(nil)
@ -182,7 +183,7 @@ func publicUniqueRecords(recordSet armdns.RecordSet, value string) map[string]st
for _, txtRecord := range recordSet.Properties.TxtRecords { for _, txtRecord := range recordSet.Properties.TxtRecords {
// Assume Value doesn't contain multiple strings // Assume Value doesn't contain multiple strings
if len(txtRecord.Value) > 0 { if len(txtRecord.Value) > 0 {
uniqRecords[deref(txtRecord.Value[0])] = struct{}{} uniqRecords[ptr.Deref(txtRecord.Value[0])] = struct{}{}
} }
} }
} }

View File

@ -9,6 +9,7 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm" "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to" "github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resourcegraph/armresourcegraph" "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resourcegraph/armresourcegraph"
"github.com/go-acme/lego/v4/providers/dns/internal/ptr"
) )
type ServiceDiscoveryZone struct { type ServiceDiscoveryZone struct {
@ -88,7 +89,7 @@ func discoverDNSZones(ctx context.Context, config *Config, credentials azcore.To
*requestOptions.Skip += ResourceGraphQueryOptionsTop *requestOptions.Skip += ResourceGraphQueryOptionsTop
if result.TotalRecords != nil { if result.TotalRecords != nil {
if int64(deref(requestOptions.Skip)) >= deref(result.TotalRecords) { if int64(ptr.Deref(requestOptions.Skip)) >= ptr.Deref(result.TotalRecords) {
break break
} }
} }

View File

@ -11,6 +11,7 @@ import (
"github.com/go-acme/lego/v4/challenge" "github.com/go-acme/lego/v4/challenge"
"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" "github.com/go-acme/lego/v4/platform/config/env"
"github.com/go-acme/lego/v4/providers/dns/internal/ptr"
"github.com/miekg/dns" "github.com/miekg/dns"
"github.com/nrdcg/bunny-go" "github.com/nrdcg/bunny-go"
"golang.org/x/net/publicsuffix" "golang.org/x/net/publicsuffix"
@ -104,20 +105,20 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error {
return fmt.Errorf("bunny: %w", err) return fmt.Errorf("bunny: %w", err)
} }
subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, deref(zone.Domain)) subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, ptr.Deref(zone.Domain))
if err != nil { if err != nil {
return fmt.Errorf("bunny: %w", err) return fmt.Errorf("bunny: %w", err)
} }
record := &bunny.AddOrUpdateDNSRecordOptions{ record := &bunny.AddOrUpdateDNSRecordOptions{
Type: pointer(bunny.DNSRecordTypeTXT), Type: ptr.Pointer(bunny.DNSRecordTypeTXT),
Name: pointer(subDomain), Name: ptr.Pointer(subDomain),
Value: pointer(info.Value), Value: ptr.Pointer(info.Value),
TTL: pointer(int32(d.config.TTL)), TTL: ptr.Pointer(int32(d.config.TTL)),
} }
if _, err := d.client.DNSZone.AddDNSRecord(ctx, deref(zone.ID), record); err != nil { if _, err := d.client.DNSZone.AddDNSRecord(ctx, ptr.Deref(zone.ID), record); err != nil {
return fmt.Errorf("bunny: failed to add TXT record: fqdn=%s, zoneID=%d: %w", info.EffectiveFQDN, deref(zone.ID), err) return fmt.Errorf("bunny: failed to add TXT record: fqdn=%s, zoneID=%d: %w", info.EffectiveFQDN, ptr.Deref(zone.ID), err)
} }
return nil return nil
@ -134,14 +135,14 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
return fmt.Errorf("bunny: %w", err) return fmt.Errorf("bunny: %w", err)
} }
subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, deref(zone.Domain)) subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, ptr.Deref(zone.Domain))
if err != nil { if err != nil {
return fmt.Errorf("bunny: %w", err) return fmt.Errorf("bunny: %w", err)
} }
var record *bunny.DNSRecord var record *bunny.DNSRecord
for _, r := range zone.Records { for _, r := range zone.Records {
if deref(r.Name) == subDomain && deref(r.Type) == bunny.DNSRecordTypeTXT { if ptr.Deref(r.Name) == subDomain && ptr.Deref(r.Type) == bunny.DNSRecordTypeTXT {
r := r r := r
record = &r record = &r
break break
@ -149,11 +150,11 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
} }
if record == nil { if record == nil {
return fmt.Errorf("bunny: could not find TXT record zone=%d, subdomain=%s", deref(zone.ID), subDomain) return fmt.Errorf("bunny: could not find TXT record zone=%d, subdomain=%s", ptr.Deref(zone.ID), subDomain)
} }
if err := d.client.DNSZone.DeleteDNSRecord(ctx, deref(zone.ID), deref(record.ID)); err != nil { if err := d.client.DNSZone.DeleteDNSRecord(ctx, ptr.Deref(zone.ID), ptr.Deref(record.ID)); err != nil {
return fmt.Errorf("bunny: failed to delete TXT record: id=%d, name=%s: %w", deref(record.ID), deref(record.Name), err) return fmt.Errorf("bunny: failed to delete TXT record: id=%d, name=%s: %w", ptr.Deref(record.ID), ptr.Deref(record.Name), err)
} }
return nil return nil
@ -184,7 +185,7 @@ func findZone(zones *bunny.DNSZones, domain string) *bunny.DNSZone {
continue continue
} }
curr := deref(item.Domain) curr := ptr.Deref(item.Domain)
if slices.Contains(domains, curr) && domainLength < len(curr) { if slices.Contains(domains, curr) && domainLength < len(curr) {
domainLength = len(curr) domainLength = len(curr)
@ -213,14 +214,3 @@ func possibleDomains(domain string) []string {
return domains return domains
} }
func pointer[T string | int | int32 | int64](v T) *T { return &v }
func deref[T string | int | int32 | int64](v *T) T {
if v == nil {
var zero T
return zero
}
return *v
}

View File

@ -4,6 +4,7 @@ import (
"testing" "testing"
"github.com/go-acme/lego/v4/platform/tester" "github.com/go-acme/lego/v4/platform/tester"
"github.com/go-acme/lego/v4/providers/dns/internal/ptr"
"github.com/nrdcg/bunny-go" "github.com/nrdcg/bunny-go"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -137,54 +138,54 @@ func Test_findZone(t *testing.T) {
desc: "found subdomain", desc: "found subdomain",
domain: "_acme-challenge.foo.bar.example.com", domain: "_acme-challenge.foo.bar.example.com",
items: []*bunny.DNSZone{ items: []*bunny.DNSZone{
{ID: pointer[int64](1), Domain: pointer("example.com")}, {ID: ptr.Pointer[int64](1), Domain: ptr.Pointer("example.com")},
{ID: pointer[int64](2), Domain: pointer("example.org")}, {ID: ptr.Pointer[int64](2), Domain: ptr.Pointer("example.org")},
{ID: pointer[int64](4), Domain: pointer("bar.example.org")}, {ID: ptr.Pointer[int64](4), Domain: ptr.Pointer("bar.example.org")},
{ID: pointer[int64](5), Domain: pointer("bar.example.com")}, {ID: ptr.Pointer[int64](5), Domain: ptr.Pointer("bar.example.com")},
{ID: pointer[int64](6), Domain: pointer("foo.example.com")}, {ID: ptr.Pointer[int64](6), Domain: ptr.Pointer("foo.example.com")},
}, },
expected: &bunny.DNSZone{ expected: &bunny.DNSZone{
ID: pointer[int64](5), ID: ptr.Pointer[int64](5),
Domain: pointer("bar.example.com"), Domain: ptr.Pointer("bar.example.com"),
}, },
}, },
{ {
desc: "found the longest subdomain", desc: "found the longest subdomain",
domain: "_acme-challenge.foo.bar.example.com", domain: "_acme-challenge.foo.bar.example.com",
items: []*bunny.DNSZone{ items: []*bunny.DNSZone{
{ID: pointer[int64](7), Domain: pointer("foo.bar.example.com")}, {ID: ptr.Pointer[int64](7), Domain: ptr.Pointer("foo.bar.example.com")},
{ID: pointer[int64](1), Domain: pointer("example.com")}, {ID: ptr.Pointer[int64](1), Domain: ptr.Pointer("example.com")},
{ID: pointer[int64](2), Domain: pointer("example.org")}, {ID: ptr.Pointer[int64](2), Domain: ptr.Pointer("example.org")},
{ID: pointer[int64](4), Domain: pointer("bar.example.org")}, {ID: ptr.Pointer[int64](4), Domain: ptr.Pointer("bar.example.org")},
{ID: pointer[int64](5), Domain: pointer("bar.example.com")}, {ID: ptr.Pointer[int64](5), Domain: ptr.Pointer("bar.example.com")},
{ID: pointer[int64](6), Domain: pointer("foo.example.com")}, {ID: ptr.Pointer[int64](6), Domain: ptr.Pointer("foo.example.com")},
}, },
expected: &bunny.DNSZone{ expected: &bunny.DNSZone{
ID: pointer[int64](7), ID: ptr.Pointer[int64](7),
Domain: pointer("foo.bar.example.com"), Domain: ptr.Pointer("foo.bar.example.com"),
}, },
}, },
{ {
desc: "found apex", desc: "found apex",
domain: "_acme-challenge.foo.bar.example.com", domain: "_acme-challenge.foo.bar.example.com",
items: []*bunny.DNSZone{ items: []*bunny.DNSZone{
{ID: pointer[int64](1), Domain: pointer("example.com")}, {ID: ptr.Pointer[int64](1), Domain: ptr.Pointer("example.com")},
{ID: pointer[int64](2), Domain: pointer("example.org")}, {ID: ptr.Pointer[int64](2), Domain: ptr.Pointer("example.org")},
{ID: pointer[int64](4), Domain: pointer("bar.example.org")}, {ID: ptr.Pointer[int64](4), Domain: ptr.Pointer("bar.example.org")},
{ID: pointer[int64](6), Domain: pointer("foo.example.com")}, {ID: ptr.Pointer[int64](6), Domain: ptr.Pointer("foo.example.com")},
}, },
expected: &bunny.DNSZone{ expected: &bunny.DNSZone{
ID: pointer[int64](1), ID: ptr.Pointer[int64](1),
Domain: pointer("example.com"), Domain: ptr.Pointer("example.com"),
}, },
}, },
{ {
desc: "not found", desc: "not found",
domain: "_acme-challenge.foo.bar.example.com", domain: "_acme-challenge.foo.bar.example.com",
items: []*bunny.DNSZone{ items: []*bunny.DNSZone{
{ID: pointer[int64](2), Domain: pointer("example.org")}, {ID: ptr.Pointer[int64](2), Domain: ptr.Pointer("example.org")},
{ID: pointer[int64](4), Domain: pointer("bar.example.org")}, {ID: ptr.Pointer[int64](4), Domain: ptr.Pointer("bar.example.org")},
{ID: pointer[int64](6), Domain: pointer("foo.example.com")}, {ID: ptr.Pointer[int64](6), Domain: ptr.Pointer("foo.example.com")},
}, },
}, },
} }

View File

@ -13,6 +13,7 @@ import (
"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" "github.com/go-acme/lego/v4/platform/config/env"
"github.com/go-acme/lego/v4/platform/wait" "github.com/go-acme/lego/v4/platform/wait"
"github.com/go-acme/lego/v4/providers/dns/internal/ptr"
hwauthbasic "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" hwauthbasic "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
hwconfig "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/config" hwconfig "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/config"
hwdns "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dns/v2" hwdns "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dns/v2"
@ -155,7 +156,7 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error {
return false, fmt.Errorf("show record set: %w", errShow) return false, fmt.Errorf("show record set: %w", errShow)
} }
return !strings.HasSuffix(deref(rs.Status), "PENDING_"), nil return !strings.HasSuffix(ptr.Deref(rs.Status), "PENDING_"), nil
}) })
if err != nil { if err != nil {
return fmt.Errorf("huaweicloud: %w", err) return fmt.Errorf("huaweicloud: %w", err)
@ -208,7 +209,7 @@ func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
func (d *DNSProvider) getOrCreateRecordSetID(domain, zoneID string, info dns01.ChallengeInfo) (string, error) { func (d *DNSProvider) getOrCreateRecordSetID(domain, zoneID string, info dns01.ChallengeInfo) (string, error) {
records, err := d.client.ListRecordSetsByZone(&hwmodel.ListRecordSetsByZoneRequest{ records, err := d.client.ListRecordSetsByZone(&hwmodel.ListRecordSetsByZoneRequest{
ZoneId: zoneID, ZoneId: zoneID,
Name: pointer(info.EffectiveFQDN), Name: ptr.Pointer(info.EffectiveFQDN),
}) })
if err != nil { if err != nil {
return "", fmt.Errorf("record list: unable to get record %s for zone %s: %w", info.EffectiveFQDN, domain, err) return "", fmt.Errorf("record list: unable to get record %s for zone %s: %w", info.EffectiveFQDN, domain, err)
@ -216,8 +217,8 @@ func (d *DNSProvider) getOrCreateRecordSetID(domain, zoneID string, info dns01.C
var existingRecordSet *hwmodel.ListRecordSets var existingRecordSet *hwmodel.ListRecordSets
for _, record := range deref(records.Recordsets) { for _, record := range ptr.Deref(records.Recordsets) {
if deref(record.Type) == "TXT" && deref(record.Name) == info.EffectiveFQDN { if ptr.Deref(record.Type) == "TXT" && ptr.Deref(record.Name) == info.EffectiveFQDN {
existingRecordSet = &record existingRecordSet = &record
} }
} }
@ -229,9 +230,9 @@ func (d *DNSProvider) getOrCreateRecordSetID(domain, zoneID string, info dns01.C
ZoneId: zoneID, ZoneId: zoneID,
Body: &hwmodel.CreateRecordSetRequestBody{ Body: &hwmodel.CreateRecordSetRequestBody{
Name: info.EffectiveFQDN, Name: info.EffectiveFQDN,
Description: pointer("Added TXT record for ACME dns-01 challenge using lego client"), Description: ptr.Pointer("Added TXT record for ACME dns-01 challenge using lego client"),
Type: "TXT", Type: "TXT",
Ttl: pointer(d.config.TTL), Ttl: ptr.Pointer(d.config.TTL),
Records: []string{value}, Records: []string{value},
}, },
} }
@ -241,18 +242,18 @@ func (d *DNSProvider) getOrCreateRecordSetID(domain, zoneID string, info dns01.C
return "", fmt.Errorf("create record set: %w", errCreate) return "", fmt.Errorf("create record set: %w", errCreate)
} }
return deref(resp.Id), nil return ptr.Deref(resp.Id), nil
} }
updateRequest := &hwmodel.UpdateRecordSetRequest{ updateRequest := &hwmodel.UpdateRecordSetRequest{
ZoneId: zoneID, ZoneId: zoneID,
RecordsetId: deref(existingRecordSet.Id), RecordsetId: ptr.Deref(existingRecordSet.Id),
Body: &hwmodel.UpdateRecordSetReq{ Body: &hwmodel.UpdateRecordSetReq{
Name: existingRecordSet.Name, Name: existingRecordSet.Name,
Description: existingRecordSet.Description, Description: existingRecordSet.Description,
Type: existingRecordSet.Type, Type: existingRecordSet.Type,
Ttl: existingRecordSet.Ttl, Ttl: existingRecordSet.Ttl,
Records: pointer(append(deref(existingRecordSet.Records), value)), Records: ptr.Pointer(append(ptr.Deref(existingRecordSet.Records), value)),
}, },
} }
@ -261,7 +262,7 @@ func (d *DNSProvider) getOrCreateRecordSetID(domain, zoneID string, info dns01.C
return "", fmt.Errorf("update record set: %w", err) return "", fmt.Errorf("update record set: %w", err)
} }
return deref(resp.Id), nil return ptr.Deref(resp.Id), nil
} }
func (d *DNSProvider) getZoneID(authZone string) (string, error) { func (d *DNSProvider) getZoneID(authZone string) (string, error) {
@ -270,22 +271,11 @@ func (d *DNSProvider) getZoneID(authZone string) (string, error) {
return "", fmt.Errorf("unable to get zone: %w", err) return "", fmt.Errorf("unable to get zone: %w", err)
} }
for _, zone := range deref(zones.Zones) { for _, zone := range ptr.Deref(zones.Zones) {
if deref(zone.Name) == authZone { if ptr.Deref(zone.Name) == authZone {
return deref(zone.Id), nil return ptr.Deref(zone.Id), nil
} }
} }
return "", fmt.Errorf("zone %q not found", authZone) return "", fmt.Errorf("zone %q not found", authZone)
} }
func pointer[T any](v T) *T { return &v }
func deref[T any](v *T) T {
if v == nil {
var zero T
return zero
}
return *v
}

View File

@ -0,0 +1,12 @@
package ptr
func Deref[T any](v *T) T {
if v == nil {
var zero T
return zero
}
return *v
}
func Pointer[T any](v T) *T { return &v }

View File

@ -97,14 +97,14 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
// Present creates a TXT record using the specified parameters. // Present creates a TXT record using the specified parameters.
func (d *DNSProvider) Present(domain, token, keyAuth string) error { func (d *DNSProvider) Present(domain, token, keyAuth string) error {
challengeInfo := dns01.GetChallengeInfo(domain, keyAuth) info := dns01.GetChallengeInfo(domain, keyAuth)
authZone, err := dns01.FindZoneByFqdn(challengeInfo.EffectiveFQDN) authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
if err != nil { if err != nil {
return fmt.Errorf("inwx: could not find zone for domain %q (%s): %w", domain, challengeInfo.EffectiveFQDN, err) return fmt.Errorf("inwx: could not find zone for domain %q (%s): %w", domain, info.EffectiveFQDN, err)
} }
info, err := d.client.Account.Login() login, err := d.client.Account.Login()
if err != nil { if err != nil {
return fmt.Errorf("inwx: %w", err) return fmt.Errorf("inwx: %w", err)
} }
@ -116,28 +116,25 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error {
} }
}() }()
err = d.twoFactorAuth(info) err = d.twoFactorAuth(login)
if err != nil { if err != nil {
return fmt.Errorf("inwx: %w", err) return fmt.Errorf("inwx: %w", err)
} }
request := &goinwx.NameserverRecordRequest{ request := &goinwx.NameserverRecordRequest{
Domain: dns01.UnFqdn(authZone), Domain: dns01.UnFqdn(authZone),
Name: dns01.UnFqdn(challengeInfo.EffectiveFQDN), Name: dns01.UnFqdn(info.EffectiveFQDN),
Type: "TXT", Type: "TXT",
Content: challengeInfo.Value, Content: info.Value,
TTL: d.config.TTL, TTL: d.config.TTL,
} }
_, err = d.client.Nameservers.CreateRecord(request) _, err = d.client.Nameservers.CreateRecord(request)
if err != nil { if err != nil {
var er *goinwx.ErrorResponse var er *goinwx.ErrorResponse
if errors.As(err, &er) { if errors.As(err, &er) && er.Message == "Object exists" {
if er.Message == "Object exists" {
return nil return nil
} }
return fmt.Errorf("inwx: %w", err)
}
return fmt.Errorf("inwx: %w", err) return fmt.Errorf("inwx: %w", err)
} }
@ -147,14 +144,14 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error {
// CleanUp removes the TXT record matching the specified parameters. // CleanUp removes the TXT record matching the specified parameters.
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
challengeInfo := dns01.GetChallengeInfo(domain, keyAuth) info := dns01.GetChallengeInfo(domain, keyAuth)
authZone, err := dns01.FindZoneByFqdn(challengeInfo.EffectiveFQDN) authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
if err != nil { if err != nil {
return fmt.Errorf("inwx: could not find zone for domain %q (%s): %w", domain, challengeInfo.EffectiveFQDN, err) return fmt.Errorf("inwx: could not find zone for domain %q (%s): %w", domain, info.EffectiveFQDN, err)
} }
info, err := d.client.Account.Login() login, err := d.client.Account.Login()
if err != nil { if err != nil {
return fmt.Errorf("inwx: %w", err) return fmt.Errorf("inwx: %w", err)
} }
@ -166,14 +163,14 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
} }
}() }()
err = d.twoFactorAuth(info) err = d.twoFactorAuth(login)
if err != nil { if err != nil {
return fmt.Errorf("inwx: %w", err) return fmt.Errorf("inwx: %w", err)
} }
response, err := d.client.Nameservers.Info(&goinwx.NameserverInfoRequest{ response, err := d.client.Nameservers.Info(&goinwx.NameserverInfoRequest{
Domain: dns01.UnFqdn(authZone), Domain: dns01.UnFqdn(authZone),
Name: dns01.UnFqdn(challengeInfo.EffectiveFQDN), Name: dns01.UnFqdn(info.EffectiveFQDN),
Type: "TXT", Type: "TXT",
}) })
if err != nil { if err != nil {
@ -182,7 +179,7 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
var recordID int var recordID int
for _, record := range response.Records { for _, record := range response.Records {
if record.Content != challengeInfo.Value { if record.Content != info.Value {
continue continue
} }

View File

@ -7,6 +7,7 @@ import (
"github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/aws"
awsconfig "github.com/aws/aws-sdk-go-v2/config" awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/lightsail" "github.com/aws/aws-sdk-go-v2/service/lightsail"
"github.com/go-acme/lego/v4/providers/dns/internal/ptr"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -52,19 +53,10 @@ func TestLiveTTL(t *testing.T) {
entries := resp.Domain.DomainEntries entries := resp.Domain.DomainEntries
for _, entry := range entries { for _, entry := range entries {
if deref(entry.Type) == "TXT" && deref(entry.Name) == fqdn { if ptr.Deref(entry.Type) == "TXT" && ptr.Deref(entry.Name) == fqdn {
return return
} }
} }
t.Fatalf("Could not find a TXT record for _acme-challenge.%s", domain) t.Fatalf("Could not find a TXT record for _acme-challenge.%s", domain)
} }
func deref[T string | int | int32 | int64 | bool](v *T) T {
if v == nil {
var zero T
return zero
}
return *v
}

View File

@ -21,6 +21,7 @@ import (
"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" "github.com/go-acme/lego/v4/platform/config/env"
"github.com/go-acme/lego/v4/platform/wait" "github.com/go-acme/lego/v4/platform/wait"
"github.com/go-acme/lego/v4/providers/dns/internal/ptr"
) )
// Environment variables names. // Environment variables names.
@ -151,7 +152,7 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error {
var found bool var found bool
for _, record := range records { for _, record := range records {
if deref(record.Value) == realValue { if ptr.Deref(record.Value) == realValue {
found = true found = true
} }
} }
@ -196,7 +197,7 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
var nonLegoRecords []awstypes.ResourceRecord var nonLegoRecords []awstypes.ResourceRecord
for _, record := range existingRecords { for _, record := range existingRecords {
if deref(record.Value) != `"`+info.Value+`"` { if ptr.Deref(record.Value) != `"`+info.Value+`"` {
nonLegoRecords = append(nonLegoRecords, record) nonLegoRecords = append(nonLegoRecords, record)
} }
} }
@ -255,7 +256,7 @@ func (d *DNSProvider) changeRecord(ctx context.Context, action awstypes.ChangeAc
return true, nil return true, nil
} }
return false, fmt.Errorf("unable to retrieve change: ID=%s", deref(changeID)) return false, fmt.Errorf("unable to retrieve change: ID=%s", ptr.Deref(changeID))
}) })
} }
@ -281,7 +282,7 @@ func (d *DNSProvider) getExistingRecordSets(ctx context.Context, hostedZoneID, f
var records []awstypes.ResourceRecord var records []awstypes.ResourceRecord
for _, recordSet := range recordSetsOutput.ResourceRecordSets { for _, recordSet := range recordSetsOutput.ResourceRecordSets {
if deref(recordSet.Name) == fqdn { if ptr.Deref(recordSet.Name) == fqdn {
records = append(records, recordSet.ResourceRecords...) records = append(records, recordSet.ResourceRecords...)
} }
} }
@ -311,8 +312,8 @@ func (d *DNSProvider) getHostedZoneID(ctx context.Context, fqdn string) (string,
var hostedZoneID string var hostedZoneID string
for _, hostedZone := range resp.HostedZones { for _, hostedZone := range resp.HostedZones {
// .Name has a trailing dot // .Name has a trailing dot
if !hostedZone.Config.PrivateZone && deref(hostedZone.Name) == authZone { if !hostedZone.Config.PrivateZone && ptr.Deref(hostedZone.Name) == authZone {
hostedZoneID = deref(hostedZone.Id) hostedZoneID = ptr.Deref(hostedZone.Id)
break break
} }
} }
@ -394,12 +395,3 @@ func createAWSConfigCheckParams(config *Config) error {
return nil return nil
} }
func deref[T string | int | int32 | int64 | bool](v *T) T {
if v == nil {
var zero T
return zero
}
return *v
}

View File

@ -7,6 +7,7 @@ import (
"github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/aws"
awsconfig "github.com/aws/aws-sdk-go-v2/config" awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/route53" "github.com/aws/aws-sdk-go-v2/service/route53"
"github.com/go-acme/lego/v4/providers/dns/internal/ptr"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -52,7 +53,7 @@ func TestLiveTTL(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
for _, v := range resp.ResourceRecordSets { for _, v := range resp.ResourceRecordSets {
if deref(v.Name) == fqdn && v.Type == "TXT" && deref(v.TTL) == 10 { if ptr.Deref(v.Name) == fqdn && v.Type == "TXT" && ptr.Deref(v.TTL) == 10 {
return return
} }
} }

View File

@ -12,6 +12,7 @@ import (
"github.com/go-acme/lego/v4/challenge" "github.com/go-acme/lego/v4/challenge"
"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" "github.com/go-acme/lego/v4/platform/config/env"
"github.com/go-acme/lego/v4/providers/dns/internal/ptr"
"github.com/miekg/dns" "github.com/miekg/dns"
"github.com/volcengine/volc-sdk-golang/base" "github.com/volcengine/volc-sdk-golang/base"
volc "github.com/volcengine/volc-sdk-golang/service/dns" volc "github.com/volcengine/volc-sdk-golang/service/dns"
@ -126,16 +127,16 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error {
return fmt.Errorf("volcengine: get zone ID: %w", err) return fmt.Errorf("volcengine: get zone ID: %w", err)
} }
subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, deref(zone.ZoneName)) subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, ptr.Deref(zone.ZoneName))
if err != nil { if err != nil {
return fmt.Errorf("volcengine: %w", err) return fmt.Errorf("volcengine: %w", err)
} }
crr := &volc.CreateRecordRequest{ crr := &volc.CreateRecordRequest{
Host: pointer(subDomain), Host: ptr.Pointer(subDomain),
TTL: pointer(int64(d.config.TTL)), TTL: ptr.Pointer(int64(d.config.TTL)),
Type: pointer("TXT"), Type: ptr.Pointer("TXT"),
Value: pointer(info.Value), Value: ptr.Pointer(info.Value),
ZID: zone.ZID, ZID: zone.ZID,
} }
@ -178,8 +179,8 @@ func (d *DNSProvider) getZone(ctx context.Context, fqdn string) (volc.TopZoneRes
domain := fqdn[index:] domain := fqdn[index:]
lzr := &volc.ListZonesRequest{ lzr := &volc.ListZonesRequest{
Key: pointer(dns01.UnFqdn(domain)), Key: ptr.Pointer(dns01.UnFqdn(domain)),
SearchMode: pointer("exact"), SearchMode: ptr.Pointer("exact"),
} }
zones, err := d.client.ListZones(ctx, lzr) zones, err := d.client.ListZones(ctx, lzr)
@ -187,7 +188,7 @@ func (d *DNSProvider) getZone(ctx context.Context, fqdn string) (volc.TopZoneRes
return volc.TopZoneResponse{}, fmt.Errorf("list zones: %w", err) return volc.TopZoneResponse{}, fmt.Errorf("list zones: %w", err)
} }
total := deref(zones.Total) total := ptr.Deref(zones.Total)
if total == 0 || len(zones.Zones) == 0 { if total == 0 || len(zones.Zones) == 0 {
continue continue
@ -233,14 +234,3 @@ func newClient(config *Config) *volc.Client {
return volc.NewClient(caller) return volc.NewClient(caller)
} }
func pointer[T any](v T) *T { return &v }
func deref[T any](v *T) T {
if v == nil {
var zero T
return zero
}
return *v
}