2018-09-15 17:07:24 +00:00
|
|
|
package digitalocean
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2018-09-15 17:07:24 +00:00
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
2018-12-06 21:50:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const defaultBaseURL = "https://api.digitalocean.com"
|
2018-09-15 17:07:24 +00:00
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// txtRecordResponse represents a response from DO's API after making a TXT record.
|
2018-09-15 17:07:24 +00:00
|
|
|
type txtRecordResponse struct {
|
2018-12-06 21:50:17 +00:00
|
|
|
DomainRecord record `json:"domain_record"`
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
type record struct {
|
|
|
|
ID int `json:"id,omitempty"`
|
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Data string `json:"data,omitempty"`
|
|
|
|
TTL int `json:"ttl,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type apiError struct {
|
2018-09-15 17:07:24 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
func (d *DNSProvider) removeTxtRecord(domain string, recordID int) error {
|
|
|
|
authZone, err := dns01.FindZoneByFqdn(dns01.ToFqdn(domain))
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("could not determine zone for domain %q: %w", domain, err)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reqURL := fmt.Sprintf("%s/v2/domains/%s/records/%d", d.config.BaseURL, dns01.UnFqdn(authZone), recordID)
|
|
|
|
req, err := d.newRequest(http.MethodDelete, reqURL, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := d.config.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
return readError(req, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-14 13:20:49 +00:00
|
|
|
func (d *DNSProvider) addTxtRecord(fqdn, value string) (*txtRecordResponse, error) {
|
|
|
|
authZone, err := dns01.FindZoneByFqdn(dns01.ToFqdn(fqdn))
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("could not determine zone for domain %q: %w", fqdn, err)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reqData := record{Type: "TXT", Name: fqdn, Data: value, TTL: d.config.TTL}
|
|
|
|
body, err := json.Marshal(reqData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
reqURL := fmt.Sprintf("%s/v2/domains/%s/records", d.config.BaseURL, dns01.UnFqdn(authZone))
|
|
|
|
req, err := d.newRequest(http.MethodPost, reqURL, bytes.NewReader(body))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := d.config.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
return nil, readError(req, resp)
|
|
|
|
}
|
|
|
|
|
2021-08-25 09:44:11 +00:00
|
|
|
content, err := io.ReadAll(resp.Body)
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New(toUnreadableBodyMessage(req, content))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Everything looks good; but we'll need the ID later to delete the record
|
|
|
|
respData := &txtRecordResponse{}
|
|
|
|
err = json.Unmarshal(content, respData)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s", err, toUnreadableBodyMessage(req, content))
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return respData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DNSProvider) newRequest(method, reqURL string, body io.Reader) (*http.Request, error) {
|
|
|
|
req, err := http.NewRequest(method, reqURL, body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", d.config.AuthToken))
|
|
|
|
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readError(req *http.Request, resp *http.Response) error {
|
2021-08-25 09:44:11 +00:00
|
|
|
content, err := io.ReadAll(resp.Body)
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.New(toUnreadableBodyMessage(req, content))
|
|
|
|
}
|
|
|
|
|
|
|
|
var errInfo apiError
|
|
|
|
err = json.Unmarshal(content, &errInfo)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("apiError unmarshaling error: %w: %s", err, toUnreadableBodyMessage(req, content))
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("HTTP %d: %s: %s", resp.StatusCode, errInfo.ID, errInfo.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func toUnreadableBodyMessage(req *http.Request, rawBody []byte) string {
|
|
|
|
return fmt.Sprintf("the request %s sent a response with a body which is an invalid format: %q", req.URL, string(rawBody))
|
|
|
|
}
|