-
Notifications
You must be signed in to change notification settings - Fork 11
Add asset backend proxy pipeline through authenticated s3 and fastly io #668
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
ChristianPavilonis
wants to merge
18
commits into
main
Choose a base branch
from
feature/multi-backend-asset-proxy
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
18 commits
Select commit
Hold shift + click to select a range
6d2267e
Add multi-backend asset proxy routing
ChristianPavilonis 32661ce
Address asset proxy review feedback
ChristianPavilonis dedee10
Add S3 auth and Fastly IO asset routes
ChristianPavilonis 7714348
Accept env bools for image optimizer settings
ChristianPavilonis cb040a8
s3 debug
ChristianPavilonis f5da55a
Surface S3 origin errors before image optimization
ChristianPavilonis 56ecd5b
Fix asset proxy routing review issues
ChristianPavilonis 25eb535
Cache S3 credentials and remove debug listing
ChristianPavilonis 0bc4bbe
Prepare Image Optimizer metadata for EdgeZero extraction
ChristianPavilonis 82290c1
Address S3 and Image Optimizer review findings
ChristianPavilonis e1dd6c7
Bypass Fastly IO for SVG asset routes
ChristianPavilonis 244da72
Fix asset route rebase fallout
ChristianPavilonis e5093dc
Update integration test lockfile
ChristianPavilonis 8cfb33f
Remove duplicate EC cookie helpers
ChristianPavilonis f45d582
Add publisher origin Host header override (#748)
ChristianPavilonis d8dea15
Address asset proxy review findings
ChristianPavilonis 52d21e2
Stream asset proxy responses directly
ChristianPavilonis 60356d2
Address asset proxy review comments
ChristianPavilonis 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
|
|
@@ -27,8 +27,9 @@ | |
| use trusted_server_core::integrations::{IntegrationRegistry, ProxyDispatchInput}; | ||
| use trusted_server_core::platform::RuntimeServices; | ||
| use trusted_server_core::proxy::{ | ||
| handle_first_party_click, handle_first_party_proxy, handle_first_party_proxy_rebuild, | ||
| handle_first_party_proxy_sign, | ||
| handle_asset_proxy_request, handle_first_party_click, handle_first_party_proxy, | ||
| handle_first_party_proxy_rebuild, handle_first_party_proxy_sign, stream_asset_body, | ||
| AssetProxyCachePolicy, | ||
| }; | ||
| use trusted_server_core::publisher::{ | ||
| handle_publisher_request, handle_tsjs_dynamic, stream_publisher_body, PublisherResponse, | ||
|
|
@@ -74,7 +75,7 @@ | |
| } | ||
| }; | ||
| // lgtm[rust/cleartext-logging] | ||
| // `Settings` uses `Redacted<T>` for secrets, so this debug dump is redacted. | ||
Check failureCode scanning / CodeQL Cleartext logging of sensitive information High
This operation writes
settings.reject_placeholder_secrets() Error loading related location Loading This operation writes settings.validate_admin_handler_passwords() to a log file. |
||
| log::debug!("Settings {settings:?}"); | ||
|
|
||
| // Short-circuit the ja4 debug probe before finalize_response so that | ||
|
|
@@ -325,6 +326,9 @@ | |
| let path = req.get_path().to_string(); | ||
| let method = req.get_method().clone(); | ||
|
|
||
| let mut asset_cache_policy = AssetProxyCachePolicy::OriginControlled; | ||
| let mut should_finalize_ec = true; | ||
|
|
||
| // Match known routes and handle them | ||
| let (result, organic_route) = match (method, path.as_str()) { | ||
| // Serve the tsjs library | ||
|
|
@@ -387,18 +391,22 @@ | |
| } | ||
|
|
||
| // tsjs endpoints | ||
| (Method::GET, "/first-party/proxy") => { | ||
| (handle_first_party_proxy(settings, req).await, false) | ||
| } | ||
| (Method::GET, "/first-party/click") => { | ||
| (handle_first_party_click(settings, req).await, false) | ||
| } | ||
| (Method::GET, "/first-party/sign") | (Method::POST, "/first-party/sign") => { | ||
| (handle_first_party_proxy_sign(settings, req).await, false) | ||
| } | ||
| (Method::POST, "/first-party/proxy-rebuild") => { | ||
| (handle_first_party_proxy_rebuild(settings, req).await, false) | ||
| } | ||
| (Method::GET, "/first-party/proxy") => ( | ||
| handle_first_party_proxy(settings, runtime_services, req).await, | ||
| false, | ||
| ), | ||
| (Method::GET, "/first-party/click") => ( | ||
| handle_first_party_click(settings, runtime_services, req).await, | ||
| false, | ||
| ), | ||
| (Method::GET, "/first-party/sign") | (Method::POST, "/first-party/sign") => ( | ||
| handle_first_party_proxy_sign(settings, runtime_services, req).await, | ||
| false, | ||
| ), | ||
| (Method::POST, "/first-party/proxy-rebuild") => ( | ||
| handle_first_party_proxy_rebuild(settings, runtime_services, req).await, | ||
| false, | ||
| ), | ||
| (m, path) if integration_registry.has_route(&m, path) => { | ||
| let result = integration_registry | ||
| .handle_proxy(ProxyDispatchInput { | ||
|
|
@@ -407,6 +415,7 @@ | |
| settings, | ||
| kv: kv_graph.as_ref(), | ||
| ec_context: &mut ec_context, | ||
| services: runtime_services, | ||
| req, | ||
| }) | ||
| .await | ||
|
|
@@ -418,78 +427,121 @@ | |
| (result, true) | ||
| } | ||
|
|
||
| // No known route matched, proxy to publisher origin as fallback | ||
| _ => { | ||
| log::info!( | ||
| "No known route matched for path: {}, proxying to publisher origin", | ||
| path | ||
| ); | ||
| // No known route matched, proxy to an asset origin or publisher origin as fallback | ||
| (method, _) => { | ||
| let matched_asset_route = matches!(method, Method::GET | Method::HEAD) | ||
| .then(|| settings.asset_route_for_path(&path)) | ||
| .flatten(); | ||
|
|
||
| if let Some(asset_route) = matched_asset_route { | ||
| should_finalize_ec = false; | ||
| log::info!("No explicit route matched; proxying via configured asset route"); | ||
| let result = | ||
| match handle_asset_proxy_request(settings, runtime_services, req, asset_route) | ||
| .await | ||
| { | ||
| Ok(asset_response) => { | ||
| asset_cache_policy = asset_response.cache_policy(); | ||
| let (mut response, stream_body) = | ||
| asset_response.into_response_and_body(); | ||
| if let Some(body) = stream_body { | ||
| finalize_response(settings, geo_info.as_ref(), &mut response); | ||
| asset_cache_policy.apply_after_route_finalization(&mut response); | ||
|
|
||
| let mut streaming_body = response.stream_to_client(); | ||
| if let Err(err) = stream_asset_body(body, &mut streaming_body).await | ||
| { | ||
| log::error!("Asset streaming failed: {err:?}"); | ||
| drop(streaming_body); | ||
| } else if let Err(err) = streaming_body.finish() { | ||
| log::error!("Failed to finish asset streaming body: {err}"); | ||
| } | ||
|
|
||
| return Ok(RouteOutcome { | ||
| response: None, | ||
| pull_sync_context: None, | ||
| }); | ||
| } | ||
| Ok(response) | ||
| } | ||
| Err(e) => { | ||
| asset_cache_policy = AssetProxyCachePolicy::NoStorePrivate; | ||
| Err(e) | ||
| } | ||
| }; | ||
| (result, false) | ||
| } else { | ||
| log::info!( | ||
| "No known route matched for path: {}, proxying to publisher origin", | ||
| path | ||
| ); | ||
|
|
||
| match handle_publisher_request( | ||
| settings, | ||
| integration_registry, | ||
| runtime_services, | ||
| kv_graph.as_ref(), | ||
| &mut ec_context, | ||
| req, | ||
| ) { | ||
| Ok(PublisherResponse::Stream { | ||
| mut response, | ||
| body, | ||
| params, | ||
| }) => { | ||
| // Publisher fallback has multiple delivery modes. | ||
| // EC finalization is header-only, so it must happen before | ||
| // headers are committed on the streaming path. | ||
| ec_finalize_response( | ||
| settings, | ||
| &ec_context, | ||
| finalize_kv_graph.as_ref(), | ||
| partner_registry, | ||
| eids_cookie.as_deref(), | ||
| sharedid_cookie.as_deref(), | ||
| &mut response, | ||
| ); | ||
| finalize_response(settings, geo_info.as_ref(), &mut response); | ||
|
|
||
| let mut streaming_body = response.stream_to_client(); | ||
| let mut stream_succeeded = false; | ||
| if let Err(err) = stream_publisher_body( | ||
| match handle_publisher_request( | ||
| settings, | ||
| integration_registry, | ||
| runtime_services, | ||
| kv_graph.as_ref(), | ||
| &mut ec_context, | ||
| req, | ||
| ) { | ||
| Ok(PublisherResponse::Stream { | ||
| mut response, | ||
| body, | ||
| &mut streaming_body, | ||
| ¶ms, | ||
| settings, | ||
| integration_registry, | ||
| ) { | ||
| // Headers are already committed. Log and abort rather | ||
| // than trying to replace the response mid-stream. | ||
| log::error!("Streaming processing failed: {err:?}"); | ||
| drop(streaming_body); | ||
| } else if let Err(err) = streaming_body.finish() { | ||
| log::error!("Failed to finish streaming body: {err}"); | ||
| } else { | ||
| stream_succeeded = true; | ||
| params, | ||
| }) => { | ||
| // Publisher fallback has multiple delivery modes. | ||
| // EC finalization is header-only, so it must happen before | ||
| // headers are committed on the streaming path. | ||
| ec_finalize_response( | ||
| settings, | ||
| &ec_context, | ||
| finalize_kv_graph.as_ref(), | ||
| partner_registry, | ||
| eids_cookie.as_deref(), | ||
| sharedid_cookie.as_deref(), | ||
| &mut response, | ||
| ); | ||
| finalize_response(settings, geo_info.as_ref(), &mut response); | ||
|
|
||
| let mut streaming_body = response.stream_to_client(); | ||
| let mut stream_succeeded = false; | ||
| if let Err(err) = stream_publisher_body( | ||
| body, | ||
| &mut streaming_body, | ||
| ¶ms, | ||
| settings, | ||
| integration_registry, | ||
| ) { | ||
| // Headers are already committed. Log and abort rather | ||
| // than trying to replace the response mid-stream. | ||
| log::error!("Streaming processing failed: {err:?}"); | ||
| drop(streaming_body); | ||
| } else if let Err(err) = streaming_body.finish() { | ||
| log::error!("Failed to finish streaming body: {err}"); | ||
| } else { | ||
| stream_succeeded = true; | ||
| } | ||
|
|
||
| let pull_sync_context = if is_real_browser && stream_succeeded { | ||
| build_pull_sync_context(&ec_context) | ||
| } else { | ||
| None | ||
| }; | ||
|
|
||
| return Ok(RouteOutcome { | ||
| response: None, | ||
| pull_sync_context, | ||
| }); | ||
| } | ||
| Ok(PublisherResponse::PassThrough { mut response, body }) => { | ||
| response.set_body(body); | ||
| (Ok(response), true) | ||
| } | ||
| Ok(PublisherResponse::Buffered(response)) => (Ok(response), true), | ||
| Err(e) => { | ||
| log::error!("Failed to proxy to publisher origin: {:?}", e); | ||
| (Err(e), true) | ||
| } | ||
|
|
||
| let pull_sync_context = if is_real_browser && stream_succeeded { | ||
| build_pull_sync_context(&ec_context) | ||
| } else { | ||
| None | ||
| }; | ||
|
|
||
| return Ok(RouteOutcome { | ||
| response: None, | ||
| pull_sync_context, | ||
| }); | ||
| } | ||
| Ok(PublisherResponse::PassThrough { mut response, body }) => { | ||
| response.set_body(body); | ||
| (Ok(response), true) | ||
| } | ||
| Ok(PublisherResponse::Buffered(response)) => (Ok(response), true), | ||
| Err(e) => { | ||
| log::error!("Failed to proxy to publisher origin: {:?}", e); | ||
| (Err(e), true) | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -503,17 +555,20 @@ | |
| // Bot gate still suppresses generated EC writes and pull sync via | ||
| // `kv_graph = None`, but withdrawal finalization uses `finalize_kv_graph` | ||
| // so revocation tombstones are not lost for bot-classified clients. | ||
| ec_finalize_response( | ||
| settings, | ||
| &ec_context, | ||
| finalize_kv_graph.as_ref(), | ||
| partner_registry, | ||
| eids_cookie.as_deref(), | ||
| sharedid_cookie.as_deref(), | ||
| &mut response, | ||
| ); | ||
| if should_finalize_ec { | ||
| ec_finalize_response( | ||
| settings, | ||
| &ec_context, | ||
| finalize_kv_graph.as_ref(), | ||
| partner_registry, | ||
| eids_cookie.as_deref(), | ||
| sharedid_cookie.as_deref(), | ||
| &mut response, | ||
| ); | ||
| } | ||
|
|
||
| finalize_response(settings, geo_info.as_ref(), &mut response); | ||
| asset_cache_policy.apply_after_route_finalization(&mut response); | ||
|
|
||
| let pull_sync_context = if is_real_browser && organic_route && route_succeeded { | ||
| // Pull sync is intentionally refreshed only from successful organic | ||
|
|
||
Oops, something went wrong.
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.