Skip to content

Commit

Permalink
feat: add the important flag to entries; related to #118
Browse files Browse the repository at this point in the history
BREAKING CHANGE: add the important flag by default (ADGUARD_SET_IMPORTANT_FLAG = true)
  • Loading branch information
muhlba91 committed Aug 9, 2024
1 parent e1cc63a commit 5a9ab31
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 17 deletions.
8 changes: 5 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ linters:
- govet
- ineffassign
- lll
- megacheck
- gosimple
- staticcheck
- misspell
- nakedret
- unconvert
Expand All @@ -16,8 +17,9 @@ linters:
linters-settings:
lll:
line-length: 250
run:
skip-files:
issues:
exclude-files:
- schema.go
- pulumiManifest.go
run:
timeout: 10m
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
17 changes: 17 additions & 0 deletions cmd/webhook/init/dnsprovider/dnsprovider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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)
})
Expand Down
18 changes: 14 additions & 4 deletions internal/adguard/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
38 changes: 38 additions & 0 deletions internal/adguard/configuration_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
18 changes: 11 additions & 7 deletions internal/adguard/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
type Provider struct {
provider.BaseProvider

Configuration *Configuration

client Client
domainFilter endpoint.DomainFilter
}
Expand All @@ -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
Expand Down Expand Up @@ -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...)
}

Expand Down Expand Up @@ -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
}
13 changes: 10 additions & 3 deletions internal/adguard/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -209,6 +214,7 @@ func TestSerializeToString(t *testing.T) {
name string
text []string
endpoint *endpoint.Endpoint
flags string
}{
{
name: "A record",
Expand Down Expand Up @@ -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)
})
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5a9ab31

Please sign in to comment.