Skip to content

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

Merged
lizan merged 9 commits into
envoyproxy:mainfrom
theshubhamp:theshubhamp/envoy-jwt-cookie
Sep 3, 2021
Merged

lizan merged 9 commits into
envoyproxy:mainfrom
theshubhamp:theshubhamp/envoy-jwt-cookie

Conversation

@theshubhamp

@theshubhamp theshubhamp commented Aug 15, 2021 •

Copy link
Copy Markdown
Contributor

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 [email protected]

Added "from_cookies" config directive to jwt_authn that enables JWT extraction from request cookies.

Testing: unit tests

Signed-off-by: Shubham Patil <[email protected]>
@theshubhamp
theshubhamp requested a review from lizan as a code owner August 15, 2021 18:47
@repokitteh-read-only

Copy link
Copy Markdown

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
Copy Markdown

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

theshubhamp commented Aug 15, 2021 •

Copy link
Copy Markdown
Contributor Author

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.

Comment thread source/common/http/utility.cc Outdated
return EMPTY_STRING;
}

std::map<std::string, std::string> Utility::parseCookies(const RequestHeaderMap& headers) {

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

@rojkov rojkov left a comment

Copy link
Copy Markdown
Member

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.

Comment thread source/common/http/utility.cc Outdated
return EMPTY_STRING;
}

std::map<std::string, std::string> Utility::parseCookies(const RequestHeaderMap& headers) {

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

qiwzhang
qiwzhang previously approved these changes Aug 16, 2021

@qiwzhang qiwzhang left a comment

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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

rojkov commented Aug 17, 2021

Copy link
Copy Markdown
Member

/retest

@repokitteh-read-only

Copy link
Copy Markdown

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

🐱

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

see: more, trace.

@theshubhamp

Copy link
Copy Markdown
Contributor Author

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

@theshubhamp
theshubhamp requested review from qiwzhang and rojkov August 17, 2021 18:00
@theshubhamp

Copy link
Copy Markdown
Contributor Author

/retest

@repokitteh-read-only

Copy link
Copy Markdown

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

🐱

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

see: more, trace.

@rojkov

rojkov commented Aug 18, 2021

Copy link
Copy Markdown
Member

/retest

@repokitteh-read-only

Copy link
Copy Markdown

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

@rojkov rojkov left a comment

Copy link
Copy Markdown
Member

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

theshubhamp commented Aug 18, 2021 •

Copy link
Copy Markdown
Contributor Author

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

@lizan lizan left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
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

Comment thread source/common/http/utility.cc Outdated
return EMPTY_STRING;
}

std::map<std::string, std::string> Utility::parseCookies(const RequestHeaderMap& headers) {

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 <[email protected]>
@theshubhamp

Copy link
Copy Markdown
Contributor Author

/retest

@repokitteh-read-only

Copy link
Copy Markdown

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

🐱

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

see: more, trace.

@adisuissa adisuissa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

/lgtm api

@lizan

lizan commented Aug 30, 2021

Copy link
Copy Markdown
Member

can you resolve conflicts?

@lizan lizan added the waiting label Aug 30, 2021
@theshubhamp

Copy link
Copy Markdown
Contributor Author

Merged in master and resolved conflicts. PTAL!

@rojkov rojkov left a comment

Copy link
Copy Markdown
Member

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