Request Transformation Policy #921
Tharsanan1
started this conversation in
General
Replies: 2 comments 6 replies
-
|
Isn't this policy name too generic, |
Beta Was this translation helpful? Give feedback.
4 replies
-
Here, the transformation is only applied to the operation path. What if I need to change the context as well?
|
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Overview
The Request Transformation Policy enables rewriting request paths and HTTP methods before requests reach backend services. This is useful for API versioning migrations, protocol adaptations, and request normalization.
Problem Statement
Teams need to:
/api/v1/*to/api/v2/*without breaking existing integrationsArchitecture
Component Responsibilities
Why Lua for Method Rewriting?
Envoy's External Processor cannot directly modify HTTP methods. The workaround:
request_transformation.target_methodFilter Chain Order
Policy Definition
Name:
request-transformationVersion:
v0.1.0policy-definition.yaml
Parameter Reference
match
Optional match conditions that gate whether rewrites are applied. All conditions in
headersandqueryParamsmust be satisfied (logical AND).headersqueryParamsHeader/Query Matcher
nametypeExact,Regex,PresentvalueExactorRegexpathRewrite
Rewrites the request path. Supports three rewrite types.
typeReplacePrefixMatch,ReplaceFullPath, orReplaceRegexMatchreplacePrefixMatchReplacePrefixMatch)replaceFullPathReplaceFullPath)replaceRegexMatchReplaceRegexMatch)Type: ReplacePrefixMatch
Replaces the matched prefix portion of the path. The prefix to match is automatically determined by the operation path this policy is attached to.
Constraint: The incoming request path must start with the operation path for this rewrite to apply. You cannot match a different prefix than the operation path.
/api/v1/users/123/api/v1/api/v2/api/v2/users/123/old/resource/1/old/new/new/resource/1/api/v1/api/v1/api/v2/api/v2/api/v1/users/api/v1//usersType: ReplaceFullPath
Replaces the entire request path with the specified value. Query parameters are preserved.
/any/path/here/fixed/destination/fixed/destination/api/v1/users?id=1/v2/users/v2/users?id=1Type: ReplaceRegexMatch
Uses RE2 regular expressions to match and replace portions of the path. This is the same regex engine used by Envoy and Envoy Gateway.
patternsubstitution\1,\2, etc. for capture groups)RE2 Syntax Highlights:
([^/]+)captures one or more non-slash characters\1,\2in substitution refer to captured groups^(start),$(end)(?i)prefix[0-9],[a-zA-Z],\d,\wExamples:
/service/foo/v1/api^/service/([^/]+)(/.*)$\2/instance/\1/v1/api/instance/foo/xxx/one/yyy/one/zzzonetwo/xxx/two/yyy/two/zzz/xxx/one/yyy/one/zzz^(.*?)one(.*)$\1two\2/xxx/two/yyy/one/zzz/users/123/profile^/users/([0-9]+)/(.*)$/v2/accounts/\1/\2/v2/accounts/123/profile/aaa/XxX/bbb(?i)/xxx//yyy//aaa/yyy/bbbmethodRewrite
Changes the HTTP method of the request.
GETPOSTPUTDELETEPATCHHEADOPTIONSqueryRewrite
Rewrites query parameters using an ordered list of rules.
rulesQuery Rewrite Rule
actionReplace,Remove,Add,Append,ReplaceRegexMatchnamevalueReplace,Add,AppendseparatorAppend(default: empty string)patternReplaceRegexMatchsubstitutionReplaceRegexMatchAction semantics:
Replace: Replace all existing values fornamewithvalue(adds it if missing).Remove: Remove all values forname.Add: Add an additionalname=valueentry (preserves existing values).Append: Appendvalueto each existing value forname(createsname=valueif missing). Ifseparatoris provided, it is inserted between the old value andvalue.ReplaceRegexMatch: For each value ofname, apply regex replacement usingpatternandsubstitution.Usage Examples
Example 1: Prefix Replacement (Version Migration)
Rewrite
/api/v1/*to/api/v2/*:Example 2: Full Path Replacement
Redirect all requests to a fixed endpoint:
Example 3: Regex Path Rewrite
Extract path segments and reorganize:
This transforms
/users/123/orders/456→/v2/orders/456/user/123Example 4: Method Rewrite
Convert GET to POST for a specific operation:
Example 5: Combined Path and Method Rewrite
Example 6: Conditional Query Rewrite
Only add a
source=legacyquery parameter when a header is present:Validation Rules
pathRewrite,queryRewrite, normethodRewritespecifiedat least one of 'pathRewrite', 'queryRewrite', or 'methodRewrite' must be specifiedmatchprovided but emptymatch must include at least one header or query parameter matchermatch.headers[].typeisExactorRegexandvaluemissingmatch.headers[].value is required when type is Exact or Regexmatch.queryParams[].typeisExactorRegexandvaluemissingmatch.queryParams[].value is required when type is Exact or RegexpathRewrite.typemissingpathRewrite.type is requiredpathRewrite.typeisReplacePrefixMatchbutreplacePrefixMatchmissingreplacePrefixMatch is required when type is ReplacePrefixMatchpathRewrite.typeisReplaceFullPathbutreplaceFullPathmissingreplaceFullPath is required when type is ReplaceFullPathpathRewrite.typeisReplaceFullPathandreplaceFullPathdoesn't start with/replaceFullPath must start with '/'pathRewrite.typeisReplaceRegexMatchbutreplaceRegexMatchmissingreplaceRegexMatch is required when type is ReplaceRegexMatchreplaceRegexMatch.patternmissing or emptyreplaceRegexMatch.pattern is requiredreplaceRegexMatch.substitutionmissingreplaceRegexMatch.substitution is requiredreplaceRegexMatch.patternis invalid RE2 regexreplaceRegexMatch.pattern is not a valid RE2 regular expression: <error>queryRewrite.rulesmissing or emptyqueryRewrite.rules must contain at least one rulequeryRewrite.rules[].actionmissingqueryRewrite.rules[].action is requiredqueryRewrite.rules[].namemissingqueryRewrite.rules[].name is requiredqueryRewrite.rules[].actionisReplace,Add, orAppendbutvaluemissingqueryRewrite.rules[].value is required for Replace, Add, and AppendqueryRewrite.rules[].actionisReplaceRegexMatchbutpatternmissingqueryRewrite.rules[].pattern is required for ReplaceRegexMatchqueryRewrite.rules[].actionisReplaceRegexMatchbutsubstitutionmissingqueryRewrite.rules[].substitution is required for ReplaceRegexMatchmethodRewriteis not a valid HTTP methodmethodRewrite must be one of: GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONSImplementation Notes
Lua Script Delivery
The method rewrite Lua script is delivered via xDS as an inline string:
inline_codeWhy inline over file mount:
Dynamic Metadata Key
The Policy Engine sets the target method in dynamic metadata:
api_platform.policy_engine.envoy.filters.http.ext_procrequest_transformation.target_methodPOST)Error Handling
Beta Was this translation helpful? Give feedback.
All reactions