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

226 lines
5.3 KiB
Go
Raw Permalink Normal View History

2020-05-08 12:26:30 +00:00
package internal
import (
"bytes"
2023-05-05 07:49:38 +00:00
"context"
2020-05-08 12:26:30 +00:00
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
2023-05-05 07:49:38 +00:00
"time"
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
2020-05-08 12:26:30 +00:00
)
// defaultBaseURL represents the API endpoint to call.
const defaultBaseURL = "https://dns.hetzner.com"
const authHeader = "Auth-API-Token"
// Client the Hetzner client.
type Client struct {
apiKey string
2023-05-05 07:49:38 +00:00
baseURL *url.URL
HTTPClient *http.Client
2020-05-08 12:26:30 +00:00
}
// NewClient Creates a new Hetzner client.
func NewClient(apiKey string) *Client {
2023-05-05 07:49:38 +00:00
baseURL, _ := url.Parse(defaultBaseURL)
2020-05-08 12:26:30 +00:00
return &Client{
apiKey: apiKey,
2023-05-05 07:49:38 +00:00
baseURL: baseURL,
HTTPClient: &http.Client{Timeout: 5 * time.Second},
2020-05-08 12:26:30 +00:00
}
}
// GetTxtRecord gets a TXT record.
2023-05-05 07:49:38 +00:00
func (c *Client) GetTxtRecord(ctx context.Context, name, value, zoneID string) (*DNSRecord, error) {
records, err := c.getRecords(ctx, zoneID)
2020-05-08 12:26:30 +00:00
if err != nil {
return nil, err
}
for _, record := range records.Records {
if record.Type == "TXT" && record.Name == name && record.Value == value {
return &record, nil
}
}
return nil, fmt.Errorf("could not find record: zone ID: %s; Record: %s", zoneID, name)
}
// https://dns.hetzner.com/api-docs#operation/GetRecords
2023-05-05 07:49:38 +00:00
func (c *Client) getRecords(ctx context.Context, zoneID string) (*DNSRecords, error) {
endpoint := c.baseURL.JoinPath("api", "v1", "records")
2020-05-08 12:26:30 +00:00
query := endpoint.Query()
query.Set("zone_id", zoneID)
endpoint.RawQuery = query.Encode()
2023-05-05 07:49:38 +00:00
req, err := c.newRequest(ctx, http.MethodGet, endpoint, nil)
2020-05-08 12:26:30 +00:00
if err != nil {
return nil, err
}
2023-05-05 07:49:38 +00:00
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, errutils.NewHTTPDoError(req, err)
}
2020-05-08 12:26:30 +00:00
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
2023-05-05 07:49:38 +00:00
return nil, errutils.NewUnexpectedResponseStatusCodeError(req, resp)
}
raw, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errutils.NewReadResponseError(req, resp.StatusCode, err)
2020-05-08 12:26:30 +00:00
}
records := &DNSRecords{}
2023-05-05 07:49:38 +00:00
err = json.Unmarshal(raw, records)
2020-05-08 12:26:30 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return nil, errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
2020-05-08 12:26:30 +00:00
}
return records, nil
}
// CreateRecord creates a DNS record.
// https://dns.hetzner.com/api-docs#operation/CreateRecord
2023-05-05 07:49:38 +00:00
func (c *Client) CreateRecord(ctx context.Context, record DNSRecord) error {
endpoint := c.baseURL.JoinPath("api", "v1", "records")
req, err := c.newRequest(ctx, http.MethodPost, endpoint, record)
2020-05-08 12:26:30 +00:00
if err != nil {
return err
}
2023-05-05 07:49:38 +00:00
resp, err := c.HTTPClient.Do(req)
2020-05-08 12:26:30 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return errutils.NewHTTPDoError(req, err)
2020-05-08 12:26:30 +00:00
}
2023-05-05 07:49:38 +00:00
defer func() { _ = resp.Body.Close() }()
2020-05-08 12:26:30 +00:00
if resp.StatusCode != http.StatusOK {
2023-05-05 07:49:38 +00:00
return errutils.NewUnexpectedResponseStatusCodeError(req, resp)
2020-05-08 12:26:30 +00:00
}
return nil
}
// DeleteRecord deletes a DNS record.
// https://dns.hetzner.com/api-docs#operation/DeleteRecord
2023-05-05 07:49:38 +00:00
func (c *Client) DeleteRecord(ctx context.Context, recordID string) error {
endpoint := c.baseURL.JoinPath("api", "v1", "records", recordID)
req, err := c.newRequest(ctx, http.MethodDelete, endpoint, nil)
2020-05-08 12:26:30 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return err
2020-05-08 12:26:30 +00:00
}
2023-05-05 07:49:38 +00:00
resp, err := c.HTTPClient.Do(req)
2020-05-08 12:26:30 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return errutils.NewHTTPDoError(req, err)
2020-05-08 12:26:30 +00:00
}
2023-05-05 07:49:38 +00:00
defer func() { _ = resp.Body.Close() }()
2020-05-08 12:26:30 +00:00
if resp.StatusCode != http.StatusOK {
2023-05-05 07:49:38 +00:00
return errutils.NewUnexpectedResponseStatusCodeError(req, resp)
2020-05-08 12:26:30 +00:00
}
return nil
}
// GetZoneID gets the zone ID for a domain.
2023-05-05 07:49:38 +00:00
func (c *Client) GetZoneID(ctx context.Context, domain string) (string, error) {
zones, err := c.getZones(ctx, domain)
2020-05-08 12:26:30 +00:00
if err != nil {
return "", err
}
for _, zone := range zones.Zones {
if zone.Name == domain {
return zone.ID, nil
}
}
return "", fmt.Errorf("could not get zone for domain %s not found", domain)
}
// https://dns.hetzner.com/api-docs#operation/GetZones
2023-05-05 07:49:38 +00:00
func (c *Client) getZones(ctx context.Context, name string) (*Zones, error) {
endpoint := c.baseURL.JoinPath("api", "v1", "zones")
2020-05-08 12:26:30 +00:00
query := endpoint.Query()
query.Set("name", name)
endpoint.RawQuery = query.Encode()
2023-05-05 07:49:38 +00:00
req, err := c.newRequest(ctx, http.MethodGet, endpoint, nil)
2020-05-08 12:26:30 +00:00
if err != nil {
return nil, fmt.Errorf("could not get zones: %w", err)
}
2023-05-05 07:49:38 +00:00
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, errutils.NewHTTPDoError(req, err)
}
defer func() { _ = resp.Body.Close() }()
// EOF fallback
if resp.StatusCode == http.StatusNotFound {
return &Zones{}, nil
}
2020-05-08 12:26:30 +00:00
if resp.StatusCode != http.StatusOK {
2023-05-05 07:49:38 +00:00
return nil, errutils.NewUnexpectedResponseStatusCodeError(req, resp)
}
raw, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errutils.NewReadResponseError(req, resp.StatusCode, err)
2020-05-08 12:26:30 +00:00
}
zones := &Zones{}
2023-05-05 07:49:38 +00:00
err = json.Unmarshal(raw, zones)
2020-05-08 12:26:30 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return nil, errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
2020-05-08 12:26:30 +00:00
}
return zones, nil
}
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)
}
}
req, err := http.NewRequestWithContext(ctx, method, endpoint.String(), buf)
2020-05-08 12:26:30 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return nil, fmt.Errorf("unable to create request: %w", err)
2020-05-08 12:26:30 +00:00
}
req.Header.Set("Accept", "application/json")
2023-05-05 07:49:38 +00:00
if payload != nil {
req.Header.Set("Content-Type", "application/json")
2020-05-08 12:26:30 +00:00
}
2023-05-05 07:49:38 +00:00
req.Header.Set(authHeader, c.apiKey)
return req, nil
2020-05-08 12:26:30 +00:00
}