cloud-sdk 0.55.0

no_std-first provider-neutral cloud SDK foundations.
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
//! Versioned plan-confirm identity with exact or strong-digest comparison.

use core::fmt;

use cloud_sdk_sanitization::sanitize_bytes;
use subtle::ConstantTimeEq;

use super::{
    AttemptBudget, PermitContext, PermitIdempotencyKey, PermitScope, PermitValidity, PlanChange,
    PlanCost, PlanFingerprintScope, ReplayPolicy,
};
use crate::buffer::{encode_snapshot_bounded, measure_snapshot_bounded};
use crate::operation::{CostIntent, OperationImpact, PreparedRequest};
use crate::retry::{DigestAlgorithm, FingerprintHasher};
use crate::transport::EndpointIdentity;

mod encoding;
mod error;
use encoding::encode;
pub use error::PlanFingerprintBuildError;
use error::map_infallible;

const DOMAIN: &[u8] = b"cloud-sdk/plan-confirm/v1\0";
/// Maximum complete exact plan-confirm input.
pub const MAX_CANONICAL_PLAN_BYTES: usize = 16_777_216;

/// Complete immutable plan-confirm inputs.
#[derive(Clone, Copy)]
pub struct PlanConfirmation<'a, 'request> {
    prepared: PreparedRequest<'request>,
    endpoint: EndpointIdentity<'a>,
    account: PlanFingerprintScope<'a>,
    tenant: PlanFingerprintScope<'a>,
    context: PermitContext<'a>,
    validity: PermitValidity,
    replay: ReplayPolicy,
    attempts: AttemptBudget,
    change: PlanChange,
    cost: Option<PlanCost>,
    idempotency: Option<PermitIdempotencyKey<'a>>,
}

impl<'a, 'request> PlanConfirmation<'a, 'request> {
    /// Binds caller policy and current plan observations to one prepared request.
    #[allow(clippy::too_many_arguments)]
    #[must_use]
    pub const fn new(
        prepared: PreparedRequest<'request>,
        endpoint: EndpointIdentity<'a>,
        account: PlanFingerprintScope<'a>,
        tenant: PlanFingerprintScope<'a>,
        context: PermitContext<'a>,
        validity: PermitValidity,
        replay: ReplayPolicy,
        attempts: AttemptBudget,
        change: PlanChange,
        cost: Option<PlanCost>,
        idempotency: Option<PermitIdempotencyKey<'a>>,
    ) -> Self {
        Self {
            prepared,
            endpoint,
            account,
            tenant,
            context,
            validity,
            replay,
            attempts,
            change,
            cost,
            idempotency,
        }
    }
}

impl fmt::Debug for PlanConfirmation<'_, '_> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("PlanConfirmation")
            .field("prepared", &self.prepared)
            .field("endpoint", &self.endpoint)
            .field("account", &"[redacted]")
            .field("tenant", &"[redacted]")
            .field("context", &"[redacted]")
            .field("validity", &self.validity)
            .field("replay", &self.replay)
            .field("attempts", &self.attempts)
            .field("change", &self.change)
            .field("cost", &self.cost)
            .field("idempotency", &"[redacted]")
            .finish()
    }
}

/// Caller-buffer exact plan-confirm input, cleared in full on drop.
pub struct CanonicalPlanFingerprint<'output, 'plan, 'request> {
    storage: &'output mut [u8],
    len: usize,
    plan: PlanConfirmation<'plan, 'request>,
    scope: PermitScope,
}

impl<'output, 'plan, 'request> CanonicalPlanFingerprint<'output, 'plan, 'request> {
    /// Returns a redacted exact comparison reference.
    #[must_use]
    pub fn as_ref(&self) -> PlanFingerprintRef<'_> {
        PlanFingerprintRef(PlanFingerprintKind::Exact(self.bytes()))
    }

    /// Returns the exact prepared request and confirmed authority metadata.
    #[must_use]
    pub fn subject(&self) -> PlanSubject<'request, '_> {
        subject(&self.plan, self.scope, self.as_ref())
    }

    /// Returns the complete canonical byte length without exposing bytes.
    #[must_use]
    pub const fn len(&self) -> usize {
        self.len
    }

    /// Reports whether the canonical input is empty. It is never empty.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        false
    }

    fn bytes(&self) -> &[u8] {
        self.storage.get(..self.len).unwrap_or_default()
    }
}

impl fmt::Debug for CanonicalPlanFingerprint<'_, '_, '_> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("CanonicalPlanFingerprint")
            .field("len", &self.len)
            .field("scope", &self.scope)
            .field("bytes", &"[redacted]")
            .finish()
    }
}

impl Drop for CanonicalPlanFingerprint<'_, '_, '_> {
    fn drop(&mut self) {
        sanitize_bytes(self.storage);
        self.len = 0;
    }
}

/// Caller-buffer strong digest of a plan confirmation, cleared on drop.
pub struct PlanFingerprintDigest<'output, 'plan, 'request> {
    algorithm: DigestAlgorithm,
    storage: &'output mut [u8],
    len: usize,
    plan: PlanConfirmation<'plan, 'request>,
    scope: PermitScope,
}

impl PlanFingerprintDigest<'_, '_, '_> {
    /// Returns the admitted collision-resistant digest algorithm.
    #[must_use]
    pub const fn algorithm(&self) -> DigestAlgorithm {
        self.algorithm
    }

    /// Returns a redacted strong-digest comparison reference.
    #[must_use]
    pub fn as_ref(&self) -> PlanFingerprintRef<'_> {
        PlanFingerprintRef(PlanFingerprintKind::Digest {
            algorithm: self.algorithm,
            bytes: self.storage.get(..self.len).unwrap_or_default(),
        })
    }

    /// Returns the exact request and confirmed authority metadata.
    #[must_use]
    pub fn subject(&self) -> PlanSubject<'_, '_> {
        subject(&self.plan, self.scope, self.as_ref())
    }
}

impl fmt::Debug for PlanFingerprintDigest<'_, '_, '_> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("PlanFingerprintDigest")
            .field("algorithm", &self.algorithm)
            .field("scope", &self.scope)
            .field("bytes", &"[redacted]")
            .finish()
    }
}

impl Drop for PlanFingerprintDigest<'_, '_, '_> {
    fn drop(&mut self) {
        sanitize_bytes(self.storage);
        self.len = 0;
    }
}

/// Borrowed exact or strong-digest plan identity.
#[derive(Clone, Copy)]
pub struct PlanFingerprintRef<'a>(PlanFingerprintKind<'a>);

#[derive(Clone, Copy)]
enum PlanFingerprintKind<'a> {
    Exact(&'a [u8]),
    Digest {
        algorithm: DigestAlgorithm,
        bytes: &'a [u8],
    },
}

impl PlanFingerprintRef<'_> {
    pub(crate) fn matches(self, other: Self) -> bool {
        match (self.0, other.0) {
            (PlanFingerprintKind::Exact(left), PlanFingerprintKind::Exact(right)) => {
                constant_time_eq(left, right)
            }
            (
                PlanFingerprintKind::Digest {
                    algorithm: left,
                    bytes: left_bytes,
                },
                PlanFingerprintKind::Digest {
                    algorithm: right,
                    bytes: right_bytes,
                },
            ) => left == right && constant_time_eq(left_bytes, right_bytes),
            _ => false,
        }
    }
}

impl fmt::Debug for PlanFingerprintRef<'_> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("PlanFingerprintRef([redacted])")
    }
}

/// One prepared request inseparably bound to a validated plan confirmation.
#[derive(Clone, Copy)]
pub struct PlanSubject<'request, 'fingerprint> {
    prepared: &'fingerprint PreparedRequest<'request>,
    fingerprint: PlanFingerprintRef<'fingerprint>,
    endpoint: EndpointIdentity<'fingerprint>,
    scope: PermitScope,
    validity: PermitValidity,
    replay: ReplayPolicy,
    attempts: AttemptBudget,
    idempotency: Option<PermitIdempotencyKey<'fingerprint>>,
}

impl<'request, 'fingerprint> PlanSubject<'request, 'fingerprint> {
    /// Returns the required permit scope.
    #[must_use]
    pub const fn scope(self) -> PermitScope {
        self.scope
    }

    /// Returns the caller-selected replay policy.
    #[must_use]
    pub const fn replay_policy(self) -> ReplayPolicy {
        self.replay
    }

    /// Returns the hard attempt budget.
    #[must_use]
    pub const fn attempt_budget(self) -> AttemptBudget {
        self.attempts
    }

    pub(crate) const fn endpoint(self) -> EndpointIdentity<'fingerprint> {
        self.endpoint
    }

    pub(crate) const fn prepared(self) -> PreparedRequest<'request> {
        *self.prepared
    }

    pub(crate) const fn fingerprint(self) -> PlanFingerprintRef<'fingerprint> {
        self.fingerprint
    }

    pub(crate) const fn validity(self) -> PermitValidity {
        self.validity
    }

    pub(crate) const fn idempotency(self) -> Option<PermitIdempotencyKey<'fingerprint>> {
        self.idempotency
    }
}

impl fmt::Debug for PlanSubject<'_, '_> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("PlanSubject")
            .field("prepared", &self.prepared)
            .field("fingerprint", &"[redacted]")
            .field("endpoint", &self.endpoint)
            .field("scope", &self.scope)
            .field("validity", &self.validity)
            .field("replay", &self.replay)
            .field("attempts", &self.attempts)
            .field("idempotency", &"[redacted]")
            .finish()
    }
}

/// Builds exact canonical plan-confirm bytes into caller-owned storage.
pub fn build_canonical_plan<'output, 'plan, 'request>(
    plan: PlanConfirmation<'plan, 'request>,
    output: &'output mut [u8],
) -> Result<
    CanonicalPlanFingerprint<'output, 'plan, 'request>,
    PlanFingerprintBuildError<core::convert::Infallible>,
> {
    sanitize_bytes(output);
    let scope = validate(&plan)?;
    let required = measure_snapshot_bounded(
        &plan,
        MAX_CANONICAL_PLAN_BYTES,
        PlanFingerprintBuildError::InputTooLarge,
        encode,
    )?;
    if output.len() < required {
        return Err(PlanFingerprintBuildError::OutputTooSmall);
    }
    let len = encode_snapshot_bounded(
        &plan,
        output,
        MAX_CANONICAL_PLAN_BYTES,
        PlanFingerprintBuildError::InputTooLarge,
        encode,
    )?;
    Ok(CanonicalPlanFingerprint {
        storage: output,
        len,
        plan,
        scope,
    })
}

/// Builds a caller-selected collision-resistant digest and clears scratch.
pub fn build_plan_digest<'output, 'plan, 'request, H: FingerprintHasher>(
    plan: PlanConfirmation<'plan, 'request>,
    scratch: &mut [u8],
    output: &'output mut [u8],
    hasher: &H,
) -> Result<PlanFingerprintDigest<'output, 'plan, 'request>, PlanFingerprintBuildError<H::Error>> {
    sanitize_bytes(output);
    let exact = build_canonical_plan(plan, scratch).map_err(map_infallible)?;
    let algorithm = hasher.algorithm();
    let expected = algorithm.output_len();
    if output.len() < expected {
        return Err(PlanFingerprintBuildError::OutputTooSmall);
    }
    let mut rollback = DigestRollback::new(output);
    let len = hasher
        .digest(exact.bytes(), rollback.target(expected))
        .map_err(PlanFingerprintBuildError::Hasher)?;
    if len != expected {
        return Err(PlanFingerprintBuildError::InvalidDigestLength);
    }
    let output = rollback.disarm();
    Ok(PlanFingerprintDigest {
        algorithm,
        storage: output,
        len,
        plan,
        scope: exact.scope,
    })
}

struct DigestRollback<'a> {
    output: &'a mut [u8],
    armed: bool,
}

impl<'a> DigestRollback<'a> {
    fn new(output: &'a mut [u8]) -> Self {
        Self {
            output,
            armed: true,
        }
    }

    fn target(&mut self, len: usize) -> &mut [u8] {
        self.output.get_mut(..len).unwrap_or_default()
    }

    fn disarm(mut self) -> &'a mut [u8] {
        self.armed = false;
        core::mem::take(&mut self.output)
    }
}

impl Drop for DigestRollback<'_> {
    fn drop(&mut self) {
        if self.armed {
            sanitize_bytes(self.output);
        }
    }
}

pub(super) fn validate<E>(
    plan: &PlanConfirmation<'_, '_>,
) -> Result<PermitScope, PlanFingerprintBuildError<E>> {
    if plan.prepared.operation_id().is_none() {
        return Err(PlanFingerprintBuildError::MissingOperationId);
    }
    if !plan
        .prepared
        .service()
        .endpoint_policy()
        .admits(plan.endpoint)
    {
        return Err(PlanFingerprintBuildError::EndpointNotAdmitted);
    }
    if plan.change == PlanChange::NoOp {
        return Err(PlanFingerprintBuildError::NoOp);
    }
    plan.account
        .bytes()
        .map_err(PlanFingerprintBuildError::Context)?;
    plan.tenant
        .bytes()
        .map_err(PlanFingerprintBuildError::Context)?;
    let metadata = plan.prepared.metadata();
    let scope = match (metadata.cost_intent(), metadata.impact()) {
        (CostIntent::MayIncurCost, _) => PermitScope::Cost,
        (_, OperationImpact::Destructive) => PermitScope::Destructive,
        (_, OperationImpact::Mutation) => PermitScope::Mutation,
        (_, OperationImpact::ReadOnly) => return Err(PlanFingerprintBuildError::ReadOnlyOperation),
    };
    match (scope, plan.cost) {
        (PermitScope::Cost, None) => return Err(PlanFingerprintBuildError::MissingCost),
        (PermitScope::Mutation | PermitScope::Destructive, Some(_)) => {
            return Err(PlanFingerprintBuildError::UnexpectedCost);
        }
        _ => {}
    }
    match (plan.replay, plan.attempts.get(), plan.idempotency) {
        (ReplayPolicy::SingleAttempt, 1, None)
        | (ReplayPolicy::RecoverNotSent, _, None)
        | (ReplayPolicy::ReconcileThenRetry, _, Some(_)) => {}
        (ReplayPolicy::SingleAttempt, _, _) => {
            return Err(PlanFingerprintBuildError::InvalidSingleAttemptBudget);
        }
        (ReplayPolicy::ReconcileThenRetry, _, None) => {
            return Err(PlanFingerprintBuildError::MissingIdempotency);
        }
        (_, _, Some(_)) => return Err(PlanFingerprintBuildError::UnexpectedIdempotency),
    }
    Ok(scope)
}

fn constant_time_eq(left: &[u8], right: &[u8]) -> bool {
    left.len() == right.len() && bool::from(left.ct_eq(right))
}

fn subject<'request, 'plan: 'fingerprint, 'fingerprint>(
    plan: &'fingerprint PlanConfirmation<'plan, 'request>,
    scope: PermitScope,
    fingerprint: PlanFingerprintRef<'fingerprint>,
) -> PlanSubject<'request, 'fingerprint> {
    PlanSubject {
        prepared: &plan.prepared,
        fingerprint,
        endpoint: plan.endpoint,
        scope,
        validity: plan.validity,
        replay: plan.replay,
        attempts: plan.attempts,
        idempotency: plan.idempotency,
    }
}