ic-blob-storage-cli 0.117.0

Blob storage inspection, offline preparation and guarded maintenance CLI for the Internet Computer
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
//! Immutable maintenance-request custody and guarded dispatch; no renewal or resubmission.

mod activate;
mod start;
pub(crate) mod status;
mod stop;
mod upgrade;

use super::{Context, read};
use crate::{files, overview::hex};
use ic_blob_storage::protocol::{Command, Installation, Outcome};
use ic_blob_storage_wire::{Contract, Update};
use ic_host_fs::{Directory, FilePolicy};
use ic_platform::{
    ingress::{Content, decode_envelope},
    native::RequestStatus,
};
use ic_platform_types::Principal;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::{
    path::Path,
    process::Stdio,
    time::{SystemTime, UNIX_EPOCH},
};

const LIMITS: usize = 16 * 1024;
const INTENT_PREFIX: [u8; 16] = *b"blob-management\x01";

#[derive(Clone, Copy)]
pub(super) enum Operation<'a> {
    Drain,
    Stop,
    Upgrade {
        wasm: &'a [u8],
        stop_record: &'a Path,
    },
    Start {
        wasm: &'a [u8],
        stop_record: &'a Path,
        upgrade_record: &'a Path,
    },
    Activate {
        wasm: &'a [u8],
        stop_record: &'a Path,
        upgrade_record: &'a Path,
        start_record: &'a Path,
    },
}

impl<'a> Operation<'a> {
    fn name(self) -> &'static str {
        match self {
            Self::Drain => "drain",
            Self::Stop => "stop",
            Self::Upgrade { .. } => "upgrade",
            Self::Start { .. } => "start",
            Self::Activate { .. } => "activate",
        }
    }

    fn recipient(self, context: &Context) -> Principal {
        match self {
            Self::Drain | Self::Activate { .. } => context.canister,
            Self::Stop | Self::Upgrade { .. } | Self::Start { .. } => {
                Principal::management_canister()
            }
        }
    }

    fn method(self) -> &'static str {
        match self {
            Self::Drain | Self::Activate { .. } => Update::NAME,
            Self::Stop => "stop_canister",
            Self::Upgrade { .. } => "install_code",
            Self::Start { .. } => "start_canister",
        }
    }

    fn argument(self, context: &Context, attempt: u64) -> Result<Vec<u8>, String> {
        match self {
            Self::Drain => argument(&Command::Drain(attempt)),
            Self::Activate { .. } => argument(&Command::Activate(attempt)),
            Self::Stop | Self::Start { .. } => stop::argument(context),
            Self::Upgrade { wasm, .. } => upgrade::argument(context, wasm),
        }
    }

    async fn admit(self, context: &Context, attempt: u64) -> Result<(), String> {
        if let Self::Activate { wasm, .. } = self {
            let original = self
                .predecessor(context, attempt)?
                .expect("start predecessor");
            // Observe the original start and recheck the currently drained host.
            // The service checks manager and exact attempt again when activation executes.
            start::observe(
                context,
                &original.id,
                attempt,
                request_status(context, &original).await?,
                wasm,
            )
            .await?;

            return Ok(());
        }
        if let Self::Upgrade { stop_record, .. } = self {
            return upgrade::admit(context, stop_record, attempt).await;
        }
        if let Self::Start {
            wasm,
            stop_record,
            upgrade_record,
        } = self
        {
            return start::admit(context, stop_record, upgrade_record, attempt, wasm).await;
        }
        let view = context.inspect().await?;

        match self {
            Self::Drain
                if view.maintenance.active
                    && view.maintenance.attempt.checked_add(1) == Some(attempt) =>
            {
                Ok(())
            }
            Self::Stop if !view.maintenance.active && view.maintenance.attempt == attempt => {
                stop::admit(context).await
            }
            _ => Err(format!(
                "current admission does not permit this {} attempt",
                self.name()
            )),
        }
    }

    fn marker(self, attempt: u64) -> String {
        match self {
            // Preserve the 0.100.3 claim namespace and intent format.
            Self::Drain => format!("attempt-{attempt}.claim"),
            Self::Stop => format!("attempt-{attempt}.stop.claim"),
            Self::Upgrade { .. } => format!("attempt-{attempt}.upgrade.claim"),
            Self::Start { .. } => format!("attempt-{attempt}.start.claim"),
            Self::Activate { .. } => format!("attempt-{attempt}.activate.claim"),
        }
    }

    fn packet_limit(self) -> usize {
        match self {
            Self::Upgrade { .. } => 2 * 1024 * 1024,
            _ => LIMITS,
        }
    }

    fn predecessor(self, context: &Context, attempt: u64) -> Result<Option<Original>, String> {
        if let Some((operation, record)) = self.parent() {
            let original = load(context, record, operation)?;
            if original.attempt != attempt {
                return Err("predecessor attempt mismatch".into());
            }

            return Ok(Some(original));
        }

        Ok(None)
    }

    fn parent(self) -> Option<(Self, &'a Path)> {
        match self {
            Self::Activate {
                wasm,
                stop_record,
                upgrade_record,
                start_record,
            } => Some((
                Self::Start {
                    wasm,
                    stop_record,
                    upgrade_record,
                },
                start_record,
            )),
            Self::Upgrade { stop_record, .. } => Some((Self::Stop, stop_record)),
            Self::Start {
                wasm,
                stop_record,
                upgrade_record,
            } => Some((Self::Upgrade { wasm, stop_record }, upgrade_record)),
            _ => None,
        }
    }

    fn admit_custody(
        self,
        context: &Context,
        attempt: u64,
        target: &Directory,
    ) -> Result<(), String> {
        // The closed operation set bounds this chain: activate -> start -> upgrade -> stop.
        if let Some((operation, record)) = self.parent() {
            let previous = load(context, record, operation)?;
            if previous.attempt != attempt
                || files::read_at(target, operation.marker(attempt).as_ref(), 32)?
                    != previous.id.to_vec()
            {
                return Err(format!(
                    "{} dispatch claim does not match predecessor",
                    operation.name()
                ));
            }
            operation.admit_custody(context, attempt, target)?;
        }

        Ok(())
    }
}

#[derive(Serialize, Deserialize)]
struct Intent {
    format: [u8; INTENT_PREFIX.len()],
    url: String,
    canister: Principal,
    configuration: Installation,
    wasm_hash: [u8; 32],
    root_hash: [u8; 32],
    state_version: u32,
    attempt: u64,
    packet_hash: [u8; 32],
}

fn argument(command: &Command) -> Result<Vec<u8>, String> {
    Update::encode_request(command).map_err(|_| "cannot encode admission command".into())
}

pub(super) async fn prepare(
    context: &Context,
    directory: &Path,
    attempt: u64,
    operation: Operation<'_>,
) -> Result<String, String> {
    operation.admit(context, attempt).await?;
    let recipient = operation.recipient(context);
    let signed = context.client.prepare(
        recipient,
        context.canister,
        operation.method(),
        &operation.argument(context, attempt)?,
    )?;
    let intent = Intent {
        format: INTENT_PREFIX,
        url: context.url.clone(),
        canister: context.canister,
        configuration: context.configuration,
        wasm_hash: context.wasm_hash,
        root_hash: context.root_hash,
        state_version: context.state_version,
        attempt,
        packet_hash: Sha256::digest(signed.envelope_bytes()).into(),
    };
    let bytes = ic_blob_storage::storage_codec::encode(&intent, LIMITS)
        .map_err(|_| "cannot encode management intent")?;
    if signed.envelope_bytes().len() > operation.packet_limit() {
        return Err("signed management request exceeds record limit".into());
    }
    // A partial directory remains claimed. No automatic cleanup or repair on any
    // failure, including a sync failure after publication. Parent custody is caller-owned.
    let held = files::claim(directory).map_err(|error| {
        format!("cannot claim management directory; preserve any existing claim: {error}")
    })?;
    files::publish(&held, "intent.wire".as_ref(), &bytes).map_err(|error| {
        format!("management intent publication failed; preserve claim: {error}")
    })?;
    if let Some(previous) = operation.predecessor(context, attempt)? {
        files::publish(&held, "predecessor.id".as_ref(), &previous.id)
            .map_err(|_| "predecessor publication failed; preserve claim")?;
    }
    files::publish(&held, "request.cbor".as_ref(), signed.envelope_bytes()).map_err(|error| {
        format!("signed management request publication failed; preserve claim: {error}")
    })?;

    Ok(format!(
        "Prepared {}: request_id={} attempt={attempt}; NOT submitted. Preserve the original claim; no renewal or retry is authorized.",
        operation.name(),
        hex(signed.request_id())
    ))
}

struct Original {
    attempt: u64,
    packet: Vec<u8>,
    id: [u8; 32],
    expires_at: u64,
}

fn load(context: &Context, directory: &Path, operation: Operation<'_>) -> Result<Original, String> {
    let held = files::directory(directory, false)?;
    let intent: Intent = ic_blob_storage::storage_codec::decode_with_prefix(
        &files::read_at(&held, "intent.wire".as_ref(), LIMITS)?,
        LIMITS,
        &INTENT_PREFIX,
    )
    .map_err(|_| "invalid management intent")?;
    let packet = files::read_at(&held, "request.cbor".as_ref(), operation.packet_limit())?;
    let hash: [u8; 32] = Sha256::digest(&packet).into();
    if intent.url != context.url
        || intent.canister != context.canister
        || intent.configuration != context.configuration
        || intent.root_hash != context.root_hash
        || intent.wasm_hash != context.wasm_hash
        || intent.state_version != context.state_version
        || intent.attempt == 0
        || intent.packet_hash != hash
    {
        return Err("management claim binding mismatch".into());
    }
    // Strictly inspect correspondence, not signature authenticity. The authenticated
    // read_state result below establishes the original request's observed outcome.
    if let Some(previous) = operation.predecessor(context, intent.attempt)?
        && files::read_at(&held, "predecessor.id".as_ref(), 32)? != previous.id.to_vec()
    {
        return Err("predecessor identity mismatch".into());
    }
    let content = decode_envelope(&packet, operation.packet_limit())
        .map_err(|_| "invalid signed management content")?
        .value
        .content;
    let Content::Call {
        sender,
        canister_id,
        method_name,
        arg,
        sender_info,
        ingress_expiry,
        ..
    } = &content
    else {
        return Err("management claim is not a call".into());
    };
    if *sender != context.configuration.manager
        || *canister_id != operation.recipient(context)
        || method_name != operation.method()
        || *arg != operation.argument(context, intent.attempt)?
        || sender_info.is_some()
    {
        return Err("signed management fields do not match intent".into());
    }
    // Derive the request ID from original content; never regenerate a fresh signed call.
    // Expired requests remain inspectable until the network forgets their outcomes.
    let id = content
        .request_id()
        .map_err(|_| "cannot derive original request ID")?;

    Ok(Original {
        attempt: intent.attempt,
        packet,
        id,
        expires_at: *ingress_expiry,
    })
}

pub(super) async fn observe(
    context: &Context,
    directory: &Path,
    operation: Operation<'_>,
) -> Result<String, String> {
    let original = load(context, directory, operation)?;
    let status = request_status(context, &original).await?;
    if let Operation::Activate { wasm, .. } = operation {
        return activate::observe(context, &original.id, original.attempt, status, wasm).await;
    }
    if let Operation::Start { wasm, .. } = operation {
        return start::observe(context, &original.id, original.attempt, status, wasm).await;
    }
    if let Operation::Upgrade { wasm, .. } = operation {
        return upgrade::observe(context, &original.id, status, wasm).await;
    }
    if matches!(operation, Operation::Stop) {
        return stop::observe(context, &original.id, status).await;
    }

    match status {
        RequestStatus::Replied(reply) => acknowledge(&original.id, original.attempt, &reply, false),
        RequestStatus::Rejected { .. } => {
            Err("original drain was rejected; no retry authorized".into())
        }
        RequestStatus::Unknown | RequestStatus::Unavailable | RequestStatus::Done => {
            Err("original drain outcome unavailable; keep unresolved, no resubmission".into())
        }
        RequestStatus::Received | RequestStatus::Processing => {
            Err("original drain remains pending; no resubmission".into())
        }
    }
}

async fn request_status(context: &Context, original: &Original) -> Result<RequestStatus, String> {
    let prepared = ic_platform::native::PreparedUpdate::from_bytes(
        &original.packet,
        context.canister,
        crate::network::MESSAGE_BYTES,
    )
    .map_err(|_| "invalid original signed request")?;

    Ok(context.client.observe(&prepared).await?.status().clone())
}

fn acknowledge(id: &[u8; 32], attempt: u64, bytes: &[u8], active: bool) -> Result<String, String> {
    let operation = if active { "activation" } else { "drain" };
    let result = Update::decode_response(bytes)
        .map_err(|_| format!("invalid original {operation} reply"))?;

    match result {
        Ok(Outcome::Maintenance(state)) if state.attempt == attempt && state.active == active => {
            Ok(format!(
                "Original {operation} acknowledged: request_id={display_id} attempt={}; historical outcome only, not current admission or activation approval.",
                attempt,
                display_id = hex(id)
            ))
        }
        _ => Err(format!(
            "original {operation} did not acknowledge the requested admission state"
        )),
    }
}

pub(super) async fn admit_tooling(checkout: &Path) -> Result<(), String> {
    let snapshot = read(&checkout.join(".toko-tooling.snapshot"), 64 * 1024)?;
    if hex(&Sha256::digest(&snapshot))
        != include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/tooling.sha256")).trim()
    {
        return Err("checkout tooling snapshot differs from this binary".into());
    }
    // The checkout, its Makefile and inherited execution environment are trusted
    // operator inputs. This is shared admission, not a sandbox for arbitrary code.
    let status = tokio::process::Command::new("make")
        .current_dir(checkout)
        .args(["--no-print-directory", "toko-tooling-check"])
        .env_remove("MAKEFLAGS")
        .env_remove("MFLAGS")
        .env_remove("MAKEFILES")
        .stdout(Stdio::null())
        .stderr(Stdio::inherit())
        .kill_on_drop(true)
        .status()
        .await
        .map_err(|_| "cannot run tooling admission")?;
    if !status.success() {
        return Err("tooling admission refused dispatch".into());
    }

    Ok(())
}

fn unexpired(expires_at: u64) -> Result<(), String> {
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_err(|_| "invalid local clock")?
        .as_nanos();
    if u128::from(expires_at) <= now {
        return Err("original request expired; no renewal authorized".into());
    }

    Ok(())
}

pub(super) async fn dispatch(
    context: &Context,
    directory: &Path,
    custody: &Path,
    operation: Operation<'_>,
) -> Result<String, String> {
    let original = load(context, directory, operation)?;
    unexpired(original.expires_at)?;
    let network = hex(&context.root_hash);
    let target_directory = custody.join(network).join(context.canister.to_text());
    let held =
        files::directory(&target_directory, true).map_err(|_| "cannot admit target custody")?;
    let _lock = held
        .lock(
            "manager.lock".as_ref(),
            0o600,
            FilePolicy {
                forbidden: 0o077,
                single_link: true,
            },
        )
        .map_err(|_| "target management is already locked or lock custody invalid")?;
    let marker = operation.marker(original.attempt);
    // Presence of any entry (even partial or corrupt) conservatively bars dispatch.
    if held
        .open_file(
            marker.as_ref(),
            ic_host_fs::Access::Read,
            FilePolicy {
                forbidden: 0,
                single_link: false,
            },
        )
        .map_err(|_| "cannot inspect dispatch claim")?
        .is_some()
    {
        return Err(
            "dispatch already claimed; observe the original request, never resubmit".into(),
        );
    }
    operation.admit_custody(context, original.attempt, &held)?;
    operation.admit(context, original.attempt).await?;
    unexpired(original.expires_at)?;
    // This is an irreversible local exposure boundary, not proof of transmission.
    // A crash or sync failure may strand a safe unsent request; never delete/retry it.
    files::publish(&held, marker.as_ref(), &original.id).map_err(|error| {
        format!("dispatch claim failed; preserve custody and do not resubmit: {error}")
    })?;
    let prepared = ic_platform::native::PreparedUpdate::from_bytes(
        &original.packet,
        context.canister,
        crate::network::MESSAGE_BYTES,
    )
    .map_err(|_| "invalid original signed request")?;
    let result = context
        .client
        .transport
        .dispatch(&prepared, crate::network::policy(false)?)
        .await;

    match result {
        Ok(ic_platform::native::Dispatch::Observed(observed))
            if matches!(observed.status(), RequestStatus::Replied(_)) =>
        {
            let RequestStatus::Replied(bytes) = observed.status() else {
                unreachable!()
            };
            match operation {
                Operation::Drain => acknowledge(&original.id, original.attempt, bytes, false),
                Operation::Stop => stop::acknowledge(&original.id, bytes),
                Operation::Upgrade { .. } => upgrade::acknowledge(&original.id, bytes),
                Operation::Start { wasm, .. } => {
                    start::acknowledge(context, &original.id, original.attempt, bytes, wasm).await
                }
                Operation::Activate { wasm, .. } => {
                    activate::acknowledge(context, &original.id, original.attempt, bytes, wasm)
                        .await
                }
            }
        }
        Ok(ic_platform::native::Dispatch::UncertifiedRejection { .. }) => Err("uncertified dispatch rejection; preserve custody and observe the original request".into()),
        Ok(ic_platform::native::Dispatch::Observed(observed)) if matches!(observed.status(), RequestStatus::Rejected { .. } | RequestStatus::Done | RequestStatus::Unavailable) => Err("original request rejected or outcome unavailable; preserve custody, no resubmission".into()),
        Ok(_) => Err("original request pending; observe it, do not resubmit".into()),
        Err(_) => Err(
            "dispatch outcome uncertain; preserve custody and observe the original request".into(),
        ),
    }
}