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

248 lines
6.1 KiB
Go
Raw Normal View History

2017-02-07 21:33:23 +00:00
package dnsmadeeasy
import (
"bytes"
"crypto/hmac"
"crypto/sha1"
"crypto/tls"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"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
)
2018-05-31 07:30:04 +00:00
// DNSProvider is an implementation of the acme.ChallengeProvider interface that uses
2017-02-07 21:33:23 +00:00
// DNSMadeEasy's DNS API to manage TXT records for a domain.
type DNSProvider struct {
baseURL string
apiKey string
apiSecret string
2018-07-03 10:44:04 +00:00
client *http.Client
2017-02-07 21:33:23 +00:00
}
// Domain holds the DNSMadeEasy API representation of a Domain
type Domain struct {
ID int `json:"id"`
Name string `json:"name"`
}
// Record holds the DNSMadeEasy API representation of a Domain Record
type Record struct {
ID int `json:"id"`
Type string `json:"type"`
Name string `json:"name"`
Value string `json:"value"`
TTL int `json:"ttl"`
SourceID int `json:"sourceId"`
}
// NewDNSProvider returns a DNSProvider instance configured for DNSMadeEasy DNS.
// Credentials must be passed in the environment variables: DNSMADEEASY_API_KEY
// and DNSMADEEASY_API_SECRET.
func NewDNSProvider() (*DNSProvider, error) {
2018-07-03 10:44:04 +00:00
values, err := env.Get("DNSMADEEASY_API_KEY", "DNSMADEEASY_API_SECRET")
if err != nil {
return nil, fmt.Errorf("DNSMadeEasy: %v", err)
}
2017-02-07 21:33:23 +00:00
var baseURL string
2018-07-03 10:44:04 +00:00
if sandbox, _ := strconv.ParseBool(os.Getenv("DNSMADEEASY_SANDBOX")); sandbox {
2017-02-07 21:33:23 +00:00
baseURL = "https://api.sandbox.dnsmadeeasy.com/V2.0"
} else {
baseURL = "https://api.dnsmadeeasy.com/V2.0"
}
2018-07-03 10:44:04 +00:00
return NewDNSProviderCredentials(baseURL, values["DNSMADEEASY_API_KEY"], values["DNSMADEEASY_API_SECRET"])
2017-02-07 21:33:23 +00:00
}
// NewDNSProviderCredentials uses the supplied credentials to return a
// DNSProvider instance configured for DNSMadeEasy.
func NewDNSProviderCredentials(baseURL, apiKey, apiSecret string) (*DNSProvider, error) {
if baseURL == "" || apiKey == "" || apiSecret == "" {
return nil, fmt.Errorf("DNS Made Easy credentials missing")
}
2018-07-03 10:44:04 +00:00
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{
Transport: transport,
Timeout: 10 * time.Second,
}
2017-02-07 21:33:23 +00:00
return &DNSProvider{
baseURL: baseURL,
apiKey: apiKey,
apiSecret: apiSecret,
2018-07-03 10:44:04 +00:00
client: client,
2017-02-07 21:33:23 +00:00
}, nil
}
// Present creates a TXT record using the specified parameters
func (d *DNSProvider) Present(domainName, token, keyAuth string) error {
2018-05-31 07:30:04 +00:00
fqdn, value, ttl := acme.DNS01Record(domainName, keyAuth)
2017-02-07 21:33:23 +00:00
2018-05-31 07:30:04 +00:00
authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers)
2017-02-07 21:33:23 +00:00
if err != nil {
return err
}
// fetch the domain details
domain, err := d.getDomain(authZone)
if err != nil {
return err
}
// create the TXT record
name := strings.Replace(fqdn, "."+authZone, "", 1)
record := &Record{Type: "TXT", Name: name, Value: value, TTL: ttl}
err = d.createRecord(domain, record)
2018-05-31 07:30:04 +00:00
return err
2017-02-07 21:33:23 +00:00
}
// CleanUp removes the TXT records matching the specified parameters
func (d *DNSProvider) CleanUp(domainName, token, keyAuth string) error {
2018-05-31 07:30:04 +00:00
fqdn, _, _ := acme.DNS01Record(domainName, keyAuth)
2017-02-07 21:33:23 +00:00
2018-05-31 07:30:04 +00:00
authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers)
2017-02-07 21:33:23 +00:00
if err != nil {
return err
}
// fetch the domain details
domain, err := d.getDomain(authZone)
if err != nil {
return err
}
// find matching records
name := strings.Replace(fqdn, "."+authZone, "", 1)
records, err := d.getRecords(domain, name, "TXT")
if err != nil {
return err
}
// delete records
for _, record := range *records {
err = d.deleteRecord(record)
if err != nil {
return err
}
}
return nil
}
func (d *DNSProvider) getDomain(authZone string) (*Domain, error) {
domainName := authZone[0 : len(authZone)-1]
resource := fmt.Sprintf("%s%s", "/dns/managed/name?domainname=", domainName)
2018-07-03 10:44:04 +00:00
resp, err := d.sendRequest(http.MethodGet, resource, nil)
2017-02-07 21:33:23 +00:00
if err != nil {
return nil, err
}
defer resp.Body.Close()
domain := &Domain{}
err = json.NewDecoder(resp.Body).Decode(&domain)
if err != nil {
return nil, err
}
return domain, nil
}
func (d *DNSProvider) getRecords(domain *Domain, recordName, recordType string) (*[]Record, error) {
resource := fmt.Sprintf("%s/%d/%s%s%s%s", "/dns/managed", domain.ID, "records?recordName=", recordName, "&type=", recordType)
2018-07-03 10:44:04 +00:00
resp, err := d.sendRequest(http.MethodGet, resource, nil)
2017-02-07 21:33:23 +00:00
if err != nil {
return nil, err
}
defer resp.Body.Close()
type recordsResponse struct {
Records *[]Record `json:"data"`
}
records := &recordsResponse{}
err = json.NewDecoder(resp.Body).Decode(&records)
if err != nil {
return nil, err
}
return records.Records, nil
}
func (d *DNSProvider) createRecord(domain *Domain, record *Record) error {
url := fmt.Sprintf("%s/%d/%s", "/dns/managed", domain.ID, "records")
2018-07-03 10:44:04 +00:00
resp, err := d.sendRequest(http.MethodPost, url, record)
2017-02-07 21:33:23 +00:00
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
func (d *DNSProvider) deleteRecord(record Record) error {
resource := fmt.Sprintf("%s/%d/%s/%d", "/dns/managed", record.SourceID, "records", record.ID)
2018-07-03 10:44:04 +00:00
resp, err := d.sendRequest(http.MethodDelete, resource, nil)
2017-02-07 21:33:23 +00:00
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
func (d *DNSProvider) sendRequest(method, resource string, payload interface{}) (*http.Response, error) {
url := fmt.Sprintf("%s%s", d.baseURL, resource)
body, err := json.Marshal(payload)
if err != nil {
return nil, err
}
timestamp := time.Now().UTC().Format(time.RFC1123)
signature := computeHMAC(timestamp, d.apiSecret)
req, err := http.NewRequest(method, url, bytes.NewReader(body))
if err != nil {
return nil, err
}
req.Header.Set("x-dnsme-apiKey", d.apiKey)
req.Header.Set("x-dnsme-requestDate", timestamp)
req.Header.Set("x-dnsme-hmac", signature)
req.Header.Set("accept", "application/json")
req.Header.Set("content-type", "application/json")
2018-07-03 10:44:04 +00:00
resp, err := d.client.Do(req)
2017-02-07 21:33:23 +00:00
if err != nil {
return nil, err
}
if resp.StatusCode > 299 {
return nil, fmt.Errorf("DNSMadeEasy API request failed with HTTP status code %d", resp.StatusCode)
}
return resp, nil
}
func computeHMAC(message string, secret string) string {
key := []byte(secret)
h := hmac.New(sha1.New, key)
h.Write([]byte(message))
return hex.EncodeToString(h.Sum(nil))
}