lego/providers/dns/dnsimple/dnsimple.go

180 lines
4.4 KiB
Go
Raw Normal View History

// Package dnsimple implements a DNS provider for solving the DNS-01 challenge
// using dnsimple DNS.
2016-02-29 02:48:41 +00:00
package dnsimple
import (
"fmt"
"os"
2017-03-17 18:40:51 +00:00
"strconv"
"strings"
2017-03-17 18:40:51 +00:00
"github.com/dnsimple/dnsimple-go/dnsimple"
2016-02-29 02:48:41 +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-03-17 18:40:51 +00:00
// Credentials must be passed in the environment variables: DNSIMPLE_OAUTH_TOKEN.
//
// See: https://developer.dnsimple.com/v2/#authentication
func NewDNSProvider() (*DNSProvider, error) {
2017-03-17 18:40:51 +00:00
accessToken := os.Getenv("DNSIMPLE_OAUTH_TOKEN")
2018-05-30 17:53:04 +00:00
baseURL := os.Getenv("DNSIMPLE_BASE_URL")
2017-03-17 18:40:51 +00:00
2018-05-30 17:53:04 +00:00
return NewDNSProviderCredentials(accessToken, baseURL)
}
// NewDNSProviderCredentials uses the supplied credentials to return a
// DNSProvider instance configured for dnsimple.
2018-05-30 17:53:04 +00:00
func NewDNSProviderCredentials(accessToken, baseURL string) (*DNSProvider, error) {
2017-03-17 18:40:51 +00:00
if accessToken == "" {
return nil, fmt.Errorf("DNSimple OAuth token is missing")
2017-03-17 18:40:51 +00:00
}
client := dnsimple.NewClient(dnsimple.NewOauthTokenCredentials(accessToken))
client.UserAgent = "lego"
2018-05-30 17:53:04 +00:00
if baseURL != "" {
client.BaseURL = baseURL
}
2017-03-17 18:40:51 +00:00
return &DNSProvider{client: client}, nil
}
// Present creates a TXT record to fulfil the dns-01 challenge.
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
2016-02-29 02:48:41 +00:00
fqdn, value, ttl := acme.DNS01Record(domain, keyAuth)
zoneName, err := d.getHostedZone(domain)
2017-03-17 18:40:51 +00:00
if err != nil {
return err
}
accountID, err := d.getAccountID()
if err != nil {
return err
}
recordAttributes := d.newTxtRecord(zoneName, fqdn, value, ttl)
_, err = d.client.Zones.CreateRecord(accountID, zoneName, *recordAttributes)
if err != nil {
return fmt.Errorf("DNSimple API call failed: %v", err)
}
return nil
}
// CleanUp removes the TXT record matching the specified parameters.
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
2016-02-29 02:48:41 +00:00
fqdn, _, _ := acme.DNS01Record(domain, keyAuth)
records, err := d.findTxtRecords(domain, fqdn)
if err != nil {
return err
}
accountID, err := d.getAccountID()
2017-03-17 18:40:51 +00:00
if err != nil {
return err
}
for _, rec := range records {
_, err := d.client.Zones.DeleteRecord(accountID, rec.ZoneID, rec.ID)
if err != nil {
return err
}
}
2017-03-17 18:40:51 +00:00
return nil
}
func (d *DNSProvider) getHostedZone(domain string) (string, error) {
2017-03-17 18:40:51 +00:00
authZone, err := acme.FindZoneByFqdn(acme.ToFqdn(domain), acme.RecursiveNameservers)
if err != nil {
2017-03-17 18:40:51 +00:00
return "", err
}
accountID, err := d.getAccountID()
if err != nil {
2017-03-17 18:40:51 +00:00
return "", err
}
2017-03-17 18:40:51 +00:00
zoneName := acme.UnFqdn(authZone)
zones, err := d.client.Zones.ListZones(accountID, &dnsimple.ZoneListOptions{NameLike: zoneName})
2017-03-17 18:40:51 +00:00
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 {
hostedZone = zone
}
}
2017-03-17 18:40:51 +00:00
if hostedZone.ID == 0 {
2018-05-30 17:53:04 +00:00
return "", fmt.Errorf("zone %s not found in DNSimple for domain %s", authZone, domain)
}
2017-03-17 18:40:51 +00:00
return hostedZone.Name, nil
}
func (d *DNSProvider) findTxtRecords(domain, fqdn string) ([]dnsimple.ZoneRecord, error) {
zoneName, err := d.getHostedZone(domain)
if err != nil {
return nil, err
}
accountID, err := d.getAccountID()
if err != nil {
2017-03-17 18:40:51 +00:00
return nil, err
}
recordName := d.extractRecordName(fqdn, zoneName)
2017-03-17 18:40:51 +00:00
result, err := d.client.Zones.ListRecords(accountID, zoneName, &dnsimple.ZoneRecordListOptions{Name: recordName, Type: "TXT", ListOptions: dnsimple.ListOptions{}})
2017-03-17 18:40:51 +00:00
if err != nil {
return []dnsimple.ZoneRecord{}, fmt.Errorf("DNSimple API call has failed: %v", err)
}
2017-03-17 18:40:51 +00:00
return result.Data, nil
}
func (d *DNSProvider) newTxtRecord(zoneName, fqdn, value string, ttl int) *dnsimple.ZoneRecord {
name := d.extractRecordName(fqdn, zoneName)
2017-03-17 18:40:51 +00:00
return &dnsimple.ZoneRecord{
Type: "TXT",
Name: name,
Content: value,
TTL: ttl,
}
}
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
}
2017-03-17 18:40:51 +00:00
func (d *DNSProvider) getAccountID() (string, error) {
whoamiResponse, err := d.client.Identity.Whoami()
2017-03-17 18:40:51 +00:00
if err != nil {
return "", err
}
if whoamiResponse.Data.Account == nil {
2018-05-30 17:53:04 +00:00
return "", fmt.Errorf("DNSimple user tokens are not supported, please use an account token")
2017-03-17 18:40:51 +00:00
}
2018-04-15 13:49:13 +00:00
return strconv.FormatInt(whoamiResponse.Data.Account.ID, 10), nil
2017-03-17 18:40:51 +00:00
}