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
//! The high-level tenancy contract, what most implementers provide.
use ;
use crateSpiError;
use cratePlacementAt;
use crate;
use crate;
/// The tenancy-focused contract most implementers provide.
///
/// It declares tenancy *rules*, how to find the partition, how to build the
/// document `_id`, which fields to inject, which are sensitive, plus a
/// placement lookup. `osproxy-tenancy` turns this into a [`crate::RoutingSpi`],
/// so tenancy implementers never touch [`crate::RouteDecision`] plumbing
/// (`docs/02` §2).
///
/// # Invariants
///
/// - [`TenancySpi::resolve_partition`] MUST yield a partition id for every
/// routable request, or it returns [`SpiError::PartitionUnresolved`] and the
/// request is rejected.
/// - In `SharedIndex` mode the partition id MUST be part of the constructed
/// `_id` to prevent cross-tenant id collisions (`docs/03`); the adapter
/// enforces this.
/// - [`TenancySpi::injected_fields`] names and [`TenancySpi::sensitive_fields`]
/// MUST be stable for a given logical-index version, so the read-path
/// strip/filter stays symmetric with the write-path inject.
///
/// # Examples
///
/// ```
/// use osproxy_core::{ClusterId, Epoch, FieldName, IndexName, PartitionId};
/// use osproxy_spi::{
/// BodyDoc, InjectedField, InjectedValue, Placement, PlacementAt, PartitionKeySpecKind,
/// RequestCtx, SensitivitySpec, SpiError, TenancySpi,
/// };
///
/// struct OneTenantPerHeader;
///
/// impl TenancySpi for OneTenantPerHeader {
/// fn resolve_partition(&self, ctx: &RequestCtx<'_>, _body: BodyDoc<'_>)
/// -> Result<PartitionId, SpiError>
/// {
/// // Real impls usually defer to `osproxy_tenancy::resolve_partition_spec`;
/// // here we resolve inline to keep the SPI crate self-contained.
/// ctx.headers().get("x-tenant").map(PartitionId::from).ok_or(
/// SpiError::PartitionUnresolved { tried: vec![PartitionKeySpecKind::Header] })
/// }
/// fn doc_id_rule(&self) -> Option<osproxy_spi::DocIdRule> { None }
/// fn injected_fields(&self) -> Vec<InjectedField> {
/// vec![InjectedField::new(FieldName::from("_tenant"), InjectedValue::PartitionId)]
/// }
/// fn sensitive_fields(&self) -> SensitivitySpec { SensitivitySpec::none() }
/// async fn placement_for(&self, p: &PartitionId) -> Result<PlacementAt, SpiError> {
/// Ok(PlacementAt::new(
/// Placement::SharedIndex {
/// cluster: ClusterId::from("eu-1"),
/// index: IndexName::from("logs-shared"),
/// inject: self.injected_fields(),
/// },
/// Epoch::ZERO,
/// ))
/// }
/// }
/// ```