2021-02-08 19:13:21 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-05-05 07:49:38 +00:00
|
|
|
"context"
|
2021-02-08 19:13:21 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-08-25 09:44:11 +00:00
|
|
|
"io"
|
2021-02-08 19:13:21 +00:00
|
|
|
"net/http"
|
2023-05-05 07:49:38 +00:00
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2021-02-08 19:13:21 +00:00
|
|
|
"time"
|
2023-05-05 07:49:38 +00:00
|
|
|
|
|
|
|
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
|
2021-02-08 19:13:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const defaultBaseURL string = "https://api.domeneshop.no/v0"
|
|
|
|
|
|
|
|
// Client implements a very simple wrapper around the Domeneshop API.
|
2023-07-29 10:59:24 +00:00
|
|
|
// For now, it will only deal with adding and removing TXT records, as required by ACME providers.
|
2021-02-08 19:13:21 +00:00
|
|
|
// https://api.domeneshop.no/docs/
|
|
|
|
type Client struct {
|
2023-05-05 07:49:38 +00:00
|
|
|
apiToken string
|
|
|
|
apiSecret string
|
|
|
|
|
|
|
|
baseURL *url.URL
|
2021-02-08 19:13:21 +00:00
|
|
|
HTTPClient *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient returns an instance of the Domeneshop API wrapper.
|
|
|
|
func NewClient(apiToken, apiSecret string) *Client {
|
2023-05-05 07:49:38 +00:00
|
|
|
baseURL, _ := url.Parse(defaultBaseURL)
|
|
|
|
|
2021-02-08 19:13:21 +00:00
|
|
|
return &Client{
|
|
|
|
apiToken: apiToken,
|
|
|
|
apiSecret: apiSecret,
|
2023-05-05 07:49:38 +00:00
|
|
|
baseURL: baseURL,
|
|
|
|
HTTPClient: &http.Client{Timeout: 5 * time.Second},
|
2021-02-08 19:13:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDomainByName fetches the domain list and returns the Domain object for the matching domain.
|
|
|
|
// https://api.domeneshop.no/docs/#operation/getDomains
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) GetDomainByName(ctx context.Context, domain string) (*Domain, error) {
|
|
|
|
endpoint := c.baseURL.JoinPath("domains")
|
|
|
|
|
|
|
|
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-08 19:13:21 +00:00
|
|
|
var domains []Domain
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err = c.do(req, &domains)
|
2021-02-08 19:13:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range domains {
|
|
|
|
if !d.Services.DNS {
|
|
|
|
// Domains without DNS service cannot have DNS record added.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.Name == domain {
|
|
|
|
return &d, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("failed to find matching domain name: %s", domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateTXTRecord creates a TXT record with the provided host (subdomain) and data.
|
|
|
|
// https://api.domeneshop.no/docs/#tag/dns/paths/~1domains~1{domainId}~1dns/post
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) CreateTXTRecord(ctx context.Context, domain *Domain, host string, data string) error {
|
|
|
|
endpoint := c.baseURL.JoinPath("domains", strconv.Itoa(domain.ID), "dns")
|
|
|
|
|
|
|
|
record := DNSRecord{
|
2021-02-08 19:13:21 +00:00
|
|
|
Data: data,
|
|
|
|
Host: host,
|
|
|
|
TTL: 300,
|
|
|
|
Type: "TXT",
|
2023-05-05 07:49:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req, err := newJSONRequest(ctx, http.MethodPost, endpoint, record)
|
2021-02-08 19:13:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return c.do(req, nil)
|
2021-02-08 19:13:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteTXTRecord deletes the DNS record matching the provided host and data.
|
|
|
|
// https://api.domeneshop.no/docs/#tag/dns/paths/~1domains~1{domainId}~1dns~1{recordId}/delete
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) DeleteTXTRecord(ctx context.Context, domain *Domain, host string, data string) error {
|
|
|
|
record, err := c.getDNSRecordByHostData(ctx, *domain, host, data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint := c.baseURL.JoinPath("domains", strconv.Itoa(domain.ID), "dns", strconv.Itoa(record.ID))
|
|
|
|
|
|
|
|
req, err := newJSONRequest(ctx, http.MethodDelete, endpoint, nil)
|
2021-02-08 19:13:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return c.do(req, nil)
|
2021-02-08 19:13:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getDNSRecordByHostData finds the first matching DNS record with the provided host and data.
|
|
|
|
// https://api.domeneshop.no/docs/#operation/getDnsRecords
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) getDNSRecordByHostData(ctx context.Context, domain Domain, host string, data string) (*DNSRecord, error) {
|
|
|
|
endpoint := c.baseURL.JoinPath("domains", strconv.Itoa(domain.ID), "dns")
|
|
|
|
|
|
|
|
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-08 19:13:21 +00:00
|
|
|
var records []DNSRecord
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err = c.do(req, &records)
|
2021-02-08 19:13:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range records {
|
|
|
|
if r.Host == host && r.Data == data {
|
|
|
|
return &r, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("failed to find record with host %s for domain %s", host, domain.Name)
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
// do a request against the API,
|
2021-02-08 19:13:21 +00:00
|
|
|
// and makes sure that the required Authorization header is set using `setBasicAuth`.
|
2023-05-05 07:49:38 +00:00
|
|
|
func (c *Client) do(req *http.Request, result any) error {
|
2021-02-08 19:13:21 +00:00
|
|
|
req.SetBasicAuth(c.apiToken, c.apiSecret)
|
|
|
|
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewHTTPDoError(req, err)
|
2021-02-08 19:13:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
|
|
|
if resp.StatusCode >= http.StatusBadRequest {
|
2023-05-05 07:49:38 +00:00
|
|
|
return errutils.NewUnexpectedResponseStatusCodeError(req, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
if result == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
raw, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return errutils.NewReadResponseError(req, resp.StatusCode, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-08 19:13:21 +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-02-08 19:13:21 +00:00
|
|
|
}
|
2023-05-05 07:49:38 +00:00
|
|
|
}
|
2021-02-08 19:13:21 +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-02-08 19:13:21 +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")
|
2021-02-08 19:13:21 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return req, nil
|
2021-02-08 19:13:21 +00:00
|
|
|
}
|