From 169d7c80625b5c8b4287c5dfe95cab61eb110844 Mon Sep 17 00:00:00 2001 From: Tom Limoncelli Date: Mon, 27 Feb 2023 20:28:17 -0500 Subject: [PATCH] CHORE: linting (#2098) --- models/recorddb.go | 5 ++-- pkg/js/js.go | 1 + pkg/normalize/validate.go | 2 +- pkg/recorddb/recorddb.go | 2 ++ pkg/soautil/soautil.go | 7 +++--- providers/bind/bindProvider.go | 1 + providers/gcore/gcoreExtend.go | 4 ++-- providers/hostingde/api.go | 14 +++++------ providers/hostingde/hostingdeProvider.go | 30 ++++++++++++------------ 9 files changed, 35 insertions(+), 31 deletions(-) diff --git a/models/recorddb.go b/models/recorddb.go index 916301684..72cc350ae 100644 --- a/models/recorddb.go +++ b/models/recorddb.go @@ -2,6 +2,8 @@ package models // 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 { labelAndTypeMap map[RecordKey]struct{} } @@ -10,10 +12,8 @@ type RecordDB struct { func NewRecordDBFromRecords(recs Records, zone string) *RecordDB { result := &RecordDB{} - //fmt.Printf("DEBUG: BUILDING RecordDB: zone=%v\n", zone) result.labelAndTypeMap = make(map[RecordKey]struct{}, len(recs)) for _, rec := range recs { - //fmt.Printf(" DEBUG: Adding %+v\n", rec.Key()) result.labelAndTypeMap[rec.Key()] = struct{}{} } //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) func (recdb *RecordDB) ContainsLT(rec *RecordConfig) bool { _, ok := recdb.labelAndTypeMap[rec.Key()] - //fmt.Printf("DEBUG: ContainsLT(%q) = %v (%v)\n", rec.Key(), ok, recdb) return ok } diff --git a/pkg/js/js.go b/pkg/js/js.go index 910dc4431..b88ca5f6a 100644 --- a/pkg/js/js.go +++ b/pkg/js/js.go @@ -48,6 +48,7 @@ func ExecuteJavascript(file string, devMode bool, variables map[string]string) ( 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) { vm := otto.New() diff --git a/pkg/normalize/validate.go b/pkg/normalize/validate.go index 15bfa58b7..9a77ced65 100644 --- a/pkg/normalize/validate.go +++ b/pkg/normalize/validate.go @@ -548,7 +548,7 @@ func processSplitHorizonDomains(config *models.DNSConfig) error { func checkAutoDNSSEC(dc *models.DomainConfig) (errs []error) { if dc.AutoDNSSEC == "on" { - for providerName, _ := range dc.DNSProviderNames { + for providerName := range dc.DNSProviderNames { if dc.RegistrarName != providerName { errs = append(errs, fmt.Errorf("AutoDNSSEC is enabled, but DNS provider %s does not match registrar %s", providerName, dc.RegistrarName)) } diff --git a/pkg/recorddb/recorddb.go b/pkg/recorddb/recorddb.go index 5000100e5..d9b4ddbd4 100644 --- a/pkg/recorddb/recorddb.go +++ b/pkg/recorddb/recorddb.go @@ -6,10 +6,12 @@ import "github.com/StackExchange/dnscontrol/v3/models" // a group of records. // +// RecordDB is a container of many model.RecordConfig type RecordDB = struct { labelAndTypeMap map[models.RecordKey]struct{} } +// NewFromRecords creates a RecordDB from a list of model.RecordConfig. func NewFromRecords(recs models.Records) *RecordDB { result := &RecordDB{} diff --git a/pkg/soautil/soautil.go b/pkg/soautil/soautil.go index e99a930ec..7c6a9d573 100644 --- a/pkg/soautil/soautil.go +++ b/pkg/soautil/soautil.go @@ -2,10 +2,11 @@ package soautil import "strings" +// RFC5322MailToBind converts a user@host email address to BIND format. func RFC5322MailToBind(rfc5322Mail string) string { res := strings.SplitN(rfc5322Mail, "@", 2) - user_part, domain_part := res[0], res[1] + user, domain := res[0], res[1] // RFC-1035 [Section-8] - user_part = strings.ReplaceAll(user_part, ".", "\\.") - return user_part + "." + domain_part + user = strings.ReplaceAll(user, ".", "\\.") + return user + "." + domain } diff --git a/providers/bind/bindProvider.go b/providers/bind/bindProvider.go index 588eb7cdd..d76c7895d 100644 --- a/providers/bind/bindProvider.go +++ b/providers/bind/bindProvider.go @@ -181,6 +181,7 @@ func (c *bindProvider) GetZoneRecords(domain string) (models.Records, error) { 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) { zp := dns.NewZoneParser(strings.NewReader(content), zoneName, zonefileName) diff --git a/providers/gcore/gcoreExtend.go b/providers/gcore/gcoreExtend.go index e40e11ead..b0bfa43d7 100644 --- a/providers/gcore/gcoreExtend.go +++ b/providers/gcore/gcoreExtend.go @@ -28,7 +28,7 @@ type gcoreRRSetExtended struct { 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 // No way to reflect a private method in Golang @@ -96,7 +96,7 @@ func (c *gcoreProvider) dnssdkRRSets(domain string) (gcoreRRSets, error) { var result gcoreRRSets 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 { return gcoreRRSets{}, err } diff --git a/providers/hostingde/api.go b/providers/hostingde/api.go index d832de7aa..ec4d20ad3 100644 --- a/providers/hostingde/api.go +++ b/providers/hostingde/api.go @@ -16,7 +16,7 @@ const endpoint = "%s/api/%s/v1/json/%s" type hostingdeProvider struct { authToken string ownerAccountID string - filterAccountId string + filterAccountID string baseURL string nameservers []string 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{} for _, c := range create { r := recordToNative(c.Desired) @@ -153,7 +153,7 @@ func (hp *hostingdeProvider) updateZone(zc *zoneConfig, DnsSecOptions *dnsSecOpt RecordsToAdd: toAdd, RecordsToDelete: toDelete, RecordsToModify: toModify, - DNSSECOptions: DnsSecOptions, + DNSSECOptions: options, } _, err := hp.get("dns", "zoneUpdate", params) @@ -223,11 +223,11 @@ func (hp *hostingdeProvider) getZoneConfig(domain string) (*zoneConfig, error) { return zc[0], nil } -func (hp *hostingdeProvider) getDNSSECOptions(zoneConfigId string) (*dnsSecOptions, error) { +func (hp *hostingdeProvider) getDNSSECOptions(zoneConfigID string) (*dnsSecOptions, error) { params := request{ Filter: &filter{ Field: "zoneConfigId", - Value: zoneConfigId, + Value: zoneConfigID, }, } @@ -301,10 +301,10 @@ func (hp *hostingdeProvider) getAllZoneConfigs() ([]*zoneConfig, error) { params := request{ Limit: 10000, } - if hp.filterAccountId != "" { + if hp.filterAccountID != "" { params.Filter = &filter{ Field: "accountId", - Value: hp.filterAccountId, + Value: hp.filterAccountID, } } diff --git a/providers/hostingde/hostingdeProvider.go b/providers/hostingde/hostingdeProvider.go index 9d74713c9..f39afd478 100644 --- a/providers/hostingde/hostingdeProvider.go +++ b/providers/hostingde/hostingdeProvider.go @@ -47,7 +47,7 @@ type providerMeta struct { } 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 == "" { 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{ authToken: authToken, ownerAccountID: ownerAccountID, - filterAccountId: filterAccountId, + filterAccountID: filterAccountID, baseURL: baseURL, nameservers: defaultNameservers, } @@ -97,10 +97,10 @@ func (hp *hostingdeProvider) GetZoneRecords(domain string) (models.Records, erro if err != nil { 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{} for _, r := range src { if r.Type == "SOA" { @@ -138,7 +138,7 @@ func (hp *hostingdeProvider) GetDomainCorrections(dc *models.DomainConfig) ([]*m return nil, err } - records := hp.ApiRecordsToStandardRecordsModel(dc.Name, zone.Records) + records := hp.APIRecordsToStandardRecordsModel(dc.Name, zone.Records) var create, del, mod diff.Changeset if !diff2.EnableDiff2 { @@ -203,7 +203,7 @@ func (hp *hostingdeProvider) GetDomainCorrections(dc *models.DomainConfig) ([]*m } if desiredSoa.SoaMbox != "" { - var desiredMail string = "" + desiredMail := "" if desiredSoa.SoaMbox[len(desiredSoa.SoaMbox)-1] != '.' { desiredMail = desiredSoa.SoaMbox + "@" + dc.Name } @@ -217,33 +217,33 @@ func (hp *hostingdeProvider) GetDomainCorrections(dc *models.DomainConfig) ([]*m existingAutoDNSSecEnabled := zone.ZoneConfig.DNSSECMode == "automatic" desiredAutoDNSSecEnabled := dc.AutoDNSSEC == "on" - var DnsSecOptions *dnsSecOptions + var DNSSecOptions *dnsSecOptions var removeDNSSecEntries []dnsSecEntry // ensure that publishKsk is set for domains with AutoDNSSec if existingAutoDNSSecEnabled && desiredAutoDNSSecEnabled { - CurrentDnsSecOptions, err := hp.getDNSSECOptions(zone.ZoneConfig.ID) + currentDNSSecOptions, err := hp.getDNSSECOptions(zone.ZoneConfig.ID) if err != nil { return nil, err } - if !CurrentDnsSecOptions.PublishKSK { + if !currentDNSSecOptions.PublishKSK { msg = append(msg, "Enabling publishKsk for AutoDNSSec") - DnsSecOptions = CurrentDnsSecOptions - DnsSecOptions.PublishKSK = true + DNSSecOptions = currentDNSSecOptions + DNSSecOptions.PublishKSK = true zoneChanged = true } } if !existingAutoDNSSecEnabled && desiredAutoDNSSecEnabled { msg = append(msg, "Enable AutoDNSSEC") - DnsSecOptions = &dnsSecOptions{ + DNSSecOptions = &dnsSecOptions{ NSECMode: "nsec3", PublishKSK: true, } zone.ZoneConfig.DNSSECMode = "automatic" zoneChanged = true } else if existingAutoDNSSecEnabled && !desiredAutoDNSSecEnabled { - CurrentDnsSecOptions, err := hp.getDNSSECOptions(zone.ZoneConfig.ID) + currentDNSSecOptions, err := hp.getDNSSECOptions(zone.ZoneConfig.ID) if err != nil { return nil, err } @@ -256,7 +256,7 @@ func (hp *hostingdeProvider) GetDomainCorrections(dc *models.DomainConfig) ([]*m return nil, err } for _, entry := range DomainConfig.DNSSecEntries { - for _, autoDNSKey := range CurrentDnsSecOptions.Keys { + for _, autoDNSKey := range currentDNSSecOptions.Keys { if entry.KeyData.PublicKey == autoDNSKey.KeyData.PublicKey { 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")), F: func() error { 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 { return nil }