lean-ctx-cloud 3.12.11

The LeanCTX connector: enrollment, bus, knowledge sync, tasks and policy on top of LeanCTX | OSS.
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
// SPDX-License-Identifier: Apache-2.0
//! Filter packs from Cloud.
//!
//! `GET /v1/policy/bundle` (conditional) → verified here before anything is
//! kept: filter packs must be signed by an Cloud key of the verified key
//! set, match their digest and validate completely (every pattern
//! compiles); all packs together stay within [`CLOUD_MAX_RULES`]. A bundle
//! that fails any check never replaces the last good one; the refusal is
//! shown in `cloud status` and `lean-ctx doctor`, and local work goes on
//! under the last verified bundle.
//!
//! Stored in `<data_dir>/cloud/policy/`: the verified bundle, its state,
//! and `filters/set.json`, the one file the policy snapshot reads to apply
//! the patterns ([`filter_rules`]).

use std::path::Path;

use base64::Engine as _;
use base64::engine::general_purpose::STANDARD;
use ed25519_dalek::{Signature, VerifyingKey};
use lean_ctx_time::UtcDateTime;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use super::CloudError;
use super::auth::Session;
use super::lease::verify_keyset;
use super::state::write_private;
use lean_ctx_protocol::cloud::lease::CloudKeySetV1;
use lean_ctx_protocol::cloud::policy::{
    FilterPackRefV1, FilterPackV1, MAX_POLICY_BUNDLE_BYTES, PolicyBundleV1,
    filter_pack_signing_input,
};

pub use lean_ctx::core::policy::cloud_filters::{
    CLOUD_MAX_RULES, FILTER_SET_SCHEMA, FilterRules, FilterSet, filter_rules, filter_set_path,
    installed_filter_rules, load_filter_set, required_packs_installed,
};
use lean_ctx::core::policy::cloud_filters::{FILTER_SET_FILE, digest_hex};

const STATE_FILE: &str = "state.json";
const BUNDLE_FILE: &str = "bundle.json";

/// What the last verified bundle established.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct PolicyState {
    pub etag: Option<String>,
    /// `pack_id → version` of the accepted filter packs.
    #[serde(default)]
    pub filter_packs: std::collections::BTreeMap<String, u64>,
    pub verified_at: Option<String>,
    /// Why the newest bundle was refused (the last good one stays).
    pub last_refusal: Option<String>,
    /// The effective policy in force (2.x, [`super::effective_policy`]).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub effective: Option<super::effective_policy::EffectiveState>,
    /// Why the newest effective policy was refused (the last valid stays).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub effective_refusal: Option<String>,
    /// That refusal as reported to Cloud (SERVER-B4), once per received
    /// version and digest.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub effective_refused: Option<super::effective_policy::RefusalRecord>,
}

pub fn load_state(data_dir: &Path) -> PolicyState {
    std::fs::read(data_dir.join("policy").join(STATE_FILE))
        .ok()
        .and_then(|b| serde_json::from_slice(&b).ok())
        .unwrap_or_default()
}

fn save_state(data_dir: &Path, state: &PolicyState) -> Result<(), CloudError> {
    let bytes = serde_json::to_vec_pretty(state).map_err(|e| CloudError::Local(e.to_string()))?;
    write_private(&data_dir.join("policy"), STATE_FILE, &bytes)
}

/// The last verified bundle, as received.
pub fn load_bundle(data_dir: &Path) -> Option<Value> {
    std::fs::read(data_dir.join("policy").join(BUNDLE_FILE))
        .ok()
        .and_then(|b| serde_json::from_slice(&b).ok())
}

fn verify(public: &[u8; 32], message: &[u8], signature: &[u8; 64]) -> bool {
    VerifyingKey::from_bytes(public).is_ok_and(|key| {
        key.verify_strict(message, &Signature::from_bytes(signature))
            .is_ok()
    })
}

/// Check one filter pack against the verified Cloud key set; returns the pack.
fn verify_filter_pack(
    pack: &FilterPackRefV1,
    keyset: &CloudKeySetV1,
) -> Result<FilterPackV1, String> {
    let id = &pack.pack_id;
    let statement = STANDARD
        .decode(&pack.envelope.statement_b64)
        .map_err(|_| format!("filter pack {id}: statement is not base64"))?;
    if digest_hex(&statement) != pack.sha256 {
        return Err(format!("filter pack {id}: digest mismatch"));
    }
    let key = keyset
        .key(&pack.envelope.kid)
        .ok_or_else(|| format!("filter pack {id}: unknown signing key"))?;
    let public: [u8; 32] = STANDARD
        .decode(&key.ed25519_pub)
        .ok()
        .and_then(|b| b.try_into().ok())
        .ok_or("key set key is malformed")?;
    let signature: [u8; 64] = STANDARD
        .decode(&pack.envelope.signature)
        .ok()
        .and_then(|b| b.try_into().ok())
        .ok_or_else(|| format!("filter pack {id}: signature is malformed"))?;
    if !verify(&public, &filter_pack_signing_input(&statement), &signature) {
        return Err(format!("filter pack {id}: signature does not verify"));
    }
    let parsed: FilterPackV1 = serde_json::from_slice(&statement)
        .map_err(|_| format!("filter pack {id}: statement is not a filter pack"))?;
    parsed
        .validate()
        .map_err(|e| format!("filter pack {id}: {e}"))?;
    if parsed.pack_id != *id || parsed.version != pack.version {
        return Err(format!(
            "filter pack {id}: statement does not match its listing"
        ));
    }
    Ok(parsed)
}

/// Verify a whole bundle against the key set, and against the last
/// accepted state (filter pack versions only move forward). Returns the
/// state it establishes and the verified filter packs. The bundle's TOML
/// org policy fields are not read.
pub fn verify_bundle(
    raw: &Value,
    keyset: &CloudKeySetV1,
    previous: &PolicyState,
    now: UtcDateTime,
) -> Result<(PolicyState, FilterSet), String> {
    let bundle: PolicyBundleV1 =
        serde_json::from_value(raw.clone()).map_err(|e| format!("bundle: {e}"))?;
    bundle.validate().map_err(|e| format!("bundle: {e}"))?;
    let mut state = PolicyState {
        etag: None,
        verified_at: Some(now.to_rfc3339_opts(lean_ctx_time::SecondsFormat::Secs, true)),
        effective: previous.effective.clone(),
        effective_refusal: previous.effective_refusal.clone(),
        effective_refused: previous.effective_refused.clone(),
        ..PolicyState::default()
    };
    let mut packs = Vec::new();
    let mut statements = std::collections::BTreeMap::new();
    let mut labels = std::collections::BTreeSet::new();
    for pack in &bundle.filter_packs {
        let verified = verify_filter_pack(pack, keyset)?;
        statements.insert(pack.pack_id.clone(), pack.envelope.statement_b64.clone());
        if previous
            .filter_packs
            .get(&verified.pack_id)
            .is_some_and(|accepted| verified.version < *accepted)
        {
            return Err(format!(
                "filter pack {} version {} is older than the accepted one",
                verified.pack_id, verified.version
            ));
        }
        for pattern in &verified.patterns {
            if !labels.insert(pattern.rule_label()) {
                return Err(format!(
                    "filter pack {}: rule {} repeats across packs",
                    verified.pack_id,
                    pattern.rule_label()
                ));
            }
        }
        state
            .filter_packs
            .insert(verified.pack_id.clone(), verified.version);
        packs.push(verified);
    }
    if labels.len() > CLOUD_MAX_RULES {
        return Err(format!(
            "the filter packs hold {} patterns; at most {CLOUD_MAX_RULES} are applied as a whole",
            labels.len()
        ));
    }
    packs.sort_by(|a, b| a.pack_id.cmp(&b.pack_id));
    Ok((
        state,
        FilterSet {
            schema: FILTER_SET_SCHEMA.into(),
            packs,
            statements,
        },
    ))
}

/// What a policy pull did.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PolicyPull {
    Unchanged,
    Accepted(Box<PolicyState>),
    Refused(String),
}

/// The `If-None-Match` of the next pull: the kept ETag, if any.
pub(crate) fn if_none_match(state: &PolicyState) -> Option<&str> {
    state.etag.as_deref().filter(|etag| !etag.is_empty())
}

/// The last step of a pull with a full answer, once its effective policy
/// was received (`effective_taken`, [`super::effective_policy::receive`]):
/// a refused effective policy keeps no ETag. Cloud tags the bundle it
/// signed, so a copy refused here (e.g. a signature broken on the way, the
/// genuine ETag passed through) would otherwise turn every later pull into
/// a 304 that never fetches the genuine bundle; without a tag the next pull
/// fetches it in full.
///
/// A newer policy in force than `previous` (effective version or digest)
/// ends every policy hold ([`super::throttle`]):
/// each was the old policy's answer, and an admin who loosened it expects
/// the next claim or publish to ask Cloud again. Quota throttles stay.
pub(crate) fn settle(
    store: &super::CloudStore,
    previous: &PolicyState,
    state: &mut PolicyState,
    effective_taken: bool,
) {
    if !effective_taken {
        state.etag = None;
    }
    let effective = |s: &PolicyState| s.effective.as_ref().map(|e| (e.version, e.digest.clone()));
    if effective(state).is_some() && effective(state) != effective(previous) {
        super::throttle::policy_changed(store);
    }
}

/// Pull, verify and keep the policy bundle.
pub fn pull(session: &Session) -> Result<PolicyPull, CloudError> {
    let data = session.store.data_dir().to_path_buf();
    let previous = load_state(&data);
    // `since=0` asks for the whole bundle.
    let path = "/policy/bundle?since=0";
    let extra: Vec<(&str, &str)> = if_none_match(&previous)
        .map(|etag| ("If-None-Match", etag))
        .into_iter()
        .collect();
    let answer =
        session.with_client(|c| c.send("GET", path, None, &extra, MAX_POLICY_BUNDLE_BYTES))?;
    let now = session.lease.service_time(UtcDateTime::now()).to_utc();
    let stamp = now.to_rfc3339_opts(lean_ctx_time::SecondsFormat::Secs, true);
    if answer.status == 304 {
        let mut state = previous.clone();
        // An ack that failed (offline) is sent again.
        super::effective_policy::on_pull(session, None, &mut state, now);
        if state != previous {
            save_state(&data, &state)?;
        }
        session.store.record_success("policy", &stamp);
        return Ok(PolicyPull::Unchanged);
    }
    let keys_raw: Value = session
        .store
        .read_config(super::state::KEYS_FILE)?
        .ok_or(CloudError::NotConnected)?;
    let keyset = verify_keyset(&keys_raw, session.root_key())?;
    match verify_bundle(&answer.body, &keyset, &previous, now) {
        Ok((mut state, filters)) => {
            state.etag = answer.etag;
            let local = |e: serde_json::Error| CloudError::Local(e.to_string());
            write_private(
                &data.join("policy"),
                BUNDLE_FILE,
                &serde_json::to_vec_pretty(&answer.body).map_err(local)?,
            )?;
            write_private(
                &data.join("policy").join("filters"),
                FILTER_SET_FILE,
                &serde_json::to_vec(&filters).map_err(local)?,
            )?;
            let taken =
                super::effective_policy::on_pull(session, Some(&answer.body), &mut state, now);
            settle(&session.store, &previous, &mut state, taken);
            save_state(&data, &state)?;
            session.store.record_success("policy", &stamp);
            Ok(PolicyPull::Accepted(Box::new(state)))
        }
        Err(reason) => {
            let mut kept = previous.clone();
            kept.last_refusal = Some(reason.clone());
            // The effective policy is verified on its own: a refused
            // filter pack does not hold back a newer, stricter effective
            // policy.
            let taken =
                super::effective_policy::on_pull(session, Some(&answer.body), &mut kept, now);
            settle(&session.store, &previous, &mut kept, taken);
            save_state(&data, &kept)?;
            session.store.record_error(&format!("policy: {reason}"));
            Ok(PolicyPull::Refused(reason))
        }
    }
}

const PULLS_FILE: &str = "pulls.json";

/// What started a policy pull.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PulledVia {
    /// A bus hint (`policy.advanced`, a reconnect).
    Nudge,
    /// The scheduler's periodic pull (the fallback).
    Poll,
    /// `lean-ctx cloud sync`.
    Manual,
}

/// The last policy pull, and whether Cloud has shown it sends policy
/// nudges (`<data_dir>/cloud/policy/pulls.json`).
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct PullRecord {
    pub last_pull_at: Option<String>,
    pub pulled_via: Option<PulledVia>,
    /// Cloud sent a `policy.advanced` frame or a `hello.ack` with
    /// `policy_version` at least once: changes arrive at once; until then
    /// only the periodic pull brings them.
    pub nudges_seen: bool,
}

pub fn load_pulls(data_dir: &Path) -> PullRecord {
    std::fs::read(data_dir.join("policy").join(PULLS_FILE))
        .ok()
        .and_then(|b| serde_json::from_slice(&b).ok())
        .unwrap_or_default()
}

fn save_pulls(data_dir: &Path, record: &PullRecord) {
    if let Ok(bytes) = serde_json::to_vec_pretty(record) {
        let _ = write_private(&data_dir.join("policy"), PULLS_FILE, &bytes);
    }
}

/// Note a successful pull started by `via`.
pub fn note_pull(data_dir: &Path, via: PulledVia) {
    let mut record = load_pulls(data_dir);
    record.last_pull_at =
        Some(UtcDateTime::now().to_rfc3339_opts(lean_ctx_time::SecondsFormat::Secs, true));
    record.pulled_via = Some(via);
    save_pulls(data_dir, &record);
}

/// Note that Cloud sends policy nudges (written once).
pub fn note_nudges_seen(data_dir: &Path) {
    let mut record = load_pulls(data_dir);
    if !record.nudges_seen {
        record.nudges_seen = true;
        save_pulls(data_dir, &record);
    }
}

#[cfg(test)]
pub(crate) mod test_support {
    use super::*;
    use ed25519_dalek::{Signer as _, SigningKey};

    /// A signed filter pack listing for `pack_id` at `version` with `patterns`.
    #[allow(clippy::needless_pass_by_value)] // builder: callers hand over fresh JSON
    pub(crate) fn filter_pack_with(
        seed: [u8; 32],
        kid: &str,
        pack_id: &str,
        version: u64,
        patterns: Value,
    ) -> Value {
        let statement = serde_json::to_vec(&serde_json::json!({
            "schema": lean_ctx_protocol::cloud::policy::FILTER_PACK_SCHEMA,
            "pack_id": pack_id, "version": version, "patterns": patterns,
        }))
        .unwrap();
        let signature = SigningKey::from_bytes(&seed).sign(&filter_pack_signing_input(&statement));
        serde_json::json!({
            "pack_id": pack_id, "version": version, "sha256": digest_hex(&statement),
            "envelope": {"statement_b64": STANDARD.encode(&statement), "kid": kid, "signature": STANDARD.encode(signature.to_bytes())},
        })
    }

    pub(crate) fn filter_pack(seed: [u8; 32], kid: &str, pack_id: &str, version: u64) -> Value {
        filter_pack_with(
            seed,
            kid,
            pack_id,
            version,
            serde_json::json!([{"id": "ahv", "kind": "regex", "pattern": "756\\.\\d{4}\\.\\d{4}\\.\\d{2}", "action": "redact"}]),
        )
    }

    #[allow(clippy::needless_pass_by_value)] // builder: callers hand over fresh JSON
    pub(crate) fn bundle(packs: Vec<Value>) -> Value {
        serde_json::json!({
            "schema": lean_ctx_protocol::cloud::policy::POLICY_BUNDLE_SCHEMA,
            "filter_packs": packs,
        })
    }
}

#[cfg(test)]
#[path = "policy_tests.rs"]
mod tests;