use std::fmt;
use std::sync::Arc;
use lambda_http::http::{HeaderName, HeaderValue, Method, StatusCode, header};
use serde_json::{Map, Value, json};
use super::Server;
use super::tool::{INVALID_ARGUMENTS, Run, Tool};
use crate::RuntimeError;
use crate::http::{Admission, Failure, Policy, codes};
pub const DEFAULT_RESPONSE_LIMIT: usize = 1024 * 1024;
pub const UPSTREAM_REFUSED: &str = "ERROR_UPSTREAM_REFUSED";
pub const UPSTREAM_UNAVAILABLE: &str = "ERROR_UPSTREAM_UNAVAILABLE";
const SCHEMA_BUDGET: usize = 10_000;
const METHODS: [(&str, Method); 8] = [
("get", Method::GET),
("put", Method::PUT),
("post", Method::POST),
("delete", Method::DELETE),
("options", Method::OPTIONS),
("head", Method::HEAD),
("patch", Method::PATCH),
("trace", Method::TRACE),
];
pub struct OpenApi {
operations: Vec<Operation>,
base_url: String,
http: reqwest::Client,
response_limit: usize,
forward_caller_token: bool,
headers: Vec<(HeaderName, HeaderValue)>,
}
impl OpenApi {
pub fn new(
document: &Value,
base_url: impl Into<String>,
http: reqwest::Client,
) -> Result<Self, RuntimeError> {
let version = document["openapi"].as_str().unwrap_or_default();
if !version.starts_with("3.") {
return Err(RuntimeError::Configuration(
"the tools document is not OpenAPI 3".to_owned(),
));
}
let mut operations = Vec::new();
for (path, item) in document["paths"].as_object().into_iter().flatten() {
if !path.starts_with('/') {
return Err(RuntimeError::Configuration(format!(
"the tools document has a path that does not start with /: {path}"
)));
}
for (key, method) in &METHODS {
if let Some(operation) = derive(document, path, item, key, method) {
operations.push(operation);
}
}
}
Ok(Self {
operations,
base_url: base_url.into(),
http,
response_limit: DEFAULT_RESPONSE_LIMIT,
forward_caller_token: false,
headers: Vec::new(),
})
}
#[must_use]
pub fn forward_caller_token(mut self) -> Self {
self.forward_caller_token = true;
self
}
#[must_use]
pub fn header(mut self, name: HeaderName, mut value: HeaderValue) -> Self {
value.set_sensitive(true);
self.headers.push((name, value));
self
}
#[must_use]
pub fn select(mut self, keep: impl Fn(&Operation) -> bool) -> Self {
self.operations.retain(|operation| keep(operation));
self
}
#[must_use]
pub fn response_limit(mut self, bytes: usize) -> Self {
self.response_limit = bytes;
self
}
pub fn operations(&self) -> &[Operation] {
&self.operations
}
}
impl fmt::Debug for OpenApi {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OpenApi")
.field("operations", &self.operations)
.field("response_limit", &self.response_limit)
.field("forward_caller_token", &self.forward_caller_token)
.field(
"headers",
&self
.headers
.iter()
.map(|(name, _)| name)
.collect::<Vec<_>>(),
)
.finish_non_exhaustive()
}
}
impl<App, P, A> Server<App, P, A>
where
App: Send + Sync + 'static,
P: Policy,
A: Admission,
{
#[must_use]
pub fn openapi(mut self, api: OpenApi) -> Self {
let upstream = Arc::new(Upstream {
base_url: api.base_url,
http: api.http,
limit: api.response_limit,
forward_caller_token: api.forward_caller_token,
headers: api.headers,
});
for operation in api.operations {
let definition = operation.tool.clone();
let operation = Arc::new(operation);
let upstream = Arc::clone(&upstream);
let run: Run<App, P::Scope> =
Box::new(move |_app, arguments, _context, authorization| {
Box::pin(forward(
Arc::clone(&operation),
Arc::clone(&upstream),
arguments,
authorization,
))
});
self = self.tool(Tool::from_parts(definition, run));
}
self
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct Operation {
name: String,
method: Method,
path: String,
definition: Value,
parameters: Vec<Parameter>,
body: Body,
required: Vec<String>,
tool: Value,
}
impl Operation {
pub fn name(&self) -> &str {
&self.name
}
pub fn method(&self) -> &str {
self.method.as_str()
}
pub fn path(&self) -> &str {
&self.path
}
pub fn has_tag(&self, tag: &str) -> bool {
self.definition["tags"]
.as_array()
.is_some_and(|tags| tags.iter().any(|own| own == tag))
}
pub fn definition(&self) -> &Value {
&self.definition
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Location {
Path,
Query,
Header,
}
#[derive(Debug, PartialEq, Eq)]
struct Parameter {
name: String,
location: Location,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Body {
None,
Fields,
Whole,
}
struct Upstream {
base_url: String,
http: reqwest::Client,
limit: usize,
forward_caller_token: bool,
headers: Vec<(HeaderName, HeaderValue)>,
}
fn derive(
document: &Value,
path: &str,
item: &Value,
key: &str,
method: &Method,
) -> Option<Operation> {
let definition = item.get(key)?;
let name = tool_name(definition["operationId"].as_str()?)?;
let mut budget = SCHEMA_BUDGET;
let mut resolve = |node: &Value| inline(node, document, &mut Vec::new(), &mut budget);
let mut declared: Vec<Value> = Vec::new();
for parameter in [&item["parameters"], &definition["parameters"]]
.into_iter()
.filter_map(Value::as_array)
.flatten()
{
let parameter = resolve(parameter);
declared
.retain(|other| other["name"] != parameter["name"] || other["in"] != parameter["in"]);
declared.push(parameter);
}
let mut properties = Map::new();
let mut required = Vec::new();
let mut parameters = Vec::new();
for parameter in declared {
let (Some(pname), Some(location)) = (parameter["name"].as_str(), location(¶meter))
else {
continue;
};
let mut schema = parameter
.get("schema")
.cloned()
.unwrap_or(json!({ "type": "string" }));
if let (Some(schema), Some(description)) =
(schema.as_object_mut(), parameter.get("description"))
{
schema.insert("description".into(), description.clone());
}
if location == Location::Path || parameter["required"] == true {
required.push(pname.to_owned());
}
properties.insert(pname.to_owned(), schema);
parameters.push(Parameter {
name: pname.to_owned(),
location,
});
}
let request = resolve(&definition["requestBody"]);
let body = match request["content"].as_object() {
None => Body::None,
Some(content) => {
let schema = content
.get("application/json")?
.get("schema")
.cloned()
.unwrap_or(json!({}));
let fields = schema["properties"].as_object().filter(|fields| {
schema["type"] == "object" && fields.keys().all(|key| !properties.contains_key(key))
});
match fields {
Some(fields) => {
properties.extend(fields.clone());
let names = schema["required"].as_array().into_iter().flatten();
required.extend(names.filter_map(Value::as_str).map(str::to_owned));
Body::Fields
}
None => {
if request["required"] == true {
required.push("body".to_owned());
}
properties.insert("body".to_owned(), schema);
Body::Whole
}
}
}
};
let mut input = json!({ "type": "object", "properties": properties });
if !required.is_empty() {
input["required"] = json!(required);
}
let summary = definition["summary"].as_str();
let description = definition["description"]
.as_str()
.or(summary)
.map_or_else(|| format!("{method} {path}"), str::to_owned);
let read_only = matches!(
*method,
Method::GET | Method::HEAD | Method::OPTIONS | Method::TRACE
);
let mut tool = json!({
"name": name,
"description": description,
"inputSchema": input,
"annotations": {
"readOnlyHint": read_only,
"destructiveHint": !read_only,
"idempotentHint": read_only || matches!(*method, Method::PUT | Method::DELETE),
},
});
if let Some(summary) = summary {
tool["title"] = json!(summary);
}
Some(Operation {
name,
method: method.clone(),
path: path.to_owned(),
definition: definition.clone(),
parameters,
body,
required,
tool,
})
}
const TRANSPORT_HEADERS: [&str; 6] = [
"accept",
"content-type",
"authorization",
"host",
"content-length",
"transfer-encoding",
];
fn location(parameter: &Value) -> Option<Location> {
let name = parameter["name"].as_str().unwrap_or_default();
match parameter["in"].as_str()? {
"path" => Some(Location::Path),
"query" => Some(Location::Query),
"header"
if !TRANSPORT_HEADERS
.iter()
.any(|own| name.eq_ignore_ascii_case(own)) =>
{
Some(Location::Header)
}
_ => None,
}
}
fn tool_name(operation_id: &str) -> Option<String> {
let name: String = operation_id
.chars()
.map(|c| {
if c.is_ascii_alphanumeric() || matches!(c, '_' | '-' | '.') {
c
} else {
'_'
}
})
.take(128)
.collect();
(!name.is_empty()).then_some(name)
}
fn inline(node: &Value, document: &Value, seen: &mut Vec<String>, budget: &mut usize) -> Value {
if *budget == 0 {
return node.clone();
}
*budget -= 1;
match node {
Value::Array(items) => Value::Array(
items
.iter()
.map(|item| inline(item, document, seen, budget))
.collect(),
),
Value::Object(object) => {
if let Some(reference) = object.get("$ref").and_then(Value::as_str) {
let target = reference
.strip_prefix('#')
.and_then(|pointer| document.pointer(pointer));
return match target {
Some(target) if !seen.iter().any(|open| open == reference) => {
seen.push(reference.to_owned());
let resolved = inline(target, document, seen, budget);
seen.pop();
let keywords = object
.iter()
.filter(|(key, _)| key.as_str() != "$ref")
.map(|(key, value)| {
(key.clone(), inline(value, document, seen, budget))
})
.collect();
beside(resolved, keywords)
}
_ => node.clone(),
};
}
Value::Object(
object
.iter()
.map(|(key, value)| (key.clone(), inline(value, document, seen, budget)))
.collect(),
)
}
other => other.clone(),
}
}
const ANNOTATIONS: [&str; 11] = [
"title",
"summary",
"description",
"default",
"example",
"examples",
"deprecated",
"readOnly",
"writeOnly",
"externalDocs",
"$comment",
];
fn beside(target: Value, keywords: Map<String, Value>) -> Value {
if keywords.is_empty() {
return target;
}
let Value::Object(mut schema) = target else {
let mut schema = keywords;
all_of(&mut schema, vec![target]);
return Value::Object(schema);
};
let mut apart = Vec::new();
for (key, value) in keywords {
let Some(own) = schema.get_mut(&key) else {
schema.insert(key, value);
continue;
};
match (own, value) {
(own, value) if *own == value => {}
(own, value) if ANNOTATIONS.contains(&key.as_str()) => *own = value,
(Value::Array(own), Value::Array(more)) if key == "required" => {
for name in more {
if !own.contains(&name) {
own.push(name);
}
}
}
(Value::Array(own), Value::Array(more)) if key == "allOf" => own.extend(more),
(Value::Object(own), Value::Object(more)) if key == "properties" => {
for (name, property) in more {
match own.get_mut(&name) {
Some(existing) if *existing != property => {
*existing = json!({ "allOf": [existing.take(), property] });
}
Some(_) => {}
None => {
own.insert(name, property);
}
}
}
}
(_, value) => {
let mut keyword = Map::new();
keyword.insert(key, value);
apart.push(Value::Object(keyword));
}
}
}
all_of(&mut schema, apart);
Value::Object(schema)
}
fn all_of(schema: &mut Map<String, Value>, schemas: Vec<Value>) {
if schemas.is_empty() {
return;
}
match schema.entry("allOf").or_insert_with(|| json!([])) {
Value::Array(items) => items.extend(schemas),
other => {
let own = other.take();
*other = Value::Array(std::iter::once(own).chain(schemas).collect());
}
}
}
async fn forward(
operation: Arc<Operation>,
upstream: Arc<Upstream>,
mut arguments: Map<String, Value>,
authorization: Option<String>,
) -> Result<Value, Failure> {
let is_parameter = |name: &String| operation.parameters.iter().any(|p| p.name == *name);
arguments.retain(|name, value| {
!value.is_null() || (operation.body == Body::Fields && !is_parameter(name))
});
let missing: Vec<&str> = operation
.required
.iter()
.filter(|name| !arguments.contains_key(*name))
.map(String::as_str)
.collect();
if !missing.is_empty() {
return Err(invalid(format!(
"Missing required arguments: {}",
missing.join(", ")
)));
}
let mut path = operation.path.clone();
let mut query = Vec::new();
let mut headers = Vec::new();
for parameter in &operation.parameters {
let Some(value) = arguments.remove(¶meter.name) else {
continue;
};
let items: Vec<String> = match value {
Value::Array(items) => items.iter().map(text).collect(),
other => vec![text(&other)],
};
match parameter.location {
Location::Path => {
let value = items.join(",");
path = path.replace(&format!("{{{}}}", parameter.name), &encode(&value));
}
Location::Query => query.extend(
items
.iter()
.map(|item| format!("{}={}", encode(¶meter.name), encode(item))),
),
Location::Header => {
let name = HeaderName::try_from(parameter.name.as_str());
let value = HeaderValue::try_from(items.join(","));
let (Ok(name), Ok(value)) = (name, value) else {
return Err(invalid(format!(
"{} cannot be sent as a header",
parameter.name
)));
};
headers.push((name, value));
}
}
}
if path.split('/').any(|segment| matches!(segment, "." | "..")) {
return Err(invalid(
"Path arguments cannot form a dot path segment".to_owned(),
));
}
let mut url = format!("{}{path}", upstream.base_url.trim_end_matches('/'));
if !query.is_empty() {
url = format!("{url}?{}", query.join("&"));
}
let mut request = upstream
.http
.request(operation.method.clone(), url)
.header(header::ACCEPT, "application/json");
let configured = |name: &HeaderName| upstream.headers.iter().any(|(fixed, _)| fixed == name);
if !configured(&header::AUTHORIZATION)
&& let Some(authorization) = authorization.filter(|_| upstream.forward_caller_token)
{
request = request.header(header::AUTHORIZATION, authorization);
}
for (name, value) in &upstream.headers {
request = request.header(name, value);
}
for (name, value) in headers.into_iter().filter(|(name, _)| !configured(name)) {
request = request.header(name, value);
}
request = match operation.body {
Body::None => request,
Body::Whole => match arguments.remove("body") {
Some(body) => request.json(&body),
None => request,
},
Body::Fields => request.json(&arguments),
};
let response = request.send().await.map_err(no_answer)?;
let status = response.status();
let body = crate::client::read_bounded(response, upstream.limit)
.await
.map_err(|error| match error {
RuntimeError::LimitExceeded { .. } => {
Failure::new(StatusCode::BAD_GATEWAY, codes::LIMIT_EXCEEDED, "")
}
other => Failure::new(StatusCode::BAD_GATEWAY, UPSTREAM_UNAVAILABLE, "")
.with_detail(crate::error_chain(&other)),
})?;
if status.is_success() {
return Ok(if body.is_empty() {
Value::String(format!("HTTP {}", status.as_u16()))
} else {
serde_json::from_slice(&body)
.unwrap_or_else(|_| Value::String(String::from_utf8_lossy(&body).into_owned()))
});
}
if status.is_client_error() {
let message = format!(
"The API refused the call with HTTP {}: {}",
status.as_u16(),
String::from_utf8_lossy(&body)
);
return Err(Failure::new(status, UPSTREAM_REFUSED, message));
}
Err(
Failure::new(StatusCode::BAD_GATEWAY, UPSTREAM_UNAVAILABLE, "")
.with_detail(format!("the API answered HTTP {}", status.as_u16())),
)
}
fn invalid(message: String) -> Failure {
Failure::new(StatusCode::BAD_REQUEST, INVALID_ARGUMENTS, message)
}
fn no_answer(error: reqwest::Error) -> Failure {
if error.is_timeout() {
return Failure::new(StatusCode::GATEWAY_TIMEOUT, codes::TIMEOUT, "");
}
let detail = crate::error_chain(&crate::client::send_error("calling the API", error));
Failure::new(StatusCode::BAD_GATEWAY, UPSTREAM_UNAVAILABLE, "").with_detail(detail)
}
fn text(value: &Value) -> String {
match value {
Value::String(text) => text.clone(),
other => other.to_string(),
}
}
fn encode(value: &str) -> String {
value
.bytes()
.map(|byte| match byte {
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
char::from(byte).to_string()
}
other => format!("%{other:02X}"),
})
.collect()
}