-
Notifications
You must be signed in to change notification settings - Fork 259
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
[Feature] Shared cache grants check option #466
Open
hulk8
wants to merge
8
commits into
ContentSquare:master
Choose a base branch
from
hulk8:shared_cache_check_option
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9d00aa8
added mvp ping endpoint proxy
hulk8 ef5d020
added allow_ping config option
hulk8 1f0d569
minor refactoring path routing
hulk8 4e65443
added ping tests
hulk8 ea41bf9
fixed lint error by making /ping endpoint constant
hulk8 9b46898
added test for ping is not allowed configuration
hulk8 ebb7e32
Merge branch 'shared_cache_check_option'
hulk8 60b7596
added check grants for shared cache queries
hulk8 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 |
---|---|---|
|
@@ -85,7 +85,7 @@ | |
} | ||
} | ||
|
||
func (rp *reverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) { | ||
startTime := time.Now() | ||
s, status, err := rp.getScope(req) | ||
if err != nil { | ||
|
@@ -146,6 +146,25 @@ | |
} | ||
|
||
if shouldReturnFromCache { | ||
// if cache shared between all users | ||
// try to check if cached query is allowed for current user | ||
if s.user.cache != nil && s.user.cache.SharedWithAllUsers && s.user.cache.CheckGrantsForSharedCache { | ||
checkReq, checkQuery, _ := s.createCheckGrantsRequest(req) | ||
|
||
srwCheck := &checkGrantsResponseWriter{ | ||
ResponseWriter: srw.ResponseWriter, | ||
} | ||
|
||
rp.proxyRequest(s, srwCheck, srw, checkReq) | ||
|
||
if srwCheck.statusCode == http.StatusOK { | ||
log.Debugf("%s: check grants for shared cached query request success; query: %q; Method: %s; URL: %q", s, checkQuery, checkReq.Method, checkReq.URL.String()) | ||
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. Maybe adding the user would be nice in the debug logs, wdyt ? |
||
} else { | ||
log.Debugf("%s: check grants for shared cached query request failure: non-200 status code %d; query: %q; Method: %s; URL: %q", s, srwCheck.statusCode, checkQuery, checkReq.Method, checkReq.URL.String()) | ||
return | ||
} | ||
} | ||
|
||
rp.serveFromCache(s, srw, req, origParams, q) | ||
} else { | ||
rp.proxyRequest(s, srw, srw, req) | ||
|
@@ -925,3 +944,40 @@ | |
s.requestPacketSize = len(q) | ||
return s, 0, nil | ||
} | ||
|
||
// create a new request based on proxied one | ||
// with query wrapped to fetch result types like: | ||
// 'DESC ({original_query})' | ||
// along with query parsed and analyzed for return types (which is fast) | ||
// ClickHouse check permissions to execute this query for the user | ||
func (s *scope) createCheckGrantsRequest(originalReq *http.Request) (*http.Request, string, error) { | ||
originalQuery := originalReq.URL.Query().Get("query") | ||
checkQuery := fmt.Sprintf("DESC (%s);", strings.TrimRight(originalQuery, ";")) | ||
|
||
newURL := *originalReq.URL | ||
|
||
queryParams, err := url.ParseQuery(newURL.RawQuery) | ||
if err != nil { | ||
return nil, checkQuery, err | ||
} | ||
|
||
queryParams.Set("query", checkQuery) | ||
|
||
newURL.RawQuery = queryParams.Encode() | ||
|
||
req := &http.Request{ | ||
Method: originalReq.Method, | ||
URL: &newURL, | ||
Proto: originalReq.Proto, | ||
ProtoMajor: originalReq.ProtoMajor, | ||
ProtoMinor: originalReq.ProtoMinor, | ||
Header: originalReq.Header.Clone(), | ||
Body: originalReq.Body, | ||
Host: originalReq.Host, | ||
ContentLength: originalReq.ContentLength, | ||
Close: originalReq.Close, | ||
TLS: originalReq.TLS, | ||
} | ||
|
||
return req, checkQuery, nil | ||
} |
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.
I am not sure to properly understand why we need to check
s.user.cache
here ? what is the goal of this.Isn't Checking if a cache is shared && that we should check grants enough ?