reductstore 1.19.7

ReductStore is a time series database designed specifically for storing and managing large amounts of blob data.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// Copyright 2021-2026 ReductSoftware UG
// Licensed under the Apache License, Version 2.0

use crate::cfg::{parse_bool, CfgParser, ExtCfgBounds};
use crate::core::env::{Env, GetEnv};
use std::fmt::{Display, Formatter};
use std::str::FromStr;

const DEFAULT_BUCKET: &str = "zenoh";

/// Configuration for the minimal Zenoh API integration (single-bucket mode).
///
/// # Supported Environment Variables
///
/// - `RS_ZENOH_ENABLED`: Enable/disable the Zenoh integration (default: false)
/// - `RS_ZENOH_CONFIG`: Inline Zenoh config string (e.g., "mode=client;peer=localhost:7447")
/// - `RS_ZENOH_CONFIG_PATH`: Path to a Zenoh JSON5 config file
/// - `RS_ZENOH_BUCKET`: The single bucket for all Zenoh data (default: "zenoh")
/// - `RS_ZENOH_SUB_KEYEXPRS`: Key expression for subscriber (write path), e.g., "**"
/// - `RS_ZENOH_QUERY_KEYEXPRS`: Key expression for queryable (read path), e.g., "**"
/// - `RS_ZENOH_QUERY_LOCALITY`: Allowed origin for query replies. One of `SessionLocal`, `Remote`, or `Any` (default)
///
/// ## Inline Credential Files (for cloud environments)
///
/// When using `RS_ZENOH_CONFIG`, the Zenoh config can reference credential file paths.
/// These env vars allow providing file content inline, which is written to temp files:
///
/// - `RS_ZENOH_TLS_ROOT_CA`: Inline root CA certificate (for `transport/link/tls/root_ca_certificate`)
/// - `RS_ZENOH_TLS_CONNECT_CERT`: Inline mTLS client certificate (for `transport/link/tls/connect_certificate`)
/// - `RS_ZENOH_TLS_CONNECT_KEY`: Inline mTLS client private key (for `transport/link/tls/connect_private_key`)
/// - `RS_ZENOH_AUTH_DICTIONARY`: Inline auth dictionary content (for `transport/auth/usrpwd/dictionary_file`)
///
/// If both `RS_ZENOH_CONFIG` and `RS_ZENOH_CONFIG_PATH` are set, inline config takes precedence.
#[derive(Clone, Debug, PartialEq)]
pub struct ZenohApiConfig {
    /// Enables the Zenoh API runtime.
    pub enabled: bool,
    /// Inline Zenoh configuration string (e.g., "mode=client;peer=localhost:7447").
    /// Takes precedence over `config_path` if both are set.
    pub config_inline: Option<String>,
    /// Path to a Zenoh JSON5 configuration file.
    pub config_path: Option<String>,
    /// The single ReductStore bucket used for all Zenoh data.
    pub bucket: String,
    /// Key expression for the Zenoh subscriber (write path).
    /// If unset, the write path is disabled.
    pub sub_keyexprs: Option<String>,
    /// Key expression for the Zenoh queryable (read path).
    /// If unset, the read path is disabled.
    pub query_keyexprs: Option<String>,
    /// Allowed locality for the Zenoh queryable responses.
    pub query_locality: ZenohQueryableLocality,
    /// Inline root CA certificate content.
    /// Written to a temp file and injected as `transport/link/tls/root_ca_certificate`.
    pub tls_root_ca_cert: Option<String>,
    /// Inline mTLS client certificate content.
    /// Written to a temp file and injected as `transport/link/tls/connect_certificate`.
    pub tls_connect_cert: Option<String>,
    /// Inline mTLS client private key content.
    /// Written to a temp file and injected as `transport/link/tls/connect_private_key`.
    pub tls_connect_key: Option<String>,
    /// Inline user/password dictionary content (user:password per line).
    /// Written to a temp file and injected as `transport/auth/usrpwd/dictionary_file`.
    pub auth_dictionary: Option<String>,
}

impl Default for ZenohApiConfig {
    fn default() -> Self {
        ZenohApiConfig {
            enabled: false,
            config_inline: None,
            config_path: None,
            bucket: DEFAULT_BUCKET.to_string(),
            sub_keyexprs: None,
            query_keyexprs: None,
            query_locality: ZenohQueryableLocality::default(),
            tls_root_ca_cert: None,
            tls_connect_cert: None,
            tls_connect_key: None,
            auth_dictionary: None,
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ZenohQueryableLocality {
    SessionLocal,
    Remote,
    Any,
}

impl Default for ZenohQueryableLocality {
    fn default() -> Self {
        ZenohQueryableLocality::Any
    }
}

impl Display for ZenohQueryableLocality {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let value = match self {
            ZenohQueryableLocality::SessionLocal => "SessionLocal",
            ZenohQueryableLocality::Remote => "Remote",
            ZenohQueryableLocality::Any => "Any",
        };
        write!(f, "{}", value)
    }
}

impl FromStr for ZenohQueryableLocality {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.trim().to_lowercase().as_str() {
            "sessionlocal" => Ok(ZenohQueryableLocality::SessionLocal),
            "remote" => Ok(ZenohQueryableLocality::Remote),
            "any" => Ok(ZenohQueryableLocality::Any),
            _ => Err(()),
        }
    }
}

impl Display for ZenohApiConfig {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "enabled={}, bucket={}, config={}, config_path={}, sub_keyexprs={}, query_keyexprs={}, query_locality={}, tls={}, auth={}",
            self.enabled,
            self.bucket,
            self.config_inline.as_deref().unwrap_or("<none>"),
            self.config_path.as_deref().unwrap_or("<none>"),
            self.sub_keyexprs.as_deref().unwrap_or("<disabled>"),
            self.query_keyexprs.as_deref().unwrap_or("<disabled>"),
            self.query_locality,
            self.tls_root_ca_cert.is_some() || self.tls_connect_cert.is_some(),
            self.auth_dictionary.is_some()
        )
    }
}

impl<EnvGetter: GetEnv, ExtCfg: ExtCfgBounds> CfgParser<EnvGetter, ExtCfg> {
    pub(super) fn parse_zenoh_api_config(env: &mut Env<EnvGetter>) -> ZenohApiConfig {
        ZenohApiConfig {
            enabled: parse_bool(env.get_optional::<String>("RS_ZENOH_ENABLED"), false),
            config_inline: parse_optional_string(env.get_optional::<String>("RS_ZENOH_CONFIG")),
            config_path: parse_optional_string(env.get_optional::<String>("RS_ZENOH_CONFIG_PATH")),
            bucket: env
                .get_optional("RS_ZENOH_BUCKET")
                .filter(|value: &String| !value.trim().is_empty())
                .unwrap_or_else(|| DEFAULT_BUCKET.to_string()),
            sub_keyexprs: parse_optional_string(
                env.get_optional::<String>("RS_ZENOH_SUB_KEYEXPRS"),
            ),
            query_keyexprs: parse_optional_string(
                env.get_optional::<String>("RS_ZENOH_QUERY_KEYEXPRS"),
            ),
            query_locality: parse_query_locality(
                env.get_optional::<String>("RS_ZENOH_QUERY_LOCALITY"),
            ),
            tls_root_ca_cert: parse_optional_string(
                env.get_optional::<String>("RS_ZENOH_TLS_ROOT_CA"),
            ),
            tls_connect_cert: parse_optional_string(
                env.get_optional::<String>("RS_ZENOH_TLS_CONNECT_CERT"),
            ),
            tls_connect_key: parse_optional_string(
                env.get_optional::<String>("RS_ZENOH_TLS_CONNECT_KEY"),
            ),
            auth_dictionary: parse_optional_string(
                env.get_optional::<String>("RS_ZENOH_AUTH_DICTIONARY"),
            ),
        }
    }
}

fn parse_optional_string(raw: Option<String>) -> Option<String> {
    raw.map(|s| s.trim().to_string()).filter(|s| !s.is_empty())
}

fn parse_query_locality(raw: Option<String>) -> ZenohQueryableLocality {
    raw.and_then(|value| ZenohQueryableLocality::from_str(&value).ok())
        .unwrap_or_default()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cfg::tests::MockEnvGetter;
    use mockall::predicate::eq;
    use rstest::rstest;
    use std::env::VarError;

    #[rstest]
    fn parses_custom_values() {
        let mut env_getter = MockEnvGetter::new();
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_ENABLED"))
            .times(1)
            .return_const(Ok("yes".to_string()));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_CONFIG"))
            .times(1)
            .return_const(Ok("mode=client;peer=localhost:7447".to_string()));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_CONFIG_PATH"))
            .times(1)
            .return_const(Err(VarError::NotPresent));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_BUCKET"))
            .times(1)
            .return_const(Ok("telemetry".to_string()));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_SUB_KEYEXPRS"))
            .times(1)
            .return_const(Ok("**".to_string()));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_QUERY_KEYEXPRS"))
            .times(1)
            .return_const(Ok("factory/**".to_string()));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_QUERY_LOCALITY"))
            .times(1)
            .return_const(Ok("Remote".to_string()));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_TLS_ROOT_CA"))
            .times(1)
            .return_const(Err(VarError::NotPresent));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_TLS_CONNECT_CERT"))
            .times(1)
            .return_const(Err(VarError::NotPresent));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_TLS_CONNECT_KEY"))
            .times(1)
            .return_const(Err(VarError::NotPresent));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_AUTH_DICTIONARY"))
            .times(1)
            .return_const(Err(VarError::NotPresent));

        let cfg = CfgParser::<MockEnvGetter>::parse_zenoh_api_config(&mut Env::new(env_getter));
        assert!(cfg.enabled);
        assert_eq!(
            cfg.config_inline,
            Some("mode=client;peer=localhost:7447".to_string())
        );
        assert_eq!(cfg.config_path, None);
        assert_eq!(cfg.bucket, "telemetry");
        assert_eq!(cfg.sub_keyexprs, Some("**".to_string()));
        assert_eq!(cfg.query_keyexprs, Some("factory/**".to_string()));
        assert_eq!(cfg.query_locality, ZenohQueryableLocality::Remote);
    }

    #[rstest]
    fn parses_config_path() {
        let mut env_getter = MockEnvGetter::new();
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_ENABLED"))
            .times(1)
            .return_const(Ok("true".to_string()));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_CONFIG"))
            .times(1)
            .return_const(Err(VarError::NotPresent));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_CONFIG_PATH"))
            .times(1)
            .return_const(Ok("/etc/reductstore/zenoh.json5".to_string()));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_BUCKET"))
            .times(1)
            .return_const(Err(VarError::NotPresent));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_SUB_KEYEXPRS"))
            .times(1)
            .return_const(Err(VarError::NotPresent));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_QUERY_KEYEXPRS"))
            .times(1)
            .return_const(Err(VarError::NotPresent));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_QUERY_LOCALITY"))
            .times(1)
            .return_const(Err(VarError::NotPresent));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_TLS_ROOT_CA"))
            .times(1)
            .return_const(Err(VarError::NotPresent));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_TLS_CONNECT_CERT"))
            .times(1)
            .return_const(Err(VarError::NotPresent));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_TLS_CONNECT_KEY"))
            .times(1)
            .return_const(Err(VarError::NotPresent));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_AUTH_DICTIONARY"))
            .times(1)
            .return_const(Err(VarError::NotPresent));

        let cfg = CfgParser::<MockEnvGetter>::parse_zenoh_api_config(&mut Env::new(env_getter));
        assert!(cfg.enabled);
        assert_eq!(cfg.config_inline, None);
        assert_eq!(
            cfg.config_path,
            Some("/etc/reductstore/zenoh.json5".to_string())
        );
        assert_eq!(cfg.bucket, DEFAULT_BUCKET);
        assert_eq!(cfg.sub_keyexprs, None);
        assert_eq!(cfg.query_keyexprs, None);
        assert_eq!(cfg.query_locality, ZenohQueryableLocality::default());
    }

    #[rstest]
    fn parses_defaults() {
        let mut env_getter = MockEnvGetter::new();
        env_getter
            .expect_get()
            .return_const(Err(VarError::NotPresent));

        let cfg = CfgParser::<MockEnvGetter>::parse_zenoh_api_config(&mut Env::new(env_getter));
        assert!(!cfg.enabled);
        assert_eq!(cfg.config_inline, None);
        assert_eq!(cfg.config_path, None);
        assert_eq!(cfg.bucket, DEFAULT_BUCKET);
        assert_eq!(cfg.sub_keyexprs, None);
        assert_eq!(cfg.query_keyexprs, None);
        assert_eq!(cfg.query_locality, ZenohQueryableLocality::default());
    }

    #[rstest]
    fn parses_invalid_enabled_falls_back_to_default() {
        let mut env_getter = MockEnvGetter::new();
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_ENABLED"))
            .times(1)
            .return_const(Ok("maybe".to_string()));
        env_getter
            .expect_get()
            .return_const(Err(VarError::NotPresent));

        let cfg = CfgParser::<MockEnvGetter>::parse_zenoh_api_config(&mut Env::new(env_getter));
        assert!(!cfg.enabled);
    }

    #[rstest]
    fn parses_empty_bucket_falls_back_to_default() {
        let mut env_getter = MockEnvGetter::new();
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_ENABLED"))
            .times(1)
            .return_const(Err(VarError::NotPresent));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_CONFIG"))
            .times(1)
            .return_const(Err(VarError::NotPresent));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_CONFIG_PATH"))
            .times(1)
            .return_const(Err(VarError::NotPresent));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_BUCKET"))
            .times(1)
            .return_const(Ok("   ".to_string())); // whitespace-only
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_SUB_KEYEXPRS"))
            .times(1)
            .return_const(Err(VarError::NotPresent));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_QUERY_KEYEXPRS"))
            .times(1)
            .return_const(Err(VarError::NotPresent));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_QUERY_LOCALITY"))
            .times(1)
            .return_const(Err(VarError::NotPresent));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_TLS_ROOT_CA"))
            .times(1)
            .return_const(Err(VarError::NotPresent));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_TLS_CONNECT_CERT"))
            .times(1)
            .return_const(Err(VarError::NotPresent));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_TLS_CONNECT_KEY"))
            .times(1)
            .return_const(Err(VarError::NotPresent));
        env_getter
            .expect_get()
            .with(eq("RS_ZENOH_AUTH_DICTIONARY"))
            .times(1)
            .return_const(Err(VarError::NotPresent));

        let cfg = CfgParser::<MockEnvGetter>::parse_zenoh_api_config(&mut Env::new(env_getter));
        assert_eq!(cfg.bucket, DEFAULT_BUCKET);
    }

    #[rstest]
    fn test_display() {
        let cfg = ZenohApiConfig {
            enabled: true,
            config_inline: Some("mode=client".to_string()),
            config_path: Some("/etc/zenoh.json5".to_string()),
            bucket: "sensor-data".to_string(),
            sub_keyexprs: Some("**".to_string()),
            query_keyexprs: None,
            query_locality: ZenohQueryableLocality::Remote,
            tls_root_ca_cert: None,
            tls_connect_cert: Some("-----BEGIN CERTIFICATE-----".to_string()),
            tls_connect_key: None,
            auth_dictionary: None,
        };
        let display = format!("{cfg}");
        assert!(display.contains("enabled=true"));
        assert!(display.contains("bucket=sensor-data"));
        assert!(display.contains("config=mode=client"));
        assert!(display.contains("config_path=/etc/zenoh.json5"));
        assert!(display.contains("sub_keyexprs=**"));
        assert!(display.contains("query_keyexprs=<disabled>"));
        assert!(display.contains("query_locality=Remote"));
        assert!(display.contains("tls=true"));
    }

    #[rstest]
    fn test_display_defaults() {
        let cfg = ZenohApiConfig::default();
        let display = format!("{cfg}");
        assert!(display.contains("enabled=false"));
        assert!(display.contains(&format!("bucket={DEFAULT_BUCKET}")));
        assert!(display.contains("config=<none>"));
        assert!(display.contains("config_path=<none>"));
        assert!(display.contains("sub_keyexprs=<disabled>"));
        assert!(display.contains("query_keyexprs=<disabled>"));
        assert!(display.contains("query_locality=Any"));
        assert!(display.contains("tls=false"));
    }
}