policy-rs 1.7.0

Policy library for working with protobuf-defined policy objects
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
//! Matchable trait for field access.

use std::borrow::Cow;

use super::compiled::TypedValue;
use super::signal::Signal;

/// Trait for types that can be matched against policies.
///
/// Implementors provide field access for telemetry records. The engine uses
/// two primitives:
///
/// - [`get_field`](Matchable::get_field) returns the field's string value for
///   pattern matching, or `None` when the field is absent **or** present but
///   not representable as a string (e.g. OTel `intValue: 42`).
/// - [`field_exists`](Matchable::field_exists) reports whether the field is
///   present at all, regardless of value type. The default body delegates to
///   `get_field(...).is_some()`, which is only correct when every present value
///   is a string. Records carrying non-string values **must** override this
///   method so that `exists: true` matchers fire correctly.
///
/// # Contract for OTel-compatible adapters
///
/// The rules below are derived from the policy conformance suite. Violating any
/// of them produces silent match failures — the engine never surfaces a "wrong
/// type" error, it simply treats the field as absent.
///
/// ### String fields (body, severity_text, span name, metric name, …)
/// - Return `Some(Cow::Borrowed(s))` for a non-empty string value.
/// - Return `Some(Cow::Borrowed(""))` for a present-but-empty string. The engine
///   will not match regex/exact/prefix patterns against an empty string, but
///   `field_exists` should still return `true` if the field is logically present.
/// - Return `None` when the field is absent.
///
/// ### Attribute fields (log/span/resource/scope attributes)
/// - An OTel `AnyValue` is **only matchable** when it is a `stringValue`. Any
///   other variant (`intValue`, `doubleValue`, `boolValue`, `bytesValue`,
///   `arrayValue`, `kvlistValue`) must return `None` from `get_field` but
///   `true` from `field_exists`, so that:
///   - String matchers (`regex`, `exact`, etc.) do not fire on non-string values.
///   - `exists: true` matchers still fire for any present value.
///
/// ### Enum fields (metric type, aggregation temporality, span status, span kind)
/// - Synthesize a string using the helpers in [`crate::canonical`]:
///   `canonical::metric_type_str(mt)`, `canonical::span_status_code_str(sc)`, etc.
/// - These return the proto enum's `as_str_name()` output, which is what the
///   engine compiles its exact-match pattern against.
/// - **SpanStatus / Unset**: OTel's `Unset` status maps to proto
///   `SpanStatusCode::Unspecified`. A span with an absent or default (proto3
///   zero-value) status code is still considered *present* — return
///   `canonical::span_status_code_str(SpanStatusCode::Unspecified)` rather
///   than `None`.
///
/// ### ID fields (trace_id, span_id)
/// - Must be encoded as lowercase hex strings (`[0-9a-f]`).
/// - `trace_id` is 32 hex chars (128-bit); `span_id` is 16 hex chars (64-bit).
/// - The sampling code slices the last 14 hex characters of `trace_id` for the
///   56-bit randomness value — a non-hex `trace_id` returns `None` from the
///   sampler rather than panicking, but consistent sampling will fall back.
///
/// ### Per-signal method mapping
///
/// | Signal  | Evaluation method                | Transform support |
/// |---------|----------------------------------|-------------------|
/// | Log     | `evaluate` / `evaluate_and_transform` | Yes (redact, remove, rename, add) |
/// | Metric  | `evaluate`                       | No                |
/// | Trace   | `evaluate_trace`                 | Yes (tracestate `th` update) |
///
/// # Path-based Attribute Access
///
/// For attribute selectors, the path is a `Vec<String>` representing nested
/// attribute access:
///
/// - Single segment `["user_id"]` — flat attribute
/// - Multiple segments `["http", "method"]` — nested map/struct traversal
///
/// # Example Implementation
///
/// ```ignore
/// impl Matchable for MyLog {
///     type Signal = LogSignal;
///
///     fn get_field(&self, field: &LogFieldSelector) -> Option<Cow<'_, str>> {
///         match field {
///             LogFieldSelector::Simple(f) => match f {
///                 LogField::Body => self.body.as_deref().map(Cow::Borrowed),
///                 _ => None,
///             },
///             LogFieldSelector::LogAttribute(path) => {
///                 // Only return Some for stringValue; other OTel value types → None.
///                 path.first()
///                     .and_then(|k| self.attrs.get(k))
///                     .and_then(|v| v.as_string())
///                     .map(Cow::Borrowed)
///             }
///             _ => None,
///         }
///     }
///
///     // Override because attributes can hold non-string OTel values.
///     fn field_exists(&self, field: &LogFieldSelector) -> bool {
///         match field {
///             LogFieldSelector::LogAttribute(path) => path
///                 .first()
///                 .map(|k| self.attrs.contains_key(k))
///                 .unwrap_or(false),
///             _ => self.get_field(field).is_some(),
///         }
///     }
/// }
/// ```
pub trait Matchable {
    /// The telemetry signal type this implementation handles.
    type Signal: Signal;

    /// Get a field value by selector.
    ///
    /// Returns `None` when the field doesn't exist or isn't representable as
    /// a string. Returns `Cow::Borrowed` for fields stored as strings, or
    /// `Cow::Owned` for computed/converted fields.
    ///
    /// For attribute selectors with multi-segment paths, traverse the nested
    /// structure and return the leaf value as a string.
    fn get_field(&self, field: &<Self::Signal as Signal>::FieldSelector) -> Option<Cow<'_, str>>;

    /// Report whether the field is present, regardless of whether its value
    /// can be represented as a string.
    ///
    /// The default returns `self.get_field(field).is_some()`, which conflates
    /// "absent" with "present but non-string." Override this method when your
    /// records carry non-string values (numbers, booleans, structured values)
    /// that should still satisfy `exists: true` matchers.
    fn field_exists(&self, field: &<Self::Signal as Signal>::FieldSelector) -> bool {
        self.get_field(field).is_some()
    }

    /// Get a typed value for use with `equals`, `gt`, `gte`, `lt`, and `lte` matchers.
    ///
    /// The default implementation wraps [`get_field`] and returns the string value
    /// as `TypedValue::String`. Override this method to expose non-string field
    /// values (booleans, integers, floats, raw bytes) so that typed matchers
    /// can match them.
    ///
    /// Return `None` when the field is absent. A present-but-wrong-type value
    /// causes a non-match (not an error), consistent with the fail-open policy.
    ///
    /// # Example
    ///
    /// ```ignore
    /// fn get_typed_value(&self, field: &LogFieldSelector) -> Option<TypedValue<'_>> {
    ///     if let LogFieldSelector::LogAttribute(path) = field {
    ///         let key = path.first()?;
    ///         return match self.attrs.get(key)? {
    ///             Attr::String(s)  => Some(TypedValue::String(Cow::Borrowed(s))),
    ///             Attr::Int(i)     => Some(TypedValue::Int(*i)),
    ///             Attr::Double(d)  => Some(TypedValue::Double(*d)),
    ///             Attr::Bool(b)    => Some(TypedValue::Bool(*b)),
    ///             Attr::Bytes(bs)  => Some(TypedValue::Bytes(bs)),
    ///         };
    ///     }
    ///     self.get_field(field).map(TypedValue::String)
    /// }
    /// ```
    ///
    /// [`get_field`]: Matchable::get_field
    fn get_typed_value(
        &self,
        field: &<Self::Signal as Signal>::FieldSelector,
    ) -> Option<TypedValue<'_>> {
        self.get_field(field).map(TypedValue::String)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::engine::signal::LogSignal;
    use crate::field::LogFieldSelector;
    use crate::proto::tero::policy::v1::LogField;
    use std::borrow::Cow;
    use std::collections::HashMap;

    /// A simple test log record for testing flat attributes.
    struct TestLog {
        body: String,
        severity_text: String,
        log_attributes: HashMap<String, String>,
        resource_attributes: HashMap<String, String>,
    }

    impl Matchable for TestLog {
        type Signal = LogSignal;

        fn get_field(&self, field: &LogFieldSelector) -> Option<Cow<'_, str>> {
            match field {
                LogFieldSelector::Simple(log_field) => match log_field {
                    LogField::Body => Some(Cow::Borrowed(&self.body)),
                    LogField::SeverityText => Some(Cow::Borrowed(&self.severity_text)),
                    _ => None,
                },
                LogFieldSelector::LogAttribute(path) => {
                    // For flat attributes, use first path segment as key
                    path.first()
                        .and_then(|key| self.log_attributes.get(key))
                        .map(|s| Cow::Borrowed(s.as_str()))
                }
                LogFieldSelector::ResourceAttribute(path) => path
                    .first()
                    .and_then(|key| self.resource_attributes.get(key))
                    .map(|s| Cow::Borrowed(s.as_str())),
                LogFieldSelector::ScopeAttribute(_) => None,
            }
        }
    }

    /// A nested attribute value that can hold strings or nested maps.
    #[derive(Clone, Debug)]
    enum NestedValue {
        String(String),
        Map(HashMap<String, NestedValue>),
    }

    impl NestedValue {
        fn as_str(&self) -> Option<&str> {
            match self {
                NestedValue::String(s) => Some(s),
                NestedValue::Map(_) => None,
            }
        }

        fn get(&self, key: &str) -> Option<&NestedValue> {
            match self {
                NestedValue::String(_) => None,
                NestedValue::Map(m) => m.get(key),
            }
        }

        /// Traverse a path and return the leaf value.
        fn traverse(&self, path: &[String]) -> Option<&NestedValue> {
            match path {
                [] => Some(self),
                [first, rest @ ..] => self.get(first).and_then(|v| v.traverse(rest)),
            }
        }
    }

    /// A test log record with nested attribute support.
    struct NestedTestLog {
        body: String,
        log_attributes: HashMap<String, NestedValue>,
        resource_attributes: HashMap<String, NestedValue>,
        scope_attributes: HashMap<String, NestedValue>,
    }

    impl NestedTestLog {
        fn new(body: &str) -> Self {
            Self {
                body: body.to_string(),
                log_attributes: HashMap::new(),
                resource_attributes: HashMap::new(),
                scope_attributes: HashMap::new(),
            }
        }

        fn with_log_attr(mut self, key: &str, value: NestedValue) -> Self {
            self.log_attributes.insert(key.to_string(), value);
            self
        }

        fn with_resource_attr(mut self, key: &str, value: NestedValue) -> Self {
            self.resource_attributes.insert(key.to_string(), value);
            self
        }

        fn with_scope_attr(mut self, key: &str, value: NestedValue) -> Self {
            self.scope_attributes.insert(key.to_string(), value);
            self
        }
    }

    impl Matchable for NestedTestLog {
        type Signal = LogSignal;

        fn get_field(&self, field: &LogFieldSelector) -> Option<Cow<'_, str>> {
            match field {
                LogFieldSelector::Simple(log_field) => match log_field {
                    LogField::Body => Some(Cow::Borrowed(&self.body)),
                    _ => None,
                },
                LogFieldSelector::LogAttribute(path) => {
                    if path.is_empty() {
                        return None;
                    }
                    let (first, rest) = path.split_first()?;
                    self.log_attributes
                        .get(first)
                        .and_then(|v| v.traverse(rest))
                        .and_then(|v| v.as_str())
                        .map(Cow::Borrowed)
                }
                LogFieldSelector::ResourceAttribute(path) => {
                    if path.is_empty() {
                        return None;
                    }
                    let (first, rest) = path.split_first()?;
                    self.resource_attributes
                        .get(first)
                        .and_then(|v| v.traverse(rest))
                        .and_then(|v| v.as_str())
                        .map(Cow::Borrowed)
                }
                LogFieldSelector::ScopeAttribute(path) => {
                    if path.is_empty() {
                        return None;
                    }
                    let (first, rest) = path.split_first()?;
                    self.scope_attributes
                        .get(first)
                        .and_then(|v| v.traverse(rest))
                        .and_then(|v| v.as_str())
                        .map(Cow::Borrowed)
                }
            }
        }
    }

    /// Helper to create a string value.
    fn str_val(s: &str) -> NestedValue {
        NestedValue::String(s.to_string())
    }

    /// Helper to create a map value.
    fn map_val(entries: Vec<(&str, NestedValue)>) -> NestedValue {
        NestedValue::Map(
            entries
                .into_iter()
                .map(|(k, v)| (k.to_string(), v))
                .collect(),
        )
    }

    #[test]
    fn get_simple_field() {
        let log = TestLog {
            body: "test message".to_string(),
            severity_text: "ERROR".to_string(),
            log_attributes: HashMap::new(),
            resource_attributes: HashMap::new(),
        };

        assert_eq!(
            log.get_field(&LogFieldSelector::Simple(LogField::Body)),
            Some(Cow::Borrowed("test message"))
        );
        assert_eq!(
            log.get_field(&LogFieldSelector::Simple(LogField::SeverityText)),
            Some(Cow::Borrowed("ERROR"))
        );
        assert_eq!(
            log.get_field(&LogFieldSelector::Simple(LogField::TraceId)),
            None
        );
    }

    #[test]
    fn get_attribute_field() {
        let mut log_attrs = HashMap::new();
        log_attrs.insert("ddsource".to_string(), "nginx".to_string());

        let mut resource_attrs = HashMap::new();
        resource_attrs.insert("service.name".to_string(), "my-service".to_string());

        let log = TestLog {
            body: "test".to_string(),
            severity_text: "INFO".to_string(),
            log_attributes: log_attrs,
            resource_attributes: resource_attrs,
        };

        assert_eq!(
            log.get_field(&LogFieldSelector::LogAttribute(vec![
                "ddsource".to_string()
            ])),
            Some(Cow::Borrowed("nginx"))
        );
        assert_eq!(
            log.get_field(&LogFieldSelector::ResourceAttribute(vec![
                "service.name".to_string()
            ])),
            Some(Cow::Borrowed("my-service"))
        );
        assert_eq!(
            log.get_field(&LogFieldSelector::LogAttribute(vec!["missing".to_string()])),
            None
        );
    }

    #[test]
    fn get_field_returns_owned_value() {
        struct ComputedLog;

        impl Matchable for ComputedLog {
            type Signal = LogSignal;

            fn get_field(&self, field: &LogFieldSelector) -> Option<Cow<'_, str>> {
                match field {
                    LogFieldSelector::Simple(LogField::Body) => {
                        Some(Cow::Owned("computed value".to_string()))
                    }
                    _ => None,
                }
            }
        }

        let log = ComputedLog;
        let value = log.get_field(&LogFieldSelector::Simple(LogField::Body));
        assert_eq!(value.as_deref(), Some("computed value"));

        if let Some(Cow::Owned(s)) = value {
            assert_eq!(s, "computed value");
        } else {
            panic!("Expected Cow::Owned");
        }
    }

    // ==================== Nested Path Traversal Tests ====================

    #[test]
    fn nested_path_single_level() {
        let log = NestedTestLog::new("test").with_log_attr("user_id", str_val("12345"));

        let result = log.get_field(&LogFieldSelector::LogAttribute(vec!["user_id".to_string()]));
        assert_eq!(result.as_deref(), Some("12345"));
    }

    #[test]
    fn nested_path_two_levels() {
        let log = NestedTestLog::new("test").with_log_attr(
            "http",
            map_val(vec![
                ("method", str_val("GET")),
                ("status_code", str_val("200")),
            ]),
        );

        let result = log.get_field(&LogFieldSelector::LogAttribute(vec![
            "http".to_string(),
            "method".to_string(),
        ]));
        assert_eq!(result.as_deref(), Some("GET"));

        let result = log.get_field(&LogFieldSelector::LogAttribute(vec![
            "http".to_string(),
            "status_code".to_string(),
        ]));
        assert_eq!(result.as_deref(), Some("200"));
    }

    #[test]
    fn nested_path_three_levels() {
        let log = NestedTestLog::new("test").with_log_attr(
            "request",
            map_val(vec![
                (
                    "headers",
                    map_val(vec![
                        ("content_type", str_val("application/json")),
                        ("authorization", str_val("Bearer token123")),
                    ]),
                ),
                ("body", str_val("{\"key\": \"value\"}")),
            ]),
        );

        let result = log.get_field(&LogFieldSelector::LogAttribute(vec![
            "request".to_string(),
            "headers".to_string(),
            "content_type".to_string(),
        ]));
        assert_eq!(result.as_deref(), Some("application/json"));

        let result = log.get_field(&LogFieldSelector::LogAttribute(vec![
            "request".to_string(),
            "headers".to_string(),
            "authorization".to_string(),
        ]));
        assert_eq!(result.as_deref(), Some("Bearer token123"));

        let result = log.get_field(&LogFieldSelector::LogAttribute(vec![
            "request".to_string(),
            "body".to_string(),
        ]));
        assert_eq!(result.as_deref(), Some("{\"key\": \"value\"}"));
    }

    #[test]
    fn nested_path_four_levels() {
        let log = NestedTestLog::new("test").with_log_attr(
            "service",
            map_val(vec![(
                "config",
                map_val(vec![(
                    "database",
                    map_val(vec![
                        ("host", str_val("localhost")),
                        ("port", str_val("5432")),
                    ]),
                )]),
            )]),
        );

        let result = log.get_field(&LogFieldSelector::LogAttribute(vec![
            "service".to_string(),
            "config".to_string(),
            "database".to_string(),
            "host".to_string(),
        ]));
        assert_eq!(result.as_deref(), Some("localhost"));

        let result = log.get_field(&LogFieldSelector::LogAttribute(vec![
            "service".to_string(),
            "config".to_string(),
            "database".to_string(),
            "port".to_string(),
        ]));
        assert_eq!(result.as_deref(), Some("5432"));
    }

    #[test]
    fn nested_path_missing_intermediate_key() {
        let log = NestedTestLog::new("test")
            .with_log_attr("http", map_val(vec![("method", str_val("GET"))]));

        let result = log.get_field(&LogFieldSelector::LogAttribute(vec![
            "http".to_string(),
            "nonexistent".to_string(),
            "field".to_string(),
        ]));
        assert_eq!(result, None);
    }

    #[test]
    fn nested_path_intermediate_is_string_not_map() {
        let log = NestedTestLog::new("test")
            .with_log_attr("http", map_val(vec![("method", str_val("GET"))]));

        let result = log.get_field(&LogFieldSelector::LogAttribute(vec![
            "http".to_string(),
            "method".to_string(),
            "something".to_string(),
        ]));
        assert_eq!(result, None);
    }

    #[test]
    fn nested_path_empty_returns_none() {
        let log = NestedTestLog::new("test").with_log_attr("user_id", str_val("12345"));

        let result = log.get_field(&LogFieldSelector::LogAttribute(vec![]));
        assert_eq!(result, None);
    }

    #[test]
    fn nested_path_resource_attributes() {
        let log = NestedTestLog::new("test").with_resource_attr(
            "k8s",
            map_val(vec![
                (
                    "pod",
                    map_val(vec![
                        ("name", str_val("my-pod-abc123")),
                        ("namespace", str_val("production")),
                    ]),
                ),
                ("cluster", str_val("us-east-1")),
            ]),
        );

        let result = log.get_field(&LogFieldSelector::ResourceAttribute(vec![
            "k8s".to_string(),
            "pod".to_string(),
            "name".to_string(),
        ]));
        assert_eq!(result.as_deref(), Some("my-pod-abc123"));

        let result = log.get_field(&LogFieldSelector::ResourceAttribute(vec![
            "k8s".to_string(),
            "pod".to_string(),
            "namespace".to_string(),
        ]));
        assert_eq!(result.as_deref(), Some("production"));

        let result = log.get_field(&LogFieldSelector::ResourceAttribute(vec![
            "k8s".to_string(),
            "cluster".to_string(),
        ]));
        assert_eq!(result.as_deref(), Some("us-east-1"));
    }

    #[test]
    fn nested_path_scope_attributes() {
        let log = NestedTestLog::new("test").with_scope_attr(
            "instrumentation",
            map_val(vec![
                ("name", str_val("opentelemetry-rust")),
                (
                    "version",
                    map_val(vec![
                        ("major", str_val("0")),
                        ("minor", str_val("21")),
                        ("patch", str_val("0")),
                    ]),
                ),
            ]),
        );

        let result = log.get_field(&LogFieldSelector::ScopeAttribute(vec![
            "instrumentation".to_string(),
            "name".to_string(),
        ]));
        assert_eq!(result.as_deref(), Some("opentelemetry-rust"));

        let result = log.get_field(&LogFieldSelector::ScopeAttribute(vec![
            "instrumentation".to_string(),
            "version".to_string(),
            "minor".to_string(),
        ]));
        assert_eq!(result.as_deref(), Some("21"));
    }

    #[test]
    fn nested_path_top_level_missing() {
        let log = NestedTestLog::new("test");

        let result = log.get_field(&LogFieldSelector::LogAttribute(vec![
            "nonexistent".to_string(),
            "field".to_string(),
        ]));
        assert_eq!(result, None);
    }

    #[test]
    fn nested_path_accessing_map_as_value_returns_none() {
        let log = NestedTestLog::new("test")
            .with_log_attr("http", map_val(vec![("method", str_val("GET"))]));

        let result = log.get_field(&LogFieldSelector::LogAttribute(vec!["http".to_string()]));
        assert_eq!(result, None);
    }
}