reddb-io-server 1.40.10

RedDB server-side engine: storage, runtime, replication, MCP, AI, and the gRPC/HTTP/RedWire/PG-wire dispatchers. Re-exported by the umbrella `reddb` crate.
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
//! Runtime config / secret / KV resolution.
//!
//! Extracted verbatim from `impl_core.rs` (impl_core slice 7/10, issue #1628).
//! Houses the config-tier readers, secret encrypt/decrypt pipeline, and vault
//! KV accessors:
//!
//! - **Free helpers** — `seed_storage_deploy_config`, `show_secrets_allows_key`,
//!   `secret_sql_value_to_string`, `insert_config_json_path`,
//!   `insert_config_json_segments`, `show_config_json_result`.
//! - **Methods** — `vault_kv_get`, `vault_kv_try_set`, `secret_aes_key`,
//!   `config_bool`, `binary_document_body_enabled`, `config_u64`, `config_f64`,
//!   `config_string`, `secret_auto_encrypt`.
use super::*;

pub(crate) fn seed_storage_deploy_config(
    store: &crate::storage::UnifiedStore,
    selection: crate::storage::StorageProfileSelection,
) {
    store.set_config_tree(
        "storage.deploy",
        &crate::json!({
            "profile": selection.deploy_profile.as_str(),
            "packaging": selection.packaging.as_str(),
            "preset": selection.preset_name(),
            "replica_count": selection.replica_count,
            "managed_backup": selection.managed_backup,
            "wal_retention": selection.wal_retention,
        }),
    );
}

pub(crate) fn show_secrets_allows_key(key: &str) -> bool {
    ![
        "red.secret.",
        "red.config.",
        "red.iam.",
        "red.acl.",
        "red.vault.",
    ]
    .iter()
    .any(|prefix| key.starts_with(prefix))
}

pub(crate) fn secret_sql_value_to_string(value: &Value) -> RedDBResult<String> {
    match value {
        Value::Text(s) => Ok(s.to_string()),
        Value::Integer(n) => Ok(n.to_string()),
        Value::UnsignedInteger(n) => Ok(n.to_string()),
        Value::Float(n) => Ok(n.to_string()),
        Value::Boolean(b) => Ok(b.to_string()),
        Value::Null => Err(RedDBError::Query(
            "SET SECRET key = NULL deletes the secret; use DELETE SECRET for explicit deletes"
                .to_string(),
        )),
        Value::Password(_) | Value::Secret(_) => Err(RedDBError::Query(
            "SET SECRET accepts plain scalar literals; PASSWORD() and SECRET() are for typed columns"
                .to_string(),
        )),
        _ => Err(RedDBError::Query(format!(
            "SET SECRET does not support value type {:?} yet",
            value.data_type()
        ))),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn show_secrets_allows_only_user_managed_keys() {
        assert!(!show_secrets_allows_key("red.secret.aes_key"));
        assert!(!show_secrets_allows_key(
            "red.secret.ai.providers.anthropic.tokens.default"
        ));
        assert!(!show_secrets_allows_key("red.config.ai.default.provider"));
        assert!(show_secrets_allows_key("acme.key"));
    }

    #[test]
    fn typed_config_readers_preserve_cold_values_and_live_updates() {
        let directory = tempfile::tempdir().expect("config fixture");
        let runtime = RedDBRuntime::with_options(
            RedDBOptions::persistent(directory.path().join("config.rdb"))
                .with_memory_budget(32 * 1024 * 1024)
                .with_bounded_disk_residency(true),
        )
        .expect("runtime");
        let store = runtime.db().store();
        store
            .try_set_config_tree(
                "probe",
                &crate::json!({"boolean": false, "integer": 42, "float": 1.5, "string": "saved"}),
            )
            .expect("persist configuration");
        store
            .try_set_config_tree("queue.max_wait_ms", &crate::json!(1))
            .expect("queue wait limit");
        store.persist().expect("checkpoint");
        let manager = store
            .get_collection("red.config")
            .expect("config collection");
        assert!(manager.has_backing(), "fixture must read persisted rows");
        assert!(
            manager.query_all(|_| true).is_empty(),
            "fixture must be cold"
        );
        assert_eq!(
            store.try_get_config("probe.integer").unwrap(),
            Some(Value::UnsignedInteger(42))
        );
        assert!(!runtime.config_bool("probe.boolean", true));
        assert_eq!(runtime.config_u64("probe.integer", 9), 42);
        assert_eq!(runtime.config_f64("probe.float", 9.0), 1.5);
        assert_eq!(runtime.config_string("probe.string", "default"), "saved");
        assert!(runtime.redwire_queue_wait_cap_check(5).is_err());
        store
            .try_set_config_tree("probe.integer", &crate::json!(73))
            .expect("live replacement");
        assert_eq!(runtime.config_u64("probe.integer", 9), 73);
        assert_eq!(runtime.config_u64("probe.missing", 9), 9);
    }

    #[test]
    fn show_config_lists_values_that_are_on_disk() {
        let directory = tempfile::tempdir().expect("config fixture");
        let runtime = RedDBRuntime::with_options(
            RedDBOptions::persistent(directory.path().join("config.rdb"))
                .with_memory_budget(32 * 1024 * 1024)
                .with_bounded_disk_residency(true),
        )
        .expect("runtime");
        let shown = |runtime: &RedDBRuntime| -> Vec<Value> {
            runtime
                .execute_query("SHOW CONFIG probe.string")
                .expect("show")
                .result
                .records
                .iter()
                .filter_map(|record| record.get("value").cloned())
                .collect()
        };
        runtime
            .execute_query("SET CONFIG probe.string = 'saved'")
            .expect("set");
        let store = runtime.db().store();
        store.persist().expect("checkpoint");
        let manager = store
            .get_collection("red.config")
            .expect("config collection");
        assert!(manager.has_backing(), "fixture must read persisted rows");
        assert!(
            manager.query_all(|_| true).is_empty(),
            "fixture must be cold"
        );
        assert_eq!(shown(&runtime), vec![Value::text("saved")]);

        // A value set afterwards is resident and replaces the one on disk.
        runtime
            .execute_query("SET CONFIG probe.string = 'newer'")
            .expect("replace");
        assert_eq!(shown(&runtime), vec![Value::text("newer")]);
    }

    #[test]
    fn boot_defaults_and_overlay_preserve_cold_operator_configuration() {
        let directory = tempfile::tempdir().expect("config fixture");
        let runtime = RedDBRuntime::with_options(
            RedDBOptions::persistent(directory.path().join("config.rdb"))
                .with_memory_budget(32 * 1024 * 1024)
                .with_bounded_disk_residency(true),
        )
        .expect("runtime");
        let store = runtime.db().store();
        store
            .try_set_config_tree("concurrency.locking.enabled", &crate::json!(false))
            .expect("operator configuration");
        store.persist().expect("checkpoint");
        super::super::config_matrix::heal_critical_keys(store.as_ref());
        assert_eq!(
            store.try_get_config("concurrency.locking.enabled").unwrap(),
            Some(Value::Boolean(false))
        );
        let overlay = directory.path().join("overlay.json");
        std::fs::write(&overlay, r#"{"concurrency":{"locking":{"enabled":true}}}"#)
            .expect("overlay fixture");
        assert_eq!(
            super::super::config_overlay::apply_config_file(
                store.as_ref(),
                overlay.to_str().unwrap()
            ),
            0
        );
        assert_eq!(
            store.try_get_config("concurrency.locking.enabled").unwrap(),
            Some(Value::Boolean(false))
        );
    }

    #[test]
    fn fresh_boot_never_persists_a_redundant_red_config_prefix() {
        let runtime = RedDBRuntime::in_memory().expect("runtime");
        let manager = runtime
            .db()
            .store()
            .get_collection("red.config")
            .expect("red.config");
        let mut invalid = Vec::new();
        manager.for_each_entity(|entity| {
            if let Some(Value::Text(key)) =
                entity.data.as_row().and_then(|row| row.get_field("key"))
            {
                if key.starts_with("red.") {
                    invalid.push(key.to_string());
                }
            }
            true
        });
        assert!(
            invalid.is_empty(),
            "fresh red.config contains redundant prefixes: {invalid:?}"
        );
    }

    #[test]
    fn sql_config_surfaces_reject_redundant_red_prefixes() {
        let runtime = RedDBRuntime::in_memory().expect("runtime");
        for query in ["SET CONFIG red.auth.enabled = true", "SHOW CONFIG red.auth"] {
            let error = match runtime.execute_query(query) {
                Ok(_) => panic!("legacy config key unexpectedly accepted: {query}"),
                Err(error) => error,
            };
            assert!(
                error
                    .to_string()
                    .contains("omit the redundant `red.` prefix"),
                "unexpected error for {query}: {error}"
            );
        }
    }
}

pub(crate) fn insert_config_json_path(
    root: &mut crate::serde_json::Value,
    path: &str,
    value: crate::serde_json::Value,
) {
    let segments: Vec<&str> = path
        .split('.')
        .filter(|segment| !segment.is_empty())
        .collect();
    insert_config_json_segments(root, &segments, value);
}

fn insert_config_json_segments(
    root: &mut crate::serde_json::Value,
    segments: &[&str],
    value: crate::serde_json::Value,
) {
    if segments.is_empty() {
        *root = value;
        return;
    }

    if !matches!(root, crate::serde_json::Value::Object(_)) {
        *root = crate::serde_json::Value::Object(crate::serde_json::Map::new());
    }

    let crate::serde_json::Value::Object(map) = root else {
        return;
    };
    if segments.len() == 1 {
        map.insert(segments[0].to_string(), value);
        return;
    }
    let entry = map
        .entry(segments[0].to_string())
        .or_insert_with(|| crate::serde_json::Value::Object(crate::serde_json::Map::new()));
    insert_config_json_segments(entry, &segments[1..], value);
}

pub(crate) fn show_config_json_result(
    query: &str,
    mode: reddb_rql::modes::QueryMode,
    prefix: &Option<String>,
    value: crate::serde_json::Value,
) -> RuntimeQueryResult {
    let mut result = UnifiedResult::with_columns(vec!["key".into(), "value".into()]);
    let mut record = UnifiedRecord::new();
    record.set(
        "key",
        prefix
            .as_ref()
            .map(|key| Value::text(key.clone()))
            .unwrap_or(Value::Null),
    );
    record.set("value", Value::Json(value.to_string_compact().into_bytes()));
    result.push(record);
    RuntimeQueryResult {
        query: query.to_string(),
        mode,
        statement: "show_config_json",
        engine: "runtime-config",
        result,
        affected_rows: 0,
        statement_type: "select",
        bookmark: None,
        notice: None,
    }
}

impl RedDBRuntime {
    pub(crate) fn set_internal_provider_secret(
        &self,
        key: &str,
        value: Option<String>,
    ) -> RedDBResult<()> {
        self.reject_vault_transaction()?;
        if super::execution_context::current_tenant().is_some() {
            return Err(RedDBError::Query(
                "system provider credentials are platform-scoped".into(),
            ));
        }
        let auth =
            self.inner.auth_store.read().clone().ok_or_else(|| {
                RedDBError::Query("secret storage requires an enabled vault".into())
            })?;
        self.check_secret_write_privilege(&auth, key)?;
        match value {
            Some(value) => auth.vault_kv_try_set(key.into(), value),
            None => auth.vault_kv_try_delete(key).map(|_| ()),
        }
        .map_err(|err| RedDBError::Query(err.to_string()))?;
        self.invalidate_result_cache();
        Ok(())
    }

    /// Read a vault KV secret from the configured AuthStore, if present.
    pub fn vault_kv_get(&self, key: &str) -> Option<String> {
        self.inner
            .auth_store
            .read()
            .as_ref()
            .and_then(|store| store.vault_kv_get(key))
    }

    /// Write a vault KV secret and fail if the encrypted vault write is
    /// unavailable or cannot be made durable.
    pub fn vault_kv_try_set(&self, key: String, value: String) -> RedDBResult<()> {
        let store = self.inner.auth_store.read().clone().ok_or_else(|| {
            RedDBError::Query("secret storage requires an enabled, unsealed vault".to_string())
        })?;
        store
            .vault_kv_try_set(key, value)
            .map_err(|err| RedDBError::Query(err.to_string()))
    }

    /// Returns the vault AES key (`red.secret.aes_key`) if an auth
    /// store is wired and a key has been generated. Used by the
    /// `Value::Secret` encrypt/decrypt pipeline.
    pub(crate) fn secret_aes_key(&self) -> Option<[u8; 32]> {
        let guard = self.inner.auth_store.read();
        guard.as_ref().and_then(|s| s.vault_secret_key())
    }

    /// Resolve a boolean flag from `red.config`. Defaults to `default`
    /// when the key is missing or not coercible. If the same key has
    /// been written multiple times (SET CONFIG appends new rows), the
    /// most recent entity wins. Env-var overrides
    /// (`REDDB_<UP_DOTTED>`) take highest precedence.
    pub(crate) fn config_bool(&self, key: &str, default: bool) -> bool {
        if let Some(raw) = self.inner.env_config_overrides.get(key) {
            if let Some(reddb_types::Value::Boolean(b)) =
                crate::runtime::config_overlay::coerce_env_value(key, raw)
            {
                return b;
            }
        }
        match self.inner.db.store().get_config(key) {
            Some(Value::Boolean(value)) => value,
            Some(Value::Text(value)) => matches!(value.as_ref(), "true" | "TRUE" | "True" | "1"),
            Some(Value::Integer(value)) => value != 0,
            _ => default,
        }
    }

    /// Whether DOCUMENT writes should store the body as the native binary
    /// container (PRD-1398, ADR-0063). On by default after the production
    /// cutover. Reads decode the container transparently regardless of this
    /// flag.
    pub(crate) fn binary_document_body_enabled(&self) -> bool {
        self.config_bool("storage.binary_document_body", true)
    }

    pub(crate) fn config_u64(&self, key: &str, default: u64) -> u64 {
        if let Some(raw) = self.inner.env_config_overrides.get(key) {
            if let Some(Value::UnsignedInteger(value)) =
                crate::runtime::config_overlay::coerce_env_value(key, raw)
            {
                return value;
            }
        }
        match self.inner.db.store().get_config(key) {
            Some(Value::Integer(value)) => value as u64,
            Some(Value::UnsignedInteger(value)) => value,
            Some(Value::Text(value)) => value.parse::<u64>().unwrap_or(default),
            _ => default,
        }
    }

    pub(crate) fn config_f64(&self, key: &str, default: f64) -> f64 {
        if let Some(raw) = self.inner.env_config_overrides.get(key) {
            if let Ok(value) = raw.parse::<f64>() {
                return value;
            }
        }
        match self.inner.db.store().get_config(key) {
            Some(Value::Float(value)) => value,
            Some(Value::Integer(value)) => value as f64,
            Some(Value::UnsignedInteger(value)) => value as f64,
            Some(Value::Text(value)) => value.parse::<f64>().unwrap_or(default),
            _ => default,
        }
    }

    pub(crate) fn config_string(&self, key: &str, default: &str) -> String {
        if let Some(raw) = self.inner.env_config_overrides.get(key) {
            return raw.clone();
        }
        match self.inner.db.store().get_config(key) {
            Some(Value::Text(value)) => value.to_string(),
            _ => default.to_string(),
        }
    }

    /// Whether `SECRET('...')` literals should be encrypted with the
    /// vault AES key on INSERT. Default `true`.
    pub(crate) fn secret_auto_encrypt(&self) -> bool {
        self.config_bool("secret.auto_encrypt", true)
    }
}