Pull request 1995: AG-25392 confmigrate vol.3

Merge in DNS/adguard-home from AG-25392-confmigrate-vol.3 to master

Squashed commit of the following:

commit 925b35b9fedcdcf18474785530189a1eb504e1d6
Merge: 232f18481 581891af2
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Sep 6 12:48:05 2023 +0300

    Merge branch 'master' into AG-25392-confmigrate-vol.3

commit 232f18481bbf3400da0b95bcec7949fb3868bf85
Merge: c0d880bed 5bd452516
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Sep 5 18:54:47 2023 +0300

    Merge branch 'master' into AG-25392-confmigrate-vol.3

commit c0d880bed6e3270e88f5377e66b787565e1249ce
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Sep 5 17:21:57 2023 +0300

    confmigrate: fix v15

commit 8a27148b708e6d6a61f8097bf7792aefa1055772
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Sep 5 15:47:18 2023 +0300

    confmigrate: imp gocognit, overall
This commit is contained in:
Eugene Burkov 2023-09-06 13:43:29 +03:00
parent 581891af22
commit 30c6676dc0
25 changed files with 124 additions and 167 deletions

View File

@ -96,7 +96,7 @@ func TestUpgradeSchema5to6(t *testing.T) {
}, },
want: yobj{ want: yobj{
"clients": yarr{yobj{ "clients": yarr{yobj{
"ids": []string{"127.0.0.1"}, "ids": yarr{"127.0.0.1"},
"ip": "127.0.0.1", "ip": "127.0.0.1",
}}, }},
"schema_version": newSchemaVer, "schema_version": newSchemaVer,
@ -109,7 +109,7 @@ func TestUpgradeSchema5to6(t *testing.T) {
}, },
want: yobj{ want: yobj{
"clients": yarr{yobj{ "clients": yarr{yobj{
"ids": []string{"mac"}, "ids": yarr{"mac"},
"mac": "mac", "mac": "mac",
}}, }},
"schema_version": newSchemaVer, "schema_version": newSchemaVer,
@ -122,7 +122,7 @@ func TestUpgradeSchema5to6(t *testing.T) {
}, },
want: yobj{ want: yobj{
"clients": yarr{yobj{ "clients": yarr{yobj{
"ids": []string{"127.0.0.1", "mac"}, "ids": yarr{"127.0.0.1", "mac"},
"ip": "127.0.0.1", "ip": "127.0.0.1",
"mac": "mac", "mac": "mac",
}}, }},

View File

@ -34,36 +34,47 @@ func migrateTo10(diskConf yobj) (err error) {
diskConf["schema_version"] = 10 diskConf["schema_version"] = 10
dns, ok, err := fieldVal[yobj](diskConf, "dns") dns, ok, err := fieldVal[yobj](diskConf, "dns")
if err != nil { if !ok {
return err return err
} else if !ok {
return nil
} }
const quicPort = 784 const quicPort = 784
for _, upsField := range []string{ ups, ok, err := fieldVal[yarr](dns, "upstream_dns")
"upstream_dns", if err != nil {
"local_ptr_upstreams", return err
} { } else if ok {
var ups yarr if err = addQUICPorts(ups, quicPort); err != nil {
ups, ok, err = fieldVal[yarr](dns, upsField)
if err != nil {
return err return err
} else if !ok {
continue
} }
var u string dns["upstream_dns"] = ups
for i, uVal := range ups { }
u, ok = uVal.(string)
if !ok {
return fmt.Errorf("unexpected type of upstream field: %T", uVal)
}
ups[i] = addQUICPort(u, quicPort) ups, ok, err = fieldVal[yarr](dns, "local_ptr_upstreams")
if err != nil {
return err
} else if ok {
if err = addQUICPorts(ups, quicPort); err != nil {
return err
} }
dns[upsField] = ups
dns["local_ptr_upstreams"] = ups
}
return nil
}
// addQUICPorts inserts a port into each QUIC upstream's hostname in ups if
// those are missing.
func addQUICPorts(ups yarr, port int) (err error) {
for i, uVal := range ups {
u, ok := uVal.(string)
if !ok {
return fmt.Errorf("unexpected type of upstream field: %T", uVal)
}
ups[i] = addQUICPort(u, port)
} }
return nil return nil

View File

@ -21,18 +21,18 @@ func migrateTo12(diskConf yobj) (err error) {
diskConf["schema_version"] = 12 diskConf["schema_version"] = 12
dns, ok, err := fieldVal[yobj](diskConf, "dns") dns, ok, err := fieldVal[yobj](diskConf, "dns")
if err != nil { if !ok {
return err return err
} else if !ok {
return nil
} }
const field = "querylog_interval" const field = "querylog_interval"
qlogIvl, ok, err := fieldVal[int](dns, field) qlogIvl, ok, err := fieldVal[int](dns, field)
if err != nil { if !ok {
return err if err != nil {
} else if !ok { return err
}
// Set the initial value from home.initConfig function. // Set the initial value from home.initConfig function.
qlogIvl = 90 qlogIvl = 90
} }

View File

@ -19,17 +19,13 @@ func migrateTo13(diskConf yobj) (err error) {
diskConf["schema_version"] = 13 diskConf["schema_version"] = 13
dns, ok, err := fieldVal[yobj](diskConf, "dns") dns, ok, err := fieldVal[yobj](diskConf, "dns")
if err != nil { if !ok {
return err return err
} else if !ok {
return nil
} }
dhcp, ok, err := fieldVal[yobj](diskConf, "dhcp") dhcp, ok, err := fieldVal[yobj](diskConf, "dhcp")
if err != nil { if !ok {
return err return err
} else if !ok {
return nil
} }
return moveSameVal[string](dns, dhcp, "local_domain_name") return moveSameVal[string](dns, dhcp, "local_domain_name")

View File

@ -31,35 +31,32 @@ func migrateTo14(diskConf yobj) (err error) {
diskConf["schema_version"] = 14 diskConf["schema_version"] = 14
persistent, ok, err := fieldVal[yarr](diskConf, "clients") persistent, ok, err := fieldVal[yarr](diskConf, "clients")
if err != nil { if !ok {
return err if err != nil {
} else if !ok { return err
}
persistent = yarr{} persistent = yarr{}
} }
var rdnsSrc bool runtimeClients := yobj{
"whois": true,
"arp": true,
"rdns": false,
"dhcp": true,
"hosts": true,
}
diskConf["clients"] = yobj{
"persistent": persistent,
"runtime_sources": runtimeClients,
}
dns, ok, err := fieldVal[yobj](diskConf, "dns") dns, ok, err := fieldVal[yobj](diskConf, "dns")
if err != nil { if err != nil {
return err return err
} else if ok { } else if !ok {
rdnsSrc, ok, err = fieldVal[bool](dns, "resolve_clients") return nil
if err != nil {
return err
} else if ok {
delete(dns, "resolve_clients")
}
} }
diskConf["clients"] = yobj{ return moveVal[bool](dns, runtimeClients, "resolve_clients", "rdns")
"persistent": persistent,
"runtime_sources": yobj{
"whois": true,
"arp": true,
"rdns": rdnsSrc,
"dhcp": true,
"hosts": true,
},
}
return nil
} }

View File

@ -30,10 +30,8 @@ func migrateTo15(diskConf yobj) (err error) {
diskConf["schema_version"] = 15 diskConf["schema_version"] = 15
dns, ok, err := fieldVal[yobj](diskConf, "dns") dns, ok, err := fieldVal[yobj](diskConf, "dns")
if err != nil { if !ok {
return err return err
} else if !ok {
return nil
} }
qlog := map[string]any{ qlog := map[string]any{
@ -43,17 +41,12 @@ func migrateTo15(diskConf yobj) (err error) {
"interval": "2160h", "interval": "2160h",
"size_memory": 1000, "size_memory": 1000,
} }
err = coalesceError(
moveVal[bool](dns, qlog, "querylog_enabled", "enabled"),
moveVal[bool](dns, qlog, "querylog_file_enabled", "file_enabled"),
moveVal[string](dns, qlog, "querylog_interval", "interval"),
moveVal[int](dns, qlog, "querylog_size_memory", "size_memory"),
)
if err != nil {
return err
}
diskConf["querylog"] = qlog diskConf["querylog"] = qlog
return nil return coalesceError(
moveVal[bool](dns, qlog, "querylog_enabled", "enabled"),
moveVal[bool](dns, qlog, "querylog_file_enabled", "file_enabled"),
moveVal[any](dns, qlog, "querylog_interval", "interval"),
moveVal[int](dns, qlog, "querylog_size_memory", "size_memory"),
)
} }

View File

@ -47,10 +47,8 @@ func migrateTo16(diskConf yobj) (err error) {
diskConf["schema_version"] = 16 diskConf["schema_version"] = 16
dns, ok, err := fieldVal[yobj](diskConf, "dns") dns, ok, err := fieldVal[yobj](diskConf, "dns")
if err != nil { if !ok {
return err return err
} else if !ok {
return nil
} }
stats := yobj{ stats := yobj{
@ -58,24 +56,23 @@ func migrateTo16(diskConf yobj) (err error) {
"interval": 1, "interval": 1,
"ignored": yarr{}, "ignored": yarr{},
} }
diskConf["statistics"] = stats
const field = "statistics_interval" const field = "statistics_interval"
statsIvl, ok, err := fieldVal[int](dns, field) statsIvl, ok, err := fieldVal[int](dns, field)
if err != nil { if !ok {
return err return err
} else if ok {
if statsIvl == 0 {
// Set the interval to the default value of one day to make sure
// that it passes the validations.
stats["enabled"] = false
} else {
stats["interval"] = statsIvl
}
delete(dns, field)
} }
diskConf["statistics"] = stats if statsIvl == 0 {
// Set the interval to the default value of one day to make sure
// that it passes the validations.
stats["enabled"] = false
} else {
stats["interval"] = statsIvl
}
delete(dns, field)
return nil return nil
} }

View File

@ -22,10 +22,8 @@ func migrateTo17(diskConf yobj) (err error) {
diskConf["schema_version"] = 17 diskConf["schema_version"] = 17
dns, ok, err := fieldVal[yobj](diskConf, "dns") dns, ok, err := fieldVal[yobj](diskConf, "dns")
if err != nil { if !ok {
return err return err
} else if !ok {
return nil
} }
const field = "edns_client_subnet" const field = "edns_client_subnet"

View File

@ -26,10 +26,8 @@ func migrateTo18(diskConf yobj) (err error) {
diskConf["schema_version"] = 18 diskConf["schema_version"] = 18
dns, ok, err := fieldVal[yobj](diskConf, "dns") dns, ok, err := fieldVal[yobj](diskConf, "dns")
if err != nil { if !ok {
return err return err
} else if !ok {
return nil
} }
safeSearch := yobj{ safeSearch := yobj{
@ -41,7 +39,6 @@ func migrateTo18(diskConf yobj) (err error) {
"yandex": true, "yandex": true,
"youtube": true, "youtube": true,
} }
dns["safe_search"] = safeSearch dns["safe_search"] = safeSearch
return moveVal[bool](dns, safeSearch, "safesearch_enabled", "enabled") return moveVal[bool](dns, safeSearch, "safesearch_enabled", "enabled")

View File

@ -34,10 +34,8 @@ func migrateTo19(diskConf yobj) (err error) {
diskConf["schema_version"] = 19 diskConf["schema_version"] = 19
clients, ok, err := fieldVal[yobj](diskConf, "clients") clients, ok, err := fieldVal[yobj](diskConf, "clients")
if err != nil { if !ok {
return err return err
} else if !ok {
return nil
} }
persistent, ok, _ := fieldVal[yarr](clients, "persistent") persistent, ok, _ := fieldVal[yarr](clients, "persistent")

View File

@ -25,10 +25,8 @@ func migrateTo20(diskConf yobj) (err error) {
diskConf["schema_version"] = 20 diskConf["schema_version"] = 20
stats, ok, err := fieldVal[yobj](diskConf, "statistics") stats, ok, err := fieldVal[yobj](diskConf, "statistics")
if err != nil { if !ok {
return err return err
} else if !ok {
return nil
} }
const field = "interval" const field = "interval"

View File

@ -28,10 +28,8 @@ func migrateTo21(diskConf yobj) (err error) {
const field = "blocked_services" const field = "blocked_services"
dns, ok, err := fieldVal[yobj](diskConf, "dns") dns, ok, err := fieldVal[yobj](diskConf, "dns")
if err != nil { if !ok {
return err return err
} else if !ok {
return nil
} }
svcs := yobj{ svcs := yobj{

View File

@ -36,17 +36,13 @@ func migrateTo22(diskConf yobj) (err error) {
const field = "blocked_services" const field = "blocked_services"
clients, ok, err := fieldVal[yobj](diskConf, "clients") clients, ok, err := fieldVal[yobj](diskConf, "clients")
if err != nil { if !ok {
return err return err
} else if !ok {
return nil
} }
persistent, ok, err := fieldVal[yarr](clients, "persistent") persistent, ok, err := fieldVal[yarr](clients, "persistent")
if err != nil { if !ok {
return err return err
} else if !ok {
return nil
} }
for i, p := range persistent { for i, p := range persistent {

View File

@ -27,10 +27,8 @@ func migrateTo23(diskConf yobj) (err error) {
diskConf["schema_version"] = 23 diskConf["schema_version"] = 23
bindHost, ok, err := fieldVal[string](diskConf, "bind_host") bindHost, ok, err := fieldVal[string](diskConf, "bind_host")
if err != nil { if !ok {
return err return err
} else if !ok {
return nil
} }
bindHostAddr, err := netip.ParseAddr(bindHost) bindHostAddr, err := netip.ParseAddr(bindHost)

View File

@ -18,14 +18,13 @@ func migrateTo25(diskConf yobj) (err error) {
diskConf["schema_version"] = 25 diskConf["schema_version"] = 25
httpObj, ok, err := fieldVal[yobj](diskConf, "http") httpObj, ok, err := fieldVal[yobj](diskConf, "http")
if err != nil { if !ok {
return err return err
} else if !ok {
return nil
} }
pprofObj := yobj{ pprofObj := yobj{
"port": 6060, "enabled": false,
"port": 6060,
} }
err = moveVal[bool](diskConf, pprofObj, "debug_pprof", "enabled") err = moveVal[bool](diskConf, pprofObj, "debug_pprof", "enabled")

View File

@ -73,10 +73,8 @@ func migrateTo26(diskConf yobj) (err error) {
diskConf["schema_version"] = 26 diskConf["schema_version"] = 26
dns, ok, err := fieldVal[yobj](diskConf, "dns") dns, ok, err := fieldVal[yobj](diskConf, "dns")
if err != nil { if !ok {
return err return err
} else if !ok {
return nil
} }
filteringObj := yobj{} filteringObj := yobj{}

View File

@ -14,24 +14,18 @@ package confmigrate
// 'bootstrap_dns': // 'bootstrap_dns':
// - '1.1.1.1' // - '1.1.1.1'
// # … // # …
func migrateTo3(diskConf yobj) error { func migrateTo3(diskConf yobj) (err error) {
diskConf["schema_version"] = 3 diskConf["schema_version"] = 3
dnsConfig, ok, err := fieldVal[yobj](diskConf, "dns") dnsConfig, ok, err := fieldVal[yobj](diskConf, "dns")
if err != nil { if !ok {
return err return err
} else if !ok {
return nil
} }
bootstrapDNS, ok, err := fieldVal[any](dnsConfig, "bootstrap_dns") bootstrapDNS, ok, err := fieldVal[any](dnsConfig, "bootstrap_dns")
if err != nil { if ok {
return err dnsConfig["bootstrap_dns"] = yarr{bootstrapDNS}
} else if !ok {
return nil
} }
dnsConfig["bootstrap_dns"] = yarr{bootstrapDNS} return err
return nil
} }

View File

@ -6,20 +6,21 @@ package confmigrate
// 'schema_version': 3 // 'schema_version': 3
// 'clients': // 'clients':
// - # … // - # …
// # …
// //
// # AFTER: // # AFTER:
// 'schema_version': 4 // 'schema_version': 4
// 'clients': // 'clients':
// - 'use_global_blocked_services': true // - 'use_global_blocked_services': true
// # … // # …
// # …
func migrateTo4(diskConf yobj) (err error) { func migrateTo4(diskConf yobj) (err error) {
diskConf["schema_version"] = 4 diskConf["schema_version"] = 4
clients, ok, _ := fieldVal[yarr](diskConf, "clients") clients, ok, _ := fieldVal[yarr](diskConf, "clients")
if ok { if ok {
for i := range clients { for i := range clients {
var c yobj if c, isYobj := clients[i].(yobj); isYobj {
if c, ok = clients[i].(yobj); ok {
c["use_global_blocked_services"] = true c["use_global_blocked_services"] = true
} }
} }

View File

@ -30,10 +30,8 @@ func migrateTo5(diskConf yobj) (err error) {
} }
pass, ok, err := fieldVal[string](diskConf, "auth_pass") pass, ok, err := fieldVal[string](diskConf, "auth_pass")
if err != nil { if !ok {
return err return err
} else if !ok {
return nil
} }
delete(diskConf, "auth_pass") delete(diskConf, "auth_pass")

View File

@ -10,24 +10,26 @@ import "fmt"
// - # … // - # …
// 'ip': '127.0.0.1' // 'ip': '127.0.0.1'
// 'mac': 'AA:AA:AA:AA:AA:AA' // 'mac': 'AA:AA:AA:AA:AA:AA'
// # …
// # … // # …
// //
// # AFTER: // # AFTER:
// 'schema_version': 6 // 'schema_version': 6
// 'clients': // 'clients':
// - # … // - # …
// 'ip': '127.0.0.1'
// 'mac': 'AA:AA:AA:AA:AA:AA'
// 'ids': // 'ids':
// - '127.0.0.1' // - '127.0.0.1'
// - 'AA:AA:AA:AA:AA:AA' // - 'AA:AA:AA:AA:AA:AA'
// # …
// # … // # …
func migrateTo6(diskConf yobj) (err error) { func migrateTo6(diskConf yobj) (err error) {
diskConf["schema_version"] = 6 diskConf["schema_version"] = 6
clients, ok, err := fieldVal[yarr](diskConf, "clients") clients, ok, err := fieldVal[yarr](diskConf, "clients")
if err != nil { if !ok {
return err return err
} else if !ok {
return nil
} }
for i, client := range clients { for i, client := range clients {
@ -37,22 +39,14 @@ func migrateTo6(diskConf yobj) (err error) {
return fmt.Errorf("unexpected type of client at index %d: %T", i, client) return fmt.Errorf("unexpected type of client at index %d: %T", i, client)
} }
var ids []string ids := yarr{}
for _, id := range []string{"ip", "mac"} {
var ip string val, _, valErr := fieldVal[string](c, id)
ip, _, err = fieldVal[string](c, "ip") if valErr != nil {
if err != nil { return fmt.Errorf("client at index %d: %w", i, valErr)
return fmt.Errorf("client at index %d: %w", i, err) } else if val != "" {
} else if ip != "" { ids = append(ids, val)
ids = append(ids, ip) }
}
var mac string
mac, _, err = fieldVal[string](c, "mac")
if err != nil {
return fmt.Errorf("client at index %d: %w", i, err)
} else if mac != "" {
ids = append(ids, mac)
} }
c["ids"] = ids c["ids"] = ids

View File

@ -28,11 +28,11 @@ package confmigrate
// 'lease_duration': 86400 // 'lease_duration': 86400
// 'icmp_timeout_msec': 1000 // 'icmp_timeout_msec': 1000
// # … // # …
func migrateTo7(diskConf yobj) error { func migrateTo7(diskConf yobj) (err error) {
diskConf["schema_version"] = 7 diskConf["schema_version"] = 7
dhcp, ok, err := fieldVal[yobj](diskConf, "dhcp") dhcp, ok, _ := fieldVal[yobj](diskConf, "dhcp")
if err != nil || !ok { if !ok {
return nil return nil
} }

View File

@ -20,17 +20,13 @@ func migrateTo8(diskConf yobj) (err error) {
diskConf["schema_version"] = 8 diskConf["schema_version"] = 8
dns, ok, err := fieldVal[yobj](diskConf, "dns") dns, ok, err := fieldVal[yobj](diskConf, "dns")
if err != nil { if !ok {
return err return err
} else if !ok {
return nil
} }
bindHost, ok, err := fieldVal[string](dns, "bind_host") bindHost, ok, err := fieldVal[string](dns, "bind_host")
if err != nil { if !ok {
return err return err
} else if !ok {
return nil
} }
delete(dns, "bind_host") delete(dns, "bind_host")

View File

@ -19,10 +19,8 @@ func migrateTo9(diskConf yobj) (err error) {
diskConf["schema_version"] = 9 diskConf["schema_version"] = 9
dns, ok, err := fieldVal[yobj](diskConf, "dns") dns, ok, err := fieldVal[yobj](diskConf, "dns")
if err != nil { if !ok {
return err return err
} else if !ok {
return nil
} }
return moveVal[string](dns, dns, "autohost_tld", "local_domain_name") return moveVal[string](dns, dns, "autohost_tld", "local_domain_name")

View File

@ -1,6 +1,8 @@
package confmigrate package confmigrate
import "fmt" import (
"fmt"
)
type ( type (
// yarr is the convenience alias for YAML array. // yarr is the convenience alias for YAML array.

View File

@ -177,7 +177,6 @@ run_linter gocyclo --over 10 .
gocognit_paths="\ gocognit_paths="\
./internal/aghnet/ 20 ./internal/aghnet/ 20
./internal/querylog/ 20 ./internal/querylog/ 20
./internal/confmigrate/ 19
./internal/dnsforward/ 19 ./internal/dnsforward/ 19
./internal/home/ 19 ./internal/home/ 19
./internal/aghtls/ 18 ./internal/aghtls/ 18
@ -194,6 +193,7 @@ gocognit_paths="\
./internal/aghrenameio/ 10 ./internal/aghrenameio/ 10
./internal/arpdb/ 10 ./internal/arpdb/ 10
./internal/client/ 10 ./internal/client/ 10
./internal/confmigrate/ 10
./internal/dhcpsvc 10 ./internal/dhcpsvc 10
./internal/filtering/hashprefix/ 10 ./internal/filtering/hashprefix/ 10
./internal/filtering/rulelist/ 10 ./internal/filtering/rulelist/ 10