ore tests; determine session-ness of credentials purely from user's response
This commit is contained in:
1 parent
fe9afa2ce0
commit
8164defb1c
2 files changed
+100
-20
No files matched your search
@@ -30,7 +30,7 @@ pub struct AwsSessionCredential {
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct AwsSessionCache {
|
||||
sessions: HashMap<String, AwsSessionCredential>,
|
||||
pub sessions: HashMap<String, AwsSessionCredential>,
|
||||
}
|
||||
|
||||
impl AwsSessionCache {
|
||||
|
||||
+99
-19
@@ -167,17 +167,21 @@ async fn handle_aws_get(uri: Uri, app: AsyncApp) -> Result<Response<Full<Bytes>>
|
||||
|
||||
// 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::<AppSession, _>(|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::<PendingRequests>(move |cx| {
|
||||
// popping notifies this observer again, so make sure that stops when the queue is empty
|
||||
if cx.global::<PendingRequests>().0.is_empty() {
|
||||
return;
|
||||
}
|
||||
let (_, sender) = cx.global_mut::<PendingRequests>().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::<AppSession>() else {
|
||||
panic!("locked")
|
||||
};
|
||||
db.save_aws_credential(&cred).unwrap();
|
||||
cx.global_mut::<AwsSessionCache>()
|
||||
.sessions
|
||||
.insert(cred.access_key_id.clone(), session.clone());
|
||||
});
|
||||
(cred, session)
|
||||
}
|
||||
|
||||
async fn aws_get<T: serde::de::DeserializeOwned>(uri: &'static str, cx: &TestAppContext) -> TestResult<T> {
|
||||
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::<PendingRequests>(|cx| {
|
||||
// popping notifies this observer again, so make sure that stops when the queue is empty
|
||||
if cx.global::<PendingRequests>().0.is_empty() {
|
||||
return;
|
||||
}
|
||||
let (_, sender) = cx.global_mut::<PendingRequests>().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(())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user