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

222 lines
4.9 KiB
Go
Raw Permalink Normal View History

2019-03-11 16:56:48 +00:00
package internal
2018-09-15 17:26:45 +00:00
import (
"bytes"
2023-05-05 07:49:38 +00:00
"context"
2018-09-15 17:26:45 +00:00
"crypto/md5"
"encoding/hex"
"encoding/json"
2020-02-27 18:14:46 +00:00
"errors"
2018-09-15 17:26:45 +00:00
"fmt"
2021-08-25 09:44:11 +00:00
"io"
2018-09-15 17:26:45 +00:00
"net/http"
2023-05-05 07:49:38 +00:00
"net/url"
2018-09-15 17:26:45 +00:00
"strconv"
"time"
2020-09-02 01:20:01 +00:00
"github.com/go-acme/lego/v4/challenge/dns01"
2023-05-05 07:49:38 +00:00
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
2018-09-15 17:26:45 +00:00
)
const defaultBaseURL = "https://www.cloudxns.net/api2/"
2023-05-05 07:49:38 +00:00
// Client CloudXNS client.
type Client struct {
apiKey string
secretKey string
2018-09-15 17:26:45 +00:00
2023-05-05 07:49:38 +00:00
baseURL *url.URL
HTTPClient *http.Client
2018-09-15 17:26:45 +00:00
}
2020-05-08 17:35:25 +00:00
// NewClient creates a CloudXNS client.
2020-07-09 23:48:18 +00:00
func NewClient(apiKey, secretKey string) (*Client, error) {
2018-09-15 17:26:45 +00:00
if apiKey == "" {
2023-05-05 07:49:38 +00:00
return nil, errors.New("credentials missing: apiKey")
2018-09-15 17:26:45 +00:00
}
if secretKey == "" {
2023-05-05 07:49:38 +00:00
return nil, errors.New("credentials missing: secretKey")
2018-09-15 17:26:45 +00:00
}
2023-05-05 07:49:38 +00:00
baseURL, _ := url.Parse(defaultBaseURL)
2018-09-15 17:26:45 +00:00
return &Client{
apiKey: apiKey,
secretKey: secretKey,
2023-05-05 07:49:38 +00:00
baseURL: baseURL,
HTTPClient: &http.Client{Timeout: 10 * time.Second},
2018-09-15 17:26:45 +00:00
}, nil
}
2020-05-08 17:35:25 +00:00
// GetDomainInformation Get domain name information for a FQDN.
2023-05-05 07:49:38 +00:00
func (c *Client) GetDomainInformation(ctx context.Context, fqdn string) (*Data, error) {
endpoint := c.baseURL.JoinPath("domain")
req, err := c.newRequest(ctx, http.MethodGet, endpoint, nil)
2018-09-15 17:26:45 +00:00
if err != nil {
return nil, err
}
2023-05-05 07:49:38 +00:00
authZone, err := dns01.FindZoneByFqdn(fqdn)
2018-09-15 17:26:45 +00:00
if err != nil {
return nil, fmt.Errorf("could not find zone: %w", err)
2018-09-15 17:26:45 +00:00
}
var domains []Data
2023-05-05 07:49:38 +00:00
err = c.do(req, &domains)
if err != nil {
return nil, err
2018-09-15 17:26:45 +00:00
}
for _, data := range domains {
if data.Domain == authZone {
return &data, nil
}
}
2023-05-05 07:49:38 +00:00
return nil, fmt.Errorf("zone %s not found for domain %s", authZone, fqdn)
2018-09-15 17:26:45 +00:00
}
2020-05-08 17:35:25 +00:00
// FindTxtRecord return the TXT record a zone ID and a FQDN.
2023-05-05 07:49:38 +00:00
func (c *Client) FindTxtRecord(ctx context.Context, zoneID, fqdn string) (*TXTRecord, error) {
endpoint := c.baseURL.JoinPath("record", zoneID)
query := endpoint.Query()
query.Set("host_id", "0")
query.Set("offset", "0")
query.Set("row_num", "2000")
endpoint.RawQuery = query.Encode()
req, err := c.newRequest(ctx, http.MethodGet, endpoint, nil)
2018-09-15 17:26:45 +00:00
if err != nil {
return nil, err
}
var records []TXTRecord
2023-05-05 07:49:38 +00:00
err = c.do(req, &records)
2018-09-15 17:26:45 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return nil, err
2018-09-15 17:26:45 +00:00
}
for _, record := range records {
if record.Host == dns01.UnFqdn(fqdn) && record.Type == "TXT" {
2018-09-15 17:26:45 +00:00
return &record, nil
}
}
2023-05-05 07:49:38 +00:00
return nil, fmt.Errorf("no existing record found for %q", fqdn)
2018-09-15 17:26:45 +00:00
}
2020-05-08 17:35:25 +00:00
// AddTxtRecord add a TXT record.
2023-05-05 07:49:38 +00:00
func (c *Client) AddTxtRecord(ctx context.Context, info *Data, fqdn, value string, ttl int) error {
2018-09-15 17:26:45 +00:00
id, err := strconv.Atoi(info.ID)
if err != nil {
2023-05-05 07:49:38 +00:00
return fmt.Errorf("invalid zone ID: %w", err)
2018-09-15 17:26:45 +00:00
}
2023-05-05 07:49:38 +00:00
endpoint := c.baseURL.JoinPath("record")
subDomain, err := dns01.ExtractSubDomain(fqdn, info.Domain)
if err != nil {
2023-05-05 07:49:38 +00:00
return err
}
2023-05-05 07:49:38 +00:00
record := TXTRecord{
2018-09-15 17:26:45 +00:00
ID: id,
Host: subDomain,
2018-09-15 17:26:45 +00:00
Value: value,
Type: "TXT",
LineID: 1,
TTL: ttl,
}
2023-05-05 07:49:38 +00:00
req, err := c.newRequest(ctx, http.MethodPost, endpoint, record)
2018-09-15 17:26:45 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return err
2018-09-15 17:26:45 +00:00
}
2023-05-05 07:49:38 +00:00
return c.do(req, nil)
2018-09-15 17:26:45 +00:00
}
2020-05-08 17:35:25 +00:00
// RemoveTxtRecord remove a TXT record.
2023-05-05 07:49:38 +00:00
func (c *Client) RemoveTxtRecord(ctx context.Context, recordID, zoneID string) error {
endpoint := c.baseURL.JoinPath("record", recordID, zoneID)
2018-09-15 17:26:45 +00:00
2023-05-05 07:49:38 +00:00
req, err := c.newRequest(ctx, http.MethodDelete, endpoint, nil)
2018-09-15 17:26:45 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return err
2018-09-15 17:26:45 +00:00
}
2023-05-05 07:49:38 +00:00
return c.do(req, nil)
}
func (c *Client) do(req *http.Request, result any) error {
2018-09-15 17:26:45 +00:00
resp, err := c.HTTPClient.Do(req)
if err != nil {
2023-05-05 07:49:38 +00:00
return errutils.NewHTTPDoError(req, err)
2018-09-15 17:26:45 +00:00
}
2023-05-05 07:49:38 +00:00
defer func() { _ = resp.Body.Close() }()
2018-09-15 17:26:45 +00:00
2023-05-05 07:49:38 +00:00
raw, err := io.ReadAll(resp.Body)
2018-09-15 17:26:45 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return errutils.NewReadResponseError(req, resp.StatusCode, err)
2018-09-15 17:26:45 +00:00
}
2023-05-05 07:49:38 +00:00
var response apiResponse
err = json.Unmarshal(raw, &response)
2018-09-15 17:26:45 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
}
if response.Code != 1 {
return fmt.Errorf("[status code %d] invalid code (%v) error: %s", resp.StatusCode, response.Code, response.Message)
}
if result == nil {
return nil
2018-09-15 17:26:45 +00:00
}
2023-05-05 07:49:38 +00:00
if len(response.Data) == 0 {
return nil
2018-09-15 17:26:45 +00:00
}
2023-05-05 07:49:38 +00:00
err = json.Unmarshal(response.Data, result)
if err != nil {
return errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
}
return nil
2018-09-15 17:26:45 +00:00
}
2023-05-05 07:49:38 +00:00
func (c *Client) newRequest(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)
}
}
2018-09-15 17:26:45 +00:00
2023-05-05 07:49:38 +00:00
req, err := http.NewRequestWithContext(ctx, method, endpoint.String(), buf)
2018-09-15 17:26:45 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return nil, fmt.Errorf("unable to create request: %w", err)
2018-09-15 17:26:45 +00:00
}
requestDate := time.Now().Format(time.RFC1123Z)
req.Header.Set("API-KEY", c.apiKey)
req.Header.Set("API-REQUEST-DATE", requestDate)
2023-05-05 07:49:38 +00:00
req.Header.Set("API-HMAC", c.hmac(endpoint.String(), requestDate, buf.String()))
2018-09-15 17:26:45 +00:00
req.Header.Set("API-FORMAT", "json")
return req, nil
}
2023-05-05 07:49:38 +00:00
func (c *Client) hmac(endpoint, date, body string) string {
sum := md5.Sum([]byte(c.apiKey + endpoint + body + date + c.secretKey))
2018-09-15 17:26:45 +00:00
return hex.EncodeToString(sum[:])
}