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

118 lines
2.7 KiB
Go
Raw Normal View History

2017-02-07 21:33:23 +00:00
// Package ns1 implements a DNS provider for solving the DNS-01 challenge
// using NS1 DNS.
package ns1
import (
"fmt"
"net/http"
2018-07-23 15:30:03 +00:00
"strings"
2017-02-07 21:33:23 +00:00
"time"
2018-05-31 07:30:04 +00:00
"github.com/xenolf/lego/acme"
2018-07-03 10:44:04 +00:00
"github.com/xenolf/lego/platform/config/env"
2017-02-07 21:33:23 +00:00
"gopkg.in/ns1/ns1-go.v2/rest"
"gopkg.in/ns1/ns1-go.v2/rest/model/dns"
)
2018-05-31 07:30:04 +00:00
// DNSProvider is an implementation of the acme.ChallengeProvider interface.
2017-02-07 21:33:23 +00:00
type DNSProvider struct {
client *rest.Client
}
// NewDNSProvider returns a DNSProvider instance configured for NS1.
// Credentials must be passed in the environment variables: NS1_API_KEY.
func NewDNSProvider() (*DNSProvider, error) {
2018-07-03 10:44:04 +00:00
values, err := env.Get("NS1_API_KEY")
if err != nil {
return nil, fmt.Errorf("NS1: %v", err)
2017-02-07 21:33:23 +00:00
}
2018-07-03 10:44:04 +00:00
return NewDNSProviderCredentials(values["NS1_API_KEY"])
2017-02-07 21:33:23 +00:00
}
// NewDNSProviderCredentials uses the supplied credentials to return a
// DNSProvider instance configured for NS1.
func NewDNSProviderCredentials(key string) (*DNSProvider, error) {
if key == "" {
return nil, fmt.Errorf("NS1 credentials missing")
}
httpClient := &http.Client{Timeout: time.Second * 10}
client := rest.NewClient(httpClient, rest.SetAPIKey(key))
return &DNSProvider{client}, nil
}
// Present creates a TXT record to fulfil the dns-01 challenge.
2018-07-03 10:44:04 +00:00
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
2018-05-31 07:30:04 +00:00
fqdn, value, ttl := acme.DNS01Record(domain, keyAuth)
2017-02-07 21:33:23 +00:00
2018-07-03 10:44:04 +00:00
zone, err := d.getHostedZone(domain)
2017-02-07 21:33:23 +00:00
if err != nil {
return err
}
2018-07-03 10:44:04 +00:00
record := d.newTxtRecord(zone, fqdn, value, ttl)
_, err = d.client.Records.Create(record)
2017-02-07 21:33:23 +00:00
if err != nil && err != rest.ErrRecordExists {
return err
}
return nil
}
// CleanUp removes the TXT record matching the specified parameters.
2018-07-03 10:44:04 +00:00
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
2018-05-31 07:30:04 +00:00
fqdn, _, _ := acme.DNS01Record(domain, keyAuth)
2017-02-07 21:33:23 +00:00
2018-07-03 10:44:04 +00:00
zone, err := d.getHostedZone(domain)
2017-02-07 21:33:23 +00:00
if err != nil {
return err
}
2018-05-31 07:30:04 +00:00
name := acme.UnFqdn(fqdn)
2018-07-03 10:44:04 +00:00
_, err = d.client.Records.Delete(zone.Zone, name, "TXT")
2017-02-07 21:33:23 +00:00
return err
}
2018-07-03 10:44:04 +00:00
func (d *DNSProvider) getHostedZone(domain string) (*dns.Zone, error) {
2018-07-23 15:30:03 +00:00
authZone, err := getAuthZone(domain)
if err != nil {
return nil, err
}
zone, _, err := d.client.Zones.Get(authZone)
2017-02-07 21:33:23 +00:00
if err != nil {
return nil, err
}
return zone, nil
}
2018-07-23 15:30:03 +00:00
func getAuthZone(fqdn string) (string, error) {
authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers)
if err != nil {
return "", err
}
if strings.HasSuffix(authZone, ".") {
authZone = authZone[:len(authZone)-len(".")]
}
return authZone, err
}
2018-07-03 10:44:04 +00:00
func (d *DNSProvider) newTxtRecord(zone *dns.Zone, fqdn, value string, ttl int) *dns.Record {
2018-05-31 07:30:04 +00:00
name := acme.UnFqdn(fqdn)
2017-02-07 21:33:23 +00:00
return &dns.Record{
Type: "TXT",
Zone: zone.Zone,
Domain: name,
TTL: ttl,
Answers: []*dns.Answer{
{Rdata: []string{value}},
},
}
}