lego/providers/dns/gandiv5/client.go

201 lines
4.4 KiB
Go
Raw Normal View History

package gandiv5
2018-10-08 14:51:29 +00:00
import (
"bytes"
"encoding/json"
2020-02-27 18:14:46 +00:00
"errors"
2018-10-08 14:51:29 +00:00
"fmt"
"io/ioutil"
"net/http"
2020-09-02 01:20:01 +00:00
"github.com/go-acme/lego/v4/log"
2018-10-08 14:51:29 +00:00
)
2018-10-08 14:51:29 +00:00
const apiKeyHeader = "X-Api-Key"
2020-05-08 17:35:25 +00:00
// types for JSON responses with only a message.
2018-10-08 14:51:29 +00:00
type apiResponse struct {
Message string `json:"message"`
UUID string `json:"uuid,omitempty"`
}
2020-05-08 17:35:25 +00:00
// Record TXT record representation.
2018-10-08 14:51:29 +00:00
type Record struct {
RRSetTTL int `json:"rrset_ttl"`
RRSetValues []string `json:"rrset_values"`
2018-10-08 14:51:29 +00:00
RRSetName string `json:"rrset_name,omitempty"`
RRSetType string `json:"rrset_type,omitempty"`
}
2020-07-09 23:48:18 +00:00
func (d *DNSProvider) addTXTRecord(domain, name, value string, ttl int) error {
// Get exiting values for the TXT records
// Needed to create challenges for both wildcard and base name domains
txtRecord, err := d.getTXTRecord(domain, name)
if err != nil {
return err
}
values := []string{value}
if len(txtRecord.RRSetValues) > 0 {
values = append(values, txtRecord.RRSetValues...)
}
target := fmt.Sprintf("domains/%s/records/%s/TXT", domain, name)
newRecord := &Record{RRSetTTL: ttl, RRSetValues: values}
req, err := d.newRequest(http.MethodPut, target, newRecord)
if err != nil {
return err
}
message := apiResponse{}
err = d.do(req, &message)
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("unable to create TXT record for domain %s and name %s: %w", domain, name, err)
}
if len(message.Message) > 0 {
log.Infof("API response: %s", message.Message)
}
return nil
}
func (d *DNSProvider) getTXTRecord(domain, name string) (*Record, error) {
target := fmt.Sprintf("domains/%s/records/%s/TXT", domain, name)
// Get exiting values for the TXT records
// Needed to create challenges for both wildcard and base name domains
req, err := d.newRequest(http.MethodGet, target, nil)
if err != nil {
return nil, err
}
txtRecord := &Record{}
err = d.do(req, txtRecord)
if err != nil {
2020-02-27 18:14:46 +00:00
return nil, fmt.Errorf("unable to get TXT records for domain %s and name %s: %w", domain, name, err)
}
return txtRecord, nil
}
2020-07-09 23:48:18 +00:00
func (d *DNSProvider) deleteTXTRecord(domain, name string) error {
target := fmt.Sprintf("domains/%s/records/%s/TXT", domain, name)
req, err := d.newRequest(http.MethodDelete, target, nil)
if err != nil {
return err
}
message := apiResponse{}
err = d.do(req, &message)
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("unable to delete TXT record for domain %s and name %s: %w", domain, name, err)
}
if len(message.Message) > 0 {
log.Infof("API response: %s", message.Message)
}
return nil
}
2018-10-08 14:51:29 +00:00
func (d *DNSProvider) newRequest(method, resource string, body interface{}) (*http.Request, error) {
u := fmt.Sprintf("%s/%s", d.config.BaseURL, resource)
if body == nil {
req, err := http.NewRequest(method, u, nil)
if err != nil {
return nil, err
}
return req, nil
}
reqBody, err := json.Marshal(body)
if err != nil {
return nil, err
}
req, err := http.NewRequest(method, u, bytes.NewBuffer(reqBody))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
return req, nil
}
2018-10-08 14:51:29 +00:00
func (d *DNSProvider) do(req *http.Request, v interface{}) error {
if len(d.config.APIKey) > 0 {
req.Header.Set(apiKeyHeader, d.config.APIKey)
}
2018-10-08 14:51:29 +00:00
resp, err := d.config.HTTPClient.Do(req)
if err != nil {
return err
}
err = checkResponse(resp)
if err != nil {
return err
}
if v == nil {
return nil
}
raw, err := readBody(resp)
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("failed to read body: %w", err)
2018-10-08 14:51:29 +00:00
}
if len(raw) > 0 {
err = json.Unmarshal(raw, v)
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("unmarshaling error: %w: %s", err, string(raw))
2018-10-08 14:51:29 +00:00
}
}
return nil
}
func checkResponse(resp *http.Response) error {
if resp.StatusCode == 404 && resp.Request.Method == http.MethodGet {
return nil
}
if resp.StatusCode >= 400 {
data, err := readBody(resp)
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("%d [%s] request failed: %w", resp.StatusCode, http.StatusText(resp.StatusCode), err)
2018-10-08 14:51:29 +00:00
}
message := &apiResponse{}
err = json.Unmarshal(data, message)
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("%d [%s] request failed: %w: %s", resp.StatusCode, http.StatusText(resp.StatusCode), err, data)
2018-10-08 14:51:29 +00:00
}
return fmt.Errorf("%d [%s] request failed: %s", resp.StatusCode, http.StatusText(resp.StatusCode), message.Message)
}
return nil
}
func readBody(resp *http.Response) ([]byte, error) {
if resp.Body == nil {
2020-02-27 18:14:46 +00:00
return nil, errors.New("response body is nil")
2018-10-08 14:51:29 +00:00
}
defer resp.Body.Close()
rawBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return rawBody, nil
}