mirror of
https://github.com/rustfs/rustfs.git
synced 2026-07-31 10:32:24 +00:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 57d196771a | |||
| 6202f50e15 | |||
| c5df1f92c2 | |||
| 4f1770d3fe | |||
| d56cee26db |
Generated
+412
-110
File diff suppressed because it is too large
Load Diff
+31
-15
@@ -33,10 +33,12 @@ members = [
|
||||
"crates/s3select-api", # S3 Select API interface
|
||||
"crates/s3select-query", # S3 Select query engine
|
||||
"crates/signer", # client signer
|
||||
"crates/checksums", # client checksums
|
||||
"crates/utils", # Utility functions and helpers
|
||||
"crates/workers", # Worker thread pools and task scheduling
|
||||
"crates/zip", # ZIP file handling and compression
|
||||
"crates/ahm",
|
||||
"crates/mcp", # MCP server for S3 operations
|
||||
]
|
||||
resolver = "2"
|
||||
|
||||
@@ -84,8 +86,11 @@ rustfs-utils = { path = "crates/utils", version = "0.0.5" }
|
||||
rustfs-rio = { path = "crates/rio", version = "0.0.5" }
|
||||
rustfs-filemeta = { path = "crates/filemeta", version = "0.0.5" }
|
||||
rustfs-signer = { path = "crates/signer", version = "0.0.5" }
|
||||
rustfs-checksums = { path = "crates/checksums", version = "0.0.5" }
|
||||
rustfs-workers = { path = "crates/workers", version = "0.0.5" }
|
||||
rustfs-mcp = { path = "crates/mcp", version = "0.0.5" }
|
||||
aes-gcm = { version = "0.10.3", features = ["std"] }
|
||||
anyhow = "1.0.98"
|
||||
arc-swap = "1.7.1"
|
||||
argon2 = { version = "0.5.3", features = ["std"] }
|
||||
atoi = "2.0.0"
|
||||
@@ -94,7 +99,8 @@ async-recursion = "1.1.1"
|
||||
async-trait = "0.1.88"
|
||||
async-compression = { version = "0.4.0" }
|
||||
atomic_enum = "0.3.0"
|
||||
aws-sdk-s3 = "1.96.0"
|
||||
aws-config = { version = "1.8.3" }
|
||||
aws-sdk-s3 = "1.100.0"
|
||||
axum = "0.8.4"
|
||||
axum-extra = "0.10.1"
|
||||
axum-server = { version = "0.7.2", features = ["tls-rustls"] }
|
||||
@@ -104,11 +110,13 @@ brotli = "8.0.1"
|
||||
bytes = { version = "1.10.1", features = ["serde"] }
|
||||
bytesize = "2.0.1"
|
||||
byteorder = "1.5.0"
|
||||
bytes-utils = "0.1.4"
|
||||
cfg-if = "1.0.1"
|
||||
crc-fast = "1.3.0"
|
||||
chacha20poly1305 = { version = "0.10.1" }
|
||||
chrono = { version = "0.4.41", features = ["serde"] }
|
||||
clap = { version = "4.5.41", features = ["derive", "env"] }
|
||||
const-str = { version = "0.6.3", features = ["std", "proc"] }
|
||||
clap = { version = "4.5.42", features = ["derive", "env"] }
|
||||
const-str = { version = "0.6.4", features = ["std", "proc"] }
|
||||
crc32fast = "1.5.0"
|
||||
criterion = { version = "0.5", features = ["html_reports"] }
|
||||
dashmap = "6.1.0"
|
||||
@@ -141,7 +149,7 @@ http-body = "1.0.1"
|
||||
humantime = "2.2.0"
|
||||
ipnetwork = { version = "0.21.1", features = ["serde"] }
|
||||
jsonwebtoken = "9.3.1"
|
||||
keyring = { version = "3.6.2", features = [
|
||||
keyring = { version = "3.6.3", features = [
|
||||
"apple-native",
|
||||
"windows-native",
|
||||
"sync-secret-service",
|
||||
@@ -182,7 +190,8 @@ blake3 = { version = "1.8.2" }
|
||||
pbkdf2 = "0.12.2"
|
||||
percent-encoding = "2.3.1"
|
||||
pin-project-lite = "0.2.16"
|
||||
prost = "0.13.5"
|
||||
prost = "0.14.1"
|
||||
pretty_assertions = "1.4.1"
|
||||
quick-xml = "0.38.0"
|
||||
rand = "0.9.2"
|
||||
rdkafka = { version = "0.38.0", features = ["tokio"] }
|
||||
@@ -201,6 +210,7 @@ rfd = { version = "0.15.4", default-features = false, features = [
|
||||
"xdg-portal",
|
||||
"tokio",
|
||||
] }
|
||||
rmcp = { version = "0.3.1" }
|
||||
rmp = "0.8.14"
|
||||
rmp-serde = "1.3.0"
|
||||
rsa = "0.9.8"
|
||||
@@ -208,17 +218,18 @@ rumqttc = { version = "0.24" }
|
||||
rust-embed = { version = "8.7.2" }
|
||||
rust-i18n = { version = "3.1.5" }
|
||||
rustfs-rsc = "2025.506.1"
|
||||
rustls = { version = "0.23.29" }
|
||||
rustls = { version = "0.23.31" }
|
||||
rustls-pki-types = "1.12.0"
|
||||
rustls-pemfile = "2.2.0"
|
||||
s3s = { version = "0.12.0-minio-preview.2" }
|
||||
shadow-rs = { version = "1.2.0", default-features = false }
|
||||
s3s = { version = "0.12.0-minio-preview.3" }
|
||||
schemars = "1.0.4"
|
||||
serde = { version = "1.0.219", features = ["derive"] }
|
||||
serde_json = { version = "1.0.141", features = ["raw_value"] }
|
||||
serde-xml-rs = "0.8.1"
|
||||
serde_urlencoded = "0.7.1"
|
||||
serial_test = "3.2.0"
|
||||
sha1 = "0.10.6"
|
||||
sha2 = "0.10.9"
|
||||
shadow-rs = { version = "1.2.0", default-features = false }
|
||||
siphasher = "1.0.1"
|
||||
smallvec = { version = "1.15.1", features = ["serde"] }
|
||||
snafu = "0.8.6"
|
||||
@@ -238,22 +249,24 @@ time = { version = "0.3.41", features = [
|
||||
"macros",
|
||||
"serde",
|
||||
] }
|
||||
tokio = { version = "1.46.1", features = ["fs", "rt-multi-thread"] }
|
||||
tokio = { version = "1.47.0", features = ["fs", "rt-multi-thread"] }
|
||||
tokio-rustls = { version = "0.26.2", default-features = false }
|
||||
tokio-stream = { version = "0.1.17" }
|
||||
tokio-tar = "0.3.1"
|
||||
tokio-test = "0.4.4"
|
||||
tokio-util = { version = "0.7.15", features = ["io", "compat"] }
|
||||
tonic = { version = "0.13.1", features = ["gzip"] }
|
||||
tonic-build = { version = "0.13.1" }
|
||||
tonic = { version = "0.14.0", features = ["gzip"] }
|
||||
tonic-prost = { version = "0.14.0" }
|
||||
tonic-prost-build = { version = "0.14.0" }
|
||||
tower = { version = "0.5.2", features = ["timeout"] }
|
||||
tower-http = { version = "0.6.6", features = ["cors"] }
|
||||
tracing = "0.1.41"
|
||||
tracing-appender = "0.2.3"
|
||||
tracing-core = "0.1.34"
|
||||
tracing-error = "0.2.1"
|
||||
tracing-subscriber = { version = "0.3.19", features = ["env-filter", "time"] }
|
||||
tracing-appender = "0.2.3"
|
||||
tracing-opentelemetry = "0.31.0"
|
||||
tracing-subscriber = { version = "0.3.19", features = ["env-filter", "time"] }
|
||||
tracing-test = "0.2.5"
|
||||
transform-stream = "0.3.1"
|
||||
url = "2.5.4"
|
||||
urlencoding = "2.1.3"
|
||||
@@ -267,7 +280,10 @@ winapi = { version = "0.3.9" }
|
||||
xxhash-rust = { version = "0.8.15", features = ["xxh64", "xxh3"] }
|
||||
zip = "2.4.2"
|
||||
zstd = "0.13.3"
|
||||
anyhow = "1.0.98"
|
||||
|
||||
|
||||
[workspace.metadata.cargo-shear]
|
||||
ignored = ["rustfs", "rust-i18n"]
|
||||
|
||||
[profile.wasm-dev]
|
||||
inherits = "dev"
|
||||
|
||||
@@ -371,7 +371,7 @@ impl ServiceManager {
|
||||
StdCommand::new("taskkill")
|
||||
.arg("/F")
|
||||
.arg("/PID")
|
||||
.arg(&service_pid.to_string())
|
||||
.arg(service_pid.to_string())
|
||||
.output()?;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
# 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-checksums"
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
repository.workspace = true
|
||||
rust-version.workspace = true
|
||||
version.workspace = true
|
||||
homepage.workspace = true
|
||||
description = "Checksum calculation and verification callbacks for HTTP request and response bodies sent by service clients generated by RustFS, ensuring data integrity and authenticity."
|
||||
keywords = ["checksum-calculation", "verification", "integrity", "authenticity", "rustfs"]
|
||||
categories = ["web-programming", "development-tools", "network-programming"]
|
||||
documentation = "https://docs.rs/rustfs-signer/latest/rustfs_checksum/"
|
||||
|
||||
[dependencies]
|
||||
bytes = { workspace = true }
|
||||
crc-fast = { workspace = true }
|
||||
hex = { workspace = true }
|
||||
http = { workspace = true }
|
||||
http-body = { workspace = true }
|
||||
base64-simd = { workspace = true }
|
||||
md-5 = { workspace = true }
|
||||
pin-project-lite = { workspace = true }
|
||||
sha1 = { workspace = true }
|
||||
sha2 = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
bytes-utils = { workspace = true }
|
||||
pretty_assertions = { workspace = true }
|
||||
tracing-test = { workspace = true }
|
||||
tokio = { workspace = true, features = ["macros", "rt"] }
|
||||
@@ -0,0 +1,3 @@
|
||||
# rustfs-checksums
|
||||
|
||||
Checksum calculation and verification callbacks for HTTP request and response bodies sent by service clients generated by RustFS object storage.
|
||||
@@ -0,0 +1,44 @@
|
||||
// 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.
|
||||
#![allow(dead_code)]
|
||||
|
||||
use base64_simd::STANDARD;
|
||||
use std::error::Error;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct DecodeError(base64_simd::Error);
|
||||
|
||||
impl Error for DecodeError {
|
||||
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
||||
Some(&self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for DecodeError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "failed to decode base64")
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn decode(input: impl AsRef<str>) -> Result<Vec<u8>, DecodeError> {
|
||||
STANDARD.decode_to_vec(input.as_ref()).map_err(DecodeError)
|
||||
}
|
||||
|
||||
pub(crate) fn encode(input: impl AsRef<[u8]>) -> String {
|
||||
STANDARD.encode_to_string(input.as_ref())
|
||||
}
|
||||
|
||||
pub(crate) fn encoded_length(length: usize) -> usize {
|
||||
STANDARD.encoded_length(length)
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
// 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.
|
||||
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct UnknownChecksumAlgorithmError {
|
||||
checksum_algorithm: String,
|
||||
}
|
||||
|
||||
impl UnknownChecksumAlgorithmError {
|
||||
pub(crate) fn new(checksum_algorithm: impl Into<String>) -> Self {
|
||||
Self {
|
||||
checksum_algorithm: checksum_algorithm.into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn checksum_algorithm(&self) -> &str {
|
||||
&self.checksum_algorithm
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for UnknownChecksumAlgorithmError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
r#"unknown checksum algorithm "{}", please pass a known algorithm name ("crc32", "crc32c", "sha1", "sha256", "md5")"#,
|
||||
self.checksum_algorithm
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for UnknownChecksumAlgorithmError {}
|
||||
@@ -0,0 +1,196 @@
|
||||
// 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.
|
||||
|
||||
use crate::base64;
|
||||
use http::header::{HeaderMap, HeaderValue};
|
||||
|
||||
use crate::Crc64Nvme;
|
||||
use crate::{CRC_32_C_NAME, CRC_32_NAME, CRC_64_NVME_NAME, Checksum, Crc32, Crc32c, Md5, SHA_1_NAME, SHA_256_NAME, Sha1, Sha256};
|
||||
|
||||
pub const CRC_32_HEADER_NAME: &str = "x-amz-checksum-crc32";
|
||||
pub const CRC_32_C_HEADER_NAME: &str = "x-amz-checksum-crc32c";
|
||||
pub const SHA_1_HEADER_NAME: &str = "x-amz-checksum-sha1";
|
||||
pub const SHA_256_HEADER_NAME: &str = "x-amz-checksum-sha256";
|
||||
pub const CRC_64_NVME_HEADER_NAME: &str = "x-amz-checksum-crc64nvme";
|
||||
|
||||
pub(crate) static MD5_HEADER_NAME: &str = "content-md5";
|
||||
|
||||
pub const CHECKSUM_ALGORITHMS_IN_PRIORITY_ORDER: [&str; 5] =
|
||||
[CRC_64_NVME_NAME, CRC_32_C_NAME, CRC_32_NAME, SHA_1_NAME, SHA_256_NAME];
|
||||
|
||||
pub trait HttpChecksum: Checksum + Send + Sync {
|
||||
fn headers(self: Box<Self>) -> HeaderMap<HeaderValue> {
|
||||
let mut header_map = HeaderMap::new();
|
||||
header_map.insert(self.header_name(), self.header_value());
|
||||
|
||||
header_map
|
||||
}
|
||||
|
||||
fn header_name(&self) -> &'static str;
|
||||
|
||||
fn header_value(self: Box<Self>) -> HeaderValue {
|
||||
let hash = self.finalize();
|
||||
HeaderValue::from_str(&base64::encode(&hash[..])).expect("base64 encoded bytes are always valid header values")
|
||||
}
|
||||
|
||||
fn size(&self) -> u64 {
|
||||
let trailer_name_size_in_bytes = self.header_name().len();
|
||||
let base64_encoded_checksum_size_in_bytes = base64::encoded_length(Checksum::size(self) as usize);
|
||||
|
||||
let size = trailer_name_size_in_bytes + ":".len() + base64_encoded_checksum_size_in_bytes;
|
||||
|
||||
size as u64
|
||||
}
|
||||
}
|
||||
|
||||
impl HttpChecksum for Crc32 {
|
||||
fn header_name(&self) -> &'static str {
|
||||
CRC_32_HEADER_NAME
|
||||
}
|
||||
}
|
||||
|
||||
impl HttpChecksum for Crc32c {
|
||||
fn header_name(&self) -> &'static str {
|
||||
CRC_32_C_HEADER_NAME
|
||||
}
|
||||
}
|
||||
|
||||
impl HttpChecksum for Crc64Nvme {
|
||||
fn header_name(&self) -> &'static str {
|
||||
CRC_64_NVME_HEADER_NAME
|
||||
}
|
||||
}
|
||||
|
||||
impl HttpChecksum for Sha1 {
|
||||
fn header_name(&self) -> &'static str {
|
||||
SHA_1_HEADER_NAME
|
||||
}
|
||||
}
|
||||
|
||||
impl HttpChecksum for Sha256 {
|
||||
fn header_name(&self) -> &'static str {
|
||||
SHA_256_HEADER_NAME
|
||||
}
|
||||
}
|
||||
|
||||
impl HttpChecksum for Md5 {
|
||||
fn header_name(&self) -> &'static str {
|
||||
MD5_HEADER_NAME
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::base64;
|
||||
use bytes::Bytes;
|
||||
|
||||
use crate::{CRC_32_C_NAME, CRC_32_NAME, CRC_64_NVME_NAME, ChecksumAlgorithm, SHA_1_NAME, SHA_256_NAME};
|
||||
|
||||
use super::HttpChecksum;
|
||||
|
||||
#[test]
|
||||
fn test_trailer_length_of_crc32_checksum_body() {
|
||||
let checksum = CRC_32_NAME.parse::<ChecksumAlgorithm>().unwrap().into_impl();
|
||||
let expected_size = 29;
|
||||
let actual_size = HttpChecksum::size(&*checksum);
|
||||
assert_eq!(expected_size, actual_size)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_trailer_value_of_crc32_checksum_body() {
|
||||
let checksum = CRC_32_NAME.parse::<ChecksumAlgorithm>().unwrap().into_impl();
|
||||
// The CRC32 of an empty string is all zeroes
|
||||
let expected_value = Bytes::from_static(b"\0\0\0\0");
|
||||
let expected_value = base64::encode(&expected_value);
|
||||
let actual_value = checksum.header_value();
|
||||
assert_eq!(expected_value, actual_value)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_trailer_length_of_crc32c_checksum_body() {
|
||||
let checksum = CRC_32_C_NAME.parse::<ChecksumAlgorithm>().unwrap().into_impl();
|
||||
let expected_size = 30;
|
||||
let actual_size = HttpChecksum::size(&*checksum);
|
||||
assert_eq!(expected_size, actual_size)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_trailer_value_of_crc32c_checksum_body() {
|
||||
let checksum = CRC_32_C_NAME.parse::<ChecksumAlgorithm>().unwrap().into_impl();
|
||||
// The CRC32C of an empty string is all zeroes
|
||||
let expected_value = Bytes::from_static(b"\0\0\0\0");
|
||||
let expected_value = base64::encode(&expected_value);
|
||||
let actual_value = checksum.header_value();
|
||||
assert_eq!(expected_value, actual_value)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_trailer_length_of_crc64nvme_checksum_body() {
|
||||
let checksum = CRC_64_NVME_NAME.parse::<ChecksumAlgorithm>().unwrap().into_impl();
|
||||
let expected_size = 37;
|
||||
let actual_size = HttpChecksum::size(&*checksum);
|
||||
assert_eq!(expected_size, actual_size)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_trailer_value_of_crc64nvme_checksum_body() {
|
||||
let checksum = CRC_64_NVME_NAME.parse::<ChecksumAlgorithm>().unwrap().into_impl();
|
||||
// The CRC64NVME of an empty string is all zeroes
|
||||
let expected_value = Bytes::from_static(b"\0\0\0\0\0\0\0\0");
|
||||
let expected_value = base64::encode(&expected_value);
|
||||
let actual_value = checksum.header_value();
|
||||
assert_eq!(expected_value, actual_value)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_trailer_length_of_sha1_checksum_body() {
|
||||
let checksum = SHA_1_NAME.parse::<ChecksumAlgorithm>().unwrap().into_impl();
|
||||
let expected_size = 48;
|
||||
let actual_size = HttpChecksum::size(&*checksum);
|
||||
assert_eq!(expected_size, actual_size)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_trailer_value_of_sha1_checksum_body() {
|
||||
let checksum = SHA_1_NAME.parse::<ChecksumAlgorithm>().unwrap().into_impl();
|
||||
// The SHA1 of an empty string is da39a3ee5e6b4b0d3255bfef95601890afd80709
|
||||
let expected_value = Bytes::from_static(&[
|
||||
0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07,
|
||||
0x09,
|
||||
]);
|
||||
let expected_value = base64::encode(&expected_value);
|
||||
let actual_value = checksum.header_value();
|
||||
assert_eq!(expected_value, actual_value)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_trailer_length_of_sha256_checksum_body() {
|
||||
let checksum = SHA_256_NAME.parse::<ChecksumAlgorithm>().unwrap().into_impl();
|
||||
let expected_size = 66;
|
||||
let actual_size = HttpChecksum::size(&*checksum);
|
||||
assert_eq!(expected_size, actual_size)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_trailer_value_of_sha256_checksum_body() {
|
||||
let checksum = SHA_256_NAME.parse::<ChecksumAlgorithm>().unwrap().into_impl();
|
||||
let expected_value = Bytes::from_static(&[
|
||||
0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41,
|
||||
0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
|
||||
]);
|
||||
let expected_value = base64::encode(&expected_value);
|
||||
let actual_value = checksum.header_value();
|
||||
assert_eq!(expected_value, actual_value)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,446 @@
|
||||
// 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.
|
||||
|
||||
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
||||
#![allow(clippy::derive_partial_eq_without_eq)]
|
||||
#![warn(
|
||||
// missing_docs,
|
||||
rustdoc::missing_crate_level_docs,
|
||||
unreachable_pub,
|
||||
rust_2018_idioms
|
||||
)]
|
||||
|
||||
use crate::error::UnknownChecksumAlgorithmError;
|
||||
|
||||
use bytes::Bytes;
|
||||
use std::{fmt::Debug, str::FromStr};
|
||||
|
||||
mod base64;
|
||||
pub mod error;
|
||||
pub mod http;
|
||||
|
||||
pub const CRC_32_NAME: &str = "crc32";
|
||||
pub const CRC_32_C_NAME: &str = "crc32c";
|
||||
pub const CRC_64_NVME_NAME: &str = "crc64nvme";
|
||||
pub const SHA_1_NAME: &str = "sha1";
|
||||
pub const SHA_256_NAME: &str = "sha256";
|
||||
pub const MD5_NAME: &str = "md5";
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
#[non_exhaustive]
|
||||
pub enum ChecksumAlgorithm {
|
||||
#[default]
|
||||
Crc32,
|
||||
Crc32c,
|
||||
#[deprecated]
|
||||
Md5,
|
||||
Sha1,
|
||||
Sha256,
|
||||
Crc64Nvme,
|
||||
}
|
||||
|
||||
impl FromStr for ChecksumAlgorithm {
|
||||
type Err = UnknownChecksumAlgorithmError;
|
||||
|
||||
fn from_str(checksum_algorithm: &str) -> Result<Self, Self::Err> {
|
||||
if checksum_algorithm.eq_ignore_ascii_case(CRC_32_NAME) {
|
||||
Ok(Self::Crc32)
|
||||
} else if checksum_algorithm.eq_ignore_ascii_case(CRC_32_C_NAME) {
|
||||
Ok(Self::Crc32c)
|
||||
} else if checksum_algorithm.eq_ignore_ascii_case(SHA_1_NAME) {
|
||||
Ok(Self::Sha1)
|
||||
} else if checksum_algorithm.eq_ignore_ascii_case(SHA_256_NAME) {
|
||||
Ok(Self::Sha256)
|
||||
} else if checksum_algorithm.eq_ignore_ascii_case(MD5_NAME) {
|
||||
// MD5 is now an alias for the default Crc32 since it is deprecated
|
||||
Ok(Self::Crc32)
|
||||
} else if checksum_algorithm.eq_ignore_ascii_case(CRC_64_NVME_NAME) {
|
||||
Ok(Self::Crc64Nvme)
|
||||
} else {
|
||||
Err(UnknownChecksumAlgorithmError::new(checksum_algorithm))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ChecksumAlgorithm {
|
||||
pub fn into_impl(self) -> Box<dyn http::HttpChecksum> {
|
||||
match self {
|
||||
Self::Crc32 => Box::<Crc32>::default(),
|
||||
Self::Crc32c => Box::<Crc32c>::default(),
|
||||
Self::Crc64Nvme => Box::<Crc64Nvme>::default(),
|
||||
#[allow(deprecated)]
|
||||
Self::Md5 => Box::<Crc32>::default(),
|
||||
Self::Sha1 => Box::<Sha1>::default(),
|
||||
Self::Sha256 => Box::<Sha256>::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Crc32 => CRC_32_NAME,
|
||||
Self::Crc32c => CRC_32_C_NAME,
|
||||
Self::Crc64Nvme => CRC_64_NVME_NAME,
|
||||
#[allow(deprecated)]
|
||||
Self::Md5 => MD5_NAME,
|
||||
Self::Sha1 => SHA_1_NAME,
|
||||
Self::Sha256 => SHA_256_NAME,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Checksum: Send + Sync {
|
||||
fn update(&mut self, bytes: &[u8]);
|
||||
fn finalize(self: Box<Self>) -> Bytes;
|
||||
fn size(&self) -> u64;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Crc32 {
|
||||
hasher: crc_fast::Digest,
|
||||
}
|
||||
|
||||
impl Default for Crc32 {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
hasher: crc_fast::Digest::new(crc_fast::CrcAlgorithm::Crc32IsoHdlc),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Crc32 {
|
||||
fn update(&mut self, bytes: &[u8]) {
|
||||
self.hasher.update(bytes);
|
||||
}
|
||||
|
||||
fn finalize(self) -> Bytes {
|
||||
let checksum = self.hasher.finalize() as u32;
|
||||
|
||||
Bytes::copy_from_slice(checksum.to_be_bytes().as_slice())
|
||||
}
|
||||
|
||||
fn size() -> u64 {
|
||||
4
|
||||
}
|
||||
}
|
||||
|
||||
impl Checksum for Crc32 {
|
||||
fn update(&mut self, bytes: &[u8]) {
|
||||
Self::update(self, bytes)
|
||||
}
|
||||
fn finalize(self: Box<Self>) -> Bytes {
|
||||
Self::finalize(*self)
|
||||
}
|
||||
fn size(&self) -> u64 {
|
||||
Self::size()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Crc32c {
|
||||
hasher: crc_fast::Digest,
|
||||
}
|
||||
|
||||
impl Default for Crc32c {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
hasher: crc_fast::Digest::new(crc_fast::CrcAlgorithm::Crc32Iscsi),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Crc32c {
|
||||
fn update(&mut self, bytes: &[u8]) {
|
||||
self.hasher.update(bytes);
|
||||
}
|
||||
|
||||
fn finalize(self) -> Bytes {
|
||||
let checksum = self.hasher.finalize() as u32;
|
||||
|
||||
Bytes::copy_from_slice(checksum.to_be_bytes().as_slice())
|
||||
}
|
||||
|
||||
fn size() -> u64 {
|
||||
4
|
||||
}
|
||||
}
|
||||
|
||||
impl Checksum for Crc32c {
|
||||
fn update(&mut self, bytes: &[u8]) {
|
||||
Self::update(self, bytes)
|
||||
}
|
||||
fn finalize(self: Box<Self>) -> Bytes {
|
||||
Self::finalize(*self)
|
||||
}
|
||||
fn size(&self) -> u64 {
|
||||
Self::size()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Crc64Nvme {
|
||||
hasher: crc_fast::Digest,
|
||||
}
|
||||
|
||||
impl Default for Crc64Nvme {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
hasher: crc_fast::Digest::new(crc_fast::CrcAlgorithm::Crc64Nvme),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Crc64Nvme {
|
||||
fn update(&mut self, bytes: &[u8]) {
|
||||
self.hasher.update(bytes);
|
||||
}
|
||||
|
||||
fn finalize(self) -> Bytes {
|
||||
Bytes::copy_from_slice(self.hasher.finalize().to_be_bytes().as_slice())
|
||||
}
|
||||
|
||||
fn size() -> u64 {
|
||||
8
|
||||
}
|
||||
}
|
||||
|
||||
impl Checksum for Crc64Nvme {
|
||||
fn update(&mut self, bytes: &[u8]) {
|
||||
Self::update(self, bytes)
|
||||
}
|
||||
fn finalize(self: Box<Self>) -> Bytes {
|
||||
Self::finalize(*self)
|
||||
}
|
||||
fn size(&self) -> u64 {
|
||||
Self::size()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct Sha1 {
|
||||
hasher: sha1::Sha1,
|
||||
}
|
||||
|
||||
impl Sha1 {
|
||||
fn update(&mut self, bytes: &[u8]) {
|
||||
use sha1::Digest;
|
||||
self.hasher.update(bytes);
|
||||
}
|
||||
|
||||
fn finalize(self) -> Bytes {
|
||||
use sha1::Digest;
|
||||
Bytes::copy_from_slice(self.hasher.finalize().as_slice())
|
||||
}
|
||||
|
||||
fn size() -> u64 {
|
||||
use sha1::Digest;
|
||||
sha1::Sha1::output_size() as u64
|
||||
}
|
||||
}
|
||||
|
||||
impl Checksum for Sha1 {
|
||||
fn update(&mut self, bytes: &[u8]) {
|
||||
Self::update(self, bytes)
|
||||
}
|
||||
|
||||
fn finalize(self: Box<Self>) -> Bytes {
|
||||
Self::finalize(*self)
|
||||
}
|
||||
fn size(&self) -> u64 {
|
||||
Self::size()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct Sha256 {
|
||||
hasher: sha2::Sha256,
|
||||
}
|
||||
|
||||
impl Sha256 {
|
||||
fn update(&mut self, bytes: &[u8]) {
|
||||
use sha2::Digest;
|
||||
self.hasher.update(bytes);
|
||||
}
|
||||
|
||||
fn finalize(self) -> Bytes {
|
||||
use sha2::Digest;
|
||||
Bytes::copy_from_slice(self.hasher.finalize().as_slice())
|
||||
}
|
||||
|
||||
fn size() -> u64 {
|
||||
use sha2::Digest;
|
||||
sha2::Sha256::output_size() as u64
|
||||
}
|
||||
}
|
||||
|
||||
impl Checksum for Sha256 {
|
||||
fn update(&mut self, bytes: &[u8]) {
|
||||
Self::update(self, bytes);
|
||||
}
|
||||
fn finalize(self: Box<Self>) -> Bytes {
|
||||
Self::finalize(*self)
|
||||
}
|
||||
fn size(&self) -> u64 {
|
||||
Self::size()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct Md5 {
|
||||
hasher: md5::Md5,
|
||||
}
|
||||
|
||||
impl Md5 {
|
||||
fn update(&mut self, bytes: &[u8]) {
|
||||
use md5::Digest;
|
||||
self.hasher.update(bytes);
|
||||
}
|
||||
|
||||
fn finalize(self) -> Bytes {
|
||||
use md5::Digest;
|
||||
Bytes::copy_from_slice(self.hasher.finalize().as_slice())
|
||||
}
|
||||
|
||||
fn size() -> u64 {
|
||||
use md5::Digest;
|
||||
md5::Md5::output_size() as u64
|
||||
}
|
||||
}
|
||||
|
||||
impl Checksum for Md5 {
|
||||
fn update(&mut self, bytes: &[u8]) {
|
||||
Self::update(self, bytes)
|
||||
}
|
||||
fn finalize(self: Box<Self>) -> Bytes {
|
||||
Self::finalize(*self)
|
||||
}
|
||||
fn size(&self) -> u64 {
|
||||
Self::size()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{
|
||||
Crc32, Crc32c, Md5, Sha1, Sha256,
|
||||
http::{CRC_32_C_HEADER_NAME, CRC_32_HEADER_NAME, MD5_HEADER_NAME, SHA_1_HEADER_NAME, SHA_256_HEADER_NAME},
|
||||
};
|
||||
|
||||
use crate::ChecksumAlgorithm;
|
||||
use crate::http::HttpChecksum;
|
||||
|
||||
use crate::base64;
|
||||
use http::HeaderValue;
|
||||
use pretty_assertions::assert_eq;
|
||||
use std::fmt::Write;
|
||||
|
||||
const TEST_DATA: &str = r#"test data"#;
|
||||
|
||||
fn base64_encoded_checksum_to_hex_string(header_value: &HeaderValue) -> String {
|
||||
let decoded_checksum = base64::decode(header_value.to_str().unwrap()).unwrap();
|
||||
let decoded_checksum = decoded_checksum.into_iter().fold(String::new(), |mut acc, byte| {
|
||||
write!(acc, "{byte:02X?}").expect("string will always be writeable");
|
||||
acc
|
||||
});
|
||||
|
||||
format!("0x{decoded_checksum}")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_crc32_checksum() {
|
||||
let mut checksum = Crc32::default();
|
||||
checksum.update(TEST_DATA.as_bytes());
|
||||
let checksum_result = Box::new(checksum).headers();
|
||||
let encoded_checksum = checksum_result.get(CRC_32_HEADER_NAME).unwrap();
|
||||
let decoded_checksum = base64_encoded_checksum_to_hex_string(encoded_checksum);
|
||||
|
||||
let expected_checksum = "0xD308AEB2";
|
||||
|
||||
assert_eq!(decoded_checksum, expected_checksum);
|
||||
}
|
||||
|
||||
#[cfg(not(any(target_arch = "powerpc", target_arch = "powerpc64")))]
|
||||
#[test]
|
||||
fn test_crc32c_checksum() {
|
||||
let mut checksum = Crc32c::default();
|
||||
checksum.update(TEST_DATA.as_bytes());
|
||||
let checksum_result = Box::new(checksum).headers();
|
||||
let encoded_checksum = checksum_result.get(CRC_32_C_HEADER_NAME).unwrap();
|
||||
let decoded_checksum = base64_encoded_checksum_to_hex_string(encoded_checksum);
|
||||
|
||||
let expected_checksum = "0x3379B4CA";
|
||||
|
||||
assert_eq!(decoded_checksum, expected_checksum);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_crc64nvme_checksum() {
|
||||
use crate::{Crc64Nvme, http::CRC_64_NVME_HEADER_NAME};
|
||||
let mut checksum = Crc64Nvme::default();
|
||||
checksum.update(TEST_DATA.as_bytes());
|
||||
let checksum_result = Box::new(checksum).headers();
|
||||
let encoded_checksum = checksum_result.get(CRC_64_NVME_HEADER_NAME).unwrap();
|
||||
let decoded_checksum = base64_encoded_checksum_to_hex_string(encoded_checksum);
|
||||
|
||||
let expected_checksum = "0xAECAF3AF9C98A855";
|
||||
|
||||
assert_eq!(decoded_checksum, expected_checksum);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sha1_checksum() {
|
||||
let mut checksum = Sha1::default();
|
||||
checksum.update(TEST_DATA.as_bytes());
|
||||
let checksum_result = Box::new(checksum).headers();
|
||||
let encoded_checksum = checksum_result.get(SHA_1_HEADER_NAME).unwrap();
|
||||
let decoded_checksum = base64_encoded_checksum_to_hex_string(encoded_checksum);
|
||||
|
||||
let expected_checksum = "0xF48DD853820860816C75D54D0F584DC863327A7C";
|
||||
|
||||
assert_eq!(decoded_checksum, expected_checksum);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sha256_checksum() {
|
||||
let mut checksum = Sha256::default();
|
||||
checksum.update(TEST_DATA.as_bytes());
|
||||
let checksum_result = Box::new(checksum).headers();
|
||||
let encoded_checksum = checksum_result.get(SHA_256_HEADER_NAME).unwrap();
|
||||
let decoded_checksum = base64_encoded_checksum_to_hex_string(encoded_checksum);
|
||||
|
||||
let expected_checksum = "0x916F0027A575074CE72A331777C3478D6513F786A591BD892DA1A577BF2335F9";
|
||||
|
||||
assert_eq!(decoded_checksum, expected_checksum);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_md5_checksum() {
|
||||
let mut checksum = Md5::default();
|
||||
checksum.update(TEST_DATA.as_bytes());
|
||||
let checksum_result = Box::new(checksum).headers();
|
||||
let encoded_checksum = checksum_result.get(MD5_HEADER_NAME).unwrap();
|
||||
let decoded_checksum = base64_encoded_checksum_to_hex_string(encoded_checksum);
|
||||
|
||||
let expected_checksum = "0xEB733A00C0C9D336E65691A37AB54293";
|
||||
|
||||
assert_eq!(decoded_checksum, expected_checksum);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_checksum_algorithm_returns_error_for_unknown() {
|
||||
let error = "some invalid checksum algorithm"
|
||||
.parse::<ChecksumAlgorithm>()
|
||||
.expect_err("it should error");
|
||||
assert_eq!("some invalid checksum algorithm", error.checksum_algorithm());
|
||||
}
|
||||
}
|
||||
@@ -26,9 +26,6 @@ categories = ["web-programming", "development-tools", "config"]
|
||||
|
||||
[dependencies]
|
||||
const-str = { workspace = true, optional = true }
|
||||
serde = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
@@ -15,9 +15,9 @@
|
||||
use const_str::concat;
|
||||
|
||||
/// Application name
|
||||
/// Default value: RustFs
|
||||
/// Default value: RustFS
|
||||
/// Environment variable: RUSTFS_APP_NAME
|
||||
pub const APP_NAME: &str = "RustFs";
|
||||
pub const APP_NAME: &str = "RustFS";
|
||||
/// Application version
|
||||
/// Default value: 1.0.0
|
||||
/// Environment variable: RUSTFS_VERSION
|
||||
@@ -71,6 +71,16 @@ pub const DEFAULT_ACCESS_KEY: &str = "rustfsadmin";
|
||||
/// Example: --secret-key rustfsadmin
|
||||
pub const DEFAULT_SECRET_KEY: &str = "rustfsadmin";
|
||||
|
||||
/// Default console enable
|
||||
/// This is the default value for the console server.
|
||||
/// It is used to enable or disable the console server.
|
||||
/// Default value: true
|
||||
/// Environment variable: RUSTFS_CONSOLE_ENABLE
|
||||
/// Command line argument: --console-enable
|
||||
/// Example: RUSTFS_CONSOLE_ENABLE=true
|
||||
/// Example: --console-enable true
|
||||
pub const DEFAULT_CONSOLE_ENABLE: bool = true;
|
||||
|
||||
/// Default OBS configuration endpoint
|
||||
/// Environment variable: DEFAULT_OBS_ENDPOINT
|
||||
/// Command line argument: --obs-endpoint
|
||||
@@ -126,28 +136,28 @@ pub const DEFAULT_SINK_FILE_LOG_FILE: &str = concat!(DEFAULT_LOG_FILENAME, "-sin
|
||||
/// This is the default log directory for rustfs.
|
||||
/// It is used to store the logs of the application.
|
||||
/// Default value: logs
|
||||
/// Environment variable: RUSTFS_OBSERVABILITY_LOG_DIRECTORY
|
||||
pub const DEFAULT_LOG_DIR: &str = "/logs";
|
||||
/// Environment variable: RUSTFS_LOG_DIRECTORY
|
||||
pub const DEFAULT_LOG_DIR: &str = "logs";
|
||||
|
||||
/// Default log rotation size mb for rustfs
|
||||
/// This is the default log rotation size for rustfs.
|
||||
/// It is used to rotate the logs of the application.
|
||||
/// Default value: 100 MB
|
||||
/// Environment variable: RUSTFS_OBSERVABILITY_LOG_ROTATION_SIZE_MB
|
||||
/// Environment variable: RUSTFS_OBS_LOG_ROTATION_SIZE_MB
|
||||
pub const DEFAULT_LOG_ROTATION_SIZE_MB: u64 = 100;
|
||||
|
||||
/// Default log rotation time for rustfs
|
||||
/// This is the default log rotation time for rustfs.
|
||||
/// It is used to rotate the logs of the application.
|
||||
/// Default value: hour, eg: day,hour,minute,second
|
||||
/// Environment variable: RUSTFS_OBSERVABILITY_LOG_ROTATION_TIME
|
||||
/// Environment variable: RUSTFS_OBS_LOG_ROTATION_TIME
|
||||
pub const DEFAULT_LOG_ROTATION_TIME: &str = "day";
|
||||
|
||||
/// Default log keep files for rustfs
|
||||
/// This is the default log keep files for rustfs.
|
||||
/// It is used to keep the logs of the application.
|
||||
/// Default value: 30
|
||||
/// Environment variable: RUSTFS_OBSERVABILITY_LOG_KEEP_FILES
|
||||
/// Environment variable: RUSTFS_OBS_LOG_KEEP_FILES
|
||||
pub const DEFAULT_LOG_KEEP_FILES: u16 = 30;
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -157,7 +167,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_app_basic_constants() {
|
||||
// Test application basic constants
|
||||
assert_eq!(APP_NAME, "RustFs");
|
||||
assert_eq!(APP_NAME, "RustFS");
|
||||
assert!(!APP_NAME.contains(' '), "App name should not contain spaces");
|
||||
|
||||
assert_eq!(VERSION, "0.0.1");
|
||||
|
||||
@@ -27,7 +27,15 @@ pub const DEFAULT_TARGET: &str = "1";
|
||||
|
||||
pub const NOTIFY_PREFIX: &str = "notify";
|
||||
|
||||
pub const NOTIFY_ROUTE_PREFIX: &str = "notify_";
|
||||
pub const NOTIFY_ROUTE_PREFIX: &str = const_str::concat!(NOTIFY_PREFIX, "_");
|
||||
|
||||
/// Standard config keys and values.
|
||||
pub const ENABLE_KEY: &str = "enable";
|
||||
pub const COMMENT_KEY: &str = "comment";
|
||||
|
||||
/// Enable values
|
||||
pub const ENABLE_ON: &str = "on";
|
||||
pub const ENABLE_OFF: &str = "off";
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub const NOTIFY_SUB_SYSTEMS: &[&str] = &[NOTIFY_MQTT_SUB_SYS, NOTIFY_WEBHOOK_SUB_SYS];
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use crate::notify::{COMMENT_KEY, ENABLE_KEY};
|
||||
|
||||
// MQTT Keys
|
||||
pub const MQTT_BROKER: &str = "broker";
|
||||
pub const MQTT_TOPIC: &str = "topic";
|
||||
@@ -23,6 +25,21 @@ pub const MQTT_KEEP_ALIVE_INTERVAL: &str = "keep_alive_interval";
|
||||
pub const MQTT_QUEUE_DIR: &str = "queue_dir";
|
||||
pub const MQTT_QUEUE_LIMIT: &str = "queue_limit";
|
||||
|
||||
/// A list of all valid configuration keys for an MQTT target.
|
||||
pub const NOTIFY_MQTT_KEYS: &[&str] = &[
|
||||
ENABLE_KEY, // "enable" is a common key
|
||||
MQTT_BROKER,
|
||||
MQTT_TOPIC,
|
||||
MQTT_QOS,
|
||||
MQTT_USERNAME,
|
||||
MQTT_PASSWORD,
|
||||
MQTT_RECONNECT_INTERVAL,
|
||||
MQTT_KEEP_ALIVE_INTERVAL,
|
||||
MQTT_QUEUE_DIR,
|
||||
MQTT_QUEUE_LIMIT,
|
||||
COMMENT_KEY,
|
||||
];
|
||||
|
||||
// MQTT Environment Variables
|
||||
pub const ENV_MQTT_ENABLE: &str = "RUSTFS_NOTIFY_MQTT_ENABLE";
|
||||
pub const ENV_MQTT_BROKER: &str = "RUSTFS_NOTIFY_MQTT_BROKER";
|
||||
@@ -34,3 +51,16 @@ pub const ENV_MQTT_RECONNECT_INTERVAL: &str = "RUSTFS_NOTIFY_MQTT_RECONNECT_INTE
|
||||
pub const ENV_MQTT_KEEP_ALIVE_INTERVAL: &str = "RUSTFS_NOTIFY_MQTT_KEEP_ALIVE_INTERVAL";
|
||||
pub const ENV_MQTT_QUEUE_DIR: &str = "RUSTFS_NOTIFY_MQTT_QUEUE_DIR";
|
||||
pub const ENV_MQTT_QUEUE_LIMIT: &str = "RUSTFS_NOTIFY_MQTT_QUEUE_LIMIT";
|
||||
|
||||
pub const ENV_NOTIFY_MQTT_KEYS: &[&str; 10] = &[
|
||||
ENV_MQTT_ENABLE,
|
||||
ENV_MQTT_BROKER,
|
||||
ENV_MQTT_TOPIC,
|
||||
ENV_MQTT_QOS,
|
||||
ENV_MQTT_USERNAME,
|
||||
ENV_MQTT_PASSWORD,
|
||||
ENV_MQTT_RECONNECT_INTERVAL,
|
||||
ENV_MQTT_KEEP_ALIVE_INTERVAL,
|
||||
ENV_MQTT_QUEUE_DIR,
|
||||
ENV_MQTT_QUEUE_LIMIT,
|
||||
];
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use crate::notify::{COMMENT_KEY, ENABLE_KEY};
|
||||
|
||||
// Webhook Keys
|
||||
pub const WEBHOOK_ENDPOINT: &str = "endpoint";
|
||||
pub const WEBHOOK_AUTH_TOKEN: &str = "auth_token";
|
||||
@@ -20,6 +22,18 @@ pub const WEBHOOK_QUEUE_DIR: &str = "queue_dir";
|
||||
pub const WEBHOOK_CLIENT_CERT: &str = "client_cert";
|
||||
pub const WEBHOOK_CLIENT_KEY: &str = "client_key";
|
||||
|
||||
/// A list of all valid configuration keys for a webhook target.
|
||||
pub const NOTIFY_WEBHOOK_KEYS: &[&str] = &[
|
||||
ENABLE_KEY, // "enable" is a common key
|
||||
WEBHOOK_ENDPOINT,
|
||||
WEBHOOK_AUTH_TOKEN,
|
||||
WEBHOOK_QUEUE_LIMIT,
|
||||
WEBHOOK_QUEUE_DIR,
|
||||
WEBHOOK_CLIENT_CERT,
|
||||
WEBHOOK_CLIENT_KEY,
|
||||
COMMENT_KEY,
|
||||
];
|
||||
|
||||
// Webhook Environment Variables
|
||||
pub const ENV_WEBHOOK_ENABLE: &str = "RUSTFS_NOTIFY_WEBHOOK_ENABLE";
|
||||
pub const ENV_WEBHOOK_ENDPOINT: &str = "RUSTFS_NOTIFY_WEBHOOK_ENDPOINT";
|
||||
@@ -28,3 +42,13 @@ pub const ENV_WEBHOOK_QUEUE_LIMIT: &str = "RUSTFS_NOTIFY_WEBHOOK_QUEUE_LIMIT";
|
||||
pub const ENV_WEBHOOK_QUEUE_DIR: &str = "RUSTFS_NOTIFY_WEBHOOK_QUEUE_DIR";
|
||||
pub const ENV_WEBHOOK_CLIENT_CERT: &str = "RUSTFS_NOTIFY_WEBHOOK_CLIENT_CERT";
|
||||
pub const ENV_WEBHOOK_CLIENT_KEY: &str = "RUSTFS_NOTIFY_WEBHOOK_CLIENT_KEY";
|
||||
|
||||
pub const ENV_NOTIFY_WEBHOOK_KEYS: &[&str; 7] = &[
|
||||
ENV_WEBHOOK_ENABLE,
|
||||
ENV_WEBHOOK_ENDPOINT,
|
||||
ENV_WEBHOOK_AUTH_TOKEN,
|
||||
ENV_WEBHOOK_QUEUE_LIMIT,
|
||||
ENV_WEBHOOK_QUEUE_DIR,
|
||||
ENV_WEBHOOK_CLIENT_CERT,
|
||||
ENV_WEBHOOK_CLIENT_KEY,
|
||||
];
|
||||
|
||||
@@ -12,279 +12,24 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use crate::observability::logger::LoggerConfig;
|
||||
use crate::observability::otel::OtelConfig;
|
||||
use crate::observability::sink::SinkConfig;
|
||||
use serde::{Deserialize, Serialize};
|
||||
// Observability Keys
|
||||
|
||||
/// Observability configuration
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
pub struct ObservabilityConfig {
|
||||
pub otel: OtelConfig,
|
||||
pub sinks: Vec<SinkConfig>,
|
||||
pub logger: Option<LoggerConfig>,
|
||||
}
|
||||
pub const ENV_OBS_ENDPOINT: &str = "RUSTFS_OBS_ENDPOINT";
|
||||
pub const ENV_OBS_USE_STDOUT: &str = "RUSTFS_OBS_USE_STDOUT";
|
||||
pub const ENV_OBS_SAMPLE_RATIO: &str = "RUSTFS_OBS_SAMPLE_RATIO";
|
||||
pub const ENV_OBS_METER_INTERVAL: &str = "RUSTFS_OBS_METER_INTERVAL";
|
||||
pub const ENV_OBS_SERVICE_NAME: &str = "RUSTFS_OBS_SERVICE_NAME";
|
||||
pub const ENV_OBS_SERVICE_VERSION: &str = "RUSTFS_OBS_SERVICE_VERSION";
|
||||
pub const ENV_OBS_ENVIRONMENT: &str = "RUSTFS_OBS_ENVIRONMENT";
|
||||
pub const ENV_OBS_LOGGER_LEVEL: &str = "RUSTFS_OBS_LOGGER_LEVEL";
|
||||
pub const ENV_OBS_LOCAL_LOGGING_ENABLED: &str = "RUSTFS_OBS_LOCAL_LOGGING_ENABLED";
|
||||
pub const ENV_OBS_LOG_DIRECTORY: &str = "RUSTFS_OBS_LOG_DIRECTORY";
|
||||
pub const ENV_OBS_LOG_FILENAME: &str = "RUSTFS_OBS_LOG_FILENAME";
|
||||
pub const ENV_OBS_LOG_ROTATION_SIZE_MB: &str = "RUSTFS_OBS_LOG_ROTATION_SIZE_MB";
|
||||
pub const ENV_OBS_LOG_ROTATION_TIME: &str = "RUSTFS_OBS_LOG_ROTATION_TIME";
|
||||
pub const ENV_OBS_LOG_KEEP_FILES: &str = "RUSTFS_OBS_LOG_KEEP_FILES";
|
||||
|
||||
impl ObservabilityConfig {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
otel: OtelConfig::new(),
|
||||
sinks: vec![SinkConfig::new()],
|
||||
logger: Some(LoggerConfig::new()),
|
||||
}
|
||||
}
|
||||
}
|
||||
pub const ENV_AUDIT_LOGGER_QUEUE_CAPACITY: &str = "RUSTFS_AUDIT_LOGGER_QUEUE_CAPACITY";
|
||||
|
||||
impl Default for ObservabilityConfig {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_observability_config_new() {
|
||||
let config = ObservabilityConfig::new();
|
||||
|
||||
// Verify OTEL config is initialized
|
||||
assert!(config.otel.use_stdout.is_some(), "OTEL use_stdout should be configured");
|
||||
assert!(config.otel.sample_ratio.is_some(), "OTEL sample_ratio should be configured");
|
||||
assert!(config.otel.meter_interval.is_some(), "OTEL meter_interval should be configured");
|
||||
assert!(config.otel.service_name.is_some(), "OTEL service_name should be configured");
|
||||
assert!(config.otel.service_version.is_some(), "OTEL service_version should be configured");
|
||||
assert!(config.otel.environment.is_some(), "OTEL environment should be configured");
|
||||
assert!(config.otel.logger_level.is_some(), "OTEL logger_level should be configured");
|
||||
|
||||
// Verify sinks are initialized
|
||||
assert!(!config.sinks.is_empty(), "Sinks should not be empty");
|
||||
assert_eq!(config.sinks.len(), 1, "Should have exactly one default sink");
|
||||
|
||||
// Verify logger is initialized
|
||||
assert!(config.logger.is_some(), "Logger should be configured");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_observability_config_default() {
|
||||
let config = ObservabilityConfig::default();
|
||||
let new_config = ObservabilityConfig::new();
|
||||
|
||||
// Default should be equivalent to new()
|
||||
assert_eq!(config.sinks.len(), new_config.sinks.len());
|
||||
assert_eq!(config.logger.is_some(), new_config.logger.is_some());
|
||||
|
||||
// OTEL configs should be equivalent
|
||||
assert_eq!(config.otel.use_stdout, new_config.otel.use_stdout);
|
||||
assert_eq!(config.otel.sample_ratio, new_config.otel.sample_ratio);
|
||||
assert_eq!(config.otel.meter_interval, new_config.otel.meter_interval);
|
||||
assert_eq!(config.otel.service_name, new_config.otel.service_name);
|
||||
assert_eq!(config.otel.service_version, new_config.otel.service_version);
|
||||
assert_eq!(config.otel.environment, new_config.otel.environment);
|
||||
assert_eq!(config.otel.logger_level, new_config.otel.logger_level);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_observability_config_otel_defaults() {
|
||||
let config = ObservabilityConfig::new();
|
||||
|
||||
// Test OTEL default values
|
||||
if let Some(_use_stdout) = config.otel.use_stdout {
|
||||
// Test boolean values - any boolean value is valid
|
||||
}
|
||||
|
||||
if let Some(sample_ratio) = config.otel.sample_ratio {
|
||||
assert!((0.0..=1.0).contains(&sample_ratio), "Sample ratio should be between 0.0 and 1.0");
|
||||
}
|
||||
|
||||
if let Some(meter_interval) = config.otel.meter_interval {
|
||||
assert!(meter_interval > 0, "Meter interval should be positive");
|
||||
assert!(meter_interval <= 3600, "Meter interval should be reasonable (≤ 1 hour)");
|
||||
}
|
||||
|
||||
if let Some(service_name) = &config.otel.service_name {
|
||||
assert!(!service_name.is_empty(), "Service name should not be empty");
|
||||
assert!(!service_name.contains(' '), "Service name should not contain spaces");
|
||||
}
|
||||
|
||||
if let Some(service_version) = &config.otel.service_version {
|
||||
assert!(!service_version.is_empty(), "Service version should not be empty");
|
||||
}
|
||||
|
||||
if let Some(environment) = &config.otel.environment {
|
||||
assert!(!environment.is_empty(), "Environment should not be empty");
|
||||
assert!(
|
||||
["development", "staging", "production", "test"].contains(&environment.as_str()),
|
||||
"Environment should be a standard environment name"
|
||||
);
|
||||
}
|
||||
|
||||
if let Some(logger_level) = &config.otel.logger_level {
|
||||
assert!(
|
||||
["trace", "debug", "info", "warn", "error"].contains(&logger_level.as_str()),
|
||||
"Logger level should be a valid tracing level"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_observability_config_sinks() {
|
||||
let config = ObservabilityConfig::new();
|
||||
|
||||
// Test default sink configuration
|
||||
assert_eq!(config.sinks.len(), 1, "Should have exactly one default sink");
|
||||
|
||||
let _default_sink = &config.sinks[0];
|
||||
// Test that the sink has valid configuration
|
||||
// Note: We can't test specific values without knowing SinkConfig implementation
|
||||
// but we can test that it's properly initialized
|
||||
|
||||
// Test that we can add more sinks
|
||||
let mut config_mut = config.clone();
|
||||
config_mut.sinks.push(SinkConfig::new());
|
||||
assert_eq!(config_mut.sinks.len(), 2, "Should be able to add more sinks");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_observability_config_logger() {
|
||||
let config = ObservabilityConfig::new();
|
||||
|
||||
// Test logger configuration
|
||||
assert!(config.logger.is_some(), "Logger should be configured by default");
|
||||
|
||||
if let Some(_logger) = &config.logger {
|
||||
// Test that logger has valid configuration
|
||||
// Note: We can't test specific values without knowing LoggerConfig implementation
|
||||
// but we can test that it's properly initialized
|
||||
}
|
||||
|
||||
// Test that logger can be disabled
|
||||
let mut config_mut = config.clone();
|
||||
config_mut.logger = None;
|
||||
assert!(config_mut.logger.is_none(), "Logger should be able to be disabled");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_observability_config_serialization() {
|
||||
let config = ObservabilityConfig::new();
|
||||
|
||||
// Test serialization to JSON
|
||||
let json_result = serde_json::to_string(&config);
|
||||
assert!(json_result.is_ok(), "Config should be serializable to JSON");
|
||||
|
||||
let json_str = json_result.unwrap();
|
||||
assert!(!json_str.is_empty(), "Serialized JSON should not be empty");
|
||||
assert!(json_str.contains("otel"), "JSON should contain otel configuration");
|
||||
assert!(json_str.contains("sinks"), "JSON should contain sinks configuration");
|
||||
assert!(json_str.contains("logger"), "JSON should contain logger configuration");
|
||||
|
||||
// Test deserialization from JSON
|
||||
let deserialized_result: Result<ObservabilityConfig, _> = serde_json::from_str(&json_str);
|
||||
assert!(deserialized_result.is_ok(), "Config should be deserializable from JSON");
|
||||
|
||||
let deserialized_config = deserialized_result.unwrap();
|
||||
assert_eq!(deserialized_config.sinks.len(), config.sinks.len());
|
||||
assert_eq!(deserialized_config.logger.is_some(), config.logger.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_observability_config_debug_format() {
|
||||
let config = ObservabilityConfig::new();
|
||||
|
||||
let debug_str = format!("{config:?}");
|
||||
assert!(!debug_str.is_empty(), "Debug output should not be empty");
|
||||
assert!(debug_str.contains("ObservabilityConfig"), "Debug output should contain struct name");
|
||||
assert!(debug_str.contains("otel"), "Debug output should contain otel field");
|
||||
assert!(debug_str.contains("sinks"), "Debug output should contain sinks field");
|
||||
assert!(debug_str.contains("logger"), "Debug output should contain logger field");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_observability_config_clone() {
|
||||
let config = ObservabilityConfig::new();
|
||||
let cloned_config = config.clone();
|
||||
|
||||
// Test that clone creates an independent copy
|
||||
assert_eq!(cloned_config.sinks.len(), config.sinks.len());
|
||||
assert_eq!(cloned_config.logger.is_some(), config.logger.is_some());
|
||||
assert_eq!(cloned_config.otel.endpoint, config.otel.endpoint);
|
||||
assert_eq!(cloned_config.otel.use_stdout, config.otel.use_stdout);
|
||||
assert_eq!(cloned_config.otel.sample_ratio, config.otel.sample_ratio);
|
||||
assert_eq!(cloned_config.otel.meter_interval, config.otel.meter_interval);
|
||||
assert_eq!(cloned_config.otel.service_name, config.otel.service_name);
|
||||
assert_eq!(cloned_config.otel.service_version, config.otel.service_version);
|
||||
assert_eq!(cloned_config.otel.environment, config.otel.environment);
|
||||
assert_eq!(cloned_config.otel.logger_level, config.otel.logger_level);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_observability_config_modification() {
|
||||
let mut config = ObservabilityConfig::new();
|
||||
|
||||
// Test modifying OTEL endpoint
|
||||
let original_endpoint = config.otel.endpoint.clone();
|
||||
config.otel.endpoint = "http://localhost:4317".to_string();
|
||||
assert_ne!(config.otel.endpoint, original_endpoint);
|
||||
assert_eq!(config.otel.endpoint, "http://localhost:4317");
|
||||
|
||||
// Test modifying sinks
|
||||
let original_sinks_len = config.sinks.len();
|
||||
config.sinks.push(SinkConfig::new());
|
||||
assert_eq!(config.sinks.len(), original_sinks_len + 1);
|
||||
|
||||
// Test disabling logger
|
||||
config.logger = None;
|
||||
assert!(config.logger.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_observability_config_edge_cases() {
|
||||
// Test with empty sinks
|
||||
let mut config = ObservabilityConfig::new();
|
||||
config.sinks.clear();
|
||||
assert!(config.sinks.is_empty(), "Sinks should be empty after clearing");
|
||||
|
||||
// Test serialization with empty sinks
|
||||
let json_result = serde_json::to_string(&config);
|
||||
assert!(json_result.is_ok(), "Config with empty sinks should be serializable");
|
||||
|
||||
// Test with no logger
|
||||
config.logger = None;
|
||||
let json_result = serde_json::to_string(&config);
|
||||
assert!(json_result.is_ok(), "Config with no logger should be serializable");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_observability_config_memory_efficiency() {
|
||||
let config = ObservabilityConfig::new();
|
||||
|
||||
// Test that config doesn't use excessive memory
|
||||
let config_size = std::mem::size_of_val(&config);
|
||||
assert!(config_size < 5000, "Config should not use excessive memory");
|
||||
|
||||
// Test that endpoint string is not excessively long
|
||||
assert!(config.otel.endpoint.len() < 1000, "Endpoint should not be excessively long");
|
||||
|
||||
// Test that collections are reasonably sized
|
||||
assert!(config.sinks.len() < 100, "Sinks collection should be reasonably sized");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_observability_config_consistency() {
|
||||
// Create multiple configs and ensure they're consistent
|
||||
let config1 = ObservabilityConfig::new();
|
||||
let config2 = ObservabilityConfig::new();
|
||||
|
||||
// Both configs should have the same default structure
|
||||
assert_eq!(config1.sinks.len(), config2.sinks.len());
|
||||
assert_eq!(config1.logger.is_some(), config2.logger.is_some());
|
||||
assert_eq!(config1.otel.use_stdout, config2.otel.use_stdout);
|
||||
assert_eq!(config1.otel.sample_ratio, config2.otel.sample_ratio);
|
||||
assert_eq!(config1.otel.meter_interval, config2.otel.meter_interval);
|
||||
assert_eq!(config1.otel.service_name, config2.otel.service_name);
|
||||
assert_eq!(config1.otel.service_version, config2.otel.service_version);
|
||||
assert_eq!(config1.otel.environment, config2.otel.environment);
|
||||
assert_eq!(config1.otel.logger_level, config2.otel.logger_level);
|
||||
}
|
||||
}
|
||||
// Default values for observability configuration
|
||||
pub const DEFAULT_AUDIT_LOGGER_QUEUE_CAPACITY: usize = 10000;
|
||||
|
||||
@@ -12,62 +12,17 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::env;
|
||||
// RUSTFS_SINKS_FILE_PATH
|
||||
pub const ENV_SINKS_FILE_PATH: &str = "RUSTFS_SINKS_FILE_PATH";
|
||||
// RUSTFS_SINKS_FILE_BUFFER_SIZE
|
||||
pub const ENV_SINKS_FILE_BUFFER_SIZE: &str = "RUSTFS_SINKS_FILE_BUFFER_SIZE";
|
||||
// RUSTFS_SINKS_FILE_FLUSH_INTERVAL_MS
|
||||
pub const ENV_SINKS_FILE_FLUSH_INTERVAL_MS: &str = "RUSTFS_SINKS_FILE_FLUSH_INTERVAL_MS";
|
||||
// RUSTFS_SINKS_FILE_FLUSH_THRESHOLD
|
||||
pub const ENV_SINKS_FILE_FLUSH_THRESHOLD: &str = "RUSTFS_SINKS_FILE_FLUSH_THRESHOLD";
|
||||
|
||||
/// File sink configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct FileSink {
|
||||
pub path: String,
|
||||
#[serde(default = "default_buffer_size")]
|
||||
pub buffer_size: Option<usize>,
|
||||
#[serde(default = "default_flush_interval_ms")]
|
||||
pub flush_interval_ms: Option<u64>,
|
||||
#[serde(default = "default_flush_threshold")]
|
||||
pub flush_threshold: Option<usize>,
|
||||
}
|
||||
pub const DEFAULT_SINKS_FILE_BUFFER_SIZE: usize = 8192;
|
||||
|
||||
impl FileSink {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
path: env::var("RUSTFS_SINKS_FILE_PATH")
|
||||
.ok()
|
||||
.filter(|s| !s.trim().is_empty())
|
||||
.unwrap_or_else(default_path),
|
||||
buffer_size: default_buffer_size(),
|
||||
flush_interval_ms: default_flush_interval_ms(),
|
||||
flush_threshold: default_flush_threshold(),
|
||||
}
|
||||
}
|
||||
}
|
||||
pub const DEFAULT_SINKS_FILE_FLUSH_INTERVAL_MS: u64 = 1000;
|
||||
|
||||
impl Default for FileSink {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
fn default_buffer_size() -> Option<usize> {
|
||||
Some(8192)
|
||||
}
|
||||
fn default_flush_interval_ms() -> Option<u64> {
|
||||
Some(1000)
|
||||
}
|
||||
fn default_flush_threshold() -> Option<usize> {
|
||||
Some(100)
|
||||
}
|
||||
|
||||
fn default_path() -> String {
|
||||
let temp_dir = env::temp_dir().join("rustfs");
|
||||
|
||||
if let Err(e) = std::fs::create_dir_all(&temp_dir) {
|
||||
eprintln!("Failed to create log directory: {e}");
|
||||
return "rustfs/rustfs.log".to_string();
|
||||
}
|
||||
|
||||
temp_dir
|
||||
.join("rustfs.log")
|
||||
.to_str()
|
||||
.unwrap_or("rustfs/rustfs.log")
|
||||
.to_string()
|
||||
}
|
||||
pub const DEFAULT_SINKS_FILE_FLUSH_THRESHOLD: usize = 100;
|
||||
|
||||
@@ -12,39 +12,16 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
// RUSTFS_SINKS_KAFKA_BROKERS
|
||||
pub const ENV_SINKS_KAFKA_BROKERS: &str = "RUSTFS_SINKS_KAFKA_BROKERS";
|
||||
pub const ENV_SINKS_KAFKA_TOPIC: &str = "RUSTFS_SINKS_KAFKA_TOPIC";
|
||||
// batch_size
|
||||
pub const ENV_SINKS_KAFKA_BATCH_SIZE: &str = "RUSTFS_SINKS_KAFKA_BATCH_SIZE";
|
||||
// batch_timeout_ms
|
||||
pub const ENV_SINKS_KAFKA_BATCH_TIMEOUT_MS: &str = "RUSTFS_SINKS_KAFKA_BATCH_TIMEOUT_MS";
|
||||
|
||||
/// Kafka sink configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct KafkaSink {
|
||||
pub brokers: String,
|
||||
pub topic: String,
|
||||
#[serde(default = "default_batch_size")]
|
||||
pub batch_size: Option<usize>,
|
||||
#[serde(default = "default_batch_timeout_ms")]
|
||||
pub batch_timeout_ms: Option<u64>,
|
||||
}
|
||||
|
||||
impl KafkaSink {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
brokers: "localhost:9092".to_string(),
|
||||
topic: "rustfs".to_string(),
|
||||
batch_size: default_batch_size(),
|
||||
batch_timeout_ms: default_batch_timeout_ms(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for KafkaSink {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
fn default_batch_size() -> Option<usize> {
|
||||
Some(100)
|
||||
}
|
||||
fn default_batch_timeout_ms() -> Option<u64> {
|
||||
Some(1000)
|
||||
}
|
||||
// brokers
|
||||
pub const DEFAULT_SINKS_KAFKA_BROKERS: &str = "localhost:9092";
|
||||
pub const DEFAULT_SINKS_KAFKA_TOPIC: &str = "rustfs-sinks";
|
||||
pub const DEFAULT_SINKS_KAFKA_BATCH_SIZE: usize = 100;
|
||||
pub const DEFAULT_SINKS_KAFKA_BATCH_TIMEOUT_MS: u64 = 1000;
|
||||
|
||||
@@ -1,35 +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.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Logger configuration
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
pub struct LoggerConfig {
|
||||
pub queue_capacity: Option<usize>,
|
||||
}
|
||||
|
||||
impl LoggerConfig {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
queue_capacity: Some(10000),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for LoggerConfig {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
@@ -12,10 +12,12 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
pub(crate) mod config;
|
||||
pub(crate) mod file;
|
||||
pub(crate) mod kafka;
|
||||
pub(crate) mod logger;
|
||||
pub(crate) mod otel;
|
||||
pub(crate) mod sink;
|
||||
pub(crate) mod webhook;
|
||||
mod config;
|
||||
mod file;
|
||||
mod kafka;
|
||||
mod webhook;
|
||||
|
||||
pub use config::*;
|
||||
pub use file::*;
|
||||
pub use kafka::*;
|
||||
pub use webhook::*;
|
||||
|
||||
@@ -1,83 +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.
|
||||
|
||||
use crate::constants::app::{ENVIRONMENT, METER_INTERVAL, SAMPLE_RATIO, SERVICE_VERSION, USE_STDOUT};
|
||||
use crate::{APP_NAME, DEFAULT_LOG_LEVEL};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::env;
|
||||
|
||||
/// OpenTelemetry configuration
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
pub struct OtelConfig {
|
||||
pub endpoint: String, // Endpoint for metric collection
|
||||
pub use_stdout: Option<bool>, // Output to stdout
|
||||
pub sample_ratio: Option<f64>, // Trace sampling ratio
|
||||
pub meter_interval: Option<u64>, // Metric collection interval
|
||||
pub service_name: Option<String>, // Service name
|
||||
pub service_version: Option<String>, // Service version
|
||||
pub environment: Option<String>, // Environment
|
||||
pub logger_level: Option<String>, // Logger level
|
||||
pub local_logging_enabled: Option<bool>, // Local logging enabled
|
||||
}
|
||||
|
||||
impl OtelConfig {
|
||||
pub fn new() -> Self {
|
||||
extract_otel_config_from_env()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for OtelConfig {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function: Extract observable configuration from environment variables
|
||||
fn extract_otel_config_from_env() -> OtelConfig {
|
||||
OtelConfig {
|
||||
endpoint: env::var("RUSTFS_OBSERVABILITY_ENDPOINT").unwrap_or_else(|_| "".to_string()),
|
||||
use_stdout: env::var("RUSTFS_OBSERVABILITY_USE_STDOUT")
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(USE_STDOUT)),
|
||||
sample_ratio: env::var("RUSTFS_OBSERVABILITY_SAMPLE_RATIO")
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(SAMPLE_RATIO)),
|
||||
meter_interval: env::var("RUSTFS_OBSERVABILITY_METER_INTERVAL")
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(METER_INTERVAL)),
|
||||
service_name: env::var("RUSTFS_OBSERVABILITY_SERVICE_NAME")
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(APP_NAME.to_string())),
|
||||
service_version: env::var("RUSTFS_OBSERVABILITY_SERVICE_VERSION")
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(SERVICE_VERSION.to_string())),
|
||||
environment: env::var("RUSTFS_OBSERVABILITY_ENVIRONMENT")
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(ENVIRONMENT.to_string())),
|
||||
logger_level: env::var("RUSTFS_OBSERVABILITY_LOGGER_LEVEL")
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(DEFAULT_LOG_LEVEL.to_string())),
|
||||
local_logging_enabled: env::var("RUSTFS_OBSERVABILITY_LOCAL_LOGGING_ENABLED")
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(false)),
|
||||
}
|
||||
}
|
||||
@@ -1,39 +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.
|
||||
|
||||
use crate::observability::file::FileSink;
|
||||
use crate::observability::kafka::KafkaSink;
|
||||
use crate::observability::webhook::WebhookSink;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Sink configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum SinkConfig {
|
||||
Kafka(KafkaSink),
|
||||
Webhook(WebhookSink),
|
||||
File(FileSink),
|
||||
}
|
||||
|
||||
impl SinkConfig {
|
||||
pub fn new() -> Self {
|
||||
Self::File(FileSink::new())
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for SinkConfig {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
@@ -12,42 +12,17 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
// RUSTFS_SINKS_WEBHOOK_ENDPOINT
|
||||
pub const ENV_SINKS_WEBHOOK_ENDPOINT: &str = "RUSTFS_SINKS_WEBHOOK_ENDPOINT";
|
||||
// RUSTFS_SINKS_WEBHOOK_AUTH_TOKEN
|
||||
pub const ENV_SINKS_WEBHOOK_AUTH_TOKEN: &str = "RUSTFS_SINKS_WEBHOOK_AUTH_TOKEN";
|
||||
// max_retries
|
||||
pub const ENV_SINKS_WEBHOOK_MAX_RETRIES: &str = "RUSTFS_SINKS_WEBHOOK_MAX_RETRIES";
|
||||
// retry_delay_ms
|
||||
pub const ENV_SINKS_WEBHOOK_RETRY_DELAY_MS: &str = "RUSTFS_SINKS_WEBHOOK_RETRY_DELAY_MS";
|
||||
|
||||
/// Webhook sink configuration
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
pub struct WebhookSink {
|
||||
pub endpoint: String,
|
||||
pub auth_token: String,
|
||||
pub headers: Option<HashMap<String, String>>,
|
||||
#[serde(default = "default_max_retries")]
|
||||
pub max_retries: Option<usize>,
|
||||
#[serde(default = "default_retry_delay_ms")]
|
||||
pub retry_delay_ms: Option<u64>,
|
||||
}
|
||||
|
||||
impl WebhookSink {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
endpoint: "".to_string(),
|
||||
auth_token: "".to_string(),
|
||||
headers: Some(HashMap::new()),
|
||||
max_retries: default_max_retries(),
|
||||
retry_delay_ms: default_retry_delay_ms(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for WebhookSink {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
fn default_max_retries() -> Option<usize> {
|
||||
Some(3)
|
||||
}
|
||||
fn default_retry_delay_ms() -> Option<u64> {
|
||||
Some(100)
|
||||
}
|
||||
// Default values for webhook sink configuration
|
||||
pub const DEFAULT_SINKS_WEBHOOK_ENDPOINT: &str = "http://localhost:8080";
|
||||
pub const DEFAULT_SINKS_WEBHOOK_AUTH_TOKEN: &str = "";
|
||||
pub const DEFAULT_SINKS_WEBHOOK_MAX_RETRIES: usize = 3;
|
||||
pub const DEFAULT_SINKS_WEBHOOK_RETRY_DELAY_MS: u64 = 100;
|
||||
|
||||
@@ -38,7 +38,7 @@ url.workspace = true
|
||||
rustfs-madmin.workspace = true
|
||||
rustfs-filemeta.workspace = true
|
||||
bytes.workspace = true
|
||||
serial_test = "3.2.0"
|
||||
aws-sdk-s3 = "1.99.0"
|
||||
aws-config = "1.8.3"
|
||||
async-trait = { workspace = true }
|
||||
serial_test = { workspace = true }
|
||||
aws-sdk-s3.workspace = true
|
||||
aws-config = { workspace = true }
|
||||
async-trait = { workspace = true }
|
||||
@@ -50,7 +50,7 @@ serde.workspace = true
|
||||
time.workspace = true
|
||||
bytesize.workspace = true
|
||||
serde_json.workspace = true
|
||||
serde-xml-rs.workspace = true
|
||||
quick-xml.workspace = true
|
||||
s3s.workspace = true
|
||||
http.workspace = true
|
||||
url.workspace = true
|
||||
@@ -66,6 +66,7 @@ rmp-serde.workspace = true
|
||||
tokio-util = { workspace = true, features = ["io", "compat"] }
|
||||
base64 = { workspace = true }
|
||||
hmac = { workspace = true }
|
||||
sha1 = { workspace = true }
|
||||
sha2 = { workspace = true }
|
||||
hex-simd = { workspace = true }
|
||||
path-clean = { workspace = true }
|
||||
@@ -98,6 +99,7 @@ rustfs-filemeta.workspace = true
|
||||
rustfs-utils = { workspace = true, features = ["full"] }
|
||||
rustfs-rio.workspace = true
|
||||
rustfs-signer.workspace = true
|
||||
rustfs-checksums.workspace = true
|
||||
futures-util.workspace = true
|
||||
|
||||
[target.'cfg(not(windows))'.dependencies]
|
||||
|
||||
@@ -346,8 +346,12 @@ impl ExpiryState {
|
||||
}
|
||||
|
||||
pub async fn worker(rx: &mut Receiver<Option<ExpiryOpType>>, api: Arc<ECStore>) {
|
||||
//let cancel_token =
|
||||
// get_background_services_cancel_token().ok_or_else(|| Error::other("Background services not initialized"))?;
|
||||
|
||||
loop {
|
||||
select! {
|
||||
//_ = cancel_token.cancelled() => {
|
||||
_ = tokio::signal::ctrl_c() => {
|
||||
info!("got ctrl+c, exits");
|
||||
break;
|
||||
@@ -811,8 +815,8 @@ impl LifecycleOps for ObjectInfo {
|
||||
num_versions: self.num_versions,
|
||||
delete_marker: self.delete_marker,
|
||||
successor_mod_time: self.successor_mod_time,
|
||||
//restore_ongoing: self.restore_ongoing,
|
||||
//restore_expires: self.restore_expires,
|
||||
restore_ongoing: self.restore_ongoing,
|
||||
restore_expires: self.restore_expires,
|
||||
transition_status: self.transitioned_object.status.clone(),
|
||||
..Default::default()
|
||||
}
|
||||
|
||||
@@ -132,7 +132,7 @@ pub trait Lifecycle {
|
||||
async fn has_transition(&self) -> bool;
|
||||
fn has_expiry(&self) -> bool;
|
||||
async fn has_active_rules(&self, prefix: &str) -> bool;
|
||||
async fn validate(&self, lr_retention: bool) -> Result<(), std::io::Error>;
|
||||
async fn validate(&self, lr: &ObjectLockConfiguration) -> Result<(), std::io::Error>;
|
||||
async fn filter_rules(&self, obj: &ObjectOpts) -> Option<Vec<LifecycleRule>>;
|
||||
async fn eval(&self, obj: &ObjectOpts) -> Event;
|
||||
async fn eval_inner(&self, obj: &ObjectOpts, now: OffsetDateTime) -> Event;
|
||||
@@ -213,7 +213,7 @@ impl Lifecycle for BucketLifecycleConfiguration {
|
||||
false
|
||||
}
|
||||
|
||||
async fn validate(&self, lr_retention: bool) -> Result<(), std::io::Error> {
|
||||
async fn validate(&self, lr: &ObjectLockConfiguration) -> Result<(), std::io::Error> {
|
||||
if self.rules.len() > 1000 {
|
||||
return Err(std::io::Error::other(ERR_LIFECYCLE_TOO_MANY_RULES));
|
||||
}
|
||||
@@ -223,13 +223,15 @@ impl Lifecycle for BucketLifecycleConfiguration {
|
||||
|
||||
for r in &self.rules {
|
||||
r.validate()?;
|
||||
if let Some(expiration) = r.expiration.as_ref() {
|
||||
if let Some(expired_object_delete_marker) = expiration.expired_object_delete_marker {
|
||||
if lr_retention && (expired_object_delete_marker) {
|
||||
return Err(std::io::Error::other(ERR_LIFECYCLE_BUCKET_LOCKED));
|
||||
/*if let Some(object_lock_enabled) = lr.object_lock_enabled.as_ref() {
|
||||
if let Some(expiration) = r.expiration.as_ref() {
|
||||
if let Some(expired_object_delete_marker) = expiration.expired_object_delete_marker {
|
||||
if object_lock_enabled.as_str() == ObjectLockEnabled::ENABLED && (expired_object_delete_marker) {
|
||||
return Err(std::io::Error::other(ERR_LIFECYCLE_BUCKET_LOCKED));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
for (i, _) in self.rules.iter().enumerate() {
|
||||
if i == self.rules.len() - 1 {
|
||||
@@ -600,7 +602,7 @@ pub fn expected_expiry_time(mod_time: OffsetDateTime, days: i32) -> OffsetDateTi
|
||||
}
|
||||
let t = mod_time
|
||||
.to_offset(offset!(-0:00:00))
|
||||
.saturating_add(Duration::days(0 /*days as i64*/)); //debug
|
||||
.saturating_add(Duration::days(days as i64));
|
||||
let mut hour = 3600;
|
||||
if let Ok(env_ilm_hour) = env::var("_RUSTFS_ILM_HOUR") {
|
||||
if let Ok(num_hour) = env_ilm_hour.parse::<usize>() {
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
#![allow(clippy::all)]
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use rustfs_utils::HashAlgorithm;
|
||||
use rustfs_checksums::ChecksumAlgorithm;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::client::{api_put_object::PutObjectOptions, api_s3_datatypes::ObjectPart};
|
||||
@@ -103,15 +103,34 @@ impl ChecksumMode {
|
||||
}
|
||||
|
||||
pub fn can_composite(&self) -> bool {
|
||||
todo!();
|
||||
let s = EnumSet::from(*self).intersection(*C_ChecksumMask);
|
||||
match s.as_u8() {
|
||||
2_u8 => true,
|
||||
4_u8 => true,
|
||||
8_u8 => true,
|
||||
16_u8 => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn can_merge_crc(&self) -> bool {
|
||||
todo!();
|
||||
let s = EnumSet::from(*self).intersection(*C_ChecksumMask);
|
||||
match s.as_u8() {
|
||||
8_u8 => true,
|
||||
16_u8 => true,
|
||||
32_u8 => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn full_object_requested(&self) -> bool {
|
||||
todo!();
|
||||
let s = EnumSet::from(*self).intersection(*C_ChecksumMask);
|
||||
match s.as_u8() {
|
||||
//C_ChecksumFullObjectCRC32 as u8 => true,
|
||||
//C_ChecksumFullObjectCRC32C as u8 => true,
|
||||
32_u8 => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn key_capitalized(&self) -> String {
|
||||
@@ -123,33 +142,35 @@ impl ChecksumMode {
|
||||
if u == ChecksumMode::ChecksumCRC32 as u8 || u == ChecksumMode::ChecksumCRC32C as u8 {
|
||||
4
|
||||
} else if u == ChecksumMode::ChecksumSHA1 as u8 {
|
||||
4 //sha1.size
|
||||
use sha1::Digest;
|
||||
sha1::Sha1::output_size() as usize
|
||||
} else if u == ChecksumMode::ChecksumSHA256 as u8 {
|
||||
4 //sha256.size
|
||||
use sha2::Digest;
|
||||
sha2::Sha256::output_size() as usize
|
||||
} else if u == ChecksumMode::ChecksumCRC64NVME as u8 {
|
||||
4 //crc64.size
|
||||
8
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
pub fn hasher(&self) -> Result<HashAlgorithm, std::io::Error> {
|
||||
pub fn hasher(&self) -> Result<Box<dyn rustfs_checksums::http::HttpChecksum>, std::io::Error> {
|
||||
match /*C_ChecksumMask & **/self {
|
||||
/*ChecksumMode::ChecksumCRC32 => {
|
||||
return Ok(Box::new(crc32fast::Hasher::new()));
|
||||
}*/
|
||||
/*ChecksumMode::ChecksumCRC32C => {
|
||||
return Ok(Box::new(crc32::new(crc32.MakeTable(crc32.Castagnoli))));
|
||||
ChecksumMode::ChecksumCRC32 => {
|
||||
return Ok(ChecksumAlgorithm::Crc32.into_impl());
|
||||
}
|
||||
ChecksumMode::ChecksumCRC32C => {
|
||||
return Ok(ChecksumAlgorithm::Crc32c.into_impl());
|
||||
}
|
||||
ChecksumMode::ChecksumSHA1 => {
|
||||
return Ok(Box::new(sha1::new()));
|
||||
}*/
|
||||
ChecksumMode::ChecksumSHA256 => {
|
||||
return Ok(HashAlgorithm::SHA256);
|
||||
return Ok(ChecksumAlgorithm::Sha1.into_impl());
|
||||
}
|
||||
ChecksumMode::ChecksumSHA256 => {
|
||||
return Ok(ChecksumAlgorithm::Sha256.into_impl());
|
||||
}
|
||||
ChecksumMode::ChecksumCRC64NVME => {
|
||||
return Ok(ChecksumAlgorithm::Crc64Nvme.into_impl());
|
||||
}
|
||||
/*ChecksumMode::ChecksumCRC64NVME => {
|
||||
return Ok(Box::new(crc64nvme.New());
|
||||
}*/
|
||||
_ => return Err(std::io::Error::other("unsupported checksum type")),
|
||||
}
|
||||
}
|
||||
@@ -170,7 +191,8 @@ impl ChecksumMode {
|
||||
return Ok("".to_string());
|
||||
}
|
||||
let mut h = self.hasher()?;
|
||||
let hash = h.hash_encode(b);
|
||||
h.update(b);
|
||||
let hash = h.finalize();
|
||||
Ok(base64_encode(hash.as_ref()))
|
||||
}
|
||||
|
||||
@@ -227,7 +249,8 @@ impl ChecksumMode {
|
||||
let c = self.base();
|
||||
let crc_bytes = Vec::<u8>::with_capacity(p.len() * self.raw_byte_len() as usize);
|
||||
let mut h = self.hasher()?;
|
||||
let hash = h.hash_encode(crc_bytes.as_ref());
|
||||
h.update(crc_bytes.as_ref());
|
||||
let hash = h.finalize();
|
||||
Ok(Checksum {
|
||||
checksum_type: self.clone(),
|
||||
r: hash.as_ref().to_vec(),
|
||||
|
||||
@@ -63,7 +63,7 @@ impl TransitionClient {
|
||||
//defer closeResponse(resp)
|
||||
//if resp != nil {
|
||||
if resp.status() != StatusCode::NO_CONTENT && resp.status() != StatusCode::OK {
|
||||
return Err(std::io::Error::other(http_resp_to_error_response(resp, vec![], bucket_name, "")));
|
||||
return Err(std::io::Error::other(http_resp_to_error_response(&resp, vec![], bucket_name, "")));
|
||||
}
|
||||
//}
|
||||
Ok(())
|
||||
@@ -98,7 +98,7 @@ impl TransitionClient {
|
||||
//defer closeResponse(resp)
|
||||
|
||||
if resp.status() != StatusCode::NO_CONTENT {
|
||||
return Err(std::io::Error::other(http_resp_to_error_response(resp, vec![], bucket_name, "")));
|
||||
return Err(std::io::Error::other(http_resp_to_error_response(&resp, vec![], bucket_name, "")));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -95,13 +95,13 @@ pub fn to_error_response(err: &std::io::Error) -> ErrorResponse {
|
||||
}
|
||||
|
||||
pub fn http_resp_to_error_response(
|
||||
resp: http::Response<Body>,
|
||||
resp: &http::Response<Body>,
|
||||
b: Vec<u8>,
|
||||
bucket_name: &str,
|
||||
object_name: &str,
|
||||
) -> ErrorResponse {
|
||||
let err_body = String::from_utf8(b).unwrap();
|
||||
let err_resp_ = serde_xml_rs::from_str::<ErrorResponse>(&err_body);
|
||||
let err_resp_ = quick_xml::de::from_str::<ErrorResponse>(&err_body);
|
||||
let mut err_resp = ErrorResponse::default();
|
||||
if err_resp_.is_err() {
|
||||
match resp.status() {
|
||||
|
||||
@@ -87,11 +87,11 @@ impl TransitionClient {
|
||||
|
||||
if resp.status() != http::StatusCode::OK {
|
||||
let b = resp.body().bytes().expect("err").to_vec();
|
||||
return Err(std::io::Error::other(http_resp_to_error_response(resp, b, bucket_name, object_name)));
|
||||
return Err(std::io::Error::other(http_resp_to_error_response(&resp, b, bucket_name, object_name)));
|
||||
}
|
||||
|
||||
let b = resp.body_mut().store_all_unlimited().await.unwrap().to_vec();
|
||||
let mut res = match serde_xml_rs::from_str::<AccessControlPolicy>(&String::from_utf8(b).unwrap()) {
|
||||
let mut res = match quick_xml::de::from_str::<AccessControlPolicy>(&String::from_utf8(b).unwrap()) {
|
||||
Ok(result) => result,
|
||||
Err(err) => {
|
||||
return Err(std::io::Error::other(err.to_string()));
|
||||
|
||||
@@ -144,7 +144,7 @@ impl ObjectAttributes {
|
||||
self.version_id = h.get(X_AMZ_VERSION_ID).unwrap().to_str().unwrap().to_string();
|
||||
|
||||
let b = resp.body_mut().store_all_unlimited().await.unwrap().to_vec();
|
||||
let mut response = match serde_xml_rs::from_str::<ObjectAttributesResponse>(&String::from_utf8(b).unwrap()) {
|
||||
let mut response = match quick_xml::de::from_str::<ObjectAttributesResponse>(&String::from_utf8(b).unwrap()) {
|
||||
Ok(result) => result,
|
||||
Err(err) => {
|
||||
return Err(std::io::Error::other(err.to_string()));
|
||||
@@ -226,7 +226,7 @@ impl TransitionClient {
|
||||
if resp.status() != http::StatusCode::OK {
|
||||
let b = resp.body_mut().store_all_unlimited().await.unwrap().to_vec();
|
||||
let err_body = String::from_utf8(b).unwrap();
|
||||
let mut er = match serde_xml_rs::from_str::<AccessControlPolicy>(&err_body) {
|
||||
let mut er = match quick_xml::de::from_str::<AccessControlPolicy>(&err_body) {
|
||||
Ok(result) => result,
|
||||
Err(err) => {
|
||||
return Err(std::io::Error::other(err.to_string()));
|
||||
|
||||
@@ -98,12 +98,12 @@ impl TransitionClient {
|
||||
)
|
||||
.await?;
|
||||
if resp.status() != StatusCode::OK {
|
||||
return Err(std::io::Error::other(http_resp_to_error_response(resp, vec![], bucket_name, "")));
|
||||
return Err(std::io::Error::other(http_resp_to_error_response(&resp, vec![], bucket_name, "")));
|
||||
}
|
||||
|
||||
//let mut list_bucket_result = ListBucketV2Result::default();
|
||||
let b = resp.body_mut().store_all_unlimited().await.unwrap().to_vec();
|
||||
let mut list_bucket_result = match serde_xml_rs::from_str::<ListBucketV2Result>(&String::from_utf8(b).unwrap()) {
|
||||
let mut list_bucket_result = match quick_xml::de::from_str::<ListBucketV2Result>(&String::from_utf8(b).unwrap()) {
|
||||
Ok(result) => result,
|
||||
Err(err) => {
|
||||
return Err(std::io::Error::other(err.to_string()));
|
||||
|
||||
@@ -85,7 +85,7 @@ pub struct PutObjectOptions {
|
||||
pub expires: OffsetDateTime,
|
||||
pub mode: ObjectLockRetentionMode,
|
||||
pub retain_until_date: OffsetDateTime,
|
||||
//pub server_side_encryption: encrypt.ServerSide,
|
||||
//pub server_side_encryption: encrypt::ServerSide,
|
||||
pub num_threads: u64,
|
||||
pub storage_class: String,
|
||||
pub website_redirect_location: String,
|
||||
@@ -135,7 +135,7 @@ impl Default for PutObjectOptions {
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl PutObjectOptions {
|
||||
fn set_match_tag(&mut self, etag: &str) {
|
||||
fn set_match_etag(&mut self, etag: &str) {
|
||||
if etag == "*" {
|
||||
self.custom_header
|
||||
.insert("If-Match", HeaderValue::from_str("*").expect("err"));
|
||||
@@ -145,7 +145,7 @@ impl PutObjectOptions {
|
||||
}
|
||||
}
|
||||
|
||||
fn set_match_tag_except(&mut self, etag: &str) {
|
||||
fn set_match_etag_except(&mut self, etag: &str) {
|
||||
if etag == "*" {
|
||||
self.custom_header
|
||||
.insert("If-None-Match", HeaderValue::from_str("*").expect("err"));
|
||||
@@ -366,7 +366,8 @@ impl TransitionClient {
|
||||
md5_base64 = base64_encode(hash.as_ref());
|
||||
} else {
|
||||
let mut crc = opts.auto_checksum.hasher()?;
|
||||
let csum = crc.hash_encode(&buf[..length]);
|
||||
crc.update(&buf[..length]);
|
||||
let csum = crc.finalize();
|
||||
|
||||
if let Ok(header_name) = HeaderName::from_bytes(opts.auto_checksum.key().as_bytes()) {
|
||||
custom_header.insert(header_name, base64_encode(csum.as_ref()).parse().expect("err"));
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
// 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.
|
||||
#![allow(unused_imports)]
|
||||
#![allow(unused_variables)]
|
||||
#![allow(unused_mut)]
|
||||
#![allow(unused_assignments)]
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
// 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.
|
||||
#![allow(unused_imports)]
|
||||
#![allow(unused_variables)]
|
||||
#![allow(unused_mut)]
|
||||
#![allow(unused_assignments)]
|
||||
@@ -19,20 +18,14 @@
|
||||
#![allow(clippy::all)]
|
||||
|
||||
use bytes::Bytes;
|
||||
use http::{HeaderMap, HeaderName, HeaderValue, StatusCode};
|
||||
use http::{HeaderMap, HeaderName, StatusCode};
|
||||
use s3s::S3ErrorCode;
|
||||
use std::io::Read;
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
use time::{OffsetDateTime, format_description};
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use std::collections::HashMap;
|
||||
use time::OffsetDateTime;
|
||||
use tracing::warn;
|
||||
use tracing::{error, info};
|
||||
use url::form_urlencoded::Serializer;
|
||||
use uuid::Uuid;
|
||||
|
||||
use s3s::header::{X_AMZ_EXPIRATION, X_AMZ_VERSION_ID};
|
||||
use s3s::{Body, dto::StreamingBlob};
|
||||
//use crate::disk::{Reader, BufferReader};
|
||||
use crate::checksum::ChecksumMode;
|
||||
use crate::client::{
|
||||
api_error_response::{
|
||||
err_entity_too_large, err_entity_too_small, err_invalid_argument, http_resp_to_error_response, to_error_response,
|
||||
@@ -42,15 +35,11 @@ use crate::client::{
|
||||
api_s3_datatypes::{
|
||||
CompleteMultipartUpload, CompleteMultipartUploadResult, CompletePart, InitiateMultipartUploadResult, ObjectPart,
|
||||
},
|
||||
constants::{ABS_MIN_PART_SIZE, ISO8601_DATEFORMAT, MAX_PART_SIZE, MAX_SINGLE_PUT_OBJECT_SIZE},
|
||||
constants::{ISO8601_DATEFORMAT, MAX_PART_SIZE, MAX_SINGLE_PUT_OBJECT_SIZE},
|
||||
transition_api::{ReaderImpl, RequestMetadata, TransitionClient, UploadInfo},
|
||||
};
|
||||
use crate::{
|
||||
checksum::ChecksumMode,
|
||||
disk::DiskAPI,
|
||||
store_api::{GetObjectReader, StorageAPI},
|
||||
};
|
||||
use rustfs_utils::{crypto::base64_encode, path::trim_etag};
|
||||
use s3s::header::{X_AMZ_EXPIRATION, X_AMZ_VERSION_ID};
|
||||
|
||||
impl TransitionClient {
|
||||
pub async fn put_object_multipart(
|
||||
@@ -133,7 +122,8 @@ impl TransitionClient {
|
||||
//}
|
||||
if hash_sums.len() == 0 {
|
||||
let mut crc = opts.auto_checksum.hasher()?;
|
||||
let csum = crc.hash_encode(&buf[..length]);
|
||||
crc.update(&buf[..length]);
|
||||
let csum = crc.finalize();
|
||||
|
||||
if let Ok(header_name) = HeaderName::from_bytes(opts.auto_checksum.key().as_bytes()) {
|
||||
custom_header.insert(header_name, base64_encode(csum.as_ref()).parse().expect("err"));
|
||||
@@ -236,7 +226,12 @@ impl TransitionClient {
|
||||
let resp = self.execute_method(http::Method::POST, &mut req_metadata).await?;
|
||||
//if resp.is_none() {
|
||||
if resp.status() != StatusCode::OK {
|
||||
return Err(std::io::Error::other(http_resp_to_error_response(resp, vec![], bucket_name, object_name)));
|
||||
return Err(std::io::Error::other(http_resp_to_error_response(
|
||||
&resp,
|
||||
vec![],
|
||||
bucket_name,
|
||||
object_name,
|
||||
)));
|
||||
}
|
||||
//}
|
||||
let initiate_multipart_upload_result = InitiateMultipartUploadResult::default();
|
||||
@@ -293,7 +288,7 @@ impl TransitionClient {
|
||||
let resp = self.execute_method(http::Method::PUT, &mut req_metadata).await?;
|
||||
if resp.status() != StatusCode::OK {
|
||||
return Err(std::io::Error::other(http_resp_to_error_response(
|
||||
resp,
|
||||
&resp,
|
||||
vec![],
|
||||
&p.bucket_name.clone(),
|
||||
&p.object_name,
|
||||
|
||||
@@ -156,7 +156,8 @@ impl TransitionClient {
|
||||
md5_base64 = base64_encode(hash.as_ref());
|
||||
} else {
|
||||
let mut crc = opts.auto_checksum.hasher()?;
|
||||
let csum = crc.hash_encode(&buf[..length]);
|
||||
crc.update(&buf[..length]);
|
||||
let csum = crc.finalize();
|
||||
|
||||
if let Ok(header_name) = HeaderName::from_bytes(opts.auto_checksum.key().as_bytes()) {
|
||||
custom_header.insert(header_name, base64_encode(csum.as_ref()).parse().expect("err"));
|
||||
@@ -303,7 +304,8 @@ impl TransitionClient {
|
||||
let mut custom_header = HeaderMap::new();
|
||||
if !opts.send_content_md5 {
|
||||
let mut crc = opts.auto_checksum.hasher()?;
|
||||
let csum = crc.hash_encode(&buf[..length]);
|
||||
crc.update(&buf[..length]);
|
||||
let csum = crc.finalize();
|
||||
|
||||
if let Ok(header_name) = HeaderName::from_bytes(opts.auto_checksum.key().as_bytes()) {
|
||||
custom_header.insert(header_name, base64_encode(csum.as_ref()).parse().expect("err"));
|
||||
@@ -477,7 +479,12 @@ impl TransitionClient {
|
||||
let resp = self.execute_method(http::Method::PUT, &mut req_metadata).await?;
|
||||
|
||||
if resp.status() != StatusCode::OK {
|
||||
return Err(std::io::Error::other(http_resp_to_error_response(resp, vec![], bucket_name, object_name)));
|
||||
return Err(std::io::Error::other(http_resp_to_error_response(
|
||||
&resp,
|
||||
vec![],
|
||||
bucket_name,
|
||||
object_name,
|
||||
)));
|
||||
}
|
||||
|
||||
let (exp_time, rule_id) = if let Some(h_x_amz_expiration) = resp.headers().get(X_AMZ_EXPIRATION) {
|
||||
|
||||
@@ -425,7 +425,12 @@ impl TransitionClient {
|
||||
};
|
||||
}
|
||||
_ => {
|
||||
return Err(std::io::Error::other(http_resp_to_error_response(resp, vec![], bucket_name, object_name)));
|
||||
return Err(std::io::Error::other(http_resp_to_error_response(
|
||||
&resp,
|
||||
vec![],
|
||||
bucket_name,
|
||||
object_name,
|
||||
)));
|
||||
}
|
||||
}
|
||||
return Err(std::io::Error::other(error_response));
|
||||
|
||||
@@ -125,7 +125,7 @@ impl TransitionClient {
|
||||
version_id: &str,
|
||||
restore_req: &RestoreRequest,
|
||||
) -> Result<(), std::io::Error> {
|
||||
let restore_request = match serde_xml_rs::to_string(restore_req) {
|
||||
let restore_request = match quick_xml::se::to_string(restore_req) {
|
||||
Ok(buf) => buf,
|
||||
Err(e) => {
|
||||
return Err(std::io::Error::other(e));
|
||||
@@ -165,7 +165,7 @@ impl TransitionClient {
|
||||
|
||||
let b = resp.body().bytes().expect("err").to_vec();
|
||||
if resp.status() != http::StatusCode::ACCEPTED && resp.status() != http::StatusCode::OK {
|
||||
return Err(std::io::Error::other(http_resp_to_error_response(resp, b, bucket_name, "")));
|
||||
return Err(std::io::Error::other(http_resp_to_error_response(&resp, b, bucket_name, "")));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -279,7 +279,7 @@ pub struct CompleteMultipartUpload {
|
||||
impl CompleteMultipartUpload {
|
||||
pub fn marshal_msg(&self) -> Result<String, std::io::Error> {
|
||||
//let buf = serde_json::to_string(self)?;
|
||||
let buf = match serde_xml_rs::to_string(self) {
|
||||
let buf = match quick_xml::se::to_string(self) {
|
||||
Ok(buf) => buf,
|
||||
Err(e) => {
|
||||
return Err(std::io::Error::other(e));
|
||||
@@ -329,7 +329,7 @@ pub struct DeleteMultiObjects {
|
||||
impl DeleteMultiObjects {
|
||||
pub fn marshal_msg(&self) -> Result<String, std::io::Error> {
|
||||
//let buf = serde_json::to_string(self)?;
|
||||
let buf = match serde_xml_rs::to_string(self) {
|
||||
let buf = match quick_xml::se::to_string(self) {
|
||||
Ok(buf) => buf,
|
||||
Err(e) => {
|
||||
return Err(std::io::Error::other(e));
|
||||
|
||||
@@ -59,7 +59,7 @@ impl TransitionClient {
|
||||
|
||||
if let Ok(resp) = resp {
|
||||
let b = resp.body().bytes().expect("err").to_vec();
|
||||
let resperr = http_resp_to_error_response(resp, b, bucket_name, "");
|
||||
let resperr = http_resp_to_error_response(&resp, b, bucket_name, "");
|
||||
/*if to_error_response(resperr).code == "NoSuchBucket" {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
@@ -177,7 +177,7 @@ impl TransitionClient {
|
||||
async fn process_bucket_location_response(mut resp: http::Response<Body>, bucket_name: &str) -> Result<String, std::io::Error> {
|
||||
//if resp != nil {
|
||||
if resp.status() != StatusCode::OK {
|
||||
let err_resp = http_resp_to_error_response(resp, vec![], bucket_name, "");
|
||||
let err_resp = http_resp_to_error_response(&resp, vec![], bucket_name, "");
|
||||
match err_resp.code {
|
||||
S3ErrorCode::NotImplemented => {
|
||||
match err_resp.server.as_str() {
|
||||
@@ -208,7 +208,7 @@ async fn process_bucket_location_response(mut resp: http::Response<Body>, bucket
|
||||
//}
|
||||
|
||||
let b = resp.body_mut().store_all_unlimited().await.unwrap().to_vec();
|
||||
let Document(location_constraint) = serde_xml_rs::from_str::<Document>(&String::from_utf8(b).unwrap()).unwrap();
|
||||
let Document(location_constraint) = quick_xml::de::from_str::<Document>(&String::from_utf8(b).unwrap()).unwrap();
|
||||
|
||||
let mut location = location_constraint;
|
||||
if location == "" {
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
#![allow(clippy::all)]
|
||||
|
||||
use bytes::Bytes;
|
||||
use futures::Future;
|
||||
use futures::{Future, StreamExt};
|
||||
use http::{HeaderMap, HeaderName};
|
||||
use http::{
|
||||
HeaderValue, Response, StatusCode,
|
||||
@@ -65,7 +65,9 @@ use crate::{checksum::ChecksumMode, store_api::GetObjectReader};
|
||||
use rustfs_rio::HashReader;
|
||||
use rustfs_utils::{
|
||||
net::get_endpoint_url,
|
||||
retry::{MAX_RETRY, new_retry_timer},
|
||||
retry::{
|
||||
DEFAULT_RETRY_CAP, DEFAULT_RETRY_UNIT, MAX_JITTER, MAX_RETRY, RetryTimer, is_http_status_retryable, is_s3code_retryable,
|
||||
},
|
||||
};
|
||||
use s3s::S3ErrorCode;
|
||||
use s3s::dto::ReplicationStatus;
|
||||
@@ -186,6 +188,7 @@ impl TransitionClient {
|
||||
|
||||
clnt.trailing_header_support = opts.trailing_headers && clnt.override_signer_type == SignatureType::SignatureV4;
|
||||
|
||||
clnt.max_retries = MAX_RETRY;
|
||||
if opts.max_retries > 0 {
|
||||
clnt.max_retries = opts.max_retries;
|
||||
}
|
||||
@@ -313,12 +316,9 @@ impl TransitionClient {
|
||||
}
|
||||
//}
|
||||
|
||||
//let mut retry_timer = RetryTimer::new();
|
||||
//while let Some(v) = retry_timer.next().await {
|
||||
for _ in [1; 1]
|
||||
/*new_retry_timer(req_retry, default_retry_unit, default_retry_cap, max_jitter)*/
|
||||
{
|
||||
let req = self.new_request(method, metadata).await?;
|
||||
let mut retry_timer = RetryTimer::new(req_retry, DEFAULT_RETRY_UNIT, DEFAULT_RETRY_CAP, MAX_JITTER, self.random);
|
||||
while let Some(v) = retry_timer.next().await {
|
||||
let req = self.new_request(&method, metadata).await?;
|
||||
|
||||
resp = self.doit(req).await?;
|
||||
|
||||
@@ -329,7 +329,7 @@ impl TransitionClient {
|
||||
}
|
||||
|
||||
let b = resp.body_mut().store_all_unlimited().await.unwrap().to_vec();
|
||||
let err_response = http_resp_to_error_response(resp, b.clone(), &metadata.bucket_name, &metadata.object_name);
|
||||
let err_response = http_resp_to_error_response(&resp, b.clone(), &metadata.bucket_name, &metadata.object_name);
|
||||
|
||||
if self.region == "" {
|
||||
match err_response.code {
|
||||
@@ -360,6 +360,14 @@ impl TransitionClient {
|
||||
}
|
||||
}
|
||||
|
||||
if is_s3code_retryable(err_response.code.as_str()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if is_http_status_retryable(&resp.status()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -368,7 +376,7 @@ impl TransitionClient {
|
||||
|
||||
async fn new_request(
|
||||
&self,
|
||||
method: http::Method,
|
||||
method: &http::Method,
|
||||
metadata: &mut RequestMetadata,
|
||||
) -> Result<http::Request<Body>, std::io::Error> {
|
||||
let location = metadata.bucket_location.clone();
|
||||
|
||||
@@ -2014,6 +2014,8 @@ impl ReplicateObjectInfo {
|
||||
version_id: Uuid::try_parse(&self.version_id).ok(),
|
||||
delete_marker: self.delete_marker,
|
||||
transitioned_object: TransitionedObject::default(),
|
||||
restore_ongoing: false,
|
||||
restore_expires: Some(OffsetDateTime::now_utc()),
|
||||
user_tags: self.user_tags.clone(),
|
||||
parts: Vec::new(),
|
||||
is_latest: true,
|
||||
|
||||
@@ -12,16 +12,16 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use super::{Config, GLOBAL_StorageClass, storageclass};
|
||||
use crate::config::{Config, GLOBAL_STORAGE_CLASS, storageclass};
|
||||
use crate::disk::RUSTFS_META_BUCKET;
|
||||
use crate::error::{Error, Result};
|
||||
use crate::store_api::{ObjectInfo, ObjectOptions, PutObjReader, StorageAPI};
|
||||
use http::HeaderMap;
|
||||
use lazy_static::lazy_static;
|
||||
use rustfs_config::DEFAULT_DELIMITER;
|
||||
use rustfs_utils::path::SLASH_SEPARATOR;
|
||||
use std::collections::HashSet;
|
||||
use std::sync::Arc;
|
||||
use std::sync::LazyLock;
|
||||
use tracing::{error, warn};
|
||||
|
||||
pub const CONFIG_PREFIX: &str = "config";
|
||||
@@ -29,14 +29,13 @@ const CONFIG_FILE: &str = "config.json";
|
||||
|
||||
pub const STORAGE_CLASS_SUB_SYS: &str = "storage_class";
|
||||
|
||||
lazy_static! {
|
||||
static ref CONFIG_BUCKET: String = format!("{}{}{}", RUSTFS_META_BUCKET, SLASH_SEPARATOR, CONFIG_PREFIX);
|
||||
static ref SubSystemsDynamic: HashSet<String> = {
|
||||
let mut h = HashSet::new();
|
||||
h.insert(STORAGE_CLASS_SUB_SYS.to_owned());
|
||||
h
|
||||
};
|
||||
}
|
||||
static CONFIG_BUCKET: LazyLock<String> = LazyLock::new(|| format!("{RUSTFS_META_BUCKET}{SLASH_SEPARATOR}{CONFIG_PREFIX}"));
|
||||
|
||||
static SUB_SYSTEMS_DYNAMIC: LazyLock<HashSet<String>> = LazyLock::new(|| {
|
||||
let mut h = HashSet::new();
|
||||
h.insert(STORAGE_CLASS_SUB_SYS.to_owned());
|
||||
h
|
||||
});
|
||||
pub async fn read_config<S: StorageAPI>(api: Arc<S>, file: &str) -> Result<Vec<u8>> {
|
||||
let (data, _obj) = read_config_with_metadata(api, file, &ObjectOptions::default()).await?;
|
||||
Ok(data)
|
||||
@@ -197,7 +196,7 @@ pub async fn lookup_configs<S: StorageAPI>(cfg: &mut Config, api: Arc<S>) {
|
||||
}
|
||||
|
||||
async fn apply_dynamic_config<S: StorageAPI>(cfg: &mut Config, api: Arc<S>) -> Result<()> {
|
||||
for key in SubSystemsDynamic.iter() {
|
||||
for key in SUB_SYSTEMS_DYNAMIC.iter() {
|
||||
apply_dynamic_config_for_sub_sys(cfg, api.clone(), key).await?;
|
||||
}
|
||||
|
||||
@@ -212,9 +211,9 @@ async fn apply_dynamic_config_for_sub_sys<S: StorageAPI>(cfg: &mut Config, api:
|
||||
for (i, count) in set_drive_counts.iter().enumerate() {
|
||||
match storageclass::lookup_config(&kvs, *count) {
|
||||
Ok(res) => {
|
||||
if i == 0 && GLOBAL_StorageClass.get().is_none() {
|
||||
if let Err(r) = GLOBAL_StorageClass.set(res) {
|
||||
error!("GLOBAL_StorageClass.set failed {:?}", r);
|
||||
if i == 0 && GLOBAL_STORAGE_CLASS.get().is_none() {
|
||||
if let Err(r) = GLOBAL_STORAGE_CLASS.set(res) {
|
||||
error!("GLOBAL_STORAGE_CLASS.set failed {:?}", r);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,26 +21,17 @@ pub mod storageclass;
|
||||
use crate::error::Result;
|
||||
use crate::store::ECStore;
|
||||
use com::{STORAGE_CLASS_SUB_SYS, lookup_configs, read_config_without_migrate};
|
||||
use lazy_static::lazy_static;
|
||||
use rustfs_config::DEFAULT_DELIMITER;
|
||||
use rustfs_config::notify::{COMMENT_KEY, NOTIFY_MQTT_SUB_SYS, NOTIFY_WEBHOOK_SUB_SYS};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::LazyLock;
|
||||
use std::sync::{Arc, OnceLock};
|
||||
|
||||
lazy_static! {
|
||||
pub static ref GLOBAL_StorageClass: OnceLock<storageclass::Config> = OnceLock::new();
|
||||
pub static ref DefaultKVS: OnceLock<HashMap<String, KVS>> = OnceLock::new();
|
||||
pub static ref GLOBAL_ServerConfig: OnceLock<Config> = OnceLock::new();
|
||||
pub static ref GLOBAL_ConfigSys: ConfigSys = ConfigSys::new();
|
||||
}
|
||||
|
||||
/// Standard config keys and values.
|
||||
pub const ENABLE_KEY: &str = "enable";
|
||||
pub const COMMENT_KEY: &str = "comment";
|
||||
|
||||
/// Enable values
|
||||
pub const ENABLE_ON: &str = "on";
|
||||
pub const ENABLE_OFF: &str = "off";
|
||||
pub static GLOBAL_STORAGE_CLASS: LazyLock<OnceLock<storageclass::Config>> = LazyLock::new(OnceLock::new);
|
||||
pub static DEFAULT_KVS: LazyLock<OnceLock<HashMap<String, KVS>>> = LazyLock::new(OnceLock::new);
|
||||
pub static GLOBAL_SERVER_CONFIG: LazyLock<OnceLock<Config>> = LazyLock::new(OnceLock::new);
|
||||
pub static GLOBAL_CONFIG_SYS: LazyLock<ConfigSys> = LazyLock::new(ConfigSys::new);
|
||||
|
||||
pub const ENV_ACCESS_KEY: &str = "RUSTFS_ACCESS_KEY";
|
||||
pub const ENV_SECRET_KEY: &str = "RUSTFS_SECRET_KEY";
|
||||
@@ -66,7 +57,7 @@ impl ConfigSys {
|
||||
|
||||
lookup_configs(&mut cfg, api).await;
|
||||
|
||||
let _ = GLOBAL_ServerConfig.set(cfg);
|
||||
let _ = GLOBAL_SERVER_CONFIG.set(cfg);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -131,6 +122,28 @@ impl KVS {
|
||||
|
||||
keys
|
||||
}
|
||||
|
||||
/// Insert or update a pair of key/values in KVS
|
||||
pub fn insert(&mut self, key: String, value: String) {
|
||||
for kv in self.0.iter_mut() {
|
||||
if kv.key == key {
|
||||
kv.value = value.clone();
|
||||
return;
|
||||
}
|
||||
}
|
||||
self.0.push(KV {
|
||||
key,
|
||||
value,
|
||||
hidden_if_empty: false,
|
||||
});
|
||||
}
|
||||
|
||||
/// Merge all entries from another KVS to the current instance
|
||||
pub fn extend(&mut self, other: KVS) {
|
||||
for KV { key, value, .. } in other.0.into_iter() {
|
||||
self.insert(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -159,7 +172,7 @@ impl Config {
|
||||
}
|
||||
|
||||
pub fn set_defaults(&mut self) {
|
||||
if let Some(defaults) = DefaultKVS.get() {
|
||||
if let Some(defaults) = DEFAULT_KVS.get() {
|
||||
for (k, v) in defaults.iter() {
|
||||
if !self.0.contains_key(k) {
|
||||
let mut default = HashMap::new();
|
||||
@@ -198,20 +211,17 @@ pub fn register_default_kvs(kvs: HashMap<String, KVS>) {
|
||||
p.insert(k, v);
|
||||
}
|
||||
|
||||
let _ = DefaultKVS.set(p);
|
||||
let _ = DEFAULT_KVS.set(p);
|
||||
}
|
||||
|
||||
pub fn init() {
|
||||
let mut kvs = HashMap::new();
|
||||
// Load storageclass default configuration
|
||||
kvs.insert(STORAGE_CLASS_SUB_SYS.to_owned(), storageclass::DefaultKVS.clone());
|
||||
kvs.insert(STORAGE_CLASS_SUB_SYS.to_owned(), storageclass::DEFAULT_KVS.clone());
|
||||
// New: Loading default configurations for notify_webhook and notify_mqtt
|
||||
// Referring subsystem names through constants to improve the readability and maintainability of the code
|
||||
kvs.insert(
|
||||
rustfs_config::notify::NOTIFY_WEBHOOK_SUB_SYS.to_owned(),
|
||||
notify::DefaultWebhookKVS.clone(),
|
||||
);
|
||||
kvs.insert(rustfs_config::notify::NOTIFY_MQTT_SUB_SYS.to_owned(), notify::DefaultMqttKVS.clone());
|
||||
kvs.insert(NOTIFY_WEBHOOK_SUB_SYS.to_owned(), notify::DEFAULT_WEBHOOK_KVS.clone());
|
||||
kvs.insert(NOTIFY_MQTT_SUB_SYS.to_owned(), notify::DEFAULT_MQTT_KVS.clone());
|
||||
|
||||
// Register all default configurations
|
||||
register_default_kvs(kvs)
|
||||
|
||||
@@ -12,40 +12,120 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use crate::config::{ENABLE_KEY, ENABLE_OFF, KV, KVS};
|
||||
use lazy_static::lazy_static;
|
||||
use crate::config::{KV, KVS};
|
||||
use rustfs_config::notify::{
|
||||
DEFAULT_DIR, DEFAULT_LIMIT, MQTT_BROKER, MQTT_KEEP_ALIVE_INTERVAL, MQTT_PASSWORD, MQTT_QOS, MQTT_QUEUE_DIR, MQTT_QUEUE_LIMIT,
|
||||
MQTT_RECONNECT_INTERVAL, MQTT_TOPIC, MQTT_USERNAME, WEBHOOK_AUTH_TOKEN, WEBHOOK_CLIENT_CERT, WEBHOOK_CLIENT_KEY,
|
||||
WEBHOOK_ENDPOINT, WEBHOOK_QUEUE_DIR, WEBHOOK_QUEUE_LIMIT,
|
||||
COMMENT_KEY, DEFAULT_DIR, DEFAULT_LIMIT, ENABLE_KEY, ENABLE_OFF, MQTT_BROKER, MQTT_KEEP_ALIVE_INTERVAL, MQTT_PASSWORD,
|
||||
MQTT_QOS, MQTT_QUEUE_DIR, MQTT_QUEUE_LIMIT, MQTT_RECONNECT_INTERVAL, MQTT_TOPIC, MQTT_USERNAME, WEBHOOK_AUTH_TOKEN,
|
||||
WEBHOOK_CLIENT_CERT, WEBHOOK_CLIENT_KEY, WEBHOOK_ENDPOINT, WEBHOOK_QUEUE_DIR, WEBHOOK_QUEUE_LIMIT,
|
||||
};
|
||||
use std::sync::LazyLock;
|
||||
|
||||
lazy_static! {
|
||||
/// The default configuration collection of webhooks,
|
||||
/// Use lazy_static! to ensure that these configurations are initialized only once during the program life cycle, enabling high-performance lazy loading.
|
||||
pub static ref DefaultWebhookKVS: KVS = KVS(vec![
|
||||
KV { key: ENABLE_KEY.to_owned(), value: ENABLE_OFF.to_owned(), hidden_if_empty: false },
|
||||
KV { key: WEBHOOK_ENDPOINT.to_owned(), value: "".to_owned(), hidden_if_empty: false },
|
||||
/// The default configuration collection of webhooks,
|
||||
/// Initialized only once during the program life cycle, enabling high-performance lazy loading.
|
||||
pub static DEFAULT_WEBHOOK_KVS: LazyLock<KVS> = LazyLock::new(|| {
|
||||
KVS(vec![
|
||||
KV {
|
||||
key: ENABLE_KEY.to_owned(),
|
||||
value: ENABLE_OFF.to_owned(),
|
||||
hidden_if_empty: false,
|
||||
},
|
||||
KV {
|
||||
key: WEBHOOK_ENDPOINT.to_owned(),
|
||||
value: "".to_owned(),
|
||||
hidden_if_empty: false,
|
||||
},
|
||||
// Sensitive information such as authentication tokens is hidden when the value is empty, enhancing security
|
||||
KV { key: WEBHOOK_AUTH_TOKEN.to_owned(), value: "".to_owned(), hidden_if_empty: true },
|
||||
KV { key: WEBHOOK_QUEUE_LIMIT.to_owned(), value: DEFAULT_LIMIT.to_string().to_owned(), hidden_if_empty: false },
|
||||
KV { key: WEBHOOK_QUEUE_DIR.to_owned(), value: DEFAULT_DIR.to_owned(), hidden_if_empty: false },
|
||||
KV { key: WEBHOOK_CLIENT_CERT.to_owned(), value: "".to_owned(), hidden_if_empty: false },
|
||||
KV { key: WEBHOOK_CLIENT_KEY.to_owned(), value: "".to_owned(), hidden_if_empty: false },
|
||||
]);
|
||||
KV {
|
||||
key: WEBHOOK_AUTH_TOKEN.to_owned(),
|
||||
value: "".to_owned(),
|
||||
hidden_if_empty: true,
|
||||
},
|
||||
KV {
|
||||
key: WEBHOOK_QUEUE_LIMIT.to_owned(),
|
||||
value: DEFAULT_LIMIT.to_string(),
|
||||
hidden_if_empty: false,
|
||||
},
|
||||
KV {
|
||||
key: WEBHOOK_QUEUE_DIR.to_owned(),
|
||||
value: DEFAULT_DIR.to_owned(),
|
||||
hidden_if_empty: false,
|
||||
},
|
||||
KV {
|
||||
key: WEBHOOK_CLIENT_CERT.to_owned(),
|
||||
value: "".to_owned(),
|
||||
hidden_if_empty: false,
|
||||
},
|
||||
KV {
|
||||
key: WEBHOOK_CLIENT_KEY.to_owned(),
|
||||
value: "".to_owned(),
|
||||
hidden_if_empty: false,
|
||||
},
|
||||
KV {
|
||||
key: COMMENT_KEY.to_owned(),
|
||||
value: "".to_owned(),
|
||||
hidden_if_empty: false,
|
||||
},
|
||||
])
|
||||
});
|
||||
|
||||
/// MQTT's default configuration collection
|
||||
pub static ref DefaultMqttKVS: KVS = KVS(vec![
|
||||
KV { key: ENABLE_KEY.to_owned(), value: ENABLE_OFF.to_owned(), hidden_if_empty: false },
|
||||
KV { key: MQTT_BROKER.to_owned(), value: "".to_owned(), hidden_if_empty: false },
|
||||
KV { key: MQTT_TOPIC.to_owned(), value: "".to_owned(), hidden_if_empty: false },
|
||||
/// MQTT's default configuration collection
|
||||
pub static DEFAULT_MQTT_KVS: LazyLock<KVS> = LazyLock::new(|| {
|
||||
KVS(vec![
|
||||
KV {
|
||||
key: ENABLE_KEY.to_owned(),
|
||||
value: ENABLE_OFF.to_owned(),
|
||||
hidden_if_empty: false,
|
||||
},
|
||||
KV {
|
||||
key: MQTT_BROKER.to_owned(),
|
||||
value: "".to_owned(),
|
||||
hidden_if_empty: false,
|
||||
},
|
||||
KV {
|
||||
key: MQTT_TOPIC.to_owned(),
|
||||
value: "".to_owned(),
|
||||
hidden_if_empty: false,
|
||||
},
|
||||
// Sensitive information such as passwords are hidden when the value is empty
|
||||
KV { key: MQTT_PASSWORD.to_owned(), value: "".to_owned(), hidden_if_empty: true },
|
||||
KV { key: MQTT_USERNAME.to_owned(), value: "".to_owned(), hidden_if_empty: false },
|
||||
KV { key: MQTT_QOS.to_owned(), value: "0".to_owned(), hidden_if_empty: false },
|
||||
KV { key: MQTT_KEEP_ALIVE_INTERVAL.to_owned(), value: "0s".to_owned(), hidden_if_empty: false },
|
||||
KV { key: MQTT_RECONNECT_INTERVAL.to_owned(), value: "0s".to_owned(), hidden_if_empty: false },
|
||||
KV { key: MQTT_QUEUE_DIR.to_owned(), value: DEFAULT_DIR.to_owned(), hidden_if_empty: false },
|
||||
KV { key: MQTT_QUEUE_LIMIT.to_owned(), value: DEFAULT_LIMIT.to_string().to_owned(), hidden_if_empty: false },
|
||||
]);
|
||||
}
|
||||
KV {
|
||||
key: MQTT_PASSWORD.to_owned(),
|
||||
value: "".to_owned(),
|
||||
hidden_if_empty: true,
|
||||
},
|
||||
KV {
|
||||
key: MQTT_USERNAME.to_owned(),
|
||||
value: "".to_owned(),
|
||||
hidden_if_empty: false,
|
||||
},
|
||||
KV {
|
||||
key: MQTT_QOS.to_owned(),
|
||||
value: "0".to_owned(),
|
||||
hidden_if_empty: false,
|
||||
},
|
||||
KV {
|
||||
key: MQTT_KEEP_ALIVE_INTERVAL.to_owned(),
|
||||
value: "0s".to_owned(),
|
||||
hidden_if_empty: false,
|
||||
},
|
||||
KV {
|
||||
key: MQTT_RECONNECT_INTERVAL.to_owned(),
|
||||
value: "0s".to_owned(),
|
||||
hidden_if_empty: false,
|
||||
},
|
||||
KV {
|
||||
key: MQTT_QUEUE_DIR.to_owned(),
|
||||
value: DEFAULT_DIR.to_owned(),
|
||||
hidden_if_empty: false,
|
||||
},
|
||||
KV {
|
||||
key: MQTT_QUEUE_LIMIT.to_owned(),
|
||||
value: DEFAULT_LIMIT.to_string(),
|
||||
hidden_if_empty: false,
|
||||
},
|
||||
KV {
|
||||
key: COMMENT_KEY.to_owned(),
|
||||
value: "".to_owned(),
|
||||
hidden_if_empty: false,
|
||||
},
|
||||
])
|
||||
});
|
||||
|
||||
@@ -15,9 +15,9 @@
|
||||
use super::KVS;
|
||||
use crate::config::KV;
|
||||
use crate::error::{Error, Result};
|
||||
use lazy_static::lazy_static;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::env;
|
||||
use std::sync::LazyLock;
|
||||
use tracing::warn;
|
||||
|
||||
/// Default parity count for a given drive count
|
||||
@@ -62,34 +62,32 @@ pub const DEFAULT_RRS_PARITY: usize = 1;
|
||||
|
||||
pub static DEFAULT_INLINE_BLOCK: usize = 128 * 1024;
|
||||
|
||||
lazy_static! {
|
||||
pub static ref DefaultKVS: KVS = {
|
||||
let kvs = vec![
|
||||
KV {
|
||||
key: CLASS_STANDARD.to_owned(),
|
||||
value: "".to_owned(),
|
||||
hidden_if_empty: false,
|
||||
},
|
||||
KV {
|
||||
key: CLASS_RRS.to_owned(),
|
||||
value: "EC:1".to_owned(),
|
||||
hidden_if_empty: false,
|
||||
},
|
||||
KV {
|
||||
key: OPTIMIZE.to_owned(),
|
||||
value: "availability".to_owned(),
|
||||
hidden_if_empty: false,
|
||||
},
|
||||
KV {
|
||||
key: INLINE_BLOCK.to_owned(),
|
||||
value: "".to_owned(),
|
||||
hidden_if_empty: true,
|
||||
},
|
||||
];
|
||||
pub static DEFAULT_KVS: LazyLock<KVS> = LazyLock::new(|| {
|
||||
let kvs = vec![
|
||||
KV {
|
||||
key: CLASS_STANDARD.to_owned(),
|
||||
value: "".to_owned(),
|
||||
hidden_if_empty: false,
|
||||
},
|
||||
KV {
|
||||
key: CLASS_RRS.to_owned(),
|
||||
value: "EC:1".to_owned(),
|
||||
hidden_if_empty: false,
|
||||
},
|
||||
KV {
|
||||
key: OPTIMIZE.to_owned(),
|
||||
value: "availability".to_owned(),
|
||||
hidden_if_empty: false,
|
||||
},
|
||||
KV {
|
||||
key: INLINE_BLOCK.to_owned(),
|
||||
value: "".to_owned(),
|
||||
hidden_if_empty: true,
|
||||
},
|
||||
];
|
||||
|
||||
KVS(kvs)
|
||||
};
|
||||
}
|
||||
KVS(kvs)
|
||||
});
|
||||
|
||||
// StorageClass - holds storage class information
|
||||
#[derive(Serialize, Deserialize, Debug, Default)]
|
||||
|
||||
@@ -36,8 +36,6 @@ pub const DISK_MIN_INODES: u64 = 1000;
|
||||
pub const DISK_FILL_FRACTION: f64 = 0.99;
|
||||
pub const DISK_RESERVE_FRACTION: f64 = 0.15;
|
||||
|
||||
pub const DEFAULT_PORT: u16 = 9000;
|
||||
|
||||
lazy_static! {
|
||||
static ref GLOBAL_RUSTFS_PORT: OnceLock<u16> = OnceLock::new();
|
||||
pub static ref GLOBAL_OBJECT_API: OnceLock<Arc<ECStore>> = OnceLock::new();
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
#![allow(unused_imports)]
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@@ -12,6 +11,8 @@
|
||||
// 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.
|
||||
|
||||
#![allow(unused_imports)]
|
||||
#![allow(unused_variables)]
|
||||
|
||||
use crate::bitrot::{create_bitrot_reader, create_bitrot_writer};
|
||||
@@ -33,7 +34,7 @@ use crate::store_api::{ListPartsInfo, ObjectToDelete};
|
||||
use crate::{
|
||||
bucket::lifecycle::bucket_lifecycle_ops::{gen_transition_objname, get_transitioned_object_reader, put_restore_opts},
|
||||
cache_value::metacache_set::{ListPathRawOptions, list_path_raw},
|
||||
config::{GLOBAL_StorageClass, storageclass},
|
||||
config::{GLOBAL_STORAGE_CLASS, storageclass},
|
||||
disk::{
|
||||
CheckPartsResp, DeleteOptions, DiskAPI, DiskInfo, DiskInfoOptions, DiskOption, DiskStore, FileInfoVersions,
|
||||
RUSTFS_META_BUCKET, RUSTFS_META_MULTIPART_BUCKET, RUSTFS_META_TMP_BUCKET, ReadMultipleReq, ReadMultipleResp, ReadOptions,
|
||||
@@ -626,7 +627,7 @@ impl SetDisks {
|
||||
&& !found.etag.is_empty()
|
||||
&& part_meta_quorum.get(max_etag).unwrap_or(&0) >= &read_quorum
|
||||
{
|
||||
ret[part_idx] = found;
|
||||
ret[part_idx] = found.clone();
|
||||
} else {
|
||||
ret[part_idx] = ObjectPartInfo {
|
||||
number: part_numbers[part_idx],
|
||||
@@ -2011,12 +2012,12 @@ impl SetDisks {
|
||||
if errs.iter().any(|err| err.is_some()) {
|
||||
let _ =
|
||||
rustfs_common::heal_channel::send_heal_request(rustfs_common::heal_channel::create_heal_request_with_options(
|
||||
fi.volume.to_string(), // bucket
|
||||
Some(fi.name.to_string()), // object_prefix
|
||||
false, // force_start
|
||||
Some(rustfs_common::heal_channel::HealChannelPriority::Normal), // priority
|
||||
Some(self.pool_index), // pool_index
|
||||
Some(self.set_index), // set_index
|
||||
fi.volume.to_string(), // bucket
|
||||
Some(fi.name.to_string()), // object_prefix
|
||||
false, // force_start
|
||||
Some(HealChannelPriority::Normal), // priority
|
||||
Some(self.pool_index), // pool_index
|
||||
Some(self.set_index), // set_index
|
||||
))
|
||||
.await;
|
||||
}
|
||||
@@ -2154,7 +2155,7 @@ impl SetDisks {
|
||||
bucket.to_string(),
|
||||
Some(object.to_string()),
|
||||
false,
|
||||
Some(rustfs_common::heal_channel::HealChannelPriority::Normal),
|
||||
Some(HealChannelPriority::Normal),
|
||||
Some(pool_index),
|
||||
Some(set_index),
|
||||
),
|
||||
@@ -2632,7 +2633,7 @@ impl SetDisks {
|
||||
}
|
||||
|
||||
let is_inline_buffer = {
|
||||
if let Some(sc) = GLOBAL_StorageClass.get() {
|
||||
if let Some(sc) = GLOBAL_STORAGE_CLASS.get() {
|
||||
sc.should_inline(erasure.shard_file_size(latest_meta.size), false)
|
||||
} else {
|
||||
false
|
||||
@@ -3287,12 +3288,7 @@ impl ObjectIO for SetDisks {
|
||||
let paths = vec![object.to_string()];
|
||||
let lock_acquired = self
|
||||
.namespace_lock
|
||||
.lock_batch(
|
||||
&paths,
|
||||
&self.locker_owner,
|
||||
std::time::Duration::from_secs(5),
|
||||
std::time::Duration::from_secs(10),
|
||||
)
|
||||
.lock_batch(&paths, &self.locker_owner, Duration::from_secs(5), Duration::from_secs(10))
|
||||
.await?;
|
||||
|
||||
if !lock_acquired {
|
||||
@@ -3303,7 +3299,7 @@ impl ObjectIO for SetDisks {
|
||||
let mut user_defined = opts.user_defined.clone();
|
||||
|
||||
let sc_parity_drives = {
|
||||
if let Some(sc) = GLOBAL_StorageClass.get() {
|
||||
if let Some(sc) = GLOBAL_STORAGE_CLASS.get() {
|
||||
sc.get_parity_for_sc(user_defined.get(AMZ_STORAGE_CLASS).cloned().unwrap_or_default().as_str())
|
||||
} else {
|
||||
None
|
||||
@@ -3348,7 +3344,7 @@ impl ObjectIO for SetDisks {
|
||||
let erasure = erasure_coding::Erasure::new(fi.erasure.data_blocks, fi.erasure.parity_blocks, fi.erasure.block_size);
|
||||
|
||||
let is_inline_buffer = {
|
||||
if let Some(sc) = GLOBAL_StorageClass.get() {
|
||||
if let Some(sc) = GLOBAL_STORAGE_CLASS.get() {
|
||||
sc.should_inline(erasure.shard_file_size(data.size()), opts.versioned)
|
||||
} else {
|
||||
false
|
||||
@@ -3919,7 +3915,7 @@ impl StorageAPI for SetDisks {
|
||||
bucket.to_string(),
|
||||
Some(object.to_string()),
|
||||
false,
|
||||
Some(rustfs_common::heal_channel::HealChannelPriority::Normal),
|
||||
Some(HealChannelPriority::Normal),
|
||||
Some(self.pool_index),
|
||||
Some(self.set_index),
|
||||
))
|
||||
@@ -4056,11 +4052,9 @@ impl StorageAPI for SetDisks {
|
||||
return to_object_err(err, vec![bucket, object]);
|
||||
}
|
||||
}*/
|
||||
//let traceFn = GLOBAL_LifecycleSys.trace(fi.to_object_info(bucket, object, opts.Versioned || opts.VersionSuspended));
|
||||
|
||||
let dest_obj = gen_transition_objname(bucket);
|
||||
if let Err(err) = dest_obj {
|
||||
//traceFn(ILMTransition, nil, err)
|
||||
return Err(to_object_err(err, vec![]));
|
||||
}
|
||||
let dest_obj = dest_obj.unwrap();
|
||||
@@ -4068,8 +4062,6 @@ impl StorageAPI for SetDisks {
|
||||
let oi = ObjectInfo::from_file_info(&fi, bucket, object, opts.versioned || opts.version_suspended);
|
||||
|
||||
let (pr, mut pw) = tokio::io::duplex(fi.erasure.block_size);
|
||||
//let h = HeaderMap::new();
|
||||
//let reader = ReaderImpl::ObjectBody(GetObjectReader {stream: StreamingBlob::wrap(tokio_util::io::ReaderStream::new(pr)), object_info: oi});
|
||||
let reader = ReaderImpl::ObjectBody(GetObjectReader {
|
||||
stream: Box::new(pr),
|
||||
object_info: oi,
|
||||
@@ -4106,9 +4098,7 @@ impl StorageAPI for SetDisks {
|
||||
m
|
||||
})
|
||||
.await;
|
||||
//pr.CloseWithError(err);
|
||||
if let Err(err) = rv {
|
||||
//traceFn(ILMTransition, nil, err)
|
||||
return Err(StorageError::Io(err));
|
||||
}
|
||||
let rv = rv.unwrap();
|
||||
@@ -4172,7 +4162,6 @@ impl StorageAPI for SetDisks {
|
||||
//if err != nil {
|
||||
// return set_restore_header_fn(&mut oi, Some(toObjectErr(err, bucket, object)));
|
||||
//}
|
||||
//defer gr.Close()
|
||||
let hash_reader = HashReader::new(gr, gr.obj_info.size, "", "", gr.obj_info.size);
|
||||
let p_reader = PutObjReader::new(StreamingBlob::from(Box::pin(hash_reader)), hash_reader.size());
|
||||
if let Err(err) = self.put_object(bucket, object, &mut p_reader, &ropts).await {
|
||||
@@ -4736,7 +4725,7 @@ impl StorageAPI for SetDisks {
|
||||
}
|
||||
|
||||
let sc_parity_drives = {
|
||||
if let Some(sc) = GLOBAL_StorageClass.get() {
|
||||
if let Some(sc) = GLOBAL_STORAGE_CLASS.get() {
|
||||
sc.get_parity_for_sc(user_defined.get(AMZ_STORAGE_CLASS).cloned().unwrap_or_default().as_str())
|
||||
} else {
|
||||
None
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
#![allow(clippy::map_entry)]
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@@ -13,10 +12,12 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#![allow(clippy::map_entry)]
|
||||
|
||||
use crate::bucket::lifecycle::bucket_lifecycle_ops::init_background_expiry;
|
||||
use crate::bucket::metadata_sys::{self, set_bucket_metadata};
|
||||
use crate::bucket::utils::{check_valid_bucket_name, check_valid_bucket_name_strict, is_meta_bucketname};
|
||||
use crate::config::GLOBAL_StorageClass;
|
||||
use crate::config::GLOBAL_STORAGE_CLASS;
|
||||
use crate::config::storageclass;
|
||||
use crate::disk::endpoint::{Endpoint, EndpointType};
|
||||
use crate::disk::{DiskAPI, DiskInfo, DiskInfoOptions};
|
||||
@@ -1139,7 +1140,7 @@ impl StorageAPI for ECStore {
|
||||
#[tracing::instrument(skip(self))]
|
||||
async fn backend_info(&self) -> rustfs_madmin::BackendInfo {
|
||||
let (standard_sc_parity, rr_sc_parity) = {
|
||||
if let Some(sc) = GLOBAL_StorageClass.get() {
|
||||
if let Some(sc) = GLOBAL_STORAGE_CLASS.get() {
|
||||
let sc_parity = sc
|
||||
.get_parity_for_sc(storageclass::CLASS_STANDARD)
|
||||
.or(Some(self.pools[0].default_parity_count));
|
||||
|
||||
@@ -387,6 +387,8 @@ pub struct ObjectInfo {
|
||||
pub version_id: Option<Uuid>,
|
||||
pub delete_marker: bool,
|
||||
pub transitioned_object: TransitionedObject,
|
||||
pub restore_ongoing: bool,
|
||||
pub restore_expires: Option<OffsetDateTime>,
|
||||
pub user_tags: String,
|
||||
pub parts: Vec<ObjectPartInfo>,
|
||||
pub is_latest: bool,
|
||||
@@ -421,6 +423,8 @@ impl Clone for ObjectInfo {
|
||||
version_id: self.version_id,
|
||||
delete_marker: self.delete_marker,
|
||||
transitioned_object: self.transitioned_object.clone(),
|
||||
restore_ongoing: self.restore_ongoing,
|
||||
restore_expires: self.restore_expires,
|
||||
user_tags: self.user_tags.clone(),
|
||||
parts: self.parts.clone(),
|
||||
is_latest: self.is_latest,
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
# 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-mcp"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
repository.workspace = true
|
||||
rust-version.workspace = true
|
||||
homepage.workspace = true
|
||||
description = "RustFS MCP (Model Context Protocol) Server"
|
||||
keywords = ["mcp", "s3", "aws", "rustfs", "server"]
|
||||
categories = ["development-tools", "web-programming"]
|
||||
documentation = "https://docs.rs/rustfs-mcp/latest/rustfs_mcp/"
|
||||
|
||||
[[bin]]
|
||||
name = "rustfs-mcp"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
# AWS SDK for S3 operations
|
||||
aws-sdk-s3.workspace = true
|
||||
|
||||
# Async runtime and utilities
|
||||
tokio = { workspace = true, features = ["io-std", "io-util", "macros", "signal"] }
|
||||
|
||||
# MCP SDK with macros support
|
||||
rmcp = { workspace = true, features = ["server", "transport-io", "macros"] }
|
||||
|
||||
# Command line argument parsing
|
||||
clap = { workspace = true, features = ["derive", "env"] }
|
||||
|
||||
# Serialization (still needed for S3 data structures)
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
schemars = { workspace = true }
|
||||
|
||||
# Error handling
|
||||
anyhow.workspace = true
|
||||
|
||||
# Logging
|
||||
tracing.workspace = true
|
||||
tracing-subscriber.workspace = true
|
||||
|
||||
# File handling and MIME type detection
|
||||
mime_guess = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
# Testing framework and utilities
|
||||
@@ -0,0 +1,184 @@
|
||||
[](https://rustfs.com)
|
||||
|
||||
# RustFS MCP Server - Model Context Protocol
|
||||
|
||||
<p align="center">
|
||||
<strong>High-performance MCP server providing S3-compatible object storage operations for AI/LLM integration</strong>
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
<a href="https://github.com/rustfs/rustfs/actions/workflows/ci.yml"><img alt="CI" src="https://github.com/rustfs/rustfs/actions/workflows/ci.yml/badge.svg" /></a>
|
||||
<a href="https://docs.rustfs.com/en/">📖 Documentation</a>
|
||||
<a href="https://github.com/rustfs/rustfs/issues">🐛 Bug Reports</a>
|
||||
<a href="https://github.com/rustfs/rustfs/discussions">💬 Discussions</a>
|
||||
</p>
|
||||
|
||||
---
|
||||
|
||||
## 📖 Overview
|
||||
|
||||
**RustFS MCP Server** is a high-performance [Model Context Protocol (MCP)](https://spec.modelcontextprotocol.org) server that provides AI/LLM tools with seamless access to S3-compatible object storage operations. Built with Rust for maximum performance and safety, it enables AI assistants like Claude Desktop to interact with cloud storage through a standardized protocol.
|
||||
|
||||
### What is MCP?
|
||||
|
||||
The Model Context Protocol is an open standard that enables secure, controlled connections between AI applications and external systems. This server acts as a bridge between AI tools and S3-compatible storage services, providing structured access to file operations while maintaining security and observability.
|
||||
|
||||
## ✨ Features
|
||||
|
||||
### Supported S3 Operations
|
||||
|
||||
- **List Buckets**: List all accessible S3 buckets
|
||||
- **List Objects**: Browse bucket contents with optional prefix filtering
|
||||
- **Upload Files**: Upload local files with automatic MIME type detection and cache control
|
||||
- **Get Objects**: Retrieve objects from S3 storage with read or download modes
|
||||
|
||||
## 🔧 Installation
|
||||
|
||||
### Prerequisites
|
||||
|
||||
- Rust 1.70+ (for building from source)
|
||||
- AWS credentials configured (via environment variables, AWS CLI, or IAM roles)
|
||||
- Access to S3-compatible storage service
|
||||
|
||||
### Build from Source
|
||||
|
||||
```bash
|
||||
# Clone the repository
|
||||
git clone https://github.com/rustfs/rustfs.git
|
||||
cd rustfs
|
||||
|
||||
# Build the MCP server
|
||||
cargo build --release -p rustfs-mcp
|
||||
|
||||
# The binary will be available at
|
||||
./target/release/rustfs-mcp
|
||||
```
|
||||
|
||||
## ⚙️ Configuration
|
||||
|
||||
### Environment Variables
|
||||
|
||||
```bash
|
||||
# AWS Credentials (required)
|
||||
export AWS_ACCESS_KEY_ID=your_access_key
|
||||
export AWS_SECRET_ACCESS_KEY=your_secret_key
|
||||
export AWS_REGION=us-east-1 # Optional, defaults to us-east-1
|
||||
|
||||
# Optional: Custom S3 endpoint (for MinIO, etc.)
|
||||
export AWS_ENDPOINT_URL=http://localhost:9000
|
||||
|
||||
# Logging level (optional)
|
||||
export RUST_LOG=info
|
||||
```
|
||||
|
||||
### Command Line Options
|
||||
|
||||
```bash
|
||||
rustfs-mcp --help
|
||||
```
|
||||
|
||||
The server supports various command-line options for customizing behavior:
|
||||
|
||||
- `--access-key-id`: AWS Access Key ID for S3 authentication
|
||||
- `--secret-access-key`: AWS Secret Access Key for S3 authentication
|
||||
- `--region`: AWS region to use for S3 operations (default: us-east-1)
|
||||
- `--endpoint-url`: Custom S3 endpoint URL (for MinIO, LocalStack, etc.)
|
||||
- `--log-level`: Log level configuration (default: rustfs_mcp_server=info)
|
||||
|
||||
## 🚀 Usage
|
||||
|
||||
### Starting the Server
|
||||
|
||||
```bash
|
||||
# Start the MCP server
|
||||
rustfs-mcp
|
||||
|
||||
# Or with custom options
|
||||
rustfs-mcp --log-level debug --region us-west-2
|
||||
```
|
||||
|
||||
### Integration with chat client
|
||||
#### Option 1: Using Command Line Arguments
|
||||
```json
|
||||
{
|
||||
"mcpServers": {
|
||||
"rustfs-mcp": {
|
||||
"command": "/path/to/rustfs-mcp",
|
||||
"args": [
|
||||
"--access-key-id", "your_access_key",
|
||||
"--secret-access-key", "your_secret_key",
|
||||
"--region", "us-west-2",
|
||||
"--log-level", "info"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Option 2: Using Environment Variables
|
||||
```json
|
||||
{
|
||||
"mcpServers": {
|
||||
"rustfs-mcp": {
|
||||
"command": "/path/to/rustfs-mcp",
|
||||
"env": {
|
||||
"AWS_ACCESS_KEY_ID": "your_access_key",
|
||||
"AWS_SECRET_ACCESS_KEY": "your_secret_key",
|
||||
"AWS_REGION": "us-east-1"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
## 🛠️ Available Tools
|
||||
|
||||
The MCP server exposes the following tools that AI assistants can use:
|
||||
|
||||
### `list_buckets`
|
||||
List all S3 buckets accessible with the configured credentials.
|
||||
|
||||
**Parameters:** None
|
||||
|
||||
### `list_objects`
|
||||
List objects in an S3 bucket with optional prefix filtering.
|
||||
|
||||
**Parameters:**
|
||||
- `bucket_name` (string): Name of the S3 bucket
|
||||
- `prefix` (string, optional): Prefix to filter objects
|
||||
|
||||
### `upload_file`
|
||||
Upload a local file to S3 with automatic MIME type detection.
|
||||
|
||||
**Parameters:**
|
||||
- `local_file_path` (string): Path to the local file
|
||||
- `bucket_name` (string): Target S3 bucket
|
||||
- `object_key` (string): S3 object key (destination path)
|
||||
- `content_type` (string, optional): Content type (auto-detected if not provided)
|
||||
- `storage_class` (string, optional): S3 storage class
|
||||
- `cache_control` (string, optional): Cache control header
|
||||
|
||||
### `get_object`
|
||||
Retrieve an object from S3 with two operation modes: read content directly or download to a file.
|
||||
|
||||
**Parameters:**
|
||||
- `bucket_name` (string): Source S3 bucket
|
||||
- `object_key` (string): S3 object key
|
||||
- `version_id` (string, optional): Version ID for versioned objects
|
||||
- `mode` (string, optional): Operation mode - "read" (default) returns content directly, "download" saves to local file
|
||||
- `local_path` (string, optional): Local file path (required when mode is "download")
|
||||
- `max_content_size` (number, optional): Maximum content size in bytes for read mode (default: 1MB)
|
||||
|
||||
## Architecture
|
||||
|
||||
The MCP server is built with a modular architecture:
|
||||
|
||||
```
|
||||
rustfs-mcp/
|
||||
├── src/
|
||||
│ ├── main.rs # Entry point, CLI parsing, and server initialization
|
||||
│ ├── server.rs # MCP server implementation and tool handlers
|
||||
│ ├── s3_client.rs # S3 client wrapper with async operations
|
||||
│ ├── config.rs # Configuration management and CLI options
|
||||
│ └── lib.rs # Library exports and public API
|
||||
└── Cargo.toml # Dependencies, metadata, and binary configuration
|
||||
```
|
||||
@@ -0,0 +1,224 @@
|
||||
// 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.
|
||||
|
||||
use anyhow::Result;
|
||||
use clap::Parser;
|
||||
use tracing::info;
|
||||
|
||||
/// Configuration for RustFS MCP Server
|
||||
#[derive(Parser, Debug, Clone)]
|
||||
#[command(
|
||||
name = "rustfs-mcp-server",
|
||||
about = "RustFS MCP (Model Context Protocol) Server for S3 operations",
|
||||
version,
|
||||
long_about = r#"
|
||||
RustFS MCP Server - Model Context Protocol server for S3 operations
|
||||
|
||||
This server provides S3 operations through the Model Context Protocol (MCP),
|
||||
allowing AI assistants to interact with S3-compatible storage systems.
|
||||
|
||||
ENVIRONMENT VARIABLES:
|
||||
All command-line options can also be set via environment variables.
|
||||
Command-line arguments take precedence over environment variables.
|
||||
|
||||
EXAMPLES:
|
||||
# Using command-line arguments
|
||||
rustfs-mcp-server --access-key-id your_key --secret-access-key your_secret
|
||||
|
||||
# Using environment variables
|
||||
export AWS_ACCESS_KEY_ID=your_key
|
||||
export AWS_SECRET_ACCESS_KEY=your_secret
|
||||
rustfs-mcp-server
|
||||
|
||||
# Mixed usage (command-line overrides environment)
|
||||
export AWS_REGION=us-east-1
|
||||
rustfs-mcp-server --access-key-id mykey --secret-access-key mysecret --endpoint-url http://localhost:9000
|
||||
"#
|
||||
)]
|
||||
pub struct Config {
|
||||
/// AWS Access Key ID
|
||||
#[arg(
|
||||
long = "access-key-id",
|
||||
env = "AWS_ACCESS_KEY_ID",
|
||||
help = "AWS Access Key ID for S3 authentication"
|
||||
)]
|
||||
pub access_key_id: Option<String>,
|
||||
|
||||
/// AWS Secret Access Key
|
||||
#[arg(
|
||||
long = "secret-access-key",
|
||||
env = "AWS_SECRET_ACCESS_KEY",
|
||||
help = "AWS Secret Access Key for S3 authentication"
|
||||
)]
|
||||
pub secret_access_key: Option<String>,
|
||||
|
||||
/// AWS Region
|
||||
#[arg(
|
||||
long = "region",
|
||||
env = "AWS_REGION",
|
||||
default_value = "us-east-1",
|
||||
help = "AWS region to use for S3 operations"
|
||||
)]
|
||||
pub region: String,
|
||||
|
||||
/// Custom S3 endpoint URL
|
||||
#[arg(
|
||||
long = "endpoint-url",
|
||||
env = "AWS_ENDPOINT_URL",
|
||||
help = "Custom S3 endpoint URL (for MinIO, LocalStack, etc.)"
|
||||
)]
|
||||
pub endpoint_url: Option<String>,
|
||||
|
||||
/// Log level
|
||||
#[arg(
|
||||
long = "log-level",
|
||||
env = "RUST_LOG",
|
||||
default_value = "rustfs_mcp_server=info",
|
||||
help = "Log level configuration"
|
||||
)]
|
||||
pub log_level: String,
|
||||
|
||||
/// Force path-style addressing
|
||||
#[arg(
|
||||
long = "force-path-style",
|
||||
help = "Force path-style S3 addressing (automatically enabled for custom endpoints)"
|
||||
)]
|
||||
pub force_path_style: bool,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn new() -> Self {
|
||||
Config::parse()
|
||||
}
|
||||
|
||||
pub fn validate(&self) -> Result<()> {
|
||||
if self.access_key_id.is_none() {
|
||||
anyhow::bail!("AWS Access Key ID is required. Set via --access-key-id or AWS_ACCESS_KEY_ID environment variable");
|
||||
}
|
||||
|
||||
if self.secret_access_key.is_none() {
|
||||
anyhow::bail!(
|
||||
"AWS Secret Access Key is required. Set via --secret-access-key or AWS_SECRET_ACCESS_KEY environment variable"
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn access_key_id(&self) -> &str {
|
||||
self.access_key_id.as_ref().expect("Access key ID should be validated")
|
||||
}
|
||||
|
||||
pub fn secret_access_key(&self) -> &str {
|
||||
self.secret_access_key
|
||||
.as_ref()
|
||||
.expect("Secret access key should be validated")
|
||||
}
|
||||
|
||||
pub fn log_configuration(&self) {
|
||||
let access_key_display = self
|
||||
.access_key_id
|
||||
.as_ref()
|
||||
.map(|key| {
|
||||
if key.len() > 8 {
|
||||
format!("{}...{}", &key[..4], &key[key.len() - 4..])
|
||||
} else {
|
||||
"*".repeat(key.len())
|
||||
}
|
||||
})
|
||||
.unwrap_or_else(|| "Not set".to_string());
|
||||
|
||||
let endpoint_display = self
|
||||
.endpoint_url
|
||||
.as_ref()
|
||||
.map(|url| format!("Custom endpoint: {url}"))
|
||||
.unwrap_or_else(|| "Default AWS endpoints".to_string());
|
||||
|
||||
info!("Configuration:");
|
||||
info!(" AWS Region: {}", self.region);
|
||||
info!(" AWS Access Key ID: {}", access_key_display);
|
||||
info!(" AWS Secret Access Key: [HIDDEN]");
|
||||
info!(" S3 Endpoint: {}", endpoint_display);
|
||||
info!(" Force Path Style: {}", self.force_path_style);
|
||||
info!(" Log Level: {}", self.log_level);
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Config {
|
||||
access_key_id: None,
|
||||
secret_access_key: None,
|
||||
region: "us-east-1".to_string(),
|
||||
endpoint_url: None,
|
||||
log_level: "rustfs_mcp_server=info".to_string(),
|
||||
force_path_style: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_config_validation_success() {
|
||||
let config = Config {
|
||||
access_key_id: Some("test_key".to_string()),
|
||||
secret_access_key: Some("test_secret".to_string()),
|
||||
..Config::default()
|
||||
};
|
||||
|
||||
assert!(config.validate().is_ok());
|
||||
assert_eq!(config.access_key_id(), "test_key");
|
||||
assert_eq!(config.secret_access_key(), "test_secret");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_config_validation_missing_access_key() {
|
||||
let config = Config {
|
||||
access_key_id: None,
|
||||
secret_access_key: Some("test_secret".to_string()),
|
||||
..Config::default()
|
||||
};
|
||||
|
||||
let result = config.validate();
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().to_string().contains("Access Key ID"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_config_validation_missing_secret_key() {
|
||||
let config = Config {
|
||||
access_key_id: Some("test_key".to_string()),
|
||||
secret_access_key: None,
|
||||
..Config::default()
|
||||
};
|
||||
|
||||
let result = config.validate();
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().to_string().contains("Secret Access Key"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_config_default() {
|
||||
let config = Config::default();
|
||||
assert_eq!(config.region, "us-east-1");
|
||||
assert_eq!(config.log_level, "rustfs_mcp_server=info");
|
||||
assert!(!config.force_path_style);
|
||||
assert!(config.access_key_id.is_none());
|
||||
assert!(config.secret_access_key.is_none());
|
||||
assert!(config.endpoint_url.is_none());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
// 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.
|
||||
|
||||
pub mod config;
|
||||
pub mod s3_client;
|
||||
pub mod server;
|
||||
|
||||
pub use config::Config;
|
||||
pub use s3_client::{BucketInfo, S3Client};
|
||||
pub use server::RustfsMcpServer;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use rmcp::ServiceExt;
|
||||
use tokio::io::{stdin, stdout};
|
||||
use tracing::info;
|
||||
|
||||
/// Run the MCP server with the provided configuration
|
||||
pub async fn run_server_with_config(config: Config) -> Result<()> {
|
||||
info!("Starting RustFS MCP Server with provided configuration");
|
||||
|
||||
config.validate().context("Configuration validation failed")?;
|
||||
|
||||
let server = RustfsMcpServer::new(config).await?;
|
||||
|
||||
info!("Running MCP server with stdio transport");
|
||||
|
||||
// Run the server with stdio
|
||||
server
|
||||
.serve((stdin(), stdout()))
|
||||
.await
|
||||
.context("Failed to serve MCP server")?
|
||||
.waiting()
|
||||
.await
|
||||
.context("Error while waiting for server shutdown")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Run the MCP server with default configuration (from environment variables)
|
||||
pub async fn run_server() -> Result<()> {
|
||||
info!("Starting RustFS MCP Server with default configuration");
|
||||
|
||||
let config = Config::default();
|
||||
run_server_with_config(config).await
|
||||
}
|
||||
|
||||
/// Validate environment configuration (legacy function for backward compatibility)
|
||||
pub fn validate_environment() -> Result<()> {
|
||||
use std::env;
|
||||
|
||||
if env::var("AWS_ACCESS_KEY_ID").is_err() {
|
||||
anyhow::bail!("AWS_ACCESS_KEY_ID environment variable is required");
|
||||
}
|
||||
|
||||
if env::var("AWS_SECRET_ACCESS_KEY").is_err() {
|
||||
anyhow::bail!("AWS_SECRET_ACCESS_KEY environment variable is required");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_config_creation() {
|
||||
let config = Config {
|
||||
access_key_id: Some("test_key".to_string()),
|
||||
secret_access_key: Some("test_secret".to_string()),
|
||||
..Config::default()
|
||||
};
|
||||
|
||||
assert!(config.validate().is_ok());
|
||||
assert_eq!(config.access_key_id(), "test_key");
|
||||
assert_eq!(config.secret_access_key(), "test_secret");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_run_server_with_invalid_config() {
|
||||
let config = Config::default();
|
||||
|
||||
let result = run_server_with_config(config).await;
|
||||
assert!(result.is_err());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
// 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.
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use clap::Parser;
|
||||
use rmcp::ServiceExt;
|
||||
use rustfs_mcp::{Config, RustfsMcpServer};
|
||||
use std::env;
|
||||
use tokio::io::{stdin, stdout};
|
||||
use tracing::{Level, error, info};
|
||||
use tracing_subscriber::{EnvFilter, FmtSubscriber};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
let config = Config::parse();
|
||||
|
||||
init_tracing(&config)?;
|
||||
|
||||
info!("Starting RustFS MCP Server v{}", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
if let Err(e) = config.validate() {
|
||||
error!("Configuration validation failed: {}", e);
|
||||
print_usage_help();
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
config.log_configuration();
|
||||
|
||||
if let Err(e) = run_server(config).await {
|
||||
error!("Server error: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
info!("RustFS MCP Server shutdown complete");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn run_server(config: Config) -> Result<()> {
|
||||
info!("Initializing RustFS MCP Server");
|
||||
|
||||
let server = RustfsMcpServer::new(config).await?;
|
||||
|
||||
info!("Starting MCP server with stdio transport");
|
||||
|
||||
server
|
||||
.serve((stdin(), stdout()))
|
||||
.await
|
||||
.context("Failed to serve MCP server")?
|
||||
.waiting()
|
||||
.await
|
||||
.context("Error while waiting for server shutdown")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn init_tracing(config: &Config) -> Result<()> {
|
||||
let filter = EnvFilter::try_from_default_env()
|
||||
.or_else(|_| EnvFilter::try_new(&config.log_level))
|
||||
.context("Failed to create log filter")?;
|
||||
|
||||
let subscriber = FmtSubscriber::builder()
|
||||
.with_max_level(Level::TRACE)
|
||||
.with_env_filter(filter)
|
||||
.with_target(false)
|
||||
.with_thread_ids(false)
|
||||
.with_thread_names(false)
|
||||
.with_writer(std::io::stderr) // Force logs to stderr to avoid interfering with MCP protocol on stdout
|
||||
.finish();
|
||||
|
||||
tracing::subscriber::set_global_default(subscriber).context("Failed to set global tracing subscriber")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn print_usage_help() {
|
||||
eprintln!();
|
||||
eprintln!("RustFS MCP Server - Model Context Protocol server for S3 operations");
|
||||
eprintln!();
|
||||
eprintln!("For more help, run: rustfs-mcp --help");
|
||||
eprintln!();
|
||||
eprintln!("QUICK START:");
|
||||
eprintln!(" # Using command-line arguments");
|
||||
eprintln!(" rustfs-mcp --access-key-id YOUR_KEY --secret-access-key YOUR_SECRET");
|
||||
eprintln!();
|
||||
eprintln!(" # Using environment variables");
|
||||
eprintln!(" export AWS_ACCESS_KEY_ID=YOUR_KEY");
|
||||
eprintln!(" export AWS_SECRET_ACCESS_KEY=YOUR_SECRET");
|
||||
eprintln!(" rustfs-mcp");
|
||||
eprintln!();
|
||||
eprintln!(" # For local development with RustFS");
|
||||
eprintln!(" rustfs-mcp --access-key-id minioadmin --secret-access-key minioadmin --endpoint-url http://localhost:9000");
|
||||
eprintln!();
|
||||
}
|
||||
@@ -0,0 +1,796 @@
|
||||
// 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.
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use aws_sdk_s3::config::{Credentials, Region};
|
||||
use aws_sdk_s3::primitives::ByteStream;
|
||||
use aws_sdk_s3::{Client, Config as S3Config};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::Path;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tracing::{debug, info};
|
||||
|
||||
use crate::config::Config;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct BucketInfo {
|
||||
pub name: String,
|
||||
pub creation_date: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ObjectInfo {
|
||||
pub key: String,
|
||||
pub size: Option<i64>,
|
||||
pub last_modified: Option<String>,
|
||||
pub etag: Option<String>,
|
||||
pub storage_class: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct ListObjectsOptions {
|
||||
pub prefix: Option<String>,
|
||||
pub delimiter: Option<String>,
|
||||
pub max_keys: Option<i32>,
|
||||
pub continuation_token: Option<String>,
|
||||
pub start_after: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ListObjectsResult {
|
||||
pub objects: Vec<ObjectInfo>,
|
||||
pub common_prefixes: Vec<String>,
|
||||
pub is_truncated: bool,
|
||||
pub next_continuation_token: Option<String>,
|
||||
pub max_keys: Option<i32>,
|
||||
pub key_count: i32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct UploadFileOptions {
|
||||
pub content_type: Option<String>,
|
||||
pub metadata: Option<std::collections::HashMap<String, String>>,
|
||||
pub storage_class: Option<String>,
|
||||
pub server_side_encryption: Option<String>,
|
||||
pub cache_control: Option<String>,
|
||||
pub content_disposition: Option<String>,
|
||||
pub content_encoding: Option<String>,
|
||||
pub content_language: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct GetObjectOptions {
|
||||
pub version_id: Option<String>,
|
||||
pub range: Option<String>,
|
||||
pub if_modified_since: Option<String>,
|
||||
pub if_unmodified_since: Option<String>,
|
||||
pub max_content_size: Option<usize>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub enum DetectedFileType {
|
||||
Text,
|
||||
NonText(String), // mime type for non-text files
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct GetObjectResult {
|
||||
pub bucket: String,
|
||||
pub key: String,
|
||||
pub content_type: String,
|
||||
pub content_length: u64,
|
||||
pub last_modified: Option<String>,
|
||||
pub etag: Option<String>,
|
||||
pub version_id: Option<String>,
|
||||
pub detected_type: DetectedFileType,
|
||||
pub content: Option<Vec<u8>>, // Raw content bytes
|
||||
pub text_content: Option<String>, // UTF-8 decoded content for text files
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct UploadResult {
|
||||
pub bucket: String,
|
||||
pub key: String,
|
||||
pub etag: String,
|
||||
pub location: String,
|
||||
pub version_id: Option<String>,
|
||||
pub file_size: u64,
|
||||
pub content_type: String,
|
||||
pub upload_id: Option<String>,
|
||||
}
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct S3Client {
|
||||
client: Client,
|
||||
}
|
||||
|
||||
impl S3Client {
|
||||
pub async fn new(config: &Config) -> Result<Self> {
|
||||
info!("Initializing S3 client from configuration");
|
||||
|
||||
let access_key = config.access_key_id();
|
||||
let secret_key = config.secret_access_key();
|
||||
|
||||
debug!("Using AWS region: {}", config.region);
|
||||
if let Some(ref endpoint) = config.endpoint_url {
|
||||
debug!("Using custom endpoint: {}", endpoint);
|
||||
}
|
||||
|
||||
let credentials = Credentials::new(access_key, secret_key, None, None, "rustfs-mcp-server");
|
||||
|
||||
let mut config_builder = S3Config::builder()
|
||||
.credentials_provider(credentials)
|
||||
.region(Region::new(config.region.clone()))
|
||||
.behavior_version(aws_sdk_s3::config::BehaviorVersion::latest());
|
||||
|
||||
// Set force path style if custom endpoint or explicitly requested
|
||||
let should_force_path_style = config.endpoint_url.is_some() || config.force_path_style;
|
||||
if should_force_path_style {
|
||||
config_builder = config_builder.force_path_style(true);
|
||||
}
|
||||
|
||||
if let Some(endpoint) = &config.endpoint_url {
|
||||
config_builder = config_builder.endpoint_url(endpoint);
|
||||
}
|
||||
|
||||
let s3_config = config_builder.build();
|
||||
let client = Client::from_conf(s3_config);
|
||||
|
||||
info!("S3 client initialized successfully");
|
||||
|
||||
Ok(Self { client })
|
||||
}
|
||||
|
||||
pub async fn list_buckets(&self) -> Result<Vec<BucketInfo>> {
|
||||
debug!("Listing S3 buckets");
|
||||
|
||||
let response = self.client.list_buckets().send().await.context("Failed to list S3 buckets")?;
|
||||
|
||||
let buckets: Vec<BucketInfo> = response
|
||||
.buckets()
|
||||
.iter()
|
||||
.map(|bucket| {
|
||||
let name = bucket.name().unwrap_or("unknown").to_string();
|
||||
let creation_date = bucket
|
||||
.creation_date()
|
||||
.map(|dt| dt.fmt(aws_sdk_s3::primitives::DateTimeFormat::DateTime).unwrap());
|
||||
|
||||
BucketInfo { name, creation_date }
|
||||
})
|
||||
.collect();
|
||||
|
||||
debug!("Found {} buckets", buckets.len());
|
||||
Ok(buckets)
|
||||
}
|
||||
|
||||
pub async fn list_objects_v2(&self, bucket_name: &str, options: ListObjectsOptions) -> Result<ListObjectsResult> {
|
||||
debug!("Listing objects in bucket '{}' with options: {:?}", bucket_name, options);
|
||||
|
||||
let mut request = self.client.list_objects_v2().bucket(bucket_name);
|
||||
|
||||
if let Some(prefix) = options.prefix {
|
||||
request = request.prefix(prefix);
|
||||
}
|
||||
|
||||
if let Some(delimiter) = options.delimiter {
|
||||
request = request.delimiter(delimiter);
|
||||
}
|
||||
|
||||
if let Some(max_keys) = options.max_keys {
|
||||
request = request.max_keys(max_keys);
|
||||
}
|
||||
|
||||
if let Some(continuation_token) = options.continuation_token {
|
||||
request = request.continuation_token(continuation_token);
|
||||
}
|
||||
|
||||
if let Some(start_after) = options.start_after {
|
||||
request = request.start_after(start_after);
|
||||
}
|
||||
|
||||
let response = request
|
||||
.send()
|
||||
.await
|
||||
.context(format!("Failed to list objects in bucket '{bucket_name}'"))?;
|
||||
|
||||
let objects: Vec<ObjectInfo> = response
|
||||
.contents()
|
||||
.iter()
|
||||
.map(|obj| {
|
||||
let key = obj.key().unwrap_or("unknown").to_string();
|
||||
let size = obj.size();
|
||||
let last_modified = obj
|
||||
.last_modified()
|
||||
.map(|dt| dt.fmt(aws_sdk_s3::primitives::DateTimeFormat::DateTime).unwrap());
|
||||
let etag = obj.e_tag().map(|e| e.to_string());
|
||||
let storage_class = obj.storage_class().map(|sc| sc.as_str().to_string());
|
||||
|
||||
ObjectInfo {
|
||||
key,
|
||||
size,
|
||||
last_modified,
|
||||
etag,
|
||||
storage_class,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
let common_prefixes: Vec<String> = response
|
||||
.common_prefixes()
|
||||
.iter()
|
||||
.filter_map(|cp| cp.prefix())
|
||||
.map(|p| p.to_string())
|
||||
.collect();
|
||||
|
||||
let result = ListObjectsResult {
|
||||
objects,
|
||||
common_prefixes,
|
||||
is_truncated: response.is_truncated().unwrap_or(false),
|
||||
next_continuation_token: response.next_continuation_token().map(|t| t.to_string()),
|
||||
max_keys: response.max_keys(),
|
||||
key_count: response.key_count().unwrap_or(0),
|
||||
};
|
||||
|
||||
debug!(
|
||||
"Found {} objects and {} common prefixes in bucket '{}'",
|
||||
result.objects.len(),
|
||||
result.common_prefixes.len(),
|
||||
bucket_name
|
||||
);
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub async fn upload_file(
|
||||
&self,
|
||||
local_path: &str,
|
||||
bucket_name: &str,
|
||||
object_key: &str,
|
||||
options: UploadFileOptions,
|
||||
) -> Result<UploadResult> {
|
||||
info!("Starting file upload: '{}' -> s3://{}/{}", local_path, bucket_name, object_key);
|
||||
|
||||
let path = Path::new(local_path);
|
||||
let canonical_path = path
|
||||
.canonicalize()
|
||||
.context(format!("Failed to resolve file path: {local_path}"))?;
|
||||
|
||||
if !canonical_path.exists() {
|
||||
anyhow::bail!("File does not exist: {}", local_path);
|
||||
}
|
||||
|
||||
if !canonical_path.is_file() {
|
||||
anyhow::bail!("Path is not a file: {}", local_path);
|
||||
}
|
||||
|
||||
let metadata = tokio::fs::metadata(&canonical_path)
|
||||
.await
|
||||
.context(format!("Failed to read file metadata: {local_path}"))?;
|
||||
|
||||
let file_size = metadata.len();
|
||||
debug!("File size: {file_size} bytes");
|
||||
|
||||
let content_type = options.content_type.unwrap_or_else(|| {
|
||||
let detected = mime_guess::from_path(&canonical_path).first_or_octet_stream().to_string();
|
||||
debug!("Auto-detected content type: {detected}");
|
||||
detected
|
||||
});
|
||||
|
||||
let file_content = tokio::fs::read(&canonical_path)
|
||||
.await
|
||||
.context(format!("Failed to read file content: {local_path}"))?;
|
||||
|
||||
let byte_stream = ByteStream::from(file_content);
|
||||
|
||||
let mut request = self
|
||||
.client
|
||||
.put_object()
|
||||
.bucket(bucket_name)
|
||||
.key(object_key)
|
||||
.body(byte_stream)
|
||||
.content_type(&content_type)
|
||||
.content_length(file_size as i64);
|
||||
|
||||
if let Some(storage_class) = &options.storage_class {
|
||||
request = request.storage_class(storage_class.as_str().into());
|
||||
}
|
||||
|
||||
if let Some(cache_control) = &options.cache_control {
|
||||
request = request.cache_control(cache_control);
|
||||
}
|
||||
|
||||
if let Some(content_disposition) = &options.content_disposition {
|
||||
request = request.content_disposition(content_disposition);
|
||||
}
|
||||
|
||||
if let Some(content_encoding) = &options.content_encoding {
|
||||
request = request.content_encoding(content_encoding);
|
||||
}
|
||||
|
||||
if let Some(content_language) = &options.content_language {
|
||||
request = request.content_language(content_language);
|
||||
}
|
||||
|
||||
if let Some(sse) = &options.server_side_encryption {
|
||||
request = request.server_side_encryption(sse.as_str().into());
|
||||
}
|
||||
|
||||
if let Some(metadata_map) = &options.metadata {
|
||||
for (key, value) in metadata_map {
|
||||
request = request.metadata(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
debug!("Executing S3 put_object request");
|
||||
let response = request
|
||||
.send()
|
||||
.await
|
||||
.context(format!("Failed to upload file to s3://{bucket_name}/{object_key}"))?;
|
||||
|
||||
let etag = response.e_tag().unwrap_or("unknown").to_string();
|
||||
let version_id = response.version_id().map(|v| v.to_string());
|
||||
|
||||
let location = format!("s3://{bucket_name}/{object_key}");
|
||||
|
||||
let upload_result = UploadResult {
|
||||
bucket: bucket_name.to_string(),
|
||||
key: object_key.to_string(),
|
||||
etag,
|
||||
location,
|
||||
version_id,
|
||||
file_size,
|
||||
content_type,
|
||||
upload_id: None,
|
||||
};
|
||||
|
||||
info!(
|
||||
"File upload completed successfully: {} bytes uploaded to s3://{}/{}",
|
||||
file_size, bucket_name, object_key
|
||||
);
|
||||
|
||||
Ok(upload_result)
|
||||
}
|
||||
|
||||
pub async fn get_object(&self, bucket_name: &str, object_key: &str, options: GetObjectOptions) -> Result<GetObjectResult> {
|
||||
info!("Getting object: s3://{}/{}", bucket_name, object_key);
|
||||
|
||||
let mut request = self.client.get_object().bucket(bucket_name).key(object_key);
|
||||
|
||||
if let Some(version_id) = &options.version_id {
|
||||
request = request.version_id(version_id);
|
||||
}
|
||||
|
||||
if let Some(range) = &options.range {
|
||||
request = request.range(range);
|
||||
}
|
||||
|
||||
if let Some(if_modified_since) = &options.if_modified_since {
|
||||
request = request.if_modified_since(
|
||||
aws_sdk_s3::primitives::DateTime::from_str(if_modified_since, aws_sdk_s3::primitives::DateTimeFormat::DateTime)
|
||||
.context("Failed to parse if_modified_since date")?,
|
||||
);
|
||||
}
|
||||
|
||||
debug!("Executing S3 get_object request");
|
||||
let response = request
|
||||
.send()
|
||||
.await
|
||||
.context(format!("Failed to get object from s3://{bucket_name}/{object_key}"))?;
|
||||
|
||||
let content_type = response.content_type().unwrap_or("application/octet-stream").to_string();
|
||||
let content_length = response.content_length().unwrap_or(0) as u64;
|
||||
let last_modified = response
|
||||
.last_modified()
|
||||
.map(|dt| dt.fmt(aws_sdk_s3::primitives::DateTimeFormat::DateTime).unwrap());
|
||||
let etag = response.e_tag().map(|e| e.to_string());
|
||||
let version_id = response.version_id().map(|v| v.to_string());
|
||||
|
||||
let max_size = options.max_content_size.unwrap_or(10 * 1024 * 1024);
|
||||
let mut content = Vec::new();
|
||||
let mut byte_stream = response.body;
|
||||
let mut total_read = 0;
|
||||
|
||||
while let Some(bytes_result) = byte_stream.try_next().await.context("Failed to read object content")? {
|
||||
if total_read + bytes_result.len() > max_size {
|
||||
anyhow::bail!("Object size exceeds maximum allowed size of {} bytes", max_size);
|
||||
}
|
||||
content.extend_from_slice(&bytes_result);
|
||||
total_read += bytes_result.len();
|
||||
}
|
||||
|
||||
debug!("Read {} bytes from object", content.len());
|
||||
|
||||
let detected_type = Self::detect_file_type(Some(&content_type), &content);
|
||||
debug!("Detected file type: {detected_type:?}");
|
||||
|
||||
let text_content = match &detected_type {
|
||||
DetectedFileType::Text => match std::str::from_utf8(&content) {
|
||||
Ok(text) => Some(text.to_string()),
|
||||
Err(_) => {
|
||||
debug!("Failed to decode content as UTF-8, treating as binary");
|
||||
None
|
||||
}
|
||||
},
|
||||
_ => None,
|
||||
};
|
||||
|
||||
let result = GetObjectResult {
|
||||
bucket: bucket_name.to_string(),
|
||||
key: object_key.to_string(),
|
||||
content_type,
|
||||
content_length,
|
||||
last_modified,
|
||||
etag,
|
||||
version_id,
|
||||
detected_type,
|
||||
content: Some(content),
|
||||
text_content,
|
||||
};
|
||||
|
||||
info!(
|
||||
"Object retrieved successfully: {} bytes from s3://{}/{}",
|
||||
result.content_length, bucket_name, object_key
|
||||
);
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn detect_file_type(content_type: Option<&str>, content_bytes: &[u8]) -> DetectedFileType {
|
||||
if let Some(ct) = content_type {
|
||||
let ct_lower = ct.to_lowercase();
|
||||
|
||||
if ct_lower.starts_with("text/")
|
||||
|| ct_lower == "application/json"
|
||||
|| ct_lower == "application/xml"
|
||||
|| ct_lower == "application/yaml"
|
||||
|| ct_lower == "application/javascript"
|
||||
|| ct_lower == "application/x-yaml"
|
||||
|| ct_lower == "application/x-sh"
|
||||
|| ct_lower == "application/x-shellscript"
|
||||
|| ct_lower.contains("script")
|
||||
|| ct_lower.contains("xml")
|
||||
|| ct_lower.contains("json")
|
||||
{
|
||||
return DetectedFileType::Text;
|
||||
}
|
||||
|
||||
return DetectedFileType::NonText(ct.to_string());
|
||||
}
|
||||
|
||||
if content_bytes.len() >= 4 {
|
||||
match &content_bytes[0..4] {
|
||||
// PNG: 89 50 4E 47
|
||||
[0x89, 0x50, 0x4E, 0x47] => return DetectedFileType::NonText("image/png".to_string()),
|
||||
// JPEG: FF D8 FF
|
||||
[0xFF, 0xD8, 0xFF, _] => return DetectedFileType::NonText("image/jpeg".to_string()),
|
||||
// GIF: 47 49 46 38
|
||||
[0x47, 0x49, 0x46, 0x38] => return DetectedFileType::NonText("image/gif".to_string()),
|
||||
// BMP: 42 4D
|
||||
[0x42, 0x4D, _, _] => return DetectedFileType::NonText("image/bmp".to_string()),
|
||||
// RIFF container (WebP/WAV)
|
||||
[0x52, 0x49, 0x46, 0x46] if content_bytes.len() >= 12 => {
|
||||
if &content_bytes[8..12] == b"WEBP" {
|
||||
return DetectedFileType::NonText("image/webp".to_string());
|
||||
} else if &content_bytes[8..12] == b"WAVE" {
|
||||
return DetectedFileType::NonText("audio/wav".to_string());
|
||||
}
|
||||
return DetectedFileType::NonText("application/octet-stream".to_string());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Check if content is valid UTF-8 text as fallback
|
||||
if std::str::from_utf8(content_bytes).is_ok() {
|
||||
// Additional heuristics for text detection
|
||||
let non_printable_count = content_bytes
|
||||
.iter()
|
||||
.filter(|&&b| b < 0x20 && b != 0x09 && b != 0x0A && b != 0x0D) // Control chars except tab, LF, CR
|
||||
.count();
|
||||
let total_chars = content_bytes.len();
|
||||
|
||||
// If less than 5% are non-printable control characters, consider it text
|
||||
if total_chars > 0 && (non_printable_count as f64 / total_chars as f64) < 0.05 {
|
||||
return DetectedFileType::Text;
|
||||
}
|
||||
}
|
||||
|
||||
// Default to non-text binary
|
||||
DetectedFileType::NonText("application/octet-stream".to_string())
|
||||
}
|
||||
|
||||
pub async fn download_object_to_file(
|
||||
&self,
|
||||
bucket_name: &str,
|
||||
object_key: &str,
|
||||
local_path: &str,
|
||||
options: GetObjectOptions,
|
||||
) -> Result<(u64, String)> {
|
||||
info!("Downloading object: s3://{}/{} -> {}", bucket_name, object_key, local_path);
|
||||
|
||||
let mut request = self.client.get_object().bucket(bucket_name).key(object_key);
|
||||
|
||||
if let Some(version_id) = &options.version_id {
|
||||
request = request.version_id(version_id);
|
||||
}
|
||||
|
||||
if let Some(range) = &options.range {
|
||||
request = request.range(range);
|
||||
}
|
||||
|
||||
if let Some(if_modified_since) = &options.if_modified_since {
|
||||
request = request.if_modified_since(
|
||||
aws_sdk_s3::primitives::DateTime::from_str(if_modified_since, aws_sdk_s3::primitives::DateTimeFormat::DateTime)
|
||||
.context("Failed to parse if_modified_since date")?,
|
||||
);
|
||||
}
|
||||
|
||||
debug!("Executing S3 get_object request for download");
|
||||
let response = request
|
||||
.send()
|
||||
.await
|
||||
.context(format!("Failed to get object from s3://{bucket_name}/{object_key}"))?;
|
||||
|
||||
let local_file_path = Path::new(local_path);
|
||||
|
||||
if let Some(parent) = local_file_path.parent() {
|
||||
tokio::fs::create_dir_all(parent)
|
||||
.await
|
||||
.context(format!("Failed to create parent directories for {local_path}"))?;
|
||||
}
|
||||
|
||||
let mut file = tokio::fs::File::create(local_file_path)
|
||||
.await
|
||||
.context(format!("Failed to create local file: {local_path}"))?;
|
||||
|
||||
let mut byte_stream = response.body;
|
||||
let mut total_bytes = 0u64;
|
||||
|
||||
while let Some(bytes_result) = byte_stream.try_next().await.context("Failed to read object content")? {
|
||||
file.write_all(&bytes_result)
|
||||
.await
|
||||
.context(format!("Failed to write to local file: {local_path}"))?;
|
||||
total_bytes += bytes_result.len() as u64;
|
||||
}
|
||||
|
||||
file.flush().await.context("Failed to flush file to disk")?;
|
||||
|
||||
let absolute_path = local_file_path
|
||||
.canonicalize()
|
||||
.unwrap_or_else(|_| local_file_path.to_path_buf())
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
|
||||
info!(
|
||||
"Object downloaded successfully: {} bytes from s3://{}/{} to {}",
|
||||
total_bytes, bucket_name, object_key, absolute_path
|
||||
);
|
||||
|
||||
Ok((total_bytes, absolute_path))
|
||||
}
|
||||
|
||||
pub async fn health_check(&self) -> Result<()> {
|
||||
debug!("Performing S3 health check");
|
||||
|
||||
self.client.list_buckets().send().await.context("S3 health check failed")?;
|
||||
|
||||
debug!("S3 health check passed");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore] // Requires AWS credentials
|
||||
async fn test_s3_client_creation() {
|
||||
let config = Config {
|
||||
access_key_id: Some("test_key".to_string()),
|
||||
secret_access_key: Some("test_secret".to_string()),
|
||||
region: "us-east-1".to_string(),
|
||||
..Config::default()
|
||||
};
|
||||
|
||||
let result = S3Client::new(&config).await;
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bucket_info_serialization() {
|
||||
let bucket = BucketInfo {
|
||||
name: "test-bucket".to_string(),
|
||||
creation_date: Some("2024-01-01T00:00:00Z".to_string()),
|
||||
};
|
||||
|
||||
let json = serde_json::to_string(&bucket).unwrap();
|
||||
let deserialized: BucketInfo = serde_json::from_str(&json).unwrap();
|
||||
|
||||
assert_eq!(bucket.name, deserialized.name);
|
||||
assert_eq!(bucket.creation_date, deserialized.creation_date);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_detect_file_type_text_content_type() {
|
||||
let test_cases = vec![
|
||||
("text/plain", "Hello world"),
|
||||
("text/html", "<html></html>"),
|
||||
("application/json", r#"{"key": "value"}"#),
|
||||
("application/xml", "<xml></xml>"),
|
||||
("application/yaml", "key: value"),
|
||||
("application/javascript", "console.log('hello');"),
|
||||
];
|
||||
|
||||
for (content_type, content) in test_cases {
|
||||
let result = S3Client::detect_file_type(Some(content_type), content.as_bytes());
|
||||
match result {
|
||||
DetectedFileType::Text => {}
|
||||
_ => panic!("Expected Text for content type {content_type}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_detect_file_type_non_text_content_type() {
|
||||
// Test various non-text content types
|
||||
let test_cases = vec![
|
||||
("image/png", "image/png"),
|
||||
("image/jpeg", "image/jpeg"),
|
||||
("audio/mp3", "audio/mp3"),
|
||||
("video/mp4", "video/mp4"),
|
||||
("application/pdf", "application/pdf"),
|
||||
];
|
||||
|
||||
for (content_type, expected_mime) in test_cases {
|
||||
let result = S3Client::detect_file_type(Some(content_type), b"some content");
|
||||
match result {
|
||||
DetectedFileType::NonText(mime_type) => {
|
||||
assert_eq!(mime_type, expected_mime);
|
||||
}
|
||||
_ => panic!("Expected NonText for content type {content_type}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_detect_file_type_magic_bytes_simplified() {
|
||||
// Test magic bytes detection (now all return NonText)
|
||||
let test_cases = vec![
|
||||
// PNG magic bytes: 89 50 4E 47
|
||||
(vec![0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A], "image/png"),
|
||||
// JPEG magic bytes: FF D8 FF
|
||||
(vec![0xFF, 0xD8, 0xFF, 0xE0], "image/jpeg"),
|
||||
// GIF magic bytes: 47 49 46 38
|
||||
(vec![0x47, 0x49, 0x46, 0x38, 0x37, 0x61], "image/gif"),
|
||||
];
|
||||
|
||||
for (content, expected_mime) in test_cases {
|
||||
let result = S3Client::detect_file_type(None, &content);
|
||||
match result {
|
||||
DetectedFileType::NonText(mime_type) => {
|
||||
assert_eq!(mime_type, expected_mime);
|
||||
}
|
||||
_ => panic!("Expected NonText for magic bytes: {content:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_detect_file_type_webp_magic_bytes() {
|
||||
// WebP has more complex magic bytes: RIFF....WEBP
|
||||
let mut webp_content = vec![0x52, 0x49, 0x46, 0x46]; // RIFF
|
||||
webp_content.extend_from_slice(&[0x00, 0x00, 0x00, 0x00]); // Size (4 bytes)
|
||||
webp_content.extend_from_slice(b"WEBP"); // WEBP signature
|
||||
|
||||
let result = S3Client::detect_file_type(None, &webp_content);
|
||||
match result {
|
||||
DetectedFileType::NonText(mime_type) => {
|
||||
assert_eq!(mime_type, "image/webp");
|
||||
}
|
||||
_ => panic!("Expected WebP NonText detection"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_detect_file_type_wav_magic_bytes() {
|
||||
// WAV has magic bytes: RIFF....WAVE
|
||||
let mut wav_content = vec![0x52, 0x49, 0x46, 0x46]; // RIFF
|
||||
wav_content.extend_from_slice(&[0x00, 0x00, 0x00, 0x00]); // Size (4 bytes)
|
||||
wav_content.extend_from_slice(b"WAVE"); // WAVE signature
|
||||
|
||||
let result = S3Client::detect_file_type(None, &wav_content);
|
||||
match result {
|
||||
DetectedFileType::NonText(mime_type) => {
|
||||
assert_eq!(mime_type, "audio/wav");
|
||||
}
|
||||
_ => panic!("Expected WAV NonText detection"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_detect_file_type_utf8_text() {
|
||||
// Test UTF-8 text detection
|
||||
let utf8_content = "Hello, 世界! 🌍".as_bytes();
|
||||
let result = S3Client::detect_file_type(None, utf8_content);
|
||||
match result {
|
||||
DetectedFileType::Text => {}
|
||||
_ => panic!("Expected Text for UTF-8 content"),
|
||||
}
|
||||
|
||||
// Test ASCII text
|
||||
let ascii_content = b"Hello, world! This is ASCII text.";
|
||||
let result = S3Client::detect_file_type(None, ascii_content);
|
||||
match result {
|
||||
DetectedFileType::Text => {}
|
||||
_ => panic!("Expected Text for ASCII content"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_detect_file_type_binary() {
|
||||
// Test binary content that should not be detected as text
|
||||
let binary_content = vec![0x00, 0x01, 0x02, 0x03, 0xFF, 0xFE, 0xFD, 0xFC];
|
||||
let result = S3Client::detect_file_type(None, &binary_content);
|
||||
match result {
|
||||
DetectedFileType::NonText(mime_type) => {
|
||||
assert_eq!(mime_type, "application/octet-stream");
|
||||
}
|
||||
_ => panic!("Expected NonText for binary content"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_detect_file_type_priority() {
|
||||
// Content-Type should take priority over magic bytes
|
||||
let png_magic_bytes = vec![0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
|
||||
|
||||
// Even with PNG magic bytes, text content-type should win
|
||||
let result = S3Client::detect_file_type(Some("text/plain"), &png_magic_bytes);
|
||||
match result {
|
||||
DetectedFileType::Text => {}
|
||||
_ => panic!("Expected Text due to content-type priority"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_object_options_default() {
|
||||
let options = GetObjectOptions::default();
|
||||
assert!(options.version_id.is_none());
|
||||
assert!(options.range.is_none());
|
||||
assert!(options.if_modified_since.is_none());
|
||||
assert!(options.if_unmodified_since.is_none());
|
||||
assert!(options.max_content_size.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_detected_file_type_serialization() {
|
||||
let test_cases = vec![
|
||||
DetectedFileType::Text,
|
||||
DetectedFileType::NonText("image/png".to_string()),
|
||||
DetectedFileType::NonText("audio/mpeg".to_string()),
|
||||
DetectedFileType::NonText("application/octet-stream".to_string()),
|
||||
];
|
||||
|
||||
for file_type in test_cases {
|
||||
let json = serde_json::to_string(&file_type).unwrap();
|
||||
let deserialized: DetectedFileType = serde_json::from_str(&json).unwrap();
|
||||
|
||||
match (&file_type, &deserialized) {
|
||||
(DetectedFileType::Text, DetectedFileType::Text) => {}
|
||||
(DetectedFileType::NonText(a), DetectedFileType::NonText(b)) => assert_eq!(a, b),
|
||||
_ => panic!("Serialization/deserialization mismatch"),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,670 @@
|
||||
// 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.
|
||||
|
||||
use anyhow::Result;
|
||||
use rmcp::{
|
||||
ErrorData, RoleServer, ServerHandler,
|
||||
handler::server::{router::tool::ToolRouter, tool::Parameters},
|
||||
model::{Implementation, ProtocolVersion, ServerCapabilities, ServerInfo, ToolsCapability},
|
||||
service::{NotificationContext, RequestContext},
|
||||
tool, tool_handler, tool_router,
|
||||
};
|
||||
use schemars::JsonSchema;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tracing::{debug, error, info};
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::s3_client::{DetectedFileType, GetObjectOptions, ListObjectsOptions, S3Client, UploadFileOptions};
|
||||
|
||||
#[derive(Serialize, Deserialize, JsonSchema)]
|
||||
pub struct ListObjectsRequest {
|
||||
pub bucket_name: String,
|
||||
#[serde(default)]
|
||||
#[schemars(description = "Optional prefix to filter objects")]
|
||||
pub prefix: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, JsonSchema)]
|
||||
pub struct UploadFileRequest {
|
||||
#[schemars(description = "Path to the local file to upload")]
|
||||
pub local_file_path: String,
|
||||
#[schemars(description = "Name of the S3 bucket to upload to")]
|
||||
pub bucket_name: String,
|
||||
#[schemars(description = "S3 object key (path/filename in the bucket)")]
|
||||
pub object_key: String,
|
||||
#[serde(default)]
|
||||
#[schemars(description = "Optional content type (auto-detected if not specified)")]
|
||||
pub content_type: Option<String>,
|
||||
#[serde(default)]
|
||||
#[schemars(description = "Optional storage class (STANDARD, REDUCED_REDUNDANCY, etc.)")]
|
||||
pub storage_class: Option<String>,
|
||||
#[serde(default)]
|
||||
#[schemars(description = "Optional cache control header")]
|
||||
pub cache_control: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, JsonSchema)]
|
||||
pub struct GetObjectRequest {
|
||||
#[schemars(description = "Name of the S3 bucket")]
|
||||
pub bucket_name: String,
|
||||
#[schemars(description = "S3 object key (path/filename in the bucket)")]
|
||||
pub object_key: String,
|
||||
#[serde(default)]
|
||||
#[schemars(description = "Optional version ID for versioned objects")]
|
||||
pub version_id: Option<String>,
|
||||
#[serde(default = "default_operation_mode")]
|
||||
#[schemars(description = "Operation mode: read (return content) or download (save to local file)")]
|
||||
pub mode: GetObjectMode,
|
||||
#[serde(default)]
|
||||
#[schemars(description = "Local file path for download mode (required when mode is download)")]
|
||||
pub local_path: Option<String>,
|
||||
#[serde(default = "default_max_content_size")]
|
||||
#[schemars(description = "Maximum content size to read in bytes for read mode (default: 1MB)")]
|
||||
pub max_content_size: usize,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, PartialEq)]
|
||||
pub enum GetObjectMode {
|
||||
#[serde(rename = "read")]
|
||||
Read,
|
||||
#[serde(rename = "download")]
|
||||
Download,
|
||||
}
|
||||
|
||||
fn default_operation_mode() -> GetObjectMode {
|
||||
GetObjectMode::Read
|
||||
}
|
||||
fn default_max_content_size() -> usize {
|
||||
1024 * 1024
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RustfsMcpServer {
|
||||
s3_client: S3Client,
|
||||
_config: Config,
|
||||
tool_router: ToolRouter<Self>,
|
||||
}
|
||||
|
||||
#[tool_router(router = tool_router)]
|
||||
impl RustfsMcpServer {
|
||||
pub async fn new(config: Config) -> Result<Self> {
|
||||
info!("Creating RustFS MCP Server");
|
||||
|
||||
let s3_client = S3Client::new(&config).await?;
|
||||
|
||||
Ok(Self {
|
||||
s3_client,
|
||||
_config: config,
|
||||
tool_router: Self::tool_router(),
|
||||
})
|
||||
}
|
||||
|
||||
#[tool(description = "List all S3 buckets accessible with the configured credentials")]
|
||||
pub async fn list_buckets(&self) -> String {
|
||||
info!("Executing list_buckets tool");
|
||||
|
||||
match self.s3_client.list_buckets().await {
|
||||
Ok(buckets) => {
|
||||
debug!("Successfully retrieved {} buckets", buckets.len());
|
||||
|
||||
if buckets.is_empty() {
|
||||
return "No S3 buckets found. The AWS credentials may not have access to any buckets, or no buckets exist in this account.".to_string();
|
||||
}
|
||||
|
||||
let mut result_text = format!("Found {} S3 bucket(s):\n\n", buckets.len());
|
||||
|
||||
for (index, bucket) in buckets.iter().enumerate() {
|
||||
result_text.push_str(&format!("{}. **{}**", index + 1, bucket.name));
|
||||
|
||||
if let Some(ref creation_date) = bucket.creation_date {
|
||||
result_text.push_str(&format!("\n - Created: {creation_date}"));
|
||||
}
|
||||
result_text.push_str("\n\n");
|
||||
}
|
||||
|
||||
result_text.push_str("---\n");
|
||||
result_text.push_str(&format!("Total buckets: {}\n", buckets.len()));
|
||||
result_text.push_str("Note: Only buckets accessible with the current AWS credentials are shown.");
|
||||
|
||||
info!("list_buckets tool executed successfully");
|
||||
result_text
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to list buckets: {:?}", e);
|
||||
|
||||
format!(
|
||||
"Failed to list S3 buckets: {e}\n\nPossible causes:\n\
|
||||
• AWS credentials are not set or invalid\n\
|
||||
• Network connectivity issues\n\
|
||||
• AWS region is not set correctly\n\
|
||||
• Insufficient permissions to list buckets\n\
|
||||
• Custom endpoint is misconfigured\n\n\
|
||||
Please verify your AWS configuration and try again."
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tool(description = "List objects in a specific S3 bucket with optional prefix filtering")]
|
||||
pub async fn list_objects(&self, Parameters(req): Parameters<ListObjectsRequest>) -> String {
|
||||
info!("Executing list_objects tool for bucket: {}", req.bucket_name);
|
||||
|
||||
let options = ListObjectsOptions {
|
||||
prefix: req.prefix.clone(),
|
||||
delimiter: None,
|
||||
max_keys: Some(1000),
|
||||
..ListObjectsOptions::default()
|
||||
};
|
||||
|
||||
match self.s3_client.list_objects_v2(&req.bucket_name, options).await {
|
||||
Ok(result) => {
|
||||
debug!(
|
||||
"Successfully retrieved {} objects and {} common prefixes from bucket '{}'",
|
||||
result.objects.len(),
|
||||
result.common_prefixes.len(),
|
||||
req.bucket_name
|
||||
);
|
||||
|
||||
if result.objects.is_empty() && result.common_prefixes.is_empty() {
|
||||
let prefix_msg = req.prefix.as_ref().map(|p| format!(" with prefix '{p}'")).unwrap_or_default();
|
||||
return format!(
|
||||
"No objects found in bucket '{}'{prefix_msg}. The bucket may be empty or the prefix may not match any objects.",
|
||||
req.bucket_name
|
||||
);
|
||||
}
|
||||
|
||||
let mut result_text = format!("Found {} object(s) in bucket **{}**", result.key_count, req.bucket_name);
|
||||
|
||||
if let Some(ref p) = req.prefix {
|
||||
result_text.push_str(&format!(" with prefix '{p}'"));
|
||||
}
|
||||
result_text.push_str(":\n\n");
|
||||
|
||||
if !result.common_prefixes.is_empty() {
|
||||
result_text.push_str("**Directories:**\n");
|
||||
for (index, prefix) in result.common_prefixes.iter().enumerate() {
|
||||
result_text.push_str(&format!("{}. 📁 {prefix}\n", index + 1));
|
||||
}
|
||||
result_text.push('\n');
|
||||
}
|
||||
|
||||
if !result.objects.is_empty() {
|
||||
result_text.push_str("**Objects:**\n");
|
||||
for (index, obj) in result.objects.iter().enumerate() {
|
||||
result_text.push_str(&format!("{}. **{}**\n", index + 1, obj.key));
|
||||
|
||||
if let Some(size) = obj.size {
|
||||
result_text.push_str(&format!(" - Size: {size} bytes\n"));
|
||||
}
|
||||
|
||||
if let Some(ref last_modified) = obj.last_modified {
|
||||
result_text.push_str(&format!(" - Last Modified: {last_modified}\n"));
|
||||
}
|
||||
|
||||
if let Some(ref etag) = obj.etag {
|
||||
result_text.push_str(&format!(" - ETag: {etag}\n"));
|
||||
}
|
||||
|
||||
if let Some(ref storage_class) = obj.storage_class {
|
||||
result_text.push_str(&format!(" - Storage Class: {storage_class}\n"));
|
||||
}
|
||||
|
||||
result_text.push('\n');
|
||||
}
|
||||
}
|
||||
|
||||
if result.is_truncated {
|
||||
result_text.push_str("**Note:** Results are truncated. ");
|
||||
if let Some(ref token) = result.next_continuation_token {
|
||||
result_text.push_str(&format!("Use continuation token '{token}' to get more results.\n"));
|
||||
}
|
||||
result_text.push('\n');
|
||||
}
|
||||
|
||||
result_text.push_str("---\n");
|
||||
result_text.push_str(&format!(
|
||||
"Total: {} object(s), {} directory/ies",
|
||||
result.objects.len(),
|
||||
result.common_prefixes.len()
|
||||
));
|
||||
|
||||
if let Some(max_keys) = result.max_keys {
|
||||
result_text.push_str(&format!(", Max keys: {max_keys}"));
|
||||
}
|
||||
|
||||
info!("list_objects tool executed successfully for bucket '{}'", req.bucket_name);
|
||||
result_text
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to list objects in bucket '{}': {:?}", req.bucket_name, e);
|
||||
|
||||
format!(
|
||||
"Failed to list objects in S3 bucket '{}': {}\n\nPossible causes:\n\
|
||||
• Bucket does not exist or is not accessible\n\
|
||||
• AWS credentials lack permissions to list objects in this bucket\n\
|
||||
• Network connectivity issues\n\
|
||||
• Custom endpoint is misconfigured\n\
|
||||
• Bucket name contains invalid characters\n\n\
|
||||
Please verify the bucket name, your AWS configuration, and permissions.",
|
||||
req.bucket_name, e
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tool(
|
||||
description = "Get/download an object from an S3 bucket - supports read mode for text files and download mode for all files"
|
||||
)]
|
||||
pub async fn get_object(&self, Parameters(req): Parameters<GetObjectRequest>) -> String {
|
||||
info!(
|
||||
"Executing get_object tool: s3://{}/{} (mode: {:?})",
|
||||
req.bucket_name, req.object_key, req.mode
|
||||
);
|
||||
|
||||
match req.mode {
|
||||
GetObjectMode::Read => self.handle_read_mode(req).await,
|
||||
GetObjectMode::Download => self.handle_download_mode(req).await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_read_mode(&self, req: GetObjectRequest) -> String {
|
||||
let options = GetObjectOptions {
|
||||
version_id: req.version_id.clone(),
|
||||
max_content_size: Some(req.max_content_size),
|
||||
..GetObjectOptions::default()
|
||||
};
|
||||
|
||||
match self.s3_client.get_object(&req.bucket_name, &req.object_key, options).await {
|
||||
Ok(result) => {
|
||||
debug!(
|
||||
"Successfully retrieved object s3://{}/{} ({} bytes)",
|
||||
req.bucket_name, req.object_key, result.content_length
|
||||
);
|
||||
|
||||
match result.detected_type {
|
||||
DetectedFileType::Text => {
|
||||
if let Some(ref text_content) = result.text_content {
|
||||
format!(
|
||||
"✅ **Text file content retrieved!**\n\n\
|
||||
**S3 Location:** s3://{}/{}\n\
|
||||
**File Size:** {} bytes\n\
|
||||
**Content Type:** {}\n\n\
|
||||
**Content:**\n```\n{}\n```",
|
||||
result.bucket, result.key, result.content_length, result.content_type, text_content
|
||||
)
|
||||
} else {
|
||||
format!(
|
||||
"⚠️ **Text file detected but content could not be decoded!**\n\n\
|
||||
**S3 Location:** s3://{}/{}\n\
|
||||
**File Size:** {} bytes\n\
|
||||
**Content Type:** {}\n\n\
|
||||
**Note:** Could not decode file as UTF-8 text. \
|
||||
Try using download mode instead.",
|
||||
result.bucket, result.key, result.content_length, result.content_type
|
||||
)
|
||||
}
|
||||
}
|
||||
DetectedFileType::NonText(ref mime_type) => {
|
||||
let file_category = if mime_type.starts_with("image/") {
|
||||
"Image"
|
||||
} else if mime_type.starts_with("audio/") {
|
||||
"Audio"
|
||||
} else if mime_type.starts_with("video/") {
|
||||
"Video"
|
||||
} else {
|
||||
"Binary"
|
||||
};
|
||||
|
||||
format!(
|
||||
"⚠️ **Non-text file detected!**\n\n\
|
||||
**S3 Location:** s3://{}/{}\n\
|
||||
**File Type:** {} ({})\n\
|
||||
**File Size:** {} bytes ({:.2} MB)\n\n\
|
||||
**Note:** This file type cannot be displayed as text.\n\
|
||||
Please use download mode to save it to a local file:\n\n\
|
||||
```json\n{{\n \"mode\": \"download\",\n \"local_path\": \"/path/to/save/file\"\n}}\n```",
|
||||
result.bucket,
|
||||
result.key,
|
||||
file_category,
|
||||
mime_type,
|
||||
result.content_length,
|
||||
result.content_length as f64 / 1_048_576.0
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to read object s3://{}/{}: {:?}", req.bucket_name, req.object_key, e);
|
||||
self.format_error_message(&req, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_download_mode(&self, req: GetObjectRequest) -> String {
|
||||
let local_path = match req.local_path {
|
||||
Some(ref path) => path,
|
||||
None => {
|
||||
return "❌ **Error:** local_path is required when using download mode.\n\n\
|
||||
**Example:**\n```json\n{\n \"mode\": \"download\",\n \"local_path\": \"/path/to/save/file.ext\"\n}\n```"
|
||||
.to_string();
|
||||
}
|
||||
};
|
||||
|
||||
let options = GetObjectOptions {
|
||||
version_id: req.version_id.clone(),
|
||||
..GetObjectOptions::default()
|
||||
};
|
||||
|
||||
match self
|
||||
.s3_client
|
||||
.download_object_to_file(&req.bucket_name, &req.object_key, local_path, options)
|
||||
.await
|
||||
{
|
||||
Ok((bytes_downloaded, absolute_path)) => {
|
||||
info!(
|
||||
"Successfully downloaded object s3://{}/{} to {} ({} bytes)",
|
||||
req.bucket_name, req.object_key, absolute_path, bytes_downloaded
|
||||
);
|
||||
|
||||
format!(
|
||||
"✅ **File downloaded successfully!**\n\n\
|
||||
**S3 Location:** s3://{}/{}\n\
|
||||
**Local Path (requested):** {}\n\
|
||||
**Absolute Path:** {}\n\
|
||||
**File Size:** {} bytes ({:.2} MB)\n\n\
|
||||
**✨ File saved successfully!** You can now access it at:\n\
|
||||
`{}`",
|
||||
req.bucket_name,
|
||||
req.object_key,
|
||||
local_path,
|
||||
absolute_path,
|
||||
bytes_downloaded,
|
||||
bytes_downloaded as f64 / 1_048_576.0,
|
||||
absolute_path
|
||||
)
|
||||
}
|
||||
Err(e) => {
|
||||
error!(
|
||||
"Failed to download object s3://{}/{} to {}: {:?}",
|
||||
req.bucket_name, req.object_key, local_path, e
|
||||
);
|
||||
|
||||
format!(
|
||||
"❌ **Failed to download file from S3**\n\n\
|
||||
**S3 Location:** s3://{}/{}\n\
|
||||
**Local Path:** {}\n\
|
||||
**Error:** {}\n\n\
|
||||
**Possible causes:**\n\
|
||||
• Object does not exist in the specified bucket\n\
|
||||
• AWS credentials lack permissions to read this object\n\
|
||||
• Cannot write to the specified local path\n\
|
||||
• Insufficient disk space\n\
|
||||
• Network connectivity issues\n\n\
|
||||
**Troubleshooting steps:**\n\
|
||||
1. Verify the object exists using list_objects\n\
|
||||
2. Check your AWS credentials and permissions\n\
|
||||
3. Ensure the local directory exists and is writable\n\
|
||||
4. Check available disk space",
|
||||
req.bucket_name, req.object_key, local_path, e
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn format_error_message(&self, req: &GetObjectRequest, error: anyhow::Error) -> String {
|
||||
format!(
|
||||
"❌ **Failed to get object from S3 bucket '{}'**\n\n\
|
||||
**Object Key:** {}\n\
|
||||
**Mode:** {:?}\n\
|
||||
**Error:** {}\n\n\
|
||||
**Possible causes:**\n\
|
||||
• Object does not exist in the specified bucket\n\
|
||||
• AWS credentials lack permissions to read this object\n\
|
||||
• Network connectivity issues\n\
|
||||
• Object key contains invalid characters\n\
|
||||
• Bucket does not exist or is not accessible\n\
|
||||
• Object is in a different AWS region\n\
|
||||
• Version ID is invalid (for versioned objects)\n\n\
|
||||
**Troubleshooting steps:**\n\
|
||||
1. Verify the object exists using list_objects\n\
|
||||
2. Check your AWS credentials and permissions\n\
|
||||
3. Ensure the bucket name and object key are correct\n\
|
||||
4. Try with a different object to test connectivity\n\
|
||||
5. Check if the bucket has versioning enabled",
|
||||
req.bucket_name, req.object_key, req.mode, error
|
||||
)
|
||||
}
|
||||
|
||||
#[tool(description = "Upload a local file to an S3 bucket")]
|
||||
pub async fn upload_file(&self, Parameters(req): Parameters<UploadFileRequest>) -> String {
|
||||
info!(
|
||||
"Executing upload_file tool: '{}' -> s3://{}/{}",
|
||||
req.local_file_path, req.bucket_name, req.object_key
|
||||
);
|
||||
|
||||
let options = UploadFileOptions {
|
||||
content_type: req.content_type.clone(),
|
||||
storage_class: req.storage_class.clone(),
|
||||
cache_control: req.cache_control.clone(),
|
||||
..UploadFileOptions::default()
|
||||
};
|
||||
|
||||
match self
|
||||
.s3_client
|
||||
.upload_file(&req.local_file_path, &req.bucket_name, &req.object_key, options)
|
||||
.await
|
||||
{
|
||||
Ok(result) => {
|
||||
debug!(
|
||||
"Successfully uploaded file '{}' to s3://{}/{} ({} bytes)",
|
||||
req.local_file_path, req.bucket_name, req.object_key, result.file_size
|
||||
);
|
||||
|
||||
let mut result_text = format!(
|
||||
"✅ **File uploaded successfully!**\n\n\
|
||||
**Local File:** {}\n\
|
||||
**S3 Location:** s3://{}/{}\n\
|
||||
**File Size:** {} bytes ({:.2} MB)\n\
|
||||
**Content Type:** {}\n\
|
||||
**ETag:** {}\n",
|
||||
req.local_file_path,
|
||||
result.bucket,
|
||||
result.key,
|
||||
result.file_size,
|
||||
result.file_size as f64 / 1_048_576.0,
|
||||
result.content_type,
|
||||
result.etag
|
||||
);
|
||||
|
||||
if let Some(ref version_id) = result.version_id {
|
||||
result_text.push_str(&format!("**Version ID:** {version_id}\n"));
|
||||
}
|
||||
|
||||
result_text.push_str("\n---\n");
|
||||
result_text.push_str("**Upload Summary:**\n");
|
||||
result_text.push_str(&format!("• Source: {}\n", req.local_file_path));
|
||||
result_text.push_str(&format!("• Destination: {}\n", result.location));
|
||||
result_text.push_str(&format!("• Size: {} bytes\n", result.file_size));
|
||||
result_text.push_str(&format!("• Type: {}\n", result.content_type));
|
||||
|
||||
if result.file_size > 5 * 1024 * 1024 {
|
||||
result_text.push_str("\n💡 **Note:** Large file uploaded successfully. Consider using multipart upload for files larger than 100MB for better performance and reliability.");
|
||||
}
|
||||
|
||||
info!(
|
||||
"upload_file tool executed successfully: {} bytes uploaded to s3://{}/{}",
|
||||
result.file_size, req.bucket_name, req.object_key
|
||||
);
|
||||
result_text
|
||||
}
|
||||
Err(e) => {
|
||||
error!(
|
||||
"Failed to upload file '{}' to s3://{}/{}: {:?}",
|
||||
req.local_file_path, req.bucket_name, req.object_key, e
|
||||
);
|
||||
|
||||
format!(
|
||||
"❌ **Failed to upload file '{}' to S3 bucket '{}'**\n\n\
|
||||
**Error:** {}\n\n\
|
||||
**Possible causes:**\n\
|
||||
• Local file does not exist or is not readable\n\
|
||||
• AWS credentials lack permissions to upload to this bucket\n\
|
||||
• S3 bucket does not exist or is not accessible\n\
|
||||
• Network connectivity issues\n\
|
||||
• File path contains invalid characters or is too long\n\
|
||||
• Insufficient disk space or memory\n\
|
||||
• Custom endpoint is misconfigured\n\
|
||||
• File is locked by another process\n\n\
|
||||
**Troubleshooting steps:**\n\
|
||||
1. Verify the local file exists and is readable\n\
|
||||
2. Check your AWS credentials and permissions\n\
|
||||
3. Ensure the bucket name is correct and accessible\n\
|
||||
4. Try with a smaller file to test connectivity\n\
|
||||
5. Check the file path for special characters\n\n\
|
||||
**File:** {}\n\
|
||||
**Bucket:** {}\n\
|
||||
**Object Key:** {}",
|
||||
req.local_file_path, req.bucket_name, e, req.local_file_path, req.bucket_name, req.object_key
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tool_handler(router = self.tool_router)]
|
||||
impl ServerHandler for RustfsMcpServer {
|
||||
fn get_info(&self) -> ServerInfo {
|
||||
ServerInfo {
|
||||
protocol_version: ProtocolVersion::V_2024_11_05,
|
||||
capabilities: ServerCapabilities {
|
||||
tools: Some(ToolsCapability {
|
||||
list_changed: Some(false),
|
||||
}),
|
||||
..Default::default()
|
||||
},
|
||||
instructions: Some("RustFS MCP Server providing S3 operations through Model Context Protocol".into()),
|
||||
server_info: Implementation {
|
||||
name: "rustfs-mcp-server".into(),
|
||||
version: env!("CARGO_PKG_VERSION").into(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
async fn ping(&self, _ctx: RequestContext<RoleServer>) -> Result<(), ErrorData> {
|
||||
info!("Received ping request");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn on_initialized(&self, _ctx: NotificationContext<RoleServer>) {
|
||||
info!("Client initialized successfully");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_server_creation() {
|
||||
let config = Config {
|
||||
access_key_id: Some("test_key".to_string()),
|
||||
secret_access_key: Some("test_secret".to_string()),
|
||||
..Config::default()
|
||||
};
|
||||
|
||||
let result = RustfsMcpServer::new(config).await;
|
||||
assert!(result.is_err() || result.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_object_request_defaults() {
|
||||
let request = GetObjectRequest {
|
||||
bucket_name: "test-bucket".to_string(),
|
||||
object_key: "test-key".to_string(),
|
||||
version_id: None,
|
||||
mode: default_operation_mode(),
|
||||
local_path: None,
|
||||
max_content_size: default_max_content_size(),
|
||||
};
|
||||
|
||||
assert_eq!(request.bucket_name, "test-bucket");
|
||||
assert_eq!(request.object_key, "test-key");
|
||||
assert!(request.version_id.is_none());
|
||||
assert_eq!(request.mode, GetObjectMode::Read);
|
||||
assert!(request.local_path.is_none());
|
||||
assert_eq!(request.max_content_size, 1024 * 1024);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_object_request_serialization() {
|
||||
let request = GetObjectRequest {
|
||||
bucket_name: "test-bucket".to_string(),
|
||||
object_key: "test-key".to_string(),
|
||||
version_id: Some("version123".to_string()),
|
||||
mode: GetObjectMode::Download,
|
||||
local_path: Some("/path/to/file".to_string()),
|
||||
max_content_size: 2048,
|
||||
};
|
||||
|
||||
let json = serde_json::to_string(&request).unwrap();
|
||||
let deserialized: GetObjectRequest = serde_json::from_str(&json).unwrap();
|
||||
|
||||
assert_eq!(request.bucket_name, deserialized.bucket_name);
|
||||
assert_eq!(request.object_key, deserialized.object_key);
|
||||
assert_eq!(request.version_id, deserialized.version_id);
|
||||
assert_eq!(request.mode, deserialized.mode);
|
||||
assert_eq!(request.local_path, deserialized.local_path);
|
||||
assert_eq!(request.max_content_size, deserialized.max_content_size);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_object_request_serde_with_defaults() {
|
||||
let json = r#"{
|
||||
"bucket_name": "test-bucket",
|
||||
"object_key": "test-key"
|
||||
}"#;
|
||||
|
||||
let request: GetObjectRequest = serde_json::from_str(json).unwrap();
|
||||
assert_eq!(request.bucket_name, "test-bucket");
|
||||
assert_eq!(request.object_key, "test-key");
|
||||
assert!(request.version_id.is_none());
|
||||
assert_eq!(request.mode, GetObjectMode::Read);
|
||||
assert!(request.local_path.is_none());
|
||||
assert_eq!(request.max_content_size, 1024 * 1024);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_default_functions() {
|
||||
assert_eq!(default_operation_mode(), GetObjectMode::Read);
|
||||
assert_eq!(default_max_content_size(), 1024 * 1024);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_object_mode_serialization() {
|
||||
let read_mode = GetObjectMode::Read;
|
||||
let download_mode = GetObjectMode::Download;
|
||||
|
||||
let read_json = serde_json::to_string(&read_mode).unwrap();
|
||||
let download_json = serde_json::to_string(&download_mode).unwrap();
|
||||
|
||||
assert_eq!(read_json, r#""read""#);
|
||||
assert_eq!(download_json, r#""download""#);
|
||||
|
||||
let read_mode_deser: GetObjectMode = serde_json::from_str(r#""read""#).unwrap();
|
||||
let download_mode_deser: GetObjectMode = serde_json::from_str(r#""download""#).unwrap();
|
||||
|
||||
assert_eq!(read_mode_deser, GetObjectMode::Read);
|
||||
assert_eq!(download_mode_deser, GetObjectMode::Download);
|
||||
}
|
||||
}
|
||||
@@ -27,11 +27,12 @@ documentation = "https://docs.rs/rustfs-notify/latest/rustfs_notify/"
|
||||
|
||||
[dependencies]
|
||||
rustfs-config = { workspace = true, features = ["notify"] }
|
||||
rustfs-ecstore = { workspace = true }
|
||||
rustfs-utils = { workspace = true, features = ["path", "sys"] }
|
||||
async-trait = { workspace = true }
|
||||
chrono = { workspace = true, features = ["serde"] }
|
||||
dashmap = { workspace = true }
|
||||
rustfs-ecstore = { workspace = true }
|
||||
futures = { workspace = true }
|
||||
form_urlencoded = { workspace = true }
|
||||
once_cell = { workspace = true }
|
||||
quick-xml = { workspace = true, features = ["serialize", "async-tokio"] }
|
||||
@@ -49,8 +50,6 @@ url = { workspace = true }
|
||||
urlencoding = { workspace = true }
|
||||
wildmatch = { workspace = true, features = ["serde"] }
|
||||
|
||||
|
||||
|
||||
[dev-dependencies]
|
||||
tokio = { workspace = true, features = ["test-util"] }
|
||||
reqwest = { workspace = true }
|
||||
|
||||
@@ -13,11 +13,11 @@
|
||||
// limitations under the License.
|
||||
|
||||
use rustfs_config::notify::{
|
||||
DEFAULT_LIMIT, DEFAULT_TARGET, MQTT_BROKER, MQTT_PASSWORD, MQTT_QOS, MQTT_QUEUE_DIR, MQTT_QUEUE_LIMIT, MQTT_TOPIC,
|
||||
MQTT_USERNAME, NOTIFY_MQTT_SUB_SYS, NOTIFY_WEBHOOK_SUB_SYS, WEBHOOK_AUTH_TOKEN, WEBHOOK_ENDPOINT, WEBHOOK_QUEUE_DIR,
|
||||
WEBHOOK_QUEUE_LIMIT,
|
||||
DEFAULT_LIMIT, DEFAULT_TARGET, ENABLE_KEY, ENABLE_ON, MQTT_BROKER, MQTT_PASSWORD, MQTT_QOS, MQTT_QUEUE_DIR, MQTT_QUEUE_LIMIT,
|
||||
MQTT_TOPIC, MQTT_USERNAME, NOTIFY_MQTT_SUB_SYS, NOTIFY_WEBHOOK_SUB_SYS, WEBHOOK_AUTH_TOKEN, WEBHOOK_ENDPOINT,
|
||||
WEBHOOK_QUEUE_DIR, WEBHOOK_QUEUE_LIMIT,
|
||||
};
|
||||
use rustfs_ecstore::config::{Config, ENABLE_KEY, ENABLE_ON, KV, KVS};
|
||||
use rustfs_ecstore::config::{Config, KV, KVS};
|
||||
use rustfs_notify::arn::TargetID;
|
||||
use rustfs_notify::{BucketNotificationConfig, Event, EventName, LogLevel, NotificationError, init_logger};
|
||||
use rustfs_notify::{initialize, notification_system};
|
||||
|
||||
@@ -14,11 +14,11 @@
|
||||
|
||||
// Using Global Accessories
|
||||
use rustfs_config::notify::{
|
||||
DEFAULT_LIMIT, DEFAULT_TARGET, MQTT_BROKER, MQTT_PASSWORD, MQTT_QOS, MQTT_QUEUE_DIR, MQTT_QUEUE_LIMIT, MQTT_TOPIC,
|
||||
MQTT_USERNAME, NOTIFY_MQTT_SUB_SYS, NOTIFY_WEBHOOK_SUB_SYS, WEBHOOK_AUTH_TOKEN, WEBHOOK_ENDPOINT, WEBHOOK_QUEUE_DIR,
|
||||
WEBHOOK_QUEUE_LIMIT,
|
||||
DEFAULT_LIMIT, DEFAULT_TARGET, ENABLE_KEY, ENABLE_ON, MQTT_BROKER, MQTT_PASSWORD, MQTT_QOS, MQTT_QUEUE_DIR, MQTT_QUEUE_LIMIT,
|
||||
MQTT_TOPIC, MQTT_USERNAME, NOTIFY_MQTT_SUB_SYS, NOTIFY_WEBHOOK_SUB_SYS, WEBHOOK_AUTH_TOKEN, WEBHOOK_ENDPOINT,
|
||||
WEBHOOK_QUEUE_DIR, WEBHOOK_QUEUE_LIMIT,
|
||||
};
|
||||
use rustfs_ecstore::config::{Config, ENABLE_KEY, ENABLE_ON, KV, KVS};
|
||||
use rustfs_ecstore::config::{Config, KV, KVS};
|
||||
use rustfs_notify::arn::TargetID;
|
||||
use rustfs_notify::{BucketNotificationConfig, Event, EventName, LogLevel, NotificationError, init_logger};
|
||||
use rustfs_notify::{initialize, notification_system};
|
||||
|
||||
@@ -82,6 +82,15 @@ pub enum TargetError {
|
||||
|
||||
#[error("Target is disabled")]
|
||||
Disabled,
|
||||
|
||||
#[error("Configuration parsing error: {0}")]
|
||||
ParseError(String),
|
||||
|
||||
#[error("Failed to save configuration: {0}")]
|
||||
SaveConfig(String),
|
||||
|
||||
#[error("Server not initialized: {0}")]
|
||||
ServerNotInitialized(String),
|
||||
}
|
||||
|
||||
/// Error types for the notification system
|
||||
@@ -112,7 +121,7 @@ pub enum NotificationError {
|
||||
AlreadyInitialized,
|
||||
|
||||
#[error("I/O error: {0}")]
|
||||
Io(std::io::Error),
|
||||
Io(io::Error),
|
||||
|
||||
#[error("Failed to read configuration: {0}")]
|
||||
ReadConfig(String),
|
||||
|
||||
+91
-144
@@ -19,40 +19,17 @@ use crate::{
|
||||
use async_trait::async_trait;
|
||||
use rumqttc::QoS;
|
||||
use rustfs_config::notify::{
|
||||
DEFAULT_DIR, DEFAULT_LIMIT, ENV_MQTT_BROKER, ENV_MQTT_ENABLE, ENV_MQTT_KEEP_ALIVE_INTERVAL, ENV_MQTT_PASSWORD, ENV_MQTT_QOS,
|
||||
ENV_MQTT_QUEUE_DIR, ENV_MQTT_QUEUE_LIMIT, ENV_MQTT_RECONNECT_INTERVAL, ENV_MQTT_TOPIC, ENV_MQTT_USERNAME,
|
||||
ENV_WEBHOOK_AUTH_TOKEN, ENV_WEBHOOK_CLIENT_CERT, ENV_WEBHOOK_CLIENT_KEY, ENV_WEBHOOK_ENABLE, ENV_WEBHOOK_ENDPOINT,
|
||||
ENV_WEBHOOK_QUEUE_DIR, ENV_WEBHOOK_QUEUE_LIMIT, MQTT_BROKER, MQTT_KEEP_ALIVE_INTERVAL, MQTT_PASSWORD, MQTT_QOS,
|
||||
MQTT_QUEUE_DIR, MQTT_QUEUE_LIMIT, MQTT_RECONNECT_INTERVAL, MQTT_TOPIC, MQTT_USERNAME, WEBHOOK_AUTH_TOKEN,
|
||||
WEBHOOK_CLIENT_CERT, WEBHOOK_CLIENT_KEY, WEBHOOK_ENDPOINT, WEBHOOK_QUEUE_DIR, WEBHOOK_QUEUE_LIMIT,
|
||||
DEFAULT_DIR, DEFAULT_LIMIT, ENV_NOTIFY_MQTT_KEYS, ENV_NOTIFY_WEBHOOK_KEYS, MQTT_BROKER, MQTT_KEEP_ALIVE_INTERVAL,
|
||||
MQTT_PASSWORD, MQTT_QOS, MQTT_QUEUE_DIR, MQTT_QUEUE_LIMIT, MQTT_RECONNECT_INTERVAL, MQTT_TOPIC, MQTT_USERNAME,
|
||||
NOTIFY_MQTT_KEYS, NOTIFY_WEBHOOK_KEYS, WEBHOOK_AUTH_TOKEN, WEBHOOK_CLIENT_CERT, WEBHOOK_CLIENT_KEY, WEBHOOK_ENDPOINT,
|
||||
WEBHOOK_QUEUE_DIR, WEBHOOK_QUEUE_LIMIT,
|
||||
};
|
||||
use rustfs_config::{DEFAULT_DELIMITER, ENV_WORD_DELIMITER_DASH};
|
||||
use rustfs_ecstore::config::{ENABLE_KEY, ENABLE_ON, KVS};
|
||||
use rustfs_ecstore::config::KVS;
|
||||
use std::collections::HashSet;
|
||||
use std::time::Duration;
|
||||
use tracing::{debug, warn};
|
||||
use url::Url;
|
||||
|
||||
/// Helper function to get values from environment variables or KVS configurations.
|
||||
///
|
||||
/// It will give priority to reading from environment variables such as `BASE_ENV_KEY_ID` and fall back to the KVS configuration if it fails.
|
||||
fn get_config_value(id: &str, base_env_key: &str, config_key: &str, config: &KVS) -> Option<String> {
|
||||
let env_key = if id != DEFAULT_DELIMITER {
|
||||
format!(
|
||||
"{}{}{}",
|
||||
base_env_key,
|
||||
DEFAULT_DELIMITER,
|
||||
id.to_uppercase().replace(ENV_WORD_DELIMITER_DASH, DEFAULT_DELIMITER)
|
||||
)
|
||||
} else {
|
||||
base_env_key.to_string()
|
||||
};
|
||||
|
||||
match std::env::var(&env_key) {
|
||||
Ok(val) => Some(val),
|
||||
Err(_) => config.lookup(config_key),
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait for creating targets from configuration
|
||||
#[async_trait]
|
||||
pub trait TargetFactory: Send + Sync {
|
||||
@@ -61,6 +38,14 @@ pub trait TargetFactory: Send + Sync {
|
||||
|
||||
/// Validates target configuration
|
||||
fn validate_config(&self, id: &str, config: &KVS) -> Result<(), TargetError>;
|
||||
|
||||
/// Returns a set of valid configuration field names for this target type.
|
||||
/// This is used to filter environment variables.
|
||||
fn get_valid_fields(&self) -> HashSet<String>;
|
||||
|
||||
/// Returns a set of valid configuration env field names for this target type.
|
||||
/// This is used to filter environment variables.
|
||||
fn get_valid_env_fields(&self) -> HashSet<String>;
|
||||
}
|
||||
|
||||
/// Factory for creating Webhook targets
|
||||
@@ -69,65 +54,42 @@ pub struct WebhookTargetFactory;
|
||||
#[async_trait]
|
||||
impl TargetFactory for WebhookTargetFactory {
|
||||
async fn create_target(&self, id: String, config: &KVS) -> Result<Box<dyn Target + Send + Sync>, TargetError> {
|
||||
let get = |base_env_key: &str, config_key: &str| get_config_value(&id, base_env_key, config_key, config);
|
||||
|
||||
let enable = get(ENV_WEBHOOK_ENABLE, ENABLE_KEY)
|
||||
.map(|v| v.eq_ignore_ascii_case(ENABLE_ON) || v.eq_ignore_ascii_case("true"))
|
||||
.unwrap_or(false);
|
||||
|
||||
if !enable {
|
||||
return Err(TargetError::Configuration("Target is disabled".to_string()));
|
||||
}
|
||||
|
||||
let endpoint = get(ENV_WEBHOOK_ENDPOINT, WEBHOOK_ENDPOINT)
|
||||
// All config values are now read directly from the merged `config` KVS.
|
||||
let endpoint = config
|
||||
.lookup(WEBHOOK_ENDPOINT)
|
||||
.ok_or_else(|| TargetError::Configuration("Missing webhook endpoint".to_string()))?;
|
||||
let endpoint_url = Url::parse(&endpoint)
|
||||
.map_err(|e| TargetError::Configuration(format!("Invalid endpoint URL: {e} (value: '{endpoint}')")))?;
|
||||
|
||||
let auth_token = get(ENV_WEBHOOK_AUTH_TOKEN, WEBHOOK_AUTH_TOKEN).unwrap_or_default();
|
||||
let queue_dir = get(ENV_WEBHOOK_QUEUE_DIR, WEBHOOK_QUEUE_DIR).unwrap_or(DEFAULT_DIR.to_string());
|
||||
|
||||
let queue_limit = get(ENV_WEBHOOK_QUEUE_LIMIT, WEBHOOK_QUEUE_LIMIT)
|
||||
.and_then(|v| v.parse::<u64>().ok())
|
||||
.unwrap_or(DEFAULT_LIMIT);
|
||||
|
||||
let client_cert = get(ENV_WEBHOOK_CLIENT_CERT, WEBHOOK_CLIENT_CERT).unwrap_or_default();
|
||||
let client_key = get(ENV_WEBHOOK_CLIENT_KEY, WEBHOOK_CLIENT_KEY).unwrap_or_default();
|
||||
|
||||
let args = WebhookArgs {
|
||||
enable,
|
||||
enable: true, // If we are here, it's already enabled.
|
||||
endpoint: endpoint_url,
|
||||
auth_token,
|
||||
queue_dir,
|
||||
queue_limit,
|
||||
client_cert,
|
||||
client_key,
|
||||
auth_token: config.lookup(WEBHOOK_AUTH_TOKEN).unwrap_or_default(),
|
||||
queue_dir: config.lookup(WEBHOOK_QUEUE_DIR).unwrap_or(DEFAULT_DIR.to_string()),
|
||||
queue_limit: config
|
||||
.lookup(WEBHOOK_QUEUE_LIMIT)
|
||||
.and_then(|v| v.parse::<u64>().ok())
|
||||
.unwrap_or(DEFAULT_LIMIT),
|
||||
client_cert: config.lookup(WEBHOOK_CLIENT_CERT).unwrap_or_default(),
|
||||
client_key: config.lookup(WEBHOOK_CLIENT_KEY).unwrap_or_default(),
|
||||
};
|
||||
|
||||
let target = crate::target::webhook::WebhookTarget::new(id, args)?;
|
||||
Ok(Box::new(target))
|
||||
}
|
||||
|
||||
fn validate_config(&self, id: &str, config: &KVS) -> Result<(), TargetError> {
|
||||
let get = |base_env_key: &str, config_key: &str| get_config_value(id, base_env_key, config_key, config);
|
||||
|
||||
let enable = get(ENV_WEBHOOK_ENABLE, ENABLE_KEY)
|
||||
.map(|v| v.eq_ignore_ascii_case(ENABLE_ON) || v.eq_ignore_ascii_case("true"))
|
||||
.unwrap_or(false);
|
||||
|
||||
if !enable {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let endpoint = get(ENV_WEBHOOK_ENDPOINT, WEBHOOK_ENDPOINT)
|
||||
fn validate_config(&self, _id: &str, config: &KVS) -> Result<(), TargetError> {
|
||||
// Validation also uses the merged `config` KVS directly.
|
||||
let endpoint = config
|
||||
.lookup(WEBHOOK_ENDPOINT)
|
||||
.ok_or_else(|| TargetError::Configuration("Missing webhook endpoint".to_string()))?;
|
||||
debug!("endpoint: {}", endpoint);
|
||||
let parsed_endpoint = endpoint.trim();
|
||||
Url::parse(parsed_endpoint)
|
||||
.map_err(|e| TargetError::Configuration(format!("Invalid endpoint URL: {e} (value: '{parsed_endpoint}')")))?;
|
||||
|
||||
let client_cert = get(ENV_WEBHOOK_CLIENT_CERT, WEBHOOK_CLIENT_CERT).unwrap_or_default();
|
||||
let client_key = get(ENV_WEBHOOK_CLIENT_KEY, WEBHOOK_CLIENT_KEY).unwrap_or_default();
|
||||
let client_cert = config.lookup(WEBHOOK_CLIENT_CERT).unwrap_or_default();
|
||||
let client_key = config.lookup(WEBHOOK_CLIENT_KEY).unwrap_or_default();
|
||||
|
||||
if client_cert.is_empty() != client_key.is_empty() {
|
||||
return Err(TargetError::Configuration(
|
||||
@@ -135,15 +97,21 @@ impl TargetFactory for WebhookTargetFactory {
|
||||
));
|
||||
}
|
||||
|
||||
let queue_dir = get(ENV_WEBHOOK_QUEUE_DIR, WEBHOOK_QUEUE_DIR)
|
||||
.and_then(|v| v.parse::<String>().ok())
|
||||
.unwrap_or(DEFAULT_DIR.to_string());
|
||||
let queue_dir = config.lookup(WEBHOOK_QUEUE_DIR).unwrap_or(DEFAULT_DIR.to_string());
|
||||
if !queue_dir.is_empty() && !std::path::Path::new(&queue_dir).is_absolute() {
|
||||
return Err(TargetError::Configuration("Webhook queue directory must be an absolute path".to_string()));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_valid_fields(&self) -> HashSet<String> {
|
||||
NOTIFY_WEBHOOK_KEYS.iter().map(|s| s.to_string()).collect()
|
||||
}
|
||||
|
||||
fn get_valid_env_fields(&self) -> HashSet<String> {
|
||||
ENV_NOTIFY_WEBHOOK_KEYS.iter().map(|s| s.to_string()).collect()
|
||||
}
|
||||
}
|
||||
|
||||
/// Factory for creating MQTT targets
|
||||
@@ -152,84 +120,57 @@ pub struct MQTTTargetFactory;
|
||||
#[async_trait]
|
||||
impl TargetFactory for MQTTTargetFactory {
|
||||
async fn create_target(&self, id: String, config: &KVS) -> Result<Box<dyn Target + Send + Sync>, TargetError> {
|
||||
let get = |base_env_key: &str, config_key: &str| get_config_value(&id, base_env_key, config_key, config);
|
||||
|
||||
let enable = get(ENV_MQTT_ENABLE, ENABLE_KEY)
|
||||
.map(|v| v.eq_ignore_ascii_case(ENABLE_ON) || v.eq_ignore_ascii_case("true"))
|
||||
.unwrap_or(false);
|
||||
|
||||
if !enable {
|
||||
return Err(TargetError::Configuration("Target is disabled".to_string()));
|
||||
}
|
||||
|
||||
let broker =
|
||||
get(ENV_MQTT_BROKER, MQTT_BROKER).ok_or_else(|| TargetError::Configuration("Missing MQTT broker".to_string()))?;
|
||||
let broker = config
|
||||
.lookup(MQTT_BROKER)
|
||||
.ok_or_else(|| TargetError::Configuration("Missing MQTT broker".to_string()))?;
|
||||
let broker_url = Url::parse(&broker)
|
||||
.map_err(|e| TargetError::Configuration(format!("Invalid broker URL: {e} (value: '{broker}')")))?;
|
||||
|
||||
let topic =
|
||||
get(ENV_MQTT_TOPIC, MQTT_TOPIC).ok_or_else(|| TargetError::Configuration("Missing MQTT topic".to_string()))?;
|
||||
|
||||
let qos = get(ENV_MQTT_QOS, MQTT_QOS)
|
||||
.and_then(|v| v.parse::<u8>().ok())
|
||||
.map(|q| match q {
|
||||
0 => QoS::AtMostOnce,
|
||||
1 => QoS::AtLeastOnce,
|
||||
2 => QoS::ExactlyOnce,
|
||||
_ => QoS::AtLeastOnce,
|
||||
})
|
||||
.unwrap_or(QoS::AtLeastOnce);
|
||||
|
||||
let username = get(ENV_MQTT_USERNAME, MQTT_USERNAME).unwrap_or_default();
|
||||
let password = get(ENV_MQTT_PASSWORD, MQTT_PASSWORD).unwrap_or_default();
|
||||
|
||||
let reconnect_interval = get(ENV_MQTT_RECONNECT_INTERVAL, MQTT_RECONNECT_INTERVAL)
|
||||
.and_then(|v| v.parse::<u64>().ok())
|
||||
.map(Duration::from_secs)
|
||||
.unwrap_or_else(|| Duration::from_secs(5));
|
||||
|
||||
let keep_alive = get(ENV_MQTT_KEEP_ALIVE_INTERVAL, MQTT_KEEP_ALIVE_INTERVAL)
|
||||
.and_then(|v| v.parse::<u64>().ok())
|
||||
.map(Duration::from_secs)
|
||||
.unwrap_or_else(|| Duration::from_secs(30));
|
||||
|
||||
let queue_dir = get(ENV_MQTT_QUEUE_DIR, MQTT_QUEUE_DIR)
|
||||
.and_then(|v| v.parse::<String>().ok())
|
||||
.unwrap_or(DEFAULT_DIR.to_string());
|
||||
let queue_limit = get(ENV_MQTT_QUEUE_LIMIT, MQTT_QUEUE_LIMIT)
|
||||
.and_then(|v| v.parse::<u64>().ok())
|
||||
.unwrap_or(DEFAULT_LIMIT);
|
||||
let topic = config
|
||||
.lookup(MQTT_TOPIC)
|
||||
.ok_or_else(|| TargetError::Configuration("Missing MQTT topic".to_string()))?;
|
||||
|
||||
let args = MQTTArgs {
|
||||
enable,
|
||||
enable: true, // Assumed enabled.
|
||||
broker: broker_url,
|
||||
topic,
|
||||
qos,
|
||||
username,
|
||||
password,
|
||||
max_reconnect_interval: reconnect_interval,
|
||||
keep_alive,
|
||||
queue_dir,
|
||||
queue_limit,
|
||||
qos: config
|
||||
.lookup(MQTT_QOS)
|
||||
.and_then(|v| v.parse::<u8>().ok())
|
||||
.map(|q| match q {
|
||||
0 => QoS::AtMostOnce,
|
||||
1 => QoS::AtLeastOnce,
|
||||
2 => QoS::ExactlyOnce,
|
||||
_ => QoS::AtLeastOnce,
|
||||
})
|
||||
.unwrap_or(QoS::AtLeastOnce),
|
||||
username: config.lookup(MQTT_USERNAME).unwrap_or_default(),
|
||||
password: config.lookup(MQTT_PASSWORD).unwrap_or_default(),
|
||||
max_reconnect_interval: config
|
||||
.lookup(MQTT_RECONNECT_INTERVAL)
|
||||
.and_then(|v| v.parse::<u64>().ok())
|
||||
.map(Duration::from_secs)
|
||||
.unwrap_or_else(|| Duration::from_secs(5)),
|
||||
keep_alive: config
|
||||
.lookup(MQTT_KEEP_ALIVE_INTERVAL)
|
||||
.and_then(|v| v.parse::<u64>().ok())
|
||||
.map(Duration::from_secs)
|
||||
.unwrap_or_else(|| Duration::from_secs(30)),
|
||||
queue_dir: config.lookup(MQTT_QUEUE_DIR).unwrap_or(DEFAULT_DIR.to_string()),
|
||||
queue_limit: config
|
||||
.lookup(MQTT_QUEUE_LIMIT)
|
||||
.and_then(|v| v.parse::<u64>().ok())
|
||||
.unwrap_or(DEFAULT_LIMIT),
|
||||
};
|
||||
|
||||
let target = crate::target::mqtt::MQTTTarget::new(id, args)?;
|
||||
Ok(Box::new(target))
|
||||
}
|
||||
|
||||
fn validate_config(&self, id: &str, config: &KVS) -> Result<(), TargetError> {
|
||||
let get = |base_env_key: &str, config_key: &str| get_config_value(id, base_env_key, config_key, config);
|
||||
|
||||
let enable = get(ENV_MQTT_ENABLE, ENABLE_KEY)
|
||||
.map(|v| v.eq_ignore_ascii_case(ENABLE_ON) || v.eq_ignore_ascii_case("true"))
|
||||
.unwrap_or(false);
|
||||
|
||||
if !enable {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let broker =
|
||||
get(ENV_MQTT_BROKER, MQTT_BROKER).ok_or_else(|| TargetError::Configuration("Missing MQTT broker".to_string()))?;
|
||||
fn validate_config(&self, _id: &str, config: &KVS) -> Result<(), TargetError> {
|
||||
let broker = config
|
||||
.lookup(MQTT_BROKER)
|
||||
.ok_or_else(|| TargetError::Configuration("Missing MQTT broker".to_string()))?;
|
||||
let url = Url::parse(&broker)
|
||||
.map_err(|e| TargetError::Configuration(format!("Invalid broker URL: {e} (value: '{broker}')")))?;
|
||||
|
||||
@@ -240,11 +181,11 @@ impl TargetFactory for MQTTTargetFactory {
|
||||
}
|
||||
}
|
||||
|
||||
if get(ENV_MQTT_TOPIC, MQTT_TOPIC).is_none() {
|
||||
if config.lookup(MQTT_TOPIC).is_none() {
|
||||
return Err(TargetError::Configuration("Missing MQTT topic".to_string()));
|
||||
}
|
||||
|
||||
if let Some(qos_str) = get(ENV_MQTT_QOS, MQTT_QOS) {
|
||||
if let Some(qos_str) = config.lookup(MQTT_QOS) {
|
||||
let qos = qos_str
|
||||
.parse::<u8>()
|
||||
.map_err(|_| TargetError::Configuration("Invalid QoS value".to_string()))?;
|
||||
@@ -253,14 +194,12 @@ impl TargetFactory for MQTTTargetFactory {
|
||||
}
|
||||
}
|
||||
|
||||
let queue_dir = get(ENV_MQTT_QUEUE_DIR, MQTT_QUEUE_DIR)
|
||||
.and_then(|v| v.parse::<String>().ok())
|
||||
.unwrap_or(DEFAULT_DIR.to_string());
|
||||
let queue_dir = config.lookup(MQTT_QUEUE_DIR).unwrap_or_default();
|
||||
if !queue_dir.is_empty() {
|
||||
if !std::path::Path::new(&queue_dir).is_absolute() {
|
||||
return Err(TargetError::Configuration("MQTT queue directory must be an absolute path".to_string()));
|
||||
}
|
||||
if let Some(qos_str) = get(ENV_MQTT_QOS, MQTT_QOS) {
|
||||
if let Some(qos_str) = config.lookup(MQTT_QOS) {
|
||||
if qos_str == "0" {
|
||||
warn!("Using queue_dir with QoS 0 may result in event loss");
|
||||
}
|
||||
@@ -269,4 +208,12 @@ impl TargetFactory for MQTTTargetFactory {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_valid_fields(&self) -> HashSet<String> {
|
||||
NOTIFY_MQTT_KEYS.iter().map(|s| s.to_string()).collect()
|
||||
}
|
||||
|
||||
fn get_valid_env_fields(&self) -> HashSet<String> {
|
||||
ENV_NOTIFY_MQTT_KEYS.iter().map(|s| s.to_string()).collect()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,10 +210,10 @@ impl NotificationSystem {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if let Err(e) = rustfs_ecstore::config::com::save_server_config(store, &new_config).await {
|
||||
error!("Failed to save config: {}", e);
|
||||
return Err(NotificationError::SaveConfig(e.to_string()));
|
||||
}
|
||||
// if let Err(e) = rustfs_ecstore::config::com::save_server_config(store, &new_config).await {
|
||||
// error!("Failed to save config: {}", e);
|
||||
// return Err(NotificationError::SaveConfig(e.to_string()));
|
||||
// }
|
||||
|
||||
info!("Configuration updated. Reloading system...");
|
||||
self.reload_config(new_config).await
|
||||
@@ -323,7 +323,6 @@ impl NotificationSystem {
|
||||
metrics: Arc<NotificationMetrics>,
|
||||
semaphore: Arc<Semaphore>,
|
||||
) -> mpsc::Sender<()> {
|
||||
// Event Stream Processing Using Batch Version
|
||||
stream::start_event_stream_with_batching(store, target, metrics, semaphore)
|
||||
}
|
||||
|
||||
@@ -348,6 +347,7 @@ impl NotificationSystem {
|
||||
self.update_config(new_config.clone()).await;
|
||||
|
||||
// Create a new target from configuration
|
||||
// This function will now be responsible for merging env, creating and persisting the final configuration.
|
||||
let targets: Vec<Box<dyn Target + Send + Sync>> = self
|
||||
.registry
|
||||
.create_targets_from_config(&new_config)
|
||||
|
||||
+192
-64
@@ -18,11 +18,12 @@ use crate::{
|
||||
factory::{MQTTTargetFactory, TargetFactory, WebhookTargetFactory},
|
||||
target::Target,
|
||||
};
|
||||
use rustfs_config::notify::NOTIFY_ROUTE_PREFIX;
|
||||
use futures::stream::{FuturesUnordered, StreamExt};
|
||||
use rustfs_config::notify::{ENABLE_KEY, ENABLE_ON, NOTIFY_ROUTE_PREFIX};
|
||||
use rustfs_config::{DEFAULT_DELIMITER, ENV_PREFIX};
|
||||
use rustfs_ecstore::config::{Config, ENABLE_KEY, ENABLE_OFF, ENABLE_ON, KVS};
|
||||
use std::collections::HashMap;
|
||||
use tracing::{debug, error, info};
|
||||
use rustfs_ecstore::config::{Config, KVS};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use tracing::{debug, error, info, warn};
|
||||
|
||||
/// Registry for managing target factories
|
||||
pub struct TargetRegistry {
|
||||
@@ -74,77 +75,204 @@ impl TargetRegistry {
|
||||
}
|
||||
|
||||
/// Creates all targets from a configuration
|
||||
/// Create all notification targets from system configuration and environment variables.
|
||||
/// This method processes the creation of each target concurrently as follows:
|
||||
/// 1. Iterate through all registered target types (e.g. webhooks, mqtt).
|
||||
/// 2. For each type, resolve its configuration in the configuration file and environment variables.
|
||||
/// 3. Identify all target instance IDs that need to be created.
|
||||
/// 4. Combine the default configuration, file configuration, and environment variable configuration for each instance.
|
||||
/// 5. If the instance is enabled, create an asynchronous task for it to instantiate.
|
||||
/// 6. Concurrency executes all creation tasks and collects results.
|
||||
pub async fn create_targets_from_config(&self, config: &Config) -> Result<Vec<Box<dyn Target + Send + Sync>>, TargetError> {
|
||||
let mut targets: Vec<Box<dyn Target + Send + Sync>> = Vec::new();
|
||||
// Collect only environment variables with the relevant prefix to reduce memory usage
|
||||
let all_env: Vec<(String, String)> = std::env::vars().filter(|(key, _)| key.starts_with(ENV_PREFIX)).collect();
|
||||
// A collection of asynchronous tasks for concurrently executing target creation
|
||||
let mut tasks = FuturesUnordered::new();
|
||||
let mut final_config = config.clone(); // Clone a configuration for aggregating the final result
|
||||
// 1. Traverse all registered plants and process them by target type
|
||||
for (target_type, factory) in &self.factories {
|
||||
tracing::Span::current().record("target_type", target_type.as_str());
|
||||
info!("Start working on target types...");
|
||||
|
||||
// Iterate through configuration sections
|
||||
for (section, subsections) in &config.0 {
|
||||
// Only process notification sections
|
||||
if !section.starts_with(NOTIFY_ROUTE_PREFIX) {
|
||||
continue;
|
||||
// 2. Prepare the configuration source
|
||||
// 2.1. Get the configuration segment in the file, e.g. 'notify_webhook'
|
||||
let section_name = format!("{NOTIFY_ROUTE_PREFIX}{target_type}");
|
||||
let file_configs = config.0.get(§ion_name).cloned().unwrap_or_default();
|
||||
// 2.2. Get the default configuration for that type
|
||||
let default_cfg = file_configs.get(DEFAULT_DELIMITER).cloned().unwrap_or_default();
|
||||
debug!(?default_cfg, "Get the default configuration");
|
||||
|
||||
// *** Optimization point 1: Get all legitimate fields of the current target type ***
|
||||
let valid_fields = factory.get_valid_fields();
|
||||
debug!(?valid_fields, "Get the legitimate configuration fields");
|
||||
|
||||
// 3. Resolve instance IDs and configuration overrides from environment variables
|
||||
let mut instance_ids_from_env = HashSet::new();
|
||||
// 3.1. Instance discovery: Based on the '..._ENABLE_INSTANCEID' format
|
||||
let enable_prefix = format!("{ENV_PREFIX}{NOTIFY_ROUTE_PREFIX}{target_type}_{ENABLE_KEY}_").to_uppercase();
|
||||
for (key, value) in &all_env {
|
||||
if value.eq_ignore_ascii_case(ENABLE_ON)
|
||||
|| value.eq_ignore_ascii_case("true")
|
||||
|| value.eq_ignore_ascii_case("1")
|
||||
|| value.eq_ignore_ascii_case("yes")
|
||||
{
|
||||
if let Some(id) = key.strip_prefix(&enable_prefix) {
|
||||
if !id.is_empty() {
|
||||
instance_ids_from_env.insert(id.to_lowercase());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Extract target type from section name
|
||||
let target_type = section.trim_start_matches(NOTIFY_ROUTE_PREFIX);
|
||||
// 3.2. Parse all relevant environment variable configurations
|
||||
// 3.2.1. Build environment variable prefixes such as 'RUSTFS_NOTIFY_WEBHOOK_'
|
||||
let env_prefix = format!("{ENV_PREFIX}{NOTIFY_ROUTE_PREFIX}{target_type}_").to_uppercase();
|
||||
// 3.2.2. 'env_overrides' is used to store configurations parsed from environment variables in the format: {instance id -> {field -> value}}
|
||||
let mut env_overrides: HashMap<String, HashMap<String, String>> = HashMap::new();
|
||||
for (key, value) in &all_env {
|
||||
if let Some(rest) = key.strip_prefix(&env_prefix) {
|
||||
// Use rsplitn to split from the right side to properly extract the INSTANCE_ID at the end
|
||||
// Format: <FIELD_NAME>_<INSTANCE_ID> or <FIELD_NAME>
|
||||
let mut parts = rest.rsplitn(2, '_');
|
||||
|
||||
// Iterate through subsections (each representing a target instance)
|
||||
for (target_id, target_config) in subsections {
|
||||
// Skip disabled targets
|
||||
// The first part from the right is INSTANCE_ID
|
||||
let instance_id_part = parts.next().unwrap_or(DEFAULT_DELIMITER);
|
||||
// The remaining part is FIELD_NAME
|
||||
let field_name_part = parts.next();
|
||||
|
||||
let enable_from_config = target_config.lookup(ENABLE_KEY).unwrap_or_else(|| ENABLE_OFF.to_string());
|
||||
debug!("Target enablement from config: {}/{}: {}", target_type, target_id, enable_from_config);
|
||||
// Check environment variable for target enablement example: RUSTFS_NOTIFY_WEBHOOK_ENABLE|RUSTFS_NOTIFY_WEBHOOK_ENABLE_[TARGET_ID]
|
||||
let env_key = if target_id == DEFAULT_DELIMITER {
|
||||
// If no specific target ID, use the base target type, example: RUSTFS_NOTIFY_WEBHOOK_ENABLE
|
||||
format!(
|
||||
"{}{}{}{}{}",
|
||||
ENV_PREFIX,
|
||||
NOTIFY_ROUTE_PREFIX,
|
||||
target_type.to_uppercase(),
|
||||
DEFAULT_DELIMITER,
|
||||
ENABLE_KEY
|
||||
)
|
||||
} else {
|
||||
// If specific target ID, append it to the key, example: RUSTFS_NOTIFY_WEBHOOK_ENABLE_[TARGET_ID]
|
||||
format!(
|
||||
"{}{}{}{}{}{}{}",
|
||||
ENV_PREFIX,
|
||||
NOTIFY_ROUTE_PREFIX,
|
||||
target_type.to_uppercase(),
|
||||
DEFAULT_DELIMITER,
|
||||
ENABLE_KEY,
|
||||
DEFAULT_DELIMITER,
|
||||
target_id.to_uppercase()
|
||||
)
|
||||
let (field_name, instance_id) = match field_name_part {
|
||||
// Case 1: The format is <FIELD_NAME>_<INSTANCE_ID>
|
||||
// e.g., rest = "ENDPOINT_PRIMARY" -> field_name="ENDPOINT", instance_id="PRIMARY"
|
||||
Some(field) => (field.to_lowercase(), instance_id_part.to_lowercase()),
|
||||
// Case 2: The format is <FIELD_NAME> (无 INSTANCE_ID)
|
||||
// e.g., rest = "ENABLE" -> field_name="ENABLE", instance_id="" (Universal configuration `_ DEFAULT_DELIMITER`)
|
||||
None => (instance_id_part.to_lowercase(), DEFAULT_DELIMITER.to_string()),
|
||||
};
|
||||
|
||||
// *** Optimization point 2: Verify whether the parsed field_name is legal ***
|
||||
if !field_name.is_empty() && valid_fields.contains(&field_name) {
|
||||
debug!(
|
||||
instance_id = %if instance_id.is_empty() { DEFAULT_DELIMITER } else { &instance_id },
|
||||
%field_name,
|
||||
%value,
|
||||
"Parsing to environment variables"
|
||||
);
|
||||
env_overrides
|
||||
.entry(instance_id)
|
||||
.or_default()
|
||||
.insert(field_name, value.clone());
|
||||
} else {
|
||||
// Ignore illegal field names
|
||||
warn!(
|
||||
field_name = %field_name,
|
||||
"Ignore environment variable fields, not found in the list of valid fields for target type {}",
|
||||
target_type
|
||||
);
|
||||
}
|
||||
}
|
||||
.to_uppercase();
|
||||
debug!("Target env key: {},Target id: {}", env_key, target_id);
|
||||
let enable_from_env = std::env::var(&env_key)
|
||||
.map(|v| v.eq_ignore_ascii_case(ENABLE_ON) || v.eq_ignore_ascii_case("true"))
|
||||
}
|
||||
debug!(?env_overrides, "Complete the environment variable analysis");
|
||||
|
||||
// 4. Determine all instance IDs that need to be processed
|
||||
let mut all_instance_ids: HashSet<String> =
|
||||
file_configs.keys().filter(|k| *k != DEFAULT_DELIMITER).cloned().collect();
|
||||
all_instance_ids.extend(instance_ids_from_env);
|
||||
debug!(?all_instance_ids, "Determine all instance IDs");
|
||||
|
||||
// 5. Merge configurations and create tasks for each instance
|
||||
for id in all_instance_ids {
|
||||
// 5.1. Merge configuration, priority: Environment variables > File instance configuration > File default configuration
|
||||
let mut merged_config = default_cfg.clone();
|
||||
// Instance-specific configuration in application files
|
||||
if let Some(file_instance_cfg) = file_configs.get(&id) {
|
||||
merged_config.extend(file_instance_cfg.clone());
|
||||
}
|
||||
// Application instance-specific environment variable configuration
|
||||
if let Some(env_instance_cfg) = env_overrides.get(&id) {
|
||||
// Convert HashMap<String, String> to KVS
|
||||
let mut kvs_from_env = KVS::new();
|
||||
for (k, v) in env_instance_cfg {
|
||||
kvs_from_env.insert(k.clone(), v.clone());
|
||||
}
|
||||
merged_config.extend(kvs_from_env);
|
||||
}
|
||||
debug!(instance_id = %id, ?merged_config, "Complete configuration merge");
|
||||
|
||||
// 5.2. Check if the instance is enabled
|
||||
let enabled = merged_config
|
||||
.lookup(ENABLE_KEY)
|
||||
.map(|v| {
|
||||
v.eq_ignore_ascii_case(ENABLE_ON)
|
||||
|| v.eq_ignore_ascii_case("true")
|
||||
|| v.eq_ignore_ascii_case("1")
|
||||
|| v.eq_ignore_ascii_case("yes")
|
||||
})
|
||||
.unwrap_or(false);
|
||||
debug!("Target env value: {},key: {},Target id: {}", enable_from_env, env_key, target_id);
|
||||
debug!(
|
||||
"Target enablement from env: {}/{}: result: {}",
|
||||
target_type, target_id, enable_from_config
|
||||
);
|
||||
if enable_from_config != ENABLE_ON && !enable_from_env {
|
||||
info!("Skipping disabled target: {}/{}", target_type, target_id);
|
||||
continue;
|
||||
}
|
||||
debug!("create target: {}/{} start", target_type, target_id);
|
||||
// Create target
|
||||
match self.create_target(target_type, target_id.clone(), target_config).await {
|
||||
Ok(target) => {
|
||||
info!("Created target: {}/{}", target_type, target_id);
|
||||
targets.push(target);
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to create target {}/{}: reason: {}", target_type, target_id, e);
|
||||
}
|
||||
|
||||
if enabled {
|
||||
info!(instance_id = %id, "Target is enabled, ready to create a task");
|
||||
// 5.3. Create asynchronous tasks for enabled instances
|
||||
let target_type_clone = target_type.clone();
|
||||
let tid = id.clone();
|
||||
let merged_config_arc = std::sync::Arc::new(merged_config);
|
||||
tasks.push(async move {
|
||||
let result = factory.create_target(tid.clone(), &merged_config_arc).await;
|
||||
(target_type_clone, tid, result, std::sync::Arc::clone(&merged_config_arc))
|
||||
});
|
||||
} else {
|
||||
info!(instance_id = %id, "Skip the disabled target and will be removed from the final configuration");
|
||||
// Remove disabled target from final configuration
|
||||
final_config.0.entry(section_name.clone()).or_default().remove(&id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(targets)
|
||||
// 6. Concurrently execute all creation tasks and collect results
|
||||
let mut successful_targets = Vec::new();
|
||||
let mut successful_configs = Vec::new();
|
||||
while let Some((target_type, id, result, final_config)) = tasks.next().await {
|
||||
match result {
|
||||
Ok(target) => {
|
||||
info!(target_type = %target_type, instance_id = %id, "Create a target successfully");
|
||||
successful_targets.push(target);
|
||||
successful_configs.push((target_type, id, final_config));
|
||||
}
|
||||
Err(e) => {
|
||||
error!(target_type = %target_type, instance_id = %id, error = %e, "Failed to create a target");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 7. Aggregate new configuration and write back to system configuration
|
||||
if !successful_configs.is_empty() {
|
||||
info!(
|
||||
"Prepare to update {} successfully created target configurations to the system configuration...",
|
||||
successful_configs.len()
|
||||
);
|
||||
let mut new_config = config.clone();
|
||||
for (target_type, id, kvs) in successful_configs {
|
||||
let section_name = format!("{NOTIFY_ROUTE_PREFIX}{target_type}").to_lowercase();
|
||||
new_config.0.entry(section_name).or_default().insert(id, (*kvs).clone());
|
||||
}
|
||||
|
||||
let Some(store) = rustfs_ecstore::global::new_object_layer_fn() else {
|
||||
return Err(TargetError::ServerNotInitialized(
|
||||
"Failed to save target configuration: server storage not initialized".to_string(),
|
||||
));
|
||||
};
|
||||
|
||||
match rustfs_ecstore::config::com::save_server_config(store, &new_config).await {
|
||||
Ok(_) => {
|
||||
info!("The new configuration was saved to the system successfully.")
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to save the new configuration: {}", e);
|
||||
return Err(TargetError::SaveConfig(e.to_string()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
info!(count = successful_targets.len(), "All target processing completed");
|
||||
Ok(successful_targets)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,3 +109,11 @@ impl std::fmt::Display for ChannelTargetType {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_bool(value: &str) -> Result<bool, TargetError> {
|
||||
match value.to_lowercase().as_str() {
|
||||
"true" | "on" | "yes" | "1" => Ok(true),
|
||||
"false" | "off" | "no" | "0" => Ok(false),
|
||||
_ => Err(TargetError::ParseError(format!("Unable to parse boolean: {value}"))),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,8 @@ webhook = ["dep:reqwest"]
|
||||
kafka = ["dep:rdkafka"]
|
||||
|
||||
[dependencies]
|
||||
rustfs-config = { workspace = true, features = ["constants"] }
|
||||
rustfs-config = { workspace = true, features = ["constants", "observability"] }
|
||||
rustfs-utils = { workspace = true, features = ["ip", "path"] }
|
||||
async-trait = { workspace = true }
|
||||
chrono = { workspace = true }
|
||||
flexi_logger = { workspace = true, features = ["trc", "kv"] }
|
||||
@@ -49,7 +50,6 @@ opentelemetry_sdk = { workspace = true, features = ["rt-tokio"] }
|
||||
opentelemetry-stdout = { workspace = true }
|
||||
opentelemetry-otlp = { workspace = true, features = ["grpc-tonic", "gzip-tonic", "trace", "metrics", "logs", "internal-logs"] }
|
||||
opentelemetry-semantic-conventions = { workspace = true, features = ["semconv_experimental"] }
|
||||
rustfs-utils = { workspace = true, features = ["ip"] }
|
||||
serde = { workspace = true }
|
||||
smallvec = { workspace = true, features = ["serde"] }
|
||||
tracing = { workspace = true, features = ["std", "attributes"] }
|
||||
|
||||
+67
-57
@@ -12,11 +12,24 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use rustfs_config::{
|
||||
APP_NAME, DEFAULT_LOG_DIR, DEFAULT_LOG_FILENAME, DEFAULT_LOG_KEEP_FILES, DEFAULT_LOG_LEVEL, DEFAULT_LOG_ROTATION_SIZE_MB,
|
||||
DEFAULT_LOG_ROTATION_TIME, DEFAULT_OBS_LOG_FILENAME, DEFAULT_SINK_FILE_LOG_FILE, ENVIRONMENT, METER_INTERVAL, SAMPLE_RATIO,
|
||||
SERVICE_VERSION, USE_STDOUT,
|
||||
use rustfs_config::observability::{
|
||||
DEFAULT_AUDIT_LOGGER_QUEUE_CAPACITY, DEFAULT_SINKS_FILE_BUFFER_SIZE, DEFAULT_SINKS_FILE_FLUSH_INTERVAL_MS,
|
||||
DEFAULT_SINKS_FILE_FLUSH_THRESHOLD, DEFAULT_SINKS_KAFKA_BATCH_SIZE, DEFAULT_SINKS_KAFKA_BATCH_TIMEOUT_MS,
|
||||
DEFAULT_SINKS_KAFKA_BROKERS, DEFAULT_SINKS_KAFKA_TOPIC, DEFAULT_SINKS_WEBHOOK_AUTH_TOKEN, DEFAULT_SINKS_WEBHOOK_ENDPOINT,
|
||||
DEFAULT_SINKS_WEBHOOK_MAX_RETRIES, DEFAULT_SINKS_WEBHOOK_RETRY_DELAY_MS, ENV_AUDIT_LOGGER_QUEUE_CAPACITY, ENV_OBS_ENDPOINT,
|
||||
ENV_OBS_ENVIRONMENT, ENV_OBS_LOCAL_LOGGING_ENABLED, ENV_OBS_LOG_FILENAME, ENV_OBS_LOG_KEEP_FILES,
|
||||
ENV_OBS_LOG_ROTATION_SIZE_MB, ENV_OBS_LOG_ROTATION_TIME, ENV_OBS_LOGGER_LEVEL, ENV_OBS_METER_INTERVAL, ENV_OBS_SAMPLE_RATIO,
|
||||
ENV_OBS_SERVICE_NAME, ENV_OBS_SERVICE_VERSION, ENV_SINKS_FILE_BUFFER_SIZE, ENV_SINKS_FILE_FLUSH_INTERVAL_MS,
|
||||
ENV_SINKS_FILE_FLUSH_THRESHOLD, ENV_SINKS_FILE_PATH, ENV_SINKS_KAFKA_BATCH_SIZE, ENV_SINKS_KAFKA_BATCH_TIMEOUT_MS,
|
||||
ENV_SINKS_KAFKA_BROKERS, ENV_SINKS_KAFKA_TOPIC, ENV_SINKS_WEBHOOK_AUTH_TOKEN, ENV_SINKS_WEBHOOK_ENDPOINT,
|
||||
ENV_SINKS_WEBHOOK_MAX_RETRIES, ENV_SINKS_WEBHOOK_RETRY_DELAY_MS,
|
||||
};
|
||||
use rustfs_config::observability::{ENV_OBS_LOG_DIRECTORY, ENV_OBS_USE_STDOUT};
|
||||
use rustfs_config::{
|
||||
APP_NAME, DEFAULT_LOG_KEEP_FILES, DEFAULT_LOG_LEVEL, DEFAULT_LOG_ROTATION_SIZE_MB, DEFAULT_LOG_ROTATION_TIME,
|
||||
DEFAULT_OBS_LOG_FILENAME, ENVIRONMENT, METER_INTERVAL, SAMPLE_RATIO, SERVICE_VERSION, USE_STDOUT,
|
||||
};
|
||||
use rustfs_utils::dirs::get_log_directory_to_string;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::env;
|
||||
|
||||
@@ -52,14 +65,14 @@ impl OtelConfig {
|
||||
pub fn extract_otel_config_from_env(endpoint: Option<String>) -> OtelConfig {
|
||||
let endpoint = if let Some(endpoint) = endpoint {
|
||||
if endpoint.is_empty() {
|
||||
env::var("RUSTFS_OBS_ENDPOINT").unwrap_or_else(|_| "".to_string())
|
||||
env::var(ENV_OBS_ENDPOINT).unwrap_or_else(|_| "".to_string())
|
||||
} else {
|
||||
endpoint
|
||||
}
|
||||
} else {
|
||||
env::var("RUSTFS_OBS_ENDPOINT").unwrap_or_else(|_| "".to_string())
|
||||
env::var(ENV_OBS_ENDPOINT).unwrap_or_else(|_| "".to_string())
|
||||
};
|
||||
let mut use_stdout = env::var("RUSTFS_OBS_USE_STDOUT")
|
||||
let mut use_stdout = env::var(ENV_OBS_USE_STDOUT)
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(USE_STDOUT));
|
||||
@@ -70,51 +83,48 @@ impl OtelConfig {
|
||||
OtelConfig {
|
||||
endpoint,
|
||||
use_stdout,
|
||||
sample_ratio: env::var("RUSTFS_OBS_SAMPLE_RATIO")
|
||||
sample_ratio: env::var(ENV_OBS_SAMPLE_RATIO)
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(SAMPLE_RATIO)),
|
||||
meter_interval: env::var("RUSTFS_OBS_METER_INTERVAL")
|
||||
meter_interval: env::var(ENV_OBS_METER_INTERVAL)
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(METER_INTERVAL)),
|
||||
service_name: env::var("RUSTFS_OBS_SERVICE_NAME")
|
||||
service_name: env::var(ENV_OBS_SERVICE_NAME)
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(APP_NAME.to_string())),
|
||||
service_version: env::var("RUSTFS_OBS_SERVICE_VERSION")
|
||||
service_version: env::var(ENV_OBS_SERVICE_VERSION)
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(SERVICE_VERSION.to_string())),
|
||||
environment: env::var("RUSTFS_OBS_ENVIRONMENT")
|
||||
environment: env::var(ENV_OBS_ENVIRONMENT)
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(ENVIRONMENT.to_string())),
|
||||
logger_level: env::var("RUSTFS_OBS_LOGGER_LEVEL")
|
||||
logger_level: env::var(ENV_OBS_LOGGER_LEVEL)
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(DEFAULT_LOG_LEVEL.to_string())),
|
||||
local_logging_enabled: env::var("RUSTFS_OBS_LOCAL_LOGGING_ENABLED")
|
||||
local_logging_enabled: env::var(ENV_OBS_LOCAL_LOGGING_ENABLED)
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(false)),
|
||||
log_directory: env::var("RUSTFS_OBS_LOG_DIRECTORY")
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(DEFAULT_LOG_DIR.to_string())),
|
||||
log_filename: env::var("RUSTFS_OBS_LOG_FILENAME")
|
||||
log_directory: Some(get_log_directory_to_string(ENV_OBS_LOG_DIRECTORY)),
|
||||
log_filename: env::var(ENV_OBS_LOG_FILENAME)
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(DEFAULT_OBS_LOG_FILENAME.to_string())),
|
||||
log_rotation_size_mb: env::var("RUSTFS_OBS_LOG_ROTATION_SIZE_MB")
|
||||
log_rotation_size_mb: env::var(ENV_OBS_LOG_ROTATION_SIZE_MB)
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(DEFAULT_LOG_ROTATION_SIZE_MB)), // Default to 100 MB
|
||||
log_rotation_time: env::var("RUSTFS_OBS_LOG_ROTATION_TIME")
|
||||
log_rotation_time: env::var(ENV_OBS_LOG_ROTATION_TIME)
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(DEFAULT_LOG_ROTATION_TIME.to_string())), // Default to "Day"
|
||||
log_keep_files: env::var("RUSTFS_OBS_LOG_KEEP_FILES")
|
||||
log_keep_files: env::var(ENV_OBS_LOG_KEEP_FILES)
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(DEFAULT_LOG_KEEP_FILES)), // Default to keeping 30 log files
|
||||
@@ -154,16 +164,22 @@ impl KafkaSinkConfig {
|
||||
impl Default for KafkaSinkConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
brokers: env::var("RUSTFS_SINKS_KAFKA_BROKERS")
|
||||
brokers: env::var(ENV_SINKS_KAFKA_BROKERS)
|
||||
.ok()
|
||||
.filter(|s| !s.trim().is_empty())
|
||||
.unwrap_or_else(|| "localhost:9092".to_string()),
|
||||
topic: env::var("RUSTFS_SINKS_KAFKA_TOPIC")
|
||||
.unwrap_or_else(|| DEFAULT_SINKS_KAFKA_BROKERS.to_string()),
|
||||
topic: env::var(ENV_SINKS_KAFKA_TOPIC)
|
||||
.ok()
|
||||
.filter(|s| !s.trim().is_empty())
|
||||
.unwrap_or_else(|| "rustfs_sink".to_string()),
|
||||
batch_size: Some(100),
|
||||
batch_timeout_ms: Some(1000),
|
||||
.unwrap_or_else(|| DEFAULT_SINKS_KAFKA_TOPIC.to_string()),
|
||||
batch_size: env::var(ENV_SINKS_KAFKA_BATCH_SIZE)
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(DEFAULT_SINKS_KAFKA_BATCH_SIZE)),
|
||||
batch_timeout_ms: env::var(ENV_SINKS_KAFKA_BATCH_TIMEOUT_MS)
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(DEFAULT_SINKS_KAFKA_BATCH_TIMEOUT_MS)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -186,16 +202,22 @@ impl WebhookSinkConfig {
|
||||
impl Default for WebhookSinkConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
endpoint: env::var("RUSTFS_SINKS_WEBHOOK_ENDPOINT")
|
||||
endpoint: env::var(ENV_SINKS_WEBHOOK_ENDPOINT)
|
||||
.ok()
|
||||
.filter(|s| !s.trim().is_empty())
|
||||
.unwrap_or_else(|| "http://localhost:8080".to_string()),
|
||||
auth_token: env::var("RUSTFS_SINKS_WEBHOOK_AUTH_TOKEN")
|
||||
.unwrap_or_else(|| DEFAULT_SINKS_WEBHOOK_ENDPOINT.to_string()),
|
||||
auth_token: env::var(ENV_SINKS_WEBHOOK_AUTH_TOKEN)
|
||||
.ok()
|
||||
.filter(|s| !s.trim().is_empty())
|
||||
.unwrap_or_else(|| "rustfs_webhook_token".to_string()),
|
||||
max_retries: Some(3),
|
||||
retry_delay_ms: Some(100),
|
||||
.unwrap_or_else(|| DEFAULT_SINKS_WEBHOOK_AUTH_TOKEN.to_string()),
|
||||
max_retries: env::var(ENV_SINKS_WEBHOOK_MAX_RETRIES)
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(DEFAULT_SINKS_WEBHOOK_MAX_RETRIES)),
|
||||
retry_delay_ms: env::var(ENV_SINKS_WEBHOOK_RETRY_DELAY_MS)
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(DEFAULT_SINKS_WEBHOOK_RETRY_DELAY_MS)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -210,18 +232,6 @@ pub struct FileSinkConfig {
|
||||
}
|
||||
|
||||
impl FileSinkConfig {
|
||||
pub fn get_default_log_path() -> String {
|
||||
let temp_dir = env::temp_dir().join(DEFAULT_LOG_FILENAME);
|
||||
if let Err(e) = std::fs::create_dir_all(&temp_dir) {
|
||||
eprintln!("Failed to create log directory: {e}");
|
||||
return DEFAULT_LOG_DIR.to_string();
|
||||
}
|
||||
temp_dir
|
||||
.join(DEFAULT_SINK_FILE_LOG_FILE)
|
||||
.to_str()
|
||||
.unwrap_or(DEFAULT_LOG_DIR)
|
||||
.to_string()
|
||||
}
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
@@ -230,22 +240,19 @@ impl FileSinkConfig {
|
||||
impl Default for FileSinkConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
path: env::var("RUSTFS_SINKS_FILE_PATH")
|
||||
.ok()
|
||||
.filter(|s| !s.trim().is_empty())
|
||||
.unwrap_or_else(Self::get_default_log_path),
|
||||
buffer_size: env::var("RUSTFS_SINKS_FILE_BUFFER_SIZE")
|
||||
path: get_log_directory_to_string(ENV_SINKS_FILE_PATH),
|
||||
buffer_size: env::var(ENV_SINKS_FILE_BUFFER_SIZE)
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(8192)),
|
||||
flush_interval_ms: env::var("RUSTFS_SINKS_FILE_FLUSH_INTERVAL_MS")
|
||||
.or(Some(DEFAULT_SINKS_FILE_BUFFER_SIZE)),
|
||||
flush_interval_ms: env::var(ENV_SINKS_FILE_FLUSH_INTERVAL_MS)
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(1000)),
|
||||
flush_threshold: env::var("RUSTFS_SINKS_FILE_FLUSH_THRESHOLD")
|
||||
.or(Some(DEFAULT_SINKS_FILE_FLUSH_INTERVAL_MS)),
|
||||
flush_threshold: env::var(ENV_SINKS_FILE_FLUSH_THRESHOLD)
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(100)),
|
||||
.or(Some(DEFAULT_SINKS_FILE_FLUSH_THRESHOLD)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -280,7 +287,10 @@ pub struct LoggerConfig {
|
||||
impl LoggerConfig {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
queue_capacity: Some(10000),
|
||||
queue_capacity: env::var(ENV_AUDIT_LOGGER_QUEUE_CAPACITY)
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.or(Some(DEFAULT_AUDIT_LOGGER_QUEUE_CAPACITY)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ impl FileSink {
|
||||
}
|
||||
let file = if file_exists {
|
||||
// If the file exists, open it in append mode
|
||||
tracing::debug!("FileSink: File exists, opening in append mode.");
|
||||
tracing::debug!("FileSink: File exists, opening in append mode. Path: {:?}", path);
|
||||
OpenOptions::new().append(true).create(true).open(&path).await?
|
||||
} else {
|
||||
// If the file does not exist, create it
|
||||
|
||||
+23
-10
@@ -14,7 +14,6 @@
|
||||
|
||||
use crate::{AppConfig, SinkConfig, UnifiedLogEntry};
|
||||
use async_trait::async_trait;
|
||||
use rustfs_config::DEFAULT_SINK_FILE_LOG_FILE;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[cfg(feature = "file")]
|
||||
@@ -47,8 +46,12 @@ pub async fn create_sinks(config: &AppConfig) -> Vec<Arc<dyn Sink>> {
|
||||
sinks.push(Arc::new(kafka::KafkaSink::new(
|
||||
producer,
|
||||
kafka_config.topic.clone(),
|
||||
kafka_config.batch_size.unwrap_or(100),
|
||||
kafka_config.batch_timeout_ms.unwrap_or(1000),
|
||||
kafka_config
|
||||
.batch_size
|
||||
.unwrap_or(rustfs_config::observability::DEFAULT_SINKS_KAFKA_BATCH_SIZE),
|
||||
kafka_config
|
||||
.batch_timeout_ms
|
||||
.unwrap_or(rustfs_config::observability::DEFAULT_SINKS_KAFKA_BATCH_TIMEOUT_MS),
|
||||
)));
|
||||
tracing::info!("Kafka sink created for topic: {}", kafka_config.topic);
|
||||
}
|
||||
@@ -57,25 +60,35 @@ pub async fn create_sinks(config: &AppConfig) -> Vec<Arc<dyn Sink>> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "webhook")]
|
||||
SinkConfig::Webhook(webhook_config) => {
|
||||
sinks.push(Arc::new(webhook::WebhookSink::new(
|
||||
webhook_config.endpoint.clone(),
|
||||
webhook_config.auth_token.clone(),
|
||||
webhook_config.max_retries.unwrap_or(3),
|
||||
webhook_config.retry_delay_ms.unwrap_or(100),
|
||||
webhook_config
|
||||
.max_retries
|
||||
.unwrap_or(rustfs_config::observability::DEFAULT_SINKS_WEBHOOK_MAX_RETRIES),
|
||||
webhook_config
|
||||
.retry_delay_ms
|
||||
.unwrap_or(rustfs_config::observability::DEFAULT_SINKS_WEBHOOK_RETRY_DELAY_MS),
|
||||
)));
|
||||
tracing::info!("Webhook sink created for endpoint: {}", webhook_config.endpoint);
|
||||
}
|
||||
|
||||
#[cfg(feature = "file")]
|
||||
SinkConfig::File(file_config) => {
|
||||
tracing::debug!("FileSink: Using path: {}", file_config.path);
|
||||
match file::FileSink::new(
|
||||
format!("{}/{}", file_config.path.clone(), DEFAULT_SINK_FILE_LOG_FILE),
|
||||
file_config.buffer_size.unwrap_or(8192),
|
||||
file_config.flush_interval_ms.unwrap_or(1000),
|
||||
file_config.flush_threshold.unwrap_or(100),
|
||||
format!("{}/{}", file_config.path.clone(), rustfs_config::DEFAULT_SINK_FILE_LOG_FILE),
|
||||
file_config
|
||||
.buffer_size
|
||||
.unwrap_or(rustfs_config::observability::DEFAULT_SINKS_FILE_BUFFER_SIZE),
|
||||
file_config
|
||||
.flush_interval_ms
|
||||
.unwrap_or(rustfs_config::observability::DEFAULT_SINKS_FILE_FLUSH_INTERVAL_MS),
|
||||
file_config
|
||||
.flush_threshold
|
||||
.unwrap_or(rustfs_config::observability::DEFAULT_SINKS_FILE_FLUSH_THRESHOLD),
|
||||
)
|
||||
.await
|
||||
{
|
||||
|
||||
@@ -29,9 +29,9 @@ use opentelemetry_semantic_conventions::{
|
||||
SCHEMA_URL,
|
||||
attribute::{DEPLOYMENT_ENVIRONMENT_NAME, NETWORK_LOCAL_ADDRESS, SERVICE_VERSION as OTEL_SERVICE_VERSION},
|
||||
};
|
||||
use rustfs_config::observability::ENV_OBS_LOG_DIRECTORY;
|
||||
use rustfs_config::{
|
||||
APP_NAME, DEFAULT_LOG_DIR, DEFAULT_LOG_KEEP_FILES, DEFAULT_LOG_LEVEL, ENVIRONMENT, METER_INTERVAL, SAMPLE_RATIO,
|
||||
SERVICE_VERSION, USE_STDOUT,
|
||||
APP_NAME, DEFAULT_LOG_KEEP_FILES, DEFAULT_LOG_LEVEL, ENVIRONMENT, METER_INTERVAL, SAMPLE_RATIO, SERVICE_VERSION, USE_STDOUT,
|
||||
};
|
||||
use rustfs_utils::get_local_ip_with_default;
|
||||
use smallvec::SmallVec;
|
||||
@@ -293,7 +293,8 @@ pub(crate) fn init_telemetry(config: &OtelConfig) -> OtelGuard {
|
||||
}
|
||||
} else {
|
||||
// Obtain the log directory and file name configuration
|
||||
let log_directory = config.log_directory.as_deref().unwrap_or(DEFAULT_LOG_DIR);
|
||||
let default_log_directory = rustfs_utils::dirs::get_log_directory_to_string(ENV_OBS_LOG_DIRECTORY);
|
||||
let log_directory = config.log_directory.as_deref().unwrap_or(default_log_directory.as_str());
|
||||
let log_filename = config.log_filename.as_deref().unwrap_or(service_name);
|
||||
|
||||
if let Err(e) = fs::create_dir_all(log_directory) {
|
||||
|
||||
@@ -37,4 +37,5 @@ rustfs-common.workspace = true
|
||||
flatbuffers = { workspace = true }
|
||||
prost = { workspace = true }
|
||||
tonic = { workspace = true, features = ["transport"] }
|
||||
tonic-build = { workspace = true }
|
||||
tonic-prost = { workspace = true }
|
||||
tonic-prost-build = { workspace = true }
|
||||
@@ -1,3 +1,17 @@
|
||||
// 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.
|
||||
|
||||
#![allow(unused_imports)]
|
||||
#![allow(clippy::all)]
|
||||
pub mod proto_gen;
|
||||
|
||||
@@ -1 +1,15 @@
|
||||
// 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.
|
||||
|
||||
pub mod node_service;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -53,21 +53,19 @@ fn main() -> Result<(), AnyError> {
|
||||
let flatbuffer_out_dir = project_root_dir.join("generated").join("flatbuffers_generated");
|
||||
// let descriptor_set_path = PathBuf::from(env::var(ENV_OUT_DIR).unwrap()).join("proto-descriptor.bin");
|
||||
|
||||
tonic_build::configure()
|
||||
tonic_prost_build::configure()
|
||||
.out_dir(proto_out_dir)
|
||||
// .file_descriptor_set_path(descriptor_set_path)
|
||||
.protoc_arg("--experimental_allow_proto3_optional")
|
||||
.compile_well_known_types(true)
|
||||
.bytes(["."])
|
||||
.bytes(".")
|
||||
.emit_rerun_if_changed(false)
|
||||
.compile_protos(proto_files, &[proto_dir.clone()])
|
||||
.compile_protos(proto_files, &[proto_dir.to_string_lossy().as_ref()])
|
||||
.map_err(|e| format!("Failed to generate protobuf file: {e}."))?;
|
||||
|
||||
// protos/gen/mod.rs
|
||||
let generated_mod_rs_path = project_root_dir.join("generated").join("proto_gen").join("mod.rs");
|
||||
|
||||
let mut generated_mod_rs = fs::File::create(generated_mod_rs_path)?;
|
||||
writeln!(&mut generated_mod_rs, "pub mod node_service;")?;
|
||||
writeln!(
|
||||
&mut generated_mod_rs,
|
||||
r#"// Copyright 2024 RustFS Team
|
||||
@@ -84,12 +82,13 @@ fn main() -> Result<(), AnyError> {
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License."#
|
||||
)?;
|
||||
writeln!(&mut generated_mod_rs, "\n")?;
|
||||
writeln!(&mut generated_mod_rs, "pub mod node_service;")?;
|
||||
generated_mod_rs.flush()?;
|
||||
|
||||
let generated_mod_rs_path = project_root_dir.join("generated").join("mod.rs");
|
||||
|
||||
let mut generated_mod_rs = fs::File::create(generated_mod_rs_path)?;
|
||||
writeln!(&mut generated_mod_rs, "#![allow(unused_imports)]")?;
|
||||
|
||||
writeln!(
|
||||
&mut generated_mod_rs,
|
||||
r#"// Copyright 2024 RustFS Team
|
||||
@@ -106,6 +105,9 @@ fn main() -> Result<(), AnyError> {
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License."#
|
||||
)?;
|
||||
writeln!(&mut generated_mod_rs, "\n")?;
|
||||
writeln!(&mut generated_mod_rs, "#![allow(unused_imports)]")?;
|
||||
writeln!(&mut generated_mod_rs, "\n")?;
|
||||
writeln!(&mut generated_mod_rs, "#![allow(clippy::all)]")?;
|
||||
writeln!(&mut generated_mod_rs, "pub mod proto_gen;")?;
|
||||
generated_mod_rs.flush()?;
|
||||
|
||||
@@ -12,7 +12,9 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use rustfs_config::{DEFAULT_LOG_DIR, DEFAULT_LOG_FILENAME};
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// Get the absolute path to the current project
|
||||
@@ -57,6 +59,72 @@ pub fn get_project_root() -> Result<PathBuf, String> {
|
||||
Err("The project root directory cannot be obtained. Please check the running environment and project structure.".to_string())
|
||||
}
|
||||
|
||||
/// Get the log directory as a string
|
||||
/// This function will try to find a writable log directory in the following order:
|
||||
pub fn get_log_directory_to_string(key: &str) -> String {
|
||||
get_log_directory(key).to_string_lossy().to_string()
|
||||
}
|
||||
|
||||
/// Get the log directory
|
||||
/// This function will try to find a writable log directory in the following order:
|
||||
pub fn get_log_directory(key: &str) -> PathBuf {
|
||||
// Environment variables are specified
|
||||
if let Ok(log_dir) = env::var(key) {
|
||||
let path = PathBuf::from(log_dir);
|
||||
if ensure_directory_writable(&path) {
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
// System temporary directory
|
||||
if let Ok(mut temp_dir) = env::temp_dir().canonicalize() {
|
||||
temp_dir.push(DEFAULT_LOG_FILENAME);
|
||||
temp_dir.push(DEFAULT_LOG_DIR);
|
||||
if ensure_directory_writable(&temp_dir) {
|
||||
return temp_dir;
|
||||
}
|
||||
}
|
||||
|
||||
// User home directory
|
||||
if let Ok(home_dir) = env::var("HOME").or_else(|_| env::var("USERPROFILE")) {
|
||||
let mut path = PathBuf::from(home_dir);
|
||||
path.push(format!(".{DEFAULT_LOG_FILENAME}"));
|
||||
path.push(DEFAULT_LOG_DIR);
|
||||
if ensure_directory_writable(&path) {
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
// Current working directory
|
||||
if let Ok(current_dir) = env::current_dir() {
|
||||
let mut path = current_dir;
|
||||
path.push(DEFAULT_LOG_DIR);
|
||||
if ensure_directory_writable(&path) {
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
// Relative path
|
||||
PathBuf::from(DEFAULT_LOG_DIR)
|
||||
}
|
||||
|
||||
fn ensure_directory_writable(path: &PathBuf) -> bool {
|
||||
// Try creating a catalog
|
||||
if fs::create_dir_all(path).is_err() {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check write permissions
|
||||
let test_file = path.join(".write_test");
|
||||
match fs::write(&test_file, "test") {
|
||||
Ok(_) => {
|
||||
let _ = fs::remove_file(&test_file);
|
||||
true
|
||||
}
|
||||
Err(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
+97
-41
@@ -12,44 +12,39 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use std::time::Duration;
|
||||
use futures::Stream;
|
||||
use hyper::http;
|
||||
use std::{
|
||||
pin::Pin,
|
||||
sync::LazyLock,
|
||||
task::{Context, Poll},
|
||||
time::Duration,
|
||||
};
|
||||
use tokio::time::interval;
|
||||
|
||||
pub const MAX_RETRY: i64 = 10;
|
||||
pub const MAX_JITTER: f64 = 1.0;
|
||||
pub const NO_JITTER: f64 = 0.0;
|
||||
|
||||
/*
|
||||
struct Delay {
|
||||
when: Instant,
|
||||
}
|
||||
pub const DEFAULT_RETRY_UNIT: Duration = Duration::from_millis(200);
|
||||
pub const DEFAULT_RETRY_CAP: Duration = Duration::from_secs(1);
|
||||
|
||||
impl Future for Delay {
|
||||
type Output = &'static str;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>)
|
||||
-> Poll<&'static str>
|
||||
{
|
||||
if Instant::now() >= self.when {
|
||||
println!("Hello world");
|
||||
Poll::Ready("done")
|
||||
} else {
|
||||
// Ignore this line for now.
|
||||
cx.waker().wake_by_ref();
|
||||
Poll::Pending
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct RetryTimer {
|
||||
rem: usize,
|
||||
delay: Delay,
|
||||
pub struct RetryTimer {
|
||||
base_sleep: Duration,
|
||||
max_sleep: Duration,
|
||||
jitter: f64,
|
||||
random: u64,
|
||||
rem: i64,
|
||||
}
|
||||
|
||||
impl RetryTimer {
|
||||
fn new() -> Self {
|
||||
pub fn new(max_retry: i64, base_sleep: Duration, max_sleep: Duration, jitter: f64, random: u64) -> Self {
|
||||
Self {
|
||||
rem: 3,
|
||||
delay: Delay { when: Instant::now() }
|
||||
base_sleep,
|
||||
max_sleep,
|
||||
jitter,
|
||||
random,
|
||||
rem: max_retry,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -57,26 +52,87 @@ impl RetryTimer {
|
||||
impl Stream for RetryTimer {
|
||||
type Item = ();
|
||||
|
||||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>)
|
||||
-> Poll<Option<()>>
|
||||
{
|
||||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<()>> {
|
||||
let jitter = self.jitter.clamp(NO_JITTER, MAX_JITTER);
|
||||
|
||||
let attempt = MAX_RETRY - self.rem;
|
||||
let mut sleep = self.base_sleep * (1 << attempt);
|
||||
if sleep > self.max_sleep {
|
||||
sleep = self.max_sleep;
|
||||
}
|
||||
if (jitter - NO_JITTER).abs() > 1e-9 {
|
||||
sleep -= sleep * self.random as u32 * jitter as u32;
|
||||
}
|
||||
|
||||
if self.rem == 0 {
|
||||
// No more delays
|
||||
return Poll::Ready(None);
|
||||
}
|
||||
|
||||
match Pin::new(&mut self.delay).poll(cx) {
|
||||
Poll::Ready(_) => {
|
||||
let when = self.delay.when + Duration::from_millis(10);
|
||||
self.delay = Delay { when };
|
||||
self.rem -= 1;
|
||||
Poll::Ready(Some(()))
|
||||
}
|
||||
self.rem -= 1;
|
||||
let mut t = interval(sleep);
|
||||
match t.poll_tick(cx) {
|
||||
Poll::Ready(_) => Poll::Ready(Some(())),
|
||||
Poll::Pending => Poll::Pending,
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
pub fn new_retry_timer(_max_retry: i32, _base_sleep: Duration, _max_sleep: Duration, _jitter: f64) -> Vec<i32> {
|
||||
static RETRYABLE_S3CODES: LazyLock<Vec<String>> = LazyLock::new(|| {
|
||||
vec![
|
||||
"RequestError".to_string(),
|
||||
"RequestTimeout".to_string(),
|
||||
"Throttling".to_string(),
|
||||
"ThrottlingException".to_string(),
|
||||
"RequestLimitExceeded".to_string(),
|
||||
"RequestThrottled".to_string(),
|
||||
"InternalError".to_string(),
|
||||
"ExpiredToken".to_string(),
|
||||
"ExpiredTokenException".to_string(),
|
||||
"SlowDown".to_string(),
|
||||
]
|
||||
});
|
||||
|
||||
static RETRYABLE_HTTP_STATUSCODES: LazyLock<Vec<http::StatusCode>> = LazyLock::new(|| {
|
||||
vec![
|
||||
http::StatusCode::REQUEST_TIMEOUT,
|
||||
http::StatusCode::TOO_MANY_REQUESTS,
|
||||
//499,
|
||||
http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
http::StatusCode::BAD_GATEWAY,
|
||||
http::StatusCode::SERVICE_UNAVAILABLE,
|
||||
http::StatusCode::GATEWAY_TIMEOUT,
|
||||
//520,
|
||||
]
|
||||
});
|
||||
|
||||
pub fn is_s3code_retryable(s3code: &str) -> bool {
|
||||
RETRYABLE_S3CODES.contains(&s3code.to_string())
|
||||
}
|
||||
|
||||
pub fn is_http_status_retryable(http_statuscode: &http::StatusCode) -> bool {
|
||||
RETRYABLE_HTTP_STATUSCODES.contains(http_statuscode)
|
||||
}
|
||||
|
||||
pub fn is_request_error_retryable(_err: std::io::Error) -> bool {
|
||||
/*if err == Err::Canceled || err == Err::DeadlineExceeded {
|
||||
return err() == nil;
|
||||
}
|
||||
let uerr = err.(*url.Error);
|
||||
if uerr.is_ok() {
|
||||
let e = uerr.unwrap();
|
||||
return match e.type {
|
||||
x509.UnknownAuthorityError => {
|
||||
false
|
||||
}
|
||||
_ => true,
|
||||
};
|
||||
return match e.error() {
|
||||
"http: server gave HTTP response to HTTPS client" => {
|
||||
false
|
||||
}
|
||||
_ => rue,
|
||||
};
|
||||
}
|
||||
true*/
|
||||
todo!();
|
||||
}
|
||||
|
||||
@@ -88,7 +88,7 @@ impl UserAgent {
|
||||
Some(version) => version,
|
||||
None => "Windows NT Unknown".to_string(),
|
||||
};
|
||||
format!("Windows NT {}", version)
|
||||
format!("Windows NT {version}")
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
|
||||
@@ -16,7 +16,7 @@ use crate::admin::router::Operation;
|
||||
use crate::auth::{check_key_valid, get_session_token};
|
||||
use http::{HeaderMap, StatusCode};
|
||||
use matchit::Params;
|
||||
use rustfs_config::notify::{NOTIFY_MQTT_SUB_SYS, NOTIFY_WEBHOOK_SUB_SYS};
|
||||
use rustfs_config::notify::{ENABLE_KEY, ENABLE_ON, NOTIFY_MQTT_SUB_SYS, NOTIFY_WEBHOOK_SUB_SYS};
|
||||
use rustfs_notify::EventName;
|
||||
use rustfs_notify::rules::{BucketNotificationConfig, PatternRules};
|
||||
use s3s::header::CONTENT_LENGTH;
|
||||
@@ -75,11 +75,8 @@ impl Operation for SetNotificationTarget {
|
||||
let mut kvs_map: HashMap<String, String> = serde_json::from_slice(&body)
|
||||
.map_err(|e| s3_error!(InvalidArgument, "invalid json body for target config: {}", e))?;
|
||||
// If there is an enable key, add an enable key value to "on"
|
||||
if !kvs_map.contains_key(rustfs_ecstore::config::ENABLE_KEY) {
|
||||
kvs_map.insert(
|
||||
rustfs_ecstore::config::ENABLE_KEY.to_string(),
|
||||
rustfs_ecstore::config::ENABLE_ON.to_string(),
|
||||
);
|
||||
if !kvs_map.contains_key(ENABLE_KEY) {
|
||||
kvs_map.insert(ENABLE_KEY.to_string(), ENABLE_ON.to_string());
|
||||
}
|
||||
|
||||
let kvs = rustfs_ecstore::config::KVS(
|
||||
|
||||
@@ -465,182 +465,3 @@ impl Operation for ClearTier {
|
||||
Ok(S3Response::with_headers((StatusCode::OK, Body::empty()), header))
|
||||
}
|
||||
}
|
||||
|
||||
/*pub struct PostRestoreObject {}
|
||||
#[async_trait::async_trait]
|
||||
impl Operation for PostRestoreObject {
|
||||
async fn call(&self, req: S3Request<Body>, params: Params<'_, '_>) -> S3Result<S3Response<(StatusCode, Body)>> {
|
||||
let query = {
|
||||
if let Some(query) = req.uri.query() {
|
||||
let input: PostRestoreObject =
|
||||
from_bytes(query.as_bytes()).map_err(|_e| s3_error!(InvalidArgument, "get query failed"))?;
|
||||
input
|
||||
} else {
|
||||
PostRestoreObject::default()
|
||||
}
|
||||
};
|
||||
|
||||
let bucket = params.bucket;
|
||||
if let Err(e) = un_escape_path(params.object) {
|
||||
warn!("post restore object failed, e: {:?}", e);
|
||||
return Err(S3Error::with_message(S3ErrorCode::Custom("PostRestoreObjectFailed".into()), "post restore object failed"));
|
||||
}
|
||||
|
||||
let Some(store) = new_object_layer_fn() else {
|
||||
return Err(S3Error::with_message(S3ErrorCode::InternalError, "Not init".to_string()));
|
||||
};
|
||||
|
||||
let get_object_info = store.get_object_info();
|
||||
|
||||
if Err(err) = check_request_auth_type(req, policy::RestoreObjectAction, bucket, object) {
|
||||
return Err(S3Error::with_message(S3ErrorCode::Custom("PostRestoreObjectFailed".into()), "post restore object failed"));
|
||||
}
|
||||
|
||||
if req.content_length <= 0 {
|
||||
return Err(S3Error::with_message(S3ErrorCode::Custom("ErrEmptyRequestBody".into()), "post restore object failed"));
|
||||
}
|
||||
let Some(opts) = post_restore_opts(req, bucket, object) else {
|
||||
return Err(S3Error::with_message(S3ErrorCode::Custom("ErrEmptyRequestBody".into()), "post restore object failed"));
|
||||
};
|
||||
|
||||
let Some(obj_info) = getObjectInfo(ctx, bucket, object, opts) else {
|
||||
return Err(S3Error::with_message(S3ErrorCode::Custom("ErrEmptyRequestBody".into()), "post restore object failed"));
|
||||
};
|
||||
|
||||
if obj_info.transitioned_object.status != lifecycle::TRANSITION_COMPLETE {
|
||||
return Err(S3Error::with_message(S3ErrorCode::Custom("ErrEmptyRequestBody".into()), "post restore object failed"));
|
||||
}
|
||||
|
||||
let mut api_err;
|
||||
let Some(rreq) = parsere_store_request(req.body(), req.content_length) else {
|
||||
let api_err = errorCodes.ToAPIErr(ErrMalformedXML);
|
||||
api_err.description = err.Error()
|
||||
return Err(S3Error::with_message(S3ErrorCode::Custom("ErrEmptyRequestBody".into()), "post restore object failed"));
|
||||
};
|
||||
let mut status_code = http::StatusCode::OK;
|
||||
let mut already_restored = false;
|
||||
if Err(err) = rreq.validate(store) {
|
||||
api_err = errorCodes.ToAPIErr(ErrMalformedXML)
|
||||
api_err.description = err.Error()
|
||||
return Err(S3Error::with_message(S3ErrorCode::Custom("ErrEmptyRequestBody".into()), "post restore object failed"));
|
||||
} else {
|
||||
if obj_info.restore_ongoing && rreq.Type != "SELECT" {
|
||||
return Err(S3Error::with_message(S3ErrorCode::Custom("ErrObjectRestoreAlreadyInProgress".into()), "post restore object failed"));
|
||||
}
|
||||
if !obj_info.restore_ongoing && !obj_info.restore_expires.unix_timestamp() == 0 {
|
||||
status_code = http::StatusCode::Accepted;
|
||||
already_restored = true;
|
||||
}
|
||||
}
|
||||
let restore_expiry = lifecycle::expected_expiry_time(OffsetDateTime::now_utc(), rreq.days);
|
||||
let mut metadata = clone_mss(obj_info.user_defined);
|
||||
|
||||
if rreq.type != "SELECT" {
|
||||
obj_info.metadataOnly = true;
|
||||
metadata[xhttp.AmzRestoreExpiryDays] = rreq.days;
|
||||
metadata[xhttp.AmzRestoreRequestDate] = OffsetDateTime::now_utc().format(http::TimeFormat);
|
||||
if already_restored {
|
||||
metadata[xhttp.AmzRestore] = completedRestoreObj(restore_expiry).String()
|
||||
} else {
|
||||
metadata[xhttp.AmzRestore] = ongoingRestoreObj().String()
|
||||
}
|
||||
obj_info.user_defined = metadata;
|
||||
if let Err(err) = store.copy_object(bucket, object, bucket, object, obj_info, ObjectOptions {
|
||||
version_id: obj_info.version_id,
|
||||
}, ObjectOptions {
|
||||
version_id: obj_info.version_id,
|
||||
m_time: obj_info.mod_time,
|
||||
}) {
|
||||
return Err(S3Error::with_message(S3ErrorCode::Custom("ErrInvalidObjectState".into()), "post restore object failed"));
|
||||
}
|
||||
if already_restored {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
let restore_object = must_get_uuid();
|
||||
if rreq.output_location.s3.bucket_name != "" {
|
||||
w.Header()[xhttp.AmzRestoreOutputPath] = []string{pathJoin(rreq.OutputLocation.S3.BucketName, rreq.OutputLocation.S3.Prefix, restoreObject)}
|
||||
}
|
||||
w.WriteHeader(status_code)
|
||||
send_event(EventArgs {
|
||||
event_name: event::ObjectRestorePost,
|
||||
bucket_name: bucket,
|
||||
object: obj_info,
|
||||
req_params: extract_req_params(r),
|
||||
user_agent: req.user_agent(),
|
||||
host: handlers::get_source_ip(r),
|
||||
});
|
||||
tokio::spawn(async move {
|
||||
if !rreq.SelectParameters.IsEmpty() {
|
||||
let actual_size = obj_info.get_actual_size();
|
||||
if actual_size.is_err() {
|
||||
return Err(S3Error::with_message(S3ErrorCode::Custom("ErrInvalidObjectState".into()), "post restore object failed"));
|
||||
}
|
||||
|
||||
let object_rsc = s3select.NewObjectReadSeekCloser(
|
||||
|offset int64| -> (io.ReadCloser, error) {
|
||||
rs := &HTTPRangeSpec{
|
||||
IsSuffixLength: false,
|
||||
Start: offset,
|
||||
End: -1,
|
||||
}
|
||||
return getTransitionedObjectReader(bucket, object, rs, r.Header,
|
||||
obj_info, ObjectOptions {version_id: obj_info.version_id});
|
||||
},
|
||||
actual_size.unwrap(),
|
||||
);
|
||||
if err = rreq.SelectParameters.Open(objectRSC); err != nil {
|
||||
if serr, ok := err.(s3select.SelectError); ok {
|
||||
let encoded_error_response = encodeResponse(APIErrorResponse {
|
||||
code: serr.ErrorCode(),
|
||||
message: serr.ErrorMessage(),
|
||||
bucket_name: bucket,
|
||||
key: object,
|
||||
resource: r.URL.Path,
|
||||
request_id: w.Header().Get(xhttp.AmzRequestID),
|
||||
host_id: globalDeploymentID(),
|
||||
});
|
||||
//writeResponse(w, serr.HTTPStatusCode(), encodedErrorResponse, mimeXML)
|
||||
Ok(S3Response::with_headers((StatusCode::OK, Body::empty()), header));
|
||||
} else {
|
||||
return Err(S3Error::with_message(S3ErrorCode::Custom("ErrInvalidObjectState".into()), "post restore object failed"));
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
let nr = httptest.NewRecorder();
|
||||
let rw = xhttp.NewResponseRecorder(nr);
|
||||
rw.log_err_body = true;
|
||||
rw.log_all_body = true;
|
||||
rreq.select_parameters.evaluate(rw);
|
||||
rreq.select_parameters.Close();
|
||||
return Ok(S3Response::with_headers((StatusCode::OK, Body::empty()), header));
|
||||
}
|
||||
let opts = ObjectOptions {
|
||||
transition: TransitionOptions {
|
||||
restore_request: rreq,
|
||||
restore_expiry: restore_expiry,
|
||||
},
|
||||
version_id: objInfo.version_id,
|
||||
}
|
||||
if Err(err) = store.restore_transitioned_object(bucket, object, opts) {
|
||||
format!(format!("unable to restore transitioned bucket/object {}/{}: {}", bucket, object, err.to_string()));
|
||||
return Ok(S3Response::with_headers((StatusCode::OK, Body::empty()), header));
|
||||
}
|
||||
|
||||
send_event(EventArgs {
|
||||
EventName: event.ObjectRestoreCompleted,
|
||||
BucketName: bucket,
|
||||
Object: objInfo,
|
||||
ReqParams: extractReqParams(r),
|
||||
UserAgent: r.UserAgent(),
|
||||
Host: handlers.GetSourceIP(r),
|
||||
});
|
||||
});
|
||||
|
||||
let mut header = HeaderMap::new();
|
||||
header.insert(CONTENT_TYPE, "application/json".parse().unwrap());
|
||||
header.insert(CONTENT_LENGTH, "0".parse().unwrap());
|
||||
Ok(S3Response::with_headers((StatusCode::OK, Body::empty()), header))
|
||||
}
|
||||
}*/
|
||||
|
||||
@@ -25,6 +25,7 @@ use handlers::{
|
||||
sts, tier, user,
|
||||
};
|
||||
|
||||
use crate::admin::handlers::event::{ListNotificationTargets, RemoveNotificationTarget, SetNotificationTarget};
|
||||
use handlers::{GetReplicationMetricsHandler, ListRemoteTargetHandler, RemoveRemoteTargetHandler, SetRemoteTargetHandler};
|
||||
use hyper::Method;
|
||||
use router::{AdminOperation, S3Router};
|
||||
@@ -365,5 +366,28 @@ fn register_user_route(r: &mut S3Router<AdminOperation>) -> std::io::Result<()>
|
||||
AdminOperation(&policies::SetPolicyForUserOrGroup {}),
|
||||
)?;
|
||||
|
||||
r.insert(
|
||||
Method::GET,
|
||||
format!("{}{}", ADMIN_PREFIX, "/v3/target-list").as_str(),
|
||||
AdminOperation(&ListNotificationTargets {}),
|
||||
)?;
|
||||
|
||||
r.insert(
|
||||
Method::POST,
|
||||
format!("{}{}", ADMIN_PREFIX, "/v3/target-set").as_str(),
|
||||
AdminOperation(&SetNotificationTarget {}),
|
||||
)?;
|
||||
|
||||
// Remove notification target
|
||||
// This endpoint removes a notification target based on its type and name.
|
||||
// target-remove?target_type=xxx&target_name=xxx
|
||||
// * `target_type` - Target type, such as "notify_webhook" or "notify_mqtt".
|
||||
// * `target_name` - A unique name for a Target, such as "1".
|
||||
r.insert(
|
||||
Method::DELETE,
|
||||
format!("{}{}", ADMIN_PREFIX, "/v3/target-remove").as_str(),
|
||||
AdminOperation(&RemoveNotificationTarget {}),
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ pub struct Opt {
|
||||
pub secret_key: String,
|
||||
|
||||
/// Enable console server
|
||||
#[arg(long, default_value_t = true, env = "RUSTFS_CONSOLE_ENABLE")]
|
||||
#[arg(long, default_value_t = rustfs_config::DEFAULT_CONSOLE_ENABLE, env = "RUSTFS_CONSOLE_ENABLE")]
|
||||
pub console_enable: bool,
|
||||
|
||||
/// Observability endpoint for trace, metrics and logs,only support grpc mode.
|
||||
|
||||
+4
-4
@@ -37,8 +37,8 @@ use rustfs_config::DEFAULT_DELIMITER;
|
||||
use rustfs_ecstore::bucket::metadata_sys::init_bucket_metadata_sys;
|
||||
use rustfs_ecstore::cmd::bucket_replication::init_bucket_replication_pool;
|
||||
use rustfs_ecstore::config as ecconfig;
|
||||
use rustfs_ecstore::config::GLOBAL_ConfigSys;
|
||||
use rustfs_ecstore::config::GLOBAL_ServerConfig;
|
||||
use rustfs_ecstore::config::GLOBAL_CONFIG_SYS;
|
||||
use rustfs_ecstore::config::GLOBAL_SERVER_CONFIG;
|
||||
use rustfs_ecstore::store_api::BucketOptions;
|
||||
use rustfs_ecstore::{
|
||||
StorageAPI,
|
||||
@@ -159,7 +159,7 @@ async fn run(opt: config::Opt) -> Result<()> {
|
||||
|
||||
ecconfig::init();
|
||||
// config system configuration
|
||||
GLOBAL_ConfigSys.init(store.clone()).await?;
|
||||
GLOBAL_CONFIG_SYS.init(store.clone()).await?;
|
||||
|
||||
// Initialize event notifier
|
||||
init_event_notifier().await;
|
||||
@@ -281,7 +281,7 @@ pub(crate) async fn init_event_notifier() {
|
||||
info!("Initializing event notifier...");
|
||||
|
||||
// 1. Get the global configuration loaded by ecstore
|
||||
let server_config = match GLOBAL_ServerConfig.get() {
|
||||
let server_config = match GLOBAL_SERVER_CONFIG.get() {
|
||||
Some(config) => config.clone(), // Clone the config to pass ownership
|
||||
None => {
|
||||
error!("Event notifier initialization failed: Global server config not loaded.");
|
||||
|
||||
+167
-19
@@ -486,6 +486,167 @@ impl S3 for FS {
|
||||
Ok(S3Response::new(output))
|
||||
}
|
||||
|
||||
async fn restore_object(&self, _req: S3Request<RestoreObjectInput>) -> S3Result<S3Response<RestoreObjectOutput>> {
|
||||
Err(s3_error!(NotImplemented, "RestoreObject is not implemented yet"))
|
||||
/*
|
||||
let bucket = params.bucket;
|
||||
if let Err(e) = un_escape_path(params.object) {
|
||||
warn!("post restore object failed, e: {:?}", e);
|
||||
return Err(S3Error::with_message(S3ErrorCode::Custom("PostRestoreObjectFailed".into()), "post restore object failed"));
|
||||
}
|
||||
|
||||
let Some(store) = new_object_layer_fn() else {
|
||||
return Err(S3Error::with_message(S3ErrorCode::InternalError, "Not init".to_string()));
|
||||
};
|
||||
|
||||
if Err(err) = check_request_auth_type(req, policy::RestoreObjectAction, bucket, object) {
|
||||
return Err(S3Error::with_message(S3ErrorCode::Custom("PostRestoreObjectFailed".into()), "post restore object failed"));
|
||||
}
|
||||
|
||||
if req.content_length <= 0 {
|
||||
return Err(S3Error::with_message(S3ErrorCode::Custom("ErrEmptyRequestBody".into()), "post restore object failed"));
|
||||
}
|
||||
let Some(opts) = post_restore_opts(req, bucket, object) else {
|
||||
return Err(S3Error::with_message(S3ErrorCode::Custom("ErrEmptyRequestBody".into()), "post restore object failed"));
|
||||
};
|
||||
|
||||
let Some(obj_info) = store.get_object_info(bucket, object, opts) else {
|
||||
return Err(S3Error::with_message(S3ErrorCode::Custom("ErrEmptyRequestBody".into()), "post restore object failed"));
|
||||
};
|
||||
|
||||
if obj_info.transitioned_object.status != lifecycle::TRANSITION_COMPLETE {
|
||||
return Err(S3Error::with_message(S3ErrorCode::Custom("ErrEmptyRequestBody".into()), "post restore object failed"));
|
||||
}
|
||||
|
||||
let mut api_err;
|
||||
let Some(rreq) = parse_restore_request(req.body(), req.content_length) else {
|
||||
let api_err = errorCodes.ToAPIErr(ErrMalformedXML);
|
||||
api_err.description = err.Error()
|
||||
return Err(S3Error::with_message(S3ErrorCode::Custom("ErrEmptyRequestBody".into()), "post restore object failed"));
|
||||
};
|
||||
let mut status_code = http::StatusCode::OK;
|
||||
let mut already_restored = false;
|
||||
if Err(err) = rreq.validate(store) {
|
||||
api_err = errorCodes.ToAPIErr(ErrMalformedXML)
|
||||
api_err.description = err.Error()
|
||||
return Err(S3Error::with_message(S3ErrorCode::Custom("ErrEmptyRequestBody".into()), "post restore object failed"));
|
||||
} else {
|
||||
if obj_info.restore_ongoing && rreq.Type != "SELECT" {
|
||||
return Err(S3Error::with_message(S3ErrorCode::Custom("ErrObjectRestoreAlreadyInProgress".into()), "post restore object failed"));
|
||||
}
|
||||
if !obj_info.restore_ongoing && !obj_info.restore_expires.unix_timestamp() == 0 {
|
||||
status_code = http::StatusCode::Accepted;
|
||||
already_restored = true;
|
||||
}
|
||||
}
|
||||
let restore_expiry = lifecycle::expected_expiry_time(OffsetDateTime::now_utc(), rreq.days);
|
||||
let mut metadata = clone_mss(obj_info.user_defined);
|
||||
|
||||
if rreq.type != "SELECT" {
|
||||
obj_info.metadataOnly = true;
|
||||
metadata[xhttp.AmzRestoreExpiryDays] = rreq.days;
|
||||
metadata[xhttp.AmzRestoreRequestDate] = OffsetDateTime::now_utc().format(http::TimeFormat);
|
||||
if already_restored {
|
||||
metadata[AmzRestore] = completed_restore_obj(restore_expiry).String()
|
||||
} else {
|
||||
metadata[AmzRestore] = ongoing_restore_obj().to_string()
|
||||
}
|
||||
obj_info.user_defined = metadata;
|
||||
if let Err(err) = store.copy_object(bucket, object, bucket, object, obj_info, ObjectOptions {
|
||||
version_id: obj_info.version_id,
|
||||
}, ObjectOptions {
|
||||
version_id: obj_info.version_id,
|
||||
m_time: obj_info.mod_time,
|
||||
}) {
|
||||
return Err(S3Error::with_message(S3ErrorCode::Custom("ErrInvalidObjectState".into()), "post restore object failed"));
|
||||
}
|
||||
if already_restored {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
let restore_object = must_get_uuid();
|
||||
if rreq.output_location.s3.bucket_name != "" {
|
||||
w.Header()[AmzRestoreOutputPath] = []string{pathJoin(rreq.OutputLocation.S3.BucketName, rreq.OutputLocation.S3.Prefix, restore_object)}
|
||||
}
|
||||
w.WriteHeader(status_code)
|
||||
send_event(EventArgs {
|
||||
event_name: event::ObjectRestorePost,
|
||||
bucket_name: bucket,
|
||||
object: obj_info,
|
||||
req_params: extract_req_params(r),
|
||||
user_agent: req.user_agent(),
|
||||
host: handlers::get_source_ip(r),
|
||||
});
|
||||
tokio::spawn(async move {
|
||||
if !rreq.SelectParameters.IsEmpty() {
|
||||
let actual_size = obj_info.get_actual_size();
|
||||
if actual_size.is_err() {
|
||||
return Err(S3Error::with_message(S3ErrorCode::Custom("ErrInvalidObjectState".into()), "post restore object failed"));
|
||||
}
|
||||
|
||||
let object_rsc = s3select.NewObjectReadSeekCloser(
|
||||
|offset int64| -> (io.ReadCloser, error) {
|
||||
rs := &HTTPRangeSpec{
|
||||
IsSuffixLength: false,
|
||||
Start: offset,
|
||||
End: -1,
|
||||
}
|
||||
return getTransitionedObjectReader(bucket, object, rs, r.Header,
|
||||
obj_info, ObjectOptions {version_id: obj_info.version_id});
|
||||
},
|
||||
actual_size.unwrap(),
|
||||
);
|
||||
if err = rreq.SelectParameters.Open(objectRSC); err != nil {
|
||||
if serr, ok := err.(s3select.SelectError); ok {
|
||||
let encoded_error_response = encodeResponse(APIErrorResponse {
|
||||
code: serr.ErrorCode(),
|
||||
message: serr.ErrorMessage(),
|
||||
bucket_name: bucket,
|
||||
key: object,
|
||||
resource: r.URL.Path,
|
||||
request_id: w.Header().Get(xhttp.AmzRequestID),
|
||||
host_id: globalDeploymentID(),
|
||||
});
|
||||
//writeResponse(w, serr.HTTPStatusCode(), encodedErrorResponse, mimeXML)
|
||||
Ok(S3Response::with_headers((StatusCode::OK, Body::empty()), header));
|
||||
} else {
|
||||
return Err(S3Error::with_message(S3ErrorCode::Custom("ErrInvalidObjectState".into()), "post restore object failed"));
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
let nr = httptest.NewRecorder();
|
||||
let rw = xhttp.NewResponseRecorder(nr);
|
||||
rw.log_err_body = true;
|
||||
rw.log_all_body = true;
|
||||
rreq.select_parameters.evaluate(rw);
|
||||
rreq.select_parameters.Close();
|
||||
return Ok(S3Response::with_headers((StatusCode::OK, Body::empty()), header));
|
||||
}
|
||||
let opts = ObjectOptions {
|
||||
transition: TransitionOptions {
|
||||
restore_request: rreq,
|
||||
restore_expiry: restore_expiry,
|
||||
},
|
||||
version_id: objInfo.version_id,
|
||||
}
|
||||
if Err(err) = store.restore_transitioned_object(bucket, object, opts) {
|
||||
format!(format!("unable to restore transitioned bucket/object {}/{}: {}", bucket, object, err.to_string()));
|
||||
return Ok(S3Response::with_headers((StatusCode::OK, Body::empty()), header));
|
||||
}
|
||||
|
||||
send_event(EventArgs {
|
||||
EventName: event.ObjectRestoreCompleted,
|
||||
BucketName: bucket,
|
||||
Object: objInfo,
|
||||
ReqParams: extractReqParams(r),
|
||||
UserAgent: r.UserAgent(),
|
||||
Host: handlers.GetSourceIP(r),
|
||||
});
|
||||
});
|
||||
*/
|
||||
}
|
||||
|
||||
/// Delete a bucket
|
||||
#[tracing::instrument(level = "debug", skip(self, req))]
|
||||
async fn delete_bucket(&self, req: S3Request<DeleteBucketInput>) -> S3Result<S3Response<DeleteBucketOutput>> {
|
||||
@@ -2097,27 +2258,14 @@ impl S3 for FS {
|
||||
..
|
||||
} = req.input;
|
||||
|
||||
let lr_retention = false;
|
||||
/*let rcfg = metadata_sys::get_object_lock_config(&bucket).await;
|
||||
if let Ok(rcfg) = rcfg {
|
||||
if let Some(rule) = rcfg.0.rule {
|
||||
if let Some(retention) = rule.default_retention {
|
||||
if let Some(mode) = retention.mode {
|
||||
//if mode == ObjectLockRetentionMode::from_static(ObjectLockRetentionMode::GOVERNANCE) {
|
||||
lr_retention = true;
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
//info!("lifecycle_configuration: {:?}", &lifecycle_configuration);
|
||||
|
||||
let Some(input_cfg) = lifecycle_configuration else { return Err(s3_error!(InvalidArgument)) };
|
||||
|
||||
if let Err(err) = input_cfg.validate(lr_retention).await {
|
||||
//return Err(S3Error::with_message(S3ErrorCode::Custom("BucketLockValidateFailed".into()), "bucket lock validate failed."));
|
||||
return Err(S3Error::with_message(S3ErrorCode::Custom("ValidateFailed".into()), err.to_string()));
|
||||
let rcfg = metadata_sys::get_object_lock_config(&bucket).await;
|
||||
if let Ok(rcfg) = rcfg {
|
||||
if let Err(err) = input_cfg.validate(&rcfg.0).await {
|
||||
//return Err(S3Error::with_message(S3ErrorCode::Custom("BucketLockValidateFailed".into()), err.to_string()));
|
||||
return Err(S3Error::with_message(S3ErrorCode::Custom("ValidateFailed".into()), err.to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
if let Err(err) = validate_transition_tier(&input_cfg).await {
|
||||
|
||||
+12
-4
@@ -58,11 +58,11 @@ export RUSTFS_CONSOLE_ADDRESS=":9001"
|
||||
#export RUSTFS_OBS_SERVICE_NAME=rustfs # 服务名称
|
||||
#export RUSTFS_OBS_SERVICE_VERSION=0.1.0 # 服务版本
|
||||
export RUSTFS_OBS_ENVIRONMENT=develop # 环境名称
|
||||
export RUSTFS_OBS_LOGGER_LEVEL=debug # 日志级别,支持 trace, debug, info, warn, error
|
||||
export RUSTFS_OBS_LOGGER_LEVEL=info # 日志级别,支持 trace, debug, info, warn, error
|
||||
export RUSTFS_OBS_LOCAL_LOGGING_ENABLED=true # 是否启用本地日志记录
|
||||
export RUSTFS_OBS_LOG_DIRECTORY="$current_dir/deploy/logs" # Log directory
|
||||
export RUSTFS_OBS_LOG_ROTATION_TIME="minute" # Log rotation time unit, can be "second", "minute", "hour", "day"
|
||||
export RUSTFS_OBS_LOG_ROTATION_SIZE_MB=1 # Log rotation size in MB
|
||||
export RUSTFS_OBS_LOG_ROTATION_TIME="hour" # Log rotation time unit, can be "second", "minute", "hour", "day"
|
||||
export RUSTFS_OBS_LOG_ROTATION_SIZE_MB=100 # Log rotation size in MB
|
||||
|
||||
export RUSTFS_SINKS_FILE_PATH="$current_dir/deploy/logs"
|
||||
export RUSTFS_SINKS_FILE_BUFFER_SIZE=12
|
||||
@@ -89,10 +89,18 @@ export OTEL_INSTRUMENTATION_SCHEMA_URL="https://opentelemetry.io/schemas/1.31.0"
|
||||
export OTEL_INSTRUMENTATION_ATTRIBUTES="env=production"
|
||||
|
||||
# notify
|
||||
export RUSTFS_NOTIFY_WEBHOOK_ENABLE="true" # 是否启用 webhook 通知
|
||||
export RUSTFS_NOTIFY_WEBHOOK_ENABLE="on" # 是否启用 webhook 通知
|
||||
export RUSTFS_NOTIFY_WEBHOOK_ENDPOINT="http://[::]:3020/webhook" # webhook 通知地址
|
||||
export RUSTFS_NOTIFY_WEBHOOK_QUEUE_DIR="$current_dir/deploy/logs/notify"
|
||||
|
||||
export RUSTFS_NOTIFY_WEBHOOK_ENABLE_PRIMARY="on" # 是否启用 webhook 通知
|
||||
export RUSTFS_NOTIFY_WEBHOOK_ENDPOINT_PRIMARY="http://[::]:3020/webhook" # webhook 通知地址
|
||||
export RUSTFS_NOTIFY_WEBHOOK_QUEUE_DIR_PRIMARY="$current_dir/deploy/logs/notify"
|
||||
|
||||
export RUSTFS_NOTIFY_WEBHOOK_ENABLE_MASTER="on" # 是否启用 webhook 通知
|
||||
export RUSTFS_NOTIFY_WEBHOOK_ENDPOINT_MASTER="http://[::]:3020/webhook" # webhook 通知地址
|
||||
export RUSTFS_NOTIFY_WEBHOOK_QUEUE_DIR_MASTER="$current_dir/deploy/logs/notify"
|
||||
|
||||
|
||||
export RUSTFS_NS_SCANNER_INTERVAL=60 # 对象扫描间隔时间,单位为秒
|
||||
# exportRUSTFS_SKIP_BACKGROUND_TASK=true
|
||||
|
||||
Reference in New Issue
Block a user