-
Notifications
You must be signed in to change notification settings - Fork 0
feat(api): resolve export identity by idempotency key on loopback #465
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
Closed
seonghobae
wants to merge
4
commits into
feat/export-retrieval-get-gap-003a
from
feat/export-idempotency-lookup-get-gap-003a
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
515fd3b
feat(api): resolve export identity by idempotency key on loopback
seonghobae 79cb5d6
test(api): reproduce export lookup review defects
seonghobae e40b407
fix(api): make export idempotency lookup path-safe and metric-recursive
seonghobae 0fd64f7
fix(api): reject reserved export retrieval identities at construction
seonghobae 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 @@ | ||
| - `tepp_api` loopback `GET /v1/exports/by-idempotency/{idempotency_key}` returns the metric-free identity of the unique naruon export that used that key on `AnalysisRunLiveService`, so operators can jump from a 200 authorization receipt to `export_id` without scanning identities (ADR 0093). `NaruonLiveService` stays POST-only. LineageWeave is refused. Not GET-by-id, not collection GET, not stored-request GET, not analysis-run lookup, not cancel, not GAP-010 Figma/export, not persistence. |
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 |
|---|---|---|
|
|
@@ -3,7 +3,8 @@ | |
| //! This module keeps the Naruon compatibility listener intact while providing | ||
| //! the shared `/v1/analysis-runs` and cutoff-safe `/v1/temporal-context` | ||
| //! boundaries needed by Naruon and `LineageWeave`. Naruon may also POST and | ||
| //! GET `/v1/exports/{export_id}` for metric-free purpose-bound retrieval. | ||
| //! GET `/v1/exports/{export_id}` for metric-free purpose-bound retrieval | ||
| //! and `GET /v1/exports/by-idempotency/{idempotency_key}` for key lookup. | ||
| //! It accepts transport acknowledgements, temporal evidence context, and | ||
| //! export identities only; completed psychometric results remain outside this | ||
| //! crate. | ||
|
|
@@ -13,6 +14,10 @@ use std::io::Write; | |
| use std::net::{SocketAddr, TcpListener}; | ||
|
|
||
| use crate::export_http::{export_retrieval_path_id, refuse_metrics_on_export_retrieval_payload}; | ||
| use crate::export_idempotency_lookup_http::{ | ||
| ExportIdempotencyLookup, export_idempotency_lookup_path_key, | ||
| refuse_metrics_on_export_idempotency_lookup_payload, | ||
| }; | ||
| use crate::lineageweave_http::{ | ||
| LINEAGEWEAVE_CONSUMER_CODE, NARUON_CONSUMER_CODE, consumer_is_supported, | ||
| }; | ||
|
|
@@ -162,6 +167,12 @@ impl AnalysisRunLiveService { | |
| let (method, path) = parse_request_line(lines.next().unwrap_or(""))?; | ||
| let headers = parse_headers(&mut lines)?; | ||
| if method == "GET" { | ||
| if matches!( | ||
| export_idempotency_lookup_path_key(path), | ||
| Ok(_) | Err(ApiError::LimitExceeded) | ||
| ) { | ||
| return self.lookup_export_by_idempotency(path, &headers, body); | ||
| } | ||
| if matches!( | ||
| export_retrieval_path_id(path), | ||
| Ok(_) | Err(ApiError::LimitExceeded) | ||
|
|
@@ -342,6 +353,45 @@ impl AnalysisRunLiveService { | |
| Ok(json_response(200, "OK", response_body)) | ||
| } | ||
|
|
||
| fn lookup_export_by_idempotency( | ||
| &self, | ||
| path: &str, | ||
| headers: &HashMap<String, String>, | ||
| body: &str, | ||
| ) -> Result<NaruonLiveResponse, ApiError> { | ||
| let idempotency_key = export_idempotency_lookup_path_key(path)?; | ||
| if !body.trim().is_empty() { | ||
| return Err(ApiError::InvalidWirePayload); | ||
| } | ||
| let consumer = require_headers(headers, self.bound_addr, false)?; | ||
| if consumer != NARUON_CONSUMER_CODE { | ||
| return Err(ApiError::InvalidWirePayload); | ||
| } | ||
| refuse_metrics_on_export_idempotency_lookup_payload(body)?; | ||
| let prefix = format!("{consumer}\u{1f}"); | ||
| let mut matches: Vec<&StoredExport> = self | ||
| .authorized_exports | ||
| .iter() | ||
| .filter(|(replay_key, stored)| { | ||
| replay_key.starts_with(&prefix) | ||
| && stored.retrieval.idempotency_key == idempotency_key | ||
| }) | ||
| .map(|(_, stored)| stored) | ||
| .collect(); | ||
| if matches.len() != 1 { | ||
| return Err(ApiError::InvalidWirePayload); | ||
| } | ||
| let stored = matches.remove(0); | ||
|
Comment on lines
+371
to
+384
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| let payload = ExportIdempotencyLookup::new( | ||
| stored.retrieval.export_id.clone(), | ||
| stored.retrieval.decision_code.clone(), | ||
| stored.retrieval.idempotency_key.clone(), | ||
| )?; | ||
| let response_body = payload.to_json()?; | ||
| refuse_metrics_on_export_idempotency_lookup_payload(&response_body)?; | ||
| Ok(json_response(200, "OK", response_body)) | ||
| } | ||
|
|
||
| fn response_from_error(&mut self, error: ApiError) -> NaruonLiveResponse { | ||
| let request_id = format!("analysis-run-live-{}", self.next_request_serial); | ||
| self.next_request_serial += 1; | ||
|
|
@@ -1202,6 +1252,77 @@ mod tests { | |
| 400 | ||
| ); | ||
|
|
||
| let looked_up = service.handle_http_request(&export_lookup_http( | ||
| "export-idem-1", | ||
| NARUON_CONSUMER_CODE, | ||
| )); | ||
| assert_eq!(looked_up.status_code, 200); | ||
| let lookup = crate::ExportIdempotencyLookup::from_json(&looked_up.body).expect("lookup"); | ||
| assert_eq!(lookup.export_id, retrieval.export_id); | ||
| assert_eq!(lookup.idempotency_key, "export-idem-1"); | ||
| assert_eq!(lookup.decision_code, "purpose_bound_export_allowed"); | ||
| assert!(!looked_up.body.contains("tenant_workspace_id")); | ||
| assert!(!looked_up.body.contains("principal_id")); | ||
| assert!(!looked_up.body.contains("includes_source_text")); | ||
| assert!(!looked_up.body.contains("scientific_acceptance")); | ||
| assert!(!looked_up.body.contains("rmse")); | ||
| assert_eq!( | ||
| service | ||
| .handle_http_request(&export_lookup_http( | ||
| "export-idem-1", | ||
| LINEAGEWEAVE_CONSUMER_CODE | ||
| )) | ||
| .status_code, | ||
| 400 | ||
| ); | ||
| assert_eq!( | ||
| service | ||
| .handle_http_request(&export_lookup_http("missing-key", NARUON_CONSUMER_CODE)) | ||
| .status_code, | ||
| 400 | ||
| ); | ||
| assert_eq!( | ||
| service | ||
| .handle_http_request(&export_lookup_body_http( | ||
| "export-idem-1", | ||
| NARUON_CONSUMER_CODE, | ||
| "{}", | ||
| )) | ||
| .status_code, | ||
| 400 | ||
| ); | ||
| assert_eq!( | ||
| service | ||
| .handle_http_request(&export_lookup_post_http( | ||
| "export-idem-1", | ||
| NARUON_CONSUMER_CODE, | ||
| )) | ||
| .status_code, | ||
| 400 | ||
| ); | ||
| assert_eq!( | ||
| service | ||
| .handle_http_request(&export_get_http("by-idempotency", NARUON_CONSUMER_CODE)) | ||
| .status_code, | ||
| 400 | ||
| ); | ||
|
|
||
| let mut other_tenant = request.clone(); | ||
| other_tenant.tenant_workspace_id = "export-live-tenant-b".into(); | ||
| let other_body = crate::wire::to_json(&other_tenant).expect("other json"); | ||
| let other_posted = service.handle_http_request(&export_post_http( | ||
| &other_body, | ||
| NARUON_CONSUMER_CODE, | ||
| "export-idem-1", | ||
| )); | ||
| assert_eq!(other_posted.status_code, 200); | ||
| assert_eq!( | ||
| service | ||
| .handle_http_request(&export_lookup_http("export-idem-1", NARUON_CONSUMER_CODE)) | ||
| .status_code, | ||
| 400 | ||
| ); | ||
|
|
||
| let principal_as_key = service.handle_http_request(&export_post_http( | ||
| &body, | ||
| NARUON_CONSUMER_CODE, | ||
|
|
@@ -1233,6 +1354,23 @@ mod tests { | |
| ) | ||
| } | ||
|
|
||
| fn export_lookup_http(idempotency_key: &str, consumer: &str) -> String { | ||
| export_lookup_body_http(idempotency_key, consumer, "") | ||
| } | ||
|
|
||
| fn export_lookup_body_http(idempotency_key: &str, consumer: &str, body: &str) -> String { | ||
| format!( | ||
| "GET {NARUON_EXPORT_PATH}/by-idempotency/{idempotency_key} HTTP/1.1\r\nHost: 127.0.0.1\r\ncontent-type: application/json\r\ntepp-consumer: {consumer}\r\ntepp-contract-version: 1\r\ncontent-length: {}\r\n\r\n{body}", | ||
| body.len() | ||
| ) | ||
| } | ||
|
|
||
| fn export_lookup_post_http(idempotency_key: &str, consumer: &str) -> String { | ||
| format!( | ||
| "POST {NARUON_EXPORT_PATH}/by-idempotency/{idempotency_key} HTTP/1.1\r\nHost: 127.0.0.1\r\ncontent-type: application/json\r\ntepp-consumer: {consumer}\r\ntepp-contract-version: 1\r\nidempotency-key: {idempotency_key}\r\ncontent-length: 0\r\n\r\n" | ||
| ) | ||
| } | ||
|
|
||
| struct ScriptedRead { | ||
| reader: Cursor<Vec<u8>>, | ||
| first_error: Option<std::io::ErrorKind>, | ||
|
|
||
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
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.
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.
📝 Info: Oversized keys retain limit status
Routing
LimitExceededinto the lookup handler preserves the documented 413 response instead of converting oversized keys to malformed-path errors.Was this helpful? React with 👍 or 👎 to provide feedback.