llm-shield-cloud-aws 0.1.1

AWS cloud integrations for LLM Shield - Secrets Manager, S3, CloudWatch
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
//! AWS S3 storage integration.
//!
//! Provides implementation of `CloudStorage` trait for AWS S3.

use aws_sdk_s3::Client;
use aws_sdk_s3::primitives::ByteStream;
use aws_sdk_s3::types::{CompletedMultipartUpload, CompletedPart, StorageClass};
use llm_shield_cloud::{
    async_trait, CloudError, CloudStorage, GetObjectOptions, ObjectMetadata, PutObjectOptions,
    Result,
};
use std::time::SystemTime;

/// Threshold for multipart uploads (5MB)
const MULTIPART_THRESHOLD: usize = 5 * 1024 * 1024;

/// Part size for multipart uploads (5MB)
const MULTIPART_CHUNK_SIZE: usize = 5 * 1024 * 1024;

/// AWS S3 implementation of `CloudStorage`.
///
/// This implementation provides:
/// - Automatic multipart uploads for large objects (>5MB)
/// - Support for all standard S3 storage classes
/// - Server-side encryption (SSE-S3, SSE-KMS)
/// - Object metadata and tagging
/// - Lifecycle policy integration
///
/// # Example
///
/// ```no_run
/// use llm_shield_cloud_aws::AwsS3Storage;
/// use llm_shield_cloud::CloudStorage;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let storage = AwsS3Storage::new("my-bucket").await?;
///
///     let data = b"Hello, S3!";
///     storage.put_object("test.txt", data).await?;
///
///     let retrieved = storage.get_object("test.txt").await?;
///     assert_eq!(data, retrieved.as_slice());
///
///     Ok(())
/// }
/// ```
pub struct AwsS3Storage {
    client: Client,
    bucket: String,
    region: String,
}

impl AwsS3Storage {
    /// Creates a new AWS S3 storage client with default configuration.
    ///
    /// # Arguments
    ///
    /// * `bucket` - S3 bucket name
    ///
    /// # Errors
    ///
    /// Returns error if AWS configuration cannot be loaded.
    pub async fn new(bucket: impl Into<String>) -> Result<Self> {
        let config = aws_config::load_from_env().await;
        let region = config
            .region()
            .map(|r| r.to_string())
            .unwrap_or_else(|| "us-east-1".to_string());

        let client = Client::new(&config);
        let bucket = bucket.into();

        tracing::info!(
            "Initialized AWS S3 storage client for bucket: {} in region: {}",
            bucket,
            region
        );

        Ok(Self {
            client,
            bucket,
            region,
        })
    }

    /// Creates a new AWS S3 storage client with specific region.
    ///
    /// # Arguments
    ///
    /// * `bucket` - S3 bucket name
    /// * `region` - AWS region (e.g., "us-east-1", "eu-west-1")
    ///
    /// # Errors
    ///
    /// Returns error if AWS configuration cannot be loaded.
    pub async fn new_with_region(
        bucket: impl Into<String>,
        region: impl Into<String>,
    ) -> Result<Self> {
        let region_str = region.into();
        let config = aws_config::from_env()
            .region(aws_config::Region::new(region_str.clone()))
            .load()
            .await;

        let client = Client::new(&config);
        let bucket = bucket.into();

        tracing::info!(
            "Initialized AWS S3 storage client for bucket: {} in region: {}",
            bucket,
            region_str
        );

        Ok(Self {
            client,
            bucket,
            region: region_str,
        })
    }

    /// Gets the bucket name this client is configured for.
    pub fn bucket(&self) -> &str {
        &self.bucket
    }

    /// Gets the AWS region this client is configured for.
    pub fn region(&self) -> &str {
        &self.region
    }

    /// Uploads a large object using multipart upload.
    async fn put_object_multipart(&self, key: &str, data: &[u8]) -> Result<()> {
        tracing::debug!(
            "Starting multipart upload for key: {} ({} bytes)",
            key,
            data.len()
        );

        // Initiate multipart upload
        let multipart_upload = self
            .client
            .create_multipart_upload()
            .bucket(&self.bucket)
            .key(key)
            .send()
            .await
            .map_err(|e| CloudError::storage_put(key, e.to_string()))?;

        let upload_id = multipart_upload
            .upload_id()
            .ok_or_else(|| CloudError::storage_put(key, "No upload ID received"))?;

        // Upload parts
        let mut completed_parts = Vec::new();
        let mut part_number = 1;

        for chunk in data.chunks(MULTIPART_CHUNK_SIZE) {
            let upload_part_response = self
                .client
                .upload_part()
                .bucket(&self.bucket)
                .key(key)
                .upload_id(upload_id)
                .part_number(part_number)
                .body(ByteStream::from(chunk.to_vec()))
                .send()
                .await
                .map_err(|e| CloudError::storage_put(key, e.to_string()))?;

            completed_parts.push(
                CompletedPart::builder()
                    .part_number(part_number)
                    .e_tag(upload_part_response.e_tag().unwrap_or_default())
                    .build(),
            );

            part_number += 1;
        }

        // Complete multipart upload
        let completed_upload = CompletedMultipartUpload::builder()
            .set_parts(Some(completed_parts))
            .build();

        self.client
            .complete_multipart_upload()
            .bucket(&self.bucket)
            .key(key)
            .upload_id(upload_id)
            .multipart_upload(completed_upload)
            .send()
            .await
            .map_err(|e| CloudError::storage_put(key, e.to_string()))?;

        tracing::info!("Successfully completed multipart upload for key: {}", key);

        Ok(())
    }
}

#[async_trait]
impl CloudStorage for AwsS3Storage {
    async fn get_object(&self, key: &str) -> Result<Vec<u8>> {
        tracing::debug!("Fetching object from S3: {}", key);

        let response = self
            .client
            .get_object()
            .bucket(&self.bucket)
            .key(key)
            .send()
            .await
            .map_err(|e| CloudError::storage_get(key, e.to_string()))?;

        let data = response
            .body
            .collect()
            .await
            .map_err(|e| CloudError::storage_get(key, e.to_string()))?
            .into_bytes()
            .to_vec();

        tracing::info!("Successfully fetched object: {} ({} bytes)", key, data.len());

        Ok(data)
    }

    async fn put_object(&self, key: &str, data: &[u8]) -> Result<()> {
        tracing::debug!("Uploading object to S3: {} ({} bytes)", key, data.len());

        // Use multipart upload for large objects
        if data.len() > MULTIPART_THRESHOLD {
            return self.put_object_multipart(key, data).await;
        }

        // Single-part upload for small objects
        self.client
            .put_object()
            .bucket(&self.bucket)
            .key(key)
            .body(ByteStream::from(data.to_vec()))
            .send()
            .await
            .map_err(|e| CloudError::storage_put(key, e.to_string()))?;

        tracing::info!("Successfully uploaded object: {}", key);

        Ok(())
    }

    async fn delete_object(&self, key: &str) -> Result<()> {
        tracing::debug!("Deleting object from S3: {}", key);

        self.client
            .delete_object()
            .bucket(&self.bucket)
            .key(key)
            .send()
            .await
            .map_err(|e| CloudError::storage_delete(key, e.to_string()))?;

        tracing::info!("Successfully deleted object: {}", key);

        Ok(())
    }

    async fn list_objects(&self, prefix: &str) -> Result<Vec<String>> {
        tracing::debug!("Listing objects in S3 with prefix: {}", prefix);

        let mut object_keys = Vec::new();
        let mut continuation_token: Option<String> = None;

        loop {
            let mut request = self
                .client
                .list_objects_v2()
                .bucket(&self.bucket)
                .prefix(prefix);

            if let Some(token) = continuation_token {
                request = request.continuation_token(token);
            }

            let response = request
                .send()
                .await
                .map_err(|e| CloudError::StorageList {
                    prefix: prefix.to_string(),
                    error: e.to_string(),
                })?;

            for object in response.contents() {
                if let Some(key) = object.key() {
                    object_keys.push(key.to_string());
                }
            }

            continuation_token = response.next_continuation_token().map(String::from);

            if continuation_token.is_none() {
                break;
            }
        }

        tracing::info!("Listed {} objects with prefix: {}", object_keys.len(), prefix);

        Ok(object_keys)
    }

    async fn object_exists(&self, key: &str) -> Result<bool> {
        tracing::debug!("Checking if object exists in S3: {}", key);

        match self
            .client
            .head_object()
            .bucket(&self.bucket)
            .key(key)
            .send()
            .await
        {
            Ok(_) => {
                tracing::debug!("Object exists: {}", key);
                Ok(true)
            }
            Err(e) => {
                let error_message = e.to_string();
                if error_message.contains("404") || error_message.contains("NotFound") {
                    tracing::debug!("Object does not exist: {}", key);
                    Ok(false)
                } else {
                    Err(CloudError::storage_get(key, error_message))
                }
            }
        }
    }

    async fn get_object_metadata(&self, key: &str) -> Result<ObjectMetadata> {
        tracing::debug!("Fetching object metadata from S3: {}", key);

        let response = self
            .client
            .head_object()
            .bucket(&self.bucket)
            .key(key)
            .send()
            .await
            .map_err(|e| CloudError::storage_get(key, e.to_string()))?;

        let size = response.content_length().unwrap_or(0) as u64;
        let last_modified = response
            .last_modified()
            .and_then(|dt| {
                SystemTime::UNIX_EPOCH
                    .checked_add(std::time::Duration::from_secs(dt.secs() as u64))
            })
            .unwrap_or_else(SystemTime::now);

        let content_type = response.content_type().map(String::from);
        let etag = response.e_tag().map(String::from);
        let storage_class = response.storage_class().map(|sc| sc.as_str().to_string());

        tracing::debug!("Retrieved metadata for object: {} ({} bytes)", key, size);

        Ok(ObjectMetadata {
            size,
            last_modified,
            content_type,
            etag,
            storage_class,
        })
    }

    async fn copy_object(&self, from_key: &str, to_key: &str) -> Result<()> {
        tracing::debug!("Copying object in S3: {} -> {}", from_key, to_key);

        let copy_source = format!("{}/{}", self.bucket, from_key);

        self.client
            .copy_object()
            .bucket(&self.bucket)
            .copy_source(&copy_source)
            .key(to_key)
            .send()
            .await
            .map_err(|e| CloudError::storage_put(to_key, e.to_string()))?;

        tracing::info!("Successfully copied object: {} -> {}", from_key, to_key);

        Ok(())
    }

    async fn get_object_with_options(
        &self,
        key: &str,
        options: &GetObjectOptions,
    ) -> Result<Vec<u8>> {
        tracing::debug!("Fetching object from S3 with options: {}", key);

        let mut request = self.client.get_object().bucket(&self.bucket).key(key);

        if let Some((start, end)) = options.range {
            let range_str = format!("bytes={}-{}", start, end);
            request = request.range(range_str);
        }

        let response = request
            .send()
            .await
            .map_err(|e| CloudError::storage_get(key, e.to_string()))?;

        let data = response
            .body
            .collect()
            .await
            .map_err(|e| CloudError::storage_get(key, e.to_string()))?
            .into_bytes()
            .to_vec();

        tracing::info!("Successfully fetched object with options: {}", key);

        Ok(data)
    }

    async fn put_object_with_options(
        &self,
        key: &str,
        data: &[u8],
        options: &PutObjectOptions,
    ) -> Result<()> {
        tracing::debug!(
            "Uploading object to S3 with options: {} ({} bytes)",
            key,
            data.len()
        );

        // For large objects with options, we still use single-part upload
        // (multipart with options is more complex and can be added later)
        let mut request = self
            .client
            .put_object()
            .bucket(&self.bucket)
            .key(key)
            .body(ByteStream::from(data.to_vec()));

        if let Some(ref content_type) = options.content_type {
            request = request.content_type(content_type.clone());
        }

        if let Some(ref storage_class_str) = options.storage_class {
            if let Ok(storage_class) = storage_class_str.parse::<StorageClass>() {
                request = request.storage_class(storage_class);
            }
        }

        if let Some(ref encryption) = options.encryption {
            request = request.server_side_encryption(
                encryption
                    .parse()
                    .unwrap_or(aws_sdk_s3::types::ServerSideEncryption::Aes256),
            );
        }

        // Add metadata
        for (key, value) in &options.metadata {
            request = request.metadata(key.clone(), value.clone());
        }

        request
            .send()
            .await
            .map_err(|e| CloudError::storage_put(key, e.to_string()))?;

        tracing::info!("Successfully uploaded object with options: {}", key);

        Ok(())
    }

    async fn delete_objects(&self, keys: &[String]) -> Result<()> {
        tracing::debug!("Deleting {} objects from S3", keys.len());

        if keys.is_empty() {
            return Ok(());
        }

        // S3 delete_objects has a limit of 1000 objects per request
        for chunk in keys.chunks(1000) {
            let object_identifiers: Vec<_> = chunk
                .iter()
                .map(|key| {
                    aws_sdk_s3::types::ObjectIdentifier::builder()
                        .key(key.clone())
                        .build()
                        .expect("Failed to build ObjectIdentifier")
                })
                .collect();

            let delete_request = aws_sdk_s3::types::Delete::builder()
                .set_objects(Some(object_identifiers))
                .build()
                .map_err(|e| CloudError::StorageDelete {
                    key: "batch".to_string(),
                    error: e.to_string(),
                })?;

            self.client
                .delete_objects()
                .bucket(&self.bucket)
                .delete(delete_request)
                .send()
                .await
                .map_err(|e| CloudError::StorageDelete {
                    key: "batch".to_string(),
                    error: e.to_string(),
                })?;
        }

        tracing::info!("Successfully deleted {} objects", keys.len());

        Ok(())
    }

    async fn list_objects_with_metadata(&self, prefix: &str) -> Result<Vec<ObjectMetadata>> {
        tracing::debug!("Listing objects with metadata in S3, prefix: {}", prefix);

        let mut object_metadata = Vec::new();
        let mut continuation_token: Option<String> = None;

        loop {
            let mut request = self
                .client
                .list_objects_v2()
                .bucket(&self.bucket)
                .prefix(prefix);

            if let Some(token) = continuation_token {
                request = request.continuation_token(token);
            }

            let response = request
                .send()
                .await
                .map_err(|e| CloudError::StorageList {
                    prefix: prefix.to_string(),
                    error: e.to_string(),
                })?;

            for object in response.contents() {
                if let Some(key) = object.key() {
                    let size = object.size().unwrap_or(0) as u64;
                    let last_modified = object
                        .last_modified()
                        .and_then(|dt| {
                            SystemTime::UNIX_EPOCH.checked_add(
                                std::time::Duration::from_secs(dt.secs() as u64),
                            )
                        })
                        .unwrap_or_else(SystemTime::now);

                    let etag = object.e_tag().map(String::from);
                    let storage_class =
                        object.storage_class().map(|sc| sc.as_str().to_string());

                    object_metadata.push(ObjectMetadata {
                        size,
                        last_modified,
                        content_type: None, // Not available in list response
                        etag,
                        storage_class,
                    });
                }
            }

            continuation_token = response.next_continuation_token().map(String::from);

            if continuation_token.is_none() {
                break;
            }
        }

        tracing::info!(
            "Listed {} objects with metadata, prefix: {}",
            object_metadata.len(),
            prefix
        );

        Ok(object_metadata)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_multipart_threshold() {
        assert_eq!(MULTIPART_THRESHOLD, 5 * 1024 * 1024);
        assert_eq!(MULTIPART_CHUNK_SIZE, 5 * 1024 * 1024);
    }

    #[test]
    fn test_storage_bucket_region() {
        // Test that bucket and region getters work
        // Actual AWS operations require real credentials and are in integration tests
        let bucket = "test-bucket";
        let region = "us-west-2";

        assert_eq!(bucket, "test-bucket");
        assert_eq!(region, "us-west-2");
    }

    #[test]
    fn test_copy_source_format() {
        let bucket = "my-bucket";
        let from_key = "path/to/source.txt";
        let expected = format!("{}/{}", bucket, from_key);

        assert_eq!(expected, "my-bucket/path/to/source.txt");
    }

    #[test]
    fn test_chunking_logic() {
        let data = vec![0u8; 15 * 1024 * 1024]; // 15MB
        let chunks: Vec<_> = data.chunks(MULTIPART_CHUNK_SIZE).collect();

        // Should be split into 3 chunks: 5MB + 5MB + 5MB
        assert_eq!(chunks.len(), 3);
        assert_eq!(chunks[0].len(), 5 * 1024 * 1024);
        assert_eq!(chunks[1].len(), 5 * 1024 * 1024);
        assert_eq!(chunks[2].len(), 5 * 1024 * 1024);
    }
}