Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GitHub warnings #931

Merged
merged 15 commits into from
Feb 17, 2023
Prev Previous commit
Next Next commit
move StringMatcher to package stringtool
  • Loading branch information
localvar committed Feb 6, 2023
commit faae5f0159638e3bb635e83faddf0bcf3f9485b8
25 changes: 14 additions & 11 deletions doc/reference/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@
- [DataBuilder](#databuilder)
- [Configuration](#configuration-18)
- [Results](#results-18)
- [OIDCAdaptor](#OIDCAdaptor)
- [OIDCAdaptor](#oidcadaptor)
- [Configuration](#configuration-19)
- [Results](#results-19)
- [OPAFilter](#OPAFilter)
- [OPAFilter](#opafilter)
- [Configuration](#configuration-20)
- [Results](#results-20)
- [Redirector](#Redirector)
- [Results](#results-20)
- [Redirector](#redirector)
- [Configuration](#configuration-21)
- [Results](#results-21)
- [Results](#results-21)
- [Common Types](#common-types)
- [pathadaptor.Spec](#pathadaptorspec)
- [pathadaptor.RegexpReplace](#pathadaptorregexpreplace)
Expand All @@ -75,9 +75,10 @@
- [proxy.Server](#proxyserver)
- [proxy.LoadBalanceSpec](#proxyloadbalancespec)
- [proxy.StickySessionSpec](#proxystickysessionspec)
- [proxy.HealthCheckSpec](#proxyhealthcheckspec)
- [proxy.MemoryCacheSpec](#proxymemorycachespec)
- [proxy.RequestMatcherSpec](#proxyrequestmatcherspec)
- [proxy.StringMatcher](#proxystringmatcher)
- [StringMatcher](#stringmatcher)
- [proxy.MethodAndURLMatcher](#proxymethodandurlmatcher)
- [urlrule.URLRule](#urlruleurlrule)
- [proxy.Compression](#proxycompression)
Expand All @@ -88,6 +89,8 @@
- [ratelimiter.Policy](#ratelimiterpolicy)
- [httpheader.ValueValidator](#httpheadervaluevalidator)
- [validator.JWTValidatorSpec](#validatorjwtvalidatorspec)
- [validator.BasicAuthValidatorSpec](#validatorbasicauthvalidatorspec)
- [basicAuth.LDAPSpec](#basicauthldapspec)
- [signer.Spec](#signerspec)
- [signer.HeaderHoisting](#signerheaderhoisting)
- [signer.Literal](#signerliteral)
Expand Down Expand Up @@ -1394,13 +1397,13 @@ Polices:
| Name | Type | Description | Required |
| ---- | ---- | ----------- | -------- |
| policy | string | Policy used to match requests, support `general`, `ipHash`, `headerHash`, `random` | No |
| headers | map[string][proxy.StringMatcher](#proxystringmatcher) | Request header filter options. The key of this map is header name, and the value of this map is header value match criteria | No |
| headers | map[string][StringMatcher](#stringmatcher) | Request header filter options. The key of this map is header name, and the value of this map is header value match criteria | No |
| urls | [][proxy.MethodAndURLMatcher](#proxyMethodAndURLMatcher) | Request URL match criteria | No |
| permil | uint32 | the probability of requests been matched. Value between 0 to 1000 | No |
| matchAllHeaders | bool | All rules in headers should be match | No |
| headerHashKey | string | Used by policy `headerHash`. | No |

### proxy.StringMatcher
### StringMatcher

The relationship between `exact`, `prefix`, and `regex` is `OR`.

Expand All @@ -1418,7 +1421,7 @@ The relationship between `methods` and `url` is `AND`.
| Name | Type | Description | Required |
| ------- | ------------------------------------------ | ---------------------------------------------------------------- | -------- |
| methods | []string | HTTP method criteria, Default is an empty list means all methods | No |
| url | [proxy.StringMatcher](#proxystringmatcher) | Criteria to match a URL | Yes |
| url | [StringMatcher](#stringmatcher) | Criteria to match a URL | Yes |

### urlrule.URLRule

Expand All @@ -1427,7 +1430,7 @@ The relationship between `methods` and `url` is `AND`.
| Name | Type | Description | Required |
| --------- | ------------------------------------------ | ---------------------------------------------------------------- | -------- |
| methods | []string | HTTP method criteria, Default is an empty list means all methods | No |
| url | [urlrule.StringMatch](#urlruleStringMatch) | Criteria to match a URL | Yes |
| url | [StringMatcher](#StringMatcher) | Criteria to match a URL | Yes |
| policyRef | string | Name of resilience policy for matched requests | No |


Expand Down Expand Up @@ -1472,7 +1475,7 @@ The relationship between `methods` and `url` is `AND`.
| path | string | Path match criteria, if request path is the value of this option, then the response of the request is mocked according to this rule | No |
| pathPrefix | string | Path prefix match criteria, if request path begins with the value of this option, then the response of the request is mocked according to this rule | No |
| matchAllHeaders | bool | Whether to match all headers | No |
| headers | map[string][url.StringMatch](#urlrulestringmatch) | Headers to match, key is a header name, value is the rule to match the header value | No |
| headers | map[string][StringMatcher](#stringmatcher) | Headers to match, key is a header name, value is the rule to match the header value | No |


### ratelimiter.Policy
Expand Down
12 changes: 6 additions & 6 deletions pkg/filters/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/megaease/easegress/pkg/filters"
"github.com/megaease/easegress/pkg/logger"
"github.com/megaease/easegress/pkg/protocols/httpprot"
"github.com/megaease/easegress/pkg/util/urlrule"
"github.com/megaease/easegress/pkg/util/stringtool"
)

const (
Expand Down Expand Up @@ -77,10 +77,10 @@ type (

// MatchRule is the rule to match a request
MatchRule struct {
Path string `json:"path,omitempty" jsonschema:"omitempty,pattern=^/"`
PathPrefix string `json:"pathPrefix,omitempty" jsonschema:"omitempty,pattern=^/"`
Headers map[string]*urlrule.StringMatch `json:"headers" jsonschema:"omitempty"`
MatchAllHeaders bool `json:"matchAllHeaders" jsonschema:"omitempty"`
Path string `json:"path,omitempty" jsonschema:"omitempty,pattern=^/"`
PathPrefix string `json:"pathPrefix,omitempty" jsonschema:"omitempty,pattern=^/"`
Headers map[string]*stringtool.StringMatcher `json:"headers" jsonschema:"omitempty"`
MatchAllHeaders bool `json:"matchAllHeaders" jsonschema:"omitempty"`
}
)

Expand Down Expand Up @@ -149,7 +149,7 @@ func (m *Mock) match(ctx *context.Context) *Rule {
return strings.HasPrefix(path, rule.Match.PathPrefix)
}

matchOneHeader := func(key string, rule *urlrule.StringMatch) bool {
matchOneHeader := func(key string, rule *stringtool.StringMatcher) bool {
values := header.Values(key)
if len(values) == 0 {
return rule.Empty
Expand Down
7 changes: 4 additions & 3 deletions pkg/filters/proxies/grpcproxy/requestmatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ import (
"github.com/megaease/easegress/pkg/filters/proxies"
"github.com/megaease/easegress/pkg/protocols"
"github.com/megaease/easegress/pkg/protocols/grpcprot"
"github.com/megaease/easegress/pkg/util/stringtool"
)

// RequestMatcherSpec describe RequestMatcher
type RequestMatcherSpec struct {
proxies.RequestMatcherBaseSpec `json:",inline"`
Methods []*proxies.StringMatcher `json:"methods" jsonschema:"omitempty"`
Methods []*stringtool.StringMatcher `json:"methods" jsonschema:"omitempty"`
}

// Validate validtes the RequestMatcherSpec.
Expand Down Expand Up @@ -63,8 +64,8 @@ func NewRequestMatcher(spec *RequestMatcherSpec) RequestMatcher {
// generalMatcher implements general grpc matcher.
type generalMatcher struct {
matchAllHeaders bool
headers map[string]*proxies.StringMatcher
methods []*proxies.StringMatcher
headers map[string]*stringtool.StringMatcher
methods []*stringtool.StringMatcher
}

func (gm *generalMatcher) init() {
Expand Down
27 changes: 14 additions & 13 deletions pkg/filters/proxies/grpcproxy/requestmatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/megaease/easegress/pkg/filters/proxies"
"github.com/megaease/easegress/pkg/protocols/grpcprot"
"github.com/megaease/easegress/pkg/util/stringtool"
"google.golang.org/grpc/metadata"

"github.com/stretchr/testify/assert"
Expand All @@ -38,21 +39,21 @@ func TestRequestMatcherSpecValidate(t *testing.T) {
spec.Permil = 100
assert.Error(spec.Validate())

spec.Headers = map[string]*proxies.StringMatcher{}
spec.Headers["X-Test"] = &proxies.StringMatcher{
spec.Headers = map[string]*stringtool.StringMatcher{}
spec.Headers["X-Test"] = &stringtool.StringMatcher{
Empty: true,
Exact: "abc",
}
assert.Error(spec.Validate())

spec.Headers["X-Test"] = &proxies.StringMatcher{Exact: "abc"}
spec.Methods = append(spec.Methods, &proxies.StringMatcher{
spec.Headers["X-Test"] = &stringtool.StringMatcher{Exact: "abc"}
spec.Methods = append(spec.Methods, &stringtool.StringMatcher{
Empty: true,
Exact: "abc",
})
assert.Error(spec.Validate())

spec.Methods[0] = &proxies.StringMatcher{Empty: true}
spec.Methods[0] = &stringtool.StringMatcher{Empty: true}
assert.Error(spec.Validate())

spec.HeaderHashKey = "X-Test"
Expand All @@ -66,7 +67,7 @@ func TestGeneralMatche(t *testing.T) {
rm := NewRequestMatcher(&RequestMatcherSpec{
RequestMatcherBaseSpec: proxies.RequestMatcherBaseSpec{
MatchAllHeaders: true,
Headers: map[string]*proxies.StringMatcher{
Headers: map[string]*stringtool.StringMatcher{
"X-Test1": {Exact: "test1"},
"X-Test2": {Exact: "test2"},
},
Expand All @@ -85,7 +86,7 @@ func TestGeneralMatche(t *testing.T) {
rm = NewRequestMatcher(&RequestMatcherSpec{
RequestMatcherBaseSpec: proxies.RequestMatcherBaseSpec{
MatchAllHeaders: true,
Headers: map[string]*proxies.StringMatcher{
Headers: map[string]*stringtool.StringMatcher{
"X-Test1": {Exact: "test1"},
"X-Test2": {Empty: true, Exact: "test2"},
},
Expand All @@ -98,7 +99,7 @@ func TestGeneralMatche(t *testing.T) {
// match one header
rm = NewRequestMatcher(&RequestMatcherSpec{
RequestMatcherBaseSpec: proxies.RequestMatcherBaseSpec{
Headers: map[string]*proxies.StringMatcher{
Headers: map[string]*stringtool.StringMatcher{
"X-Test1": {Exact: "test1"},
"X-Test2": {Empty: true, Exact: "test2"},
},
Expand All @@ -111,7 +112,7 @@ func TestGeneralMatche(t *testing.T) {

rm = NewRequestMatcher(&RequestMatcherSpec{
RequestMatcherBaseSpec: proxies.RequestMatcherBaseSpec{
Headers: map[string]*proxies.StringMatcher{
Headers: map[string]*stringtool.StringMatcher{
"X-Test1": {Exact: "test1"},
"X-Test2": {Exact: "test2"},
},
Expand All @@ -124,25 +125,25 @@ func TestGeneralMatche(t *testing.T) {
req.SetFullMethod("/abc")
rm = NewRequestMatcher(&RequestMatcherSpec{
RequestMatcherBaseSpec: proxies.RequestMatcherBaseSpec{
Headers: map[string]*proxies.StringMatcher{
Headers: map[string]*stringtool.StringMatcher{
"X-Test1": {Exact: "test1"},
"X-Test2": {Exact: "test2"},
},
},
Methods: []*proxies.StringMatcher{
Methods: []*stringtool.StringMatcher{
{Exact: "/abc"},
},
})
assert.True(rm.Match(req))

rm = NewRequestMatcher(&RequestMatcherSpec{
RequestMatcherBaseSpec: proxies.RequestMatcherBaseSpec{
Headers: map[string]*proxies.StringMatcher{
Headers: map[string]*stringtool.StringMatcher{
"X-Test1": {Exact: "test1"},
"X-Test2": {Exact: "test2"},
},
},
Methods: []*proxies.StringMatcher{
Methods: []*stringtool.StringMatcher{
{Exact: "/abcd"},
},
})
Expand Down
6 changes: 3 additions & 3 deletions pkg/filters/proxies/httpproxy/requestmatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func NewRequestMatcher(spec *RequestMatcherSpec) proxies.RequestMatcher {
// generalMatcher implements general HTTP matcher.
type generalMatcher struct {
matchAllHeaders bool
headers map[string]*proxies.StringMatcher
headers map[string]*stringtool.StringMatcher
urls []*MethodAndURLMatcher
}

Expand Down Expand Up @@ -150,8 +150,8 @@ func (gm *generalMatcher) matchURL(req *httpprot.Request) bool {

// MethodAndURLMatcher defines the match rule of a http request
type MethodAndURLMatcher struct {
Methods []string `json:"methods" jsonschema:"omitempty,uniqueItems=true,format=httpmethod-array"`
URL *proxies.StringMatcher `json:"url" jsonschema:"required"`
Methods []string `json:"methods" jsonschema:"omitempty,uniqueItems=true,format=httpmethod-array"`
URL *stringtool.StringMatcher `json:"url" jsonschema:"required"`
}

// Validate validates the MethodAndURLMatcher.
Expand Down
29 changes: 15 additions & 14 deletions pkg/filters/proxies/httpproxy/requestmatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/megaease/easegress/pkg/filters/proxies"
"github.com/megaease/easegress/pkg/protocols/httpprot"
"github.com/megaease/easegress/pkg/util/stringtool"
"github.com/stretchr/testify/assert"
)

Expand All @@ -36,24 +37,24 @@ func TestRequestMatcherSpecValidate(t *testing.T) {
spec.Permil = 100
assert.Error(spec.Validate())

spec.Headers = map[string]*proxies.StringMatcher{}
spec.Headers["X-Test"] = &proxies.StringMatcher{
spec.Headers = map[string]*stringtool.StringMatcher{}
spec.Headers["X-Test"] = &stringtool.StringMatcher{
Empty: true,
Exact: "abc",
}
assert.Error(spec.Validate())

spec.Headers["X-Test"] = &proxies.StringMatcher{Exact: "abc"}
spec.Headers["X-Test"] = &stringtool.StringMatcher{Exact: "abc"}
spec.URLs = append(spec.URLs, &MethodAndURLMatcher{
URL: &proxies.StringMatcher{
URL: &stringtool.StringMatcher{
Empty: true,
Exact: "abc",
},
})
assert.Error(spec.Validate())

spec.URLs[0] = &MethodAndURLMatcher{
URL: &proxies.StringMatcher{Empty: true},
URL: &stringtool.StringMatcher{Empty: true},
}
assert.Error(spec.Validate())

Expand All @@ -68,7 +69,7 @@ func TestGeneralMatche(t *testing.T) {
rm := NewRequestMatcher(&RequestMatcherSpec{
RequestMatcherBaseSpec: proxies.RequestMatcherBaseSpec{
MatchAllHeaders: true,
Headers: map[string]*proxies.StringMatcher{
Headers: map[string]*stringtool.StringMatcher{
"X-Test1": {Exact: "test1"},
"X-Test2": {Exact: "test2"},
},
Expand All @@ -87,7 +88,7 @@ func TestGeneralMatche(t *testing.T) {
rm = NewRequestMatcher(&RequestMatcherSpec{
RequestMatcherBaseSpec: proxies.RequestMatcherBaseSpec{
MatchAllHeaders: true,
Headers: map[string]*proxies.StringMatcher{
Headers: map[string]*stringtool.StringMatcher{
"X-Test1": {Exact: "test1"},
"X-Test2": {Empty: true, Exact: "test2"},
},
Expand All @@ -100,7 +101,7 @@ func TestGeneralMatche(t *testing.T) {
// match one header
rm = NewRequestMatcher(&RequestMatcherSpec{
RequestMatcherBaseSpec: proxies.RequestMatcherBaseSpec{
Headers: map[string]*proxies.StringMatcher{
Headers: map[string]*stringtool.StringMatcher{
"X-Test1": {Exact: "test1"},
"X-Test2": {Empty: true, Exact: "test2"},
},
Expand All @@ -113,7 +114,7 @@ func TestGeneralMatche(t *testing.T) {

rm = NewRequestMatcher(&RequestMatcherSpec{
RequestMatcherBaseSpec: proxies.RequestMatcherBaseSpec{
Headers: map[string]*proxies.StringMatcher{
Headers: map[string]*stringtool.StringMatcher{
"X-Test1": {Exact: "test1"},
"X-Test2": {Exact: "test2"},
},
Expand All @@ -126,15 +127,15 @@ func TestGeneralMatche(t *testing.T) {

rm = NewRequestMatcher(&RequestMatcherSpec{
RequestMatcherBaseSpec: proxies.RequestMatcherBaseSpec{
Headers: map[string]*proxies.StringMatcher{
Headers: map[string]*stringtool.StringMatcher{
"X-Test1": {Exact: "test1"},
"X-Test2": {Exact: "test2"},
},
},
URLs: []*MethodAndURLMatcher{
{
Methods: []string{http.MethodGet},
URL: &proxies.StringMatcher{
URL: &stringtool.StringMatcher{
Exact: "/abc",
},
},
Expand All @@ -144,15 +145,15 @@ func TestGeneralMatche(t *testing.T) {

rm = NewRequestMatcher(&RequestMatcherSpec{
RequestMatcherBaseSpec: proxies.RequestMatcherBaseSpec{
Headers: map[string]*proxies.StringMatcher{
Headers: map[string]*stringtool.StringMatcher{
"X-Test1": {Exact: "test1"},
"X-Test2": {Exact: "test2"},
},
},
URLs: []*MethodAndURLMatcher{
{
Methods: []string{http.MethodGet},
URL: &proxies.StringMatcher{
URL: &stringtool.StringMatcher{
Exact: "/abcd",
},
},
Expand All @@ -165,7 +166,7 @@ func TestMethodAndURLMatcher(t *testing.T) {
assert := assert.New(t)

m := &MethodAndURLMatcher{
URL: &proxies.StringMatcher{
URL: &stringtool.StringMatcher{
Exact: "/abc",
},
}
Expand Down
Loading