From 8164defb1cbe3a21071c99a53651757679114cef Mon Sep 17 00:00:00 2001 From: Joseph Montanaro Date: Wed, 25 Nov 2026 11:38:36 -0500 Subject: [PATCH] ore tests; determine session-ness of credentials purely from user's response --- src/credentials/aws.rs | 2 +- src/srv/creddy_server.rs | 118 ++++++++++++++++++++++++++++++++------- 2 files changed, 100 insertions(+), 20 deletions(-) diff --git a/src/credentials/aws.rs b/src/credentials/aws.rs index 16aa4f3..1eef0db 100644 --- a/src/credentials/aws.rs +++ b/src/credentials/aws.rs @@ -30,7 +30,7 @@ pub struct AwsSessionCredential { #[derive(Default)] pub struct AwsSessionCache { - sessions: HashMap, + pub sessions: HashMap, } impl AwsSessionCache { diff --git a/src/srv/creddy_server.rs b/src/srv/creddy_server.rs index 4008487..49047a0 100644 --- a/src/srv/creddy_server.rs +++ b/src/srv/creddy_server.rs @@ -167,17 +167,21 @@ async fn handle_aws_get(uri: Uri, app: AsyncApp) -> Result> // it's possible to approve a request while the app is still locked, so we have to account for that AppSession::wait_for_unlock(&app).await; - let name = name.unwrap_or_else(|| "default".into()); let creds = app .read_global::(|session, _cx| { let AppSession::Unlocked(db) = session else { unreachable!("App re-locked immediately after unlock") }; - db.load_aws_credential(&name) + match &name { + Some(name) => db.load_aws_credential(name), + None => db.load_aws_default(), + } })? - .ok_or(CreddyError::CredentialNotFound(name))?; + .ok_or_else(|| CreddyError::CredentialNotFound(name.unwrap_or_else(|| "default".into())))?; - let body = if session { + // regardless of what was requested, the user's response + // determines what type of credentials are issued + let body = if resp.session { let session_creds = AwsSessionCache::get_session(&creds, &app).await?; serde_json::to_vec(&session_creds).unwrap() } else { @@ -212,6 +216,61 @@ mod tests { Ok(resp) } + /// Unlock the session, and stand in for the user by approving requests as they arrive. + fn unlock_and_approve(env: &TestEnv, cx: &mut TestAppContext, session: bool) -> TestResult { + let db = env.db()?; + cx.update(|cx| { + cx.set_global(AppSession::Unlocked(db)); + cx.set_global(AwsSessionCache::default()); + cx.set_global(PendingRequests::new()); + cx.observe_global::(move |cx| { + // popping notifies this observer again, so make sure that stops when the queue is empty + if cx.global::().0.is_empty() { + return; + } + let (_, sender) = cx.global_mut::().0.pop_front().unwrap(); + let resp = CredentialResponse { approved: true, session }; + let _ = sender.send(resp); + }) + .detach(); + }); + Ok(()) + } + + /// Save a long-term credential, along with a cached session for it so nothing has to talk to AWS. + fn save_cred(name: &str, is_default: bool, cx: &mut TestAppContext) -> (AwsCredential, AwsSessionCredential) { + let cred = AwsCredential { + id: uuid::Uuid::new_v4(), + name: name.into(), + access_key_id: format!("AKIA{name}"), + secret_access_key: "EXAMPLESECRET".into(), + is_default, + }; + let session = AwsSessionCredential { + version: 1, + access_key_id: format!("ASIA{name}"), + secret_access_key: "SESSIONSECRET".into(), + session_token: "TOKEN".into(), + expiration: "2100-01-01T00:00:00Z".parse().unwrap(), + }; + cx.update(|cx| { + let AppSession::Unlocked(db) = cx.global::() else { + panic!("locked") + }; + db.save_aws_credential(&cred).unwrap(); + cx.global_mut::() + .sessions + .insert(cred.access_key_id.clone(), session.clone()); + }); + (cred, session) + } + + async fn aws_get(uri: &'static str, cx: &TestAppContext) -> TestResult { + let resp = handle_aws_get(Uri::from_static(uri), cx.to_async()).await?; + let body = resp.into_body().collect().await?.to_bytes(); + Ok(serde_json::from_slice(&body)?) + } + #[gpui_kit::test] async fn test_start_server(cx: &mut TestAppContext) -> TestResult { let env = TestEnv::new()?; @@ -232,21 +291,7 @@ mod tests { async fn test_aws_get_missing_credential(cx: &mut TestAppContext) -> TestResult { let env = TestEnv::new()?; cx.executor().allow_parking(); - let db = env.db()?; - cx.update(|cx| { - cx.set_global(AppSession::Unlocked(db)); - cx.set_global(PendingRequests::new()); - // stand in for the user, approving requests as they arrive - cx.observe_global::(|cx| { - // popping notifies this observer again, so make sure that stops when the queue is empty - if cx.global::().0.is_empty() { - return; - } - let (_, sender) = cx.global_mut::().0.pop_front().unwrap(); - let _ = sender.send(CredentialResponse::yes_session()); - }) - .detach(); - }); + unlock_and_approve(&env, cx, true)?; let addr = env.config.creddy_server_addr(); start_creddy_server(&addr, cx.to_async())?; @@ -259,4 +304,39 @@ mod tests { Ok(()) } + + #[gpui_kit::test] + async fn test_aws_get_default(cx: &mut TestAppContext) -> TestResult { + let env = TestEnv::new()?; + unlock_and_approve(&env, cx, true)?; + save_cred("other", false, cx); + let (_, session) = save_cred("work", true, cx); + + let returned: AwsSessionCredential = aws_get("/aws", cx).await?; + assert_eq!(returned, session); + Ok(()) + } + + #[gpui_kit::test] + async fn test_aws_get_named(cx: &mut TestAppContext) -> TestResult { + let env = TestEnv::new()?; + unlock_and_approve(&env, cx, true)?; + let (_, session) = save_cred("other", false, cx); + save_cred("work", true, cx); + + let returned: AwsSessionCredential = aws_get("/aws?name=other", cx).await?; + assert_eq!(returned, session); + Ok(()) + } + + #[gpui_kit::test] + async fn test_aws_get_longterm(cx: &mut TestAppContext) -> TestResult { + let env = TestEnv::new()?; + unlock_and_approve(&env, cx, false)?; + let (cred, _) = save_cred("work", true, cx); + + let returned: AwsCredential = aws_get("/aws?name=work&session=false", cx).await?; + assert_eq!(returned, cred); + Ok(()) + } }