-
Notifications
You must be signed in to change notification settings - Fork 5.5k
ext_authz: add grpc_service field on the per-route filter #40169
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
465e6a6
ext_authz: add grpc_service field on the per-route filter
agrawroh a6bfc33
Merge branch 'main' of github.com:envoyproxy/envoy into ext-authz-clu…
agrawroh 713548d
Merge branch 'main' of github.com:envoyproxy/envoy into ext-authz-clu…
agrawroh bb0fd60
addressed comments from @wbpcode
agrawroh 18bb804
fixes
agrawroh 03ec656
Merge branch 'main' into ext-authz-cluster
agrawroh 8fcd7b1
Merge branch 'main' of github.com:envoyproxy/envoy into ext-authz-clu…
agrawroh 6b71150
addressed comments from @wbpcode
agrawroh d921670
fix
agrawroh 25bab2d
fix
agrawroh 7ffdadb
add tests
agrawroh 56f0983
Merge branch 'main' of github.com:envoyproxy/envoy into ext-authz-clu…
agrawroh 63d75e3
addressed comments from @wbpcode
agrawroh 7a65cea
fixes
agrawroh 00d5cb9
fixes
agrawroh f5ea213
fixes
agrawroh 0b87c69
Merge branch 'main' into ext-authz-cluster
agrawroh 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| #include "ext_authz.h" | ||
| #include "source/extensions/filters/http/ext_authz/ext_authz.h" | ||
|
|
||
| #include <chrono> | ||
|
|
@@ -21,6 +20,9 @@ namespace ExtAuthz { | |
|
|
||
| namespace { | ||
|
|
||
| // Default timeout for per-route gRPC client creation. | ||
| constexpr uint32_t kDefaultPerRouteTimeoutMs = 200; | ||
|
|
||
| using MetadataProto = ::envoy::config::core::v3::Metadata; | ||
| using Filters::Common::MutationRules::CheckOperation; | ||
| using Filters::Common::MutationRules::CheckResult; | ||
|
|
@@ -172,6 +174,90 @@ void FilterConfigPerRoute::merge(const FilterConfigPerRoute& other) { | |
| } | ||
| } | ||
|
|
||
| // Constructor used for merging configurations from different levels (vhost, route, etc.) | ||
| FilterConfigPerRoute::FilterConfigPerRoute(const FilterConfigPerRoute& less_specific, | ||
| const FilterConfigPerRoute& more_specific) | ||
| : context_extensions_(less_specific.context_extensions_), | ||
| check_settings_(more_specific.check_settings_), disabled_(more_specific.disabled_), | ||
| // Only use the most specific per-route override. Do not inherit overrides from less | ||
| // specific configuration. If the more specific configuration has no override, leave both | ||
| // unset so that the main filter configuration is used. | ||
| grpc_service_(more_specific.grpc_service_.has_value() ? more_specific.grpc_service_ | ||
| : absl::nullopt), | ||
| http_service_(more_specific.http_service_.has_value() ? more_specific.http_service_ | ||
| : absl::nullopt) { | ||
| // Merge context extensions from more specific configuration, overriding less specific ones. | ||
| for (const auto& extension : more_specific.context_extensions_) { | ||
| context_extensions_[extension.first] = extension.second; | ||
| } | ||
| } | ||
|
wbpcode marked this conversation as resolved.
|
||
|
|
||
| Filters::Common::ExtAuthz::ClientPtr | ||
| Filter::createPerRouteGrpcClient(const envoy::config::core::v3::GrpcService& grpc_service) { | ||
| if (server_context_ == nullptr) { | ||
| ENVOY_STREAM_LOG( | ||
| debug, "ext_authz filter: server context not available for per-route gRPC client creation.", | ||
| *decoder_callbacks_); | ||
| return nullptr; | ||
| } | ||
|
|
||
| // Use the timeout from the gRPC service configuration, use default if not specified. | ||
| const uint32_t timeout_ms = | ||
| PROTOBUF_GET_MS_OR_DEFAULT(grpc_service, timeout, kDefaultPerRouteTimeoutMs); | ||
|
|
||
| // We can skip transport version check for per-route gRPC service here. | ||
| // The transport version is already validated at the main configuration level. | ||
| Envoy::Grpc::GrpcServiceConfigWithHashKey config_with_hash_key = | ||
| Envoy::Grpc::GrpcServiceConfigWithHashKey(grpc_service); | ||
|
|
||
| auto client_or_error = server_context_->clusterManager() | ||
| .grpcAsyncClientManager() | ||
| .getOrCreateRawAsyncClientWithHashKey(config_with_hash_key, | ||
| server_context_->scope(), true); | ||
| if (!client_or_error.ok()) { | ||
| ENVOY_STREAM_LOG(warn, | ||
| "ext_authz filter: failed to create per-route gRPC client: {}. Falling back " | ||
| "to default client.", | ||
| *decoder_callbacks_, client_or_error.status().ToString()); | ||
| return nullptr; | ||
| } | ||
|
|
||
| ENVOY_STREAM_LOG(debug, "ext_authz filter: created per-route gRPC client for cluster: {}.", | ||
| *decoder_callbacks_, | ||
| grpc_service.has_envoy_grpc() ? grpc_service.envoy_grpc().cluster_name() | ||
|
agrawroh marked this conversation as resolved.
|
||
| : "google_grpc"); | ||
|
|
||
| return std::make_unique<Filters::Common::ExtAuthz::GrpcClientImpl>( | ||
| client_or_error.value(), std::chrono::milliseconds(timeout_ms)); | ||
| } | ||
|
|
||
| Filters::Common::ExtAuthz::ClientPtr Filter::createPerRouteHttpClient( | ||
| const envoy::extensions::filters::http::ext_authz::v3::HttpService& http_service) { | ||
| if (server_context_ == nullptr) { | ||
| ENVOY_STREAM_LOG( | ||
| debug, "ext_authz filter: server context not available for per-route HTTP client creation.", | ||
| *decoder_callbacks_); | ||
| return nullptr; | ||
| } | ||
|
|
||
| // Use the timeout from the HTTP service configuration, use default if not specified. | ||
| const uint32_t timeout_ms = | ||
| PROTOBUF_GET_MS_OR_DEFAULT(http_service.server_uri(), timeout, kDefaultPerRouteTimeoutMs); | ||
|
|
||
| ENVOY_STREAM_LOG(debug, "ext_authz filter: creating per-route HTTP client for URI: {}.", | ||
| *decoder_callbacks_, http_service.server_uri().uri()); | ||
|
|
||
| // Create a temporary ExtAuthz config with the HTTP service for the ClientConfig constructor. | ||
| envoy::extensions::filters::http::ext_authz::v3::ExtAuthz temp_config; | ||
| temp_config.mutable_http_service()->CopyFrom(http_service); | ||
|
Member
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. I think maybe we should update the constructor of ClientConfig to accept HttpService only to avoid the this copy. But it's not in a hurry. The auth is slow anyway. |
||
|
|
||
| const auto client_config = std::make_shared<Extensions::Filters::Common::ExtAuthz::ClientConfig>( | ||
| temp_config, timeout_ms, http_service.path_prefix(), *server_context_); | ||
|
|
||
| return std::make_unique<Extensions::Filters::Common::ExtAuthz::RawHttpClientImpl>( | ||
| server_context_->clusterManager(), client_config); | ||
| } | ||
|
|
||
| void Filter::initiateCall(const Http::RequestHeaderMap& headers) { | ||
| if (filter_return_ == FilterReturn::StopDecoding) { | ||
| return; | ||
|
|
@@ -205,9 +291,10 @@ void Filter::initiateCall(const Http::RequestHeaderMap& headers) { | |
| for (const FilterConfigPerRoute& cfg : | ||
| Http::Utility::getAllPerFilterConfig<FilterConfigPerRoute>(decoder_callbacks_)) { | ||
| if (maybe_merged_per_route_config.has_value()) { | ||
| maybe_merged_per_route_config.value().merge(cfg); | ||
| FilterConfigPerRoute current_config = maybe_merged_per_route_config.value(); | ||
| maybe_merged_per_route_config.emplace(current_config, cfg); | ||
| } else { | ||
| maybe_merged_per_route_config = cfg; | ||
| maybe_merged_per_route_config.emplace(cfg); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -216,6 +303,46 @@ void Filter::initiateCall(const Http::RequestHeaderMap& headers) { | |
| context_extensions = maybe_merged_per_route_config.value().takeContextExtensions(); | ||
| } | ||
|
|
||
| // Check if we need to use a per-route service override (gRPC or HTTP). | ||
| Filters::Common::ExtAuthz::ClientPtr* client_to_use = &client_; | ||
|
agrawroh marked this conversation as resolved.
Outdated
|
||
| if (maybe_merged_per_route_config) { | ||
| if (maybe_merged_per_route_config->grpcService().has_value()) { | ||
| const auto& grpc_service = maybe_merged_per_route_config->grpcService().value(); | ||
| ENVOY_STREAM_LOG(debug, "ext_authz filter: using per-route gRPC service configuration.", | ||
| *decoder_callbacks_); | ||
|
|
||
| // Create a new gRPC client for this route. | ||
| per_route_client_ = createPerRouteGrpcClient(grpc_service); | ||
| if (per_route_client_ != nullptr) { | ||
| client_to_use = &per_route_client_; | ||
| ENVOY_STREAM_LOG(debug, "ext_authz filter: successfully created per-route gRPC client.", | ||
| *decoder_callbacks_); | ||
| } else { | ||
| ENVOY_STREAM_LOG( | ||
| warn, | ||
| "ext_authz filter: failed to create per-route gRPC client, falling back to default.", | ||
| *decoder_callbacks_); | ||
| } | ||
| } else if (maybe_merged_per_route_config->httpService().has_value()) { | ||
| const auto& http_service = maybe_merged_per_route_config->httpService().value(); | ||
| ENVOY_STREAM_LOG(debug, "ext_authz filter: using per-route HTTP service configuration.", | ||
| *decoder_callbacks_); | ||
|
|
||
| // Create a new HTTP client for this route. | ||
| per_route_client_ = createPerRouteHttpClient(http_service); | ||
| if (per_route_client_ != nullptr) { | ||
| client_to_use = &per_route_client_; | ||
| ENVOY_STREAM_LOG(debug, "ext_authz filter: successfully created per-route HTTP client.", | ||
| *decoder_callbacks_); | ||
| } else { | ||
| ENVOY_STREAM_LOG( | ||
| warn, | ||
| "ext_authz filter: failed to create per-route HTTP client, falling back to default.", | ||
| *decoder_callbacks_); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // If metadata_context_namespaces or typed_metadata_context_namespaces is specified, | ||
| // pass matching filter metadata to the ext_authz service. | ||
| // If metadata key is set in both the connection and request metadata, | ||
|
|
@@ -241,7 +368,7 @@ void Filter::initiateCall(const Http::RequestHeaderMap& headers) { | |
| config_->destinationLabels(), config_->allowedHeadersMatcher(), | ||
| config_->disallowedHeadersMatcher()); | ||
|
|
||
| ENVOY_STREAM_LOG(trace, "ext_authz filter calling authorization server", *decoder_callbacks_); | ||
| ENVOY_STREAM_LOG(trace, "ext_authz filter calling authorization server.", *decoder_callbacks_); | ||
| // Store start time of ext_authz filter call | ||
| start_time_ = decoder_callbacks_->dispatcher().timeSource().monotonicTime(); | ||
|
|
||
|
|
@@ -250,8 +377,9 @@ void Filter::initiateCall(const Http::RequestHeaderMap& headers) { | |
| // going to invoke check call. | ||
| cluster_ = decoder_callbacks_->clusterInfo(); | ||
| initiating_call_ = true; | ||
| client_->check(*this, check_request_, decoder_callbacks_->activeSpan(), | ||
| decoder_callbacks_->streamInfo()); | ||
| (*client_to_use) | ||
| ->check(*this, check_request_, decoder_callbacks_->activeSpan(), | ||
| decoder_callbacks_->streamInfo()); | ||
| initiating_call_ = false; | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.