cellos-supervisor 0.5.1

CellOS execution-cell runner — boots cells in Firecracker microVMs or gVisor, enforces narrow typed authority, emits signed CloudEvents.
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
//! Local end-to-end smoke for the GitHub Actions OIDC + presigned S3 runner path.
//!
//! This uses a tiny in-test HTTP server so CI can exercise the reference slice
//! without external AWS or GitHub dependencies.

#[cfg(unix)]
mod unix {
    use std::fs::File;
    use std::io::{BufRead, BufReader, Read, Write};
    use std::net::{TcpListener, TcpStream};
    use std::path::{Path, PathBuf};
    use std::process::Command;
    use std::thread;
    use std::time::{Duration, Instant};

    use serde_json::Value;

    #[derive(Debug)]
    struct CapturedRequest {
        method: String,
        target: String,
        authorization: Option<String>,
        body: Vec<u8>,
    }

    fn supervisor_exe() -> PathBuf {
        if let Some(p) = std::env::var_os("CARGO_BIN_EXE_cellos_supervisor") {
            return PathBuf::from(p);
        }
        let root = Path::new(env!("CARGO_MANIFEST_DIR"))
            .parent()
            .and_then(|p| p.parent())
            .expect("cellos-supervisor crate under workspace root");
        let profile = std::env::var("PROFILE").unwrap_or_else(|_| "debug".into());
        root.join("target").join(profile).join("cellos-supervisor")
    }

    fn read_request(stream: &mut TcpStream) -> CapturedRequest {
        let mut reader = BufReader::new(stream.try_clone().expect("clone stream"));
        let mut request_line = String::new();
        reader
            .read_line(&mut request_line)
            .expect("read request line");
        assert!(
            !request_line.trim().is_empty(),
            "expected non-empty request line"
        );

        let mut authorization = None;
        let mut content_length = 0usize;
        loop {
            let mut line = String::new();
            reader.read_line(&mut line).expect("read header");
            if line == "\r\n" || line.is_empty() {
                break;
            }
            if let Some((name, value)) = line.split_once(':') {
                let header_name = name.trim().to_ascii_lowercase();
                let header_value = value.trim().to_string();
                if header_name == "authorization" {
                    authorization = Some(header_value);
                } else if header_name == "content-length" {
                    content_length = header_value.parse::<usize>().expect("parse content-length");
                }
            }
        }

        let mut body = vec![0u8; content_length];
        reader.read_exact(&mut body).expect("read request body");

        let mut parts = request_line.split_whitespace();
        let method = parts.next().expect("method").to_string();
        let target = parts.next().expect("target").to_string();
        CapturedRequest {
            method,
            target,
            authorization,
            body,
        }
    }

    fn write_response(stream: &mut TcpStream, status_line: &str, body: &[u8], content_type: &str) {
        write!(
            stream,
            "HTTP/1.1 {status_line}\r\nContent-Type: {content_type}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
            body.len()
        )
        .expect("write response headers");
        stream.write_all(body).expect("write response body");
        stream.flush().expect("flush response");
    }

    fn start_mock_runner_services(
        fail_oidc: bool,
        expected_requests: usize,
    ) -> (String, String, thread::JoinHandle<Vec<CapturedRequest>>) {
        let listener = TcpListener::bind("127.0.0.1:0").expect("bind mock server");
        listener
            .set_nonblocking(true)
            .expect("set mock server nonblocking");
        let addr = listener.local_addr().expect("mock server local addr");
        let handle = thread::spawn(move || {
            let deadline = Instant::now() + Duration::from_secs(10);
            let mut requests = Vec::new();
            while requests.len() < expected_requests && Instant::now() < deadline {
                match listener.accept() {
                    Ok((mut stream, _peer)) => {
                        stream
                            .set_nonblocking(false)
                            .expect("set accepted stream blocking");
                        let request = read_request(&mut stream);
                        match (request.method.as_str(), request.target.as_str()) {
                            ("GET", target) if target.starts_with("/oidc?") => {
                                if fail_oidc {
                                    write_response(
                                        &mut stream,
                                        "500 Internal Server Error",
                                        b"mock oidc failure",
                                        "text/plain",
                                    );
                                } else {
                                    write_response(
                                        &mut stream,
                                        "200 OK",
                                        br#"{"value":"eyJhbGciOiJIUzI1NiJ9.mock.payload"}"#,
                                        "application/json",
                                    );
                                }
                            }
                            ("PUT", target) if target.starts_with("/upload?") => {
                                write_response(&mut stream, "200 OK", b"", "text/plain");
                            }
                            _ => {
                                write_response(
                                    &mut stream,
                                    "404 Not Found",
                                    b"unexpected request",
                                    "text/plain",
                                );
                            }
                        }
                        requests.push(request);
                    }
                    Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => {
                        thread::sleep(Duration::from_millis(20));
                    }
                    Err(err) => panic!("mock server accept failed: {err}"),
                }
            }
            requests
        });

        (
            format!("http://{addr}/oidc?api-version=2"),
            format!("http://{addr}/upload?X-Amz-Signature=fake"),
            handle,
        )
    }

    fn read_jsonl_events(path: &Path) -> Vec<Value> {
        let file = File::open(path).expect("open jsonl");
        BufReader::new(file)
            .lines()
            .map(|line| {
                serde_json::from_str::<Value>(&line.expect("read jsonl line"))
                    .expect("parse jsonl line")
            })
            .collect()
    }

    #[test]
    fn github_oidc_and_s3_export_emit_identity_and_export_events() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let jsonl_path = tmp.path().join("events.jsonl");
        let spec_path = tmp.path().join("spec.json");
        let artifact_path = tmp.path().join("test-results.xml");

        let (oidc_url, s3_url, server) = start_mock_runner_services(false, 2);

        let spec = serde_json::json!({
            "apiVersion": "cellos.io/v1",
            "kind": "ExecutionCell",
            "spec": {
                "id": "gha-oidc-s3-smoke",
                "correlation": {
                    "platform": "github",
                    "externalRunId": "123456789",
                    "externalJobId": "build-and-test",
                    "tenantId": "org-acme"
                },
                "identity": {
                    "kind": "federatedOidc",
                    "provider": "github-actions",
                    "audience": "sts.amazonaws.com",
                    "secretRef": "AWS_WEB_IDENTITY",
                    "ttlSeconds": 300
                },
                "authority": {
                    "secretRefs": ["AWS_WEB_IDENTITY"]
                },
                "lifetime": {
                    "ttlSeconds": 600
                },
                "run": {
                    "secretDelivery": "env",
                    "argv": [
                        "/bin/sh",
                        "-c",
                        format!(
                            "test -n \"$AWS_WEB_IDENTITY\" && printf 'hello from cellos' > \"{}\"",
                            artifact_path.display()
                        )
                    ]
                },
                "export": {
                    "targets": [
                        {
                            "name": "artifact-bucket",
                            "kind": "s3",
                            "bucket": "demo-bucket",
                            "keyPrefix": "github/run-123",
                            "secretRef": "AWS_WEB_IDENTITY"
                        }
                    ],
                    "artifacts": [
                        {
                            "name": "test-results",
                            "path": artifact_path,
                            "target": "artifact-bucket",
                            "contentType": "application/xml"
                        }
                    ]
                }
            }
        });
        std::fs::write(&spec_path, serde_json::to_string(&spec).unwrap()).expect("write spec");

        let status = Command::new(supervisor_exe())
            .env("CELLOS_DEPLOYMENT_PROFILE", "portable")
            .env("CELL_OS_USE_NOOP_SINK", "1")
            .env("CELLOS_CELL_BACKEND", "stub")
            .env("CELL_OS_JSONL_EVENTS", &jsonl_path)
            .env("CELLOS_BROKER", "github-oidc")
            .env("ACTIONS_ID_TOKEN_REQUEST_URL", &oidc_url)
            .env("ACTIONS_ID_TOKEN_REQUEST_TOKEN", "gha-request-token")
            .env("CELLOS_EXPORT_S3_PRESIGNED_URL__ARTIFACT_BUCKET", &s3_url)
            .env("CELL_OS_REQUIRE_S3_EXPORT", "1")
            .env("CELLOS_RUN_ID", "gha-local-smoke")
            .current_dir(env!("CARGO_MANIFEST_DIR"))
            .arg(&spec_path)
            .status()
            .expect("spawn cellos-supervisor");

        assert!(
            status.success(),
            "expected successful supervisor run: {status:?}"
        );

        let requests = server.join().expect("join mock server");
        assert_eq!(requests.len(), 2, "expected OIDC GET and S3 PUT");
        assert_eq!(requests[0].method, "GET");
        assert!(
            requests[0].target.contains("audience=sts.amazonaws.com"),
            "OIDC request should use identity.audience, got {}",
            requests[0].target
        );
        assert_eq!(
            requests[0].authorization.as_deref(),
            Some("Bearer gha-request-token")
        );
        assert_eq!(requests[1].method, "PUT");
        assert_eq!(requests[1].target, "/upload?X-Amz-Signature=fake");
        assert_eq!(
            String::from_utf8(requests[1].body.clone()).unwrap(),
            "hello from cellos"
        );

        let events = read_jsonl_events(&jsonl_path);
        let event_types: Vec<&str> = events
            .iter()
            .filter_map(|event| event["type"].as_str())
            .collect();
        assert!(
            event_types.contains(&"dev.cellos.events.cell.identity.v1.materialized"),
            "missing identity materialized event: {event_types:?}"
        );
        assert!(
            event_types.contains(&"dev.cellos.events.cell.identity.v1.revoked"),
            "missing identity revoked event: {event_types:?}"
        );
        assert!(
            event_types.contains(&"dev.cellos.events.cell.export.v2.completed"),
            "missing export completed event: {event_types:?}"
        );
        assert!(
            !event_types.contains(&"dev.cellos.events.cell.identity.v1.failed"),
            "unexpected identity failed event: {event_types:?}"
        );

        let identity_event = events
            .iter()
            .find(|event| event["type"] == "dev.cellos.events.cell.identity.v1.materialized")
            .expect("identity materialized event");
        assert_eq!(
            identity_event["data"]["identity"]["audience"],
            "sts.amazonaws.com"
        );
        assert_eq!(
            identity_event["data"]["identity"]["secretRef"],
            "AWS_WEB_IDENTITY"
        );

        let export_event = events
            .iter()
            .find(|event| event["type"] == "dev.cellos.events.cell.export.v2.completed")
            .expect("export completed event");
        assert_eq!(export_event["data"]["receipt"]["targetKind"], "s3");
        assert_eq!(
            export_event["data"]["receipt"]["destination"],
            "s3://demo-bucket/github/run-123/test-results"
        );
        assert!(
            export_event["data"]["receipt"]["bytesWritten"]
                .as_u64()
                .expect("bytesWritten")
                > 0
        );
    }

    #[test]
    fn github_oidc_failures_emit_identity_failed_event() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let jsonl_path = tmp.path().join("events.jsonl");
        let spec_path = tmp.path().join("spec.json");

        let (oidc_url, _s3_url, server) = start_mock_runner_services(true, 1);

        let spec = serde_json::json!({
            "apiVersion": "cellos.io/v1",
            "kind": "ExecutionCell",
            "spec": {
                "id": "gha-oidc-failure",
                "identity": {
                    "kind": "federatedOidc",
                    "provider": "github-actions",
                    "audience": "sts.amazonaws.com",
                    "secretRef": "AWS_WEB_IDENTITY",
                    "ttlSeconds": 300
                },
                "authority": {
                    "secretRefs": ["AWS_WEB_IDENTITY"]
                },
                "lifetime": {
                    "ttlSeconds": 600
                }
            }
        });
        std::fs::write(&spec_path, serde_json::to_string(&spec).unwrap()).expect("write spec");

        let status = Command::new(supervisor_exe())
            .env("CELLOS_DEPLOYMENT_PROFILE", "portable")
            .env("CELL_OS_USE_NOOP_SINK", "1")
            .env("CELLOS_CELL_BACKEND", "stub")
            .env("CELL_OS_JSONL_EVENTS", &jsonl_path)
            .env("CELLOS_BROKER", "github-oidc")
            .env("ACTIONS_ID_TOKEN_REQUEST_URL", &oidc_url)
            .env("ACTIONS_ID_TOKEN_REQUEST_TOKEN", "gha-request-token")
            .env("CELLOS_RUN_ID", "gha-local-failure")
            .current_dir(env!("CARGO_MANIFEST_DIR"))
            .arg(&spec_path)
            .status()
            .expect("spawn cellos-supervisor");

        assert!(
            !status.success(),
            "expected supervisor failure when OIDC endpoint fails"
        );

        let requests = server.join().expect("join mock server");
        assert_eq!(requests.len(), 1, "expected only the OIDC request");
        assert_eq!(requests[0].method, "GET");
        assert!(
            requests[0].target.contains("audience=sts.amazonaws.com"),
            "OIDC request should use identity.audience, got {}",
            requests[0].target
        );

        let events = read_jsonl_events(&jsonl_path);
        let identity_failed = events
            .iter()
            .find(|event| event["type"] == "dev.cellos.events.cell.identity.v1.failed")
            .expect("identity failed event");
        assert_eq!(identity_failed["data"]["operation"], "materialize");
        assert!(
            identity_failed["data"]["reason"]
                .as_str()
                .expect("failure reason")
                .contains("oidc token request returned 500"),
            "unexpected failure reason: {}",
            identity_failed["data"]["reason"]
        );
    }
}