Skip to content

Commit

Permalink
chore: shared deref and pointer functions (#2376)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored Dec 5, 2024
1 parent 1a62bba commit 0bbf5ab
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 148 deletions.
9 changes: 0 additions & 9 deletions providers/dns/azuredns/azuredns.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,3 @@ func getZoneName(config *Config, fqdn string) (string, error) {

return authZone, nil
}

func deref[T any](v *T) T {
if v == nil {
var zero T
return zero
}

return *v
}
3 changes: 2 additions & 1 deletion providers/dns/azuredns/private.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"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/dns01"
"github.com/go-acme/lego/v4/providers/dns/internal/ptr"
)

var _ challenge.ProviderTimeout = (*DNSProviderPrivate)(nil)
Expand Down Expand Up @@ -184,7 +185,7 @@ func privateUniqueRecords(recordSet armprivatedns.RecordSet, value string) map[s
for _, txtRecord := range recordSet.Properties.TxtRecords {
// Assume Value doesn't contain multiple strings
if len(txtRecord.Value) > 0 {
uniqRecords[deref(txtRecord.Value[0])] = struct{}{}
uniqRecords[ptr.Deref(txtRecord.Value[0])] = struct{}{}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion providers/dns/azuredns/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"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/dns01"
"github.com/go-acme/lego/v4/providers/dns/internal/ptr"
)

var _ challenge.ProviderTimeout = (*DNSProviderPublic)(nil)
Expand Down Expand Up @@ -182,7 +183,7 @@ func publicUniqueRecords(recordSet armdns.RecordSet, value string) map[string]st
for _, txtRecord := range recordSet.Properties.TxtRecords {
// Assume Value doesn't contain multiple strings
if len(txtRecord.Value) > 0 {
uniqRecords[deref(txtRecord.Value[0])] = struct{}{}
uniqRecords[ptr.Deref(txtRecord.Value[0])] = struct{}{}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion providers/dns/azuredns/servicediscovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"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/resourcemanager/resourcegraph/armresourcegraph"
"github.com/go-acme/lego/v4/providers/dns/internal/ptr"
)

type ServiceDiscoveryZone struct {
Expand Down Expand Up @@ -88,7 +89,7 @@ func discoverDNSZones(ctx context.Context, config *Config, credentials azcore.To
*requestOptions.Skip += ResourceGraphQueryOptionsTop

if result.TotalRecords != nil {
if int64(deref(requestOptions.Skip)) >= deref(result.TotalRecords) {
if int64(ptr.Deref(requestOptions.Skip)) >= ptr.Deref(result.TotalRecords) {
break
}
}
Expand Down
38 changes: 14 additions & 24 deletions providers/dns/bunny/bunny.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/go-acme/lego/v4/challenge"
"github.com/go-acme/lego/v4/challenge/dns01"
"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/nrdcg/bunny-go"
"golang.org/x/net/publicsuffix"
Expand Down Expand Up @@ -104,20 +105,20 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error {
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 {
return fmt.Errorf("bunny: %w", err)
}

record := &bunny.AddOrUpdateDNSRecordOptions{
Type: pointer(bunny.DNSRecordTypeTXT),
Name: pointer(subDomain),
Value: pointer(info.Value),
TTL: pointer(int32(d.config.TTL)),
Type: ptr.Pointer(bunny.DNSRecordTypeTXT),
Name: ptr.Pointer(subDomain),
Value: ptr.Pointer(info.Value),
TTL: ptr.Pointer(int32(d.config.TTL)),
}

if _, err := d.client.DNSZone.AddDNSRecord(ctx, 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)
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, ptr.Deref(zone.ID), err)
}

return nil
Expand All @@ -134,26 +135,26 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
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 {
return fmt.Errorf("bunny: %w", err)
}

var record *bunny.DNSRecord
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
record = &r
break
}
}

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 {
return fmt.Errorf("bunny: failed to delete TXT record: id=%d, name=%s: %w", deref(record.ID), deref(record.Name), err)
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", ptr.Deref(record.ID), ptr.Deref(record.Name), err)
}

return nil
Expand Down Expand Up @@ -184,7 +185,7 @@ func findZone(zones *bunny.DNSZones, domain string) *bunny.DNSZone {
continue
}

curr := deref(item.Domain)
curr := ptr.Deref(item.Domain)

if slices.Contains(domains, curr) && domainLength < len(curr) {
domainLength = len(curr)
Expand Down Expand Up @@ -213,14 +214,3 @@ func possibleDomains(domain string) []string {

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
}
49 changes: 25 additions & 24 deletions providers/dns/bunny/bunny_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"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/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -137,54 +138,54 @@ func Test_findZone(t *testing.T) {
desc: "found subdomain",
domain: "_acme-challenge.foo.bar.example.com",
items: []*bunny.DNSZone{
{ID: pointer[int64](1), Domain: pointer("example.com")},
{ID: pointer[int64](2), Domain: pointer("example.org")},
{ID: pointer[int64](4), Domain: pointer("bar.example.org")},
{ID: pointer[int64](5), Domain: pointer("bar.example.com")},
{ID: pointer[int64](6), Domain: pointer("foo.example.com")},
{ID: ptr.Pointer[int64](1), Domain: ptr.Pointer("example.com")},
{ID: ptr.Pointer[int64](2), Domain: ptr.Pointer("example.org")},
{ID: ptr.Pointer[int64](4), Domain: ptr.Pointer("bar.example.org")},
{ID: ptr.Pointer[int64](5), Domain: ptr.Pointer("bar.example.com")},
{ID: ptr.Pointer[int64](6), Domain: ptr.Pointer("foo.example.com")},
},
expected: &bunny.DNSZone{
ID: pointer[int64](5),
Domain: pointer("bar.example.com"),
ID: ptr.Pointer[int64](5),
Domain: ptr.Pointer("bar.example.com"),
},
},
{
desc: "found the longest subdomain",
domain: "_acme-challenge.foo.bar.example.com",
items: []*bunny.DNSZone{
{ID: pointer[int64](7), Domain: pointer("foo.bar.example.com")},
{ID: pointer[int64](1), Domain: pointer("example.com")},
{ID: pointer[int64](2), Domain: pointer("example.org")},
{ID: pointer[int64](4), Domain: pointer("bar.example.org")},
{ID: pointer[int64](5), Domain: pointer("bar.example.com")},
{ID: pointer[int64](6), Domain: pointer("foo.example.com")},
{ID: ptr.Pointer[int64](7), Domain: ptr.Pointer("foo.bar.example.com")},
{ID: ptr.Pointer[int64](1), Domain: ptr.Pointer("example.com")},
{ID: ptr.Pointer[int64](2), Domain: ptr.Pointer("example.org")},
{ID: ptr.Pointer[int64](4), Domain: ptr.Pointer("bar.example.org")},
{ID: ptr.Pointer[int64](5), Domain: ptr.Pointer("bar.example.com")},
{ID: ptr.Pointer[int64](6), Domain: ptr.Pointer("foo.example.com")},
},
expected: &bunny.DNSZone{
ID: pointer[int64](7),
Domain: pointer("foo.bar.example.com"),
ID: ptr.Pointer[int64](7),
Domain: ptr.Pointer("foo.bar.example.com"),
},
},
{
desc: "found apex",
domain: "_acme-challenge.foo.bar.example.com",
items: []*bunny.DNSZone{
{ID: pointer[int64](1), Domain: pointer("example.com")},
{ID: pointer[int64](2), Domain: pointer("example.org")},
{ID: pointer[int64](4), Domain: pointer("bar.example.org")},
{ID: pointer[int64](6), Domain: pointer("foo.example.com")},
{ID: ptr.Pointer[int64](1), Domain: ptr.Pointer("example.com")},
{ID: ptr.Pointer[int64](2), Domain: ptr.Pointer("example.org")},
{ID: ptr.Pointer[int64](4), Domain: ptr.Pointer("bar.example.org")},
{ID: ptr.Pointer[int64](6), Domain: ptr.Pointer("foo.example.com")},
},
expected: &bunny.DNSZone{
ID: pointer[int64](1),
Domain: pointer("example.com"),
ID: ptr.Pointer[int64](1),
Domain: ptr.Pointer("example.com"),
},
},
{
desc: "not found",
domain: "_acme-challenge.foo.bar.example.com",
items: []*bunny.DNSZone{
{ID: pointer[int64](2), Domain: pointer("example.org")},
{ID: pointer[int64](4), Domain: pointer("bar.example.org")},
{ID: pointer[int64](6), Domain: pointer("foo.example.com")},
{ID: ptr.Pointer[int64](2), Domain: ptr.Pointer("example.org")},
{ID: ptr.Pointer[int64](4), Domain: ptr.Pointer("bar.example.org")},
{ID: ptr.Pointer[int64](6), Domain: ptr.Pointer("foo.example.com")},
},
},
}
Expand Down
38 changes: 14 additions & 24 deletions providers/dns/huaweicloud/huaweicloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/go-acme/lego/v4/challenge/dns01"
"github.com/go-acme/lego/v4/platform/config/env"
"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"
hwconfig "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/config"
hwdns "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dns/v2"
Expand Down Expand Up @@ -155,7 +156,7 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error {
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 {
return fmt.Errorf("huaweicloud: %w", err)
Expand Down Expand Up @@ -208,16 +209,16 @@ func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
func (d *DNSProvider) getOrCreateRecordSetID(domain, zoneID string, info dns01.ChallengeInfo) (string, error) {
records, err := d.client.ListRecordSetsByZone(&hwmodel.ListRecordSetsByZoneRequest{
ZoneId: zoneID,
Name: pointer(info.EffectiveFQDN),
Name: ptr.Pointer(info.EffectiveFQDN),
})
if err != nil {
return "", fmt.Errorf("record list: unable to get record %s for zone %s: %w", info.EffectiveFQDN, domain, err)
}

var existingRecordSet *hwmodel.ListRecordSets

for _, record := range deref(records.Recordsets) {
if deref(record.Type) == "TXT" && deref(record.Name) == info.EffectiveFQDN {
for _, record := range ptr.Deref(records.Recordsets) {
if ptr.Deref(record.Type) == "TXT" && ptr.Deref(record.Name) == info.EffectiveFQDN {
existingRecordSet = &record
}
}
Expand All @@ -229,9 +230,9 @@ func (d *DNSProvider) getOrCreateRecordSetID(domain, zoneID string, info dns01.C
ZoneId: zoneID,
Body: &hwmodel.CreateRecordSetRequestBody{
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",
Ttl: pointer(d.config.TTL),
Ttl: ptr.Pointer(d.config.TTL),
Records: []string{value},
},
}
Expand All @@ -241,18 +242,18 @@ func (d *DNSProvider) getOrCreateRecordSetID(domain, zoneID string, info dns01.C
return "", fmt.Errorf("create record set: %w", errCreate)
}

return deref(resp.Id), nil
return ptr.Deref(resp.Id), nil
}

updateRequest := &hwmodel.UpdateRecordSetRequest{
ZoneId: zoneID,
RecordsetId: deref(existingRecordSet.Id),
RecordsetId: ptr.Deref(existingRecordSet.Id),
Body: &hwmodel.UpdateRecordSetReq{
Name: existingRecordSet.Name,
Description: existingRecordSet.Description,
Type: existingRecordSet.Type,
Ttl: existingRecordSet.Ttl,
Records: pointer(append(deref(existingRecordSet.Records), value)),
Records: ptr.Pointer(append(ptr.Deref(existingRecordSet.Records), value)),
},
}

Expand All @@ -261,7 +262,7 @@ func (d *DNSProvider) getOrCreateRecordSetID(domain, zoneID string, info dns01.C
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) {
Expand All @@ -270,22 +271,11 @@ func (d *DNSProvider) getZoneID(authZone string) (string, error) {
return "", fmt.Errorf("unable to get zone: %w", err)
}

for _, zone := range deref(zones.Zones) {
if deref(zone.Name) == authZone {
return deref(zone.Id), nil
for _, zone := range ptr.Deref(zones.Zones) {
if ptr.Deref(zone.Name) == authZone {
return ptr.Deref(zone.Id), nil
}
}

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
}
12 changes: 12 additions & 0 deletions providers/dns/internal/ptr/types.go
Original file line number Diff line number Diff line change
@@ -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 }
Loading

0 comments on commit 0bbf5ab

Please sign in to comment.