Skip to main content

rustfs_obs/
logging.rs

1// Copyright 2024 RustFS Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use rustfs_security_governance::{RedactionLevel, RedactionPolicyError, RedactionRule, validate_redaction_rules};
16pub use rustfs_utils::MaskedAccessKey;
17
18pub const REDACTED_LOG_VALUE: &str = "***redacted***";
19
20pub const LOGGING_REDACTION_RULES: &[RedactionRule] = &[
21    RedactionRule::new("access_key", RedactionLevel::Sensitive, "principal identifiers should be masked in logs"),
22    RedactionRule::new(
23        "authorization",
24        RedactionLevel::Secret,
25        "authorization headers may carry bearer or signature material",
26    ),
27    RedactionRule::new(
28        "client_secret",
29        RedactionLevel::Secret,
30        "OIDC and external provider client secrets must never be logged",
31    ),
32    RedactionRule::new("secret_key", RedactionLevel::Secret, "secret access keys must never be logged"),
33    RedactionRule::new(
34        "session_token",
35        RedactionLevel::Secret,
36        "session tokens can be replayed if exposed in logs",
37    ),
38    RedactionRule::new("token", RedactionLevel::Secret, "generic token fields are treated as secret by default"),
39];
40
41pub fn validate_logging_redaction_rules() -> Result<(), RedactionPolicyError> {
42    validate_redaction_rules(LOGGING_REDACTION_RULES)
43}
44
45pub fn is_sensitive_log_field(field_name: &str) -> bool {
46    LOGGING_REDACTION_RULES
47        .iter()
48        .any(|rule| rule.field().eq_ignore_ascii_case(field_name.trim()) && rule.level().requires_redaction())
49}
50
51pub fn redacted_log_value(value: &str) -> &'static str {
52    if value.is_empty() { "" } else { REDACTED_LOG_VALUE }
53}
54
55pub fn redacted_optional_log_value(value: Option<&str>) -> Option<&'static str> {
56    value.map(redacted_log_value)
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn logging_redaction_rules_are_valid() {
65        assert!(validate_logging_redaction_rules().is_ok());
66    }
67
68    #[test]
69    fn identifies_sensitive_log_fields_case_insensitively() {
70        assert!(is_sensitive_log_field("access_key"));
71        assert!(is_sensitive_log_field("Authorization"));
72        assert!(is_sensitive_log_field("session_token"));
73        assert!(!is_sensitive_log_field("bucket"));
74        assert!(!is_sensitive_log_field("status_code"));
75    }
76
77    #[test]
78    fn redacted_log_value_preserves_empty_values() {
79        assert_eq!(redacted_log_value(""), "");
80        assert_eq!(redacted_log_value("secret"), REDACTED_LOG_VALUE);
81        assert_eq!(redacted_optional_log_value(None), None);
82        assert_eq!(redacted_optional_log_value(Some("")), Some(""));
83        assert_eq!(redacted_optional_log_value(Some("secret")), Some(REDACTED_LOG_VALUE));
84    }
85
86    #[test]
87    fn masked_access_key_masks_short_values() {
88        assert_eq!(MaskedAccessKey("").to_string(), "");
89        assert_eq!(MaskedAccessKey("a").to_string(), "***");
90        assert_eq!(MaskedAccessKey("abcd").to_string(), "***");
91        assert_eq!(MaskedAccessKey("abcde").to_string(), "a***e");
92        assert_eq!(MaskedAccessKey("abcdefgh").to_string(), "a***h");
93    }
94
95    #[test]
96    fn masked_access_key_masks_long_values() {
97        assert_eq!(MaskedAccessKey("AKIAIOSFODNN7EXAMPLE").to_string(), "AKIA***MPLE");
98        assert_eq!(format!("{:?}", MaskedAccessKey("keystone:user-1234")), "keys***1234");
99    }
100}