-
Notifications
You must be signed in to change notification settings - Fork 36
feat: Add a rust plugin to add geo query string to URL #294
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
Open
mateustd-ciandt
wants to merge
10
commits into
GoogleCloudPlatform:main
Choose a base branch
from
mateustd-ciandt:rust-plugin-add-geo-query
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ce18442
feat: Add a rust plugin to add geo query string to URL
mateustd-ciandt 84cd3cd
Merge branch 'main' into rust-plugin-add-geo-query
mateustd-ciandt 5c22028
using getProperty to get client_region for add_geo_query rust plugin
mateustd-ciandt 13ca09b
Merge branch 'main' into rust-plugin-add-geo-query
mateustd-ciandt 12b870e
Updating the plugin with aux functions and better optimization
mateustd-ciandt 99e09d2
Updating copyright year to 2026
mateustd-ciandt 323fa9c
Merge branch 'main' into rust-plugin-add-geo-query
mateustd-ciandt 5a5edce
Updating tests.textpb for add geo query in rust
mateustd-ciandt 94d1df1
Merge branch 'rust-plugin-add-geo-query' of https://github.com/mateus…
mateustd-ciandt 781bf64
Merge branch 'main' into rust-plugin-add-geo-query
mateustd-ciandt 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| load("//:plugins.bzl", "proxy_wasm_plugin_rust", "proxy_wasm_tests") | ||
|
|
||
| licenses(["notice"]) # Apache 2 | ||
|
|
||
| proxy_wasm_plugin_rust( | ||
| name = "plugin_rust.wasm", | ||
| srcs = ["plugin.rs"], | ||
| deps = [ | ||
| "//bazel/cargo/remote:log", | ||
| "//bazel/cargo/remote:proxy-wasm", | ||
| ], | ||
| ) | ||
|
|
||
| proxy_wasm_tests( | ||
| name = "tests", | ||
| plugins = [ | ||
| ":plugin_rust.wasm", | ||
| ], | ||
| tests = ":tests.textpb", | ||
| ) |
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 |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // [START serviceextensions_plugin_country_query] | ||
| use proxy_wasm::traits::{Context, HttpContext}; | ||
| use proxy_wasm::types::{Action, LogLevel}; | ||
|
|
||
| const CLIENT_REGION_PATH: &[&str] = &["request", "client_region"]; | ||
| const DEFAULT_COUNTRY: &str = "unknown"; | ||
|
|
||
| proxy_wasm::main! {{ | ||
| proxy_wasm::set_log_level(LogLevel::Trace); | ||
| proxy_wasm::set_http_context(|_, _| -> Box<dyn HttpContext> { Box::new(MyHttpContext) }); | ||
| }} | ||
|
|
||
| struct MyHttpContext; | ||
|
|
||
| impl Context for MyHttpContext {} | ||
|
|
||
| impl HttpContext for MyHttpContext { | ||
| fn on_http_request_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action { | ||
| let country_value = self.get_country_value(); | ||
|
|
||
| log::info!("country: {}", country_value); | ||
|
|
||
| let path = self.get_http_request_header(":path").unwrap_or_default(); | ||
| let new_path = self.add_country_parameter(&path, &country_value); | ||
|
|
||
| self.set_http_request_header(":path", Some(&new_path)); | ||
|
|
||
| Action::Continue | ||
| } | ||
| } | ||
|
|
||
| impl MyHttpContext { | ||
| fn get_country_value(&self) -> String { | ||
| if let Some(bytes) = self.get_property(CLIENT_REGION_PATH.to_vec()) { | ||
| if let Ok(country) = String::from_utf8(bytes) { | ||
| if !country.is_empty() { | ||
| return country; | ||
| } | ||
| } | ||
| } | ||
| DEFAULT_COUNTRY.to_string() | ||
| } | ||
|
|
||
| fn add_country_parameter(&self, path: &str, country: &str) -> String { | ||
| let mut new_path = String::with_capacity(path.len() + country.len() + 10); | ||
| new_path.push_str(path); | ||
|
|
||
| if path.contains('?') { | ||
| new_path.push_str("&country="); | ||
| } else { | ||
| new_path.push_str("?country="); | ||
| } | ||
| new_path.push_str(country); | ||
|
|
||
| new_path | ||
| } | ||
| } | ||
| // [END serviceextensions_plugin_country_query] | ||
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 |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| # Test basic country parameter addition using request.client_region | ||
| test { | ||
| name: "AddCountryParameterWithClientRegion" | ||
| properties { | ||
| path: "request.client_region" | ||
| value: "BR" | ||
| } | ||
| request_headers { | ||
| input { | ||
| header { key: ":path" value: "/api/data" } | ||
| } | ||
| result { | ||
| has_header { key: ":path" value: "/api/data?country=BR" } | ||
| log { regex: ".*country: BR.*" } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| # Test fallback to unknown when client_region property is missing | ||
| test { | ||
| name: "FallbackToUnknownWhenNoClientRegion" | ||
| request_headers { | ||
| input { | ||
| header { key: ":path" value: "/api/data" } | ||
| } | ||
| result { | ||
| has_header { key: ":path" value: "/api/data?country=unknown" } | ||
| log { regex: ".*country: unknown.*" } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| # Test fallback to unknown when client_region is present but empty | ||
| test { | ||
| name: "FallbackToUnknownWhenClientRegionEmpty" | ||
| properties { | ||
| path: "request.client_region" | ||
| value: "" | ||
| } | ||
| request_headers { | ||
| input { | ||
| header { key: ":path" value: "/api/data" } | ||
| } | ||
| result { | ||
| has_header { key: ":path" value: "/api/data?country=unknown" } | ||
| log { regex: ".*country: unknown.*" } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| # Test appending to existing query string | ||
| test { | ||
| name: "AppendCountryToExistingQueryString" | ||
| properties { | ||
| path: "request.client_region" | ||
| value: "DE" | ||
| } | ||
| request_headers { | ||
| input { | ||
| header { key: ":path" value: "/api/data?page=1&limit=10" } | ||
| } | ||
| result { | ||
| headers { regex: ":path: /api/data\\?.*country=DE.*" } | ||
| log { regex: ".*country: DE.*" } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| # Test root path without query string | ||
| test { | ||
| name: "AddCountryToRootPath" | ||
| properties { | ||
| path: "request.client_region" | ||
| value: "MX" | ||
| } | ||
| request_headers { | ||
| input { | ||
| header { key: ":path" value: "/" } | ||
| } | ||
| result { | ||
| has_header { key: ":path" value: "/?country=MX" } | ||
| log { regex: ".*country: MX.*" } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| # Test special characters | ||
| test { | ||
| name: "HandleClientRegionCountryCodeUK" | ||
| properties { | ||
| path: "request.client_region" | ||
| value: "UK" | ||
| } | ||
| request_headers { | ||
| input { | ||
| header { key: ":path" value: "/api/data" } | ||
| } | ||
| result { | ||
| has_header { key: ":path" value: "/api/data?country=UK" } | ||
| log { regex: ".*country: UK.*" } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| # Test empty path scenario | ||
| test { | ||
| name: "HandleEmptyPath" | ||
| properties { | ||
| path: "request.client_region" | ||
| value: "AU" | ||
| } | ||
| request_headers { | ||
| input { | ||
| header { key: ":path" value: "" } | ||
| } | ||
| result { | ||
| has_header { key: ":path" value: "?country=AU" } | ||
| log { regex: ".*country: AU.*" } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| # Test anti-spoofing: replaces existing country parameter | ||
| test { | ||
| name: "RemoveExistingCountryParameter" | ||
| properties { | ||
| path: "request.client_region" | ||
| value: "US" | ||
| } | ||
| request_headers { | ||
| input { | ||
| header { key: ":path" value: "/api/data?country=SPOOFED&page=1" } | ||
| } | ||
| result { | ||
| headers { regex: ":path: /api/data\\?.*country=US.*" } | ||
| log { regex: ".*country: US.*" } | ||
| } | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is more idiomatic to adjust this to be:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Mike! I believe in Rust
[&str]doesn't compile because unsized types cannot be used inconst. So as far as I know, we need either&[&str](reference to slice) or[&str; 2](array with explicit size).