mirror of
https://github.com/openai/codex.git
synced 2026-09-05 15:18:41 +00:00
[codex-api] keep URL-scoped auth hooks protocol agnostic [ci changed_files]
This commit is contained in:
@@ -19,10 +19,7 @@ impl crate::auth::AuthProvider for RecordingAuthProvider {
|
||||
fn add_auth_headers(&self, _headers: &mut HeaderMap) {}
|
||||
|
||||
fn add_auth_headers_for_url(&self, _request_url: &str, headers: &mut HeaderMap) {
|
||||
headers.insert(
|
||||
"x-oai-is",
|
||||
HeaderValue::from_static("ois1.sent.nonce.ciphertext"),
|
||||
);
|
||||
headers.insert("x-test-state", HeaderValue::from_static("sent-state"));
|
||||
}
|
||||
|
||||
fn observe_response_headers(
|
||||
@@ -124,14 +121,11 @@ fn test_session() -> (
|
||||
HeaderMap,
|
||||
) {
|
||||
let mut request_headers = HeaderMap::new();
|
||||
request_headers.insert(
|
||||
"x-oai-is",
|
||||
HeaderValue::from_static("ois1.sent.nonce.ciphertext"),
|
||||
);
|
||||
request_headers.insert("x-test-state", HeaderValue::from_static("sent-state"));
|
||||
let mut response_headers = HeaderMap::new();
|
||||
response_headers.insert(
|
||||
"x-oai-is-update",
|
||||
HeaderValue::from_static("ois1.rotated.nonce.ciphertext"),
|
||||
"x-test-state-update",
|
||||
HeaderValue::from_static("rotated-state"),
|
||||
);
|
||||
let transport = RejectingTransport::new(response_headers.clone());
|
||||
let auth = Arc::new(RecordingAuthProvider::default());
|
||||
|
||||
@@ -309,10 +309,10 @@ mod tests {
|
||||
use wiremock::matchers::method;
|
||||
use wiremock::matchers::path;
|
||||
|
||||
const SENT_STATE: &str = "ois1.sent.nonce.ciphertext";
|
||||
const CREATED_STATE: &str = "ois1.created.nonce.ciphertext";
|
||||
const RETRY_STATE: &str = "ois1.retry.nonce.ciphertext";
|
||||
const FINALIZED_STATE: &str = "ois1.finalized.nonce.ciphertext";
|
||||
const SENT_STATE: &str = "sent-state";
|
||||
const CREATED_STATE: &str = "created-state";
|
||||
const RETRY_STATE: &str = "retry-state";
|
||||
const FINALIZED_STATE: &str = "finalized-state";
|
||||
type ObservedUpdate = (String, Option<String>);
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
@@ -331,7 +331,7 @@ mod tests {
|
||||
|
||||
fn add_auth_headers_for_url(&self, _request_url: &str, headers: &mut http::HeaderMap) {
|
||||
self.add_auth_headers(headers);
|
||||
headers.insert("x-oai-is", HeaderValue::from_static(SENT_STATE));
|
||||
headers.insert("x-test-state", HeaderValue::from_static(SENT_STATE));
|
||||
}
|
||||
|
||||
fn observe_response_headers(
|
||||
@@ -346,7 +346,7 @@ mod tests {
|
||||
.push((
|
||||
request_url.to_string(),
|
||||
response_headers
|
||||
.get("x-oai-is-update")
|
||||
.get("x-test-state-update")
|
||||
.and_then(|value| value.to_str().ok())
|
||||
.map(ToString::to_string),
|
||||
));
|
||||
@@ -367,7 +367,7 @@ mod tests {
|
||||
Mock::given(method("POST"))
|
||||
.and(path("/backend-api/files"))
|
||||
.and(header("chatgpt-account-id", "account_id"))
|
||||
.and(header("x-oai-is", SENT_STATE))
|
||||
.and(header("x-test-state", SENT_STATE))
|
||||
.and(body_json(serde_json::json!({
|
||||
"file_name": "hello.txt",
|
||||
"file_size": 5,
|
||||
@@ -375,7 +375,7 @@ mod tests {
|
||||
})))
|
||||
.respond_with(
|
||||
ResponseTemplate::new(200)
|
||||
.insert_header("x-oai-is-update", CREATED_STATE)
|
||||
.insert_header("x-test-state-update", CREATED_STATE)
|
||||
.set_body_json(serde_json::json!({"file_id": "file_123", "upload_url": format!("{}/upload/file_123", server.uri())})),
|
||||
)
|
||||
.mount(&server)
|
||||
@@ -391,18 +391,18 @@ mod tests {
|
||||
let download_url = format!("{}/download/file_123", server.uri());
|
||||
Mock::given(method("POST"))
|
||||
.and(path("/backend-api/files/file_123/uploaded"))
|
||||
.and(header("x-oai-is", SENT_STATE))
|
||||
.and(header("x-test-state", SENT_STATE))
|
||||
.respond_with(move |_request: &Request| {
|
||||
if finalize_attempts_responder.fetch_add(1, Ordering::SeqCst) == 0 {
|
||||
return ResponseTemplate::new(200)
|
||||
.insert_header("x-oai-is-update", RETRY_STATE)
|
||||
.insert_header("x-test-state-update", RETRY_STATE)
|
||||
.set_body_json(serde_json::json!({
|
||||
"status": "retry"
|
||||
}));
|
||||
}
|
||||
|
||||
ResponseTemplate::new(200)
|
||||
.insert_header("x-oai-is-update", FINALIZED_STATE)
|
||||
.insert_header("x-test-state-update", FINALIZED_STATE)
|
||||
.set_body_json(serde_json::json!({
|
||||
"status": "success",
|
||||
"download_url": download_url,
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
use crate::INTEGRITY_STATE_HEADER_NAME;
|
||||
use crate::INTEGRITY_STATE_UPDATE_HEADER_NAME;
|
||||
use http::Error as HttpError;
|
||||
use http::HeaderMap;
|
||||
use http::HeaderName;
|
||||
@@ -117,12 +115,11 @@ impl CodexRequestBuilder {
|
||||
|
||||
match self.builder.headers(headers).send().await {
|
||||
Ok(response) => {
|
||||
let headers = redacted_headers_for_logging(response.headers());
|
||||
tracing::debug!(
|
||||
method = %self.method,
|
||||
url = %self.url,
|
||||
status = %response.status(),
|
||||
headers = ?headers,
|
||||
headers = ?response.headers(),
|
||||
version = ?response.version(),
|
||||
"Request completed"
|
||||
);
|
||||
@@ -144,19 +141,6 @@ impl CodexRequestBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
fn redacted_headers_for_logging(headers: &HeaderMap) -> HeaderMap {
|
||||
let mut headers = headers.clone();
|
||||
for name in [
|
||||
INTEGRITY_STATE_HEADER_NAME,
|
||||
INTEGRITY_STATE_UPDATE_HEADER_NAME,
|
||||
] {
|
||||
if headers.contains_key(name) {
|
||||
headers.insert(name, HeaderValue::from_static("<redacted>"));
|
||||
}
|
||||
}
|
||||
headers
|
||||
}
|
||||
|
||||
struct HeaderMapInjector<'a>(&'a mut HeaderMap);
|
||||
|
||||
impl<'a> Injector for HeaderMapInjector<'a> {
|
||||
@@ -190,39 +174,10 @@ mod tests {
|
||||
use opentelemetry::trace::TracerProvider;
|
||||
use opentelemetry_sdk::propagation::TraceContextPropagator;
|
||||
use opentelemetry_sdk::trace::SdkTracerProvider;
|
||||
use pretty_assertions::assert_eq;
|
||||
use tracing::trace_span;
|
||||
use tracing_subscriber::layer::SubscriberExt;
|
||||
use tracing_subscriber::util::SubscriberInitExt;
|
||||
|
||||
#[test]
|
||||
fn redacted_headers_for_logging_hides_integrity_state() {
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
INTEGRITY_STATE_HEADER_NAME,
|
||||
HeaderValue::from_static("ois1.request.nonce.ciphertext"),
|
||||
);
|
||||
headers.insert(
|
||||
INTEGRITY_STATE_UPDATE_HEADER_NAME,
|
||||
HeaderValue::from_static("ois1.response.nonce.ciphertext"),
|
||||
);
|
||||
headers.insert("x-request-id", HeaderValue::from_static("request-id"));
|
||||
|
||||
let redacted = redacted_headers_for_logging(&headers);
|
||||
|
||||
let mut expected = HeaderMap::new();
|
||||
expected.insert(
|
||||
INTEGRITY_STATE_HEADER_NAME,
|
||||
HeaderValue::from_static("<redacted>"),
|
||||
);
|
||||
expected.insert(
|
||||
INTEGRITY_STATE_UPDATE_HEADER_NAME,
|
||||
HeaderValue::from_static("<redacted>"),
|
||||
);
|
||||
expected.insert("x-request-id", HeaderValue::from_static("request-id"));
|
||||
assert_eq!(redacted, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inject_trace_headers_uses_current_span_context() {
|
||||
global::set_text_map_propagator(TraceContextPropagator::new());
|
||||
|
||||
Reference in New Issue
Block a user