mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-11 15:46:53 +00:00
refactor: replace chrono with jiff for time handling (#1582)
Co-authored-by: houseme <housemecn@gmail.com>
This commit is contained in:
@@ -32,7 +32,7 @@ workspace = true
|
||||
async-trait = { workspace = true }
|
||||
tokio = { workspace = true, features = ["full"] }
|
||||
uuid = { workspace = true, features = ["serde"] }
|
||||
chrono = { workspace = true, features = ["serde"] }
|
||||
jiff = { workspace = true }
|
||||
serde = { workspace = true, features = ["derive"] }
|
||||
serde_json = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
|
||||
@@ -24,6 +24,7 @@ use aes_gcm::{
|
||||
aead::{Aead, KeyInit},
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
use jiff::Zoned;
|
||||
use rand::Rng;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
@@ -51,8 +52,8 @@ struct StoredMasterKey {
|
||||
status: KeyStatus,
|
||||
description: Option<String>,
|
||||
metadata: HashMap<String, String>,
|
||||
created_at: chrono::DateTime<chrono::Utc>,
|
||||
rotated_at: Option<chrono::DateTime<chrono::Utc>>,
|
||||
created_at: Zoned,
|
||||
rotated_at: Option<Zoned>,
|
||||
created_by: Option<String>,
|
||||
/// Encrypted key material (32 bytes for AES-256)
|
||||
encrypted_key_material: Vec<u8>,
|
||||
@@ -69,7 +70,7 @@ struct DataKeyEnvelope {
|
||||
encrypted_key: Vec<u8>,
|
||||
nonce: Vec<u8>,
|
||||
encryption_context: HashMap<String, String>,
|
||||
created_at: chrono::DateTime<chrono::Utc>,
|
||||
created_at: Zoned,
|
||||
}
|
||||
|
||||
impl LocalKmsClient {
|
||||
@@ -182,8 +183,8 @@ impl LocalKmsClient {
|
||||
status: master_key.status.clone(),
|
||||
description: master_key.description.clone(),
|
||||
metadata: master_key.metadata.clone(),
|
||||
created_at: master_key.created_at,
|
||||
rotated_at: master_key.rotated_at,
|
||||
created_at: master_key.created_at.clone(),
|
||||
rotated_at: master_key.rotated_at.clone(),
|
||||
created_by: master_key.created_by.clone(),
|
||||
encrypted_key_material,
|
||||
nonce,
|
||||
@@ -317,7 +318,7 @@ impl KmsClient for LocalKmsClient {
|
||||
encrypted_key: encrypted_key.clone(),
|
||||
nonce,
|
||||
encryption_context: request.encryption_context.clone(),
|
||||
created_at: chrono::Utc::now(),
|
||||
created_at: Zoned::now(),
|
||||
};
|
||||
|
||||
// Serialize the envelope as the ciphertext
|
||||
@@ -561,7 +562,7 @@ impl KmsClient for LocalKmsClient {
|
||||
|
||||
let mut master_key = self.load_master_key(key_id).await?;
|
||||
master_key.version += 1;
|
||||
master_key.rotated_at = Some(chrono::Utc::now());
|
||||
master_key.rotated_at = Some(Zoned::now());
|
||||
|
||||
// Generate new key material
|
||||
let key_material = Self::generate_key_material();
|
||||
@@ -648,7 +649,7 @@ impl KmsBackend for LocalKmsBackend {
|
||||
key_state: KeyState::Enabled,
|
||||
key_usage: request.key_usage,
|
||||
description: request.description,
|
||||
creation_date: chrono::Utc::now(),
|
||||
creation_date: Zoned::now(),
|
||||
deletion_date: None,
|
||||
origin: "KMS".to_string(),
|
||||
key_manager: "CUSTOMER".to_string(),
|
||||
@@ -768,7 +769,7 @@ impl KmsBackend for LocalKmsBackend {
|
||||
key_usage: master_key.usage,
|
||||
key_state: KeyState::PendingDeletion, // AWS KMS compatibility
|
||||
creation_date: master_key.created_at,
|
||||
deletion_date: Some(chrono::Utc::now()),
|
||||
deletion_date: Some(Zoned::now()),
|
||||
key_manager: "CUSTOMER".to_string(),
|
||||
origin: "AWS_KMS".to_string(),
|
||||
tags: master_key.metadata,
|
||||
@@ -786,10 +787,10 @@ impl KmsBackend for LocalKmsBackend {
|
||||
return Err(KmsError::invalid_parameter("pending_window_in_days must be between 7 and 30".to_string()));
|
||||
}
|
||||
|
||||
let deletion_date = chrono::Utc::now() + chrono::Duration::days(days as i64);
|
||||
let deletion_date = Zoned::now() + jiff::Span::new().days(days as i64);
|
||||
master_key.status = KeyStatus::PendingDeletion;
|
||||
|
||||
(Some(deletion_date.to_rfc3339()), Some(deletion_date))
|
||||
(Some(deletion_date.to_string()), Some(deletion_date))
|
||||
};
|
||||
|
||||
// Save the updated key to disk - preserve existing key material!
|
||||
|
||||
@@ -20,6 +20,7 @@ use crate::error::{KmsError, Result};
|
||||
use crate::types::*;
|
||||
use async_trait::async_trait;
|
||||
use base64::{Engine as _, engine::general_purpose};
|
||||
use jiff::Zoned;
|
||||
use rand::RngCore;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
@@ -47,7 +48,7 @@ struct VaultKeyData {
|
||||
/// Key usage type
|
||||
usage: KeyUsage,
|
||||
/// Key creation timestamp
|
||||
created_at: chrono::DateTime<chrono::Utc>,
|
||||
created_at: Zoned,
|
||||
/// Key status
|
||||
status: KeyStatus,
|
||||
/// Key version
|
||||
@@ -156,7 +157,7 @@ impl VaultKmsClient {
|
||||
let key_data = VaultKeyData {
|
||||
algorithm: "AES_256".to_string(),
|
||||
usage: request.key_usage.clone(),
|
||||
created_at: chrono::Utc::now(),
|
||||
created_at: Zoned::now(),
|
||||
status: KeyStatus::Active,
|
||||
version: 1,
|
||||
description: request.description.clone(),
|
||||
@@ -252,7 +253,7 @@ impl KmsClient for VaultKmsClient {
|
||||
.map_err(|e| KmsError::cryptographic_error("decode", e.to_string()))?,
|
||||
key_spec: request.key_spec.clone(),
|
||||
metadata: request.encryption_context.clone(),
|
||||
created_at: chrono::Utc::now(),
|
||||
created_at: Zoned::now(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -302,7 +303,7 @@ impl KmsClient for VaultKmsClient {
|
||||
let key_data = VaultKeyData {
|
||||
algorithm: algorithm.to_string(),
|
||||
usage: KeyUsage::EncryptDecrypt,
|
||||
created_at: chrono::Utc::now(),
|
||||
created_at: Zoned::now(),
|
||||
status: KeyStatus::Active,
|
||||
version: 1,
|
||||
description: None,
|
||||
@@ -458,7 +459,7 @@ impl KmsClient for VaultKmsClient {
|
||||
description: None, // Rotate preserves existing description (would need key lookup)
|
||||
metadata: key_data.metadata,
|
||||
created_at: key_data.created_at,
|
||||
rotated_at: Some(chrono::Utc::now()),
|
||||
rotated_at: Some(Zoned::now()),
|
||||
created_by: None,
|
||||
};
|
||||
|
||||
@@ -549,7 +550,7 @@ impl KmsBackend for VaultKmsBackend {
|
||||
key_state: KeyState::Enabled,
|
||||
key_usage: request.key_usage,
|
||||
description: request.description,
|
||||
creation_date: chrono::Utc::now(),
|
||||
creation_date: Zoned::now(),
|
||||
deletion_date: None,
|
||||
origin: "VAULT".to_string(),
|
||||
key_manager: "VAULT".to_string(),
|
||||
@@ -664,7 +665,7 @@ impl KmsBackend for VaultKmsBackend {
|
||||
} else {
|
||||
// For non-pending keys, mark as PendingDeletion
|
||||
key_metadata.key_state = KeyState::PendingDeletion;
|
||||
key_metadata.deletion_date = Some(chrono::Utc::now());
|
||||
key_metadata.deletion_date = Some(Zoned::now());
|
||||
|
||||
// Update the key metadata in Vault storage to reflect the new state
|
||||
self.update_key_metadata_in_storage(key_id, &key_metadata).await?;
|
||||
@@ -680,14 +681,14 @@ impl KmsBackend for VaultKmsBackend {
|
||||
));
|
||||
}
|
||||
|
||||
let deletion_date = chrono::Utc::now() + chrono::Duration::days(days as i64);
|
||||
let deletion_date = Zoned::now() + jiff::Span::new().days(days as i64);
|
||||
key_metadata.key_state = KeyState::PendingDeletion;
|
||||
key_metadata.deletion_date = Some(deletion_date);
|
||||
key_metadata.deletion_date = Some(deletion_date.clone());
|
||||
|
||||
// Update the key metadata in Vault storage to reflect the new state
|
||||
self.update_key_metadata_in_storage(key_id, &key_metadata).await?;
|
||||
|
||||
Some(deletion_date.to_rfc3339())
|
||||
Some(deletion_date.to_string())
|
||||
};
|
||||
|
||||
Ok(DeleteKeyResponse {
|
||||
|
||||
@@ -154,6 +154,7 @@ impl KmsCache {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::types::{KeyState, KeyUsage};
|
||||
use jiff::Zoned;
|
||||
use std::time::Duration;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -202,7 +203,7 @@ mod tests {
|
||||
key_state: KeyState::Enabled,
|
||||
key_usage: KeyUsage::EncryptDecrypt,
|
||||
description: Some("Test key".to_string()),
|
||||
creation_date: chrono::Utc::now(),
|
||||
creation_date: Zoned::now(),
|
||||
deletion_date: None,
|
||||
origin: "KMS".to_string(),
|
||||
key_manager: "CUSTOMER".to_string(),
|
||||
@@ -252,7 +253,7 @@ mod tests {
|
||||
key_state: KeyState::Enabled,
|
||||
key_usage: KeyUsage::EncryptDecrypt,
|
||||
description: Some("TTL test key".to_string()),
|
||||
creation_date: chrono::Utc::now(),
|
||||
creation_date: Zoned::now(),
|
||||
deletion_date: None,
|
||||
origin: "KMS".to_string(),
|
||||
key_manager: "CUSTOMER".to_string(),
|
||||
@@ -283,7 +284,7 @@ mod tests {
|
||||
key_state: KeyState::Enabled,
|
||||
key_usage: KeyUsage::EncryptDecrypt,
|
||||
description: None,
|
||||
creation_date: chrono::Utc::now(),
|
||||
creation_date: Zoned::now(),
|
||||
deletion_date: None,
|
||||
origin: "KMS".to_string(),
|
||||
key_manager: "CUSTOMER".to_string(),
|
||||
|
||||
@@ -19,6 +19,7 @@ use crate::error::{KmsError, Result};
|
||||
use crate::manager::KmsManager;
|
||||
use crate::types::*;
|
||||
use base64::Engine;
|
||||
use jiff::Zoned;
|
||||
use rand::random;
|
||||
use std::collections::HashMap;
|
||||
use std::io::Cursor;
|
||||
@@ -339,7 +340,7 @@ impl ObjectEncryptionService {
|
||||
iv,
|
||||
tag: Some(tag),
|
||||
encryption_context: context,
|
||||
encrypted_at: chrono::Utc::now(),
|
||||
encrypted_at: Zoned::now(),
|
||||
original_size,
|
||||
encrypted_data_key: data_key.ciphertext_blob,
|
||||
};
|
||||
@@ -482,7 +483,7 @@ impl ObjectEncryptionService {
|
||||
iv,
|
||||
tag: Some(tag),
|
||||
encryption_context: context,
|
||||
encrypted_at: chrono::Utc::now(),
|
||||
encrypted_at: Zoned::now(),
|
||||
original_size,
|
||||
encrypted_data_key: Vec::new(), // Empty for SSE-C
|
||||
};
|
||||
@@ -695,7 +696,7 @@ impl ObjectEncryptionService {
|
||||
iv,
|
||||
tag,
|
||||
encryption_context,
|
||||
encrypted_at: chrono::Utc::now(),
|
||||
encrypted_at: Zoned::now(),
|
||||
original_size: 0, // Not available from headers
|
||||
encrypted_data_key,
|
||||
})
|
||||
@@ -809,7 +810,7 @@ mod tests {
|
||||
iv: vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
|
||||
tag: Some(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]),
|
||||
encryption_context: HashMap::from([("bucket".to_string(), "test-bucket".to_string())]),
|
||||
encrypted_at: chrono::Utc::now(),
|
||||
encrypted_at: Zoned::now(),
|
||||
original_size: 100,
|
||||
encrypted_data_key: vec![1, 2, 3, 4],
|
||||
};
|
||||
|
||||
+12
-12
@@ -14,7 +14,7 @@
|
||||
|
||||
//! Core type definitions for KMS operations
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use jiff::Zoned;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use uuid::Uuid;
|
||||
@@ -37,7 +37,7 @@ pub struct DataKey {
|
||||
/// Associated metadata
|
||||
pub metadata: HashMap<String, String>,
|
||||
/// Key creation timestamp
|
||||
pub created_at: DateTime<Utc>,
|
||||
pub created_at: Zoned,
|
||||
}
|
||||
|
||||
impl DataKey {
|
||||
@@ -61,7 +61,7 @@ impl DataKey {
|
||||
ciphertext,
|
||||
key_spec,
|
||||
metadata: HashMap::new(),
|
||||
created_at: Utc::now(),
|
||||
created_at: Zoned::now(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,9 +112,9 @@ pub struct MasterKey {
|
||||
/// Associated metadata
|
||||
pub metadata: HashMap<String, String>,
|
||||
/// Key creation timestamp
|
||||
pub created_at: DateTime<Utc>,
|
||||
pub created_at: Zoned,
|
||||
/// Key last rotation timestamp
|
||||
pub rotated_at: Option<DateTime<Utc>>,
|
||||
pub rotated_at: Option<Zoned>,
|
||||
/// Key creator/owner
|
||||
pub created_by: Option<String>,
|
||||
}
|
||||
@@ -139,7 +139,7 @@ impl MasterKey {
|
||||
status: KeyStatus::Active,
|
||||
description: None,
|
||||
metadata: HashMap::new(),
|
||||
created_at: Utc::now(),
|
||||
created_at: Zoned::now(),
|
||||
rotated_at: None,
|
||||
created_by,
|
||||
}
|
||||
@@ -170,7 +170,7 @@ impl MasterKey {
|
||||
status: KeyStatus::Active,
|
||||
description,
|
||||
metadata: HashMap::new(),
|
||||
created_at: Utc::now(),
|
||||
created_at: Zoned::now(),
|
||||
rotated_at: None,
|
||||
created_by,
|
||||
}
|
||||
@@ -219,9 +219,9 @@ pub struct KeyInfo {
|
||||
/// Key tags
|
||||
pub tags: HashMap<String, String>,
|
||||
/// Key creation timestamp
|
||||
pub created_at: DateTime<Utc>,
|
||||
pub created_at: Zoned,
|
||||
/// Key last rotation timestamp
|
||||
pub rotated_at: Option<DateTime<Utc>>,
|
||||
pub rotated_at: Option<Zoned>,
|
||||
/// Key creator
|
||||
pub created_by: Option<String>,
|
||||
}
|
||||
@@ -612,7 +612,7 @@ pub struct EncryptionMetadata {
|
||||
/// Encryption context
|
||||
pub encryption_context: HashMap<String, String>,
|
||||
/// Timestamp when encrypted
|
||||
pub encrypted_at: DateTime<Utc>,
|
||||
pub encrypted_at: Zoned,
|
||||
/// Size of original data
|
||||
pub original_size: u64,
|
||||
/// Encrypted data key
|
||||
@@ -697,9 +697,9 @@ pub struct KeyMetadata {
|
||||
/// Key description
|
||||
pub description: Option<String>,
|
||||
/// Key creation timestamp
|
||||
pub creation_date: DateTime<Utc>,
|
||||
pub creation_date: Zoned,
|
||||
/// Key deletion timestamp
|
||||
pub deletion_date: Option<DateTime<Utc>>,
|
||||
pub deletion_date: Option<Zoned>,
|
||||
/// Key origin
|
||||
pub origin: String,
|
||||
/// Key manager
|
||||
|
||||
Reference in New Issue
Block a user