traefik/vendor/github.com/xenolf/lego/providers/dns/sakuracloud/sakuracloud.go

177 lines
5.1 KiB
Go
Raw Normal View History

2019-01-07 17:30:06 +00:00
// Package sakuracloud implements a DNS provider for solving the DNS-01 challenge using SakuraCloud DNS.
2018-07-03 10:44:04 +00:00
package sakuracloud
import (
"errors"
"fmt"
"net/http"
"strings"
2018-09-17 13:16:03 +00:00
"time"
2018-07-03 10:44:04 +00:00
"github.com/sacloud/libsacloud/api"
"github.com/sacloud/libsacloud/sacloud"
2019-01-07 17:30:06 +00:00
"github.com/xenolf/lego/challenge/dns01"
2018-07-03 10:44:04 +00:00
"github.com/xenolf/lego/platform/config/env"
)
2018-09-17 13:16:03 +00: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{
2019-01-07 17:30:06 +00: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-17 13:16:03 +00:00
}
}
2018-07-03 10:44:04 +00:00
// DNSProvider is an implementation of the acme.ChallengeProvider interface.
type DNSProvider struct {
2018-09-17 13:16:03 +00:00
config *Config
2018-07-03 10:44:04 +00:00
client *api.Client
}
2019-01-07 17:30:06 +00:00
// NewDNSProvider returns a DNSProvider instance configured for SakuraCloud.
2018-07-03 10:44:04 +00: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-17 13:16:03 +00:00
return nil, fmt.Errorf("sakuracloud: %v", err)
2018-07-03 10:44:04 +00:00
}
2018-09-17 13:16:03 +00:00
config := NewDefaultConfig()
config.Token = values["SAKURACLOUD_ACCESS_TOKEN"]
config.Secret = values["SAKURACLOUD_ACCESS_TOKEN_SECRET"]
return NewDNSProviderConfig(config)
2018-07-03 10:44:04 +00:00
}
2019-01-07 17:30:06 +00:00
// NewDNSProviderConfig return a DNSProvider instance configured for SakuraCloud.
2018-09-17 13:16:03 +00:00
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
if config == nil {
return nil, errors.New("sakuracloud: the configuration of the DNS provider is nil")
2018-07-03 10:44:04 +00:00
}
2018-09-17 13:16:03 +00:00
if config.Token == "" {
return nil, errors.New("sakuracloud: AccessToken is missing")
2018-07-03 10:44:04 +00:00
}
2018-09-17 13:16:03 +00:00
if config.Secret == "" {
return nil, errors.New("sakuracloud: AccessSecret is missing")
}
client := api.NewClient(config.Token, config.Secret, "tk1a")
2018-07-03 10:44:04 +00:00
2018-10-11 14:50:04 +00:00
return &DNSProvider{client: client, config: config}, nil
2018-07-03 10:44:04 +00:00
}
2018-10-10 14:28:04 +00:00
// Present creates a TXT record to fulfill the dns-01 challenge.
2018-07-03 10:44:04 +00:00
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
2019-01-07 17:30:06 +00:00
fqdn, value := dns01.GetRecord(domain, keyAuth)
2018-07-03 10:44:04 +00:00
zone, err := d.getHostedZone(domain)
if err != nil {
2018-09-17 13:16:03 +00:00
return fmt.Errorf("sakuracloud: %v", err)
2018-07-03 10:44:04 +00:00
}
name := d.extractRecordName(fqdn, zone.Name)
2018-09-17 13:16:03 +00:00
zone.AddRecord(zone.CreateNewRecord(name, "TXT", value, d.config.TTL))
2018-07-03 10:44:04 +00:00
_, err = d.client.GetDNSAPI().Update(zone.ID, zone)
if err != nil {
2018-09-17 13:16:03 +00:00
return fmt.Errorf("sakuracloud: API call failed: %v", err)
2018-07-03 10:44:04 +00:00
}
return nil
}
// CleanUp removes the TXT record matching the specified parameters.
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
2019-01-07 17:30:06 +00:00
fqdn, _ := dns01.GetRecord(domain, keyAuth)
2018-07-03 10:44:04 +00:00
zone, err := d.getHostedZone(domain)
if err != nil {
2018-09-17 13:16:03 +00:00
return fmt.Errorf("sakuracloud: %v", err)
2018-07-03 10:44:04 +00:00
}
records, err := d.findTxtRecords(fqdn, zone)
if err != nil {
2018-09-17 13:16:03 +00:00
return fmt.Errorf("sakuracloud: %v", err)
2018-07-03 10:44:04 +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.GetDNSAPI().Update(zone.ID, zone)
if err != nil {
2018-09-17 13:16:03 +00:00
return fmt.Errorf("sakuracloud: API call failed: %v", err)
2018-07-03 10:44:04 +00:00
}
return nil
}
2018-09-17 13:16:03 +00: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-07-03 10:44:04 +00:00
func (d *DNSProvider) getHostedZone(domain string) (*sacloud.DNS, error) {
2019-01-07 17:30:06 +00:00
authZone, err := dns01.FindZoneByFqdn(dns01.ToFqdn(domain))
2018-07-03 10:44:04 +00:00
if err != nil {
return nil, err
}
2019-01-07 17:30:06 +00:00
zoneName := dns01.UnFqdn(authZone)
2018-07-03 10:44:04 +00: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-17 13:16:03 +00:00
return nil, fmt.Errorf("API call failed: %v", err)
2018-07-03 10:44:04 +00:00
}
for _, zone := range res.CommonServiceDNSItems {
if zone.Name == zoneName {
return &zone, nil
}
}
2018-09-17 13:16:03 +00:00
return nil, fmt.Errorf("zone %s not found", zoneName)
2018-07-03 10:44:04 +00:00
}
func (d *DNSProvider) findTxtRecords(fqdn string, zone *sacloud.DNS) ([]sacloud.DNSRecordSet, error) {
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)
}
}
return res, nil
}
func (d *DNSProvider) extractRecordName(fqdn, domain string) string {
2019-01-07 17:30:06 +00:00
name := dns01.UnFqdn(fqdn)
2018-07-03 10:44:04 +00:00
if idx := strings.Index(name, "."+domain); idx != -1 {
return name[:idx]
}
return name
}