Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 69 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions terraform/docs-rs/fastly-compute-docs-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ publish = false

[dependencies]
fastly = "0.12.1"
uuid = { version = "1.25.0", features = ["v4", "fast-rng"] }

12 changes: 11 additions & 1 deletion terraform/docs-rs/fastly-compute-docs-rs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use fastly::{
header::{CACHE_CONTROL, EXPIRES, STRICT_TRANSPORT_SECURITY},
},
};
use uuid::Uuid;

// Should match the backend name in terraform
const DOCS_RS_BACKEND: &str = "docs_rs_origin";
Expand All @@ -27,9 +28,10 @@ const NGWAF_WORKSPACE_KEY: &str = "ngwaf_workspace";

const FASTLY_CLIENT_IP: HeaderName = HeaderName::from_static("fastly-client-ip");
const SURROGATE_CONTROL: HeaderName = HeaderName::from_static("surrogate-control");
const X_ORIGIN_AUTH: HeaderName = HeaderName::from_static("x-origin-auth");
const X_COMPRESS_HINT: HeaderName = HeaderName::from_static("x-compress-hint");
const X_FORWARDED_HOST: HeaderName = HeaderName::from_static("x-forwarded-host");
const X_ORIGIN_AUTH: HeaderName = HeaderName::from_static("x-origin-auth");
const X_REQUEST_ID: HeaderName = HeaderName::from_static("x-request-id");

#[fastly::main]
fn main(mut req: Request) -> Result<Response, Error> {
Expand Down Expand Up @@ -82,6 +84,14 @@ fn main(mut req: Request) -> Result<Response, Error> {
}
}

// Generate the request ID at the POP receiving the client request. On
// shield POPs, preserve the ID set by the edge POP.
if shield.response_is_for_client() {
req.set_header(X_REQUEST_ID, Uuid::new_v4().to_string());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

did you consider using https://docs.rs/fastly/latest/fastly/struct.Request.html#method.get_client_request_id ?

Suggested change
req.set_header(X_REQUEST_ID, Uuid::new_v4().to_string());
let request_id = req
.get_client_request_id()
.context("this is the client request, it should have a request ID")?
.to_owned();
req.set_header(X_REQUEST_ID, request_id);

Disclaimer: finding found by AI

} else if !req.contains_header(&X_REQUEST_ID) {
eprintln!("shield POP received request without expected X-Request-ID header from edge POP");
}

if shield.target_is_origin() {
let origin_auth = secrets
.get(ORIGIN_AUTH_KEY)
Expand Down