mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2024-05-11 05:55:12 +00:00
New DNS provider PowerDNS (#748)
* Added PowerDNS as dns provider * Remove unnecessary comments * Some tests * Implemented feedback
This commit is contained in:
committed by
GitHub
parent
5269540827
commit
ffd4e46dda
@@ -25,6 +25,7 @@ import (
|
||||
_ "github.com/StackExchange/dnscontrol/v3/providers/octodns"
|
||||
_ "github.com/StackExchange/dnscontrol/v3/providers/opensrs"
|
||||
_ "github.com/StackExchange/dnscontrol/v3/providers/ovh"
|
||||
_ "github.com/StackExchange/dnscontrol/v3/providers/powerdns"
|
||||
_ "github.com/StackExchange/dnscontrol/v3/providers/route53"
|
||||
_ "github.com/StackExchange/dnscontrol/v3/providers/softlayer"
|
||||
_ "github.com/StackExchange/dnscontrol/v3/providers/vultr"
|
||||
|
244
providers/powerdns/powerdnsProvider.go
Normal file
244
providers/powerdns/powerdnsProvider.go
Normal file
@@ -0,0 +1,244 @@
|
||||
package powerdns
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/StackExchange/dnscontrol/v3/models"
|
||||
"github.com/StackExchange/dnscontrol/v3/pkg/diff"
|
||||
"github.com/StackExchange/dnscontrol/v3/providers"
|
||||
"github.com/miekg/dns/dnsutil"
|
||||
"github.com/mittwald/go-powerdns"
|
||||
"github.com/mittwald/go-powerdns/apis/zones"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var features = providers.DocumentationNotes{
|
||||
providers.CanUseAlias: providers.Can(),
|
||||
providers.CanUseCAA: providers.Can(),
|
||||
providers.CanUsePTR: providers.Can(),
|
||||
providers.CanUseSRV: providers.Can(),
|
||||
providers.CanUseTLSA: providers.Can(),
|
||||
providers.CanUseSSHFP: providers.Can(),
|
||||
providers.CanAutoDNSSEC: providers.Unimplemented("Need support in library first"),
|
||||
providers.DocCreateDomains: providers.Can(),
|
||||
providers.DocOfficiallySupported: providers.Cannot(),
|
||||
providers.CanGetZones: providers.Can(),
|
||||
}
|
||||
|
||||
func init() {
|
||||
providers.RegisterDomainServiceProviderType("POWERDNS", NewProvider, features)
|
||||
}
|
||||
|
||||
// PowerDNS represents the PowerDNS DNSServiceProvider.
|
||||
type PowerDNS struct {
|
||||
client pdns.Client
|
||||
APIKey string
|
||||
APIUrl string
|
||||
ServerName string
|
||||
DefaultNS []string `json:"default_ns"`
|
||||
DNSSecOnCreate bool `json:"dnssec_on_create"`
|
||||
|
||||
nameservers []*models.Nameserver
|
||||
}
|
||||
|
||||
// NewProvider initializes a PowerDNS DNSServiceProvider.
|
||||
func NewProvider(m map[string]string, metadata json.RawMessage) (providers.DNSServiceProvider, error) {
|
||||
api := &PowerDNS{}
|
||||
|
||||
api.APIKey = m["apiKey"]
|
||||
if api.APIKey == "" {
|
||||
return nil, fmt.Errorf("PowerDNS API Key is required")
|
||||
}
|
||||
|
||||
api.APIUrl = m["apiUrl"]
|
||||
if api.APIUrl == "" {
|
||||
return nil, fmt.Errorf("PowerDNS API URL is required")
|
||||
}
|
||||
|
||||
api.ServerName = m["serverName"]
|
||||
if api.ServerName == "" {
|
||||
return nil, fmt.Errorf("PowerDNS server name is required")
|
||||
}
|
||||
|
||||
// load js config
|
||||
if len(metadata) != 0 {
|
||||
err := json.Unmarshal(metadata, api)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
var nss []string
|
||||
for _, ns := range api.DefaultNS {
|
||||
nss = append(nss, ns[0:len(ns)-1])
|
||||
}
|
||||
var err error
|
||||
api.nameservers, err = models.ToNameservers(nss)
|
||||
if err != nil {
|
||||
return api, err
|
||||
}
|
||||
|
||||
var clientErr error
|
||||
api.client, clientErr = pdns.New(
|
||||
pdns.WithBaseURL(api.APIUrl),
|
||||
pdns.WithAPIKeyAuthentication(api.APIKey),
|
||||
)
|
||||
return api, clientErr
|
||||
}
|
||||
|
||||
// GetNameservers returns the nameservers for a domain.
|
||||
func (api *PowerDNS) GetNameservers(string) ([]*models.Nameserver, error) {
|
||||
var r []string
|
||||
for _, j := range api.nameservers {
|
||||
r = append(r, j.Name)
|
||||
}
|
||||
return models.ToNameservers(r)
|
||||
}
|
||||
|
||||
// ListZones returns all the zones in an account
|
||||
func (api *PowerDNS) ListZones() ([]string, error) {
|
||||
var result []string
|
||||
zones, err := api.client.Zones().ListZones(context.Background(), api.ServerName)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
for _, zone := range zones {
|
||||
result = append(result, zone.Name)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetZoneRecords gets the records of a zone and returns them in RecordConfig format.
|
||||
func (api *PowerDNS) GetZoneRecords(domain string) (models.Records, error) {
|
||||
zone, err := api.client.Zones().GetZone(context.Background(), api.ServerName, domain)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
curRecords := models.Records{}
|
||||
// loop over grouped records by type, called RRSet
|
||||
for _, rrset := range zone.ResourceRecordSets {
|
||||
if rrset.Type == "SOA" {
|
||||
continue
|
||||
}
|
||||
// loop over single records of this group and create records
|
||||
for _, pdnsRecord := range rrset.Records {
|
||||
r, err := toRecordConfig(domain, pdnsRecord, rrset.TTL, rrset.Name, rrset.Type)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
curRecords = append(curRecords, r)
|
||||
}
|
||||
}
|
||||
|
||||
return curRecords, nil
|
||||
}
|
||||
|
||||
// GetDomainCorrections returns a list of corrections to update a domain.
|
||||
func (api *PowerDNS) GetDomainCorrections(dc *models.DomainConfig) ([]*models.Correction, error) {
|
||||
var corrections []*models.Correction
|
||||
|
||||
// record corrections
|
||||
curRecords, err := api.GetZoneRecords(dc.Name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// post-process records
|
||||
dc.Punycode()
|
||||
models.PostProcessRecords(curRecords)
|
||||
|
||||
// create record diff by group
|
||||
keysToUpdate := (diff.New(dc)).ChangedGroups(curRecords)
|
||||
desiredRecords := dc.Records.GroupedByKey()
|
||||
|
||||
// create corrections by group
|
||||
for label, msgs := range keysToUpdate {
|
||||
labelName := label.NameFQDN + "."
|
||||
labelType := label.Type
|
||||
|
||||
if _, ok := desiredRecords[label]; !ok {
|
||||
// nothing found, must be a delete
|
||||
corrections = append(corrections, &models.Correction{
|
||||
Msg: strings.Join(msgs, "\n "),
|
||||
F: func() error {
|
||||
return api.client.Zones().RemoveRecordSetFromZone(context.Background(), api.ServerName, dc.Name, labelName, labelType)
|
||||
},
|
||||
})
|
||||
} else {
|
||||
ttl := desiredRecords[label][0].TTL
|
||||
records := []zones.Record{}
|
||||
for _, recordContent := range desiredRecords[label] {
|
||||
records = append(records, zones.Record{
|
||||
Content: recordContent.GetTargetCombined(),
|
||||
})
|
||||
}
|
||||
corrections = append(corrections, &models.Correction{
|
||||
Msg: strings.Join(msgs, "\n "),
|
||||
F: func() error {
|
||||
return api.client.Zones().AddRecordSetToZone(context.Background(), api.ServerName, dc.Name, zones.ResourceRecordSet{
|
||||
Name: labelName,
|
||||
Type: labelType,
|
||||
TTL: int(ttl),
|
||||
Records: records,
|
||||
})
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// DNSSec corrections
|
||||
// TODO: Implementing of on-demand DNSSec creation in library
|
||||
|
||||
return corrections, nil
|
||||
}
|
||||
|
||||
// EnsureDomainExists adds a domain to the DNS service if it does not exist
|
||||
func (api *PowerDNS) EnsureDomainExists(domain string) error {
|
||||
if zone, _ := api.client.Zones().GetZone(context.Background(), api.ServerName, domain); zone.ID != "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
_, err := api.client.Zones().CreateZone(context.Background(), api.ServerName, zones.Zone{
|
||||
Name: domain,
|
||||
Type: zones.ZoneTypeZone,
|
||||
DNSSec: api.DNSSecOnCreate,
|
||||
Nameservers: api.DefaultNS,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// toRecordConfig converts a PowerDNS DNSRecord to a RecordConfig. #rtype_variations
|
||||
func toRecordConfig(domain string, r zones.Record, ttl int, name string, rtype string) (*models.RecordConfig, error) {
|
||||
// trimming trailing dot and domain from name
|
||||
name = strings.TrimSuffix(name, domain+".")
|
||||
name = strings.TrimSuffix(name, ".")
|
||||
|
||||
rc := &models.RecordConfig{
|
||||
TTL: uint32(ttl),
|
||||
Original: r,
|
||||
Type: rtype,
|
||||
}
|
||||
rc.SetLabel(name, domain)
|
||||
|
||||
content := r.Content
|
||||
switch rtype {
|
||||
case "CNAME", "NS":
|
||||
return rc, rc.SetTarget(dnsutil.AddOrigin(content, domain))
|
||||
case "CAA":
|
||||
return rc, rc.SetTargetCAAString(content)
|
||||
case "MX":
|
||||
return rc, rc.SetTargetMXString(content)
|
||||
case "SRV":
|
||||
return rc, rc.SetTargetSRVString(content)
|
||||
case "TXT":
|
||||
// Remove quotes if it is a TXT record.
|
||||
if !strings.HasPrefix(content, `"`) || !strings.HasSuffix(content, `"`) {
|
||||
return nil, errors.New("Unexpected lack of quotes in TXT record from PowerDNS")
|
||||
}
|
||||
return rc, rc.SetTargetTXT(content[1 : len(content)-1])
|
||||
default:
|
||||
return rc, rc.PopulateFromString(rtype, content, domain)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user