2018-06-21 19:24:45 +00:00
|
|
|
// Package sakuracloud implements a DNS provider for solving the DNS-01 challenge
|
|
|
|
// using sakuracloud DNS.
|
|
|
|
package sakuracloud
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2018-09-15 17:07:24 +00:00
|
|
|
"time"
|
2018-06-21 19:24:45 +00:00
|
|
|
|
|
|
|
"github.com/sacloud/libsacloud/api"
|
|
|
|
"github.com/sacloud/libsacloud/sacloud"
|
|
|
|
"github.com/xenolf/lego/acme"
|
|
|
|
"github.com/xenolf/lego/platform/config/env"
|
|
|
|
)
|
|
|
|
|
2018-09-15 17:07:24 +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{
|
|
|
|
TTL: env.GetOrDefaultInt("SAKURACLOUD_TTL", 120),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond("SAKURACLOUD_PROPAGATION_TIMEOUT", acme.DefaultPropagationTimeout),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond("SAKURACLOUD_POLLING_INTERVAL", acme.DefaultPollingInterval),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-21 19:24:45 +00:00
|
|
|
// DNSProvider is an implementation of the acme.ChallengeProvider interface.
|
|
|
|
type DNSProvider struct {
|
2018-09-15 17:07:24 +00:00
|
|
|
config *Config
|
2018-06-21 19:24:45 +00:00
|
|
|
client *api.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for sakuracloud.
|
|
|
|
// 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 17:07:24 +00:00
|
|
|
return nil, fmt.Errorf("sakuracloud: %v", err)
|
2018-06-21 19:24:45 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.Token = values["SAKURACLOUD_ACCESS_TOKEN"]
|
|
|
|
config.Secret = values["SAKURACLOUD_ACCESS_TOKEN_SECRET"]
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2018-06-21 19:24:45 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
// NewDNSProviderCredentials uses the supplied credentials
|
|
|
|
// to return a DNSProvider instance configured for sakuracloud.
|
|
|
|
// Deprecated
|
2018-06-21 19:24:45 +00:00
|
|
|
func NewDNSProviderCredentials(token, secret string) (*DNSProvider, error) {
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.Token = token
|
|
|
|
config.Secret = secret
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for GleSYS.
|
|
|
|
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 19:24:45 +00:00
|
|
|
}
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
if config.Token == "" {
|
|
|
|
return nil, errors.New("sakuracloud: AccessToken is missing")
|
2018-06-21 19:24:45 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
if config.Secret == "" {
|
|
|
|
return nil, errors.New("sakuracloud: AccessSecret is missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
client := api.NewClient(config.Token, config.Secret, "tk1a")
|
2018-06-21 19:24:45 +00:00
|
|
|
client.UserAgent = acme.UserAgent
|
|
|
|
|
2018-10-11 13:49:33 +00:00
|
|
|
return &DNSProvider{client: client, config: config}, nil
|
2018-06-21 19:24:45 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
// Present creates a TXT record to fulfill the dns-01 challenge.
|
2018-06-21 19:24:45 +00:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2018-09-15 17:07:24 +00:00
|
|
|
fqdn, value, _ := acme.DNS01Record(domain, keyAuth)
|
2018-06-21 19:24:45 +00:00
|
|
|
|
|
|
|
zone, err := d.getHostedZone(domain)
|
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return fmt.Errorf("sakuracloud: %v", err)
|
2018-06-21 19:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
name := d.extractRecordName(fqdn, zone.Name)
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
zone.AddRecord(zone.CreateNewRecord(name, "TXT", value, d.config.TTL))
|
2018-06-21 19:24:45 +00:00
|
|
|
_, err = d.client.GetDNSAPI().Update(zone.ID, zone)
|
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return fmt.Errorf("sakuracloud: API call failed: %v", err)
|
2018-06-21 19:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
|
|
|
fqdn, _, _ := acme.DNS01Record(domain, keyAuth)
|
|
|
|
|
|
|
|
zone, err := d.getHostedZone(domain)
|
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return fmt.Errorf("sakuracloud: %v", err)
|
2018-06-21 19:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
records, err := d.findTxtRecords(fqdn, zone)
|
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return fmt.Errorf("sakuracloud: %v", err)
|
2018-06-21 19:24:45 +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-15 17:07:24 +00:00
|
|
|
return fmt.Errorf("sakuracloud: API call failed: %v", err)
|
2018-06-21 19:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +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-06-21 19:24:45 +00:00
|
|
|
func (d *DNSProvider) getHostedZone(domain string) (*sacloud.DNS, error) {
|
|
|
|
authZone, err := acme.FindZoneByFqdn(acme.ToFqdn(domain), acme.RecursiveNameservers)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
zoneName := acme.UnFqdn(authZone)
|
|
|
|
|
|
|
|
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 17:07:24 +00:00
|
|
|
return nil, fmt.Errorf("API call failed: %v", err)
|
2018-06-21 19:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, zone := range res.CommonServiceDNSItems {
|
|
|
|
if zone.Name == zoneName {
|
|
|
|
return &zone, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
return nil, fmt.Errorf("zone %s not found", zoneName)
|
2018-06-21 19:24:45 +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 {
|
|
|
|
name := acme.UnFqdn(fqdn)
|
|
|
|
if idx := strings.Index(name, "."+domain); idx != -1 {
|
|
|
|
return name[:idx]
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|