Skip to content

Commit 3983004

Browse files
committed
fix: security properties defaults
1 parent 77f3f97 commit 3983004

3 files changed

Lines changed: 86 additions & 4 deletions

File tree

rust/operator-binary/src/controller/build/config_map.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,15 @@ pub fn build_rolegroup_config_map(
102102
}
103103
};
104104

105-
let jvm_sec_props = &validated_rg
106-
.config_overrides
107-
.security_properties()
108-
.overrides;
105+
// The `networkaddress.cache.*` defaults are always emitted (with user `security.properties`
106+
// overrides winning); see `security_properties::build`.
107+
let jvm_sec_props = crate::controller::build::properties::security_properties::build(
108+
validated_rg
109+
.config_overrides
110+
.security_properties()
111+
.overrides
112+
.clone(),
113+
);
109114

110115
let mut cm_builder = ConfigMapBuilder::new();
111116
cm_builder

rust/operator-binary/src/controller/build/properties/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub mod broker_properties;
44
pub mod controller_properties;
55
pub mod listener;
66
pub mod logging;
7+
pub mod security_properties;
78

89
use crate::crd::{KafkaPodDescriptor, role::KafkaRole};
910

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//! Builder for `security.properties` (the JVM security properties file).
2+
//!
3+
//! `networkaddress.cache.ttl` and `networkaddress.cache.negative.ttl` are `required`
4+
//! properties with recommended values, so they are always emitted.
5+
6+
use std::collections::BTreeMap;
7+
8+
const NETWORKADDRESS_CACHE_TTL: &str = "networkaddress.cache.ttl";
9+
const NETWORKADDRESS_CACHE_NEGATIVE_TTL: &str = "networkaddress.cache.negative.ttl";
10+
11+
const DEFAULT_NETWORKADDRESS_CACHE_TTL: &str = "30";
12+
const DEFAULT_NETWORKADDRESS_CACHE_NEGATIVE_TTL: &str = "0";
13+
14+
/// Build the `security.properties` key/value pairs.
15+
///
16+
/// `overrides` are the resolved user overrides for `security.properties`
17+
/// (highest precedence).
18+
pub fn build(overrides: BTreeMap<String, String>) -> BTreeMap<String, String> {
19+
let mut props: BTreeMap<String, String> = BTreeMap::new();
20+
21+
// 1. Defaults (recommended values for the required properties).
22+
props.insert(
23+
NETWORKADDRESS_CACHE_TTL.to_string(),
24+
DEFAULT_NETWORKADDRESS_CACHE_TTL.to_string(),
25+
);
26+
props.insert(
27+
NETWORKADDRESS_CACHE_NEGATIVE_TTL.to_string(),
28+
DEFAULT_NETWORKADDRESS_CACHE_NEGATIVE_TTL.to_string(),
29+
);
30+
31+
// 2. User overrides (highest precedence).
32+
for (k, v) in overrides {
33+
props.insert(k, v);
34+
}
35+
36+
props
37+
}
38+
39+
#[cfg(test)]
40+
mod tests {
41+
use super::*;
42+
43+
#[test]
44+
fn defaults_present_without_overrides() {
45+
let props = build(BTreeMap::new());
46+
assert_eq!(
47+
props.get("networkaddress.cache.ttl"),
48+
Some(&"30".to_string())
49+
);
50+
assert_eq!(
51+
props.get("networkaddress.cache.negative.ttl"),
52+
Some(&"0".to_string())
53+
);
54+
}
55+
56+
#[test]
57+
fn user_override_wins() {
58+
let overrides = [("networkaddress.cache.ttl".to_string(), "60".to_string())]
59+
.into_iter()
60+
.collect();
61+
let props = build(overrides);
62+
assert_eq!(
63+
props.get("networkaddress.cache.ttl"),
64+
Some(&"60".to_string())
65+
);
66+
}
67+
68+
#[test]
69+
fn extra_user_override_key_is_added() {
70+
let overrides = [("custom.security.prop".to_string(), "x".to_string())]
71+
.into_iter()
72+
.collect();
73+
let props = build(overrides);
74+
assert_eq!(props.get("custom.security.prop"), Some(&"x".to_string()));
75+
}
76+
}

0 commit comments

Comments
 (0)