From 5a9ab31a2eac8697e9f3b9bf5429cd3c6611bba8 Mon Sep 17 00:00:00 2001 From: Daniel Muehlbachler-Pietrzykowski Date: Fri, 9 Aug 2024 08:32:38 +0200 Subject: [PATCH] feat: add the important flag to entries; related to #118 BREAKING CHANGE: add the important flag by default (ADGUARD_SET_IMPORTANT_FLAG = true) --- .golangci.yml | 8 ++-- README.md | 6 +++ .../init/dnsprovider/dnsprovider_test.go | 17 +++++++++ internal/adguard/configuration.go | 18 +++++++-- internal/adguard/configuration_test.go | 38 +++++++++++++++++++ internal/adguard/provider.go | 18 +++++---- internal/adguard/provider_test.go | 13 +++++-- 7 files changed, 101 insertions(+), 17 deletions(-) create mode 100644 internal/adguard/configuration_test.go diff --git a/.golangci.yml b/.golangci.yml index 0b15bc1..dfde18d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -7,7 +7,8 @@ linters: - govet - ineffassign - lll - - megacheck + - gosimple + - staticcheck - misspell - nakedret - unconvert @@ -16,8 +17,9 @@ linters: linters-settings: lll: line-length: 250 -run: - skip-files: +issues: + exclude-files: - schema.go - pulumiManifest.go +run: timeout: 10m diff --git a/README.md b/README.md index 4e04bdf..833628c 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,12 @@ However, rules **not matching** above format, for example, `|domain.to.block`, * > **If** an **upgrade path** between version is **listed here**, please make sure to **follow** those paths **without skipping a version**! > Otherwise, the correct behaviour cannot be guaranteed, resulting in possible inconsistencies or errors. +### v5 to v6 + +`v6` introduces the `ADGUARD_SET_IMPORTANT_FLAG` environment variable to set the `important` flag for AdGuard rules. This is enabled by default. + +To keep the previous behaviour of `v5`, set `ADGUARD_SET_IMPORTANT_FLAG` to `false`. + ### v4 to v5 In `v5` removes the automated migration from the old rules syntax (`v3`) to the new syntax introduced in `v4`. diff --git a/cmd/webhook/init/dnsprovider/dnsprovider_test.go b/cmd/webhook/init/dnsprovider/dnsprovider_test.go index 3dc0f4c..0ccf91f 100644 --- a/cmd/webhook/init/dnsprovider/dnsprovider_test.go +++ b/cmd/webhook/init/dnsprovider/dnsprovider_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/muhlba91/external-dns-provider-adguard/cmd/webhook/init/configuration" + "github.com/muhlba91/external-dns-provider-adguard/internal/adguard" log "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" ) @@ -16,6 +17,7 @@ func TestInit(t *testing.T) { config configuration.Config env map[string]string expectedError string + expectedFlags string }{ { name: "minimal config for adguard provider", @@ -24,6 +26,7 @@ func TestInit(t *testing.T) { "ADGUARD_URL": "https://domain.com", "DRY_RUN": "true", }, + expectedFlags: ",important", }, { name: "domain filter config for adguard provider", @@ -35,6 +38,7 @@ func TestInit(t *testing.T) { "ADGUARD_URL": "https://domain.com", "DRY_RUN": "true", }, + expectedFlags: ",important", }, { name: "regex domain filter config for adguard provider", @@ -46,6 +50,17 @@ func TestInit(t *testing.T) { "ADGUARD_URL": "https://domain.com", "DRY_RUN": "true", }, + expectedFlags: ",important", + }, + { + name: "disable setting important flag for entries", + config: configuration.Config{}, + env: map[string]string{ + "ADGUARD_URL": "https://domain.com", + "DRY_RUN": "true", + "ADGUARD_SET_IMPORTANT_FLAG": "false", + }, + expectedFlags: "", }, { name: "empty configuration", @@ -67,6 +82,8 @@ func TestInit(t *testing.T) { return } + assert.Equal(t, tc.expectedFlags, dnsProvider.(*adguard.Provider).Configuration.DNSEntryFlags()) + assert.NoErrorf(t, err, "error creating provider") assert.NotNil(t, dnsProvider) }) diff --git a/internal/adguard/configuration.go b/internal/adguard/configuration.go index c319a07..5ec126c 100644 --- a/internal/adguard/configuration.go +++ b/internal/adguard/configuration.go @@ -2,8 +2,18 @@ package adguard // Configuration holds configuration from environmental variables type Configuration struct { - URL string `env:"ADGUARD_URL,notEmpty"` - User string `env:"ADGUARD_USER"` - Password string `env:"ADGUARD_PASSWORD"` - DryRun bool `env:"DRY_RUN" envDefault:"false"` + URL string `env:"ADGUARD_URL,notEmpty"` + User string `env:"ADGUARD_USER"` + Password string `env:"ADGUARD_PASSWORD"` + DryRun bool `env:"DRY_RUN" envDefault:"false"` + SetImportantFlag bool `env:"ADGUARD_SET_IMPORTANT_FLAG" envDefault:"true"` +} + +// DNSEntryFlags returns additional flags set for DNS entries +func (c *Configuration) DNSEntryFlags() string { + flags := "" + if c.SetImportantFlag { + flags += ",important" + } + return flags } diff --git a/internal/adguard/configuration_test.go b/internal/adguard/configuration_test.go new file mode 100644 index 0000000..865853b --- /dev/null +++ b/internal/adguard/configuration_test.go @@ -0,0 +1,38 @@ +package adguard + +import ( + "testing" + + log "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" +) + +func TestDNSEntryFlags(t *testing.T) { + log.SetLevel(log.DebugLevel) + + cases := []struct { + name string + config Configuration + expectedFlags string + }{ + { + name: "minimal config for adguard provider", + config: Configuration{}, + expectedFlags: "", + }, + { + name: "enable important flag", + config: Configuration{ + SetImportantFlag: true, + }, + expectedFlags: ",important", + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + flags := tc.config.DNSEntryFlags() + assert.Equal(t, tc.expectedFlags, flags) + }) + } +} diff --git a/internal/adguard/provider.go b/internal/adguard/provider.go index 785b5a4..ff76e81 100644 --- a/internal/adguard/provider.go +++ b/internal/adguard/provider.go @@ -18,6 +18,8 @@ import ( type Provider struct { provider.BaseProvider + Configuration *Configuration + client Client domainFilter endpoint.DomainFilter } @@ -44,8 +46,9 @@ func NewAdguardProvider(domainFilter endpoint.DomainFilter, config *Configuratio } p := &Provider{ - client: c, - domainFilter: domainFilter, + Configuration: config, + client: c, + domainFilter: domainFilter, } return p, nil @@ -128,7 +131,7 @@ func (p *Provider) ApplyChanges(ctx context.Context, changes *plan.Changes) erro // convert endpoints to rules for _, e := range eps { - s := serializeToString(e) + s := serializeToString(e, p.Configuration.DNSEntryFlags()) rr = append(rr, s...) } @@ -198,22 +201,23 @@ func deserializeToEndpoint(rule string) (*endpoint.Endpoint, error) { return nil, fmt.Errorf("invalid rule: %s", rule) } d := strings.TrimPrefix(dp[0], "|") + t := strings.Split(p[2], ",") // see serializeToString for the format r := &endpoint.Endpoint{ RecordType: p[1], DNSName: d, - Targets: endpoint.Targets{p[2]}, + Targets: endpoint.Targets{t[0]}, } return r, nil } -func serializeToString(e *endpoint.Endpoint) []string { +func serializeToString(e *endpoint.Endpoint, f string) []string { r := []string{} for _, t := range e.Targets { - // format: "|DNS.NAME^dnsrewrite=NOERROR;RECORD_TYPE;TARGET" - r = append(r, fmt.Sprintf("|%s^$dnsrewrite=NOERROR;%s;%s", e.DNSName, e.RecordType, t)) + // format: "|DNS.NAME^dnsrewrite=NOERROR;RECORD_TYPE;TARGET(,additional_flags)" + r = append(r, fmt.Sprintf("|%s^$dnsrewrite=NOERROR;%s;%s%s", e.DNSName, e.RecordType, t, f)) } return r } diff --git a/internal/adguard/provider_test.go b/internal/adguard/provider_test.go index f46577f..5762e9a 100644 --- a/internal/adguard/provider_test.go +++ b/internal/adguard/provider_test.go @@ -142,6 +142,11 @@ func TestDeserializeToEndpoint(t *testing.T) { text: "|domain.com^$dnsrewrite=NOERROR;A;1.1.1.1", endpoint: &endpoint.Endpoint{DNSName: "domain.com", RecordType: endpoint.RecordTypeA, Targets: []string{"1.1.1.1"}}, }, + { + name: "A record with flags", + text: "|domain.com^$dnsrewrite=NOERROR;A;1.1.1.1,important", + endpoint: &endpoint.Endpoint{DNSName: "domain.com", RecordType: endpoint.RecordTypeA, Targets: []string{"1.1.1.1"}}, + }, { name: "AAAA record", text: "|domain.com^$dnsrewrite=NOERROR;AAAA;1111:1111::1", @@ -209,6 +214,7 @@ func TestSerializeToString(t *testing.T) { name string text []string endpoint *endpoint.Endpoint + flags string }{ { name: "A record", @@ -242,7 +248,7 @@ func TestSerializeToString(t *testing.T) { for i, tc := range testCases { t.Run(fmt.Sprintf("%d. %s", i+1, tc.name), func(t *testing.T) { - rr := serializeToString(tc.endpoint) + rr := serializeToString(tc.endpoint, tc.flags) require.Equal(t, tc.text, rr) }) } @@ -832,8 +838,9 @@ func TestApplyChanges(t *testing.T) { t: t, } testProvider = &Provider{ - client: mockHTTPClient, - domainFilter: tc.domainFilter, + Configuration: &Configuration{}, + client: mockHTTPClient, + domainFilter: tc.domainFilter, } err := testProvider.ApplyChanges(context.TODO(), tc.changes)