add session credential handling
This commit is contained in:
1 parent
009eec764e
commit
179e853dc6
7 files changed
+952
-43
No files matched your search
Generated
+823
-38
File diff suppressed because it is too large.
Load diff
@@ -10,6 +10,8 @@ edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
argon2 = { version = "0.6.0", features = ["parallel", "zeroize"] }
|
||||
async-compat = "0.2.7"
|
||||
aws-sdk-sts = "1.128.0"
|
||||
base64ct = "1.8.3"
|
||||
dirs = "7.0.0"
|
||||
duckdb = { version = "1.20000.1", features = ["bundled", "uuid"] }
|
||||
|
||||
@@ -5,6 +5,7 @@ use gpui_kit::{
|
||||
use gpui_omarchy::ActiveTheme;
|
||||
|
||||
use crate::config::AppConfig;
|
||||
use crate::credentials::AwsSessionCache;
|
||||
use crate::screens::{ApprovalScreen, CredentialsScreen, HomeScreen, Screen, UnlockScreen, WelcomeScreen};
|
||||
use crate::session::AppSession;
|
||||
use crate::srv::{self, PendingRequests};
|
||||
@@ -55,6 +56,7 @@ pub fn launch(cx: &mut App, config: AppConfig, session: AppSession) -> WindowHan
|
||||
cx.set_global(session);
|
||||
cx.set_global(Screen::Home);
|
||||
cx.set_global(PendingRequests::new());
|
||||
cx.set_global(AwsSessionCache::default());
|
||||
// refresh_windows() forces everything to re-render, which is kind of a nuclear solution,
|
||||
// but these values aren't expected to change very frequently, so it shouldn't be a problem
|
||||
cx.observe_global::<AppConfig>(|cx| cx.refresh_windows())
|
||||
|
||||
+114
-1
@@ -1,7 +1,15 @@
|
||||
use jiff::Timestamp;
|
||||
use async_compat::CompatExt;
|
||||
use aws_sdk_sts::config::{BehaviorVersion, Credentials, Region};
|
||||
use aws_sdk_sts::error::{DisplayErrorContext, ProvideErrorMetadata};
|
||||
use aws_sdk_sts::{Client, Config};
|
||||
use gpui_kit::{AsyncApp, Global};
|
||||
use jiff::{SignedDuration, Timestamp};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::error::CreddyError;
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Default, Serialize, Deserialize)]
|
||||
pub struct AwsCredential {
|
||||
pub id: Uuid,
|
||||
@@ -19,3 +27,108 @@ pub struct AwsSessionCredential {
|
||||
pub session_token: String,
|
||||
pub expiration: Timestamp,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct AwsSessionCache {
|
||||
sessions: HashMap<String, AwsSessionCredential>,
|
||||
}
|
||||
|
||||
impl AwsSessionCache {
|
||||
pub async fn get_session(
|
||||
longterm: &AwsCredential,
|
||||
cx: &AsyncApp,
|
||||
) -> Result<AwsSessionCredential, CreddyError> {
|
||||
let cached = cx.read_global::<AwsSessionCache, _>(|cache, _| {
|
||||
cache.sessions.get(&longterm.access_key_id).cloned()
|
||||
});
|
||||
// a session that's about to expire isn't much use to the client
|
||||
if let Some(session) = cached
|
||||
&& session.expiration > Timestamp::now() + SignedDuration::from_secs(60)
|
||||
{
|
||||
return Ok(session);
|
||||
}
|
||||
|
||||
let config = Config::builder()
|
||||
.behavior_version(BehaviorVersion::latest())
|
||||
.region(Region::new("us-east-1"))
|
||||
.credentials_provider(Credentials::new(
|
||||
&longterm.access_key_id,
|
||||
&longterm.secret_access_key,
|
||||
None,
|
||||
None,
|
||||
"Creddy",
|
||||
))
|
||||
.build();
|
||||
// the SDK needs a Tokio context, which compat() supplies
|
||||
let response = Client::from_conf(config)
|
||||
.get_session_token()
|
||||
.duration_seconds(43_200)
|
||||
.send()
|
||||
.compat()
|
||||
.await
|
||||
.map_err(|e| {
|
||||
// only errors from the service itself come with a readable message
|
||||
let message = match e.message() {
|
||||
Some(message) => message.to_string(),
|
||||
None => DisplayErrorContext(&e).to_string(),
|
||||
};
|
||||
CreddyError::GetSession(message)
|
||||
})?;
|
||||
let creds = response
|
||||
.credentials
|
||||
.ok_or_else(|| CreddyError::GetSession("response contained no credentials".into()))?;
|
||||
|
||||
let session = AwsSessionCredential {
|
||||
version: 1,
|
||||
access_key_id: creds.access_key_id,
|
||||
secret_access_key: creds.secret_access_key,
|
||||
session_token: creds.session_token,
|
||||
expiration: Timestamp::new(creds.expiration.secs(), creds.expiration.subsec_nanos() as i32)
|
||||
.map_err(|e| CreddyError::GetSession(e.to_string()))?,
|
||||
};
|
||||
cx.update_global::<AwsSessionCache, _>(|cache, _| {
|
||||
cache.sessions.insert(longterm.access_key_id.clone(), session.clone())
|
||||
});
|
||||
Ok(session)
|
||||
}
|
||||
}
|
||||
|
||||
impl Global for AwsSessionCache {}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use gpui_kit::TestAppContext;
|
||||
|
||||
fn test_cred() -> AwsCredential {
|
||||
AwsCredential {
|
||||
id: Uuid::new_v4(),
|
||||
name: "test".into(),
|
||||
access_key_id: "AKIAEXAMPLE".into(),
|
||||
secret_access_key: "EXAMPLESECRET".into(),
|
||||
is_default: false,
|
||||
}
|
||||
}
|
||||
|
||||
#[gpui_kit::test]
|
||||
fn test_get_session_cached(cx: &mut TestAppContext) {
|
||||
let cached = AwsSessionCredential {
|
||||
version: 1,
|
||||
access_key_id: "ASIAEXAMPLE".into(),
|
||||
secret_access_key: "SESSIONSECRET".into(),
|
||||
session_token: "TOKEN".into(),
|
||||
expiration: Timestamp::now() + SignedDuration::from_hours(1),
|
||||
};
|
||||
let mut cache = AwsSessionCache::default();
|
||||
cache.sessions.insert("AKIAEXAMPLE".into(), cached.clone());
|
||||
cx.update(|cx| cx.set_global(cache));
|
||||
|
||||
// parking isn't allowed here, so this would panic if it went to the network
|
||||
let cx = cx.to_async();
|
||||
let session = cx
|
||||
.foreground_executor()
|
||||
.block_test(AwsSessionCache::get_session(&test_cred(), &cx))
|
||||
.unwrap();
|
||||
assert_eq!(session, cached);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,3 @@
|
||||
mod aws;
|
||||
|
||||
pub use aws::{AwsCredential, AwsSessionCredential};
|
||||
pub use aws::{AwsCredential, AwsSessionCache, AwsSessionCredential};
|
||||
@@ -34,6 +34,9 @@ pub enum CreddyError {
|
||||
#[error("Failed to bind socket: {0}")]
|
||||
SocketBind(String),
|
||||
|
||||
#[error("Failed to get AWS session: {0}")]
|
||||
GetSession(String),
|
||||
|
||||
#[error("Credential request was denied.")]
|
||||
RequestDenied,
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::credentials::{AwsCredential, AwsSessionCredential};
|
||||
use crate::credentials::{AwsCredential, AwsSessionCache, AwsSessionCredential};
|
||||
use crate::error::CreddyError;
|
||||
use crate::session::AppSession;
|
||||
use futures::channel::oneshot::{self, Sender};
|
||||
@@ -169,9 +169,13 @@ async fn handle_aws_get(uri: Uri, app: AsyncApp) -> Result<Response<Full<Bytes>>
|
||||
db.load_aws_credential(&name.unwrap_or_else(|| "default".into()))
|
||||
})?;
|
||||
|
||||
// todo: session credential handling
|
||||
let body = if session {
|
||||
let session_creds = AwsSessionCache::get_session(&creds, &app).await?;
|
||||
serde_json::to_vec(&session_creds).unwrap()
|
||||
} else {
|
||||
serde_json::to_vec(&creds).unwrap()
|
||||
};
|
||||
|
||||
let body = serde_json::to_vec(&creds).unwrap();
|
||||
Ok(Response::new(Full::new(Bytes::from(body))))
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user