Skip to content

Commit

Permalink
Replace deprecated API endpoints (#326)
Browse files Browse the repository at this point in the history
* extend query log config
* replace deprecated services
* replace more deprecated services
* implement equals for stats config
  • Loading branch information
bakito authored Apr 2, 2024
1 parent 4a62b80 commit 3c58a8f
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 167 deletions.
43 changes: 15 additions & 28 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,16 @@ type Client interface {
SetSafeSearchConfig(settings *model.SafeSearchConfig) error
ProfileInfo() (*model.ProfileInfo, error)
SetProfileInfo(settings *model.ProfileInfo) error
BlockedServices() (*model.BlockedServicesArray, error)
BlockedServicesSchedule() (*model.BlockedServicesSchedule, error)
SetBlockedServices(services *model.BlockedServicesArray) error
SetBlockedServicesSchedule(schedule *model.BlockedServicesSchedule) error
Clients() (*model.Clients, error)
AddClient(client *model.Client) error
UpdateClient(client *model.Client) error
DeleteClient(client *model.Client) error
QueryLogConfig() (*model.QueryLogConfig, error)
SetQueryLogConfig(*model.QueryLogConfig) error
StatsConfig() (*model.StatsConfig, error)
SetStatsConfig(sc *model.StatsConfig) error
QueryLogConfig() (*model.QueryLogConfigWithIgnored, error)
SetQueryLogConfig(*model.QueryLogConfigWithIgnored) error
StatsConfig() (*model.GetStatsConfigResponse, error)
SetStatsConfig(sc *model.PutStatsConfigUpdateRequest) error
Setup() error
AccessList() (*model.AccessList, error)
SetAccessList(*model.AccessList) error
Expand Down Expand Up @@ -287,17 +285,6 @@ func (cl *client) ToggleFiltering(enabled bool, interval int) error {
}), "/filtering/config")
}

func (cl *client) BlockedServices() (*model.BlockedServicesArray, error) {
svcs := &model.BlockedServicesArray{}
err := cl.doGet(cl.client.R().EnableTrace().SetResult(svcs), "/blocked_services/list")
return svcs, err
}

func (cl *client) SetBlockedServices(services *model.BlockedServicesArray) error {
cl.log.With("services", model.ArrayString(services)).Info("Set blocked services")
return cl.doPost(cl.client.R().EnableTrace().SetBody(services), "/blocked_services/set")
}

func (cl *client) BlockedServicesSchedule() (*model.BlockedServicesSchedule, error) {
sched := &model.BlockedServicesSchedule{}
err := cl.doGet(cl.client.R().EnableTrace().SetResult(sched), "/blocked_services/get")
Expand Down Expand Up @@ -330,26 +317,26 @@ func (cl *client) DeleteClient(client *model.Client) error {
return cl.doPost(cl.client.R().EnableTrace().SetBody(client), "/clients/delete")
}

func (cl *client) QueryLogConfig() (*model.QueryLogConfig, error) {
qlc := &model.QueryLogConfig{}
err := cl.doGet(cl.client.R().EnableTrace().SetResult(qlc), "/querylog_info")
func (cl *client) QueryLogConfig() (*model.QueryLogConfigWithIgnored, error) {
qlc := &model.QueryLogConfigWithIgnored{}
err := cl.doGet(cl.client.R().EnableTrace().SetResult(qlc), "/querylog/config")
return qlc, err
}

func (cl *client) SetQueryLogConfig(qlc *model.QueryLogConfig) error {
func (cl *client) SetQueryLogConfig(qlc *model.QueryLogConfigWithIgnored) error {
cl.log.With("enabled", *qlc.Enabled, "interval", *qlc.Interval, "anonymizeClientIP", *qlc.AnonymizeClientIp).Info("Set query log config")
return cl.doPost(cl.client.R().EnableTrace().SetBody(qlc), "/querylog_config")
return cl.doPut(cl.client.R().EnableTrace().SetBody(qlc), "/querylog/config/update")
}

func (cl *client) StatsConfig() (*model.StatsConfig, error) {
stats := &model.StatsConfig{}
err := cl.doGet(cl.client.R().EnableTrace().SetResult(stats), "/stats_info")
func (cl *client) StatsConfig() (*model.GetStatsConfigResponse, error) {
stats := &model.GetStatsConfigResponse{}
err := cl.doGet(cl.client.R().EnableTrace().SetResult(stats), "/stats/config")
return stats, err
}

func (cl *client) SetStatsConfig(sc *model.StatsConfig) error {
cl.log.With("interval", *sc.Interval).Info("Set stats config")
return cl.doPost(cl.client.R().EnableTrace().SetBody(sc), "/stats_config")
func (cl *client) SetStatsConfig(sc *model.PutStatsConfigUpdateRequest) error {
cl.log.With("interval", sc.Interval).Info("Set stats config")
return cl.doPut(cl.client.R().EnableTrace().SetBody(sc), "/stats/config/update")
}

func (cl *client) Setup() error {
Expand Down
41 changes: 17 additions & 24 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,20 +239,6 @@ var _ = Describe("Client", func() {
})
})

Context("BlockedServices", func() {
It("should read BlockedServices", func() {
ts, cl = ClientGet("blockedservices-list.json", "/blocked_services/list")
s, err := cl.BlockedServices()
Ω(err).ShouldNot(HaveOccurred())
Ω(*s).Should(HaveLen(2))
})
It("should set BlockedServices", func() {
ts, cl = ClientPost("/blocked_services/set", `["bar","foo"]`)
err := cl.SetBlockedServices(&model.BlockedServicesArray{"foo", "bar"})
Ω(err).ShouldNot(HaveOccurred())
})
})

Context("BlockedServicesSchedule", func() {
It("should read BlockedServicesSchedule", func() {
ts, cl = ClientGet("blockedservicesschedule-get.json", "/blocked_services/get")
Expand Down Expand Up @@ -308,7 +294,7 @@ var _ = Describe("Client", func() {

Context("QueryLogConfig", func() {
It("should read QueryLogConfig", func() {
ts, cl = ClientGet("querylog_info.json", "/querylog_info")
ts, cl = ClientGet("querylog_config.json", "/querylog/config")
qlc, err := cl.QueryLogConfig()
Ω(err).ShouldNot(HaveOccurred())
Ω(qlc.Enabled).ShouldNot(BeNil())
Expand All @@ -317,26 +303,33 @@ var _ = Describe("Client", func() {
Ω(*qlc.Interval).Should(Equal(model.QueryLogConfigInterval(90)))
})
It("should set QueryLogConfig", func() {
ts, cl = ClientPost("/querylog_config", `{"anonymize_client_ip":true,"enabled":true,"interval":123}`)
ts, cl = ClientPut("/querylog/config/update", `{"anonymize_client_ip":true,"enabled":true,"interval":123,"ignored":["foo.bar"]}`)

var interval model.QueryLogConfigInterval = 123
err := cl.SetQueryLogConfig(&model.QueryLogConfig{AnonymizeClientIp: utils.Ptr(true), Interval: &interval, Enabled: utils.Ptr(true)})
err := cl.SetQueryLogConfig(&model.QueryLogConfigWithIgnored{
QueryLogConfig: model.QueryLogConfig{
AnonymizeClientIp: utils.Ptr(true),
Interval: &interval,
Enabled: utils.Ptr(true),
},
Ignored: []string{"foo.bar"},
})
Ω(err).ShouldNot(HaveOccurred())
})
})
Context("StatsConfig", func() {
It("should read StatsConfig", func() {
ts, cl = ClientGet("stats_info.json", "/stats_info")
ts, cl = ClientGet("stats_info.json", "/stats/config")
sc, err := cl.StatsConfig()
Ω(err).ShouldNot(HaveOccurred())
Ω(sc.Interval).ShouldNot(BeNil())
Ω(*sc.Interval).Should(Equal(model.StatsConfigInterval(1)))
Ω(sc.Interval).Should(Equal(float32(1)))
})
It("should set StatsConfig", func() {
ts, cl = ClientPost("/stats_config", `{"interval":123}`)
ts, cl = ClientPost("/stats/config/update", `{"enabled":false,"ignored":null,"interval":123}`)

var interval model.StatsConfigInterval = 123
err := cl.SetStatsConfig(&model.StatsConfig{Interval: &interval})
var interval float32 = 123
err := cl.SetStatsConfig(&model.PutStatsConfigUpdateRequest{Interval: interval})
Ω(err).ShouldNot(HaveOccurred())
})
})
Expand All @@ -361,8 +354,8 @@ var _ = Describe("Client", func() {

Context("doPost", func() {
It("should return an error on status code != 200", func() {
var interval model.StatsConfigInterval = 123
err := cl.SetStatsConfig(&model.StatsConfig{Interval: &interval})
var interval float32 = 123
err := cl.SetStatsConfig(&model.PutStatsConfigUpdateRequest{Interval: interval})
Ω(err).Should(HaveOccurred())
Ω(err.Error()).Should(Equal("401 Unauthorized"))
})
Expand Down
18 changes: 14 additions & 4 deletions pkg/client/model/model-functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,16 @@ func (f *Filter) Equals(o *Filter) bool {
return f.Enabled == o.Enabled && f.Url == o.Url && f.Name == o.Name
}

type QueryLogConfigWithIgnored struct {
QueryLogConfig

// Ignored List of host names, which should not be written to log
Ignored []string `json:"ignored,omitempty"`
}

// Equals QueryLogConfig equal check
func (qlc *QueryLogConfig) Equals(o *QueryLogConfig) bool {
return ptrEquals(qlc.Enabled, o.Enabled) &&
ptrEquals(qlc.AnonymizeClientIp, o.AnonymizeClientIp) &&
qlc.Interval.Equals(o.Interval)
func (qlc *QueryLogConfigWithIgnored) Equals(o *QueryLogConfigWithIgnored) bool {
return utils.JsonEquals(qlc, o)
}

// Equals QueryLogConfigInterval equal check
Expand Down Expand Up @@ -411,3 +416,8 @@ func (c *DNSConfig) Sanitize(l *zap.SugaredLogger) {
c.UsePrivatePtrResolvers = utils.Ptr(false)
}
}

// Equals GetStatsConfigResponse equal check
func (sc *GetStatsConfigResponse) Equals(o *GetStatsConfigResponse) bool {
return utils.JsonEquals(sc, o)
}
8 changes: 4 additions & 4 deletions pkg/client/model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ var _ = Describe("Types", func() {
Context("QueryLogConfig", func() {
Context("Equal", func() {
var (
a *model.QueryLogConfig
b *model.QueryLogConfig
a *model.QueryLogConfigWithIgnored
b *model.QueryLogConfigWithIgnored
)
BeforeEach(func() {
a = &model.QueryLogConfig{}
b = &model.QueryLogConfig{}
a = &model.QueryLogConfigWithIgnored{}
b = &model.QueryLogConfigWithIgnored{}
})
It("should be equal", func() {
a.Enabled = utils.Ptr(true)
Expand Down
41 changes: 6 additions & 35 deletions pkg/mocks/client/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 1 addition & 12 deletions pkg/sync/action-general.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ var (
if err != nil {
return err
}
if ac.origin.statsConfig.Interval != sc.Interval {
if !sc.Equals(ac.origin.statsConfig) {
return ac.client.SetStatsConfig(ac.origin.statsConfig)
}
return nil
Expand Down Expand Up @@ -111,17 +111,6 @@ var (
return nil
}

actionBlockedServices = func(ac *actionContext) error {
rs, err := ac.client.BlockedServices()
if err != nil {
return err
}

if !model.EqualsStringSlice(ac.origin.blockedServices, rs, true) {
return ac.client.SetBlockedServices(ac.origin.blockedServices)
}
return nil
}
actionBlockedServicesSchedule = func(ac *actionContext) error {
rbss, err := ac.client.BlockedServicesSchedule()
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion pkg/sync/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func setupActions(cfg *types.Config) (actions []syncAction) {
}
if cfg.Features.Services {
actions = append(actions,
action("blocked services", actionBlockedServices),
action("blocked services schedule", actionBlockedServicesSchedule),
)
}
Expand Down
11 changes: 2 additions & 9 deletions pkg/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,6 @@ func (w *worker) sync() {
return
}

o.blockedServices, err = oc.BlockedServices()
if err != nil {
sl.With("error", err).Error("Error getting origin blocked services")
return
}

o.blockedServicesSchedule, err = oc.BlockedServicesSchedule()
if err != nil {
sl.With("error", err).Error("Error getting origin blocked services schedule")
Expand Down Expand Up @@ -327,12 +321,11 @@ func (w *worker) statusWithSetup(rl *zap.SugaredLogger, replica types.AdGuardIns
type origin struct {
status *model.ServerStatus
rewrites *model.RewriteEntries
blockedServices *model.BlockedServicesArray
blockedServicesSchedule *model.BlockedServicesSchedule
filters *model.FilterStatus
clients *model.Clients
queryLogConfig *model.QueryLogConfig
statsConfig *model.StatsConfig
queryLogConfig *model.QueryLogConfigWithIgnored
statsConfig *model.GetStatsConfigResponse
accessList *model.AccessList
dnsConfig *model.DNSConfig
dhcpServerConfig *model.DhcpStatus
Expand Down
Loading

0 comments on commit 3c58a8f

Please sign in to comment.