2019-04-09 20:56:12 +00:00
|
|
|
package sakuracloud
|
|
|
|
|
|
|
|
import (
|
2020-10-27 11:01:05 +00:00
|
|
|
"errors"
|
2019-04-09 20:56:12 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
2019-04-09 20:56:12 +00:00
|
|
|
"github.com/sacloud/libsacloud/api"
|
|
|
|
"github.com/sacloud/libsacloud/sacloud"
|
|
|
|
)
|
|
|
|
|
|
|
|
const sacloudAPILockKey = "lego/dns/sacloud"
|
|
|
|
|
|
|
|
func (d *DNSProvider) addTXTRecord(fqdn, domain, value string, ttl int) error {
|
|
|
|
sacloud.LockByKey(sacloudAPILockKey)
|
|
|
|
defer sacloud.UnlockByKey(sacloudAPILockKey)
|
|
|
|
|
|
|
|
zone, err := d.getHostedZone(domain)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("sakuracloud: %w", err)
|
2019-04-09 20:56:12 +00:00
|
|
|
}
|
|
|
|
|
2020-06-27 12:28:59 +00:00
|
|
|
name := extractRecordName(fqdn, zone.Name)
|
2019-04-09 20:56:12 +00:00
|
|
|
|
|
|
|
zone.AddRecord(zone.CreateNewRecord(name, "TXT", value, ttl))
|
|
|
|
_, err = d.client.Update(zone.ID, zone)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("sakuracloud: API call failed: %w", err)
|
2019-04-09 20:56:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DNSProvider) cleanupTXTRecord(fqdn, domain string) error {
|
|
|
|
sacloud.LockByKey(sacloudAPILockKey)
|
|
|
|
defer sacloud.UnlockByKey(sacloudAPILockKey)
|
|
|
|
|
|
|
|
zone, err := d.getHostedZone(domain)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("sakuracloud: %w", err)
|
2019-04-09 20:56:12 +00:00
|
|
|
}
|
|
|
|
|
2020-06-27 12:28:59 +00:00
|
|
|
records := findTxtRecords(fqdn, zone)
|
2019-04-09 20:56:12 +00:00
|
|
|
|
|
|
|
for _, record := range records {
|
|
|
|
var updRecords []sacloud.DNSRecordSet
|
|
|
|
for _, r := range zone.Settings.DNS.ResourceRecordSets {
|
|
|
|
if !(r.Name == record.Name && r.Type == record.Type && r.RData == record.RData) {
|
|
|
|
updRecords = append(updRecords, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
zone.Settings.DNS.ResourceRecordSets = updRecords
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = d.client.Update(zone.ID, zone)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("sakuracloud: API call failed: %w", err)
|
2019-04-09 20:56:12 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DNSProvider) getHostedZone(domain string) (*sacloud.DNS, error) {
|
|
|
|
authZone, err := dns01.FindZoneByFqdn(dns01.ToFqdn(domain))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
zoneName := dns01.UnFqdn(authZone)
|
|
|
|
|
|
|
|
res, err := d.client.Reset().WithNameLike(zoneName).Find()
|
|
|
|
if err != nil {
|
2020-10-27 11:01:05 +00:00
|
|
|
var notFound api.Error
|
|
|
|
if errors.As(err, ¬Found) && notFound.ResponseCode() == http.StatusNotFound {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("zone %s not found on SakuraCloud DNS: %w", zoneName, err)
|
2019-04-09 20:56:12 +00:00
|
|
|
}
|
2020-10-27 11:01:05 +00:00
|
|
|
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("API call failed: %w", err)
|
2019-04-09 20:56:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, zone := range res.CommonServiceDNSItems {
|
|
|
|
if zone.Name == zoneName {
|
|
|
|
return &zone, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("zone %s not found", zoneName)
|
|
|
|
}
|
|
|
|
|
2020-06-27 12:28:59 +00:00
|
|
|
func findTxtRecords(fqdn string, zone *sacloud.DNS) []sacloud.DNSRecordSet {
|
|
|
|
recordName := extractRecordName(fqdn, zone.Name)
|
2019-04-09 20:56:12 +00:00
|
|
|
|
|
|
|
var res []sacloud.DNSRecordSet
|
|
|
|
for _, record := range zone.Settings.DNS.ResourceRecordSets {
|
|
|
|
if record.Name == recordName && record.Type == "TXT" {
|
|
|
|
res = append(res, record)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2020-06-27 12:28:59 +00:00
|
|
|
func extractRecordName(fqdn, zone string) string {
|
2019-04-09 20:56:12 +00:00
|
|
|
name := dns01.UnFqdn(fqdn)
|
2020-06-27 12:28:59 +00:00
|
|
|
if idx := strings.Index(name, "."+zone); idx != -1 {
|
2019-04-09 20:56:12 +00:00
|
|
|
return name[:idx]
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|