2019-03-11 16:56:48 +00:00
|
|
|
package pdns
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-10-27 11:01:05 +00:00
|
|
|
"errors"
|
2018-12-06 21:50:17 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2020-11-28 16:54:26 +00:00
|
|
|
"path"
|
2018-12-06 21:50:17 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
2020-12-12 14:59:22 +00:00
|
|
|
"github.com/miekg/dns"
|
2018-12-06 21:50:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Record struct {
|
|
|
|
Content string `json:"content"`
|
|
|
|
Disabled bool `json:"disabled"`
|
|
|
|
|
|
|
|
// pre-v1 API
|
|
|
|
Name string `json:"name"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
TTL int `json:"ttl,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type hostedZone struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
RRSets []rrSet `json:"rrsets"`
|
|
|
|
|
|
|
|
// pre-v1 API
|
|
|
|
Records []Record `json:"records"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type rrSet struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Kind string `json:"kind"`
|
|
|
|
ChangeType string `json:"changetype"`
|
2021-03-17 08:53:38 +00:00
|
|
|
Records []Record `json:"records,omitempty"`
|
2018-12-06 21:50:17 +00:00
|
|
|
TTL int `json:"ttl,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type rrSets struct {
|
|
|
|
RRSets []rrSet `json:"rrsets"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type apiError struct {
|
|
|
|
ShortMsg string `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a apiError) Error() string {
|
|
|
|
return a.ShortMsg
|
|
|
|
}
|
|
|
|
|
|
|
|
type apiVersion struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
Version int `json:"version"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DNSProvider) getHostedZone(fqdn string) (*hostedZone, error) {
|
|
|
|
authZone, err := dns01.FindZoneByFqdn(fqdn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-17 08:53:38 +00:00
|
|
|
p := path.Join("/servers", d.config.ServerName, "/zones/", dns.Fqdn(authZone))
|
2018-12-06 21:50:17 +00:00
|
|
|
|
2020-12-12 14:59:22 +00:00
|
|
|
result, err := d.sendRequest(http.MethodGet, p, nil)
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-12 14:59:22 +00:00
|
|
|
var zone hostedZone
|
2018-12-06 21:50:17 +00:00
|
|
|
err = json.Unmarshal(result, &zone)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert pre-v1 API result
|
|
|
|
if len(zone.Records) > 0 {
|
|
|
|
zone.RRSets = []rrSet{}
|
|
|
|
for _, record := range zone.Records {
|
|
|
|
set := rrSet{
|
|
|
|
Name: record.Name,
|
|
|
|
Type: record.Type,
|
|
|
|
Records: []Record{record},
|
|
|
|
}
|
|
|
|
zone.RRSets = append(zone.RRSets, set)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &zone, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DNSProvider) findTxtRecord(fqdn string) (*rrSet, error) {
|
|
|
|
zone, err := d.getHostedZone(fqdn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = d.sendRequest(http.MethodGet, zone.URL, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, set := range zone.RRSets {
|
2020-11-28 16:54:26 +00:00
|
|
|
if set.Type == "TXT" && (set.Name == dns01.UnFqdn(fqdn) || set.Name == fqdn) {
|
2018-12-06 21:50:17 +00:00
|
|
|
return &set, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-21 14:46:21 +00:00
|
|
|
return nil, nil
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DNSProvider) getAPIVersion() (int, error) {
|
|
|
|
result, err := d.sendRequest(http.MethodGet, "/api", nil)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var versions []apiVersion
|
|
|
|
err = json.Unmarshal(result, &versions)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
latestVersion := 0
|
|
|
|
for _, v := range versions {
|
|
|
|
if v.Version > latestVersion {
|
|
|
|
latestVersion = v.Version
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return latestVersion, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DNSProvider) sendRequest(method, uri string, body io.Reader) (json.RawMessage, error) {
|
|
|
|
req, err := d.makeRequest(method, uri, body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := d.config.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
2020-03-20 21:53:09 +00:00
|
|
|
return nil, fmt.Errorf("error talking to PDNS API: %w", err)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusUnprocessableEntity && (resp.StatusCode < 200 || resp.StatusCode >= 300) {
|
2021-03-17 08:53:38 +00:00
|
|
|
return nil, fmt.Errorf("unexpected HTTP status code %d when %sing '%s'", resp.StatusCode, req.Method, req.URL)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var msg json.RawMessage
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&msg)
|
|
|
|
if err != nil {
|
2020-10-27 11:01:05 +00:00
|
|
|
if errors.Is(err, io.EOF) {
|
2018-12-06 21:50:17 +00:00
|
|
|
// empty body
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
// other error
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for PowerDNS error message
|
|
|
|
if len(msg) > 0 && msg[0] == '{' {
|
|
|
|
var errInfo apiError
|
|
|
|
err = json.Unmarshal(msg, &errInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if errInfo.ShortMsg != "" {
|
2020-03-20 21:53:09 +00:00
|
|
|
return nil, fmt.Errorf("error talking to PDNS API: %w", errInfo)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DNSProvider) makeRequest(method, uri string, body io.Reader) (*http.Request, error) {
|
2020-11-28 16:54:26 +00:00
|
|
|
p := path.Join("/", uri)
|
2018-12-06 21:50:17 +00:00
|
|
|
|
2020-11-28 16:54:26 +00:00
|
|
|
if p != "/api" && d.apiVersion > 0 && !strings.HasPrefix(p, "/api/v") {
|
|
|
|
p = path.Join("/api", "v"+strconv.Itoa(d.apiVersion), p)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
2020-11-28 16:54:26 +00:00
|
|
|
u, err := d.config.Host.Parse(path.Join(d.config.Host.Path, p))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
2020-11-28 16:54:26 +00:00
|
|
|
req, err := http.NewRequest(method, u.String(), body)
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("X-API-Key", d.config.APIKey)
|
|
|
|
|
2021-03-17 08:53:38 +00:00
|
|
|
if method != http.MethodGet && method != http.MethodDelete {
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
}
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
return req, nil
|
|
|
|
}
|