Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions ak-agent-desktop/src-tauri/src/cmd/version.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use ak_platform::{
generated::ping::ping_client::PingClient,
grpc::grpc_endpoint,
http::ReqwestResponseError,
paths::{SysdSocketID, sysd_socket_path},
string::PlatformString,
};
Expand Down Expand Up @@ -89,10 +90,11 @@ async fn latest_version() -> Option<StableVersion> {
.get(VERSION_URL)
.send()
.await?
.error_for_status()?
.error_with_response()
.await?
.json()
.await?;
reqwest::Result::Ok(info.stable)
eyre::Ok(info.stable)
}
.await
.inspect_err(|e| tracing::debug!("version check failed: {e:?}"))
Expand Down
18 changes: 9 additions & 9 deletions ak-agent/src/grpc/agent_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use ak_platform::{
agent_auth_server::AgentAuth, current_token_request::Type,
},
},
http::ReqwestResponseError,
net::server::{creds::ProcCredentials, proc_info::process_user},
oauth2_http,
string::PlatformString,
Expand Down Expand Up @@ -141,9 +142,10 @@ impl AgentAuth for AgentGRPCServer {
Ok(r) => r,
Err(e) => return Err(Status::from_error(e.into())),
};
if !req.status().is_success() {
return Err(Status::internal("Invalid status code for whoami request"));
}
let req = req
.error_with_response()
.await
.map_err(|e| Status::internal(format!("whoami request failed: {e}")))?;

Ok(Response::new(WhoAmIResponse {
header: Some(ResponseHeader { successful: true }),
Expand Down Expand Up @@ -368,12 +370,10 @@ impl AgentGRPCServer {
.body(body)
.send()
.await
.map_err(|e| Status::from_error(e.into()))?;

if !res.status().is_success() {
let body = res.text().await.unwrap_or_default();
return Err(Status::internal(format!("token exchange failed: {body}")));
}
.map_err(|e| Status::from_error(e.into()))?
.error_with_response()
.await
.map_err(|e| Status::internal(format!("token exchange failed: {e}")))?;

let new_token: OAuthTokenResponse =
res.json().await.map_err(|e| Status::from_error(e.into()))?;
Expand Down
7 changes: 5 additions & 2 deletions ak-agent/src/ssh/txn.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::sync::Arc;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use ak_platform::{net::server::creds::ProcCredentials, string::PlatformString};
use ak_platform::{
http::ReqwestResponseError, net::server::creds::ProcCredentials, string::PlatformString,
};
use ak_platform_authz::AuthorizeAction;
use authentik_client::apis::endpoints_api::endpoints_agents_connectors_auth_fed_create;
use eyre::{Result, WrapErr, bail};
Expand Down Expand Up @@ -170,7 +172,8 @@ impl SSHAgentTransaction {
.get(userinfo_url)
.send()
.await?
.error_for_status()?;
.error_with_response()
.await?;
Ok(response.json::<UserInfo>().await?.preferred_username)
}

Expand Down
20 changes: 11 additions & 9 deletions ak-agent/src/token/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use std::sync::Arc;
use std::time::Duration;

use ak_meta::user_agent;
use ak_platform::http::ReqwestResponseError;
use ak_platform::shared::AuthentikClaims;
use chrono::{TimeDelta, Utc};
use jsonwebtoken::{DecodingKey, Validation, decode, decode_header, jwk::JwkSet};
use tokio::sync::{Notify, RwLock};

use ak_platform::storage::cfgmgr::ConfigManager;
use eyre::{Result, bail};
use eyre::{Result, WrapErr};

use crate::config::ConfigV1;
use crate::token::Token;
Expand Down Expand Up @@ -249,14 +250,15 @@ impl ProfileTokenManager {
req = req.header("DPoP", proof);
}

let res = req.body(body).send().await?;

if !res.status().is_success() {
let body = res.text().await?;
bail!("token renewal failed: {body}");
}

let new_token: Token = res.json().await?;
let new_token: Token = req
.body(body)
.send()
.await?
.error_with_response()
.await
.wrap_err("token renewal failed")?
.json()
.await?;

{
let mut config = self.cfg.write().await;
Expand Down
4 changes: 3 additions & 1 deletion ak-cli/src/auth/claude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use ak_platform::{
client::user::{AnyService, Client},
generated::{agent::RequestHeader, agent_auth::TokenExchangeRequest},
grpc::assert_response_valid,
http::ReqwestResponseError,
};
use chrono::{TimeDelta, Utc};
use eyre::{Result, WrapErr};
Expand Down Expand Up @@ -102,7 +103,8 @@ pub async fn get_credentials(
})
.send()
.await?
.error_for_status()?
.error_with_response()
.await?
.json()
.await?;

Expand Down
4 changes: 3 additions & 1 deletion ak-platform-e2e/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{env, path::PathBuf, time::Duration};

use ak_flow_executor::executor::FlowExecutor;
use ak_platform::http::ReqwestResponseError;
use ak_platform::log::{LevelFilter, LogBuilder};
use ak_platform::oauth::device_flow::{poll_for_device_token, request_device_authorization};
use ak_platform::string::PlatformString;
Expand Down Expand Up @@ -226,7 +227,8 @@ pub async fn cleanup_hosts() -> Result<()> {
.send()
.await
.wrap_err("failed to list devices")?
.error_for_status()
.error_with_response()
.await
.wrap_err("failed to list devices")?
.json()
.await
Expand Down
20 changes: 20 additions & 0 deletions ak-platform/src/http.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use eyre::Error;
use reqwest::Response;

pub trait ReqwestResponseError {
fn error_with_response(self) -> impl std::future::Future<Output = Result<Self, Error>> + Send
where
Self: Sized;
}

impl ReqwestResponseError for Response {
async fn error_with_response(self) -> Result<Self, Error> {
let status = self.status();
if status.is_client_error() || status.is_server_error() {
let body = self.text().await?;
Err(eyre::eyre!("Error in HTTP request: {} {}", status, body))
} else {
Ok(self)
}
}
}
1 change: 1 addition & 0 deletions ak-platform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod config;
pub mod dpop;
pub mod generated;
pub mod grpc;
pub mod http;
pub mod log;
pub mod net;
pub mod oauth;
Expand Down
Loading