Skip to content

Add header cloning for downstream and upstream#2774

Merged
Krishanx92 merged 4 commits into
wso2:mainfrom
SavinduDimal:policy-engine-header-clone
Jul 22, 2026
Merged

Add header cloning for downstream and upstream#2774
Krishanx92 merged 4 commits into
wso2:mainfrom
SavinduDimal:policy-engine-header-clone

Conversation

@SavinduDimal

Copy link
Copy Markdown
Contributor

Purpose

Description

This pull request introduces support for exposing both the original (pristine) and mutated request/response headers to custom Python policies in the gateway. It adds new downstream and upstream context fields to all policy context types, ensuring that policies can reliably distinguish between the headers as originally received/sent and those potentially modified by earlier policies. The documentation is updated to explain the correct usage and fallback patterns, and comprehensive tests are added to verify correctness and backward compatibility.

API and Protocol Buffer Additions:

  • Added DownstreamContext (with DownstreamRequest) and UpstreamRequestContext/UpstreamResponseContext (with UpstreamResponse) messages to the policy engine proto, and threaded these through all request and response context messages. These fields provide snapshots of the original client request and upstream response headers, respectively. [1] [2] [3] [4] [5] [6] [7]

Kernel Implementation:

  • In PolicyExecutionContext, captured a snapshot of the original client request headers before any policy mutation, storing it as downstreamHeaders and exposing it via the new context fields for both request and response phases. [1] [2] [3] [4]

Documentation Improvements:

  • Updated the Python policy authoring docs to describe the new downstream and upstream fields, how they differ from the mutable headers, and how to safely use them with fallback logic for older gateways. Provided code examples and guidance on validating original header values. [1] [2] [3] [4] [5]

Testing:

  • Added downstream_upstream_test.go with tests ensuring that the downstream and upstream snapshots are correctly populated, survive header mutations, and are available on all context types. Tests also confirm that the correct (original vs. mutated) header values are observed in all relevant scenarios.

These changes ensure that custom policies can reliably validate headers as originally received from the client or backend, regardless of any in-flight mutations by other policies, while maintaining backward compatibility with older gateway versions.

@coderabbitai

coderabbitai Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 610b6b1a-4b05-413e-abb6-1ec07697d31b

📥 Commits

Reviewing files that changed from the base of the PR and between 006d3c9 and 0720c07.

⛔ Files ignored due to path filters (1)
  • gateway/gateway-runtime/policy-engine/go.sum is excluded by !**/*.sum
📒 Files selected for processing (2)
  • gateway/gateway-runtime/policy-engine/go.mod
  • gateway/gateway-runtime/python-executor/requirements.txt
🚧 Files skipped from review as they are similar to previous changes (1)
  • gateway/gateway-runtime/policy-engine/go.mod

📝 Walkthrough

Walkthrough

The gateway preserves original downstream request and upstream response headers, exposes them through protobuf and Python policy contexts across request, response, body, and stream phases, translates optional snapshot fields, tests mutation behavior, and documents snapshot access.

Changes

Context snapshot propagation

Layer / File(s) Summary
Context schema contracts
gateway/gateway-runtime/api/proto/python_executor.proto, gateway/gateway-runtime/python-executor/proto/python_executor_pb2.py, gateway/gateway-runtime/policy-engine/go.mod, gateway/gateway-runtime/python-executor/requirements.txt
Adds downstream and upstream context messages and fields, regenerates Python protobuf descriptors, and updates SDK dependencies.
Kernel snapshot capture and validation
gateway/gateway-runtime/policy-engine/internal/kernel/execution_context.go, gateway/gateway-runtime/policy-engine/internal/kernel/downstream_upstream_test.go, gateway/gateway-runtime/policy-engine/internal/testutils/contexts.go
Captures headers before mutation, populates policy contexts, and tests preservation across request and response phases.
Go bridge transport wiring
gateway/gateway-runtime/policy-engine/internal/pythonbridge/translator.go, gateway/gateway-runtime/policy-engine/internal/pythonbridge/bridge_execution.go
Translates optional snapshot contexts and attaches them to request, response, and stream protobuf payloads.
Python context decoding and documentation
gateway/gateway-runtime/python-executor/executor/translator.py, docs/gateway/policies/writing-custom-python-policies.md
Reconstructs optional snapshots in Python contexts and documents current headers, original snapshots, and legacy nil handling.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant PolicyExecutionContext
  participant GoPythonBridge
  participant PythonTranslator
  participant PythonPolicy
  PolicyExecutionContext->>GoPythonBridge: attach downstream and upstream snapshots
  GoPythonBridge->>PythonTranslator: send protobuf context
  PythonTranslator->>PythonPolicy: provide decoded snapshot contexts
Loading

Possibly related PRs

  • wso2/api-platform#2631: Adds SDK context types and wiring aligned with the downstream and upstream snapshot fields.

Suggested reviewers: krishanx92, renuka-fernando, chamilaadhi, pubudu538, lasanthas

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers purpose and high-level changes, but it misses most required template sections like goals, tests, security, samples, and environment. Fill in the missing template sections with goals, approach, user stories, documentation links, automation/security test details, samples, related PRs, and test environment.
Docstring Coverage ⚠️ Warning Docstring coverage is 42.86% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title matches the main change: exposing original request/response headers via downstream/upstream snapshots.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 Checkov (3.3.8)
gateway/gateway-runtime/policy-engine/go.mod

Traceback (most recent call last):
File "/usr/local/bin/checkov", line 2, in
from checkov.main import Checkov
ModuleNotFoundError: No module named 'checkov'

gateway/gateway-runtime/python-executor/requirements.txt

Traceback (most recent call last):
File "/usr/local/bin/checkov", line 2, in
from checkov.main import Checkov
ModuleNotFoundError: No module named 'checkov'

🔧 golangci-lint (2.12.2)

level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain modules listed in go.work or their selected dependencies"


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

coderabbitai[bot]
coderabbitai Bot previously approved these changes Jul 21, 2026
@SavinduDimal

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

coderabbitai[bot]
coderabbitai Bot previously approved these changes Jul 21, 2026
Update go and python sdk core versions
@SavinduDimal

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@Krishanx92
Krishanx92 merged commit 2789753 into wso2:main Jul 22, 2026
10 of 12 checks passed
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.

3 participants