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

jwt_authn: Support extraction of JWT from Cookies in JWT Extension #17721

Merged
merged 9 commits into from
Sep 3, 2021
Merged

jwt_authn: Support extraction of JWT from Cookies in JWT Extension #17721

merged 9 commits into from
Sep 3, 2021

Conversation

theshubhamp
Copy link
Contributor

@theshubhamp theshubhamp commented Aug 15, 2021

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

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>
@theshubhamp theshubhamp requested a review from lizan as a code owner August 15, 2021 18:47
@repokitteh-read-only
Copy link

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.

🐱

Caused by: #17721 was opened by theshubhamp.

see: more, trace.

@repokitteh-read-only
Copy link

CC @envoyproxy/api-shepherds: Your approval is needed for changes made to api/envoy/.
envoyproxy/api-shepherds assignee is @adisuissa
CC @envoyproxy/api-watchers: FYI only for changes made to api/envoy/.

🐱

Caused by: #17721 was opened by theshubhamp.

see: more, trace.

@theshubhamp
Copy link
Contributor Author

theshubhamp commented Aug 15, 2021

Running tools/code_format/check_format.py fix made a few formatting changes related to my change. I have left them out of this PR.

	modified:   api/envoy/config/filter/http/jwt_authn/v2alpha/config.proto
	modified:   api/envoy/extensions/filters/http/jwt_authn/v3/config.proto
	modified:   api/envoy/extensions/filters/http/jwt_authn/v4alpha/config.proto
	modified:   generated_api_shadow/envoy/config/filter/http/jwt_authn/v2alpha/config.proto
	modified:   generated_api_shadow/envoy/extensions/filters/http/jwt_authn/v3/config.proto
	modified:   generated_api_shadow/envoy/extensions/filters/http/jwt_authn/v4alpha/config.proto
	modified:   test/common/http/header_map_impl_test.cc

Example:
Screenshot 2021-08-16 at 12 11 09 AM

LMK if these should be included.

@@ -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) {
Copy link
Contributor Author

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 ?

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Member

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.

https://github.com/envoyproxy/envoy/blob/main/source/extensions/filters/http/oauth2/filter.cc#L141-L143

I would suggest you to land the refactoring before this PR.

Copy link
Contributor Author

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

@rojkov rojkov left a 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.

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

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.

test/extensions/filters/http/jwt_authn/extractor_test.cc Outdated Show resolved Hide resolved
Signed-off-by: Shubham Patil <theshubhamp@gmail.com>
qiwzhang
qiwzhang previously approved these changes Aug 16, 2021
Copy link
Contributor

@qiwzhang qiwzhang left a 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.

@theshubhamp
Copy link
Contributor Author

Thanks @rojkov for your review! Commented on your suggestions, PTAL.

Thanks @qiwzhang for your approval! Unfortunately it got reset because of a small test data change I just pushed. PTAL again.

qiwzhang
qiwzhang previously approved these changes Aug 16, 2021
@rojkov
Copy link
Member

rojkov commented Aug 17, 2021

/retest

@repokitteh-read-only
Copy link

Retrying Azure Pipelines:
Retried failed jobs in: envoy-presubmit

🐱

Caused by: a #17721 (comment) was created by @rojkov.

see: more, trace.

Signed-off-by: Shubham Patil <theshubhamp@gmail.com>
@theshubhamp
Copy link
Contributor Author

Pushed a commit ^ that adds TODO for cookie iterator improvements suggested in the review.

@theshubhamp theshubhamp requested review from rojkov and qiwzhang August 17, 2021 18:00
Signed-off-by: Shubham Patil <theshubhamp@gmail.com>
@theshubhamp
Copy link
Contributor Author

/retest

@repokitteh-read-only
Copy link

Retrying Azure Pipelines:
Retried failed jobs in: envoy-presubmit

🐱

Caused by: a #17721 (comment) was created by @theshubhamp.

see: more, trace.

@rojkov
Copy link
Member

rojkov commented Aug 18, 2021

/retest

@repokitteh-read-only
Copy link

Retrying Azure Pipelines:
Retried failed jobs in: envoy-presubmit

🐱

Caused by: a #17721 (comment) was created by @rojkov.

see: more, trace.

rojkov
rojkov previously approved these changes Aug 18, 2021
Copy link
Member

@rojkov rojkov left a 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

@theshubhamp
Copy link
Contributor Author

theshubhamp commented Aug 18, 2021

cc: @lizan, this PR requires a mandatory review from you. PTAL whenever you can!

Copy link
Member

@lizan lizan left a comment

Choose a reason for hiding this comment

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

/wait

Comment on lines 227 to 239

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

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.

Copy link
Contributor Author

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

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

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.

https://github.com/envoyproxy/envoy/blob/main/source/extensions/filters/http/oauth2/filter.cc#L141-L143

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

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

Copy link
Contributor Author

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>
@theshubhamp
Copy link
Contributor Author

/retest

@repokitteh-read-only
Copy link

Retrying Azure Pipelines:
Retried failed jobs in: envoy-presubmit

🐱

Caused by: a #17721 (comment) was created by @theshubhamp.

see: more, trace.

Copy link
Contributor

@adisuissa adisuissa left a comment

Choose a reason for hiding this comment

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

/lgtm api

@lizan
Copy link
Member

lizan commented Aug 30, 2021

can you resolve conflicts?

@lizan lizan added the waiting label Aug 30, 2021
…-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>
@theshubhamp
Copy link
Contributor Author

Merged in master and resolved conflicts. PTAL!

Copy link
Member

@rojkov rojkov left a 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.

@lizan lizan merged commit 143083c into envoyproxy:main Sep 3, 2021
@theshubhamp theshubhamp deleted the theshubhamp/envoy-jwt-cookie branch September 3, 2021 08:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support JWT extraction from cookies in jwt_authn
5 participants