a2a_protocol_server/push/
tenant_sqlite_config_store.rs1mod caller_keys;
14
15use std::future::Future;
16use std::pin::Pin;
17use std::sync::Arc;
18
19use a2a_protocol_types::error::{A2aError, A2aResult};
20use a2a_protocol_types::push::TaskPushNotificationConfig;
21use sqlx::sqlite::SqlitePool;
22
23use super::cipher::{PushConfigCipher, Sealing};
24use super::config_store::PushConfigStore;
25use crate::store::tenant::TenantContext;
26
27#[derive(Debug, Clone)]
45pub struct TenantAwareSqlitePushConfigStore {
46 pool: SqlitePool,
47 sealing: Sealing,
49}
50
51use crate::sqlite_pool::sqlite_pool;
52
53fn to_a2a_error(e: &sqlx::Error) -> A2aError {
54 crate::sql_error::sql_error(crate::sql_error::Db::Sqlite, "sqlite error", e)
55}
56
57impl TenantAwareSqlitePushConfigStore {
58 pub async fn new(url: &str) -> Result<Self, sqlx::Error> {
64 let pool = sqlite_pool(url).await?;
65 Self::from_pool(pool).await
66 }
67
68 #[must_use]
71 pub fn with_cipher(mut self, cipher: Arc<dyn PushConfigCipher>) -> Self {
72 self.sealing = self.sealing.with_cipher(cipher);
73 self
74 }
75
76 #[must_use]
80 pub fn accept_unsealed_rows(mut self) -> Self {
81 self.sealing = self.sealing.accepting_unsealed();
82 self
83 }
84
85 pub async fn from_pool(pool: SqlitePool) -> Result<Self, sqlx::Error> {
91 sqlx::query(
92 "CREATE TABLE IF NOT EXISTS tenant_push_configs (
93 tenant_id TEXT NOT NULL DEFAULT '',
94 task_id TEXT NOT NULL,
95 id TEXT NOT NULL,
96 data TEXT NOT NULL,
97 PRIMARY KEY (tenant_id, task_id, id)
98 )",
99 )
100 .execute(&pool)
101 .await?;
102
103 Ok(Self {
104 pool,
105 sealing: Sealing::default(),
106 })
107 }
108}
109
110#[allow(clippy::manual_async_fn)]
111impl PushConfigStore for TenantAwareSqlitePushConfigStore {
112 fn is_process_local(&self) -> bool {
113 crate::sqlite_pool::is_in_memory(&self.pool)
114 }
115
116 fn isolates_tenants(&self) -> bool {
117 true
118 }
119
120 fn set<'a>(
121 &'a self,
122 mut config: TaskPushNotificationConfig,
123 ) -> Pin<Box<dyn Future<Output = A2aResult<TaskPushNotificationConfig>> + Send + 'a>> {
124 Box::pin(async move {
125 let tenant = TenantContext::partition();
126 let Some(task_id) = config.task_id.clone() else {
130 return Err(A2aError::invalid_params(
131 "taskId is required to store a push notification config",
132 ));
133 };
134 let id = config
135 .id
136 .clone()
137 .unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
138 config.id = Some(id.clone());
139
140 let data = self
141 .sealing
142 .encode_text(&config, &Sealing::context(&tenant, &task_id, &id))
143 .await?;
144
145 sqlx::query(
146 "INSERT INTO tenant_push_configs (tenant_id, task_id, id, data)
147 VALUES (?1, ?2, ?3, ?4)
148 ON CONFLICT(tenant_id, task_id, id) DO UPDATE SET data = excluded.data",
149 )
150 .bind(&tenant)
151 .bind(&task_id)
152 .bind(&id)
153 .bind(&data)
154 .execute(&self.pool)
155 .await
156 .map_err(|e| to_a2a_error(&e))?;
157
158 Ok(config)
159 })
160 }
161
162 fn get<'a>(
163 &'a self,
164 task_id: &'a str,
165 id: &'a str,
166 ) -> Pin<Box<dyn Future<Output = A2aResult<Option<TaskPushNotificationConfig>>> + Send + 'a>>
167 {
168 Box::pin(async move {
169 let tenant = TenantContext::partition();
170 let row: Option<(String,)> = sqlx::query_as(
171 "SELECT data FROM tenant_push_configs WHERE tenant_id = ?1 AND task_id = ?2 AND id = ?3",
172 )
173 .bind(&tenant)
174 .bind(task_id)
175 .bind(id)
176 .fetch_optional(&self.pool)
177 .await
178 .map_err(|e| to_a2a_error(&e))?;
179
180 match row {
181 Some((data,)) => Ok(Some(
182 self.sealing
183 .decode_text(&data, &Sealing::context(&tenant, task_id, id))
184 .await?,
185 )),
186 None => Ok(None),
187 }
188 })
189 }
190
191 fn list<'a>(
192 &'a self,
193 task_id: &'a str,
194 ) -> Pin<Box<dyn Future<Output = A2aResult<Vec<TaskPushNotificationConfig>>> + Send + 'a>> {
195 Box::pin(async move {
196 let tenant = TenantContext::partition();
197 let rows: Vec<(String, String)> = sqlx::query_as(
198 "SELECT id, data FROM tenant_push_configs WHERE tenant_id = ?1 AND task_id = ?2",
199 )
200 .bind(&tenant)
201 .bind(task_id)
202 .fetch_all(&self.pool)
203 .await
204 .map_err(|e| to_a2a_error(&e))?;
205
206 let mut configs = Vec::with_capacity(rows.len());
207 for (id, data) in rows {
208 configs.push(
209 self.sealing
210 .decode_text(&data, &Sealing::context(&tenant, task_id, &id))
211 .await?,
212 );
213 }
214 Ok(configs)
215 })
216 }
217
218 fn delete<'a>(
219 &'a self,
220 task_id: &'a str,
221 id: &'a str,
222 ) -> Pin<Box<dyn Future<Output = A2aResult<()>> + Send + 'a>> {
223 Box::pin(async move {
224 let tenant = TenantContext::partition();
225 sqlx::query(
226 "DELETE FROM tenant_push_configs WHERE tenant_id = ?1 AND task_id = ?2 AND id = ?3",
227 )
228 .bind(&tenant)
229 .bind(task_id)
230 .bind(id)
231 .execute(&self.pool)
232 .await
233 .map_err(|e| to_a2a_error(&e))?;
234 Ok(())
235 })
236 }
237
238 fn count(&self) -> Pin<Box<dyn Future<Output = A2aResult<Option<usize>>> + Send + '_>> {
239 Box::pin(async move {
240 let tenant = TenantContext::partition();
242 let (total,): (i64,) =
243 sqlx::query_as("SELECT COUNT(*) FROM tenant_push_configs WHERE tenant_id = ?1")
244 .bind(&tenant)
245 .fetch_one(&self.pool)
246 .await
247 .map_err(|e| to_a2a_error(&e))?;
248 Ok(Some(usize::try_from(total).unwrap_or(usize::MAX)))
249 })
250 }
251}
252
253#[cfg(test)]
254#[path = "tenant_sqlite_config_store_tests.rs"]
255mod tests;