2016-03-17 20:59:15 +00:00
|
|
|
// Package cloudflare implements a DNS provider for solving the DNS-01
|
|
|
|
// challenge using cloudflare DNS.
|
2016-02-29 02:48:41 +00:00
|
|
|
package cloudflare
|
2015-12-03 20:01:46 +00:00
|
|
|
|
|
|
|
import (
|
2016-02-16 14:50:24 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2015-12-03 20:01:46 +00:00
|
|
|
"fmt"
|
2016-02-16 14:50:24 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
2015-12-03 20:01:46 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2016-02-16 14:50:24 +00:00
|
|
|
"time"
|
2016-02-29 02:48:41 +00:00
|
|
|
|
|
|
|
"github.com/xenolf/lego/acme"
|
2015-12-03 20:01:46 +00:00
|
|
|
)
|
|
|
|
|
2016-02-15 02:59:43 +00:00
|
|
|
// CloudFlareAPIURL represents the API endpoint to call.
|
|
|
|
// TODO: Unexport?
|
|
|
|
const CloudFlareAPIURL = "https://api.cloudflare.com/client/v4"
|
2016-02-16 14:50:24 +00:00
|
|
|
|
2016-03-11 02:46:09 +00:00
|
|
|
// DNSProvider is an implementation of the acme.ChallengeProvider interface
|
|
|
|
type DNSProvider struct {
|
2016-02-16 14:50:24 +00:00
|
|
|
authEmail string
|
|
|
|
authKey string
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 20:59:15 +00:00
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for cloudflare.
|
|
|
|
// Credentials must be passed in the environment variables: CLOUDFLARE_EMAIL
|
|
|
|
// and CLOUDFLARE_API_KEY.
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
|
|
|
email := os.Getenv("CLOUDFLARE_EMAIL")
|
|
|
|
key := os.Getenv("CLOUDFLARE_API_KEY")
|
|
|
|
return NewDNSProviderCredentials(email, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProviderCredentials uses the supplied credentials to return a
|
|
|
|
// DNSProvider instance configured for cloudflare.
|
|
|
|
func NewDNSProviderCredentials(email, key string) (*DNSProvider, error) {
|
|
|
|
if email == "" || key == "" {
|
|
|
|
return nil, fmt.Errorf("CloudFlare credentials missing")
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2016-03-11 02:46:09 +00:00
|
|
|
return &DNSProvider{
|
2016-03-17 20:59:15 +00:00
|
|
|
authEmail: email,
|
|
|
|
authKey: key,
|
2016-02-16 14:50:24 +00:00
|
|
|
}, nil
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 22:55:15 +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 (c *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
|
|
|
return 120 * time.Second, 2 * time.Second
|
|
|
|
}
|
|
|
|
|
2016-01-15 04:06:25 +00:00
|
|
|
// Present creates a TXT record to fulfil the dns-01 challenge
|
2016-03-11 02:46:09 +00:00
|
|
|
func (c *DNSProvider) Present(domain, token, keyAuth string) error {
|
2016-02-29 02:48:41 +00:00
|
|
|
fqdn, value, _ := acme.DNS01Record(domain, keyAuth)
|
2015-12-03 20:01:46 +00:00
|
|
|
zoneID, err := c.getHostedZoneID(fqdn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-16 14:50:24 +00:00
|
|
|
rec := cloudFlareRecord{
|
|
|
|
Type: "TXT",
|
2016-03-11 02:20:25 +00:00
|
|
|
Name: acme.UnFqdn(fqdn),
|
2016-02-16 14:50:24 +00:00
|
|
|
Content: value,
|
|
|
|
TTL: 120,
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := json.Marshal(rec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.makeRequest("POST", fmt.Sprintf("/zones/%s/dns_records", zoneID), bytes.NewReader(body))
|
2015-12-03 20:01:46 +00:00
|
|
|
if err != nil {
|
2016-02-16 14:50:24 +00:00
|
|
|
return err
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-15 04:06:25 +00:00
|
|
|
// CleanUp removes the TXT record matching the specified parameters
|
2016-03-11 02:46:09 +00:00
|
|
|
func (c *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2016-02-29 02:48:41 +00:00
|
|
|
fqdn, _, _ := acme.DNS01Record(domain, keyAuth)
|
2016-02-16 14:50:24 +00:00
|
|
|
|
|
|
|
record, err := c.findTxtRecord(fqdn)
|
2015-12-03 20:01:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-16 14:50:24 +00:00
|
|
|
_, err = c.makeRequest("DELETE", fmt.Sprintf("/zones/%s/dns_records/%s", record.ZoneID, record.ID), nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
2016-02-16 14:50:24 +00:00
|
|
|
|
2015-12-03 20:01:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-11 02:46:09 +00:00
|
|
|
func (c *DNSProvider) getHostedZoneID(fqdn string) (string, error) {
|
2016-02-16 14:50:24 +00:00
|
|
|
// HostedZone represents a CloudFlare DNS zone
|
|
|
|
type HostedZone struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 14:50:24 +00:00
|
|
|
result, err := c.makeRequest("GET", "/zones?per_page=1000", nil)
|
2015-12-03 20:01:46 +00:00
|
|
|
if err != nil {
|
2016-02-16 14:50:24 +00:00
|
|
|
return "", err
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 14:50:24 +00:00
|
|
|
var zones []HostedZone
|
|
|
|
err = json.Unmarshal(result, &zones)
|
2015-12-03 20:01:46 +00:00
|
|
|
if err != nil {
|
2016-02-16 14:50:24 +00:00
|
|
|
return "", err
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 14:50:24 +00:00
|
|
|
var hostedZone HostedZone
|
2015-12-03 20:01:46 +00:00
|
|
|
for _, zone := range zones {
|
2016-03-11 02:20:25 +00:00
|
|
|
name := acme.ToFqdn(zone.Name)
|
2015-12-03 20:01:46 +00:00
|
|
|
if strings.HasSuffix(fqdn, name) {
|
|
|
|
if len(zone.Name) > len(hostedZone.Name) {
|
2016-02-16 14:50:24 +00:00
|
|
|
hostedZone = zone
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if hostedZone.ID == "" {
|
2016-02-16 14:50:24 +00:00
|
|
|
return "", fmt.Errorf("No matching CloudFlare zone found for %s", fqdn)
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return hostedZone.ID, nil
|
|
|
|
}
|
|
|
|
|
2016-03-11 02:46:09 +00:00
|
|
|
func (c *DNSProvider) findTxtRecord(fqdn string) (*cloudFlareRecord, error) {
|
2016-02-16 14:50:24 +00:00
|
|
|
zoneID, err := c.getHostedZoneID(fqdn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
2016-02-16 14:50:24 +00:00
|
|
|
|
|
|
|
result, err := c.makeRequest("GET", fmt.Sprintf("/zones/%s/dns_records?per_page=1000", zoneID), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var records []cloudFlareRecord
|
|
|
|
err = json.Unmarshal(result, &records)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rec := range records {
|
2016-03-11 02:20:25 +00:00
|
|
|
if rec.Name == acme.UnFqdn(fqdn) && rec.Type == "TXT" {
|
2016-02-16 14:50:24 +00:00
|
|
|
return &rec, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("No existing record found for %s", fqdn)
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2016-03-11 02:46:09 +00:00
|
|
|
func (c *DNSProvider) makeRequest(method, uri string, body io.Reader) (json.RawMessage, error) {
|
2016-02-15 02:59:43 +00:00
|
|
|
// APIError contains error details for failed requests
|
|
|
|
type APIError struct {
|
2016-02-16 14:50:24 +00:00
|
|
|
Code int `json:"code,omitempty"`
|
|
|
|
Message string `json:"message,omitempty"`
|
|
|
|
}
|
|
|
|
|
2016-02-15 02:59:43 +00:00
|
|
|
// APIResponse represents a response from CloudFlare API
|
|
|
|
type APIResponse struct {
|
2016-02-16 14:50:24 +00:00
|
|
|
Success bool `json:"success"`
|
2016-02-15 02:59:43 +00:00
|
|
|
Errors []*APIError `json:"errors"`
|
2016-02-16 14:50:24 +00:00
|
|
|
Result json.RawMessage `json:"result"`
|
|
|
|
}
|
|
|
|
|
2016-02-15 02:59:43 +00:00
|
|
|
req, err := http.NewRequest(method, fmt.Sprintf("%s%s", CloudFlareAPIURL, uri), body)
|
2016-02-16 14:50:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("X-Auth-Email", c.authEmail)
|
|
|
|
req.Header.Set("X-Auth-Key", c.authKey)
|
2016-02-29 02:48:41 +00:00
|
|
|
//req.Header.Set("User-Agent", userAgent())
|
2016-02-16 14:50:24 +00:00
|
|
|
|
2016-03-23 22:48:20 +00:00
|
|
|
client := http.Client{Timeout: 30 * time.Second}
|
2016-02-16 14:50:24 +00:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error querying API -> %v", err)
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
2016-02-16 14:50:24 +00:00
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2016-02-15 02:59:43 +00:00
|
|
|
var r APIResponse
|
2016-02-16 14:50:24 +00:00
|
|
|
err = json.NewDecoder(resp.Body).Decode(&r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !r.Success {
|
|
|
|
if len(r.Errors) > 0 {
|
|
|
|
return nil, fmt.Errorf("API error -> %d: %s", r.Errors[0].Code, r.Errors[0].Message)
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("API error")
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.Result, nil
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 14:50:24 +00:00
|
|
|
// cloudFlareRecord represents a CloudFlare DNS record
|
|
|
|
type cloudFlareRecord struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Content string `json:"content"`
|
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
TTL int `json:"ttl,omitempty"`
|
|
|
|
ZoneID string `json:"zone_id,omitempty"`
|
|
|
|
}
|