This repository has been archived by the owner on Apr 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Implement logic for TriggerRules config #3
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ import ( | |
|
||
envoy "github.com/envoyproxy/go-control-plane/envoy/service/auth/v3" | ||
"github.com/stretchr/testify/require" | ||
"github.com/tetratelabs/telemetry" | ||
"google.golang.org/grpc/codes" | ||
|
||
configv1 "github.com/tetrateio/authservice-go/config/gen/go/v1" | ||
|
@@ -161,6 +162,162 @@ func TestGrpcNoChainsMatched(t *testing.T) { | |
require.Equal(t, int32(codes.PermissionDenied), ok.Status.Code) | ||
} | ||
|
||
func TestStringMatch(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
match *configv1.StringMatch | ||
path string | ||
want bool | ||
}{ | ||
{"no-match", nil, "", false}, | ||
{"equality-match", stringExact("test"), "test", true}, | ||
{"equality-no-match", stringExact("test"), "no-match", false}, | ||
{"prefix-match", stringPrefix("test"), "test-123", true}, | ||
{"prefix-no-match", stringPrefix("test"), "no-match", false}, | ||
{"suffix-match", stringSuffix("test"), "123-test", true}, | ||
{"suffix-no-match", stringSuffix("test"), "no-match", false}, | ||
{"regex-match", stringRegex(".*st"), "test", true}, | ||
{"regex-no-match", stringRegex(".*st"), "no-match", false}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a test for the regexp error as well. |
||
{"regex-invalid", stringRegex("["), "no-match", false}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
require.Equal(t, tt.want, stringMatch(telemetry.NoopLogger(), tt.match, tt.path)) | ||
}) | ||
} | ||
} | ||
|
||
func TestMatchTriggerRule(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
rule *configv1.TriggerRule | ||
path string | ||
want bool | ||
}{ | ||
{"no-rule", nil, "/path", false}, | ||
{"no-path", &configv1.TriggerRule{}, "", true}, | ||
{"empty-rule", &configv1.TriggerRule{}, "/path", true}, | ||
{"excluded-path-match", &configv1.TriggerRule{ExcludedPaths: []*configv1.StringMatch{stringExact("/path")}}, "/path", false}, | ||
{"excluded-path-no-match", &configv1.TriggerRule{ExcludedPaths: []*configv1.StringMatch{stringExact("/path")}}, "/no-match", true}, | ||
{"included-path-match", &configv1.TriggerRule{IncludedPaths: []*configv1.StringMatch{stringExact("/path")}}, "/path", true}, | ||
{"included-path-no-match", &configv1.TriggerRule{IncludedPaths: []*configv1.StringMatch{stringExact("/path")}}, "/no-match", false}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
require.Equal(t, tt.want, matchTriggerRule(telemetry.NoopLogger(), tt.rule, tt.path)) | ||
}) | ||
} | ||
|
||
} | ||
|
||
func TestMustTriggerCheck(t *testing.T) { | ||
test := []struct { | ||
name string | ||
rules []*configv1.TriggerRule | ||
path string | ||
want bool | ||
}{ | ||
{"no-rules", nil, "/path", true}, | ||
{"no-path", nil, "", true}, | ||
{"empty-rules", []*configv1.TriggerRule{}, "/path", true}, | ||
{"inclusions-match", []*configv1.TriggerRule{{IncludedPaths: []*configv1.StringMatch{stringExact("/path")}}}, "/path", true}, | ||
{"inclusions-no-match", []*configv1.TriggerRule{{IncludedPaths: []*configv1.StringMatch{stringExact("/path")}}}, "/no-match", false}, | ||
{"exclusions-match", []*configv1.TriggerRule{{ExcludedPaths: []*configv1.StringMatch{stringExact("/path")}}}, "/path", false}, | ||
{"exclusions-no-match", []*configv1.TriggerRule{{ExcludedPaths: []*configv1.StringMatch{stringExact("/path")}}}, "/no-match", true}, | ||
{"multiple-rules-no-match", []*configv1.TriggerRule{ | ||
{ExcludedPaths: []*configv1.StringMatch{stringExact("/ex-path")}}, | ||
{IncludedPaths: []*configv1.StringMatch{stringExact("/path")}}, | ||
}, "/ex-path", false}, | ||
{"multiple-rules-match", []*configv1.TriggerRule{ | ||
{ExcludedPaths: []*configv1.StringMatch{stringExact("/no-path")}}, | ||
{IncludedPaths: []*configv1.StringMatch{stringExact("/path")}}, | ||
}, "/path", true}, | ||
{"inverse-order-multiple-rules-no-match", []*configv1.TriggerRule{ | ||
{IncludedPaths: []*configv1.StringMatch{stringExact("/path")}}, | ||
{ExcludedPaths: []*configv1.StringMatch{stringExact("/ex-path")}}, | ||
}, "/ex-path", false}, | ||
{"inverse-order-multiple-rules-match", []*configv1.TriggerRule{ | ||
{IncludedPaths: []*configv1.StringMatch{stringExact("/path")}}, | ||
{ExcludedPaths: []*configv1.StringMatch{stringExact("/no-path")}}, | ||
}, "/path", true}, | ||
} | ||
|
||
for _, tt := range test { | ||
t.Run(tt.name, func(t *testing.T) { | ||
req := &envoy.CheckRequest{ | ||
Attributes: &envoy.AttributeContext{ | ||
Request: &envoy.AttributeContext_Request{ | ||
Http: &envoy.AttributeContext_HttpRequest{ | ||
Path: tt.path, | ||
}, | ||
}, | ||
}, | ||
} | ||
require.Equal(t, tt.want, mustTriggerCheck(telemetry.NoopLogger(), tt.rules, req)) | ||
}) | ||
} | ||
} | ||
|
||
func TestCheckTriggerRules(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
config *configv1.Config | ||
path string | ||
want codes.Code | ||
}{ | ||
{ | ||
"no-rules-triggers-check", | ||
&configv1.Config{ | ||
Chains: []*configv1.FilterChain{{Filters: []*configv1.Filter{mock(false)}}}, | ||
}, | ||
"/path", codes.PermissionDenied, | ||
}, | ||
{ | ||
"rules-inclusions-matching-triggers-check", | ||
&configv1.Config{ | ||
Chains: []*configv1.FilterChain{{Filters: []*configv1.Filter{mock(false)}}}, | ||
TriggerRules: []*configv1.TriggerRule{ | ||
{ | ||
IncludedPaths: []*configv1.StringMatch{stringExact("/path")}, | ||
}, | ||
}, | ||
}, | ||
"/path", codes.PermissionDenied}, | ||
{ | ||
"rules-inclusions-no-match-does-not-trigger-check", | ||
&configv1.Config{ | ||
Chains: []*configv1.FilterChain{{Filters: []*configv1.Filter{mock(false)}}}, | ||
TriggerRules: []*configv1.TriggerRule{ | ||
{ | ||
IncludedPaths: []*configv1.StringMatch{stringExact("/path")}, | ||
}, | ||
}, | ||
}, | ||
"/no-path", codes.OK, // it does not reach mock(allow=false), so it returns OK | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
e := NewExtAuthZFilter(tt.config) | ||
req := &envoy.CheckRequest{ | ||
Attributes: &envoy.AttributeContext{ | ||
Request: &envoy.AttributeContext_Request{ | ||
Http: &envoy.AttributeContext_HttpRequest{ | ||
Path: tt.path, | ||
}, | ||
}, | ||
}, | ||
} | ||
got, err := e.Check(context.Background(), req) | ||
require.NoError(t, err) | ||
require.Equal(t, int32(tt.want), got.Status.Code) | ||
}) | ||
} | ||
} | ||
|
||
func mock(allow bool) *configv1.Filter { | ||
return &configv1.Filter{ | ||
Type: &configv1.Filter_Mock{ | ||
|
@@ -202,3 +359,35 @@ func header(value string) *envoy.CheckRequest { | |
}, | ||
} | ||
} | ||
|
||
func stringExact(s string) *configv1.StringMatch { | ||
return &configv1.StringMatch{ | ||
MatchType: &configv1.StringMatch_Exact{ | ||
Exact: s, | ||
}, | ||
} | ||
} | ||
|
||
func stringPrefix(s string) *configv1.StringMatch { | ||
return &configv1.StringMatch{ | ||
MatchType: &configv1.StringMatch_Prefix{ | ||
Prefix: s, | ||
}, | ||
} | ||
} | ||
|
||
func stringSuffix(s string) *configv1.StringMatch { | ||
return &configv1.StringMatch{ | ||
MatchType: &configv1.StringMatch_Suffix{ | ||
Suffix: s, | ||
}, | ||
} | ||
} | ||
|
||
func stringRegex(s string) *configv1.StringMatch { | ||
return &configv1.StringMatch{ | ||
MatchType: &configv1.StringMatch_Regex{ | ||
Regex: s, | ||
}, | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In line 90 we're already getting the log from the context. let's just create the
log
variable here and reuse it later?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
totally, didn't notice that.