lego/providers/dns/dnsmadeeasy/internal/client.go

205 lines
4.5 KiB
Go
Raw Normal View History

2019-03-11 16:56:48 +00:00
package internal
import (
"bytes"
2023-05-05 07:49:38 +00:00
"context"
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"encoding/json"
2020-02-27 18:14:46 +00:00
"errors"
"fmt"
2021-08-25 09:44:11 +00:00
"io"
"net/http"
2023-05-05 07:49:38 +00:00
"net/url"
"strconv"
"time"
2023-05-05 07:49:38 +00:00
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
)
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"
)
2020-05-08 17:35:25 +00:00
// Client DNSMadeEasy client.
type Client struct {
2023-05-05 07:49:38 +00:00
apiKey string
apiSecret string
BaseURL *url.URL
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) {
if apiKey == "" {
2020-02-27 18:14:46 +00:00
return nil, errors.New("credentials missing: API key")
}
if apiSecret == "" {
2020-02-27 18:14:46 +00:00
return nil, errors.New("credentials missing: API secret")
}
2023-05-05 07:49:38 +00:00
baseURL, _ := url.Parse(DefaultProdBaseURL)
return &Client{
apiKey: apiKey,
apiSecret: apiSecret,
2023-05-05 07:49:38 +00:00
BaseURL: baseURL,
HTTPClient: &http.Client{Timeout: 5 * time.Second},
}, 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")
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)
if err != nil {
return nil, err
}
domain := &Domain{}
2023-05-05 07:49:38 +00:00
err = c.do(req, domain)
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-05-05 07:49:38 +00:00
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
records := &recordsResponse{}
2023-05-05 07:49:38 +00:00
err = c.do(req, records)
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")
2023-05-05 07:49:38 +00:00
req, err := newJSONRequest(ctx, http.MethodPost, endpoint, record)
if err != nil {
return err
}
2023-05-05 07:49:38 +00:00
return c.do(req, nil)
}
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 {
endpoint := c.BaseURL.JoinPath("/dns/managed", strconv.Itoa(record.SourceID), "records", strconv.Itoa(record.ID))
2023-05-05 07:49:38 +00:00
req, err := newJSONRequest(ctx, http.MethodDelete, endpoint, nil)
if err != nil {
return err
}
2023-05-05 07:49:38 +00:00
return c.do(req, nil)
}
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))
if err != nil {
2023-05-05 07:49:38 +00:00
return err
}
2023-05-05 07:49:38 +00:00
resp, err := c.HTTPClient.Do(req)
if err != nil {
2023-05-05 07:49:38 +00:00
return errutils.NewHTTPDoError(req, err)
}
2023-05-05 07:49:38 +00:00
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode/100 != 2 {
return errutils.NewUnexpectedResponseStatusCodeError(req, resp)
}
2023-05-05 07:49:38 +00:00
if result == nil {
return nil
}
raw, err := io.ReadAll(resp.Body)
if err != nil {
2023-05-05 07:49:38 +00:00
return errutils.NewReadResponseError(req, resp.StatusCode, err)
}
2023-05-05 07:49:38 +00:00
if err = json.Unmarshal(raw, result); err != nil {
return errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
}
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
}
2020-07-09 23:48:18 +00:00
func computeHMAC(message, secret string) (string, error) {
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
}