use super::types::{LegalHoldStatus, ObjectLegalHold, ObjectRetention, RetentionMode};
use rustfs_filemeta::metadata_keys;
use std::collections::HashMap;
use time::{OffsetDateTime, format_description};
const _ERR_MALFORMED_BUCKET_OBJECT_CONFIG: &str = "invalid bucket object lock config";
const _ERR_INVALID_RETENTION_DATE: &str = "date must be provided in ISO 8601 format";
const _ERR_PAST_OBJECTLOCK_RETAIN_DATE: &str = "the retain until date must be in the future";
const _ERR_UNKNOWN_WORMMODE_DIRECTIVE: &str = "unknown WORM mode directive";
const _ERR_OBJECTLOCK_MISSING_CONTENT_MD5: &str =
"content-MD5 HTTP header is required for Put Object requests with Object Lock parameters";
const _ERR_OBJECTLOCK_INVALID_HEADERS: &str =
"x-amz-object-lock-retain-until-date and x-amz-object-lock-mode must both be supplied";
const _ERR_MALFORMED_XML: &str = "the XML you provided was not well-formed or did not validate against our published schema";
pub fn utc_now_ntp() -> OffsetDateTime {
OffsetDateTime::now_utc()
}
pub fn get_object_retention_meta(meta: &HashMap<String, String>) -> ObjectRetention {
let mode_str = meta.get(metadata_keys::OBJECT_LOCK_MODE);
let Some(mode_str) = mode_str else {
return ObjectRetention::default();
};
let Some(mode) = parse_ret_mode(mode_str.as_str()) else {
return ObjectRetention::default();
};
let till_str = meta.get(metadata_keys::OBJECT_LOCK_RETAIN_UNTIL_DATE);
let retain_until_date =
till_str.and_then(|s| OffsetDateTime::parse(s, &format_description::well_known::Iso8601::DEFAULT).ok());
ObjectRetention {
mode: Some(mode),
retain_until_date,
}
}
pub fn get_object_legalhold_meta(meta: &HashMap<String, String>) -> ObjectLegalHold {
let hold_str = meta.get(metadata_keys::OBJECT_LOCK_LEGAL_HOLD);
ObjectLegalHold {
status: hold_str.and_then(|s| parse_legalhold_status(s)),
}
}
pub fn parse_ret_mode(mode_str: &str) -> Option<RetentionMode> {
RetentionMode::parse(mode_str)
}
pub fn parse_legalhold_status(hold_str: &str) -> Option<LegalHoldStatus> {
LegalHoldStatus::parse(hold_str)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_ret_mode_valid() {
let mode = parse_ret_mode("GOVERNANCE");
assert!(mode.is_some());
assert_eq!(mode.unwrap().as_str(), RetentionMode::GOVERNANCE);
let mode = parse_ret_mode("COMPLIANCE");
assert!(mode.is_some());
assert_eq!(mode.unwrap().as_str(), RetentionMode::COMPLIANCE);
let mode = parse_ret_mode("governance");
assert!(mode.is_some());
assert_eq!(mode.unwrap().as_str(), RetentionMode::GOVERNANCE);
let mode = parse_ret_mode("compliance");
assert!(mode.is_some());
assert_eq!(mode.unwrap().as_str(), RetentionMode::COMPLIANCE);
let mode = parse_ret_mode("Governance");
assert!(mode.is_some());
assert_eq!(mode.unwrap().as_str(), RetentionMode::GOVERNANCE);
}
#[test]
fn test_parse_ret_mode_invalid() {
assert!(parse_ret_mode("INVALID").is_none());
assert!(parse_ret_mode("").is_none());
assert!(parse_ret_mode("gov").is_none());
assert!(parse_ret_mode("comp").is_none());
}
#[test]
fn test_parse_legalhold_status_valid() {
let status = parse_legalhold_status("ON");
assert!(status.is_some());
assert_eq!(status.unwrap().as_str(), LegalHoldStatus::ON);
let status = parse_legalhold_status("OFF");
assert!(status.is_some());
assert_eq!(status.unwrap().as_str(), LegalHoldStatus::OFF);
let status = parse_legalhold_status("on");
assert!(status.is_some());
assert_eq!(status.unwrap().as_str(), LegalHoldStatus::ON);
let status = parse_legalhold_status("off");
assert!(status.is_some());
assert_eq!(status.unwrap().as_str(), LegalHoldStatus::OFF);
}
#[test]
fn test_parse_legalhold_status_invalid() {
assert!(parse_legalhold_status("INVALID").is_none());
assert!(parse_legalhold_status("").is_none());
assert!(parse_legalhold_status("true").is_none());
assert!(parse_legalhold_status("false").is_none());
}
#[test]
fn test_get_object_retention_meta_empty() {
let meta = HashMap::new();
let retention = get_object_retention_meta(&meta);
assert!(retention.mode.is_none());
assert!(retention.retain_until_date.is_none());
}
#[test]
fn test_get_object_retention_meta_with_mode() {
let mut meta = HashMap::new();
meta.insert("x-amz-object-lock-mode".to_string(), "GOVERNANCE".to_string());
let retention = get_object_retention_meta(&meta);
assert!(retention.mode.is_some());
assert_eq!(retention.mode.unwrap().as_str(), RetentionMode::GOVERNANCE);
assert!(retention.retain_until_date.is_none());
}
#[test]
fn test_get_object_retention_meta_with_invalid_mode() {
let mut meta = HashMap::new();
meta.insert("x-amz-object-lock-mode".to_string(), "INVALID_MODE".to_string());
let retention = get_object_retention_meta(&meta);
assert!(retention.mode.is_none());
assert!(retention.retain_until_date.is_none());
}
#[test]
fn test_get_object_retention_meta_with_date() {
let mut meta = HashMap::new();
meta.insert("x-amz-object-lock-mode".to_string(), "COMPLIANCE".to_string());
meta.insert("x-amz-object-lock-retain-until-date".to_string(), "2030-01-01T00:00:00Z".to_string());
let retention = get_object_retention_meta(&meta);
assert!(retention.mode.is_some());
assert_eq!(retention.mode.unwrap().as_str(), RetentionMode::COMPLIANCE);
assert!(retention.retain_until_date.is_some());
}
#[test]
fn persisted_compliance_lock_metadata_remains_effective() {
let mut meta = HashMap::new();
meta.insert("x-amz-object-lock-mode".to_string(), "COMPLIANCE".to_string());
meta.insert("x-amz-object-lock-retain-until-date".to_string(), "9999-01-01T00:00:00Z".to_string());
meta.insert("x-amz-object-lock-legal-hold".to_string(), "ON".to_string());
let retention = get_object_retention_meta(&meta);
assert_eq!(retention.mode.as_ref().map(|mode| mode.as_str()), Some(RetentionMode::COMPLIANCE));
assert!(retention.retain_until_date.is_some(), "persisted retention date must remain readable");
let legal_hold = get_object_legalhold_meta(&meta);
assert_eq!(legal_hold.status.as_ref().map(|status| status.as_str()), Some(LegalHoldStatus::ON));
}
#[test]
fn test_get_object_legalhold_meta_empty() {
let meta = HashMap::new();
let legalhold = get_object_legalhold_meta(&meta);
assert!(legalhold.status.is_none());
}
#[test]
fn test_get_object_legalhold_meta_on() {
let mut meta = HashMap::new();
meta.insert("x-amz-object-lock-legal-hold".to_string(), "ON".to_string());
let legalhold = get_object_legalhold_meta(&meta);
assert!(legalhold.status.is_some());
assert_eq!(legalhold.status.unwrap().as_str(), LegalHoldStatus::ON);
}
#[test]
fn test_get_object_legalhold_meta_off() {
let mut meta = HashMap::new();
meta.insert("x-amz-object-lock-legal-hold".to_string(), "OFF".to_string());
let legalhold = get_object_legalhold_meta(&meta);
assert!(legalhold.status.is_some());
assert_eq!(legalhold.status.unwrap().as_str(), LegalHoldStatus::OFF);
}
#[test]
fn test_get_object_legalhold_meta_invalid() {
let mut meta = HashMap::new();
meta.insert("x-amz-object-lock-legal-hold".to_string(), "INVALID".to_string());
let legalhold = get_object_legalhold_meta(&meta);
assert!(legalhold.status.is_none());
}
}