Skip to content

Commit

Permalink
fix: fault injection regex (#734)
Browse files Browse the repository at this point in the history
* fix(kuma-cp): fault injection regex
  • Loading branch information
lobkovilya authored May 12, 2020
1 parent 909a924 commit bb26ae7
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
4 changes: 2 additions & 2 deletions pkg/xds/envoy/listeners/fault_injection_configurer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ var _ = Describe("FaultInjectionConfigurer", func() {
safeRegexMatch:
googleRe2:
maxProgramSize: 500
regex: '&tag1=[^&]*value1[,&].*&tag2=[^&]*value2[,&].*'
regex: '.*&tag1=[^&]*value1[,&].*&tag2=[^&]*value2[,&].*'
- name: envoy.router
statPrefix: stats`,
}),
Expand Down Expand Up @@ -114,7 +114,7 @@ var _ = Describe("FaultInjectionConfigurer", func() {
safeRegexMatch:
googleRe2:
maxProgramSize: 500
regex: '(&tag1=[^&]*value1m1[,&].*&tag2=[^&]*value2m1[,&].*|&tag1=[^&]*value1m2[,&].*&tag2=[^&]*value2m2[,&].*)'
regex: '(.*&tag1=[^&]*value1m1[,&].*&tag2=[^&]*value2m1[,&].*|.*&tag1=[^&]*value1m2[,&].*&tag2=[^&]*value2m2[,&].*)'
- name: envoy.router
statPrefix: stats`,
}),
Expand Down
1 change: 1 addition & 0 deletions pkg/xds/envoy/tags/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func MatchingRegex(tags mesh_proto.SingleValueTagSet) (re string) {
expr := keyIsEqual + value + `.*`
re += expr
}
re = `.*` + re
return
}

Expand Down
24 changes: 22 additions & 2 deletions pkg/xds/envoy/tags/match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ import (
"github.com/Kong/kuma/pkg/xds/envoy/tags"
)

func strictMatch(re *regexp.Regexp, s string) bool {
idx := re.FindStringIndex(s)
if idx == nil {
return false
}
return idx[0] == 0 && idx[1] == len(s)
}

var _ = Describe("MatchingRegex", func() {

type testCase struct {
Expand All @@ -27,7 +35,7 @@ var _ = Describe("MatchingRegex", func() {
// then
Expect(err).ToNot(HaveOccurred())
// when
matched := re.MatchString(tags.Serialize(given.serviceTags))
matched := strictMatch(re, tags.Serialize(given.serviceTags))
// then
Expect(matched).To(Equal(given.expected))
},
Expand All @@ -42,6 +50,18 @@ var _ = Describe("MatchingRegex", func() {
},
expected: true,
}),
Entry("match 3 one-value tags", testCase{
serviceTags: mesh_proto.MultiValueTagSet{
"tag1": {"value1": true},
"tag2": {"value2": true},
"tag3": {"value3": true},
},
selector: mesh_proto.SingleValueTagSet{
"tag2": "value2",
"tag3": "value3",
},
expected: true,
}),
Entry("match without middle tag2", testCase{
serviceTags: mesh_proto.MultiValueTagSet{
"tag1": {"value1": true, "value2": true},
Expand Down Expand Up @@ -142,7 +162,7 @@ var _ = Describe("RegexOR", func() {
Expect(err).ToNot(HaveOccurred())

for i, service := range given.servicesTags {
matched := re.MatchString(tags.Serialize(service))
matched := strictMatch(re, tags.Serialize(service))
Expect(matched).To(Equal(given.expected[i]))
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ resources:
safeRegexMatch:
googleRe2:
maxProgramSize: 500
regex: '&service=[^&]*frontend[,&].*'
regex: '.*&service=[^&]*frontend[,&].*'
- name: envoy.router
routeConfig:
name: inbound:backend1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ resources:
safeRegexMatch:
googleRe2:
maxProgramSize: 500
regex: '&service=[^&]*frontend[,&].*'
regex: '.*&service=[^&]*frontend[,&].*'
- name: envoy.router
routeConfig:
name: inbound:backend1
Expand Down

0 comments on commit bb26ae7

Please sign in to comment.