Skip to content

xds: Add header mutations library#12494

Open
sauravzg wants to merge 11 commits intogrpc:masterfrom
sauravzg:feat/header-mutations
Open

xds: Add header mutations library#12494
sauravzg wants to merge 11 commits intogrpc:masterfrom
sauravzg:feat/header-mutations

Conversation

@sauravzg
Copy link
Copy Markdown
Collaborator

@sauravzg sauravzg commented Nov 11, 2025

This PR sits on top of #12690, so only the last commit + any fixups need to be reviewed.

This commit introduces a library for handling header mutations as specified by the xDS protocol. This library provides the core functionality for modifying request and response headers based on a set of rules.

The main components of this library are:

  • HeaderMutator: Applies header mutations to Metadata objects.
  • HeaderMutationFilter: Filters header mutations based on a set of configurable rules, such as disallowing mutations of system headers.
  • HeaderMutations: A value class that represents the set of mutations to be applied to request and response headers.
  • HeaderMutationDisallowedException: An exception that is thrown when a disallowed header mutation is attempted.

This commit also includes comprehensive unit tests for the new library.

Comment thread xds/src/main/java/io/grpc/xds/internal/headermutations/HeaderMutator.java Outdated
@sauravzg sauravzg force-pushed the feat/header-mutations branch from 86d68ac to 759130e Compare March 12, 2026 13:46
@sauravzg
Copy link
Copy Markdown
Collaborator Author

@kannanjgithub PTAL, if the implementation mutation meets the spec for ext proc. You need to TAL at the last two commits only.
I'll eventually move it down the PR chain later to ensure that the dependencies for ext_proc are merged earlier.

@sauravzg sauravzg force-pushed the feat/header-mutations branch from 759130e to 4bfd5e6 Compare March 12, 2026 18:14
@sauravzg sauravzg changed the title feat(xds): Add header mutations library xds: Add header mutations library Mar 12, 2026
@sauravzg sauravzg force-pushed the feat/header-mutations branch from 4bfd5e6 to 6961989 Compare March 12, 2026 19:11
Copy link
Copy Markdown
Contributor

@kannanjgithub kannanjgithub left a comment

Choose a reason for hiding this comment

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

Sending comments for headermutations.

Comment thread xds/src/main/java/io/grpc/xds/internal/headermutations/HeaderMutations.java Outdated
Comment thread xds/src/main/java/io/grpc/xds/internal/headermutations/HeaderMutator.java Outdated
Comment thread xds/src/main/java/io/grpc/xds/internal/headermutations/HeaderMutationFilter.java Outdated
Copy link
Copy Markdown
Contributor

@kannanjgithub kannanjgithub left a comment

Choose a reason for hiding this comment

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

Reviewed rest of the PR.

@sauravzg
Copy link
Copy Markdown
Collaborator Author

@kannanjgithub I've resolved or clarified the comments as applicable. PTAL

@sauravzg sauravzg force-pushed the feat/header-mutations branch 2 times, most recently from 90e5491 to efb725f Compare March 15, 2026 21:44
@sauravzg
Copy link
Copy Markdown
Collaborator Author

@kannanjgithub Addressed the comments. PTAL.

@sauravzg
Copy link
Copy Markdown
Collaborator Author

/gemini review

@kannanjgithub kannanjgithub requested a review from AgraVator March 17, 2026 06:04
@sauravzg sauravzg force-pushed the feat/header-mutations branch 2 times, most recently from 399e233 to 677b251 Compare March 17, 2026 08:40
Comment thread xds/src/main/java/io/grpc/xds/GrpcServiceConfigParser.java Outdated
Comment thread xds/src/main/java/io/grpc/xds/internal/headermutations/HeaderMutationFilter.java Outdated
Comment thread xds/src/main/java/io/grpc/xds/internal/headermutations/HeaderMutator.java Outdated
sauravzg added a commit to sauravzg/grpc-java that referenced this pull request Mar 31, 2026
@sauravzg sauravzg force-pushed the feat/header-mutations branch from d999d4b to 0a3efdf Compare March 31, 2026 13:03
sauravzg added a commit to sauravzg/grpc-java that referenced this pull request Mar 31, 2026
@sauravzg sauravzg force-pushed the feat/header-mutations branch from 0a3efdf to 5261286 Compare March 31, 2026 13:07
@sauravzg
Copy link
Copy Markdown
Collaborator Author

sauravzg commented Apr 1, 2026

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces the core infrastructure for xDS external authorization (ext_authz), including parsers for ExtAuthz and GrpcService configurations, a CachedChannelManager for authorization service connections, and a comprehensive header mutation system. Feedback focuses on HeaderMutator.java, specifically identifying a bug where the keepEmptyValue logic does not correctly align with Envoy's behavior for the APPEND action, which could lead to incorrect header persistence. Additionally, it is recommended to refactor duplicated Metadata.Key creation logic into a helper method to improve maintainability.

Comment thread xds/src/main/java/io/grpc/xds/internal/headermutations/HeaderMutator.java Outdated
Comment thread xds/src/main/java/io/grpc/xds/internal/headermutations/HeaderMutator.java Outdated
sauravzg added a commit to sauravzg/grpc-java that referenced this pull request Apr 1, 2026
@sauravzg sauravzg force-pushed the feat/header-mutations branch from 5261286 to 9ec272a Compare April 1, 2026 13:31
sauravzg added a commit to sauravzg/grpc-java that referenced this pull request Apr 2, 2026
@sauravzg sauravzg force-pushed the feat/header-mutations branch from 9ec272a to 951b761 Compare April 2, 2026 11:58
@sauravzg
Copy link
Copy Markdown
Collaborator Author

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a CachedChannelManager to manage gRPC channels based on service configurations, alongside a new framework for processing xDS header mutations. The mutation framework includes components for filtering, applying, and validating header changes. Review feedback highlights the need to align the mutation order with the xDS specification, correct the logic for header allowance expressions, optimize performance by pre-calculating metadata keys, and improve error reporting with more descriptive exception messages.

Comment thread xds/src/main/java/io/grpc/xds/internal/headermutations/HeaderMutationFilter.java Outdated
@sauravzg sauravzg force-pushed the feat/header-mutations branch from 8d6cd46 to 312f166 Compare April 22, 2026 13:16
@sauravzg
Copy link
Copy Markdown
Collaborator Author

/gemini review - Please don't repeat previously addressed comments.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a CachedChannelManager for managing gRPC channels and a framework for xDS header mutations, including filtering and application logic. Key feedback includes aligning the order of header operations in HeaderMutator with Envoy by processing removals before additions and caching Metadata.Key objects to optimize performance. Furthermore, the filtering logic in HeaderMutationFilter needs adjustment to ensure disallowed headers properly trigger exceptions when strict validation is active.

@sauravzg sauravzg force-pushed the feat/header-mutations branch from c7777c2 to 46c951d Compare April 22, 2026 14:10
sauravzg added 11 commits April 22, 2026 14:18
This commit introduces a library for handling header mutations as specified by the xDS protocol. This library provides the core functionality for modifying request and response headers based on a set of rules.

The main components of this library are:
- `HeaderMutator`: Applies header mutations to `Metadata` objects.
- `HeaderMutationFilter`: Filters header mutations based on a set of configurable rules, such as disallowing mutations of system headers.
- `HeaderMutations`: A value class that represents the set of mutations to be applied to request and response headers.
- `HeaderMutationDisallowedException`: An exception that is thrown when a disallowed header mutation is attempted.

This commit also includes comprehensive unit tests for the new library.
@sauravzg sauravzg force-pushed the feat/header-mutations branch from 46c951d to 9640f87 Compare April 22, 2026 14:27
Copy link
Copy Markdown
Contributor

@kannanjgithub kannanjgithub left a comment

Choose a reason for hiding this comment

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

Some unit tests are missing.

assertThat(filtered.headers()).containsExactly(header("x-allowed-key", "value"));
}


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.

nit: Remove extra empty line.

assertThat(headers.get(keepEmptyKey)).isEqualTo("");
assertThat(headers.containsKey(keepEmptyOverwriteKey)).isTrue();
assertThat(headers.get(keepEmptyOverwriteKey)).isEqualTo("");

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.

nit: Remove unnecessary blank line.

private void updateHeader(final HeaderValueOption option, Metadata mutableHeaders) {
HeaderValue header = option.header();
HeaderAppendAction action = option.appendAction();
boolean keepEmptyValue = option.keepEmptyValue();
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.

The commit b9caa47 refactored how keepEmptyValue is handled in HeaderMutator.java, moving the check from a post-action cleanup (checkAndRemoveEmpty) to a pre-action filter. The following
cases are not covered by the existing tests:

  1. Binary Headers: No tests exist for binary headers with empty values (empty ByteString) when keepEmptyValue is either true or false.
  2. Specific Actions: The OVERWRITE_IF_EXISTS and ADD_IF_ABSENT actions are not tested with empty values and keepEmptyValue=false.
  3. Behavior Change Verification: The new implementation skips the updateHeader call entirely if the value is empty and keepEmptyValue is false. This means existing values are preserved during an OVERWRITE action, whereas the old implementation would have removed them. This deliberate change in behavior needs explicit verification.

* A generic helper to filter a collection based on a predicate.
*/
private <T> ImmutableList<T> filterCollection(Collection<T> items,
Predicate<T> isIgnoredPredicate, Predicate<T> isAllowedPredicate)
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.

The commit 9640f87 updated HeaderMutationFilter.java to match Envoy's implementation, where headers that are either "ignored" (system headers like :path or grpc-*) or "disallowed" (by custom
rules) trigger a HeaderMutationDisallowedException when disallowIsError is enabled. Previously, ignored headers were skipped silently even if disallowIsError was true.

The following scenarios are missing coverage:

  1. DisallowIsError = False: No test verifies that disallowed/ignored headers are still silently skipped when disallowIsError is false.
  2. Rule-based Disallowance: No tests verify that headers disallowed by custom regex (disallowExpression) or disallowAll trigger the exception when disallowIsError is true.
  3. Rule-based Allowance Override: No test verifies that allowExpression correctly overrides disallowAll even when disallowIsError is true.
  4. System Header Rejection: While some tests were added in the commit for system headers, they don't cover the full range of HeaderValueValidationUtils logic (e.g., uppercase keys).

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.

5 participants