use std::fmt;
use adk_core::{AdkError, ErrorCategory, ErrorComponent};
use crate::sso::TokenError;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum CloudProvider {
Aws,
AgentCore,
Google,
Entra,
}
impl fmt::Display for CloudProvider {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Aws => "AWS STS",
Self::AgentCore => "Amazon Bedrock AgentCore Identity",
Self::Google => "Google Cloud",
Self::Entra => "Microsoft Entra",
})
}
}
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum FederationError {
#[error("invalid federation configuration: {reason}")]
InvalidConfig {
reason: String,
},
#[error(
"invalid issuer signing key: {reason}; pass an unencrypted PKCS#8 RSA private key of 2048 bits or more (`openssl pkcs8 -topk8 -nocrypt` converts a PKCS#1 key)"
)]
InvalidKey {
reason: String,
},
#[error(
"signing the agent token failed: {reason}; check that the signing key or key service is available"
)]
Signing {
reason: String,
},
#[error(
"the request to {provider} at '{endpoint}' failed: {reason}; check network access to the endpoint"
)]
Transport {
provider: CloudProvider,
endpoint: String,
reason: String,
},
#[error(
"{provider} refused the exchange with HTTP {status} ({code}): {message}; check that the cloud trusts this issuer, subject and audience"
)]
Refused {
provider: CloudProvider,
status: u16,
code: String,
message: String,
},
#[error("{provider} returned a response this client cannot read: {reason}")]
InvalidResponse {
provider: CloudProvider,
reason: String,
},
#[error("{provider} tokens cannot be requested for '{resource}': {reason}")]
UnsupportedRequest {
provider: CloudProvider,
resource: String,
reason: String,
},
#[error(transparent)]
Token(#[from] TokenError),
#[error("the agent token has no usable `{claim}` claim: {reason}")]
Claim {
claim: String,
reason: String,
},
}
impl From<FederationError> for AdkError {
fn from(err: FederationError) -> Self {
let err = match err {
FederationError::Token(token) => return token.into(),
other => other,
};
let (category, code, status) = match &err {
FederationError::InvalidConfig { .. } => {
(ErrorCategory::InvalidInput, "identity.federation.invalid_config", None)
}
FederationError::InvalidKey { .. } => {
(ErrorCategory::InvalidInput, "identity.federation.invalid_key", None)
}
FederationError::Signing { .. } => {
(ErrorCategory::Internal, "identity.federation.signing_failed", None)
}
FederationError::Transport { .. } => {
(ErrorCategory::Unavailable, "identity.federation.exchange_failed", None)
}
FederationError::Refused { status, .. } => {
let category = match *status {
429 => ErrorCategory::RateLimited,
500..=599 => ErrorCategory::Unavailable,
403 => ErrorCategory::Forbidden,
_ => ErrorCategory::Unauthorized,
};
(category, "identity.federation.exchange_refused", Some(*status))
}
FederationError::InvalidResponse { .. } => {
(ErrorCategory::Internal, "identity.federation.invalid_response", None)
}
FederationError::UnsupportedRequest { .. } => {
(ErrorCategory::Forbidden, "identity.federation.request_unsupported", None)
}
FederationError::Token(_) | FederationError::Claim { .. } => {
(ErrorCategory::Unauthorized, "identity.federation.claim_invalid", None)
}
};
let error = AdkError::new(ErrorComponent::Auth, category, code, err.to_string());
match status {
Some(status) => error.with_upstream_status(status).with_source(err),
None => error.with_source(err),
}
}
}