-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
jwt_authn: Support extraction of JWT from Cookies in JWT Extension #17721
jwt_authn: Support extraction of JWT from Cookies in JWT Extension #17721
Conversation
Added "from_cookies" config directive to jwt_authn that enables JWT extraction from request cookies. Testing: unit tests Signed-off-by: Shubham Patil <theshubhamp@gmail.com>
Hi @theshubhamp, welcome and thank you for your contribution. We will try to review your Pull Request as quickly as possible. In the meantime, please take a look at the contribution guidelines if you have not done so already. |
CC @envoyproxy/api-shepherds: Your approval is needed for changes made to |
Running
LMK if these should be included. |
source/common/http/utility.cc
Outdated
@@ -296,6 +296,39 @@ std::string parseCookie(const HeaderMap& headers, const std::string& key, | |||
return EMPTY_STRING; | |||
} | |||
|
|||
std::map<std::string, std::string> Utility::parseCookies(const RequestHeaderMap& headers) { |
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.
This is very similar to the existing code in source/common/http/utility.cc #L257-L282
Could not use it as-is because that'd scan over cookies multiple times. Any suggestions on merging them together ?
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.
Perhaps you could introduce something like void forEachCookie(const Http::HeaderMap::GetResult cookie_headers, std::function<bool(...)> fn)
where fn
is a closure dealing with a cookie and returning false
to stop iteration.
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.
Thanks for the suggestion! Was able to get this working locally 👍
I would prefer to do this change in a follow-up PR. I hope that's okay.
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.
Yes, a follow-up would be Ok. Just put a TODO note to the function.
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 other places using parseCookieValue
would be benefit from this logic if you see how oauth2 filter use it.
I would suggest you to land the refactoring before this PR.
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.
Cool, I'll do that first
Signed-off-by: Shubham Patil <theshubhamp@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.
Thank you! Added a couple of suggestions.
source/common/http/utility.cc
Outdated
@@ -296,6 +296,39 @@ std::string parseCookie(const HeaderMap& headers, const std::string& key, | |||
return EMPTY_STRING; | |||
} | |||
|
|||
std::map<std::string, std::string> Utility::parseCookies(const RequestHeaderMap& headers) { |
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.
Perhaps you could introduce something like void forEachCookie(const Http::HeaderMap::GetResult cookie_headers, std::function<bool(...)> fn)
where fn
is a closure dealing with a cookie and returning false
to stop iteration.
Signed-off-by: Shubham Patil <theshubhamp@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 on jwt_authn extractor code . Thanks.
/retest |
Retrying Azure Pipelines: |
Signed-off-by: Shubham Patil <theshubhamp@gmail.com>
Pushed a commit ^ that adds TODO for cookie iterator improvements suggested in the review. |
Signed-off-by: Shubham Patil <theshubhamp@gmail.com>
/retest |
Retrying Azure Pipelines: |
/retest |
Retrying Azure Pipelines: |
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.
Thanks! Looks good to me
cc: @lizan, this PR requires a mandatory review from you. PTAL whenever you can! |
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.
/wait
|
||
// JWT is sent in a cookie. `from_cookies` represents the cookie names to extract from. | ||
// | ||
// For example, if config is: | ||
// | ||
// .. code-block:: yaml | ||
// | ||
// from_cookies: | ||
// - auth-token | ||
// | ||
// Then JWT will be extracted from `auth-token` cookie in the request. | ||
// | ||
repeated string from_cookies = 13; |
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.
Move this block after from_params
.
This doesn't have to be ordered by field tag.
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.
Done, thanks for pointing this out
source/common/http/utility.cc
Outdated
@@ -296,6 +296,39 @@ std::string parseCookie(const HeaderMap& headers, const std::string& key, | |||
return EMPTY_STRING; | |||
} | |||
|
|||
std::map<std::string, std::string> Utility::parseCookies(const RequestHeaderMap& headers) { |
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 other places using parseCookieValue
would be benefit from this logic if you see how oauth2 filter use it.
I would suggest you to land the refactoring before this PR.
: JwtLocationBase(token, issuer_checker) {} | ||
|
||
void removeJwt(Http::HeaderMap&) const override { | ||
// TODO(theshubhamp): remove JWT from cookies. |
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.
Use NOT_IMPLEMENTED_GCOVR_EXCL_LINE
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.
Added below the comment
Reordered `from_cookies` to be after `from_params` in proto. Added NOT_IMPLEMENTED_GCOVR_EXCL_LINE in JwtCookieLocation::removeJwt(..) Signed-off-by: Shubham Patil <theshubhamp@gmail.com>
/retest |
Retrying Azure Pipelines: |
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 api
can you resolve conflicts? |
…-cookie Signed-off-by: Shubham Patil <theshubhamp@gmail.com>
Signed-off-by: Shubham Patil <theshubhamp@gmail.com>
Signed-off-by: Shubham Patil <theshubhamp@gmail.com>
Merged in master and resolved conflicts. PTAL! |
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.
Thank you! Looks good to me.
Support extraction of JWT from Cookies in JWT Extension
Added "from_cookies" config directive to jwt_authn that enables JWT extraction from request cookies.
Risk Level: low
Testing: unit tests
Docs Changes: Updated
docs/root/configuration/http/http_filters/jwt_authn_filter.rst
Release Notes: Updated
docs/root/version_history/current.rst
Platform Specific Features: None
Fixes #17424
Signed-off-by: Shubham Patil theshubhamp@gmail.com