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

183 lines
4.7 KiB
Go
Raw Permalink Normal View History

2019-03-11 16:56:48 +00:00
package internal
2018-09-08 12:08:07 +00:00
import (
"bytes"
2023-05-05 07:49:38 +00:00
"context"
2018-09-08 12:08:07 +00:00
"encoding/json"
2020-02-27 18:14:46 +00:00
"errors"
2018-09-08 12:08:07 +00:00
"fmt"
2021-08-25 09:44:11 +00:00
"io"
2018-09-08 12:08:07 +00:00
"net/http"
"time"
2023-05-05 07:49:38 +00:00
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
2018-09-08 12:08:07 +00:00
)
2020-05-08 17:35:25 +00:00
// defaultBaseURL for reaching the jSON-based API-Endpoint of netcup.
const defaultBaseURL = "https://ccp.netcup.net/run/webservice/servers/endpoint.php?JSON"
2018-09-08 12:08:07 +00:00
2020-05-08 17:35:25 +00:00
// Client netcup DNS client.
2018-09-08 12:08:07 +00:00
type Client struct {
customerNumber string
apiKey string
apiPassword string
2023-05-05 07:49:38 +00:00
baseURL string
HTTPClient *http.Client
2018-09-08 12:08:07 +00:00
}
2020-05-08 17:35:25 +00:00
// NewClient creates a netcup DNS client.
2020-07-09 23:48:18 +00:00
func NewClient(customerNumber, apiKey, apiPassword string) (*Client, error) {
if customerNumber == "" || apiKey == "" || apiPassword == "" {
2020-02-27 18:14:46 +00:00
return nil, errors.New("credentials missing")
}
2018-09-08 12:08:07 +00:00
return &Client{
customerNumber: customerNumber,
apiKey: apiKey,
apiPassword: apiPassword,
2023-05-05 07:49:38 +00:00
baseURL: defaultBaseURL,
HTTPClient: &http.Client{Timeout: 10 * time.Second},
}, nil
2018-09-08 12:08:07 +00:00
}
2020-05-08 17:35:25 +00:00
// UpdateDNSRecord performs an update of the DNSRecords as specified by the netcup WSDL.
2018-09-08 12:08:07 +00:00
// https://ccp.netcup.net/run/webservice/servers/endpoint.php
2023-05-05 07:49:38 +00:00
func (c *Client) UpdateDNSRecord(ctx context.Context, domainName string, records []DNSRecord) error {
2018-09-08 12:08:07 +00:00
payload := &Request{
Action: "updateDnsRecords",
Param: UpdateDNSRecordsRequest{
2018-09-08 12:08:07 +00:00
DomainName: domainName,
CustomerNumber: c.customerNumber,
APIKey: c.apiKey,
2023-05-05 07:49:38 +00:00
APISessionID: getSessionID(ctx),
2018-09-08 12:08:07 +00:00
ClientRequestID: "",
DNSRecordSet: DNSRecordSet{DNSRecords: records},
2018-09-08 12:08:07 +00:00
},
}
2023-05-05 07:49:38 +00:00
err := c.doRequest(ctx, payload, nil)
2018-09-08 12:08:07 +00:00
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("error when sending the request: %w", err)
2018-09-08 12:08:07 +00:00
}
return nil
}
// GetDNSRecords retrieves all dns records of an DNS-Zone as specified by the netcup WSDL
2020-05-08 17:35:25 +00:00
// returns an array of DNSRecords.
2018-09-08 12:08:07 +00:00
// https://ccp.netcup.net/run/webservice/servers/endpoint.php
2023-05-05 07:49:38 +00:00
func (c *Client) GetDNSRecords(ctx context.Context, hostname string) ([]DNSRecord, error) {
2018-09-08 12:08:07 +00:00
payload := &Request{
Action: "infoDnsRecords",
Param: InfoDNSRecordsRequest{
2018-09-08 12:08:07 +00:00
DomainName: hostname,
CustomerNumber: c.customerNumber,
APIKey: c.apiKey,
2023-05-05 07:49:38 +00:00
APISessionID: getSessionID(ctx),
2018-09-08 12:08:07 +00:00
ClientRequestID: "",
},
}
var responseData InfoDNSRecordsResponse
2023-05-05 07:49:38 +00:00
err := c.doRequest(ctx, payload, &responseData)
2018-09-08 12:08:07 +00:00
if err != nil {
2020-02-27 18:14:46 +00:00
return nil, fmt.Errorf("error when sending the request: %w", err)
2018-09-08 12:08:07 +00:00
}
return responseData.DNSRecords, nil
2018-09-08 12:08:07 +00:00
}
// doRequest marshals given body to JSON, send the request to netcup API
2020-05-08 17:35:25 +00:00
// and returns body of response.
2023-05-05 07:49:38 +00:00
func (c *Client) doRequest(ctx context.Context, payload, result any) error {
req, err := newJSONRequest(ctx, http.MethodPost, c.baseURL, payload)
2018-09-08 12:08:07 +00:00
if err != nil {
return err
2018-09-08 12:08:07 +00:00
}
req.Close = true
2018-09-08 12:08:07 +00:00
resp, err := c.HTTPClient.Do(req)
2018-09-08 12:08:07 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return errutils.NewHTTPDoError(req, err)
2018-09-08 12:08:07 +00:00
}
2023-05-05 07:49:38 +00:00
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode >= http.StatusMultipleChoices {
return errutils.NewUnexpectedResponseStatusCodeError(req, resp)
2018-09-08 12:08:07 +00:00
}
2023-05-05 07:49:38 +00:00
respMsg, err := unmarshalResponseMsg(req, resp)
2018-09-08 12:08:07 +00:00
if err != nil {
return err
2018-09-08 12:08:07 +00:00
}
if respMsg.Status != success {
return respMsg
}
2023-05-05 07:49:38 +00:00
if result == nil {
return nil
}
err = json.Unmarshal(respMsg.ResponseData, result)
if err != nil {
return errutils.NewUnmarshalError(req, resp.StatusCode, respMsg.ResponseData, err)
}
return nil
2018-09-08 12:08:07 +00:00
}
2023-05-05 07:49:38 +00:00
// GetDNSRecordIdx searches a given array of DNSRecords for a given DNSRecord
// equivalence is determined by Destination and RecortType attributes
// returns index of given DNSRecord in given array of DNSRecords.
func GetDNSRecordIdx(records []DNSRecord, record DNSRecord) (int, error) {
for index, element := range records {
if record.Destination == element.Destination && record.RecordType == element.RecordType {
return index, nil
2018-09-08 12:08:07 +00:00
}
2023-05-05 07:49:38 +00:00
}
return -1, errors.New("no DNS Record found")
}
2023-05-05 07:49:38 +00:00
func newJSONRequest(ctx context.Context, method string, endpoint string, payload any) (*http.Request, error) {
buf := new(bytes.Buffer)
2023-05-05 07:49:38 +00:00
if payload != nil {
err := json.NewEncoder(buf).Encode(payload)
if err != nil {
2023-05-05 07:49:38 +00:00
return nil, fmt.Errorf("failed to create request JSON body: %w", err)
}
2023-05-05 07:49:38 +00:00
}
2023-05-05 07:49:38 +00:00
req, err := http.NewRequestWithContext(ctx, method, endpoint, buf)
if err != nil {
return nil, fmt.Errorf("unable to create request: %w", err)
2018-09-08 12:08:07 +00:00
}
2023-05-05 07:49:38 +00:00
req.Header.Set("Accept", "application/json")
2018-09-08 12:08:07 +00:00
2023-05-05 07:49:38 +00:00
if payload != nil {
req.Header.Set("Content-Type", "application/json")
}
2023-05-05 07:49:38 +00:00
return req, nil
}
2023-05-05 07:49:38 +00:00
func unmarshalResponseMsg(req *http.Request, resp *http.Response) (*ResponseMsg, error) {
2021-08-25 09:44:11 +00:00
raw, err := io.ReadAll(resp.Body)
if err != nil {
2023-05-05 07:49:38 +00:00
return nil, errutils.NewReadResponseError(req, resp.StatusCode, err)
2018-09-08 12:08:07 +00:00
}
var respMsg ResponseMsg
err = json.Unmarshal(raw, &respMsg)
if err != nil {
2023-05-05 07:49:38 +00:00
return nil, errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
}
return &respMsg, nil
2018-09-08 12:08:07 +00:00
}