mirror of
https://github.com/rustfs/rustfs.git
synced 2026-11-22 16:02:46 +00:00
Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8585d39b6b | |||
| bc78014592 | |||
| 781cf7360e | |||
| d8406f9494 | |||
| ed12c5320d | |||
| eb57789f0f | |||
| 791b1bcd86 | |||
| 0517566fad | |||
| 73895b37e8 | |||
| 00243c7e71 | |||
| 36bc1057ac | |||
| 32f7f70a29 | |||
| 5d32827af5 | |||
| 04ef836aa3 | |||
| 03850c1cd6 | |||
| 654be96186 |
@@ -57,7 +57,6 @@ rustfs-scanner 1392 20
|
||||
rustfs-scanner-metrics 90 0
|
||||
rustfs-security-governance 20 0
|
||||
rustfs-signer 34 0
|
||||
rustfs-sse 145 0
|
||||
rustfs-storage-api 34 0
|
||||
rustfs-targets 612 26
|
||||
rustfs-test-utils 1 0
|
||||
|
||||
@@ -231,7 +231,7 @@ jobs:
|
||||
# --all-targets links the same test binaries as the reader lane.
|
||||
CARGO_BUILD_JOBS: "2"
|
||||
run: |
|
||||
cargo build -p rustfs -p rustfs-ecstore -p rustfs-sse --all-targets --features rio-v2
|
||||
cargo build -p rustfs -p rustfs-ecstore --all-targets --features rio-v2
|
||||
cargo build -p rustfs --bins --features rio-v2,e2e-test-hooks
|
||||
|
||||
# Readers: the swift and sftp legs of test-and-lint-protocols. Built in
|
||||
|
||||
@@ -563,7 +563,7 @@ jobs:
|
||||
run: chmod go-w "$(realpath "$HOME")"
|
||||
|
||||
- name: Run rio-v2 clippy lints
|
||||
run: cargo clippy -p rustfs -p rustfs-ecstore -p rustfs-sse --all-targets --features rio-v2 -- -D warnings
|
||||
run: cargo clippy -p rustfs -p rustfs-ecstore --all-targets --features rio-v2 -- -D warnings
|
||||
|
||||
- name: Run rio-v2 feature tests
|
||||
shell: bash
|
||||
@@ -580,7 +580,7 @@ jobs:
|
||||
rm -f target/nextest/ci/junit.xml
|
||||
./scripts/ci/resource_sampler.sh start nextest-rio-v2
|
||||
trap './scripts/ci/resource_sampler.sh stop' EXIT
|
||||
cargo nextest run --profile ci -p rustfs -p rustfs-ecstore -p rustfs-sse --features rio-v2 \
|
||||
cargo nextest run --profile ci -p rustfs -p rustfs-ecstore --features rio-v2 \
|
||||
2>&1 | tee artifacts/workspace/nextest.log
|
||||
cargo test -p rustfs --doc --features rio-v2
|
||||
|
||||
|
||||
+1
-1
@@ -94,7 +94,7 @@ refactors.
|
||||
|--------|--------------------------|----------------|
|
||||
| Foundation | `checksums`, `common`, `config`, `data-usage`, `heal-contracts`, `scanner-metrics`, `utils` | Shared configuration, data-usage models, heal domain contracts, scanner telemetry types, utilities, and checksums. |
|
||||
| I/O and storage | `concurrency`, `ecstore`, `filemeta`, `heal`, `io-core`, `io-metrics`, `lifecycle`, `lock`, `object-capacity`, `object-data-cache`, `replication`, `rio`, `rio-v2`, `s3-client`, `scanner`, `storage-api` | Erasure-coded object storage, metadata, recovery, lifecycle, replication, locking, cache, I/O pipelines, and the engine-side S3 client for remote tier/transition targets. |
|
||||
| Security and identity | `credentials`, `crypto`, `iam`, `keystone`, `kms`, `policy`, `security-governance`, `signer`, `sse`, `tls-runtime`, `trusted-proxies` | Credentials, authentication, authorization, encryption, key management, server-side encryption key resolution, TLS, and security contracts. |
|
||||
| Security and identity | `credentials`, `crypto`, `iam`, `keystone`, `kms`, `policy`, `security-governance`, `signer`, `tls-runtime`, `trusted-proxies` | Credentials, authentication, authorization, encryption, key management, TLS, and security contracts. |
|
||||
| Protocols and contracts | `extension-schema`, `madmin`, `protos`, `protocols`, `s3-ops`, `s3-types`, `s3select-api`, `s3select-query` | Admin, inter-node, S3, S3 Select, and optional protocol contracts. |
|
||||
| Operations and integration | `audit`, `notify`, `obs`, `targets`, `zip` | Auditing, observability, event delivery, notification targets, and archive support. |
|
||||
| Test support | `e2e_test`, `test-utils` | End-to-end validation and shared test bootstrap utilities. |
|
||||
|
||||
@@ -116,7 +116,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
- Four-layer regression-prevention tests guard against silent feature deletion: compile-time module assertion, module-presence unit test, cross-module `Protocol` enum assertion, end-to-end SSH banner test against the running binary
|
||||
|
||||
### Changed
|
||||
- **Startup reports the state of `RUSTFS_SSE_S3_MASTER_KEY`**: the server now checks the local SSE-S3 master key when it starts and logs one `sse_local_master_key` event with `state` `loaded`, `not_set`, or `invalid`. An unusable value used to surface only as a `400 InvalidRequest` on the first SSE-S3 request served without a KMS; it is now logged as an error at startup with the reason. Startup still succeeds and request behavior is unchanged. The line "Using RUSTFS_SSE_S3_MASTER_KEY for SSE-S3 (KMS not configured)", logged when the key was first used, is replaced by this event.
|
||||
- **Scanner bucket walkers are no longer tied to the disk count**: a new scanner key `walkers_per_disk` (`RUSTFS_SCANNER_WALKERS_PER_DISK`, `1` through `16`, default `1`) spawns that many bucket walkers per local disk of an erasure set, so a single-disk deployment can walk several buckets at once instead of one bucket, one object at a time. The walkers of one disk share its scanner admission, so a peer still sees one scan session per physical disk, and remote disks keep one walker. The walker count is what `max_concurrent_disk_scans` caps and the disk count stays the floor; the `disk_concurrency_budget` log line now reports `walkers_per_disk`, `walkers`, and the `reason` for the effective limit. The default keeps today's I/O profile because a local disk cannot yet report whether it is rotational; the design intends a higher default on flash once it can (`docs/architecture/scanner-per-bucket-convergence.md`). As groundwork for pacing, the raw-enumeration page writer inserts into its building page in place and commits pages without copying them, and a regression test pins the cost of recording 20,000 entries in one prefix so the quadratic bookkeeping removed by [#8114](https://github.com/rustfs/rustfs/pull/8114) (rustfs#8113) cannot return. No persisted format, protocol or admin API changes beyond the additional config key, which older releases preserve and ignore.
|
||||
- **Encryption and KMS work merged since `1.0.0-rc.5`** (entries were missing from this section):
|
||||
- **Persisted KMS configuration secrets** are sealed field-by-field with `RUSTFS_KMS_CONFIG_SECRET`. **When the variable is unset the secrets are persisted in cleartext and the server only warns** (`persisted KMS configuration carries cleartext secrets`); it never refuses the write. Set it, identically, on every node, and re-save the configuration to seal an existing one.
|
||||
|
||||
Generated
+187
-212
File diff suppressed because it is too large
Load Diff
+19
-24
@@ -60,7 +60,6 @@ members = [
|
||||
"crates/security-governance", # Security governance contracts
|
||||
"crates/extension-schema", # Extension schema contracts
|
||||
"crates/signer", # client signer
|
||||
"crates/sse", # S3 server-side encryption key resolution and metadata
|
||||
"crates/storage-api", # Storage API contracts
|
||||
"crates/test-utils", # Shared test bootstrap helpers (dev-dependency only)
|
||||
"crates/targets", # Target-specific configurations and utilities
|
||||
@@ -141,7 +140,6 @@ rustfs-scanner = { path = "crates/scanner", version = "1.0.5" }
|
||||
rustfs-security-governance = { path = "crates/security-governance", version = "1.0.5" }
|
||||
rustfs-extension-schema = { path = "crates/extension-schema", version = "1.0.5" }
|
||||
rustfs-signer = { path = "crates/signer", version = "1.0.5" }
|
||||
rustfs-sse = { path = "crates/sse", version = "1.0.5" }
|
||||
rustfs-storage-api = { path = "crates/storage-api", version = "1.0.5" }
|
||||
rustfs-trusted-proxies = { path = "crates/trusted-proxies", version = "1.0.5" }
|
||||
rustfs-targets = { path = "crates/targets", version = "1.0.5" }
|
||||
@@ -177,9 +175,9 @@ futures = "0.3.34"
|
||||
futures-core = "0.3.34"
|
||||
futures-lite = "2.6.1"
|
||||
futures-util = "0.3.34"
|
||||
pollster = "1.0.3"
|
||||
pollster = "1.0.2"
|
||||
pulsar = { default-features = false, version = "=6.9.0" }
|
||||
lapin = { default-features = false, version = "4.13.0" }
|
||||
lapin = { default-features = false, version = "4.12.3" }
|
||||
hyper = { version = "1.12.0" }
|
||||
h2 = { version = "0.4.20" }
|
||||
hyper-rustls = { default-features = false, version = "0.27.10" }
|
||||
@@ -218,7 +216,7 @@ csv-core = "0.1.13"
|
||||
bytesize = "2.7.0"
|
||||
byteorder = "1.5.0"
|
||||
flatbuffers = "25.12.19"
|
||||
form_urlencoded = "1.2.3"
|
||||
form_urlencoded = "1.2.2"
|
||||
prost = "0.14.6"
|
||||
quick-xml = "0.42.0"
|
||||
rmp = { version = "0.8.15" }
|
||||
@@ -260,7 +258,7 @@ proptest = "1"
|
||||
# Time and Date
|
||||
chrono = { version = "0.4.45" }
|
||||
humantime = "2.4.0"
|
||||
jiff = { version = "1.0.2" }
|
||||
jiff = { version = "1.0.1" }
|
||||
time = { version = "0.3.55" }
|
||||
|
||||
# Database
|
||||
@@ -280,15 +278,15 @@ atomic_enum = "0.3.0"
|
||||
aws-config = { version = "1.13.3" }
|
||||
aws-credential-types = { version = "1.3.1" }
|
||||
aws-sdk-kms = { default-features = false, version = "1.133.0" }
|
||||
aws-sdk-s3 = { default-features = false, version = "1.161.0" }
|
||||
aws-sdk-sts = { default-features = false, version = "1.128.0" }
|
||||
aws-sdk-s3 = { default-features = false, version = "1.160.0" }
|
||||
aws-sdk-sts = { default-features = false, version = "1.127.0" }
|
||||
aws-smithy-async = { version = "1.3.1" }
|
||||
aws-smithy-http-client = { default-features = false, version = "1.5.3" }
|
||||
aws-smithy-runtime-api = { version = "1.22.0" }
|
||||
aws-smithy-types = { version = "1.9.4" }
|
||||
base64-simd = "0.8.0"
|
||||
brotli = "9.1.3"
|
||||
clap = { version = "4.6.9" }
|
||||
clap = { version = "4.6.8" }
|
||||
const-str = { version = "1.1.0" }
|
||||
convert_case = "0.12.0"
|
||||
criterion = { version = "0.8" }
|
||||
@@ -300,12 +298,12 @@ datafusion = { default-features = false, version = "55.3.0" }
|
||||
derive_builder = "0.20.2"
|
||||
ed25519-dalek = "3.0.0"
|
||||
enumset = "1.1.15"
|
||||
faster-hex = "2.0.0"
|
||||
faster-hex = "1.0.0"
|
||||
flate2 = "1.1.11"
|
||||
glob = "0.3.4"
|
||||
google-cloud-storage = "1.22.0"
|
||||
google-cloud-auth = "1.19.0"
|
||||
hashbrown = { version = "0.17.2" }
|
||||
hashbrown = { version = "0.17.1" }
|
||||
# Base32 for RFC 6238 TOTP shared secrets (RFC 4648 unpadded, the alphabet
|
||||
# every authenticator app expects). Already in the graph transitively.
|
||||
data-encoding = "2.11.1"
|
||||
@@ -339,7 +337,7 @@ percent-encoding = "2.3.2"
|
||||
# CLI needs its own QR encoder. No default features: the image/render backends
|
||||
# pull in an image stack this only needs SVG and text output from.
|
||||
qrcode-rs = { version = "2.2.0", default-features = false, features = ["std", "svg"] }
|
||||
pin-project-lite = "0.2.18"
|
||||
pin-project-lite = "0.2.17"
|
||||
pretty_assertions = "1.4.1"
|
||||
rand = { version = "0.10.4" }
|
||||
ratelimit = "2.0.0"
|
||||
@@ -362,7 +360,7 @@ legacy-s3s = { package = "s3s", git = "https://github.com/s3s-project/s3s", rev
|
||||
serial_test = "4.0.1"
|
||||
shadow-rs = { default-features = false, version = "2.0.0" }
|
||||
siphasher = "1.0.4"
|
||||
smallvec = { version = "1.16.6" }
|
||||
smallvec = { version = "1.16.5" }
|
||||
compact_str = "0.10.0"
|
||||
snap = "1.1.2"
|
||||
starshard = { version = "2.4.1" }
|
||||
@@ -380,23 +378,23 @@ tracing-error = "0.2.1"
|
||||
tracing-opentelemetry = { version = "0.34.0" }
|
||||
tracing-subscriber = { version = "0.3.23" }
|
||||
transform-stream = "0.3.1"
|
||||
url = "2.5.9"
|
||||
url = "2.5.8"
|
||||
urlencoding = "2.1.3"
|
||||
uuid = { version = "1.29.3" }
|
||||
vaultrs = { version = "0.8.0" }
|
||||
tar = "0.4.46"
|
||||
walkdir = "2.5.1"
|
||||
walkdir = "2.5.0"
|
||||
winapi-util = "0.1.11"
|
||||
windows = { version = "0.62.2" }
|
||||
windows-sys = "0.61.2"
|
||||
xxhash-rust = { version = "0.8.21" }
|
||||
zip = "9.8.1"
|
||||
zip = "9.7.0"
|
||||
zstd = "0.14.4"
|
||||
|
||||
# Observability and Metrics
|
||||
metrics = "0.24.6"
|
||||
metrics-util = "0.20"
|
||||
dial9 = { version = "0.5.9", default-features = false }
|
||||
dial9 = { version = "0.5.8", default-features = false }
|
||||
opentelemetry = { version = "0.33.1" }
|
||||
opentelemetry-appender-tracing = { version = "0.33.1" }
|
||||
opentelemetry-otlp = { version = "0.33.1" }
|
||||
@@ -407,12 +405,12 @@ opentelemetry-stdout = { version = "0.33.1" }
|
||||
pyroscope = { version = "2.1.5" }
|
||||
|
||||
# FTP and SFTP
|
||||
libunftp = { version = "0.25.1" }
|
||||
libunftp = { version = "0.25.0" }
|
||||
unftp-core = "0.1.1"
|
||||
suppaftp = { version = "13.0.0" }
|
||||
rcgen = { version = "0.14.10", default-features = false, features = ["aws_lc_rs", "crypto", "pem"] }
|
||||
russh = { version = "0.65.4" }
|
||||
russh-sftp = "4.0.0"
|
||||
russh = { version = "0.65.2" }
|
||||
russh-sftp = "3.0.1"
|
||||
|
||||
# WebDAV
|
||||
dav-server = "0.12.0"
|
||||
@@ -425,10 +423,7 @@ hotpath = { version = "0.32.1", default-features = false }
|
||||
insta = { version = "1.49" }
|
||||
|
||||
[workspace.metadata.cargo-shear]
|
||||
# The `#[path]` test modules under `rustfs/src/server/http/tests/` are linked by
|
||||
# rustc (including the `http.rs` + `http/` sibling-directory rule), but cargo-shear
|
||||
# resolves those nested `#[path]` attributes against the wrong base directory.
|
||||
ignored-paths = ["rustfs/src/server/http/tests/**/*.rs"]
|
||||
ignored = ["hotpath", "rustfs"]
|
||||
|
||||
[profile.dev]
|
||||
# Full debuginfo roughly doubles compile+link time and produces multi-GB
|
||||
|
||||
@@ -15,11 +15,12 @@ workspace = true
|
||||
|
||||
[features]
|
||||
default = []
|
||||
hotpath = ["rustfs-io-core/hotpath"]
|
||||
hotpath-alloc = ["hotpath", "rustfs-io-core/hotpath-alloc"]
|
||||
hotpath-cpu = ["hotpath", "rustfs-io-core/hotpath-cpu"]
|
||||
hotpath = ["hotpath/hotpath", "hotpath/tokio", "rustfs-io-core/hotpath"]
|
||||
hotpath-alloc = ["hotpath", "hotpath/hotpath-alloc", "rustfs-io-core/hotpath-alloc"]
|
||||
hotpath-cpu = ["hotpath", "hotpath/hotpath-cpu", "rustfs-io-core/hotpath-cpu"]
|
||||
|
||||
[dependencies]
|
||||
hotpath.workspace = true
|
||||
# Internal crates
|
||||
rustfs-io-core = { workspace = true }
|
||||
serde = { workspace = true, features = ["derive"] }
|
||||
|
||||
@@ -171,6 +171,70 @@ impl DataUsageBucketObservation {
|
||||
}
|
||||
|
||||
impl DataUsageInfo {
|
||||
/// Hide values that cannot be bound to the current storage-owned identity.
|
||||
///
|
||||
/// `incarnations` contains canonical non-nil identities read from storage;
|
||||
/// a missing entry means identity was unavailable. Neither legacy values
|
||||
/// without identity nor metadata read failures establish confirmed zero.
|
||||
/// Changed responses are display-only and cannot authorize quota or writes.
|
||||
/// If retained counters overflow, all values remain unknown.
|
||||
/// Returns the number of canonical values removed.
|
||||
pub fn retain_current_bucket_usage_for_admin(&mut self, incarnations: &HashMap<String, String>) -> usize {
|
||||
let original_count = self.buckets_usage.len();
|
||||
let original_observation_count = self.usage_snapshot_bucket_observations.len();
|
||||
let matches_current = |bucket: &String, usage: &BucketUsageInfo| {
|
||||
incarnations.get(bucket).is_some_and(|current| {
|
||||
!current.is_empty()
|
||||
&& current != "00000000-0000-0000-0000-000000000000"
|
||||
&& usage.bucket_incarnation.as_ref() == Some(current)
|
||||
})
|
||||
};
|
||||
self.buckets_usage.retain(|bucket, usage| matches_current(bucket, usage));
|
||||
self.usage_snapshot_bucket_observations.retain(|bucket, observation| {
|
||||
matches_current(bucket, &observation.usage)
|
||||
&& observation
|
||||
.sources
|
||||
.iter()
|
||||
.all(|source| incarnations.get(bucket) == Some(&source.bucket_incarnation))
|
||||
});
|
||||
if original_count == self.buckets_usage.len()
|
||||
&& original_observation_count == self.usage_snapshot_bucket_observations.len()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
let totals = self.buckets_usage.values().try_fold([0_u64; 4], |total, usage| {
|
||||
Some([
|
||||
total[0].checked_add(usage.size)?,
|
||||
total[1].checked_add(usage.objects_count)?,
|
||||
total[2].checked_add(usage.versions_count)?,
|
||||
total[3].checked_add(usage.delete_markers_count)?,
|
||||
])
|
||||
});
|
||||
let [size, objects, versions, markers] = totals.unwrap_or_else(|| {
|
||||
self.buckets_usage.clear();
|
||||
self.usage_snapshot_bucket_observations.clear();
|
||||
[0; 4]
|
||||
});
|
||||
self.bucket_sizes = self
|
||||
.buckets_usage
|
||||
.iter()
|
||||
.map(|(bucket, usage)| (bucket.clone(), usage.size))
|
||||
.collect();
|
||||
self.buckets_count = u64::try_from(self.buckets_usage.len()).unwrap_or(u64::MAX);
|
||||
self.objects_total_size = size;
|
||||
self.objects_total_count = objects;
|
||||
self.versions_total_count = versions;
|
||||
self.delete_markers_total_count = markers;
|
||||
self.replication_info.clear();
|
||||
self.tier_stats = None;
|
||||
self.unknown_tier_stats = None;
|
||||
self.usage_snapshot_complete = false;
|
||||
self.usage_snapshot_partial = true;
|
||||
self.usage_snapshot_converged = Some(false);
|
||||
self.usage_snapshot_admin_projection = true;
|
||||
original_count.saturating_sub(self.buckets_usage.len())
|
||||
}
|
||||
|
||||
/// Overlay validated companions on an owned admin response only.
|
||||
///
|
||||
/// `incarnations` must come from current storage metadata, not the companion.
|
||||
|
||||
@@ -321,3 +321,110 @@ fn provenance_and_companions_change_candidate_bytes_and_round_trip_as_maps() {
|
||||
let restored: DataUsageInfo = rmp_serde::from_slice(&named).expect("read named map with additive fields");
|
||||
assert_eq!(restored, canonical);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn current_identity_filter_preserves_an_unchanged_complete_snapshot() {
|
||||
let mut candidate = snapshot();
|
||||
let original = candidate.clone();
|
||||
assert_eq!(candidate.retain_current_bucket_usage_for_admin(&incarnations()), 0);
|
||||
assert_eq!(candidate, original);
|
||||
assert!(candidate.is_complete_bucket_usage_snapshot());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn current_identity_filter_hides_unbound_values_without_confirming_zero() {
|
||||
for identity in [
|
||||
None,
|
||||
Some(""),
|
||||
Some("corrupt"),
|
||||
Some("00000000-0000-0000-0000-000000000000"),
|
||||
Some("22222222-2222-4222-8222-222222222222"),
|
||||
] {
|
||||
let mut candidate = snapshot();
|
||||
candidate.usage_snapshot_bucket_observations.clear();
|
||||
candidate
|
||||
.buckets_usage
|
||||
.get_mut("bucket")
|
||||
.expect("canonical")
|
||||
.bucket_incarnation = identity.map(str::to_string);
|
||||
assert_eq!(
|
||||
candidate.retain_current_bucket_usage_for_admin(&incarnations()),
|
||||
1,
|
||||
"identity {identity:?}"
|
||||
);
|
||||
assert!(!candidate.buckets_usage.contains_key("bucket"));
|
||||
assert!(!candidate.bucket_sizes.contains_key("bucket"));
|
||||
assert_eq!(candidate.usage_snapshot_converged, Some(false));
|
||||
assert!(candidate.usage_snapshot_admin_projection);
|
||||
assert!(!candidate.is_complete_bucket_usage_snapshot());
|
||||
assert!(!candidate.is_valid_partial_snapshot());
|
||||
}
|
||||
for current in ["", "00000000-0000-0000-0000-000000000000"] {
|
||||
let mut candidate = snapshot();
|
||||
candidate
|
||||
.buckets_usage
|
||||
.get_mut("bucket")
|
||||
.expect("canonical")
|
||||
.bucket_incarnation = Some(current.to_string());
|
||||
assert_eq!(
|
||||
candidate.retain_current_bucket_usage_for_admin(&HashMap::from([("bucket".to_string(), current.to_string())])),
|
||||
1
|
||||
);
|
||||
assert!(candidate.usage_snapshot_admin_projection);
|
||||
}
|
||||
let mut candidate = snapshot();
|
||||
assert_eq!(candidate.retain_current_bucket_usage_for_admin(&HashMap::new()), 1);
|
||||
assert!(candidate.usage_snapshot_bucket_observations.is_empty());
|
||||
assert_eq!(candidate.usage_snapshot_converged, Some(false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn current_identity_filter_keeps_healthy_buckets_and_recalculates_partial_totals() {
|
||||
let mut candidate = snapshot();
|
||||
candidate.buckets_usage.insert("cold".to_string(), usage(7));
|
||||
candidate.buckets_count = 2;
|
||||
candidate.bucket_sizes.insert("cold".to_string(), 7);
|
||||
candidate.calculate_totals();
|
||||
candidate.replication_info.insert("target".to_string(), Default::default());
|
||||
candidate.tier_stats = Some(Default::default());
|
||||
candidate.unknown_tier_stats = Some(Default::default());
|
||||
assert_eq!(
|
||||
candidate.retain_current_bucket_usage_for_admin(&HashMap::from([("cold".to_string(), INCARNATION.to_string())])),
|
||||
1
|
||||
);
|
||||
assert_eq!(candidate.buckets_usage.len(), 1);
|
||||
assert_eq!(candidate.buckets_usage["cold"].size, 7);
|
||||
assert_eq!(candidate.bucket_sizes, HashMap::from([("cold".to_string(), 7)]));
|
||||
assert_eq!(
|
||||
(
|
||||
candidate.buckets_count,
|
||||
candidate.objects_total_size,
|
||||
candidate.objects_total_count,
|
||||
candidate.versions_total_count
|
||||
),
|
||||
(1, 7, 7, 7)
|
||||
);
|
||||
assert!(candidate.replication_info.is_empty());
|
||||
assert!(candidate.tier_stats.is_none());
|
||||
assert!(candidate.unknown_tier_stats.is_none());
|
||||
assert!(candidate.usage_snapshot_admin_projection);
|
||||
assert_eq!(candidate.usage_snapshot_converged, Some(false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn current_identity_filter_overflow_leaves_totals_unknown() {
|
||||
let mut candidate = snapshot();
|
||||
candidate.buckets_usage.insert("large".to_string(), usage(u64::MAX));
|
||||
candidate.buckets_usage.insert("small".to_string(), usage(1));
|
||||
candidate.buckets_count = 3;
|
||||
let current = HashMap::from([
|
||||
("large".to_string(), INCARNATION.to_string()),
|
||||
("small".to_string(), INCARNATION.to_string()),
|
||||
]);
|
||||
assert_eq!(candidate.retain_current_bucket_usage_for_admin(¤t), 3);
|
||||
assert!(candidate.buckets_usage.is_empty());
|
||||
assert!(candidate.bucket_sizes.is_empty());
|
||||
assert_eq!(candidate.usage_snapshot_converged, Some(false));
|
||||
assert!(candidate.usage_snapshot_admin_projection);
|
||||
assert!(!candidate.is_complete_bucket_usage_snapshot());
|
||||
}
|
||||
|
||||
@@ -183,6 +183,7 @@ rmp.workspace = true
|
||||
rmp-serde.workspace = true
|
||||
tokio-util = { workspace = true, features = ["io", "compat", "rt"] }
|
||||
tokio-stream = { workspace = true, features = ["sync"] }
|
||||
hmac = { workspace = true }
|
||||
sha2 = { workspace = true }
|
||||
hex-simd = { workspace = true }
|
||||
tempfile.workspace = true
|
||||
|
||||
@@ -454,8 +454,8 @@ pub mod data_usage {
|
||||
DATA_USAGE_CACHE_NAME, apply_bucket_usage_memory_overlay, compute_bucket_usage,
|
||||
init_compression_total_memory_from_backend, invalidate_admin_data_usage_snapshot_cache,
|
||||
invalidate_data_usage_snapshot_cache, live_bucket_usage_computations, load_admin_data_usage_from_backend_cached,
|
||||
load_bucket_usage_from_backend_cached, load_compression_total_from_memory, load_data_usage_from_backend,
|
||||
load_data_usage_from_backend_cached, quota_object_size, record_bucket_delete_marker_memory,
|
||||
load_admin_data_usage_with_memory_overlay, load_bucket_usage_from_backend_cached, load_compression_total_from_memory,
|
||||
load_data_usage_from_backend, load_data_usage_from_backend_cached, quota_object_size, record_bucket_delete_marker_memory,
|
||||
record_bucket_object_delete_memory, record_bucket_object_version_write_memory, record_bucket_object_write_memory,
|
||||
record_bucket_object_write_unknown_previous_memory, record_compression_total_memory,
|
||||
refresh_bucket_usage_from_object_layer, refresh_versioned_bucket_usage_from_object_layer,
|
||||
|
||||
@@ -1463,22 +1463,39 @@ async fn load_admin_data_usage_from_backend(store: Arc<ECStore>) -> Result<DataU
|
||||
let observed = load_observed_data_usage_snapshot(store.clone()).await.ok().flatten();
|
||||
let (selected, selected_is_current_format) =
|
||||
select_admin_data_usage_snapshot(authoritative, source.is_authoritative(), observed);
|
||||
let mut response = normalize_loaded_data_usage(selected, selected_is_current_format).await.0;
|
||||
Ok(normalize_loaded_data_usage(selected, selected_is_current_format).await.0)
|
||||
}
|
||||
|
||||
async fn project_admin_data_usage_for_current_buckets(
|
||||
store: Arc<ECStore>,
|
||||
mut response: DataUsageInfo,
|
||||
with_memory_overlay: bool,
|
||||
) -> DataUsageInfo {
|
||||
use futures::StreamExt as _;
|
||||
if !(response.is_complete_bucket_usage_snapshot() || response.is_valid_partial_snapshot()) {
|
||||
return DataUsageInfo::default();
|
||||
}
|
||||
|
||||
let overlay_authoritative = with_memory_overlay && !runtime_sources::setup_is_dist_erasure().await;
|
||||
let deadline = tokio::time::Instant::now() + ADMIN_USAGE_INCARNATION_TIMEOUT;
|
||||
let mut incarnations = HashMap::new();
|
||||
{
|
||||
// Display-only identity reads must not serialize every bucket behind a
|
||||
// slow metadata lock while holding the shared admin refresh permit.
|
||||
// The common deadline bounds their total added latency; dropping this
|
||||
// stream cancels all unfinished reads without spawning detached work.
|
||||
// Own the names and store across awaits so this future remains Send
|
||||
// when composed into spawned metrics collectors.
|
||||
let buckets = response
|
||||
.usage_snapshot_bucket_observations
|
||||
// Identity reads happen after releasing snapshot and refresh locks,
|
||||
// including on cache hits. Metadata transaction reads linearize each
|
||||
// bucket against creation/deletion without relying on peer notices.
|
||||
// The common deadline cancels unfinished work and bounds added latency.
|
||||
let mut buckets = response
|
||||
.buckets_usage
|
||||
.keys()
|
||||
.chain(response.usage_snapshot_bucket_observations.keys())
|
||||
.cloned()
|
||||
.collect::<Vec<_>>();
|
||||
.collect::<HashSet<_>>();
|
||||
if overlay_authoritative {
|
||||
// Copy only names while holding the memory lock, then release it
|
||||
// before storage I/O. A later new entry has no identity proof and
|
||||
// will remain unknown in this response.
|
||||
buckets.extend(memory_cache().read().await.keys().cloned());
|
||||
}
|
||||
let mut identities = futures::stream::iter(buckets)
|
||||
.map(move |bucket| {
|
||||
let store = store.clone();
|
||||
@@ -1492,7 +1509,6 @@ async fn load_admin_data_usage_from_backend(store: Arc<ECStore>) -> Result<DataU
|
||||
let Ok(Some((bucket, identity))) = tokio::time::timeout_at(deadline, identities.next()).await else {
|
||||
break;
|
||||
};
|
||||
// Unavailable, timed-out or recreated buckets retain canonical values.
|
||||
if let Ok(incarnation) = identity
|
||||
&& !incarnation.is_nil()
|
||||
{
|
||||
@@ -1501,7 +1517,17 @@ async fn load_admin_data_usage_from_backend(store: Arc<ECStore>) -> Result<DataU
|
||||
}
|
||||
}
|
||||
response.project_bucket_observations_for_admin(&incarnations);
|
||||
Ok(response)
|
||||
if apply_bucket_usage_memory_overlay_if_authoritative(&mut response, overlay_authoritative).await {
|
||||
response.usage_snapshot_complete = false;
|
||||
response.usage_snapshot_partial = true;
|
||||
response.usage_snapshot_converged = Some(false);
|
||||
response.usage_snapshot_admin_projection = true;
|
||||
response.replication_info.clear();
|
||||
response.tier_stats = None;
|
||||
response.unknown_tier_stats = None;
|
||||
}
|
||||
response.retain_current_bucket_usage_for_admin(&incarnations);
|
||||
response
|
||||
}
|
||||
|
||||
fn discard_incomplete_bucket_usage(data_usage_info: &mut DataUsageInfo) {
|
||||
@@ -1690,11 +1716,7 @@ async fn project_data_usage_from_backend_cached<T>(
|
||||
}
|
||||
}
|
||||
|
||||
/// Load the freshest structurally complete snapshot for authenticated admin
|
||||
/// observability. A scan raced by namespace activity may be selected here, but
|
||||
/// never by [`load_data_usage_from_backend_cached`], which remains the
|
||||
/// converged source for quota admission.
|
||||
pub async fn load_admin_data_usage_from_backend_cached(store: Arc<ECStore>) -> Result<DataUsageInfo, Error> {
|
||||
async fn load_admin_data_usage_snapshot_cached(store: Arc<ECStore>) -> Result<DataUsageInfo, Error> {
|
||||
let ttl = Duration::from_secs(DATA_USAGE_CACHE_TTL_SECS);
|
||||
|
||||
loop {
|
||||
@@ -1743,6 +1765,38 @@ pub async fn load_admin_data_usage_from_backend_cached(store: Arc<ECStore>) -> R
|
||||
}
|
||||
}
|
||||
|
||||
/// Load cached scanner statistics for authenticated admin observability.
|
||||
///
|
||||
/// Each response verifies bucket identities against current storage metadata,
|
||||
/// including cache hits. Missing, failed, or timed-out identity reads leave that
|
||||
/// bucket's usage absent (unknown). Legacy values without an incarnation stay
|
||||
/// unknown until a scanner snapshot certifies their identity. Per-bucket reads
|
||||
/// linearize independently; this is not an atomic namespace snapshot.
|
||||
/// Identity reads have a shared deadline and do not outlive cancellation. The
|
||||
/// unchanged cached baseline remains independent of quota and persistence.
|
||||
///
|
||||
/// # Errors
|
||||
/// Returns the cached snapshot loading error when no baseline can be loaded.
|
||||
pub async fn load_admin_data_usage_from_backend_cached(store: Arc<ECStore>) -> Result<DataUsageInfo, Error> {
|
||||
let response = load_admin_data_usage_snapshot_cached(store.clone()).await?;
|
||||
Ok(project_admin_data_usage_for_current_buckets(store, response, false).await)
|
||||
}
|
||||
|
||||
/// Load the admin view with the existing authoritative single-process overlay.
|
||||
///
|
||||
/// Only a complete or valid partial scanner snapshot can receive memory values.
|
||||
/// The final identity check excludes statistics from retired or unverified buckets.
|
||||
/// Distributed nodes continue to use scanner statistics without local totals.
|
||||
/// Cancellation, unknown values, and per-bucket read semantics are the same as
|
||||
/// [`load_admin_data_usage_from_backend_cached`]. No overlay is persisted.
|
||||
///
|
||||
/// # Errors
|
||||
/// Returns the cached snapshot loading error when no baseline can be loaded.
|
||||
pub async fn load_admin_data_usage_with_memory_overlay(store: Arc<ECStore>) -> Result<DataUsageInfo, Error> {
|
||||
let response = load_admin_data_usage_snapshot_cached(store.clone()).await?;
|
||||
Ok(project_admin_data_usage_for_current_buckets(store, response, true).await)
|
||||
}
|
||||
|
||||
/// Invalidate the process-local persisted usage snapshot cache after a durable
|
||||
/// scanner write.
|
||||
pub async fn invalidate_data_usage_snapshot_cache() {
|
||||
@@ -2805,16 +2859,17 @@ async fn replace_bucket_usage_memory_from_info_if_generation(data_usage_info: &D
|
||||
*cache = next_cache;
|
||||
}
|
||||
|
||||
async fn apply_bucket_usage_memory_overlay_if_authoritative(data_usage_info: &mut DataUsageInfo, authoritative: bool) {
|
||||
async fn apply_bucket_usage_memory_overlay_if_authoritative(data_usage_info: &mut DataUsageInfo, authoritative: bool) -> bool {
|
||||
if !authoritative {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
let cache = memory_cache().read().await;
|
||||
if cache.is_empty() {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
let was_complete = data_usage_info.usage_snapshot_complete;
|
||||
let persisted_update = data_usage_info.last_update;
|
||||
let mut changed = false;
|
||||
let mut added_bucket = false;
|
||||
@@ -2851,6 +2906,7 @@ async fn apply_bucket_usage_memory_overlay_if_authoritative(data_usage_info: &mu
|
||||
data_usage_info.buckets_count = data_usage_info.buckets_usage.len() as u64;
|
||||
data_usage_info.calculate_totals();
|
||||
}
|
||||
changed || was_complete != data_usage_info.usage_snapshot_complete
|
||||
}
|
||||
|
||||
pub async fn apply_bucket_usage_memory_overlay(data_usage_info: &mut DataUsageInfo) {
|
||||
@@ -3744,6 +3800,23 @@ mod tests {
|
||||
*cache_updating().write().await = false;
|
||||
}
|
||||
|
||||
async fn bind_admin_usage_test_identities(store: &Arc<ECStore>, snapshot: &mut DataUsageInfo) {
|
||||
crate::bucket::metadata_sys::init_bucket_metadata_sys(store.clone(), Vec::new()).await;
|
||||
for (bucket, usage) in &mut snapshot.buckets_usage {
|
||||
store
|
||||
.make_bucket(bucket, &crate::storage_api_contracts::bucket::MakeBucketOptions::default())
|
||||
.await
|
||||
.expect("create a real bucket for current admin identity validation");
|
||||
usage.bucket_incarnation = Some(
|
||||
store
|
||||
.bucket_incarnation_id_from_disk(bucket)
|
||||
.await
|
||||
.expect("read the storage-owned fixture identity")
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn data_usage_info_for_test(bucket: &str, objects_count: u64, size: u64, last_update: SystemTime) -> DataUsageInfo {
|
||||
let mut info = DataUsageInfo {
|
||||
last_update: Some(last_update),
|
||||
@@ -4279,7 +4352,11 @@ mod tests {
|
||||
use crate::config::com::save_config;
|
||||
|
||||
let (_dirs, store) = isolated_store_over_temp_disks().await;
|
||||
let (authoritative, observed) = usage_snapshots_with_missing_observed_set();
|
||||
let (mut authoritative, mut observed) = usage_snapshots_with_missing_observed_set();
|
||||
bind_admin_usage_test_identities(&store, &mut authoritative).await;
|
||||
for (bucket, usage) in &mut observed.buckets_usage {
|
||||
usage.bucket_incarnation = authoritative.buckets_usage[bucket].bucket_incarnation.clone();
|
||||
}
|
||||
let primary_bytes = serde_json::to_vec(&authoritative).expect("encode complete usage");
|
||||
let observed_bytes = serde_json::to_vec(&observed).expect("encode partial observation");
|
||||
save_config(store.clone(), DATA_USAGE_OBJ_NAME_PATH.as_str(), primary_bytes.clone())
|
||||
@@ -4328,6 +4405,11 @@ mod tests {
|
||||
reconciled.scanner_epoch = Some(4);
|
||||
reconciled.scanner_cycle = Some(12);
|
||||
reconciled.usage_snapshot_converged = Some(true);
|
||||
reconciled
|
||||
.buckets_usage
|
||||
.get_mut("large-bucket")
|
||||
.expect("reconciled bucket")
|
||||
.bucket_incarnation = authoritative.buckets_usage["large-bucket"].bucket_incarnation.clone();
|
||||
save_config(
|
||||
store.clone(),
|
||||
DATA_USAGE_OBJ_NAME_PATH.as_str(),
|
||||
@@ -4849,6 +4931,12 @@ mod tests {
|
||||
backup.scanner_epoch = Some(4);
|
||||
backup.scanner_cycle = Some(10);
|
||||
backup.usage_snapshot_converged = Some(true);
|
||||
bind_admin_usage_test_identities(&store, &mut current).await;
|
||||
backup
|
||||
.buckets_usage
|
||||
.get_mut("existing")
|
||||
.expect("backup bucket")
|
||||
.bucket_incarnation = current.buckets_usage["existing"].bucket_incarnation.clone();
|
||||
for (path, info) in [
|
||||
(DATA_USAGE_OBJ_NAME_PATH.as_str(), ¤t),
|
||||
(DATA_USAGE_OBJ_BACKUP_PATH.as_str(), &backup),
|
||||
@@ -4871,6 +4959,11 @@ mod tests {
|
||||
after_delete.scanner_epoch = current.scanner_epoch;
|
||||
after_delete.scanner_cycle = current.scanner_cycle;
|
||||
after_delete.usage_snapshot_converged = Some(true);
|
||||
after_delete
|
||||
.buckets_usage
|
||||
.get_mut("existing")
|
||||
.expect("post-delete bucket")
|
||||
.bucket_incarnation = current.buckets_usage["existing"].bucket_incarnation.clone();
|
||||
let mut stale_observation =
|
||||
data_usage_info_for_test("retired-incarnation", 99, 999, SystemTime::UNIX_EPOCH + Duration::from_secs(4));
|
||||
stale_observation.scanner_epoch = Some(4);
|
||||
@@ -4932,6 +5025,7 @@ mod tests {
|
||||
authoritative.scanner_epoch = Some(7);
|
||||
authoritative.scanner_cycle = Some(10);
|
||||
authoritative.usage_snapshot_converged = Some(true);
|
||||
bind_admin_usage_test_identities(&store, &mut authoritative).await;
|
||||
save_config(
|
||||
store.clone(),
|
||||
DATA_USAGE_OBJ_NAME_PATH.as_str(),
|
||||
@@ -4946,6 +5040,10 @@ mod tests {
|
||||
info.scanner_cycle = Some(cycle);
|
||||
info.usage_snapshot_converged = Some(false);
|
||||
info.usage_snapshot_authoritative_baseline = Some(authoritative.snapshot_identity());
|
||||
info.buckets_usage
|
||||
.get_mut("bucket")
|
||||
.expect("observed bucket")
|
||||
.bucket_incarnation = authoritative.buckets_usage["bucket"].bucket_incarnation.clone();
|
||||
info
|
||||
};
|
||||
let first = observation(7, 11, 200, 11, 1_100);
|
||||
|
||||
@@ -221,7 +221,7 @@ async fn companion_admin_cache_isolated_from_quota_memory_and_persistence() {
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
||||
#[serial]
|
||||
async fn blocked_companion_identity_keeps_canonical_without_delaying_healthy_buckets() {
|
||||
async fn blocked_companion_identity_stays_unknown_without_delaying_healthy_buckets() {
|
||||
use crate::bucket::metadata_sys::{
|
||||
acquire_metadata_transaction_guard, init_bucket_metadata_sys, test_support::isolated_store_over_temp_disks,
|
||||
};
|
||||
@@ -280,8 +280,11 @@ async fn blocked_companion_identity_keeps_canonical_without_delaying_healthy_buc
|
||||
.await
|
||||
.expect("optional projection must return before the blocked metadata lock's long timeout")
|
||||
.expect("canonical admin response stays available");
|
||||
assert_eq!(response.buckets_usage[blocked].size, 100);
|
||||
assert_eq!(response.buckets_usage[blocked].usage_value_state, Some(BucketUsageValueState::Carried));
|
||||
assert!(
|
||||
!response.buckets_usage.contains_key(blocked),
|
||||
"an unavailable current identity cannot bind the canonical value"
|
||||
);
|
||||
assert!(!response.bucket_sizes.contains_key(blocked));
|
||||
assert_eq!(response.buckets_usage[healthy].size, 250);
|
||||
assert_eq!(response.buckets_usage[healthy].usage_value_state, Some(BucketUsageValueState::Observed));
|
||||
assert!(response.usage_snapshot_admin_projection);
|
||||
@@ -293,13 +296,12 @@ async fn blocked_companion_identity_keeps_canonical_without_delaying_healthy_buc
|
||||
.is_ok()
|
||||
);
|
||||
drop(blocked_guard);
|
||||
invalidate_data_usage_snapshot_cache().await;
|
||||
let recovered = load_admin_data_usage_from_backend_cached(store.clone())
|
||||
.await
|
||||
.expect("refresh after lock recovery");
|
||||
assert_eq!(
|
||||
recovered.buckets_usage[blocked].size, 250,
|
||||
"unavailable identity can recover on the next refresh"
|
||||
"unavailable identity can recover on a cache hit"
|
||||
);
|
||||
assert_eq!(recovered.buckets_usage[healthy].size, 250);
|
||||
let quota = load_data_usage_from_backend_cached(store)
|
||||
@@ -308,3 +310,331 @@ async fn blocked_companion_identity_keeps_canonical_without_delaying_healthy_buc
|
||||
assert_eq!(quota.buckets_usage[blocked].size, 100);
|
||||
assert_eq!(quota.buckets_usage[healthy].size, 100);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
||||
#[serial]
|
||||
async fn cached_canonical_admin_usage_rechecks_current_identity_without_peer_invalidation() {
|
||||
use crate::bucket::metadata::{load_bucket_incarnation, save_bucket_incarnation};
|
||||
use crate::bucket::metadata_sys::{init_bucket_metadata_sys, test_support::isolated_store_over_temp_disks};
|
||||
use crate::config::com::save_config;
|
||||
use crate::storage_api_contracts::bucket::MakeBucketOptions;
|
||||
|
||||
let (_dirs, store) = isolated_store_over_temp_disks().await;
|
||||
init_bucket_metadata_sys(store.clone(), Vec::new()).await;
|
||||
let bucket = "usage-recreated-cache";
|
||||
store
|
||||
.make_bucket(bucket, &MakeBucketOptions::default())
|
||||
.await
|
||||
.expect("create identity fixture");
|
||||
let old_identity = store
|
||||
.bucket_incarnation_id_from_disk(bucket)
|
||||
.await
|
||||
.expect("original identity");
|
||||
let mut canonical = companion_snapshot(bucket, old_identity);
|
||||
canonical.usage_snapshot_bucket_observations.clear();
|
||||
canonical.usage_snapshot_converged = Some(true);
|
||||
canonical.usage_snapshot_pending_buckets.clear();
|
||||
canonical.usage_snapshot_pending_bucket_count = 0;
|
||||
canonical
|
||||
.buckets_usage
|
||||
.get_mut(bucket)
|
||||
.expect("canonical bucket")
|
||||
.objects_count = 107;
|
||||
canonical.calculate_totals();
|
||||
let original_bytes = serde_json::to_vec(&canonical).expect("encode canonical old bucket");
|
||||
save_config(store.clone(), DATA_USAGE_OBJ_NAME_PATH.as_str(), original_bytes.clone())
|
||||
.await
|
||||
.expect("persist canonical root");
|
||||
invalidate_data_usage_snapshot_cache().await;
|
||||
let first = load_admin_data_usage_from_backend_cached(store.clone())
|
||||
.await
|
||||
.expect("prime raw admin cache");
|
||||
assert_eq!(first.buckets_usage[bucket].objects_count, 107);
|
||||
assert!(first.is_complete_bucket_usage_snapshot());
|
||||
let cache_generation = admin_data_usage_snapshot_generation();
|
||||
let new_identity = uuid::Uuid::new_v4();
|
||||
// Simulate the storage-owned identity changing on another node while this
|
||||
// process has not received its invalidation notice. Do not clear the cache.
|
||||
save_bucket_incarnation(store.clone(), bucket, new_identity)
|
||||
.await
|
||||
.expect("publish changed disk identity");
|
||||
assert_eq!(
|
||||
load_bucket_incarnation(store.clone(), bucket)
|
||||
.await
|
||||
.expect("read new sidecar"),
|
||||
Some(new_identity)
|
||||
);
|
||||
let second = load_admin_data_usage_from_backend_cached(store.clone())
|
||||
.await
|
||||
.expect("read raw cache with fresh identity");
|
||||
assert_eq!(admin_data_usage_snapshot_generation(), cache_generation);
|
||||
assert!(
|
||||
!second.buckets_usage.contains_key(bucket),
|
||||
"same name cannot retain the predecessor's 107"
|
||||
);
|
||||
assert!(!second.bucket_sizes.contains_key(bucket));
|
||||
assert_eq!(second.usage_snapshot_converged, Some(false));
|
||||
assert!(second.usage_snapshot_admin_projection);
|
||||
assert!(reject_admin_usage_projection(&second).is_err());
|
||||
assert!(certified_bucket_usage(&second, bucket, new_identity).is_none());
|
||||
assert!(parse_usage_snapshot(&serde_json::to_vec(&second).expect("encode display response")).is_err());
|
||||
let quota = load_data_usage_from_backend_cached(store.clone())
|
||||
.await
|
||||
.expect("independent quota baseline");
|
||||
assert_eq!(quota.buckets_usage[bucket].objects_count, 107);
|
||||
assert_eq!(
|
||||
read_config(store.clone(), DATA_USAGE_OBJ_NAME_PATH.as_str())
|
||||
.await
|
||||
.expect("unchanged persisted root"),
|
||||
original_bytes
|
||||
);
|
||||
// Rebinding a cached raw baseline is transient: recovery does not need to
|
||||
// reload or permanently blank a healthy bucket's statistics.
|
||||
save_bucket_incarnation(store.clone(), bucket, old_identity)
|
||||
.await
|
||||
.expect("restore fixture identity");
|
||||
let recovered = load_admin_data_usage_from_backend_cached(store)
|
||||
.await
|
||||
.expect("recover on another cache hit");
|
||||
assert_eq!(recovered.buckets_usage[bucket].objects_count, 107);
|
||||
assert!(recovered.is_complete_bucket_usage_snapshot());
|
||||
assert_eq!(admin_data_usage_snapshot_generation(), cache_generation);
|
||||
invalidate_data_usage_snapshot_cache().await;
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
||||
#[serial]
|
||||
async fn admin_memory_overlay_requires_a_measured_scanner_baseline() {
|
||||
use crate::bucket::metadata_sys::{init_bucket_metadata_sys, test_support::isolated_store_over_temp_disks};
|
||||
use crate::config::com::save_config;
|
||||
use crate::storage_api_contracts::bucket::MakeBucketOptions;
|
||||
|
||||
let (_dirs, store) = isolated_store_over_temp_disks().await;
|
||||
init_bucket_metadata_sys(store.clone(), Vec::new()).await;
|
||||
assert!(!runtime_sources::setup_is_dist_erasure().await);
|
||||
let bucket = "usage-unmeasured-overlay";
|
||||
store.make_bucket(bucket, &MakeBucketOptions::default()).await.unwrap();
|
||||
let incarnation = store.bucket_incarnation_id_from_disk(bucket).await.unwrap();
|
||||
let mut baseline = data_usage_info_for_test(bucket, 1, 100, SystemTime::UNIX_EPOCH);
|
||||
let usage = baseline.buckets_usage.get_mut(bucket).unwrap();
|
||||
usage.bucket_incarnation = Some(incarnation.to_string());
|
||||
let memory_usage = BucketUsageInfo {
|
||||
size: 150,
|
||||
..usage.clone()
|
||||
};
|
||||
clear_usage_memory_cache_for_test().await;
|
||||
replace_bucket_usage_memory_from_authoritative(bucket, memory_usage, SystemTime::now()).await;
|
||||
|
||||
// A missing snapshot and a mismatched bucket count both leave usage unknown.
|
||||
for (buckets_count, expected_size) in [(None, None), (Some(2), None), (Some(1), Some(150))] {
|
||||
if let Some(count) = buckets_count {
|
||||
baseline.buckets_count = count;
|
||||
save_config(store.clone(), DATA_USAGE_OBJ_NAME_PATH.as_str(), serde_json::to_vec(&baseline).unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
invalidate_data_usage_snapshot_cache().await;
|
||||
let response = load_admin_data_usage_with_memory_overlay(store.clone()).await.unwrap();
|
||||
assert_eq!(
|
||||
response.buckets_usage.get(bucket).map(|usage| usage.size),
|
||||
expected_size,
|
||||
"baseline count {buckets_count:?}"
|
||||
);
|
||||
assert_eq!(response.bucket_sizes.get(bucket).copied(), expected_size);
|
||||
assert_eq!(response.objects_total_size, expected_size.unwrap_or_default());
|
||||
assert_eq!(response.usage_snapshot_admin_projection, expected_size.is_some());
|
||||
assert!(!response.is_complete_bucket_usage_snapshot());
|
||||
assert!(!response.is_valid_partial_snapshot());
|
||||
}
|
||||
clear_usage_memory_cache_for_test().await;
|
||||
invalidate_data_usage_snapshot_cache().await;
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
||||
#[serial]
|
||||
async fn admin_identity_filter_does_not_validate_an_unmeasured_partial_snapshot() {
|
||||
use crate::bucket::metadata_sys::test_support::isolated_store_over_temp_disks;
|
||||
use crate::config::com::save_config;
|
||||
|
||||
let (_dirs, store) = isolated_store_over_temp_disks().await;
|
||||
let current = "usage-partial-current";
|
||||
let stale = "usage-partial-stale";
|
||||
let mut baseline = data_usage_info_for_test(current, 5, 100, SystemTime::UNIX_EPOCH);
|
||||
baseline.buckets_usage.insert(
|
||||
stale.to_owned(),
|
||||
BucketUsageInfo {
|
||||
size: 200,
|
||||
objects_count: 10,
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
baseline.bucket_sizes.insert(stale.to_owned(), 200);
|
||||
baseline.buckets_count = 2;
|
||||
baseline.calculate_totals();
|
||||
baseline.usage_snapshot_complete = false;
|
||||
baseline.usage_snapshot_partial = true;
|
||||
baseline.usage_snapshot_converged = Some(false);
|
||||
baseline.scanner_epoch = Some(2);
|
||||
baseline.scanner_cycle = Some(9);
|
||||
baseline.usage_snapshot_set_states = vec![DataUsageSnapshotSetState {
|
||||
scanner_epoch: baseline.scanner_epoch,
|
||||
scanner_cycle: baseline.scanner_cycle,
|
||||
scan_plan_digest: Some([1; 32]),
|
||||
..Default::default()
|
||||
}];
|
||||
bind_admin_usage_test_identities(&store, &mut baseline).await;
|
||||
let current_incarnation = baseline.buckets_usage[current].bucket_incarnation.clone();
|
||||
assert_ne!(current_incarnation, baseline.buckets_usage[stale].bucket_incarnation);
|
||||
baseline
|
||||
.buckets_usage
|
||||
.get_mut(stale)
|
||||
.expect("stale bucket fixture")
|
||||
.bucket_incarnation = current_incarnation;
|
||||
|
||||
for (case, snapshot, expected_size) in [
|
||||
(
|
||||
"missing set provenance",
|
||||
DataUsageInfo {
|
||||
usage_snapshot_set_states: Vec::new(),
|
||||
..baseline.clone()
|
||||
},
|
||||
None,
|
||||
),
|
||||
(
|
||||
"mismatched bucket count",
|
||||
DataUsageInfo {
|
||||
buckets_count: 3,
|
||||
..baseline.clone()
|
||||
},
|
||||
None,
|
||||
),
|
||||
("valid partial snapshot", baseline, Some(100)),
|
||||
] {
|
||||
save_config(
|
||||
store.clone(),
|
||||
DATA_USAGE_OBJ_NAME_PATH.as_str(),
|
||||
serde_json::to_vec(&snapshot).expect("encode partial snapshot"),
|
||||
)
|
||||
.await
|
||||
.expect("persist partial snapshot");
|
||||
invalidate_admin_data_usage_snapshot_cache().await;
|
||||
|
||||
let response = load_admin_data_usage_from_backend_cached(store.clone())
|
||||
.await
|
||||
.expect("load admin usage through the identity filter");
|
||||
assert_eq!(response.buckets_usage.get(current).map(|usage| usage.size), expected_size, "{case}");
|
||||
assert_eq!(response.bucket_sizes.get(current).copied(), expected_size, "{case}");
|
||||
assert!(!response.buckets_usage.contains_key(stale), "{case}");
|
||||
assert!(!response.bucket_sizes.contains_key(stale), "{case}");
|
||||
assert_eq!(response.objects_total_size, expected_size.unwrap_or_default(), "{case}");
|
||||
assert_eq!(response.usage_snapshot_admin_projection, expected_size.is_some(), "{case}");
|
||||
}
|
||||
invalidate_admin_data_usage_snapshot_cache().await;
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
||||
#[serial]
|
||||
async fn admin_memory_overlay_cannot_reintroduce_a_predecessor_identity() {
|
||||
use crate::bucket::metadata::save_bucket_incarnation;
|
||||
use crate::bucket::metadata_sys::{init_bucket_metadata_sys, test_support::isolated_store_over_temp_disks};
|
||||
use crate::config::com::save_config;
|
||||
use crate::storage_api_contracts::bucket::MakeBucketOptions;
|
||||
|
||||
let (_dirs, store) = isolated_store_over_temp_disks().await;
|
||||
init_bucket_metadata_sys(store.clone(), Vec::new()).await;
|
||||
let bucket = "usage-recreated-overlay";
|
||||
store
|
||||
.make_bucket(bucket, &MakeBucketOptions::default())
|
||||
.await
|
||||
.expect("create overlay identity fixture");
|
||||
let old_identity = store
|
||||
.bucket_incarnation_id_from_disk(bucket)
|
||||
.await
|
||||
.expect("original identity");
|
||||
let mut canonical = companion_snapshot(bucket, old_identity);
|
||||
canonical.usage_snapshot_bucket_observations.clear();
|
||||
save_config(
|
||||
store.clone(),
|
||||
DATA_USAGE_OBJ_NAME_PATH.as_str(),
|
||||
serde_json::to_vec(&canonical).expect("encode canonical root"),
|
||||
)
|
||||
.await
|
||||
.expect("persist overlay fixture");
|
||||
invalidate_data_usage_snapshot_cache().await;
|
||||
let cached_usage = canonical.buckets_usage[bucket].clone();
|
||||
replace_bucket_usage_memory_from_authoritative(bucket, cached_usage, SystemTime::now()).await;
|
||||
let new_identity = uuid::Uuid::new_v4();
|
||||
save_bucket_incarnation(store.clone(), bucket, new_identity)
|
||||
.await
|
||||
.expect("publish a replacement identity");
|
||||
let response = load_admin_data_usage_with_memory_overlay(store.clone())
|
||||
.await
|
||||
.expect("project after memory overlay");
|
||||
assert!(!response.buckets_usage.contains_key(bucket));
|
||||
assert_eq!(response.usage_snapshot_converged, Some(false));
|
||||
assert!(response.usage_snapshot_admin_projection);
|
||||
clear_usage_memory_cache_for_test().await;
|
||||
invalidate_data_usage_snapshot_cache().await;
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
||||
#[serial]
|
||||
async fn admin_overlay_flags_only_change_keeps_identity_bound_cold_usage() {
|
||||
use crate::bucket::metadata_sys::{init_bucket_metadata_sys, test_support::isolated_store_over_temp_disks};
|
||||
use crate::config::com::save_config;
|
||||
use crate::storage_api_contracts::bucket::MakeBucketOptions;
|
||||
|
||||
let (_dirs, store) = isolated_store_over_temp_disks().await;
|
||||
init_bucket_metadata_sys(store.clone(), Vec::new()).await;
|
||||
assert!(
|
||||
!runtime_sources::setup_is_dist_erasure().await,
|
||||
"exercise the production single-process overlay"
|
||||
);
|
||||
let cold = "usage-overlay-cold";
|
||||
let unverified = "usage-overlay-unverified";
|
||||
for bucket in [cold, unverified] {
|
||||
store
|
||||
.make_bucket(bucket, &MakeBucketOptions::default())
|
||||
.await
|
||||
.expect("create overlay fixture bucket");
|
||||
}
|
||||
let identity = store
|
||||
.bucket_incarnation_id_from_disk(cold)
|
||||
.await
|
||||
.expect("cold bucket identity");
|
||||
let mut canonical = companion_snapshot(cold, identity);
|
||||
canonical.usage_snapshot_bucket_observations.clear();
|
||||
canonical.usage_snapshot_converged = Some(true);
|
||||
save_config(
|
||||
store.clone(),
|
||||
DATA_USAGE_OBJ_NAME_PATH.as_str(),
|
||||
serde_json::to_vec(&canonical).expect("encode cold canonical value"),
|
||||
)
|
||||
.await
|
||||
.expect("persist cold baseline");
|
||||
clear_usage_memory_cache_for_test().await;
|
||||
invalidate_data_usage_snapshot_cache().await;
|
||||
memory_cache().write().await.insert(
|
||||
unverified.to_string(),
|
||||
cached_bucket_usage_from_backend(BucketUsageInfo::default(), SystemTime::UNIX_EPOCH, false),
|
||||
);
|
||||
record_bucket_object_write_memory(unverified, None, 42).await;
|
||||
{
|
||||
let cache = memory_cache().read().await;
|
||||
assert!(cache[unverified].dirty);
|
||||
assert!(!cache[unverified].authoritative);
|
||||
}
|
||||
let response = load_admin_data_usage_with_memory_overlay(store)
|
||||
.await
|
||||
.expect("identity-filtered flags-only overlay");
|
||||
assert_eq!(response.buckets_usage[cold].size, 100);
|
||||
assert_eq!(response.objects_total_size, 100);
|
||||
assert_eq!(response.bucket_sizes[cold], 100);
|
||||
assert!(!response.buckets_usage.contains_key(unverified));
|
||||
assert!(response.usage_snapshot_admin_projection);
|
||||
assert_eq!(response.usage_snapshot_converged, Some(false));
|
||||
assert!(!response.is_complete_bucket_usage_snapshot());
|
||||
assert!(!response.is_valid_partial_snapshot());
|
||||
clear_usage_memory_cache_for_test().await;
|
||||
invalidate_data_usage_snapshot_cache().await;
|
||||
}
|
||||
|
||||
@@ -1033,16 +1033,26 @@ mod tests {
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn server_info_reads_complete_observation_without_promoting_authority() {
|
||||
use crate::bucket::metadata_sys::test_support::isolated_store_over_temp_disks;
|
||||
use crate::bucket::metadata_sys::{init_bucket_metadata_sys, test_support::isolated_store_over_temp_disks};
|
||||
use crate::config::com::{read_config, save_config};
|
||||
use crate::data_usage::{
|
||||
DATA_USAGE_OBJ_NAME_PATH, DATA_USAGE_OBSERVED_OBJ_NAME_PATH, invalidate_data_usage_snapshot_cache,
|
||||
load_data_usage_from_backend_cached,
|
||||
};
|
||||
use crate::storage_api_contracts::bucket::{BucketOperations as _, MakeBucketOptions};
|
||||
use rustfs_data_usage::{BucketUsageInfo, DataUsageInfo};
|
||||
use std::time::{Duration, SystemTime};
|
||||
|
||||
let (_dirs, store) = isolated_store_over_temp_disks().await;
|
||||
init_bucket_metadata_sys(store.clone(), Vec::new()).await;
|
||||
store
|
||||
.make_bucket("bucket", &MakeBucketOptions::default())
|
||||
.await
|
||||
.expect("create a real observed bucket");
|
||||
let incarnation = store
|
||||
.bucket_incarnation_id_from_disk("bucket")
|
||||
.await
|
||||
.expect("read current storage-owned incarnation");
|
||||
let primary = DataUsageInfo {
|
||||
last_update: Some(SystemTime::UNIX_EPOCH + Duration::from_secs(1)),
|
||||
scanner_epoch: Some(1),
|
||||
@@ -1066,6 +1076,7 @@ mod tests {
|
||||
versions_count: 4,
|
||||
delete_markers_count: 1,
|
||||
size: 6,
|
||||
bucket_incarnation: Some(incarnation.to_string()),
|
||||
..Default::default()
|
||||
},
|
||||
)]
|
||||
@@ -1117,6 +1128,46 @@ mod tests {
|
||||
invalidate_data_usage_snapshot_cache().await;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn identity_filtered_usage_remains_unknown_in_server_info() {
|
||||
let mut info = rustfs_data_usage::DataUsageInfo {
|
||||
last_update: Some(std::time::SystemTime::UNIX_EPOCH),
|
||||
buckets_count: 1,
|
||||
objects_total_count: 107,
|
||||
objects_total_size: 1024,
|
||||
buckets_usage: [(
|
||||
"bucket".to_string(),
|
||||
rustfs_data_usage::BucketUsageInfo {
|
||||
objects_count: 107,
|
||||
size: 1024,
|
||||
bucket_incarnation: Some("11111111-1111-4111-8111-111111111111".to_string()),
|
||||
..Default::default()
|
||||
},
|
||||
)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
usage_snapshot_complete: true,
|
||||
..Default::default()
|
||||
};
|
||||
let current =
|
||||
std::collections::HashMap::from([("bucket".to_string(), "22222222-2222-4222-8222-222222222222".to_string())]);
|
||||
assert_eq!(info.retain_current_bucket_usage_for_admin(¤t), 1);
|
||||
assert!(info.buckets_usage.is_empty());
|
||||
assert!(info.usage_snapshot_admin_projection);
|
||||
assert_eq!(info.usage_snapshot_converged, Some(false));
|
||||
let mut buckets = rustfs_madmin::Buckets::default();
|
||||
let mut objects = rustfs_madmin::Objects::default();
|
||||
let mut versions = rustfs_madmin::Versions::default();
|
||||
let mut delete_markers = rustfs_madmin::DeleteMarkers::default();
|
||||
let mut usage = rustfs_madmin::Usage::default();
|
||||
apply_data_usage_result(Ok(info), &mut buckets, &mut objects, &mut versions, &mut delete_markers, &mut usage);
|
||||
assert_eq!(buckets.error.as_deref(), Some(DATA_USAGE_UNAVAILABLE_ERROR));
|
||||
assert_eq!(objects.error.as_deref(), Some(DATA_USAGE_UNAVAILABLE_ERROR));
|
||||
assert_eq!(versions.error.as_deref(), Some(DATA_USAGE_UNAVAILABLE_ERROR));
|
||||
assert_eq!(delete_markers.error.as_deref(), Some(DATA_USAGE_UNAVAILABLE_ERROR));
|
||||
assert_eq!(usage.error.as_deref(), Some(DATA_USAGE_UNAVAILABLE_ERROR));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn incomplete_data_usage_is_unavailable_in_server_info() {
|
||||
let mut buckets = rustfs_madmin::Buckets::default();
|
||||
|
||||
@@ -29,11 +29,12 @@ workspace = true
|
||||
|
||||
[features]
|
||||
default = []
|
||||
hotpath = ["rustfs-io-metrics/hotpath"]
|
||||
hotpath-alloc = ["hotpath", "rustfs-io-metrics/hotpath-alloc"]
|
||||
hotpath-cpu = ["hotpath", "rustfs-io-metrics/hotpath-cpu"]
|
||||
hotpath = ["hotpath/hotpath", "hotpath/tokio", "rustfs-io-metrics/hotpath"]
|
||||
hotpath-alloc = ["hotpath", "hotpath/hotpath-alloc", "rustfs-io-metrics/hotpath-alloc"]
|
||||
hotpath-cpu = ["hotpath", "hotpath/hotpath-cpu", "rustfs-io-metrics/hotpath-cpu"]
|
||||
|
||||
[dependencies]
|
||||
hotpath.workspace = true
|
||||
bytes = { workspace = true, features = ["serde"] }
|
||||
thiserror = { workspace = true }
|
||||
tokio = { workspace = true, features = ["io-util", "fs", "sync", "rt-multi-thread"] }
|
||||
|
||||
@@ -6,8 +6,7 @@ Applies to `crates/kms/`.
|
||||
|
||||
When changing key-management behavior, verify compatibility with:
|
||||
|
||||
- `crates/sse/` (SSE key resolution; consumes this crate on the data path)
|
||||
- `rustfs/src/storage/sse.rs` (binds `crates/sse/` to the running server)
|
||||
- `rustfs/src/storage/sse.rs`
|
||||
- `rustfs/src/admin/handlers/kms.rs`
|
||||
- `rustfs/src/admin/handlers/kms_dynamic.rs`
|
||||
- `rustfs/src/admin/handlers/kms_keys.rs`
|
||||
|
||||
@@ -410,10 +410,7 @@ impl<S: StorageBackend + Send + Sync + 'static> SftpDriver<S> {
|
||||
}
|
||||
};
|
||||
let handle = self.allocate_handle(HandleState::Dir(cursor))?;
|
||||
Ok(Handle {
|
||||
id,
|
||||
handle: handle.into(),
|
||||
})
|
||||
Ok(Handle { id, handle })
|
||||
}
|
||||
|
||||
/// READDIR body shared with the Handler trait wrapper. Removes the
|
||||
@@ -669,11 +666,8 @@ mod tests {
|
||||
assert_eq!(auth.continuation_token, call.continuation_token);
|
||||
}
|
||||
assert_eq!(authorized[2].continuation_token.as_ref().map(|value| value.as_str()), Some("next-page"));
|
||||
let denied = with_test_auth_override(
|
||||
|_, _, prefix| prefix != Some("private/"),
|
||||
driver.readdir_inner(2, String::from_utf8(handle.handle.into_bytes()).expect("utf8 handle")),
|
||||
)
|
||||
.await;
|
||||
let denied =
|
||||
with_test_auth_override(|_, _, prefix| prefix != Some("private/"), driver.readdir_inner(2, handle.handle)).await;
|
||||
assert!(denied.is_err(), "policy change must deny the next READDIR");
|
||||
assert_eq!(backend.list_objects_calls().len(), 2, "denied READDIR cannot reach storage");
|
||||
}
|
||||
@@ -722,7 +716,7 @@ mod tests {
|
||||
};
|
||||
let handle_id = driver.allocate_handle(HandleState::Dir(cursor)).expect("allocate");
|
||||
|
||||
let readdir_fut = driver.readdir(1, handle_id.clone().into());
|
||||
let readdir_fut = driver.readdir(1, handle_id.clone());
|
||||
|
||||
with_test_auth_override(|_, _, _| true, async {
|
||||
tokio::select! {
|
||||
@@ -764,7 +758,7 @@ mod tests {
|
||||
// token, no skipped entries).
|
||||
backend.clear_stall_list_objects_v2();
|
||||
backend.queue_list_objects_v2_ok_empty();
|
||||
let result = with_test_auth_override(|_, _, _| true, driver.readdir(2, handle_id.into())).await;
|
||||
let result = with_test_auth_override(|_, _, _| true, driver.readdir(2, handle_id)).await;
|
||||
// Empty page returns Eof per readdir's empty-Name-to-Eof translation.
|
||||
let err = result.expect_err("re-issued READDIR against an empty listing must return Eof, not Ok");
|
||||
assert!(
|
||||
@@ -791,7 +785,7 @@ mod tests {
|
||||
let handle_id = driver.allocate_handle(HandleState::Dir(cursor)).expect("allocate");
|
||||
|
||||
let (result, captured) =
|
||||
capture_tracing_at(Level::ERROR, with_test_auth_override(|_, _, _| true, driver.readdir(7, handle_id.into()))).await;
|
||||
capture_tracing_at(Level::ERROR, with_test_auth_override(|_, _, _| true, driver.readdir(7, handle_id))).await;
|
||||
let err = result.expect_err("exhausted cursor must return Eof");
|
||||
assert!(matches!(StatusCode::from(err), StatusCode::Eof));
|
||||
assert!(
|
||||
@@ -824,7 +818,7 @@ mod tests {
|
||||
let handle_id = driver.allocate_handle(HandleState::Dir(cursor)).expect("allocate");
|
||||
|
||||
let (result, captured) =
|
||||
capture_tracing_at(Level::ERROR, with_test_auth_override(|_, _, _| true, driver.readdir(8, handle_id.into()))).await;
|
||||
capture_tracing_at(Level::ERROR, with_test_auth_override(|_, _, _| true, driver.readdir(8, handle_id))).await;
|
||||
let err = result.expect_err("backend error must propagate as Err");
|
||||
assert!(!matches!(StatusCode::from(err), StatusCode::Eof), "backend error must not be Eof");
|
||||
assert!(
|
||||
|
||||
@@ -33,9 +33,7 @@ use super::write::{build_write_tombstone, fstat_reported_size, rejects_excl_or_t
|
||||
use crate::common::client::s3::StorageBackend;
|
||||
use crate::common::gateway::{AuthorizationError, S3Action};
|
||||
use crate::common::session::SessionContext;
|
||||
use russh_sftp::protocol::{
|
||||
Attrs, Data, File, FileAttributes, Handle, Name, OpenFlags, Packet, SftpHandle, Status, StatusCode, Version,
|
||||
};
|
||||
use russh_sftp::protocol::{Attrs, Data, File, FileAttributes, Handle, Name, OpenFlags, Packet, Status, StatusCode, Version};
|
||||
use rustfs_credentials::Credentials;
|
||||
use rustfs_gateway_types::dto::{AbortMultipartUploadInput, CopyObjectInput, ListObjectsV2Input};
|
||||
use rustfs_gateway_types::{BucketName, ObjectKey, UploadIdClaim};
|
||||
@@ -210,12 +208,6 @@ impl<S: StorageBackend + Send + Sync + 'static> SftpDriver<S> {
|
||||
}
|
||||
}
|
||||
|
||||
/// russh-sftp 4 hands the Handler opaque `SftpHandle` bytes; the table
|
||||
/// keys remain the UTF-8 UUID strings this driver mints.
|
||||
fn handle_table_key(handle: SftpHandle) -> Result<String, SftpError> {
|
||||
String::from_utf8(handle.into_bytes()).map_err(|_| SftpError::code(StatusCode::Failure))
|
||||
}
|
||||
|
||||
/// Generate a fresh UUID v4 handle, insert the given state into the
|
||||
/// per-session handle table, and return the handle string. Enforces
|
||||
/// self.handles_per_session before any UUID generation. Cap-exceeded
|
||||
@@ -502,9 +494,8 @@ impl<S: StorageBackend + Send + Sync + 'static> russh_sftp::server::Handler for
|
||||
/// Streaming returns (next_part_number - 1) * part_size + buffer
|
||||
/// length. Failed returns the size recorded at the most recent
|
||||
/// successful write. An unknown handle returns Failure.
|
||||
#[tracing::instrument(level = "debug", skip(self), fields(id, handle = ?handle), err(Debug))]
|
||||
async fn fstat(&mut self, id: u32, handle: SftpHandle) -> Result<Attrs, Self::Error> {
|
||||
let handle = Self::handle_table_key(handle)?;
|
||||
#[tracing::instrument(level = "debug", skip(self), fields(id, handle = %handle), err(Debug))]
|
||||
async fn fstat(&mut self, id: u32, handle: String) -> Result<Attrs, Self::Error> {
|
||||
self.session_diag.stamp();
|
||||
let part_size = self.part_size;
|
||||
let result = self.with_handle_ref(&handle, |state| match state {
|
||||
@@ -550,9 +541,8 @@ impl<S: StorageBackend + Send + Sync + 'static> russh_sftp::server::Handler for
|
||||
/// exhaustion, so it is normal control flow on this handler. The
|
||||
/// instrument attribute therefore omits err(Debug) and non-Eof
|
||||
/// failures are surfaced via the explicit error log below.
|
||||
#[tracing::instrument(level = "debug", skip(self), fields(id, handle = ?handle))]
|
||||
async fn readdir(&mut self, id: u32, handle: SftpHandle) -> Result<Name, Self::Error> {
|
||||
let handle = Self::handle_table_key(handle)?;
|
||||
#[tracing::instrument(level = "debug", skip(self), fields(id, handle = %handle))]
|
||||
async fn readdir(&mut self, id: u32, handle: String) -> Result<Name, Self::Error> {
|
||||
self.session_diag.stamp();
|
||||
let result = self.readdir_inner(id, handle.clone()).await;
|
||||
self.session_diag.stamp();
|
||||
@@ -627,9 +617,8 @@ impl<S: StorageBackend + Send + Sync + 'static> russh_sftp::server::Handler for
|
||||
/// read-past-end-of-file, so it is normal control flow on this
|
||||
/// handler. The instrument attribute therefore omits err(Debug) and
|
||||
/// non-Eof failures are surfaced via the explicit error log below.
|
||||
#[tracing::instrument(level = "debug", skip(self), fields(id, handle = ?handle, offset, len))]
|
||||
async fn read(&mut self, id: u32, handle: SftpHandle, offset: u64, len: u32) -> Result<Data, Self::Error> {
|
||||
let handle = Self::handle_table_key(handle)?;
|
||||
#[tracing::instrument(level = "debug", skip(self), fields(id, handle = %handle, offset, len))]
|
||||
async fn read(&mut self, id: u32, handle: String, offset: u64, len: u32) -> Result<Data, Self::Error> {
|
||||
self.session_diag.stamp();
|
||||
let result = self.read_inner(id, handle.clone(), offset, len).await;
|
||||
self.session_diag.stamp();
|
||||
@@ -661,9 +650,8 @@ impl<S: StorageBackend + Send + Sync + 'static> russh_sftp::server::Handler for
|
||||
///
|
||||
/// A missing handle is treated as Ok to tolerate clients that
|
||||
/// double-close on session teardown.
|
||||
#[tracing::instrument(level = "info", skip(self), fields(id, handle = ?handle), err(Debug))]
|
||||
async fn close(&mut self, id: u32, handle: SftpHandle) -> Result<Status, Self::Error> {
|
||||
let handle = Self::handle_table_key(handle)?;
|
||||
#[tracing::instrument(level = "info", skip(self), fields(id, handle = %handle), err(Debug))]
|
||||
async fn close(&mut self, id: u32, handle: String) -> Result<Status, Self::Error> {
|
||||
self.session_diag.stamp();
|
||||
let removed = self.handles.remove(&handle);
|
||||
let Some(HandleState::Write {
|
||||
@@ -767,9 +755,8 @@ impl<S: StorageBackend + Send + Sync + 'static> russh_sftp::server::Handler for
|
||||
/// to Streaming by issuing CreateMultipartUpload. A Failed handle
|
||||
/// rejects every subsequent write with the same status that caused
|
||||
/// the failure.
|
||||
#[tracing::instrument(level = "debug", skip(self, data), fields(id, handle = ?handle, offset, len = data.len()), err(Debug))]
|
||||
async fn write(&mut self, id: u32, handle: SftpHandle, offset: u64, data: Vec<u8>) -> Result<Status, Self::Error> {
|
||||
let handle = Self::handle_table_key(handle)?;
|
||||
#[tracing::instrument(level = "debug", skip(self, data), fields(id, handle = %handle, offset, len = data.len()), err(Debug))]
|
||||
async fn write(&mut self, id: u32, handle: String, offset: u64, data: Vec<u8>) -> Result<Status, Self::Error> {
|
||||
self.session_diag.stamp();
|
||||
self.enforce_server_readonly()?;
|
||||
|
||||
@@ -996,8 +983,8 @@ impl<S: StorageBackend + Send + Sync + 'static> russh_sftp::server::Handler for
|
||||
/// SSH_FXP_FSETSTAT, SFTP Internet Draft section 6.6. Same rationale
|
||||
/// as setstat: S3 cannot honour POSIX attributes, and clients use
|
||||
/// FSETSTAT during transfers to stamp the in-flight handle.
|
||||
#[tracing::instrument(level = "debug", skip(self, _attrs), fields(id, handle = ?_handle), err(Debug))]
|
||||
async fn fsetstat(&mut self, id: u32, _handle: SftpHandle, _attrs: FileAttributes) -> Result<Status, Self::Error> {
|
||||
#[tracing::instrument(level = "debug", skip(self, _attrs), fields(id, handle = %_handle), err(Debug))]
|
||||
async fn fsetstat(&mut self, id: u32, _handle: String, _attrs: FileAttributes) -> Result<Status, Self::Error> {
|
||||
self.enforce_server_readonly()?;
|
||||
Ok(ok_status(id))
|
||||
}
|
||||
@@ -1267,7 +1254,7 @@ mod tests {
|
||||
let handle_id = driver
|
||||
.allocate_handle(file_handle("bucket", "k", 1234, attrs.clone()))
|
||||
.expect("allocate");
|
||||
let out = driver.fstat(4, handle_id.into()).await.expect("fstat on File must succeed");
|
||||
let out = driver.fstat(4, handle_id).await.expect("fstat on File must succeed");
|
||||
assert_eq!(out.attrs.size, Some(1234));
|
||||
assert_eq!(out.attrs.mtime, Some(1_700_000_000));
|
||||
}
|
||||
@@ -1280,7 +1267,7 @@ mod tests {
|
||||
part_buffer: vec![0u8; 4096],
|
||||
};
|
||||
let handle_id = driver.allocate_handle(write_handle("bucket", "k", phase)).expect("allocate");
|
||||
let out = driver.fstat(5, handle_id.into()).await.expect("fstat on Write must succeed");
|
||||
let out = driver.fstat(5, handle_id).await.expect("fstat on Write must succeed");
|
||||
assert_eq!(
|
||||
out.attrs.size,
|
||||
Some(4096),
|
||||
@@ -1296,7 +1283,7 @@ mod tests {
|
||||
.allocate_handle(file_handle("bucket", "k", 0, FileAttributes::default()))
|
||||
.expect("allocate");
|
||||
let status = driver
|
||||
.fsetstat(6, handle_id.into(), FileAttributes::default())
|
||||
.fsetstat(6, handle_id, FileAttributes::default())
|
||||
.await
|
||||
.expect("fsetstat must succeed on any attrs");
|
||||
assert!(matches!(status.status_code, StatusCode::Ok));
|
||||
@@ -1430,7 +1417,7 @@ mod tests {
|
||||
let handle_id = driver
|
||||
.allocate_handle(file_handle("bucket", "k", 0, FileAttributes::default()))
|
||||
.expect("allocate");
|
||||
let result = driver.fsetstat(10, handle_id.into(), FileAttributes::default()).await;
|
||||
let result = driver.fsetstat(10, handle_id, FileAttributes::default()).await;
|
||||
match result {
|
||||
Err(err) => assert!(matches!(err.0, StatusCode::PermissionDenied)),
|
||||
Ok(_) => panic!("fsetstat must error in read-only mode"),
|
||||
|
||||
@@ -69,10 +69,7 @@ impl<S: StorageBackend + Send + Sync + 'static> SftpDriver<S> {
|
||||
attrs,
|
||||
read_cache,
|
||||
})?;
|
||||
Ok(Handle {
|
||||
id,
|
||||
handle: handle.into(),
|
||||
})
|
||||
Ok(Handle { id, handle })
|
||||
}
|
||||
|
||||
/// Body of the SSH_FXP_READ handler. Returns up to len bytes starting
|
||||
@@ -259,7 +256,7 @@ mod tests {
|
||||
.allocate_handle(file_handle("bucket", "secret.txt", 4096, FileAttributes::default()))
|
||||
.expect("allocate read handle");
|
||||
driver.try_populate_read_cache(&handle, 0, vec![42; 4096]);
|
||||
let error = with_test_auth_override(|_, _, _| true, driver.read(1, handle.into(), 0, 1024))
|
||||
let error = with_test_auth_override(|_, _, _| true, driver.read(1, handle, 0, 1024))
|
||||
.await
|
||||
.expect_err("backend policy denial must prevent cached content disclosure");
|
||||
assert!(matches!(StatusCode::from(error), StatusCode::PermissionDenied));
|
||||
@@ -273,7 +270,7 @@ mod tests {
|
||||
.allocate_handle(file_handle("bucket", "k", 100, FileAttributes::default()))
|
||||
.expect("allocate");
|
||||
let err = driver
|
||||
.read(1, handle_id.into(), 0, 0)
|
||||
.read(1, handle_id, 0, 0)
|
||||
.await
|
||||
.expect_err("len=0 must return BadMessage");
|
||||
assert!(matches!(StatusCode::from(err), StatusCode::BadMessage));
|
||||
@@ -287,7 +284,7 @@ mod tests {
|
||||
.allocate_handle(file_handle("bucket", "k", 10, FileAttributes::default()))
|
||||
.expect("allocate");
|
||||
let err = driver
|
||||
.read(2, handle_id.into(), 10, 4)
|
||||
.read(2, handle_id, 10, 4)
|
||||
.await
|
||||
.expect_err("offset==size must return Eof");
|
||||
assert!(matches!(StatusCode::from(err), StatusCode::Eof));
|
||||
@@ -302,7 +299,7 @@ mod tests {
|
||||
.allocate_handle(file_handle("bucket", "k", 5, FileAttributes::default()))
|
||||
.expect("allocate");
|
||||
|
||||
let data = with_test_auth_override(|_, _, _| true, driver.read(3, handle_id.into(), 0, 1024))
|
||||
let data = with_test_auth_override(|_, _, _| true, driver.read(3, handle_id, 0, 1024))
|
||||
.await
|
||||
.expect("read must succeed");
|
||||
assert_eq!(data.data, b"hello".to_vec());
|
||||
@@ -320,7 +317,7 @@ mod tests {
|
||||
.allocate_handle(file_handle("bucket", "k", 10, FileAttributes::default()))
|
||||
.expect("allocate");
|
||||
|
||||
let (result, captured) = capture_tracing_at(Level::ERROR, async { driver.read(11, handle_id.into(), 10, 4).await }).await;
|
||||
let (result, captured) = capture_tracing_at(Level::ERROR, async { driver.read(11, handle_id, 10, 4).await }).await;
|
||||
let err = result.expect_err("offset==size must return Eof");
|
||||
assert!(matches!(StatusCode::from(err), StatusCode::Eof));
|
||||
assert!(
|
||||
@@ -345,11 +342,8 @@ mod tests {
|
||||
.allocate_handle(file_handle("bucket", "k", 1024, FileAttributes::default()))
|
||||
.expect("allocate");
|
||||
|
||||
let (result, captured) = capture_tracing_at(
|
||||
Level::ERROR,
|
||||
with_test_auth_override(|_, _, _| true, driver.read(12, handle_id.into(), 0, 256)),
|
||||
)
|
||||
.await;
|
||||
let (result, captured) =
|
||||
capture_tracing_at(Level::ERROR, with_test_auth_override(|_, _, _| true, driver.read(12, handle_id, 0, 256))).await;
|
||||
let err = result.expect_err("backend error must propagate as Err");
|
||||
assert!(!matches!(StatusCode::from(err), StatusCode::Eof), "backend error must not be Eof");
|
||||
assert!(
|
||||
@@ -385,7 +379,7 @@ mod tests {
|
||||
let start = Instant::now();
|
||||
let outcome = tokio::time::timeout(
|
||||
Duration::from_secs(10),
|
||||
with_test_auth_override(|_, _, _| true, driver.read(14, handle_id.into(), 0, 4096)),
|
||||
with_test_auth_override(|_, _, _| true, driver.read(14, handle_id, 0, 4096)),
|
||||
)
|
||||
.await;
|
||||
let elapsed = start.elapsed();
|
||||
@@ -431,7 +425,7 @@ mod tests {
|
||||
let mut assembled: Vec<u8> = Vec::with_capacity(object_size as usize);
|
||||
let mut reads: u32 = 0;
|
||||
while offset < object_size {
|
||||
let data = with_test_auth_override(|_, _, _| true, driver.read(20 + reads, handle_id.clone().into(), offset, chunk))
|
||||
let data = with_test_auth_override(|_, _, _| true, driver.read(20 + reads, handle_id.clone(), offset, chunk))
|
||||
.await
|
||||
.expect("read inside the cached window must succeed without a second backend call");
|
||||
assert!(!data.data.is_empty(), "non-empty hit");
|
||||
@@ -467,21 +461,21 @@ mod tests {
|
||||
.expect("allocate");
|
||||
|
||||
// First read fetches from the backend and populates window 1.
|
||||
let r1 = with_test_auth_override(|_, _, _| true, driver.read(30, handle_id.clone().into(), 0, 1024))
|
||||
let r1 = with_test_auth_override(|_, _, _| true, driver.read(30, handle_id.clone(), 0, 1024))
|
||||
.await
|
||||
.expect("first read must succeed");
|
||||
assert!(r1.data.iter().all(|b| *b == 0xAA), "first read must come from window 1");
|
||||
|
||||
// Second read inside the cached chunk is served from the
|
||||
// buffer. No second backend call yet.
|
||||
let r2 = with_test_auth_override(|_, _, _| true, driver.read(31, handle_id.clone().into(), 1024, 1024))
|
||||
let r2 = with_test_auth_override(|_, _, _| true, driver.read(31, handle_id.clone(), 1024, 1024))
|
||||
.await
|
||||
.expect("mid-window read must succeed from cache");
|
||||
assert!(r2.data.iter().all(|b| *b == 0xAA), "mid-window read still in window 1");
|
||||
|
||||
// Reading at offset == window falls outside the cached chunk
|
||||
// and triggers the second backend call.
|
||||
let r3 = with_test_auth_override(|_, _, _| true, driver.read(32, handle_id.clone().into(), window, 1024))
|
||||
let r3 = with_test_auth_override(|_, _, _| true, driver.read(32, handle_id.clone(), window, 1024))
|
||||
.await
|
||||
.expect("read at offset=window must succeed via second backend call");
|
||||
assert!(r3.data.iter().all(|b| *b == 0xBB), "read at window boundary must come from window 2");
|
||||
@@ -489,7 +483,7 @@ mod tests {
|
||||
// A read inside the second cached chunk is served from the
|
||||
// buffer. The queue is empty by now, so any third backend
|
||||
// call would fail.
|
||||
let r4 = with_test_auth_override(|_, _, _| true, driver.read(33, handle_id.into(), window + 1024, 1024))
|
||||
let r4 = with_test_auth_override(|_, _, _| true, driver.read(33, handle_id, window + 1024, 1024))
|
||||
.await
|
||||
.expect("mid-window-2 read must succeed from cache");
|
||||
assert!(r4.data.iter().all(|b| *b == 0xBB), "mid-window-2 read still in window 2");
|
||||
@@ -517,13 +511,13 @@ mod tests {
|
||||
.expect("allocate");
|
||||
|
||||
// Populate window 1 with a full read.
|
||||
let _ = with_test_auth_override(|_, _, _| true, driver.read(40, handle_id.clone().into(), 0, window as u32))
|
||||
let _ = with_test_auth_override(|_, _, _| true, driver.read(40, handle_id.clone(), 0, window as u32))
|
||||
.await
|
||||
.expect("populate window 1");
|
||||
|
||||
// Ask for 256 bytes starting 64 bytes before window end. Only
|
||||
// 64 bytes are in the window. The driver must return 64.
|
||||
let edge = with_test_auth_override(|_, _, _| true, driver.read(41, handle_id.into(), window - 64, 256))
|
||||
let edge = with_test_auth_override(|_, _, _| true, driver.read(41, handle_id, window - 64, 256))
|
||||
.await
|
||||
.expect("partial-hit read must succeed");
|
||||
assert_eq!(edge.data.len(), 64, "partial hit must return only the in-window portion");
|
||||
@@ -558,10 +552,9 @@ mod tests {
|
||||
|
||||
for i in 0..read_count {
|
||||
let offset = (chunk_size as u64) * (i as u64);
|
||||
let data =
|
||||
with_test_auth_override(|_, _, _| true, driver.read(50 + i, handle_id.clone().into(), offset, chunk_size as u32))
|
||||
.await
|
||||
.expect("each read must succeed via the backend");
|
||||
let data = with_test_auth_override(|_, _, _| true, driver.read(50 + i, handle_id.clone(), offset, chunk_size as u32))
|
||||
.await
|
||||
.expect("each read must succeed via the backend");
|
||||
assert_eq!(data.data.len(), chunk_size, "read must return full requested length");
|
||||
let expected_byte = (i + 1) as u8;
|
||||
assert!(
|
||||
|
||||
@@ -336,10 +336,7 @@ impl<S: StorageBackend + Send + Sync + 'static> SftpDriver<S> {
|
||||
open_attrs,
|
||||
phase: WritePhase::Buffering { part_buffer: Vec::new() },
|
||||
})?;
|
||||
Ok(Handle {
|
||||
id,
|
||||
handle: handle.into(),
|
||||
})
|
||||
Ok(Handle { id, handle })
|
||||
}
|
||||
|
||||
/// Upload the buffered bytes to S3 with a single PutObject call.
|
||||
@@ -2108,7 +2105,7 @@ mod tests {
|
||||
)
|
||||
.await
|
||||
.expect("WRITE | CREATE | TRUNCATE on a missing file must allocate a handle");
|
||||
assert!(!handle.handle.as_bytes().is_empty());
|
||||
assert!(!handle.handle.is_empty());
|
||||
assert!(backend.head_object_calls().is_empty(), "the non-EXCL accept path must not HEAD");
|
||||
}
|
||||
|
||||
@@ -2133,7 +2130,7 @@ mod tests {
|
||||
)
|
||||
.await
|
||||
.expect("WRITE | CREATE | TRUNCATE on an existing file must allocate a handle");
|
||||
assert!(!handle.handle.as_bytes().is_empty());
|
||||
assert!(!handle.handle.is_empty());
|
||||
assert!(backend.head_object_calls().is_empty(), "the non-EXCL accept path must not HEAD");
|
||||
}
|
||||
|
||||
@@ -2180,7 +2177,7 @@ mod tests {
|
||||
)
|
||||
.await
|
||||
.expect("EXCL on a missing key must allow creation");
|
||||
assert!(!handle.handle.as_bytes().is_empty());
|
||||
assert!(!handle.handle.is_empty());
|
||||
assert_eq!(backend.head_object_calls().len(), 1, "EXCL must HEAD exactly once");
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,8 @@ default = []
|
||||
# event. Requires `RUSTFLAGS="--cfg hyper_unstable_tracing"`.
|
||||
diagnostic-internode-http2-ping = ["dep:hyper", "rustfs-io-metrics/diagnostic-internode-http2-ping"]
|
||||
hotpath = [
|
||||
"hotpath/hotpath",
|
||||
"hotpath/tokio",
|
||||
"rustfs-common/hotpath",
|
||||
"rustfs-config/hotpath",
|
||||
"rustfs-io-metrics/hotpath",
|
||||
@@ -46,6 +48,7 @@ hotpath = [
|
||||
]
|
||||
hotpath-alloc = [
|
||||
"hotpath",
|
||||
"hotpath/hotpath-alloc",
|
||||
"rustfs-common/hotpath-alloc",
|
||||
"rustfs-config/hotpath-alloc",
|
||||
"rustfs-io-metrics/hotpath-alloc",
|
||||
@@ -54,6 +57,7 @@ hotpath-alloc = [
|
||||
]
|
||||
hotpath-cpu = [
|
||||
"hotpath",
|
||||
"hotpath/hotpath-cpu",
|
||||
"rustfs-common/hotpath-cpu",
|
||||
"rustfs-config/hotpath-cpu",
|
||||
"rustfs-io-metrics/hotpath-cpu",
|
||||
@@ -62,6 +66,7 @@ hotpath-cpu = [
|
||||
]
|
||||
|
||||
[dependencies]
|
||||
hotpath.workspace = true
|
||||
rustfs-common.workspace = true
|
||||
rustfs-heal-contracts.workspace = true
|
||||
rustfs-io-metrics.workspace = true
|
||||
|
||||
@@ -31,24 +31,29 @@ workspace = true
|
||||
[features]
|
||||
default = []
|
||||
hotpath = [
|
||||
"hotpath/hotpath",
|
||||
"hotpath/tokio",
|
||||
"rustfs-rio/hotpath",
|
||||
"rustfs-utils/hotpath",
|
||||
"rustfs-filemeta/hotpath",
|
||||
]
|
||||
hotpath-alloc = [
|
||||
"hotpath",
|
||||
"hotpath/hotpath-alloc",
|
||||
"rustfs-rio/hotpath-alloc",
|
||||
"rustfs-utils/hotpath-alloc",
|
||||
"rustfs-filemeta/hotpath-alloc",
|
||||
]
|
||||
hotpath-cpu = [
|
||||
"hotpath",
|
||||
"hotpath/hotpath-cpu",
|
||||
"rustfs-rio/hotpath-cpu",
|
||||
"rustfs-utils/hotpath-cpu",
|
||||
"rustfs-filemeta/hotpath-cpu",
|
||||
]
|
||||
|
||||
[dependencies]
|
||||
hotpath.workspace = true
|
||||
aes-gcm = { workspace = true, features = ["rand_core"] }
|
||||
bytes = { workspace = true, features = ["serde"] }
|
||||
chacha20poly1305.workspace = true
|
||||
|
||||
@@ -32,7 +32,6 @@ workspace = true
|
||||
internode-transport = []
|
||||
default = []
|
||||
hotpath = [
|
||||
"dep:hotpath",
|
||||
"hotpath/hotpath",
|
||||
"hotpath/tokio",
|
||||
"hotpath/futures",
|
||||
@@ -60,8 +59,7 @@ hotpath-cpu = [
|
||||
]
|
||||
|
||||
[dependencies]
|
||||
# Feature-gated in src (measure_block!); optional so default builds do not warn.
|
||||
hotpath = { workspace = true, optional = true }
|
||||
hotpath.workspace = true
|
||||
arc-swap.workspace = true
|
||||
tokio = { workspace = true, features = ["io-util", "macros", "net", "rt-multi-thread", "sync", "time"] }
|
||||
rand = { workspace = true, features = ["serde"] }
|
||||
@@ -81,6 +79,7 @@ rustfs-io-metrics.workspace = true
|
||||
rustfs-tls-runtime.workspace = true
|
||||
rustfs-utils = { workspace = true, features = ["io", "hash", "compress"] }
|
||||
serde_json = { workspace = true, features = ["raw_value"] }
|
||||
md-5 = { workspace = true }
|
||||
hmac.workspace = true
|
||||
minlz.workspace = true
|
||||
tracing.workspace = true
|
||||
@@ -92,7 +91,6 @@ xxhash-rust = { workspace = true, features = ["xxh64", "xxh3"] }
|
||||
hex-simd.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
md-5 = { workspace = true }
|
||||
faster-hex.workspace = true
|
||||
rcgen.workspace = true
|
||||
tokio-rustls.workspace = true
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
pub mod init_time;
|
||||
pub mod last_minute;
|
||||
pub mod metrics;
|
||||
pub mod scan_observation;
|
||||
pub mod startup;
|
||||
pub mod usage_candidate;
|
||||
|
||||
|
||||
@@ -13,6 +13,9 @@
|
||||
// limitations under the License.
|
||||
|
||||
use crate::last_minute::{AccElem, LastMinuteLatency};
|
||||
use crate::scan_observation::{
|
||||
ScannerMetadataReadGuard, ScannerMetadataReadOutcome, ScannerScanObservation, ScannerScanObservationSnapshot,
|
||||
};
|
||||
use crate::usage_candidate::{
|
||||
ScannerUsageCandidateCount, ScannerUsageCandidateKind, ScannerUsageCandidateOutcome, USAGE_CANDIDATE_COUNTER_SLOTS,
|
||||
};
|
||||
@@ -977,6 +980,7 @@ impl ScannerUsagePublicationObservation {
|
||||
|
||||
pub struct Metrics {
|
||||
startup: crate::startup::ScannerStartupObservation,
|
||||
scan_observation: ScannerScanObservation,
|
||||
operations: Vec<AtomicU64>,
|
||||
latency: Vec<LockedLastMinuteLatency>,
|
||||
actions: Vec<AtomicU64>,
|
||||
@@ -1631,6 +1635,9 @@ pub struct ScannerMetricsReport {
|
||||
pub lifecycle_transition: ScannerLifecycleTransitionSnapshot,
|
||||
#[serde(default)]
|
||||
pub usage_freshness: ScannerUsageFreshnessSnapshot,
|
||||
/// Node-local, process-lifetime work observations. Older nodes have no evidence.
|
||||
#[serde(default)]
|
||||
pub scan_observation: ScannerScanObservationSnapshot,
|
||||
/// Process-lifetime candidate-construction results. Missing on older reports
|
||||
/// means unobserved, not accepted or healthy. Publication is tracked separately.
|
||||
#[serde(default)]
|
||||
@@ -2125,9 +2132,38 @@ impl Metrics {
|
||||
&self.startup
|
||||
}
|
||||
|
||||
pub fn scan_observation(&self) -> &ScannerScanObservation {
|
||||
&self.scan_observation
|
||||
}
|
||||
|
||||
/// Start after scanner node admission, before polling the disk read future.
|
||||
/// Disk-permit and RPC waits remain included; decoding is excluded. Dropping
|
||||
/// an unfinished guard observes cancellation, not physical I/O completion.
|
||||
pub fn start_scanner_metadata_read(&self) -> ScannerMetadataReadGuard<'_> {
|
||||
self.scan_observation.start_metadata_read();
|
||||
ScannerMetadataReadGuard {
|
||||
metrics: self,
|
||||
started: std::time::Instant::now(),
|
||||
outcome: ScannerMetadataReadOutcome::Cancelled,
|
||||
bytes: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn finish_scanner_metadata_read(&self, outcome: ScannerMetadataReadOutcome, bytes: u64, elapsed: Duration) {
|
||||
let bytes = if outcome == ScannerMetadataReadOutcome::Success {
|
||||
bytes
|
||||
} else {
|
||||
0
|
||||
};
|
||||
self.scan_observation.finish_metadata_read(outcome, bytes, elapsed);
|
||||
self.operations[Metric::ReadMetadata as usize].fetch_add(1, Ordering::Relaxed);
|
||||
self.latency[Metric::ReadMetadata as usize].add_size(elapsed, bytes);
|
||||
}
|
||||
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
startup: crate::startup::ScannerStartupObservation::default(),
|
||||
scan_observation: ScannerScanObservation::default(),
|
||||
operations: (0..Metric::Last as usize).map(|_| AtomicU64::new(0)).collect(),
|
||||
// Each slot gets its own fresh LockedLastMinuteLatency so that
|
||||
// different metrics never accidentally share state.
|
||||
@@ -2733,6 +2769,8 @@ impl Metrics {
|
||||
/// walked the whole namespace, so an operator can tell why scoped
|
||||
/// convergence is not happening.
|
||||
pub fn record_scanner_scope_fallback(&self, reason: impl Into<String>) {
|
||||
let reason = reason.into();
|
||||
self.scan_observation.record_scope_fallback(&reason);
|
||||
self.scanner_scope_fallback_total.fetch_add(1, Ordering::Relaxed);
|
||||
self.scanner_scope_fallback_last_unix_secs
|
||||
.store(unix_now_secs(), Ordering::Relaxed);
|
||||
@@ -2740,7 +2778,7 @@ impl Metrics {
|
||||
Ok(guard) => guard,
|
||||
Err(poisoned) => poisoned.into_inner(),
|
||||
};
|
||||
*last_reason = reason.into();
|
||||
*last_reason = reason;
|
||||
}
|
||||
|
||||
pub fn record_scanner_usage_durable_success(&self) {
|
||||
@@ -3875,6 +3913,7 @@ impl Metrics {
|
||||
failed: self.scanner_transition_failed.load(Ordering::Relaxed),
|
||||
};
|
||||
m.usage_candidates = self.usage_candidate_counts();
|
||||
m.scan_observation = self.scan_observation.snapshot();
|
||||
let usage_save_result = ScannerUsageSaveResult::from_code(self.scanner_usage_last_save_result.load(Ordering::Relaxed));
|
||||
m.usage_freshness = ScannerUsageFreshnessSnapshot {
|
||||
dirty_pending_buckets: self.scanner_dirty_usage_pending_buckets.load(Ordering::Relaxed),
|
||||
|
||||
@@ -0,0 +1,675 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Node-local, process-lifetime observations of scanner work. Counters are
|
||||
//! independently loaded, so concurrent reports need not balance exactly.
|
||||
//! These observations never grant cache reuse or publication authority.
|
||||
|
||||
use crate::metrics::Metrics;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
sync::atomic::{AtomicU64, Ordering},
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
pub const PREFIX_REUSE_BLOCKERS: [&str; 8] = [
|
||||
"missing_producer_identity",
|
||||
"missing_durable_journal_replay",
|
||||
"restart_gap",
|
||||
"generation_gap",
|
||||
"overflow",
|
||||
"stale_ack_generation",
|
||||
"missing_cold_zero_walk_oracle",
|
||||
"distributed_without_peer_invalidation",
|
||||
];
|
||||
|
||||
const SCOPE_FALLBACK_REASONS: [&str; 6] = [
|
||||
"dirty_snapshot_incomplete",
|
||||
"dirty_generation_exhausted",
|
||||
"dirty_bucket_overflow",
|
||||
"no_listed_dirty_buckets",
|
||||
"baseline_unavailable",
|
||||
"peer_scope_unavailable",
|
||||
];
|
||||
|
||||
/// A storage read result, independent of later metadata decoding or ILM work.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum ScannerMetadataReadOutcome {
|
||||
Success,
|
||||
NotFound,
|
||||
Error,
|
||||
Cancelled,
|
||||
}
|
||||
|
||||
/// The existing path that actually copied a cached subtree instead of walking it.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum ScannerCacheReuseKind {
|
||||
CleanPrefix,
|
||||
CoveredNamespace,
|
||||
SampledNamespace,
|
||||
ClassifiedNamespace,
|
||||
}
|
||||
|
||||
impl ScannerCacheReuseKind {
|
||||
const ALL: [Self; 4] = [
|
||||
Self::CleanPrefix,
|
||||
Self::CoveredNamespace,
|
||||
Self::SampledNamespace,
|
||||
Self::ClassifiedNamespace,
|
||||
];
|
||||
|
||||
fn label(self) -> &'static str {
|
||||
match self {
|
||||
Self::CleanPrefix => "clean_prefix",
|
||||
Self::CoveredNamespace => "covered_namespace",
|
||||
Self::SampledNamespace => "sampled_namespace",
|
||||
Self::ClassifiedNamespace => "classified_namespace",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Completed bucket-level reuse work, independent of durable publication.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum ScannerBucketReuseKind {
|
||||
ScopedColdBucket,
|
||||
CurrentRoot,
|
||||
}
|
||||
|
||||
impl ScannerBucketReuseKind {
|
||||
const ALL: [Self; 2] = [Self::ScopedColdBucket, Self::CurrentRoot];
|
||||
|
||||
fn label(self) -> &'static str {
|
||||
match self {
|
||||
Self::ScopedColdBucket => "scoped_cold_bucket",
|
||||
Self::CurrentRoot => "current_root",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A production folder gate, not a diagnostic preflight.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum ScannerCacheReuseGate {
|
||||
CleanPrefix,
|
||||
CoveredNamespace,
|
||||
NamespaceSampling,
|
||||
}
|
||||
|
||||
impl ScannerCacheReuseGate {
|
||||
const ALL: [Self; 3] = [Self::CleanPrefix, Self::CoveredNamespace, Self::NamespaceSampling];
|
||||
|
||||
fn label(self) -> &'static str {
|
||||
match self {
|
||||
Self::CleanPrefix => "clean_prefix",
|
||||
Self::CoveredNamespace => "covered_namespace",
|
||||
Self::NamespaceSampling => "namespace_sampling",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The first existing condition reached by a folder reuse gate.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum ScannerCacheReuseDecision {
|
||||
MissingScope,
|
||||
NoResumeSweep,
|
||||
DeepScan,
|
||||
ForwardSweep,
|
||||
ActiveReplication,
|
||||
NotRoot,
|
||||
CompactedParent,
|
||||
MissingCachedChild,
|
||||
InvalidPrefix,
|
||||
DirtyPrefix,
|
||||
ErasureHealthRequired,
|
||||
MissingCoverage,
|
||||
CachedFailures,
|
||||
UncompactedChild,
|
||||
SelectedForWalk,
|
||||
Eligible,
|
||||
}
|
||||
|
||||
impl ScannerCacheReuseDecision {
|
||||
const ALL: [Self; 16] = [
|
||||
Self::MissingScope,
|
||||
Self::NoResumeSweep,
|
||||
Self::DeepScan,
|
||||
Self::ForwardSweep,
|
||||
Self::ActiveReplication,
|
||||
Self::NotRoot,
|
||||
Self::CompactedParent,
|
||||
Self::MissingCachedChild,
|
||||
Self::InvalidPrefix,
|
||||
Self::DirtyPrefix,
|
||||
Self::ErasureHealthRequired,
|
||||
Self::MissingCoverage,
|
||||
Self::CachedFailures,
|
||||
Self::UncompactedChild,
|
||||
Self::SelectedForWalk,
|
||||
Self::Eligible,
|
||||
];
|
||||
|
||||
fn label(self) -> &'static str {
|
||||
match self {
|
||||
Self::MissingScope => "missing_scope",
|
||||
Self::NoResumeSweep => "no_resume_sweep",
|
||||
Self::DeepScan => "deep_scan",
|
||||
Self::ForwardSweep => "forward_sweep",
|
||||
Self::ActiveReplication => "active_replication",
|
||||
Self::NotRoot => "not_root",
|
||||
Self::CompactedParent => "compacted_parent",
|
||||
Self::MissingCachedChild => "missing_cached_child",
|
||||
Self::InvalidPrefix => "invalid_prefix",
|
||||
Self::DirtyPrefix => "dirty_prefix",
|
||||
Self::ErasureHealthRequired => "erasure_health_required",
|
||||
Self::MissingCoverage => "missing_coverage",
|
||||
Self::CachedFailures => "cached_failures",
|
||||
Self::UncompactedChild => "uncompacted_child",
|
||||
Self::SelectedForWalk => "selected_for_walk",
|
||||
Self::Eligible => "eligible",
|
||||
}
|
||||
}
|
||||
|
||||
fn not_evaluated(self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Self::MissingScope | Self::NoResumeSweep | Self::DeepScan | Self::ForwardSweep | Self::ActiveReplication
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Scope admission and end-of-cycle evidence are separate observations. An
|
||||
/// activated completion proof does not mean this cycle used prefix reuse.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum ScannerPrefixReuseStage {
|
||||
ScopeResolution,
|
||||
CycleCompletion,
|
||||
}
|
||||
|
||||
/// The first existing baseline validation check that rejected a snapshot.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum ScannerBaselineRejection {
|
||||
Missing,
|
||||
Decode,
|
||||
Incomplete,
|
||||
Partial,
|
||||
Marks,
|
||||
Epoch,
|
||||
SourceCount,
|
||||
PlanMissing,
|
||||
PlanMismatch,
|
||||
SourceIndex,
|
||||
SourceMembership,
|
||||
SourceDuplicate,
|
||||
SetIncomplete,
|
||||
SetTombstone,
|
||||
SetEpoch,
|
||||
SetCycle,
|
||||
SetPlan,
|
||||
SourceCoverage,
|
||||
AuthoritativeIdentity,
|
||||
NotNewer,
|
||||
}
|
||||
|
||||
impl ScannerBaselineRejection {
|
||||
const ALL: [Self; 20] = [
|
||||
Self::Missing,
|
||||
Self::Decode,
|
||||
Self::Incomplete,
|
||||
Self::Partial,
|
||||
Self::Marks,
|
||||
Self::Epoch,
|
||||
Self::SourceCount,
|
||||
Self::PlanMissing,
|
||||
Self::PlanMismatch,
|
||||
Self::SourceIndex,
|
||||
Self::SourceMembership,
|
||||
Self::SourceDuplicate,
|
||||
Self::SetIncomplete,
|
||||
Self::SetTombstone,
|
||||
Self::SetEpoch,
|
||||
Self::SetCycle,
|
||||
Self::SetPlan,
|
||||
Self::SourceCoverage,
|
||||
Self::AuthoritativeIdentity,
|
||||
Self::NotNewer,
|
||||
];
|
||||
|
||||
fn label(self) -> &'static str {
|
||||
match self {
|
||||
Self::Missing => "missing",
|
||||
Self::Decode => "decode",
|
||||
Self::Incomplete => "incomplete",
|
||||
Self::Partial => "partial",
|
||||
Self::Marks => "marks",
|
||||
Self::Epoch => "epoch",
|
||||
Self::SourceCount => "source_count",
|
||||
Self::PlanMissing => "plan_missing",
|
||||
Self::PlanMismatch => "plan_mismatch",
|
||||
Self::SourceIndex => "source_index",
|
||||
Self::SourceMembership => "source_membership",
|
||||
Self::SourceDuplicate => "source_duplicate",
|
||||
Self::SetIncomplete => "set_incomplete",
|
||||
Self::SetTombstone => "set_tombstone",
|
||||
Self::SetEpoch => "set_epoch",
|
||||
Self::SetCycle => "set_cycle",
|
||||
Self::SetPlan => "set_plan",
|
||||
Self::SourceCoverage => "source_coverage",
|
||||
Self::AuthoritativeIdentity => "authoritative_identity",
|
||||
Self::NotNewer => "not_newer",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
|
||||
#[serde(default)]
|
||||
pub struct ScannerMetadataReadSnapshot {
|
||||
pub attempts: u64,
|
||||
pub succeeded: u64,
|
||||
pub not_found: u64,
|
||||
pub errors: u64,
|
||||
pub cancelled: u64,
|
||||
/// Bytes returned by successful storage reads, including corrupt metadata.
|
||||
pub bytes: u64,
|
||||
/// Elapsed read-future time, including failed and cancelled reads.
|
||||
pub elapsed_nanos: u64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
|
||||
#[serde(default)]
|
||||
pub struct ScannerCacheReuseSnapshot {
|
||||
/// Completed subtree reuse decisions; not cache entries or objects skipped.
|
||||
pub count: u64,
|
||||
/// Time spent copying and attaching the reused subtree, excluding listing.
|
||||
pub elapsed_nanos: u64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
|
||||
#[serde(default)]
|
||||
pub struct ScannerBucketReuseSnapshot {
|
||||
/// Cold bucket copies or current roots successfully handed to the collector.
|
||||
pub count: u64,
|
||||
/// Cold copy/attachment time or current-root channel-send time; excludes validation.
|
||||
pub elapsed_nanos: u64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
|
||||
#[serde(default)]
|
||||
pub struct ScannerCacheReuseDecisionSnapshot {
|
||||
/// Policy prerequisites that stopped the gate before cache proof checks.
|
||||
pub not_evaluated: BTreeMap<String, u64>,
|
||||
/// First failed cache/work condition; later conditions are not evaluated.
|
||||
pub blocked: BTreeMap<String, u64>,
|
||||
/// Admitted candidates, which may still need EC classification before copying.
|
||||
pub eligible: u64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
|
||||
#[serde(default)]
|
||||
pub struct ScannerPrefixReuseSnapshot {
|
||||
pub evaluated: u64,
|
||||
pub activated: u64,
|
||||
/// All failed checks are counted; one evaluation can have several blockers.
|
||||
pub blockers: BTreeMap<String, u64>,
|
||||
}
|
||||
|
||||
/// Missing on an older node means unobserved, not zero work or healthy reuse.
|
||||
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
|
||||
#[serde(default)]
|
||||
pub struct ScannerScanObservationSnapshot {
|
||||
pub metadata_reads: ScannerMetadataReadSnapshot,
|
||||
pub cache_reuse: BTreeMap<String, ScannerCacheReuseSnapshot>,
|
||||
pub bucket_reuse: BTreeMap<String, ScannerBucketReuseSnapshot>,
|
||||
pub reuse_decisions: BTreeMap<String, ScannerCacheReuseDecisionSnapshot>,
|
||||
/// Clean root children still walked because EC scans also check health.
|
||||
pub erasure_clean_prefix_walks: u64,
|
||||
pub prefix_scope_resolution: ScannerPrefixReuseSnapshot,
|
||||
pub prefix_cycle_completion: ScannerPrefixReuseSnapshot,
|
||||
pub scope_fallbacks: BTreeMap<String, u64>,
|
||||
/// Rejections only for cycles with a baseline_unavailable scope fallback.
|
||||
pub authoritative_baseline_rejections: BTreeMap<String, u64>,
|
||||
pub observed_baseline_rejections: BTreeMap<String, u64>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ScannerScanObservation {
|
||||
metadata: [AtomicU64; 7],
|
||||
cache_count: [AtomicU64; 4],
|
||||
cache_nanos: [AtomicU64; 4],
|
||||
bucket_count: [AtomicU64; 2],
|
||||
bucket_nanos: [AtomicU64; 2],
|
||||
reuse_decisions: [[AtomicU64; ScannerCacheReuseDecision::ALL.len()]; 3],
|
||||
erasure_clean_prefix_walks: AtomicU64,
|
||||
prefix_evaluated: [AtomicU64; 2],
|
||||
prefix_activated: [AtomicU64; 2],
|
||||
prefix_blockers: [[AtomicU64; PREFIX_REUSE_BLOCKERS.len()]; 2],
|
||||
scope_fallbacks: [AtomicU64; SCOPE_FALLBACK_REASONS.len()],
|
||||
baseline_rejections: [[AtomicU64; ScannerBaselineRejection::ALL.len()]; 2],
|
||||
}
|
||||
|
||||
fn duration_nanos(duration: Duration) -> u64 {
|
||||
u64::try_from(duration.as_nanos()).unwrap_or(u64::MAX)
|
||||
}
|
||||
|
||||
fn labelled_counts(labels: impl IntoIterator<Item = &'static str>, counters: &[AtomicU64]) -> BTreeMap<String, u64> {
|
||||
labels
|
||||
.into_iter()
|
||||
.zip(counters)
|
||||
.map(|(label, count)| (label.to_owned(), count.load(Ordering::Relaxed)))
|
||||
.collect()
|
||||
}
|
||||
|
||||
impl ScannerScanObservation {
|
||||
pub(crate) fn start_metadata_read(&self) {
|
||||
self.metadata[0].fetch_add(1, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
pub(crate) fn finish_metadata_read(&self, outcome: ScannerMetadataReadOutcome, bytes: u64, elapsed: Duration) {
|
||||
self.metadata[1 + outcome as usize].fetch_add(1, Ordering::Relaxed);
|
||||
if outcome == ScannerMetadataReadOutcome::Success {
|
||||
self.metadata[5].fetch_add(bytes, Ordering::Relaxed);
|
||||
}
|
||||
self.metadata[6].fetch_add(duration_nanos(elapsed), Ordering::Relaxed);
|
||||
}
|
||||
|
||||
pub fn record_cache_reuse(&self, kind: ScannerCacheReuseKind, elapsed: Duration) {
|
||||
self.cache_count[kind as usize].fetch_add(1, Ordering::Relaxed);
|
||||
self.cache_nanos[kind as usize].fetch_add(duration_nanos(elapsed), Ordering::Relaxed);
|
||||
}
|
||||
|
||||
/// Records completed reuse work only; it grants no publication authority.
|
||||
pub fn record_bucket_reuse(&self, kind: ScannerBucketReuseKind, count: u64, elapsed: Duration) {
|
||||
self.bucket_count[kind as usize].fetch_add(count, Ordering::Relaxed);
|
||||
self.bucket_nanos[kind as usize].fetch_add(duration_nanos(elapsed), Ordering::Relaxed);
|
||||
}
|
||||
|
||||
/// Records one first-condition decision without inspecting later prerequisites.
|
||||
pub fn record_cache_reuse_decision(&self, gate: ScannerCacheReuseGate, decision: ScannerCacheReuseDecision) {
|
||||
self.reuse_decisions[gate as usize][decision as usize].fetch_add(1, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
pub fn record_erasure_clean_prefix_walk(&self) {
|
||||
self.erasure_clean_prefix_walks.fetch_add(1, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
pub fn record_prefix_reuse(&self, stage: ScannerPrefixReuseStage, activated: bool, blockers: &[Option<&str>; 8]) {
|
||||
let stage = stage as usize;
|
||||
self.prefix_evaluated[stage].fetch_add(1, Ordering::Relaxed);
|
||||
if activated {
|
||||
self.prefix_activated[stage].fetch_add(1, Ordering::Relaxed);
|
||||
}
|
||||
for blocker in blockers.iter().flatten() {
|
||||
if let Some(index) = PREFIX_REUSE_BLOCKERS.iter().position(|label| label == blocker) {
|
||||
self.prefix_blockers[stage][index].fetch_add(1, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn record_scope_fallback(&self, reason: &str) {
|
||||
if let Some(index) = SCOPE_FALLBACK_REASONS.iter().position(|label| *label == reason) {
|
||||
self.scope_fallbacks[index].fetch_add(1, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn record_baseline_rejection(&self, authoritative: ScannerBaselineRejection, observed: Option<ScannerBaselineRejection>) {
|
||||
self.baseline_rejections[0][authoritative as usize].fetch_add(1, Ordering::Relaxed);
|
||||
if let Some(observed) = observed {
|
||||
self.baseline_rejections[1][observed as usize].fetch_add(1, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn snapshot(&self) -> ScannerScanObservationSnapshot {
|
||||
let prefix = |stage: ScannerPrefixReuseStage| ScannerPrefixReuseSnapshot {
|
||||
evaluated: self.prefix_evaluated[stage as usize].load(Ordering::Relaxed),
|
||||
activated: self.prefix_activated[stage as usize].load(Ordering::Relaxed),
|
||||
blockers: labelled_counts(PREFIX_REUSE_BLOCKERS, &self.prefix_blockers[stage as usize]),
|
||||
};
|
||||
ScannerScanObservationSnapshot {
|
||||
metadata_reads: ScannerMetadataReadSnapshot {
|
||||
attempts: self.metadata[0].load(Ordering::Relaxed),
|
||||
succeeded: self.metadata[1].load(Ordering::Relaxed),
|
||||
not_found: self.metadata[2].load(Ordering::Relaxed),
|
||||
errors: self.metadata[3].load(Ordering::Relaxed),
|
||||
cancelled: self.metadata[4].load(Ordering::Relaxed),
|
||||
bytes: self.metadata[5].load(Ordering::Relaxed),
|
||||
elapsed_nanos: self.metadata[6].load(Ordering::Relaxed),
|
||||
},
|
||||
cache_reuse: ScannerCacheReuseKind::ALL
|
||||
.into_iter()
|
||||
.map(|kind| {
|
||||
(
|
||||
kind.label().to_owned(),
|
||||
ScannerCacheReuseSnapshot {
|
||||
count: self.cache_count[kind as usize].load(Ordering::Relaxed),
|
||||
elapsed_nanos: self.cache_nanos[kind as usize].load(Ordering::Relaxed),
|
||||
},
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
bucket_reuse: ScannerBucketReuseKind::ALL
|
||||
.into_iter()
|
||||
.map(|kind| {
|
||||
(
|
||||
kind.label().to_owned(),
|
||||
ScannerBucketReuseSnapshot {
|
||||
count: self.bucket_count[kind as usize].load(Ordering::Relaxed),
|
||||
elapsed_nanos: self.bucket_nanos[kind as usize].load(Ordering::Relaxed),
|
||||
},
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
reuse_decisions: ScannerCacheReuseGate::ALL
|
||||
.into_iter()
|
||||
.map(|gate| {
|
||||
let counts = &self.reuse_decisions[gate as usize];
|
||||
let decisions = |not_evaluated| {
|
||||
ScannerCacheReuseDecision::ALL
|
||||
.into_iter()
|
||||
.filter(|decision| {
|
||||
*decision != ScannerCacheReuseDecision::Eligible && decision.not_evaluated() == not_evaluated
|
||||
})
|
||||
.map(|decision| (decision.label().to_owned(), counts[decision as usize].load(Ordering::Relaxed)))
|
||||
.collect()
|
||||
};
|
||||
(
|
||||
gate.label().to_owned(),
|
||||
ScannerCacheReuseDecisionSnapshot {
|
||||
not_evaluated: decisions(true),
|
||||
blocked: decisions(false),
|
||||
eligible: counts[ScannerCacheReuseDecision::Eligible as usize].load(Ordering::Relaxed),
|
||||
},
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
erasure_clean_prefix_walks: self.erasure_clean_prefix_walks.load(Ordering::Relaxed),
|
||||
prefix_scope_resolution: prefix(ScannerPrefixReuseStage::ScopeResolution),
|
||||
prefix_cycle_completion: prefix(ScannerPrefixReuseStage::CycleCompletion),
|
||||
scope_fallbacks: labelled_counts(SCOPE_FALLBACK_REASONS, &self.scope_fallbacks),
|
||||
authoritative_baseline_rejections: labelled_counts(
|
||||
ScannerBaselineRejection::ALL.map(ScannerBaselineRejection::label),
|
||||
&self.baseline_rejections[0],
|
||||
),
|
||||
observed_baseline_rejections: labelled_counts(
|
||||
ScannerBaselineRejection::ALL.map(ScannerBaselineRejection::label),
|
||||
&self.baseline_rejections[1],
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Dropping an unfinished read records cancellation. The guard observes the
|
||||
/// read future only; it cannot cancel it or change its storage result.
|
||||
#[must_use]
|
||||
pub struct ScannerMetadataReadGuard<'a> {
|
||||
pub(crate) metrics: &'a Metrics,
|
||||
pub(crate) started: Instant,
|
||||
pub(crate) outcome: ScannerMetadataReadOutcome,
|
||||
pub(crate) bytes: u64,
|
||||
}
|
||||
|
||||
impl ScannerMetadataReadGuard<'_> {
|
||||
pub fn finish(mut self, outcome: ScannerMetadataReadOutcome, bytes: u64) {
|
||||
self.outcome = outcome;
|
||||
self.bytes = bytes;
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for ScannerMetadataReadGuard<'_> {
|
||||
fn drop(&mut self) {
|
||||
self.metrics
|
||||
.finish_scanner_metadata_read(self.outcome, self.bytes, self.started.elapsed());
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[tokio::test]
|
||||
async fn metadata_read_reports_storage_results_and_existing_read_metric() {
|
||||
let metrics = Metrics::new();
|
||||
metrics
|
||||
.start_scanner_metadata_read()
|
||||
.finish(ScannerMetadataReadOutcome::Success, 127);
|
||||
metrics
|
||||
.start_scanner_metadata_read()
|
||||
.finish(ScannerMetadataReadOutcome::Success, 0);
|
||||
metrics
|
||||
.start_scanner_metadata_read()
|
||||
.finish(ScannerMetadataReadOutcome::NotFound, 0);
|
||||
metrics
|
||||
.start_scanner_metadata_read()
|
||||
.finish(ScannerMetadataReadOutcome::Error, 0);
|
||||
let report = metrics.report().await;
|
||||
let reads = &report.scan_observation.metadata_reads;
|
||||
assert_eq!(
|
||||
(reads.attempts, reads.succeeded, reads.not_found, reads.errors, reads.cancelled),
|
||||
(4, 2, 1, 1, 0)
|
||||
);
|
||||
assert_eq!(reads.bytes, 127);
|
||||
assert_eq!(report.life_time_ops["read_metadata"], 4);
|
||||
assert_eq!(report.last_minute.actions["read_metadata"].count, 4);
|
||||
assert_eq!(report.last_minute.actions["read_metadata"].bytes, 127);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn metadata_read_cancellation_balances_an_actually_polled_future_once() {
|
||||
let metrics = Arc::new(Metrics::new());
|
||||
let worker_metrics = Arc::clone(&metrics);
|
||||
let (started, admitted) = tokio::sync::oneshot::channel();
|
||||
let worker = tokio::spawn(async move {
|
||||
let read = worker_metrics.start_scanner_metadata_read();
|
||||
started.send(()).expect("observe started read");
|
||||
std::future::pending::<()>().await;
|
||||
read.finish(ScannerMetadataReadOutcome::Success, 99);
|
||||
});
|
||||
tokio::time::timeout(Duration::from_secs(5), admitted)
|
||||
.await
|
||||
.expect("read starts within deadline")
|
||||
.expect("read started");
|
||||
let in_flight = metrics.scan_observation().snapshot().metadata_reads;
|
||||
assert_eq!((in_flight.attempts, in_flight.cancelled, in_flight.succeeded), (1, 0, 0));
|
||||
worker.abort();
|
||||
assert!(worker.await.expect_err("read task was aborted").is_cancelled());
|
||||
let report = metrics.report().await;
|
||||
let reads = &report.scan_observation.metadata_reads;
|
||||
assert_eq!((reads.attempts, reads.cancelled, reads.succeeded, reads.bytes), (1, 1, 0, 0));
|
||||
assert_eq!(report.life_time_ops["read_metadata"], 1);
|
||||
assert_eq!(report.last_minute.actions["read_metadata"].count, 1);
|
||||
assert_eq!(
|
||||
Duration::from_nanos(reads.elapsed_nanos).as_secs(),
|
||||
report.last_minute.actions["read_metadata"].acc_time
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn observation_report_keeps_stages_reasons_and_copy_costs_separate() {
|
||||
let metrics = Metrics::new();
|
||||
let observation = metrics.scan_observation();
|
||||
observation.record_cache_reuse(ScannerCacheReuseKind::CleanPrefix, Duration::from_nanos(321));
|
||||
observation.record_bucket_reuse(ScannerBucketReuseKind::ScopedColdBucket, 2, Duration::from_nanos(654));
|
||||
observation.record_bucket_reuse(ScannerBucketReuseKind::CurrentRoot, 1, Duration::from_nanos(987));
|
||||
observation.record_cache_reuse_decision(ScannerCacheReuseGate::CleanPrefix, ScannerCacheReuseDecision::MissingScope);
|
||||
observation
|
||||
.record_cache_reuse_decision(ScannerCacheReuseGate::CleanPrefix, ScannerCacheReuseDecision::ErasureHealthRequired);
|
||||
observation.record_cache_reuse_decision(ScannerCacheReuseGate::CoveredNamespace, ScannerCacheReuseDecision::Eligible);
|
||||
observation.record_erasure_clean_prefix_walk();
|
||||
observation.record_prefix_reuse(
|
||||
ScannerPrefixReuseStage::ScopeResolution,
|
||||
false,
|
||||
&[
|
||||
Some("missing_producer_identity"),
|
||||
Some("missing_durable_journal_replay"),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
],
|
||||
);
|
||||
observation.record_prefix_reuse(ScannerPrefixReuseStage::CycleCompletion, true, &[None; 8]);
|
||||
metrics.record_scanner_scope_fallback("baseline_unavailable");
|
||||
observation.record_baseline_rejection(ScannerBaselineRejection::Epoch, Some(ScannerBaselineRejection::Missing));
|
||||
let report = metrics.report().await;
|
||||
let snapshot = &report.scan_observation;
|
||||
assert_eq!(snapshot.cache_reuse["clean_prefix"].count, 1);
|
||||
assert_eq!(snapshot.cache_reuse["clean_prefix"].elapsed_nanos, 321);
|
||||
assert_eq!(snapshot.cache_reuse["sampled_namespace"].count, 0);
|
||||
assert_eq!(snapshot.bucket_reuse["scoped_cold_bucket"].count, 2);
|
||||
assert_eq!(snapshot.bucket_reuse["scoped_cold_bucket"].elapsed_nanos, 654);
|
||||
assert_eq!(snapshot.bucket_reuse["current_root"].count, 1);
|
||||
assert_eq!(snapshot.bucket_reuse["current_root"].elapsed_nanos, 987);
|
||||
assert_eq!(snapshot.reuse_decisions["clean_prefix"].not_evaluated["missing_scope"], 1);
|
||||
assert_eq!(snapshot.reuse_decisions["clean_prefix"].blocked["erasure_health_required"], 1);
|
||||
assert_eq!(snapshot.reuse_decisions["covered_namespace"].eligible, 1);
|
||||
|
||||
let mut prior_observations = serde_json::to_value(snapshot).expect("encode observations");
|
||||
let fields = prior_observations.as_object_mut().expect("observation object");
|
||||
fields.remove("bucket_reuse");
|
||||
fields.remove("reuse_decisions");
|
||||
let prior: ScannerScanObservationSnapshot =
|
||||
serde_json::from_value(prior_observations).expect("read earlier observations");
|
||||
assert!(prior.bucket_reuse.is_empty());
|
||||
assert!(prior.reuse_decisions.is_empty());
|
||||
assert_eq!(prior.cache_reuse["clean_prefix"].count, 1);
|
||||
|
||||
assert_eq!(snapshot.erasure_clean_prefix_walks, 1);
|
||||
assert_eq!(
|
||||
(snapshot.prefix_scope_resolution.evaluated, snapshot.prefix_scope_resolution.activated),
|
||||
(1, 0)
|
||||
);
|
||||
assert_eq!(snapshot.prefix_scope_resolution.blockers["missing_producer_identity"], 1);
|
||||
assert_eq!(snapshot.prefix_scope_resolution.blockers["missing_durable_journal_replay"], 1);
|
||||
assert_eq!(
|
||||
(snapshot.prefix_cycle_completion.evaluated, snapshot.prefix_cycle_completion.activated),
|
||||
(1, 1)
|
||||
);
|
||||
assert_eq!(
|
||||
snapshot.scope_fallbacks["baseline_unavailable"],
|
||||
report.usage_freshness.scope_fallback_total
|
||||
);
|
||||
assert_eq!(snapshot.authoritative_baseline_rejections["epoch"], 1);
|
||||
assert_eq!(snapshot.observed_baseline_rejections["missing"], 1);
|
||||
|
||||
let mut legacy = serde_json::to_value(&report).expect("encode report");
|
||||
legacy
|
||||
.as_object_mut()
|
||||
.expect("report is an object")
|
||||
.remove("scan_observation");
|
||||
let decoded: crate::metrics::ScannerMetricsReport = serde_json::from_value(legacy).expect("read legacy report");
|
||||
assert!(decoded.scan_observation.cache_reuse.is_empty(), "legacy counters are unobserved");
|
||||
assert_eq!(decoded.scan_observation.metadata_reads.attempts, 0);
|
||||
}
|
||||
}
|
||||
@@ -206,6 +206,32 @@ pub(crate) struct DataUsageCacheRevisions {
|
||||
backup: Option<DataUsageCacheRevision>,
|
||||
}
|
||||
|
||||
/// In-memory provenance of the decoded cache and the revisions observed by its loader.
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct DataUsageCacheBaselineRevisions {
|
||||
revisions: DataUsageCacheRevisions,
|
||||
loaded_from_main: bool,
|
||||
}
|
||||
|
||||
impl DataUsageCacheBaselineRevisions {
|
||||
pub(crate) fn revisions(&self) -> &DataUsageCacheRevisions {
|
||||
&self.revisions
|
||||
}
|
||||
|
||||
/// A backup may catch up to an unchanged primary without changing the decoded baseline.
|
||||
/// Both reads must have decoded that primary; an ETag on a corrupt primary is insufficient.
|
||||
pub(crate) fn permits_unchanged_baseline_since(&self, initial: &Self) -> bool {
|
||||
if self.revisions == initial.revisions {
|
||||
return true;
|
||||
}
|
||||
self.loaded_from_main
|
||||
&& initial.loaded_from_main
|
||||
&& matches!(&initial.revisions.main, DataUsageCacheRevision::Etag(etag) if !etag.is_empty())
|
||||
&& self.revisions.main == initial.revisions.main
|
||||
&& self.revisions.backup.as_ref() == Some(&self.revisions.main)
|
||||
}
|
||||
}
|
||||
|
||||
pub static DATA_USAGE_BUCKET: LazyLock<String> =
|
||||
LazyLock::new(|| format!("{RUSTFS_META_BUCKET}{SLASH_SEPARATOR}{BUCKET_META_PREFIX}"));
|
||||
|
||||
|
||||
@@ -71,6 +71,7 @@ struct DataUsageCacheLoadResult {
|
||||
cache: DataUsageCache,
|
||||
main_revision: DataUsageCacheRevision,
|
||||
backup_revision: Option<DataUsageCacheRevision>,
|
||||
loaded_from_main: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
@@ -107,6 +108,18 @@ impl DataUsageCache {
|
||||
store: Arc<S>,
|
||||
name: &str,
|
||||
) -> StorageResult<DataUsageCacheRevisions> {
|
||||
self.load_with_baseline_revisions(store, name)
|
||||
.await
|
||||
.map(|baseline| baseline.revisions)
|
||||
}
|
||||
|
||||
/// Loads cache revisions with provenance for unchanged-primary backup reconciliation.
|
||||
/// Missing or rebuilt caches and a decoded backup do not establish a decoded primary.
|
||||
pub(crate) async fn load_with_baseline_revisions<S: ScannerObjectIO>(
|
||||
&mut self,
|
||||
store: Arc<S>,
|
||||
name: &str,
|
||||
) -> StorageResult<DataUsageCacheBaselineRevisions> {
|
||||
let backup_name = format!("{name}.bkp");
|
||||
let backup_path = path_join_buf(&[BUCKET_META_PREFIX, &backup_name]);
|
||||
let loaded = Self::load_cache(store.clone(), name).await?;
|
||||
@@ -134,7 +147,10 @@ impl DataUsageCache {
|
||||
let main = loaded.main_revision;
|
||||
*self = loaded.cache;
|
||||
|
||||
Ok(DataUsageCacheRevisions { main, backup })
|
||||
Ok(DataUsageCacheBaselineRevisions {
|
||||
revisions: DataUsageCacheRevisions { main, backup },
|
||||
loaded_from_main: loaded.loaded_from_main,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) async fn read_revisions<S: ScannerObjectIO>(store: Arc<S>, name: &str) -> StorageResult<DataUsageCacheRevisions> {
|
||||
@@ -157,10 +173,13 @@ impl DataUsageCache {
|
||||
cache,
|
||||
revision: Some(main_revision),
|
||||
} => {
|
||||
// Legacy-location fallback carries Missing for the primary path.
|
||||
let loaded_from_main = matches!(&main_revision, DataUsageCacheRevision::Etag(_));
|
||||
return Ok(DataUsageCacheLoadResult {
|
||||
cache: *cache,
|
||||
main_revision,
|
||||
backup_revision: None,
|
||||
loaded_from_main,
|
||||
});
|
||||
}
|
||||
DataUsageCacheLoadAttempt::Loaded { revision: None, .. } => {
|
||||
@@ -198,6 +217,7 @@ impl DataUsageCache {
|
||||
cache: *cache,
|
||||
main_revision,
|
||||
backup_revision: Some(backup_revision),
|
||||
loaded_from_main: false,
|
||||
});
|
||||
}
|
||||
DataUsageCacheLoadState::Loaded
|
||||
@@ -221,6 +241,7 @@ impl DataUsageCache {
|
||||
main_revision: main_revision
|
||||
.ok_or_else(|| Error::other(format!("scanner cache missing state has no revision: {name}")))?,
|
||||
backup_revision,
|
||||
loaded_from_main: false,
|
||||
});
|
||||
}
|
||||
(DataUsageCacheLoadState::Corrupt, DataUsageCacheLoadState::Missing)
|
||||
@@ -240,6 +261,7 @@ impl DataUsageCache {
|
||||
main_revision: main_revision
|
||||
.ok_or_else(|| Error::other(format!("scanner cache corrupt state has no revision: {name}")))?,
|
||||
backup_revision,
|
||||
loaded_from_main: false,
|
||||
});
|
||||
}
|
||||
_ => {}
|
||||
|
||||
@@ -772,6 +772,102 @@ async fn corrupt_primary_cache_loads_valid_backup() {
|
||||
assert_eq!(store.puts.load(Ordering::SeqCst), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn baseline_revisions_allow_only_a_decoded_unchanged_primary_backup_catchup() {
|
||||
let etag = |value: &str| DataUsageCacheRevision::Etag(value.to_owned());
|
||||
let initial = DataUsageCacheBaselineRevisions {
|
||||
revisions: DataUsageCacheRevisions {
|
||||
main: etag("primary"),
|
||||
backup: Some(etag("old-backup")),
|
||||
},
|
||||
loaded_from_main: true,
|
||||
};
|
||||
assert!(initial.permits_unchanged_baseline_since(&initial));
|
||||
let mut caught_up = initial.clone();
|
||||
caught_up.revisions.backup = Some(etag("primary"));
|
||||
assert!(caught_up.permits_unchanged_baseline_since(&initial));
|
||||
assert!(
|
||||
!initial.permits_unchanged_baseline_since(&caught_up),
|
||||
"backup rollback changes the baseline"
|
||||
);
|
||||
for backup in [None, Some(DataUsageCacheRevision::Missing)] {
|
||||
let mut missing_backup = initial.clone();
|
||||
missing_backup.revisions.backup = backup;
|
||||
assert!(caught_up.permits_unchanged_baseline_since(&missing_backup));
|
||||
assert!(!missing_backup.permits_unchanged_baseline_since(&initial));
|
||||
}
|
||||
for (initial_from_main, current_from_main) in [(false, true), (true, false), (false, false)] {
|
||||
let mut baseline = initial.clone();
|
||||
baseline.loaded_from_main = initial_from_main;
|
||||
let mut current = caught_up.clone();
|
||||
current.loaded_from_main = current_from_main;
|
||||
assert!(
|
||||
!current.permits_unchanged_baseline_since(&baseline),
|
||||
"backup fallback grants no primary proof"
|
||||
);
|
||||
current.revisions = baseline.revisions.clone();
|
||||
assert!(
|
||||
current.permits_unchanged_baseline_since(&baseline),
|
||||
"exact revisions retain existing admission"
|
||||
);
|
||||
}
|
||||
let mut changed_main = caught_up.clone();
|
||||
changed_main.revisions.main = etag("new-primary");
|
||||
changed_main.revisions.backup = Some(etag("new-primary"));
|
||||
assert!(!changed_main.permits_unchanged_baseline_since(&initial));
|
||||
let mut changed_backup = caught_up;
|
||||
changed_backup.revisions.backup = Some(etag("unrelated-backup"));
|
||||
assert!(!changed_backup.permits_unchanged_baseline_since(&initial));
|
||||
for main in [DataUsageCacheRevision::Missing, etag("")] {
|
||||
let mut baseline = initial.clone();
|
||||
baseline.revisions.main = main.clone();
|
||||
let mut current = baseline.clone();
|
||||
current.revisions.backup = Some(main);
|
||||
assert!(
|
||||
!current.permits_unchanged_baseline_since(&baseline),
|
||||
"missing or empty primary ETag is not proof"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn baseline_revisions_record_the_actual_decoded_cache_source() {
|
||||
let mut expected = DataUsageCache::default();
|
||||
expected.info.name = "decoded".to_owned();
|
||||
let valid = expected.marshal_msg().expect("encode valid cache");
|
||||
for (main, backup, from_main, name) in [
|
||||
(valid.clone(), Some(valid.clone()), true, "decoded"),
|
||||
(b"corrupt".to_vec(), Some(valid.clone()), false, "decoded"),
|
||||
(b"corrupt".to_vec(), None, false, ""),
|
||||
(Vec::new(), None, false, ""),
|
||||
] {
|
||||
let store = Arc::new(CacheReadStore::new(CacheReadBody::Bytes(main), backup));
|
||||
let mut loaded = DataUsageCache::default();
|
||||
let baseline = loaded
|
||||
.load_with_baseline_revisions(store.clone(), DATA_USAGE_CACHE_NAME)
|
||||
.await
|
||||
.expect("load actual cache source");
|
||||
assert_eq!(baseline.loaded_from_main, from_main);
|
||||
assert_eq!(loaded.info.name, name);
|
||||
let mut raw_loaded = DataUsageCache::default();
|
||||
let raw = raw_loaded
|
||||
.load_with_revisions(store, DATA_USAGE_CACHE_NAME)
|
||||
.await
|
||||
.expect("load compatible raw revisions");
|
||||
assert_eq!(baseline.revisions(), &raw, "raw loader retains its revision contract");
|
||||
}
|
||||
let mut legacy_store = CacheReadStore::new(CacheReadBody::Bytes(Vec::new()), None);
|
||||
legacy_store.legacy = Some(valid);
|
||||
let mut legacy_cache = DataUsageCache::default();
|
||||
let legacy = legacy_cache
|
||||
.load_with_baseline_revisions(Arc::new(legacy_store), "legacy-cache")
|
||||
.await
|
||||
.expect("load cache from the legacy location");
|
||||
assert_eq!(legacy_cache.info.name, "decoded");
|
||||
assert!(!legacy.loaded_from_main);
|
||||
assert_eq!(legacy.revisions.main, DataUsageCacheRevision::Missing);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn corrupt_cache_without_valid_backup_rebuilds_with_cas_revisions() {
|
||||
for backup in [None, Some(b"also-not-msgpack".to_vec())] {
|
||||
|
||||
@@ -248,13 +248,13 @@ const CLEAN_IDLE_BACKOFF_FACTOR: u32 = 2;
|
||||
/// otherwise idle instance whose clean-idle backoff had doubled a 60 s
|
||||
/// interval), which defeats the fast path it is meant to protect.
|
||||
///
|
||||
/// The exponential growth in [`ScannerRetryBackoff::retry_interval`] is
|
||||
/// what protects against a persistently hot bucket driving an unbroken
|
||||
/// full-scan loop, so it can start small: 5 s, 10 s, 20 s … capped by
|
||||
/// [`SCANNER_RETRY_MAX_INTERVAL`]. A one-off race recovers in seconds; a
|
||||
/// genuinely hot bucket still reaches minute-scale backoff within a handful of
|
||||
/// cycles. Preflight deferrals use the same bounded schedule so a temporarily
|
||||
/// unavailable peer cannot drive a tight retry loop.
|
||||
/// The exponential growth in [`ScannerRetryBackoff::retry_interval`] protects
|
||||
/// against a persistently hot bucket driving an unbroken full-scan loop, so
|
||||
/// retries can start small: 5 s, 10 s, 20 s … Superseded retries cap this
|
||||
/// schedule at the ordinary cycle interval and preserve an operator-selected
|
||||
/// nominal cadence. Preflight deferrals retain the generic
|
||||
/// [`SCANNER_RETRY_MAX_INTERVAL`] cap so a temporarily unavailable peer cannot
|
||||
/// drive a tight retry loop.
|
||||
const SCANNER_RETRY_BASE_INTERVAL: Duration = Duration::from_secs(5);
|
||||
const SCANNER_RETRY_MAX_INTERVAL: Duration = Duration::from_secs(30 * 60);
|
||||
/// Probe cadence while data movement pauses the scanner. Recovery retries use
|
||||
@@ -3528,8 +3528,7 @@ where
|
||||
));
|
||||
let mut wait_plan =
|
||||
scanner_cycle_wait_plan(&runtime_config, clean_idle_backoff, backoff_enabled, randomized_cycle_delay_for);
|
||||
let superseded_retry_interval =
|
||||
scanner_superseded_retry_interval(superseded_backoff, &runtime_config, maintenance_features);
|
||||
let superseded_retry_interval = scanner_superseded_retry_interval(superseded_backoff, &runtime_config);
|
||||
let deferred_retry_interval = deferred_backoff.retry_interval(runtime_config.cycle_interval);
|
||||
let scope_fallback_retry_interval = scanner_scope_fallback_retry_interval(scope_fallback_backoff, &runtime_config);
|
||||
let mut convergence_retry_interval = superseded_retry_interval
|
||||
|
||||
@@ -192,19 +192,16 @@ impl ScannerRetryBackoff {
|
||||
pub(super) fn scanner_superseded_retry_interval(
|
||||
backoff: ScannerRetryBackoff,
|
||||
runtime_config: &ScannerRuntimeConfig,
|
||||
maintenance_features: ScannerMaintenanceFeatures,
|
||||
) -> Option<Duration> {
|
||||
let mut retry_interval = backoff.retry_interval(runtime_config.cycle_interval)?;
|
||||
// Preserve an operator-selected duty-cycle floor during convergence.
|
||||
let cycle_interval = runtime_config.cycle_interval.max(Duration::from_secs(1));
|
||||
let retry_interval = backoff.retry_interval(cycle_interval)?;
|
||||
// Preserve an operator-selected nominal cadence during convergence.
|
||||
if runtime_config.cycle_interval_source != ScannerRuntimeConfigSource::Default {
|
||||
retry_interval = retry_interval.max(runtime_config.cycle_interval);
|
||||
return Some(cycle_interval);
|
||||
}
|
||||
// A complete but non-authoritative walk must not postpone lifecycle or
|
||||
// replication evaluation beyond its ordinary maintenance cadence.
|
||||
if maintenance_features.needs_regular_cycle() {
|
||||
retry_interval = retry_interval.min(runtime_config.cycle_interval.max(Duration::from_secs(1)));
|
||||
}
|
||||
Some(retry_interval)
|
||||
// A complete but non-authoritative walk must not postpone convergence
|
||||
// beyond the ordinary cadence, even without lifecycle or replication.
|
||||
Some(retry_interval.min(cycle_interval))
|
||||
}
|
||||
|
||||
/// Pacing for the cycle after a full-namespace scope fallback while dirty
|
||||
|
||||
@@ -8947,7 +8947,7 @@ fn clean_idle_backoff_grows_to_cap() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn superseded_retry_backoff_grows_caps_and_resets_after_convergence() {
|
||||
fn retry_backoff_grows_caps_and_resets_after_convergence() {
|
||||
let mut backoff = ScannerRetryBackoff::default();
|
||||
assert_eq!(backoff.retry_interval(Duration::from_secs(24 * 60 * 60)), None);
|
||||
|
||||
@@ -8971,7 +8971,7 @@ fn superseded_retry_backoff_grows_caps_and_resets_after_convergence() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn superseded_retry_backoff_respects_a_faster_configured_cycle() {
|
||||
fn retry_backoff_respects_a_faster_configured_cycle() {
|
||||
let mut backoff = ScannerRetryBackoff::default();
|
||||
backoff.record_retryable_cycle(true);
|
||||
|
||||
@@ -8983,7 +8983,7 @@ fn superseded_retry_backoff_respects_a_faster_configured_cycle() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn superseded_retry_backoff_grows_from_the_default_cycle() {
|
||||
fn retry_backoff_grows_from_the_default_cycle() {
|
||||
let mut backoff = ScannerRetryBackoff::default();
|
||||
// The first race after a write burst retries in seconds, not a whole
|
||||
// cycle, while repeated supersedes still climb toward the cap.
|
||||
@@ -9003,7 +9003,7 @@ fn superseded_retry_preserves_explicit_cycle_cadence() {
|
||||
..Default::default()
|
||||
};
|
||||
assert_eq!(
|
||||
scanner_superseded_retry_interval(backoff, &default_config, ScannerMaintenanceFeatures::default()),
|
||||
scanner_superseded_retry_interval(backoff, &default_config),
|
||||
Some(SCANNER_RETRY_BASE_INTERVAL),
|
||||
"the default adaptive cadence must retain fast convergence"
|
||||
);
|
||||
@@ -9013,24 +9013,24 @@ fn superseded_retry_preserves_explicit_cycle_cadence() {
|
||||
ScannerRuntimeConfigSource::Config,
|
||||
ScannerRuntimeConfigSource::ScannerCompatConfig,
|
||||
] {
|
||||
let runtime_config = ScannerRuntimeConfig {
|
||||
cycle_interval: Duration::from_secs(6 * 60 * 60),
|
||||
cycle_interval_source: source,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
for features in [
|
||||
ScannerMaintenanceFeatures::default(),
|
||||
ScannerMaintenanceFeatures {
|
||||
lifecycle: true,
|
||||
for cycle_secs in [1, 3, 600, 6 * 60 * 60] {
|
||||
let runtime_config = ScannerRuntimeConfig {
|
||||
cycle_interval: Duration::from_secs(cycle_secs),
|
||||
cycle_interval_source: source,
|
||||
..Default::default()
|
||||
},
|
||||
] {
|
||||
assert_eq!(
|
||||
scanner_superseded_retry_interval(backoff, &runtime_config, features),
|
||||
Some(runtime_config.cycle_interval),
|
||||
"{source:?} cycle cadence must not be shortened by convergence retries"
|
||||
);
|
||||
};
|
||||
let mut backoff = ScannerRetryBackoff::default();
|
||||
assert_eq!(scanner_superseded_retry_interval(backoff, &runtime_config), None);
|
||||
for _ in 0..40 {
|
||||
backoff.record_retryable_cycle(true);
|
||||
assert_eq!(
|
||||
scanner_superseded_retry_interval(backoff, &runtime_config),
|
||||
Some(runtime_config.cycle_interval),
|
||||
"{source:?} cycle cadence must hold throughout convergence retries"
|
||||
);
|
||||
}
|
||||
backoff.record_retryable_cycle(false);
|
||||
assert_eq!(scanner_superseded_retry_interval(backoff, &runtime_config), None);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9051,7 +9051,7 @@ fn superseded_retry_does_not_delay_required_maintenance() {
|
||||
for consecutive_cycles in 1..=12 {
|
||||
backoff.record_retryable_cycle(true);
|
||||
assert_eq!(
|
||||
scanner_superseded_retry_interval(backoff, &config, features),
|
||||
scanner_superseded_retry_interval(backoff, &config),
|
||||
Some(config.cycle_interval),
|
||||
"superseded cycle {consecutive_cycles} must preserve regular maintenance"
|
||||
);
|
||||
@@ -9137,19 +9137,50 @@ fn scope_fallback_retry_preserves_explicit_and_fast_cycle_cadence() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn superseded_retry_only_caps_regular_maintenance() {
|
||||
fn superseded_retry_caps_at_the_default_cycle_and_resets_after_convergence() {
|
||||
let config = ScannerRuntimeConfig {
|
||||
cycle_interval: Duration::from_secs(60),
|
||||
..Default::default()
|
||||
};
|
||||
let mut backoff = ScannerRetryBackoff::default();
|
||||
for _ in 0..6 {
|
||||
assert_eq!(scanner_superseded_retry_interval(backoff, &config), None);
|
||||
for expected in [5, 10, 20, 40, 60, 60, 60, 60] {
|
||||
backoff.record_retryable_cycle(true);
|
||||
assert_eq!(scanner_superseded_retry_interval(backoff, &config), Some(Duration::from_secs(expected)));
|
||||
}
|
||||
backoff.consecutive_cycles = u32::MAX;
|
||||
assert_eq!(scanner_superseded_retry_interval(backoff, &config), Some(config.cycle_interval));
|
||||
backoff.record_retryable_cycle(true);
|
||||
assert_eq!(scanner_superseded_retry_interval(backoff, &config), Some(config.cycle_interval));
|
||||
backoff.record_retryable_cycle(false);
|
||||
assert_eq!(scanner_superseded_retry_interval(backoff, &config), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn superseded_retry_caps_fast_cycles_without_changing_the_generic_retry_cap() {
|
||||
let mut backoff = ScannerRetryBackoff::default();
|
||||
for _ in 0..40 {
|
||||
backoff.record_retryable_cycle(true);
|
||||
}
|
||||
assert_eq!(
|
||||
scanner_superseded_retry_interval(backoff, &config, ScannerMaintenanceFeatures::default()),
|
||||
Some(Duration::from_secs(160)),
|
||||
backoff.retry_interval(Duration::from_secs(60)),
|
||||
Some(Duration::from_secs(30 * 60)),
|
||||
"deferred and transient retries must retain the generic cap"
|
||||
);
|
||||
for cycle_secs in [0, 1, 3, 60, 6 * 60 * 60] {
|
||||
let config = ScannerRuntimeConfig {
|
||||
cycle_interval: Duration::from_secs(cycle_secs),
|
||||
..Default::default()
|
||||
};
|
||||
assert_eq!(
|
||||
scanner_superseded_retry_interval(backoff, &config),
|
||||
Some(Duration::from_secs(cycle_secs.max(1))),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn superseded_retry_timer_keeps_required_maintenance_full_scans() {
|
||||
for features in [
|
||||
ScannerMaintenanceFeatures {
|
||||
lifecycle: true,
|
||||
@@ -9164,28 +9195,23 @@ fn superseded_retry_only_caps_regular_maintenance() {
|
||||
..Default::default()
|
||||
},
|
||||
] {
|
||||
assert_eq!(scanner_superseded_retry_interval(backoff, &config, features), Some(config.cycle_interval));
|
||||
assert_eq!(scanner_superseded_retry_interval(ScannerRetryBackoff::default(), &config, features), None);
|
||||
assert!(features.requires_full_scan(Some(7), 7, ScannerCycleWakeReason::Timer, true));
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test(start_paused = true)]
|
||||
#[serial]
|
||||
async fn superseded_maintenance_wait_keeps_convergence_fencing() {
|
||||
async fn superseded_retry_wait_keeps_convergence_fencing_at_the_default_cycle() {
|
||||
crate::scanner_io::clear_dirty_usage_buckets_for_tests();
|
||||
let config = ScannerRuntimeConfig {
|
||||
cycle_interval: Duration::from_secs(60),
|
||||
..Default::default()
|
||||
};
|
||||
let features = ScannerMaintenanceFeatures {
|
||||
lifecycle: true,
|
||||
..Default::default()
|
||||
};
|
||||
let mut backoff = ScannerRetryBackoff::default();
|
||||
for _ in 0..12 {
|
||||
backoff.record_retryable_cycle(true);
|
||||
}
|
||||
let interval = scanner_superseded_retry_interval(backoff, &config, features);
|
||||
let interval = scanner_superseded_retry_interval(backoff, &config);
|
||||
assert_eq!(interval, Some(config.cycle_interval));
|
||||
let generations = ScannerCycleObservedGenerations::for_wait(
|
||||
&config,
|
||||
@@ -9200,19 +9226,28 @@ async fn superseded_maintenance_wait_keeps_convergence_fencing() {
|
||||
let mut seen = Some(BTreeMap::from([("node-2".to_string(), scanner_node_activity("epoch-a", 7, 3))]));
|
||||
let changed = BTreeMap::from([("node-2".to_string(), scanner_node_activity("epoch-a", 8, 3))]);
|
||||
let started = tokio::time::Instant::now();
|
||||
let reason = wait_for_next_scanner_cycle_with_activity(
|
||||
let mut wait = Box::pin(wait_for_next_scanner_cycle_with_activity(
|
||||
&ctx,
|
||||
interval.expect("maintenance must retain its convergence interval"),
|
||||
interval.expect("superseded retry must retain its convergence interval"),
|
||||
Some(Duration::from_secs(1)),
|
||||
&mut seen,
|
||||
generations,
|
||||
|| false,
|
||||
|| std::future::ready(Ok(changed.clone())),
|
||||
)
|
||||
.await;
|
||||
));
|
||||
assert!(matches!(futures::poll!(&mut wait), Poll::Pending));
|
||||
crate::scanner_io::record_dirty_usage_bucket("photos");
|
||||
assert!(matches!(futures::poll!(&mut wait), Poll::Pending));
|
||||
for _ in 0..3 {
|
||||
tokio::time::advance(Duration::from_secs(15)).await;
|
||||
crate::scanner_io::record_dirty_usage_bucket("photos");
|
||||
assert!(matches!(futures::poll!(&mut wait), Poll::Pending));
|
||||
}
|
||||
let reason = wait.await;
|
||||
assert_eq!(reason, ScannerCycleWakeReason::Timer);
|
||||
assert_eq!(started.elapsed(), config.cycle_interval);
|
||||
assert_eq!(seen, Some(changed));
|
||||
crate::scanner_io::clear_dirty_usage_buckets_for_tests();
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -59,6 +59,7 @@ use rustfs_scanner_metrics::metrics::{
|
||||
CloseDiskGuard, IlmAction, Metric, Metrics, ScannerReplicationRepairKind, ScannerSourceWorkUpdate, ScannerWorkSource,
|
||||
UpdateCurrentPathFn, current_path_updater, global_metrics,
|
||||
};
|
||||
use rustfs_scanner_metrics::scan_observation::{ScannerCacheReuseDecision, ScannerCacheReuseGate, ScannerCacheReuseKind};
|
||||
use rustfs_utils::path::{SLASH_SEPARATOR, path_join_buf};
|
||||
use sha2::{Digest, Sha256};
|
||||
use time::OffsetDateTime;
|
||||
@@ -1060,38 +1061,51 @@ impl FolderScanner {
|
||||
child_hash: &DataUsageHash,
|
||||
abandoned_children: &DataUsageHashMap,
|
||||
) -> bool {
|
||||
let Some(prefix_scan_scope) = &self.prefix_scan_scope else {
|
||||
return false;
|
||||
let decision = if let Some(prefix_scan_scope) = &self.prefix_scan_scope {
|
||||
if folder.parent.is_some() || folder.name != self.old_cache.info.name {
|
||||
ScannerCacheReuseDecision::NotRoot
|
||||
} else if into.compacted {
|
||||
ScannerCacheReuseDecision::CompactedParent
|
||||
} else if !abandoned_children.contains(&child_hash.0) {
|
||||
ScannerCacheReuseDecision::MissingCachedChild
|
||||
} else if let Some(entry) = child
|
||||
.name
|
||||
.strip_prefix(folder.name.as_str())
|
||||
.and_then(|entry| entry.strip_prefix('/'))
|
||||
{
|
||||
if entry.is_empty() || entry.contains('/') {
|
||||
ScannerCacheReuseDecision::InvalidPrefix
|
||||
} else if prefix_scan_scope.contains(entry) {
|
||||
ScannerCacheReuseDecision::DirtyPrefix
|
||||
} else if self.is_erasure_mode {
|
||||
// EC scans must still perform independent object-health work.
|
||||
global_metrics().scan_observation().record_erasure_clean_prefix_walk();
|
||||
ScannerCacheReuseDecision::ErasureHealthRequired
|
||||
} else {
|
||||
ScannerCacheReuseDecision::Eligible
|
||||
}
|
||||
} else {
|
||||
ScannerCacheReuseDecision::InvalidPrefix
|
||||
}
|
||||
} else {
|
||||
ScannerCacheReuseDecision::MissingScope
|
||||
};
|
||||
// Erasure-mode usage scans also perform probabilistic object-health
|
||||
// work. Reusing a clean subtree here would silently suppress that
|
||||
// independent maintenance path, so prefix reuse is limited to the
|
||||
// non-erasure data-usage scanner.
|
||||
if self.is_erasure_mode
|
||||
|| folder.parent.is_some()
|
||||
|| folder.name != self.old_cache.info.name
|
||||
|| into.compacted
|
||||
|| !abandoned_children.contains(&clone_cache_value(&child_hash.0))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
let Some(entry) = child
|
||||
.name
|
||||
.strip_prefix(folder.name.as_str())
|
||||
.and_then(|entry| entry.strip_prefix('/'))
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
!entry.is_empty() && !entry.contains('/') && !prefix_scan_scope.contains(entry)
|
||||
global_metrics()
|
||||
.scan_observation()
|
||||
.record_cache_reuse_decision(ScannerCacheReuseGate::CleanPrefix, decision);
|
||||
decision == ScannerCacheReuseDecision::Eligible
|
||||
}
|
||||
|
||||
fn reuse_clean_root_child(&mut self, child: &CachedFolder, child_hash: &DataUsageHash, into: &mut DataUsageEntry) {
|
||||
let started = Instant::now();
|
||||
self.new_cache.copy_with_children(&self.old_cache, child_hash, &child.parent);
|
||||
if let Some(updates) = &mut self.usage_updates {
|
||||
updates.cache.copy_with_children(&self.old_cache, child_hash, &child.parent);
|
||||
}
|
||||
into.add_child(child_hash);
|
||||
global_metrics()
|
||||
.scan_observation()
|
||||
.record_cache_reuse(ScannerCacheReuseKind::CleanPrefix, started.elapsed());
|
||||
}
|
||||
|
||||
fn failed_retry_suppressed(&self, path: &str) -> bool {
|
||||
@@ -1278,14 +1292,60 @@ impl FolderScanner {
|
||||
}
|
||||
|
||||
fn can_reuse_covered_namespace(&self, name: &str, compacted: bool) -> bool {
|
||||
self.old_cache.info.scan_progress.is_some()
|
||||
&& !compacted
|
||||
&& self
|
||||
.resume_frontier
|
||||
.as_deref()
|
||||
.is_some_and(|frontier| scan_frontier_covers_path(name, frontier))
|
||||
&& self.old_cache.find(name).is_some()
|
||||
&& !self.resume_subtree_has_failures(name)
|
||||
let decision = if self.old_cache.info.scan_progress.is_none() {
|
||||
ScannerCacheReuseDecision::NoResumeSweep
|
||||
} else if compacted {
|
||||
ScannerCacheReuseDecision::CompactedParent
|
||||
} else if !self
|
||||
.resume_frontier
|
||||
.as_deref()
|
||||
.is_some_and(|frontier| scan_frontier_covers_path(name, frontier))
|
||||
{
|
||||
ScannerCacheReuseDecision::MissingCoverage
|
||||
} else if self.old_cache.find(name).is_none() {
|
||||
ScannerCacheReuseDecision::MissingCachedChild
|
||||
} else if self.resume_subtree_has_failures(name) {
|
||||
ScannerCacheReuseDecision::CachedFailures
|
||||
} else {
|
||||
ScannerCacheReuseDecision::Eligible
|
||||
};
|
||||
global_metrics()
|
||||
.scan_observation()
|
||||
.record_cache_reuse_decision(ScannerCacheReuseGate::CoveredNamespace, decision);
|
||||
decision == ScannerCacheReuseDecision::Eligible
|
||||
}
|
||||
|
||||
// None preserves work that cannot use sampling; false requests this cycle's rescan.
|
||||
fn sampled_namespace_reuse(&self, hash: &DataUsageHash, compacted: bool, forward_sweep: bool) -> Option<bool> {
|
||||
let decision = if self.scan_mode == HealScanMode::Deep {
|
||||
ScannerCacheReuseDecision::DeepScan
|
||||
} else if forward_sweep {
|
||||
ScannerCacheReuseDecision::ForwardSweep
|
||||
} else if self
|
||||
.old_cache
|
||||
.info
|
||||
.replication
|
||||
.as_ref()
|
||||
.is_some_and(|config| config.has_active_rules("", true))
|
||||
{
|
||||
ScannerCacheReuseDecision::ActiveReplication
|
||||
} else if compacted {
|
||||
ScannerCacheReuseDecision::CompactedParent
|
||||
} else if !self.old_cache.is_compacted(hash) {
|
||||
ScannerCacheReuseDecision::UncompactedChild
|
||||
} else if hash.mod_(self.old_cache.info.next_cycle as u32, data_usage_update_dir_cycles()) {
|
||||
ScannerCacheReuseDecision::SelectedForWalk
|
||||
} else {
|
||||
ScannerCacheReuseDecision::Eligible
|
||||
};
|
||||
global_metrics()
|
||||
.scan_observation()
|
||||
.record_cache_reuse_decision(ScannerCacheReuseGate::NamespaceSampling, decision);
|
||||
match decision {
|
||||
ScannerCacheReuseDecision::Eligible => Some(true),
|
||||
ScannerCacheReuseDecision::SelectedForWalk => Some(false),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn resume_subtree_has_failures(&self, folder: &str) -> bool {
|
||||
@@ -2077,7 +2137,11 @@ impl FolderScanner {
|
||||
// The caller stitches this cached child into its
|
||||
// parent; do not clone a growing child set into the
|
||||
// unused provisional accumulator as well.
|
||||
let started = Instant::now();
|
||||
self.new_cache.copy_with_children(&self.old_cache, &this_hash, &folder.parent);
|
||||
global_metrics()
|
||||
.scan_observation()
|
||||
.record_cache_reuse(ScannerCacheReuseKind::ClassifiedNamespace, started.elapsed());
|
||||
self.budget.record_entries_visited(pending_entry_progress);
|
||||
self.finish_raw_enumeration_parent(&folder.name);
|
||||
done_folder();
|
||||
@@ -2664,8 +2728,12 @@ impl FolderScanner {
|
||||
FolderScanMode::Namespace
|
||||
};
|
||||
if child_mode == FolderScanMode::Namespace && reuse_covered {
|
||||
let started = Instant::now();
|
||||
self.new_cache.copy_with_children(&self.old_cache, &h, &folder_item.parent);
|
||||
into.add_child(&h);
|
||||
global_metrics()
|
||||
.scan_observation()
|
||||
.record_cache_reuse(ScannerCacheReuseKind::CoveredNamespace, started.elapsed());
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -2705,22 +2773,15 @@ impl FolderScanner {
|
||||
}
|
||||
FolderScanSource::Existing => {
|
||||
// Usage sampling is not proof that a Deep check ran.
|
||||
if self.scan_mode != HealScanMode::Deep
|
||||
&& !forward_sweep
|
||||
&& !self
|
||||
.old_cache
|
||||
.info
|
||||
.replication
|
||||
.as_ref()
|
||||
.is_some_and(|config| config.has_active_rules("", true))
|
||||
&& !into.compacted
|
||||
&& self.old_cache.is_compacted(&h)
|
||||
{
|
||||
let next_cycle = self.old_cache.info.next_cycle as u32;
|
||||
if !h.mod_(next_cycle, data_usage_update_dir_cycles()) {
|
||||
if let Some(reuse) = self.sampled_namespace_reuse(&h, into.compacted, forward_sweep) {
|
||||
if reuse {
|
||||
if child_mode == FolderScanMode::Namespace {
|
||||
let started = Instant::now();
|
||||
self.new_cache.copy_with_children(&self.old_cache, &h, &folder_item.parent);
|
||||
into.add_child(&h);
|
||||
global_metrics()
|
||||
.scan_observation()
|
||||
.record_cache_reuse(ScannerCacheReuseKind::SampledNamespace, started.elapsed());
|
||||
self.record_scan_resume_hint(&folder_item.name);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -3103,6 +3103,112 @@ async fn test_scan_folder_non_erasure_metadata_keeps_namespace_descent() {
|
||||
assert_eq!(budget.reason(), Some(crate::scanner_budget::ScannerCycleBudgetReason::Directories));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn cache_reuse_gates_report_only_the_first_production_condition() {
|
||||
let (mut scanner, temp_dir) = build_test_scanner().await;
|
||||
let _guard = TestGuard::new(60, 100, &mut scanner, temp_dir);
|
||||
scanner.old_cache.info.name = "bucket".to_string();
|
||||
scanner.old_cache.replace("bucket", "", DataUsageEntry::default());
|
||||
scanner.old_cache.replace(
|
||||
"bucket/child",
|
||||
"bucket",
|
||||
DataUsageEntry {
|
||||
compacted: true,
|
||||
objects: 2,
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
let hash = hash_path("bucket/child");
|
||||
let folder = CachedFolder {
|
||||
name: "bucket".to_string(),
|
||||
parent: None,
|
||||
object_heal_prob_div: 1,
|
||||
};
|
||||
let child = CachedFolder {
|
||||
name: "bucket/child".to_string(),
|
||||
parent: Some(hash_path("bucket")),
|
||||
object_heal_prob_div: 1,
|
||||
};
|
||||
let abandoned = HashSet::from([hash.0.clone()]);
|
||||
let before = global_metrics().scan_observation().snapshot();
|
||||
|
||||
assert!(!scanner.should_reuse_clean_root_child(&folder, &DataUsageEntry::default(), &child, &hash, &abandoned));
|
||||
let missing_scope = global_metrics().scan_observation().snapshot();
|
||||
assert_eq!(
|
||||
missing_scope.reuse_decisions["clean_prefix"].not_evaluated["missing_scope"],
|
||||
before.reuse_decisions["clean_prefix"].not_evaluated["missing_scope"] + 1
|
||||
);
|
||||
scanner.prefix_scan_scope = ScannerBucketPrefixScanScope::from_dirty_top_level_entries(HashSet::from(["dirty".to_string()]));
|
||||
scanner.is_erasure_mode = true;
|
||||
assert!(!scanner.should_reuse_clean_root_child(&folder, &DataUsageEntry::default(), &child, &hash, &abandoned));
|
||||
let erasure = global_metrics().scan_observation().snapshot();
|
||||
assert_eq!(
|
||||
erasure.reuse_decisions["clean_prefix"].blocked["erasure_health_required"],
|
||||
before.reuse_decisions["clean_prefix"].blocked["erasure_health_required"] + 1
|
||||
);
|
||||
assert_eq!(erasure.erasure_clean_prefix_walks, before.erasure_clean_prefix_walks + 1);
|
||||
|
||||
assert!(!scanner.can_reuse_covered_namespace("bucket/child", true));
|
||||
scanner.old_cache.info.scan_progress = Some(crate::DataUsageScanProgress {
|
||||
started_plan: crate::DataUsageScanPlanDigest([1; 32]),
|
||||
requested_plan: crate::DataUsageScanPlanDigest([1; 32]),
|
||||
});
|
||||
assert!(!scanner.can_reuse_covered_namespace("bucket/child", true));
|
||||
assert!(!scanner.can_reuse_covered_namespace("bucket/child", false));
|
||||
scanner.resume_frontier = Some("bucket/child".to_string());
|
||||
scanner.resume_failed_paths = vec!["bucket/child/failed".to_string()];
|
||||
assert!(!scanner.can_reuse_covered_namespace("bucket/child", false));
|
||||
scanner.resume_failed_paths.clear();
|
||||
assert!(scanner.can_reuse_covered_namespace("bucket/child", false));
|
||||
let covered = global_metrics().scan_observation().snapshot();
|
||||
let reasons = &covered.reuse_decisions["covered_namespace"];
|
||||
let prior = &before.reuse_decisions["covered_namespace"];
|
||||
assert_eq!(reasons.not_evaluated["no_resume_sweep"], prior.not_evaluated["no_resume_sweep"] + 1);
|
||||
assert_eq!(reasons.blocked["compacted_parent"], prior.blocked["compacted_parent"] + 1);
|
||||
assert_eq!(reasons.blocked["missing_coverage"], prior.blocked["missing_coverage"] + 1);
|
||||
assert_eq!(reasons.blocked["cached_failures"], prior.blocked["cached_failures"] + 1);
|
||||
assert_eq!(reasons.eligible, prior.eligible + 1);
|
||||
|
||||
with_var(ENV_DATA_USAGE_UPDATE_DIR_CYCLES, Some("16"), || {
|
||||
scanner.scan_mode = HealScanMode::Deep;
|
||||
assert_eq!(scanner.sampled_namespace_reuse(&hash, true, true), None);
|
||||
scanner.scan_mode = HealScanMode::Normal;
|
||||
assert_eq!(scanner.sampled_namespace_reuse(&hash, true, true), None);
|
||||
let replication: ReplicationConfig = serde_json::from_value(serde_json::json!({
|
||||
"config": { "role": "", "rules": [{ "destination": { "bucket": "arn:test" }, "id": "status", "prefix": "", "priority": 1, "status": "Enabled" }] },
|
||||
"remotes": crate::storage_api::EcstoreBucketTargets::default(),
|
||||
})).expect("active replication fixture");
|
||||
scanner.old_cache.info.replication = Some(Arc::new(replication));
|
||||
assert_eq!(scanner.sampled_namespace_reuse(&hash, true, false), None);
|
||||
scanner.old_cache.info.replication = None;
|
||||
assert_eq!(scanner.sampled_namespace_reuse(&hash, true, false), None);
|
||||
let rescan_cycle = (0..16)
|
||||
.find(|cycle| hash.mod_(*cycle, 16))
|
||||
.expect("hash is visited in one sampling cycle");
|
||||
scanner.old_cache.info.next_cycle = u64::from(rescan_cycle);
|
||||
assert_eq!(scanner.sampled_namespace_reuse(&hash, false, false), Some(false));
|
||||
scanner.old_cache.info.next_cycle = u64::from((rescan_cycle + 1) % 16);
|
||||
assert_eq!(scanner.sampled_namespace_reuse(&hash, false, false), Some(true));
|
||||
});
|
||||
let after = global_metrics().scan_observation().snapshot();
|
||||
let reasons = &after.reuse_decisions["namespace_sampling"];
|
||||
let prior = &before.reuse_decisions["namespace_sampling"];
|
||||
for label in ["deep_scan", "forward_sweep", "active_replication"] {
|
||||
assert_eq!(
|
||||
reasons.not_evaluated[label],
|
||||
prior.not_evaluated[label] + 1,
|
||||
"first sampling condition {label}"
|
||||
);
|
||||
}
|
||||
assert_eq!(reasons.blocked["compacted_parent"], prior.blocked["compacted_parent"] + 1);
|
||||
assert_eq!(reasons.blocked["selected_for_walk"], prior.blocked["selected_for_walk"] + 1);
|
||||
assert_eq!(reasons.eligible, prior.eligible + 1);
|
||||
for (kind, reuse) in &after.cache_reuse {
|
||||
assert_eq!(reuse.count, before.cache_reuse[kind].count, "gate admission alone does not copy {kind}");
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn scoped_root_scan_reuses_clean_top_level_entries_and_rescans_dirty_entries() {
|
||||
@@ -3144,12 +3250,21 @@ async fn scoped_root_scan_reuses_clean_top_level_entries_and_rescans_dirty_entri
|
||||
parent: None,
|
||||
object_heal_prob_div: 1,
|
||||
};
|
||||
let before = global_metrics().scan_observation().snapshot();
|
||||
let mut root = DataUsageEntry::default();
|
||||
scanner
|
||||
.scan_folder(CancellationToken::new(), folder, &mut root)
|
||||
.await
|
||||
.expect("scoped root scan should finish successfully");
|
||||
|
||||
let observed = global_metrics().scan_observation().snapshot();
|
||||
assert_eq!(observed.cache_reuse["clean_prefix"].count, before.cache_reuse["clean_prefix"].count + 1);
|
||||
assert_eq!(observed.erasure_clean_prefix_walks, before.erasure_clean_prefix_walks);
|
||||
assert_eq!(
|
||||
observed.reuse_decisions["clean_prefix"].eligible,
|
||||
before.reuse_decisions["clean_prefix"].eligible + 1
|
||||
);
|
||||
|
||||
let clean = scanner
|
||||
.new_cache
|
||||
.size_recursive("bucket/clean")
|
||||
@@ -3167,6 +3282,78 @@ async fn scoped_root_scan_reuses_clean_top_level_entries_and_rescans_dirty_entri
|
||||
assert_eq!((bucket.size, bucket.objects), (17, 3));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn scoped_cache_observations_distinguish_checkpoint_and_periodic_sampling() {
|
||||
for covered in [true, false] {
|
||||
let (mut scanner, temp_dir) = build_test_scanner().await;
|
||||
let _guard = TestGuard::new(60, 100, &mut scanner, temp_dir.clone());
|
||||
tokio::fs::create_dir_all(temp_dir.join("bucket/child"))
|
||||
.await
|
||||
.expect("create cached namespace");
|
||||
scanner.old_cache.info.name = "bucket".to_string();
|
||||
scanner.new_cache.info.name = "bucket".to_string();
|
||||
scanner.old_cache.replace("bucket", "", DataUsageEntry::default());
|
||||
scanner.old_cache.replace(
|
||||
"bucket/child",
|
||||
"bucket",
|
||||
DataUsageEntry {
|
||||
size: 17,
|
||||
objects: 5,
|
||||
compacted: true,
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
let kind = if covered {
|
||||
scanner.old_cache.info.scan_progress = Some(crate::DataUsageScanProgress {
|
||||
started_plan: crate::DataUsageScanPlanDigest([1; 32]),
|
||||
requested_plan: crate::DataUsageScanPlanDigest([1; 32]),
|
||||
});
|
||||
scanner.resume_frontier = Some("bucket/child".to_string());
|
||||
"covered_namespace"
|
||||
} else {
|
||||
let hash = hash_path("bucket/child");
|
||||
let cycle = (0..data_usage_update_dir_cycles())
|
||||
.find(|cycle| !hash.mod_(*cycle, data_usage_update_dir_cycles()))
|
||||
.expect("a cycle samples other work");
|
||||
scanner.old_cache.info.next_cycle = u64::from(cycle);
|
||||
"sampled_namespace"
|
||||
};
|
||||
let before = global_metrics().scan_observation().snapshot();
|
||||
let mut root = DataUsageEntry::default();
|
||||
scanner
|
||||
.scan_folder(
|
||||
CancellationToken::new(),
|
||||
CachedFolder {
|
||||
name: "bucket".to_string(),
|
||||
parent: None,
|
||||
object_heal_prob_div: 1,
|
||||
},
|
||||
&mut root,
|
||||
)
|
||||
.await
|
||||
.expect("cached namespace reuses accounting");
|
||||
let summary = scanner.new_cache.size_recursive("bucket").expect("complete bucket totals");
|
||||
assert_eq!((summary.size, summary.objects), (17, 5));
|
||||
let after = global_metrics().scan_observation().snapshot();
|
||||
for label in [
|
||||
"clean_prefix",
|
||||
"covered_namespace",
|
||||
"sampled_namespace",
|
||||
"classified_namespace",
|
||||
] {
|
||||
assert_eq!(
|
||||
after.cache_reuse[label].count,
|
||||
before.cache_reuse[label].count + u64::from(label == kind),
|
||||
"reuse reason {label}"
|
||||
);
|
||||
}
|
||||
assert_eq!(after.metadata_reads.attempts, before.metadata_reads.attempts);
|
||||
let gate = if covered { "covered_namespace" } else { "namespace_sampling" };
|
||||
assert_eq!(after.reuse_decisions[gate].eligible, before.reuse_decisions[gate].eligible + 1);
|
||||
}
|
||||
}
|
||||
|
||||
async fn scan_hot_cold_segment_fixture(scoped: bool) -> (DataUsageEntry, Vec<String>) {
|
||||
let (mut scanner, temp_dir) = build_test_scanner().await;
|
||||
let visits = metadata_visits::observe(&mut scanner);
|
||||
@@ -3278,6 +3465,7 @@ async fn scoped_root_scan_preserves_erasure_health_walks() {
|
||||
parent: None,
|
||||
object_heal_prob_div: 1,
|
||||
};
|
||||
let before = global_metrics().scan_observation().snapshot();
|
||||
let mut root = DataUsageEntry::default();
|
||||
scanner
|
||||
.scan_folder(CancellationToken::new(), folder, &mut root)
|
||||
@@ -3289,6 +3477,9 @@ async fn scoped_root_scan_preserves_erasure_health_walks() {
|
||||
.size_recursive("bucket/clean")
|
||||
.expect("erasure scan should visit the clean entry");
|
||||
assert_eq!((clean.size, clean.objects), (0, 0));
|
||||
let observed = global_metrics().scan_observation().snapshot();
|
||||
assert_eq!(observed.erasure_clean_prefix_walks, before.erasure_clean_prefix_walks + 1);
|
||||
assert_eq!(observed.cache_reuse["clean_prefix"].count, before.cache_reuse["clean_prefix"].count);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
@@ -774,6 +774,7 @@ async fn erasure_candidate_covered_namespace_reuses_totals_after_classification(
|
||||
},
|
||||
);
|
||||
scanner.budget = budget.clone();
|
||||
let before = global_metrics().scan_observation().snapshot();
|
||||
scanner
|
||||
.scan_folder(
|
||||
budget.token(),
|
||||
@@ -790,6 +791,15 @@ async fn erasure_candidate_covered_namespace_reuses_totals_after_classification(
|
||||
assert_eq!(budget.progress(), (0, 1), "only the parent consumes namespace quota");
|
||||
assert_eq!(charged_ops(&root), 4, "list both directories once without re-reading covered metadata");
|
||||
assert_eq!(scanner.new_cache.find(&folder.name).expect("reused candidate").objects, 1);
|
||||
let observed = global_metrics().scan_observation().snapshot();
|
||||
assert_eq!(
|
||||
observed.cache_reuse["classified_namespace"].count,
|
||||
before.cache_reuse["classified_namespace"].count + 1
|
||||
);
|
||||
assert_eq!(
|
||||
observed.metadata_reads.attempts, before.metadata_reads.attempts,
|
||||
"classification retains the existing zero metadata read path"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -869,6 +879,7 @@ async fn erasure_candidate_preserves_compacted_namespace_sampling_after_classifi
|
||||
},
|
||||
);
|
||||
scanner.budget = budget.clone();
|
||||
let before = global_metrics().scan_observation().snapshot();
|
||||
scanner
|
||||
.scan_folder(
|
||||
budget.token(),
|
||||
@@ -889,6 +900,15 @@ async fn erasure_candidate_preserves_compacted_namespace_sampling_after_classifi
|
||||
"sampling still requires full current classification, without metadata re-read"
|
||||
);
|
||||
assert_eq!(scanner.new_cache.find(&folder.name).expect("sampled candidate").objects, 1);
|
||||
let observed = global_metrics().scan_observation().snapshot();
|
||||
assert_eq!(
|
||||
observed.cache_reuse["classified_namespace"].count,
|
||||
before.cache_reuse["classified_namespace"].count + 1
|
||||
);
|
||||
assert_eq!(
|
||||
observed.metadata_reads.attempts, before.metadata_reads.attempts,
|
||||
"classification retains the existing zero metadata read path"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
@@ -40,6 +40,7 @@ use rustfs_lock::{LockError, NamespaceLockGuard};
|
||||
use rustfs_scanner_metrics::metrics::{
|
||||
Metric, Metrics, emit_scan_bucket_drive_complete, emit_scan_bucket_drive_partial, global_metrics,
|
||||
};
|
||||
use rustfs_scanner_metrics::scan_observation::{PREFIX_REUSE_BLOCKERS, ScannerBaselineRejection, ScannerPrefixReuseStage};
|
||||
use rustfs_utils::path::path_join_buf;
|
||||
use sha2::{Digest as _, Sha256};
|
||||
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
|
||||
@@ -112,16 +113,7 @@ pub(crate) const SCANNER_SEGMENT_ACTIVATION_PROOF_INPUTS: [&str; 9] = [
|
||||
"durable_dirty_producer_journal",
|
||||
"ack_generation_guard",
|
||||
];
|
||||
pub(crate) const SCANNER_SEGMENT_ACTIVATION_FAIL_CLOSED_CHECKS: [&str; 8] = [
|
||||
"missing_producer_identity",
|
||||
"missing_durable_journal_replay",
|
||||
"restart_gap",
|
||||
"generation_gap",
|
||||
"overflow",
|
||||
"stale_ack_generation",
|
||||
"missing_cold_zero_walk_oracle",
|
||||
"distributed_without_peer_invalidation",
|
||||
];
|
||||
pub(crate) const SCANNER_SEGMENT_ACTIVATION_FAIL_CLOSED_CHECKS: [&str; 8] = PREFIX_REUSE_BLOCKERS;
|
||||
|
||||
pub type DirtyUsageBuckets = HashMap<String, u64>;
|
||||
|
||||
@@ -197,6 +189,7 @@ struct VerifiedRemoteDirtyUsage {
|
||||
}
|
||||
|
||||
struct ScannerBucketScopeResolutionResult {
|
||||
segment_reuse_activation_preflight: Option<ScannerSegmentReuseActivationPreflight>,
|
||||
scope: ScannerBucketScanScope,
|
||||
remote_dirty_usage_acknowledgements: Vec<crate::scanner::ScannerDirtyUsageAcknowledgement>,
|
||||
distributed_segment_invalidation_evidence: Option<DistributedSegmentInvalidationEvidence>,
|
||||
@@ -236,6 +229,13 @@ pub(crate) struct ScannerSegmentReuseActivationPreflight {
|
||||
}
|
||||
|
||||
impl ScannerSegmentReuseActivationPreflight {
|
||||
fn record_observation(&self, stage: ScannerPrefixReuseStage) {
|
||||
global_metrics().scan_observation().record_prefix_reuse(
|
||||
stage,
|
||||
self.scanner_segment_reuse_activated,
|
||||
&self.fail_closed_blockers,
|
||||
);
|
||||
}
|
||||
#[cfg(test)]
|
||||
pub(crate) fn fail_closed_blockers(&self) -> impl Iterator<Item = &'static str> + '_ {
|
||||
self.fail_closed_blockers.iter().filter_map(|blocker| *blocker)
|
||||
@@ -319,6 +319,7 @@ fn resolve_remote_dirty_usage_scope(
|
||||
baseline_proof: ScannerCacheBaselineProof<'_>,
|
||||
) -> ScannerBucketScopeResolutionResult {
|
||||
let default_result = |scope: ScannerBucketScanScope| ScannerBucketScopeResolutionResult {
|
||||
segment_reuse_activation_preflight: None,
|
||||
scope,
|
||||
remote_dirty_usage_acknowledgements: Vec::new(),
|
||||
distributed_segment_invalidation_evidence: None,
|
||||
@@ -410,6 +411,7 @@ fn resolve_remote_dirty_usage_scope(
|
||||
};
|
||||
|
||||
ScannerBucketScopeResolutionResult {
|
||||
segment_reuse_activation_preflight: Some(segment_reuse_activation_preflight),
|
||||
scope,
|
||||
remote_dirty_usage_acknowledgements: scoped_acknowledgements,
|
||||
distributed_segment_invalidation_evidence,
|
||||
@@ -427,43 +429,81 @@ fn complete_scanner_cache_snapshot_plan_digest(
|
||||
proof: ScannerCacheBaselineProof<'_>,
|
||||
expected_converged: Option<bool>,
|
||||
) -> Option<DataUsageScanPlanDigest> {
|
||||
validate_scanner_cache_snapshot_plan_digest(snapshot, proof, expected_converged).ok()
|
||||
}
|
||||
|
||||
fn validate_scanner_cache_snapshot_plan_digest(
|
||||
snapshot: &DataUsageInfo,
|
||||
proof: ScannerCacheBaselineProof<'_>,
|
||||
expected_converged: Option<bool>,
|
||||
) -> std::result::Result<DataUsageScanPlanDigest, ScannerBaselineRejection> {
|
||||
// The observational mark disqualifies only the authoritative slot: an
|
||||
// observed candidate carries it by construction.
|
||||
let marks_accepted = match expected_converged {
|
||||
Some(expected) => snapshot.usage_snapshot_converged == Some(expected),
|
||||
None => snapshot.usage_snapshot_converged.is_some() && !snapshot.usage_snapshot_observational,
|
||||
};
|
||||
if !snapshot.is_complete_bucket_usage_snapshot()
|
||||
|| snapshot.usage_snapshot_partial
|
||||
|| !marks_accepted
|
||||
|| snapshot.scanner_epoch != Some(proof.leader_epoch)
|
||||
|| snapshot.usage_snapshot_set_states.len() != proof.expected_sources.len()
|
||||
{
|
||||
return None;
|
||||
if !snapshot.is_complete_bucket_usage_snapshot() {
|
||||
return Err(ScannerBaselineRejection::Incomplete);
|
||||
}
|
||||
if snapshot.usage_snapshot_partial {
|
||||
return Err(ScannerBaselineRejection::Partial);
|
||||
}
|
||||
if !marks_accepted {
|
||||
return Err(ScannerBaselineRejection::Marks);
|
||||
}
|
||||
if snapshot.scanner_epoch != Some(proof.leader_epoch) {
|
||||
return Err(ScannerBaselineRejection::Epoch);
|
||||
}
|
||||
if snapshot.usage_snapshot_set_states.len() != proof.expected_sources.len() {
|
||||
return Err(ScannerBaselineRejection::SourceCount);
|
||||
}
|
||||
|
||||
// Completed maintenance also covers ordinary usage. Keep its exact stored
|
||||
// proof for cache reuse, and reject mixtures of different set work proofs.
|
||||
let baseline_plan_digest = DataUsageScanPlanDigest(snapshot.usage_snapshot_set_states.first()?.scan_plan_digest?);
|
||||
let baseline_plan_digest = DataUsageScanPlanDigest(
|
||||
snapshot
|
||||
.usage_snapshot_set_states
|
||||
.first()
|
||||
.and_then(|state| state.scan_plan_digest)
|
||||
.ok_or(ScannerBaselineRejection::PlanMissing)?,
|
||||
);
|
||||
if !scanner_plan_digest_is_work_variant(proof.scan_plan_digest, baseline_plan_digest) {
|
||||
return None;
|
||||
return Err(ScannerBaselineRejection::PlanMismatch);
|
||||
}
|
||||
let mut states = HashSet::with_capacity(snapshot.usage_snapshot_set_states.len());
|
||||
for state in &snapshot.usage_snapshot_set_states {
|
||||
let source = DataUsageCacheSource::new(usize::try_from(state.pool_index).ok()?, usize::try_from(state.set_index).ok()?);
|
||||
if !proof.expected_sources.contains(&source)
|
||||
|| !states.insert(source)
|
||||
|| !state.complete
|
||||
|| state.tombstone
|
||||
|| state.scanner_epoch != Some(proof.leader_epoch)
|
||||
|| state.scanner_cycle.is_none_or(|cycle| cycle > proof.want_cycle)
|
||||
|| state.scan_plan_digest != Some(baseline_plan_digest.0)
|
||||
{
|
||||
return None;
|
||||
let source = DataUsageCacheSource::new(
|
||||
usize::try_from(state.pool_index).map_err(|_| ScannerBaselineRejection::SourceIndex)?,
|
||||
usize::try_from(state.set_index).map_err(|_| ScannerBaselineRejection::SourceIndex)?,
|
||||
);
|
||||
if !proof.expected_sources.contains(&source) {
|
||||
return Err(ScannerBaselineRejection::SourceMembership);
|
||||
}
|
||||
if !states.insert(source) {
|
||||
return Err(ScannerBaselineRejection::SourceDuplicate);
|
||||
}
|
||||
if !state.complete {
|
||||
return Err(ScannerBaselineRejection::SetIncomplete);
|
||||
}
|
||||
if state.tombstone {
|
||||
return Err(ScannerBaselineRejection::SetTombstone);
|
||||
}
|
||||
if state.scanner_epoch != Some(proof.leader_epoch) {
|
||||
return Err(ScannerBaselineRejection::SetEpoch);
|
||||
}
|
||||
if state.scanner_cycle.is_none_or(|cycle| cycle > proof.want_cycle) {
|
||||
return Err(ScannerBaselineRejection::SetCycle);
|
||||
}
|
||||
if state.scan_plan_digest != Some(baseline_plan_digest.0) {
|
||||
return Err(ScannerBaselineRejection::SetPlan);
|
||||
}
|
||||
}
|
||||
|
||||
(states == *proof.expected_sources).then_some(baseline_plan_digest)
|
||||
if states != *proof.expected_sources {
|
||||
return Err(ScannerBaselineRejection::SourceCoverage);
|
||||
}
|
||||
Ok(baseline_plan_digest)
|
||||
}
|
||||
|
||||
/// Whether `digest` is `structural` under any work flag: the same bucket
|
||||
@@ -493,10 +533,35 @@ fn scanner_baseline_pending_buckets(proof: &ScannerCacheBaselineProof<'_>) -> Ve
|
||||
}
|
||||
|
||||
fn complete_scanner_cache_baseline_plan_digest(proof: ScannerCacheBaselineProof<'_>) -> Option<DataUsageScanPlanDigest> {
|
||||
let authoritative = serde_json::from_slice::<DataUsageInfo>(proof.authoritative_data?).ok()?;
|
||||
if let Some(validated_digest) = complete_scanner_cache_snapshot_plan_digest(&authoritative, proof, None) {
|
||||
return Some(validated_digest);
|
||||
}
|
||||
validate_scanner_cache_baseline_plan_digest(proof).ok()
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
struct ScannerBaselineValidationFailure {
|
||||
authoritative: ScannerBaselineRejection,
|
||||
observed: Option<ScannerBaselineRejection>,
|
||||
}
|
||||
|
||||
fn validate_scanner_cache_baseline_plan_digest(
|
||||
proof: ScannerCacheBaselineProof<'_>,
|
||||
) -> std::result::Result<DataUsageScanPlanDigest, ScannerBaselineValidationFailure> {
|
||||
let authoritative_error = |authoritative| ScannerBaselineValidationFailure {
|
||||
authoritative,
|
||||
observed: None,
|
||||
};
|
||||
let authoritative_data = proof
|
||||
.authoritative_data
|
||||
.ok_or_else(|| authoritative_error(ScannerBaselineRejection::Missing))?;
|
||||
let authoritative = serde_json::from_slice::<DataUsageInfo>(authoritative_data)
|
||||
.map_err(|_| authoritative_error(ScannerBaselineRejection::Decode))?;
|
||||
let authoritative_rejection = match validate_scanner_cache_snapshot_plan_digest(&authoritative, proof, None) {
|
||||
Ok(validated_digest) => return Ok(validated_digest),
|
||||
Err(reason) => reason,
|
||||
};
|
||||
let observed_error = |observed| ScannerBaselineValidationFailure {
|
||||
authoritative: authoritative_rejection,
|
||||
observed: Some(observed),
|
||||
};
|
||||
|
||||
// A complete but superseded observation may reuse its per-set cache only
|
||||
// when it was explicitly tied to the durable authoritative baseline. It
|
||||
@@ -506,14 +571,18 @@ fn complete_scanner_cache_baseline_plan_digest(proof: ScannerCacheBaselineProof<
|
||||
&& !authoritative.usage_snapshot_observational)
|
||||
|| crate::scanner::data_usage_info_is_bootstrap_pending(&authoritative);
|
||||
if !authoritative_has_identity {
|
||||
return None;
|
||||
return Err(observed_error(ScannerBaselineRejection::AuthoritativeIdentity));
|
||||
}
|
||||
let observed = serde_json::from_slice::<DataUsageInfo>(proof.observed_candidate_data?).ok()?;
|
||||
let observed_data = proof
|
||||
.observed_candidate_data
|
||||
.ok_or_else(|| observed_error(ScannerBaselineRejection::Missing))?;
|
||||
let observed =
|
||||
serde_json::from_slice::<DataUsageInfo>(observed_data).map_err(|_| observed_error(ScannerBaselineRejection::Decode))?;
|
||||
if !observed_data_usage_is_newer(&observed, &authoritative) {
|
||||
return None;
|
||||
return Err(observed_error(ScannerBaselineRejection::NotNewer));
|
||||
}
|
||||
|
||||
complete_scanner_cache_snapshot_plan_digest(&observed, proof, Some(false))
|
||||
validate_scanner_cache_snapshot_plan_digest(&observed, proof, Some(false)).map_err(observed_error)
|
||||
}
|
||||
|
||||
fn scanner_segment_reuse_baseline_producer_evidence(
|
||||
@@ -658,6 +727,12 @@ impl ScannerScopeFallbackReason {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct ScannerScopeFallback {
|
||||
reason: ScannerScopeFallbackReason,
|
||||
baseline_rejection: Option<ScannerBaselineValidationFailure>,
|
||||
}
|
||||
|
||||
/// Classify a resolved full-namespace scope against the preconditions
|
||||
/// `resolve_scanner_bucket_scan_scope` checks, in the same order. Returns
|
||||
/// `None` when the cycle was required to walk everything or was scoped.
|
||||
@@ -668,18 +743,24 @@ fn scanner_scope_fallback_reason(
|
||||
all_buckets: &[BucketInfo],
|
||||
baseline_proof: ScannerCacheBaselineProof<'_>,
|
||||
distributed: bool,
|
||||
) -> Option<ScannerScopeFallbackReason> {
|
||||
) -> Option<ScannerScopeFallback> {
|
||||
let fallback = |reason| {
|
||||
Some(ScannerScopeFallback {
|
||||
reason,
|
||||
baseline_rejection: None,
|
||||
})
|
||||
};
|
||||
if !scope_eligible || !resolved_scope.is_default() {
|
||||
return None;
|
||||
}
|
||||
if dirty_usage_snapshot.generation == u64::MAX {
|
||||
return Some(ScannerScopeFallbackReason::DirtyGenerationExhausted);
|
||||
return fallback(ScannerScopeFallbackReason::DirtyGenerationExhausted);
|
||||
}
|
||||
if !dirty_usage_snapshot.covers_listed_pending {
|
||||
return Some(ScannerScopeFallbackReason::DirtySnapshotIncomplete);
|
||||
return fallback(ScannerScopeFallbackReason::DirtySnapshotIncomplete);
|
||||
}
|
||||
if dirty_usage_snapshot.buckets.len() > crate::SCANNER_DIRTY_USAGE_SNAPSHOT_MAX_ENTRIES {
|
||||
return Some(ScannerScopeFallbackReason::DirtyBucketOverflow);
|
||||
return fallback(ScannerScopeFallbackReason::DirtyBucketOverflow);
|
||||
}
|
||||
// Peers may contribute dirty buckets the local snapshot does not hold.
|
||||
if !distributed {
|
||||
@@ -689,13 +770,19 @@ fn scanner_scope_fallback_reason(
|
||||
.keys()
|
||||
.any(|bucket| listed.contains(bucket.as_str()))
|
||||
{
|
||||
return Some(ScannerScopeFallbackReason::NoListedDirtyBuckets);
|
||||
return fallback(ScannerScopeFallbackReason::NoListedDirtyBuckets);
|
||||
}
|
||||
}
|
||||
if complete_scanner_cache_baseline_plan_digest(baseline_proof).is_none() {
|
||||
return Some(ScannerScopeFallbackReason::BaselineUnavailable);
|
||||
if let Err(failure) = validate_scanner_cache_baseline_plan_digest(baseline_proof) {
|
||||
return Some(ScannerScopeFallback {
|
||||
reason: ScannerScopeFallbackReason::BaselineUnavailable,
|
||||
baseline_rejection: Some(failure),
|
||||
});
|
||||
}
|
||||
distributed.then_some(ScannerScopeFallbackReason::PeerScopeUnavailable)
|
||||
distributed.then_some(ScannerScopeFallback {
|
||||
reason: ScannerScopeFallbackReason::PeerScopeUnavailable,
|
||||
baseline_rejection: None,
|
||||
})
|
||||
}
|
||||
|
||||
fn scanner_segment_reuse_activation_preflight() -> ScannerSegmentReuseActivationPreflight {
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
// limitations under the License.
|
||||
use super::*;
|
||||
/// scanner cache locks and the cache snapshot persist/publish path.
|
||||
use crate::data_usage_define::clone_cache_value;
|
||||
use crate::data_usage_define::{DataUsageCacheBaselineRevisions, clone_cache_value};
|
||||
use crate::{ScannerConfigObjectDelete, ScannerObjectIO};
|
||||
use rustfs_scanner_metrics::usage_candidate::{
|
||||
ScannerUsageCandidateKind as CandidateKind, ScannerUsageCandidateOutcome as CandidateOutcome,
|
||||
@@ -1570,7 +1570,7 @@ pub(super) async fn persist_and_publish_cache_snapshot(
|
||||
store: Arc<SetDisks>,
|
||||
updates: &mpsc::Sender<DataUsageCache>,
|
||||
mut cache_snapshot: DataUsageCache,
|
||||
initial_revisions: Option<&DataUsageCacheRevisions>,
|
||||
initial_revisions: Option<&DataUsageCacheBaselineRevisions>,
|
||||
cache_cycle_floor: &AtomicU64,
|
||||
expected_publication_epoch: u64,
|
||||
) -> Option<SystemTime> {
|
||||
@@ -1612,7 +1612,10 @@ pub(super) async fn persist_and_publish_cache_snapshot(
|
||||
};
|
||||
|
||||
let mut persisted = DataUsageCache::default();
|
||||
let revisions = match persisted.load_with_revisions(store.clone(), DATA_USAGE_CACHE_NAME).await {
|
||||
let current_baseline = match persisted
|
||||
.load_with_baseline_revisions(store.clone(), DATA_USAGE_CACHE_NAME)
|
||||
.await
|
||||
{
|
||||
Ok(revisions) => revisions,
|
||||
Err(err) => {
|
||||
error!(
|
||||
@@ -1634,6 +1637,7 @@ pub(super) async fn persist_and_publish_cache_snapshot(
|
||||
return None;
|
||||
}
|
||||
};
|
||||
let revisions = current_baseline.revisions();
|
||||
let Some(scan_plan_digest) = cache_snapshot.info.scan_plan_digest else {
|
||||
log_missing_set_snapshot_metadata(&store, &cache_snapshot.info, "plan_digest");
|
||||
return None;
|
||||
@@ -1697,9 +1701,9 @@ pub(super) async fn persist_and_publish_cache_snapshot(
|
||||
{
|
||||
cache_snapshot = persisted;
|
||||
} else {
|
||||
// A later execution may have completed while this scan was walking.
|
||||
// Only replace the cache revision from which this scan started.
|
||||
if initial_revisions != Some(&revisions) {
|
||||
// Another execution must not advance the baseline. A backup catching
|
||||
// up to the same decoded primary changes no baseline content.
|
||||
if !initial_revisions.is_some_and(|initial| current_baseline.permits_unchanged_baseline_since(initial)) {
|
||||
warn!(
|
||||
target: "rustfs::scanner::io",
|
||||
event = EVENT_SCANNER_CACHE_PERSIST_STATE,
|
||||
@@ -1741,7 +1745,7 @@ pub(super) async fn persist_and_publish_cache_snapshot(
|
||||
.save_with_revisions_for_epoch(
|
||||
store.clone(),
|
||||
DATA_USAGE_CACHE_NAME,
|
||||
&revisions,
|
||||
revisions,
|
||||
expected_publication_epoch,
|
||||
CancellationToken::new(),
|
||||
)
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
use super::*;
|
||||
/// ScannerIOCache implementation for SetDisks: bucket ordering, worker fan-out, merge, and publish.
|
||||
use crate::data_usage_define::clone_cache_value;
|
||||
use rustfs_scanner_metrics::scan_observation::{ScannerBucketReuseKind, ScannerScanObservation};
|
||||
|
||||
pub(super) async fn collect_completed_bucket_results(
|
||||
ctx: CancellationToken,
|
||||
@@ -142,6 +143,32 @@ pub(super) struct PreparedScopedSetScan {
|
||||
pub(super) buckets: Vec<BucketInfo>,
|
||||
pub(super) cache: DataUsageCache,
|
||||
pub(super) cold_bucket_reuse_proof: Option<ScopedColdBucketReuseProof>,
|
||||
cold_bucket_reuse_count: u64,
|
||||
cold_bucket_reuse_elapsed: Duration,
|
||||
}
|
||||
|
||||
impl PreparedScopedSetScan {
|
||||
pub(super) fn record_cold_bucket_reuse(&self, observation: &ScannerScanObservation) {
|
||||
observation.record_bucket_reuse(
|
||||
ScannerBucketReuseKind::ScopedColdBucket,
|
||||
self.cold_bucket_reuse_count,
|
||||
self.cold_bucket_reuse_elapsed,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Counts only roots delivered to the collector; cancellation or channel failure is not reuse.
|
||||
pub(super) async fn send_current_cache_root_entry(
|
||||
bucket_result_tx: &mpsc::Sender<DataUsageEntryInfo>,
|
||||
root: DataUsageEntryInfo,
|
||||
cache: &DataUsageCache,
|
||||
pending_maintenance_work: &AtomicBool,
|
||||
observation: &ScannerScanObservation,
|
||||
) -> std::result::Result<(), ScannerError> {
|
||||
let started = Instant::now();
|
||||
send_cache_root_entry(bucket_result_tx, root, cache, pending_maintenance_work).await?;
|
||||
observation.record_bucket_reuse(ScannerBucketReuseKind::CurrentRoot, 1, started.elapsed());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) struct ScopedColdBucketReuseProof {
|
||||
@@ -225,6 +252,8 @@ pub(super) fn prepare_scoped_set_scan(
|
||||
};
|
||||
cache.replace(DATA_USAGE_ROOT, "", DataUsageEntry::default());
|
||||
let mut current_bucket_names = HashSet::with_capacity(all_buckets.len());
|
||||
let mut cold_bucket_reuse_count = 0u64;
|
||||
let mut cold_bucket_reuse_elapsed = Duration::ZERO;
|
||||
for bucket in all_buckets {
|
||||
if !current_bucket_names.insert(bucket.name.as_str()) {
|
||||
return None;
|
||||
@@ -233,11 +262,14 @@ pub(super) fn prepare_scoped_set_scan(
|
||||
cache.info.scan_bucket_value_origins.remove(&bucket.name);
|
||||
cache.replace(&bucket.name, DATA_USAGE_ROOT, DataUsageEntry::default());
|
||||
} else {
|
||||
let started = Instant::now();
|
||||
cache.copy_with_children(
|
||||
old_cache,
|
||||
&rustfs_data_usage::hash_path(&bucket.name),
|
||||
&Some(rustfs_data_usage::hash_path(DATA_USAGE_ROOT)),
|
||||
);
|
||||
cold_bucket_reuse_count = cold_bucket_reuse_count.saturating_add(1);
|
||||
cold_bucket_reuse_elapsed = cold_bucket_reuse_elapsed.saturating_add(started.elapsed());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -249,6 +281,8 @@ pub(super) fn prepare_scoped_set_scan(
|
||||
.collect(),
|
||||
cache,
|
||||
cold_bucket_reuse_proof,
|
||||
cold_bucket_reuse_count,
|
||||
cold_bucket_reuse_elapsed,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -426,7 +460,10 @@ impl ScannerIOCache for SetDisks {
|
||||
.ok_or_else(|| StorageError::other("scanner cache publication is blocked by data movement"))?,
|
||||
};
|
||||
let mut old_cache = DataUsageCache::default();
|
||||
let initial_revisions = match old_cache.load_with_revisions(self.clone(), DATA_USAGE_CACHE_NAME).await {
|
||||
let initial_revisions = match old_cache
|
||||
.load_with_baseline_revisions(self.clone(), DATA_USAGE_CACHE_NAME)
|
||||
.await
|
||||
{
|
||||
Ok(revisions) => Some(revisions),
|
||||
Err(e) => {
|
||||
warn!(
|
||||
@@ -556,6 +593,8 @@ impl ScannerIOCache for SetDisks {
|
||||
})
|
||||
});
|
||||
let mut scoped_cache = scoped_scan.map(|mut prepared| {
|
||||
// The copies are completed work even if this turn later fails publication.
|
||||
prepared.record_cold_bucket_reuse(global_metrics().scan_observation());
|
||||
buckets = prepared.buckets;
|
||||
prepared.cache.info.scan_coverage_digest = Some(bucket_coverage_digest);
|
||||
prepared.cache
|
||||
@@ -1560,8 +1599,14 @@ impl ScannerIOCache for SetDisks {
|
||||
}
|
||||
record_completed_usage_watch(&usage_watch_completions_clone, source, &bucket.name, bucket_usage_watch.as_ref()).await;
|
||||
if let Err(e) =
|
||||
send_cache_root_entry(&bucket_result_tx_clone, *root, &cache, &pending_maintenance_work_clone)
|
||||
.await
|
||||
send_current_cache_root_entry(
|
||||
&bucket_result_tx_clone,
|
||||
*root,
|
||||
&cache,
|
||||
&pending_maintenance_work_clone,
|
||||
global_metrics().scan_observation(),
|
||||
)
|
||||
.await
|
||||
{
|
||||
record_failed_dirty_bucket(&failed_dirty_buckets_clone, &bucket.name).await;
|
||||
error!(
|
||||
|
||||
@@ -357,6 +357,7 @@ where
|
||||
S: ScannerStorage,
|
||||
{
|
||||
let default_result = |scope: ScannerBucketScanScope| ScannerBucketScopeResolutionResult {
|
||||
segment_reuse_activation_preflight: None,
|
||||
scope,
|
||||
remote_dirty_usage_acknowledgements: Vec::new(),
|
||||
distributed_segment_invalidation_evidence: None,
|
||||
@@ -506,7 +507,7 @@ where
|
||||
distributed,
|
||||
resolution.baseline_proof,
|
||||
);
|
||||
default_result(scoped_scan_scope_from_dirty_buckets(
|
||||
let scope = scoped_scan_scope_from_dirty_buckets(
|
||||
resolution.requested_scope,
|
||||
dirty_buckets,
|
||||
(!distributed).then_some(resolution.dirty_usage_snapshot.scopes.as_ref()),
|
||||
@@ -514,7 +515,11 @@ where
|
||||
segment_reuse_activation_preflight.scanner_segment_reuse_activated,
|
||||
resolution.all_buckets,
|
||||
resolution.baseline_proof,
|
||||
))
|
||||
);
|
||||
ScannerBucketScopeResolutionResult {
|
||||
segment_reuse_activation_preflight: Some(segment_reuse_activation_preflight),
|
||||
..default_result(scope)
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs once between the bucket listing and the dirty-usage snapshot, the
|
||||
@@ -775,6 +780,9 @@ where
|
||||
},
|
||||
)
|
||||
.await;
|
||||
if let Some(preflight) = scope_resolution.segment_reuse_activation_preflight {
|
||||
preflight.record_observation(ScannerPrefixReuseStage::ScopeResolution);
|
||||
}
|
||||
let remote_dirty_usage_acknowledgements = scope_resolution.remote_dirty_usage_acknowledgements;
|
||||
let distributed_segment_invalidation_evidence = scope_resolution.distributed_segment_invalidation_evidence;
|
||||
let scan_scope = scope_resolution.scope;
|
||||
@@ -786,8 +794,14 @@ where
|
||||
baseline_proof,
|
||||
distributed,
|
||||
);
|
||||
if let Some(reason) = scope_fallback {
|
||||
if let Some(fallback) = scope_fallback {
|
||||
let reason = fallback.reason;
|
||||
global_metrics().record_scanner_scope_fallback(reason.as_str());
|
||||
if let Some(failure) = fallback.baseline_rejection {
|
||||
global_metrics()
|
||||
.scan_observation()
|
||||
.record_baseline_rejection(failure.authoritative, failure.observed);
|
||||
}
|
||||
tracing::info!(
|
||||
target: "rustfs::scanner::io",
|
||||
event = EVENT_SCANNER_SET_STATE,
|
||||
@@ -801,6 +815,7 @@ where
|
||||
"Scanner cycle walks every bucket because its dirty-bucket scope was not admitted"
|
||||
);
|
||||
}
|
||||
let scope_fallback = scope_fallback.map(|fallback| fallback.reason);
|
||||
let segment_invalidation_proof = dirty_usage_producer_evidence(&dirty_usage_snapshot).segment_invalidation_proof();
|
||||
#[cfg(test)]
|
||||
if let Some(observer) = resolved_scope_observer {
|
||||
@@ -877,6 +892,7 @@ where
|
||||
None,
|
||||
false,
|
||||
);
|
||||
segment_reuse_activation_preflight.record_observation(ScannerPrefixReuseStage::CycleCompletion);
|
||||
return Ok(ScannerCycleResult::new(status, dirty_usage_clear)
|
||||
.with_publication_epoch(publication_epoch)
|
||||
.with_activity_digest(activity_digest)
|
||||
@@ -1224,6 +1240,7 @@ where
|
||||
distributed_segment_invalidation_evidence,
|
||||
cold_zero_walk_oracle,
|
||||
);
|
||||
segment_reuse_activation_preflight.record_observation(ScannerPrefixReuseStage::CycleCompletion);
|
||||
let pending_maintenance_work = pending_maintenance_work_for_cycle(&pending_maintenance_work, &results);
|
||||
let observed_cycle_floor = cache_cycle_floor.load(Ordering::Acquire);
|
||||
let required_cycle_floor = (observed_cycle_floor > want_cycle).then_some(observed_cycle_floor);
|
||||
|
||||
@@ -15,6 +15,7 @@ use super::*;
|
||||
use crate::UNKNOWN_TIER;
|
||||
/// ScannerIODisk implementation for Disk: get_size and the per-disk bucket scan.
|
||||
use crate::data_usage_define::clone_cache_value;
|
||||
use rustfs_scanner_metrics::scan_observation::ScannerMetadataReadOutcome;
|
||||
|
||||
///
|
||||
/// Seed [`SizeSummary::tier_stats`] from the cached tier-name list.
|
||||
@@ -62,8 +63,20 @@ impl ScannerIODisk for Disk {
|
||||
|
||||
ensure_scanner_node_operation_active(cancellation.as_ref())?;
|
||||
let metadata_object_path = item.object_path();
|
||||
let metadata_result =
|
||||
crate::workload_admission::with_scanner_metadata_io(self.read_metadata(&item.bucket, &metadata_object_path)).await;
|
||||
let metadata_result = crate::workload_admission::with_scanner_metadata_io(async {
|
||||
let read = global_metrics().start_scanner_metadata_read();
|
||||
let result = self.read_metadata(&item.bucket, &metadata_object_path).await;
|
||||
let (outcome, bytes) = match &result {
|
||||
Ok(data) => (ScannerMetadataReadOutcome::Success, u64::try_from(data.len()).unwrap_or(u64::MAX)),
|
||||
Err(e) if DiskError::is_err_object_not_found(e) || DiskError::is_err_version_not_found(e) => {
|
||||
(ScannerMetadataReadOutcome::NotFound, 0)
|
||||
}
|
||||
Err(_) => (ScannerMetadataReadOutcome::Error, 0),
|
||||
};
|
||||
read.finish(outcome, bytes);
|
||||
result
|
||||
})
|
||||
.await;
|
||||
ensure_scanner_node_operation_active(cancellation.as_ref())?;
|
||||
let data = match metadata_result {
|
||||
Ok(data) => data,
|
||||
|
||||
@@ -27,10 +27,10 @@ use crate::storage_api::owner::{
|
||||
};
|
||||
use crate::storage_api::scan::{BucketOperations as _, DeleteBucketOptions, MakeBucketOptions, ObjectIO as _};
|
||||
use crate::{
|
||||
DATA_USAGE_BLOOM_NAME_PATH, DiskOption, ECStore, Endpoint, EndpointServerPools, Endpoints, InstanceContext, PoolEndpoints,
|
||||
ScannerObjectOptions, ScannerPutObjReader, UNKNOWN_TIER, init_bucket_metadata_sys_for_scanner_tests,
|
||||
init_ecstore_config_for_scanner_tests, init_local_disks_with_instance_ctx, new_disk, path2_bucket_object_with_base_path,
|
||||
save_config,
|
||||
BUCKET_META_PREFIX, DATA_USAGE_BLOOM_NAME_PATH, DiskOption, ECStore, Endpoint, EndpointServerPools, Endpoints,
|
||||
InstanceContext, PoolEndpoints, ScannerObjectOptions, ScannerPutObjReader, UNKNOWN_TIER,
|
||||
init_bucket_metadata_sys_for_scanner_tests, init_ecstore_config_for_scanner_tests, init_local_disks_with_instance_ctx,
|
||||
new_disk, path2_bucket_object_with_base_path, save_config,
|
||||
};
|
||||
use rustfs_concurrency::{
|
||||
AdmissionState, WorkloadAdmissionRegistrySnapshot, WorkloadAdmissionSnapshot, WorkloadAdmissionSnapshotProvider,
|
||||
@@ -104,6 +104,7 @@ fn scanner_activity_preflight_defers_a_temporarily_offline_peer() {
|
||||
|
||||
#[test]
|
||||
fn scanner_segment_reuse_activation_preflight_reports_release_gate_inputs() {
|
||||
let before = global_metrics().scan_observation().snapshot();
|
||||
let preflight = scanner_segment_reuse_activation_preflight();
|
||||
|
||||
assert!(preflight.production_activation);
|
||||
@@ -124,6 +125,9 @@ fn scanner_segment_reuse_activation_preflight_reports_release_gate_inputs() {
|
||||
"distributed_without_peer_invalidation",
|
||||
]
|
||||
);
|
||||
let after = global_metrics().scan_observation().snapshot();
|
||||
assert_eq!(after.prefix_scope_resolution.evaluated, before.prefix_scope_resolution.evaluated);
|
||||
assert_eq!(after.prefix_cycle_completion.evaluated, before.prefix_cycle_completion.evaluated);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -2487,6 +2491,260 @@ async fn set_cache_send_failure_logs_receiver_and_snapshot_without_changing_retu
|
||||
assert_set_cache_context(set_cache_event(&events, "publish_failed"), Some((0, 0)));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn set_snapshot_accepts_backup_catchup_to_the_same_decoded_primary() {
|
||||
use crate::data_usage_define::read_config_with_revision;
|
||||
|
||||
let (_temp_dir, store, cache, epoch) = set_cache_log_fixture().await;
|
||||
let set = Arc::clone(&store.pools[0].disk_set[0]);
|
||||
let primary_path = path_join_buf(&[BUCKET_META_PREFIX, DATA_USAGE_CACHE_NAME]);
|
||||
let backup_path = format!("{primary_path}.bkp");
|
||||
let (primary_body, _) = read_config_with_revision(Arc::clone(&set), &primary_path)
|
||||
.await
|
||||
.expect("read decoded primary bytes");
|
||||
let primary_body = primary_body.expect("primary exists");
|
||||
let mut older = cache.clone();
|
||||
older.info.scan_execution_digest = Some(DataUsageScanPlanDigest([8; 32]));
|
||||
save_config(Arc::clone(&set), &backup_path, older.marshal_msg().expect("encode older backup"))
|
||||
.await
|
||||
.expect("seed a lagging backup");
|
||||
let mut loaded = DataUsageCache::default();
|
||||
let initial = loaded
|
||||
.load_with_baseline_revisions(Arc::clone(&set), DATA_USAGE_CACHE_NAME)
|
||||
.await
|
||||
.expect("capture primary with lagging backup");
|
||||
save_config(Arc::clone(&set), &backup_path, primary_body)
|
||||
.await
|
||||
.expect("backup catches up without changing primary");
|
||||
let current = loaded
|
||||
.load_with_baseline_revisions(Arc::clone(&set), DATA_USAGE_CACHE_NAME)
|
||||
.await
|
||||
.expect("capture the reconciled backup");
|
||||
assert_ne!(initial.revisions(), current.revisions());
|
||||
assert!(current.permits_unchanged_baseline_since(&initial));
|
||||
|
||||
let mut candidate = cache;
|
||||
candidate.info.next_cycle += 1;
|
||||
candidate.info.scan_execution_digest = Some(DataUsageScanPlanDigest([3; 32]));
|
||||
candidate.replace(
|
||||
"photos",
|
||||
DATA_USAGE_ROOT,
|
||||
DataUsageEntry {
|
||||
size: 20,
|
||||
objects: 1,
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
let cycle = candidate.info.next_cycle;
|
||||
let cycle_floor = AtomicU64::new(cycle);
|
||||
let (tx, mut rx) = mpsc::channel(1);
|
||||
assert!(
|
||||
persist_and_publish_cache_snapshot(Arc::clone(&set), &tx, candidate, Some(&initial), &cycle_floor, epoch)
|
||||
.await
|
||||
.is_some(),
|
||||
"content-preserving backup catchup must not veto the new snapshot"
|
||||
);
|
||||
let published = rx.try_recv().expect("new snapshot reached the collector");
|
||||
assert_eq!(published.info.next_cycle, cycle);
|
||||
assert_eq!(published.find("photos").expect("new bucket entry").size, 20);
|
||||
let (primary, primary_revision) = read_config_with_revision(Arc::clone(&set), &primary_path)
|
||||
.await
|
||||
.expect("read new primary");
|
||||
let (backup, backup_revision) = read_config_with_revision(set, &backup_path).await.expect("read new backup");
|
||||
assert_eq!(primary, backup, "backup CAS must use the reconciled current revision");
|
||||
assert_eq!(primary_revision, backup_revision);
|
||||
let persisted = DataUsageCache::unmarshal(&primary.expect("new primary body")).expect("decode new primary");
|
||||
assert_eq!(persisted.info.next_cycle, cycle);
|
||||
assert_eq!(persisted.find("photos").expect("persisted new entry").size, 20);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn set_snapshot_backup_catchup_keeps_revision_provenance_and_epoch_fences() {
|
||||
use crate::data_usage_define::read_config_with_revision;
|
||||
|
||||
for case in ["different_backup", "different_primary", "corrupt_primary", "changed_epoch"] {
|
||||
let (_temp_dir, store, cache, epoch) = set_cache_log_fixture().await;
|
||||
let set = Arc::clone(&store.pools[0].disk_set[0]);
|
||||
let primary_path = path_join_buf(&[BUCKET_META_PREFIX, DATA_USAGE_CACHE_NAME]);
|
||||
let backup_path = format!("{primary_path}.bkp");
|
||||
let (primary_body, _) = read_config_with_revision(Arc::clone(&set), &primary_path)
|
||||
.await
|
||||
.expect("read original primary");
|
||||
let primary_body = primary_body.expect("original primary exists");
|
||||
let mut older = cache.clone();
|
||||
older.info.scan_execution_digest = Some(DataUsageScanPlanDigest([8; 32]));
|
||||
save_config(Arc::clone(&set), &backup_path, older.marshal_msg().expect("encode lagging backup"))
|
||||
.await
|
||||
.expect("seed lagging backup");
|
||||
if case == "corrupt_primary" {
|
||||
save_config(Arc::clone(&set), &primary_path, b"corrupt-primary".to_vec())
|
||||
.await
|
||||
.expect("inject primary corruption before the initial load");
|
||||
}
|
||||
let mut loaded = DataUsageCache::default();
|
||||
let initial = loaded
|
||||
.load_with_baseline_revisions(Arc::clone(&set), DATA_USAGE_CACHE_NAME)
|
||||
.await
|
||||
.expect("capture original baseline source and revisions");
|
||||
let mut replacement = cache.clone();
|
||||
replacement.info.scan_execution_digest = Some(DataUsageScanPlanDigest([9; 32]));
|
||||
match case {
|
||||
"different_backup" => {
|
||||
save_config(Arc::clone(&set), &backup_path, replacement.marshal_msg().expect("encode changed backup"))
|
||||
.await
|
||||
.expect("backup changes to different contents");
|
||||
}
|
||||
"different_primary" => {
|
||||
let body = replacement.marshal_msg().expect("encode changed primary");
|
||||
save_config(Arc::clone(&set), &primary_path, body.clone())
|
||||
.await
|
||||
.expect("primary advances");
|
||||
save_config(Arc::clone(&set), &backup_path, body)
|
||||
.await
|
||||
.expect("backup follows changed primary");
|
||||
}
|
||||
"corrupt_primary" => {
|
||||
save_config(Arc::clone(&set), &backup_path, b"corrupt-primary".to_vec())
|
||||
.await
|
||||
.expect("backup matches the corrupt primary ETag without a decoded primary proof");
|
||||
}
|
||||
"changed_epoch" => {
|
||||
save_config(Arc::clone(&set), &backup_path, primary_body)
|
||||
.await
|
||||
.expect("backup catches up before epoch refusal");
|
||||
}
|
||||
_ => unreachable!("fixed test cases"),
|
||||
}
|
||||
let before_primary = read_config_with_revision(Arc::clone(&set), &primary_path)
|
||||
.await
|
||||
.expect("current primary");
|
||||
let before_backup = read_config_with_revision(Arc::clone(&set), &backup_path)
|
||||
.await
|
||||
.expect("current backup");
|
||||
let mut candidate = cache;
|
||||
candidate.info.scan_execution_digest = Some(DataUsageScanPlanDigest([3; 32]));
|
||||
let cycle_floor = AtomicU64::new(candidate.info.next_cycle);
|
||||
let requested_epoch = if case == "changed_epoch" {
|
||||
epoch.checked_add(1).expect("fixture epoch can advance")
|
||||
} else {
|
||||
epoch
|
||||
};
|
||||
let (tx, mut rx) = mpsc::channel(1);
|
||||
let (result, events) = capture_set_cache_logs(persist_and_publish_cache_snapshot(
|
||||
Arc::clone(&set),
|
||||
&tx,
|
||||
candidate,
|
||||
Some(&initial),
|
||||
&cycle_floor,
|
||||
requested_epoch,
|
||||
))
|
||||
.await;
|
||||
assert!(result.is_none(), "{case} must still refuse publication");
|
||||
assert!(
|
||||
matches!(rx.try_recv(), Err(mpsc::error::TryRecvError::Empty)),
|
||||
"{case} forwards no snapshot"
|
||||
);
|
||||
let expected_state = if case == "changed_epoch" {
|
||||
"save_failed"
|
||||
} else {
|
||||
"scan_baseline_revision_changed"
|
||||
};
|
||||
assert_set_cache_context(set_cache_event(&events, expected_state), Some((0, 0)));
|
||||
assert_eq!(
|
||||
read_config_with_revision(Arc::clone(&set), &primary_path)
|
||||
.await
|
||||
.expect("protected primary"),
|
||||
before_primary,
|
||||
"{case} does not overwrite primary"
|
||||
);
|
||||
assert_eq!(
|
||||
read_config_with_revision(set, &backup_path).await.expect("protected backup"),
|
||||
before_backup,
|
||||
"{case} does not overwrite backup"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn set_cache_cas_preserves_updates_after_backup_catchup_validation() {
|
||||
use crate::data_usage_define::read_config_with_revision;
|
||||
|
||||
for changed_copy in ["primary", "backup"] {
|
||||
let (_temp_dir, store, cache, epoch) = set_cache_log_fixture().await;
|
||||
let set = Arc::clone(&store.pools[0].disk_set[0]);
|
||||
let primary_path = path_join_buf(&[BUCKET_META_PREFIX, DATA_USAGE_CACHE_NAME]);
|
||||
let backup_path = format!("{primary_path}.bkp");
|
||||
let (primary_body, _) = read_config_with_revision(Arc::clone(&set), &primary_path)
|
||||
.await
|
||||
.expect("read initial primary");
|
||||
let mut lagging = cache.clone();
|
||||
lagging.info.scan_execution_digest = Some(DataUsageScanPlanDigest([8; 32]));
|
||||
save_config(Arc::clone(&set), &backup_path, lagging.marshal_msg().expect("lagging backup bytes"))
|
||||
.await
|
||||
.expect("seed backup lag");
|
||||
let mut loaded = DataUsageCache::default();
|
||||
let initial = loaded
|
||||
.load_with_baseline_revisions(Arc::clone(&set), DATA_USAGE_CACHE_NAME)
|
||||
.await
|
||||
.expect("read initial baseline");
|
||||
save_config(Arc::clone(&set), &backup_path, primary_body.expect("initial primary exists"))
|
||||
.await
|
||||
.expect("finish backup catchup");
|
||||
let current = loaded
|
||||
.load_with_baseline_revisions(Arc::clone(&set), DATA_USAGE_CACHE_NAME)
|
||||
.await
|
||||
.expect("read reconciled baseline");
|
||||
assert!(current.permits_unchanged_baseline_since(&initial));
|
||||
let changed_path = if changed_copy == "primary" {
|
||||
&primary_path
|
||||
} else {
|
||||
&backup_path
|
||||
};
|
||||
let mut concurrent = cache.clone();
|
||||
concurrent.info.scan_execution_digest = Some(DataUsageScanPlanDigest([9; 32]));
|
||||
let concurrent_body = concurrent.marshal_msg().expect("encode concurrent update");
|
||||
save_config(Arc::clone(&set), changed_path, concurrent_body.clone())
|
||||
.await
|
||||
.expect("copy changes after the baseline validation");
|
||||
let mut candidate = cache;
|
||||
candidate.info.next_cycle += 1;
|
||||
candidate.info.scan_execution_digest = Some(DataUsageScanPlanDigest([3; 32]));
|
||||
let result = candidate
|
||||
.save_with_revisions_for_epoch(
|
||||
Arc::clone(&set),
|
||||
DATA_USAGE_CACHE_NAME,
|
||||
current.revisions(),
|
||||
epoch,
|
||||
CancellationToken::new(),
|
||||
)
|
||||
.await;
|
||||
if changed_copy == "primary" {
|
||||
assert!(result.is_err(), "primary CAS refuses the intervening update");
|
||||
} else {
|
||||
result.expect("primary remains durable when the best-effort backup CAS refuses an update");
|
||||
}
|
||||
let (protected, _) = read_config_with_revision(Arc::clone(&set), changed_path)
|
||||
.await
|
||||
.expect("read protected concurrent copy");
|
||||
assert_eq!(
|
||||
protected,
|
||||
Some(concurrent_body),
|
||||
"{changed_copy} CAS must not overwrite the concurrent update"
|
||||
);
|
||||
let (primary, _) = read_config_with_revision(set, &primary_path)
|
||||
.await
|
||||
.expect("read resulting primary");
|
||||
let primary = DataUsageCache::unmarshal(&primary.expect("primary exists")).expect("primary remains decodable");
|
||||
assert_eq!(
|
||||
primary.info.scan_execution_digest,
|
||||
Some(DataUsageScanPlanDigest([if changed_copy == "primary" { 9 } else { 3 }; 32])),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn set_snapshot_reuse_requires_execution_identity_and_fences_stale_writers() {
|
||||
@@ -2501,7 +2759,7 @@ async fn set_snapshot_reuse_requires_execution_identity_and_fences_stale_writers
|
||||
.expect("seed legacy set cache");
|
||||
let mut persisted = DataUsageCache::default();
|
||||
let initial = persisted
|
||||
.load_with_revisions(Arc::clone(&set), DATA_USAGE_CACHE_NAME)
|
||||
.load_with_baseline_revisions(Arc::clone(&set), DATA_USAGE_CACHE_NAME)
|
||||
.await
|
||||
.expect("capture the shared starting revision");
|
||||
let mut fresh = legacy.clone();
|
||||
@@ -2527,7 +2785,7 @@ async fn set_snapshot_reuse_requires_execution_identity_and_fences_stale_writers
|
||||
assert_eq!(published.find("photos").expect("published bucket").size, 20);
|
||||
assert_eq!(published.info.scan_execution_digest, fresh.info.scan_execution_digest);
|
||||
let current = persisted
|
||||
.load_with_revisions(Arc::clone(&set), DATA_USAGE_CACHE_NAME)
|
||||
.load_with_baseline_revisions(Arc::clone(&set), DATA_USAGE_CACHE_NAME)
|
||||
.await
|
||||
.expect("capture the current revision for the unidentified execution");
|
||||
|
||||
@@ -2819,6 +3077,130 @@ fn scoped_scan_requires_a_complete_authoritative_baseline_with_exact_set_provena
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scoped_scan_baseline_rejections_preserve_the_existing_first_failed_check() {
|
||||
let source = DataUsageCacheSource::new(1, 2);
|
||||
let expected_sources = HashSet::from([source]);
|
||||
let digest = DataUsageScanPlanDigest([9; 32]);
|
||||
let baseline = complete_usage_baseline(source, digest, 7, 11);
|
||||
let snapshot: DataUsageInfo = serde_json::from_slice(&baseline).expect("decode complete baseline");
|
||||
let proof = ScannerCacheBaselineProof {
|
||||
authoritative_data: Some(&baseline),
|
||||
observed_candidate_data: None,
|
||||
expected_sources: &expected_sources,
|
||||
leader_epoch: 11,
|
||||
want_cycle: 8,
|
||||
scan_plan_digest: digest,
|
||||
};
|
||||
type RejectionCase = (ScannerBaselineRejection, fn(&mut DataUsageInfo));
|
||||
let cases: [RejectionCase; 13] = [
|
||||
(ScannerBaselineRejection::Incomplete, |s| s.usage_snapshot_complete = false),
|
||||
(ScannerBaselineRejection::Partial, |s| s.usage_snapshot_partial = true),
|
||||
(ScannerBaselineRejection::Marks, |s| s.usage_snapshot_converged = None),
|
||||
(ScannerBaselineRejection::Epoch, |s| s.scanner_epoch = Some(10)),
|
||||
(ScannerBaselineRejection::SourceCount, |s| s.usage_snapshot_set_states.clear()),
|
||||
(ScannerBaselineRejection::PlanMissing, |s| {
|
||||
s.usage_snapshot_set_states[0].scan_plan_digest = None
|
||||
}),
|
||||
(ScannerBaselineRejection::PlanMismatch, |s| {
|
||||
s.usage_snapshot_set_states[0].scan_plan_digest = Some([8; 32])
|
||||
}),
|
||||
(ScannerBaselineRejection::SourceMembership, |s| {
|
||||
s.usage_snapshot_set_states[0].set_index = 3
|
||||
}),
|
||||
(ScannerBaselineRejection::SetIncomplete, |s| {
|
||||
s.usage_snapshot_set_states[0].complete = false
|
||||
}),
|
||||
(ScannerBaselineRejection::SetTombstone, |s| {
|
||||
s.usage_snapshot_set_states[0].tombstone = true
|
||||
}),
|
||||
(ScannerBaselineRejection::SetEpoch, |s| {
|
||||
s.usage_snapshot_set_states[0].scanner_epoch = None
|
||||
}),
|
||||
(ScannerBaselineRejection::SetCycle, |s| {
|
||||
s.usage_snapshot_set_states[0].scanner_cycle = Some(9)
|
||||
}),
|
||||
(ScannerBaselineRejection::SetCycle, |s| {
|
||||
s.usage_snapshot_set_states[0].scanner_cycle = None
|
||||
}),
|
||||
];
|
||||
assert_eq!(validate_scanner_cache_snapshot_plan_digest(&snapshot, proof, None), Ok(digest));
|
||||
for (expected, mutate) in cases {
|
||||
let mut invalid = snapshot.clone();
|
||||
mutate(&mut invalid);
|
||||
assert_eq!(validate_scanner_cache_snapshot_plan_digest(&invalid, proof, None), Err(expected));
|
||||
assert_eq!(complete_scanner_cache_snapshot_plan_digest(&invalid, proof, None), None);
|
||||
}
|
||||
let mut invalid = snapshot.clone();
|
||||
invalid.usage_snapshot_partial = true;
|
||||
invalid.scanner_epoch = Some(10);
|
||||
assert_eq!(
|
||||
validate_scanner_cache_snapshot_plan_digest(&invalid, proof, None),
|
||||
Err(ScannerBaselineRejection::Partial)
|
||||
);
|
||||
|
||||
let two_sources = HashSet::from([source, DataUsageCacheSource::new(1, 3)]);
|
||||
let mut two_sets = snapshot.clone();
|
||||
two_sets
|
||||
.usage_snapshot_set_states
|
||||
.push(two_sets.usage_snapshot_set_states[0].clone());
|
||||
let two_set_proof = ScannerCacheBaselineProof {
|
||||
expected_sources: &two_sources,
|
||||
..proof
|
||||
};
|
||||
assert_eq!(
|
||||
validate_scanner_cache_snapshot_plan_digest(&two_sets, two_set_proof, None),
|
||||
Err(ScannerBaselineRejection::SourceDuplicate)
|
||||
);
|
||||
two_sets.usage_snapshot_set_states[1].set_index = 3;
|
||||
two_sets.usage_snapshot_set_states[1].scan_plan_digest = Some([8; 32]);
|
||||
assert_eq!(
|
||||
validate_scanner_cache_snapshot_plan_digest(&two_sets, two_set_proof, None),
|
||||
Err(ScannerBaselineRejection::SetPlan)
|
||||
);
|
||||
|
||||
let undecodable = Bytes::from_static(b"not-json");
|
||||
for (data, expected) in [
|
||||
(None, ScannerBaselineRejection::Missing),
|
||||
(Some(&undecodable), ScannerBaselineRejection::Decode),
|
||||
] {
|
||||
assert_eq!(
|
||||
validate_scanner_cache_baseline_plan_digest(ScannerCacheBaselineProof {
|
||||
authoritative_data: data,
|
||||
..proof
|
||||
}),
|
||||
Err(ScannerBaselineValidationFailure {
|
||||
authoritative: expected,
|
||||
observed: None
|
||||
})
|
||||
);
|
||||
}
|
||||
let mut wrong_epoch = snapshot;
|
||||
wrong_epoch.scanner_epoch = Some(10);
|
||||
let wrong_epoch = Bytes::from(serde_json::to_vec(&wrong_epoch).expect("encode stale epoch baseline"));
|
||||
let stale_proof = ScannerCacheBaselineProof {
|
||||
authoritative_data: Some(&wrong_epoch),
|
||||
..proof
|
||||
};
|
||||
assert_eq!(
|
||||
validate_scanner_cache_baseline_plan_digest(stale_proof),
|
||||
Err(ScannerBaselineValidationFailure {
|
||||
authoritative: ScannerBaselineRejection::Epoch,
|
||||
observed: Some(ScannerBaselineRejection::Missing),
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
validate_scanner_cache_baseline_plan_digest(ScannerCacheBaselineProof {
|
||||
observed_candidate_data: Some(&undecodable),
|
||||
..stale_proof
|
||||
}),
|
||||
Err(ScannerBaselineValidationFailure {
|
||||
authoritative: ScannerBaselineRejection::Epoch,
|
||||
observed: Some(ScannerBaselineRejection::Decode)
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scoped_scan_accepts_only_a_complete_observation_tied_to_the_authoritative_baseline() {
|
||||
let source = DataUsageCacheSource::new(1, 2);
|
||||
@@ -3252,6 +3634,12 @@ fn distributed_segment_reuse_activation_keeps_remote_dirty_buckets_at_bucket_sco
|
||||
result.scope.prefix_scope_for("photos").is_some(),
|
||||
"durable local producer proof may activate local segment reuse after distributed ACK capability evidence"
|
||||
);
|
||||
assert!(
|
||||
result
|
||||
.segment_reuse_activation_preflight
|
||||
.expect("actual scope proof is retained for observation")
|
||||
.scanner_segment_reuse_activated
|
||||
);
|
||||
assert!(
|
||||
result.scope.prefix_scope_for("archive").is_none(),
|
||||
"remote dirty usage has only bucket-granularity evidence and must not be narrowed to a local prefix"
|
||||
@@ -3670,6 +4058,10 @@ fn scoped_set_scan_rebuilds_selected_buckets_and_drops_deleted_buckets() {
|
||||
.checked_flatten("stable")
|
||||
.expect("selected bucket placeholder should exist");
|
||||
assert_eq!((stable.size, stable.objects), (0, 0));
|
||||
let observation = rustfs_scanner_metrics::scan_observation::ScannerScanObservation::default();
|
||||
prepared.record_cold_bucket_reuse(&observation);
|
||||
assert_eq!(observation.snapshot().bucket_reuse["scoped_cold_bucket"].count, 0);
|
||||
|
||||
assert!(prepared.cache.find("stable/prefix").is_none());
|
||||
assert_eq!(prepared.cache.find("dirty").map(|entry| (entry.size, entry.objects)), Some((0, 0)));
|
||||
assert!(prepared.cache.find("deleted").is_none());
|
||||
@@ -3681,6 +4073,124 @@ fn scoped_set_scan_rebuilds_selected_buckets_and_drops_deleted_buckets() {
|
||||
assert_eq!(prepared.cache.info.lkg_scan_plan_digest, Some(baseline_digest));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cold_bucket_reuse_observation_rejects_unbound_or_failed_preparation() {
|
||||
let baseline_digest = DataUsageScanPlanDigest([1; 32]);
|
||||
let mut old_cache = complete_set_usage_cache(&[("stable", 10), ("dirty", 20)], baseline_digest);
|
||||
old_cache.info.scan_bucket_incarnations = test_bucket_incarnations(&["stable", "dirty"]);
|
||||
let incarnations = old_cache.info.scan_bucket_incarnations.clone();
|
||||
let buckets = vec![
|
||||
bucket_info_with_created_time("stable"),
|
||||
bucket_info_with_created_time("dirty"),
|
||||
];
|
||||
let scope = ScannerBucketScanScope {
|
||||
selected_buckets: Some(Arc::new(HashSet::from(["dirty".to_string()]))),
|
||||
selected_bucket_prefixes: None,
|
||||
baseline_scan_plan_digest: Some(baseline_digest),
|
||||
};
|
||||
let generation = ScannerSetCacheGeneration {
|
||||
want_cycle: 8,
|
||||
leader_epoch: 11,
|
||||
tier_registry_generation: 13,
|
||||
source: DataUsageCacheSource::new(1, 2),
|
||||
scan_plan_digest: DataUsageScanPlanDigest([2; 32]),
|
||||
};
|
||||
let observation = rustfs_scanner_metrics::scan_observation::ScannerScanObservation::default();
|
||||
let prepared = prepare_scoped_set_scan(&old_cache, &buckets, &buckets, &scope, generation, Some(&incarnations))
|
||||
.expect("bound complete cache permits a cold bucket copy");
|
||||
prepared.record_cold_bucket_reuse(&observation);
|
||||
assert_eq!(observation.snapshot().bucket_reuse["scoped_cold_bucket"].count, 1);
|
||||
|
||||
for case in ["incomplete", "duplicate", "nil_incarnation"] {
|
||||
let mut invalid_cache = old_cache.clone();
|
||||
let mut invalid_buckets = buckets.clone();
|
||||
let mut invalid_incarnations = incarnations.clone();
|
||||
match case {
|
||||
"incomplete" => invalid_cache.info.snapshot_complete = false,
|
||||
"duplicate" => invalid_buckets.push(buckets[0].clone()),
|
||||
"nil_incarnation" => {
|
||||
invalid_incarnations.insert("stable".to_string(), Uuid::nil());
|
||||
}
|
||||
_ => unreachable!("fixed test cases"),
|
||||
}
|
||||
let prepared = prepare_scoped_set_scan(
|
||||
&invalid_cache,
|
||||
&invalid_buckets,
|
||||
&invalid_buckets,
|
||||
&scope,
|
||||
generation,
|
||||
Some(&invalid_incarnations),
|
||||
);
|
||||
assert!(prepared.is_none(), "{case} must not return a reusable prepared cache");
|
||||
if let Some(prepared) = prepared {
|
||||
prepared.record_cold_bucket_reuse(&observation);
|
||||
}
|
||||
assert_eq!(
|
||||
observation.snapshot().bucket_reuse["scoped_cold_bucket"].count,
|
||||
1,
|
||||
"{case} does not count completed reuse"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn current_root_reuse_counts_only_successful_collector_delivery() {
|
||||
use rustfs_scanner_metrics::scan_observation::ScannerScanObservation;
|
||||
|
||||
let observation = ScannerScanObservation::default();
|
||||
let cache = DataUsageCache::default();
|
||||
let pending = AtomicBool::new(false);
|
||||
let root = || DataUsageEntryInfo {
|
||||
name: "bucket".to_string(),
|
||||
parent: DATA_USAGE_ROOT.to_string(),
|
||||
entry: DataUsageEntry {
|
||||
size: 10,
|
||||
objects: 2,
|
||||
..Default::default()
|
||||
},
|
||||
bucket_incarnation: Some(Uuid::from_u128(7)),
|
||||
tier_registry_generation: None,
|
||||
value_origin: None,
|
||||
};
|
||||
let (sender, mut receiver) = mpsc::channel(1);
|
||||
super::io_cache::send_current_cache_root_entry(&sender, root(), &cache, &pending, &observation)
|
||||
.await
|
||||
.expect("current root is delivered to the collector");
|
||||
let delivered = receiver.recv().await.expect("collector received the root");
|
||||
assert_eq!((delivered.entry.size, delivered.entry.objects), (10, 2));
|
||||
assert_eq!(observation.snapshot().bucket_reuse["current_root"].count, 1);
|
||||
drop(receiver);
|
||||
assert!(
|
||||
super::io_cache::send_current_cache_root_entry(&sender, root(), &cache, &pending, &observation)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
assert_eq!(
|
||||
observation.snapshot().bucket_reuse["current_root"].count,
|
||||
1,
|
||||
"closed collector is not completed reuse"
|
||||
);
|
||||
|
||||
let (sender, _receiver) = mpsc::channel(1);
|
||||
sender.send(root()).await.expect("fill bounded collector channel");
|
||||
{
|
||||
let mut send = Box::pin(super::io_cache::send_current_cache_root_entry(
|
||||
&sender,
|
||||
root(),
|
||||
&cache,
|
||||
&pending,
|
||||
&observation,
|
||||
));
|
||||
assert!(futures::poll!(send.as_mut()).is_pending(), "full collector keeps reuse unfinished");
|
||||
}
|
||||
assert_eq!(
|
||||
observation.snapshot().bucket_reuse["current_root"].count,
|
||||
1,
|
||||
"dropping unfinished delivery is not reuse"
|
||||
);
|
||||
assert_eq!(observation.snapshot().bucket_reuse["scoped_cold_bucket"].count, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scoped_set_scan_reuses_unselected_buckets_with_matching_incarnations() {
|
||||
let baseline_digest = DataUsageScanPlanDigest([1; 32]);
|
||||
@@ -3722,6 +4232,12 @@ fn scoped_set_scan_reuses_unselected_buckets_with_matching_incarnations() {
|
||||
)
|
||||
.expect("matching bucket incarnations should authorize cold bucket reuse");
|
||||
|
||||
let observation = rustfs_scanner_metrics::scan_observation::ScannerScanObservation::default();
|
||||
prepared.record_cold_bucket_reuse(&observation);
|
||||
let observed = observation.snapshot();
|
||||
assert_eq!(observed.bucket_reuse["scoped_cold_bucket"].count, 1);
|
||||
assert_eq!(observed.bucket_reuse["current_root"].count, 0);
|
||||
assert!(observed.cache_reuse.values().all(|reuse| reuse.count == 0));
|
||||
assert_eq!(prepared.buckets.iter().map(|bucket| bucket.name.as_str()).collect::<Vec<_>>(), ["dirty"]);
|
||||
let stable = prepared
|
||||
.cache
|
||||
@@ -4727,12 +5243,36 @@ async fn get_size_treats_missing_metadata_as_skip_file() {
|
||||
debug: false,
|
||||
};
|
||||
|
||||
let before = global_metrics().scan_observation().snapshot().metadata_reads;
|
||||
let err = disk
|
||||
.get_size(item)
|
||||
.get_size(item.clone())
|
||||
.await
|
||||
.expect_err("missing metadata should be skipped instead of reported as a scanner failure");
|
||||
assert!(matches!(err, StorageError::Io(ref io) if io.to_string() == SCANNER_SKIP_FILE_ERROR));
|
||||
|
||||
let missing = global_metrics().scan_observation().snapshot().metadata_reads;
|
||||
assert_eq!(missing.attempts, before.attempts + 1);
|
||||
assert_eq!(missing.not_found, before.not_found + 1);
|
||||
assert_eq!(missing.bytes, before.bytes);
|
||||
|
||||
#[cfg(unix)]
|
||||
{
|
||||
tokio::fs::create_dir_all(&object_dir)
|
||||
.await
|
||||
.expect("recreate object directory");
|
||||
std::os::unix::fs::symlink(STORAGE_FORMAT_FILE, &metadata_path).expect("inject a metadata symlink loop");
|
||||
let err = disk
|
||||
.get_size(item)
|
||||
.await
|
||||
.expect_err("symlink loop must remain a read failure");
|
||||
assert!(is_scanner_metadata_transient_error(&err), "read error: {err:?}");
|
||||
let failed = global_metrics().scan_observation().snapshot().metadata_reads;
|
||||
assert_eq!(failed.attempts, missing.attempts + 1);
|
||||
assert_eq!(failed.errors, missing.errors + 1);
|
||||
assert_eq!(failed.succeeded, before.succeeded);
|
||||
assert_eq!(failed.bytes, before.bytes);
|
||||
}
|
||||
|
||||
let _ = tokio::fs::remove_dir_all(&temp_dir).await;
|
||||
}
|
||||
|
||||
@@ -4783,6 +5323,7 @@ async fn get_size_marks_corrupt_metadata_for_heal() {
|
||||
debug: false,
|
||||
};
|
||||
|
||||
let before = global_metrics().scan_observation().snapshot().metadata_reads;
|
||||
for contents in [b"".as_slice(), b"not-valid-filemeta".as_slice()] {
|
||||
tokio::fs::write(&metadata_path, contents)
|
||||
.await
|
||||
@@ -4794,6 +5335,16 @@ async fn get_size_marks_corrupt_metadata_for_heal() {
|
||||
assert!(is_scanner_metadata_corrupt_error(&err));
|
||||
}
|
||||
|
||||
let reads = global_metrics().scan_observation().snapshot().metadata_reads;
|
||||
assert_eq!(reads.attempts, before.attempts + 2);
|
||||
assert_eq!(
|
||||
reads.succeeded,
|
||||
before.succeeded + 2,
|
||||
"storage reads succeed before decoding rejects corruption"
|
||||
);
|
||||
assert_eq!(reads.errors, before.errors);
|
||||
assert_eq!(reads.bytes, before.bytes + 18);
|
||||
|
||||
let _ = tokio::fs::remove_dir_all(&temp_dir).await;
|
||||
}
|
||||
|
||||
@@ -4866,11 +5417,17 @@ async fn get_size_counts_delete_markers_separately_from_versions() {
|
||||
debug: false,
|
||||
};
|
||||
|
||||
let before = global_metrics().scan_observation().snapshot().metadata_reads;
|
||||
let expected_bytes = tokio::fs::metadata(&metadata_path).await.expect("stat stored metadata").len();
|
||||
let summary = disk.get_size(item).await.expect("scanner should read versioned metadata");
|
||||
|
||||
assert_eq!(summary.versions, 2);
|
||||
assert_eq!(summary.delete_markers, 1);
|
||||
assert_eq!(summary.total_size, 30);
|
||||
let reads = global_metrics().scan_observation().snapshot().metadata_reads;
|
||||
assert_eq!(reads.attempts, before.attempts + 1);
|
||||
assert_eq!(reads.succeeded, before.succeeded + 1);
|
||||
assert_eq!(reads.bytes, before.bytes + expected_bytes);
|
||||
|
||||
let _ = tokio::fs::remove_dir_all(&temp_dir).await;
|
||||
}
|
||||
|
||||
@@ -183,9 +183,11 @@ async fn dirty_entry_removed_during_listing_still_forces_a_full_scan() {
|
||||
);
|
||||
assert_eq!(second.buckets_usage["hot-bucket"].objects_count, 2);
|
||||
assert_eq!(second.buckets_usage["cold-bucket"].objects_count, 2);
|
||||
let freshness = global_metrics().report().await.usage_freshness;
|
||||
let report = global_metrics().report().await;
|
||||
let freshness = report.usage_freshness;
|
||||
assert_eq!(freshness.scope_fallback_total, fallbacks_before + 1);
|
||||
assert_eq!(freshness.last_scope_fallback_reason, "dirty_snapshot_incomplete");
|
||||
assert_eq!(report.scan_observation.scope_fallbacks["dirty_snapshot_incomplete"], 1);
|
||||
clear_dirty_usage_buckets_for_tests();
|
||||
}
|
||||
|
||||
@@ -199,11 +201,53 @@ async fn required_full_scan_is_not_reported_as_a_scope_fallback() {
|
||||
wait_for_namespace_commit_tails(store.as_ref()).await;
|
||||
super::io_cycle::set_bucket_listing_window_hook_for_tests(|| record_dirty_usage_bucket("created-after-listing"));
|
||||
|
||||
let before = global_metrics().scan_observation().snapshot();
|
||||
let (result, _, scope) = run_cycle(&store, 2, Some(baseline), true).await;
|
||||
|
||||
assert!(scope.is_default());
|
||||
assert_eq!(result.pending_dirty_usage_buckets(), 1);
|
||||
assert_eq!(result.scope_fallback(), None, "scheduled maintenance walks are not degradations");
|
||||
assert_eq!(result.completed_scope_fallback(), None);
|
||||
let after = global_metrics().scan_observation().snapshot();
|
||||
assert_eq!(after.scope_fallbacks, before.scope_fallbacks);
|
||||
assert_eq!(after.prefix_scope_resolution.evaluated, before.prefix_scope_resolution.evaluated);
|
||||
assert_eq!(after.prefix_cycle_completion.evaluated, before.prefix_cycle_completion.evaluated + 1);
|
||||
clear_dirty_usage_buckets_for_tests();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn baseline_unavailable_cycle_records_real_scope_and_baseline_rejections_once() {
|
||||
let (_temp_dir, store) = setup_local_scanner_store(1, 1).await;
|
||||
let _baseline = converged_baseline(&store).await;
|
||||
put_fixture_object(&store, "hot-bucket", "second").await;
|
||||
record_dirty_usage_bucket("hot-bucket");
|
||||
wait_for_namespace_commit_tails(store.as_ref()).await;
|
||||
let before = global_metrics().scan_observation().snapshot();
|
||||
let (result, snapshot, scope) = run_cycle(&store, 2, Some(Bytes::from_static(b"not-json")), false).await;
|
||||
assert!(scope.is_default());
|
||||
assert_eq!(result.status, ScannerCycleStatus::Complete);
|
||||
assert_eq!(result.scope_fallback(), Some(ScannerScopeFallbackReason::BaselineUnavailable));
|
||||
assert_eq!(snapshot.buckets_usage["hot-bucket"].objects_count, 2);
|
||||
let after = global_metrics().scan_observation().snapshot();
|
||||
assert_eq!(
|
||||
after.scope_fallbacks["baseline_unavailable"],
|
||||
before.scope_fallbacks["baseline_unavailable"] + 1
|
||||
);
|
||||
assert_eq!(
|
||||
after.authoritative_baseline_rejections["decode"],
|
||||
before.authoritative_baseline_rejections["decode"] + 1
|
||||
);
|
||||
assert_eq!(
|
||||
after.observed_baseline_rejections, before.observed_baseline_rejections,
|
||||
"an undecodable authority never admits an observation"
|
||||
);
|
||||
assert_eq!(after.prefix_scope_resolution.evaluated, before.prefix_scope_resolution.evaluated + 1);
|
||||
assert_eq!(after.prefix_scope_resolution.activated, before.prefix_scope_resolution.activated);
|
||||
assert_eq!(
|
||||
after.prefix_scope_resolution.blockers["missing_cold_zero_walk_oracle"],
|
||||
before.prefix_scope_resolution.blockers["missing_cold_zero_walk_oracle"] + 1
|
||||
);
|
||||
assert_eq!(after.prefix_cycle_completion.evaluated, before.prefix_cycle_completion.evaluated + 1);
|
||||
clear_dirty_usage_buckets_for_tests();
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
// Copyright 2026 RustFS Team
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
//! A bucket whose walk stops early without exhausting the cycle budget (here
|
||||
//! a sweep resumed under a newer plan, the `mixed_coverage` case of
|
||||
//! rustfs/rustfs#8494) no longer makes the whole cycle `Incomplete`. The set
|
||||
//! publishes the bucket's last certified value, the cycle completes with the
|
||||
//! bucket pending, and the next cycle walks it again. A bucket that never
|
||||
//! certified keeps the cycle incomplete.
|
||||
//! A bucket whose walk stops early can publish its last certified value as
|
||||
//! pending. This fixture tests usage-scan status, not the outer scanner cycle
|
||||
//! outcome: budget exhaustion may leave complete validated usage while the
|
||||
//! coordinator records `Partial`. A continuation under a newer plan exercises
|
||||
//! `mixed_coverage` (rustfs/rustfs#8494). A bucket that never certified keeps
|
||||
//! the usage scan incomplete.
|
||||
|
||||
use super::*;
|
||||
|
||||
@@ -38,8 +38,7 @@ async fn run_cycle(store: &Arc<ECStore>, cycle: u64, options: CycleOptions) -> (
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
// These cycles persist real bucket checkpoints and source caches. Keep
|
||||
// CI storage latency inside the production drain window after a count stop.
|
||||
// Real checkpoint commits need the runtime drain grace after the count budget stops the walk.
|
||||
budget.use_production_drain_grace_for_tests();
|
||||
let (updates, mut receiver) = mpsc::channel(1);
|
||||
let result = tokio::time::timeout(
|
||||
@@ -49,7 +48,7 @@ async fn run_cycle(store: &Arc<ECStore>, cycle: u64, options: CycleOptions) -> (
|
||||
ScannerCycleRequest {
|
||||
owner_ctx: ctx.clone(),
|
||||
ctx: budget.token(),
|
||||
budget,
|
||||
budget: Arc::clone(&budget),
|
||||
updates,
|
||||
want_cycle: cycle,
|
||||
leader_epoch: LEADER_EPOCH,
|
||||
@@ -69,6 +68,13 @@ async fn run_cycle(store: &Arc<ECStore>, cycle: u64, options: CycleOptions) -> (
|
||||
.await
|
||||
.expect("cycle should finish within the test deadline")
|
||||
.expect("cycle should succeed");
|
||||
if options.max_objects.is_some() {
|
||||
assert_eq!(
|
||||
budget.reason(),
|
||||
Some(crate::scanner_budget::ScannerCycleBudgetReason::Objects),
|
||||
"a count-limited fixture must stop for its object budget"
|
||||
);
|
||||
}
|
||||
let snapshot = receiver.recv().await;
|
||||
assert!(receiver.recv().await.is_none(), "a cycle publishes at most one terminal snapshot");
|
||||
(result, snapshot)
|
||||
@@ -125,9 +131,8 @@ async fn partial_bucket_with_a_certified_value_completes_the_cycle_as_pending()
|
||||
assert!(result.clear_verified_usage().is_empty());
|
||||
assert!(dirty_usage_buckets_for_tests().is_empty());
|
||||
|
||||
// Cycle 3: the object budget stops the hot bucket's walk and leaves a
|
||||
// checkpoint under this cycle's plan. Its certified value is carried, so
|
||||
// budget exhaustion leaves the bucket pending without blocking the cycle.
|
||||
// Cycle 3: the budget stops the walk and saves a checkpoint. Usage can
|
||||
// carry the certified value as pending; the outer coordinator records Partial.
|
||||
for object in ["p4/third", "p5/third"] {
|
||||
put_fixture_object(&store, "hot-bucket", object).await;
|
||||
}
|
||||
@@ -144,12 +149,38 @@ async fn partial_bucket_with_a_certified_value_completes_the_cycle_as_pending()
|
||||
},
|
||||
)
|
||||
.await;
|
||||
let third = third.expect("a carried partial cycle publishes");
|
||||
assert_eq!(result.status, ScannerCycleStatus::Complete);
|
||||
assert_eq!(
|
||||
result.status,
|
||||
ScannerCycleStatus::Complete,
|
||||
"certified carried usage remains complete after a budget stop"
|
||||
);
|
||||
let third = third.expect("validated carried usage publishes after the budget stop");
|
||||
assert_eq!(result.pending_dirty_usage_buckets(), 1);
|
||||
assert!(!third.usage_snapshot_observational);
|
||||
assert_eq!(third.usage_snapshot_converged, Some(false));
|
||||
assert_eq!(third.usage_snapshot_pending_buckets, vec!["hot-bucket".to_string()]);
|
||||
assert_eq!(third.buckets_usage["hot-bucket"].objects_count, 4);
|
||||
assert_eq!(third.usage_snapshot_pending_bucket_count, 1);
|
||||
assert!(third.is_complete_bucket_usage_snapshot());
|
||||
let carried_hot = &third.buckets_usage["hot-bucket"];
|
||||
let certified_hot = &second.buckets_usage["hot-bucket"];
|
||||
assert_eq!(carried_hot.usage_value_state, Some(rustfs_data_usage::BucketUsageValueState::Carried));
|
||||
assert!(certified_hot.usage_certification.is_some(), "cycle 2 certified the prior hot value");
|
||||
assert_eq!(
|
||||
carried_hot.usage_certification, certified_hot.usage_certification,
|
||||
"carrying must not mint a new certificate"
|
||||
);
|
||||
assert_eq!(carried_hot.objects_count, 4);
|
||||
assert_eq!(carried_hot.size, certified_hot.size);
|
||||
assert_eq!(third.buckets_usage["cold-bucket"].objects_count, 1);
|
||||
assert_eq!(third.buckets_usage["idle-bucket"].objects_count, 1);
|
||||
assert_eq!(third.objects_total_count, 6);
|
||||
// Only hot was dirty in this scoped scan; it received no fresh certification or ACK.
|
||||
assert!(result.clear_verified_usage().is_empty());
|
||||
assert_eq!(
|
||||
dirty_usage_buckets_for_tests().keys().cloned().collect::<Vec<_>>(),
|
||||
vec!["hot-bucket".to_string()],
|
||||
"the budget-stopped hot bucket retains its dirty responsibility"
|
||||
);
|
||||
let set = Arc::clone(&store.pools[0].disk_set[0]);
|
||||
let mut interrupted = DataUsageCache::default();
|
||||
interrupted
|
||||
|
||||
@@ -15,30 +15,6 @@
|
||||
use super::*;
|
||||
use crate::data_usage_define::{DATA_USAGE_OBJ_NAME_PATH, read_config_with_revision};
|
||||
|
||||
// Two-pool EC fixtures keep many scan futures live at once and overflow
|
||||
// Linux libtest's default 2 MiB test-thread stack. Run each case on a
|
||||
// dedicated 32 MiB thread (same approach as scoped_entry_fallback).
|
||||
fn run_cohort_test<F, Fut>(thread_name: &'static str, test_fn: F)
|
||||
where
|
||||
F: FnOnce() -> Fut + Send + 'static,
|
||||
Fut: std::future::Future<Output = ()> + 'static,
|
||||
{
|
||||
let handle = std::thread::Builder::new()
|
||||
.name(thread_name.to_string())
|
||||
.stack_size(32 * 1024 * 1024)
|
||||
.spawn(move || {
|
||||
let runtime = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.expect("service cohort test runtime should build");
|
||||
runtime.block_on(test_fn());
|
||||
})
|
||||
.expect("service cohort test thread should spawn");
|
||||
if let Err(payload) = handle.join() {
|
||||
std::panic::resume_unwind(payload);
|
||||
}
|
||||
}
|
||||
|
||||
async fn create_cohort_bucket(store: &ECStore, bucket: &str) {
|
||||
create_cohort_bucket_objects(store, bucket, 1).await;
|
||||
}
|
||||
@@ -128,16 +104,9 @@ async fn run_cohort_cycle(
|
||||
(result, usage)
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
fn service_cohort_production_dispatch_services_waiters_across_sources() {
|
||||
run_cohort_test(
|
||||
"cohort-prod-disp",
|
||||
service_cohort_production_dispatch_services_waiters_across_sources_case,
|
||||
);
|
||||
}
|
||||
|
||||
async fn service_cohort_production_dispatch_services_waiters_across_sources_case() {
|
||||
async fn service_cohort_production_dispatch_services_waiters_across_sources() {
|
||||
let (_dir, store) = setup_two_pool_scanner_store().await;
|
||||
clear_dirty_usage_buckets_for_tests();
|
||||
let hot = format!("a-hot-{}", Uuid::new_v4().simple());
|
||||
@@ -228,16 +197,9 @@ async fn service_cohort_production_dispatch_services_waiters_across_sources_case
|
||||
clear_dirty_usage_buckets_for_tests();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
fn service_cohort_flat_bucket_budget_does_not_publish_unscanned_small_bucket() {
|
||||
run_cohort_test(
|
||||
"cohort-flat-bud",
|
||||
service_cohort_flat_bucket_budget_does_not_publish_unscanned_small_bucket_case,
|
||||
);
|
||||
}
|
||||
|
||||
async fn service_cohort_flat_bucket_budget_does_not_publish_unscanned_small_bucket_case() {
|
||||
async fn service_cohort_flat_bucket_budget_does_not_publish_unscanned_small_bucket() {
|
||||
let (_dir, store) = setup_two_pool_scanner_store().await;
|
||||
clear_dirty_usage_buckets_for_tests();
|
||||
let flat = format!("a-flat-{}", Uuid::new_v4().simple());
|
||||
@@ -321,16 +283,9 @@ async fn service_cohort_flat_bucket_budget_does_not_publish_unscanned_small_buck
|
||||
clear_dirty_usage_buckets_for_tests();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
fn service_cohort_fresh_complete_aggregate_preserves_reordered_sources() {
|
||||
run_cohort_test(
|
||||
"cohort-fresh-agg",
|
||||
service_cohort_fresh_complete_aggregate_preserves_reordered_sources_case,
|
||||
);
|
||||
}
|
||||
|
||||
async fn service_cohort_fresh_complete_aggregate_preserves_reordered_sources_case() {
|
||||
async fn service_cohort_fresh_complete_aggregate_preserves_reordered_sources() {
|
||||
let (_dir, store) = setup_two_pool_scanner_store().await;
|
||||
clear_dirty_usage_buckets_for_tests();
|
||||
for bucket in ["cohort-first", "cohort-second"] {
|
||||
@@ -374,16 +329,9 @@ async fn service_cohort_fresh_complete_aggregate_preserves_reordered_sources_cas
|
||||
clear_dirty_usage_buckets_for_tests();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
fn service_cohort_cancelled_dispatch_does_not_consume_waiters_or_leak_permits() {
|
||||
run_cohort_test(
|
||||
"cohort-cancelled",
|
||||
service_cohort_cancelled_dispatch_does_not_consume_waiters_or_leak_permits_case,
|
||||
);
|
||||
}
|
||||
|
||||
async fn service_cohort_cancelled_dispatch_does_not_consume_waiters_or_leak_permits_case() {
|
||||
async fn service_cohort_cancelled_dispatch_does_not_consume_waiters_or_leak_permits() {
|
||||
let (_dir, store) = setup_two_pool_scanner_store().await;
|
||||
clear_dirty_usage_buckets_for_tests();
|
||||
let bucket = format!("cancel-{}", Uuid::new_v4().simple());
|
||||
@@ -412,16 +360,9 @@ async fn service_cohort_cancelled_dispatch_does_not_consume_waiters_or_leak_perm
|
||||
/// N=17, q=4: every bucket reaches a worker within ceil(17/4)=5 rounds while
|
||||
/// one bucket stays hot and another fails forever, with leader-local state
|
||||
/// discarded after each round as a restart would.
|
||||
#[test]
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
fn cold_bucket_service_bound_survives_restart_with_hot_and_failing_bucket() {
|
||||
run_cohort_test(
|
||||
"cold-svc-bound",
|
||||
cold_bucket_service_bound_survives_restart_with_hot_and_failing_bucket_case,
|
||||
);
|
||||
}
|
||||
|
||||
async fn cold_bucket_service_bound_survives_restart_with_hot_and_failing_bucket_case() {
|
||||
async fn cold_bucket_service_bound_survives_restart_with_hot_and_failing_bucket() {
|
||||
const BUCKETS: usize = 17;
|
||||
const QUANTA: u64 = 4;
|
||||
const ROUND_BOUND: usize = 5;
|
||||
@@ -493,16 +434,9 @@ fn quantum_budget(quantum: crate::scanner_budget::ScannerBucketQuantum) -> Arc<S
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
fn source_turn_dispatch_rotates_real_sources_and_retains_completed_roots() {
|
||||
run_cohort_test(
|
||||
"src-turn-disp",
|
||||
source_turn_dispatch_rotates_real_sources_and_retains_completed_roots_case,
|
||||
);
|
||||
}
|
||||
|
||||
async fn source_turn_dispatch_rotates_real_sources_and_retains_completed_roots_case() {
|
||||
async fn source_turn_dispatch_rotates_real_sources_and_retains_completed_roots() {
|
||||
let mut evidence = None;
|
||||
temp_env::async_with_vars([(ENV_SCANNER_MAX_CONCURRENT_SET_SCANS, Some("1"))], async {
|
||||
crate::runtime_config::refresh_scanner_runtime_config_for_tests();
|
||||
@@ -569,16 +503,9 @@ async fn source_turn_dispatch_rotates_real_sources_and_retains_completed_roots_c
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
fn source_turn_zero_new_frontier_stops_requeuing_without_starving_other_sources() {
|
||||
run_cohort_test(
|
||||
"src-turn-zero",
|
||||
source_turn_zero_new_frontier_stops_requeuing_without_starving_other_sources_case,
|
||||
);
|
||||
}
|
||||
|
||||
async fn source_turn_zero_new_frontier_stops_requeuing_without_starving_other_sources_case() {
|
||||
async fn source_turn_zero_new_frontier_stops_requeuing_without_starving_other_sources() {
|
||||
temp_env::async_with_vars([(ENV_SCANNER_MAX_CONCURRENT_SET_SCANS, Some("1"))], async {
|
||||
crate::runtime_config::refresh_scanner_runtime_config_for_tests();
|
||||
let (_dir, store) = setup_two_pool_scanner_store().await;
|
||||
@@ -606,13 +533,9 @@ async fn source_turn_zero_new_frontier_stops_requeuing_without_starving_other_so
|
||||
crate::runtime_config::refresh_scanner_runtime_config_for_tests();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
fn fair_prefix_quantum_spent_bucket_returns_behind_waiting_buckets() {
|
||||
run_cohort_test("fair-spent-qm", fair_prefix_quantum_spent_bucket_returns_behind_waiting_buckets_case);
|
||||
}
|
||||
|
||||
async fn fair_prefix_quantum_spent_bucket_returns_behind_waiting_buckets_case() {
|
||||
async fn fair_prefix_quantum_spent_bucket_returns_behind_waiting_buckets() {
|
||||
let (_dir, store) = setup_local_scanner_store(1, 4).await;
|
||||
clear_dirty_usage_buckets_for_tests();
|
||||
create_cohort_bucket_objects(&store, "a-flat", 6).await;
|
||||
@@ -642,13 +565,9 @@ async fn fair_prefix_quantum_spent_bucket_returns_behind_waiting_buckets_case()
|
||||
clear_dirty_usage_buckets_for_tests();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
fn fair_prefix_quantum_without_durable_motion_ends_the_bucket_turn() {
|
||||
run_cohort_test("fair-no-motion", fair_prefix_quantum_without_durable_motion_ends_the_bucket_turn_case);
|
||||
}
|
||||
|
||||
async fn fair_prefix_quantum_without_durable_motion_ends_the_bucket_turn_case() {
|
||||
async fn fair_prefix_quantum_without_durable_motion_ends_the_bucket_turn() {
|
||||
let (_dir, store) = setup_local_scanner_store(1, 4).await;
|
||||
clear_dirty_usage_buckets_for_tests();
|
||||
create_cohort_bucket_objects(&store, "a-flat", 6).await;
|
||||
@@ -671,16 +590,9 @@ async fn fair_prefix_quantum_without_durable_motion_ends_the_bucket_turn_case()
|
||||
|
||||
/// A one-object share stops inside its first child, so the frontier never
|
||||
/// moves. Requeuing that bucket would spin until the cycle deadline.
|
||||
#[test]
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
fn fair_prefix_quantum_repeated_share_without_a_new_frontier_stops_requeuing() {
|
||||
run_cohort_test(
|
||||
"fair-rep-share",
|
||||
fair_prefix_quantum_repeated_share_without_a_new_frontier_stops_requeuing_case,
|
||||
);
|
||||
}
|
||||
|
||||
async fn fair_prefix_quantum_repeated_share_without_a_new_frontier_stops_requeuing_case() {
|
||||
async fn fair_prefix_quantum_repeated_share_without_a_new_frontier_stops_requeuing() {
|
||||
let (_dir, store) = setup_local_scanner_store(1, 4).await;
|
||||
clear_dirty_usage_buckets_for_tests();
|
||||
create_cohort_bucket_objects(&store, "a-flat", 3).await;
|
||||
@@ -701,16 +613,9 @@ async fn fair_prefix_quantum_repeated_share_without_a_new_frontier_stops_requeui
|
||||
clear_dirty_usage_buckets_for_tests();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
fn cold_bucket_service_saved_order_keeps_coverage_and_rejects_older_owners() {
|
||||
run_cohort_test(
|
||||
"cold-saved-ord",
|
||||
cold_bucket_service_saved_order_keeps_coverage_and_rejects_older_owners_case,
|
||||
);
|
||||
}
|
||||
|
||||
async fn cold_bucket_service_saved_order_keeps_coverage_and_rejects_older_owners_case() {
|
||||
async fn cold_bucket_service_saved_order_keeps_coverage_and_rejects_older_owners() {
|
||||
let (_dir, store) = setup_local_scanner_store(1, 4).await;
|
||||
clear_dirty_usage_buckets_for_tests();
|
||||
create_cohort_bucket(&store, "cold-only").await;
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
# SSE Crate Instructions
|
||||
|
||||
Applies to `crates/sse/`.
|
||||
|
||||
## Boundary
|
||||
|
||||
- This crate sits between the S3 layer and the storage engine and depends on neither. Its `rustfs-*` dependencies are pinned by `scripts/check_architecture_migration_rules.sh` to `rustfs-config`, `rustfs-kms`, `rustfs-policy`, `rustfs-s3-types`, and `rustfs-utils`; see [crate boundaries](../../docs/architecture/crate-boundaries.md).
|
||||
- Anything the crate needs from the running server arrives through `SseHost` (`src/host.rs`), passed as the first argument of every entry point that consults the server. Do not add a global that stands in for the host: a caller must not be able to reach key selection without naming the server it speaks for.
|
||||
- Translation to and from storage-engine types (`ObjectEncryptionResolver`, `WriteEncryption`, `MultipartCompletionAuthorization`) and request types belongs in `rustfs/src/storage/sse.rs`, not here.
|
||||
- Report failures as `SseError`, classified with the S3 error code the caller answers with. A failure that did not originate from the request goes through `SseError::internal`, which keeps its detail out of the response.
|
||||
|
||||
## Layout
|
||||
|
||||
- `src/startup.rs` settles the state that is fixed for the lifetime of the process and reports it. Nothing that follows the KMS service state belongs there: the service follows configuration and can be started, restarted, or stopped from the admin API, so every choice that depends on it is made per request.
|
||||
- `src/core/` holds the entry points (`write.rs`, `read.rs`, `rewrap.rs`) and the request principal (`request.rs`). An entry point only orders the steps; the decisions live in the two layers below it.
|
||||
- `src/core/s3/` is what S3 defines: the SSE headers of a request and a response, the scheme a write or a stored object resolves to, the SSE-C checks, and the authorization of a KMS key.
|
||||
- `src/core/keying/` is how RustFS forms, seals, and records an object's key. `direct.rs` and `object.rs` are the two modes, and each owns both directions of its stored record: the code that writes a key's metadata sits next to the code that reads it back. `mod.rs` is the only place that says which mode a build writes; `stored.rs` holds the order in which a read recognizes the modes and picks the provider that unwraps the data key; `metadata.rs` records the scheme and lists every stored key name.
|
||||
- `src/kms/` holds the data-key providers: they wrap and unwrap a data key and decide nothing about schemes. The write and rewrap entry points, `core/keying/stored.rs`, and the startup check of the local master key call them; `core/s3/` never does.
|
||||
|
||||
Rules that keep the layers apart:
|
||||
|
||||
- `core/keying/` never imports from `core/s3/`. `core/s3/` reads stored metadata only through `core/keying/metadata.rs`. Anything else that needs both layers is joined in an entry point.
|
||||
- `#[cfg(feature = "rio-v2")]` appears only under `core/keying/` and `kms/`. An entry point or `core/s3/` that needs a build-dependent answer asks `core/keying/` for it.
|
||||
- A new stored format, including a MinIO-compatible one, is added under `core/keying/` (and `kms/` when it brings its own wrapping) without changes under `core/s3/`: a mode file with its record and read functions, one entry in the recognition order of `stored.rs`, and its key names in the lists of `metadata.rs`.
|
||||
- A read decides from what the object recorded. The scheme a request advertises and whether a KMS is running now are write policy and runtime state; neither says how an existing object was keyed.
|
||||
|
||||
## Security
|
||||
|
||||
- Never log plaintext customer keys, decrypted key material, data-key envelopes, nonces, or SSE request payloads, including through error strings and `Debug` output.
|
||||
- KMS audit tags may carry only the fields documented on `KmsRequestAuditState`.
|
||||
- The `test-util` feature exposes authorization overrides and DEK-provider injection. It is a dev-dependency feature only; never enable it from a normal dependency.
|
||||
|
||||
## Change Coordination
|
||||
|
||||
When changing key resolution, metadata, or error classification, verify compatibility with:
|
||||
|
||||
- `rustfs/src/storage/sse.rs` (the host implementation and the engine-boundary adapters)
|
||||
- `crates/kms/`
|
||||
- `crates/utils/src/http/object_encryption_keys.rs` (the stored-metadata key vocabulary)
|
||||
- `rustfs/src/kms_rekey.rs` and `rustfs/src/admin/handlers/kms*.rs`
|
||||
|
||||
## Testing
|
||||
|
||||
Run both builds; the `rio-v2` feature swaps the object-key scheme and compiles different code paths:
|
||||
|
||||
```bash
|
||||
cargo nextest run -p rustfs-sse --no-tests fail
|
||||
cargo nextest run -p rustfs-sse --features rio-v2 --no-tests fail
|
||||
```
|
||||
|
||||
The adapter tests live with the adapter:
|
||||
|
||||
```bash
|
||||
cargo nextest run -p rustfs --no-tests fail -E 'test(/^storage::sse::/)'
|
||||
```
|
||||
|
||||
A change that should leave client-visible behavior or the stored form untouched (a refactor, a move between layers) is checked against a real server: run `scripts/test/sse_blackbox/` on the binary before and after the change, compare the two runs, and cross-read their data directories. Its README has the commands.
|
||||
@@ -1,70 +0,0 @@
|
||||
# Copyright 2024 RustFS Team
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
[package]
|
||||
name = "rustfs-sse"
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
repository.workspace = true
|
||||
rust-version.workspace = true
|
||||
version.workspace = true
|
||||
homepage.workspace = true
|
||||
description = "S3 server-side encryption (SSE-S3, SSE-KMS, SSE-C) key resolution and metadata for RustFS."
|
||||
keywords = ["sse", "encryption", "s3", "kms", "rustfs"]
|
||||
categories = ["cryptography"]
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[features]
|
||||
default = []
|
||||
# MinIO-compatible sealed object keys (DARE). Mirrors the `rio-v2` feature of
|
||||
# the server binary, which enables both together.
|
||||
rio-v2 = ["dep:chacha20poly1305", "dep:hmac", "dep:sha2"]
|
||||
# Test seams for dependents: per-principal authorization overrides, DEK
|
||||
# provider injection and metadata fixtures. Never enable in a release build.
|
||||
test-util = []
|
||||
|
||||
[dependencies]
|
||||
# The only RustFS crates this one may reach. It sits between the S3 layer and
|
||||
# the storage engine and must not depend on either: what it needs from the
|
||||
# running server arrives through `SseHost`.
|
||||
rustfs-config = { workspace = true }
|
||||
rustfs-kms = { workspace = true }
|
||||
rustfs-policy = { workspace = true }
|
||||
rustfs-s3-types = { workspace = true }
|
||||
rustfs-utils = { workspace = true, features = ["http", "path"] }
|
||||
|
||||
aes-gcm = { workspace = true, features = ["rand_core", "zeroize"] }
|
||||
async-trait = { workspace = true }
|
||||
base64-simd = { workspace = true }
|
||||
chacha20poly1305 = { workspace = true, optional = true }
|
||||
hmac = { workspace = true, optional = true }
|
||||
http = { workspace = true }
|
||||
md-5 = { workspace = true }
|
||||
rand = { workspace = true }
|
||||
serde = { workspace = true, features = ["derive"] }
|
||||
serde_json = { workspace = true }
|
||||
sha2 = { workspace = true, optional = true }
|
||||
tracing = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
# The fake AWS KMS endpoint the backend-selection tests talk to.
|
||||
axum = { workspace = true }
|
||||
temp-env = { workspace = true, features = ["async_closure"] }
|
||||
tempfile = { workspace = true }
|
||||
tokio = { workspace = true, features = ["macros", "net", "rt-multi-thread", "sync", "time"] }
|
||||
|
||||
[lib]
|
||||
doctest = false
|
||||
@@ -1,292 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Per-request summary of data-plane KMS use, attached to the request's audit entry.
|
||||
|
||||
use crate::core::request::SseKmsPrincipal;
|
||||
use crate::error::SseError;
|
||||
use base64_simd::STANDARD as BASE64_STANDARD;
|
||||
use rustfs_kms::is_data_key_envelope;
|
||||
use rustfs_s3_types::S3ErrorCode;
|
||||
use rustfs_utils::http::object_encryption_keys::{INTERNAL_ENCRYPTION_KEY_HEADER, SSEType, normalize_managed_metadata};
|
||||
use serde_json::Value;
|
||||
use std::collections::{BTreeSet, HashMap};
|
||||
use std::sync::{Arc, LazyLock, Mutex, Weak};
|
||||
|
||||
/// Tag keys carrying the KMS summary on an S3 audit entry.
|
||||
///
|
||||
/// Audit consumers key off these strings, so they are a wire contract: append
|
||||
/// new keys rather than renaming existing ones.
|
||||
const KMS_AUDIT_TAG_SSE_TYPE: &str = "sseType";
|
||||
|
||||
const KMS_AUDIT_TAG_KEY_ID: &str = "kmsKeyId";
|
||||
|
||||
const KMS_AUDIT_TAG_KEY_VERSION: &str = "kmsKeyVersion";
|
||||
|
||||
const KMS_AUDIT_TAG_OUTCOME: &str = "kmsOutcome";
|
||||
|
||||
const KMS_AUDIT_TAG_ERROR_CLASS: &str = "kmsErrorClass";
|
||||
|
||||
/// Separator for the rare request that touched more than one key or scheme.
|
||||
const KMS_AUDIT_VALUE_SEPARATOR: &str = ",";
|
||||
|
||||
/// What the data path did with KMS while serving one S3 request.
|
||||
///
|
||||
/// This rides on the request's existing S3 audit entry instead of becoming its
|
||||
/// own event: the entry already carries the principal, request ID and API, so
|
||||
/// attaching the KMS outcome costs no extra event volume and needs no second
|
||||
/// correlation key.
|
||||
///
|
||||
/// # Redaction
|
||||
///
|
||||
/// Only the fields below may ever be recorded. The S3 audit entry fans out to
|
||||
/// every configured target, several of which sit outside the KMS trust
|
||||
/// boundary, so nothing derived from a data key — plaintext, ciphertext,
|
||||
/// envelope, nonce — nor any caller-supplied encryption-context value belongs
|
||||
/// here. Key identifiers and scheme names are configuration, not secrets.
|
||||
#[derive(Debug, Default)]
|
||||
struct KmsRequestAuditState {
|
||||
sse_types: BTreeSet<&'static str>,
|
||||
key_ids: BTreeSet<String>,
|
||||
key_versions: BTreeSet<u32>,
|
||||
/// Set by the first failure. A request that failed any KMS interaction is
|
||||
/// reported as failed even if others succeeded: a partially completed
|
||||
/// envelope operation is not a success for an audit reader.
|
||||
error_class: Option<&'static str>,
|
||||
recorded: bool,
|
||||
}
|
||||
|
||||
/// Shared accumulator for one request's KMS summary.
|
||||
///
|
||||
/// Written by the managed-SSE code paths through the request's principal, read
|
||||
/// once through [`KmsRequestAuditScope::audit_tags`] by whoever owns the
|
||||
/// request's audit entry.
|
||||
#[derive(Debug, Default)]
|
||||
pub(crate) struct KmsRequestAudit(Mutex<KmsRequestAuditState>);
|
||||
|
||||
impl KmsRequestAudit {
|
||||
pub(crate) fn record(
|
||||
&self,
|
||||
sse_type: SSEType,
|
||||
key_id: Option<&str>,
|
||||
key_version: Option<u32>,
|
||||
error_class: Option<&'static str>,
|
||||
) {
|
||||
// A poisoned lock costs the audit entry its KMS tags; it must never cost
|
||||
// the request its result.
|
||||
let Ok(mut state) = self.0.lock() else {
|
||||
return;
|
||||
};
|
||||
|
||||
state.recorded = true;
|
||||
state.sse_types.insert(sse_type.audit_label());
|
||||
if let Some(key_id) = key_id.filter(|value| !value.is_empty()) {
|
||||
state.key_ids.insert(key_id.to_string());
|
||||
}
|
||||
if let Some(key_version) = key_version {
|
||||
state.key_versions.insert(key_version);
|
||||
}
|
||||
if state.error_class.is_none() {
|
||||
state.error_class = error_class;
|
||||
}
|
||||
}
|
||||
|
||||
/// Render the summary as audit tags, or nothing when the request performed
|
||||
/// no KMS work at all (SSE-C and unencrypted objects never reach KMS).
|
||||
pub fn audit_tags(&self) -> Vec<(&'static str, Value)> {
|
||||
let Ok(state) = self.0.lock() else {
|
||||
return Vec::new();
|
||||
};
|
||||
if !state.recorded {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let mut tags = Vec::with_capacity(5);
|
||||
tags.push((KMS_AUDIT_TAG_SSE_TYPE, join_audit_values(state.sse_types.iter().copied())));
|
||||
if !state.key_ids.is_empty() {
|
||||
tags.push((KMS_AUDIT_TAG_KEY_ID, join_audit_values(state.key_ids.iter().map(String::as_str))));
|
||||
}
|
||||
if !state.key_versions.is_empty() {
|
||||
tags.push((
|
||||
KMS_AUDIT_TAG_KEY_VERSION,
|
||||
join_audit_values(state.key_versions.iter().map(u32::to_string)),
|
||||
));
|
||||
}
|
||||
let outcome = if state.error_class.is_some() { "failure" } else { "success" };
|
||||
tags.push((KMS_AUDIT_TAG_OUTCOME, Value::String(outcome.to_string())));
|
||||
if let Some(error_class) = state.error_class {
|
||||
tags.push((KMS_AUDIT_TAG_ERROR_CLASS, Value::String(error_class.to_string())));
|
||||
}
|
||||
|
||||
tags
|
||||
}
|
||||
}
|
||||
|
||||
/// Render a set of values as one tag value.
|
||||
///
|
||||
/// Always a string, including for the single-value case: a consumer that would
|
||||
/// have to branch on the JSON type of a tag is a consumer that will get it
|
||||
/// wrong for the request that happens to touch two keys.
|
||||
fn join_audit_values(values: impl Iterator<Item = impl AsRef<str>>) -> Value {
|
||||
let joined = values.map(|value| value.as_ref().to_string()).collect::<Vec<_>>();
|
||||
Value::String(joined.join(KMS_AUDIT_VALUE_SEPARATOR))
|
||||
}
|
||||
|
||||
/// Slots of the requests currently being audited, keyed by canonical request ID.
|
||||
///
|
||||
/// Only a weak reference is held: the owning [`KmsRequestAuditScope`] decides the
|
||||
/// lifetime, so a scope that is somehow not dropped cannot keep a slot alive.
|
||||
static KMS_REQUEST_AUDITS: LazyLock<Mutex<HashMap<String, Weak<KmsRequestAudit>>>> = LazyLock::new(|| Mutex::new(HashMap::new()));
|
||||
|
||||
/// Registration of one request's KMS audit slot, dropped with the request's
|
||||
/// audit entry.
|
||||
pub struct KmsRequestAuditScope {
|
||||
request_id: String,
|
||||
audit: Arc<KmsRequestAudit>,
|
||||
}
|
||||
|
||||
impl KmsRequestAuditScope {
|
||||
/// Open a slot for `request_id`, so managed-SSE operations served under it can
|
||||
/// be summarised onto its audit entry.
|
||||
pub fn register(request_id: &str) -> Self {
|
||||
let audit = Arc::new(KmsRequestAudit::default());
|
||||
if let Ok(mut registry) = KMS_REQUEST_AUDITS.lock() {
|
||||
registry.insert(request_id.to_string(), Arc::downgrade(&audit));
|
||||
}
|
||||
|
||||
Self {
|
||||
request_id: request_id.to_string(),
|
||||
audit,
|
||||
}
|
||||
}
|
||||
|
||||
/// Tags describing the KMS work recorded under this request.
|
||||
pub fn audit_tags(&self) -> Vec<(&'static str, Value)> {
|
||||
self.audit.audit_tags()
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for KmsRequestAuditScope {
|
||||
fn drop(&mut self) {
|
||||
if let Ok(mut registry) = KMS_REQUEST_AUDITS.lock() {
|
||||
registry.remove(&self.request_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolve the audit slot opened for `request_id`, if the request is being audited.
|
||||
pub(crate) fn kms_request_audit(request_id: &str) -> Option<Arc<KmsRequestAudit>> {
|
||||
KMS_REQUEST_AUDITS.lock().ok()?.get(request_id)?.upgrade()
|
||||
}
|
||||
|
||||
/// Failure classes for data-path failures that never reached a KMS backend.
|
||||
///
|
||||
/// Backend failures carry the classification defined by the KMS audit contract;
|
||||
/// everything else is classified at the S3 boundary it surfaced through.
|
||||
pub(crate) fn kms_data_plane_error_class(error: &SseError) -> &'static str {
|
||||
if let Some(failure) = error
|
||||
.source
|
||||
.as_ref()
|
||||
.and_then(|source| source.downcast_ref::<KmsDataPlaneFailure>())
|
||||
{
|
||||
return failure.class;
|
||||
}
|
||||
|
||||
match error.code {
|
||||
S3ErrorCode::AccessDenied => "access_denied",
|
||||
S3ErrorCode::InvalidArgument | S3ErrorCode::InvalidRequest => "invalid_argument",
|
||||
_ => "sse_internal",
|
||||
}
|
||||
}
|
||||
|
||||
/// Carries a KMS failure class across the conversion to an S3-facing error.
|
||||
///
|
||||
/// The class is decided while the `KmsError` is still typed; re-deriving it from
|
||||
/// a rendered message downstream would be guesswork.
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct KmsDataPlaneFailure {
|
||||
pub(crate) class: &'static str,
|
||||
pub(crate) source: Option<Box<dyn std::error::Error + Send + Sync>>,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for KmsDataPlaneFailure {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match &self.source {
|
||||
Some(source) => source.fmt(formatter),
|
||||
None => formatter.write_str(self.class),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for KmsDataPlaneFailure {
|
||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||
self.source
|
||||
.as_ref()
|
||||
.map(|source| source.as_ref() as &(dyn std::error::Error + 'static))
|
||||
}
|
||||
}
|
||||
|
||||
/// Record the outcome of one managed-SSE operation on the request's audit entry.
|
||||
///
|
||||
/// A `None` principal marks an internal caller — replication, lifecycle, heal —
|
||||
/// which has no S3 audit entry to attach to. `key_version` is a closure for
|
||||
/// exactly that caller: extracting the version means base64-decoding and
|
||||
/// parsing the stored envelope, work that must not run on the internal hot
|
||||
/// paths that discard it.
|
||||
pub(crate) fn record_managed_kms_outcome(
|
||||
principal: Option<&SseKmsPrincipal>,
|
||||
sse_type: SSEType,
|
||||
key_id: Option<&str>,
|
||||
key_version: impl FnOnce() -> Option<u32>,
|
||||
result: Result<(), &SseError>,
|
||||
) {
|
||||
let Some(audit) = principal.and_then(|principal| principal.request_audit.as_ref()) else {
|
||||
return;
|
||||
};
|
||||
|
||||
audit.record(sse_type, key_id, key_version(), result.err().map(kms_data_plane_error_class));
|
||||
}
|
||||
|
||||
/// Master-key version recorded in a managed-SSE data-key envelope, if the
|
||||
/// wrapping backend recorded one.
|
||||
///
|
||||
/// `None` is the honest answer for every other shape: Transit and AWS keep
|
||||
/// the version inside their own opaque ciphertext, Local records no version,
|
||||
/// and pre-versioning envelopes never carried the field. The single field is
|
||||
/// read through `serde_json::Value` rather than a full `DataKeyEnvelope`
|
||||
/// parse, so the audit path cannot double-count the envelope's unknown-field
|
||||
/// observability and touches nothing else in the envelope.
|
||||
pub(crate) fn envelope_master_key_version(envelope_bytes: &[u8]) -> Option<u32> {
|
||||
if !is_data_key_envelope(envelope_bytes) {
|
||||
return None;
|
||||
}
|
||||
u32::try_from(
|
||||
serde_json::from_slice::<Value>(envelope_bytes)
|
||||
.ok()?
|
||||
.get("master_key_version")?
|
||||
.as_u64()?,
|
||||
)
|
||||
.ok()
|
||||
}
|
||||
|
||||
/// Master-key version of the envelope stored on an object, for the audit
|
||||
/// summary of a read against that object.
|
||||
pub(crate) fn stored_envelope_master_key_version(metadata: &HashMap<String, String>) -> Option<u32> {
|
||||
// No context recoder: the recode only ever inserts the context key, which
|
||||
// this lookup never reads, so the normalized result is identical without it.
|
||||
let encoded = normalize_managed_metadata(metadata, None);
|
||||
let encoded = encoded.get(INTERNAL_ENCRYPTION_KEY_HEADER)?;
|
||||
let envelope = BASE64_STANDARD.decode_to_vec(encoded).ok()?;
|
||||
envelope_master_key_version(&envelope)
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! The encryption context a data key is bound to.
|
||||
|
||||
use crate::error::SseError;
|
||||
use base64_simd::STANDARD as BASE64_STANDARD;
|
||||
use rustfs_kms::types::ObjectEncryptionContext;
|
||||
use rustfs_utils::http::object_encryption_keys::MINIO_INTERNAL_ENCRYPTION_KMS_CONTEXT_HEADER;
|
||||
use rustfs_utils::path::path_join_buf;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub(crate) fn canonical_kms_bucket_path(bucket: &str, key: &str) -> String {
|
||||
path_join_buf(&[bucket, key])
|
||||
}
|
||||
|
||||
pub(crate) fn build_kms_request_context(
|
||||
bucket: &str,
|
||||
key: &str,
|
||||
provided_context: Option<&HashMap<String, String>>,
|
||||
) -> HashMap<String, String> {
|
||||
let mut context = provided_context.cloned().unwrap_or_default();
|
||||
context
|
||||
.entry(bucket.to_string())
|
||||
.or_insert_with(|| canonical_kms_bucket_path(bucket, key));
|
||||
context
|
||||
}
|
||||
|
||||
pub(crate) fn build_object_encryption_context(
|
||||
bucket: &str,
|
||||
key: &str,
|
||||
provided_context: Option<&HashMap<String, String>>,
|
||||
) -> ObjectEncryptionContext {
|
||||
let mut context = ObjectEncryptionContext::new(bucket.to_string(), key.to_string());
|
||||
for (ctx_key, ctx_value) in build_kms_request_context(bucket, key, provided_context) {
|
||||
context = context.with_encryption_context(ctx_key, ctx_value);
|
||||
}
|
||||
context
|
||||
}
|
||||
|
||||
pub(crate) fn encode_minio_kms_context(context: &HashMap<String, String>) -> Result<String, SseError> {
|
||||
let encoded = serde_json::to_vec(context).map_err(|e| SseError::internal(format!("Failed to serialize KMS context: {e}")))?;
|
||||
Ok(BASE64_STANDARD.encode_to_string(encoded))
|
||||
}
|
||||
|
||||
pub(crate) fn decode_minio_kms_context(metadata: &HashMap<String, String>) -> Result<Option<HashMap<String, String>>, SseError> {
|
||||
let Some(encoded) = metadata.get(MINIO_INTERNAL_ENCRYPTION_KMS_CONTEXT_HEADER) else {
|
||||
return Ok(None);
|
||||
};
|
||||
let decoded = BASE64_STANDARD
|
||||
.decode_to_vec(encoded)
|
||||
.map_err(|e| SseError::internal(format!("Failed to decode MinIO KMS context: {e}")))?;
|
||||
serde_json::from_slice(&decoded)
|
||||
.map(Some)
|
||||
.map_err(|e| SseError::internal(format!("Failed to parse MinIO KMS context: {e}")))
|
||||
}
|
||||
@@ -1,212 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Direct keying: the data key encrypts the object itself, under a stored base nonce.
|
||||
|
||||
#[cfg(not(feature = "rio-v2"))]
|
||||
use crate::core::keying::NewObjectKey;
|
||||
#[cfg(not(feature = "rio-v2"))]
|
||||
use crate::core::keying::material::EncryptionKeyKind;
|
||||
use crate::core::keying::{StoredDataKey, StoredKeyMode};
|
||||
use crate::error::SseError;
|
||||
use crate::host::SseHost;
|
||||
use base64_simd::STANDARD as BASE64_STANDARD;
|
||||
use md5::{Digest as Md5Digest, Md5};
|
||||
#[cfg(not(feature = "rio-v2"))]
|
||||
use rand::Rng;
|
||||
#[cfg(not(feature = "rio-v2"))]
|
||||
use rustfs_kms::DataKey;
|
||||
#[cfg(not(feature = "rio-v2"))]
|
||||
use rustfs_utils::http::object_encryption_keys::SSEType;
|
||||
use rustfs_utils::http::object_encryption_keys::{
|
||||
INTERNAL_ENCRYPTION_ALGORITHM_HEADER, INTERNAL_ENCRYPTION_IV_HEADER, INTERNAL_ENCRYPTION_KEY_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_IV_HEADER,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// SSE-C: record the base nonce the object is encrypted under.
|
||||
///
|
||||
/// The nonce is random, so decrypt reads it back instead of recomputing a
|
||||
/// deterministic value. It is written under both the RustFS and the MinIO key per
|
||||
/// repo convention. This covers a single PUT and a multipart session alike:
|
||||
/// CreateMultipartUpload builds the session metadata through the same call.
|
||||
pub(crate) fn record_customer_key_nonce(metadata: &mut HashMap<String, String>, base_nonce: [u8; 12]) {
|
||||
metadata.insert(INTERNAL_ENCRYPTION_IV_HEADER.to_string(), BASE64_STANDARD.encode_to_string(base_nonce));
|
||||
metadata.insert(
|
||||
MINIO_INTERNAL_ENCRYPTION_IV_HEADER.to_string(),
|
||||
BASE64_STANDARD.encode_to_string(base_nonce),
|
||||
);
|
||||
}
|
||||
|
||||
/// Managed SSE: record the wrapped data key, the nonce issued with it, and the cipher.
|
||||
pub(crate) fn record_wrapped_data_key(
|
||||
metadata: &mut HashMap<String, String>,
|
||||
wrapped_data_key: &[u8],
|
||||
base_nonce: [u8; 12],
|
||||
algorithm: &str,
|
||||
) {
|
||||
metadata.insert(
|
||||
INTERNAL_ENCRYPTION_KEY_HEADER.to_string(),
|
||||
BASE64_STANDARD.encode_to_string(wrapped_data_key),
|
||||
);
|
||||
metadata.insert(INTERNAL_ENCRYPTION_IV_HEADER.to_string(), BASE64_STANDARD.encode_to_string(base_nonce));
|
||||
metadata.insert(INTERNAL_ENCRYPTION_ALGORITHM_HEADER.to_string(), algorithm.to_string());
|
||||
}
|
||||
|
||||
/// Managed SSE: read back what [`record_wrapped_data_key`] stored.
|
||||
///
|
||||
/// `normalized_metadata` is the object's metadata with the managed keys normalized.
|
||||
pub(crate) async fn read_wrapped_data_key(
|
||||
host: &dyn SseHost,
|
||||
normalized_metadata: &HashMap<String, String>,
|
||||
) -> Result<StoredDataKey, SseError> {
|
||||
let (wrapped, iv, algorithm) = if let Some(service) = host.encryption_service().await {
|
||||
// Production mode: use service for metadata parsing
|
||||
let parsed = service
|
||||
.headers_to_metadata(normalized_metadata)
|
||||
.map_err(|e| SseError::internal(format!("Failed to parse encryption metadata: {e}")))?;
|
||||
|
||||
(parsed.encrypted_data_key, parsed.iv, parsed.algorithm)
|
||||
} else {
|
||||
// Test mode: parse metadata manually
|
||||
let encrypted_key_b64 = normalized_metadata
|
||||
.get(INTERNAL_ENCRYPTION_KEY_HEADER)
|
||||
.ok_or_else(|| SseError::internal("Missing encrypted key in metadata"))?;
|
||||
let encrypted_data_key = BASE64_STANDARD
|
||||
.decode_to_vec(encrypted_key_b64)
|
||||
.map_err(|e| SseError::internal(format!("Failed to decode encrypted key: {e}")))?;
|
||||
|
||||
let iv_b64 = normalized_metadata
|
||||
.get(INTERNAL_ENCRYPTION_IV_HEADER)
|
||||
.ok_or_else(|| SseError::internal("Missing IV in metadata"))?;
|
||||
let iv = BASE64_STANDARD
|
||||
.decode_to_vec(iv_b64)
|
||||
.map_err(|e| SseError::internal(format!("Failed to decode IV: {e}")))?;
|
||||
|
||||
let algorithm = normalized_metadata
|
||||
.get(INTERNAL_ENCRYPTION_ALGORITHM_HEADER)
|
||||
.cloned()
|
||||
.unwrap_or_else(|| "AES256".to_string());
|
||||
|
||||
(encrypted_data_key, iv, algorithm)
|
||||
};
|
||||
|
||||
let base_nonce = <[u8; 12]>::try_from(iv.as_slice())
|
||||
.map_err(|_| SseError::internal("Invalid encryption nonce length; expected 12 bytes"))?;
|
||||
|
||||
Ok(StoredDataKey {
|
||||
wrapped,
|
||||
algorithm,
|
||||
mode: StoredKeyMode::Direct { base_nonce },
|
||||
})
|
||||
}
|
||||
|
||||
/// Resolve the SSE-C Direct base nonce for decryption.
|
||||
///
|
||||
/// New objects persist a random nonce at encryption time (see
|
||||
/// [`record_customer_key_nonce`]) under `INTERNAL_ENCRYPTION_IV_HEADER` (preferred) or,
|
||||
/// for MinIO interop, `MINIO_INTERNAL_ENCRYPTION_IV_HEADER`. Legacy objects written before
|
||||
/// random nonces were persisted carry no stored IV; for those we fall back to the
|
||||
/// deterministic `generate_ssec_nonce(bucket, key)` value they were originally encrypted
|
||||
/// with, so previously stored objects still decrypt correctly.
|
||||
pub(crate) fn read_stored_ssec_nonce(metadata: &HashMap<String, String>, bucket: &str, key: &str) -> [u8; 12] {
|
||||
metadata
|
||||
.get(INTERNAL_ENCRYPTION_IV_HEADER)
|
||||
.or_else(|| metadata.get(MINIO_INTERNAL_ENCRYPTION_IV_HEADER))
|
||||
.and_then(|encoded| BASE64_STANDARD.decode_to_vec(encoded).ok())
|
||||
.and_then(|bytes| <[u8; 12]>::try_from(bytes.as_slice()).ok())
|
||||
.unwrap_or_else(|| generate_ssec_nonce(bucket, key))
|
||||
}
|
||||
|
||||
/// Generate deterministic nonce for SSE-C encryption
|
||||
///
|
||||
/// The nonce is derived from the bucket and key to ensure:
|
||||
/// 1. Same object always gets the same nonce (required for SSE-C)
|
||||
/// 2. Different objects get different nonces
|
||||
pub(crate) fn generate_ssec_nonce(bucket: &str, key: &str) -> [u8; 12] {
|
||||
let nonce_source = format!("{bucket}-{key}");
|
||||
let nonce_hash: [u8; 16] = Md5::digest(nonce_source.as_bytes()).into();
|
||||
let mut nonce = [0u8; 12];
|
||||
nonce.copy_from_slice(&nonce_hash[..12]);
|
||||
nonce
|
||||
}
|
||||
|
||||
/// Whether a multipart session needs the customer key when it is created.
|
||||
///
|
||||
/// Direct mode records only a nonce for the session; the customer key is checked
|
||||
/// per part, against what the session recorded.
|
||||
#[cfg(not(feature = "rio-v2"))]
|
||||
pub(crate) const SESSION_KEY_NEEDS_CUSTOMER_KEY: bool = false;
|
||||
|
||||
#[cfg(not(feature = "rio-v2"))]
|
||||
fn random_base_nonce() -> [u8; 12] {
|
||||
let mut base_nonce = [0u8; 12];
|
||||
rand::rng().fill_bytes(&mut base_nonce);
|
||||
base_nonce
|
||||
}
|
||||
|
||||
/// SSE-C object: the customer key encrypts the object itself.
|
||||
///
|
||||
/// Uses a fresh random nonce per encryption. A deterministic nonce derived from
|
||||
/// (bucket, key) would repeat whenever the same object is overwritten under the same
|
||||
/// SSE-C key, reusing an identical (key, nonce) pair and catastrophically breaking
|
||||
/// AES-256-GCM. The nonce is persisted (see [`record_customer_key_nonce`]) and read
|
||||
/// back on decrypt (see [`read_stored_ssec_nonce`]).
|
||||
#[cfg(not(feature = "rio-v2"))]
|
||||
pub(crate) fn new_key_under_customer_key(customer_key: [u8; 32], _bucket: &str, _key: &str) -> Result<NewObjectKey, SseError> {
|
||||
Ok(NewObjectKey {
|
||||
key_bytes: customer_key,
|
||||
base_nonce: random_base_nonce(),
|
||||
key_kind: EncryptionKeyKind::Direct,
|
||||
sealed_key: None,
|
||||
})
|
||||
}
|
||||
|
||||
/// SSE-C multipart session: a nonce every part of the upload shares.
|
||||
///
|
||||
/// Generates a real random nonce for the session and persists it (see
|
||||
/// [`record_customer_key_nonce`]). Every part of this upload reads the same
|
||||
/// persisted nonce back, so all parts share one nonce without reintroducing the
|
||||
/// deterministic (bucket, key) reuse hazard. `key_bytes` stays a placeholder: it is
|
||||
/// neither used for encryption nor persisted; the real customer key is validated per
|
||||
/// part on upload.
|
||||
#[cfg(not(feature = "rio-v2"))]
|
||||
pub(crate) fn new_session_key_under_customer_key(
|
||||
_customer_key: Option<[u8; 32]>,
|
||||
_bucket: &str,
|
||||
_key: &str,
|
||||
) -> Result<NewObjectKey, SseError> {
|
||||
Ok(NewObjectKey {
|
||||
key_bytes: [0; 32],
|
||||
base_nonce: random_base_nonce(),
|
||||
key_kind: EncryptionKeyKind::Direct,
|
||||
sealed_key: None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Managed SSE: the data key encrypts the object itself, under the nonce issued with it.
|
||||
#[cfg(not(feature = "rio-v2"))]
|
||||
pub(crate) fn new_key_under_data_key(
|
||||
data_key: &DataKey,
|
||||
_sse_type: SSEType,
|
||||
_bucket: &str,
|
||||
_key: &str,
|
||||
) -> Result<NewObjectKey, SseError> {
|
||||
Ok(NewObjectKey {
|
||||
key_bytes: data_key.plaintext_key,
|
||||
base_nonce: data_key.nonce,
|
||||
key_kind: EncryptionKeyKind::Direct,
|
||||
sealed_key: None,
|
||||
})
|
||||
}
|
||||
@@ -1,115 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! The key material an encrypted write or read runs on.
|
||||
|
||||
#[cfg(feature = "rio-v2")]
|
||||
use crate::core::keying::object::{SEALED_KEY_IV_SIZE, SEALED_KEY_SIZE};
|
||||
use rustfs_utils::http::object_encryption_keys::SSEType;
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// Encryption material returned by [`sse_encryption`] / [`sse_prepare_encryption`].
|
||||
pub struct EncryptionMaterial {
|
||||
#[allow(unused)]
|
||||
pub sse_type: SSEType,
|
||||
pub server_side_encryption: String,
|
||||
pub kms_key_id: Option<String>,
|
||||
|
||||
#[allow(unused)]
|
||||
pub algorithm: String,
|
||||
|
||||
/// Encryption key bytes
|
||||
pub key_bytes: [u8; 32],
|
||||
/// Base nonce/IV used by rio to derive block/part nonces.
|
||||
pub base_nonce: [u8; 12],
|
||||
/// Encrypted DEK for managed SSE. Absent for SSE-C.
|
||||
pub encrypted_data_key: Option<Vec<u8>>,
|
||||
/// SSE-C key MD5 if customer-managed encryption is in use.
|
||||
pub customer_key_md5: Option<String>,
|
||||
/// Original plaintext size when it should be persisted alongside metadata.
|
||||
pub original_size: Option<i64>,
|
||||
/// Indicates whether `key_bytes` is a direct stream key or a MinIO-style object key.
|
||||
pub key_kind: EncryptionKeyKind,
|
||||
/// Original client-provided SSE-KMS context, stored in MinIO-compatible metadata.
|
||||
pub managed_kms_context: Option<HashMap<String, String>>,
|
||||
/// MinIO-compatible sealed object-key metadata for managed SSE object-key mode.
|
||||
#[cfg_attr(not(feature = "rio-v2"), allow(dead_code))]
|
||||
pub managed_sealed_key: Option<ManagedSealedKey>,
|
||||
}
|
||||
|
||||
/// Decryption material returned by [`sse_decryption`].
|
||||
pub struct DecryptionMaterial {
|
||||
#[allow(unused)]
|
||||
pub sse_type: SSEType,
|
||||
pub server_side_encryption: String,
|
||||
pub kms_key_id: Option<String>,
|
||||
#[allow(unused)]
|
||||
pub algorithm: String,
|
||||
pub customer_key_md5: Option<String>, // if use SSE-C, check key md5
|
||||
|
||||
/// Decryption key bytes
|
||||
pub key_bytes: [u8; 32],
|
||||
/// Base nonce/IV used by rio to derive block/part nonces.
|
||||
pub base_nonce: [u8; 12],
|
||||
/// Indicates whether `key_bytes` is a direct stream key or a MinIO-style object key.
|
||||
pub key_kind: EncryptionKeyKind,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for EncryptionMaterial {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("EncryptionMaterial")
|
||||
.field("sse_type", &self.sse_type)
|
||||
.field("server_side_encryption", &self.server_side_encryption)
|
||||
.field("original_size", &self.original_size)
|
||||
.finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for DecryptionMaterial {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("DecryptionMaterial")
|
||||
.field("sse_type", &self.sse_type)
|
||||
.field("server_side_encryption", &self.server_side_encryption)
|
||||
.finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum EncryptionKeyKind {
|
||||
Direct,
|
||||
Object,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ManagedSealedKey {
|
||||
#[cfg(feature = "rio-v2")]
|
||||
pub iv: [u8; SEALED_KEY_IV_SIZE],
|
||||
#[cfg(feature = "rio-v2")]
|
||||
pub sealed_key: [u8; SEALED_KEY_SIZE],
|
||||
}
|
||||
|
||||
impl EncryptionMaterial {
|
||||
/// The KMS key id a write response may advertise.
|
||||
///
|
||||
/// `kms_key_id` is always set for managed SSE because SSE-S3 also wraps
|
||||
/// its data key under the service default key, but that key is an
|
||||
/// internal detail of an `AES256` object: only an `aws:kms` object names
|
||||
/// a key the caller can act on, and `x-amz-server-side-encryption-aws-kms-key-id`
|
||||
/// is defined only for that scheme.
|
||||
pub fn response_kms_key_id(&self) -> Option<String> {
|
||||
matches!(self.sse_type, SSEType::SseKms)
|
||||
.then(|| self.kms_key_id.clone())
|
||||
.flatten()
|
||||
}
|
||||
}
|
||||
@@ -1,270 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Encryption metadata as it is stored with the object.
|
||||
|
||||
use crate::core::keying::context::encode_minio_kms_context;
|
||||
use crate::core::keying::direct::{record_customer_key_nonce, record_wrapped_data_key};
|
||||
use crate::core::keying::material::{EncryptionKeyKind, EncryptionMaterial};
|
||||
#[cfg(feature = "rio-v2")]
|
||||
use crate::core::keying::object::{record_data_key_in_minio_slots, record_sealed_customer_key};
|
||||
use crate::error::SseError;
|
||||
use base64_simd::STANDARD as BASE64_STANDARD;
|
||||
use rustfs_utils::http::object_encryption_keys::{
|
||||
INTERNAL_ENCRYPTION_ALGORITHM_HEADER, INTERNAL_ENCRYPTION_CONTEXT_HEADER, INTERNAL_ENCRYPTION_IV_HEADER,
|
||||
INTERNAL_ENCRYPTION_KEY_HEADER, INTERNAL_ENCRYPTION_KEY_ID_HEADER, INTERNAL_ENCRYPTION_ORIGINAL_SIZE_HEADER,
|
||||
INTERNAL_ENCRYPTION_TAG_HEADER, MINIO_INTERNAL_ENCRYPTION_ALGORITHM_HEADER, MINIO_INTERNAL_ENCRYPTION_IV_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_KMS_CONTEXT_HEADER, MINIO_INTERNAL_ENCRYPTION_KMS_DATA_KEY_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_KMS_KEY_ID_HEADER, MINIO_INTERNAL_ENCRYPTION_KMS_SEALED_KEY_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_MULTIPART_HEADER, MINIO_INTERNAL_ENCRYPTION_S3_SEALED_KEY_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_SSEC_SEALED_KEY_HEADER, SSEC_ORIGINAL_SIZE_HEADER, SSEType,
|
||||
};
|
||||
use std::borrow::Cow;
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// Two spellings of one encryption metadata key carry different values.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct ConflictingEncryptionMetadata {
|
||||
/// The canonical key whose case-variants disagree.
|
||||
pub key: &'static str,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for ConflictingEncryptionMetadata {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(formatter, "conflicting object encryption metadata for {}", self.key)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for ConflictingEncryptionMetadata {}
|
||||
|
||||
/// Fold case-variant spellings of the encryption metadata keys onto their canonical names.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// When two spellings of the same key hold different values: picking either
|
||||
/// would decrypt with material the object may not have been written under.
|
||||
pub fn normalize_encryption_metadata_case(
|
||||
metadata: &HashMap<String, String>,
|
||||
) -> Result<Cow<'_, HashMap<String, String>>, ConflictingEncryptionMetadata> {
|
||||
const CANONICAL_KEYS: &[&str] = &[
|
||||
"x-amz-server-side-encryption",
|
||||
"x-amz-server-side-encryption-aws-kms-key-id",
|
||||
"x-amz-server-side-encryption-customer-algorithm",
|
||||
"x-amz-server-side-encryption-customer-key-md5",
|
||||
SSEC_ORIGINAL_SIZE_HEADER,
|
||||
INTERNAL_ENCRYPTION_KEY_ID_HEADER,
|
||||
INTERNAL_ENCRYPTION_KEY_HEADER,
|
||||
INTERNAL_ENCRYPTION_ALGORITHM_HEADER,
|
||||
INTERNAL_ENCRYPTION_IV_HEADER,
|
||||
INTERNAL_ENCRYPTION_CONTEXT_HEADER,
|
||||
INTERNAL_ENCRYPTION_TAG_HEADER,
|
||||
INTERNAL_ENCRYPTION_ORIGINAL_SIZE_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_MULTIPART_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_IV_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_ALGORITHM_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_SSEC_SEALED_KEY_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_S3_SEALED_KEY_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_KMS_SEALED_KEY_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_KMS_KEY_ID_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_KMS_CONTEXT_HEADER,
|
||||
];
|
||||
|
||||
let needs_normalization = metadata.keys().any(|key| {
|
||||
CANONICAL_KEYS
|
||||
.iter()
|
||||
.any(|canonical| key != canonical && key.eq_ignore_ascii_case(canonical))
|
||||
});
|
||||
if !needs_normalization {
|
||||
return Ok(Cow::Borrowed(metadata));
|
||||
}
|
||||
|
||||
let mut normalized = metadata.clone();
|
||||
for canonical in CANONICAL_KEYS {
|
||||
let mut matching_values = metadata
|
||||
.iter()
|
||||
.filter_map(|(key, value)| key.eq_ignore_ascii_case(canonical).then_some(value));
|
||||
let Some(value) = matching_values.next() else {
|
||||
continue;
|
||||
};
|
||||
if matching_values.any(|candidate| candidate != value) {
|
||||
return Err(ConflictingEncryptionMetadata { key: canonical });
|
||||
}
|
||||
if !normalized.contains_key(*canonical) {
|
||||
normalized.insert((*canonical).to_string(), value.clone());
|
||||
}
|
||||
}
|
||||
Ok(Cow::Owned(normalized))
|
||||
}
|
||||
|
||||
/// Turn the material of an encrypted write into the metadata stored with the object.
|
||||
///
|
||||
/// The scheme is recorded here. How the key itself is recorded belongs to the mode
|
||||
/// that formed it, in `direct.rs` and `object.rs`, next to the code that reads it back.
|
||||
pub fn encryption_material_to_metadata(material: &EncryptionMaterial) -> Result<HashMap<String, String>, SseError> {
|
||||
let mut metadata = HashMap::new();
|
||||
|
||||
match material.sse_type {
|
||||
SSEType::SseC => {
|
||||
record_ssec_scheme(&mut metadata, material);
|
||||
|
||||
// A `rio-v2` build seals an object key instead, so a Direct key there is a
|
||||
// multipart session created without its customer key.
|
||||
if material.key_kind == EncryptionKeyKind::Direct {
|
||||
record_customer_key_nonce(&mut metadata, material.base_nonce);
|
||||
}
|
||||
#[cfg(feature = "rio-v2")]
|
||||
if let Some(sealed) = &material.managed_sealed_key {
|
||||
record_sealed_customer_key(&mut metadata, sealed);
|
||||
}
|
||||
}
|
||||
SSEType::SseS3 | SSEType::SseKms => {
|
||||
let wrapped_data_key = material
|
||||
.encrypted_data_key
|
||||
.as_deref()
|
||||
.ok_or_else(|| SseError::internal("managed SSE materials must carry an encrypted data key"))?;
|
||||
record_managed_scheme(&mut metadata, material);
|
||||
|
||||
if material.key_kind == EncryptionKeyKind::Direct {
|
||||
record_wrapped_data_key(&mut metadata, wrapped_data_key, material.base_nonce, material.algorithm.as_str());
|
||||
}
|
||||
#[cfg(feature = "rio-v2")]
|
||||
record_data_key_in_minio_slots(&mut metadata, material, wrapped_data_key);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(metadata)
|
||||
}
|
||||
|
||||
/// SSE-C: the scheme, the fingerprint of the customer key, and the plaintext size.
|
||||
fn record_ssec_scheme(metadata: &mut HashMap<String, String>, material: &EncryptionMaterial) {
|
||||
metadata.insert(
|
||||
"x-amz-server-side-encryption".to_string(),
|
||||
material.server_side_encryption.as_str().to_string(),
|
||||
);
|
||||
metadata.insert(
|
||||
"x-amz-server-side-encryption-customer-algorithm".to_string(),
|
||||
material.algorithm.as_str().to_string(),
|
||||
);
|
||||
if let Some(customer_key_md5) = &material.customer_key_md5 {
|
||||
metadata.insert("x-amz-server-side-encryption-customer-key-md5".to_string(), customer_key_md5.to_string());
|
||||
}
|
||||
if let Some(original_size) = material.original_size {
|
||||
metadata.insert(SSEC_ORIGINAL_SIZE_HEADER.to_string(), original_size.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
/// Managed SSE: the scheme, the key the data key is wrapped under, the plaintext
|
||||
/// size, and the encryption context of the request.
|
||||
fn record_managed_scheme(metadata: &mut HashMap<String, String>, material: &EncryptionMaterial) {
|
||||
metadata.insert(
|
||||
"x-amz-server-side-encryption".to_string(),
|
||||
material.server_side_encryption.as_str().to_string(),
|
||||
);
|
||||
|
||||
let internal_key_id = material.kms_key_id.clone().unwrap_or_else(|| "default".to_string());
|
||||
metadata.insert(INTERNAL_ENCRYPTION_KEY_ID_HEADER.to_string(), internal_key_id.clone());
|
||||
|
||||
if matches!(material.sse_type, SSEType::SseKms) {
|
||||
metadata.insert("x-amz-server-side-encryption-aws-kms-key-id".to_string(), internal_key_id);
|
||||
}
|
||||
|
||||
if let Some(original_size) = material.original_size {
|
||||
metadata.insert(INTERNAL_ENCRYPTION_ORIGINAL_SIZE_HEADER.to_string(), original_size.to_string());
|
||||
}
|
||||
|
||||
if let Some(kms_context) = &material.managed_kms_context
|
||||
&& !kms_context.is_empty()
|
||||
{
|
||||
if let Ok(serialized) = serde_json::to_string(kms_context) {
|
||||
metadata.insert(INTERNAL_ENCRYPTION_CONTEXT_HEADER.to_string(), serialized);
|
||||
}
|
||||
if matches!(material.sse_type, SSEType::SseKms)
|
||||
&& let Ok(encoded) = encode_minio_kms_context(kms_context)
|
||||
{
|
||||
metadata.insert(MINIO_INTERNAL_ENCRYPTION_KMS_CONTEXT_HEADER.to_string(), encoded);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Strip source encryption metadata before constructing metadata for a copy destination.
|
||||
///
|
||||
/// Encryption metadata describes the physical source representation and must never be
|
||||
/// inherited by a plaintext destination or by a destination using a different key.
|
||||
pub fn strip_managed_encryption_metadata(metadata: &mut HashMap<String, String>) {
|
||||
const KEYS: [&str; 21] = [
|
||||
"x-amz-server-side-encryption",
|
||||
"x-amz-server-side-encryption-aws-kms-key-id",
|
||||
"x-amz-server-side-encryption-customer-algorithm",
|
||||
"x-amz-server-side-encryption-customer-key-md5",
|
||||
SSEC_ORIGINAL_SIZE_HEADER,
|
||||
INTERNAL_ENCRYPTION_KEY_ID_HEADER,
|
||||
INTERNAL_ENCRYPTION_ALGORITHM_HEADER,
|
||||
INTERNAL_ENCRYPTION_IV_HEADER,
|
||||
INTERNAL_ENCRYPTION_TAG_HEADER,
|
||||
INTERNAL_ENCRYPTION_KEY_HEADER,
|
||||
INTERNAL_ENCRYPTION_CONTEXT_HEADER,
|
||||
INTERNAL_ENCRYPTION_ORIGINAL_SIZE_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_MULTIPART_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_IV_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_ALGORITHM_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_SSEC_SEALED_KEY_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_S3_SEALED_KEY_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_KMS_SEALED_KEY_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_KMS_KEY_ID_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_KMS_DATA_KEY_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_KMS_CONTEXT_HEADER,
|
||||
];
|
||||
|
||||
for key in KEYS.iter() {
|
||||
metadata.remove(*key);
|
||||
}
|
||||
rustfs_utils::http::metadata_compat::remove_str(metadata, rustfs_utils::http::SUFFIX_OPAQUE_ENCRYPTED_ETAG);
|
||||
}
|
||||
|
||||
pub fn mark_encrypted_multipart_metadata(metadata: &mut HashMap<String, String>) {
|
||||
metadata.insert(MINIO_INTERNAL_ENCRYPTION_MULTIPART_HEADER.to_string(), String::new());
|
||||
}
|
||||
|
||||
/// True when the stored metadata marks the object as SSE-C protected.
|
||||
///
|
||||
/// RustFS persists the public `x-amz-server-side-encryption-customer-algorithm`
|
||||
/// key on SSE-C writes. MinIO persists only its internal sealed-key slot and
|
||||
/// synthesizes the public header onto responses (backlog#1638), so the slot is
|
||||
/// the only durable evidence on a MinIO-written object. That shape is readable
|
||||
/// solely through the rio-v2 sealed-object-key path, so it participates in
|
||||
/// detection only in builds that can actually read it — default builds keep
|
||||
/// the resolver's fail-closed diagnosis instead of demanding customer keys
|
||||
/// they could not use.
|
||||
pub fn stored_ssec_metadata(metadata: &HashMap<String, String>) -> bool {
|
||||
metadata.contains_key("x-amz-server-side-encryption-customer-algorithm") || minio_sealed_ssec_metadata(metadata)
|
||||
}
|
||||
|
||||
/// True when the object carries MinIO's SSE-C sealed-key slot — the shape that
|
||||
/// stores no customer-key MD5 and proves the key through the AEAD unseal
|
||||
/// instead.
|
||||
pub(crate) fn minio_sealed_ssec_metadata(metadata: &HashMap<String, String>) -> bool {
|
||||
cfg!(feature = "rio-v2") && metadata.contains_key(MINIO_INTERNAL_ENCRYPTION_SSEC_SEALED_KEY_HEADER)
|
||||
}
|
||||
|
||||
/// Recodes a stored MinIO KMS context value (base64-wrapped JSON) into the
|
||||
/// plain-JSON form RustFS stores under [`INTERNAL_ENCRYPTION_CONTEXT_HEADER`].
|
||||
///
|
||||
/// Injected into the shared [`normalize_managed_metadata`] because the shared
|
||||
/// crate carries no JSON codec; any decode failure returns `None`, which skips
|
||||
/// the context mapping exactly like the historical inline `if let Ok` chain.
|
||||
pub(crate) fn recode_minio_kms_context(value: &str) -> Option<String> {
|
||||
let decoded = BASE64_STANDARD.decode_to_vec(value).ok()?;
|
||||
let context = serde_json::from_slice::<HashMap<String, String>>(&decoded).ok()?;
|
||||
serde_json::to_string(&context).ok()
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! The internal keying modes: how an object's key is formed and how it is stored.
|
||||
|
||||
pub(crate) mod context;
|
||||
pub(crate) mod direct;
|
||||
pub(crate) mod material;
|
||||
pub(crate) mod metadata;
|
||||
#[cfg(feature = "rio-v2")]
|
||||
pub(crate) mod object;
|
||||
pub(crate) mod stored;
|
||||
|
||||
use crate::core::keying::material::{EncryptionKeyKind, ManagedSealedKey};
|
||||
|
||||
/// The key a new object is encrypted with, in the mode this build writes.
|
||||
pub(crate) struct NewObjectKey {
|
||||
/// Key handed to the stream cipher.
|
||||
pub(crate) key_bytes: [u8; 32],
|
||||
/// Base nonce the stream cipher derives its block nonces from.
|
||||
pub(crate) base_nonce: [u8; 12],
|
||||
/// The mode `key_bytes` belongs to.
|
||||
pub(crate) key_kind: EncryptionKeyKind,
|
||||
/// The sealed form to store, in a mode that seals a separate object key.
|
||||
pub(crate) sealed_key: Option<ManagedSealedKey>,
|
||||
}
|
||||
|
||||
/// A managed-SSE data key as a stored object recorded it, still wrapped.
|
||||
pub(crate) struct StoredDataKey {
|
||||
/// The data key, wrapped by the provider that issued it.
|
||||
pub(crate) wrapped: Vec<u8>,
|
||||
/// The cipher the object recorded.
|
||||
pub(crate) algorithm: String,
|
||||
/// What the data key is to the object once it is unwrapped.
|
||||
pub(crate) mode: StoredKeyMode,
|
||||
}
|
||||
|
||||
/// The mode a stored key was recorded in.
|
||||
pub(crate) enum StoredKeyMode {
|
||||
/// The data key is the object's key, under the nonce stored beside it.
|
||||
Direct { base_nonce: [u8; 12] },
|
||||
/// The data key unseals the object's key.
|
||||
#[cfg(feature = "rio-v2")]
|
||||
Object { sealed_key: ManagedSealedKey },
|
||||
}
|
||||
|
||||
// The mode new objects are written in. Every build reads every mode it can
|
||||
// recognize; writing picks exactly one, and this is the only place that says which.
|
||||
#[cfg(not(feature = "rio-v2"))]
|
||||
pub(crate) use direct::{
|
||||
SESSION_KEY_NEEDS_CUSTOMER_KEY, new_key_under_customer_key, new_key_under_data_key, new_session_key_under_customer_key,
|
||||
};
|
||||
#[cfg(feature = "rio-v2")]
|
||||
pub(crate) use object::{
|
||||
SESSION_KEY_NEEDS_CUSTOMER_KEY, new_key_under_customer_key, new_key_under_data_key, new_session_key_under_customer_key,
|
||||
};
|
||||
@@ -1,436 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Object keying: a derived object key, sealed in the MinIO-compatible format.
|
||||
|
||||
use crate::core::keying::material::{EncryptionKeyKind, EncryptionMaterial, ManagedSealedKey};
|
||||
use crate::core::keying::{NewObjectKey, StoredDataKey, StoredKeyMode};
|
||||
use crate::error::SseError;
|
||||
use aes_gcm::aead::{Aead, KeyInit, Payload};
|
||||
use aes_gcm::{Aes256Gcm, Nonce};
|
||||
use base64_simd::STANDARD as BASE64_STANDARD;
|
||||
use chacha20poly1305::ChaCha20Poly1305;
|
||||
use hmac::{Hmac, Mac};
|
||||
use rand::RngExt;
|
||||
use rustfs_kms::DataKey;
|
||||
use rustfs_utils::http::object_encryption_keys::{
|
||||
INTERNAL_ENCRYPTION_ALGORITHM_HEADER, INTERNAL_ENCRYPTION_IV_HEADER, INTERNAL_ENCRYPTION_KEY_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_ALGORITHM_HEADER, MINIO_INTERNAL_ENCRYPTION_IV_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_KMS_DATA_KEY_HEADER, MINIO_INTERNAL_ENCRYPTION_KMS_KEY_ID_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_KMS_SEALED_KEY_HEADER, MINIO_INTERNAL_ENCRYPTION_S3_SEALED_KEY_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_SSEC_SEALED_KEY_HEADER, SSEType,
|
||||
};
|
||||
use sha2::Sha256;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub(crate) const MINIO_INTERNAL_ENCRYPTION_SEAL_ALGORITHM: &str = "DAREv2-HMAC-SHA256";
|
||||
|
||||
const DARE_VERSION_20: u8 = 0x20;
|
||||
|
||||
pub(crate) const DARE_CIPHER_AES_256_GCM: u8 = 0x00;
|
||||
|
||||
pub(crate) const DARE_CIPHER_CHACHA20_POLY1305: u8 = 0x01;
|
||||
|
||||
const DARE_HEADER_SIZE: usize = 16;
|
||||
|
||||
const DARE_TAG_SIZE: usize = 16;
|
||||
|
||||
pub(crate) const SEALED_KEY_IV_SIZE: usize = 32;
|
||||
|
||||
pub(crate) const SEALED_KEY_SIZE: usize = DARE_HEADER_SIZE + 32 + DARE_TAG_SIZE;
|
||||
|
||||
const OBJECT_KEY_DERIVATION_CONTEXT: &[u8] = b"object-encryption-key generation";
|
||||
|
||||
pub(crate) type HmacSha256 = Hmac<Sha256>;
|
||||
|
||||
fn managed_sse_domain(sse_type: SSEType) -> &'static str {
|
||||
match sse_type {
|
||||
SSEType::SseS3 => "SSE-S3",
|
||||
SSEType::SseKms => "SSE-KMS",
|
||||
SSEType::SseC => "SSE-C",
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn is_supported_sealed_object_key_cipher(cipher: u8) -> bool {
|
||||
matches!(cipher, DARE_CIPHER_AES_256_GCM | DARE_CIPHER_CHACHA20_POLY1305)
|
||||
}
|
||||
|
||||
fn decrypt_sealed_object_key_payload(sealing_key: [u8; 32], header: &[u8], sealed_key: &[u8]) -> Result<Vec<u8>, SseError> {
|
||||
let nonce = &header[4..16];
|
||||
let ciphertext = &sealed_key[DARE_HEADER_SIZE..];
|
||||
let aad = &header[..4];
|
||||
match header[1] {
|
||||
DARE_CIPHER_AES_256_GCM => {
|
||||
let cipher = Aes256Gcm::new_from_slice(&sealing_key)
|
||||
.map_err(|err| SseError::internal(format!("Invalid AES-GCM sealing key: {err}")))?;
|
||||
let nonce = Nonce::try_from(nonce).map_err(|_| SseError::internal("Invalid sealed object-key package nonce"))?;
|
||||
cipher.decrypt(&nonce, Payload { msg: ciphertext, aad })
|
||||
}
|
||||
DARE_CIPHER_CHACHA20_POLY1305 => {
|
||||
let cipher = ChaCha20Poly1305::new_from_slice(&sealing_key)
|
||||
.map_err(|err| SseError::internal(format!("Invalid ChaCha20-Poly1305 sealing key: {err}")))?;
|
||||
let nonce = chacha20poly1305::Nonce::try_from(nonce)
|
||||
.map_err(|_| SseError::internal("Invalid sealed object-key package nonce"))?;
|
||||
cipher.decrypt(&nonce, Payload { msg: ciphertext, aad })
|
||||
}
|
||||
_ => return Err(SseError::internal("Unsupported sealed object-key DARE header")),
|
||||
}
|
||||
.map_err(|err| SseError::internal(format!("Failed to unseal object key: {err}")))
|
||||
}
|
||||
|
||||
fn canonical_sse_path(bucket: &str, object: &str) -> String {
|
||||
let bucket = bucket.trim_matches('/');
|
||||
let object = object.trim_matches('/');
|
||||
if object.is_empty() {
|
||||
bucket.to_string()
|
||||
} else if bucket.is_empty() {
|
||||
object.to_string()
|
||||
} else {
|
||||
format!("{bucket}/{object}")
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn derive_object_key(external_key: [u8; 32]) -> Result<[u8; 32], SseError> {
|
||||
let mut random = [0u8; 32];
|
||||
rand::rng().fill(&mut random);
|
||||
|
||||
let mut mac = HmacSha256::new_from_slice(&external_key)
|
||||
.map_err(|err| SseError::internal(format!("Invalid HMAC key for object-key derivation: {err}")))?;
|
||||
mac.update(OBJECT_KEY_DERIVATION_CONTEXT);
|
||||
mac.update(&random);
|
||||
|
||||
let mut object_key = [0u8; 32];
|
||||
object_key.copy_from_slice(mac.finalize().into_bytes().as_slice());
|
||||
Ok(object_key)
|
||||
}
|
||||
|
||||
fn derive_sealing_key(
|
||||
external_key: [u8; 32],
|
||||
iv: [u8; SEALED_KEY_IV_SIZE],
|
||||
domain: &str,
|
||||
bucket: &str,
|
||||
object: &str,
|
||||
) -> Result<[u8; 32], SseError> {
|
||||
let mut mac = HmacSha256::new_from_slice(&external_key)
|
||||
.map_err(|err| SseError::internal(format!("Invalid HMAC key for sealing-key derivation: {err}")))?;
|
||||
mac.update(&iv);
|
||||
mac.update(domain.as_bytes());
|
||||
mac.update(MINIO_INTERNAL_ENCRYPTION_SEAL_ALGORITHM.as_bytes());
|
||||
mac.update(canonical_sse_path(bucket, object).as_bytes());
|
||||
|
||||
let mut sealing_key = [0u8; 32];
|
||||
sealing_key.copy_from_slice(mac.finalize().into_bytes().as_slice());
|
||||
Ok(sealing_key)
|
||||
}
|
||||
|
||||
pub(crate) fn seal_object_key(
|
||||
object_key: [u8; 32],
|
||||
external_key: [u8; 32],
|
||||
sse_type: SSEType,
|
||||
bucket: &str,
|
||||
object: &str,
|
||||
) -> Result<ManagedSealedKey, SseError> {
|
||||
let mut iv = [0u8; SEALED_KEY_IV_SIZE];
|
||||
rand::rng().fill(&mut iv);
|
||||
let sealing_key = derive_sealing_key(external_key, iv, managed_sse_domain(sse_type), bucket, object)?;
|
||||
|
||||
let mut header = [0u8; DARE_HEADER_SIZE];
|
||||
header[0] = DARE_VERSION_20;
|
||||
header[1] = DARE_CIPHER_AES_256_GCM;
|
||||
header[2..4].copy_from_slice(&(31u16).to_le_bytes());
|
||||
let mut stream_nonce = [0u8; 12];
|
||||
rand::rng().fill(&mut stream_nonce);
|
||||
stream_nonce[0] |= 0x80;
|
||||
header[4..16].copy_from_slice(&stream_nonce);
|
||||
|
||||
let cipher =
|
||||
Aes256Gcm::new_from_slice(&sealing_key).map_err(|err| SseError::internal(format!("Invalid sealing key: {err}")))?;
|
||||
let nonce =
|
||||
Nonce::try_from(stream_nonce.as_slice()).map_err(|_| SseError::internal("Invalid sealed object-key stream nonce"))?;
|
||||
let ciphertext = cipher
|
||||
.encrypt(
|
||||
&nonce,
|
||||
aes_gcm::aead::Payload {
|
||||
msg: &object_key,
|
||||
aad: &header[..4],
|
||||
},
|
||||
)
|
||||
.map_err(|err| SseError::internal(format!("Failed to seal object key: {err}")))?;
|
||||
|
||||
let mut sealed_key = [0u8; SEALED_KEY_SIZE];
|
||||
sealed_key[..DARE_HEADER_SIZE].copy_from_slice(&header);
|
||||
sealed_key[DARE_HEADER_SIZE..].copy_from_slice(&ciphertext);
|
||||
|
||||
Ok(ManagedSealedKey { iv, sealed_key })
|
||||
}
|
||||
|
||||
pub(crate) fn unseal_object_key(
|
||||
sealed: &ManagedSealedKey,
|
||||
external_key: [u8; 32],
|
||||
sse_type: SSEType,
|
||||
bucket: &str,
|
||||
object: &str,
|
||||
) -> Result<[u8; 32], SseError> {
|
||||
let header = &sealed.sealed_key[..DARE_HEADER_SIZE];
|
||||
if header[0] != DARE_VERSION_20 || !is_supported_sealed_object_key_cipher(header[1]) {
|
||||
return Err(SseError::internal("Unsupported sealed object-key DARE header"));
|
||||
}
|
||||
if u16::from_le_bytes([header[2], header[3]]) != 31 || header[4] & 0x80 == 0 {
|
||||
return Err(SseError::internal("Invalid sealed object-key payload header"));
|
||||
}
|
||||
|
||||
let sealing_key = derive_sealing_key(external_key, sealed.iv, managed_sse_domain(sse_type), bucket, object)?;
|
||||
let plaintext = decrypt_sealed_object_key_payload(sealing_key, header, &sealed.sealed_key)?;
|
||||
|
||||
let object_key: [u8; 32] = plaintext
|
||||
.as_slice()
|
||||
.try_into()
|
||||
.map_err(|_| SseError::internal("Sealed object key must decrypt to 32 bytes"))?;
|
||||
Ok(object_key)
|
||||
}
|
||||
|
||||
fn try_decode_minio_sealed_key(bytes: &str) -> Result<Option<[u8; SEALED_KEY_SIZE]>, SseError> {
|
||||
let decoded = BASE64_STANDARD
|
||||
.decode_to_vec(bytes)
|
||||
.map_err(|e| SseError::internal(format!("Failed to decode sealed object key: {e}")))?;
|
||||
match decoded.as_slice().try_into() {
|
||||
Ok(sealed_key) => Ok(Some(sealed_key)),
|
||||
Err(_) => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
fn try_decode_minio_sealing_iv(bytes: &str) -> Result<Option<[u8; SEALED_KEY_IV_SIZE]>, SseError> {
|
||||
let decoded = BASE64_STANDARD
|
||||
.decode_to_vec(bytes)
|
||||
.map_err(|e| SseError::internal(format!("Failed to decode sealing IV: {e}")))?;
|
||||
match decoded.as_slice().try_into() {
|
||||
Ok(iv) => Ok(Some(iv)),
|
||||
Err(_) => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn is_legacy_rustfs_managed_metadata(metadata: &HashMap<String, String>) -> bool {
|
||||
metadata.contains_key(INTERNAL_ENCRYPTION_KEY_HEADER)
|
||||
&& metadata.contains_key(INTERNAL_ENCRYPTION_IV_HEADER)
|
||||
&& !metadata.contains_key(MINIO_INTERNAL_ENCRYPTION_S3_SEALED_KEY_HEADER)
|
||||
&& !metadata.contains_key(MINIO_INTERNAL_ENCRYPTION_KMS_SEALED_KEY_HEADER)
|
||||
}
|
||||
|
||||
/// Infer the managed SSE scheme from the MinIO sealed-key slot that is present.
|
||||
///
|
||||
/// Returns `None` when no managed MinIO slot is present, which keeps callers on
|
||||
/// their fail-closed path. SSE-C is not a managed scheme and is handled by the
|
||||
/// SSE-C read path, so its slot is not considered here.
|
||||
pub(crate) fn infer_minio_managed_sse_type(metadata: &HashMap<String, String>) -> Option<SSEType> {
|
||||
if metadata.contains_key(MINIO_INTERNAL_ENCRYPTION_S3_SEALED_KEY_HEADER) {
|
||||
Some(SSEType::SseS3)
|
||||
} else if metadata.contains_key(MINIO_INTERNAL_ENCRYPTION_KMS_SEALED_KEY_HEADER) {
|
||||
Some(SSEType::SseKms)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn parse_minio_managed_sealed_key(
|
||||
metadata: &HashMap<String, String>,
|
||||
sse_type: SSEType,
|
||||
) -> Result<Option<ManagedSealedKey>, SseError> {
|
||||
let algorithm = match metadata.get(MINIO_INTERNAL_ENCRYPTION_ALGORITHM_HEADER) {
|
||||
Some(algorithm) => algorithm,
|
||||
None => return Ok(None),
|
||||
};
|
||||
if algorithm != MINIO_INTERNAL_ENCRYPTION_SEAL_ALGORITHM {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let iv = match metadata.get(MINIO_INTERNAL_ENCRYPTION_IV_HEADER) {
|
||||
Some(iv) => match try_decode_minio_sealing_iv(iv)? {
|
||||
Some(iv) => iv,
|
||||
None => return Ok(None),
|
||||
},
|
||||
None => return Ok(None),
|
||||
};
|
||||
|
||||
let header_key = match sse_type {
|
||||
SSEType::SseS3 => MINIO_INTERNAL_ENCRYPTION_S3_SEALED_KEY_HEADER,
|
||||
SSEType::SseKms => MINIO_INTERNAL_ENCRYPTION_KMS_SEALED_KEY_HEADER,
|
||||
SSEType::SseC => MINIO_INTERNAL_ENCRYPTION_SSEC_SEALED_KEY_HEADER,
|
||||
};
|
||||
let sealed_key = match metadata.get(header_key) {
|
||||
Some(value) => match try_decode_minio_sealed_key(value)? {
|
||||
Some(sealed_key) => sealed_key,
|
||||
None => return Ok(None),
|
||||
},
|
||||
None => return Ok(None),
|
||||
};
|
||||
|
||||
Ok(Some(ManagedSealedKey { iv, sealed_key }))
|
||||
}
|
||||
|
||||
/// SSE-C: record the object key sealed under the customer key.
|
||||
pub(crate) fn record_sealed_customer_key(metadata: &mut HashMap<String, String>, sealed: &ManagedSealedKey) {
|
||||
metadata.insert(
|
||||
MINIO_INTERNAL_ENCRYPTION_IV_HEADER.to_string(),
|
||||
BASE64_STANDARD.encode_to_string(sealed.iv),
|
||||
);
|
||||
metadata.insert(
|
||||
MINIO_INTERNAL_ENCRYPTION_ALGORITHM_HEADER.to_string(),
|
||||
MINIO_INTERNAL_ENCRYPTION_SEAL_ALGORITHM.to_string(),
|
||||
);
|
||||
metadata.insert(
|
||||
MINIO_INTERNAL_ENCRYPTION_SSEC_SEALED_KEY_HEADER.to_string(),
|
||||
BASE64_STANDARD.encode_to_string(sealed.sealed_key),
|
||||
);
|
||||
}
|
||||
|
||||
/// Managed SSE: record the wrapped data key in MinIO's slots.
|
||||
///
|
||||
/// With a sealed object key the slots hold what MinIO itself writes. Without one
|
||||
/// the key was formed in Direct mode, and the slots mirror its wrapped data key and
|
||||
/// nonce: [`parse_minio_managed_sealed_key`] turns that shape down by its length, so
|
||||
/// the object is read back in Direct mode.
|
||||
pub(crate) fn record_data_key_in_minio_slots(
|
||||
metadata: &mut HashMap<String, String>,
|
||||
material: &EncryptionMaterial,
|
||||
wrapped_data_key: &[u8],
|
||||
) {
|
||||
metadata.insert(
|
||||
MINIO_INTERNAL_ENCRYPTION_ALGORITHM_HEADER.to_string(),
|
||||
MINIO_INTERNAL_ENCRYPTION_SEAL_ALGORITHM.to_string(),
|
||||
);
|
||||
if let Some(kms_key_id) = &material.kms_key_id {
|
||||
metadata.insert(MINIO_INTERNAL_ENCRYPTION_KMS_KEY_ID_HEADER.to_string(), kms_key_id.to_string());
|
||||
}
|
||||
|
||||
let encoded_data_key = BASE64_STANDARD.encode_to_string(wrapped_data_key);
|
||||
match &material.managed_sealed_key {
|
||||
Some(sealed) => {
|
||||
metadata.insert(
|
||||
MINIO_INTERNAL_ENCRYPTION_IV_HEADER.to_string(),
|
||||
BASE64_STANDARD.encode_to_string(sealed.iv),
|
||||
);
|
||||
match material.sse_type {
|
||||
SSEType::SseS3 => {
|
||||
metadata.insert(
|
||||
MINIO_INTERNAL_ENCRYPTION_S3_SEALED_KEY_HEADER.to_string(),
|
||||
BASE64_STANDARD.encode_to_string(sealed.sealed_key),
|
||||
);
|
||||
}
|
||||
SSEType::SseKms => {
|
||||
metadata.insert(
|
||||
MINIO_INTERNAL_ENCRYPTION_KMS_SEALED_KEY_HEADER.to_string(),
|
||||
BASE64_STANDARD.encode_to_string(sealed.sealed_key),
|
||||
);
|
||||
}
|
||||
SSEType::SseC => {}
|
||||
}
|
||||
metadata.insert(MINIO_INTERNAL_ENCRYPTION_KMS_DATA_KEY_HEADER.to_string(), encoded_data_key);
|
||||
}
|
||||
None => {
|
||||
metadata.insert(
|
||||
MINIO_INTERNAL_ENCRYPTION_IV_HEADER.to_string(),
|
||||
BASE64_STANDARD.encode_to_string(material.base_nonce),
|
||||
);
|
||||
match material.sse_type {
|
||||
SSEType::SseS3 => {
|
||||
metadata.insert(MINIO_INTERNAL_ENCRYPTION_S3_SEALED_KEY_HEADER.to_string(), encoded_data_key);
|
||||
}
|
||||
SSEType::SseKms => {
|
||||
metadata.insert(MINIO_INTERNAL_ENCRYPTION_KMS_SEALED_KEY_HEADER.to_string(), encoded_data_key.clone());
|
||||
metadata.insert(MINIO_INTERNAL_ENCRYPTION_KMS_DATA_KEY_HEADER.to_string(), encoded_data_key);
|
||||
}
|
||||
SSEType::SseC => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Managed SSE: read back the wrapped data key that seals `sealed_key`.
|
||||
///
|
||||
/// `normalized_metadata` is `metadata` with the managed keys normalized.
|
||||
pub(crate) fn read_sealed_data_key(
|
||||
metadata: &HashMap<String, String>,
|
||||
normalized_metadata: &HashMap<String, String>,
|
||||
sealed_key: ManagedSealedKey,
|
||||
) -> Result<StoredDataKey, SseError> {
|
||||
let encrypted_key_b64 = normalized_metadata
|
||||
.get(INTERNAL_ENCRYPTION_KEY_HEADER)
|
||||
.or_else(|| metadata.get(MINIO_INTERNAL_ENCRYPTION_KMS_DATA_KEY_HEADER))
|
||||
.ok_or_else(|| SseError::internal("Missing encrypted key in metadata"))?;
|
||||
let wrapped = BASE64_STANDARD
|
||||
.decode_to_vec(encrypted_key_b64)
|
||||
.map_err(|e| SseError::internal(format!("Failed to decode encrypted key: {e}")))?;
|
||||
|
||||
Ok(StoredDataKey {
|
||||
wrapped,
|
||||
algorithm: normalized_metadata
|
||||
.get(INTERNAL_ENCRYPTION_ALGORITHM_HEADER)
|
||||
.cloned()
|
||||
.unwrap_or_else(|| "AES256".to_string()),
|
||||
mode: StoredKeyMode::Object { sealed_key },
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether a multipart session needs the customer key when it is created.
|
||||
///
|
||||
/// Object mode seals the session's object key under the customer key, so the key
|
||||
/// has to be present, and valid, at that point.
|
||||
pub(crate) const SESSION_KEY_NEEDS_CUSTOMER_KEY: bool = true;
|
||||
|
||||
/// Derive a fresh object key from `external_key` and seal it under the same key.
|
||||
fn new_sealed_object_key(external_key: [u8; 32], sse_type: SSEType, bucket: &str, key: &str) -> Result<NewObjectKey, SseError> {
|
||||
let object_key = derive_object_key(external_key)?;
|
||||
let sealed_key = seal_object_key(object_key, external_key, sse_type, bucket, key)?;
|
||||
Ok(NewObjectKey {
|
||||
key_bytes: object_key,
|
||||
base_nonce: [0; 12],
|
||||
key_kind: EncryptionKeyKind::Object,
|
||||
sealed_key: Some(sealed_key),
|
||||
})
|
||||
}
|
||||
|
||||
/// SSE-C object: an object key sealed under the customer key.
|
||||
pub(crate) fn new_key_under_customer_key(customer_key: [u8; 32], bucket: &str, key: &str) -> Result<NewObjectKey, SseError> {
|
||||
new_sealed_object_key(customer_key, SSEType::SseC, bucket, key)
|
||||
}
|
||||
|
||||
/// SSE-C multipart session: an object key sealed under the customer key.
|
||||
///
|
||||
/// A session created without its customer key has nothing to seal and records a
|
||||
/// placeholder instead.
|
||||
pub(crate) fn new_session_key_under_customer_key(
|
||||
customer_key: Option<[u8; 32]>,
|
||||
bucket: &str,
|
||||
key: &str,
|
||||
) -> Result<NewObjectKey, SseError> {
|
||||
match customer_key {
|
||||
Some(customer_key) => new_sealed_object_key(customer_key, SSEType::SseC, bucket, key),
|
||||
None => Ok(NewObjectKey {
|
||||
key_bytes: [0; 32],
|
||||
base_nonce: [0; 12],
|
||||
key_kind: EncryptionKeyKind::Direct,
|
||||
sealed_key: None,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
/// Managed SSE: an object key sealed under the data key.
|
||||
pub(crate) fn new_key_under_data_key(
|
||||
data_key: &DataKey,
|
||||
sse_type: SSEType,
|
||||
bucket: &str,
|
||||
key: &str,
|
||||
) -> Result<NewObjectKey, SseError> {
|
||||
new_sealed_object_key(data_key.plaintext_key, sse_type, bucket, key)
|
||||
}
|
||||
@@ -1,253 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Reading: recognize how a stored object was keyed, and recover its key.
|
||||
//!
|
||||
//! What an object recorded is the only source of truth here. The scheme a request
|
||||
//! advertises and whether a KMS is running now are write policy and runtime state;
|
||||
//! neither says how an object written earlier was keyed.
|
||||
|
||||
use crate::core::keying::context::{build_object_encryption_context, decode_minio_kms_context};
|
||||
use crate::core::keying::direct::{read_stored_ssec_nonce, read_wrapped_data_key};
|
||||
use crate::core::keying::material::EncryptionKeyKind;
|
||||
use crate::core::keying::metadata::recode_minio_kms_context;
|
||||
#[cfg(feature = "rio-v2")]
|
||||
use crate::core::keying::object::{
|
||||
infer_minio_managed_sse_type, is_legacy_rustfs_managed_metadata, parse_minio_managed_sealed_key, read_sealed_data_key,
|
||||
unseal_object_key,
|
||||
};
|
||||
use crate::core::keying::{StoredDataKey, StoredKeyMode};
|
||||
use crate::error::SseError;
|
||||
use crate::host::SseHost;
|
||||
use crate::kms::local::is_local_sse_dek;
|
||||
#[cfg(feature = "rio-v2")]
|
||||
use crate::kms::local::is_rustfs_managed_data_key;
|
||||
#[cfg(feature = "rio-v2")]
|
||||
use crate::kms::provider::decrypt_unenveloped_sealed_dek;
|
||||
use crate::kms::provider::{SseDekProvider, bare_data_key_kms_provider, get_local_sse_dek_provider, kms_sse_dek_provider};
|
||||
use rustfs_kms::is_data_key_envelope;
|
||||
use rustfs_utils::http::headers::AMZ_ENCRYPTION_KMS;
|
||||
use rustfs_utils::http::object_encryption_keys::{
|
||||
INTERNAL_ENCRYPTION_KEY_ID_HEADER, SSEType, contains_managed_encryption_metadata, normalize_managed_metadata,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// The key a stored object is decrypted with, recovered from what the object recorded.
|
||||
pub(crate) struct OpenedObjectKey {
|
||||
/// Key handed to the stream cipher.
|
||||
pub(crate) key_bytes: [u8; 32],
|
||||
/// Base nonce the stream cipher derives its block nonces from.
|
||||
pub(crate) base_nonce: [u8; 12],
|
||||
/// The mode the object was written in.
|
||||
pub(crate) key_kind: EncryptionKeyKind,
|
||||
}
|
||||
|
||||
/// The managed scheme a stored object was written under, or `None` when this build
|
||||
/// cannot establish it.
|
||||
fn stored_managed_sse_type(metadata: &HashMap<String, String>) -> Option<SSEType> {
|
||||
match metadata.get("x-amz-server-side-encryption").map(String::as_str) {
|
||||
Some(AMZ_ENCRYPTION_KMS) => Some(SSEType::SseKms),
|
||||
Some(_) => Some(SSEType::SseS3),
|
||||
// MinIO never persists the public scheme header: `crypto.S3.CreateMetadata`
|
||||
// writes only the `X-Minio-Internal-*` family and the public header is
|
||||
// synthesized onto the response by `DecryptObjectInfo`. Requiring it here
|
||||
// is what made every MinIO-encrypted object unreadable (backlog#1638).
|
||||
//
|
||||
// Inferring from the sealed-key slot is self-consistent by construction:
|
||||
// the slot decides which header the unseal reads AND which domain string
|
||||
// the sealing key is derived under, so a scheme that disagrees with the
|
||||
// slot cannot silently derive a wrong key — it finds no key at all.
|
||||
// Inferring from the KMS key id would NOT be safe: MinIO writes
|
||||
// `-S3-Kms-Key-Id` on SSE-S3 objects too.
|
||||
//
|
||||
// An object whose scheme cannot be established stays fail-closed, and
|
||||
// deliberately not through an error raised here: the read plan independently
|
||||
// classifies the object as encrypted from its markers and refuses to serve
|
||||
// it without material, so it never degrades into a plaintext read.
|
||||
#[cfg(feature = "rio-v2")]
|
||||
None => infer_minio_managed_sse_type(metadata),
|
||||
// Without the rio-v2 reader there is no MinIO-format read path to serve
|
||||
// such an object with, so it stays on the fail-closed branch.
|
||||
#[cfg(not(feature = "rio-v2"))]
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// SSE-C: recover the object's key from the customer key.
|
||||
///
|
||||
/// A sealed object key recorded in MinIO's slot is unsealed under the customer key;
|
||||
/// otherwise the customer key is the object's key, under the nonce the object recorded.
|
||||
pub(crate) fn open_under_customer_key(
|
||||
metadata: &HashMap<String, String>,
|
||||
customer_key: [u8; 32],
|
||||
bucket: &str,
|
||||
key: &str,
|
||||
) -> Result<OpenedObjectKey, SseError> {
|
||||
#[cfg(feature = "rio-v2")]
|
||||
if let Some(sealed_key) = parse_minio_managed_sealed_key(metadata, SSEType::SseC)? {
|
||||
return Ok(OpenedObjectKey {
|
||||
key_bytes: unseal_object_key(&sealed_key, customer_key, SSEType::SseC, bucket, key)?,
|
||||
base_nonce: [0; 12],
|
||||
key_kind: EncryptionKeyKind::Object,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(OpenedObjectKey {
|
||||
key_bytes: customer_key,
|
||||
base_nonce: read_stored_ssec_nonce(metadata, bucket, key),
|
||||
key_kind: EncryptionKeyKind::Direct,
|
||||
})
|
||||
}
|
||||
|
||||
/// A stored managed-SSE object, as recognized from its metadata alone.
|
||||
pub(crate) struct StoredManagedKey<'a> {
|
||||
/// The object's metadata as stored.
|
||||
metadata: &'a HashMap<String, String>,
|
||||
/// The same metadata with the managed keys normalized.
|
||||
normalized_metadata: HashMap<String, String>,
|
||||
/// The scheme the object was written under.
|
||||
pub(crate) sse_type: SSEType,
|
||||
/// The KMS key id the object recorded, if it recorded one.
|
||||
pub(crate) recorded_kms_key_id: Option<String>,
|
||||
}
|
||||
|
||||
/// Recognize a stored managed-SSE object.
|
||||
///
|
||||
/// `None` means the object carries no managed-SSE metadata, or carries some whose
|
||||
/// scheme this build cannot establish.
|
||||
pub(crate) fn recognize_stored_managed_key(metadata: &HashMap<String, String>) -> Option<StoredManagedKey<'_>> {
|
||||
if !contains_managed_encryption_metadata(metadata) {
|
||||
return None;
|
||||
}
|
||||
let sse_type = stored_managed_sse_type(metadata)?;
|
||||
|
||||
let normalized_metadata = normalize_managed_metadata(metadata, Some(recode_minio_kms_context));
|
||||
let recorded_kms_key_id = normalized_metadata
|
||||
.get(INTERNAL_ENCRYPTION_KEY_ID_HEADER)
|
||||
.or_else(|| metadata.get("x-amz-server-side-encryption-aws-kms-key-id"))
|
||||
.cloned();
|
||||
|
||||
Some(StoredManagedKey {
|
||||
metadata,
|
||||
normalized_metadata,
|
||||
sse_type,
|
||||
recorded_kms_key_id,
|
||||
})
|
||||
}
|
||||
|
||||
/// Managed SSE: unwrap the stored data key and recover the object's key from it.
|
||||
///
|
||||
/// `kms_key_id` is the key the read was authorized against. Returns the object's key
|
||||
/// together with the algorithm the object recorded.
|
||||
pub(crate) async fn open_under_stored_data_key(
|
||||
host: &dyn SseHost,
|
||||
bucket: &str,
|
||||
key: &str,
|
||||
stored: &StoredManagedKey<'_>,
|
||||
kms_key_id: &str,
|
||||
) -> Result<(OpenedObjectKey, String), SseError> {
|
||||
let record = read_stored_data_key(host, stored).await?;
|
||||
let data_key = unwrap_stored_data_key(host, bucket, key, stored, &record, kms_key_id).await?;
|
||||
|
||||
let opened = match record.mode {
|
||||
StoredKeyMode::Direct { base_nonce } => OpenedObjectKey {
|
||||
key_bytes: data_key,
|
||||
base_nonce,
|
||||
key_kind: EncryptionKeyKind::Direct,
|
||||
},
|
||||
#[cfg(feature = "rio-v2")]
|
||||
StoredKeyMode::Object { sealed_key } => OpenedObjectKey {
|
||||
key_bytes: unseal_object_key(&sealed_key, data_key, stored.sse_type, bucket, key)?,
|
||||
base_nonce: [0u8; 12],
|
||||
key_kind: EncryptionKeyKind::Object,
|
||||
},
|
||||
};
|
||||
|
||||
Ok((opened, record.algorithm))
|
||||
}
|
||||
|
||||
/// Read the stored data key in the mode the object recorded.
|
||||
///
|
||||
/// This is the recognition order: a mode listed earlier claims the object first, and
|
||||
/// Direct takes whatever no other mode recognizes. A mode this build cannot read is
|
||||
/// not listed, so its objects fail in the Direct reader instead of being misread.
|
||||
async fn read_stored_data_key(host: &dyn SseHost, stored: &StoredManagedKey<'_>) -> Result<StoredDataKey, SseError> {
|
||||
#[cfg(feature = "rio-v2")]
|
||||
if let Some(sealed_key) = parse_minio_managed_sealed_key(stored.metadata, stored.sse_type)? {
|
||||
return read_sealed_data_key(stored.metadata, &stored.normalized_metadata, sealed_key);
|
||||
}
|
||||
|
||||
read_wrapped_data_key(host, &stored.normalized_metadata).await
|
||||
}
|
||||
|
||||
/// Unwrap the stored data key with the provider its own bytes identify.
|
||||
async fn unwrap_stored_data_key(
|
||||
host: &dyn SseHost,
|
||||
bucket: &str,
|
||||
key: &str,
|
||||
stored: &StoredManagedKey<'_>,
|
||||
record: &StoredDataKey,
|
||||
kms_key_id: &str,
|
||||
) -> Result<[u8; 32], SseError> {
|
||||
let kms_context = if matches!(stored.sse_type, SSEType::SseKms) {
|
||||
decode_minio_kms_context(stored.metadata)?
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let object_context = build_object_encryption_context(bucket, key, kms_context.as_ref());
|
||||
let wrapped = record.wrapped.as_slice();
|
||||
|
||||
// Persisted wrapping format is the read-side source of truth. The
|
||||
// advertised SSE scheme and current KMS availability are write policy
|
||||
// and runtime state, neither of which identifies the historical provider.
|
||||
//
|
||||
// The one shape that cannot name its provider is a backend-native blob
|
||||
// stored without an envelope. It is offered to the KMS only when the
|
||||
// local provider could not have written those bytes and the active
|
||||
// backend is one whose released versions persisted them that way.
|
||||
let bare_kms = if is_data_key_envelope(wrapped) || is_local_sse_dek(wrapped) {
|
||||
None
|
||||
} else {
|
||||
bare_data_key_kms_provider(host).await
|
||||
};
|
||||
|
||||
// A MinIO sealed key alone does not mean MinIO wrote the object: RustFS's own
|
||||
// writer fills MinIO's metadata slots too, while still storing a RustFS
|
||||
// envelope in them, so neither the slot nor the header name distinguishes the
|
||||
// two. The data key's own shape does. RustFS envelopes are strictly-parsed
|
||||
// JSON; MinIO's builtin-KMS ciphertext is opaque bytes that match neither, so
|
||||
// recognizing RustFS positively — and treating only the remainder as MinIO —
|
||||
// keeps a RustFS envelope from ever reaching MinIO's decoder.
|
||||
#[cfg(feature = "rio-v2")]
|
||||
if matches!(record.mode, StoredKeyMode::Object { .. }) && !is_rustfs_managed_data_key(wrapped) {
|
||||
return decrypt_unenveloped_sealed_dek(bare_kms, get_local_sse_dek_provider(), wrapped, kms_key_id, &object_context)
|
||||
.await;
|
||||
}
|
||||
|
||||
let provider: Arc<dyn SseDekProvider> = if is_data_key_envelope(wrapped) {
|
||||
kms_sse_dek_provider(host).await?
|
||||
} else if let Some(kms_provider) = bare_kms {
|
||||
kms_provider
|
||||
} else {
|
||||
get_local_sse_dek_provider()?
|
||||
};
|
||||
|
||||
#[cfg(feature = "rio-v2")]
|
||||
if is_legacy_rustfs_managed_metadata(&stored.normalized_metadata) {
|
||||
return provider.decrypt_legacy_sse_dek(wrapped, kms_key_id, &object_context).await;
|
||||
}
|
||||
|
||||
provider.decrypt_sse_dek(wrapped, kms_key_id, &object_context).await
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! SSE key resolution: the S3-defined schemes, the internal keying modes, and the entry points that join them.
|
||||
|
||||
pub(crate) mod audit;
|
||||
pub(crate) mod keying;
|
||||
pub(crate) mod read;
|
||||
pub(crate) mod request;
|
||||
pub(crate) mod rewrap;
|
||||
pub(crate) mod s3;
|
||||
pub(crate) mod write;
|
||||
@@ -1,303 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Read entry points: resolve the decryption material, or the response headers, for a stored object.
|
||||
|
||||
use crate::core::audit::{record_managed_kms_outcome, stored_envelope_master_key_version};
|
||||
use crate::core::keying::material::DecryptionMaterial;
|
||||
use crate::core::keying::metadata::stored_ssec_metadata;
|
||||
use crate::core::keying::stored::{
|
||||
StoredManagedKey, open_under_customer_key, open_under_stored_data_key, recognize_stored_managed_key,
|
||||
};
|
||||
use crate::core::request::SseKmsPrincipal;
|
||||
use crate::core::s3::authorization::authorize_sse_kms_key;
|
||||
use crate::core::s3::headers::{SseReadResponseHeaders, managed_sse_public_header};
|
||||
use crate::core::s3::scheme::resolve_stored_kms_key_id;
|
||||
use crate::core::s3::ssec::{SsecParams, require_matching_ssec_key, stored_ssec_algorithm, validate_ssec_params};
|
||||
use crate::error::SseError;
|
||||
use crate::host::SseHost;
|
||||
use rustfs_policy::policy::action::KmsAction;
|
||||
use rustfs_utils::http::headers::AMZ_ENCRYPTION_AES;
|
||||
use rustfs_utils::http::object_encryption_keys::{SSEType, contains_managed_encryption_metadata, stored_managed_encryption_key};
|
||||
use std::collections::HashMap;
|
||||
|
||||
impl std::fmt::Debug for DecryptionRequest<'_> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("DecryptionRequest").finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
/// Request parameters for unified decryption
|
||||
pub struct DecryptionRequest<'a> {
|
||||
/// Bucket name
|
||||
pub bucket: &'a str,
|
||||
/// Object key
|
||||
pub key: &'a str,
|
||||
/// Object metadata containing encryption headers
|
||||
pub metadata: &'a HashMap<String, String>,
|
||||
/// SSE-C key (Base64-encoded) - required if object was encrypted with SSE-C
|
||||
pub sse_customer_key: Option<&'a String>,
|
||||
/// SSE-C key MD5 (Base64-encoded) - required if object was encrypted with SSE-C
|
||||
pub sse_customer_key_md5: Option<&'a String>,
|
||||
/// Caller the SSE-KMS key usage is authorized as. `None` marks an internal caller.
|
||||
pub principal: Option<&'a SseKmsPrincipal>,
|
||||
}
|
||||
|
||||
/// **Core API**: Apply decryption based on stored metadata
|
||||
///
|
||||
/// This function automatically detects the encryption type from metadata:
|
||||
/// - SSE-C if customer key is provided
|
||||
/// - SSE-S3/SSE-KMS if managed encryption metadata is found
|
||||
/// - None if object is not encrypted
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `request` - Decryption request with metadata and optional customer key
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Ok(Some(material))` - Decryption should be applied with the returned material
|
||||
/// * `Ok(None)` - Object is not encrypted
|
||||
/// * `Err` - Decryption configuration error or key mismatch
|
||||
///
|
||||
/// # Example
|
||||
/// ```rust,ignore
|
||||
/// let request = DecryptionRequest {
|
||||
/// bucket: &bucket,
|
||||
/// key: &key,
|
||||
/// metadata: &metadata,
|
||||
/// sse_customer_key: sse_customer_key.as_deref(),
|
||||
/// sse_customer_key_md5: sse_customer_key_md5.as_deref(),
|
||||
/// };
|
||||
///
|
||||
/// if let Some(material) = sse_decryption(host, request).await? {
|
||||
/// content_size = material.original_size.unwrap_or(actual_size);
|
||||
/// }
|
||||
/// ```
|
||||
pub async fn sse_decryption(host: &dyn SseHost, request: DecryptionRequest<'_>) -> Result<Option<DecryptionMaterial>, SseError> {
|
||||
// Check for SSE-C encryption
|
||||
if stored_ssec_metadata(request.metadata) {
|
||||
let (key, key_md5) = require_matching_ssec_key(request.metadata, request.sse_customer_key, request.sse_customer_key_md5)?;
|
||||
|
||||
let mut material = apply_ssec_decryption_material(request.bucket, request.key, request.metadata, key, key_md5).await?;
|
||||
material.customer_key_md5 = Some(key_md5.clone());
|
||||
return Ok(Some(material));
|
||||
}
|
||||
|
||||
// Check for managed SSE encryption
|
||||
if contains_managed_encryption_metadata(request.metadata) {
|
||||
return apply_managed_decryption_material(host, request.bucket, request.key, request.metadata, request.principal).await;
|
||||
}
|
||||
|
||||
// No encryption detected
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// Read-side response classification for an object whose payload the object
|
||||
/// layer's encryption resolver already decrypted.
|
||||
///
|
||||
/// [`sse_decryption`] both unwraps the data key and derives the response
|
||||
/// headers. The GET path only needs the latter — its stream comes out of the
|
||||
/// object layer decrypted (see [`SseObjectEncryptionResolver`]) — so calling
|
||||
/// [`sse_decryption`] there performed a second KMS `Decrypt` per request whose
|
||||
/// key bytes were discarded. This function reproduces that call's header,
|
||||
/// validation, authorization and audit behavior from stored metadata alone.
|
||||
///
|
||||
/// Contract, mirrored from [`sse_decryption`] item by item:
|
||||
/// - SSE-C: missing request key/MD5 and stored/request MD5 mismatch fail with
|
||||
/// the same errors and precedence, and the provided key is fully validated.
|
||||
/// - Managed SSE: per-key `kms:Decrypt` authorization runs ahead of every other
|
||||
/// failure mode, and the request's KMS audit summary records the same
|
||||
/// scheme/key/version/outcome fields the unwrap-based path recorded. The
|
||||
/// success outcome is honest because a failed unwrap aborts the read in the
|
||||
/// object layer before response classification is ever reached.
|
||||
/// - Objects without SSE metadata — and managed metadata whose scheme cannot
|
||||
/// be established in this build — classify as `None`, exactly where
|
||||
/// [`sse_decryption`] returned `None`.
|
||||
pub async fn classify_sse_read_response(
|
||||
host: &dyn SseHost,
|
||||
request: DecryptionRequest<'_>,
|
||||
) -> Result<Option<SseReadResponseHeaders>, SseError> {
|
||||
if stored_ssec_metadata(request.metadata) {
|
||||
let (key, key_md5) = require_matching_ssec_key(request.metadata, request.sse_customer_key, request.sse_customer_key_md5)?;
|
||||
|
||||
let algorithm = stored_ssec_algorithm(request.metadata);
|
||||
validate_ssec_params(SsecParams {
|
||||
algorithm: algorithm.to_string(),
|
||||
key: key.to_string(),
|
||||
key_md5: key_md5.to_string(),
|
||||
})?;
|
||||
|
||||
return Ok(Some(SseReadResponseHeaders {
|
||||
server_side_encryption: String::from(AMZ_ENCRYPTION_AES),
|
||||
sse_customer_algorithm: Some(String::from(algorithm)),
|
||||
sse_customer_key_md5: Some(key_md5.clone()),
|
||||
ssekms_key_id: None,
|
||||
}));
|
||||
}
|
||||
|
||||
// Same scheme and key resolution as the unwrap path, so authorization and the
|
||||
// response header name the same key.
|
||||
let Some((stored, kms_key_id)) = resolve_stored_managed_key(host, request.metadata).await else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
// Ahead of every other failure mode, so a denied caller learns nothing
|
||||
// about the key beyond "not yours".
|
||||
if let Err(error) =
|
||||
authorize_sse_kms_key(host, request.principal, stored.sse_type, KmsAction::DecryptAction, &kms_key_id).await
|
||||
{
|
||||
if let Some((sse_type, key_id)) = stored_managed_encryption_key(request.metadata) {
|
||||
record_managed_kms_outcome(
|
||||
request.principal,
|
||||
sse_type,
|
||||
Some(&key_id),
|
||||
|| stored_envelope_master_key_version(request.metadata),
|
||||
Err(&error),
|
||||
);
|
||||
}
|
||||
return Err(error);
|
||||
}
|
||||
|
||||
record_managed_kms_outcome(
|
||||
request.principal,
|
||||
stored.sse_type,
|
||||
Some(&kms_key_id),
|
||||
|| stored_envelope_master_key_version(request.metadata),
|
||||
Ok(()),
|
||||
);
|
||||
|
||||
Ok(Some(SseReadResponseHeaders {
|
||||
server_side_encryption: managed_sse_public_header(stored.sse_type).to_string(),
|
||||
sse_customer_algorithm: None,
|
||||
sse_customer_key_md5: None,
|
||||
// The key id was needed above to authorize the read, but an AES256
|
||||
// object's wrapping key is internal: only aws:kms objects advertise it.
|
||||
ssekms_key_id: matches!(stored.sse_type, SSEType::SseKms).then_some(kms_key_id),
|
||||
}))
|
||||
}
|
||||
|
||||
/// Recognize a stored managed-SSE object and name the key it is read under.
|
||||
///
|
||||
/// One resolution for every read: authorization, the response headers and the
|
||||
/// unwrap all name the key this returns. `None` means the object carries no
|
||||
/// managed-SSE metadata, or carries some whose scheme this build cannot establish.
|
||||
async fn resolve_stored_managed_key<'m>(
|
||||
host: &dyn SseHost,
|
||||
metadata: &'m HashMap<String, String>,
|
||||
) -> Option<(StoredManagedKey<'m>, String)> {
|
||||
let stored = recognize_stored_managed_key(metadata)?;
|
||||
let kms_key_id = resolve_stored_kms_key_id(host, stored.recorded_kms_key_id.clone()).await;
|
||||
Some((stored, kms_key_id))
|
||||
}
|
||||
|
||||
pub(crate) async fn apply_ssec_decryption_material(
|
||||
bucket: &str,
|
||||
key: &str,
|
||||
metadata: &HashMap<String, String>,
|
||||
sse_key: &str,
|
||||
sse_key_md5: &str,
|
||||
) -> Result<DecryptionMaterial, SseError> {
|
||||
// Validate provided key
|
||||
let algorithm = stored_ssec_algorithm(metadata);
|
||||
|
||||
let params = SsecParams {
|
||||
algorithm: algorithm.to_string(),
|
||||
key: sse_key.to_string(),
|
||||
key_md5: sse_key_md5.to_string(),
|
||||
};
|
||||
|
||||
let validated = validate_ssec_params(params)?;
|
||||
let opened = open_under_customer_key(metadata, validated.key_bytes, bucket, key)?;
|
||||
|
||||
Ok(DecryptionMaterial {
|
||||
sse_type: SSEType::SseC,
|
||||
server_side_encryption: String::from(AMZ_ENCRYPTION_AES), // const
|
||||
kms_key_id: None,
|
||||
algorithm: String::from(algorithm),
|
||||
|
||||
customer_key_md5: None,
|
||||
key_bytes: opened.key_bytes,
|
||||
base_nonce: opened.base_nonce,
|
||||
key_kind: opened.key_kind,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) async fn apply_managed_decryption_material(
|
||||
host: &dyn SseHost,
|
||||
bucket: &str,
|
||||
key: &str,
|
||||
metadata: &HashMap<String, String>,
|
||||
principal: Option<&SseKmsPrincipal>,
|
||||
) -> Result<Option<DecryptionMaterial>, SseError> {
|
||||
let result = apply_managed_decryption_material_inner(host, bucket, key, metadata, principal).await;
|
||||
|
||||
match &result {
|
||||
// `None` means the object carries no managed-SSE metadata — SSE-C and
|
||||
// plaintext objects never reach KMS and must not appear in the summary.
|
||||
Ok(None) => {}
|
||||
Ok(Some(material)) => record_managed_kms_outcome(
|
||||
principal,
|
||||
material.sse_type,
|
||||
material.kms_key_id.as_deref(),
|
||||
|| stored_envelope_master_key_version(metadata),
|
||||
Ok(()),
|
||||
),
|
||||
Err(error) => {
|
||||
if let Some((sse_type, key_id)) = stored_managed_encryption_key(metadata) {
|
||||
record_managed_kms_outcome(
|
||||
principal,
|
||||
sse_type,
|
||||
Some(&key_id),
|
||||
|| stored_envelope_master_key_version(metadata),
|
||||
Err(error),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
async fn apply_managed_decryption_material_inner(
|
||||
host: &dyn SseHost,
|
||||
bucket: &str,
|
||||
key: &str,
|
||||
metadata: &HashMap<String, String>,
|
||||
principal: Option<&SseKmsPrincipal>,
|
||||
) -> Result<Option<DecryptionMaterial>, SseError> {
|
||||
let Some((stored, kms_key_id)) = resolve_stored_managed_key(host, metadata).await else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
// Ahead of every other failure mode below, so a denied caller learns nothing about the
|
||||
// key beyond "not yours" — not whether it is disabled, pending deletion, or unreadable.
|
||||
authorize_sse_kms_key(host, principal, stored.sse_type, KmsAction::DecryptAction, &kms_key_id).await?;
|
||||
|
||||
let (opened, algorithm) = open_under_stored_data_key(host, bucket, key, &stored, &kms_key_id).await?;
|
||||
|
||||
Ok(Some(DecryptionMaterial {
|
||||
sse_type: stored.sse_type,
|
||||
// Synthesized from the resolved scheme rather than read back from
|
||||
// metadata: a MinIO-written object has no stored scheme header, which is
|
||||
// exactly why the scheme had to be inferred. MinIO synthesizes the same
|
||||
// header onto its own responses.
|
||||
server_side_encryption: managed_sse_public_header(stored.sse_type).to_string(),
|
||||
kms_key_id: Some(kms_key_id),
|
||||
algorithm,
|
||||
customer_key_md5: None,
|
||||
|
||||
key_bytes: opened.key_bytes,
|
||||
base_nonce: opened.base_nonce,
|
||||
key_kind: opened.key_kind,
|
||||
}))
|
||||
}
|
||||
@@ -1,148 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! The caller an SSE operation is authorized as.
|
||||
|
||||
use crate::core::audit::{KmsRequestAudit, kms_request_audit};
|
||||
#[cfg(any(test, feature = "test-util"))]
|
||||
use crate::host::KmsKeyAuthorizer;
|
||||
use serde_json::Value;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Identity a data-path KMS key operation is attributed to.
|
||||
///
|
||||
/// Only requests that crossed the S3 authorization boundary carry one. Internal
|
||||
/// callers — replication, lifecycle transition, heal, scanner — enter the object
|
||||
/// layer directly and run with server credentials, so they never build a principal
|
||||
/// and are exempt by construction; `None` therefore means "system principal".
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SseKmsPrincipal {
|
||||
pub(crate) account: String,
|
||||
groups: Option<Vec<String>>,
|
||||
is_owner: bool,
|
||||
claims: HashMap<String, Value>,
|
||||
conditions: HashMap<String, Vec<String>>,
|
||||
/// Audit slot of the S3 request this principal was built for, when the request
|
||||
/// is being audited. The principal is the only request-derived value that reaches
|
||||
/// the managed-SSE code paths, so it doubles as the carrier for the slot.
|
||||
pub(crate) request_audit: Option<Arc<KmsRequestAudit>>,
|
||||
/// Test-only decision overrides. Carried per principal rather than in a global slot so
|
||||
/// concurrent tests cannot observe each other's injection.
|
||||
#[cfg(any(test, feature = "test-util"))]
|
||||
pub(crate) test_hooks: Option<TestAuthorizationHooks>,
|
||||
}
|
||||
|
||||
impl SseKmsPrincipal {
|
||||
/// Build the principal of a request that crossed the S3 authorization boundary.
|
||||
///
|
||||
/// Anonymous requests use an empty account, matching the bucket-policy authorization
|
||||
/// path. This keeps every request that crossed the S3 boundary distinct from trusted
|
||||
/// internal callers, which are represented by `None`.
|
||||
pub fn new(
|
||||
account: String,
|
||||
groups: Option<Vec<String>>,
|
||||
is_owner: bool,
|
||||
claims: HashMap<String, Value>,
|
||||
conditions: HashMap<String, Vec<String>>,
|
||||
) -> Self {
|
||||
Self {
|
||||
account,
|
||||
groups,
|
||||
is_owner,
|
||||
claims,
|
||||
conditions,
|
||||
request_audit: None,
|
||||
#[cfg(any(test, feature = "test-util"))]
|
||||
test_hooks: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The access key the request authenticated with; empty for an anonymous request.
|
||||
pub fn account(&self) -> &str {
|
||||
&self.account
|
||||
}
|
||||
|
||||
pub fn groups(&self) -> &Option<Vec<String>> {
|
||||
&self.groups
|
||||
}
|
||||
|
||||
pub fn is_owner(&self) -> bool {
|
||||
self.is_owner
|
||||
}
|
||||
|
||||
pub fn claims(&self) -> &HashMap<String, Value> {
|
||||
&self.claims
|
||||
}
|
||||
|
||||
/// Policy condition values of the request that do not depend on the resource.
|
||||
pub fn conditions(&self) -> &HashMap<String, Vec<String>> {
|
||||
&self.conditions
|
||||
}
|
||||
|
||||
/// Attach the audit slot opened for `request_id`, if that request is being audited
|
||||
/// (see [`KmsRequestAuditScope::register`]).
|
||||
#[must_use]
|
||||
pub fn with_request_id(mut self, request_id: &str) -> Self {
|
||||
self.request_audit = kms_request_audit(request_id);
|
||||
self
|
||||
}
|
||||
|
||||
/// Override the enforcement switch and the authorizer for this principal only.
|
||||
#[cfg(any(test, feature = "test-util"))]
|
||||
#[must_use]
|
||||
pub fn with_test_hooks(mut self, hooks: Option<TestAuthorizationHooks>) -> Self {
|
||||
self.test_hooks = hooks;
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn for_test(account: &str, enforced: bool, authorizer: Arc<dyn KmsKeyAuthorizer>) -> Self {
|
||||
Self {
|
||||
account: account.to_string(),
|
||||
groups: None,
|
||||
is_owner: false,
|
||||
claims: HashMap::new(),
|
||||
conditions: HashMap::new(),
|
||||
request_audit: None,
|
||||
test_hooks: Some(TestAuthorizationHooks { enforced, authorizer }),
|
||||
}
|
||||
}
|
||||
|
||||
/// Bind this principal to `audit` so managed-SSE operations performed for it are
|
||||
/// summarised onto the request's S3 audit entry.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn with_request_audit(mut self, audit: Arc<KmsRequestAudit>) -> Self {
|
||||
self.request_audit = Some(audit);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Per-principal override of SSE-KMS key authorization, for tests.
|
||||
#[cfg(any(test, feature = "test-util"))]
|
||||
#[derive(Clone)]
|
||||
pub struct TestAuthorizationHooks {
|
||||
pub enforced: bool,
|
||||
pub authorizer: Arc<dyn KmsKeyAuthorizer>,
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "test-util"))]
|
||||
impl std::fmt::Debug for TestAuthorizationHooks {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
formatter
|
||||
.debug_struct("TestAuthorizationHooks")
|
||||
.field("enforced", &self.enforced)
|
||||
.finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
@@ -1,144 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Re-wrap a stored data key onto its master key's current version.
|
||||
|
||||
use crate::core::keying::context::{build_object_encryption_context, decode_minio_kms_context};
|
||||
use crate::core::keying::metadata::recode_minio_kms_context;
|
||||
use crate::error::SseError;
|
||||
use crate::host::SseHost;
|
||||
use crate::kms::provider::kms_sse_dek_provider;
|
||||
use base64_simd::STANDARD as BASE64_STANDARD;
|
||||
use rustfs_kms::is_data_key_envelope;
|
||||
use rustfs_utils::http::headers::AMZ_ENCRYPTION_KMS;
|
||||
use rustfs_utils::http::object_encryption_keys::{
|
||||
INTERNAL_ENCRYPTION_KEY_HEADER, MINIO_INTERNAL_ENCRYPTION_KMS_DATA_KEY_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_KMS_SEALED_KEY_HEADER, MINIO_INTERNAL_ENCRYPTION_S3_SEALED_KEY_HEADER, SSEType,
|
||||
contains_managed_encryption_metadata, normalize_managed_metadata,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// Outcome of a single object's DEK rewrap attempt.
|
||||
// Consumed by the bulk rekey sweep in the follow-up PR; tests exercise it now.
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug)]
|
||||
pub enum ObjectDekRewrapOutcome {
|
||||
/// The object carries no KMS-wrapped RustFS data-key envelope this build
|
||||
/// can rewrap: plaintext, SSE-C, or a MinIO-sealed data key.
|
||||
NotApplicable,
|
||||
/// The envelope is already on the current master key version and format;
|
||||
/// the caller must persist nothing, so a sweep re-run converges.
|
||||
AlreadyCurrent,
|
||||
/// The envelope was rewrapped. `metadata` holds the overrides to merge
|
||||
/// into the object's user-defined metadata — every stored copy of the old
|
||||
/// envelope, replaced — via `ObjectLayer::put_object_metadata`.
|
||||
Rewrapped { metadata: HashMap<String, String> },
|
||||
}
|
||||
|
||||
/// Rewrap the KMS-wrapped data key in an object's stored metadata onto its
|
||||
/// master key's current version (and current envelope format), without
|
||||
/// touching the object's data or its plaintext DEK.
|
||||
///
|
||||
/// Mirrors the managed decrypt path byte for byte where it matters: the
|
||||
/// envelope is located through the same normalized-header resolution, and the
|
||||
/// encryption context is rebuilt with the same helpers, so an envelope the
|
||||
/// read path can open is exactly an envelope this can rewrap. The KMS backend
|
||||
/// owns the format and the no-op decision.
|
||||
///
|
||||
/// The returned overrides replace every stored copy of the old envelope
|
||||
/// (RustFS's internal header and the MinIO-compatible slots that the writer
|
||||
/// fills with the same bytes). A copy left behind would win a read-path
|
||||
/// fallback and resurrect the old wrapping, so finding no replaceable copy is
|
||||
/// an error, never a silent success.
|
||||
pub async fn rewrap_object_encryption_metadata(
|
||||
host: &dyn SseHost,
|
||||
bucket: &str,
|
||||
key: &str,
|
||||
metadata: &HashMap<String, String>,
|
||||
) -> Result<ObjectDekRewrapOutcome, SseError> {
|
||||
if !contains_managed_encryption_metadata(metadata) {
|
||||
return Ok(ObjectDekRewrapOutcome::NotApplicable);
|
||||
}
|
||||
|
||||
let encryption_type = match metadata.get("x-amz-server-side-encryption").map(String::as_str) {
|
||||
Some(AMZ_ENCRYPTION_KMS) => SSEType::SseKms,
|
||||
Some(_) => SSEType::SseS3,
|
||||
// MinIO-written objects synthesize the public header on read; their
|
||||
// sealed data keys are not RustFS envelopes and fall out below.
|
||||
None => SSEType::SseS3,
|
||||
};
|
||||
|
||||
let normalized_metadata = normalize_managed_metadata(metadata, Some(recode_minio_kms_context));
|
||||
let Some(envelope_b64) = normalized_metadata
|
||||
.get(INTERNAL_ENCRYPTION_KEY_HEADER)
|
||||
.or_else(|| metadata.get(MINIO_INTERNAL_ENCRYPTION_KMS_DATA_KEY_HEADER))
|
||||
else {
|
||||
return Ok(ObjectDekRewrapOutcome::NotApplicable);
|
||||
};
|
||||
let encrypted_data_key = BASE64_STANDARD
|
||||
.decode_to_vec(envelope_b64)
|
||||
.map_err(|e| SseError::internal(format!("Failed to decode encrypted key: {e}")))?;
|
||||
// Only RustFS envelopes are rewrappable here; MinIO's builtin-KMS
|
||||
// ciphertext is opaque bytes owned by a different root of trust.
|
||||
if !is_data_key_envelope(&encrypted_data_key) {
|
||||
return Ok(ObjectDekRewrapOutcome::NotApplicable);
|
||||
}
|
||||
|
||||
let kms_context = if matches!(encryption_type, SSEType::SseKms) {
|
||||
decode_minio_kms_context(metadata)?
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let object_context = build_object_encryption_context(bucket, key, kms_context.as_ref());
|
||||
|
||||
// The same provider selection as the managed decrypt path.
|
||||
let provider = kms_sse_dek_provider(host).await?;
|
||||
let response = provider.rewrap_sse_dek(&encrypted_data_key, &object_context).await?;
|
||||
if !response.rewrapped {
|
||||
return Ok(ObjectDekRewrapOutcome::AlreadyCurrent);
|
||||
}
|
||||
|
||||
// Replace every stored copy of the old envelope, keyed by value and
|
||||
// matched case-insensitively: metadata key casing drifts through the
|
||||
// storage layer, and an override inserted under a differently-cased name
|
||||
// would sit beside the old copy instead of replacing it. The MinIO
|
||||
// sealed-key slots can instead hold a sealed *object* key (rio-v2 writer);
|
||||
// those bytes differ from the envelope and are untouched — the data key
|
||||
// they seal is unchanged by a rewrap.
|
||||
const REWRAP_ENVELOPE_HEADERS: [&str; 4] = [
|
||||
INTERNAL_ENCRYPTION_KEY_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_KMS_DATA_KEY_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_KMS_SEALED_KEY_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_S3_SEALED_KEY_HEADER,
|
||||
];
|
||||
let old_envelope_b64 = envelope_b64.clone();
|
||||
let new_envelope_b64 = BASE64_STANDARD.encode_to_string(&response.ciphertext);
|
||||
let mut overrides = HashMap::new();
|
||||
for (stored_name, stored_value) in metadata {
|
||||
let is_envelope_slot = REWRAP_ENVELOPE_HEADERS
|
||||
.iter()
|
||||
.any(|header| stored_name.eq_ignore_ascii_case(header));
|
||||
if is_envelope_slot && *stored_value == old_envelope_b64 {
|
||||
overrides.insert(stored_name.clone(), new_envelope_b64.clone());
|
||||
}
|
||||
}
|
||||
if overrides.is_empty() {
|
||||
return Err(SseError::internal(
|
||||
"rewrapped a data-key envelope but found no stored metadata copy to replace; refusing a write that would \
|
||||
leave the old wrapping live",
|
||||
));
|
||||
}
|
||||
|
||||
Ok(ObjectDekRewrapOutcome::Rewrapped { metadata: overrides })
|
||||
}
|
||||
@@ -1,177 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Per-key KMS authorization on the SSE-KMS data path.
|
||||
|
||||
use crate::core::audit::{record_managed_kms_outcome, stored_envelope_master_key_version};
|
||||
use crate::core::request::SseKmsPrincipal;
|
||||
use crate::error::SseError;
|
||||
use crate::host::SseHost;
|
||||
use crate::{LOG_COMPONENT_STORAGE, LOG_SUBSYSTEM_SSE};
|
||||
use rustfs_config::ENV_RUSTFS_KMS_ENFORCE_SSE_KEY_POLICY;
|
||||
use rustfs_policy::policy::action::KmsAction;
|
||||
use rustfs_s3_types::S3ErrorCode;
|
||||
use rustfs_utils::http::object_encryption_keys::{SSEType, stored_managed_encryption_key};
|
||||
use std::collections::HashMap;
|
||||
use tracing::debug;
|
||||
|
||||
/// Ask the authorizer that decides for `principal`: the host's, unless a test overrode it.
|
||||
async fn is_kms_key_allowed(
|
||||
host: &dyn SseHost,
|
||||
principal: &SseKmsPrincipal,
|
||||
action: KmsAction,
|
||||
key_id: &str,
|
||||
) -> Result<bool, SseError> {
|
||||
#[cfg(any(test, feature = "test-util"))]
|
||||
if let Some(hooks) = principal.test_hooks.as_ref() {
|
||||
return hooks.authorizer.is_allowed(principal, action, key_id).await;
|
||||
}
|
||||
|
||||
host.kms_key_authorizer().is_allowed(principal, action, key_id).await
|
||||
}
|
||||
|
||||
/// Whether per-key KMS authorization is enforced on the SSE-KMS data path.
|
||||
fn sse_kms_key_policy_enforced(principal: Option<&SseKmsPrincipal>) -> bool {
|
||||
#[cfg(any(test, feature = "test-util"))]
|
||||
if let Some(hooks) = principal.and_then(|candidate| candidate.test_hooks.as_ref()) {
|
||||
return hooks.enforced;
|
||||
}
|
||||
#[cfg(not(any(test, feature = "test-util")))]
|
||||
let _ = principal;
|
||||
|
||||
crate::startup::key_policy_enforced()
|
||||
}
|
||||
|
||||
/// The principal whose KMS key permissions this operation must satisfy, if any.
|
||||
///
|
||||
/// Scoping is limited to SSE-KMS, matching AWS: SSE-S3 wraps its data key with a
|
||||
/// server-owned key the caller never names, and SSE-C never reaches KMS at all.
|
||||
pub(crate) fn kms_authorization_subject(
|
||||
enforced: bool,
|
||||
principal: Option<&SseKmsPrincipal>,
|
||||
sse_type: SSEType,
|
||||
) -> Option<&SseKmsPrincipal> {
|
||||
if !enforced || !matches!(sse_type, SSEType::SseKms) {
|
||||
return None;
|
||||
}
|
||||
|
||||
principal
|
||||
}
|
||||
|
||||
/// Authorize `action` against `key_id` for the caller behind this SSE operation.
|
||||
///
|
||||
/// Runs before the key is used, so a denied request cannot distinguish an unauthorized
|
||||
/// key from a disabled or pending-deletion one.
|
||||
pub(crate) async fn authorize_sse_kms_key(
|
||||
host: &dyn SseHost,
|
||||
principal: Option<&SseKmsPrincipal>,
|
||||
sse_type: SSEType,
|
||||
action: KmsAction,
|
||||
key_id: &str,
|
||||
) -> Result<(), SseError> {
|
||||
let Some(principal) = kms_authorization_subject(sse_kms_key_policy_enforced(principal), principal, sse_type) else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
if is_kms_key_allowed(host, principal, action, key_id).await? {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
debug!(
|
||||
component = LOG_COMPONENT_STORAGE,
|
||||
subsystem = LOG_SUBSYSTEM_SSE,
|
||||
event = "sse_kms_key_authorization_denied",
|
||||
account = %principal.account,
|
||||
action = ?action,
|
||||
"Principal is not authorized for the KMS key resolved for this request"
|
||||
);
|
||||
|
||||
// One warn per process, not per request: anonymous denials are driven by
|
||||
// unauthenticated traffic, so a per-request warn would let anyone flood the
|
||||
// log. Per-request detail stays on the audit entry and the debug event above.
|
||||
if principal.account.is_empty() {
|
||||
static ANONYMOUS_DENIAL_WARNED: std::sync::Once = std::sync::Once::new();
|
||||
ANONYMOUS_DENIAL_WARNED.call_once(|| {
|
||||
tracing::warn!(
|
||||
component = LOG_COMPONENT_STORAGE,
|
||||
subsystem = LOG_SUBSYSTEM_SSE,
|
||||
event = "sse_kms_anonymous_key_authorization_denied",
|
||||
action = ?action,
|
||||
"Anonymous requests are being denied by SSE-KMS per-key authorization: anonymous \
|
||||
callers hold no kms grants, so a public bucket serving SSE-KMS objects is \
|
||||
incompatible with {ENV_RUSTFS_KMS_ENFORCE_SSE_KEY_POLICY}=true. Reported once per \
|
||||
process; per-request denials are on audit entries and at debug level."
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Err(SseError {
|
||||
code: S3ErrorCode::AccessDenied,
|
||||
message: "Access Denied".to_string(),
|
||||
source: None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Authorize `kms:Decrypt` for an object whose stored encryption material is unwrapped
|
||||
/// outside [`sse_decryption`].
|
||||
///
|
||||
/// The copy-source read resolves its material inside the object layer, which has no
|
||||
/// request identity, so the S3 layer has to run the check itself.
|
||||
pub async fn authorize_sse_kms_object_read(
|
||||
host: &dyn SseHost,
|
||||
principal: Option<&SseKmsPrincipal>,
|
||||
metadata: &HashMap<String, String>,
|
||||
) -> Result<(), SseError> {
|
||||
let Some((sse_type, key_id)) = stored_managed_encryption_key(metadata) else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let result = authorize_sse_kms_key(host, principal, sse_type, KmsAction::DecryptAction, &key_id).await;
|
||||
// Only a denial is recorded here: an allowed read goes on to unwrap the key,
|
||||
// and that operation reports its own outcome.
|
||||
if let Err(error) = &result {
|
||||
record_managed_kms_outcome(
|
||||
principal,
|
||||
sse_type,
|
||||
Some(&key_id),
|
||||
|| stored_envelope_master_key_version(metadata),
|
||||
Err(error),
|
||||
);
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
/// Authorize the current request to write under the upload's stored KMS key.
|
||||
/// Upload IDs do not confer the creator's KMS permissions on another caller.
|
||||
pub async fn authorize_sse_kms_multipart_write(
|
||||
host: &dyn SseHost,
|
||||
principal: Option<&SseKmsPrincipal>,
|
||||
metadata: &HashMap<String, String>,
|
||||
) -> Result<(), SseError> {
|
||||
let Some((sse_type, key_id)) = stored_managed_encryption_key(metadata) else {
|
||||
return Ok(());
|
||||
};
|
||||
let result = authorize_sse_kms_key(host, principal, sse_type, KmsAction::GenerateDataKeyAction, &key_id).await;
|
||||
if let Err(error) = &result {
|
||||
record_managed_kms_outcome(
|
||||
principal,
|
||||
sse_type,
|
||||
Some(&key_id),
|
||||
|| stored_envelope_master_key_version(metadata),
|
||||
Err(error),
|
||||
);
|
||||
}
|
||||
result
|
||||
}
|
||||
@@ -1,454 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! SSE request headers in, SSE response headers out.
|
||||
|
||||
use crate::core::keying::metadata::{normalize_encryption_metadata_case, recode_minio_kms_context, stored_ssec_metadata};
|
||||
use crate::core::s3::ssec::ssec_invalid_request;
|
||||
use crate::error::SseError;
|
||||
use base64_simd::STANDARD as BASE64_STANDARD;
|
||||
use http::{HeaderMap, HeaderName, HeaderValue};
|
||||
use rustfs_s3_types::S3ErrorCode;
|
||||
use rustfs_utils::http::headers::{
|
||||
AMZ_ENCRYPTION_AES, AMZ_ENCRYPTION_KMS, AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_ALGORITHM,
|
||||
AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY, AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY_MD5, AMZ_SERVER_SIDE_ENCRYPTION_KMS_CONTEXT,
|
||||
SSEC_ALGORITHM_HEADER, SSEC_KEY_MD5_HEADER,
|
||||
};
|
||||
use rustfs_utils::http::object_encryption_keys::{
|
||||
INTERNAL_ENCRYPTION_CONTEXT_HEADER, INTERNAL_ENCRYPTION_KEY_ID_HEADER, MINIO_INTERNAL_ENCRYPTION_KMS_CONTEXT_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_KMS_KEY_ID_HEADER, MINIO_INTERNAL_ENCRYPTION_KMS_SEALED_KEY_HEADER,
|
||||
MINIO_INTERNAL_ENCRYPTION_S3_SEALED_KEY_HEADER, MINIO_INTERNAL_ENCRYPTION_SSEC_SEALED_KEY_HEADER, SSEType,
|
||||
normalize_managed_metadata,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
|
||||
// The same header names s3s publishes. Owned so this module does not import s3s.
|
||||
pub(crate) const X_AMZ_SERVER_SIDE_ENCRYPTION: HeaderName = HeaderName::from_static("x-amz-server-side-encryption");
|
||||
|
||||
pub(crate) const X_AMZ_SERVER_SIDE_ENCRYPTION_AWS_KMS_KEY_ID: HeaderName =
|
||||
HeaderName::from_static("x-amz-server-side-encryption-aws-kms-key-id");
|
||||
|
||||
pub(crate) const X_AMZ_SERVER_SIDE_ENCRYPTION_CONTEXT: HeaderName =
|
||||
HeaderName::from_static("x-amz-server-side-encryption-context");
|
||||
|
||||
pub(crate) const X_AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_ALGORITHM: HeaderName =
|
||||
HeaderName::from_static("x-amz-server-side-encryption-customer-algorithm");
|
||||
|
||||
pub(crate) const X_AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY_MD5: HeaderName =
|
||||
HeaderName::from_static("x-amz-server-side-encryption-customer-key-md5");
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn sse_invalid_argument(message: &str) -> SseError {
|
||||
SseError {
|
||||
code: S3ErrorCode::InvalidArgument,
|
||||
message: message.to_string(),
|
||||
source: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// SSE-C parameters extracted from headers (algorithm, key, key MD5).
|
||||
pub type SsecParamsFromHeaders = (Option<String>, Option<String>, Option<String>);
|
||||
|
||||
/// Extract SSE-C parameters from request headers.
|
||||
/// Used as fallback when the S3 layer does not populate them in the input struct.
|
||||
///
|
||||
/// Returns an error if an SSE-C header is present but cannot be parsed as valid UTF-8,
|
||||
/// ensuring malformed headers do not bypass validation.
|
||||
pub fn extract_ssec_params_from_headers(headers: &HeaderMap) -> Result<SsecParamsFromHeaders, SseError> {
|
||||
let algorithm = match headers.get("x-amz-server-side-encryption-customer-algorithm") {
|
||||
None => Ok(None),
|
||||
Some(v) => v
|
||||
.to_str()
|
||||
.map(|s| Some(s.to_string()))
|
||||
.map_err(|_| sse_invalid_argument("The x-amz-server-side-encryption-customer-algorithm header must be valid UTF-8.")),
|
||||
}?;
|
||||
|
||||
let key = match headers.get("x-amz-server-side-encryption-customer-key") {
|
||||
None => Ok(None),
|
||||
Some(v) => v
|
||||
.to_str()
|
||||
.map(|s| Some(s.to_string()))
|
||||
.map_err(|_| sse_invalid_argument("The x-amz-server-side-encryption-customer-key header must be valid UTF-8.")),
|
||||
}?;
|
||||
|
||||
let key_md5 = match headers.get("x-amz-server-side-encryption-customer-key-md5") {
|
||||
None => Ok(None),
|
||||
Some(v) => v
|
||||
.to_str()
|
||||
.map(|s| Some(s.to_string()))
|
||||
.map_err(|_| sse_invalid_argument("The x-amz-server-side-encryption-customer-key-md5 header must be valid UTF-8.")),
|
||||
}?;
|
||||
|
||||
Ok((algorithm, key, key_md5))
|
||||
}
|
||||
|
||||
/// Extract x-amz-server-side-encryption from request headers.
|
||||
/// Used as fallback when the S3 layer does not populate it in the input struct.
|
||||
///
|
||||
/// Returns an error if the header is present but cannot be parsed as valid UTF-8,
|
||||
/// ensuring malformed headers do not bypass validation.
|
||||
pub fn extract_server_side_encryption_from_headers(headers: &HeaderMap) -> Result<Option<String>, SseError> {
|
||||
match headers.get("x-amz-server-side-encryption") {
|
||||
None => Ok(None),
|
||||
Some(v) => v
|
||||
.to_str()
|
||||
.map(|s| Some(s.to_string()))
|
||||
.map_err(|_| sse_invalid_argument("The x-amz-server-side-encryption header must be valid UTF-8.")),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn extract_ssekms_context_from_headers(headers: &HeaderMap) -> Result<Option<HashMap<String, String>>, SseError> {
|
||||
let Some(v) = headers.get(AMZ_SERVER_SIDE_ENCRYPTION_KMS_CONTEXT) else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
let value = v
|
||||
.to_str()
|
||||
.map_err(|_| sse_invalid_argument("The x-amz-server-side-encryption-context header must be valid UTF-8."))?;
|
||||
let decoded = BASE64_STANDARD.decode_to_vec(value).map_err(|_| {
|
||||
sse_invalid_argument("The x-amz-server-side-encryption-context header must be valid base64-encoded JSON.")
|
||||
})?;
|
||||
|
||||
serde_json::from_slice(&decoded).map(Some).map_err(|_| {
|
||||
sse_invalid_argument("The x-amz-server-side-encryption-context header must be a base64-encoded JSON object.")
|
||||
})
|
||||
}
|
||||
|
||||
/// Rejects a client encryption context that would replace the location binding.
|
||||
///
|
||||
/// The request context binds the data key to `{bucket: bucket/key}` only when the
|
||||
/// client supplied no entry under that name, and only the client context is
|
||||
/// persisted. A client entry keyed by the bucket name therefore travels with the
|
||||
/// object and replaces the binding on every read, so the envelope would no longer
|
||||
/// be tied to the object's location. Only the write side rejects it: objects stored
|
||||
/// with such an entry stay readable because the read side rebuilds the context
|
||||
/// unchanged.
|
||||
pub(crate) fn reject_kms_context_location_override(
|
||||
bucket: &str,
|
||||
ssekms_context: Option<&HashMap<String, String>>,
|
||||
) -> Result<(), SseError> {
|
||||
if ssekms_context.is_some_and(|context| context.contains_key(bucket)) {
|
||||
return Err(sse_invalid_argument(&format!(
|
||||
"The x-amz-server-side-encryption-context header contains the key \"{bucket}\", which equals the bucket name; \
|
||||
that key is reserved for binding the data key to the object's location."
|
||||
)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn validate_sse_headers_for_write(
|
||||
server_side_encryption: Option<&str>,
|
||||
ssekms_key_id: Option<&String>,
|
||||
ssekms_context: Option<&HashMap<String, String>>,
|
||||
sse_customer_algorithm: Option<&String>,
|
||||
sse_customer_key: Option<&String>,
|
||||
sse_customer_key_md5: Option<&String>,
|
||||
require_sse_customer_key: bool,
|
||||
) -> Result<(), SseError> {
|
||||
if let Some(sse) = server_side_encryption {
|
||||
let s = sse;
|
||||
if s != AMZ_ENCRYPTION_AES && s != AMZ_ENCRYPTION_KMS {
|
||||
return Err(sse_invalid_argument(
|
||||
"The SSE algorithm specified is not supported. The valid values are AES256 or aws:kms.",
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
let has_ssec_headers = sse_customer_algorithm.is_some() || sse_customer_key.is_some() || sse_customer_key_md5.is_some();
|
||||
let has_managed_headers = server_side_encryption.is_some() || ssekms_key_id.is_some() || ssekms_context.is_some();
|
||||
|
||||
if has_ssec_headers {
|
||||
if has_managed_headers {
|
||||
return Err(sse_invalid_argument(
|
||||
"The SSE-C and managed server-side encryption headers cannot be used together.",
|
||||
));
|
||||
}
|
||||
|
||||
let has_valid_ssec_headers = if require_sse_customer_key {
|
||||
matches!(
|
||||
(sse_customer_algorithm, sse_customer_key, sse_customer_key_md5),
|
||||
(Some(_), Some(_), Some(_))
|
||||
)
|
||||
} else {
|
||||
matches!((sse_customer_algorithm, sse_customer_key_md5), (Some(_), Some(_)))
|
||||
};
|
||||
|
||||
if !has_valid_ssec_headers {
|
||||
let message = if require_sse_customer_key {
|
||||
"Missing SSE-C parameters. Algorithm, customer key and customer key MD5 are all required."
|
||||
} else {
|
||||
"Missing SSE-C parameters. Algorithm and customer key MD5 are required."
|
||||
};
|
||||
|
||||
return Err(ssec_invalid_request(message));
|
||||
}
|
||||
}
|
||||
|
||||
if ssekms_key_id.is_some() && server_side_encryption.is_none_or(|sse| sse != AMZ_ENCRYPTION_KMS) {
|
||||
return Err(sse_invalid_argument(
|
||||
"The SSE-KMS key ID header can only be used when x-amz-server-side-encryption is set to aws:kms.",
|
||||
));
|
||||
}
|
||||
|
||||
if ssekms_context.is_some() && server_side_encryption.is_none_or(|sse| sse != AMZ_ENCRYPTION_KMS) {
|
||||
return Err(sse_invalid_argument(
|
||||
"The SSE-KMS context header can only be used when x-amz-server-side-encryption is set to aws:kms.",
|
||||
));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn validate_sse_headers_for_read(metadata: &HashMap<String, String>, headers: &HeaderMap) -> Result<(), SseError> {
|
||||
let has_req_ssec = headers.contains_key("x-amz-server-side-encryption-customer-algorithm")
|
||||
|| headers.contains_key("x-amz-server-side-encryption-customer-key")
|
||||
|| headers.contains_key("x-amz-server-side-encryption-customer-key-md5");
|
||||
|
||||
let has_req_sse = headers.contains_key("x-amz-server-side-encryption")
|
||||
|| headers.contains_key("x-amz-server-side-encryption-aws-kms-key-id")
|
||||
|| headers.contains_key("x-amz-server-side-encryption-context");
|
||||
|
||||
let is_object_ssec = stored_ssec_metadata(metadata);
|
||||
let is_object_sse = metadata.contains_key("x-amz-server-side-encryption");
|
||||
|
||||
if is_object_ssec {
|
||||
if has_req_sse {
|
||||
return Err(sse_invalid_argument(
|
||||
"Server-side encryption headers cannot be used with an object encrypted using SSE-C.",
|
||||
));
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if is_object_sse && has_req_ssec {
|
||||
return Err(sse_invalid_argument(
|
||||
"SSE-C headers cannot be used with an object encrypted using server-side managed encryption.",
|
||||
));
|
||||
}
|
||||
|
||||
if has_req_ssec {
|
||||
return Err(ssec_invalid_request(
|
||||
"The object was stored without SSE-C. The correct SSE-C parameters must not be provided.",
|
||||
));
|
||||
}
|
||||
|
||||
if has_req_sse {
|
||||
return Err(sse_invalid_argument(
|
||||
"The object is not encrypted with server-side encryption. Do not provide server-side encryption headers.",
|
||||
));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The public `x-amz-server-side-encryption` value a managed scheme reports.
|
||||
pub(crate) fn managed_sse_public_header(sse_type: SSEType) -> &'static str {
|
||||
match sse_type {
|
||||
SSEType::SseKms => AMZ_ENCRYPTION_KMS,
|
||||
// SSE-C never reaches the managed path; reporting AES256 keeps this
|
||||
// total without inventing a third public value.
|
||||
SSEType::SseS3 | SSEType::SseC => AMZ_ENCRYPTION_AES,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build_ssec_read_headers(algorithm: Option<&String>, key: Option<&String>, key_md5: Option<&String>) -> HeaderMap {
|
||||
let mut headers = HeaderMap::new();
|
||||
|
||||
if let Some(algorithm) = algorithm
|
||||
&& let Ok(mut value) = HeaderValue::from_str(algorithm.as_str())
|
||||
{
|
||||
value.set_sensitive(true);
|
||||
headers.insert(AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_ALGORITHM, value);
|
||||
}
|
||||
|
||||
if let Some(key) = key
|
||||
&& let Ok(mut value) = HeaderValue::from_str(key.as_str())
|
||||
{
|
||||
value.set_sensitive(true);
|
||||
headers.insert(AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY, value);
|
||||
}
|
||||
|
||||
if let Some(key_md5) = key_md5
|
||||
&& let Ok(mut value) = HeaderValue::from_str(key_md5.as_str())
|
||||
{
|
||||
value.set_sensitive(true);
|
||||
headers.insert(AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY_MD5, value);
|
||||
}
|
||||
|
||||
headers
|
||||
}
|
||||
|
||||
/// Response headers derived for a decrypted read without unwrapping the data key.
|
||||
#[derive(Debug)]
|
||||
pub struct SseReadResponseHeaders {
|
||||
pub server_side_encryption: String,
|
||||
pub sse_customer_algorithm: Option<String>,
|
||||
pub sse_customer_key_md5: Option<String>,
|
||||
pub ssekms_key_id: Option<String>,
|
||||
}
|
||||
|
||||
fn invalid_sse_response_metadata() -> SseError {
|
||||
SseError {
|
||||
code: S3ErrorCode::InternalError,
|
||||
message: "Persisted object encryption metadata is invalid.".to_string(),
|
||||
source: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn insert_sse_response_header(headers: &mut HeaderMap, name: HeaderName, value: &str) -> Result<(), SseError> {
|
||||
let value = HeaderValue::from_str(value).map_err(|_| invalid_sse_response_metadata())?;
|
||||
headers.insert(name, value);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Project response headers after the caller has completed its existing read
|
||||
/// validation and KMS authorization path.
|
||||
pub fn project_sse_read_response_headers(
|
||||
metadata: &HashMap<String, String>,
|
||||
sse_customer_algorithm: Option<&String>,
|
||||
sse_customer_key_md5: Option<&String>,
|
||||
) -> Result<HeaderMap, SseError> {
|
||||
if metadata.keys().any(|key| {
|
||||
key.eq_ignore_ascii_case(AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY)
|
||||
|| key.eq_ignore_ascii_case(AMZ_SERVER_SIDE_ENCRYPTION_KMS_CONTEXT)
|
||||
}) {
|
||||
return Err(invalid_sse_response_metadata());
|
||||
}
|
||||
|
||||
let metadata = normalize_encryption_metadata_case(metadata).map_err(|_| invalid_sse_response_metadata())?;
|
||||
let public_mode = metadata.get("x-amz-server-side-encryption").map(String::as_str);
|
||||
let customer_algorithm = metadata.get(SSEC_ALGORITHM_HEADER).map(String::as_str);
|
||||
|
||||
let public_mode = match public_mode {
|
||||
Some(AMZ_ENCRYPTION_AES) => Some(SSEType::SseS3),
|
||||
Some(AMZ_ENCRYPTION_KMS) => Some(SSEType::SseKms),
|
||||
Some(_) => return Err(invalid_sse_response_metadata()),
|
||||
None => None,
|
||||
};
|
||||
if customer_algorithm.is_some_and(|algorithm| algorithm != AMZ_ENCRYPTION_AES) {
|
||||
return Err(invalid_sse_response_metadata());
|
||||
}
|
||||
|
||||
let mode = if customer_algorithm.is_some() {
|
||||
if public_mode == Some(SSEType::SseKms) {
|
||||
return Err(invalid_sse_response_metadata());
|
||||
}
|
||||
Some(SSEType::SseC)
|
||||
} else {
|
||||
public_mode
|
||||
};
|
||||
for marker_mode in [
|
||||
metadata
|
||||
.contains_key(MINIO_INTERNAL_ENCRYPTION_SSEC_SEALED_KEY_HEADER)
|
||||
.then_some(SSEType::SseC),
|
||||
metadata
|
||||
.contains_key(MINIO_INTERNAL_ENCRYPTION_S3_SEALED_KEY_HEADER)
|
||||
.then_some(SSEType::SseS3),
|
||||
metadata
|
||||
.contains_key(MINIO_INTERNAL_ENCRYPTION_KMS_SEALED_KEY_HEADER)
|
||||
.then_some(SSEType::SseKms),
|
||||
]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
{
|
||||
if mode != Some(marker_mode) {
|
||||
return Err(invalid_sse_response_metadata());
|
||||
}
|
||||
}
|
||||
let Some(mode) = mode else {
|
||||
if metadata
|
||||
.keys()
|
||||
.any(|key| rustfs_utils::http::is_object_encryption_marker(key))
|
||||
{
|
||||
return Err(invalid_sse_response_metadata());
|
||||
}
|
||||
return Ok(HeaderMap::new());
|
||||
};
|
||||
|
||||
let mut key_id = None;
|
||||
for value in [
|
||||
metadata.get("x-amz-server-side-encryption-aws-kms-key-id"),
|
||||
metadata.get(INTERNAL_ENCRYPTION_KEY_ID_HEADER),
|
||||
metadata.get(MINIO_INTERNAL_ENCRYPTION_KMS_KEY_ID_HEADER),
|
||||
]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
{
|
||||
if key_id.is_some_and(|current| current != value) {
|
||||
return Err(invalid_sse_response_metadata());
|
||||
}
|
||||
key_id = Some(value);
|
||||
}
|
||||
|
||||
let normalized_managed = normalize_managed_metadata(metadata.as_ref(), Some(recode_minio_kms_context));
|
||||
let mut response_headers = HeaderMap::with_capacity(3);
|
||||
match mode {
|
||||
SSEType::SseS3 => {
|
||||
if metadata.contains_key("x-amz-server-side-encryption-aws-kms-key-id") || metadata.contains_key(SSEC_KEY_MD5_HEADER)
|
||||
{
|
||||
return Err(invalid_sse_response_metadata());
|
||||
}
|
||||
response_headers.insert(X_AMZ_SERVER_SIDE_ENCRYPTION, HeaderValue::from_static(AMZ_ENCRYPTION_AES));
|
||||
}
|
||||
SSEType::SseKms => {
|
||||
if metadata.contains_key(SSEC_KEY_MD5_HEADER) {
|
||||
return Err(invalid_sse_response_metadata());
|
||||
}
|
||||
let key_id = key_id
|
||||
.map(String::as_str)
|
||||
.filter(|value| !value.is_empty())
|
||||
.ok_or_else(invalid_sse_response_metadata)?;
|
||||
response_headers.insert(X_AMZ_SERVER_SIDE_ENCRYPTION, HeaderValue::from_static(AMZ_ENCRYPTION_KMS));
|
||||
insert_sse_response_header(&mut response_headers, X_AMZ_SERVER_SIDE_ENCRYPTION_AWS_KMS_KEY_ID, key_id)?;
|
||||
|
||||
if metadata.contains_key(MINIO_INTERNAL_ENCRYPTION_KMS_CONTEXT_HEADER)
|
||||
&& !normalized_managed.contains_key(INTERNAL_ENCRYPTION_CONTEXT_HEADER)
|
||||
{
|
||||
return Err(invalid_sse_response_metadata());
|
||||
}
|
||||
if let Some(context) = normalized_managed.get(INTERNAL_ENCRYPTION_CONTEXT_HEADER) {
|
||||
serde_json::from_str::<HashMap<String, String>>(context).map_err(|_| invalid_sse_response_metadata())?;
|
||||
let encoded = BASE64_STANDARD.encode_to_string(context);
|
||||
insert_sse_response_header(&mut response_headers, X_AMZ_SERVER_SIDE_ENCRYPTION_CONTEXT, &encoded)?;
|
||||
}
|
||||
}
|
||||
SSEType::SseC => {
|
||||
if key_id.is_some() || normalized_managed.contains_key(INTERNAL_ENCRYPTION_CONTEXT_HEADER) {
|
||||
return Err(invalid_sse_response_metadata());
|
||||
}
|
||||
let algorithm = sse_customer_algorithm
|
||||
.map(|value| value.as_ref())
|
||||
.filter(|value| *value == AMZ_ENCRYPTION_AES)
|
||||
.ok_or_else(|| {
|
||||
ssec_invalid_request(
|
||||
"The object was stored using a form of Server Side Encryption. The correct parameters must be provided to retrieve the object.",
|
||||
)
|
||||
})?;
|
||||
let key_md5 = sse_customer_key_md5
|
||||
.map(|value| value.as_ref())
|
||||
.ok_or_else(|| ssec_invalid_request("Missing SSE-C customer key MD5."))?;
|
||||
let stored_md5 = metadata.get(SSEC_KEY_MD5_HEADER).ok_or_else(invalid_sse_response_metadata)?;
|
||||
if stored_md5 != key_md5 {
|
||||
return Err(ssec_invalid_request(
|
||||
"The provided encryption parameters did not match the ones used originally to encrypt the object.",
|
||||
));
|
||||
}
|
||||
insert_sse_response_header(&mut response_headers, X_AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_ALGORITHM, algorithm)?;
|
||||
insert_sse_response_header(&mut response_headers, X_AMZ_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY_MD5, key_md5)?;
|
||||
}
|
||||
}
|
||||
Ok(response_headers)
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! The schemes S3 defines: SSE-S3, SSE-KMS and SSE-C, as a request names them and a response reports them.
|
||||
|
||||
pub(crate) mod authorization;
|
||||
pub(crate) mod headers;
|
||||
pub(crate) mod scheme;
|
||||
pub(crate) mod ssec;
|
||||
@@ -1,288 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Which SSE scheme and which key a request resolves to: the request first, then the bucket default.
|
||||
|
||||
use crate::core::s3::headers::validate_sse_headers_for_write;
|
||||
use crate::error::SseError;
|
||||
use crate::host::SseHost;
|
||||
use crate::{LOG_COMPONENT_STORAGE, LOG_SUBSYSTEM_SSE};
|
||||
use rustfs_utils::http::headers::{AMZ_ENCRYPTION_AES, AMZ_ENCRYPTION_KMS};
|
||||
use rustfs_utils::http::object_encryption_keys::SSEType;
|
||||
use std::collections::HashMap;
|
||||
use tracing::debug;
|
||||
|
||||
pub const DEFAULT_SSE_ALGORITHM: &str = "AES256";
|
||||
|
||||
pub(crate) const SUPPORT_SSE_ALGORITHMS: &[&str] = &[DEFAULT_SSE_ALGORITHM];
|
||||
|
||||
/// SSE configuration resolved from request and bucket defaults
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct SseConfiguration {
|
||||
/// Effective server-side encryption algorithm (after considering bucket defaults)
|
||||
pub effective_sse: String,
|
||||
/// Effective KMS key ID (after considering bucket defaults)
|
||||
pub effective_kms_key_id: Option<String>,
|
||||
}
|
||||
|
||||
/// Managed SSE resolved from a bucket default encryption rule on a write path.
|
||||
///
|
||||
/// The single mapping shared by every writer: the resolver in this crate, and
|
||||
/// the server's PUT, COPY and extract paths. Unknown algorithms fall back to AES256 rather
|
||||
/// than to `None`. Resolving `None` instead lets a same-name copy under a
|
||||
/// malformed bucket default pass the `copy_changes_encryption` guard and take
|
||||
/// the metadata-only shortcut while this layer still encrypts: fresh DEK
|
||||
/// metadata is committed beside the untouched plaintext blocks and the object
|
||||
/// becomes unreadable. PutBucketEncryption refuses unknown algorithms, so this
|
||||
/// is reachable only through a configuration stored before that check or
|
||||
/// through hand-edited bucket metadata (backlog#1826).
|
||||
pub fn bucket_default_write_sse(sse_algorithm: &str) -> String {
|
||||
match sse_algorithm {
|
||||
"AES256" => String::from(AMZ_ENCRYPTION_AES),
|
||||
"aws:kms" => String::from(AMZ_ENCRYPTION_KMS),
|
||||
_ => String::from(AMZ_ENCRYPTION_AES),
|
||||
}
|
||||
}
|
||||
|
||||
/// Prepare SSE configuration by resolving request parameters with bucket defaults
|
||||
///
|
||||
/// This function:
|
||||
/// 1. Queries bucket default encryption configuration
|
||||
/// 2. Resolves effective encryption (request overrides bucket default)
|
||||
/// 3. Prepares metadata headers for managed SSE
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `bucket` - Bucket name
|
||||
/// * `server_side_encryption` - SSE algorithm from request (SSE-S3 or SSE-KMS)
|
||||
/// * `ssekms_key_id` - KMS key ID from request
|
||||
/// * `sse_customer_algorithm` - SSE-C algorithm from request
|
||||
///
|
||||
/// # Returns
|
||||
/// `SseConfiguration` with resolved encryption parameters and metadata headers
|
||||
pub(crate) async fn prepare_sse_configuration(
|
||||
host: &dyn SseHost,
|
||||
bucket: &str,
|
||||
server_side_encryption: Option<String>,
|
||||
ssekms_key_id: Option<String>,
|
||||
) -> Result<Option<SseConfiguration>, SseError> {
|
||||
if let Some(server_side_encryption) = server_side_encryption.clone()
|
||||
&& server_side_encryption.as_str() == AMZ_ENCRYPTION_AES
|
||||
{
|
||||
return Ok(Some(SseConfiguration {
|
||||
effective_sse: server_side_encryption,
|
||||
effective_kms_key_id: None,
|
||||
}));
|
||||
}
|
||||
|
||||
if let Some(server_side_encryption) = server_side_encryption.clone()
|
||||
&& let Some(ssekms_key_id) = ssekms_key_id
|
||||
{
|
||||
return Ok(Some(SseConfiguration {
|
||||
effective_sse: server_side_encryption,
|
||||
effective_kms_key_id: Some(ssekms_key_id),
|
||||
}));
|
||||
}
|
||||
|
||||
// Get bucket default encryption configuration.
|
||||
let bucket_encryption = host.bucket_encryption(bucket).await;
|
||||
debug!(
|
||||
component = LOG_COMPONENT_STORAGE,
|
||||
subsystem = LOG_SUBSYSTEM_SSE,
|
||||
event = "bucket_sse_config_lookup",
|
||||
bucket = %bucket,
|
||||
found = matches!(bucket_encryption, Ok(Some(_))),
|
||||
"Bucket SSE configuration lookup completed"
|
||||
);
|
||||
|
||||
let Some(bucket_encryption) = bucket_encryption? else {
|
||||
// The bucket has no SSE config. If the user explicitly requested
|
||||
// aws:kms, we must honor that — return the explicit SSE header so
|
||||
// downstream logic can try (and fail if KMS is unavailable).
|
||||
return Ok(server_side_encryption.map(|sse| SseConfiguration {
|
||||
effective_sse: sse,
|
||||
effective_kms_key_id: ssekms_key_id,
|
||||
}));
|
||||
};
|
||||
|
||||
let default_rule = bucket_encryption.default_rule.as_ref();
|
||||
let effective_sse = server_side_encryption.clone().or_else(|| {
|
||||
default_rule.map(|sse| {
|
||||
debug!(
|
||||
component = LOG_COMPONENT_STORAGE,
|
||||
subsystem = LOG_SUBSYSTEM_SSE,
|
||||
event = "bucket_sse_default_applied",
|
||||
bucket = %bucket,
|
||||
algorithm = sse.sse_algorithm.as_str(),
|
||||
has_kms_key_id = sse.kms_master_key_id.is_some(),
|
||||
"Bucket SSE default resolved"
|
||||
);
|
||||
bucket_default_write_sse(&sse.sse_algorithm)
|
||||
})
|
||||
});
|
||||
let Some(effective_sse) = effective_sse else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
debug!(
|
||||
component = LOG_COMPONENT_STORAGE,
|
||||
subsystem = LOG_SUBSYSTEM_SSE,
|
||||
event = "effective_sse_resolved",
|
||||
bucket = %bucket,
|
||||
requested = ?server_side_encryption,
|
||||
effective = ?effective_sse,
|
||||
"Resolved effective SSE configuration"
|
||||
);
|
||||
|
||||
let effective_kms_key_id = resolve_effective_kms_key_id(Some(&effective_sse), ssekms_key_id, || {
|
||||
default_rule.and_then(|sse| sse.kms_master_key_id.clone())
|
||||
});
|
||||
|
||||
Ok(Some(SseConfiguration {
|
||||
effective_sse,
|
||||
effective_kms_key_id,
|
||||
}))
|
||||
}
|
||||
|
||||
pub(crate) fn resolve_effective_kms_key_id<F>(
|
||||
effective_sse: Option<&str>,
|
||||
requested_kms_key_id: Option<String>,
|
||||
bucket_default_kms_key_id: F,
|
||||
) -> Option<String>
|
||||
where
|
||||
F: FnOnce() -> Option<String>,
|
||||
{
|
||||
if effective_sse.is_none_or(|sse| sse != AMZ_ENCRYPTION_KMS) {
|
||||
return requested_kms_key_id;
|
||||
}
|
||||
|
||||
requested_kms_key_id.or_else(bucket_default_kms_key_id)
|
||||
}
|
||||
|
||||
/// The SSE headers of a write request, as the S3 layer received them.
|
||||
pub(crate) struct WriteSchemeHeaders<'a> {
|
||||
pub(crate) server_side_encryption: Option<String>,
|
||||
pub(crate) ssekms_key_id: Option<String>,
|
||||
pub(crate) ssekms_context: Option<&'a HashMap<String, String>>,
|
||||
pub(crate) sse_customer_algorithm: Option<String>,
|
||||
pub(crate) sse_customer_key: Option<String>,
|
||||
pub(crate) sse_customer_key_md5: Option<String>,
|
||||
}
|
||||
|
||||
/// The scheme a write resolved to, in S3's terms.
|
||||
#[derive(Clone)]
|
||||
pub(crate) enum WriteScheme {
|
||||
/// `AES256`: the server chooses the wrapping key and the caller names none.
|
||||
SseS3,
|
||||
/// `aws:kms`, under the key the request or the bucket default named, if either did.
|
||||
SseKms { key_id: Option<String> },
|
||||
/// A key the client supplies; only its MD5 is ever stored.
|
||||
SseC {
|
||||
algorithm: String,
|
||||
key: String,
|
||||
key_md5: String,
|
||||
},
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for WriteScheme {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str(match self {
|
||||
Self::SseS3 => "SseS3",
|
||||
Self::SseKms { .. } => "SseKms(..)",
|
||||
Self::SseC { .. } => "SseC(..)",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Decide the scheme of a write: validate the headers, then the request first and the
|
||||
/// bucket default second.
|
||||
///
|
||||
/// Every write goes through here, an object and a multipart session alike, so the two
|
||||
/// cannot resolve the same headers differently.
|
||||
pub(crate) async fn resolve_write_scheme(
|
||||
host: &dyn SseHost,
|
||||
bucket: &str,
|
||||
headers: WriteSchemeHeaders<'_>,
|
||||
) -> Result<Option<WriteScheme>, SseError> {
|
||||
validate_sse_headers_for_write(
|
||||
headers.server_side_encryption.as_deref(),
|
||||
headers.ssekms_key_id.as_ref(),
|
||||
headers.ssekms_context,
|
||||
headers.sse_customer_algorithm.as_ref(),
|
||||
headers.sse_customer_key.as_ref(),
|
||||
headers.sse_customer_key_md5.as_ref(),
|
||||
true,
|
||||
)?;
|
||||
|
||||
// Validation admits SSE-C headers only as a complete set, and never beside the
|
||||
// managed ones, so their presence alone decides.
|
||||
if let (Some(algorithm), Some(key), Some(key_md5)) =
|
||||
(headers.sse_customer_algorithm, headers.sse_customer_key, headers.sse_customer_key_md5)
|
||||
{
|
||||
return Ok(Some(WriteScheme::SseC { algorithm, key, key_md5 }));
|
||||
}
|
||||
|
||||
let Some(config) = prepare_sse_configuration(host, bucket, headers.server_side_encryption, headers.ssekms_key_id).await?
|
||||
else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
Ok(match config.effective_sse.as_str() {
|
||||
AMZ_ENCRYPTION_AES => {
|
||||
// Validation admits a key id only beside `aws:kms`, and the bucket default's
|
||||
// key is consulted only for `aws:kms`.
|
||||
debug_assert!(config.effective_kms_key_id.is_none(), "SSE-S3 never names a KMS key");
|
||||
Some(WriteScheme::SseS3)
|
||||
}
|
||||
AMZ_ENCRYPTION_KMS => Some(WriteScheme::SseKms {
|
||||
key_id: config.effective_kms_key_id,
|
||||
}),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Scheme a managed-SSE header names, defaulting to SSE-S3 the same way the
|
||||
/// encryption and decryption paths do.
|
||||
pub(crate) fn managed_sse_type(server_side_encryption: &str) -> SSEType {
|
||||
match server_side_encryption {
|
||||
AMZ_ENCRYPTION_KMS => SSEType::SseKms,
|
||||
_ => SSEType::SseS3,
|
||||
}
|
||||
}
|
||||
|
||||
/// Key id an SSE-KMS read authorizes and reports when the stored metadata
|
||||
/// carries none (legacy envelopes and MinIO-written objects predate the
|
||||
/// explicit key-id headers).
|
||||
///
|
||||
/// The KMS service's configured default key is the key such envelopes were
|
||||
/// actually wrapped under, so per-key authorization must target it — the bare
|
||||
/// literal `default` would silently escape any policy narrowed to real key
|
||||
/// ids. The literal remains only as the last resort when no service is
|
||||
/// running, matching what the legacy write path recorded implicitly.
|
||||
pub(crate) async fn resolve_stored_kms_key_id(host: &dyn SseHost, stored: Option<String>) -> String {
|
||||
if let Some(key_id) = stored {
|
||||
return key_id;
|
||||
}
|
||||
if let Some(service) = host.encryption_service().await
|
||||
&& let Some(key_id) = service.get_default_key_id()
|
||||
{
|
||||
return key_id.clone();
|
||||
}
|
||||
"default".to_string()
|
||||
}
|
||||
|
||||
/// Check if the server_side_encryption is a managed SSE type (SSE-S3 or SSE-KMS)
|
||||
#[inline]
|
||||
pub(crate) fn is_managed_sse(server_side_encryption: &str) -> bool {
|
||||
matches!(server_side_encryption, "AES256" | "aws:kms")
|
||||
}
|
||||
@@ -1,217 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! SSE-C: validating the customer-provided key and matching it against the stored object.
|
||||
|
||||
use crate::core::keying::metadata::{minio_sealed_ssec_metadata, stored_ssec_metadata};
|
||||
use crate::core::s3::scheme::{DEFAULT_SSE_ALGORITHM, SUPPORT_SSE_ALGORITHMS};
|
||||
use crate::error::SseError;
|
||||
use base64_simd::STANDARD as BASE64_STANDARD;
|
||||
use md5::{Digest as Md5Digest, Md5};
|
||||
use rustfs_s3_types::S3ErrorCode;
|
||||
use std::collections::HashMap;
|
||||
use tracing::error;
|
||||
|
||||
fn md5_bytes(input: impl AsRef<[u8]>) -> [u8; 16] {
|
||||
let mut hasher = Md5::new();
|
||||
hasher.update(input.as_ref());
|
||||
hasher.finalize().into()
|
||||
}
|
||||
|
||||
pub(crate) fn md5_base64(input: impl AsRef<[u8]>) -> String {
|
||||
BASE64_STANDARD.encode_to_string(md5_bytes(input))
|
||||
}
|
||||
|
||||
/// Validated SSE-C parameters
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct ValidatedSsecParams {
|
||||
/// Encryption algorithm (always "AES256" for SSE-C)
|
||||
pub algorithm: String,
|
||||
/// Decoded encryption key bytes (32 bytes for AES-256)
|
||||
pub key_bytes: [u8; 32],
|
||||
/// Base64-encoded MD5 of the key
|
||||
pub key_md5: String,
|
||||
}
|
||||
|
||||
/// SSE-C parameters from client request
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct SsecParams {
|
||||
/// Encryption algorithm
|
||||
pub algorithm: String,
|
||||
/// Base64-encoded encryption key
|
||||
pub key: String,
|
||||
/// Base64-encoded MD5 of the key
|
||||
pub key_md5: String,
|
||||
}
|
||||
|
||||
/// Validate SSE-C parameters from client request
|
||||
///
|
||||
/// Validates:
|
||||
/// 1. Algorithm is "AES256"
|
||||
/// 2. Key is valid Base64 and exactly 32 bytes
|
||||
/// 3. MD5 hash matches the key
|
||||
///
|
||||
/// # Returns
|
||||
/// `ValidatedSsecParams` with decoded key bytes
|
||||
pub(crate) fn validate_ssec_params(params: SsecParams) -> Result<ValidatedSsecParams, SseError> {
|
||||
if !SUPPORT_SSE_ALGORITHMS.contains(¶ms.algorithm.as_str()) {
|
||||
return Err(ssec_invalid_request(&format!(
|
||||
"Unsupported SSE-C algorithm. Only {DEFAULT_SSE_ALGORITHM} is supported."
|
||||
)));
|
||||
}
|
||||
|
||||
let key_bytes = BASE64_STANDARD.decode_to_vec(¶ms.key).map_err(|e| {
|
||||
error!("Failed to decode SSE-C key: {}", e);
|
||||
ssec_invalid_request("Invalid SSE-C key: not valid Base64.")
|
||||
})?;
|
||||
|
||||
if key_bytes.len() != 32 {
|
||||
return Err(ssec_invalid_request(&format!(
|
||||
"SSE-C key must be 32 bytes (256 bits), got {} bytes.",
|
||||
key_bytes.len()
|
||||
)));
|
||||
}
|
||||
|
||||
let computed_md5 = md5_base64(&key_bytes);
|
||||
if computed_md5 != params.key_md5 {
|
||||
return Err(ssec_invalid_request(
|
||||
"The calculated MD5 hash of the key did not match the hash that was provided.",
|
||||
));
|
||||
}
|
||||
|
||||
let key_array: [u8; 32] = key_bytes
|
||||
.try_into()
|
||||
.map_err(|_| ssec_invalid_request("SSE-C key must be exactly 32 bytes."))?;
|
||||
|
||||
Ok(ValidatedSsecParams {
|
||||
algorithm: params.algorithm,
|
||||
key_bytes: key_array,
|
||||
key_md5: params.key_md5,
|
||||
})
|
||||
}
|
||||
|
||||
/// Verify SSE-C key matches the stored metadata.
|
||||
///
|
||||
/// Used during GetObject/HeadObject to ensure the client provided the correct key.
|
||||
/// Returns 400 InvalidRequest on mismatch, consistent with AWS S3 behavior.
|
||||
pub(crate) fn verify_ssec_key_match(
|
||||
provided_md5: &str,
|
||||
stored_md5: Option<&String>,
|
||||
minio_sealed_ssec: bool,
|
||||
) -> Result<(), SseError> {
|
||||
match stored_md5 {
|
||||
Some(stored) if stored == provided_md5 => Ok(()),
|
||||
Some(_) => Err(ssec_invalid_request(
|
||||
"The provided encryption parameters did not match the ones used originally to encrypt the object.",
|
||||
)),
|
||||
// MinIO never persists the customer-key MD5, so this early check has
|
||||
// nothing to compare against on a migrated object. The key itself is
|
||||
// still proven: a wrong key fails the AEAD unseal of the sealed object
|
||||
// key. The check exists only for the friendlier early error, so its
|
||||
// absence is tolerated for exactly that shape and no other.
|
||||
None if minio_sealed_ssec => Ok(()),
|
||||
None => Err(ssec_invalid_request("Object has no stored SSE-C key metadata.")),
|
||||
}
|
||||
}
|
||||
|
||||
/// Algorithm an SSE-C object recorded; objects that recorded none were written as `AES256`.
|
||||
pub(crate) fn stored_ssec_algorithm(metadata: &HashMap<String, String>) -> &str {
|
||||
metadata
|
||||
.get("x-amz-server-side-encryption-customer-algorithm")
|
||||
.map(|s| s.as_str())
|
||||
.unwrap_or("AES256")
|
||||
}
|
||||
|
||||
/// The customer key and MD5 of a read of an SSE-C object, once both are present and
|
||||
/// the MD5 matches what the object recorded.
|
||||
///
|
||||
/// The key itself is not validated here; the caller does that next, so a request
|
||||
/// that names the wrong key learns of the mismatch before anything else.
|
||||
pub(crate) fn require_matching_ssec_key<'r>(
|
||||
metadata: &HashMap<String, String>,
|
||||
sse_customer_key: Option<&'r String>,
|
||||
sse_customer_key_md5: Option<&'r String>,
|
||||
) -> Result<(&'r String, &'r String), SseError> {
|
||||
let (key, key_md5) = match (sse_customer_key, sse_customer_key_md5) {
|
||||
(Some(k), Some(md5)) => (k, md5),
|
||||
_ => {
|
||||
return Err(ssec_invalid_request(
|
||||
"The object was stored using a form of Server Side Encryption. \
|
||||
The correct parameters must be provided to retrieve the object.",
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
// Verify that the provided key MD5 matches the stored MD5 for security
|
||||
let stored_md5 = metadata.get("x-amz-server-side-encryption-customer-key-md5");
|
||||
verify_ssec_key_match(key_md5, stored_md5, minio_sealed_ssec_metadata(metadata))?;
|
||||
|
||||
Ok((key, key_md5))
|
||||
}
|
||||
|
||||
/// Validate that the SSE-C headers required for reading an SSE-C encrypted object
|
||||
/// are present in the request. This is used by HeadObject which does not decrypt
|
||||
/// the data but still must verify the caller holds the correct key.
|
||||
///
|
||||
/// Performs full validation: decodes the customer key, recomputes its MD5,
|
||||
/// verifies the client-provided MD5 header matches the key, then compares
|
||||
/// the computed MD5 against the stored metadata. This prevents a client from
|
||||
/// bypassing validation by guessing/obtaining only the stored MD5 without
|
||||
/// possessing the actual encryption key.
|
||||
///
|
||||
/// Returns `Ok(())` if either the object is not SSE-C encrypted, or valid SSE-C
|
||||
/// headers are provided and the key matches. Returns 400 InvalidRequest otherwise.
|
||||
pub fn validate_ssec_for_read(
|
||||
metadata: &HashMap<String, String>,
|
||||
sse_customer_key: Option<&String>,
|
||||
sse_customer_key_md5: Option<&String>,
|
||||
) -> Result<(), SseError> {
|
||||
if !stored_ssec_metadata(metadata) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let (key, key_md5) = match (sse_customer_key, sse_customer_key_md5) {
|
||||
(Some(k), Some(md5)) => (k, md5),
|
||||
_ => {
|
||||
return Err(ssec_invalid_request(
|
||||
"The object was stored using a form of Server Side Encryption. \
|
||||
The correct parameters must be provided to retrieve the object.",
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
// Full param validation: decode key, verify 32 bytes, recompute MD5
|
||||
// from actual key bytes and compare to the client-provided MD5 header.
|
||||
let algorithm = metadata
|
||||
.get("x-amz-server-side-encryption-customer-algorithm")
|
||||
.cloned()
|
||||
.unwrap_or_else(|| DEFAULT_SSE_ALGORITHM.to_string());
|
||||
let validated = validate_ssec_params(SsecParams {
|
||||
algorithm,
|
||||
key: key.to_string(),
|
||||
key_md5: key_md5.clone(),
|
||||
})?;
|
||||
|
||||
let stored_md5 = metadata.get("x-amz-server-side-encryption-customer-key-md5");
|
||||
verify_ssec_key_match(&validated.key_md5, stored_md5, minio_sealed_ssec_metadata(metadata))
|
||||
}
|
||||
|
||||
/// Build an `SseError` with `InvalidRequest` (HTTP 400) for SSE-C related errors.
|
||||
pub fn ssec_invalid_request(message: &str) -> SseError {
|
||||
SseError {
|
||||
code: S3ErrorCode::InvalidRequest,
|
||||
message: message.to_string(),
|
||||
source: None,
|
||||
}
|
||||
}
|
||||
@@ -1,550 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Write entry points: resolve the encryption material for a new object or multipart upload.
|
||||
|
||||
use crate::core::audit::{envelope_master_key_version, record_managed_kms_outcome};
|
||||
use crate::core::keying::context::build_object_encryption_context;
|
||||
use crate::core::keying::material::EncryptionMaterial;
|
||||
use crate::core::keying::{
|
||||
SESSION_KEY_NEEDS_CUSTOMER_KEY, new_key_under_customer_key, new_key_under_data_key, new_session_key_under_customer_key,
|
||||
};
|
||||
use crate::core::request::SseKmsPrincipal;
|
||||
use crate::core::s3::authorization::authorize_sse_kms_key;
|
||||
use crate::core::s3::headers::reject_kms_context_location_override;
|
||||
use crate::core::s3::scheme::{
|
||||
DEFAULT_SSE_ALGORITHM, WriteScheme, WriteSchemeHeaders, is_managed_sse, managed_sse_type, resolve_write_scheme,
|
||||
};
|
||||
use crate::core::s3::ssec::{SsecParams, ssec_invalid_request, validate_ssec_params, verify_ssec_key_match};
|
||||
use crate::error::SseError;
|
||||
use crate::host::SseHost;
|
||||
use crate::kms::provider::get_sse_dek_provider;
|
||||
use base64_simd::STANDARD as BASE64_STANDARD;
|
||||
use rustfs_kms::KmsUnavailableError;
|
||||
use rustfs_policy::policy::action::KmsAction;
|
||||
use rustfs_s3_types::S3ErrorCode;
|
||||
use rustfs_utils::http::headers::{AMZ_ENCRYPTION_AES, AMZ_ENCRYPTION_KMS};
|
||||
use rustfs_utils::http::object_encryption_keys::SSEType;
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// Request parameters for unified encryption
|
||||
#[derive(Clone)]
|
||||
pub struct EncryptionRequest<'a> {
|
||||
/// Bucket name
|
||||
pub bucket: &'a str,
|
||||
/// Object key
|
||||
pub key: &'a str,
|
||||
/// Server-side encryption algorithm (SSE-S3 or SSE-KMS)
|
||||
pub server_side_encryption: Option<String>,
|
||||
/// KMS key ID (for SSE-KMS)
|
||||
pub ssekms_key_id: Option<String>,
|
||||
/// Optional client-provided KMS context for SSE-KMS.
|
||||
pub ssekms_context: Option<HashMap<String, String>>,
|
||||
/// SSE-C algorithm (customer-provided key)
|
||||
pub sse_customer_algorithm: Option<String>,
|
||||
/// SSE-C key (Base64-encoded)
|
||||
pub sse_customer_key: Option<String>,
|
||||
/// SSE-C key MD5 (Base64-encoded)
|
||||
pub sse_customer_key_md5: Option<String>,
|
||||
/// Content size (for metadata)
|
||||
pub content_size: i64,
|
||||
/// Caller the SSE-KMS key usage is authorized as. `None` marks an internal caller.
|
||||
pub principal: Option<&'a SseKmsPrincipal>,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for EncryptionRequest<'_> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("EncryptionRequest")
|
||||
.field("server_side_encryption", &self.server_side_encryption)
|
||||
.field("content_size", &self.content_size)
|
||||
.finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
impl EncryptionRequest<'_> {
|
||||
pub fn validate_complete_multipart_ssec(&self, user_defined: &HashMap<String, String>) -> Result<(), SseError> {
|
||||
let request_uses_ssec =
|
||||
self.sse_customer_algorithm.is_some() || self.sse_customer_key.is_some() || self.sse_customer_key_md5.is_some();
|
||||
if !request_uses_ssec {
|
||||
let stored_algorithm = user_defined.get("x-amz-server-side-encryption-customer-algorithm");
|
||||
let stored_key_md5 = user_defined.get("x-amz-server-side-encryption-customer-key-md5");
|
||||
return match (stored_algorithm, stored_key_md5) {
|
||||
(None, None) => Ok(()),
|
||||
(Some(algorithm), Some(key_md5))
|
||||
if algorithm == DEFAULT_SSE_ALGORITHM
|
||||
&& BASE64_STANDARD
|
||||
.decode_to_vec(key_md5)
|
||||
.is_ok_and(|decoded| decoded.len() == 16) =>
|
||||
{
|
||||
if rustfs_utils::http::contains_key_str(user_defined, rustfs_utils::http::SUFFIX_PLAINTEXT_CHECKSUM) {
|
||||
return Err(ssec_invalid_request(
|
||||
"SSE-C multipart uploads with additional checksums require the customer key for completion.",
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
_ => Err(ssec_invalid_request("The multipart upload contains invalid SSE-C metadata.")),
|
||||
};
|
||||
}
|
||||
self.validate_multipart_ssec(user_defined)
|
||||
}
|
||||
|
||||
pub fn validate_multipart_ssec(&self, user_defined: &HashMap<String, String>) -> Result<(), SseError> {
|
||||
let stored_algorithm = user_defined.get("x-amz-server-side-encryption-customer-algorithm");
|
||||
let stored_key_md5 = user_defined.get("x-amz-server-side-encryption-customer-key-md5");
|
||||
let session_uses_ssec = stored_algorithm.is_some() || stored_key_md5.is_some();
|
||||
let request_uses_ssec =
|
||||
self.sse_customer_algorithm.is_some() || self.sse_customer_key.is_some() || self.sse_customer_key_md5.is_some();
|
||||
|
||||
if !session_uses_ssec {
|
||||
return if request_uses_ssec {
|
||||
Err(ssec_invalid_request(
|
||||
"SSE-C parameters cannot be used for a multipart upload that was not initiated with SSE-C.",
|
||||
))
|
||||
} else {
|
||||
Ok(())
|
||||
};
|
||||
}
|
||||
|
||||
let (Some(algorithm), Some(key), Some(key_md5)) = (
|
||||
self.sse_customer_algorithm.as_ref(),
|
||||
self.sse_customer_key.as_ref(),
|
||||
self.sse_customer_key_md5.as_ref(),
|
||||
) else {
|
||||
return Err(ssec_invalid_request(
|
||||
"Missing SSE-C parameters. Algorithm, customer key and customer key MD5 are all required.",
|
||||
));
|
||||
};
|
||||
|
||||
let validated = validate_ssec_params(SsecParams {
|
||||
algorithm: algorithm.to_string(),
|
||||
key: key.to_string(),
|
||||
key_md5: key_md5.to_string(),
|
||||
})?;
|
||||
if stored_algorithm.map(String::as_str) != Some(validated.algorithm.as_str()) {
|
||||
return Err(ssec_invalid_request(
|
||||
"The provided encryption parameters did not match the multipart upload.",
|
||||
));
|
||||
}
|
||||
// Multipart session metadata is written by our own CreateMultipartUpload,
|
||||
// which always records the MD5 — the MinIO no-stored-MD5 shape cannot occur.
|
||||
verify_ssec_key_match(&validated.key_md5, stored_key_md5, false)
|
||||
}
|
||||
}
|
||||
|
||||
/// **Core API**: Apply encryption based on request parameters
|
||||
///
|
||||
/// This function automatically routes to the appropriate encryption method:
|
||||
/// - SSE-C if customer key is provided
|
||||
/// - SSE-S3/SSE-KMS if server-side encryption is requested
|
||||
/// - None if no encryption is requested
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `request` - Encryption request with all possible encryption parameters
|
||||
///
|
||||
/// # Returns
|
||||
/// * `Ok(Some(material))` - Encryption should be applied with the returned material
|
||||
/// * `Ok(None)` - No encryption requested
|
||||
/// * `Err` - Encryption configuration error
|
||||
///
|
||||
/// # Example
|
||||
/// ```rust,ignore
|
||||
/// let request = EncryptionRequest {
|
||||
/// bucket: &bucket,
|
||||
/// key: &key,
|
||||
/// server_side_encryption: effective_sse.as_ref(),
|
||||
/// ssekms_key_id: effective_kms_key_id.as_deref(),
|
||||
/// sse_customer_algorithm: sse_customer_algorithm.as_ref(),
|
||||
/// sse_customer_key: sse_customer_key.as_deref(),
|
||||
/// sse_customer_key_md5: sse_customer_key_md5.as_deref(),
|
||||
/// content_size: actual_size,
|
||||
/// part_number: None,
|
||||
/// };
|
||||
///
|
||||
/// if let Some(material) = sse_encryption(host, request).await? {
|
||||
/// reader = material.wrap_reader(reader);
|
||||
/// metadata.extend(material.metadata);
|
||||
/// }
|
||||
/// ```
|
||||
pub async fn sse_encryption(host: &dyn SseHost, request: EncryptionRequest<'_>) -> Result<Option<EncryptionMaterial>, SseError> {
|
||||
let scheme = resolve_write_scheme(
|
||||
host,
|
||||
request.bucket,
|
||||
WriteSchemeHeaders {
|
||||
server_side_encryption: request.server_side_encryption,
|
||||
ssekms_key_id: request.ssekms_key_id,
|
||||
ssekms_context: request.ssekms_context.as_ref(),
|
||||
sse_customer_algorithm: request.sse_customer_algorithm,
|
||||
sse_customer_key: request.sse_customer_key,
|
||||
sse_customer_key_md5: request.sse_customer_key_md5,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
let (server_side_encryption, kms_key_id) = match scheme {
|
||||
None => return Ok(None),
|
||||
Some(WriteScheme::SseC { algorithm, key, key_md5 }) => {
|
||||
return apply_ssec_encryption_material(request.bucket, request.key, algorithm, key, key_md5, request.content_size)
|
||||
.await
|
||||
.map(Some);
|
||||
}
|
||||
Some(WriteScheme::SseS3) => (String::from(AMZ_ENCRYPTION_AES), None),
|
||||
Some(WriteScheme::SseKms { key_id }) => (String::from(AMZ_ENCRYPTION_KMS), key_id),
|
||||
};
|
||||
|
||||
apply_managed_encryption_material(
|
||||
host,
|
||||
ManagedEncryptionRequest {
|
||||
bucket: request.bucket,
|
||||
key: request.key,
|
||||
server_side_encryption,
|
||||
kms_key_id,
|
||||
ssekms_context: request.ssekms_context,
|
||||
content_size: request.content_size,
|
||||
principal: request.principal,
|
||||
},
|
||||
)
|
||||
.await
|
||||
.map(Some)
|
||||
}
|
||||
|
||||
/// **Core API**: Apply encryption based on request parameters
|
||||
///
|
||||
/// sse_prepare_encryption, support SSE-C, SSE-S3, SSE-KMS
|
||||
pub struct PrepareEncryptionRequest<'a> {
|
||||
/// Bucket name
|
||||
pub bucket: &'a str,
|
||||
/// Object key
|
||||
pub key: &'a str,
|
||||
/// Server-side encryption algorithm (SSE-S3 or SSE-KMS)
|
||||
pub server_side_encryption: Option<String>,
|
||||
/// KMS key ID (for SSE-KMS)
|
||||
pub ssekms_key_id: Option<String>,
|
||||
/// Optional client-provided KMS context for SSE-KMS.
|
||||
pub ssekms_context: Option<HashMap<String, String>>,
|
||||
/// SSE-C algorithm (customer-provided key)
|
||||
pub sse_customer_algorithm: Option<String>,
|
||||
/// SSE-C key (Base64-encoded)
|
||||
pub sse_customer_key: Option<String>,
|
||||
/// SSE-C key MD5 (Base64-encoded)
|
||||
pub sse_customer_key_md5: Option<String>,
|
||||
/// Caller the SSE-KMS key usage is authorized as. `None` marks an internal caller.
|
||||
///
|
||||
/// This is the only point at which a multipart upload evaluates KMS key permissions:
|
||||
/// it is where the session data key is generated. `UploadPart`, `UploadPartCopy` and
|
||||
/// `CompleteMultipartUpload` reuse that envelope and are not re-authorized.
|
||||
pub principal: Option<&'a SseKmsPrincipal>,
|
||||
}
|
||||
|
||||
pub async fn sse_prepare_encryption(
|
||||
host: &dyn SseHost,
|
||||
request: PrepareEncryptionRequest<'_>,
|
||||
) -> Result<Option<EncryptionMaterial>, SseError> {
|
||||
let scheme = resolve_write_scheme(
|
||||
host,
|
||||
request.bucket,
|
||||
WriteSchemeHeaders {
|
||||
server_side_encryption: request.server_side_encryption,
|
||||
ssekms_key_id: request.ssekms_key_id,
|
||||
ssekms_context: request.ssekms_context.as_ref(),
|
||||
sse_customer_algorithm: request.sse_customer_algorithm,
|
||||
sse_customer_key: request.sse_customer_key,
|
||||
sse_customer_key_md5: request.sse_customer_key_md5,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
let (server_side_encryption, kms_key_id) = match scheme {
|
||||
None => return Ok(None),
|
||||
Some(WriteScheme::SseC { algorithm, key, key_md5 }) => {
|
||||
return apply_ssec_prepare_encryption_material(request.bucket, request.key, algorithm, Some(key), key_md5)
|
||||
.await
|
||||
.map(Some);
|
||||
}
|
||||
Some(WriteScheme::SseS3) => (String::from(AMZ_ENCRYPTION_AES), None),
|
||||
Some(WriteScheme::SseKms { key_id }) => (String::from(AMZ_ENCRYPTION_KMS), key_id),
|
||||
};
|
||||
|
||||
// A session records no plaintext size: the parts decide it.
|
||||
apply_managed_encryption_material(
|
||||
host,
|
||||
ManagedEncryptionRequest {
|
||||
bucket: request.bucket,
|
||||
key: request.key,
|
||||
server_side_encryption,
|
||||
kms_key_id,
|
||||
ssekms_context: request.ssekms_context,
|
||||
content_size: 0,
|
||||
principal: request.principal,
|
||||
},
|
||||
)
|
||||
.await
|
||||
.map(Some)
|
||||
}
|
||||
|
||||
async fn apply_ssec_prepare_encryption_material(
|
||||
bucket: &str,
|
||||
key: &str,
|
||||
algorithm: String,
|
||||
sse_key: Option<String>,
|
||||
sse_key_md5: String,
|
||||
) -> Result<EncryptionMaterial, SseError> {
|
||||
// The customer key is validated here only in a mode that seals the session's
|
||||
// key under it. Otherwise it is validated per part, against what the session
|
||||
// records.
|
||||
let customer_key = match sse_key {
|
||||
Some(sse_key) if SESSION_KEY_NEEDS_CUSTOMER_KEY => Some(
|
||||
validate_ssec_params(SsecParams {
|
||||
algorithm: algorithm.clone(),
|
||||
key: sse_key,
|
||||
key_md5: sse_key_md5.clone(),
|
||||
})?
|
||||
.key_bytes,
|
||||
),
|
||||
_ => None,
|
||||
};
|
||||
let object_key = new_session_key_under_customer_key(customer_key, bucket, key)?;
|
||||
|
||||
Ok(EncryptionMaterial {
|
||||
sse_type: SSEType::SseC,
|
||||
server_side_encryption: String::from(AMZ_ENCRYPTION_AES),
|
||||
kms_key_id: None,
|
||||
algorithm,
|
||||
key_bytes: object_key.key_bytes,
|
||||
base_nonce: object_key.base_nonce,
|
||||
encrypted_data_key: None,
|
||||
customer_key_md5: Some(sse_key_md5),
|
||||
original_size: None,
|
||||
key_kind: object_key.key_kind,
|
||||
managed_kms_context: None,
|
||||
managed_sealed_key: object_key.sealed_key,
|
||||
})
|
||||
}
|
||||
|
||||
async fn apply_ssec_encryption_material(
|
||||
bucket: &str,
|
||||
key: &str,
|
||||
algorithm: String,
|
||||
sse_key: String,
|
||||
sse_key_md5: String,
|
||||
content_size: i64,
|
||||
) -> Result<EncryptionMaterial, SseError> {
|
||||
let params = SsecParams {
|
||||
algorithm,
|
||||
key: sse_key,
|
||||
key_md5: sse_key_md5,
|
||||
};
|
||||
|
||||
let validated = validate_ssec_params(params)?;
|
||||
let object_key = new_key_under_customer_key(validated.key_bytes, bucket, key)?;
|
||||
|
||||
// Build metadata
|
||||
Ok(EncryptionMaterial {
|
||||
sse_type: SSEType::SseC,
|
||||
server_side_encryption: String::from(AMZ_ENCRYPTION_AES),
|
||||
kms_key_id: None,
|
||||
algorithm: validated.algorithm,
|
||||
key_bytes: object_key.key_bytes,
|
||||
base_nonce: object_key.base_nonce,
|
||||
encrypted_data_key: None,
|
||||
customer_key_md5: Some(validated.key_md5),
|
||||
original_size: Some(content_size),
|
||||
key_kind: object_key.key_kind,
|
||||
managed_kms_context: None,
|
||||
managed_sealed_key: object_key.sealed_key,
|
||||
})
|
||||
}
|
||||
|
||||
/// A managed-SSE (SSE-S3 / SSE-KMS) write, once its effective scheme is known.
|
||||
pub(crate) struct ManagedEncryptionRequest<'a> {
|
||||
pub(crate) bucket: &'a str,
|
||||
pub(crate) key: &'a str,
|
||||
pub(crate) server_side_encryption: String,
|
||||
pub(crate) kms_key_id: Option<String>,
|
||||
pub(crate) ssekms_context: Option<HashMap<String, String>>,
|
||||
pub(crate) content_size: i64,
|
||||
pub(crate) principal: Option<&'a SseKmsPrincipal>,
|
||||
}
|
||||
|
||||
pub(crate) async fn apply_managed_encryption_material(
|
||||
host: &dyn SseHost,
|
||||
request: ManagedEncryptionRequest<'_>,
|
||||
) -> Result<EncryptionMaterial, SseError> {
|
||||
// Request validation, not a KMS operation: reject before the KMS is called
|
||||
// and before an audit outcome is recorded.
|
||||
reject_kms_context_location_override(request.bucket, request.ssekms_context.as_ref())?;
|
||||
|
||||
let principal = request.principal;
|
||||
let requested_sse_type = managed_sse_type(request.server_side_encryption.as_str());
|
||||
let requested_key_id = request.kms_key_id.clone();
|
||||
let result = apply_managed_encryption_material_inner(host, request).await;
|
||||
|
||||
match &result {
|
||||
// The resolved key is only known on success: it may come from the request,
|
||||
// the bucket default or the KMS service default. On failure the audit entry
|
||||
// records what the caller asked for, which is what a reader needs to see.
|
||||
Ok(material) => record_managed_kms_outcome(
|
||||
principal,
|
||||
material.sse_type,
|
||||
material.kms_key_id.as_deref(),
|
||||
|| material.encrypted_data_key.as_deref().and_then(envelope_master_key_version),
|
||||
Ok(()),
|
||||
),
|
||||
Err(error) => record_managed_kms_outcome(principal, requested_sse_type, requested_key_id.as_deref(), || None, Err(error)),
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
pub(crate) async fn apply_managed_encryption_material_inner(
|
||||
host: &dyn SseHost,
|
||||
request: ManagedEncryptionRequest<'_>,
|
||||
) -> Result<EncryptionMaterial, SseError> {
|
||||
let ManagedEncryptionRequest {
|
||||
bucket,
|
||||
key,
|
||||
server_side_encryption,
|
||||
kms_key_id,
|
||||
ssekms_context,
|
||||
content_size,
|
||||
principal,
|
||||
} = request;
|
||||
if !is_managed_sse(&server_side_encryption) {
|
||||
return Err(SseError::internal(format!(
|
||||
"Unsupported server-side encryption: {}",
|
||||
server_side_encryption.as_str()
|
||||
)));
|
||||
}
|
||||
|
||||
let encryption_type = match server_side_encryption.as_str() {
|
||||
"AES256" => SSEType::SseS3,
|
||||
"aws:kms" => SSEType::SseKms,
|
||||
_ => SSEType::SseS3,
|
||||
};
|
||||
|
||||
// Determine KMS key ID to use for internal key wrapping.
|
||||
let mut kms_key_candidate = kms_key_id.clone();
|
||||
if kms_key_candidate.is_none() {
|
||||
// Try to get default key from KMS service (if available)
|
||||
if let Some(service) = host.encryption_service().await {
|
||||
kms_key_candidate = service.get_default_key_id().cloned();
|
||||
tracing::debug!(
|
||||
default_key_id = ?kms_key_candidate,
|
||||
"SSE-S3: KMS service available, default_key_id from config"
|
||||
);
|
||||
} else {
|
||||
tracing::debug!("SSE-S3: KMS encryption service not available");
|
||||
}
|
||||
}
|
||||
|
||||
let kms_key_to_use = match (encryption_type, kms_key_candidate.clone()) {
|
||||
(SSEType::SseS3, Some(kms_key_id)) => kms_key_id,
|
||||
(SSEType::SseS3, None) => {
|
||||
tracing::debug!("SSE-S3: no KMS key configured, falling back to \"default\" key ID");
|
||||
"default".to_string()
|
||||
}
|
||||
(SSEType::SseKms, Some(kms_key_id)) => kms_key_id,
|
||||
(SSEType::SseKms, None) => {
|
||||
// Neither the request nor the bucket default named a key and no
|
||||
// service default filled in. Without a service this is the same
|
||||
// outage/misconfiguration the provider check below reports, so it
|
||||
// must carry the same 503/400 split rather than an untyped
|
||||
// internal error; with a running service that has no default key
|
||||
// the caller simply has to name one.
|
||||
if host.encryption_service().await.is_none() {
|
||||
return Err(sse_kms_unavailable_error(kms_configured_but_unavailable(host).await));
|
||||
}
|
||||
return Err(SseError {
|
||||
code: S3ErrorCode::InvalidRequest,
|
||||
message: "SSE-KMS requires a KMS key id: the request named none and the KMS service has no default key"
|
||||
.to_string(),
|
||||
source: None,
|
||||
});
|
||||
}
|
||||
_ => unreachable!("managed SSE branch only supports SSE-S3 or SSE-KMS"),
|
||||
};
|
||||
|
||||
// The key is fully resolved here (request header, then bucket default, then the KMS
|
||||
// service default), so this is the first point at which the caller can be held to the
|
||||
// key it will actually be encrypted under.
|
||||
authorize_sse_kms_key(host, principal, encryption_type, KmsAction::GenerateDataKeyAction, &kms_key_to_use).await?;
|
||||
|
||||
// A node-local master key is the explicit SSE-S3 fallback. Letting it serve
|
||||
// an SSE-KMS request would persist an `aws:kms` marker and a KMS key id
|
||||
// that never wrapped the data key, so refuse rather than downgrade.
|
||||
//
|
||||
// The refusal sits after the authorization gate so an unauthorized caller
|
||||
// still sees AccessDenied whatever the KMS runtime state is, and it asks
|
||||
// the resolved provider rather than a parallel availability signal,
|
||||
// because the provider is what actually wraps the DEK.
|
||||
let provider = match get_sse_dek_provider(host).await {
|
||||
Ok(provider) => {
|
||||
if matches!(encryption_type, SSEType::SseKms) && provider.wraps_dek_with_local_master_key() {
|
||||
return Err(sse_kms_unavailable_error(kms_configured_but_unavailable(host).await));
|
||||
}
|
||||
provider
|
||||
}
|
||||
// With no master key set the local fallback fails with an SSE-S3-worded
|
||||
// configuration error. An SSE-KMS request never asked for that provider,
|
||||
// so it gets the SSE-KMS refusal instead of a message naming the wrong
|
||||
// scheme.
|
||||
Err(err) => {
|
||||
if matches!(encryption_type, SSEType::SseKms) && host.encryption_service().await.is_none() {
|
||||
return Err(sse_kms_unavailable_error(kms_configured_but_unavailable(host).await));
|
||||
}
|
||||
return Err(err);
|
||||
}
|
||||
};
|
||||
let object_context = build_object_encryption_context(bucket, key, ssekms_context.as_ref());
|
||||
let (data_key, encrypted_data_key) = provider.generate_sse_dek(&object_context, &kms_key_to_use).await?;
|
||||
|
||||
let algorithm = server_side_encryption.as_str().to_string();
|
||||
let object_key = new_key_under_data_key(&data_key, encryption_type, bucket, key)?;
|
||||
|
||||
Ok(EncryptionMaterial {
|
||||
sse_type: encryption_type,
|
||||
server_side_encryption,
|
||||
kms_key_id: Some(kms_key_to_use),
|
||||
algorithm,
|
||||
key_bytes: object_key.key_bytes,
|
||||
base_nonce: object_key.base_nonce,
|
||||
encrypted_data_key: Some(encrypted_data_key),
|
||||
customer_key_md5: None,
|
||||
original_size: Some(content_size),
|
||||
key_kind: object_key.key_kind,
|
||||
managed_kms_context: matches!(encryption_type, SSEType::SseKms).then_some(ssekms_context.unwrap_or_default()),
|
||||
managed_sealed_key: object_key.sealed_key,
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether this node has a KMS configured that is not currently serving, which
|
||||
/// separates a transient outage (retryable, 503) from a deployment that never
|
||||
/// configured KMS at all (a client-side configuration error, 400).
|
||||
async fn kms_configured_but_unavailable(host: &dyn SseHost) -> bool {
|
||||
match host.kms_service_manager() {
|
||||
Some(manager) => !matches!(manager.get_status().await, rustfs_kms::KmsServiceStatus::NotConfigured),
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn sse_kms_unavailable_error(configured_but_unavailable: bool) -> SseError {
|
||||
if configured_but_unavailable {
|
||||
return SseError::internal(KmsUnavailableError);
|
||||
}
|
||||
SseError {
|
||||
code: S3ErrorCode::InvalidRequest,
|
||||
message: "SSE-KMS requires a configured and running KMS service".to_string(),
|
||||
source: None,
|
||||
}
|
||||
}
|
||||
@@ -1,176 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! The error every SSE operation reports, already classified for the S3 data path.
|
||||
|
||||
use rustfs_kms::{KmsError, KmsUnavailableError};
|
||||
use rustfs_s3_types::S3ErrorCode;
|
||||
|
||||
type BoxError = Box<dyn std::error::Error + Send + Sync>;
|
||||
|
||||
/// S3 error code a KMS key that does not exist is reported under.
|
||||
pub const KMS_KEY_NOT_FOUND_ERROR_CODE: &str = "KMS.NotFoundException";
|
||||
|
||||
pub(crate) const INTERNAL_ERROR_MESSAGE: &str = "We encountered an internal error, please try again.";
|
||||
const SERVICE_UNAVAILABLE_MESSAGE: &str = "The service is unavailable. Please retry.";
|
||||
const ACCESS_DENIED_MESSAGE: &str = "Access Denied.";
|
||||
|
||||
/// A failed SSE operation, carrying the S3 error code and message the caller answers with.
|
||||
///
|
||||
/// `source` keeps the underlying failure for logs and for classification by
|
||||
/// callers; it is never part of the response.
|
||||
#[derive(Debug)]
|
||||
pub struct SseError {
|
||||
pub code: S3ErrorCode,
|
||||
pub message: String,
|
||||
pub source: Option<BoxError>,
|
||||
}
|
||||
|
||||
impl SseError {
|
||||
/// Classify a failure that did not originate from the request itself.
|
||||
///
|
||||
/// A [`KmsUnavailableError`] is a retryable `ServiceUnavailable`, a
|
||||
/// [`KmsError`] follows [`data_plane_kms_error`], and everything else is an
|
||||
/// `InternalError` whose detail stays in `source` and out of the response.
|
||||
pub fn internal(error: impl Into<BoxError>) -> Self {
|
||||
let source = error.into();
|
||||
let (code, message) = if source.downcast_ref::<KmsUnavailableError>().is_some() {
|
||||
(S3ErrorCode::ServiceUnavailable, SERVICE_UNAVAILABLE_MESSAGE.to_string())
|
||||
} else if let Some(classified) = source.downcast_ref::<KmsError>().and_then(data_plane_kms_error) {
|
||||
classified
|
||||
} else {
|
||||
(S3ErrorCode::InternalError, INTERNAL_ERROR_MESSAGE.to_string())
|
||||
};
|
||||
Self {
|
||||
code,
|
||||
message,
|
||||
source: Some(source),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for SseError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str(&self.message)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for SseError {
|
||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||
self.source.as_deref().map(|source| source as _)
|
||||
}
|
||||
}
|
||||
|
||||
/// Map a KMS failure that surfaced on the S3 data path to its S3 error code.
|
||||
///
|
||||
/// The contract deliberately differs from the admin key-lifecycle handlers:
|
||||
/// there a key id is the resource being addressed, so a missing key is `404`.
|
||||
/// Here the key id arrives inside a request header or a bucket default, so a
|
||||
/// key that is missing, disabled, or otherwise unusable is configuration the
|
||||
/// caller has to correct — AWS answers `400`, and reporting `500` instead both
|
||||
/// misfiles the failure as a server fault and makes SDKs back off and retry a
|
||||
/// request that cannot succeed.
|
||||
///
|
||||
/// `None` keeps the caller's fallthrough, which is the `500` that integrity
|
||||
/// faults — damaged, unreadable, or unknown-format key material — must keep.
|
||||
///
|
||||
/// Messages either name what the caller asked for or stay generic; detail that
|
||||
/// belongs to the deployment rather than the request stays in `source`, which
|
||||
/// the caller attaches and the logs retain.
|
||||
pub fn data_plane_kms_error(error: &KmsError) -> Option<(S3ErrorCode, String)> {
|
||||
use KmsError as Kms;
|
||||
|
||||
match error {
|
||||
Kms::KeyNotFound { key_id } => Some((
|
||||
S3ErrorCode::Custom(KMS_KEY_NOT_FOUND_ERROR_CODE.into()),
|
||||
format!("KMS key not found: {key_id}"),
|
||||
)),
|
||||
Kms::KeyVersionNotFound { key_id, version } => Some((
|
||||
S3ErrorCode::Custom(KMS_KEY_NOT_FOUND_ERROR_CODE.into()),
|
||||
format!("KMS key version {version} not found for key {key_id}"),
|
||||
)),
|
||||
// The key exists but its state forbids the operation (disabled,
|
||||
// pending deletion). AWS treats an invalid key state as a request
|
||||
// error, not a server fault.
|
||||
Kms::InvalidOperation { .. } => Some((S3ErrorCode::InvalidRequest, error.to_string())),
|
||||
// A policy decision needs a human, so it must be distinguishable from
|
||||
// the transient classes an SDK retries.
|
||||
Kms::AccessDenied { .. } => Some((S3ErrorCode::AccessDenied, ACCESS_DENIED_MESSAGE.to_string())),
|
||||
// Request-side faults: what was asked for cannot be served as asked.
|
||||
Kms::ContextMismatch { .. }
|
||||
| Kms::InvalidKey { .. }
|
||||
| Kms::ValidationError { .. }
|
||||
| Kms::UnsupportedAlgorithm { .. }
|
||||
| Kms::InvalidKeySize { .. } => Some((S3ErrorCode::InvalidRequest, error.to_string())),
|
||||
// A deployment whose KMS configuration cannot serve the request (for
|
||||
// example no key named and no default). Actionable, but the detail
|
||||
// describes the deployment, so it stays out of the response body.
|
||||
Kms::ConfigurationError { .. } => {
|
||||
Some((S3ErrorCode::InvalidRequest, "The KMS configuration cannot serve this request".to_string()))
|
||||
}
|
||||
// Transient: worth retrying, and must be counted against availability
|
||||
// rather than against the caller. `IoError` belongs here because it is
|
||||
// how a backend reports that its key store itself was unreachable —
|
||||
// rustfs/rustfs#7470 separated that from a missing key precisely so the
|
||||
// two stop looking alike, and leaving it on the 500 fallthrough would
|
||||
// erase that distinction again at the S3 boundary.
|
||||
Kms::BackendError { .. }
|
||||
| Kms::IoError { .. }
|
||||
| Kms::OperationTimedOut { .. }
|
||||
| Kms::OperationCancelled { .. }
|
||||
| Kms::CredentialsUnavailable { .. }
|
||||
| Kms::CacheError { .. } => Some((S3ErrorCode::ServiceUnavailable, SERVICE_UNAVAILABLE_MESSAGE.to_string())),
|
||||
// A permanent gap in the configured backend, never a missing resource.
|
||||
Kms::UnsupportedCapability { .. } => Some((S3ErrorCode::NotImplemented, error.to_string())),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn internal_keeps_request_free_failures_generic() {
|
||||
let error = SseError::internal("Failed to decode IV: bad base64");
|
||||
|
||||
assert_eq!(error.code, S3ErrorCode::InternalError);
|
||||
assert_eq!(error.message, INTERNAL_ERROR_MESSAGE);
|
||||
assert!(
|
||||
error
|
||||
.source
|
||||
.as_ref()
|
||||
.is_some_and(|source| source.to_string().contains("Failed to decode IV"))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn internal_reports_an_unavailable_kms_as_retryable() {
|
||||
let error = SseError::internal(KmsUnavailableError);
|
||||
|
||||
assert_eq!(error.code, S3ErrorCode::ServiceUnavailable);
|
||||
assert_eq!(error.message, SERVICE_UNAVAILABLE_MESSAGE);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn internal_classifies_kms_errors_for_the_data_path() {
|
||||
let missing = SseError::internal(KmsError::key_not_found("absent-key"));
|
||||
assert_eq!(missing.code, S3ErrorCode::Custom(KMS_KEY_NOT_FOUND_ERROR_CODE.into()));
|
||||
assert_eq!(missing.message, "KMS key not found: absent-key");
|
||||
|
||||
let damaged = SseError::internal(KmsError::cryptographic_error("decrypt", "authentication failed"));
|
||||
assert_eq!(damaged.code, S3ErrorCode::InternalError);
|
||||
assert_eq!(damaged.message, INTERNAL_ERROR_MESSAGE);
|
||||
}
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! What SSE needs from the server it runs in.
|
||||
//!
|
||||
//! This crate reaches the running server only through [`SseHost`]: it holds no
|
||||
//! handle to the storage engine, to IAM, or to the bucket metadata system. Every
|
||||
//! entry point that consults the server takes the host as its first argument, so
|
||||
//! a caller cannot reach key selection without saying which server it speaks for.
|
||||
|
||||
use crate::{SseError, SseKmsPrincipal};
|
||||
use async_trait::async_trait;
|
||||
use rustfs_kms::KmsServiceManager;
|
||||
use rustfs_kms::service::ObjectEncryptionService;
|
||||
use rustfs_policy::policy::action::KmsAction;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// The server capabilities SSE key resolution depends on.
|
||||
#[async_trait]
|
||||
pub trait SseHost: Send + Sync {
|
||||
/// The KMS object-encryption service currently serving, if one is.
|
||||
///
|
||||
/// Resolved on every call: the KMS backend can be reconfigured while the
|
||||
/// server runs, so a service handle must not outlive the operation it serves.
|
||||
async fn encryption_service(&self) -> Option<Arc<ObjectEncryptionService>>;
|
||||
|
||||
/// The KMS service manager, when the server has one.
|
||||
///
|
||||
/// Only its status is read, to tell a configured KMS that is not serving
|
||||
/// (retryable) from a deployment that never configured one (a request error).
|
||||
fn kms_service_manager(&self) -> Option<Arc<KmsServiceManager>>;
|
||||
|
||||
/// The bucket's default-encryption configuration.
|
||||
///
|
||||
/// `Ok(None)` means the bucket has no encryption configuration.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Any failure to read the configuration, already classified for the S3
|
||||
/// response. A write must not proceed as if the bucket had no default when
|
||||
/// the configuration could not be read.
|
||||
async fn bucket_encryption(&self, bucket: &str) -> Result<Option<BucketEncryption>, SseError>;
|
||||
|
||||
/// The authorizer deciding per-key KMS permissions on the data path.
|
||||
fn kms_key_authorizer(&self) -> &dyn KmsKeyAuthorizer;
|
||||
}
|
||||
|
||||
/// A bucket's stored encryption configuration, reduced to what a write consults.
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct BucketEncryption {
|
||||
/// The default-encryption rule of the first configuration rule, if it has one.
|
||||
pub default_rule: Option<BucketDefaultEncryption>,
|
||||
}
|
||||
|
||||
/// The `ApplyServerSideEncryptionByDefault` element of a bucket encryption rule.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct BucketDefaultEncryption {
|
||||
/// The stored algorithm, exactly as persisted (`AES256`, `aws:kms`, or an unknown value).
|
||||
pub sse_algorithm: String,
|
||||
pub kms_master_key_id: Option<String>,
|
||||
}
|
||||
|
||||
/// Decides whether a principal may use a specific KMS key on the data path.
|
||||
///
|
||||
/// Behind a trait so tests can pin the allow/deny contract of the SSE call sites
|
||||
/// without a running IAM subsystem.
|
||||
#[async_trait]
|
||||
pub trait KmsKeyAuthorizer: Send + Sync {
|
||||
/// # Errors
|
||||
///
|
||||
/// When the decision itself could not be made, for example because the
|
||||
/// policy subsystem is not ready. A denial is `Ok(false)`.
|
||||
async fn is_allowed(&self, principal: &SseKmsPrincipal, action: KmsAction, key_id: &str) -> Result<bool, SseError>;
|
||||
}
|
||||
@@ -1,180 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! The provider that wraps and unwraps data keys through the KMS service.
|
||||
|
||||
use crate::core::audit::KmsDataPlaneFailure;
|
||||
use crate::error::SseError;
|
||||
use crate::host::SseHost;
|
||||
use crate::kms::provider::SseDekProvider;
|
||||
use async_trait::async_trait;
|
||||
use rustfs_kms::types::ObjectEncryptionContext;
|
||||
use rustfs_kms::{DataKey, KmsUnavailableError};
|
||||
use rustfs_s3_types::S3ErrorCode;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Production KMS-backed DEK provider
|
||||
/// Resolves the latest ObjectEncryptionService on each call.
|
||||
pub(crate) struct KmsSseDekProvider<'a> {
|
||||
service_source: KmsServiceSource<'a>,
|
||||
}
|
||||
|
||||
/// Where a [`KmsSseDekProvider`] finds the service that is serving right now.
|
||||
enum KmsServiceSource<'a> {
|
||||
Host(&'a dyn SseHost),
|
||||
#[cfg(test)]
|
||||
Manager(Arc<rustfs_kms::KmsServiceManager>),
|
||||
}
|
||||
|
||||
pub(crate) fn kms_operation_error(error: rustfs_kms::KmsError) -> SseError {
|
||||
let class = rustfs_kms::audit::error_class(&error);
|
||||
let mut api_error = SseError::internal(error);
|
||||
// Wrap rather than replace the source so the original chain stays intact for
|
||||
// logging; the wrapper only adds the class the audit attachment needs.
|
||||
let source = api_error.source.take();
|
||||
api_error.source = Some(Box::new(KmsDataPlaneFailure { class, source }));
|
||||
api_error
|
||||
}
|
||||
|
||||
/// Classification for a failed data-key unwrap.
|
||||
///
|
||||
/// An AEAD failure stays `500` (the envelope is an integrity fault, not a
|
||||
/// request a retry or a different header can fix), but the generic internal
|
||||
/// error text hides the one diagnosis an operator needs: the configured
|
||||
/// backend holds different key material under this key id than the one that
|
||||
/// wrapped the object, typically after re-creating a key of the same name or
|
||||
/// switching backends.
|
||||
pub(crate) fn kms_unwrap_error(error: rustfs_kms::KmsError) -> SseError {
|
||||
let unwrap_rejected = matches!(error, rustfs_kms::KmsError::CryptographicError { .. });
|
||||
let mut api_error = kms_operation_error(error);
|
||||
if unwrap_rejected && api_error.code == S3ErrorCode::InternalError {
|
||||
api_error.message = "The object's data key envelope could not be unwrapped by the configured KMS backend: the \
|
||||
key material under this key id differs from the one that wrapped it, or the envelope is damaged"
|
||||
.to_string();
|
||||
}
|
||||
api_error
|
||||
}
|
||||
|
||||
impl<'a> KmsSseDekProvider<'a> {
|
||||
/// Create a new KMS-backed provider
|
||||
pub async fn new(host: &'a dyn SseHost) -> Result<Self, SseError> {
|
||||
let provider = Self {
|
||||
service_source: KmsServiceSource::Host(host),
|
||||
};
|
||||
provider
|
||||
.current_service()
|
||||
.await
|
||||
.ok_or_else(|| SseError::internal(KmsUnavailableError))?;
|
||||
Ok(provider)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) async fn new_with_service_manager(service_manager: Arc<rustfs_kms::KmsServiceManager>) -> Result<Self, SseError> {
|
||||
let provider = Self {
|
||||
service_source: KmsServiceSource::Manager(service_manager),
|
||||
};
|
||||
provider
|
||||
.current_service()
|
||||
.await
|
||||
.ok_or_else(|| SseError::internal(KmsUnavailableError))?;
|
||||
Ok(provider)
|
||||
}
|
||||
|
||||
async fn current_service(&self) -> Option<Arc<rustfs_kms::service::ObjectEncryptionService>> {
|
||||
match &self.service_source {
|
||||
KmsServiceSource::Host(host) => host.encryption_service().await,
|
||||
#[cfg(test)]
|
||||
KmsServiceSource::Manager(service_manager) => service_manager.get_encryption_service().await,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl SseDekProvider for KmsSseDekProvider<'_> {
|
||||
async fn generate_sse_dek(
|
||||
&self,
|
||||
context: &ObjectEncryptionContext,
|
||||
kms_key_id: &str,
|
||||
) -> Result<(DataKey, Vec<u8>), SseError> {
|
||||
let kms_key_option = Some(kms_key_id.to_string());
|
||||
let service = self
|
||||
.current_service()
|
||||
.await
|
||||
.ok_or_else(|| SseError::internal(KmsUnavailableError))?;
|
||||
let (data_key, encrypted_data_key) = service
|
||||
.create_data_key(&kms_key_option, context)
|
||||
.await
|
||||
.map_err(kms_operation_error)?;
|
||||
|
||||
Ok((data_key, encrypted_data_key))
|
||||
}
|
||||
|
||||
async fn reads_bare_data_key_ciphertext(&self) -> bool {
|
||||
self.current_service()
|
||||
.await
|
||||
.is_some_and(|service| service.reads_bare_data_key_ciphertext())
|
||||
}
|
||||
|
||||
async fn rewrap_sse_dek(
|
||||
&self,
|
||||
encrypted_dek: &[u8],
|
||||
context: &ObjectEncryptionContext,
|
||||
) -> Result<rustfs_kms::types::RewrapDataKeyResponse, SseError> {
|
||||
let service = self
|
||||
.current_service()
|
||||
.await
|
||||
.ok_or_else(|| SseError::internal(KmsUnavailableError))?;
|
||||
service
|
||||
.rewrap_data_key(encrypted_dek, context)
|
||||
.await
|
||||
.map_err(kms_operation_error)
|
||||
}
|
||||
|
||||
async fn decrypt_sse_dek(
|
||||
&self,
|
||||
encrypted_dek: &[u8],
|
||||
_kms_key_id: &str,
|
||||
context: &ObjectEncryptionContext,
|
||||
) -> Result<[u8; 32], SseError> {
|
||||
let service = self
|
||||
.current_service()
|
||||
.await
|
||||
.ok_or_else(|| SseError::internal(KmsUnavailableError))?;
|
||||
let data_key = service
|
||||
.decrypt_data_key(encrypted_dek, context)
|
||||
.await
|
||||
.map_err(kms_unwrap_error)?;
|
||||
|
||||
Ok(data_key.plaintext_key)
|
||||
}
|
||||
|
||||
#[cfg(feature = "rio-v2")]
|
||||
async fn decrypt_legacy_sse_dek(
|
||||
&self,
|
||||
encrypted_dek: &[u8],
|
||||
_kms_key_id: &str,
|
||||
_context: &ObjectEncryptionContext,
|
||||
) -> Result<[u8; 32], SseError> {
|
||||
let service = self
|
||||
.current_service()
|
||||
.await
|
||||
.ok_or_else(|| SseError::internal(KmsUnavailableError))?;
|
||||
let data_key = service
|
||||
.decrypt_legacy_data_key(encrypted_dek)
|
||||
.await
|
||||
.map_err(kms_unwrap_error)?;
|
||||
|
||||
Ok(data_key.plaintext_key)
|
||||
}
|
||||
}
|
||||
@@ -1,323 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! The provider that wraps data keys with a node-local master key when no KMS is configured.
|
||||
|
||||
use crate::error::SseError;
|
||||
#[cfg(feature = "rio-v2")]
|
||||
use crate::kms::minio::{decrypt_minio_kms_data_key, minio_kms_associated_data};
|
||||
use crate::kms::provider::SseDekProvider;
|
||||
use aes_gcm::aead::{Aead, KeyInit};
|
||||
use aes_gcm::{Aes256Gcm, Key, Nonce};
|
||||
use async_trait::async_trait;
|
||||
use base64_simd::STANDARD as BASE64_STANDARD;
|
||||
use rand::Rng;
|
||||
use rustfs_kms::DataKey;
|
||||
use rustfs_kms::types::ObjectEncryptionContext;
|
||||
use rustfs_s3_types::S3ErrorCode;
|
||||
use rustfs_utils::get_env_opt_str;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Local SSE DEK provider for deployments without a KMS.
|
||||
///
|
||||
/// Uses `RUSTFS_SSE_S3_MASTER_KEY` (base64-encoded 32-byte key) to wrap
|
||||
/// data-encryption keys with AES-256-GCM. This is the production fallback
|
||||
/// when no KMS service is configured.
|
||||
///
|
||||
/// # Environment Variable
|
||||
///
|
||||
/// ```text
|
||||
/// RUSTFS_SSE_S3_MASTER_KEY=<base64_encoded_32_byte_key>
|
||||
/// ```
|
||||
pub struct LocalSseDekProvider {
|
||||
master_key: [u8; 32],
|
||||
}
|
||||
|
||||
pub(crate) const LOCAL_SSE_DEK_FORMAT_VERSION: u8 = 1;
|
||||
|
||||
/// Environment variable that carries the local SSE-S3 master key.
|
||||
pub(crate) const ENV_SSE_S3_MASTER_KEY: &str = "RUSTFS_SSE_S3_MASTER_KEY";
|
||||
|
||||
/// The configured local master key, still encoded; `None` when unset or blank.
|
||||
fn local_master_key_env_value() -> Option<String> {
|
||||
get_env_opt_str(ENV_SSE_S3_MASTER_KEY).filter(|value| !value.trim().is_empty())
|
||||
}
|
||||
|
||||
/// Whether the operator configured a local SSE-S3 master key at all.
|
||||
///
|
||||
/// Says nothing about whether the value is usable; loading it decides that.
|
||||
pub(crate) fn local_master_key_configured() -> bool {
|
||||
local_master_key_env_value().is_some()
|
||||
}
|
||||
|
||||
#[cfg(feature = "rio-v2")]
|
||||
/// Returns true when a managed-SSE data key is one RustFS itself wrote.
|
||||
///
|
||||
/// Both RustFS envelope shapes are strict JSON — the KMS envelope
|
||||
/// ([`rustfs_kms::is_data_key_envelope`]) and the local provider's
|
||||
/// [`LocalSseDekEnvelope`], whose `deny_unknown_fields` keeps it from accepting
|
||||
/// anything else. Recognition is deliberately positive: an unrecognized payload
|
||||
/// is left to MinIO's decoder rather than guessed at, and neither decoder is
|
||||
/// ever handed the other's format.
|
||||
pub(crate) fn is_rustfs_managed_data_key(encrypted_dek: &[u8]) -> bool {
|
||||
if rustfs_kms::is_data_key_envelope(encrypted_dek) {
|
||||
return true;
|
||||
}
|
||||
std::str::from_utf8(encrypted_dek)
|
||||
.ok()
|
||||
.is_some_and(|text| serde_json::from_str::<LocalSseDekEnvelope<'_>>(text).is_ok())
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub(crate) struct LocalSseDekEnvelope<'a> {
|
||||
pub(crate) version: u8,
|
||||
pub(crate) nonce: &'a str,
|
||||
ciphertext: &'a str,
|
||||
}
|
||||
|
||||
/// Whether [`LocalSseDekProvider`] could have written these bytes.
|
||||
///
|
||||
/// Covers both shapes [`LocalSseDekProvider::decrypt_dek`] accepts — anything
|
||||
/// presented as a JSON object, so a damaged local envelope keeps its own
|
||||
/// diagnosis, and the deprecated `nonce:ciphertext` base64 pair. Everything
|
||||
/// else is bytes the local provider can only reject.
|
||||
pub(crate) fn is_local_sse_dek(encrypted_dek: &[u8]) -> bool {
|
||||
let Ok(text) = std::str::from_utf8(encrypted_dek) else {
|
||||
return false;
|
||||
};
|
||||
if text.trim_start().starts_with('{') {
|
||||
return true;
|
||||
}
|
||||
let is_base64 = |part: &str| BASE64_STANDARD.decode_to_vec(part).is_ok();
|
||||
text.split_once(':')
|
||||
.is_some_and(|(nonce, ciphertext)| is_base64(nonce) && is_base64(ciphertext))
|
||||
}
|
||||
|
||||
/// Test-only alias so existing test code that references `TestSseDekProvider`
|
||||
/// continues to compile without changes.
|
||||
#[cfg(any(test, feature = "test-util"))]
|
||||
#[allow(non_camel_case_types)]
|
||||
pub type TestSseDekProvider = LocalSseDekProvider;
|
||||
|
||||
/// Parse the base64-encoded 32-byte master key from `__RUSTFS_SSE_SIMPLE_CMK`.
|
||||
///
|
||||
/// Returns an error (never crashes) for a missing, non-base64, wrong-length, or
|
||||
/// all-zero key so callers on the request path can fail the request instead of
|
||||
/// taking the whole server down (backlog#806).
|
||||
#[cfg(any(test, feature = "test-util"))]
|
||||
pub(crate) fn parse_simple_sse_cmk(cmk_value: &str) -> Result<[u8; 32], SseError> {
|
||||
let trimmed = cmk_value.trim();
|
||||
if trimmed.is_empty() {
|
||||
return Err(SseError::internal(
|
||||
"SSE simple mode requires __RUSTFS_SSE_SIMPLE_CMK to be set to a base64-encoded 32-byte key",
|
||||
));
|
||||
}
|
||||
let decoded = BASE64_STANDARD
|
||||
.decode_to_vec(trimmed)
|
||||
.map_err(|e| SseError::internal(format!("__RUSTFS_SSE_SIMPLE_CMK must be valid base64: {e}")))?;
|
||||
let master_key: [u8; 32] = decoded.try_into().map_err(|v: Vec<u8>| {
|
||||
SseError::internal(format!("__RUSTFS_SSE_SIMPLE_CMK must decode to exactly 32 bytes, got {} bytes", v.len()))
|
||||
})?;
|
||||
if master_key == [0u8; 32] {
|
||||
return Err(SseError::internal("__RUSTFS_SSE_SIMPLE_CMK must not be an all-zero key"));
|
||||
}
|
||||
Ok(master_key)
|
||||
}
|
||||
|
||||
impl LocalSseDekProvider {
|
||||
/// Create a LocalSseDekProvider with a predefined key (for testing)
|
||||
#[cfg(any(test, feature = "test-util"))]
|
||||
pub fn new_with_key(master_key: [u8; 32]) -> Self {
|
||||
Self { master_key }
|
||||
}
|
||||
|
||||
/// Create a TestSseDekProvider from `__RUSTFS_SSE_SIMPLE_CMK` (test-only).
|
||||
#[cfg(any(test, feature = "test-util"))]
|
||||
pub fn new() -> Result<Self, SseError> {
|
||||
let cmk_value = std::env::var("__RUSTFS_SSE_SIMPLE_CMK").unwrap_or_default();
|
||||
// A missing/invalid key must surface as a request error, never crash the
|
||||
// whole server: `LocalSseDekProvider::new` is reached from the SSE request
|
||||
// path (get_sse_dek_provider), so `process::exit(1)` here turned a bad
|
||||
// `__RUSTFS_SSE_SIMPLE_CMK` into a process crash-loop DoS (backlog#806).
|
||||
let master_key = parse_simple_sse_cmk(&cmk_value)?;
|
||||
tracing::info!("Successfully loaded SSE master key (32 bytes) from __RUSTFS_SSE_SIMPLE_CMK");
|
||||
Ok(Self { master_key })
|
||||
}
|
||||
|
||||
/// Create a local SSE DEK provider for SSE-S3 when KMS is not configured.
|
||||
/// Requires RUSTFS_SSE_S3_MASTER_KEY to be a valid base64-encoded 32-byte key.
|
||||
///
|
||||
/// The failures here are server configuration problems, not internal
|
||||
/// faults: surface them as `InvalidRequest` (HTTP 400) so a managed-SSE
|
||||
/// request against an unconfigured server does not report 500 (rustfs#4844).
|
||||
pub fn new_from_env() -> Result<Self, SseError> {
|
||||
fn sse_not_configured(message: impl Into<String>) -> SseError {
|
||||
SseError {
|
||||
code: S3ErrorCode::InvalidRequest,
|
||||
message: message.into(),
|
||||
source: None,
|
||||
}
|
||||
}
|
||||
|
||||
let Some(raw_value) = local_master_key_env_value() else {
|
||||
return Err(sse_not_configured(
|
||||
"SSE-S3 requires RUSTFS_SSE_S3_MASTER_KEY to be set to a base64-encoded 32-byte key when KMS is not configured",
|
||||
));
|
||||
};
|
||||
|
||||
let decoded = BASE64_STANDARD.decode_to_vec(raw_value.trim()).map_err(|err| {
|
||||
sse_not_configured(format!(
|
||||
"RUSTFS_SSE_S3_MASTER_KEY must be valid base64 for SSE-S3 when KMS is not configured: {err}"
|
||||
))
|
||||
})?;
|
||||
let master_key: [u8; 32] = decoded.try_into().map_err(|_| {
|
||||
sse_not_configured("RUSTFS_SSE_S3_MASTER_KEY must decode to exactly 32 bytes for SSE-S3 when KMS is not configured")
|
||||
})?;
|
||||
|
||||
Ok(Self { master_key })
|
||||
}
|
||||
|
||||
// Simple encryption of DEK
|
||||
pub fn encrypt_dek(dek: [u8; 32], cmk_value: [u8; 32]) -> Result<String, SseError> {
|
||||
// Use AES-256-GCM to encrypt DEK
|
||||
let key = Key::<Aes256Gcm>::from(cmk_value);
|
||||
|
||||
let cipher = Aes256Gcm::new(&key);
|
||||
let mut nonce_bytes = [0u8; 12];
|
||||
rand::rng().fill_bytes(&mut nonce_bytes);
|
||||
let nonce = Nonce::from(nonce_bytes);
|
||||
let ciphertext = cipher
|
||||
.encrypt(&nonce, dek.as_slice())
|
||||
.map_err(|_| SseError::internal("Failed to encrypt DEK"))?;
|
||||
|
||||
let nonce = BASE64_STANDARD.encode_to_string(nonce);
|
||||
let ciphertext = BASE64_STANDARD.encode_to_string(ciphertext);
|
||||
serde_json::to_string(&LocalSseDekEnvelope {
|
||||
version: LOCAL_SSE_DEK_FORMAT_VERSION,
|
||||
nonce: &nonce,
|
||||
ciphertext: &ciphertext,
|
||||
})
|
||||
.map_err(|e| SseError::internal(format!("Failed to serialize encrypted DEK: {e}")))
|
||||
}
|
||||
|
||||
// Simple decryption of DEK
|
||||
pub fn decrypt_dek(encrypted_dek: &str, cmk_value: [u8; 32]) -> Result<[u8; 32], SseError> {
|
||||
let envelope = serde_json::from_str::<LocalSseDekEnvelope<'_>>(encrypted_dek);
|
||||
let (nonce, ciphertext) = match envelope {
|
||||
Ok(envelope) => {
|
||||
if envelope.version != LOCAL_SSE_DEK_FORMAT_VERSION {
|
||||
return Err(SseError::internal(format!(
|
||||
"Unsupported encrypted DEK format version: {}",
|
||||
envelope.version
|
||||
)));
|
||||
}
|
||||
(envelope.nonce, envelope.ciphertext)
|
||||
}
|
||||
Err(json_error) if encrypted_dek.trim_start().starts_with('{') => {
|
||||
return Err(SseError::internal(format!("Invalid encrypted DEK JSON format: {json_error}")));
|
||||
}
|
||||
Err(_) => {
|
||||
// DEPRECATED: read-only compatibility for persisted colon-delimited DEKs.
|
||||
// RUSTFS_COMPAT_TODO(sse-local-dek-json-v1): Remove after all supported upgrades have rewritten legacy DEKs.
|
||||
let Some((nonce, ciphertext)) = encrypted_dek.split_once(':') else {
|
||||
return Err(SseError::internal("Invalid encrypted DEK format"));
|
||||
};
|
||||
if ciphertext.contains(':') {
|
||||
return Err(SseError::internal("Invalid encrypted DEK format"));
|
||||
}
|
||||
(nonce, ciphertext)
|
||||
}
|
||||
};
|
||||
let nonce_vec = BASE64_STANDARD
|
||||
.decode_to_vec(nonce)
|
||||
.map_err(|_| SseError::internal("Invalid nonce format"))?;
|
||||
let ciphertext = BASE64_STANDARD
|
||||
.decode_to_vec(ciphertext)
|
||||
.map_err(|_| SseError::internal("Invalid ciphertext format"))?;
|
||||
|
||||
let key = Key::<Aes256Gcm>::from(cmk_value);
|
||||
let cipher = Aes256Gcm::new(&key);
|
||||
|
||||
let nonce_array: [u8; 12] = nonce_vec.try_into().map_err(|_| SseError::internal("Invalid nonce length"))?;
|
||||
let nonce = Nonce::from(nonce_array);
|
||||
|
||||
let plaintext = cipher
|
||||
.decrypt(&nonce, ciphertext.as_slice())
|
||||
.map_err(|e| SseError::internal(format!("Failed to decrypt DEK: {e}")))?;
|
||||
|
||||
let dek: [u8; 32] = plaintext
|
||||
.try_into()
|
||||
.map_err(|_| SseError::internal("Decrypted DEK has invalid length"))?;
|
||||
|
||||
Ok(dek)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl SseDekProvider for LocalSseDekProvider {
|
||||
fn wraps_dek_with_local_master_key(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
async fn generate_sse_dek(
|
||||
&self,
|
||||
_context: &ObjectEncryptionContext,
|
||||
_kms_key_id: &str,
|
||||
) -> Result<(DataKey, Vec<u8>), SseError> {
|
||||
// Generate a 32-byte array as data key
|
||||
let mut dek = [0u8; 32];
|
||||
rand::rng().fill_bytes(&mut dek);
|
||||
|
||||
// Generate a 12-byte array as IV
|
||||
let mut nonce = [0u8; 12];
|
||||
rand::rng().fill_bytes(&mut nonce);
|
||||
|
||||
// Encrypt data key with master key
|
||||
let encrypted_dek = Self::encrypt_dek(dek, self.master_key)?;
|
||||
|
||||
// Return data key and IV
|
||||
Ok((
|
||||
DataKey {
|
||||
plaintext_key: dek,
|
||||
nonce,
|
||||
},
|
||||
encrypted_dek.into_bytes(),
|
||||
))
|
||||
}
|
||||
|
||||
async fn decrypt_sse_dek(
|
||||
&self,
|
||||
encrypted_dek: &[u8],
|
||||
_kms_key_id: &str,
|
||||
_context: &ObjectEncryptionContext,
|
||||
) -> Result<[u8; 32], SseError> {
|
||||
// Decrypt data key with master key
|
||||
let encrypted_dek_str =
|
||||
std::str::from_utf8(encrypted_dek).map_err(|_| SseError::internal("Invalid UTF-8 in encrypted DEK"))?;
|
||||
let dek = Self::decrypt_dek(encrypted_dek_str, self.master_key)?;
|
||||
Ok(dek)
|
||||
}
|
||||
|
||||
#[cfg(feature = "rio-v2")]
|
||||
async fn decrypt_minio_sse_dek(
|
||||
&self,
|
||||
encrypted_dek: &[u8],
|
||||
_kms_key_id: &str,
|
||||
context: &ObjectEncryptionContext,
|
||||
) -> Result<[u8; 32], SseError> {
|
||||
let aad = minio_kms_associated_data(context)?;
|
||||
decrypt_minio_kms_data_key(encrypted_dek, &self.master_key, &aad)
|
||||
}
|
||||
}
|
||||
@@ -1,151 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Reading data keys sealed by MinIO's builtin KMS.
|
||||
|
||||
use crate::core::keying::context::canonical_kms_bucket_path;
|
||||
use crate::core::keying::object::HmacSha256;
|
||||
use crate::error::SseError;
|
||||
use base64_simd::STANDARD as BASE64_STANDARD;
|
||||
use hmac::Mac;
|
||||
use rustfs_kms::types::ObjectEncryptionContext;
|
||||
use serde::Deserialize;
|
||||
|
||||
/// Associated data MinIO binds when sealing a data key.
|
||||
///
|
||||
/// MinIO passes the object's encryption context as the AEAD's associated data,
|
||||
/// serialized as canonical JSON with sorted keys — the same canonicalization
|
||||
/// [`rustfs_kms::context_aad`] performs, which is why the context RustFS
|
||||
/// already rebuilds for the read can be reused verbatim. For SSE-S3 that
|
||||
/// context is `{bucket: "bucket/object"}`; for SSE-KMS it is whatever the
|
||||
/// request supplied, recovered from the stored MinIO context header.
|
||||
pub(crate) fn minio_kms_associated_data(context: &ObjectEncryptionContext) -> Result<Vec<u8>, SseError> {
|
||||
let mut ctx = context.encryption_context.clone();
|
||||
ctx.entry(context.bucket.clone())
|
||||
.or_insert_with(|| canonical_kms_bucket_path(&context.bucket, &context.object_key));
|
||||
rustfs_kms::context_aad(&ctx).map_err(|e| SseError::internal(format!("Failed to canonicalize MinIO KMS context: {e}")))
|
||||
}
|
||||
|
||||
/// MinIO's builtin-KMS ciphertext in its JSON encoding.
|
||||
///
|
||||
/// Deliberately its own type rather than a relaxation of
|
||||
/// [`LocalSseDekEnvelope`]: widening that envelope's `deny_unknown_fields`
|
||||
/// to admit this shape would also admit malformed RustFS envelopes, which
|
||||
/// backlog#1567 requires to keep failing closed.
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct MinioKmsCiphertextJson {
|
||||
aead: String,
|
||||
#[allow(
|
||||
dead_code,
|
||||
reason = "present in MinIO's encoding; the key is identified by metadata instead"
|
||||
)]
|
||||
#[serde(default)]
|
||||
id: String,
|
||||
iv: String,
|
||||
nonce: String,
|
||||
bytes: String,
|
||||
}
|
||||
|
||||
/// Bytes of trailing randomness every MinIO builtin-KMS ciphertext carries:
|
||||
/// a 16-byte IV followed by a 12-byte nonce, *after* the sealed bytes.
|
||||
const MINIO_KMS_RANDOM_LEN: usize = 28;
|
||||
|
||||
const MINIO_KMS_IV_LEN: usize = 16;
|
||||
|
||||
const MINIO_KMS_AEAD_AES_GCM: &str = "AES-256-GCM-HMAC-SHA-256";
|
||||
|
||||
const MINIO_KMS_AEAD_CHACHA20: &str = "ChaCha20Poly1305";
|
||||
|
||||
/// Unwrap a data key sealed by MinIO's builtin (static-secret) KMS.
|
||||
///
|
||||
/// The wire format is `sealed_bytes || iv[16] || nonce[12]` — the randomness
|
||||
/// trails the ciphertext rather than leading it, and MinIO's own decoder
|
||||
/// normalizes its legacy JSON encoding into exactly that byte order before
|
||||
/// opening it (`internal/kms/secret-key.go`, `parseCiphertext`). A raw
|
||||
/// (non-JSON) ciphertext is AES-256-GCM by definition there; the JSON form
|
||||
/// names its algorithm.
|
||||
///
|
||||
/// The sealing key is derived per ciphertext rather than being the master key:
|
||||
/// `HMAC-SHA256(master, iv)` for AES-256-GCM, `HChaCha20(master, iv)` for
|
||||
/// ChaCha20-Poly1305. The encryption context is bound as associated data.
|
||||
pub(crate) fn decrypt_minio_kms_data_key(encrypted_dek: &[u8], master_key: &[u8; 32], aad: &[u8]) -> Result<[u8; 32], SseError> {
|
||||
let (body, algorithm) = match std::str::from_utf8(encrypted_dek) {
|
||||
// MinIO only treats a payload as JSON when it both starts and ends like
|
||||
// an object, and falls back to the raw layout when it does not parse —
|
||||
// mirrored here so a ciphertext that merely looks like JSON is not
|
||||
// rejected outright.
|
||||
Ok(text)
|
||||
if text.starts_with('{')
|
||||
&& text.ends_with('}')
|
||||
&& let Ok(json) = serde_json::from_str::<MinioKmsCiphertextJson>(text) =>
|
||||
{
|
||||
let decode = |what: &str, value: &str| -> Result<Vec<u8>, SseError> {
|
||||
BASE64_STANDARD
|
||||
.decode_to_vec(value)
|
||||
.map_err(|e| SseError::internal(format!("Invalid MinIO KMS {what}: {e}")))
|
||||
};
|
||||
let mut body = decode("ciphertext", &json.bytes)?;
|
||||
body.extend_from_slice(&decode("iv", &json.iv)?);
|
||||
body.extend_from_slice(&decode("nonce", &json.nonce)?);
|
||||
(body, json.aead)
|
||||
}
|
||||
_ => (encrypted_dek.to_vec(), MINIO_KMS_AEAD_AES_GCM.to_string()),
|
||||
};
|
||||
|
||||
if body.len() <= MINIO_KMS_RANDOM_LEN {
|
||||
return Err(SseError::internal("MinIO KMS ciphertext is too short to carry its IV and nonce"));
|
||||
}
|
||||
let (sealed, random) = body.split_at(body.len() - MINIO_KMS_RANDOM_LEN);
|
||||
let (iv, nonce) = random.split_at(MINIO_KMS_IV_LEN);
|
||||
|
||||
let plaintext = match algorithm.as_str() {
|
||||
MINIO_KMS_AEAD_AES_GCM => {
|
||||
use aes_gcm::{Aes256Gcm, KeyInit, aead::Aead};
|
||||
let mut mac = HmacSha256::new_from_slice(master_key)
|
||||
.map_err(|_| SseError::internal("MinIO KMS sealing key derivation failed"))?;
|
||||
mac.update(iv);
|
||||
let sealing_key: [u8; 32] = mac.finalize().into_bytes().into();
|
||||
let cipher = Aes256Gcm::new_from_slice(&sealing_key)
|
||||
.map_err(|_| SseError::internal("MinIO KMS sealing key is not a valid AES-256 key"))?;
|
||||
let nonce = aes_gcm::Nonce::try_from(nonce).map_err(|_| SseError::internal("MinIO KMS nonce is not 12 bytes"))?;
|
||||
cipher.decrypt(&nonce, aes_gcm::aead::Payload { msg: sealed, aad })
|
||||
}
|
||||
MINIO_KMS_AEAD_CHACHA20 => {
|
||||
use chacha20poly1305::{KeyInit, XChaCha20Poly1305, aead::Aead};
|
||||
// MinIO derives this branch's key with HChaCha20 over the 16-byte
|
||||
// IV, which is exactly XChaCha20-Poly1305's own construction, so the
|
||||
// extended-nonce cipher does the derivation rather than hand-rolling it.
|
||||
let mut extended = Vec::with_capacity(MINIO_KMS_IV_LEN + nonce.len());
|
||||
extended.extend_from_slice(iv);
|
||||
extended.extend_from_slice(nonce);
|
||||
let cipher = XChaCha20Poly1305::new_from_slice(master_key)
|
||||
.map_err(|_| SseError::internal("MinIO KMS master key is not a valid ChaCha20 key"))?;
|
||||
let nonce = chacha20poly1305::XNonce::try_from(extended.as_slice())
|
||||
.map_err(|_| SseError::internal("MinIO KMS extended nonce is not 24 bytes"))?;
|
||||
cipher.decrypt(&nonce, chacha20poly1305::aead::Payload { msg: sealed, aad })
|
||||
}
|
||||
other => {
|
||||
return Err(SseError::internal(format!("Unsupported MinIO KMS AEAD algorithm: {other}")));
|
||||
}
|
||||
}
|
||||
// An AEAD failure here is authentication, not a decode slip: a wrong master
|
||||
// key, a tampered ciphertext, and an encryption context that does not match
|
||||
// what sealed it all land here and must all fail closed.
|
||||
.map_err(|_| SseError::internal("MinIO KMS data key failed authentication"))?;
|
||||
|
||||
plaintext
|
||||
.try_into()
|
||||
.map_err(|value: Vec<u8>| SseError::internal(format!("MinIO KMS data key must be 32 bytes, got {}", value.len())))
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Data-key providers: where a data key is wrapped and unwrapped.
|
||||
|
||||
pub(crate) mod kms_backed;
|
||||
pub(crate) mod local;
|
||||
#[cfg(feature = "rio-v2")]
|
||||
pub(crate) mod minio;
|
||||
pub(crate) mod provider;
|
||||
@@ -1,288 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! The data-key provider interface and the per-request choice between the KMS and the local master key.
|
||||
|
||||
use crate::error::SseError;
|
||||
use crate::host::SseHost;
|
||||
use crate::kms::kms_backed::KmsSseDekProvider;
|
||||
use crate::kms::local::LocalSseDekProvider;
|
||||
use async_trait::async_trait;
|
||||
use rustfs_kms::DataKey;
|
||||
use rustfs_kms::types::ObjectEncryptionContext;
|
||||
use std::sync::{Arc, LazyLock, RwLock};
|
||||
use tracing::debug;
|
||||
|
||||
/// Trait for SSE data encryption key management
|
||||
/// Abstracts the source of encryption keys (KMS, test provider, etc.)
|
||||
#[async_trait]
|
||||
pub trait SseDekProvider: Send + Sync {
|
||||
/// Generate an SSE data encryption key
|
||||
async fn generate_sse_dek(&self, context: &ObjectEncryptionContext, kms_key_id: &str)
|
||||
-> Result<(DataKey, Vec<u8>), SseError>;
|
||||
|
||||
/// Decrypt an SSE data encryption key (returns only plaintext key, nonce should be read from metadata)
|
||||
async fn decrypt_sse_dek(
|
||||
&self,
|
||||
encrypted_dek: &[u8],
|
||||
kms_key_id: &str,
|
||||
context: &ObjectEncryptionContext,
|
||||
) -> Result<[u8; 32], SseError>;
|
||||
|
||||
/// Whether this provider may own a stored data key that is not a RustFS
|
||||
/// envelope — see `KmsBackend::reads_bare_data_key_ciphertext`.
|
||||
async fn reads_bare_data_key_ciphertext(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Re-wrap a KMS-wrapped DEK envelope onto its master key's current
|
||||
/// version without exposing the plaintext DEK to the caller.
|
||||
///
|
||||
/// Defaults to refusing: only the KMS-backed provider can rewrap, and a
|
||||
/// provider that cannot must say so rather than hand back the input as if
|
||||
/// it had been re-protected.
|
||||
async fn rewrap_sse_dek(
|
||||
&self,
|
||||
_encrypted_dek: &[u8],
|
||||
_context: &ObjectEncryptionContext,
|
||||
) -> Result<rustfs_kms::types::RewrapDataKeyResponse, SseError> {
|
||||
Err(SseError::internal("This DEK provider cannot rewrap KMS-wrapped data keys"))
|
||||
}
|
||||
|
||||
/// Whether this provider wraps data keys with a node-local master key
|
||||
/// instead of a KMS service.
|
||||
///
|
||||
/// SSE-KMS must never be served by such a provider: the stored object would
|
||||
/// claim `aws:kms` and name a KMS key id that never wrapped anything.
|
||||
/// Defaults to false so only the local fallback has to declare itself.
|
||||
fn wraps_dek_with_local_master_key(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Decrypt a DEK from positively identified legacy managed metadata.
|
||||
#[cfg(feature = "rio-v2")]
|
||||
async fn decrypt_legacy_sse_dek(
|
||||
&self,
|
||||
encrypted_dek: &[u8],
|
||||
kms_key_id: &str,
|
||||
context: &ObjectEncryptionContext,
|
||||
) -> Result<[u8; 32], SseError> {
|
||||
self.decrypt_sse_dek(encrypted_dek, kms_key_id, context).await
|
||||
}
|
||||
|
||||
/// Unwrap a data key that MinIO's builtin KMS sealed.
|
||||
///
|
||||
/// A separate entry point rather than a shape sniff inside
|
||||
/// [`Self::decrypt_sse_dek`]: the caller already knows the object carries a
|
||||
/// MinIO sealed key, and MinIO's raw ciphertext is unstructured bytes that
|
||||
/// no parser can reliably tell apart from anything else. Routing on the
|
||||
/// caller's knowledge keeps a RustFS envelope from ever reaching MinIO's
|
||||
/// decoder, and vice versa.
|
||||
///
|
||||
/// Defaults to refusing: only a provider holding the MinIO master secret
|
||||
/// can serve these, and a provider that cannot must fail rather than fall
|
||||
/// back to a decoder that would misread the bytes.
|
||||
#[cfg(feature = "rio-v2")]
|
||||
async fn decrypt_minio_sse_dek(
|
||||
&self,
|
||||
_encrypted_dek: &[u8],
|
||||
_kms_key_id: &str,
|
||||
_context: &ObjectEncryptionContext,
|
||||
) -> Result<[u8; 32], SseError> {
|
||||
Err(SseError::internal(
|
||||
"This KMS provider cannot unwrap a data key sealed by MinIO's builtin KMS",
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/// Unwrap the data key of an object that carries a sealed object key but no
|
||||
/// RustFS envelope.
|
||||
///
|
||||
/// Two writers produce that shape, and nothing in the bytes tells them apart:
|
||||
/// MinIO, whose data key is sealed under the static master key, and a RustFS
|
||||
/// `rio-v2` build on a backend that persisted its native ciphertext bare.
|
||||
/// MinIO's sealing is authenticated, so opening it under the local master key
|
||||
/// is a proof rather than a guess: the bare blob is offered to the KMS only
|
||||
/// once that has failed, or when no local master key exists to try. Without a
|
||||
/// backend that reads bare ciphertext the MinIO route is the only one, exactly
|
||||
/// as before.
|
||||
#[cfg(feature = "rio-v2")]
|
||||
pub(crate) async fn decrypt_unenveloped_sealed_dek(
|
||||
bare_kms: Option<Arc<dyn SseDekProvider + '_>>,
|
||||
local: Result<Arc<dyn SseDekProvider>, SseError>,
|
||||
encrypted_dek: &[u8],
|
||||
kms_key_id: &str,
|
||||
context: &ObjectEncryptionContext,
|
||||
) -> Result<[u8; 32], SseError> {
|
||||
let Some(kms) = bare_kms else {
|
||||
return local?.decrypt_minio_sse_dek(encrypted_dek, kms_key_id, context).await;
|
||||
};
|
||||
if let Ok(local) = local
|
||||
&& let Ok(data_key) = local.decrypt_minio_sse_dek(encrypted_dek, kms_key_id, context).await
|
||||
{
|
||||
return Ok(data_key);
|
||||
}
|
||||
kms.decrypt_sse_dek(encrypted_dek, kms_key_id, context).await
|
||||
}
|
||||
|
||||
/// The provider that unwraps KMS-wrapped data keys.
|
||||
///
|
||||
/// A provider registered through `set_sse_dek_provider_for_test` stands in for
|
||||
/// the fresh [`KmsSseDekProvider`], which cannot resolve a KMS service from a
|
||||
/// host that has none. Only `GLOBAL_KMS_DEK_PROVIDER` is consulted, never
|
||||
/// `GLOBAL_SSE_DEK_PROVIDER`, so a local provider cached by an earlier
|
||||
/// `get_local_sse_dek_provider` call cannot be selected to unwrap a KMS envelope.
|
||||
pub(crate) async fn kms_sse_dek_provider<'a>(host: &'a dyn SseHost) -> Result<Arc<dyn SseDekProvider + 'a>, SseError> {
|
||||
if let Some(cached) = GLOBAL_KMS_DEK_PROVIDER.read().ok().and_then(|guard| guard.as_ref().cloned()) {
|
||||
return Ok(cached);
|
||||
}
|
||||
Ok(Arc::new(KmsSseDekProvider::new(host).await?))
|
||||
}
|
||||
|
||||
/// The KMS provider, when its backend may own a stored data key that is not a
|
||||
/// RustFS envelope. `None` without a running KMS, which leaves such a key to
|
||||
/// the local provider exactly as before.
|
||||
pub(crate) async fn bare_data_key_kms_provider<'a>(host: &'a dyn SseHost) -> Option<Arc<dyn SseDekProvider + 'a>> {
|
||||
let provider = kms_sse_dek_provider(host).await.ok()?;
|
||||
provider.reads_bare_data_key_ciphertext().await.then_some(provider)
|
||||
}
|
||||
|
||||
/// Global SSE DEK provider cache for local / test providers.
|
||||
///
|
||||
/// Populated by `get_local_sse_dek_provider` and (for backward-compat in tests)
|
||||
/// `set_sse_dek_provider_for_test`. Read by `get_local_sse_dek_provider` only —
|
||||
/// the KMS-envelope decrypt path uses `GLOBAL_KMS_DEK_PROVIDER` so that a cached
|
||||
/// local provider can never be selected for a KMS-wrapped data-key.
|
||||
pub(crate) static GLOBAL_SSE_DEK_PROVIDER: LazyLock<RwLock<Option<Arc<dyn SseDekProvider>>>> =
|
||||
LazyLock::new(|| RwLock::new(None));
|
||||
|
||||
/// Global KMS DEK provider cache for test-injected KMS providers.
|
||||
///
|
||||
/// Populated **only** by `set_sse_dek_provider_for_test` (test-only).
|
||||
/// Read **only** by the KMS-envelope branch of `apply_managed_decryption_material`.
|
||||
/// Separation from `GLOBAL_SSE_DEK_PROVIDER` prevents a previously-cached local
|
||||
/// provider from being selected to unwrap a KMS data-key envelope.
|
||||
static GLOBAL_KMS_DEK_PROVIDER: LazyLock<RwLock<Option<Arc<dyn SseDekProvider>>>> = LazyLock::new(|| RwLock::new(None));
|
||||
|
||||
/// Get or initialize the global SSE DEK provider
|
||||
///
|
||||
/// Factory function that automatically selects the appropriate provider:
|
||||
/// - If `__RUSTFS_SSE_SIMPLE_CMK` environment variable exists: use SimpleSseDekProvider (test mode)
|
||||
/// - Otherwise: use KmsSseDekProvider (production mode with real KMS)
|
||||
///
|
||||
/// # Returns
|
||||
/// Arc to the global SSE DEK provider instance
|
||||
///
|
||||
/// # Example
|
||||
/// ```rust,ignore
|
||||
/// let provider = get_sse_dek_provider(host).await?;
|
||||
/// let (data_key, encrypted_dek) = provider
|
||||
/// .generate_sse_dek("bucket", "key", "kms-key-id")
|
||||
/// .await?;
|
||||
/// ```
|
||||
pub(crate) async fn get_sse_dek_provider<'a>(host: &'a dyn SseHost) -> Result<Arc<dyn SseDekProvider + 'a>, SseError> {
|
||||
if host.encryption_service().await.is_some() {
|
||||
debug!("Using KmsSseDekProvider (KMS configured)");
|
||||
return Ok(Arc::new(KmsSseDekProvider::new(host).await?));
|
||||
}
|
||||
|
||||
get_local_sse_dek_provider()
|
||||
}
|
||||
|
||||
/// Get the local data-key provider, loading and caching it on first use.
|
||||
///
|
||||
/// Startup calls this to check the local master key ahead of traffic; a request
|
||||
/// that arrives without that step loads it the same way and gets the same error.
|
||||
pub(crate) fn get_local_sse_dek_provider() -> Result<Arc<dyn SseDekProvider>, SseError> {
|
||||
// An explicitly injected test provider overrides both the cache and the
|
||||
// environment-based selection below.
|
||||
#[cfg(any(test, feature = "test-util"))]
|
||||
if let Some(injected) = test_injected_sse_dek_provider() {
|
||||
return Ok(injected);
|
||||
}
|
||||
|
||||
// Check if already initialized
|
||||
if let Some(provider) = GLOBAL_SSE_DEK_PROVIDER
|
||||
.read()
|
||||
.map_err(|_| SseError::internal("Failed to read global SSE DEK provider cache"))?
|
||||
.as_ref()
|
||||
.cloned()
|
||||
{
|
||||
return Ok(provider);
|
||||
}
|
||||
|
||||
// In test mode, prefer the simple CMK provider when the env var is set.
|
||||
#[cfg(any(test, feature = "test-util"))]
|
||||
{
|
||||
if std::env::var("__RUSTFS_SSE_SIMPLE_CMK").is_ok() {
|
||||
debug!("Using LocalSseDekProvider (test mode) based on __RUSTFS_SSE_SIMPLE_CMK");
|
||||
let provider: Arc<dyn SseDekProvider> = Arc::new(LocalSseDekProvider::new()?);
|
||||
let mut slot = GLOBAL_SSE_DEK_PROVIDER
|
||||
.write()
|
||||
.map_err(|_| SseError::internal("Failed to update global SSE DEK provider cache"))?;
|
||||
if let Some(existing) = slot.as_ref() {
|
||||
return Ok(existing.clone());
|
||||
}
|
||||
*slot = Some(provider.clone());
|
||||
return Ok(provider);
|
||||
}
|
||||
}
|
||||
|
||||
// Production fallback: local SSE-S3 provider (no KMS configured).
|
||||
debug!("Using local SSE-S3 provider (KMS not configured)");
|
||||
let provider: Arc<dyn SseDekProvider> = Arc::new(LocalSseDekProvider::new_from_env()?);
|
||||
|
||||
let mut slot = GLOBAL_SSE_DEK_PROVIDER
|
||||
.write()
|
||||
.map_err(|_| SseError::internal("Failed to update global SSE DEK provider cache"))?;
|
||||
if let Some(existing) = slot.as_ref() {
|
||||
return Ok(existing.clone());
|
||||
}
|
||||
*slot = Some(provider.clone());
|
||||
|
||||
Ok(provider)
|
||||
}
|
||||
|
||||
/// Reset both global SSE DEK provider caches (for testing only)
|
||||
///
|
||||
/// Clears GLOBAL_SSE_DEK_PROVIDER (local/test providers) and
|
||||
/// GLOBAL_KMS_DEK_PROVIDER (test-injected KMS providers).
|
||||
#[cfg(any(test, feature = "test-util"))]
|
||||
pub fn reset_sse_dek_provider() {
|
||||
if let Ok(mut slot) = GLOBAL_SSE_DEK_PROVIDER.write() {
|
||||
*slot = None;
|
||||
}
|
||||
if let Ok(mut slot) = GLOBAL_KMS_DEK_PROVIDER.write() {
|
||||
*slot = None;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "test-util"))]
|
||||
pub fn set_sse_dek_provider_for_test(provider: Arc<dyn SseDekProvider>) {
|
||||
if let Ok(mut slot) = GLOBAL_KMS_DEK_PROVIDER.write() {
|
||||
*slot = Some(provider.clone());
|
||||
}
|
||||
if let Ok(mut slot) = GLOBAL_SSE_DEK_PROVIDER.write() {
|
||||
*slot = Some(provider);
|
||||
}
|
||||
}
|
||||
|
||||
/// Provider explicitly injected via `set_sse_dek_provider_for_test`, if any.
|
||||
///
|
||||
/// Reads `GLOBAL_KMS_DEK_PROVIDER` because that slot is populated *only* by the
|
||||
/// test setter, so a hit here always means an explicit test injection.
|
||||
#[cfg(any(test, feature = "test-util"))]
|
||||
fn test_injected_sse_dek_provider() -> Option<Arc<dyn SseDekProvider>> {
|
||||
GLOBAL_KMS_DEK_PROVIDER.read().ok().and_then(|guard| guard.as_ref().cloned())
|
||||
}
|
||||
@@ -1,110 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! S3 server-side encryption: which key encrypts an object, and what is stored to find it again.
|
||||
//!
|
||||
//! Covers the three S3 schemes:
|
||||
//! - SSE-S3 (`AES256`): a data key wrapped by a key the server owns
|
||||
//! - SSE-KMS (`aws:kms`): a data key wrapped by a KMS key the caller names
|
||||
//! - SSE-C: a key the client supplies on every request and the server never stores
|
||||
//!
|
||||
//! ## Where this crate sits
|
||||
//!
|
||||
//! Between the S3 layer and the storage engine, depending on neither. It decides
|
||||
//! the effective scheme of a write, obtains and unwraps data keys, authorizes the
|
||||
//! use of a KMS key, and encodes and reads the encryption metadata of an object.
|
||||
//! It does not transform object bytes: it hands back key material
|
||||
//! ([`EncryptionMaterial`], [`DecryptionMaterial`]) and the caller applies it.
|
||||
//!
|
||||
//! What it needs from the running server — the KMS service, a bucket's default
|
||||
//! encryption, a key-permission decision — arrives through [`SseHost`], passed
|
||||
//! to every entry point that consults the server. Failures are reported as
|
||||
//! [`SseError`], already classified with the S3 error code the caller answers with.
|
||||
//!
|
||||
//! ## Entry points
|
||||
//!
|
||||
//! - [`sse_encryption`] and [`sse_prepare_encryption`] resolve the material of a write;
|
||||
//! [`encryption_material_to_metadata`] turns it into the metadata to store.
|
||||
//! - [`sse_decryption`] resolves the material of a read from stored metadata;
|
||||
//! [`classify_sse_read_response`] derives the response headers without unwrapping a key.
|
||||
//! - [`rewrap_object_encryption_metadata`] moves a stored data key onto the current
|
||||
//! version of its KMS key.
|
||||
//! - [`startup::init`] settles the environment-driven configuration once per process
|
||||
//! and reports it; calling it is optional.
|
||||
//!
|
||||
//! ```rust,ignore
|
||||
//! let request = EncryptionRequest {
|
||||
//! bucket: &bucket,
|
||||
//! key: &key,
|
||||
//! server_side_encryption: effective_sse,
|
||||
//! ssekms_key_id: effective_kms_key_id,
|
||||
//! ssekms_context: None,
|
||||
//! sse_customer_algorithm: None,
|
||||
//! sse_customer_key: None,
|
||||
//! sse_customer_key_md5: None,
|
||||
//! content_size: actual_size,
|
||||
//! principal: principal.as_ref(),
|
||||
//! };
|
||||
//!
|
||||
//! if let Some(material) = sse_encryption(host, request).await? {
|
||||
//! metadata.extend(encryption_material_to_metadata(&material)?);
|
||||
//! }
|
||||
//! ```
|
||||
|
||||
mod core;
|
||||
mod error;
|
||||
mod host;
|
||||
mod kms;
|
||||
pub mod startup;
|
||||
#[cfg(any(test, feature = "test-util"))]
|
||||
pub mod testing;
|
||||
#[cfg(test)]
|
||||
#[allow(unused_imports)]
|
||||
mod tests;
|
||||
|
||||
pub use crate::core::audit::KmsRequestAuditScope;
|
||||
pub use crate::core::keying::material::{DecryptionMaterial, EncryptionKeyKind, EncryptionMaterial, ManagedSealedKey};
|
||||
pub use crate::core::keying::metadata::{
|
||||
ConflictingEncryptionMetadata, encryption_material_to_metadata, mark_encrypted_multipart_metadata,
|
||||
normalize_encryption_metadata_case, stored_ssec_metadata, strip_managed_encryption_metadata,
|
||||
};
|
||||
pub use crate::core::read::{DecryptionRequest, classify_sse_read_response, sse_decryption};
|
||||
pub use crate::core::request::SseKmsPrincipal;
|
||||
#[cfg(any(test, feature = "test-util"))]
|
||||
pub use crate::core::request::TestAuthorizationHooks;
|
||||
pub use crate::core::rewrap::{ObjectDekRewrapOutcome, rewrap_object_encryption_metadata};
|
||||
pub use crate::core::s3::authorization::{authorize_sse_kms_multipart_write, authorize_sse_kms_object_read};
|
||||
pub use crate::core::s3::headers::{
|
||||
SseReadResponseHeaders, SsecParamsFromHeaders, build_ssec_read_headers, extract_server_side_encryption_from_headers,
|
||||
extract_ssec_params_from_headers, extract_ssekms_context_from_headers, project_sse_read_response_headers,
|
||||
validate_sse_headers_for_read, validate_sse_headers_for_write,
|
||||
};
|
||||
pub use crate::core::s3::scheme::{DEFAULT_SSE_ALGORITHM, bucket_default_write_sse};
|
||||
pub use crate::core::s3::ssec::{ssec_invalid_request, validate_ssec_for_read};
|
||||
pub use crate::core::write::{EncryptionRequest, PrepareEncryptionRequest, sse_encryption, sse_prepare_encryption};
|
||||
pub use crate::error::{KMS_KEY_NOT_FOUND_ERROR_CODE, SseError, data_plane_kms_error};
|
||||
pub use crate::host::{BucketDefaultEncryption, BucketEncryption, KmsKeyAuthorizer, SseHost};
|
||||
pub use crate::kms::local::LocalSseDekProvider;
|
||||
#[cfg(any(test, feature = "test-util"))]
|
||||
pub use crate::kms::local::TestSseDekProvider;
|
||||
pub use crate::kms::provider::SseDekProvider;
|
||||
#[cfg(any(test, feature = "test-util"))]
|
||||
pub use crate::kms::provider::{reset_sse_dek_provider, set_sse_dek_provider_for_test};
|
||||
// The managed-SSE classifier lives in the shared encryption-keys module so the
|
||||
// scanner can reuse it; re-exported here so SSE callers find it beside the rest.
|
||||
pub use rustfs_utils::http::object_encryption_keys::SSEType;
|
||||
pub use rustfs_utils::http::object_encryption_keys::contains_managed_encryption_metadata;
|
||||
|
||||
pub(crate) const LOG_COMPONENT_STORAGE: &str = "storage";
|
||||
pub(crate) const LOG_SUBSYSTEM_SSE: &str = "sse";
|
||||
@@ -1,250 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! The crate's process-startup step.
|
||||
//!
|
||||
//! [`init`] settles the two things that cannot change without a restart, both
|
||||
//! read from the environment, and reports them once:
|
||||
//!
|
||||
//! - whether SSE-KMS requests are authorized against the key they name;
|
||||
//! - whether the local SSE-S3 master key is usable.
|
||||
//!
|
||||
//! Nothing that follows the KMS service is decided here. The KMS can be
|
||||
//! configured, started, restarted, and stopped while the server runs, so the
|
||||
//! choice between the KMS and the local master key is made per request, and no
|
||||
//! KMS state is cached by this crate.
|
||||
//!
|
||||
//! Calling [`init`] is optional. A process that never calls it reads the same
|
||||
//! switch and loads the same master key on first use, with the same errors; it
|
||||
//! only goes without the startup report.
|
||||
|
||||
use crate::error::SseError;
|
||||
use crate::kms::local::{ENV_SSE_S3_MASTER_KEY, local_master_key_configured};
|
||||
use crate::kms::provider::get_local_sse_dek_provider;
|
||||
use crate::{LOG_COMPONENT_STORAGE, LOG_SUBSYSTEM_SSE};
|
||||
use rustfs_config::{DEFAULT_KMS_ENFORCE_SSE_KEY_POLICY, ENV_RUSTFS_KMS_ENFORCE_SSE_KEY_POLICY};
|
||||
use rustfs_utils::get_env_bool;
|
||||
use std::sync::{LazyLock, OnceLock};
|
||||
|
||||
const EVENT_SSE_KMS_KEY_POLICY_MODE: &str = "sse_kms_key_policy_mode";
|
||||
const EVENT_SSE_LOCAL_MASTER_KEY: &str = "sse_local_master_key";
|
||||
|
||||
/// State of the local SSE-S3 master key at startup.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum LocalMasterKeyStatus {
|
||||
/// Configured and usable; SSE-S3 works without a KMS.
|
||||
Loaded,
|
||||
/// Not configured; SSE-S3 requires a running KMS.
|
||||
NotSet,
|
||||
/// Configured but unusable; SSE-S3 requests that need it are rejected.
|
||||
Invalid,
|
||||
}
|
||||
|
||||
impl LocalMasterKeyStatus {
|
||||
fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Self::Loaded => "loaded",
|
||||
Self::NotSet => "not_set",
|
||||
Self::Invalid => "invalid",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// What [`init`] settled for this process.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct StartupReport {
|
||||
/// Whether SSE-KMS requests are authorized against the KMS key they name.
|
||||
pub key_policy_enforced: bool,
|
||||
/// State of the local SSE-S3 master key.
|
||||
pub local_master_key: LocalMasterKeyStatus,
|
||||
}
|
||||
|
||||
/// Settle the process-wide SSE configuration and report it.
|
||||
///
|
||||
/// Idempotent: the first call does the work and logs; later calls, such as a
|
||||
/// second embedded server in the same process, return the same report silently.
|
||||
/// An unusable master key does not fail startup, because a server whose KMS is
|
||||
/// running never needs it.
|
||||
pub fn init() -> StartupReport {
|
||||
static REPORT: OnceLock<StartupReport> = OnceLock::new();
|
||||
*REPORT.get_or_init(|| {
|
||||
let key_policy_enforced = key_policy_enforced();
|
||||
log_key_policy_mode(key_policy_enforced);
|
||||
|
||||
let local_master_key = load_local_master_key();
|
||||
log_local_master_key(&local_master_key);
|
||||
|
||||
StartupReport {
|
||||
key_policy_enforced,
|
||||
local_master_key: local_master_key.unwrap_or(LocalMasterKeyStatus::Invalid),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether per-key KMS authorization is enforced on the SSE-KMS data path.
|
||||
///
|
||||
/// Frozen on first read, by [`init`] or by the first request, so one process
|
||||
/// never answers the same question two ways.
|
||||
pub(crate) fn key_policy_enforced() -> bool {
|
||||
static ENFORCED: LazyLock<bool> =
|
||||
LazyLock::new(|| get_env_bool(ENV_RUSTFS_KMS_ENFORCE_SSE_KEY_POLICY, DEFAULT_KMS_ENFORCE_SSE_KEY_POLICY));
|
||||
*ENFORCED
|
||||
}
|
||||
|
||||
/// Load the local master key through the loader the request path uses.
|
||||
///
|
||||
/// An absent key is a state, not a failure: only a configured key is loaded, so
|
||||
/// the error case is exactly "configured but unusable".
|
||||
fn load_local_master_key() -> Result<LocalMasterKeyStatus, SseError> {
|
||||
if !local_master_key_configured() {
|
||||
return Ok(LocalMasterKeyStatus::NotSet);
|
||||
}
|
||||
get_local_sse_dek_provider().map(|_| LocalMasterKeyStatus::Loaded)
|
||||
}
|
||||
|
||||
/// Report the configured SSE-KMS authorization mode.
|
||||
///
|
||||
/// The disabled case warns rather than logs: while enforcement is off, any identity
|
||||
/// allowed to write an object can encrypt it under any key, and operators should hear
|
||||
/// about that even though disabled is the long-term default.
|
||||
fn log_key_policy_mode(enforced: bool) {
|
||||
if enforced {
|
||||
tracing::info!(
|
||||
event = EVENT_SSE_KMS_KEY_POLICY_MODE,
|
||||
component = LOG_COMPONENT_STORAGE,
|
||||
subsystem = LOG_SUBSYSTEM_SSE,
|
||||
enforced = true,
|
||||
"SSE-KMS requests are authorized against the resolved KMS key (kms:GenerateDataKey / kms:Decrypt)"
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
tracing::warn!(
|
||||
event = EVENT_SSE_KMS_KEY_POLICY_MODE,
|
||||
component = LOG_COMPONENT_STORAGE,
|
||||
subsystem = LOG_SUBSYSTEM_SSE,
|
||||
enforced = false,
|
||||
"SSE-KMS requests are not authorized against the KMS key they name; any identity allowed to \
|
||||
write an object may encrypt it under any key, and any identity allowed to read it may have it \
|
||||
decrypted. Grant kms:GenerateDataKey and kms:Decrypt on the keys your workloads use, then set \
|
||||
{ENV_RUSTFS_KMS_ENFORCE_SSE_KEY_POLICY}=true."
|
||||
);
|
||||
}
|
||||
|
||||
/// Report the local master key's state.
|
||||
///
|
||||
/// The failure text is the one a request is answered with. It describes the
|
||||
/// configuration problem and never carries the configured value.
|
||||
fn log_local_master_key(outcome: &Result<LocalMasterKeyStatus, SseError>) {
|
||||
match outcome {
|
||||
Ok(status @ LocalMasterKeyStatus::Loaded) => tracing::info!(
|
||||
event = EVENT_SSE_LOCAL_MASTER_KEY,
|
||||
component = LOG_COMPONENT_STORAGE,
|
||||
subsystem = LOG_SUBSYSTEM_SSE,
|
||||
state = status.as_str(),
|
||||
"{ENV_SSE_S3_MASTER_KEY} is loaded; SSE-S3 uses it whenever no KMS is running"
|
||||
),
|
||||
Ok(status) => tracing::info!(
|
||||
event = EVENT_SSE_LOCAL_MASTER_KEY,
|
||||
component = LOG_COMPONENT_STORAGE,
|
||||
subsystem = LOG_SUBSYSTEM_SSE,
|
||||
state = status.as_str(),
|
||||
"{ENV_SSE_S3_MASTER_KEY} is not set; SSE-S3 requires a running KMS"
|
||||
),
|
||||
Err(error) => tracing::error!(
|
||||
event = EVENT_SSE_LOCAL_MASTER_KEY,
|
||||
component = LOG_COMPONENT_STORAGE,
|
||||
subsystem = LOG_SUBSYSTEM_SSE,
|
||||
state = LocalMasterKeyStatus::Invalid.as_str(),
|
||||
reason = %error.message,
|
||||
"{ENV_SSE_S3_MASTER_KEY} is set but unusable; SSE-S3 requests are rejected while no KMS is running"
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::kms::provider::reset_sse_dek_provider;
|
||||
use crate::tests::lock_sse_test_state;
|
||||
use base64_simd::STANDARD as BASE64_STANDARD;
|
||||
use rustfs_s3_types::S3ErrorCode;
|
||||
use temp_env::with_vars;
|
||||
|
||||
/// Run `check` with the local master key set to `value` and nothing cached or injected.
|
||||
async fn with_master_key<T>(value: Option<&str>, check: impl FnOnce() -> T) -> T {
|
||||
let _guard = lock_sse_test_state().await;
|
||||
reset_sse_dek_provider();
|
||||
let outcome = with_vars([(ENV_SSE_S3_MASTER_KEY, value), ("__RUSTFS_SSE_SIMPLE_CMK", None)], check);
|
||||
reset_sse_dek_provider();
|
||||
outcome
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_absent_master_key_is_reported_as_not_set() {
|
||||
let status = with_master_key(None, load_local_master_key)
|
||||
.await
|
||||
.expect("an absent key is a state, not a failure");
|
||||
assert_eq!(status, LocalMasterKeyStatus::NotSet);
|
||||
|
||||
let blank = with_master_key(Some(" "), load_local_master_key)
|
||||
.await
|
||||
.expect("a blank value counts as absent");
|
||||
assert_eq!(blank, LocalMasterKeyStatus::NotSet);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_valid_master_key_is_loaded_and_cached_for_requests() {
|
||||
let encoded = BASE64_STANDARD.encode_to_string([7u8; 32]);
|
||||
let cached = with_master_key(Some(&encoded), || {
|
||||
let status = load_local_master_key().expect("a 32-byte key should load");
|
||||
assert_eq!(status, LocalMasterKeyStatus::Loaded);
|
||||
// The request path must find the provider startup loaded, not load a second one.
|
||||
crate::kms::provider::GLOBAL_SSE_DEK_PROVIDER
|
||||
.read()
|
||||
.expect("provider cache lock")
|
||||
.is_some()
|
||||
})
|
||||
.await;
|
||||
assert!(cached, "startup should leave the provider cached for the request path");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_unusable_master_key_fails_the_way_a_request_would() {
|
||||
let short = BASE64_STANDARD.encode_to_string([7u8; 16]);
|
||||
for unusable in ["not base64 at all!", short.as_str()] {
|
||||
let error = with_master_key(Some(unusable), load_local_master_key)
|
||||
.await
|
||||
.expect_err("an unusable key must not load");
|
||||
assert_eq!(error.code, S3ErrorCode::InvalidRequest);
|
||||
assert!(
|
||||
!error.message.contains(unusable),
|
||||
"the failure text must not carry the configured value: {}",
|
||||
error.message
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn init_reports_once_and_repeats_the_same_report() {
|
||||
let encoded = BASE64_STANDARD.encode_to_string([9u8; 32]);
|
||||
let (first, second) = with_master_key(Some(&encoded), || (init(), init())).await;
|
||||
|
||||
assert_eq!(first, second, "a second call must return the first report");
|
||||
assert_eq!(first.key_policy_enforced, key_policy_enforced());
|
||||
// The report is settled once per process, so this holds whether this test
|
||||
// or another caller of `init` ran first.
|
||||
assert_eq!(init(), first);
|
||||
}
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Fixtures for tests of SSE call sites, in this crate and in its dependents.
|
||||
|
||||
use crate::{KmsKeyAuthorizer, SseError, SseKmsPrincipal};
|
||||
use async_trait::async_trait;
|
||||
use base64_simd::STANDARD as BASE64_STANDARD;
|
||||
use rustfs_policy::policy::action::KmsAction;
|
||||
use rustfs_utils::http::headers::AMZ_ENCRYPTION_KMS;
|
||||
use rustfs_utils::http::object_encryption_keys::{
|
||||
INTERNAL_ENCRYPTION_ALGORITHM_HEADER, INTERNAL_ENCRYPTION_IV_HEADER, INTERNAL_ENCRYPTION_KEY_HEADER,
|
||||
INTERNAL_ENCRYPTION_KEY_ID_HEADER,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex, PoisonError};
|
||||
|
||||
/// An authorizer that answers every request the same way and records what it was asked.
|
||||
pub struct RecordingKmsKeyAuthorizer {
|
||||
allowed: bool,
|
||||
calls: Mutex<Vec<(KmsAction, String)>>,
|
||||
}
|
||||
|
||||
impl RecordingKmsKeyAuthorizer {
|
||||
pub fn new(allowed: bool) -> Arc<Self> {
|
||||
Arc::new(Self {
|
||||
allowed,
|
||||
calls: Mutex::new(Vec::new()),
|
||||
})
|
||||
}
|
||||
|
||||
/// The `(action, key id)` pairs this authorizer was asked about, in order.
|
||||
pub fn calls(&self) -> Vec<(KmsAction, String)> {
|
||||
self.calls.lock().unwrap_or_else(PoisonError::into_inner).clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl KmsKeyAuthorizer for RecordingKmsKeyAuthorizer {
|
||||
async fn is_allowed(&self, _principal: &SseKmsPrincipal, action: KmsAction, key_id: &str) -> Result<bool, SseError> {
|
||||
self.calls
|
||||
.lock()
|
||||
.unwrap_or_else(PoisonError::into_inner)
|
||||
.push((action, key_id.to_string()));
|
||||
Ok(self.allowed)
|
||||
}
|
||||
}
|
||||
|
||||
/// Stored metadata of an SSE-KMS object wrapped under the key `finance-key`.
|
||||
pub fn sse_kms_object_metadata() -> HashMap<String, String> {
|
||||
HashMap::from([
|
||||
("x-amz-server-side-encryption".to_string(), AMZ_ENCRYPTION_KMS.to_string()),
|
||||
("x-amz-server-side-encryption-aws-kms-key-id".to_string(), "finance-key".to_string()),
|
||||
(INTERNAL_ENCRYPTION_KEY_ID_HEADER.to_string(), "finance-key".to_string()),
|
||||
(INTERNAL_ENCRYPTION_KEY_HEADER.to_string(), BASE64_STANDARD.encode_to_string([7u8; 48])),
|
||||
(INTERNAL_ENCRYPTION_IV_HEADER.to_string(), BASE64_STANDARD.encode_to_string([9u8; 12])),
|
||||
(INTERNAL_ENCRYPTION_ALGORITHM_HEADER.to_string(), "aws:kms".to_string()),
|
||||
])
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -30,11 +30,12 @@ doctest = false
|
||||
|
||||
[features]
|
||||
default = []
|
||||
hotpath = ["rustfs-rio/hotpath"]
|
||||
hotpath-alloc = ["hotpath", "rustfs-rio/hotpath-alloc"]
|
||||
hotpath-cpu = ["hotpath", "rustfs-rio/hotpath-cpu"]
|
||||
hotpath = ["hotpath/hotpath", "hotpath/tokio", "rustfs-rio/hotpath"]
|
||||
hotpath-alloc = ["hotpath", "hotpath/hotpath-alloc", "rustfs-rio/hotpath-alloc"]
|
||||
hotpath-cpu = ["hotpath", "hotpath/hotpath-cpu", "rustfs-rio/hotpath-cpu"]
|
||||
|
||||
[dependencies]
|
||||
hotpath.workspace = true
|
||||
async-compression = { workspace = true, features = [
|
||||
"tokio",
|
||||
"bzip2",
|
||||
@@ -48,6 +49,10 @@ rustfs-rio.workspace = true
|
||||
tokio = { workspace = true, features = ["io-util", "macros", "rt"] }
|
||||
thiserror = { workspace = true }
|
||||
|
||||
[target.'cfg(target_os = "macos")'.dependencies]
|
||||
# Keep XZ decoding self-contained in macOS binaries instead of linking Homebrew liblzma.
|
||||
liblzma = { version = "0.4.9", features = ["static"] }
|
||||
|
||||
[dev-dependencies]
|
||||
rustfs-tokio-tar = { workspace = true }
|
||||
futures = { workspace = true }
|
||||
|
||||
@@ -29,16 +29,14 @@ zdict_builder = ["zstd_fixed/zdict_builder"]
|
||||
zstdmt = ["zstd_fixed/zstdmt"]
|
||||
|
||||
[dev-dependencies]
|
||||
# Enables zstd-safe's `seekable` feature for tests/security.rs. The test imports
|
||||
# `zstd_safe::seekable` directly so cargo-shear treats this as a used dependency.
|
||||
zstd-safe = { version = "8.3.0", features = ["seekable"] }
|
||||
toml = "1.2.2"
|
||||
toml = "1.2.1"
|
||||
semver = "1.0.28"
|
||||
apache-avro.workspace = true
|
||||
arrow-array = "60.2.0"
|
||||
arrow-ipc = { version = "60.2.0", features = ["zstd"] }
|
||||
arrow-schema = "60.2.0"
|
||||
parquet = { version = "60.2.0", default-features = false, features = ["arrow", "zstd"] }
|
||||
arrow-array = "60.1.0"
|
||||
arrow-ipc = { version = "60.1.0", features = ["zstd"] }
|
||||
arrow-schema = "60.1.0"
|
||||
parquet = { version = "60.1.0", default-features = false, features = ["arrow", "zstd"] }
|
||||
kafka-protocol = { version = "0.18.0", default-features = false, features = ["zstd"] }
|
||||
bytes.workspace = true
|
||||
|
||||
|
||||
@@ -86,7 +86,7 @@ fn corrupt_legacy_frame_cannot_emit_bytes_when_writer_is_dropped() {
|
||||
|
||||
#[test]
|
||||
fn seekable_offsets_past_eof_keep_vec_length_within_capacity() {
|
||||
use zstd_safe::seekable::{Seekable, SeekableCStream};
|
||||
use zstd::zstd_safe::seekable::{Seekable, SeekableCStream};
|
||||
|
||||
let source = b"seekable frame fixture with enough data for several independent frames";
|
||||
let mut encoded = [0; 1024];
|
||||
|
||||
@@ -30,11 +30,9 @@ Contract crates stay below implementation crates. Forbidden edges:
|
||||
| `security-governance -> rustfs` | Governance contracts stay below the binary crate |
|
||||
| `extension-schema -> rustfs` | The extension schema is consumed by the binary, never the reverse |
|
||||
| `extension-schema -> ecstore` | The extension schema must not reach storage internals |
|
||||
| `sse -> ecstore`, `sse -> iam`, `sse -> rustfs` | SSE key resolution reaches the running server only through its `SseHost` port |
|
||||
|
||||
- `rustfs-storage-api` exposes storage-facing replication status/state contracts only through `crates/storage-api/src/replication.rs`, so its temporary dependency on `rustfs-filemeta` wire types stays centralized and no `rustfs-replication` / `rustfs-storage-api` cycle appears.
|
||||
- Leaf crates (`config`, `credentials`, `crypto`, `io-metrics`, `madmin`) may not depend on other `rustfs-*` crates, in either TOML spelling, except the adjudicated edges pinned in the guard's leaf allowlist: `io-metrics -> rustfs-s3-ops` (pure contract crates sharing the `S3Operation` vocabulary) and `madmin -> rustfs-signer` (the SigV4-signed admin SDK client). A new leaf exception must be a pure contract dependency (types and enums only, no I/O, no globals, no non-contract internal dependencies) and land together with its allowlist entry.
|
||||
- `rustfs-sse` sits between the S3 layer and the storage engine and depends on neither. Its internal dependencies are pinned in the same guard to `rustfs-config`, `rustfs-kms`, `rustfs-policy`, `rustfs-s3-types`, and `rustfs-utils`; the KMS service, a bucket's default encryption, and key-permission decisions arrive through `rustfs_sse::SseHost`, which `rustfs/src/storage/sse.rs` implements. That file also owns every translation across the engine boundary (`ObjectEncryptionResolver`, `WriteEncryption`, `MultipartCompletionAuthorization`), so neither side names the other's types.
|
||||
- Compile-time source reads follow the same direction: `include_str!` / `include!` of a `.rs` file must not resolve outside the including crate (`scripts/check_layer_dependencies.sh`). Shared source-text expectations belong in a contract surface such as `rustfs_protos::compat_manifest` (`crates/protos/src/compat_manifest.rs`) and are asserted by each owning crate.
|
||||
|
||||
## ECStore Access Boundary
|
||||
|
||||
@@ -47,7 +47,7 @@ RustFS-side statics that matter architecturally because other modules are tempte
|
||||
| `KEYSTONE_AUTH`, `KEYSTONE_MAPPER`, `KEYSTONE_CONFIG` | `rustfs/src/auth_keystone.rs` | Keystone provider, mapper, and config stay private to the Keystone owner. |
|
||||
| `DEADLOCK_DETECTOR` | `rustfs/src/storage/deadlock_detector.rs` | Detector lifecycle stays private to the storage deadlock detector. |
|
||||
| `CONCURRENCY_MANAGER` | `rustfs/src/storage/concurrency/manager.rs` | Storage concurrency scheduler state stays inside the concurrency owner. |
|
||||
| `GLOBAL_KMS_DEK_PROVIDER`, `GLOBAL_SSE_DEK_PROVIDER` | `crates/sse/src/kms/provider.rs` | DEK provider caches stay private to the SSE owner. |
|
||||
| `GLOBAL_KMS_DEK_PROVIDER`, `GLOBAL_SSE_DEK_PROVIDER` | `rustfs/src/storage/sse.rs` | DEK provider caches stay private to the SSE owner. |
|
||||
| `ECSTORE_EVENT_DISPATCH_HOOK` | `rustfs/src/server/event.rs` | Event bridge registration goes through the storage facade. |
|
||||
| `AUDIT_MODULE_ENABLED`, `NOTIFY_MODULE_ENABLED` | `rustfs/src/module_switches.rs` | Module toggles are read through module-switch helpers; `MODULE_SWITCH_RMW_LOCK` (`rustfs/src/server/module_switch.rs`) serializes persisted updates. |
|
||||
| `RUNTIME_CONFIG_RELOAD_MUTEX` | `rustfs/src/admin/service/config.rs` | Serializes dynamic config reload fanout. |
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# KMS Bulk Rekey Job Contract
|
||||
|
||||
**Use this when:** changing the bulk envelope re-wrap sweep (`rustfs/src/kms_rekey.rs`), its admin endpoints (`rustfs/src/admin/handlers/kms_rekey.rs`), the re-wrap primitive, or anything that decides which objects a rekey may touch.
|
||||
**Source of truth:** `rustfs/src/kms_rekey.rs`, `rustfs/src/admin/handlers/kms_rekey.rs`, `rewrap_object_encryption_metadata` in `crates/sse/src/core/rewrap.rs`, `KmsManager::rewrap_data_key` / `KmsManager::describe_data_key_wrapping` in `crates/kms/src/manager.rs`, `put_object_metadata` in `crates/ecstore/src/set_disk/ops/object.rs`.
|
||||
**Source of truth:** `rustfs/src/kms_rekey.rs`, `rustfs/src/admin/handlers/kms_rekey.rs`, `rewrap_object_encryption_metadata` in `rustfs/src/storage/sse.rs`, `KmsManager::rewrap_data_key` / `KmsManager::describe_data_key_wrapping` in `crates/kms/src/manager.rs`, `put_object_metadata` in `crates/ecstore/src/set_disk/ops/object.rs`.
|
||||
|
||||
The bulk rekey job re-wraps stored data-key envelopes under the current key-encryption key (KEK) without rewriting object bodies. This document is the acceptance bar; where the shipped v1 sweep deliberately narrows it, [Implementation Status](#implementation-status-v1-sweep) records the deviation.
|
||||
|
||||
@@ -52,7 +52,7 @@ Idempotency comes from object metadata itself: the envelope's state is the targe
|
||||
|
||||
### Reading the wrapping KEK version
|
||||
|
||||
There is no key-version metadata key. Object metadata carries the key id (`x-rustfs-encryption-key-id`) and the sealed blob under `x-rustfs-encryption-key`; `DecryptResponse` in `crates/kms/src/types.rs` does not report a version either. The version is recoverable because the sealed blob is structured: for every backend that builds one, the ciphertext is the JSON of `DataKeyEnvelope` (`crates/kms/src/encryption/dek.rs`), and the read path already discriminates on it via `is_data_key_envelope` in `crates/sse/src/core/read.rs`.
|
||||
There is no key-version metadata key. Object metadata carries the key id (`x-rustfs-encryption-key-id`) and the sealed blob under `x-rustfs-encryption-key`; `DecryptResponse` in `crates/kms/src/types.rs` does not report a version either. The version is recoverable because the sealed blob is structured: for every backend that builds one, the ciphertext is the JSON of `DataKeyEnvelope` (`crates/kms/src/encryption/dek.rs`), and the read path already discriminates on it via `is_data_key_envelope` in `rustfs/src/storage/sse.rs`.
|
||||
|
||||
| Backend | Rotates | Where the wrapping version lives | Recoverable by a scan |
|
||||
|---|---|---|---|
|
||||
@@ -95,7 +95,7 @@ Replication destinations are unresolved: propagation depends on `rustfs/backlog#
|
||||
|
||||
Three properties of `put_object_metadata` (`crates/ecstore/src/set_disk/ops/object.rs`) constrain the re-wrap write:
|
||||
|
||||
- **The merge is additive; it cannot remove keys.** Overwriting a key that keeps its name is safe; a re-wrap that changes *which* keys describe the envelope leaves the old keys behind. This is a live hazard: `parse_minio_managed_sealed_key` in `crates/sse/src/core/keying/object.rs` selects the MinIO decrypt branch on the mere presence of the MinIO seal-algorithm header, so a RustFS-native envelope written onto MinIO-compatible headers without neutralizing them steers reads down the stale branch. Any envelope-shape change must neutralize superseded keys in the same write.
|
||||
- **The merge is additive; it cannot remove keys.** Overwriting a key that keeps its name is safe; a re-wrap that changes *which* keys describe the envelope leaves the old keys behind. This is a live hazard: `parse_minio_managed_sealed_key` in `rustfs/src/storage/sse.rs` selects the MinIO decrypt branch on the mere presence of the MinIO seal-algorithm header, so a RustFS-native envelope written onto MinIO-compatible headers without neutralizing them steers reads down the stale branch. Any envelope-shape change must neutralize superseded keys in the same write.
|
||||
- **Object-lock retention is enforced before the merge.** `check_object_lock_retention_update` (`crates/ecstore/src/set_disk/mod.rs`) runs first; rekey inherits its decision and must not acquire a bypass.
|
||||
- **`mod_time` is preserved unless the caller sets it.** The re-wrap path leaves it unset so age-based lifecycle rules are not perturbed.
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
# MinIO On-Disk Format Interoperability
|
||||
|
||||
**Use this when:** deciding whether a MinIO drive set, bucket-metadata blob, or SSE object can be read or imported by a given RustFS build, or before touching any constant or codec listed under Version Anchors.
|
||||
**Source of truth:** `crates/filemeta/src/filemeta.rs`, `crates/filemeta/src/filemeta/codec.rs`, `crates/ecstore/src/bucket/metadata.rs`, `crates/ecstore/src/bucket/migration.rs`, `crates/sse/src/core/`, `crates/sse/src/kms/`, `rustfs/Cargo.toml` `[features]`, `.github/workflows/ci.yml`, `.github/workflows/minio-interop.yml`.
|
||||
**Source of truth:** `crates/filemeta/src/filemeta.rs`, `crates/filemeta/src/filemeta/codec.rs`, `crates/ecstore/src/bucket/metadata.rs`, `crates/ecstore/src/bucket/migration.rs`, `rustfs/src/storage/sse.rs`, `rustfs/Cargo.toml` `[features]`, `.github/workflows/ci.yml`, `.github/workflows/minio-interop.yml`.
|
||||
|
||||
This is an interop contract, not a plan. Migration is one-way (MinIO to RustFS). Erasure-coding internals are owned by [erasure-coding.md](erasure-coding.md); this document owns the interop claim, the fixture evidence, and the out-of-scope list.
|
||||
|
||||
## Scope Matrix By Build Variant
|
||||
|
||||
Build variants are the `rustfs` crate features in `rustfs/Cargo.toml`: `default`, `full`, and `rio-v2` (which enables `rustfs-ecstore/rio-v2` and `rustfs-sse/rio-v2`, and pulls in `crates/rio-v2`). `rio-v2` is absent from both `default` and `full`.
|
||||
Build variants are the `rustfs` crate features in `rustfs/Cargo.toml`: `default`, `full`, and `rio-v2` (which enables `rustfs-ecstore/rio-v2` and pulls in `crates/rio-v2`). `rio-v2` is absent from both `default` and `full`.
|
||||
|
||||
| MinIO artifact | `default` / `full` build | `rio-v2` build | Notes |
|
||||
|---|:--:|:--:|---|
|
||||
@@ -81,7 +81,7 @@ The `xl.meta` around a MinIO SSE object parses in every build, so such objects l
|
||||
|
||||
### Seams
|
||||
|
||||
The cryptography (DARE v2 stream format, object-key derivation, sealing) was never the gap. Three metadata seams above it rejected MinIO objects; all are closed in `rio-v2` builds. Symbols are under `crates/sse/src/core/` unless noted.
|
||||
The cryptography (DARE v2 stream format, object-key derivation, sealing) was never the gap. Three metadata seams above it rejected MinIO objects; all are closed in `rio-v2` builds. Symbols are in `rustfs/src/storage/sse.rs` unless noted.
|
||||
|
||||
| Seam | Resolution |
|
||||
|---|---|
|
||||
@@ -113,10 +113,10 @@ Inventory the source first: bucket default encryption means objects can be encry
|
||||
| Fact | Value |
|
||||
|---|---|
|
||||
| Shipping status | Ships in no default build: absent from `default` and `full` in `rustfs/Cargo.toml`; released binaries and container images never include it. Enable with `--features rio-v2`. |
|
||||
| Pull-request coverage | `test-and-lint-rio-v2` in `.github/workflows/ci.yml`: clippy plus `cargo nextest` for `rustfs`, `rustfs-ecstore`, and `rustfs-sse` with `--features rio-v2`. This is the cfg-seam guard; it keeps the feature compiling and its unit suite green on every pull request. |
|
||||
| Pull-request coverage | `test-and-lint-rio-v2` in `.github/workflows/ci.yml`: clippy plus `cargo nextest` for `rustfs` and `rustfs-ecstore` with `--features rio-v2`. This is the cfg-seam guard; it keeps the feature compiling and its unit suite green on every pull request. |
|
||||
| Full-suite lane | `build-rustfs-debug-binary-rio-v2` and `e2e-tests-rio-v2` in `ci.yml` run only on the nightly schedule and manual dispatch (`cache-warm.yml` keeps the `ci-feat-rio` cache warm so the scheduled build fits its timeout). |
|
||||
| Interop evidence | `.github/workflows/minio-interop.yml` (nightly plus manual) regenerates real MinIO backend trees via `crates/rio-v2/tests/minio_fixture_lab/` and runs the `#[ignore]` reader tests in `rustfs/src/storage/minio_generated_read_test.rs` with `--features rio-v2`. Its freshness is tracked in `.github/scheduled-validations.json`. |
|
||||
| Promote-or-delete condition | The variant stays dormant until one of two things happens. **Promote**: a release commits to shipping MinIO SSE migration as a supported capability; then `rio-v2` joins `default`/`full`, the scheduled lanes run on every pull request, and this section is rewritten. **Delete**: no release commits to it and the scheduled lanes are not kept green; then the feature flag, `crates/rio-v2`, the cfg seams under `crates/sse/src/`, the three `ci.yml` jobs, the `cache-warm.yml` warm step, `minio-interop.yml`, and its `scheduled-validations.json` entry are removed in one change. Either outcome must update `ARCHITECTURE.md` and the `ci.yml` job comments that cite this section. |
|
||||
| Promote-or-delete condition | The variant stays dormant until one of two things happens. **Promote**: a release commits to shipping MinIO SSE migration as a supported capability; then `rio-v2` joins `default`/`full`, the scheduled lanes run on every pull request, and this section is rewritten. **Delete**: no release commits to it and the scheduled lanes are not kept green; then the feature flag, `crates/rio-v2`, the cfg seams in `rustfs/src/storage/sse.rs`, the three `ci.yml` jobs, the `cache-warm.yml` warm step, `minio-interop.yml`, and its `scheduled-validations.json` entry are removed in one change. Either outcome must update `ARCHITECTURE.md` and the `ci.yml` job comments that cite this section. |
|
||||
|
||||
## Fixture Evidence
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# AWS KMS backend: setup, verification, and operations
|
||||
|
||||
**Use this when:** connecting RustFS to AWS KMS for the first time, verifying an existing AWS-backed deployment, upgrading one from 1.0.4 or 1.0.5, or deciding how to run and protect the AWS keys RustFS depends on.
|
||||
**Source of truth:** `crates/kms/src/backends/aws.rs` (backend behavior and its deviations), `crates/kms/src/config.rs` (`ENV_KMS_AWS_*`), `crates/kms/src/policy.rs` (retry, deadline and circuit breaker), `crates/sse/src/core/` (SSE data path). 中文版: [kms-aws-backend-guide.zh-CN.md](kms-aws-backend-guide.zh-CN.md).
|
||||
**Source of truth:** `crates/kms/src/backends/aws.rs` (backend behavior and its deviations), `crates/kms/src/config.rs` (`ENV_KMS_AWS_*`), `crates/kms/src/policy.rs` (retry, deadline and circuit breaker), `rustfs/src/storage/sse.rs` (SSE data path). 中文版: [kms-aws-backend-guide.zh-CN.md](kms-aws-backend-guide.zh-CN.md).
|
||||
|
||||
The statements about behavior on this page were observed against the real AWS KMS service (region `eu-north-1`) and, for failures AWS cannot be asked to produce on demand, against a loopback stand-in that speaks the AWS KMS wire protocol. Where a statement is guidance that was not exercised, it says so. For the contract-level differences between AWS and the other backends, see [KMS backend security properties](kms-backend-security.md#aws-kms-deviations-from-the-shared-backend-contract).
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# AWS KMS 后端:接入、验证与运维
|
||||
|
||||
**适用场景:** 首次把 RustFS 接入 AWS KMS、验证已有的 AWS 后端部署、从 1.0.4 或 1.0.5 升级,或者规划如何运维并保护 RustFS 所依赖的 AWS 密钥。
|
||||
**事实来源:** `crates/kms/src/backends/aws.rs`(后端行为及其差异)、`crates/kms/src/config.rs`(`ENV_KMS_AWS_*`)、`crates/kms/src/policy.rs`(重试、超时与熔断)、`crates/sse/src/core/`(SSE 数据面)。English: [kms-aws-backend-guide.md](kms-aws-backend-guide.md)。
|
||||
**事实来源:** `crates/kms/src/backends/aws.rs`(后端行为及其差异)、`crates/kms/src/config.rs`(`ENV_KMS_AWS_*`)、`crates/kms/src/policy.rs`(重试、超时与熔断)、`rustfs/src/storage/sse.rs`(SSE 数据面)。English: [kms-aws-backend-guide.md](kms-aws-backend-guide.md)。
|
||||
|
||||
本页关于行为的描述,均来自对真实 AWS KMS 服务(区域 `eu-north-1`)的实测;对于无法让 AWS 按需产生的故障,则使用一个实现了 AWS KMS 线上协议的本机替身来实测。属于未经实测的建议之处,文中会明确标注。AWS 与其它后端在契约层面的差异,见 [KMS backend security properties](kms-backend-security.md#aws-kms-deviations-from-the-shared-backend-contract)。
|
||||
|
||||
|
||||
@@ -902,6 +902,42 @@ done
|
||||
|
||||
The `aggregated.scanner` payload preserves the same scanner progress, checkpoint, pacing, source work, maintenance control, lifecycle expiry, and lifecycle transition fields used by the local scanner status, but only for the responding node; `by_host.*.scanner` keeps that node's host view. Compare the per-node artifacts externally to find old active paths, partial checkpoints, pacing pressure, source-level control pressure, or downstream queue admission problems across the deployment.
|
||||
|
||||
## Reading Cached Bucket Statistics
|
||||
|
||||
Admin usage reads cache the selected scanner baseline for the existing 30-second TTL, then verify every returned bucket against uncached storage-owned incarnation metadata. Identity reads use at most four concurrent operations and a shared two-second deadline after releasing snapshot/refresh locks. Each bucket read linearizes independently against metadata transactions; the response is not an atomic snapshot of the entire namespace. Delayed peer invalidation cannot authorize a predecessor's statistics for a recreated bucket.
|
||||
|
||||
Missing, failed, timed-out, nil, mismatched, or legacy absent identities leave the bucket value absent (unknown), rather than returning the predecessor's value or a confirmed zero. Legacy statistics become visible after a scanner snapshot supplies the current identity. Account-info and usage display apply the existing single-process memory overlay before the final identity filter; distributed local counters are not promoted. When a projection omits values or changes an overlay, totals are partial and `usage_snapshot_converged=false`, and per-tier/replication aggregates are unavailable. Verified cold bucket values remain visible. Such response-only projections cannot be persisted, used for quota admission, or acknowledge dirty work.
|
||||
|
||||
## Reading Scanner Work Observations
|
||||
|
||||
`GET /rustfs/admin/v3/scanner/status` exposes `metrics.scan_observation` for the responding node. Capture timestamped snapshots on every node, including the scanner leader, and compare their deltas over the same workload window. These process-lifetime counters reset on restart; a missing field on an older node means unobserved. They are loaded independently and may briefly disagree while operations finish. This telemetry does not change scan cadence, cached usage reads, quota authority, dirty acknowledgement, or maintenance requirements.
|
||||
|
||||
| Field | Meaning |
|
||||
|---|---|
|
||||
| `metadata_reads.attempts` | Disk metadata read futures started after scanner node admission. Invalid paths and operations cancelled before starting the read do not count. |
|
||||
| `metadata_reads.succeeded`, `not_found`, `errors`, `cancelled` | Storage outcomes, including object/version disappearance and dropped read futures. Successful storage reads can still contain corrupt metadata; decoding and lifecycle failures do not become read errors. Cancellation observes the future, not proof that physical I/O stopped. |
|
||||
| `metadata_reads.bytes` | Total returned buffer lengths for successful reads, including inline data and corrupt metadata. Failed reads contribute no known bytes; this is not physical disk traffic. |
|
||||
| `metadata_reads.elapsed_nanos` | Sum of read-future elapsed time, including disk-permit or RPC waits, failures, and cancellation. Folder pacing, scanner node admission, decoding, and lifecycle work are outside this interval. Concurrent reads overlap, so the sum is not cycle wall time. |
|
||||
| `cache_reuse.clean_prefix` | Clean root subtrees copied under an admitted prefix scope by the non-erasure scanner. |
|
||||
| `cache_reuse.covered_namespace` | Namespace subtrees copied from validated checkpoint coverage. |
|
||||
| `cache_reuse.sampled_namespace` | Cached namespace subtrees copied when the existing periodic sampling selected other work. |
|
||||
| `cache_reuse.classified_namespace` | EC candidate namespace subtrees copied only after the current listing finished classifying them. Health/classification work still runs. |
|
||||
| `cache_reuse.*.count`, `elapsed_nanos` | Completed subtree reuse decisions and time spent copying/attaching their cache entries. Counts are not objects skipped or a cache hit ratio; times exclude classification/listing and are not total cache maintenance cost. |
|
||||
| `bucket_reuse.scoped_cold_bucket` | Whole cold buckets copied and attached to a successfully prepared scoped set cache. Count is copied buckets, not plans selected; later turn publication can still fail. Time covers copying/attachment only. |
|
||||
| `bucket_reuse.current_root` | Existing current bucket roots successfully sent to the local collector after watch, namespace lease and publication epoch checks. Time covers channel send only; failure or cancellation contributes no completed reuse. Collector acceptance and durable publication remain separate. |
|
||||
| `reuse_decisions.*.not_evaluated`, `blocked`, `eligible` | First conditions reached by production clean-prefix, checkpoint-coverage and namespace-sampling gates. Policy exclusions (`missing_scope`, `no_resume_sweep`, `deep_scan`, `forward_sweep`, `active_replication`) precede cache proof checks. `blocked` reports the first failed work/cache condition; later checks are not repeated for diagnostics. `eligible` does not certify a copy: an EC candidate can still require classification. |
|
||||
| `erasure_clean_prefix_walks` | Clean root children that still require walking because EC scans perform independent object-health work. |
|
||||
| `prefix_scope_resolution.evaluated`, `activated`, `blockers` | Proof evaluations retained by the final scope resolver, admitted proof gates, and every failed prerequisite. An admitted proof does not guarantee a reusable child exists; actual subtree copies appear in `cache_reuse`. Startup/required full scans, early scope rejection, and discarded peer/ACK resolutions do not count here. |
|
||||
| `prefix_cycle_completion.evaluated`, `activated`, `blockers` | End-of-cycle proof evaluations for subsequent reuse. An activated completion proof does not mean this cycle reused prefixes or that its later publication/persistence succeeded. One evaluation can increment several blocker counters. |
|
||||
| `scope_fallbacks` | Per-reason breakdown of the existing `usage_freshness.scope_fallback_total`; required full scans are excluded. |
|
||||
| `authoritative_baseline_rejections`, `observed_baseline_rejections` | First failed validation checks for `baseline_unavailable` fallback cycles, preserving the authoritative rejection when the observed fallback also fails. Missing/undecodable authority never reaches observed validation; `authoritative_identity` means it could not anchor an observed fallback. |
|
||||
|
||||
`freshness` describes the responding node's completed-cycle age, not whether every bucket has incorporated new writes. Read `metrics.usage_freshness` for node-local live pending work, guardrail vetoes, durable success and publication state; read `metrics.usage_publication` for the confirmed snapshot's pending count and publication class/outcome, and `cycle_schedule` for effective retry intervals and consecutive superseded cycles. Missing or unknown publication data does not establish zero pending work. A follower or a single node's empty dirty map cannot establish cluster-wide convergence.
|
||||
|
||||
Whole cold bucket copies, current-root reuse and the four subtree categories have different prerequisites and are not interchangeable hit rates. EC clean prefixes retain health walks. Deep or required full work must run, but a completed cache matching that exact work digest may satisfy a retry of the same cycle. A normal workload need not exercise current-root reuse or every subtree category; use controlled tests for positive emission evidence and capture real gate reasons when a VM window has zero copies.
|
||||
|
||||
The existing `life_time_ops.read_metadata` and `last_minute.actions.read_metadata` now include finished and cancelled read attempts; the latter reports count, accumulated whole seconds, and returned bytes in its existing window. Use `metadata_reads.elapsed_nanos` for subsecond read costs. Use read-count and byte deltas to assess work growth, and elapsed-time deltas to assess read-future cost. Compare those with reused-subtree deltas, scope fallback reasons, cycle wall time, pacing waits, and source work before attributing freshness delays to metadata I/O or proposing a scan optimization.
|
||||
|
||||
## Reading Lifecycle Transition Status
|
||||
|
||||
`metrics.lifecycle_transition`:
|
||||
|
||||
+1
-3
@@ -80,7 +80,7 @@ e2e-test-hooks = ["rustfs-ecstore/e2e-test-hooks", "rustfs-scanner/e2e-test-hook
|
||||
connect-e2e-short-credentials = []
|
||||
# Builds the dedicated rustfs-cli-e2e target with a build-time public enrollment root.
|
||||
offline-enrollment-e2e-root = []
|
||||
rio-v2 = ["rustfs-ecstore/rio-v2", "rustfs-sse/rio-v2"]
|
||||
rio-v2 = ["rustfs-ecstore/rio-v2"]
|
||||
# Linux-only cuObject S3-over-RDMA GET into client (GPU) memory; needs libcuobjserver.
|
||||
cuobject = ["dep:rustfs-cuobject", "rustfs-cuobject/server"]
|
||||
# NIXL/UCX transport for S3-over-RDMA; replaces cuobject when both are enabled.
|
||||
@@ -281,7 +281,6 @@ rustfs-rio = { workspace = true }
|
||||
rustfs-s3-client = { workspace = true }
|
||||
rustfs-s3-types = { workspace = true, features = ["compat-s3s"] }
|
||||
rustfs-s3-ops = { workspace = true }
|
||||
rustfs-sse = { workspace = true }
|
||||
rustfs-security-governance = { workspace = true }
|
||||
rustfs-data-usage = { workspace = true }
|
||||
rustfs-s3select-api = { workspace = true }
|
||||
@@ -425,7 +424,6 @@ h2.workspace = true
|
||||
rustfs-targets = { workspace = true, features = ["test-support"] }
|
||||
uuid = { workspace = true, features = ["v4", "v5", "fast-rng", "macro-diagnostics"] }
|
||||
rustfs-iam = { workspace = true, features = ["test-util"] }
|
||||
rustfs-sse = { workspace = true, features = ["test-util"] }
|
||||
serial_test = { workspace = true }
|
||||
tempfile = { workspace = true }
|
||||
aws-config = { workspace = true }
|
||||
|
||||
@@ -20,7 +20,7 @@ use crate::admin::service::caller_identity::oidc_profile_fields;
|
||||
use crate::admin::storage_api::bucket::versioning_sys::BucketVersioningSys;
|
||||
use crate::admin::storage_api::contract::admin::StorageAdminApi;
|
||||
use crate::admin::storage_api::contract::bucket::{BucketOperations, BucketOptions};
|
||||
use crate::admin::storage_api::data_usage::{apply_bucket_usage_memory_overlay, load_admin_data_usage_from_backend_cached};
|
||||
use crate::admin::storage_api::data_usage::load_admin_data_usage_with_memory_overlay;
|
||||
use crate::admin::storage_api::metadata_sys;
|
||||
use crate::auth::get_condition_values;
|
||||
use crate::server::ADMIN_PREFIX;
|
||||
@@ -278,8 +278,7 @@ impl Operation for AccountInfoHandler {
|
||||
// process-local absolute counters to cluster-wide bucket totals.
|
||||
// This path never triggers a live full-version listing
|
||||
// (rustfs/backlog#1306); freshness is owned by the scanner.
|
||||
let mut data_usage_info = map_data_usage_result(load_admin_data_usage_from_backend_cached(store.clone()).await)?;
|
||||
apply_bucket_usage_memory_overlay(&mut data_usage_info).await;
|
||||
let data_usage_info = map_data_usage_result(load_admin_data_usage_with_memory_overlay(store.clone()).await)?;
|
||||
account_info.usage_snapshot_converged = data_usage_info.usage_snapshot_converged;
|
||||
|
||||
for bucket in buckets.iter() {
|
||||
|
||||
@@ -8617,14 +8617,18 @@ impl Operation for SiteReplicationRepairHandler {
|
||||
.to_string(),
|
||||
None => Uuid::new_v4().to_string(),
|
||||
};
|
||||
let (status, headers, body) = execute_site_replication_repair(SiteReplicationRepairExecutionRequest {
|
||||
let response = execute_site_replication_repair(SiteReplicationRepairExecutionRequest {
|
||||
local_peer,
|
||||
preflight_token: supplied_token.to_string(),
|
||||
operation_id,
|
||||
signing_key,
|
||||
})
|
||||
.await?;
|
||||
Ok(AdminResponse::with_headers(status, body, headers))
|
||||
// The repair executor still answers with the s3s response type; the
|
||||
// router reads the status from the output tuple, so only the tuple and
|
||||
// the headers carry over to the owned response.
|
||||
let (status, body) = response.output;
|
||||
Ok(AdminResponse::with_headers(status, body.into(), response.headers))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13917,18 +13921,18 @@ mod tests {
|
||||
env!("CARGO_MANIFEST_DIR"),
|
||||
"/../crates/ecstore/tests/fixtures/bucket-config-goldens/lifecycle/minio.in.xml"
|
||||
));
|
||||
// Answer of s3s 5761ddfe (`Timestamp::parse(DateTime)` on this document's
|
||||
// ExpiryUpdatedAt). The owned axis must keep that instant.
|
||||
let legacy_axis =
|
||||
parse_lifecycle_date("2026-07-07T15:58:57.337315Z").expect("the legacy expiry axis is the minio.in.xml value");
|
||||
let mut old_reader = s3s::xml::Deserializer::new(raw);
|
||||
let old: s3s::dto::BucketLifecycleConfiguration =
|
||||
s3s::xml::Deserialize::deserialize(&mut old_reader).expect("read the actual old lifecycle expiry axis");
|
||||
old_reader.expect_eof().expect("the old expiry reader consumes the document");
|
||||
let old_axis = OffsetDateTime::from(old.expiry_updated_at.expect("the old config contains its expiry axis"));
|
||||
let new = deserialize(raw).expect("read the persisted lifecycle expiry axis");
|
||||
let new_axis = local_lifecycle_staleness_axis(Some(&new), OffsetDateTime::UNIX_EPOCH)
|
||||
.expect("the parsed lifecycle axis is readable");
|
||||
let incoming = parse_lifecycle_date("2026-07-07T15:58:57.337100Z").expect("the incoming expiry axis is valid");
|
||||
assert_eq!(new_axis, legacy_axis, "the parsed axis must keep the legacy submillisecond instant");
|
||||
assert_eq!(
|
||||
is_stale_update(new_axis, Some(incoming)),
|
||||
is_stale_update(legacy_axis, Some(incoming)),
|
||||
is_stale_update(old_axis, Some(incoming)),
|
||||
"a peer update older than the full-precision local axis must remain stale"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1021,32 +1021,6 @@ impl<T> S3Router<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Dispatches one legacy admin request. Callers keep their assertions and do not name s3s.
|
||||
/// Free function (not an inherent impl) so the `OwnedOperation` bound stays `pub(crate)`.
|
||||
#[cfg(test)]
|
||||
pub(crate) async fn call_legacy<T: OwnedOperation>(
|
||||
router: &S3Router<T>,
|
||||
method: Method,
|
||||
uri: &str,
|
||||
extensions: http::Extensions,
|
||||
) -> s3s::S3Result<S3Response<Body>> {
|
||||
S3Route::call(
|
||||
router,
|
||||
S3Request {
|
||||
input: Body::empty(),
|
||||
method,
|
||||
uri: uri.parse().expect("test URI"),
|
||||
headers: HeaderMap::new(),
|
||||
extensions,
|
||||
credentials: None,
|
||||
region: None,
|
||||
service: None,
|
||||
trailing_headers: None,
|
||||
},
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
impl<T> Default for S3Router<T> {
|
||||
fn default() -> Self {
|
||||
Self::new(false)
|
||||
|
||||
@@ -141,13 +141,15 @@ mod tests {
|
||||
object_store_from_envelope, publish_test_app_context,
|
||||
};
|
||||
use crate::admin::operation::{AdminRequest, AdminResponse, Operation};
|
||||
use crate::admin::router::{S3Router, call_legacy};
|
||||
use crate::app::{RequestEnvelope, RequestHead};
|
||||
use crate::admin::router::S3Router;
|
||||
use crate::app::RequestEnvelope;
|
||||
use hyper::{Method, StatusCode};
|
||||
use matchit::Params;
|
||||
use rustfs_iam::{store::object::ObjectStore, sys::IamSys};
|
||||
use rustfs_kms::KmsServiceManager;
|
||||
use rustfs_s3_types::{S3ErrorCode, s3_error};
|
||||
use s3s::route::S3Route;
|
||||
use s3s::{Body, S3Request};
|
||||
use std::sync::Arc;
|
||||
|
||||
struct UnreadyIam;
|
||||
@@ -190,18 +192,22 @@ mod tests {
|
||||
Arc::new(AppContext::new(context.object_store(), Arc::new(UnreadyIam), Arc::new(TestKms)))
|
||||
}
|
||||
|
||||
fn envelope(extensions: http::Extensions) -> RequestEnvelope {
|
||||
RequestEnvelope::from_parts(
|
||||
RequestHead {
|
||||
method: Method::GET,
|
||||
uri: "/context-probe".parse().expect("test URI"),
|
||||
headers: http::HeaderMap::new(),
|
||||
credentials: None,
|
||||
region: None,
|
||||
service: None,
|
||||
},
|
||||
fn request(extensions: http::Extensions) -> S3Request<Body> {
|
||||
S3Request {
|
||||
input: Body::empty(),
|
||||
method: Method::GET,
|
||||
uri: "/context-probe".parse().expect("test URI"),
|
||||
headers: http::HeaderMap::new(),
|
||||
extensions,
|
||||
)
|
||||
credentials: None,
|
||||
region: None,
|
||||
service: None,
|
||||
trailing_headers: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn envelope(extensions: http::Extensions) -> RequestEnvelope {
|
||||
RequestEnvelope::from_s3s(&request(extensions))
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -323,13 +329,15 @@ mod tests {
|
||||
.insert(Method::GET, "/context-probe", ContextDependentAdminRoute)
|
||||
.expect("register test route");
|
||||
|
||||
let err = call_legacy(&router, Method::GET, "/context-probe", http::Extensions::new())
|
||||
let err = router
|
||||
.call(request(http::Extensions::new()))
|
||||
.await
|
||||
.expect_err("empty slot must fail closed");
|
||||
assert_eq!(err.code(), &S3ErrorCode::ServiceUnavailable.into());
|
||||
|
||||
assert!(slot.install(distinct_context(&ambient)));
|
||||
let response = call_legacy(&router, Method::GET, "/context-probe", http::Extensions::new())
|
||||
let response = router
|
||||
.call(request(http::Extensions::new()))
|
||||
.await
|
||||
.expect("installed slot must serve request");
|
||||
assert_eq!(response.status, Some(StatusCode::NO_CONTENT));
|
||||
|
||||
@@ -945,10 +945,6 @@ pub(crate) static ERR_TIER_RESERVED_NAME: AdminErrorRef = AdminErrorRef(|| &ecst
|
||||
pub(crate) mod data_usage {
|
||||
use std::sync::Arc;
|
||||
|
||||
pub(crate) async fn apply_bucket_usage_memory_overlay(data_usage_info: &mut rustfs_data_usage::DataUsageInfo) {
|
||||
crate::storage::storage_api::ecstore_data_usage::apply_bucket_usage_memory_overlay(data_usage_info).await;
|
||||
}
|
||||
|
||||
pub(crate) async fn load_data_usage_from_backend(
|
||||
store: Arc<crate::storage::storage_api::ECStore>,
|
||||
) -> Result<rustfs_data_usage::DataUsageInfo, crate::storage::storage_api::StorageError> {
|
||||
@@ -960,6 +956,12 @@ pub(crate) mod data_usage {
|
||||
) -> Result<rustfs_data_usage::DataUsageInfo, crate::storage::storage_api::StorageError> {
|
||||
crate::storage::storage_api::ecstore_data_usage::load_admin_data_usage_from_backend_cached(store).await
|
||||
}
|
||||
|
||||
pub(crate) async fn load_admin_data_usage_with_memory_overlay(
|
||||
store: Arc<crate::storage::storage_api::ECStore>,
|
||||
) -> Result<rustfs_data_usage::DataUsageInfo, crate::storage::storage_api::StorageError> {
|
||||
crate::storage::storage_api::ecstore_data_usage::load_admin_data_usage_with_memory_overlay(store).await
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) mod access {
|
||||
|
||||
@@ -21,9 +21,7 @@ use super::storage_api::admin_usecase::capacity::{
|
||||
};
|
||||
use super::storage_api::admin_usecase::contract::StorageAdminApi;
|
||||
use super::storage_api::admin_usecase::contract::bucket::{BucketOperations as _, BucketOptions};
|
||||
use super::storage_api::admin_usecase::data_usage::{
|
||||
apply_bucket_usage_memory_overlay, load_admin_data_usage_from_backend_cached,
|
||||
};
|
||||
use super::storage_api::admin_usecase::data_usage::load_admin_data_usage_with_memory_overlay;
|
||||
use super::storage_api::admin_usecase::{ECStore, EndpointServerPools};
|
||||
use crate::app::runtime_sources::{
|
||||
AppContext, current_app_context, current_endpoints_handle, current_object_store_handle_for_context,
|
||||
@@ -272,7 +270,7 @@ impl DefaultAdminUsecase {
|
||||
info.buckets_usage.retain(|bucket, _| current_buckets.contains(bucket));
|
||||
info.bucket_sizes.retain(|bucket, _| current_buckets.contains(bucket));
|
||||
|
||||
if info.buckets_usage.is_empty() && !current_buckets.is_empty() {
|
||||
if info.buckets_usage.is_empty() && !current_buckets.is_empty() && !info.usage_snapshot_admin_projection {
|
||||
*info = DataUsageInfo::default();
|
||||
return;
|
||||
}
|
||||
@@ -287,19 +285,14 @@ impl DefaultAdminUsecase {
|
||||
}
|
||||
}
|
||||
|
||||
/// Overlay single-process counts on a persisted snapshot and narrow it
|
||||
/// for display.
|
||||
///
|
||||
/// Whether the snapshot measured anything is decided before the overlay.
|
||||
/// The overlay clears `usage_snapshot_complete` when it holds counts it
|
||||
/// cannot reconcile with the snapshot, such as writes into a bucket that
|
||||
/// has no verified baseline. That makes the totals unconverged, not the
|
||||
/// scanned buckets unusable; treating it as structural incompleteness
|
||||
/// blanked usage for every bucket for as long as one such bucket kept
|
||||
/// receiving writes, because each write restarts its scanner fence.
|
||||
async fn present_data_usage_snapshot(mut info: DataUsageInfo, buckets: impl IntoIterator<Item = String>) -> DataUsageInfo {
|
||||
let snapshot_measured = info.is_complete_bucket_usage_snapshot() || info.is_valid_partial_snapshot();
|
||||
apply_bucket_usage_memory_overlay(&mut info).await;
|
||||
/// Keep already identity-bound display values without accepting a display
|
||||
/// projection as a persistable or quota-authoritative snapshot.
|
||||
fn present_identity_bound_data_usage_snapshot(
|
||||
mut info: DataUsageInfo,
|
||||
buckets: impl IntoIterator<Item = String>,
|
||||
) -> DataUsageInfo {
|
||||
let snapshot_measured =
|
||||
info.usage_snapshot_admin_projection || info.is_complete_bucket_usage_snapshot() || info.is_valid_partial_snapshot();
|
||||
Self::narrow_data_usage_snapshot_to_measured_buckets(&mut info, snapshot_measured, buckets);
|
||||
info
|
||||
}
|
||||
@@ -345,7 +338,7 @@ impl DefaultAdminUsecase {
|
||||
/// This path never triggers a live full-version listing
|
||||
/// (rustfs/backlog#1306); freshness is owned by the scanner.
|
||||
pub(crate) async fn query_data_usage_info_with_store(store: Arc<ECStore>) -> AdminUsecaseResult<DataUsageInfo> {
|
||||
let info = Self::map_data_usage_load_result(load_admin_data_usage_from_backend_cached(store.clone()).await)?;
|
||||
let info = Self::map_data_usage_load_result(load_admin_data_usage_with_memory_overlay(store.clone()).await)?;
|
||||
let buckets = store
|
||||
.list_bucket(&BucketOptions {
|
||||
cached: true,
|
||||
@@ -354,7 +347,7 @@ impl DefaultAdminUsecase {
|
||||
})
|
||||
.await
|
||||
.map_err(|_| Self::app_error(S3ErrorCode::InternalError, "list_bucket failed"))?;
|
||||
let mut info = Self::present_data_usage_snapshot(info, buckets.into_iter().map(|bucket| bucket.name)).await;
|
||||
let mut info = Self::present_identity_bound_data_usage_snapshot(info, buckets.into_iter().map(|bucket| bucket.name));
|
||||
|
||||
let storage_info = StorageAdminApi::storage_info(store.as_ref()).await;
|
||||
|
||||
@@ -979,6 +972,52 @@ mod tests {
|
||||
assert_eq!(info.buckets_count, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn identity_bound_partial_display_keeps_valid_buckets_and_unknown_state() {
|
||||
use std::collections::HashMap;
|
||||
|
||||
let mut projected = DataUsageInfo {
|
||||
last_update: Some(std::time::SystemTime::UNIX_EPOCH),
|
||||
usage_snapshot_admin_projection: true,
|
||||
usage_snapshot_partial: true,
|
||||
usage_snapshot_converged: Some(false),
|
||||
buckets_count: 1,
|
||||
objects_total_count: 7,
|
||||
objects_total_size: 70,
|
||||
buckets_usage: HashMap::from([(
|
||||
"cold".to_string(),
|
||||
rustfs_data_usage::BucketUsageInfo {
|
||||
size: 70,
|
||||
objects_count: 7,
|
||||
..Default::default()
|
||||
},
|
||||
)]),
|
||||
bucket_sizes: HashMap::from([("cold".to_string(), 70)]),
|
||||
..Default::default()
|
||||
};
|
||||
projected = DefaultAdminUsecase::present_identity_bound_data_usage_snapshot(
|
||||
projected,
|
||||
["cold".to_string(), "recreated".to_string()],
|
||||
);
|
||||
assert_eq!(projected.buckets_usage["cold"].objects_count, 7);
|
||||
assert!(!projected.buckets_usage.contains_key("recreated"));
|
||||
assert_eq!(projected.objects_total_count, 7);
|
||||
assert_eq!(projected.usage_snapshot_converged, Some(false));
|
||||
assert!(!projected.is_complete_bucket_usage_snapshot());
|
||||
assert!(!projected.is_valid_partial_snapshot());
|
||||
|
||||
projected.buckets_usage.clear();
|
||||
projected.bucket_sizes.clear();
|
||||
projected.calculate_totals();
|
||||
projected.buckets_count = 0;
|
||||
let unknown = DefaultAdminUsecase::present_identity_bound_data_usage_snapshot(projected, ["recreated".to_string()]);
|
||||
assert!(unknown.buckets_usage.is_empty());
|
||||
assert!(unknown.usage_snapshot_admin_projection);
|
||||
assert_eq!(unknown.usage_snapshot_converged, Some(false));
|
||||
assert!(!unknown.is_complete_bucket_usage_snapshot());
|
||||
assert_eq!(unknown.last_update, Some(std::time::SystemTime::UNIX_EPOCH));
|
||||
}
|
||||
|
||||
/// A write into a bucket without a verified baseline leaves process-local
|
||||
/// counts the overlay cannot reconcile, so it clears
|
||||
/// `usage_snapshot_complete`. That must flag the response non-converged,
|
||||
@@ -1009,11 +1048,15 @@ mod tests {
|
||||
|
||||
crate::app::storage_api::test::data_usage::record_bucket_object_write_memory(unverified_bucket, None, 1_024).await;
|
||||
|
||||
let info = DefaultAdminUsecase::present_data_usage_snapshot(
|
||||
// Exercise the production overlay, followed by the same identity-bound
|
||||
// presentation function used by the admin query.
|
||||
crate::app::storage_api::test::data_usage::apply_bucket_usage_memory_overlay(&mut persisted).await;
|
||||
persisted.usage_snapshot_admin_projection = true;
|
||||
persisted.usage_snapshot_converged = Some(false);
|
||||
let info = DefaultAdminUsecase::present_identity_bound_data_usage_snapshot(
|
||||
persisted,
|
||||
[measured_bucket.to_string(), unverified_bucket.to_string()],
|
||||
)
|
||||
.await;
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
info.buckets_usage
|
||||
|
||||
@@ -1320,22 +1320,23 @@ mod tests {
|
||||
};
|
||||
for (index, expected_key) in ["a&local", "z-local"].into_iter().enumerate() {
|
||||
let request_marker = input.marker.clone().unwrap_or_default();
|
||||
let list_req = S3Request {
|
||||
input: input.clone(),
|
||||
method: http::Method::GET,
|
||||
uri: http::Uri::from_static("/"),
|
||||
headers: HeaderMap::new(),
|
||||
extensions: http::Extensions::new(),
|
||||
credentials: None,
|
||||
region: None,
|
||||
service: None,
|
||||
trailing_headers: None,
|
||||
};
|
||||
let envelope = crate::app::RequestEnvelope::from_s3s(&list_req);
|
||||
let gateway_input =
|
||||
rustfs_gateway_types::compat::s3s_5761ddfe::generated::ops::list_objects::input_from_s3s(
|
||||
input.clone(),
|
||||
list_req.input,
|
||||
)
|
||||
.expect("convert ListObjectsInput");
|
||||
let envelope = crate::app::RequestEnvelope::from_parts(
|
||||
crate::app::RequestHead {
|
||||
method: http::Method::GET,
|
||||
uri: http::Uri::from_static("/"),
|
||||
headers: HeaderMap::new(),
|
||||
credentials: None,
|
||||
region: None,
|
||||
service: None,
|
||||
},
|
||||
http::Extensions::new(),
|
||||
);
|
||||
let gateway_output = tokio::time::timeout(
|
||||
Duration::from_secs(10),
|
||||
DefaultBucketUsecase::from_global().execute_list_objects(envelope, gateway_input),
|
||||
|
||||
@@ -509,7 +509,7 @@ impl DefaultBucketUsecase {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::app::bucket_usecase::test_support::{build_request_with_req_info, owned_envelope};
|
||||
use crate::app::bucket_usecase::test_support::{build_request, build_request_with_req_info};
|
||||
use crate::app::storage_api::s3::dto::CreateBucketInput;
|
||||
use http::Method;
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||
@@ -1005,10 +1005,11 @@ mod tests {
|
||||
|
||||
#[tokio::test]
|
||||
async fn execute_list_buckets_returns_internal_error_when_store_uninitialized() {
|
||||
let req = build_request((), Method::GET);
|
||||
let usecase = DefaultBucketUsecase::without_context();
|
||||
|
||||
let err = usecase
|
||||
.execute_list_buckets(owned_envelope(Method::GET), gateway_dto::ListBucketsInput::default())
|
||||
.execute_list_buckets(RequestEnvelope::from_s3s(&req), gateway_dto::ListBucketsInput::default())
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert_eq!(err.code(), &rustfs_s3_types::S3ErrorCode::InternalError);
|
||||
|
||||
@@ -366,7 +366,7 @@ impl DefaultBucketUsecase {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::app::bucket_usecase::test_support::{build_request_with_req_info, owned_envelope, s3_op_total};
|
||||
use crate::app::bucket_usecase::test_support::{build_request, build_request_with_req_info, s3_op_total};
|
||||
use http::Method;
|
||||
|
||||
#[test]
|
||||
@@ -399,10 +399,11 @@ mod tests {
|
||||
|
||||
/// The envelope of a request whose access phase installed `req_info`, or none.
|
||||
fn envelope(req_info: Option<ReqInfo>) -> RequestEnvelope {
|
||||
match req_info {
|
||||
Some(req_info) => RequestEnvelope::from_s3s(&build_request_with_req_info((), Method::PUT, req_info)),
|
||||
None => owned_envelope(Method::PUT),
|
||||
}
|
||||
let req = match req_info {
|
||||
Some(req_info) => build_request_with_req_info((), Method::PUT, req_info),
|
||||
None => build_request((), Method::PUT),
|
||||
};
|
||||
RequestEnvelope::from_s3s(&req)
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -203,7 +203,7 @@ impl DefaultBucketUsecase {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::app::bucket_usecase::test_support::owned_envelope;
|
||||
use crate::app::bucket_usecase::test_support::build_request;
|
||||
use crate::app::storage_api::s3::dto::{self, PutBucketEncryptionInput, ServerSideEncryption};
|
||||
use http::Method;
|
||||
use rustfs_gateway_types::persistence::{
|
||||
@@ -292,19 +292,20 @@ mod tests {
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
let prepared = crate::storage::persisted_from_s3s(&input.server_side_encryption_configuration);
|
||||
let req = build_request(input, Method::PUT);
|
||||
let prepared = crate::storage::persisted_from_s3s(&req.input.server_side_encryption_configuration);
|
||||
let usecase = DefaultBucketUsecase::without_context();
|
||||
|
||||
let err = usecase
|
||||
.execute_put_bucket_encryption(owned_envelope(Method::PUT), &input.bucket, prepared.map_err(Into::into))
|
||||
.execute_put_bucket_encryption(RequestEnvelope::from_s3s(&req), &req.input.bucket, prepared.map_err(Into::into))
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert_eq!(err.code(), &S3ErrorCode::InternalError);
|
||||
|
||||
let err = usecase
|
||||
.execute_put_bucket_encryption(
|
||||
owned_envelope(Method::PUT),
|
||||
&input.bucket,
|
||||
RequestEnvelope::from_s3s(&req),
|
||||
&req.input.bucket,
|
||||
Ok(PersistedBucketEncryptionConfiguration { rules: vec![] }),
|
||||
)
|
||||
.await
|
||||
|
||||
@@ -405,7 +405,7 @@ mod tests {
|
||||
// waiter again on the single-thread test runtime.
|
||||
assert_eq!(admission.available_permits(), 1);
|
||||
}
|
||||
use crate::app::bucket_usecase::test_support::{build_request, owned_envelope};
|
||||
use crate::app::bucket_usecase::test_support::build_request;
|
||||
use crate::app::gating_test_env::{run_large_stack_test, shared_gating_ambient};
|
||||
use crate::app::runtime_sources::ServerContextSlot;
|
||||
use crate::app::storage_api::bucket_usecase::helper::MutationCompletionProbe;
|
||||
@@ -775,12 +775,13 @@ mod tests {
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
let req = build_request(input, Method::PUT);
|
||||
let usecase = DefaultBucketUsecase::without_context();
|
||||
|
||||
let err = usecase
|
||||
.execute_put_bucket_lifecycle_configuration(
|
||||
owned_envelope(Method::PUT),
|
||||
&input.bucket,
|
||||
RequestEnvelope::from_s3s(&req),
|
||||
&req.input.bucket,
|
||||
PreparedLifecycleConfiguration::Missing,
|
||||
)
|
||||
.await
|
||||
|
||||
@@ -159,7 +159,7 @@ fn list_object_versions_output(
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::app::bucket_usecase::test_support::owned_envelope;
|
||||
use crate::app::bucket_usecase::test_support::build_request;
|
||||
use rustfs_gateway_types::BucketName;
|
||||
use rustfs_s3_types::S3ErrorCode;
|
||||
|
||||
@@ -319,7 +319,7 @@ mod tests {
|
||||
};
|
||||
|
||||
let err = DefaultBucketUsecase::without_context()
|
||||
.execute_list_object_versions(owned_envelope(http::Method::GET), input)
|
||||
.execute_list_object_versions(RequestEnvelope::from_s3s(&build_request((), http::Method::GET)), input)
|
||||
.await
|
||||
.expect_err("a negative page size");
|
||||
assert_eq!(err.code(), &S3ErrorCode::InvalidArgument);
|
||||
@@ -333,7 +333,7 @@ mod tests {
|
||||
};
|
||||
|
||||
let err = DefaultBucketUsecase::without_context()
|
||||
.execute_list_object_versions(owned_envelope(http::Method::GET), input)
|
||||
.execute_list_object_versions(RequestEnvelope::from_s3s(&build_request((), http::Method::GET)), input)
|
||||
.await
|
||||
.expect_err("no store");
|
||||
assert_eq!(err.code(), &S3ErrorCode::InternalError);
|
||||
|
||||
@@ -372,12 +372,13 @@ fn list_objects_next_marker(info: &ListObjectsV2Info, cursor: Option<&str>) -> O
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::app::bucket_usecase::test_support::owned_envelope;
|
||||
use crate::app::bucket_usecase::test_support::build_request;
|
||||
use rustfs_gateway_types::BucketName;
|
||||
|
||||
#[test]
|
||||
#[serial_test::serial]
|
||||
fn list_apis_report_storage_read_quorum_as_unavailable() {
|
||||
use crate::app::bucket_usecase::test_support::owned_envelope;
|
||||
use crate::app::gating_test_env::{run_large_stack_test, shared_gating_ambient, shared_gating_ecstore};
|
||||
run_large_stack_test("list-quorum-surface", || async {
|
||||
let usecase = DefaultBucketUsecase::with_context(Some(shared_gating_ambient().await));
|
||||
@@ -484,7 +485,7 @@ mod tests {
|
||||
}
|
||||
|
||||
fn envelope() -> RequestEnvelope {
|
||||
owned_envelope(http::Method::GET)
|
||||
RequestEnvelope::from_s3s(&build_request((), http::Method::GET))
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user