mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-13 00:26:53 +00:00
feat: improve legacy metadata and admin compatibility (#2202)
This commit is contained in:
+58
-13
@@ -32,7 +32,7 @@ use rustfs_policy::{
|
||||
format::Format,
|
||||
policy::{Policy, PolicyDoc, default::DEFAULT_POLICIES, iam_policy_claim_name_sa},
|
||||
};
|
||||
use rustfs_utils::path::path_join_buf;
|
||||
use rustfs_utils::{get_env_opt_str, path::path_join_buf};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use std::sync::atomic::AtomicU8;
|
||||
@@ -147,7 +147,7 @@ where
|
||||
|
||||
// Background ticker for synchronization
|
||||
// Check if environment variable is set
|
||||
let skip_background_task = std::env::var("RUSTFS_SKIP_BACKGROUND_TASK").is_ok();
|
||||
let skip_background_task = get_env_opt_str("RUSTFS_SKIP_BACKGROUND_TASK").is_some();
|
||||
|
||||
if !skip_background_task {
|
||||
// Background thread starts periodic updates or receives signal updates
|
||||
@@ -595,7 +595,11 @@ where
|
||||
Ok(users
|
||||
.values()
|
||||
.filter_map(|x| {
|
||||
if !access_key.is_empty() && x.credentials.parent_user.as_str() == access_key && x.credentials.is_temp() {
|
||||
if !access_key.is_empty()
|
||||
&& x.credentials.parent_user.as_str() == access_key
|
||||
&& x.credentials.is_temp()
|
||||
&& !x.credentials.is_service_account()
|
||||
{
|
||||
let mut c = x.credentials.clone();
|
||||
c.secret_key = String::new();
|
||||
c.session_token = String::new();
|
||||
@@ -1419,9 +1423,6 @@ where
|
||||
}
|
||||
|
||||
pub async fn get_group_description(&self, name: &str) -> Result<GroupDesc> {
|
||||
let (ps, updated_at) = self.policy_db_get_internal(name, true, false).await?;
|
||||
let policy = ps.join(",");
|
||||
|
||||
let gi = self
|
||||
.cache
|
||||
.groups
|
||||
@@ -1430,13 +1431,25 @@ where
|
||||
.cloned()
|
||||
.ok_or(Error::NoSuchGroup(name.to_string()))?;
|
||||
|
||||
Ok(GroupDesc {
|
||||
name: name.to_string(),
|
||||
policy,
|
||||
members: gi.members,
|
||||
updated_at: Some(updated_at),
|
||||
status: gi.status,
|
||||
})
|
||||
let mapped_policy = if let Some(policy) = self.cache.group_policies.load().get(name).cloned() {
|
||||
Some(policy)
|
||||
} else {
|
||||
let mut policies = HashMap::new();
|
||||
if let Err(err) = self.api.load_mapped_policy(name, UserType::Reg, true, &mut policies).await
|
||||
&& !is_err_no_such_policy(&err)
|
||||
{
|
||||
return Err(err);
|
||||
}
|
||||
|
||||
if let Some(policy) = policies.get(name).cloned() {
|
||||
Cache::add_or_update(&self.cache.group_policies, name, &policy, OffsetDateTime::now_utc());
|
||||
Some(policy)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
Ok(build_group_desc(name, gi, mapped_policy))
|
||||
}
|
||||
|
||||
pub async fn list_groups(&self) -> Result<Vec<String>> {
|
||||
@@ -1882,6 +1895,20 @@ fn filter_policies(cache: &Cache, policy_name: &str, bucket_name: &str) -> (Stri
|
||||
(policies.join(","), Policy::merge_policies(to_merge))
|
||||
}
|
||||
|
||||
fn build_group_desc(name: &str, group_info: GroupInfo, mapped_policy: Option<MappedPolicy>) -> GroupDesc {
|
||||
let (policy, updated_at) = mapped_policy
|
||||
.map(|policy| (policy.policies, Some(policy.update_at)))
|
||||
.unwrap_or_else(|| (String::new(), Some(OffsetDateTime::now_utc())));
|
||||
|
||||
GroupDesc {
|
||||
name: name.to_string(),
|
||||
policy,
|
||||
members: group_info.members,
|
||||
updated_at,
|
||||
status: group_info.status,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -2226,4 +2253,22 @@ mod tests {
|
||||
assert!(merged.statements.is_empty());
|
||||
assert!(merged.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_build_group_desc_preserves_policy_for_disabled_group() {
|
||||
let group_info = GroupInfo {
|
||||
status: STATUS_DISABLED.to_string(),
|
||||
members: vec!["alice".to_string()],
|
||||
..Default::default()
|
||||
};
|
||||
let mapped_policy = MappedPolicy::new("readonly");
|
||||
|
||||
let desc = build_group_desc("ops", group_info, Some(mapped_policy));
|
||||
|
||||
assert_eq!(desc.name, "ops");
|
||||
assert_eq!(desc.status, STATUS_DISABLED);
|
||||
assert_eq!(desc.policy, "readonly");
|
||||
assert_eq!(desc.members, vec!["alice".to_string()]);
|
||||
assert!(desc.updated_at.is_some());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,7 +113,11 @@ impl UserType {
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct MappedPolicy {
|
||||
pub version: i64,
|
||||
/// policy, legacy: policies. Serialize as policy.
|
||||
#[serde(rename = "policy", alias = "policies")]
|
||||
pub policies: String,
|
||||
/// updatedAt (RFC3339), legacy: update_at. Serialize as updatedAt.
|
||||
#[serde(rename = "updatedAt", alias = "update_at", with = "rustfs_policy::serde_datetime")]
|
||||
pub update_at: OffsetDateTime,
|
||||
}
|
||||
|
||||
@@ -158,6 +162,13 @@ pub struct GroupInfo {
|
||||
pub version: i64,
|
||||
pub status: String,
|
||||
pub members: Vec<String>,
|
||||
/// updatedAt (RFC3339), legacy: update_at. Serialize as updatedAt.
|
||||
#[serde(
|
||||
rename = "updatedAt",
|
||||
alias = "update_at",
|
||||
default,
|
||||
with = "rustfs_policy::serde_datetime::option"
|
||||
)]
|
||||
pub update_at: Option<OffsetDateTime>,
|
||||
}
|
||||
|
||||
@@ -171,3 +182,46 @@ impl GroupInfo {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{GroupInfo, MappedPolicy};
|
||||
|
||||
/// uses RFC3339 for updatedAt. MappedPolicy must serialize as RFC3339.
|
||||
#[test]
|
||||
fn test_mapped_policy_timestamps_serialize_as_rfc3339() {
|
||||
let mp = MappedPolicy::new("readwrite");
|
||||
let json = serde_json::to_string(&mp).expect("serialize");
|
||||
assert!(json.contains('T'), "MappedPolicy updatedAt should be RFC3339; got: {}", json);
|
||||
assert!(
|
||||
json.contains('Z') || json.contains("+00:00"),
|
||||
"MappedPolicy updatedAt should be RFC3339; got: {}",
|
||||
json
|
||||
);
|
||||
}
|
||||
|
||||
/// Deserialize MappedPolicy from JSON (RFC3339 updatedAt).
|
||||
#[test]
|
||||
fn test_mapped_policy_deserialize_minio_style_rfc3339() {
|
||||
let minio_style = r#"{"version":1,"policy":"readwrite","updatedAt":"2025-03-07T12:00:00Z"}"#;
|
||||
let mp: MappedPolicy = serde_json::from_str(minio_style).expect("deserialize");
|
||||
assert_eq!(mp.policies, "readwrite");
|
||||
}
|
||||
|
||||
/// GroupInfo updatedAt: uses RFC3339.
|
||||
#[test]
|
||||
fn test_group_info_timestamps_serialize_as_rfc3339() {
|
||||
let g = GroupInfo::new(vec!["u1".to_string()]);
|
||||
let json = serde_json::to_string(&g).expect("serialize");
|
||||
assert!(json.contains('T'), "GroupInfo updatedAt should be RFC3339; got: {}", json);
|
||||
}
|
||||
|
||||
/// Deserialize GroupInfo from JSON (RFC3339 updatedAt).
|
||||
#[test]
|
||||
fn test_group_info_deserialize_minio_style_rfc3339() {
|
||||
let minio_style = r#"{"version":1,"status":"enabled","members":["u1"],"updatedAt":"2025-03-07T12:00:00Z"}"#;
|
||||
let g: GroupInfo = serde_json::from_str(minio_style).expect("deserialize");
|
||||
assert_eq!(g.members, ["u1"]);
|
||||
assert!(g.update_at.is_some());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,15 +129,55 @@ impl ObjectStore {
|
||||
}
|
||||
|
||||
fn decrypt_data(data: &[u8]) -> Result<Vec<u8>> {
|
||||
let de = rustfs_crypto::decrypt_data(get_global_action_cred().unwrap_or_default().secret_key.as_bytes(), data)?;
|
||||
Ok(de)
|
||||
if Self::is_plaintext_json(data) {
|
||||
return Ok(data.to_vec());
|
||||
}
|
||||
|
||||
let cred = get_global_action_cred().unwrap_or_default();
|
||||
let secret_key = cred.secret_key;
|
||||
let mut keys: Vec<(Vec<u8>, bool)> = vec![(secret_key.clone().into_bytes(), false)];
|
||||
if !cred.access_key.is_empty() && !secret_key.is_empty() {
|
||||
keys.push((format!("{}:{secret_key}", cred.access_key).into_bytes(), true));
|
||||
}
|
||||
|
||||
const STREAM_IO_HEADER_LEN: usize = 41;
|
||||
let mut last_err = None;
|
||||
for (key, is_access_secret) in keys {
|
||||
if is_access_secret
|
||||
&& data.len() >= STREAM_IO_HEADER_LEN
|
||||
&& let Ok(plain) = rustfs_crypto::decrypt_stream_io(&key, data)
|
||||
{
|
||||
return Ok(plain);
|
||||
}
|
||||
match rustfs_crypto::decrypt_data(&key, data) {
|
||||
Ok(plain) => return Ok(plain),
|
||||
Err(err) => last_err = Some(err),
|
||||
}
|
||||
}
|
||||
|
||||
Err(last_err.unwrap_or(rustfs_crypto::Error::ErrUnexpectedHeader).into())
|
||||
}
|
||||
|
||||
fn encrypt_data(data: &[u8]) -> Result<Vec<u8>> {
|
||||
let en = rustfs_crypto::encrypt_data(get_global_action_cred().unwrap_or_default().secret_key.as_bytes(), data)?;
|
||||
let cred = get_global_action_cred().unwrap_or_default();
|
||||
let password = if !cred.access_key.is_empty() && !cred.secret_key.is_empty() {
|
||||
format!("{}:{}", cred.access_key, cred.secret_key).into_bytes()
|
||||
} else {
|
||||
cred.secret_key.clone().into_bytes()
|
||||
};
|
||||
let en = rustfs_crypto::encrypt_stream_io(&password, data)?;
|
||||
Ok(en)
|
||||
}
|
||||
|
||||
fn is_plaintext_json(data: &[u8]) -> bool {
|
||||
std::str::from_utf8(data).is_ok() && serde_json::from_slice::<serde_json::Value>(data).is_ok()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn encrypt_data_for_test(data: &[u8]) -> Result<Vec<u8>> {
|
||||
Self::encrypt_data(data)
|
||||
}
|
||||
|
||||
async fn load_iamconfig_bytes_with_metadata(&self, path: impl AsRef<str> + Send) -> Result<(Vec<u8>, ObjectInfo)> {
|
||||
let (data, obj) = read_config_with_metadata(self.object_api.clone(), path.as_ref(), &ObjectOptions::default()).await?;
|
||||
|
||||
@@ -1164,3 +1204,83 @@ impl Store for ObjectStore {
|
||||
// Ok(())
|
||||
// }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::ObjectStore;
|
||||
use rustfs_credentials::{Credentials, get_global_action_cred, init_global_action_credentials};
|
||||
|
||||
fn test_cred() -> Credentials {
|
||||
if let Some(cred) = get_global_action_cred() {
|
||||
return cred;
|
||||
}
|
||||
let _ = init_global_action_credentials(Some("COMPATTESTAK".to_string()), Some("COMPATTESTSK1234567890".to_string()));
|
||||
get_global_action_cred().unwrap_or_default()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_decrypt_data_accepts_plaintext_json() {
|
||||
let raw = br#"{"Version":1,"policy":"readonly"}"#;
|
||||
let out = ObjectStore::decrypt_data(raw).expect("plaintext json should pass through");
|
||||
assert_eq!(out, raw);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_decrypt_data_accepts_rustfs_legacy_secret_encryption() {
|
||||
let cred = test_cred();
|
||||
let plain = br#"{"accessKey":"ak","secretKey":"sk"}"#;
|
||||
let encrypted = rustfs_crypto::encrypt_data(cred.secret_key.as_bytes(), plain).expect("encrypt with rustfs secret");
|
||||
let out = ObjectStore::decrypt_data(&encrypted).expect("decrypt rustfs legacy encryption");
|
||||
assert_eq!(out, plain);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_decrypt_data_accepts_access_secret_encryption() {
|
||||
let cred = test_cred();
|
||||
let plain = br#"{"Version":1,"updatedAt":"2025-03-07T12:00:00Z"}"#;
|
||||
let root_cred = format!("{}:{}", cred.access_key, cred.secret_key);
|
||||
let encrypted = rustfs_crypto::encrypt_stream_io(root_cred.as_bytes(), plain).expect("encrypt with stream_io");
|
||||
let out = ObjectStore::decrypt_data(&encrypted).expect("decrypt stream_io");
|
||||
assert_eq!(out, plain);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_decrypt_data_corrupt_stream_io_fails() {
|
||||
let cred = test_cred();
|
||||
let plain = br#"{"Version":1}"#;
|
||||
let root_cred = format!("{}:{}", cred.access_key, cred.secret_key);
|
||||
let mut encrypted = rustfs_crypto::encrypt_stream_io(root_cred.as_bytes(), plain).expect("encrypt with stream_io");
|
||||
if encrypted.len() > 50 {
|
||||
encrypted[50] ^= 0xFF; // corrupt one byte
|
||||
}
|
||||
let result = ObjectStore::decrypt_data(&encrypted);
|
||||
assert!(result.is_err(), "corrupt stream_io data should fail decrypt");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_decrypt_data_short_data_fails() {
|
||||
let short = &[0x00u8; 40]; // less than 41-byte stream_io header, not valid JSON
|
||||
let result = ObjectStore::decrypt_data(short);
|
||||
assert!(result.is_err(), "short non-JSON data should fail decrypt");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_encrypt_data_produces_stream_io_format() {
|
||||
let _ = test_cred();
|
||||
let plain = br#"{"Version":1,"policy":"readonly"}"#;
|
||||
let encrypted = ObjectStore::encrypt_data_for_test(plain).expect("encrypt should succeed");
|
||||
// stream_io header: salt(32) + alg_id(1) + nonce_prefix(8) = 41 bytes
|
||||
const STREAM_IO_HEADER_LEN: usize = 41;
|
||||
assert!(
|
||||
encrypted.len() >= STREAM_IO_HEADER_LEN,
|
||||
"encrypted should have at least 41-byte stream_io header"
|
||||
);
|
||||
assert!(
|
||||
encrypted[32] == 0x00 || encrypted[32] == 0x01 || encrypted[32] == 0x02,
|
||||
"alg_id should be 0x00, 0x01, or 0x02"
|
||||
);
|
||||
// Round-trip: encrypt then decrypt
|
||||
let decrypted = ObjectStore::decrypt_data(&encrypted).expect("decrypt should succeed");
|
||||
assert_eq!(plain, decrypted.as_slice());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -361,10 +361,6 @@ impl<T: Store> IamSys<T> {
|
||||
return Err(IamError::IAMActionNotAllowed);
|
||||
}
|
||||
|
||||
if opts.expiration.is_none() {
|
||||
return Err(IamError::InvalidExpiration);
|
||||
}
|
||||
|
||||
// TODO: check allow_site_replicator_account
|
||||
|
||||
let policy_buf = if let Some(policy) = opts.session_policy {
|
||||
@@ -619,6 +615,7 @@ impl<T: Store> IamSys<T> {
|
||||
}
|
||||
|
||||
let updated_at = self.store.add_user(access_key, args).await?;
|
||||
self.load_user(access_key, UserType::Reg).await?;
|
||||
|
||||
self.notify_for_user(access_key, false).await;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user