From 974a06d8de24536190a2bd5433e98ce15b4ca387 Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Wed, 20 Dec 2023 17:26:46 +0000 Subject: [PATCH 01/16] vscode config changed in new version --- .vscode/settings.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 4f04c36b9..5dd5d8d38 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,8 +5,8 @@ "editor.formatOnSave": true, "editor.formatOnPaste": true, "editor.codeActionsOnSave": { - "source.organizeImports": true, - "source.fixAll": true + "source.organizeImports": "explicit", + "source.fixAll": "explicit" }, "editor.rulers": [120], "go.showWelcome": false, From e8401a799e83b7ddb04ad4d9b72031fd6f659731 Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Wed, 20 Dec 2023 17:31:21 +0000 Subject: [PATCH 02/16] FilteringConfig -> Filtering --- config/config.go | 2 +- config/filtering.go | 6 +++--- config/filtering_test.go | 8 ++++---- resolver/filtering_resolver.go | 4 ++-- resolver/filtering_resolver_test.go | 6 +++--- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/config/config.go b/config/config.go index c25252583..546b924d1 100644 --- a/config/config.go +++ b/config/config.go @@ -230,7 +230,7 @@ type Config struct { BootstrapDNS BootstrapDNSConfig `yaml:"bootstrapDns"` HostsFile HostsFileConfig `yaml:"hostsFile"` FQDNOnly FQDNOnly `yaml:"fqdnOnly"` - Filtering FilteringConfig `yaml:"filtering"` + Filtering Filtering `yaml:"filtering"` EDE EDE `yaml:"ede"` ECS ECS `yaml:"ecs"` SUDN SUDN `yaml:"specialUseDomains"` diff --git a/config/filtering.go b/config/filtering.go index dcc37ac72..6902f5954 100644 --- a/config/filtering.go +++ b/config/filtering.go @@ -4,17 +4,17 @@ import ( "github.com/sirupsen/logrus" ) -type FilteringConfig struct { +type Filtering struct { QueryTypes QTypeSet `yaml:"queryTypes"` } // IsEnabled implements `config.Configurable`. -func (c *FilteringConfig) IsEnabled() bool { +func (c *Filtering) IsEnabled() bool { return len(c.QueryTypes) != 0 } // LogConfig implements `config.Configurable`. -func (c *FilteringConfig) LogConfig(logger *logrus.Entry) { +func (c *Filtering) LogConfig(logger *logrus.Entry) { logger.Info("query types:") for qType := range c.QueryTypes { diff --git a/config/filtering_test.go b/config/filtering_test.go index 794cf1266..3cd1edfef 100644 --- a/config/filtering_test.go +++ b/config/filtering_test.go @@ -9,19 +9,19 @@ import ( ) var _ = Describe("FilteringConfig", func() { - var cfg FilteringConfig + var cfg Filtering suiteBeforeEach() BeforeEach(func() { - cfg = FilteringConfig{ + cfg = Filtering{ QueryTypes: NewQTypeSet(AAAA, MX), } }) Describe("IsEnabled", func() { It("should be false by default", func() { - cfg := FilteringConfig{} + cfg := Filtering{} Expect(defaults.Set(&cfg)).Should(Succeed()) Expect(cfg.IsEnabled()).Should(BeFalse()) @@ -35,7 +35,7 @@ var _ = Describe("FilteringConfig", func() { When("disabled", func() { It("should be false", func() { - cfg := FilteringConfig{} + cfg := Filtering{} Expect(cfg.IsEnabled()).Should(BeFalse()) }) diff --git a/resolver/filtering_resolver.go b/resolver/filtering_resolver.go index c6344efd8..370de979c 100644 --- a/resolver/filtering_resolver.go +++ b/resolver/filtering_resolver.go @@ -11,12 +11,12 @@ import ( // FilteringResolver filters DNS queries (for example can drop all AAAA query) // returns empty ANSWER with NOERROR type FilteringResolver struct { - configurable[*config.FilteringConfig] + configurable[*config.Filtering] NextResolver typed } -func NewFilteringResolver(cfg config.FilteringConfig) *FilteringResolver { +func NewFilteringResolver(cfg config.Filtering) *FilteringResolver { return &FilteringResolver{ configurable: withConfig(&cfg), typed: withType("filtering"), diff --git a/resolver/filtering_resolver_test.go b/resolver/filtering_resolver_test.go index cccadd2e2..79b60ba97 100644 --- a/resolver/filtering_resolver_test.go +++ b/resolver/filtering_resolver_test.go @@ -17,7 +17,7 @@ import ( var _ = Describe("FilteringResolver", func() { var ( sut *FilteringResolver - sutConfig config.FilteringConfig + sutConfig config.Filtering m *mockResolver mockAnswer *dns.Msg @@ -63,7 +63,7 @@ var _ = Describe("FilteringResolver", func() { When("Filtering query types are defined", func() { BeforeEach(func() { - sutConfig = config.FilteringConfig{ + sutConfig = config.Filtering{ QueryTypes: config.NewQTypeSet(AAAA, MX), } }) @@ -95,7 +95,7 @@ var _ = Describe("FilteringResolver", func() { When("No filtering query types are defined", func() { BeforeEach(func() { - sutConfig = config.FilteringConfig{} + sutConfig = config.Filtering{} }) It("Should return empty answer without error", func() { Expect(sut.Resolve(ctx, newRequest("example.com.", AAAA))). From 320d0b2114706f7f290cbc073fdc99f8728f7bd6 Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Wed, 20 Dec 2023 17:32:26 +0000 Subject: [PATCH 03/16] CachingConfig -> Caching --- config/caching.go | 10 +++++----- config/caching_test.go | 14 +++++++------- config/config.go | 2 +- resolver/caching_resolver.go | 8 ++++---- resolver/caching_resolver_test.go | 20 ++++++++++---------- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/config/caching.go b/config/caching.go index 0d86a3832..49aa98b3e 100644 --- a/config/caching.go +++ b/config/caching.go @@ -6,8 +6,8 @@ import ( "github.com/sirupsen/logrus" ) -// CachingConfig configuration for domain caching -type CachingConfig struct { +// Caching configuration for domain caching +type Caching struct { MinCachingTime Duration `yaml:"minTime"` MaxCachingTime Duration `yaml:"maxTime"` CacheTimeNegative Duration `yaml:"cacheTimeNegative" default:"30m"` @@ -19,12 +19,12 @@ type CachingConfig struct { } // IsEnabled implements `config.Configurable`. -func (c *CachingConfig) IsEnabled() bool { +func (c *Caching) IsEnabled() bool { return c.MaxCachingTime.IsAtLeastZero() } // LogConfig implements `config.Configurable`. -func (c *CachingConfig) LogConfig(logger *logrus.Entry) { +func (c *Caching) LogConfig(logger *logrus.Entry) { logger.Infof("minTime = %s", c.MinCachingTime) logger.Infof("maxTime = %s", c.MaxCachingTime) logger.Infof("cacheTimeNegative = %s", c.CacheTimeNegative) @@ -39,7 +39,7 @@ func (c *CachingConfig) LogConfig(logger *logrus.Entry) { } } -func (c *CachingConfig) EnablePrefetch() { +func (c *Caching) EnablePrefetch() { const day = Duration(24 * time.Hour) if !c.IsEnabled() { diff --git a/config/caching_test.go b/config/caching_test.go index 8be9dace0..80a1241ac 100644 --- a/config/caching_test.go +++ b/config/caching_test.go @@ -9,19 +9,19 @@ import ( ) var _ = Describe("CachingConfig", func() { - var cfg CachingConfig + var cfg Caching suiteBeforeEach() BeforeEach(func() { - cfg = CachingConfig{ + cfg = Caching{ MaxCachingTime: Duration(time.Hour), } }) Describe("IsEnabled", func() { It("should be true by default", func() { - cfg := CachingConfig{} + cfg := Caching{} Expect(defaults.Set(&cfg)).Should(Succeed()) Expect(cfg.IsEnabled()).Should(BeTrue()) @@ -29,7 +29,7 @@ var _ = Describe("CachingConfig", func() { When("the config is disabled", func() { BeforeEach(func() { - cfg = CachingConfig{ + cfg = Caching{ MaxCachingTime: Duration(time.Hour * -1), } }) @@ -46,7 +46,7 @@ var _ = Describe("CachingConfig", func() { When("the config is disabled", func() { It("should be false", func() { - cfg := CachingConfig{ + cfg := Caching{ MaxCachingTime: Duration(-1), } @@ -58,7 +58,7 @@ var _ = Describe("CachingConfig", func() { Describe("LogConfig", func() { When("prefetching is enabled", func() { BeforeEach(func() { - cfg = CachingConfig{ + cfg = Caching{ Prefetching: true, } }) @@ -75,7 +75,7 @@ var _ = Describe("CachingConfig", func() { Describe("EnablePrefetch", func() { When("prefetching is enabled", func() { BeforeEach(func() { - cfg = CachingConfig{} + cfg = Caching{} }) It("should return configuration", func() { diff --git a/config/config.go b/config/config.go index 546b924d1..250b770dc 100644 --- a/config/config.go +++ b/config/config.go @@ -218,7 +218,7 @@ type Config struct { Conditional ConditionalUpstream `yaml:"conditional"` Blocking Blocking `yaml:"blocking"` ClientLookup ClientLookup `yaml:"clientLookup"` - Caching CachingConfig `yaml:"caching"` + Caching Caching `yaml:"caching"` QueryLog QueryLogConfig `yaml:"queryLog"` Prometheus MetricsConfig `yaml:"prometheus"` Redis Redis `yaml:"redis"` diff --git a/resolver/caching_resolver.go b/resolver/caching_resolver.go index d10ae2782..ed056c174 100644 --- a/resolver/caching_resolver.go +++ b/resolver/caching_resolver.go @@ -24,7 +24,7 @@ const defaultCachingCleanUpInterval = 5 * time.Second // CachingResolver caches answers from dns queries with their TTL time, // to avoid external resolver calls for recurrent queries type CachingResolver struct { - configurable[*config.CachingConfig] + configurable[*config.Caching] NextResolver typed @@ -37,14 +37,14 @@ type CachingResolver struct { // NewCachingResolver creates a new resolver instance func NewCachingResolver(ctx context.Context, - cfg config.CachingConfig, + cfg config.Caching, redis *redis.Client, ) *CachingResolver { return newCachingResolver(ctx, cfg, redis, true) } func newCachingResolver(ctx context.Context, - cfg config.CachingConfig, + cfg config.Caching, redis *redis.Client, emitMetricEvents bool, ) *CachingResolver { @@ -66,7 +66,7 @@ func newCachingResolver(ctx context.Context, return c } -func configureCaches(ctx context.Context, c *CachingResolver, cfg *config.CachingConfig) { +func configureCaches(ctx context.Context, c *CachingResolver, cfg *config.Caching) { options := expirationcache.Options{ CleanupInterval: defaultCachingCleanUpInterval, MaxSize: uint(cfg.MaxItemsCount), diff --git a/resolver/caching_resolver_test.go b/resolver/caching_resolver_test.go index 3f88ef4b8..5081ab543 100644 --- a/resolver/caching_resolver_test.go +++ b/resolver/caching_resolver_test.go @@ -25,7 +25,7 @@ import ( var _ = Describe("CachingResolver", func() { var ( sut *CachingResolver - sutConfig config.CachingConfig + sutConfig config.Caching m *mockResolver mockAnswer *dns.Msg ctx context.Context @@ -39,7 +39,7 @@ var _ = Describe("CachingResolver", func() { }) BeforeEach(func() { - sutConfig = config.CachingConfig{} + sutConfig = config.Caching{} if err := defaults.Set(&sutConfig); err != nil { panic(err) } @@ -63,7 +63,7 @@ var _ = Describe("CachingResolver", func() { When("max caching time is negative", func() { BeforeEach(func() { - sutConfig = config.CachingConfig{ + sutConfig = config.Caching{ MaxCachingTime: config.Duration(time.Minute * -1), } }) @@ -86,7 +86,7 @@ var _ = Describe("CachingResolver", func() { Describe("Caching responses", func() { When("prefetching is enabled", func() { BeforeEach(func() { - sutConfig = config.CachingConfig{ + sutConfig = config.Caching{ Prefetching: true, PrefetchExpires: config.Duration(time.Minute * 120), PrefetchThreshold: 5, @@ -198,7 +198,7 @@ var _ = Describe("CachingResolver", func() { }) When("min caching time is defined", func() { BeforeEach(func() { - sutConfig = config.CachingConfig{ + sutConfig = config.Caching{ MinCachingTime: config.Duration(time.Minute * 5), } }) @@ -341,7 +341,7 @@ var _ = Describe("CachingResolver", func() { }) Context("max caching time is negative -> caching is disabled", func() { BeforeEach(func() { - sutConfig = config.CachingConfig{ + sutConfig = config.Caching{ MaxCachingTime: config.Duration(time.Minute * -1), } }) @@ -378,7 +378,7 @@ var _ = Describe("CachingResolver", func() { Context("max caching time is positive", func() { BeforeEach(func() { - sutConfig = config.CachingConfig{ + sutConfig = config.Caching{ MaxCachingTime: config.Duration(time.Minute * 4), } }) @@ -419,7 +419,7 @@ var _ = Describe("CachingResolver", func() { }) Context("max caching time is defined", func() { BeforeEach(func() { - sutConfig = config.CachingConfig{ + sutConfig = config.Caching{ MaxCachingTime: config.Duration(time.Minute * 1), } }) @@ -500,7 +500,7 @@ var _ = Describe("CachingResolver", func() { When("Upstream resolver returns NXDOMAIN without caching", func() { BeforeEach(func() { mockAnswer.Rcode = dns.RcodeNameError - sutConfig = config.CachingConfig{ + sutConfig = config.Caching{ CacheTimeNegative: config.Duration(time.Minute * -1), } }) @@ -758,7 +758,7 @@ var _ = Describe("CachingResolver", func() { }) When("cache", func() { JustBeforeEach(func() { - sutConfig = config.CachingConfig{ + sutConfig = config.Caching{ MaxCachingTime: config.Duration(time.Second * 10), } mockAnswer, _ = util.NewMsgWithAnswer("example.com.", 1000, A, "1.1.1.1") From ae9217a58d4f0dbfc5d05d2af6840ecd18453e8e Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Wed, 20 Dec 2023 17:33:14 +0000 Subject: [PATCH 04/16] QueryLogConfig -> QueryLog --- config/config.go | 2 +- config/query_log.go | 10 +++++----- config/query_log_test.go | 10 +++++----- resolver/query_logging_resolver.go | 4 ++-- resolver/query_logging_resolver_test.go | 20 ++++++++++---------- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/config/config.go b/config/config.go index 250b770dc..04de47c25 100644 --- a/config/config.go +++ b/config/config.go @@ -219,7 +219,7 @@ type Config struct { Blocking Blocking `yaml:"blocking"` ClientLookup ClientLookup `yaml:"clientLookup"` Caching Caching `yaml:"caching"` - QueryLog QueryLogConfig `yaml:"queryLog"` + QueryLog QueryLog `yaml:"queryLog"` Prometheus MetricsConfig `yaml:"prometheus"` Redis Redis `yaml:"redis"` Log log.Config `yaml:"log"` diff --git a/config/query_log.go b/config/query_log.go index a0ecd055a..616ecdfd8 100644 --- a/config/query_log.go +++ b/config/query_log.go @@ -4,8 +4,8 @@ import ( "github.com/sirupsen/logrus" ) -// QueryLogConfig configuration for the query logging -type QueryLogConfig struct { +// QueryLog configuration for the query logging +type QueryLog struct { Target string `yaml:"target"` Type QueryLogType `yaml:"type"` LogRetentionDays uint64 `yaml:"logRetentionDays"` @@ -16,19 +16,19 @@ type QueryLogConfig struct { } // SetDefaults implements `defaults.Setter`. -func (c *QueryLogConfig) SetDefaults() { +func (c *QueryLog) SetDefaults() { // Since the default depends on the enum values, set it dynamically // to avoid having to repeat the values in the annotation. c.Fields = QueryLogFieldValues() } // IsEnabled implements `config.Configurable`. -func (c *QueryLogConfig) IsEnabled() bool { +func (c *QueryLog) IsEnabled() bool { return c.Type != QueryLogTypeNone } // LogConfig implements `config.Configurable`. -func (c *QueryLogConfig) LogConfig(logger *logrus.Entry) { +func (c *QueryLog) LogConfig(logger *logrus.Entry) { logger.Infof("type: %s", c.Type) if c.Target != "" { diff --git a/config/query_log_test.go b/config/query_log_test.go index 582fa34b1..e632fc6c7 100644 --- a/config/query_log_test.go +++ b/config/query_log_test.go @@ -9,12 +9,12 @@ import ( ) var _ = Describe("QueryLogConfig", func() { - var cfg QueryLogConfig + var cfg QueryLog suiteBeforeEach() BeforeEach(func() { - cfg = QueryLogConfig{ + cfg = QueryLog{ Target: "/dev/null", Type: QueryLogTypeCsvClient, LogRetentionDays: 0, @@ -25,7 +25,7 @@ var _ = Describe("QueryLogConfig", func() { Describe("IsEnabled", func() { It("should be true by default", func() { - cfg := QueryLogConfig{} + cfg := QueryLog{} Expect(defaults.Set(&cfg)).Should(Succeed()) Expect(cfg.IsEnabled()).Should(BeTrue()) @@ -39,7 +39,7 @@ var _ = Describe("QueryLogConfig", func() { When("disabled", func() { It("should be false", func() { - cfg := QueryLogConfig{ + cfg := QueryLog{ Type: QueryLogTypeNone, } @@ -59,7 +59,7 @@ var _ = Describe("QueryLogConfig", func() { Describe("SetDefaults", func() { It("should log configuration", func() { - cfg := QueryLogConfig{} + cfg := QueryLog{} Expect(cfg.Fields).Should(BeEmpty()) Expect(defaults.Set(&cfg)).Should(Succeed()) diff --git a/resolver/query_logging_resolver.go b/resolver/query_logging_resolver.go index 106d2f764..3a229df74 100644 --- a/resolver/query_logging_resolver.go +++ b/resolver/query_logging_resolver.go @@ -21,7 +21,7 @@ const ( // QueryLoggingResolver writes query information (question, answer, duration, ...) type QueryLoggingResolver struct { - configurable[*config.QueryLogConfig] + configurable[*config.QueryLog] NextResolver typed @@ -30,7 +30,7 @@ type QueryLoggingResolver struct { } // NewQueryLoggingResolver returns a new resolver instance -func NewQueryLoggingResolver(ctx context.Context, cfg config.QueryLogConfig) *QueryLoggingResolver { +func NewQueryLoggingResolver(ctx context.Context, cfg config.QueryLog) *QueryLoggingResolver { logger := log.PrefixedLog(queryLoggingResolverType) var writer querylog.Writer diff --git a/resolver/query_logging_resolver_test.go b/resolver/query_logging_resolver_test.go index a2de61579..4e7968a38 100644 --- a/resolver/query_logging_resolver_test.go +++ b/resolver/query_logging_resolver_test.go @@ -40,7 +40,7 @@ func (m *SlowMockWriter) CleanUp() { var _ = Describe("QueryLoggingResolver", func() { var ( sut *QueryLoggingResolver - sutConfig config.QueryLogConfig + sutConfig config.QueryLog m *mockResolver tmpDir *TmpFolder mockAnswer *dns.Msg @@ -93,7 +93,7 @@ var _ = Describe("QueryLoggingResolver", func() { Describe("Process request", func() { When("Resolver has no configuration", func() { BeforeEach(func() { - sutConfig = config.QueryLogConfig{ + sutConfig = config.QueryLog{ CreationAttempts: 1, CreationCooldown: config.Duration(time.Millisecond), } @@ -111,7 +111,7 @@ var _ = Describe("QueryLoggingResolver", func() { }) When("Configuration with logging per client", func() { BeforeEach(func() { - sutConfig = config.QueryLogConfig{ + sutConfig = config.QueryLog{ Target: tmpDir.Path, Type: config.QueryLogTypeCsvClient, CreationAttempts: 1, @@ -179,7 +179,7 @@ var _ = Describe("QueryLoggingResolver", func() { }) When("Configuration with logging in one file for all clients", func() { BeforeEach(func() { - sutConfig = config.QueryLogConfig{ + sutConfig = config.QueryLog{ Target: tmpDir.Path, Type: config.QueryLogTypeCsv, CreationAttempts: 1, @@ -239,7 +239,7 @@ var _ = Describe("QueryLoggingResolver", func() { }) When("Configuration with specific fields to log", func() { BeforeEach(func() { - sutConfig = config.QueryLogConfig{ + sutConfig = config.QueryLog{ Target: tmpDir.Path, Type: config.QueryLogTypeCsv, CreationAttempts: 1, @@ -287,7 +287,7 @@ var _ = Describe("QueryLoggingResolver", func() { Describe("Slow writer", func() { When("writer is too slow", func() { BeforeEach(func() { - sutConfig = config.QueryLogConfig{ + sutConfig = config.QueryLog{ Type: config.QueryLogTypeNone, CreationAttempts: 1, CreationCooldown: config.Duration(time.Millisecond), @@ -310,7 +310,7 @@ var _ = Describe("QueryLoggingResolver", func() { Describe("Clean up of query log directory", func() { When("fallback logger is enabled, log retention is enabled", func() { BeforeEach(func() { - sutConfig = config.QueryLogConfig{ + sutConfig = config.QueryLog{ LogRetentionDays: 7, Type: config.QueryLogTypeConsole, CreationAttempts: 1, @@ -323,7 +323,7 @@ var _ = Describe("QueryLoggingResolver", func() { }) When("log directory contains old files", func() { BeforeEach(func() { - sutConfig = config.QueryLogConfig{ + sutConfig = config.QueryLog{ Target: tmpDir.Path, Type: config.QueryLogTypeCsv, LogRetentionDays: 7, @@ -352,7 +352,7 @@ var _ = Describe("QueryLoggingResolver", func() { Describe("Wrong target configuration", func() { When("mysql database path is wrong", func() { BeforeEach(func() { - sutConfig = config.QueryLogConfig{ + sutConfig = config.QueryLog{ Target: "dummy", Type: config.QueryLogTypeMysql, CreationAttempts: 1, @@ -366,7 +366,7 @@ var _ = Describe("QueryLoggingResolver", func() { When("postgresql database path is wrong", func() { BeforeEach(func() { - sutConfig = config.QueryLogConfig{ + sutConfig = config.QueryLog{ Target: "dummy", Type: config.QueryLogTypePostgresql, CreationAttempts: 1, From e3fcf79aa261b81f89f66c6f2a1ea27fbd64438f Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Wed, 20 Dec 2023 17:34:15 +0000 Subject: [PATCH 05/16] MetricsConfig -> Metrics --- config/config.go | 2 +- config/metrics.go | 8 ++++---- config/metrics_test.go | 8 ++++---- metrics/metrics.go | 2 +- resolver/metrics_resolver.go | 4 ++-- resolver/metrics_resolver_test.go | 2 +- server/server_test.go | 2 +- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/config/config.go b/config/config.go index 04de47c25..31aefc4f9 100644 --- a/config/config.go +++ b/config/config.go @@ -220,7 +220,7 @@ type Config struct { ClientLookup ClientLookup `yaml:"clientLookup"` Caching Caching `yaml:"caching"` QueryLog QueryLog `yaml:"queryLog"` - Prometheus MetricsConfig `yaml:"prometheus"` + Prometheus Metrics `yaml:"prometheus"` Redis Redis `yaml:"redis"` Log log.Config `yaml:"log"` Ports PortsConfig `yaml:"ports"` diff --git a/config/metrics.go b/config/metrics.go index 69d9e4f7e..a7d66c622 100644 --- a/config/metrics.go +++ b/config/metrics.go @@ -2,18 +2,18 @@ package config import "github.com/sirupsen/logrus" -// MetricsConfig contains the config values for prometheus -type MetricsConfig struct { +// Metrics contains the config values for prometheus +type Metrics struct { Enable bool `yaml:"enable" default:"false"` Path string `yaml:"path" default:"/metrics"` } // IsEnabled implements `config.Configurable`. -func (c *MetricsConfig) IsEnabled() bool { +func (c *Metrics) IsEnabled() bool { return c.Enable } // LogConfig implements `config.Configurable`. -func (c *MetricsConfig) LogConfig(logger *logrus.Entry) { +func (c *Metrics) LogConfig(logger *logrus.Entry) { logger.Infof("url path: %s", c.Path) } diff --git a/config/metrics_test.go b/config/metrics_test.go index b5f968b2f..9b75f353a 100644 --- a/config/metrics_test.go +++ b/config/metrics_test.go @@ -7,12 +7,12 @@ import ( ) var _ = Describe("MetricsConfig", func() { - var cfg MetricsConfig + var cfg Metrics suiteBeforeEach() BeforeEach(func() { - cfg = MetricsConfig{ + cfg = Metrics{ Enable: true, Path: "/custom/path", } @@ -20,7 +20,7 @@ var _ = Describe("MetricsConfig", func() { Describe("IsEnabled", func() { It("should be false by default", func() { - cfg := MetricsConfig{} + cfg := Metrics{} Expect(defaults.Set(&cfg)).Should(Succeed()) Expect(cfg.IsEnabled()).Should(BeFalse()) @@ -34,7 +34,7 @@ var _ = Describe("MetricsConfig", func() { When("disabled", func() { It("should be false", func() { - cfg := MetricsConfig{} + cfg := Metrics{} Expect(cfg.IsEnabled()).Should(BeFalse()) }) diff --git a/metrics/metrics.go b/metrics/metrics.go index a35c7526a..c8767917a 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -18,7 +18,7 @@ func RegisterMetric(c prometheus.Collector) { } // Start starts prometheus endpoint -func Start(router *chi.Mux, cfg config.MetricsConfig) { +func Start(router *chi.Mux, cfg config.Metrics) { if cfg.Enable { _ = reg.Register(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{})) _ = reg.Register(collectors.NewGoCollector()) diff --git a/resolver/metrics_resolver.go b/resolver/metrics_resolver.go index 80894e6d4..48b85d46b 100644 --- a/resolver/metrics_resolver.go +++ b/resolver/metrics_resolver.go @@ -15,7 +15,7 @@ import ( // MetricsResolver resolver that records metrics about requests/response type MetricsResolver struct { - configurable[*config.MetricsConfig] + configurable[*config.Metrics] NextResolver typed @@ -59,7 +59,7 @@ func (r *MetricsResolver) Resolve(ctx context.Context, request *model.Request) ( } // NewMetricsResolver creates a new intance of the MetricsResolver type -func NewMetricsResolver(cfg config.MetricsConfig) *MetricsResolver { +func NewMetricsResolver(cfg config.Metrics) *MetricsResolver { m := MetricsResolver{ configurable: withConfig(&cfg), typed: withType("metrics"), diff --git a/resolver/metrics_resolver_test.go b/resolver/metrics_resolver_test.go index d62cf5973..878218586 100644 --- a/resolver/metrics_resolver_test.go +++ b/resolver/metrics_resolver_test.go @@ -37,7 +37,7 @@ var _ = Describe("MetricResolver", func() { ctx, cancelFn = context.WithCancel(context.Background()) DeferCleanup(cancelFn) - sut = NewMetricsResolver(config.MetricsConfig{Enable: true}) + sut = NewMetricsResolver(config.Metrics{Enable: true}) m = &mockResolver{} m.On("Resolve", mock.Anything).Return(&Response{Res: new(dns.Msg)}, nil) sut.Next(m) diff --git a/server/server_test.go b/server/server_test.go index b0cf2eb0e..3f2fa7f88 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -156,7 +156,7 @@ var _ = BeforeSuite(func() { }, CertFile: certPem.Path, KeyFile: keyPem.Path, - Prometheus: config.MetricsConfig{ + Prometheus: config.Metrics{ Enable: true, Path: "/metrics", }, From f38f59cca68713ab444a22ec8185c45196412537 Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Wed, 20 Dec 2023 17:37:02 +0000 Subject: [PATCH 06/16] HostsFileConfig -> HostsFile --- config/config.go | 2 +- config/hosts_file.go | 8 ++++---- config/hosts_file_test.go | 10 +++++----- resolver/hosts_file_resolver.go | 4 ++-- resolver/hosts_file_resolver_test.go | 6 +++--- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/config/config.go b/config/config.go index 31aefc4f9..945907a19 100644 --- a/config/config.go +++ b/config/config.go @@ -228,7 +228,7 @@ type Config struct { CertFile string `yaml:"certFile"` KeyFile string `yaml:"keyFile"` BootstrapDNS BootstrapDNSConfig `yaml:"bootstrapDns"` - HostsFile HostsFileConfig `yaml:"hostsFile"` + HostsFile HostsFile `yaml:"hostsFile"` FQDNOnly FQDNOnly `yaml:"fqdnOnly"` Filtering Filtering `yaml:"filtering"` EDE EDE `yaml:"ede"` diff --git a/config/hosts_file.go b/config/hosts_file.go index d3c7230ab..895f89d52 100644 --- a/config/hosts_file.go +++ b/config/hosts_file.go @@ -6,7 +6,7 @@ import ( "github.com/sirupsen/logrus" ) -type HostsFileConfig struct { +type HostsFile struct { Sources []BytesSource `yaml:"sources"` HostsTTL Duration `yaml:"hostsTTL" default:"1h"` FilterLoopback bool `yaml:"filterLoopback"` @@ -19,7 +19,7 @@ type HostsFileConfig struct { } `yaml:",inline"` } -func (c *HostsFileConfig) migrate(logger *logrus.Entry) bool { +func (c *HostsFile) migrate(logger *logrus.Entry) bool { return Migrate(logger, "hostsFile", c.Deprecated, map[string]Migrator{ "refreshPeriod": Move(To("loading.refreshPeriod", &c.Loading)), "filePath": Apply(To("sources", c), func(value BytesSource) { @@ -29,12 +29,12 @@ func (c *HostsFileConfig) migrate(logger *logrus.Entry) bool { } // IsEnabled implements `config.Configurable`. -func (c *HostsFileConfig) IsEnabled() bool { +func (c *HostsFile) IsEnabled() bool { return len(c.Sources) != 0 } // LogConfig implements `config.Configurable`. -func (c *HostsFileConfig) LogConfig(logger *logrus.Entry) { +func (c *HostsFile) LogConfig(logger *logrus.Entry) { logger.Infof("TTL: %s", c.HostsTTL) logger.Infof("filter loopback addresses: %t", c.FilterLoopback) diff --git a/config/hosts_file_test.go b/config/hosts_file_test.go index d364a6462..cee2b92da 100644 --- a/config/hosts_file_test.go +++ b/config/hosts_file_test.go @@ -9,12 +9,12 @@ import ( ) var _ = Describe("HostsFileConfig", func() { - var cfg HostsFileConfig + var cfg HostsFile suiteBeforeEach() BeforeEach(func() { - cfg = HostsFileConfig{ + cfg = HostsFile{ Sources: append( NewBytesSources("/a/file/path"), TextBytesSource("127.0.0.1 localhost"), @@ -27,7 +27,7 @@ var _ = Describe("HostsFileConfig", func() { Describe("IsEnabled", func() { It("should be false by default", func() { - cfg := HostsFileConfig{} + cfg := HostsFile{} Expect(defaults.Set(&cfg)).Should(Succeed()) Expect(cfg.IsEnabled()).Should(BeFalse()) @@ -41,7 +41,7 @@ var _ = Describe("HostsFileConfig", func() { When("disabled", func() { It("should be false", func() { - cfg := HostsFileConfig{} + cfg := HostsFile{} Expect(cfg.IsEnabled()).Should(BeFalse()) }) @@ -62,7 +62,7 @@ var _ = Describe("HostsFileConfig", func() { Describe("migrate", func() { It("should", func() { - cfg, err := WithDefaults[HostsFileConfig]() + cfg, err := WithDefaults[HostsFile]() Expect(err).Should(Succeed()) cfg.Deprecated.Filepath = ptrOf(newBytesSource("/a/file/path")) diff --git a/resolver/hosts_file_resolver.go b/resolver/hosts_file_resolver.go index dd50a8162..1dec7eb08 100644 --- a/resolver/hosts_file_resolver.go +++ b/resolver/hosts_file_resolver.go @@ -26,7 +26,7 @@ const ( type HostsFileEntry = parsers.HostsFileEntry type HostsFileResolver struct { - configurable[*config.HostsFileConfig] + configurable[*config.HostsFile] NextResolver typed @@ -35,7 +35,7 @@ type HostsFileResolver struct { } func NewHostsFileResolver(ctx context.Context, - cfg config.HostsFileConfig, + cfg config.HostsFile, bootstrap *Bootstrap, ) (*HostsFileResolver, error) { r := HostsFileResolver{ diff --git a/resolver/hosts_file_resolver_test.go b/resolver/hosts_file_resolver_test.go index cc0da9456..34381dfed 100644 --- a/resolver/hosts_file_resolver_test.go +++ b/resolver/hosts_file_resolver_test.go @@ -19,7 +19,7 @@ var _ = Describe("HostsFileResolver", func() { TTL = uint32(time.Now().Second()) sut *HostsFileResolver - sutConfig config.HostsFileConfig + sutConfig config.HostsFile m *mockResolver tmpDir *TmpFolder tmpFile *TmpFile @@ -43,7 +43,7 @@ var _ = Describe("HostsFileResolver", func() { tmpDir = NewTmpFolder("HostsFileResolver") tmpFile = writeHostFile(tmpDir) - sutConfig = config.HostsFileConfig{ + sutConfig = config.HostsFile{ Sources: config.NewBytesSources(tmpFile.Path), HostsTTL: config.Duration(time.Duration(TTL) * time.Second), FilterLoopback: true, @@ -82,7 +82,7 @@ var _ = Describe("HostsFileResolver", func() { Describe("Using hosts file", func() { When("Hosts file cannot be located", func() { BeforeEach(func() { - sutConfig = config.HostsFileConfig{ + sutConfig = config.HostsFile{ Sources: config.NewBytesSources("/this/file/does/not/exist"), HostsTTL: config.Duration(time.Duration(TTL) * time.Second), } From d3a93d623130fd5a26cc23dd4790f23ca190e8f5 Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Wed, 20 Dec 2023 17:40:18 +0000 Subject: [PATCH 07/16] PortsConfig -> Ports --- config/config.go | 6 +++--- server/server_test.go | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/config/config.go b/config/config.go index 945907a19..6e91727e7 100644 --- a/config/config.go +++ b/config/config.go @@ -223,7 +223,7 @@ type Config struct { Prometheus Metrics `yaml:"prometheus"` Redis Redis `yaml:"redis"` Log log.Config `yaml:"log"` - Ports PortsConfig `yaml:"ports"` + Ports Ports `yaml:"ports"` MinTLSServeVer TLSVersion `yaml:"minTlsServeVersion" default:"1.2"` CertFile string `yaml:"certFile"` KeyFile string `yaml:"keyFile"` @@ -253,14 +253,14 @@ type Config struct { } `yaml:",inline"` } -type PortsConfig struct { +type Ports struct { DNS ListenConfig `yaml:"dns" default:"53"` HTTP ListenConfig `yaml:"http"` HTTPS ListenConfig `yaml:"https"` TLS ListenConfig `yaml:"tls"` } -func (c *PortsConfig) LogConfig(logger *logrus.Entry) { +func (c *Ports) LogConfig(logger *logrus.Entry) { logger.Infof("DNS = %s", c.DNS) logger.Infof("TLS = %s", c.TLS) logger.Infof("HTTP = %s", c.HTTP) diff --git a/server/server_test.go b/server/server_test.go index 3f2fa7f88..6a3f56e2a 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -148,7 +148,7 @@ var _ = BeforeSuite(func() { Upstream: upstreamClient, }, - Ports: config.PortsConfig{ + Ports: config.Ports{ DNS: config.ListenConfig{GetStringPort(dnsBasePort)}, TLS: config.ListenConfig{GetStringPort(tlsBasePort)}, HTTP: config.ListenConfig{GetStringPort(httpBasePort)}, @@ -603,7 +603,7 @@ var _ = Describe("Running DNS server", func() { }, }, Blocking: config.Blocking{BlockType: "zeroIp"}, - Ports: config.PortsConfig{ + Ports: config.Ports{ DNS: config.ListenConfig{"127.0.0.1:" + GetStringPort(dnsBasePort2)}, }, }) @@ -649,7 +649,7 @@ var _ = Describe("Running DNS server", func() { }, }, Blocking: config.Blocking{BlockType: "zeroIp"}, - Ports: config.PortsConfig{ + Ports: config.Ports{ DNS: config.ListenConfig{"127.0.0.1:" + GetStringPort(dnsBasePort2)}, }, }) @@ -712,7 +712,7 @@ var _ = Describe("Running DNS server", func() { It("should create self-signed certificate if key/cert files are not provided", func() { cfg.KeyFile = "" cfg.CertFile = "" - cfg.Ports = config.PortsConfig{ + cfg.Ports = config.Ports{ HTTPS: []string{fmt.Sprintf(":%d", GetIntPort(httpsBasePort)+100)}, } sut, err := NewServer(ctx, &cfg) From ef6aa5908731d16d1a61d6d44751ef9f0ff647f3 Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Wed, 20 Dec 2023 17:42:41 +0000 Subject: [PATCH 08/16] BootstrapDNSConfig -> BootstrapDNS --- config/config.go | 16 ++++++++-------- config/config_test.go | 6 +++--- resolver/bootstrap.go | 6 +++--- resolver/bootstrap_test.go | 2 +- resolver/mocks_test.go | 2 +- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/config/config.go b/config/config.go index 6e91727e7..5e2b25f1a 100644 --- a/config/config.go +++ b/config/config.go @@ -172,10 +172,10 @@ func (l *ListenConfig) UnmarshalText(data []byte) error { } // UnmarshalYAML creates BootstrapDNSConfig from YAML -func (b *BootstrapDNSConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { +func (b *BootstrapDNS) UnmarshalYAML(unmarshal func(interface{}) error) error { var single BootstrappedUpstreamConfig if err := unmarshal(&single); err == nil { - *b = BootstrapDNSConfig{single} + *b = BootstrapDNS{single} return nil } @@ -187,7 +187,7 @@ func (b *BootstrapDNSConfig) UnmarshalYAML(unmarshal func(interface{}) error) er return err } - *b = BootstrapDNSConfig(c) + *b = BootstrapDNS(c) return nil } @@ -227,7 +227,7 @@ type Config struct { MinTLSServeVer TLSVersion `yaml:"minTlsServeVersion" default:"1.2"` CertFile string `yaml:"certFile"` KeyFile string `yaml:"keyFile"` - BootstrapDNS BootstrapDNSConfig `yaml:"bootstrapDns"` + BootstrapDNS BootstrapDNS `yaml:"bootstrapDns"` HostsFile HostsFile `yaml:"hostsFile"` FQDNOnly FQDNOnly `yaml:"fqdnOnly"` Filtering Filtering `yaml:"filtering"` @@ -267,17 +267,17 @@ func (c *Ports) LogConfig(logger *logrus.Entry) { logger.Infof("HTTPS = %s", c.HTTPS) } -// split in two types to avoid infinite recursion. See `BootstrapDNSConfig.UnmarshalYAML`. +// split in two types to avoid infinite recursion. See `BootstrapDNS.UnmarshalYAML`. type ( - BootstrapDNSConfig bootstrapDNSConfig + BootstrapDNS bootstrapDNSConfig bootstrapDNSConfig []BootstrappedUpstreamConfig ) -func (b *BootstrapDNSConfig) IsEnabled() bool { +func (b *BootstrapDNS) IsEnabled() bool { return len(*b) != 0 } -func (b *BootstrapDNSConfig) LogConfig(*logrus.Entry) { +func (b *BootstrapDNS) LogConfig(*logrus.Entry) { // This should not be called, at least for now: // The Boostrap resolver is not in the chain and thus its config is not logged panic("not implemented") diff --git a/config/config_test.go b/config/config_test.go index 0bc52140e..10226e001 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -744,13 +744,13 @@ bootstrapDns: Describe("BootstrapDNSConfig", func() { It("is not enabled when empty", func() { - var sut BootstrapDNSConfig + var sut BootstrapDNS Expect(sut.IsEnabled()).Should(BeFalse()) }) It("is enabled if non empty", func() { - sut := BootstrapDNSConfig{ + sut := BootstrapDNS{ BootstrappedUpstreamConfig{}, BootstrappedUpstreamConfig{}, } @@ -759,7 +759,7 @@ bootstrapDns: }) It("LogConfig panics", func() { - sut := BootstrapDNSConfig{} + sut := BootstrapDNS{} Expect(func() { sut.LogConfig(logger) diff --git a/resolver/bootstrap.go b/resolver/bootstrap.go index 5152e0d53..0e6ba4b14 100644 --- a/resolver/bootstrap.go +++ b/resolver/bootstrap.go @@ -26,7 +26,7 @@ var errArbitrarySystemResolverRequest = errors.New( ) type bootstrapConfig struct { - config.BootstrapDNSConfig + config.BootstrapDNS connectIPVersion config.IPVersion timeout config.Duration @@ -34,7 +34,7 @@ type bootstrapConfig struct { func newBootstrapConfig(cfg *config.Config) *bootstrapConfig { return &bootstrapConfig{ - BootstrapDNSConfig: cfg.BootstrapDNS, + BootstrapDNS: cfg.BootstrapDNS, connectIPVersion: cfg.ConnectIPVersion, timeout: cfg.Upstreams.Timeout, @@ -266,7 +266,7 @@ func (b *Bootstrap) resolveType(ctx context.Context, hostname string, qType dns. type bootstrapedResolvers map[Resolver][]net.IP func newBootstrapedResolvers( - b *Bootstrap, cfg config.BootstrapDNSConfig, upstreamsCfg config.Upstreams, + b *Bootstrap, cfg config.BootstrapDNS, upstreamsCfg config.Upstreams, ) (bootstrapedResolvers, error) { upstreamIPs := make(bootstrapedResolvers, len(cfg)) diff --git a/resolver/bootstrap_test.go b/resolver/bootstrap_test.go index f4f88a782..eeac3c171 100644 --- a/resolver/bootstrap_test.go +++ b/resolver/bootstrap_test.go @@ -58,7 +58,7 @@ var _ = Describe("Bootstrap", Label("bootstrap"), func() { Describe("configuration", func() { When("is not specified", func() { BeforeEach(func() { - sutConfig.BootstrapDNS = config.BootstrapDNSConfig{} + sutConfig.BootstrapDNS = config.BootstrapDNS{} }) It("should use the system resolver", func() { diff --git a/resolver/mocks_test.go b/resolver/mocks_test.go index cc99220e0..3ae046ff9 100644 --- a/resolver/mocks_test.go +++ b/resolver/mocks_test.go @@ -128,7 +128,7 @@ ips: bootstrapUpstream := &mockResolver{} - var bCfg config.BootstrapDNSConfig + var bCfg config.BootstrapDNS err := yaml.UnmarshalStrict([]byte(cfgTxt), &bCfg) util.FatalOnError("test bootstrap config is broken, did you change the struct?", err) From 9a6d8518c831f023552ebc1884aaf5cfb8d5a08a Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Wed, 20 Dec 2023 17:47:08 +0000 Subject: [PATCH 09/16] BootstrappedUpstreamConfig -> BootstrappedUpstream --- config/config.go | 16 ++++++++-------- config/config_test.go | 4 ++-- resolver/bootstrap_test.go | 18 +++++++++--------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/config/config.go b/config/config.go index 5e2b25f1a..fe1bbcd16 100644 --- a/config/config.go +++ b/config/config.go @@ -171,9 +171,9 @@ func (l *ListenConfig) UnmarshalText(data []byte) error { return nil } -// UnmarshalYAML creates BootstrapDNSConfig from YAML +// UnmarshalYAML creates BootstrapDNS from YAML func (b *BootstrapDNS) UnmarshalYAML(unmarshal func(interface{}) error) error { - var single BootstrappedUpstreamConfig + var single BootstrappedUpstream if err := unmarshal(&single); err == nil { *b = BootstrapDNS{single} @@ -181,7 +181,7 @@ func (b *BootstrapDNS) UnmarshalYAML(unmarshal func(interface{}) error) error { } // bootstrapDNSConfig is used to avoid infinite recursion: - // if we used BootstrapDNSConfig, unmarshal would just call us again. + // if we used BootstrapDNS, unmarshal would just call us again. var c bootstrapDNSConfig if err := unmarshal(&c); err != nil { return err @@ -193,7 +193,7 @@ func (b *BootstrapDNS) UnmarshalYAML(unmarshal func(interface{}) error) error { } // UnmarshalYAML creates BootstrapConfig from YAML -func (b *BootstrappedUpstreamConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { +func (b *BootstrappedUpstream) UnmarshalYAML(unmarshal func(interface{}) error) error { if err := unmarshal(&b.Upstream); err == nil { return nil } @@ -205,7 +205,7 @@ func (b *BootstrappedUpstreamConfig) UnmarshalYAML(unmarshal func(interface{}) e return err } - *b = BootstrappedUpstreamConfig(c) + *b = BootstrappedUpstream(c) return nil } @@ -270,7 +270,7 @@ func (c *Ports) LogConfig(logger *logrus.Entry) { // split in two types to avoid infinite recursion. See `BootstrapDNS.UnmarshalYAML`. type ( BootstrapDNS bootstrapDNSConfig - bootstrapDNSConfig []BootstrappedUpstreamConfig + bootstrapDNSConfig []BootstrappedUpstream ) func (b *BootstrapDNS) IsEnabled() bool { @@ -283,9 +283,9 @@ func (b *BootstrapDNS) LogConfig(*logrus.Entry) { panic("not implemented") } -// split in two types to avoid infinite recursion. See `BootstrappedUpstreamConfig.UnmarshalYAML`. +// split in two types to avoid infinite recursion. See `BootstrappedUpstream.UnmarshalYAML`. type ( - BootstrappedUpstreamConfig bootstrappedUpstreamConfig + BootstrappedUpstream bootstrappedUpstreamConfig bootstrappedUpstreamConfig struct { Upstream Upstream `yaml:"upstream"` IPs []net.IP `yaml:"ips"` diff --git a/config/config_test.go b/config/config_test.go index 10226e001..e26620ccb 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -751,8 +751,8 @@ bootstrapDns: It("is enabled if non empty", func() { sut := BootstrapDNS{ - BootstrappedUpstreamConfig{}, - BootstrappedUpstreamConfig{}, + BootstrappedUpstream{}, + BootstrappedUpstream{}, } Expect(sut.IsEnabled()).Should(BeTrue()) diff --git a/resolver/bootstrap_test.go b/resolver/bootstrap_test.go index eeac3c171..d0d4df00a 100644 --- a/resolver/bootstrap_test.go +++ b/resolver/bootstrap_test.go @@ -34,7 +34,7 @@ var _ = Describe("Bootstrap", Label("bootstrap"), func() { BeforeEach(func() { sutConfig = config.Config{ - BootstrapDNS: []config.BootstrappedUpstreamConfig{ + BootstrapDNS: []config.BootstrappedUpstream{ { Upstream: config.Upstream{ Net: config.NetProtocolTcpTls, @@ -89,7 +89,7 @@ var _ = Describe("Bootstrap", Label("bootstrap"), func() { When("one of multiple upstreams is invalid", func() { It("errors", func() { cfg := config.Config{ - BootstrapDNS: []config.BootstrappedUpstreamConfig{ + BootstrapDNS: []config.BootstrappedUpstream{ { Upstream: config.Upstream{ // valid Net: config.NetProtocolTcpUdp, @@ -115,7 +115,7 @@ var _ = Describe("Bootstrap", Label("bootstrap"), func() { When("hostname is an IP", func() { BeforeEach(func() { sutConfig = config.Config{ - BootstrapDNS: []config.BootstrappedUpstreamConfig{ + BootstrapDNS: []config.BootstrappedUpstream{ { Upstream: config.Upstream{ Net: config.NetProtocolTcpUdp, @@ -137,7 +137,7 @@ var _ = Describe("Bootstrap", Label("bootstrap"), func() { When("using non IP hostname", func() { It("errors", func() { cfg := config.Config{ - BootstrapDNS: []config.BootstrappedUpstreamConfig{ + BootstrapDNS: []config.BootstrappedUpstream{ { Upstream: config.Upstream{ Net: config.NetProtocolTcpUdp, @@ -156,7 +156,7 @@ var _ = Describe("Bootstrap", Label("bootstrap"), func() { When("extra IPs are configured", func() { BeforeEach(func() { sutConfig = config.Config{ - BootstrapDNS: []config.BootstrappedUpstreamConfig{ + BootstrapDNS: []config.BootstrappedUpstream{ { Upstream: config.Upstream{ Net: config.NetProtocolTcpUdp, @@ -181,7 +181,7 @@ var _ = Describe("Bootstrap", Label("bootstrap"), func() { When("IPs are missing", func() { It("errors", func() { cfg := config.Config{ - BootstrapDNS: []config.BootstrappedUpstreamConfig{ + BootstrapDNS: []config.BootstrappedUpstream{ { Upstream: config.Upstream{ Net: config.NetProtocolTcpTls, @@ -200,7 +200,7 @@ var _ = Describe("Bootstrap", Label("bootstrap"), func() { When("hostname is IP", func() { It("doesn't require extra IPs", func() { cfg := config.Config{ - BootstrapDNS: []config.BootstrappedUpstreamConfig{ + BootstrapDNS: []config.BootstrappedUpstream{ { Upstream: config.Upstream{ Net: config.NetProtocolTcpTls, @@ -223,7 +223,7 @@ var _ = Describe("Bootstrap", Label("bootstrap"), func() { BeforeEach(func() { bootstrapUpstream = &mockResolver{} - sutConfig.BootstrapDNS = []config.BootstrappedUpstreamConfig{ + sutConfig.BootstrapDNS = []config.BootstrappedUpstream{ { Upstream: config.Upstream{ Net: config.NetProtocolTcpTls, @@ -596,7 +596,7 @@ var _ = Describe("Bootstrap", Label("bootstrap"), func() { mockUpstream1 = NewMockUDPUpstreamServer().WithAnswerRR("example.com 123 IN A 123.124.122.122") mockUpstream2 = NewMockUDPUpstreamServer().WithAnswerRR("example.com 123 IN A 123.124.122.122") - sutConfig.BootstrapDNS = []config.BootstrappedUpstreamConfig{ + sutConfig.BootstrapDNS = []config.BootstrappedUpstream{ {Upstream: mockUpstream1.Start()}, {Upstream: mockUpstream2.Start()}, } From e6db806fa8ed9a3481dbed294961839604040274 Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Wed, 20 Dec 2023 17:48:42 +0000 Subject: [PATCH 10/16] bootstrapDNSConfig -> bootstrapDNS --- config/config.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/config/config.go b/config/config.go index fe1bbcd16..3109c6025 100644 --- a/config/config.go +++ b/config/config.go @@ -182,7 +182,7 @@ func (b *BootstrapDNS) UnmarshalYAML(unmarshal func(interface{}) error) error { // bootstrapDNSConfig is used to avoid infinite recursion: // if we used BootstrapDNS, unmarshal would just call us again. - var c bootstrapDNSConfig + var c bootstrapDNS if err := unmarshal(&c); err != nil { return err } @@ -269,8 +269,8 @@ func (c *Ports) LogConfig(logger *logrus.Entry) { // split in two types to avoid infinite recursion. See `BootstrapDNS.UnmarshalYAML`. type ( - BootstrapDNS bootstrapDNSConfig - bootstrapDNSConfig []BootstrappedUpstream + BootstrapDNS bootstrapDNS + bootstrapDNS []BootstrappedUpstream ) func (b *BootstrapDNS) IsEnabled() bool { From 253e1d499873ba381ca281342b96b592d9cb0043 Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Wed, 20 Dec 2023 17:49:40 +0000 Subject: [PATCH 11/16] bootstrappedUpstreamConfig -> bootstrappedUpstream --- config/config.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/config/config.go b/config/config.go index 3109c6025..46f745979 100644 --- a/config/config.go +++ b/config/config.go @@ -200,7 +200,7 @@ func (b *BootstrappedUpstream) UnmarshalYAML(unmarshal func(interface{}) error) // bootstrapConfig is used to avoid infinite recursion: // if we used BootstrapConfig, unmarshal would just call us again. - var c bootstrappedUpstreamConfig + var c bootstrappedUpstream if err := unmarshal(&c); err != nil { return err } @@ -285,8 +285,8 @@ func (b *BootstrapDNS) LogConfig(*logrus.Entry) { // split in two types to avoid infinite recursion. See `BootstrappedUpstream.UnmarshalYAML`. type ( - BootstrappedUpstream bootstrappedUpstreamConfig - bootstrappedUpstreamConfig struct { + BootstrappedUpstream bootstrappedUpstream + bootstrappedUpstream struct { Upstream Upstream `yaml:"upstream"` IPs []net.IP `yaml:"ips"` } From 3f210343c0e38c97bd67ccc637efc27bc87e9068 Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Wed, 20 Dec 2023 17:50:57 +0000 Subject: [PATCH 12/16] SourceLoadingConfig -> SourceLoading --- config/blocking.go | 2 +- config/config.go | 8 ++++---- config/config_test.go | 8 ++++---- config/hosts_file.go | 8 ++++---- config/hosts_file_test.go | 2 +- lists/list_cache.go | 4 ++-- lists/list_cache_benchmark_test.go | 2 +- lists/list_cache_test.go | 4 ++-- resolver/blocking_resolver_test.go | 4 ++-- resolver/hosts_file_resolver_test.go | 2 +- 10 files changed, 22 insertions(+), 22 deletions(-) diff --git a/config/blocking.go b/config/blocking.go index af4d8e356..aa7520d31 100644 --- a/config/blocking.go +++ b/config/blocking.go @@ -13,7 +13,7 @@ type Blocking struct { ClientGroupsBlock map[string][]string `yaml:"clientGroupsBlock"` BlockType string `yaml:"blockType" default:"ZEROIP"` BlockTTL Duration `yaml:"blockTTL" default:"6h"` - Loading SourceLoadingConfig `yaml:"loading"` + Loading SourceLoading `yaml:"loading"` // Deprecated options Deprecated struct { diff --git a/config/config.go b/config/config.go index 46f745979..2c7e4c581 100644 --- a/config/config.go +++ b/config/config.go @@ -319,7 +319,7 @@ func (c *Init) LogConfig(logger *logrus.Entry) { logger.Debugf("strategy = %s", c.Strategy) } -type SourceLoadingConfig struct { +type SourceLoading struct { Init `yaml:",inline"` Concurrency uint `yaml:"concurrency" default:"4"` @@ -328,7 +328,7 @@ type SourceLoadingConfig struct { Downloads DownloaderConfig `yaml:"downloads"` } -func (c *SourceLoadingConfig) LogConfig(logger *logrus.Entry) { +func (c *SourceLoading) LogConfig(logger *logrus.Entry) { c.Init.LogConfig(logger) logger.Infof("concurrency = %d", c.Concurrency) logger.Debugf("maxErrorsPerSource = %d", c.MaxErrorsPerSource) @@ -343,7 +343,7 @@ func (c *SourceLoadingConfig) LogConfig(logger *logrus.Entry) { log.WithIndent(logger, " ", c.Downloads.LogConfig) } -func (c *SourceLoadingConfig) StartPeriodicRefresh( +func (c *SourceLoading) StartPeriodicRefresh( ctx context.Context, refresh func(context.Context) error, logErr func(error), ) error { err := c.Strategy.Do(ctx, refresh, logErr) @@ -358,7 +358,7 @@ func (c *SourceLoadingConfig) StartPeriodicRefresh( return nil } -func (c *SourceLoadingConfig) periodically( +func (c *SourceLoading) periodically( ctx context.Context, refresh func(context.Context) error, logErr func(error), ) { refresh = recoverToError(refresh, func(panicVal any) error { diff --git a/config/config_test.go b/config/config_test.go index e26620ccb..444aff939 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -547,10 +547,10 @@ bootstrapDns: ) Describe("SourceLoadingConfig", func() { - var cfg SourceLoadingConfig + var cfg SourceLoading BeforeEach(func() { - cfg = SourceLoadingConfig{ + cfg = SourceLoading{ Concurrency: 12, RefreshPeriod: Duration(time.Hour), } @@ -777,7 +777,7 @@ bootstrapDns: DeferCleanup(cancelFn) }) It("handles panics", func() { - sut := SourceLoadingConfig{ + sut := SourceLoading{ Init: Init{Strategy: InitStrategyFailOnError}, } @@ -793,7 +793,7 @@ bootstrapDns: }) It("periodically calls refresh", func() { - sut := SourceLoadingConfig{ + sut := SourceLoading{ Init: Init{Strategy: InitStrategyFast}, RefreshPeriod: Duration(5 * time.Millisecond), } diff --git a/config/hosts_file.go b/config/hosts_file.go index 895f89d52..042f1b31c 100644 --- a/config/hosts_file.go +++ b/config/hosts_file.go @@ -7,10 +7,10 @@ import ( ) type HostsFile struct { - Sources []BytesSource `yaml:"sources"` - HostsTTL Duration `yaml:"hostsTTL" default:"1h"` - FilterLoopback bool `yaml:"filterLoopback"` - Loading SourceLoadingConfig `yaml:"loading"` + Sources []BytesSource `yaml:"sources"` + HostsTTL Duration `yaml:"hostsTTL" default:"1h"` + FilterLoopback bool `yaml:"filterLoopback"` + Loading SourceLoading `yaml:"loading"` // Deprecated options Deprecated struct { diff --git a/config/hosts_file_test.go b/config/hosts_file_test.go index cee2b92da..26cbef1ae 100644 --- a/config/hosts_file_test.go +++ b/config/hosts_file_test.go @@ -20,7 +20,7 @@ var _ = Describe("HostsFileConfig", func() { TextBytesSource("127.0.0.1 localhost"), ), HostsTTL: Duration(29 * time.Minute), - Loading: SourceLoadingConfig{RefreshPeriod: Duration(30 * time.Minute)}, + Loading: SourceLoading{RefreshPeriod: Duration(30 * time.Minute)}, FilterLoopback: true, } }) diff --git a/lists/list_cache.go b/lists/list_cache.go index 70ae15bd0..409591f9b 100644 --- a/lists/list_cache.go +++ b/lists/list_cache.go @@ -40,7 +40,7 @@ type ListCache struct { groupedCache stringcache.GroupedStringCache regexCache stringcache.GroupedStringCache - cfg config.SourceLoadingConfig + cfg config.SourceLoading listType ListCacheType groupSources map[string][]config.BytesSource downloader FileDownloader @@ -70,7 +70,7 @@ func (b *ListCache) LogConfig(logger *logrus.Entry) { // NewListCache creates new list instance func NewListCache(ctx context.Context, - t ListCacheType, cfg config.SourceLoadingConfig, + t ListCacheType, cfg config.SourceLoading, groupSources map[string][]config.BytesSource, downloader FileDownloader, ) (*ListCache, error) { regexCache := stringcache.NewInMemoryGroupedRegexCache() diff --git a/lists/list_cache_benchmark_test.go b/lists/list_cache_benchmark_test.go index 1a710250b..de477982b 100644 --- a/lists/list_cache_benchmark_test.go +++ b/lists/list_cache_benchmark_test.go @@ -15,7 +15,7 @@ func BenchmarkRefresh(b *testing.B) { "gr1": config.NewBytesSources(file1, file2, file3), } - cfg := config.SourceLoadingConfig{ + cfg := config.SourceLoading{ Concurrency: 5, RefreshPeriod: config.Duration(-1), } diff --git a/lists/list_cache_test.go b/lists/list_cache_test.go index 8b68525b0..bc9cf1d1c 100644 --- a/lists/list_cache_test.go +++ b/lists/list_cache_test.go @@ -29,7 +29,7 @@ var _ = Describe("ListCache", func() { server1, server2, server3 *httptest.Server sut *ListCache - sutConfig config.SourceLoadingConfig + sutConfig config.SourceLoading listCacheType ListCacheType lists map[string][]config.BytesSource @@ -48,7 +48,7 @@ var _ = Describe("ListCache", func() { listCacheType = ListCacheTypeBlacklist - sutConfig, err = config.WithDefaults[config.SourceLoadingConfig]() + sutConfig, err = config.WithDefaults[config.SourceLoading]() Expect(err).Should(Succeed()) sutConfig.RefreshPeriod = -1 diff --git a/resolver/blocking_resolver_test.go b/resolver/blocking_resolver_test.go index 70db11b3c..e110849e8 100644 --- a/resolver/blocking_resolver_test.go +++ b/resolver/blocking_resolver_test.go @@ -170,7 +170,7 @@ var _ = Describe("BlockingResolver", Label("blockingResolver"), func() { ClientGroupsBlock: map[string][]string{ "default": {"gr1"}, }, - Loading: config.SourceLoadingConfig{ + Loading: config.SourceLoading{ Init: config.Init{Strategy: config.InitStrategyFast}, }, } @@ -1126,7 +1126,7 @@ var _ = Describe("BlockingResolver", Label("blockingResolver"), func() { _, err := NewBlockingResolver(ctx, config.Blocking{ BlackLists: map[string][]config.BytesSource{"gr1": config.NewBytesSources("wrongPath")}, WhiteLists: map[string][]config.BytesSource{"whitelist": config.NewBytesSources("wrongPath")}, - Loading: config.SourceLoadingConfig{ + Loading: config.SourceLoading{ Init: config.Init{Strategy: config.InitStrategyFailOnError}, }, BlockType: "zeroIp", diff --git a/resolver/hosts_file_resolver_test.go b/resolver/hosts_file_resolver_test.go index 34381dfed..b769c63af 100644 --- a/resolver/hosts_file_resolver_test.go +++ b/resolver/hosts_file_resolver_test.go @@ -47,7 +47,7 @@ var _ = Describe("HostsFileResolver", func() { Sources: config.NewBytesSources(tmpFile.Path), HostsTTL: config.Duration(time.Duration(TTL) * time.Second), FilterLoopback: true, - Loading: config.SourceLoadingConfig{ + Loading: config.SourceLoading{ RefreshPeriod: -1, MaxErrorsPerSource: 5, }, From 0c925b67aa31094dafc21c8e5e62565f10aab8c6 Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Wed, 20 Dec 2023 17:51:38 +0000 Subject: [PATCH 13/16] DownloaderConfig -> Downloader --- config/config.go | 12 ++++++------ lists/downloader.go | 6 +++--- lists/downloader_test.go | 12 ++++++------ lists/list_cache_benchmark_test.go | 2 +- lists/list_cache_test.go | 2 +- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/config/config.go b/config/config.go index 2c7e4c581..474657476 100644 --- a/config/config.go +++ b/config/config.go @@ -322,10 +322,10 @@ func (c *Init) LogConfig(logger *logrus.Entry) { type SourceLoading struct { Init `yaml:",inline"` - Concurrency uint `yaml:"concurrency" default:"4"` - MaxErrorsPerSource int `yaml:"maxErrorsPerSource" default:"5"` - RefreshPeriod Duration `yaml:"refreshPeriod" default:"4h"` - Downloads DownloaderConfig `yaml:"downloads"` + Concurrency uint `yaml:"concurrency" default:"4"` + MaxErrorsPerSource int `yaml:"maxErrorsPerSource" default:"5"` + RefreshPeriod Duration `yaml:"refreshPeriod" default:"4h"` + Downloads Downloader `yaml:"downloads"` } func (c *SourceLoading) LogConfig(logger *logrus.Entry) { @@ -394,13 +394,13 @@ func recoverToError(do func(context.Context) error, onPanic func(any) error) fun } } -type DownloaderConfig struct { +type Downloader struct { Timeout Duration `yaml:"timeout" default:"5s"` Attempts uint `yaml:"attempts" default:"3"` Cooldown Duration `yaml:"cooldown" default:"500ms"` } -func (c *DownloaderConfig) LogConfig(logger *logrus.Entry) { +func (c *Downloader) LogConfig(logger *logrus.Entry) { logger.Infof("timeout = %s", c.Timeout) logger.Infof("attempts = %d", c.Attempts) logger.Debugf("cooldown = %s", c.Cooldown) diff --git a/lists/downloader.go b/lists/downloader.go index ff95c8628..d2e964b3d 100644 --- a/lists/downloader.go +++ b/lists/downloader.go @@ -33,16 +33,16 @@ type FileDownloader interface { // httpDownloader downloads files via HTTP protocol type httpDownloader struct { - cfg config.DownloaderConfig + cfg config.Downloader client http.Client } -func NewDownloader(cfg config.DownloaderConfig, transport http.RoundTripper) FileDownloader { +func NewDownloader(cfg config.Downloader, transport http.RoundTripper) FileDownloader { return newDownloader(cfg, transport) } -func newDownloader(cfg config.DownloaderConfig, transport http.RoundTripper) *httpDownloader { +func newDownloader(cfg config.Downloader, transport http.RoundTripper) *httpDownloader { return &httpDownloader{ cfg: cfg, diff --git a/lists/downloader_test.go b/lists/downloader_test.go index f3a914381..6cb59ed43 100644 --- a/lists/downloader_test.go +++ b/lists/downloader_test.go @@ -22,7 +22,7 @@ import ( var _ = Describe("Downloader", func() { var ( - sutConfig config.DownloaderConfig + sutConfig config.Downloader sut *httpDownloader failedDownloadCountEvtChannel chan string loggerHook *test.Hook @@ -30,7 +30,7 @@ var _ = Describe("Downloader", func() { BeforeEach(func() { var err error - sutConfig, err = config.WithDefaults[config.DownloaderConfig]() + sutConfig, err = config.WithDefaults[config.Downloader]() Expect(err).Should(Succeed()) failedDownloadCountEvtChannel = make(chan string, 5) @@ -57,7 +57,7 @@ var _ = Describe("Downloader", func() { transport := &http.Transport{} sut = NewDownloader( - config.DownloaderConfig{ + config.Downloader{ Attempts: 5, Cooldown: config.Duration(2 * time.Second), Timeout: config.Duration(5 * time.Second), @@ -128,7 +128,7 @@ var _ = Describe("Downloader", func() { var attempt uint64 = 1 BeforeEach(func() { - sutConfig = config.DownloaderConfig{ + sutConfig = config.Downloader{ Timeout: config.Duration(20 * time.Millisecond), Attempts: 3, Cooldown: config.Duration(time.Millisecond), @@ -165,7 +165,7 @@ var _ = Describe("Downloader", func() { }) When("If timeout occurs on all request", func() { BeforeEach(func() { - sutConfig = config.DownloaderConfig{ + sutConfig = config.Downloader{ Timeout: config.Duration(10 * time.Millisecond), Attempts: 3, Cooldown: config.Duration(time.Millisecond), @@ -191,7 +191,7 @@ var _ = Describe("Downloader", func() { }) When("DNS resolution of passed URL fails", func() { BeforeEach(func() { - sutConfig = config.DownloaderConfig{ + sutConfig = config.Downloader{ Timeout: config.Duration(500 * time.Millisecond), Attempts: 3, Cooldown: 200 * config.Duration(time.Millisecond), diff --git a/lists/list_cache_benchmark_test.go b/lists/list_cache_benchmark_test.go index de477982b..0a0c325c4 100644 --- a/lists/list_cache_benchmark_test.go +++ b/lists/list_cache_benchmark_test.go @@ -19,7 +19,7 @@ func BenchmarkRefresh(b *testing.B) { Concurrency: 5, RefreshPeriod: config.Duration(-1), } - downloader := NewDownloader(config.DownloaderConfig{}, nil) + downloader := NewDownloader(config.Downloader{}, nil) cache, _ := NewListCache(context.Background(), ListCacheTypeBlacklist, cfg, lists, downloader) b.ReportAllocs() diff --git a/lists/list_cache_test.go b/lists/list_cache_test.go index bc9cf1d1c..dbcd910d6 100644 --- a/lists/list_cache_test.go +++ b/lists/list_cache_test.go @@ -53,7 +53,7 @@ var _ = Describe("ListCache", func() { sutConfig.RefreshPeriod = -1 - downloader = NewDownloader(config.DownloaderConfig{}, nil) + downloader = NewDownloader(config.Downloader{}, nil) mockDownloader = nil server1 = TestServer("blocked1.com\nblocked1a.com\n192.168.178.55") From 26bce0e01f1096051c2b6c7be3574533b168e653 Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Wed, 20 Dec 2023 21:37:57 +0100 Subject: [PATCH 14/16] Update config/config.go Co-authored-by: ThinkChaos --- config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 474657476..3b2d3fc9d 100644 --- a/config/config.go +++ b/config/config.go @@ -180,7 +180,7 @@ func (b *BootstrapDNS) UnmarshalYAML(unmarshal func(interface{}) error) error { return nil } - // bootstrapDNSConfig is used to avoid infinite recursion: + // bootstrapDNS is used to avoid infinite recursion: // if we used BootstrapDNS, unmarshal would just call us again. var c bootstrapDNS if err := unmarshal(&c); err != nil { From 546b136f783ed23ef5ee558220833089a0569b31 Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Wed, 20 Dec 2023 21:38:10 +0100 Subject: [PATCH 15/16] Update config/config.go Co-authored-by: ThinkChaos --- config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 3b2d3fc9d..d4c1a1878 100644 --- a/config/config.go +++ b/config/config.go @@ -192,7 +192,7 @@ func (b *BootstrapDNS) UnmarshalYAML(unmarshal func(interface{}) error) error { return nil } -// UnmarshalYAML creates BootstrapConfig from YAML +// UnmarshalYAML creates BootstrappedUpstream from YAML func (b *BootstrappedUpstream) UnmarshalYAML(unmarshal func(interface{}) error) error { if err := unmarshal(&b.Upstream); err == nil { return nil From a37ce9f0b93b7ab5937e3d45932a078854892d84 Mon Sep 17 00:00:00 2001 From: Kwitsch Date: Wed, 20 Dec 2023 21:38:19 +0100 Subject: [PATCH 16/16] Update config/config.go Co-authored-by: ThinkChaos --- config/config.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/config.go b/config/config.go index d4c1a1878..9875609f7 100644 --- a/config/config.go +++ b/config/config.go @@ -198,8 +198,8 @@ func (b *BootstrappedUpstream) UnmarshalYAML(unmarshal func(interface{}) error) return nil } - // bootstrapConfig is used to avoid infinite recursion: - // if we used BootstrapConfig, unmarshal would just call us again. + // bootstrappedUpstream is used to avoid infinite recursion: + // if we used BootstrappedUpstream, unmarshal would just call us again. var c bootstrappedUpstream if err := unmarshal(&c); err != nil { return err