2021-07-22 10:05:22 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2023-05-05 07:49:38 +00:00
|
|
|
"bytes"
|
2021-07-22 10:05:22 +00:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-08-25 09:44:11 +00:00
|
|
|
"io"
|
2021-07-22 10:05:22 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
2023-05-05 07:49:38 +00:00
|
|
|
|
|
|
|
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
|
2021-07-22 10:05:22 +00:00
|
|
|
)
|
|
|
|
|
2023-07-25 12:42:54 +00:00
|
|
|
const defaultBaseURL = "https://api.gcore.com/dns"
|
2023-05-05 07:49:38 +00:00
|
|
|
|
2021-07-22 10:05:22 +00:00
|
|
|
const (
|
2023-05-05 07:49:38 +00:00
|
|
|
authorizationHeader = "Authorization"
|
|
|
|
tokenTypeHeader = "APIKey"
|
2021-07-22 10:05:22 +00:00
|
|
|
)
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
const txtRecordType = "TXT"
|
|
|
|
|
2021-07-22 10:05:22 +00:00
|
|
|
// Client for DNS API.
|
|
|
|
type Client struct {
|
2023-05-05 07:49:38 +00:00
|
|
|
token string
|
|
|
|
|
2021-07-22 10:05:22 +00:00
|
|
|
baseURL *url.URL
|
2023-05-05 07:49:38 +00:00
|
|
|
HTTPClient *http.Client
|
2021-07-22 10:05:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient constructor of Client.
|
|
|
|
func NewClient(token string) *Client {
|
|
|
|
baseURL, _ := url.Parse(defaultBaseURL)
|
|
|
|
|
|
|
|
return &Client{
|
|
|
|
token: token,
|
|
|
|
baseURL: baseURL,
|
|
|
|
HTTPClient: &http.Client{Timeout: 10 * time.Second},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetZone gets zone information.
|
2023-07-25 12:42:54 +00:00
|
|
|
// https://api.gcore.com/docs/dns#tag/zones/operation/Zone
|
2021-07-22 10:05:22 +00:00
|
|
|
func (c *Client) GetZone(ctx context.Context, name string) (Zone, error) {
|
2023-02-09 16:19:58 +00:00
|
|
|
endpoint := c.baseURL.JoinPath("v2", "zones", name)
|
2021-07-22 10:05:22 +00:00
|
|
|
|
2023-02-09 16:19:58 +00:00
|
|
|
zone := Zone{}
|
2023-05-05 07:49:38 +00:00
|
|
|
err := c.doRequest(ctx, http.MethodGet, endpoint, nil, &zone)
|
2021-07-22 10:05:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return Zone{}, fmt.Errorf("get zone %s: %w", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return zone, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRRSet gets RRSet item.
|
2023-07-25 12:42:54 +00:00
|
|
|
// https://api.gcore.com/docs/dns#tag/rrsets/operation/RRSet
|
2021-07-22 10:05:22 +00:00
|
|
|
func (c *Client) GetRRSet(ctx context.Context, zone, name string) (RRSet, error) {
|
2023-02-09 16:19:58 +00:00
|
|
|
endpoint := c.baseURL.JoinPath("v2", "zones", zone, name, txtRecordType)
|
2021-07-22 10:05:22 +00:00
|
|
|
|
2023-02-09 16:19:58 +00:00
|
|
|
var result RRSet
|
2023-05-05 07:49:38 +00:00
|
|
|
err := c.doRequest(ctx, http.MethodGet, endpoint, nil, &result)
|
2021-07-22 10:05:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return RRSet{}, fmt.Errorf("get txt records %s -> %s: %w", zone, name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteRRSet removes RRSet record.
|
2023-07-25 12:42:54 +00:00
|
|
|
// https://api.gcore.com/docs/dns#tag/rrsets/operation/DeleteRRSet
|
2021-07-22 10:05:22 +00:00
|
|
|
func (c *Client) DeleteRRSet(ctx context.Context, zone, name string) error {
|
2023-02-09 16:19:58 +00:00
|
|
|
endpoint := c.baseURL.JoinPath("v2", "zones", zone, name, txtRecordType)
|
2021-07-22 10:05:22 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err := c.doRequest(ctx, http.MethodDelete, endpoint, nil, nil)
|
2021-07-22 10:05:22 +00:00
|
|
|
if err != nil {
|
|
|
|
// Support DELETE idempotence https://developer.mozilla.org/en-US/docs/Glossary/Idempotent
|
|
|
|
statusErr := new(APIError)
|
|
|
|
if errors.As(err, statusErr) && statusErr.StatusCode == http.StatusNotFound {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("delete record request: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddRRSet adds TXT record (create or update).
|
|
|
|
func (c *Client) AddRRSet(ctx context.Context, zone, recordName, value string, ttl int) error {
|
|
|
|
record := RRSet{TTL: ttl, Records: []Records{{Content: []string{value}}}}
|
|
|
|
|
|
|
|
txt, err := c.GetRRSet(ctx, zone, recordName)
|
|
|
|
if err == nil && len(txt.Records) > 0 {
|
|
|
|
record.Records = append(record.Records, txt.Records...)
|
|
|
|
return c.updateRRSet(ctx, zone, recordName, record)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.createRRSet(ctx, zone, recordName, record)
|
|
|
|
}
|
|
|
|
|
2023-07-25 12:42:54 +00:00
|
|
|
// https://api.gcore.com/docs/dns#tag/rrsets/operation/CreateRRSet
|
2021-07-22 10:05:22 +00:00
|
|
|
func (c *Client) createRRSet(ctx context.Context, zone, name string, record RRSet) error {
|
2023-02-09 16:19:58 +00:00
|
|
|
endpoint := c.baseURL.JoinPath("v2", "zones", zone, name, txtRecordType)
|
2021-07-22 10:05:22 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return c.doRequest(ctx, http.MethodPost, endpoint, record, nil)
|
2021-07-22 10:05:22 +00:00
|
|
|
}
|
|
|
|
|
2023-07-25 12:42:54 +00:00
|
|
|
// https://api.gcore.com/docs/dns#tag/rrsets/operation/UpdateRRSet
|
2021-07-22 10:05:22 +00:00
|
|
|
func (c *Client) updateRRSet(ctx context.Context, zone, name string, record RRSet) error {
|
2023-02-09 16:19:58 +00:00
|
|
|
endpoint := c.baseURL.JoinPath("v2", "zones", zone, name, txtRecordType)
|
2021-07-22 10:05:22 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return c.doRequest(ctx, http.MethodPut, endpoint, record, nil)
|
2021-07-22 10:05:22 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) doRequest(ctx context.Context, method string, endpoint *url.URL, bodyParams any, result any) error {
|
|
|
|
req, err := newJSONRequest(ctx, method, endpoint, bodyParams)
|
2021-07-22 10:05:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("new request: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req.Header.Set(authorizationHeader, fmt.Sprintf("%s %s", tokenTypeHeader, c.token))
|
2021-07-22 10:05:22 +00:00
|
|
|
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewHTTPDoError(req, err)
|
2021-07-22 10:05:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
|
|
|
if resp.StatusCode/100 != 2 {
|
2023-05-05 07:49:38 +00:00
|
|
|
return parseError(resp)
|
|
|
|
}
|
2021-07-22 10:05:22 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
if result == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
raw, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return errutils.NewReadResponseError(req, resp.StatusCode, err)
|
|
|
|
}
|
2021-07-22 10:05:22 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err = json.Unmarshal(raw, result)
|
|
|
|
if err != nil {
|
|
|
|
return errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2021-07-22 10:05:22 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return nil, fmt.Errorf("failed to create request JSON body: %w", err)
|
2021-07-22 10:05:22 +00:00
|
|
|
}
|
2023-05-05 07:49:38 +00:00
|
|
|
}
|
2021-07-22 10:05:22 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, method, endpoint.String(), buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to create request: %w", err)
|
2021-07-22 10:05:22 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
|
|
|
|
if payload != nil {
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
}
|
|
|
|
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseError(resp *http.Response) error {
|
|
|
|
raw, _ := io.ReadAll(resp.Body)
|
|
|
|
|
|
|
|
errAPI := APIError{StatusCode: resp.StatusCode}
|
|
|
|
err := json.Unmarshal(raw, &errAPI)
|
|
|
|
if err != nil {
|
|
|
|
errAPI.Message = string(raw)
|
2021-07-22 10:05:22 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return errAPI
|
2021-07-22 10:05:22 +00:00
|
|
|
}
|