rustack-s3-core 0.9.0

S3 service implementation for Rustack
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
//! Snapshot support for S3 buckets, metadata, multipart uploads, and object bodies.

use std::{
    collections::HashMap,
    path::{Component, Path, PathBuf},
};

use serde::{Deserialize, Serialize};
use thiserror::Error;
use tokio::io::AsyncWriteExt as _;

use crate::{
    provider::RustackS3,
    state::{
        bucket::{
            BucketEncryption, CorsRuleConfig, ObjectLockConfiguration, OwnershipControlsConfig,
            PublicAccessBlockConfig, S3Bucket, VersioningStatus, WebsiteConfig,
        },
        multipart::MultipartUpload,
        object::{ObjectVersion, Owner, S3DeleteMarker, S3Object},
    },
};

/// Errors raised while exporting or importing S3 snapshots.
#[derive(Debug, Error)]
pub enum S3SnapshotError {
    /// Bucket referenced in the bucket list disappeared during export.
    #[error("bucket disappeared during snapshot export: {bucket}")]
    BucketDisappeared {
        /// Bucket name.
        bucket: String,
    },
    /// Object data could not be read through the storage backend.
    #[error("failed to read S3 object data for {bucket}/{key}@{version_id}: {source}")]
    ReadObject {
        /// Bucket name.
        bucket: String,
        /// Object key.
        key: String,
        /// Version identifier.
        version_id: String,
        /// Source error.
        #[source]
        source: Box<crate::error::S3ServiceError>,
    },
    /// Multipart part data could not be read through the storage backend.
    #[error("failed to read S3 multipart data for {bucket}/{upload_id}/{part_number}: {source}")]
    ReadPart {
        /// Bucket name.
        bucket: String,
        /// Multipart upload identifier.
        upload_id: String,
        /// Part number.
        part_number: u32,
        /// Source error.
        #[source]
        source: Box<crate::error::S3ServiceError>,
    },
    /// Object data could not be written through the storage backend.
    #[error("failed to restore S3 object data for {bucket}/{key}@{version_id}: {source}")]
    WriteObject {
        /// Bucket name.
        bucket: String,
        /// Object key.
        key: String,
        /// Version identifier.
        version_id: String,
        /// Source error.
        #[source]
        source: Box<crate::error::S3ServiceError>,
    },
    /// Multipart part data could not be written through the storage backend.
    #[error("failed to restore S3 multipart data for {bucket}/{upload_id}/{part_number}: {source}")]
    WritePart {
        /// Bucket name.
        bucket: String,
        /// Multipart upload identifier.
        upload_id: String,
        /// Part number.
        part_number: u32,
        /// Source error.
        #[source]
        source: Box<crate::error::S3ServiceError>,
    },
    /// File-system I/O failed.
    #[error("S3 snapshot I/O failed at {path}: {source}")]
    Io {
        /// Path being accessed.
        path: String,
        /// Source error.
        #[source]
        source: std::io::Error,
    },
    /// Snapshot metadata referenced a data path outside its data directory.
    #[error("invalid S3 snapshot data path: {path}")]
    InvalidDataPath {
        /// Invalid relative path.
        path: String,
    },
}

/// Serializable S3 service snapshot.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct S3Snapshot {
    /// Bucket snapshots.
    pub buckets: Vec<S3BucketSnapshot>,
}

/// Serializable S3 bucket snapshot.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct S3BucketSnapshot {
    /// Bucket name.
    pub name: String,
    /// Bucket region.
    pub region: String,
    /// Creation date.
    pub creation_date: chrono::DateTime<chrono::Utc>,
    /// Bucket owner.
    pub owner: Owner,
    /// Whether the object store is versioned internally.
    pub object_store_versioned: bool,
    /// Object versions and delete markers.
    pub object_versions: Vec<S3ObjectVersionSnapshot>,
    /// In-progress multipart uploads.
    pub multipart_uploads: Vec<S3MultipartUploadSnapshot>,
    /// Bucket versioning status.
    pub versioning: VersioningStatus,
    /// Bucket encryption settings.
    pub encryption: Option<BucketEncryption>,
    /// CORS rules.
    pub cors_rules: Option<Vec<CorsRuleConfig>>,
    /// Lifecycle configuration.
    pub lifecycle: Option<rustack_s3_model::types::BucketLifecycleConfiguration>,
    /// Bucket policy JSON.
    pub policy: Option<String>,
    /// Bucket tags.
    pub tags: Vec<(String, String)>,
    /// Canned ACL.
    pub acl: crate::state::object::CannedAcl,
    /// Notification configuration.
    pub notification_configuration: Option<rustack_s3_model::types::NotificationConfiguration>,
    /// Logging configuration.
    pub logging: Option<serde_json::Value>,
    /// Public access block settings.
    pub public_access_block: Option<PublicAccessBlockConfig>,
    /// Ownership controls.
    pub ownership_controls: Option<OwnershipControlsConfig>,
    /// Whether object lock is enabled.
    pub object_lock_enabled: bool,
    /// Object lock configuration.
    pub object_lock_configuration: Option<ObjectLockConfiguration>,
    /// Transfer acceleration status.
    pub accelerate: Option<String>,
    /// Request payment configuration.
    pub request_payment: String,
    /// Static website hosting configuration.
    pub website: Option<WebsiteConfig>,
    /// Replication configuration.
    pub replication: Option<serde_json::Value>,
    /// Analytics configuration.
    pub analytics: Option<serde_json::Value>,
    /// Metrics configuration.
    pub metrics: Option<serde_json::Value>,
    /// Inventory configuration.
    pub inventory: Option<serde_json::Value>,
    /// Intelligent-tiering configuration.
    pub intelligent_tiering: Option<serde_json::Value>,
}

/// Object version snapshot with external body file reference.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", tag = "type")]
pub enum S3ObjectVersionSnapshot {
    /// Real object version.
    Object {
        /// Object metadata.
        object: Box<S3Object>,
        /// Body file relative to the S3 data directory.
        body_file: String,
    },
    /// Delete marker version.
    DeleteMarker(S3DeleteMarker),
}

/// Multipart upload snapshot with external part body file references.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct S3MultipartUploadSnapshot {
    /// Multipart upload metadata.
    pub upload: MultipartUpload,
    /// Part number to body file relative to the S3 data directory.
    pub part_body_files: HashMap<u32, String>,
}

impl RustackS3 {
    /// Export S3 state and object bodies into a snapshot.
    ///
    /// # Errors
    ///
    /// Returns an error if object bodies cannot be read or written.
    pub async fn export_snapshot(&self, data_dir: &Path) -> Result<S3Snapshot, S3SnapshotError> {
        create_dir_all(data_dir).await?;
        let mut buckets = Vec::new();
        let mut body_index = 0usize;

        for bucket_name in self.state.snapshot_bucket_names() {
            let bucket = self.state.get_bucket(&bucket_name).map_err(|_| {
                S3SnapshotError::BucketDisappeared {
                    bucket: bucket_name.clone(),
                }
            })?;
            let (object_store_versioned, versions) = bucket.objects.read().snapshot_versions();
            let mut object_versions = Vec::with_capacity(versions.len());

            for version in versions {
                match version {
                    ObjectVersion::Object(object) => {
                        let body_file = format!("objects/{body_index}.bin");
                        body_index = body_index.saturating_add(1);
                        let data = self
                            .storage
                            .read_object(&bucket.name, &object.key, &object.version_id, None)
                            .await
                            .map_err(|source| S3SnapshotError::ReadObject {
                                bucket: bucket.name.clone(),
                                key: object.key.clone(),
                                version_id: object.version_id.clone(),
                                source: Box::new(source),
                            })?;
                        write_data_file(data_dir, &body_file, &data).await?;
                        object_versions.push(S3ObjectVersionSnapshot::Object { object, body_file });
                    }
                    ObjectVersion::DeleteMarker(marker) => {
                        object_versions.push(S3ObjectVersionSnapshot::DeleteMarker(marker));
                    }
                }
            }

            let mut multipart_uploads = Vec::new();
            for entry in &bucket.multipart_uploads {
                let upload = entry.value().clone();
                let mut part_body_files = HashMap::new();
                for part_number in upload.parts.keys() {
                    let body_file = format!("parts/{body_index}.bin");
                    body_index = body_index.saturating_add(1);
                    let data = self
                        .storage
                        .read_part(&bucket.name, &upload.upload_id, *part_number)
                        .await
                        .map_err(|source| S3SnapshotError::ReadPart {
                            bucket: bucket.name.clone(),
                            upload_id: upload.upload_id.clone(),
                            part_number: *part_number,
                            source: Box::new(source),
                        })?;
                    write_data_file(data_dir, &body_file, &data).await?;
                    part_body_files.insert(*part_number, body_file);
                }
                multipart_uploads.push(S3MultipartUploadSnapshot {
                    upload,
                    part_body_files,
                });
            }

            buckets.push(S3BucketSnapshot {
                name: bucket.name.clone(),
                region: bucket.region.clone(),
                creation_date: bucket.creation_date,
                owner: bucket.owner.clone(),
                object_store_versioned,
                object_versions,
                multipart_uploads,
                versioning: *bucket.versioning.read(),
                encryption: bucket.encryption.read().clone(),
                cors_rules: bucket.cors_rules.read().clone(),
                lifecycle: bucket.lifecycle.read().clone(),
                policy: bucket.policy.read().clone(),
                tags: bucket.tags.read().clone(),
                acl: *bucket.acl.read(),
                notification_configuration: bucket.notification_configuration.read().clone(),
                logging: bucket.logging.read().clone(),
                public_access_block: bucket.public_access_block.read().clone(),
                ownership_controls: bucket.ownership_controls.read().clone(),
                object_lock_enabled: *bucket.object_lock_enabled.read(),
                object_lock_configuration: bucket.object_lock_configuration.read().clone(),
                accelerate: bucket.accelerate.read().clone(),
                request_payment: bucket.request_payment.read().clone(),
                website: bucket.website.read().clone(),
                replication: bucket.replication.read().clone(),
                analytics: bucket.analytics.read().clone(),
                metrics: bucket.metrics.read().clone(),
                inventory: bucket.inventory.read().clone(),
                intelligent_tiering: bucket.intelligent_tiering.read().clone(),
            });
        }

        Ok(S3Snapshot { buckets })
    }

    /// Import S3 state and object bodies from a snapshot.
    ///
    /// # Errors
    ///
    /// Returns an error if object body files cannot be read or restored.
    pub async fn import_snapshot(
        &self,
        snapshot: S3Snapshot,
        data_dir: &Path,
    ) -> Result<(), S3SnapshotError> {
        self.reset();

        for bucket_snapshot in snapshot.buckets {
            let bucket = build_bucket_from_snapshot(&bucket_snapshot);
            let mut versions = Vec::with_capacity(bucket_snapshot.object_versions.len());

            for version in bucket_snapshot.object_versions {
                match version {
                    S3ObjectVersionSnapshot::Object { object, body_file } => {
                        let data = read_data_file(data_dir, &body_file).await?;
                        self.storage
                            .write_object(&bucket.name, &object.key, &object.version_id, data)
                            .await
                            .map_err(|source| S3SnapshotError::WriteObject {
                                bucket: bucket.name.clone(),
                                key: object.key.clone(),
                                version_id: object.version_id.clone(),
                                source: Box::new(source),
                            })?;
                        versions.push(ObjectVersion::Object(object));
                    }
                    S3ObjectVersionSnapshot::DeleteMarker(marker) => {
                        versions.push(ObjectVersion::DeleteMarker(marker));
                    }
                }
            }

            bucket
                .objects
                .write()
                .replace_from_snapshot(bucket_snapshot.object_store_versioned, versions);

            for multipart in bucket_snapshot.multipart_uploads {
                for (part_number, body_file) in &multipart.part_body_files {
                    let data = read_data_file(data_dir, body_file).await?;
                    self.storage
                        .write_part(
                            &bucket.name,
                            &multipart.upload.upload_id,
                            *part_number,
                            data,
                        )
                        .await
                        .map_err(|source| S3SnapshotError::WritePart {
                            bucket: bucket.name.clone(),
                            upload_id: multipart.upload.upload_id.clone(),
                            part_number: *part_number,
                            source: Box::new(source),
                        })?;
                }
                bucket
                    .multipart_uploads
                    .insert(multipart.upload.upload_id.clone(), multipart.upload);
            }

            self.state.insert_snapshot_bucket(bucket);
        }

        Ok(())
    }
}

fn build_bucket_from_snapshot(snapshot: &S3BucketSnapshot) -> S3Bucket {
    let bucket = S3Bucket::new(
        snapshot.name.clone(),
        snapshot.region.clone(),
        snapshot.owner.clone(),
    );
    let mut bucket = bucket;
    bucket.creation_date = snapshot.creation_date;
    *bucket.versioning.write() = snapshot.versioning;
    (*bucket.encryption.write()).clone_from(&snapshot.encryption);
    (*bucket.cors_rules.write()).clone_from(&snapshot.cors_rules);
    (*bucket.lifecycle.write()).clone_from(&snapshot.lifecycle);
    (*bucket.policy.write()).clone_from(&snapshot.policy);
    (*bucket.tags.write()).clone_from(&snapshot.tags);
    *bucket.acl.write() = snapshot.acl;
    (*bucket.notification_configuration.write()).clone_from(&snapshot.notification_configuration);
    (*bucket.logging.write()).clone_from(&snapshot.logging);
    (*bucket.public_access_block.write()).clone_from(&snapshot.public_access_block);
    (*bucket.ownership_controls.write()).clone_from(&snapshot.ownership_controls);
    *bucket.object_lock_enabled.write() = snapshot.object_lock_enabled;
    (*bucket.object_lock_configuration.write()).clone_from(&snapshot.object_lock_configuration);
    (*bucket.accelerate.write()).clone_from(&snapshot.accelerate);
    (*bucket.request_payment.write()).clone_from(&snapshot.request_payment);
    (*bucket.website.write()).clone_from(&snapshot.website);
    (*bucket.replication.write()).clone_from(&snapshot.replication);
    (*bucket.analytics.write()).clone_from(&snapshot.analytics);
    (*bucket.metrics.write()).clone_from(&snapshot.metrics);
    (*bucket.inventory.write()).clone_from(&snapshot.inventory);
    (*bucket.intelligent_tiering.write()).clone_from(&snapshot.intelligent_tiering);
    bucket
}

async fn create_dir_all(path: &Path) -> Result<(), S3SnapshotError> {
    tokio::fs::create_dir_all(path)
        .await
        .map_err(|source| S3SnapshotError::Io {
            path: path.display().to_string(),
            source,
        })
}

async fn write_data_file(root: &Path, relative: &str, data: &[u8]) -> Result<(), S3SnapshotError> {
    let path = data_file_path(root, relative)?;
    if let Some(parent) = path.parent() {
        create_dir_all(parent).await?;
    }
    let mut file = tokio::fs::File::create(&path)
        .await
        .map_err(|source| S3SnapshotError::Io {
            path: path.display().to_string(),
            source,
        })?;
    file.write_all(data)
        .await
        .map_err(|source| S3SnapshotError::Io {
            path: path.display().to_string(),
            source,
        })
}

async fn read_data_file(root: &Path, relative: &str) -> Result<bytes::Bytes, S3SnapshotError> {
    let path = data_file_path(root, relative)?;
    tokio::fs::read(&path)
        .await
        .map(bytes::Bytes::from)
        .map_err(|source| S3SnapshotError::Io {
            path: path.display().to_string(),
            source,
        })
}

fn data_file_path(root: &Path, relative: &str) -> Result<PathBuf, S3SnapshotError> {
    let path = Path::new(relative);
    if path.is_absolute()
        || path
            .components()
            .any(|component| !matches!(component, Component::Normal(_)))
    {
        return Err(S3SnapshotError::InvalidDataPath {
            path: relative.to_owned(),
        });
    }
    Ok(root.join(path))
}

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

    #[test]
    fn test_should_reject_data_file_path_traversal() {
        let path = data_file_path(Path::new("/tmp/s3"), "../outside.bin");
        assert!(path.is_err());
    }

    #[test]
    fn test_should_accept_data_file_child_path() {
        let path = data_file_path(Path::new("/tmp/s3"), "objects/body.bin");
        assert!(path.is_ok());
    }
}