agenttrustid 0.3.0

AgentTrust ID SDK — runtime authorization, opaque agent tokens, and Guardian checks for AI agents
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! SIEM streaming destination management and event subscription.
//!
//! Use this API to register external SIEM destinations (Splunk, Datadog, etc.)
//! that AgentTrust ID will stream audit events to, and to inspect their delivery logs.
//!
//! For agent-side consumption of streamed events, [`Streaming::subscribe`]
//! offers a polling helper that calls a handler for each new delivery record.
//!
//! # Example
//!
//! ```rust,no_run
//! use agenttrustid::{AgentTrustClient, CreateSIEMDestinationRequest};
//!
//! let client = AgentTrustClient::builder().build().unwrap();
//!
//! let dest = client.streaming().create(&CreateSIEMDestinationRequest {
//!     name: "splunk-prod".to_string(),
//!     destination_type: "splunk".to_string(),
//!     endpoint_url: "https://siem.example.com".to_string(),
//!     auth_token: Some("token".to_string()),
//!     batch_size: Some(100),
//!     flush_interval_seconds: Some(30),
//!     filter_event_types: None,
//! }).unwrap();
//!
//! println!("destination: {}", dest.id);
//! ```

use std::time::Duration;

use serde::Deserialize;
use serde_json::Value;

use crate::client::AgentTrustClient;
use crate::error::Result;
use crate::models::{
    CreateSIEMDestinationRequest, SIEMDeliveryRecord, SIEMDestination, UpdateSIEMDestinationRequest,
};

/// Provides SIEM streaming destination management.
///
/// Obtained via [`AgentTrustClient::streaming()`].
pub struct Streaming<'a> {
    pub(crate) client: &'a AgentTrustClient,
}

#[derive(Debug, Deserialize)]
struct DestinationListResponse {
    #[serde(default)]
    destinations: Option<Vec<SIEMDestination>>,
}

#[derive(Debug, Deserialize)]
struct DeliveryLogResponse {
    #[serde(default)]
    logs: Option<Vec<SIEMDeliveryRecord>>,
}

/// Filter options for [`Streaming::subscribe`].
///
/// Currently filters by destination ID; additional filters may be added in
/// later versions.
#[derive(Debug, Clone, Default)]
pub struct StreamFilter {
    /// Required destination ID to subscribe to.
    pub destination_id: String,
    /// Polling interval. Defaults to 5 seconds when zero.
    pub poll_interval: Option<Duration>,
    /// Maximum polls before returning. `None` polls forever (use carefully).
    pub max_polls: Option<u32>,
}

impl<'a> Streaming<'a> {
    /// Create a new SIEM destination.
    ///
    /// Calls `POST /api/v1/siem/destinations`.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use agenttrustid::{AgentTrustClient, CreateSIEMDestinationRequest};
    /// # let client = AgentTrustClient::builder().build().unwrap();
    /// let d = client.streaming().create(&CreateSIEMDestinationRequest {
    ///     name: "splunk".into(),
    ///     destination_type: "splunk".into(),
    ///     endpoint_url: "https://x".into(),
    ///     auth_token: None,
    ///     batch_size: None,
    ///     flush_interval_seconds: None,
    ///     filter_event_types: None,
    /// }).unwrap();
    /// assert!(!d.id.is_empty());
    /// ```
    pub fn create(&self, req: &CreateSIEMDestinationRequest) -> Result<SIEMDestination> {
        self.client
            .request("POST", "/api/v1/siem/destinations", Some(req))
    }

    /// List SIEM destinations.
    ///
    /// Calls `GET /api/v1/siem/destinations`.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use agenttrustid::AgentTrustClient;
    /// # let client = AgentTrustClient::builder().build().unwrap();
    /// let list = client.streaming().list().unwrap();
    /// println!("{} destinations", list.len());
    /// ```
    pub fn list(&self) -> Result<Vec<SIEMDestination>> {
        let value: Value = self
            .client
            .request("GET", "/api/v1/siem/destinations", None::<&()>)?;
        if let Value::Array(_) = &value {
            let v: Vec<SIEMDestination> = serde_json::from_value(value)?;
            return Ok(v);
        }
        let resp: DestinationListResponse = serde_json::from_value(value)?;
        Ok(resp.destinations.unwrap_or_default())
    }

    /// Retrieve a SIEM destination by ID.
    ///
    /// Calls `GET /api/v1/siem/destinations/{id}`.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use agenttrustid::AgentTrustClient;
    /// # let client = AgentTrustClient::builder().build().unwrap();
    /// let d = client.streaming().get("dest-1").unwrap();
    /// println!("active: {}", d.is_active);
    /// ```
    pub fn get(&self, destination_id: &str) -> Result<SIEMDestination> {
        let path = format!("/api/v1/siem/destinations/{}", destination_id);
        self.client.request("GET", &path, None::<&()>)
    }

    /// Update a SIEM destination.
    ///
    /// Calls `PUT /api/v1/siem/destinations/{id}`.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use agenttrustid::{AgentTrustClient, UpdateSIEMDestinationRequest};
    /// # let client = AgentTrustClient::builder().build().unwrap();
    /// let mut req = UpdateSIEMDestinationRequest::default();
    /// req.is_active = Some(false);
    /// let d = client.streaming().update("dest-1", &req).unwrap();
    /// assert!(!d.is_active);
    /// ```
    pub fn update(
        &self,
        destination_id: &str,
        req: &UpdateSIEMDestinationRequest,
    ) -> Result<SIEMDestination> {
        let path = format!("/api/v1/siem/destinations/{}", destination_id);
        self.client.request("PUT", &path, Some(req))
    }

    /// Delete a SIEM destination.
    ///
    /// Calls `DELETE /api/v1/siem/destinations/{id}`.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use agenttrustid::AgentTrustClient;
    /// # let client = AgentTrustClient::builder().build().unwrap();
    /// client.streaming().delete("dest-1").unwrap();
    /// ```
    pub fn delete(&self, destination_id: &str) -> Result<()> {
        let path = format!("/api/v1/siem/destinations/{}", destination_id);
        self.client
            .request_no_response("DELETE", &path, None::<&()>)
    }

    /// Retrieve the delivery log for a SIEM destination.
    ///
    /// Calls `GET /api/v1/siem/destinations/{id}/logs`.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use agenttrustid::AgentTrustClient;
    /// # let client = AgentTrustClient::builder().build().unwrap();
    /// let logs = client.streaming().delivery_log("dest-1").unwrap();
    /// for r in logs {
    ///     println!("{}: {}", r.id, r.status);
    /// }
    /// ```
    pub fn delivery_log(&self, destination_id: &str) -> Result<Vec<SIEMDeliveryRecord>> {
        let path = format!("/api/v1/siem/destinations/{}/logs", destination_id);
        let value: Value = self.client.request("GET", &path, None::<&()>)?;
        if let Value::Array(_) = &value {
            let v: Vec<SIEMDeliveryRecord> = serde_json::from_value(value)?;
            return Ok(v);
        }
        let resp: DeliveryLogResponse = serde_json::from_value(value)?;
        Ok(resp.logs.unwrap_or_default())
    }

    /// Send a test event to a SIEM destination.
    ///
    /// Calls `POST /api/v1/siem/destinations/{id}/test`.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use agenttrustid::AgentTrustClient;
    /// # let client = AgentTrustClient::builder().build().unwrap();
    /// client.streaming().test("dest-1").unwrap();
    /// ```
    pub fn test(&self, destination_id: &str) -> Result<()> {
        let path = format!("/api/v1/siem/destinations/{}/test", destination_id);
        self.client.request_no_response("POST", &path, None::<&()>)
    }

    /// Subscribe to a SIEM destination's delivery log via polling.
    ///
    /// This is a blocking polling-based stub: it repeatedly calls
    /// [`Streaming::delivery_log`] at `filter.poll_interval` and invokes
    /// `handler` for each new record. The handler should return `false` to
    /// stop the subscription.
    ///
    /// A future async release will replace this with a tokio-based SSE stream.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use agenttrustid::{AgentTrustClient, streaming::StreamFilter};
    /// # use std::time::Duration;
    /// # let client = AgentTrustClient::builder().build().unwrap();
    /// let filter = StreamFilter {
    ///     destination_id: "dest-1".into(),
    ///     poll_interval: Some(Duration::from_secs(2)),
    ///     max_polls: Some(1),
    /// };
    /// client.streaming().subscribe(&filter, |record| {
    ///     println!("delivered: {}", record.id);
    ///     true
    /// }).unwrap();
    /// ```
    pub fn subscribe<F>(&self, filter: &StreamFilter, mut handler: F) -> Result<()>
    where
        F: FnMut(&SIEMDeliveryRecord) -> bool,
    {
        let interval = filter.poll_interval.unwrap_or(Duration::from_secs(5));
        let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();
        let mut polls = 0u32;

        loop {
            let records = self.delivery_log(&filter.destination_id)?;
            for record in &records {
                if seen.insert(record.id.clone()) && !handler(record) {
                    return Ok(());
                }
            }

            polls += 1;
            if let Some(max) = filter.max_polls {
                if polls >= max {
                    return Ok(());
                }
            }
            std::thread::sleep(interval);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::error::AgentTrustError;
    use mockito::Server;

    fn destination_body() -> &'static str {
        r#"{
            "id":"d1","org_id":"o1","name":"splunk","destination_type":"splunk",
            "endpoint_url":"https://x","is_active":true,"batch_size":100,
            "flush_interval_seconds":30
        }"#
    }

    #[test]
    fn test_create_success() {
        let mut srv = Server::new();
        let mock = srv
            .mock("POST", "/api/v1/siem/destinations")
            .with_status(200)
            .with_body(destination_body())
            .create();

        let client = AgentTrustClient::builder()
            .base_url(&srv.url())
            .build()
            .unwrap();
        let d = client
            .streaming()
            .create(&CreateSIEMDestinationRequest {
                name: "splunk".into(),
                destination_type: "splunk".into(),
                endpoint_url: "https://x".into(),
                auth_token: None,
                batch_size: None,
                flush_interval_seconds: None,
                filter_event_types: None,
            })
            .unwrap();
        assert_eq!(d.id, "d1");
        mock.assert();
    }

    #[test]
    fn test_get_not_found() {
        let mut srv = Server::new();
        let mock = srv
            .mock("GET", "/api/v1/siem/destinations/missing")
            .with_status(404)
            .with_body(r#"{"message":"not found"}"#)
            .create();

        let client = AgentTrustClient::builder()
            .base_url(&srv.url())
            .build()
            .unwrap();
        let err = client.streaming().get("missing").unwrap_err();
        assert!(matches!(err, AgentTrustError::NotFound { .. }));
        mock.assert();
    }

    #[test]
    fn test_list_validation_error() {
        let mut srv = Server::new();
        let mock = srv
            .mock("GET", "/api/v1/siem/destinations")
            .with_status(400)
            .with_body(r#"{"message":"invalid"}"#)
            .create();

        let client = AgentTrustClient::builder()
            .base_url(&srv.url())
            .build()
            .unwrap();
        let err = client.streaming().list().unwrap_err();
        assert!(matches!(err, AgentTrustError::Validation { .. }));
        mock.assert();
    }

    #[test]
    fn test_subscribe_invokes_handler() {
        let mut srv = Server::new();
        let mock = srv
            .mock("GET", "/api/v1/siem/destinations/d1/logs")
            .with_status(200)
            .with_body(
                r#"{"logs":[
                    {"id":"r1","destination_id":"d1","batch_size":1,"status":"success","delivered_at":"2026-01-01T00:00:00Z"}
                ]}"#,
            )
            .create();

        let client = AgentTrustClient::builder()
            .base_url(&srv.url())
            .build()
            .unwrap();
        let mut count = 0u32;
        let filter = StreamFilter {
            destination_id: "d1".into(),
            poll_interval: Some(Duration::from_millis(1)),
            max_polls: Some(1),
        };
        client
            .streaming()
            .subscribe(&filter, |_record| {
                count += 1;
                true
            })
            .unwrap();
        assert_eq!(count, 1);
        mock.assert();
    }

    #[test]
    fn test_delivery_log_server_error() {
        let mut srv = Server::new();
        let mock = srv
            .mock("GET", "/api/v1/siem/destinations/d1/logs")
            .with_status(500)
            .with_body(r#"{"message":"boom"}"#)
            .create();

        let client = AgentTrustClient::builder()
            .base_url(&srv.url())
            .build()
            .unwrap();
        let err = client.streaming().delivery_log("d1").unwrap_err();
        match err {
            AgentTrustError::Api { status, .. } => assert_eq!(status, 500),
            other => panic!("unexpected: {:?}", other),
        }
        mock.assert();
    }
}