-
Notifications
You must be signed in to change notification settings - Fork 5.3k
jwt_authn: support fetching jwks in the background #16298
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
Changes from all commits
5a1e32d
e187c15
6dcf39d
0a0509f
fe2bf2f
681fb81
33386e4
4d254f0
fc972d3
79e7755
5bdbb83
94325cd
4dd6478
c4d525b
5ad4636
12586d9
8dbca85
5860500
453d8a9
2dd61a5
a7f423f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -221,6 +221,7 @@ void AuthenticatorImpl::startVerify() { | |
| } | ||
|
|
||
| void AuthenticatorImpl::onJwksSuccess(google::jwt_verify::JwksPtr&& jwks) { | ||
| jwks_cache_.stats().jwks_fetch_success_.inc(); | ||
| const Status status = jwks_data_->setRemoteJwks(std::move(jwks))->getStatus(); | ||
| if (status != Status::Ok) { | ||
| doneWithStatus(status); | ||
|
|
@@ -229,7 +230,10 @@ void AuthenticatorImpl::onJwksSuccess(google::jwt_verify::JwksPtr&& jwks) { | |
| } | ||
| } | ||
|
|
||
| void AuthenticatorImpl::onJwksError(Failure) { doneWithStatus(Status::JwksFetchFail); } | ||
| void AuthenticatorImpl::onJwksError(Failure) { | ||
| jwks_cache_.stats().jwks_fetch_failed_.inc(); | ||
|
||
| doneWithStatus(Status::JwksFetchFail); | ||
| } | ||
|
|
||
| void AuthenticatorImpl::onDestroy() { | ||
| if (fetcher_) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| #include "extensions/filters/http/jwt_authn/jwks_async_fetcher.h" | ||
|
|
||
| #include "common/protobuf/utility.h" | ||
| #include "common/tracing/http_tracer_impl.h" | ||
|
|
||
| using envoy::extensions::filters::http::jwt_authn::v3::RemoteJwks; | ||
|
|
||
| namespace Envoy { | ||
| namespace Extensions { | ||
| namespace HttpFilters { | ||
| namespace JwtAuthn { | ||
| namespace { | ||
|
|
||
| // Default cache expiration time in 5 minutes. | ||
| constexpr int PubkeyCacheExpirationSec = 600; | ||
|
|
||
| } // namespace | ||
|
|
||
| JwksAsyncFetcher::JwksAsyncFetcher(const RemoteJwks& remote_jwks, | ||
| Server::Configuration::FactoryContext& context, | ||
| CreateJwksFetcherCb create_fetcher_fn, | ||
| JwtAuthnFilterStats& stats, JwksDoneFetched done_fn) | ||
| : remote_jwks_(remote_jwks), context_(context), create_fetcher_fn_(create_fetcher_fn), | ||
| stats_(stats), done_fn_(done_fn), cache_duration_(getCacheDuration(remote_jwks)), | ||
| debug_name_(absl::StrCat("Jwks async fetching url=", remote_jwks_.http_uri().uri())) { | ||
| // if async_fetch is not enabled, do nothing. | ||
| if (!remote_jwks_.has_async_fetch()) { | ||
| return; | ||
| } | ||
|
|
||
| cache_duration_timer_ = context_.dispatcher().createTimer([this]() -> void { fetch(); }); | ||
|
|
||
| // For fast_listener, just trigger a fetch, not register with init_manager. | ||
| if (remote_jwks_.async_fetch().fast_listener()) { | ||
| fetch(); | ||
| return; | ||
| } | ||
|
|
||
| // Register to init_manager, force the listener to wait for the fetching. | ||
| init_target_ = std::make_unique<Init::TargetImpl>(debug_name_, [this]() -> void { fetch(); }); | ||
| context_.initManager().add(*init_target_); | ||
| } | ||
|
|
||
| std::chrono::seconds JwksAsyncFetcher::getCacheDuration(const RemoteJwks& remote_jwks) { | ||
| if (remote_jwks.has_cache_duration()) { | ||
| return std::chrono::seconds(DurationUtil::durationToSeconds(remote_jwks.cache_duration())); | ||
| } | ||
| return std::chrono::seconds(PubkeyCacheExpirationSec); | ||
| } | ||
|
|
||
| void JwksAsyncFetcher::fetch() { | ||
| if (fetcher_) { | ||
| fetcher_->cancel(); | ||
| } | ||
|
|
||
| ENVOY_LOG(debug, "{}: started", debug_name_); | ||
| fetcher_ = create_fetcher_fn_(context_.clusterManager()); | ||
| fetcher_->fetch(remote_jwks_.http_uri(), Tracing::NullSpan::instance(), *this); | ||
| } | ||
|
|
||
| void JwksAsyncFetcher::handleFetchDone() { | ||
| if (init_target_) { | ||
| init_target_->ready(); | ||
| init_target_.reset(); | ||
| } | ||
|
|
||
| cache_duration_timer_->enableTimer(cache_duration_); | ||
| } | ||
|
|
||
| void JwksAsyncFetcher::onJwksSuccess(google::jwt_verify::JwksPtr&& jwks) { | ||
| stats_.jwks_fetch_success_.inc(); | ||
|
|
||
| done_fn_(std::move(jwks)); | ||
| handleFetchDone(); | ||
|
|
||
| // Note: not to free fetcher_ within onJwksSuccess or onJwksError function. | ||
| // They are passed to fetcher_->fetch() and are called by fetcher_ after fetch is done. | ||
| // After calling these callback functions, fetch_ calls its reset() function. | ||
| // If fetcher_ is freed by the callback, calling reset() will crash. | ||
lizan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Not need to free fetcher_. At the next fetch(), it will be freed with a cancel() call. | ||
| // The cancel() is needed to cancel the old call before the new one is created. | ||
| // But it is a no-op if the call is completed. | ||
| } | ||
|
|
||
| void JwksAsyncFetcher::onJwksError(Failure) { | ||
| stats_.jwks_fetch_failed_.inc(); | ||
lizan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ENVOY_LOG(warn, "{}: failed", debug_name_); | ||
| handleFetchDone(); | ||
|
|
||
| // Note: not to free fetcher_ in this function. Please see comment at onJwksSuccess. | ||
| } | ||
|
|
||
| } // namespace JwtAuthn | ||
| } // namespace HttpFilters | ||
| } // namespace Extensions | ||
| } // namespace Envoy | ||
Uh oh!
There was an error while loading. Please reload this page.