2019-03-11 16:56:48 +00:00
|
|
|
package internal
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-05-05 07:49:38 +00:00
|
|
|
"context"
|
2018-09-15 17:07:24 +00:00
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2020-02-27 18:14:46 +00:00
|
|
|
"errors"
|
2018-09-15 17:07:24 +00:00
|
|
|
"fmt"
|
2021-08-25 09:44:11 +00:00
|
|
|
"io"
|
2018-09-15 17:07:24 +00:00
|
|
|
"net/http"
|
2023-05-05 07:49:38 +00:00
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2018-09-15 17:07:24 +00:00
|
|
|
"time"
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
|
|
|
|
)
|
2018-09-15 17:07:24 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
// Default API endpoints.
|
|
|
|
const (
|
|
|
|
DefaultSandboxBaseURL = "https://api.sandbox.dnsmadeeasy.com/V2.0"
|
|
|
|
DefaultProdBaseURL = "https://api.dnsmadeeasy.com/V2.0"
|
|
|
|
)
|
2018-10-19 09:49:17 +00:00
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Client DNSMadeEasy client.
|
2018-09-15 17:07:24 +00:00
|
|
|
type Client struct {
|
2023-05-05 07:49:38 +00:00
|
|
|
apiKey string
|
|
|
|
apiSecret string
|
|
|
|
|
|
|
|
BaseURL *url.URL
|
2018-09-15 17:07:24 +00:00
|
|
|
HTTPClient *http.Client
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// NewClient creates a DNSMadeEasy client.
|
2020-07-09 23:48:18 +00:00
|
|
|
func NewClient(apiKey, apiSecret string) (*Client, error) {
|
2018-09-15 17:07:24 +00:00
|
|
|
if apiKey == "" {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, errors.New("credentials missing: API key")
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if apiSecret == "" {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, errors.New("credentials missing: API secret")
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
baseURL, _ := url.Parse(DefaultProdBaseURL)
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
return &Client{
|
|
|
|
apiKey: apiKey,
|
|
|
|
apiSecret: apiSecret,
|
2023-05-05 07:49:38 +00:00
|
|
|
BaseURL: baseURL,
|
|
|
|
HTTPClient: &http.Client{Timeout: 5 * time.Second},
|
2018-09-15 17:07:24 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// GetDomain gets a domain.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) GetDomain(ctx context.Context, authZone string) (*Domain, error) {
|
|
|
|
endpoint := c.BaseURL.JoinPath("dns", "managed", "name")
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
domainName := authZone[0 : len(authZone)-1]
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
query := endpoint.Query()
|
|
|
|
query.Set("domainname", domainName)
|
|
|
|
endpoint.RawQuery = query.Encode()
|
|
|
|
|
|
|
|
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil)
|
2018-09-15 17:07:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
domain := &Domain{}
|
2023-05-05 07:49:38 +00:00
|
|
|
err = c.do(req, domain)
|
2018-09-15 17:07:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return domain, nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// GetRecords gets all TXT records.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) GetRecords(ctx context.Context, domain *Domain, recordName, recordType string) (*[]Record, error) {
|
|
|
|
endpoint := c.BaseURL.JoinPath("dns", "managed", strconv.Itoa(domain.ID), "records")
|
|
|
|
|
|
|
|
query := endpoint.Query()
|
|
|
|
query.Set("recordName", recordName)
|
|
|
|
query.Set("type", recordType)
|
2023-06-19 15:28:12 +00:00
|
|
|
endpoint.RawQuery = query.Encode()
|
2018-09-15 17:07:24 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil)
|
2018-09-15 17:07:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
records := &recordsResponse{}
|
2023-05-05 07:49:38 +00:00
|
|
|
err = c.do(req, records)
|
2018-09-15 17:07:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return records.Records, nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// CreateRecord creates a TXT records.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) CreateRecord(ctx context.Context, domain *Domain, record *Record) error {
|
|
|
|
endpoint := c.BaseURL.JoinPath("dns", "managed", strconv.Itoa(domain.ID), "records")
|
2018-09-15 17:07:24 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := newJSONRequest(ctx, http.MethodPost, endpoint, record)
|
2018-09-15 17:07:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return c.do(req, nil)
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// DeleteRecord deletes a TXT records.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) DeleteRecord(ctx context.Context, record Record) error {
|
2023-06-19 15:28:12 +00:00
|
|
|
endpoint := c.BaseURL.JoinPath("dns", "managed", strconv.Itoa(record.SourceID), "records", strconv.Itoa(record.ID))
|
2018-09-15 17:07:24 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := newJSONRequest(ctx, http.MethodDelete, endpoint, nil)
|
2018-09-15 17:07:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return c.do(req, nil)
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) do(req *http.Request, result any) error {
|
|
|
|
err := c.sign(req, time.Now().UTC().Format(time.RFC1123))
|
2018-09-15 17:07:24 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return err
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
resp, err := c.HTTPClient.Do(req)
|
2018-09-15 17:07:24 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewHTTPDoError(req, err)
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
|
|
|
if resp.StatusCode/100 != 2 {
|
|
|
|
return errutils.NewUnexpectedResponseStatusCodeError(req, resp)
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
if result == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
raw, err := io.ReadAll(resp.Body)
|
2018-09-15 17:07:24 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewReadResponseError(req, resp.StatusCode, err)
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
if err = json.Unmarshal(raw, result); err != nil {
|
|
|
|
return errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) sign(req *http.Request, timestamp string) error {
|
|
|
|
signature, err := computeHMAC(timestamp, c.apiSecret)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("x-dnsme-apiKey", c.apiKey)
|
|
|
|
req.Header.Set("x-dnsme-requestDate", timestamp)
|
|
|
|
req.Header.Set("x-dnsme-hmac", signature)
|
|
|
|
|
|
|
|
return nil
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 23:48:18 +00:00
|
|
|
func computeHMAC(message, secret string) (string, error) {
|
2018-09-15 17:07:24 +00:00
|
|
|
key := []byte(secret)
|
|
|
|
h := hmac.New(sha1.New, key)
|
|
|
|
_, err := h.Write([]byte(message))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return hex.EncodeToString(h.Sum(nil)), nil
|
|
|
|
}
|
2023-05-05 07:49:38 +00:00
|
|
|
|
|
|
|
func newJSONRequest(ctx context.Context, method string, endpoint *url.URL, payload any) (*http.Request, error) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
if payload != nil {
|
|
|
|
err := json.NewEncoder(buf).Encode(payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create request JSON body: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, method, endpoint.String(), buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to create request: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
|
|
|
|
if payload != nil {
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
}
|
|
|
|
|
|
|
|
return req, nil
|
|
|
|
}
|