Skip to content

Commit

Permalink
fix: Rule set, the rule is loaded from, is considered while updating …
Browse files Browse the repository at this point in the history
…or deleting rules (#1298)
  • Loading branch information
dadrus authored Apr 1, 2024
1 parent 6accae8 commit e571248
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 40 deletions.
22 changes: 0 additions & 22 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,6 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/go-co-op/gocron/v2 v2.2.6 h1:sKRt4kemEzY9HnBx9BBnFDPXoOxBy77V4WVtoouhJgg=
github.com/go-co-op/gocron/v2 v2.2.6/go.mod h1:mZx3gMSlFnb97k3hRqX3+GdlG3+DUwTh6B8fnsTScXg=
github.com/go-co-op/gocron/v2 v2.2.7 h1:NEhw7aQ6i8L7+6FwHS5XdEAsMNX706GB+HYBwcvnNGY=
github.com/go-co-op/gocron/v2 v2.2.7/go.mod h1:mZx3gMSlFnb97k3hRqX3+GdlG3+DUwTh6B8fnsTScXg=
github.com/go-co-op/gocron/v2 v2.2.8 h1:he9V8ntTy/B7UVzNNamSqgAZtRg8KhNYH+LLD9hWiVI=
github.com/go-co-op/gocron/v2 v2.2.8/go.mod h1:mZx3gMSlFnb97k3hRqX3+GdlG3+DUwTh6B8fnsTScXg=
github.com/go-co-op/gocron/v2 v2.2.9 h1:aoKosYWSSdXFLecjFWX1i8+R6V7XdZb8sB2ZKAY5Yis=
github.com/go-co-op/gocron/v2 v2.2.9/go.mod h1:mZx3gMSlFnb97k3hRqX3+GdlG3+DUwTh6B8fnsTScXg=
github.com/go-http-utils/etag v0.0.0-20161124023236-513ea8f21eb1 h1:zga7zaRE8HCbWjcXMDlfvmQtH0/kMVLo7cQ48dy6kWg=
Expand Down Expand Up @@ -337,12 +331,8 @@ github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSz
github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc=
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
github.com/redis/rueidis v1.0.32 h1:xDVD5G0F4digNm4EOdtjLq+MdIU3arvbQNiL0g4PYn4=
github.com/redis/rueidis v1.0.32/go.mod h1:g8nPmgR4C68N3abFiOc/gUOSEKw3Tom6/teYMehg4RE=
github.com/redis/rueidis v1.0.33 h1:MYNFbWB/UFAWBuXJUetZqsQaU5eay8u1P+t5aaB9eDw=
github.com/redis/rueidis v1.0.33/go.mod h1:g8nPmgR4C68N3abFiOc/gUOSEKw3Tom6/teYMehg4RE=
github.com/redis/rueidis/rueidisotel v1.0.32 h1:+J6SUq5xmx7YZKRGMNsTKEzAA1OrAZLA567lt3ha3pw=
github.com/redis/rueidis/rueidisotel v1.0.32/go.mod h1:KgAcznoejka2GbAVRnX+sbnCSvfTvyD0bWkbNNqTODw=
github.com/redis/rueidis/rueidisotel v1.0.33 h1:0VZm6lrbtCCxDl52gVzesdcQvcLYaqL1qqlgkkbasMk=
github.com/redis/rueidis/rueidisotel v1.0.33/go.mod h1:u8kDduTW5Kx6O+Fnts6Zkok4cnv5DbqMrBIFJXXWcdw=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
Expand Down Expand Up @@ -498,8 +488,6 @@ golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1m
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20240314144324-c7f7c6466f7f h1:3CW0unweImhOzd5FmYuRsD4Y4oQFKZIjAnKbjV4WIrw=
golang.org/x/exp v0.0.0-20240314144324-c7f7c6466f7f/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc=
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 h1:aAcj0Da7eBAtrTp03QXWvm88pSyOt+UgdZw2BFZ+lEw=
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down Expand Up @@ -606,8 +594,6 @@ golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.8.0/go.mod h1:JxBZ99ISMI5ViVkT1tr6tdNmXeTrcpVSD3vZ1RsRdN4=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
golang.org/x/tools v0.18.0 h1:k8NLag8AGHnn+PHbl7g43CtqZAwG60vZkLqgyZgIHgQ=
golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg=
golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw=
golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand All @@ -629,8 +615,6 @@ google.golang.org/genproto v0.0.0-20240311173647-c811ad7063a7 h1:ImUcDPHjTrAqNhl
google.golang.org/genproto v0.0.0-20240311173647-c811ad7063a7/go.mod h1:/3XmxOjePkvmKrHuBy4zNFw7IzxJXtAgdpXi8Ll990U=
google.golang.org/genproto/googleapis/api v0.0.0-20240311173647-c811ad7063a7 h1:oqta3O3AnlWbmIE3bFnWbu4bRxZjfbWCp0cKSuZh01E=
google.golang.org/genproto/googleapis/api v0.0.0-20240311173647-c811ad7063a7/go.mod h1:VQW3tUculP/D4B+xVCo+VgSq8As6wA9ZjHl//pmk+6s=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240314234333-6e1732d8331c h1:lfpJ/2rWPa/kJgxyyXM8PrNnfCzcmxJ265mADgwmvLI=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240314234333-6e1732d8331c/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240325203815-454cdb8f5daa h1:RBgMaUMP+6soRkik4VoN8ojR2nex2TqZwjSSogic+eo=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240325203815-454cdb8f5daa/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
Expand Down Expand Up @@ -670,16 +654,10 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
k8s.io/api v0.29.2 h1:hBC7B9+MU+ptchxEqTNW2DkUosJpp1P+Wn6YncZ474A=
k8s.io/api v0.29.2/go.mod h1:sdIaaKuU7P44aoyyLlikSLayT6Vb7bvJNCX105xZXY0=
k8s.io/api v0.29.3 h1:2ORfZ7+bGC3YJqGpV0KSDDEVf8hdGQ6A03/50vj8pmw=
k8s.io/api v0.29.3/go.mod h1:y2yg2NTyHUUkIoTC+phinTnEa3KFM6RZ3szxt014a80=
k8s.io/apimachinery v0.29.2 h1:EWGpfJ856oj11C52NRCHuU7rFDwxev48z+6DSlGNsV8=
k8s.io/apimachinery v0.29.2/go.mod h1:6HVkd1FwxIagpYrHSwJlQqZI3G9LfYWRPAkUvLnXTKU=
k8s.io/apimachinery v0.29.3 h1:2tbx+5L7RNvqJjn7RIuIKu9XTsIZ9Z5wX2G22XAa5EU=
k8s.io/apimachinery v0.29.3/go.mod h1:hx/S4V2PNW4OMg3WizRrHutyB5la0iCUbZym+W0EQIU=
k8s.io/client-go v0.29.2 h1:FEg85el1TeZp+/vYJM7hkDlSTFZ+c5nnK44DJ4FyoRg=
k8s.io/client-go v0.29.2/go.mod h1:knlvFZE58VpqbQpJNbCbctTVXcd35mMyAAwBdpt4jrA=
k8s.io/client-go v0.29.3 h1:R/zaZbEAxqComZ9FHeQwOh3Y1ZUs7FaHKZdQtIc2WZg=
k8s.io/client-go v0.29.3/go.mod h1:tkDisCvgPfiRpxGnOORfkljmS+UrW+WtXAy2fTvXJB0=
k8s.io/klog/v2 v2.120.1 h1:QXU6cPEOIslTGvZaXvFWiP9VKyeet3sawzTOvdXb4Vw=
Expand Down
6 changes: 3 additions & 3 deletions internal/rules/repository_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (r *repository) updateRuleSet(srcID string, rules []rule.Rule) {
return !known
})

// find update rules
// find updated rules
updatedRules := slicex.Filter(rules, func(r rule.Rule) bool {
loaded := r.(*ruleImpl) // nolint: forcetypeassert

Expand Down Expand Up @@ -232,7 +232,7 @@ func (r *repository) removeRules(rules []rule.Rule) {

for idx, rul := range r.rules {
for _, tbd := range rules {
if rul.ID() == tbd.ID() {
if rul.SrcID() == tbd.SrcID() && rul.ID() == tbd.ID() {
idxs = append(idxs, idx)

r.logger.Debug().Str("_src", rul.SrcID()).Str("_id", rul.ID()).Msg("Rule removed")
Expand Down Expand Up @@ -266,7 +266,7 @@ func (r *repository) removeRules(rules []rule.Rule) {
func (r *repository) replaceRules(rules []rule.Rule) {
for _, updated := range rules {
for idx, existing := range r.rules {
if existing.ID() == updated.ID() {
if updated.SrcID() == existing.SrcID() && existing.ID() == updated.ID() {
r.rules[idx] = updated

r.logger.Debug().
Expand Down
31 changes: 16 additions & 15 deletions internal/rules/repository_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,35 +180,34 @@ func TestRepositoryAddAndRemoveRulesFromDifferentRuleSets(t *testing.T) {
&ruleImpl{id: "1", srcID: "bar"},
&ruleImpl{id: "2", srcID: "baz"},
&ruleImpl{id: "3", srcID: "bar"},
&ruleImpl{id: "4", srcID: "bar"},
&ruleImpl{id: "4", srcID: "foo"},
})

// THEN
assert.Len(t, repo.rules, 4)
assert.Len(t, repo.rules, 5)

// WHEN
repo.deleteRuleSet("baz")
repo.deleteRuleSet("bar")

// THEN
assert.Len(t, repo.rules, 3)
assert.Len(t, repo.rules, 2)
assert.ElementsMatch(t, repo.rules, []rule.Rule{
&ruleImpl{id: "1", srcID: "bar"},
&ruleImpl{id: "3", srcID: "bar"},
&ruleImpl{id: "2", srcID: "baz"},
&ruleImpl{id: "4", srcID: "foo"},
})

// WHEN
repo.deleteRuleSet("foo")

// THEN
assert.Len(t, repo.rules, 2)
assert.Len(t, repo.rules, 1)
assert.ElementsMatch(t, repo.rules, []rule.Rule{
&ruleImpl{id: "1", srcID: "bar"},
&ruleImpl{id: "3", srcID: "bar"},
&ruleImpl{id: "2", srcID: "baz"},
})

// WHEN
repo.deleteRuleSet("bar")
repo.deleteRuleSet("baz")

// THEN
assert.Empty(t, repo.rules)
Expand Down Expand Up @@ -306,7 +305,7 @@ func TestRepositoryRuleSetLifecycleManagement(t *testing.T) {
Source: "test2",
ChangeType: event.Create,
Rules: []rule.Rule{
&ruleImpl{id: "rule:foo1", srcID: "test2", hash: []byte{1}},
&ruleImpl{id: "rule:bar", srcID: "test2", hash: []byte{1}},
&ruleImpl{id: "rule:foo2", srcID: "test2", hash: []byte{2}},
&ruleImpl{id: "rule:foo3", srcID: "test2", hash: []byte{3}},
&ruleImpl{id: "rule:foo4", srcID: "test2", hash: []byte{4}},
Expand All @@ -316,7 +315,7 @@ func TestRepositoryRuleSetLifecycleManagement(t *testing.T) {
Source: "test2",
ChangeType: event.Update,
Rules: []rule.Rule{
&ruleImpl{id: "rule:foo1", srcID: "test2", hash: []byte{5}}, // updated
&ruleImpl{id: "rule:bar", srcID: "test2", hash: []byte{5}}, // updated
&ruleImpl{id: "rule:foo2", srcID: "test2", hash: []byte{2}}, // as before
// &ruleImpl{id: "rule:foo3", srcID: "test2", hash: []byte{3}}, // deleted
&ruleImpl{id: "rule:foo4", srcID: "test2", hash: []byte{4}}, // as before
Expand All @@ -327,10 +326,12 @@ func TestRepositoryRuleSetLifecycleManagement(t *testing.T) {
t.Helper()

require.Len(t, repo.rules, 4)
assert.Equal(t, &ruleImpl{id: "rule:bar", srcID: "test1"}, repo.rules[0])
assert.Equal(t, &ruleImpl{id: "rule:foo1", srcID: "test2", hash: []byte{5}}, repo.rules[1])
assert.Equal(t, &ruleImpl{id: "rule:foo2", srcID: "test2", hash: []byte{2}}, repo.rules[2])
assert.Equal(t, &ruleImpl{id: "rule:foo4", srcID: "test2", hash: []byte{4}}, repo.rules[3])
assert.ElementsMatch(t, repo.rules, []rule.Rule{
&ruleImpl{id: "rule:bar", srcID: "test1"},
&ruleImpl{id: "rule:bar", srcID: "test2", hash: []byte{5}},
&ruleImpl{id: "rule:foo2", srcID: "test2", hash: []byte{2}},
&ruleImpl{id: "rule:foo4", srcID: "test2", hash: []byte{4}},
})
},
},
} {
Expand Down

0 comments on commit e571248

Please sign in to comment.