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

api: add HTTP audit middleware #4537

Merged
merged 96 commits into from
Feb 11, 2022
Merged

Conversation

CabinfeverB
Copy link
Member

@CabinfeverB CabinfeverB commented Jan 4, 2022

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

  • Unit test
  • Integration test

Code changes

Release note

Add HTTP Audit middleware

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>
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>
@ti-chi-bot
Copy link
Member

ti-chi-bot commented Jan 4, 2022

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • nolouch
  • rleungx

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. labels Jan 4, 2022
@ti-chi-bot ti-chi-bot requested review from disksing and nolouch January 4, 2022 07:54
@codecov
Copy link

codecov bot commented Jan 4, 2022

Codecov Report

Merging #4537 (436892f) into master (ea69785) will increase coverage by 0.08%.
The diff coverage is 78.68%.

Impacted file tree graph

@@            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     
Flag Coverage Δ
unittests 74.76% <78.68%> (+0.08%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
server/config/config.go 77.84% <ø> (ø)
server/api/middleware.go 74.50% <62.96%> (-13.50%) ⬇️
pkg/audit/audit.go 75.00% <75.00%> (ø)
server/server.go 74.31% <81.81%> (+0.05%) ⬆️
pkg/requestutil/context.go 100.00% <100.00%> (ø)
pkg/requestutil/request_info.go 78.94% <100.00%> (ø)
server/api/admin.go 51.56% <100.00%> (ø)
server/api/router.go 99.56% <100.00%> (+2.23%) ⬆️
pkg/errs/errs.go 75.00% <0.00%> (-25.00%) ⬇️
pkg/tempurl/tempurl.go 45.00% <0.00%> (-15.00%) ⬇️
... and 15 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ea69785...436892f. Read the comment docs.

CabinfeverB and others added 9 commits January 4, 2022 16:24
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>
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
Copy link
Member

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?

Copy link
Member Author

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

Comment on lines +122 to +135
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)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)
}
}

Copy link
Member Author

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


// ExecutionInfo holds request execution info
type ExecutionInfo struct {
EndTimeStamp int64
Copy link
Member

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?

Copy link
Member Author

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)
Copy link
Member

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?

Copy link
Member Author

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?

Copy link
Member Author

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>
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
c.Assert(result.StartTimeStamp, Equals, timeNow)
}

func (s *testRequestContextSuite) TestExcutionInfo(c *C) {
Copy link
Member

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.

Copy link
Member Author

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 {
Copy link
Member

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.

Copy link
Member Author

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>
Copy link
Member

@rleungx rleungx left a 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>
Copy link
Contributor

@nolouch nolouch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Feb 8, 2022
@ti-chi-bot ti-chi-bot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Feb 10, 2022
@nolouch
Copy link
Contributor

nolouch commented Feb 11, 2022

/merge

@ti-chi-bot
Copy link
Member

@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.

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: 1b170fe

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Feb 11, 2022
@ti-chi-bot
Copy link
Member

@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.

@ti-chi-bot ti-chi-bot merged commit 62c38e9 into tikv:master Feb 11, 2022
ti-chi-bot added a commit that referenced this pull request Feb 22, 2022
ref #4537, close #4602

Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>

Co-authored-by: ShuNing <nolouch@gmail.com>
Co-authored-by: Ti Chi Robot <ti-community-prow-bot@tidb.io>
ti-chi-bot added a commit that referenced this pull request Feb 25, 2022
ref #4537, close #4608

Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>

Co-authored-by: ShuNing <nolouch@gmail.com>
Co-authored-by: Ti Chi Robot <ti-community-prow-bot@tidb.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note Denotes a PR that will be considered when it comes time to generate release notes. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement Audit Middleware
5 participants