-
Notifications
You must be signed in to change notification settings - Fork 0
[P0] /v1/me via configured API origin (CLI audit P0-4) #50
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d4d3ac0
fix(tui): fetch /v1/me via configured api origin
echobt 301d16f
test(engine): assert /v1/me stays on the loopback port
echobt c23219c
test: treat wiremock localhost and 127.0.0.1 as one origin
echobt 08d79ca
refactor(tui): drop unused me_profile_request_url helper
echobt 12b78dc
style(tui): use question-mark in me profile spawn
echobt 443cec1
refactor: extract /v1/me to keep file-length caps
echobt 3d17806
fix(cli): cover /v1/me body timeout and cortex home
echobt 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.
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
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,190 @@ | ||
| //! `cortex whoami` talks to `GET /v1/me` on the configured API origin. | ||
| //! | ||
| //! These tests prove a staging/loopback `CORTEX_API_URL` is the only host | ||
| //! contacted. Nothing here reaches `api.cortex.foundation`. | ||
|
|
||
| use std::process::Command; | ||
|
|
||
| fn whoami(home: &std::path::Path, api_url: &str, token: Option<&str>) -> std::process::Output { | ||
| whoami_with_homes(home, home, api_url, token) | ||
| } | ||
|
|
||
| fn whoami_with_homes( | ||
| home: &std::path::Path, | ||
| cortex_home: &std::path::Path, | ||
| api_url: &str, | ||
| token: Option<&str>, | ||
| ) -> std::process::Output { | ||
| let mut command = Command::new(env!("CARGO_BIN_EXE_Cortex")); | ||
| command | ||
| .arg("whoami") | ||
| .env("HOME", home) | ||
| .env("CORTEX_HOME", cortex_home) | ||
| .env("CORTEX_API_URL", api_url) | ||
| .env("RUST_LOG", "off") | ||
| .env("NO_COLOR", "1") | ||
| .env_remove("CORTEX_API_KEY") | ||
| .current_dir(cortex_home); | ||
| match token { | ||
| Some(token) => { | ||
| command.env("CORTEX_AUTH_TOKEN", token); | ||
| } | ||
| None => { | ||
| command.env_remove("CORTEX_AUTH_TOKEN"); | ||
| } | ||
| } | ||
| command.output().unwrap() | ||
| } | ||
|
|
||
| fn combined(output: &std::process::Output) -> String { | ||
| format!( | ||
| "{}{}", | ||
| String::from_utf8_lossy(&output.stdout), | ||
| String::from_utf8_lossy(&output.stderr) | ||
| ) | ||
| } | ||
|
|
||
| #[tokio::test(flavor = "multi_thread")] | ||
| async fn whoami_hits_v1_me_on_configured_origin_never_production() { | ||
| let server = wiremock::MockServer::start().await; | ||
| wiremock::Mock::given(wiremock::matchers::method("GET")) | ||
| .and(wiremock::matchers::path("/v1/me")) | ||
| .respond_with( | ||
| wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({ | ||
| "name": "Ada Lovelace", | ||
| "email": "ada@example.com", | ||
| "organizations": [{ "org_name": "Analytical Engines" }] | ||
| })), | ||
| ) | ||
| .expect(1) | ||
| .mount(&server) | ||
| .await; | ||
| wiremock::Mock::given(wiremock::matchers::method("GET")) | ||
| .and(wiremock::matchers::path("/auth/me")) | ||
| .respond_with(wiremock::ResponseTemplate::new(599)) | ||
| .expect(0) | ||
| .mount(&server) | ||
| .await; | ||
|
|
||
| let home = tempfile::tempdir().unwrap(); | ||
| let output = whoami(home.path(), &server.uri(), Some("staging-bearer")); | ||
| assert!( | ||
| output.status.success(), | ||
| "whoami against a 200 fixture must succeed: {}", | ||
| combined(&output) | ||
| ); | ||
| let text = combined(&output); | ||
| assert!( | ||
| text.contains("Ada Lovelace"), | ||
| "live /v1/me identity should be printed: {text}" | ||
| ); | ||
| assert!( | ||
| !text.contains("api.cortex.foundation"), | ||
| "production host must not appear: {text}" | ||
| ); | ||
|
|
||
| let requests = server.received_requests().await.expect("recorded requests"); | ||
| assert_eq!(requests.len(), 1); | ||
| assert_eq!(requests[0].url.path(), "/v1/me"); | ||
| let request_url = requests[0].url.to_string(); | ||
| assert!( | ||
| !request_url.contains("api.cortex.foundation"), | ||
| "production host must not be contacted: {request_url}" | ||
| ); | ||
| assert!( | ||
| request_url.contains("127.0.0.1") || request_url.contains("localhost"), | ||
| "request must stay on the loopback fixture: {request_url}" | ||
| ); | ||
| } | ||
|
|
||
| #[tokio::test(flavor = "multi_thread")] | ||
| async fn whoami_401_prints_cortex_login_and_exits_nonzero() { | ||
| let server = wiremock::MockServer::start().await; | ||
| wiremock::Mock::given(wiremock::matchers::method("GET")) | ||
| .and(wiremock::matchers::path("/v1/me")) | ||
| .respond_with(wiremock::ResponseTemplate::new(401)) | ||
| .expect(1) | ||
| .mount(&server) | ||
| .await; | ||
|
|
||
| let home = tempfile::tempdir().unwrap(); | ||
| let output = whoami(home.path(), &server.uri(), Some("revoked-token")); | ||
| assert!( | ||
| !output.status.success(), | ||
| "a revoked token must fail whoami; got {}: {}", | ||
| output.status, | ||
| combined(&output) | ||
| ); | ||
| let text = combined(&output); | ||
| assert!( | ||
| text.contains("cortex login"), | ||
| "401 must print the login recovery copy: {text}" | ||
| ); | ||
| assert!(!text.to_lowercase().contains("reqwest"), "{text}"); | ||
| assert!(!text.contains("api.cortex.foundation"), "{text}"); | ||
|
|
||
| let requests = server.received_requests().await.expect("recorded requests"); | ||
| assert_eq!(requests.len(), 1); | ||
| assert_eq!(requests[0].url.path(), "/v1/me"); | ||
| } | ||
|
|
||
| #[tokio::test(flavor = "multi_thread")] | ||
| async fn whoami_isolated_cortex_home_does_not_use_default_profile_token() { | ||
| let server = wiremock::MockServer::start().await; | ||
| wiremock::Mock::given(wiremock::matchers::method("GET")) | ||
| .and(wiremock::matchers::path("/v1/me")) | ||
| .respond_with( | ||
| wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({ | ||
| "name": "Default Profile" | ||
| })), | ||
| ) | ||
| .expect(0) | ||
| .mount(&server) | ||
| .await; | ||
|
|
||
| let default_home = tempfile::tempdir().unwrap(); | ||
| let isolated = tempfile::tempdir().unwrap(); | ||
| let default_cortex = default_home.path().join(".cortex"); | ||
| std::fs::create_dir_all(&default_cortex).unwrap(); | ||
| std::fs::write( | ||
| default_cortex.join("auth.json"), | ||
| r#"{"mode":"ApiKey","api_key":"home-profile-bearer"}"#, | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| let output = whoami_with_homes(default_home.path(), isolated.path(), &server.uri(), None); | ||
| assert!( | ||
| !output.status.success(), | ||
| "empty CORTEX_HOME must not inherit $HOME/.cortex: {}", | ||
| combined(&output) | ||
| ); | ||
| let text = combined(&output); | ||
| assert!( | ||
| text.contains("cortex login") || text.contains("CORTEX_API_KEY"), | ||
| "{text}" | ||
| ); | ||
| assert!(!text.contains("Default Profile"), "{text}"); | ||
| let requests = server.received_requests().await.expect("recorded requests"); | ||
| assert!( | ||
| requests.is_empty(), | ||
| "default-profile bearer must not reach /v1/me: {}", | ||
| requests.len() | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn whoami_without_credentials_prints_login_copy_and_does_not_need_the_network() { | ||
| let home = tempfile::tempdir().unwrap(); | ||
| let output = whoami(home.path(), "http://127.0.0.1:1", None); | ||
| assert!( | ||
| !output.status.success(), | ||
| "no credential is not a successful whoami: {}", | ||
| combined(&output) | ||
| ); | ||
| let text = combined(&output); | ||
| assert!( | ||
| text.contains("cortex login") || text.contains("CORTEX_API_KEY"), | ||
| "{text}" | ||
| ); | ||
| assert!(!text.contains("api.cortex.foundation"), "{text}"); | ||
| } | ||
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
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.