2016-03-17 22:59:15 +02:00
|
|
|
// Package digitalocean implements a DNS provider for solving the DNS-01
|
|
|
|
// challenge using digitalocean DNS.
|
2016-02-29 04:48:41 +02:00
|
|
|
package digitalocean
|
2016-01-27 02:57:55 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
2016-03-19 18:47:26 +02:00
|
|
|
"time"
|
2016-02-29 04:48:41 +02:00
|
|
|
|
|
|
|
"github.com/xenolf/lego/acme"
|
2018-06-11 17:32:50 +02:00
|
|
|
"github.com/xenolf/lego/platform/config/env"
|
2016-01-27 02:57:55 +02:00
|
|
|
)
|
|
|
|
|
2016-03-11 04:46:09 +02:00
|
|
|
// DNSProvider is an implementation of the acme.ChallengeProvider interface
|
2016-01-27 02:57:55 +02:00
|
|
|
// that uses DigitalOcean's REST API to manage TXT records for a domain.
|
2016-03-11 04:46:09 +02:00
|
|
|
type DNSProvider struct {
|
2016-01-27 02:57:55 +02:00
|
|
|
apiAuthToken string
|
|
|
|
recordIDs map[string]int
|
|
|
|
recordIDsMu sync.Mutex
|
|
|
|
}
|
|
|
|
|
2018-05-30 19:53:04 +02:00
|
|
|
// Timeout returns the timeout and interval to use when checking for DNS
|
|
|
|
// propagation. Adjusting here to cope with spikes in propagation times.
|
|
|
|
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
|
|
|
return 60 * time.Second, 5 * time.Second
|
|
|
|
}
|
|
|
|
|
2016-03-17 22:59:15 +02:00
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for Digital
|
|
|
|
// Ocean. Credentials must be passed in the environment variable:
|
|
|
|
// DO_AUTH_TOKEN.
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2018-06-11 17:32:50 +02:00
|
|
|
values, err := env.Get("DO_AUTH_TOKEN")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("DigitalOcean: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewDNSProviderCredentials(values["DO_AUTH_TOKEN"])
|
2016-03-17 22:59:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProviderCredentials uses the supplied credentials to return a
|
|
|
|
// DNSProvider instance configured for Digital Ocean.
|
|
|
|
func NewDNSProviderCredentials(apiAuthToken string) (*DNSProvider, error) {
|
|
|
|
if apiAuthToken == "" {
|
|
|
|
return nil, fmt.Errorf("DigitalOcean credentials missing")
|
|
|
|
}
|
2016-03-11 04:46:09 +02:00
|
|
|
return &DNSProvider{
|
2016-01-27 02:57:55 +02:00
|
|
|
apiAuthToken: apiAuthToken,
|
|
|
|
recordIDs: make(map[string]int),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Present creates a TXT record using the specified parameters
|
2016-03-11 04:46:09 +02:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2016-02-29 04:48:41 +02:00
|
|
|
fqdn, value, _ := acme.DNS01Record(domain, keyAuth)
|
2016-01-27 02:57:55 +02:00
|
|
|
|
2016-04-12 00:59:59 +02:00
|
|
|
authZone, err := acme.FindZoneByFqdn(acme.ToFqdn(domain), acme.RecursiveNameservers)
|
2016-03-27 03:19:49 +02:00
|
|
|
if err != nil {
|
2018-05-30 19:53:04 +02:00
|
|
|
return fmt.Errorf("could not determine zone for domain: '%s'. %s", domain, err)
|
2016-04-08 01:21:01 +02:00
|
|
|
}
|
2016-03-27 03:19:49 +02:00
|
|
|
|
|
|
|
authZone = acme.UnFqdn(authZone)
|
|
|
|
|
|
|
|
reqURL := fmt.Sprintf("%s/v2/domains/%s/records", digitalOceanBaseURL, authZone)
|
2018-05-30 19:53:04 +02:00
|
|
|
reqData := txtRecordRequest{RecordType: "TXT", Name: fqdn, Data: value, TTL: 30}
|
2016-01-27 02:57:55 +02:00
|
|
|
body, err := json.Marshal(reqData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", reqURL, bytes.NewReader(body))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", d.apiAuthToken))
|
|
|
|
|
2016-03-19 18:47:26 +02:00
|
|
|
client := http.Client{Timeout: 30 * time.Second}
|
|
|
|
resp, err := client.Do(req)
|
2016-01-27 02:57:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
var errInfo digitalOceanAPIError
|
|
|
|
json.NewDecoder(resp.Body).Decode(&errInfo)
|
|
|
|
return fmt.Errorf("HTTP %d: %s: %s", resp.StatusCode, errInfo.ID, errInfo.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Everything looks good; but we'll need the ID later to delete the record
|
|
|
|
var respData txtRecordResponse
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&respData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.recordIDsMu.Lock()
|
|
|
|
d.recordIDs[fqdn] = respData.DomainRecord.ID
|
|
|
|
d.recordIDsMu.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters
|
2016-03-11 04:46:09 +02:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2016-02-29 04:48:41 +02:00
|
|
|
fqdn, _, _ := acme.DNS01Record(domain, keyAuth)
|
2016-01-27 02:57:55 +02:00
|
|
|
|
|
|
|
// get the record's unique ID from when we created it
|
|
|
|
d.recordIDsMu.Lock()
|
|
|
|
recordID, ok := d.recordIDs[fqdn]
|
|
|
|
d.recordIDsMu.Unlock()
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unknown record ID for '%s'", fqdn)
|
|
|
|
}
|
|
|
|
|
2016-04-12 00:59:59 +02:00
|
|
|
authZone, err := acme.FindZoneByFqdn(acme.ToFqdn(domain), acme.RecursiveNameservers)
|
2016-03-27 03:19:49 +02:00
|
|
|
if err != nil {
|
2018-05-30 19:53:04 +02:00
|
|
|
return fmt.Errorf("could not determine zone for domain: '%s'. %s", domain, err)
|
2016-04-08 01:21:01 +02:00
|
|
|
}
|
2016-03-27 03:19:49 +02:00
|
|
|
|
|
|
|
authZone = acme.UnFqdn(authZone)
|
|
|
|
|
|
|
|
reqURL := fmt.Sprintf("%s/v2/domains/%s/records/%d", digitalOceanBaseURL, authZone, recordID)
|
2016-01-27 02:57:55 +02:00
|
|
|
req, err := http.NewRequest("DELETE", reqURL, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", d.apiAuthToken))
|
|
|
|
|
2016-03-19 18:47:26 +02:00
|
|
|
client := http.Client{Timeout: 30 * time.Second}
|
|
|
|
resp, err := client.Do(req)
|
2016-01-27 02:57:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
var errInfo digitalOceanAPIError
|
|
|
|
json.NewDecoder(resp.Body).Decode(&errInfo)
|
|
|
|
return fmt.Errorf("HTTP %d: %s: %s", resp.StatusCode, errInfo.ID, errInfo.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete record ID from map
|
|
|
|
d.recordIDsMu.Lock()
|
|
|
|
delete(d.recordIDs, fqdn)
|
|
|
|
d.recordIDsMu.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type digitalOceanAPIError struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var digitalOceanBaseURL = "https://api.digitalocean.com"
|
2018-05-30 19:53:04 +02:00
|
|
|
|
|
|
|
// txtRecordRequest represents the request body to DO's API to make a TXT record
|
|
|
|
type txtRecordRequest struct {
|
|
|
|
RecordType string `json:"type"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Data string `json:"data"`
|
|
|
|
TTL int `json:"ttl"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// txtRecordResponse represents a response from DO's API after making a TXT record
|
|
|
|
type txtRecordResponse struct {
|
|
|
|
DomainRecord struct {
|
|
|
|
ID int `json:"id"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Data string `json:"data"`
|
|
|
|
} `json:"domain_record"`
|
|
|
|
}
|