lenso-platform-system-plane 0.2.1

Capability-neutral System Plane routing and negotiation for Lenso Services.
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
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
//! Capability-neutral System Plane Core routing, registration, and negotiation.

mod enrollment;
mod module_operations;

pub use enrollment::*;
pub use module_operations::*;

use axum::{
    Extension, Json,
    extract::FromRequestParts,
    http::{HeaderMap, HeaderValue, StatusCode, header},
    response::{IntoResponse, Response},
};
use lenso_service::{
    AuthenticatedTransportBinding, WorkloadIdentityProvider, WorkloadIdentityVerification,
    system_plane::{
        CORE_PROTOCOL, CapabilityAdvertisement, CoreDocument, CoreIssue, validate_core_document,
    },
};
use serde::Serialize;
use std::{
    collections::{BTreeSet, HashSet},
    fmt,
    sync::Arc,
    time::{SystemTime, UNIX_EPOCH},
};
use utoipa::ToSchema;
use utoipa_axum::{router::OpenApiRouter, routes};

#[derive(Debug, Clone)]
pub struct SystemPlaneRegistry {
    document: Arc<CoreDocument>,
}

impl SystemPlaneRegistry {
    pub fn new(document: CoreDocument) -> Result<Self, Vec<CoreIssue>> {
        let issues = validate_core_document(&document);
        if issues.is_empty() {
            Ok(Self {
                document: Arc::new(document),
            })
        } else {
            Err(issues)
        }
    }

    #[must_use]
    pub fn document(&self) -> &CoreDocument {
        &self.document
    }

    #[must_use]
    pub fn negotiate(&self, requirements: &[CapabilityRequirement]) -> CapabilityNegotiation {
        negotiate_capabilities(self.document(), requirements)
    }
}

#[derive(Debug, Clone)]
pub struct SystemPlaneRegistryBuilder {
    document: CoreDocument,
}

impl SystemPlaneRegistryBuilder {
    #[must_use]
    pub fn new(
        service_id: impl Into<String>,
        service_principal: impl Into<String>,
        service_revision: impl Into<String>,
    ) -> Self {
        Self {
            document: CoreDocument {
                protocol: CORE_PROTOCOL.to_owned(),
                service_id: service_id.into(),
                service_principal: service_principal.into(),
                service_revision: service_revision.into(),
                capabilities: Vec::new(),
            },
        }
    }

    #[must_use]
    pub fn register(mut self, capability: CapabilityAdvertisement) -> Self {
        self.document.capabilities.push(capability);
        self
    }

    pub fn build(mut self) -> Result<SystemPlaneRegistry, Vec<CoreIssue>> {
        self.document
            .capabilities
            .sort_by(|left, right| left.contract_id.cmp(&right.contract_id));
        SystemPlaneRegistry::new(self.document)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CapabilityRequirement {
    pub capability_id: String,
    pub supported_major_versions: BTreeSet<u32>,
    pub required_feature_ids: BTreeSet<String>,
    pub accepted_schema_digests: BTreeSet<String>,
}

impl CapabilityRequirement {
    #[must_use]
    pub fn new(
        capability_id: impl Into<String>,
        supported_major_versions: impl IntoIterator<Item = u32>,
    ) -> Self {
        Self {
            capability_id: capability_id.into(),
            supported_major_versions: supported_major_versions.into_iter().collect(),
            required_feature_ids: BTreeSet::new(),
            accepted_schema_digests: BTreeSet::new(),
        }
    }

    #[must_use]
    pub fn requiring_features(
        mut self,
        feature_ids: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.required_feature_ids = feature_ids.into_iter().map(Into::into).collect();
        self
    }

    #[must_use]
    pub fn accepting_schema_digests(
        mut self,
        digests: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.accepted_schema_digests = digests.into_iter().map(Into::into).collect();
        self
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum CapabilityNegotiationIssueCode {
    InvalidRequirement,
    DuplicateRequirement,
    MissingCapability,
    UnsupportedMajorVersion,
    MissingRequiredFeature,
    SchemaDigestMismatch,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CapabilityNegotiationIssue {
    pub code: CapabilityNegotiationIssueCode,
    pub capability_id: String,
    pub message: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NegotiatedCapability {
    pub capability_id: String,
    pub advertisement: CapabilityAdvertisement,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CapabilityNegotiation {
    pub accepted: Vec<NegotiatedCapability>,
    pub issues: Vec<CapabilityNegotiationIssue>,
}

impl CapabilityNegotiation {
    #[must_use]
    pub const fn is_compatible(&self) -> bool {
        self.issues.is_empty()
    }
}

#[must_use]
pub fn negotiate_capabilities(
    document: &CoreDocument,
    requirements: &[CapabilityRequirement],
) -> CapabilityNegotiation {
    let mut accepted = Vec::new();
    let mut issues = Vec::new();
    let mut seen = HashSet::new();

    for requirement in requirements {
        if !valid_capability_id(&requirement.capability_id)
            || requirement.supported_major_versions.is_empty()
            || requirement.supported_major_versions.contains(&0)
        {
            negotiation_issue(
                &mut issues,
                CapabilityNegotiationIssueCode::InvalidRequirement,
                requirement,
                "Capability requirements need a canonical identifier and at least one positive major version",
            );
            continue;
        }
        if !seen.insert(requirement.capability_id.as_str()) {
            negotiation_issue(
                &mut issues,
                CapabilityNegotiationIssueCode::DuplicateRequirement,
                requirement,
                "Each capability may be negotiated once",
            );
            continue;
        }

        let prefix = format!("lenso.system-plane.{}.v", requirement.capability_id);
        let candidates = document
            .capabilities
            .iter()
            .filter(|capability| {
                capability.contract_id.starts_with(&prefix)
                    && capability.contract_id == format!("{}{}", prefix, capability.major_version)
            })
            .collect::<Vec<_>>();
        if candidates.is_empty() {
            negotiation_issue(
                &mut issues,
                CapabilityNegotiationIssueCode::MissingCapability,
                requirement,
                "The managed Service does not advertise this capability",
            );
            continue;
        }
        let candidate = candidates
            .into_iter()
            .filter(|capability| {
                requirement
                    .supported_major_versions
                    .contains(&capability.major_version)
            })
            .max_by_key(|capability| capability.major_version);
        let Some(candidate) = candidate else {
            negotiation_issue(
                &mut issues,
                CapabilityNegotiationIssueCode::UnsupportedMajorVersion,
                requirement,
                "The managed Service and consumer share no supported major version",
            );
            continue;
        };
        if !requirement
            .required_feature_ids
            .is_subset(&candidate.feature_ids)
        {
            negotiation_issue(
                &mut issues,
                CapabilityNegotiationIssueCode::MissingRequiredFeature,
                requirement,
                "The advertised contract is missing a required feature identifier",
            );
            continue;
        }
        if !requirement.accepted_schema_digests.is_empty()
            && !requirement
                .accepted_schema_digests
                .contains(&candidate.schema_digest)
        {
            negotiation_issue(
                &mut issues,
                CapabilityNegotiationIssueCode::SchemaDigestMismatch,
                requirement,
                "The advertised schema digest is not accepted by the consumer",
            );
            continue;
        }
        accepted.push(NegotiatedCapability {
            capability_id: requirement.capability_id.clone(),
            advertisement: candidate.clone(),
        });
    }

    accepted.sort_by(|left, right| left.capability_id.cmp(&right.capability_id));
    CapabilityNegotiation { accepted, issues }
}

fn negotiation_issue(
    issues: &mut Vec<CapabilityNegotiationIssue>,
    code: CapabilityNegotiationIssueCode,
    requirement: &CapabilityRequirement,
    message: &str,
) {
    issues.push(CapabilityNegotiationIssue {
        code,
        capability_id: requirement.capability_id.clone(),
        message: message.to_owned(),
    });
}

fn valid_capability_id(value: &str) -> bool {
    !value.is_empty()
        && value.split('.').all(|segment| {
            !segment.is_empty()
                && !segment.starts_with('-')
                && !segment.ends_with('-')
                && !segment.contains("--")
                && segment
                    .bytes()
                    .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'-')
        })
}

#[derive(Clone)]
pub struct SystemPlaneAccess {
    provider: Arc<dyn WorkloadIdentityProvider>,
    audience: String,
    enrollment_authorizer: Arc<dyn EnrollmentAuthorizer>,
}

impl fmt::Debug for SystemPlaneAccess {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("SystemPlaneAccess")
            .field("provider", &self.provider)
            .field("audience", &self.audience)
            .field("enrollment_authorizer", &self.enrollment_authorizer)
            .finish()
    }
}

impl SystemPlaneAccess {
    #[must_use]
    pub fn new(
        provider: Arc<dyn WorkloadIdentityProvider>,
        audience: impl Into<String>,
        enrollment_authorizer: Arc<dyn EnrollmentAuthorizer>,
    ) -> Self {
        Self {
            provider,
            audience: audience.into(),
            enrollment_authorizer,
        }
    }
}

#[derive(Debug, Clone)]
pub struct SystemPlaneRuntime {
    pub registry: Arc<SystemPlaneRegistry>,
    pub access: Arc<SystemPlaneAccess>,
    pub module_operations: Option<Arc<ModuleOperationsProvider>>,
}

impl SystemPlaneRuntime {
    #[must_use]
    pub fn new(registry: SystemPlaneRegistry, access: SystemPlaneAccess) -> Self {
        Self {
            registry: Arc::new(registry),
            access: Arc::new(access),
            module_operations: None,
        }
    }

    #[must_use]
    pub fn with_module_operations(mut self, provider: ModuleOperationsProvider) -> Self {
        self.module_operations = Some(Arc::new(provider));
        self
    }
}

#[derive(Debug, Serialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct SystemPlaneErrorBody {
    pub code: &'static str,
    pub message: String,
    pub next_actions: Vec<&'static str>,
}

#[derive(Debug)]
pub struct SystemPlaneRejection {
    status: StatusCode,
    code: &'static str,
    message: String,
    next_action: &'static str,
}

impl SystemPlaneRejection {
    #[must_use]
    pub fn new(
        status: StatusCode,
        code: &'static str,
        message: impl Into<String>,
        next_action: &'static str,
    ) -> Self {
        Self {
            status,
            code,
            message: message.into(),
            next_action,
        }
    }

    #[must_use]
    pub fn unavailable(
        code: &'static str,
        message: impl Into<String>,
        next_action: &'static str,
    ) -> Self {
        Self {
            status: StatusCode::SERVICE_UNAVAILABLE,
            code,
            message: message.into(),
            next_action,
        }
    }

    #[must_use]
    pub const fn code(&self) -> &'static str {
        self.code
    }
}

impl IntoResponse for SystemPlaneRejection {
    fn into_response(self) -> Response {
        let body = SystemPlaneErrorBody {
            code: self.code,
            message: self.message,
            next_actions: vec![self.next_action],
        };
        let mut response = (self.status, Json(body)).into_response();
        response.headers_mut().insert(
            header::CONTENT_TYPE,
            HeaderValue::from_static("application/problem+json"),
        );
        response
            .headers_mut()
            .insert("x-lenso-error-code", HeaderValue::from_static(self.code));
        response
    }
}

#[derive(Debug, Clone)]
pub struct AuthorizedSystemPlaneCaller {
    pub runtime: Arc<SystemPlaneRuntime>,
    pub service_principal: String,
    pub enrollment: EnrollmentAuthorization,
}

impl AuthorizedSystemPlaneCaller {
    pub fn require_capability(
        &self,
        contract_id: &str,
        schema_digest: &str,
        required_feature_ids: impl IntoIterator<Item = impl AsRef<str>>,
    ) -> Result<(), SystemPlaneRejection> {
        if self.enrollment.system_id == "system-sandbox" {
            return Ok(());
        }
        let required_feature_ids = required_feature_ids
            .into_iter()
            .map(|feature| feature.as_ref().to_owned())
            .collect::<BTreeSet<_>>();
        let granted = self.enrollment.capabilities.iter().any(|capability| {
            capability.contract_id == contract_id
                && capability.schema_digest == schema_digest
                && required_feature_ids.is_subset(&capability.feature_ids)
        });
        if granted {
            Ok(())
        } else {
            Err(SystemPlaneRejection {
                status: StatusCode::FORBIDDEN,
                code: "system_plane_capability_not_granted",
                message: "Active Enrollment Grant does not authorize the requested capability"
                    .to_owned(),
                next_action: "review_service_enrollment_grant",
            })
        }
    }
}

impl<S> FromRequestParts<S> for AuthorizedSystemPlaneCaller
where
    S: Send + Sync,
{
    type Rejection = SystemPlaneRejection;

    async fn from_request_parts(
        parts: &mut axum::http::request::Parts,
        _state: &S,
    ) -> Result<Self, Self::Rejection> {
        let runtime = parts
            .extensions
            .get::<Option<Arc<SystemPlaneRuntime>>>()
            .and_then(Clone::clone)
            .ok_or_else(|| SystemPlaneRejection {
                status: StatusCode::SERVICE_UNAVAILABLE,
                code: "system_plane_unavailable",
                message: "System Plane access is not configured for this Service".to_owned(),
                next_action: "configure_system_plane",
            })?;
        let token = bearer_token(&parts.headers)?;
        let binding = parts
            .extensions
            .get::<AuthenticatedTransportBinding>()
            .ok_or_else(|| SystemPlaneRejection {
                status: StatusCode::UNAUTHORIZED,
                code: "system_plane_transport_binding_required",
                message: "System Plane access requires an authenticated transport binding"
                    .to_owned(),
                next_action: "use_authenticated_transport",
            })?;
        let principal = runtime
            .access
            .provider
            .verify(
                token,
                &WorkloadIdentityVerification::new(
                    &runtime.access.audience,
                    &binding.0,
                    now_unix_ms(),
                ),
            )
            .map_err(|error| SystemPlaneRejection {
                status: StatusCode::UNAUTHORIZED,
                code: "system_plane_workload_identity_rejected",
                message: error.message,
                next_action: "refresh_workload_identity",
            })?;
        let enrollment = runtime
            .access
            .enrollment_authorizer
            .authorize(
                &runtime.registry.document().service_id,
                &principal.service_principal,
                now_unix_ms(),
            )
            .await
            .map_err(enrollment_rejection)?;
        Ok(Self {
            runtime,
            service_principal: principal.service_principal,
            enrollment,
        })
    }
}

/// Builds the mandatory Core discovery route. An absent runtime fails closed.
#[must_use]
pub fn router<S>(runtime: Option<Arc<SystemPlaneRuntime>>) -> OpenApiRouter<S>
where
    S: Clone + Send + Sync + 'static,
{
    OpenApiRouter::new()
        .routes(routes!(discover_core))
        .layer(Extension(runtime))
}

#[utoipa::path(
    get,
    path = "/system-plane/v1",
    responses(
        (status = 200, description = "Authenticated System Plane Core document", body = CoreDocument),
        (status = 401, description = "Workload Identity or transport binding was not accepted", body = SystemPlaneErrorBody, content_type = "application/problem+json"),
        (status = 403, description = "Caller is not the enrolled Console Service Principal", body = SystemPlaneErrorBody, content_type = "application/problem+json"),
        (status = 503, description = "System Plane access is not configured", body = SystemPlaneErrorBody, content_type = "application/problem+json")
    ),
    security(("bearer_auth" = [])),
    tag = "system-plane"
)]
async fn discover_core(caller: AuthorizedSystemPlaneCaller) -> Json<CoreDocument> {
    Json(caller.runtime.registry.document().clone())
}

fn bearer_token(headers: &HeaderMap) -> Result<&str, SystemPlaneRejection> {
    headers
        .get(header::AUTHORIZATION)
        .and_then(|value| value.to_str().ok())
        .and_then(|value| value.strip_prefix("Bearer "))
        .filter(|value| !value.is_empty())
        .ok_or_else(|| SystemPlaneRejection {
            status: StatusCode::UNAUTHORIZED,
            code: "system_plane_workload_identity_required",
            message: "System Plane access requires a Workload Identity Bearer credential"
                .to_owned(),
            next_action: "provide_workload_identity",
        })
}

fn enrollment_rejection(error: EnrollmentError) -> SystemPlaneRejection {
    let (status, code, next_action) = match error.code {
        EnrollmentErrorCode::StoreUnavailable => (
            StatusCode::SERVICE_UNAVAILABLE,
            "system_plane_enrollment_unavailable",
            "restore_enrollment_store",
        ),
        EnrollmentErrorCode::Expired => (
            StatusCode::FORBIDDEN,
            "system_plane_enrollment_expired",
            "renew_service_enrollment",
        ),
        EnrollmentErrorCode::Revoked => (
            StatusCode::FORBIDDEN,
            "system_plane_enrollment_revoked",
            "complete_service_enrollment",
        ),
        EnrollmentErrorCode::PrincipalMismatch => (
            StatusCode::FORBIDDEN,
            "system_plane_console_not_enrolled",
            "complete_service_enrollment",
        ),
        EnrollmentErrorCode::NotEnrolled
        | EnrollmentErrorCode::InvalidGrant
        | EnrollmentErrorCode::InvalidDecision
        | EnrollmentErrorCode::SignatureRejected
        | EnrollmentErrorCode::NonceReused
        | EnrollmentErrorCode::AlreadyEnrolled
        | EnrollmentErrorCode::StaleAuthorizationEpoch => (
            StatusCode::FORBIDDEN,
            "system_plane_enrollment_required",
            "complete_service_enrollment",
        ),
    };
    SystemPlaneRejection {
        status,
        code,
        message: error.message,
        next_action,
    }
}

fn now_unix_ms() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_or(0, |duration| {
            u64::try_from(duration.as_millis()).unwrap_or(u64::MAX)
        })
}