bamboo-server 2026.8.1

HTTP server and API layer for the Bamboo agent framework
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
use crate::{
    app_state::{AppState, ConfigUpdateEffects},
    error::AppError,
};
use actix_web::{web, HttpResponse};

use super::{super::credential_action::credential_status_view, types::ProxyAuthPayload};

/// Sets proxy authentication credentials
///
/// # HTTP Route
/// `POST /bamboo/proxy-auth`
///
/// # Request Body
/// ```json
/// {
///   "expected_revision": 0,
///   "action": "replace",
///   "username": "user",
///   "password": "pass"
/// }
/// ```
///
/// # Response Format
/// ```json
/// {
///   "success": true,
///   "revision": 1,
///   "section": { "revision": 1, "data": {} },
///   "configured": true
/// }
/// ```
///
/// # Response Status
/// - `200 OK`: Proxy auth saved and provider reloaded
/// - `500 Internal Server Error`: Failed to save or reload
///
/// # Security
/// Credentials are encrypted in the isolated credential store. `config.json`
/// retains only the stable `proxy.default.auth` reference.
///
/// # Example
/// ```bash
/// curl -X POST http://localhost:3000/bamboo/proxy-auth \
///   -H "Content-Type: application/json" \
///   -d '{"expected_revision":0,"action":"replace","username":"user","password":"pass"}'
/// ```
pub async fn set_proxy_auth(
    app_state: web::Data<AppState>,
    payload: web::Json<ProxyAuthPayload>,
) -> Result<HttpResponse, AppError> {
    let payload = payload.into_inner();
    let expected_revision = payload.expected_revision();
    let auth = payload.into_proxy_auth().map_err(AppError::BadRequest)?;

    let (config, revision, status, credential_health, section) = app_state
        .update_proxy_auth_credential(
            auth,
            expected_revision,
            ConfigUpdateEffects {
                // Best-effort inside the detached post-commit convergence task:
                // setup flows often set proxy auth before provider config is complete.
                reload_provider: true,
                // Proxy auth can affect SSE-based MCP servers too.
                reconcile_mcp: true,
            },
        )
        .await?;
    let section = section.ok_or_else(|| {
        AppError::InternalError(anyhow::anyhow!(
            "proxy mutation completed without a typed Core section envelope"
        ))
    })?;
    let credential = credential_status_view(
        config.proxy_auth_credential_ref.as_ref(),
        config.proxy_auth_credential_ref.is_some(),
        Some(&status),
        &credential_health,
    );

    Ok(HttpResponse::Ok().json(serde_json::json!({
        "success": true,
        "section": section,
        "credential_ref": credential.credential_ref,
        "state": credential.state,
        "configured": credential.configured,
        "source": credential.source,
        "updated_at": credential.updated_at,
        "revision": revision,
        "credential_health": credential_health.clone(),
        "credential_revision": credential_health.revision,
        "credential_status": credential_health.status,
        "credential_source_kind": credential_health.source,
        "credential_last_error": credential_health.last_error,
    })))
}

/// Gets proxy authentication status
///
/// # HTTP Route
/// `GET /bamboo/proxy-auth/status`
///
/// # Response Format
/// ```json
/// {
///   "credential_ref": "proxy.default.auth",
///   "configured": true,
///   "revision": 1,
///   "status": "healthy"
/// }
/// ```
///
/// # Response Status
/// - `200 OK`: Status retrieved successfully
///
/// # Note
/// Neither username nor password is returned; only credential and section metadata.
///
/// # Example
/// ```bash
/// curl http://localhost:3000/bamboo/proxy-auth/status
/// ```
pub async fn get_proxy_auth_status(
    app_state: web::Data<AppState>,
) -> Result<HttpResponse, AppError> {
    let exact = app_state
        .read_exact_credential_section(bamboo_config::SectionId::Core)
        .await?;
    let section = exact.section;
    let reference = exact.config.proxy_auth_credential_ref.clone();
    let Some(reference) = reference else {
        let health = exact.metadata.credential_health;
        let credential = credential_status_view(None, false, None, &health);
        return Ok(HttpResponse::Ok().json(serde_json::json!({
            "section": section.clone(),
            "credential_ref": credential.credential_ref,
            "state": credential.state,
            "configured": credential.configured,
            "source": credential.source,
            "updated_at": credential.updated_at,
            "revision": section.revision,
            "status": section.status,
            "source_kind": section.source_kind,
            "source_path": section.source_path,
            "loaded_at": section.loaded_at,
            "last_error": section.last_error,
            "credential_health": health.clone(),
            "credential_revision": health.revision,
            "credential_status": health.status,
            "credential_source_kind": health.source,
            "credential_last_error": health.last_error,
        })));
    };
    let health = exact.metadata.credential_health;
    let status = exact
        .metadata
        .credential_statuses
        .iter()
        .find(|status| status.credential_ref == reference);
    let credential = credential_status_view(Some(&reference), true, status, &health);
    Ok(HttpResponse::Ok().json(serde_json::json!({
        "section": section.clone(),
        "credential_ref": credential.credential_ref,
        "state": credential.state,
        "configured": credential.configured,
        "source": credential.source,
        "updated_at": credential.updated_at,
        "revision": section.revision,
        "status": section.status,
        "source_kind": section.source_kind,
        "source_path": section.source_path,
        "loaded_at": section.loaded_at,
        "last_error": section.last_error,
        "credential_health": health.clone(),
        "credential_revision": health.revision,
        "credential_status": health.status,
        "credential_source_kind": health.source,
        "credential_last_error": health.last_error,
    })))
}

#[cfg(test)]
mod tests {
    use super::*;
    use actix_web::{test, App};
    use bamboo_config::{credential_ref, CredentialSource};

    #[actix_web::test]
    async fn status_does_not_treat_an_orphan_canonical_credential_as_active_proxy_auth() {
        let _key = bamboo_config::encryption::set_test_encryption_key([0xa4; 32]);
        let dir = tempfile::tempdir().unwrap();
        let state = web::Data::new(AppState::new(dir.path().to_path_buf()).await.unwrap());
        state
            .credential_store
            .replace(
                credential_ref("proxy", "default", "auth").unwrap(),
                r#"{"username":"orphan","password":"secret"}"#,
                CredentialSource::User,
                0,
            )
            .unwrap();
        let app = test::init_service(
            App::new()
                .app_data(state)
                .route("/proxy-auth/status", web::get().to(get_proxy_auth_status)),
        )
        .await;

        let request = test::TestRequest::get()
            .uri("/proxy-auth/status")
            .to_request();
        let response: serde_json::Value = test::call_and_read_body_json(&app, request).await;
        assert_eq!(response["credential_ref"], serde_json::Value::Null);
        assert_eq!(response["configured"], false);
        assert_eq!(response["state"], "missing");
        assert_eq!(response["revision"], 0);
        assert_eq!(response["credential_revision"], 1);
    }

    #[actix_web::test]
    async fn status_reports_valid_ciphertext_with_invalid_proxy_payload_as_error() {
        let dir = tempfile::tempdir().unwrap();
        let state = web::Data::new(AppState::new(dir.path().to_path_buf()).await.unwrap());
        state
            .update_proxy_auth_credential(
                Some(bamboo_config::ProxyAuth {
                    username: "proxy-user".to_string(),
                    password: "proxy-secret".to_string(),
                }),
                0,
                Default::default(),
            )
            .await
            .unwrap();
        let reference = credential_ref("proxy", "default", "auth").unwrap();
        state
            .credential_store
            .replace(
                reference.clone(),
                r#"{"username":"","password":"still-encrypted"}"#,
                CredentialSource::User,
                1,
            )
            .unwrap();

        let app = test::init_service(
            App::new()
                .app_data(state.clone())
                .route("/proxy-auth/status", web::get().to(get_proxy_auth_status)),
        )
        .await;
        let response: serde_json::Value = test::call_and_read_body_json(
            &app,
            test::TestRequest::get()
                .uri("/proxy-auth/status")
                .to_request(),
        )
        .await;
        assert_eq!(response["revision"], 1);
        assert_eq!(response["credential_revision"], 2);
        assert_eq!(response["configured"], false);
        assert_eq!(response["state"], "error");

        state
            .credential_store
            .replace(
                reference,
                r#"{"username":"****...****","password":"still-encrypted"}"#,
                CredentialSource::User,
                2,
            )
            .unwrap();
        let masked: serde_json::Value = test::call_and_read_body_json(
            &app,
            test::TestRequest::get()
                .uri("/proxy-auth/status")
                .to_request(),
        )
        .await;
        assert_eq!(masked["credential_revision"], 3);
        assert_eq!(masked["configured"], false);
        assert_eq!(masked["state"], "error");
    }

    #[actix_web::test]
    async fn exact_status_reports_wrong_encryption_key_as_error() {
        let dir = tempfile::tempdir().unwrap();
        let state = AppState::new(dir.path().to_path_buf()).await.unwrap();
        state
            .update_proxy_auth_credential(
                Some(bamboo_config::ProxyAuth {
                    username: "proxy-user".to_string(),
                    password: "proxy-secret".to_string(),
                }),
                0,
                Default::default(),
            )
            .await
            .unwrap();
        let data_dir = dir.path().to_path_buf();
        let exact = tokio::task::spawn_blocking(move || {
            let _wrong_key = bamboo_config::encryption::set_test_encryption_key([0xa7; 32]);
            bamboo_config::read_exact_credential_section_snapshot(
                data_dir,
                bamboo_config::SectionId::Core,
                None,
            )
        })
        .await;
        let exact = exact.unwrap().unwrap();
        assert_eq!(exact.section.revision, 1);
        let reference = credential_ref("proxy", "default", "auth").unwrap();
        let status = exact
            .credential_statuses
            .iter()
            .find(|status| status.credential_ref == reference)
            .unwrap();
        assert!(!status.configured);
    }

    #[actix_web::test]
    async fn proxy_auth_update_requires_revision_and_stale_write_is_conflict() {
        let dir = tempfile::tempdir().unwrap();
        let state = web::Data::new(AppState::new(dir.path().to_path_buf()).await.unwrap());
        let mut events = state.account_sink.subscribe();
        let app = test::init_service(
            App::new()
                .app_data(state.clone())
                .route("/proxy-auth", web::post().to(set_proxy_auth)),
        )
        .await;

        let set = test::TestRequest::post()
            .uri("/proxy-auth")
            .set_json(serde_json::json!({
                "expected_revision": 0,
                "action": "replace",
                "username": "proxy-user",
                "password": "proxy-secret"
            }))
            .to_request();
        let response = test::call_service(&app, set).await;
        assert!(response.status().is_success());
        let body: serde_json::Value = test::read_body_json(response).await;
        assert_eq!(body["revision"], 1);
        assert_eq!(body["configured"], true);
        assert_eq!(body["state"], "configured");
        assert_eq!(body["credential_ref"], "proxy.default.auth");
        assert!(!body.to_string().contains("proxy-secret"));
        assert_eq!(body["section"]["revision"], 1);
        let event = tokio::time::timeout(std::time::Duration::from_secs(2), async {
            loop {
                let event = events.recv().await.unwrap();
                if matches!(
                    &event.event,
                    bamboo_agent_core::AgentEvent::ConfigChanged { section, revision: 1 }
                        if section == "core"
                ) {
                    break event;
                }
            }
        })
        .await
        .unwrap();
        assert!(matches!(
            event.event,
            bamboo_agent_core::AgentEvent::ConfigChanged { ref section, revision: 1 }
                if section == "core"
        ));
        let (committed_ref, committed_auth) = {
            let config = state.config.read().await;
            (
                config.proxy_auth_credential_ref.clone(),
                config.proxy_auth.clone(),
            )
        };
        let committed_credentials = std::fs::read(dir.path().join("credentials.json")).unwrap();

        let stale = test::TestRequest::post()
            .uri("/proxy-auth")
            .set_json(serde_json::json!({
                "expected_revision": 0,
                "username": "stale-user",
                "password": "stale-secret"
            }))
            .to_request();
        let stale = test::call_service(&app, stale).await;
        assert_eq!(stale.status(), actix_web::http::StatusCode::CONFLICT);
        let stale_body = String::from_utf8(test::read_body(stale).await.to_vec()).unwrap();
        assert!(!stale_body.contains("stale-secret"));
        {
            let config = state.config.read().await;
            assert_eq!(config.proxy_auth_credential_ref, committed_ref);
            assert_eq!(
                config
                    .proxy_auth
                    .as_ref()
                    .map(|auth| auth.username.as_str()),
                committed_auth.as_ref().map(|auth| auth.username.as_str())
            );
            assert!(
                config
                    .proxy_auth
                    .as_ref()
                    .zip(committed_auth.as_ref())
                    .is_some_and(|(current, committed)| current.password == committed.password),
                "stale conflict changed the committed proxy password"
            );
        }
        assert_eq!(
            std::fs::read(dir.path().join("credentials.json")).unwrap(),
            committed_credentials,
            "stale conflict changed the committed credential document"
        );

        let clear = test::TestRequest::post()
            .uri("/proxy-auth")
            .set_json(serde_json::json!({
                "expected_revision": 1,
                "action": "clear"
            }))
            .to_request();
        let clear = test::call_service(&app, clear).await;
        assert!(clear.status().is_success());
        let body: serde_json::Value = test::read_body_json(clear).await;
        assert_eq!(body["revision"], 2);
        assert_eq!(body["configured"], false);
        assert_eq!(body["state"], "missing");
        assert!(state.config.read().await.proxy_auth.is_none());
    }
}