2018-12-06 23:50:17 +02:00
|
|
|
// Package sakuracloud implements a DNS provider for solving the DNS-01 challenge using SakuraCloud DNS.
|
2018-06-21 21:24:45 +02:00
|
|
|
package sakuracloud
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2018-09-15 19:07:24 +02:00
|
|
|
"time"
|
2018-06-21 21:24:45 +02:00
|
|
|
|
2019-03-11 18:56:48 +02:00
|
|
|
"github.com/go-acme/lego/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/platform/config/env"
|
2018-06-21 21:24:45 +02:00
|
|
|
"github.com/sacloud/libsacloud/api"
|
|
|
|
"github.com/sacloud/libsacloud/sacloud"
|
|
|
|
)
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
// Config is used to configure the creation of the DNSProvider
|
|
|
|
type Config struct {
|
|
|
|
Token string
|
|
|
|
Secret string
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider
|
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
2018-12-06 23:50:17 +02:00
|
|
|
TTL: env.GetOrDefaultInt("SAKURACLOUD_TTL", dns01.DefaultTTL),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond("SAKURACLOUD_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond("SAKURACLOUD_POLLING_INTERVAL", dns01.DefaultPollingInterval),
|
2018-09-15 19:07:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-21 21:24:45 +02:00
|
|
|
// DNSProvider is an implementation of the acme.ChallengeProvider interface.
|
|
|
|
type DNSProvider struct {
|
2018-09-15 19:07:24 +02:00
|
|
|
config *Config
|
2018-06-21 21:24:45 +02:00
|
|
|
client *api.Client
|
|
|
|
}
|
|
|
|
|
2018-12-06 23:50:17 +02:00
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for SakuraCloud.
|
2018-06-21 21:24:45 +02:00
|
|
|
// Credentials must be passed in the environment variables: SAKURACLOUD_ACCESS_TOKEN & SAKURACLOUD_ACCESS_TOKEN_SECRET
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
|
|
|
values, err := env.Get("SAKURACLOUD_ACCESS_TOKEN", "SAKURACLOUD_ACCESS_TOKEN_SECRET")
|
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return nil, fmt.Errorf("sakuracloud: %v", err)
|
2018-06-21 21:24:45 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.Token = values["SAKURACLOUD_ACCESS_TOKEN"]
|
|
|
|
config.Secret = values["SAKURACLOUD_ACCESS_TOKEN_SECRET"]
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2018-06-21 21:24:45 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 23:50:17 +02:00
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for SakuraCloud.
|
2018-09-15 19:07:24 +02:00
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("sakuracloud: the configuration of the DNS provider is nil")
|
2018-06-21 21:24:45 +02:00
|
|
|
}
|
2018-09-15 19:07:24 +02:00
|
|
|
|
|
|
|
if config.Token == "" {
|
|
|
|
return nil, errors.New("sakuracloud: AccessToken is missing")
|
2018-06-21 21:24:45 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
if config.Secret == "" {
|
|
|
|
return nil, errors.New("sakuracloud: AccessSecret is missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
client := api.NewClient(config.Token, config.Secret, "tk1a")
|
2018-06-21 21:24:45 +02:00
|
|
|
|
2018-10-11 15:49:33 +02:00
|
|
|
return &DNSProvider{client: client, config: config}, nil
|
2018-06-21 21:24:45 +02:00
|
|
|
}
|
|
|
|
|
2018-09-24 21:07:20 +02:00
|
|
|
// Present creates a TXT record to fulfill the dns-01 challenge.
|
2018-06-21 21:24:45 +02:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2018-12-06 23:50:17 +02:00
|
|
|
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
2018-06-21 21:24:45 +02:00
|
|
|
|
|
|
|
zone, err := d.getHostedZone(domain)
|
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return fmt.Errorf("sakuracloud: %v", err)
|
2018-06-21 21:24:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
name := d.extractRecordName(fqdn, zone.Name)
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
zone.AddRecord(zone.CreateNewRecord(name, "TXT", value, d.config.TTL))
|
2018-06-21 21:24:45 +02:00
|
|
|
_, err = d.client.GetDNSAPI().Update(zone.ID, zone)
|
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return fmt.Errorf("sakuracloud: API call failed: %v", err)
|
2018-06-21 21:24:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2018-12-06 23:50:17 +02:00
|
|
|
fqdn, _ := dns01.GetRecord(domain, keyAuth)
|
2018-06-21 21:24:45 +02:00
|
|
|
|
|
|
|
zone, err := d.getHostedZone(domain)
|
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return fmt.Errorf("sakuracloud: %v", err)
|
2018-06-21 21:24:45 +02:00
|
|
|
}
|
|
|
|
|
2019-02-16 15:56:15 +02:00
|
|
|
records := d.findTxtRecords(fqdn, zone)
|
2018-06-21 21:24:45 +02: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.GetDNSAPI().Update(zone.ID, zone)
|
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return fmt.Errorf("sakuracloud: API call failed: %v", err)
|
2018-06-21 21:24:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
// Timeout returns the timeout and interval to use when checking for DNS propagation.
|
|
|
|
// Adjusting here to cope with spikes in propagation times.
|
|
|
|
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
|
|
|
return d.config.PropagationTimeout, d.config.PollingInterval
|
|
|
|
}
|
|
|
|
|
2018-06-21 21:24:45 +02:00
|
|
|
func (d *DNSProvider) getHostedZone(domain string) (*sacloud.DNS, error) {
|
2018-12-06 23:50:17 +02:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(dns01.ToFqdn(domain))
|
2018-06-21 21:24:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-06 23:50:17 +02:00
|
|
|
zoneName := dns01.UnFqdn(authZone)
|
2018-06-21 21:24:45 +02:00
|
|
|
|
|
|
|
res, err := d.client.GetDNSAPI().WithNameLike(zoneName).Find()
|
|
|
|
if err != nil {
|
|
|
|
if notFound, ok := err.(api.Error); ok && notFound.ResponseCode() == http.StatusNotFound {
|
|
|
|
return nil, fmt.Errorf("zone %s not found on SakuraCloud DNS: %v", zoneName, err)
|
|
|
|
}
|
2018-09-15 19:07:24 +02:00
|
|
|
return nil, fmt.Errorf("API call failed: %v", err)
|
2018-06-21 21:24:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, zone := range res.CommonServiceDNSItems {
|
|
|
|
if zone.Name == zoneName {
|
|
|
|
return &zone, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
return nil, fmt.Errorf("zone %s not found", zoneName)
|
2018-06-21 21:24:45 +02:00
|
|
|
}
|
|
|
|
|
2019-02-16 15:56:15 +02:00
|
|
|
func (d *DNSProvider) findTxtRecords(fqdn string, zone *sacloud.DNS) []sacloud.DNSRecordSet {
|
2018-06-21 21:24:45 +02:00
|
|
|
recordName := d.extractRecordName(fqdn, zone.Name)
|
|
|
|
|
|
|
|
var res []sacloud.DNSRecordSet
|
|
|
|
for _, record := range zone.Settings.DNS.ResourceRecordSets {
|
|
|
|
if record.Name == recordName && record.Type == "TXT" {
|
|
|
|
res = append(res, record)
|
|
|
|
}
|
|
|
|
}
|
2019-02-16 15:56:15 +02:00
|
|
|
return res
|
2018-06-21 21:24:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DNSProvider) extractRecordName(fqdn, domain string) string {
|
2018-12-06 23:50:17 +02:00
|
|
|
name := dns01.UnFqdn(fqdn)
|
2018-06-21 21:24:45 +02:00
|
|
|
if idx := strings.Index(name, "."+domain); idx != -1 {
|
|
|
|
return name[:idx]
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|