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

181 lines
4.4 KiB
Go
Raw Normal View History

2017-02-07 21:33:23 +00:00
// Package dnsimple implements a DNS provider for solving the DNS-01 challenge
// using dnsimple DNS.
package dnsimple
import (
"fmt"
"os"
2017-04-07 09:53:39 +00:00
"strconv"
2017-02-07 21:33:23 +00:00
"strings"
2017-04-07 09:53:39 +00:00
"github.com/dnsimple/dnsimple-go/dnsimple"
2017-02-07 21:33:23 +00:00
"github.com/xenolf/lego/acme"
)
// DNSProvider is an implementation of the acme.ChallengeProvider interface.
type DNSProvider struct {
client *dnsimple.Client
}
// NewDNSProvider returns a DNSProvider instance configured for dnsimple.
2017-04-07 09:53:39 +00:00
// Credentials must be passed in the environment variables: DNSIMPLE_OAUTH_TOKEN.
//
// See: https://developer.dnsimple.com/v2/#authentication
2017-02-07 21:33:23 +00:00
func NewDNSProvider() (*DNSProvider, error) {
2017-04-07 09:53:39 +00:00
accessToken := os.Getenv("DNSIMPLE_OAUTH_TOKEN")
baseUrl := os.Getenv("DNSIMPLE_BASE_URL")
return NewDNSProviderCredentials(accessToken, baseUrl)
2017-02-07 21:33:23 +00:00
}
// NewDNSProviderCredentials uses the supplied credentials to return a
// DNSProvider instance configured for dnsimple.
2017-04-07 09:53:39 +00:00
func NewDNSProviderCredentials(accessToken, baseUrl string) (*DNSProvider, error) {
if accessToken == "" {
return nil, fmt.Errorf("DNSimple OAuth token is missing")
}
client := dnsimple.NewClient(dnsimple.NewOauthTokenCredentials(accessToken))
client.UserAgent = "lego"
if baseUrl != "" {
client.BaseURL = baseUrl
2017-02-07 21:33:23 +00:00
}
2017-04-07 09:53:39 +00:00
return &DNSProvider{client: client}, nil
2017-02-07 21:33:23 +00:00
}
// Present creates a TXT record to fulfil the dns-01 challenge.
func (c *DNSProvider) Present(domain, token, keyAuth string) error {
fqdn, value, ttl := acme.DNS01Record(domain, keyAuth)
2017-04-07 09:53:39 +00:00
zoneName, err := c.getHostedZone(domain)
if err != nil {
return err
}
accountID, err := c.getAccountID()
2017-02-07 21:33:23 +00:00
if err != nil {
return err
}
recordAttributes := c.newTxtRecord(zoneName, fqdn, value, ttl)
2017-04-07 09:53:39 +00:00
_, err = c.client.Zones.CreateRecord(accountID, zoneName, *recordAttributes)
2017-02-07 21:33:23 +00:00
if err != nil {
return fmt.Errorf("DNSimple API call failed: %v", err)
}
return nil
}
// CleanUp removes the TXT record matching the specified parameters.
func (c *DNSProvider) CleanUp(domain, token, keyAuth string) error {
fqdn, _, _ := acme.DNS01Record(domain, keyAuth)
records, err := c.findTxtRecords(domain, fqdn)
if err != nil {
return err
}
2017-04-07 09:53:39 +00:00
accountID, err := c.getAccountID()
if err != nil {
return err
}
2017-02-07 21:33:23 +00:00
for _, rec := range records {
2017-04-07 09:53:39 +00:00
_, err := c.client.Zones.DeleteRecord(accountID, rec.ZoneID, rec.ID)
2017-02-07 21:33:23 +00:00
if err != nil {
return err
}
}
2017-04-07 09:53:39 +00:00
2017-02-07 21:33:23 +00:00
return nil
}
2017-04-07 09:53:39 +00:00
func (c *DNSProvider) getHostedZone(domain string) (string, error) {
authZone, err := acme.FindZoneByFqdn(acme.ToFqdn(domain), acme.RecursiveNameservers)
2017-02-07 21:33:23 +00:00
if err != nil {
2017-04-07 09:53:39 +00:00
return "", err
2017-02-07 21:33:23 +00:00
}
2017-04-07 09:53:39 +00:00
accountID, err := c.getAccountID()
2017-02-07 21:33:23 +00:00
if err != nil {
2017-04-07 09:53:39 +00:00
return "", err
2017-02-07 21:33:23 +00:00
}
2017-04-07 09:53:39 +00:00
zoneName := acme.UnFqdn(authZone)
zones, err := c.client.Zones.ListZones(accountID, &dnsimple.ZoneListOptions{NameLike: zoneName})
if err != nil {
return "", fmt.Errorf("DNSimple API call failed: %v", err)
}
var hostedZone dnsimple.Zone
for _, zone := range zones.Data {
if zone.Name == zoneName {
2017-02-07 21:33:23 +00:00
hostedZone = zone
}
}
2017-04-07 09:53:39 +00:00
if hostedZone.ID == 0 {
return "", fmt.Errorf("Zone %s not found in DNSimple for domain %s", authZone, domain)
2017-02-07 21:33:23 +00:00
}
2017-04-07 09:53:39 +00:00
return hostedZone.Name, nil
2017-02-07 21:33:23 +00:00
}
2017-04-07 09:53:39 +00:00
func (c *DNSProvider) findTxtRecords(domain, fqdn string) ([]dnsimple.ZoneRecord, error) {
zoneName, err := c.getHostedZone(domain)
2017-02-07 21:33:23 +00:00
if err != nil {
return nil, err
}
2017-04-07 09:53:39 +00:00
accountID, err := c.getAccountID()
2017-02-07 21:33:23 +00:00
if err != nil {
2017-04-07 09:53:39 +00:00
return nil, err
2017-02-07 21:33:23 +00:00
}
recordName := c.extractRecordName(fqdn, zoneName)
2017-04-07 09:53:39 +00:00
result, err := c.client.Zones.ListRecords(accountID, zoneName, &dnsimple.ZoneRecordListOptions{Name: recordName, Type: "TXT", ListOptions: dnsimple.ListOptions{}})
if err != nil {
return []dnsimple.ZoneRecord{}, fmt.Errorf("DNSimple API call has failed: %v", err)
2017-02-07 21:33:23 +00:00
}
2017-04-07 09:53:39 +00:00
return result.Data, nil
2017-02-07 21:33:23 +00:00
}
2017-04-07 09:53:39 +00:00
func (c *DNSProvider) newTxtRecord(zoneName, fqdn, value string, ttl int) *dnsimple.ZoneRecord {
name := c.extractRecordName(fqdn, zoneName)
2017-02-07 21:33:23 +00:00
2017-04-07 09:53:39 +00:00
return &dnsimple.ZoneRecord{
2017-02-07 21:33:23 +00:00
Type: "TXT",
Name: name,
Content: value,
TTL: ttl,
}
}
func (c *DNSProvider) extractRecordName(fqdn, domain string) string {
name := acme.UnFqdn(fqdn)
if idx := strings.Index(name, "."+domain); idx != -1 {
return name[:idx]
}
return name
}
2017-04-07 09:53:39 +00:00
func (c *DNSProvider) getAccountID() (string, error) {
whoamiResponse, err := c.client.Identity.Whoami()
if err != nil {
return "", err
}
if whoamiResponse.Data.Account == nil {
return "", fmt.Errorf("DNSimple user tokens are not supported, please use an account token.")
}
return strconv.Itoa(whoamiResponse.Data.Account.ID), nil
}