Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `capsule-registry` no longer reads `SPACE_MASTER_KEY` or `SPACE_ALLOW_PLAINTEXT` (#199). `registry_pipeline_from_log`, `registry_pipeline_from_storage_root`, `registry_pipeline_from_fs_backend`, `WritePipeline::open` and `S3View::new` take a `StoreSecurity`, holding the key, if any, and whether an encrypted store may take unencrypted writes without it, so the choice shows where a store is opened. `spacectl` and `serve-s3` read both variables once, before opening the store, and behave as before, except that `SPACE_ALLOW_PLAINTEXT` set to anything but `1` is now refused even with a key. `registry_pipeline_from_env` is gone, and `WritePipeline::new` opens without a key. The tests no longer change the process environment.
- `modular_pipeline::spawn_scrub` is the only way to start a background scrub, so every one runs the capsule pass (#218). `RegistryPipelineHandle::spawn_scrub`, which ran the same scrub without it, is gone.
- `common` no longer has the `Event` enum, which nothing built or matched, and the repo no longer tracks a `space.audit.log` left by a pre-reset build (#217). There's no audit log.
- `common` no longer depends on `chrono`, which nothing in it used (#237). That takes `chrono`, `iana-time-zone` and their platform crates out of every build.
- `SPACE_MASTER_KEY` is read one way everywhere (#193). `KeyManager::from_env`, which `spacectl` and `serve-s3` use, and `EnvKeyProvider` both take 64 hex characters, optionally after `0x` and with whitespace around them, so a key with a trailing newline now works. `EnvKeyProvider` no longer takes a 32-character value as raw bytes, and a short or non-hex key gets the same error from both, saying it must be 64 hex characters. A value that isn't valid text used to be reported as not set. `FileKeyProvider` still takes a key file holding the 32 raw bytes.
- `common` no longer has the pre-reset `erasure` and `qos` modules, and `capsule-registry` no longer has `PipelineError` and `DedupError` (#204). Nothing used them, and they described erasure coding, QoS admission control and typed pipeline errors the store doesn't have. `common` drops its tokio dependency and `capsule-registry` its thiserror one with them.
- An S3 PUT checks its body against the digests its headers give: `Content-MD5`, `x-amz-checksum-crc32`, `-crc32c`, `-crc64nvme`, `-sha1` and `-sha256`, and a hex `x-amz-content-sha256` (#196). A body that doesn't match gets `400 BadDigest` (`XAmzContentSHA256Mismatch` for the SigV4 payload hash) and stores nothing. A PUT asking for Object Lock, server-side encryption options, tags, ACLs other than `private`, a storage class other than `STANDARD`, a website redirect or another checksum algorithm gets `501 NotImplemented`. All of these used to be ignored with a 200. The digests use the RustCrypto `md-5` and `sha1` crates, pinned in the workspace; the CRCs are computed in the crate.
Expand Down Expand Up @@ -79,6 +80,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `NOTICE` said SPACE was licensed under Apache-2.0; it now matches `LICENSE` (BUSL-1.1).
- `spacectl create --policy-file` refuses unknown keys, so a misspelling such as `dedup: false` is an error instead of leaving dedup on. Settings the file leaves out keep the default policy, which is encrypted when `SPACE_MASTER_KEY` is set; they used to fall back to an unencrypted one. docs/capsule.md shows the format (#161).
- `Policy::validate`, which every write runs, refuses settings nothing implements: `CryptoProfile::HybridKyber`, a `transform`, `federation`, and layout strategies other than `Fixed`. A capsule written with HybridKyber used to be classical XTS, or plaintext without a key. A policy's `layout` block no longer needs `ec_profile` and `heat_threshold` (#162).
- `Policy::validate` refuses a compression level outside the range the compressor uses: LZ4 1 to 16, Zstd -5 to 22 (#236). `spacectl create --policy-file` with `!Zstd { level: 99 }` used to store the capsule, clamping the level with a warning but keeping 99 in its policy. It now fails with `invalid policy file`, naming the level and the range, before anything is written.
- Background scrub covers the whole store each interval. A cycle that reached `max_segments_per_cycle` used to leave the rest until the next interval, so a store of more than 1024 segments was only partly scrubbed. Segments deleted during a cycle are no longer reported as read errors, and the schedule forgets deleted segments rather than growing forever. `ScrubResult::MetadataMismatch` names its segment.
- `NoopEncryptor`, the encryptor used without `SPACE_MASTER_KEY`, refuses `compute_mac` and `verify_mac` with the same error as `decrypt`. They returned an empty tag and passed any bytes. Nothing called them yet (#176).
- Removed `common::StorageStub`, the record of the pre-reset cold tiering path. Nothing used it (#151).
Expand Down
107 changes: 0 additions & 107 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ blake3 = { workspace = true }
anyhow = { workspace = true }
futures = { workspace = true }
bytes = { workspace = true }
chrono = { version = "0.4", features = ["serde"] }

[dev-dependencies]
serde_norway = { workspace = true }
91 changes: 89 additions & 2 deletions crates/common/src/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ pub enum CryptoProfile {
pub enum CompressionPolicy {
/// No compression
None,
/// LZ4 fast compression (level 1-16)
/// LZ4 fast compression (level 1 to 16)
LZ4 { level: i32 },
/// Zstd balanced compression (level 1-22)
/// Zstd balanced compression (level -5 to 22)
Zstd { level: i32 },
}

/// The LZ4 levels a policy may ask for, which the compressor uses as given.
pub const LZ4_LEVELS: std::ops::RangeInclusive<i32> = 1..=16;
/// The Zstd levels a policy may ask for, which the compressor uses as given.
pub const ZSTD_LEVELS: std::ops::RangeInclusive<i32> = -5..=22;

impl Default for CompressionPolicy {
fn default() -> Self {
// Default to LZ4 level 1 for speed
Expand Down Expand Up @@ -440,6 +445,18 @@ impl Policy {
if size == 0 || size > MAX_SEGMENT_SIZE {
return Err(PolicyError::SegmentSizeOutOfRange { size });
}
let (algorithm, level, levels) = match self.compression {
CompressionPolicy::None => return Ok(()),
CompressionPolicy::LZ4 { level } => ("LZ4", level, LZ4_LEVELS),
CompressionPolicy::Zstd { level } => ("Zstd", level, ZSTD_LEVELS),
};
if !levels.contains(&level) {
return Err(PolicyError::CompressionLevelOutOfRange {
algorithm,
level,
levels,
});
}
Ok(())
}
}
Expand All @@ -456,6 +473,12 @@ pub enum PolicyError {
SegmentSizeOutOfRange {
size: usize,
},
/// A compression level outside the range the compressor uses (#236).
CompressionLevelOutOfRange {
algorithm: &'static str,
level: i32,
levels: std::ops::RangeInclusive<i32>,
},
}

impl std::fmt::Display for PolicyError {
Expand All @@ -469,6 +492,16 @@ impl std::fmt::Display for PolicyError {
"segment size {size} is out of range: it must be between 1 byte and \
{MAX_SEGMENT_SIZE} bytes (16 MiB)"
),
PolicyError::CompressionLevelOutOfRange {
algorithm,
level,
levels,
} => write!(
f,
"{algorithm} level {level} is out of range: it must be between {} and {}",
levels.start(),
levels.end()
),
}
}
}
Expand Down Expand Up @@ -702,6 +735,60 @@ mod tests {
assert!(Policy::default().validate().is_ok());
}

/// A level the compressor would clamp is refused, so the policy a
/// capsule stores is the one it was written with (#236).
#[test]
fn compression_levels_must_be_in_the_codecs_range() {
let with = |compression| Policy {
compression,
..Policy::default()
};
for level in [*LZ4_LEVELS.start(), 4, *LZ4_LEVELS.end()] {
assert!(with(CompressionPolicy::LZ4 { level }).validate().is_ok());
}
for level in [*ZSTD_LEVELS.start(), 0, 3, *ZSTD_LEVELS.end()] {
assert!(with(CompressionPolicy::Zstd { level }).validate().is_ok());
}
assert!(with(CompressionPolicy::None).validate().is_ok());

for (compression, algorithm, level) in [
(CompressionPolicy::LZ4 { level: 0 }, "LZ4", 0),
(CompressionPolicy::LZ4 { level: 17 }, "LZ4", 17),
(CompressionPolicy::Zstd { level: -6 }, "Zstd", -6),
(CompressionPolicy::Zstd { level: 99 }, "Zstd", 99),
(CompressionPolicy::Zstd { level: -5000 }, "Zstd", -5000),
] {
let err = with(compression).validate().unwrap_err();
assert!(
matches!(
&err,
PolicyError::CompressionLevelOutOfRange { algorithm: a, level: l, .. }
if *a == algorithm && *l == level
),
"{err:?}"
);
}
assert_eq!(
with(CompressionPolicy::Zstd { level: 99 })
.validate()
.unwrap_err()
.to_string(),
"Zstd level 99 is out of range: it must be between -5 and 22"
);

// The presets pass.
for policy in [
Policy::default(),
Policy::encrypted(),
Policy::encrypted_compressed(),
Policy::text_optimized(),
Policy::precompressed(),
Policy::edge_optimized(),
] {
assert!(policy.validate().is_ok(), "{:?}", policy.compression);
}
}

#[test]
fn classical_with_dedup_is_accepted() {
let policy = Policy {
Expand Down
8 changes: 5 additions & 3 deletions crates/compression/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::io::Write;

use anyhow::{Context, Result};
use common::{
policy::{LZ4_LEVELS, ZSTD_LEVELS},
traits::{CompressionSummary, Compressor},
CompressionPolicy,
};
Expand Down Expand Up @@ -153,11 +154,12 @@ pub fn decompress_zstd(data: &[u8]) -> CompressionOpResult<Vec<u8>> {
Ok(decompressed)
}

/// Adjust compression level based on policy
/// Clamp a level to the range the codec takes. `Policy::validate` refuses
/// levels outside it, so only a policy that skipped validation is clamped.
fn adjusted_level(level: i32, algorithm: &'static str) -> CompressionOpResult<i32> {
let clamped = match algorithm {
"lz4" => level.clamp(1, 16),
"zstd" => level.clamp(-5, 22),
"lz4" => level.clamp(*LZ4_LEVELS.start(), *LZ4_LEVELS.end()),
"zstd" => level.clamp(*ZSTD_LEVELS.start(), *ZSTD_LEVELS.end()),
_ => level,
};

Expand Down
5 changes: 5 additions & 0 deletions crates/spacectl/tests/cli_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,11 @@ fn policy_files_refuse_unknown_and_unsupported_settings() {
"layout:\n strategy: AdaptiveEntropy\n",
"a layout strategy other than Fixed is not supported",
),
(
"level.yaml",
"compression: !Zstd { level: 99 }\n",
"Zstd level 99 is out of range",
),
] {
std::fs::write(dir.join(name), yaml).unwrap();
let out = spacectl(&dir, &["create", "-f", "a.txt", "--policy-file", name]);
Expand Down
Loading