mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2024-09-20 06:46:19 +08:00
CHORE: linting (#2098)
This commit is contained in:
parent
4eab96226c
commit
169d7c8062
|
@ -2,6 +2,8 @@ package models
|
||||||
|
|
||||||
// Functions that make it easier to deal with a group of records.
|
// Functions that make it easier to deal with a group of records.
|
||||||
|
|
||||||
|
// RecordDB is a container of many RecordConfig, queryable by various methods.
|
||||||
|
// The first to be implemented is as a hash with label:type as the index.
|
||||||
type RecordDB struct {
|
type RecordDB struct {
|
||||||
labelAndTypeMap map[RecordKey]struct{}
|
labelAndTypeMap map[RecordKey]struct{}
|
||||||
}
|
}
|
||||||
|
@ -10,10 +12,8 @@ type RecordDB struct {
|
||||||
func NewRecordDBFromRecords(recs Records, zone string) *RecordDB {
|
func NewRecordDBFromRecords(recs Records, zone string) *RecordDB {
|
||||||
result := &RecordDB{}
|
result := &RecordDB{}
|
||||||
|
|
||||||
//fmt.Printf("DEBUG: BUILDING RecordDB: zone=%v\n", zone)
|
|
||||||
result.labelAndTypeMap = make(map[RecordKey]struct{}, len(recs))
|
result.labelAndTypeMap = make(map[RecordKey]struct{}, len(recs))
|
||||||
for _, rec := range recs {
|
for _, rec := range recs {
|
||||||
//fmt.Printf(" DEBUG: Adding %+v\n", rec.Key())
|
|
||||||
result.labelAndTypeMap[rec.Key()] = struct{}{}
|
result.labelAndTypeMap[rec.Key()] = struct{}{}
|
||||||
}
|
}
|
||||||
//fmt.Printf("DEBUG: BUILDING RecordDB: DONE!\n")
|
//fmt.Printf("DEBUG: BUILDING RecordDB: DONE!\n")
|
||||||
|
@ -25,6 +25,5 @@ func NewRecordDBFromRecords(recs Records, zone string) *RecordDB {
|
||||||
// on the record's label and type (i.e. the RecordKey)
|
// on the record's label and type (i.e. the RecordKey)
|
||||||
func (recdb *RecordDB) ContainsLT(rec *RecordConfig) bool {
|
func (recdb *RecordDB) ContainsLT(rec *RecordConfig) bool {
|
||||||
_, ok := recdb.labelAndTypeMap[rec.Key()]
|
_, ok := recdb.labelAndTypeMap[rec.Key()]
|
||||||
//fmt.Printf("DEBUG: ContainsLT(%q) = %v (%v)\n", rec.Key(), ok, recdb)
|
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,6 +48,7 @@ func ExecuteJavascript(file string, devMode bool, variables map[string]string) (
|
||||||
return ExecuteJavascriptString(script, devMode, variables)
|
return ExecuteJavascriptString(script, devMode, variables)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ExecuteJavascriptString accepts a string containing javascript and runs it, returning the resulting dnsConfig.
|
||||||
func ExecuteJavascriptString(script []byte, devMode bool, variables map[string]string) (*models.DNSConfig, error) {
|
func ExecuteJavascriptString(script []byte, devMode bool, variables map[string]string) (*models.DNSConfig, error) {
|
||||||
|
|
||||||
vm := otto.New()
|
vm := otto.New()
|
||||||
|
|
|
@ -548,7 +548,7 @@ func processSplitHorizonDomains(config *models.DNSConfig) error {
|
||||||
|
|
||||||
func checkAutoDNSSEC(dc *models.DomainConfig) (errs []error) {
|
func checkAutoDNSSEC(dc *models.DomainConfig) (errs []error) {
|
||||||
if dc.AutoDNSSEC == "on" {
|
if dc.AutoDNSSEC == "on" {
|
||||||
for providerName, _ := range dc.DNSProviderNames {
|
for providerName := range dc.DNSProviderNames {
|
||||||
if dc.RegistrarName != providerName {
|
if dc.RegistrarName != providerName {
|
||||||
errs = append(errs, fmt.Errorf("AutoDNSSEC is enabled, but DNS provider %s does not match registrar %s", providerName, dc.RegistrarName))
|
errs = append(errs, fmt.Errorf("AutoDNSSEC is enabled, but DNS provider %s does not match registrar %s", providerName, dc.RegistrarName))
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,10 +6,12 @@ import "github.com/StackExchange/dnscontrol/v3/models"
|
||||||
// a group of records.
|
// a group of records.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
// RecordDB is a container of many model.RecordConfig
|
||||||
type RecordDB = struct {
|
type RecordDB = struct {
|
||||||
labelAndTypeMap map[models.RecordKey]struct{}
|
labelAndTypeMap map[models.RecordKey]struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewFromRecords creates a RecordDB from a list of model.RecordConfig.
|
||||||
func NewFromRecords(recs models.Records) *RecordDB {
|
func NewFromRecords(recs models.Records) *RecordDB {
|
||||||
result := &RecordDB{}
|
result := &RecordDB{}
|
||||||
|
|
||||||
|
|
|
@ -2,10 +2,11 @@ package soautil
|
||||||
|
|
||||||
import "strings"
|
import "strings"
|
||||||
|
|
||||||
|
// RFC5322MailToBind converts a user@host email address to BIND format.
|
||||||
func RFC5322MailToBind(rfc5322Mail string) string {
|
func RFC5322MailToBind(rfc5322Mail string) string {
|
||||||
res := strings.SplitN(rfc5322Mail, "@", 2)
|
res := strings.SplitN(rfc5322Mail, "@", 2)
|
||||||
user_part, domain_part := res[0], res[1]
|
user, domain := res[0], res[1]
|
||||||
// RFC-1035 [Section-8]
|
// RFC-1035 [Section-8]
|
||||||
user_part = strings.ReplaceAll(user_part, ".", "\\.")
|
user = strings.ReplaceAll(user, ".", "\\.")
|
||||||
return user_part + "." + domain_part
|
return user + "." + domain
|
||||||
}
|
}
|
||||||
|
|
|
@ -181,6 +181,7 @@ func (c *bindProvider) GetZoneRecords(domain string) (models.Records, error) {
|
||||||
return ParseZoneContents(string(content), domain, zonefileName)
|
return ParseZoneContents(string(content), domain, zonefileName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ParseZoneContents parses a string as a BIND zone and returns the records.
|
||||||
func ParseZoneContents(content string, zoneName string, zonefileName string) (models.Records, error) {
|
func ParseZoneContents(content string, zoneName string, zonefileName string) (models.Records, error) {
|
||||||
zp := dns.NewZoneParser(strings.NewReader(content), zoneName, zonefileName)
|
zp := dns.NewZoneParser(strings.NewReader(content), zoneName, zonefileName)
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,7 @@ type gcoreRRSetExtended struct {
|
||||||
Filters []dnssdk.RecordFilter `json:"filters"`
|
Filters []dnssdk.RecordFilter `json:"filters"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func dnssdkDo(c *dnssdk.Client, apiKey string, ctx context.Context, method, uri string, bodyParams interface{}, dest interface{}) error {
|
func dnssdkDo(ctx context.Context, c *dnssdk.Client, apiKey string, method, uri string, bodyParams interface{}, dest interface{}) error {
|
||||||
// Adapted from https://github.com/G-Core/gcore-dns-sdk-go/blob/main/client.go#L289
|
// Adapted from https://github.com/G-Core/gcore-dns-sdk-go/blob/main/client.go#L289
|
||||||
// No way to reflect a private method in Golang
|
// No way to reflect a private method in Golang
|
||||||
|
|
||||||
|
@ -96,7 +96,7 @@ func (c *gcoreProvider) dnssdkRRSets(domain string) (gcoreRRSets, error) {
|
||||||
var result gcoreRRSets
|
var result gcoreRRSets
|
||||||
url := fmt.Sprintf("/v2/zones/%s/rrsets?all=true", domain)
|
url := fmt.Sprintf("/v2/zones/%s/rrsets?all=true", domain)
|
||||||
|
|
||||||
err := dnssdkDo(c.provider, c.apiKey, c.ctx, http.MethodGet, url, nil, &result)
|
err := dnssdkDo(c.ctx, c.provider, c.apiKey, http.MethodGet, url, nil, &result)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return gcoreRRSets{}, err
|
return gcoreRRSets{}, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ const endpoint = "%s/api/%s/v1/json/%s"
|
||||||
type hostingdeProvider struct {
|
type hostingdeProvider struct {
|
||||||
authToken string
|
authToken string
|
||||||
ownerAccountID string
|
ownerAccountID string
|
||||||
filterAccountId string
|
filterAccountID string
|
||||||
baseURL string
|
baseURL string
|
||||||
nameservers []string
|
nameservers []string
|
||||||
defaultSoa soaValues
|
defaultSoa soaValues
|
||||||
|
@ -127,7 +127,7 @@ func (hp *hostingdeProvider) updateNameservers(nss []string, domain string) func
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hp *hostingdeProvider) updateZone(zc *zoneConfig, DnsSecOptions *dnsSecOptions, create, del, mod diff.Changeset) error {
|
func (hp *hostingdeProvider) updateZone(zc *zoneConfig, options *dnsSecOptions, create, del, mod diff.Changeset) error {
|
||||||
toAdd := []*record{}
|
toAdd := []*record{}
|
||||||
for _, c := range create {
|
for _, c := range create {
|
||||||
r := recordToNative(c.Desired)
|
r := recordToNative(c.Desired)
|
||||||
|
@ -153,7 +153,7 @@ func (hp *hostingdeProvider) updateZone(zc *zoneConfig, DnsSecOptions *dnsSecOpt
|
||||||
RecordsToAdd: toAdd,
|
RecordsToAdd: toAdd,
|
||||||
RecordsToDelete: toDelete,
|
RecordsToDelete: toDelete,
|
||||||
RecordsToModify: toModify,
|
RecordsToModify: toModify,
|
||||||
DNSSECOptions: DnsSecOptions,
|
DNSSECOptions: options,
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := hp.get("dns", "zoneUpdate", params)
|
_, err := hp.get("dns", "zoneUpdate", params)
|
||||||
|
@ -223,11 +223,11 @@ func (hp *hostingdeProvider) getZoneConfig(domain string) (*zoneConfig, error) {
|
||||||
return zc[0], nil
|
return zc[0], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hp *hostingdeProvider) getDNSSECOptions(zoneConfigId string) (*dnsSecOptions, error) {
|
func (hp *hostingdeProvider) getDNSSECOptions(zoneConfigID string) (*dnsSecOptions, error) {
|
||||||
params := request{
|
params := request{
|
||||||
Filter: &filter{
|
Filter: &filter{
|
||||||
Field: "zoneConfigId",
|
Field: "zoneConfigId",
|
||||||
Value: zoneConfigId,
|
Value: zoneConfigID,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -301,10 +301,10 @@ func (hp *hostingdeProvider) getAllZoneConfigs() ([]*zoneConfig, error) {
|
||||||
params := request{
|
params := request{
|
||||||
Limit: 10000,
|
Limit: 10000,
|
||||||
}
|
}
|
||||||
if hp.filterAccountId != "" {
|
if hp.filterAccountID != "" {
|
||||||
params.Filter = &filter{
|
params.Filter = &filter{
|
||||||
Field: "accountId",
|
Field: "accountId",
|
||||||
Value: hp.filterAccountId,
|
Value: hp.filterAccountID,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,7 +47,7 @@ type providerMeta struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func newHostingde(m map[string]string, providermeta json.RawMessage) (*hostingdeProvider, error) {
|
func newHostingde(m map[string]string, providermeta json.RawMessage) (*hostingdeProvider, error) {
|
||||||
authToken, ownerAccountID, filterAccountId, baseURL := m["authToken"], m["ownerAccountId"], m["filterAccountId"], m["baseURL"]
|
authToken, ownerAccountID, filterAccountID, baseURL := m["authToken"], m["ownerAccountId"], m["filterAccountId"], m["baseURL"]
|
||||||
|
|
||||||
if authToken == "" {
|
if authToken == "" {
|
||||||
return nil, fmt.Errorf("hosting.de: authtoken must be provided")
|
return nil, fmt.Errorf("hosting.de: authtoken must be provided")
|
||||||
|
@ -61,7 +61,7 @@ func newHostingde(m map[string]string, providermeta json.RawMessage) (*hostingde
|
||||||
hp := &hostingdeProvider{
|
hp := &hostingdeProvider{
|
||||||
authToken: authToken,
|
authToken: authToken,
|
||||||
ownerAccountID: ownerAccountID,
|
ownerAccountID: ownerAccountID,
|
||||||
filterAccountId: filterAccountId,
|
filterAccountID: filterAccountID,
|
||||||
baseURL: baseURL,
|
baseURL: baseURL,
|
||||||
nameservers: defaultNameservers,
|
nameservers: defaultNameservers,
|
||||||
}
|
}
|
||||||
|
@ -97,10 +97,10 @@ func (hp *hostingdeProvider) GetZoneRecords(domain string) (models.Records, erro
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return hp.ApiRecordsToStandardRecordsModel(domain, zone.Records), nil
|
return hp.APIRecordsToStandardRecordsModel(domain, zone.Records), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hp *hostingdeProvider) ApiRecordsToStandardRecordsModel(domain string, src []record) models.Records {
|
func (hp *hostingdeProvider) APIRecordsToStandardRecordsModel(domain string, src []record) models.Records {
|
||||||
records := []*models.RecordConfig{}
|
records := []*models.RecordConfig{}
|
||||||
for _, r := range src {
|
for _, r := range src {
|
||||||
if r.Type == "SOA" {
|
if r.Type == "SOA" {
|
||||||
|
@ -138,7 +138,7 @@ func (hp *hostingdeProvider) GetDomainCorrections(dc *models.DomainConfig) ([]*m
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
records := hp.ApiRecordsToStandardRecordsModel(dc.Name, zone.Records)
|
records := hp.APIRecordsToStandardRecordsModel(dc.Name, zone.Records)
|
||||||
|
|
||||||
var create, del, mod diff.Changeset
|
var create, del, mod diff.Changeset
|
||||||
if !diff2.EnableDiff2 {
|
if !diff2.EnableDiff2 {
|
||||||
|
@ -203,7 +203,7 @@ func (hp *hostingdeProvider) GetDomainCorrections(dc *models.DomainConfig) ([]*m
|
||||||
}
|
}
|
||||||
|
|
||||||
if desiredSoa.SoaMbox != "" {
|
if desiredSoa.SoaMbox != "" {
|
||||||
var desiredMail string = ""
|
desiredMail := ""
|
||||||
if desiredSoa.SoaMbox[len(desiredSoa.SoaMbox)-1] != '.' {
|
if desiredSoa.SoaMbox[len(desiredSoa.SoaMbox)-1] != '.' {
|
||||||
desiredMail = desiredSoa.SoaMbox + "@" + dc.Name
|
desiredMail = desiredSoa.SoaMbox + "@" + dc.Name
|
||||||
}
|
}
|
||||||
|
@ -217,33 +217,33 @@ func (hp *hostingdeProvider) GetDomainCorrections(dc *models.DomainConfig) ([]*m
|
||||||
existingAutoDNSSecEnabled := zone.ZoneConfig.DNSSECMode == "automatic"
|
existingAutoDNSSecEnabled := zone.ZoneConfig.DNSSECMode == "automatic"
|
||||||
desiredAutoDNSSecEnabled := dc.AutoDNSSEC == "on"
|
desiredAutoDNSSecEnabled := dc.AutoDNSSEC == "on"
|
||||||
|
|
||||||
var DnsSecOptions *dnsSecOptions
|
var DNSSecOptions *dnsSecOptions
|
||||||
var removeDNSSecEntries []dnsSecEntry
|
var removeDNSSecEntries []dnsSecEntry
|
||||||
|
|
||||||
// ensure that publishKsk is set for domains with AutoDNSSec
|
// ensure that publishKsk is set for domains with AutoDNSSec
|
||||||
if existingAutoDNSSecEnabled && desiredAutoDNSSecEnabled {
|
if existingAutoDNSSecEnabled && desiredAutoDNSSecEnabled {
|
||||||
CurrentDnsSecOptions, err := hp.getDNSSECOptions(zone.ZoneConfig.ID)
|
currentDNSSecOptions, err := hp.getDNSSECOptions(zone.ZoneConfig.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if !CurrentDnsSecOptions.PublishKSK {
|
if !currentDNSSecOptions.PublishKSK {
|
||||||
msg = append(msg, "Enabling publishKsk for AutoDNSSec")
|
msg = append(msg, "Enabling publishKsk for AutoDNSSec")
|
||||||
DnsSecOptions = CurrentDnsSecOptions
|
DNSSecOptions = currentDNSSecOptions
|
||||||
DnsSecOptions.PublishKSK = true
|
DNSSecOptions.PublishKSK = true
|
||||||
zoneChanged = true
|
zoneChanged = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !existingAutoDNSSecEnabled && desiredAutoDNSSecEnabled {
|
if !existingAutoDNSSecEnabled && desiredAutoDNSSecEnabled {
|
||||||
msg = append(msg, "Enable AutoDNSSEC")
|
msg = append(msg, "Enable AutoDNSSEC")
|
||||||
DnsSecOptions = &dnsSecOptions{
|
DNSSecOptions = &dnsSecOptions{
|
||||||
NSECMode: "nsec3",
|
NSECMode: "nsec3",
|
||||||
PublishKSK: true,
|
PublishKSK: true,
|
||||||
}
|
}
|
||||||
zone.ZoneConfig.DNSSECMode = "automatic"
|
zone.ZoneConfig.DNSSECMode = "automatic"
|
||||||
zoneChanged = true
|
zoneChanged = true
|
||||||
} else if existingAutoDNSSecEnabled && !desiredAutoDNSSecEnabled {
|
} else if existingAutoDNSSecEnabled && !desiredAutoDNSSecEnabled {
|
||||||
CurrentDnsSecOptions, err := hp.getDNSSECOptions(zone.ZoneConfig.ID)
|
currentDNSSecOptions, err := hp.getDNSSECOptions(zone.ZoneConfig.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -256,7 +256,7 @@ func (hp *hostingdeProvider) GetDomainCorrections(dc *models.DomainConfig) ([]*m
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for _, entry := range DomainConfig.DNSSecEntries {
|
for _, entry := range DomainConfig.DNSSecEntries {
|
||||||
for _, autoDNSKey := range CurrentDnsSecOptions.Keys {
|
for _, autoDNSKey := range currentDNSSecOptions.Keys {
|
||||||
if entry.KeyData.PublicKey == autoDNSKey.KeyData.PublicKey {
|
if entry.KeyData.PublicKey == autoDNSKey.KeyData.PublicKey {
|
||||||
removeDNSSecEntries = append(removeDNSSecEntries, entry)
|
removeDNSSecEntries = append(removeDNSSecEntries, entry)
|
||||||
}
|
}
|
||||||
|
@ -274,7 +274,7 @@ func (hp *hostingdeProvider) GetDomainCorrections(dc *models.DomainConfig) ([]*m
|
||||||
Msg: fmt.Sprintf("\n%s", strings.Join(msg, "\n")),
|
Msg: fmt.Sprintf("\n%s", strings.Join(msg, "\n")),
|
||||||
F: func() error {
|
F: func() error {
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
err := hp.updateZone(&zone.ZoneConfig, DnsSecOptions, create, del, mod)
|
err := hp.updateZone(&zone.ZoneConfig, DNSSecOptions, create, del, mod)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue