2020-05-30 21:54:07 +08:00
package powerdns
import (
"encoding/json"
"fmt"
2022-08-15 08:46:56 +08:00
2020-05-30 21:54:07 +08:00
"github.com/StackExchange/dnscontrol/v3/models"
"github.com/StackExchange/dnscontrol/v3/providers"
2020-08-31 07:52:37 +08:00
pdns "github.com/mittwald/go-powerdns"
2020-05-30 21:54:07 +08:00
)
var features = providers . DocumentationNotes {
2022-03-03 00:19:15 +08:00
providers . CanAutoDNSSEC : providers . Can ( ) ,
providers . CanGetZones : providers . Can ( ) ,
2021-03-01 20:09:49 +08:00
providers . CanUseAlias : providers . Can ( "Needs to be enabled in PowerDNS first" , "https://doc.powerdns.com/authoritative/guides/alias.html" ) ,
2020-05-30 21:54:07 +08:00
providers . CanUseCAA : providers . Can ( ) ,
2022-02-18 06:16:15 +08:00
providers . CanUseDS : providers . Can ( ) ,
2023-03-17 22:22:10 +08:00
providers . CanUseLOC : providers . Unimplemented ( "Normalization within the PowerDNS API seems to be buggy, so disabled" , "https://github.com/PowerDNS/pdns/issues/10558" ) ,
2022-03-03 00:19:15 +08:00
providers . CanUseNAPTR : providers . Can ( ) ,
2020-05-30 21:54:07 +08:00
providers . CanUsePTR : providers . Can ( ) ,
providers . CanUseSRV : providers . Can ( ) ,
providers . CanUseSSHFP : providers . Can ( ) ,
2022-03-03 00:19:15 +08:00
providers . CanUseTLSA : providers . Can ( ) ,
2020-05-30 21:54:07 +08:00
providers . DocCreateDomains : providers . Can ( ) ,
2021-03-01 20:09:49 +08:00
providers . DocDualHost : providers . Can ( ) ,
2022-03-03 00:19:15 +08:00
providers . DocOfficiallySupported : providers . Cannot ( ) ,
2020-05-30 21:54:07 +08:00
}
func init ( ) {
2021-03-08 02:19:22 +08:00
fns := providers . DspFuncs {
2022-06-21 00:27:05 +08:00
Initializer : newDSP ,
2021-03-09 09:14:30 +08:00
RecordAuditor : AuditRecords ,
2021-03-08 02:19:22 +08:00
}
providers . RegisterDomainServiceProviderType ( "POWERDNS" , fns , features )
2020-05-30 21:54:07 +08:00
}
2020-10-26 21:25:30 +08:00
// powerdnsProvider represents the powerdnsProvider DNSServiceProvider.
type powerdnsProvider struct {
2020-05-30 21:54:07 +08:00
client pdns . Client
APIKey string
APIUrl string
ServerName string
DefaultNS [ ] string ` json:"default_ns" `
DNSSecOnCreate bool ` json:"dnssec_on_create" `
nameservers [ ] * models . Nameserver
}
2022-06-21 00:27:05 +08:00
// newDSP initializes a PowerDNS DNSServiceProvider.
func newDSP ( m map [ string ] string , metadata json . RawMessage ) ( providers . DNSServiceProvider , error ) {
dsp := & powerdnsProvider { }
2020-05-30 21:54:07 +08:00
2022-06-21 00:27:05 +08:00
dsp . APIKey = m [ "apiKey" ]
if dsp . APIKey == "" {
2020-05-30 21:54:07 +08:00
return nil , fmt . Errorf ( "PowerDNS API Key is required" )
}
2022-06-21 00:27:05 +08:00
dsp . APIUrl = m [ "apiUrl" ]
if dsp . APIUrl == "" {
2020-05-30 21:54:07 +08:00
return nil , fmt . Errorf ( "PowerDNS API URL is required" )
}
2022-06-21 00:27:05 +08:00
dsp . ServerName = m [ "serverName" ]
if dsp . ServerName == "" {
2020-05-30 21:54:07 +08:00
return nil , fmt . Errorf ( "PowerDNS server name is required" )
}
// load js config
if len ( metadata ) != 0 {
2022-06-21 00:27:05 +08:00
err := json . Unmarshal ( metadata , dsp )
2020-05-30 21:54:07 +08:00
if err != nil {
return nil , err
}
}
var nss [ ] string
2022-06-21 00:27:05 +08:00
for _ , ns := range dsp . DefaultNS {
2020-05-30 21:54:07 +08:00
nss = append ( nss , ns [ 0 : len ( ns ) - 1 ] )
}
var err error
2022-06-21 00:27:05 +08:00
dsp . nameservers , err = models . ToNameservers ( nss )
2020-05-30 21:54:07 +08:00
if err != nil {
2022-06-21 00:27:05 +08:00
return dsp , err
2020-05-30 21:54:07 +08:00
}
var clientErr error
2022-06-21 00:27:05 +08:00
dsp . client , clientErr = pdns . New (
pdns . WithBaseURL ( dsp . APIUrl ) ,
pdns . WithAPIKeyAuthentication ( dsp . APIKey ) ,
2020-05-30 21:54:07 +08:00
)
2022-06-21 00:27:05 +08:00
return dsp , clientErr
2020-05-30 21:54:07 +08:00
}