-
Notifications
You must be signed in to change notification settings - Fork 726
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
api: add HTTP audit middleware #4537
Conversation
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
[REVIEW NOTIFICATION] This pull request has been approved by:
To complete the pull request process, please ask the reviewers in the list to review by filling The full list of commands accepted by this bot can be found here. Reviewer can indicate their review by submitting an approval review. |
Codecov Report
@@ Coverage Diff @@
## master #4537 +/- ##
==========================================
+ Coverage 74.68% 74.76% +0.08%
==========================================
Files 281 282 +1
Lines 27714 27760 +46
==========================================
+ Hits 20697 20756 +59
+ Misses 5154 5147 -7
+ Partials 1863 1857 -6
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
@@ -38,10 +38,21 @@ func (m *LabelMatcher) Match(labels *BackendLabels) bool { | |||
return false | |||
} | |||
|
|||
// Sequence is used to help backend implement audit.Backend |
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.
Where does the Sequence
init?
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.
creates and inits Sequence
when creating Backend
beforeNextBackends := make([]audit.Backend, 0) | ||
afterNextBackends := make([]audit.Backend, 0) | ||
for _, backend := range s.svr.GetAuditBackend() { | ||
if backend.Match(labels) { | ||
if backend.ProcessBeforeHandler() { | ||
beforeNextBackends = append(beforeNextBackends, backend) | ||
} else { | ||
afterNextBackends = append(afterNextBackends, backend) | ||
} | ||
} | ||
} | ||
for _, backend := range beforeNextBackends { | ||
backend.ProcessHTTPRequest(r) | ||
} |
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.
beforeNextBackends := make([]audit.Backend, 0) | |
afterNextBackends := make([]audit.Backend, 0) | |
for _, backend := range s.svr.GetAuditBackend() { | |
if backend.Match(labels) { | |
if backend.ProcessBeforeHandler() { | |
beforeNextBackends = append(beforeNextBackends, backend) | |
} else { | |
afterNextBackends = append(afterNextBackends, backend) | |
} | |
} | |
} | |
for _, backend := range beforeNextBackends { | |
backend.ProcessHTTPRequest(r) | |
} | |
for _, backend := range s.svr.GetAuditBackend() { | |
if backend.Match(labels) && backend.ProcessBeforeHandler() { | |
backend.ProcessHTTPRequest(r) | |
} | |
} |
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.
The backend can be divided into two types: before the service Handle and after the service Handle
pkg/requestutil/request_info.go
Outdated
|
||
// ExecutionInfo holds request execution info | ||
type ExecutionInfo struct { | ||
EndTimeStamp int64 |
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.
How about making it simple as we only have one field here?
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.
has replaced struct to int64
|
||
requestInfo, ok := requestutil.RequestInfoFrom(r.Context()) | ||
if !ok { | ||
next(w, r) |
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.
Do we need to return?
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.
I'm not sure. If return here, what response do we write?
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.
Add error log if can't get requestInfo
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
pkg/requestutil/context_test.go
Outdated
c.Assert(result.StartTimeStamp, Equals, timeNow) | ||
} | ||
|
||
func (s *testRequestContextSuite) TestExcutionInfo(c *C) { |
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.
The name of the case should also be changed.
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.
fix
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
@@ -124,6 +125,11 @@ func (s *serviceMiddlewareBuilder) middlewareFunc(next func(http.ResponseWriter, | |||
// @BasePath /pd/api/v1 | |||
func createRouter(prefix string, svr *server.Server) *mux.Router { | |||
rd := createIndentRender() | |||
setAudit := func(labels ...string) createRouteOption { |
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.
RegistServiceForHTTP
is not only for audit I think.
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.
I will change RegistServiceForHTTP
name to "SetServiceAuditBackendForHTTP". Other actions such as rate limit may be processed by other createRouteOption
functions.
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
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.
Mostly LGTM
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
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.
lgtm
/merge |
@nolouch: It seems you want to merge this PR, I will help you trigger all the tests: /run-all-tests Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository. |
This pull request has been accepted and is ready to merge. Commit hash: 1b170fe
|
@CabinfeverB: Your PR was out of date, I have automatically updated it for you. At the same time I will also trigger all tests for you: /run-all-tests If the CI test fails, you just re-trigger the test that failed and the bot will merge the PR for you after the CI passes. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository. |
Signed-off-by: Cabinfever_B cabinfeveroier@gmail.com
What problem does this PR solve?
close #4601
This PR is used to support the audit
This PR should be merged after #4526
What is changed and how it works?
Add audit middleware for HTTP API.
Define audit backend interface.
Check List
Tests
Code changes
Release note