acme-proxy 0.6.1

An ACME (RFC 8555) server that issues from a local CA, relays to an upstream CA, or delegates to a script
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
//! Covers the multi-endpoint (profile) surface: several ACME endpoints served
//! by one process, over one database, under `/profile/<name>`.
//!
//! Most of this crate is about what must **not** work across endpoints. The
//! URL prefix alone is cosmetic; what makes a profile a boundary is that
//! accounts and orders are scoped to it in the database. Without that, a client
//! could register at a permissive endpoint and then use that account at a
//! stricter one — bypassing its `eab.enabled`, and carrying orders whose
//! challenges were never really validated to a signer that relays to a public
//! CA. Each test below pins one half of that boundary.

use std::sync::Arc;

use acme_proxy_policy::filter::ConnectionContext;
use acme_proxy_policy::filter::IdentifierContext;
use acme_proxy_policy::filter::policy::Check;
use acme_proxy_policy::filter::policy::StageSet;
use acme_proxy_policy::filter::policy::Verdict;
use async_trait::async_trait;
use axum::Router;
use axum::body::Body;
use axum::http::{Request, StatusCode};
use axum::response::Response;
use base64::prelude::*;
use http_body_util::BodyExt;
use serde_json::{Value, json};
use tower::ServiceExt;

mod common;
use common::{
    EcSigner, HOST, TestProfile, TestSigner, body_json, first_certificate, make_csr,
    make_csr_and_keypair, send_from, test_app_with_profiles,
};

/// Two endpoints: `a` issues freely, `b` demands an External Account Binding.
/// They have distinct local CAs, so the issuer of a leaf says which one signed.
async fn two_profiles() -> Router {
    test_app_with_profiles(vec![
        TestProfile::new("a").await,
        TestProfile::new("b").await.requiring_eab(),
    ])
    .await
    .0
}

fn base(name: &str) -> String {
    TestProfile::base(name)
}

fn path(name: &str, path: &str) -> String {
    TestProfile::path(name, path)
}

async fn post(app: &Router, path: &str, body: String) -> Response {
    app.clone()
        .oneshot(
            Request::post(path)
                .header("content-type", "application/jose+json")
                .body(Body::from(body))
                .unwrap(),
        )
        .await
        .unwrap()
}

async fn get(app: &Router, path: &str) -> Response {
    app.clone()
        .oneshot(Request::get(path).body(Body::empty()).unwrap())
        .await
        .unwrap()
}

async fn nonce(app: &Router, profile: &str) -> String {
    get(app, &path(profile, "/newNonce"))
        .await
        .headers()
        .get("replay-nonce")
        .expect("newNonce must hand out a nonce")
        .to_str()
        .unwrap()
        .to_string()
}

/// POST-as-GETs `order_url` at `profile` until the worker has settled it,
/// returning the certificate URL. Issuance is queued work: finalize answers
/// `processing`, and the client polls.
async fn await_certificate(
    app: &Router,
    profile: &str,
    signer: &EcSigner,
    account: &str,
    order_url: &str,
) -> String {
    for _ in 0..600 {
        let n = nonce(app, profile).await;
        let body = signer.sign_kid_empty(account, order_url, &n);
        let order = body_json(post(app, to_path(order_url), body).await).await;
        if order["status"] != "processing" {
            assert_eq!(order["status"], "valid", "issuance at {profile}");
            return order["certificate"].as_str().unwrap().to_string();
        }
        tokio::time::sleep(std::time::Duration::from_millis(10)).await;
    }
    panic!("`{order_url}` never left `processing`");
}

/// Registers an account at `profile` and returns its account URL.
async fn register(app: &Router, profile: &str, signer: &EcSigner) -> String {
    let n = nonce(app, profile).await;
    let body = signer.sign(
        &format!("{}/newAccount", base(profile)),
        &n,
        &json!({ "termsOfServiceAgreed": true }),
    );
    let res = post(app, &path(profile, "/newAccount"), body).await;
    assert_eq!(
        res.status(),
        StatusCode::CREATED,
        "registering at {profile}"
    );
    res.headers()
        .get("location")
        .and_then(|v| v.to_str().ok())
        .expect("newAccount must set a Location header")
        .to_string()
}

/// The path a returned absolute URL maps to — the host goes, the profile
/// prefix stays.
fn to_path(url: &str) -> &str {
    url.strip_prefix(HOST).expect("URL must be under the host")
}

/// Drives one order at `profile` all the way to an issued certificate chain.
async fn issue(app: &Router, profile: &str, signer: &EcSigner, account: &str, dns: &str) -> String {
    let n = nonce(app, profile).await;
    let payload = json!({ "identifiers": [{ "type": "dns", "value": dns }] });
    let body = signer.sign_kid(
        account,
        &format!("{}/newOrder", base(profile)),
        &n,
        &payload,
    );
    let res = post(app, &path(profile, "/newOrder"), body).await;
    assert_eq!(res.status(), StatusCode::CREATED);
    let order_url = res
        .headers()
        .get("location")
        .and_then(|v| v.to_str().ok())
        .unwrap()
        .to_string();
    let order = body_json(res).await;

    let authz_url = order["authorizations"][0].as_str().unwrap().to_string();
    let n = nonce(app, profile).await;
    let body = signer.sign_kid_empty(account, &authz_url, &n);
    let authz = body_json(post(app, to_path(&authz_url), body).await).await;
    let chall_url = authz["challenges"][0]["url"].as_str().unwrap().to_string();

    // Validation is queued work, so the trigger only starts it. Poll with the
    // repeat `{}` POST §7.5.1 makes explicitly not a state change.
    let mut status = serde_json::Value::Null;
    for _ in 0..600 {
        let n = nonce(app, profile).await;
        let body = signer.sign_kid(account, &chall_url, &n, &json!({}));
        let res = post(app, to_path(&chall_url), body).await;
        assert_eq!(res.status(), StatusCode::OK);
        status = body_json(res).await["status"].clone();
        if status != "processing" {
            break;
        }
        tokio::time::sleep(std::time::Duration::from_millis(10)).await;
    }
    assert_eq!(status, "valid");

    let finalize_url = format!("{order_url}/finalize");
    let n = nonce(app, profile).await;
    let body = signer.sign_kid(account, &finalize_url, &n, &json!({ "csr": make_csr(dns) }));
    let res = post(app, to_path(&finalize_url), body).await;
    assert_eq!(res.status(), StatusCode::OK, "finalize at {profile}");
    let cert_url = await_certificate(app, profile, signer, account, &order_url).await;

    let n = nonce(app, profile).await;
    let body = signer.sign_kid_empty(account, &cert_url, &n);
    let res = post(app, to_path(&cert_url), body).await;
    assert_eq!(res.status(), StatusCode::OK);
    String::from_utf8(res.into_body().collect().await.unwrap().to_bytes().to_vec()).unwrap()
}

/// Each endpoint advertises its own URLs, and only its own.
#[tokio::test]
async fn each_directory_advertises_its_own_prefix() {
    let app = two_profiles().await;

    for name in ["a", "b"] {
        let directory = body_json(get(&app, &path(name, "/directory")).await).await;
        for key in ["newNonce", "newAccount", "newOrder", "revokeCert"] {
            let url = directory[key].as_str().unwrap();
            assert_eq!(
                url,
                format!("{}/{key}", base(name)),
                "{name}'s directory must advertise its own {key}"
            );
        }
    }

    // Only the EAB-requiring endpoint says so.
    let a = body_json(get(&app, &path("a", "/directory")).await).await;
    let b = body_json(get(&app, &path("b", "/directory")).await).await;
    assert!(a.get("meta").is_none());
    assert_eq!(b["meta"]["externalAccountRequired"], true);
}

/// The whole lifecycle works through a nested router — the proof that
/// `Router::nest`'s path rewriting and the RFC 8555 §6.4 `url` check agree.
#[tokio::test]
async fn a_full_lifecycle_runs_under_a_profile_prefix() {
    let app = two_profiles().await;
    let signer = EcSigner::new();
    let account = register(&app, "a", &signer).await;

    assert!(
        account.starts_with(&format!("{}/acct/", base("a"))),
        "the account URL carries the endpoint's prefix: {account}"
    );

    let chain = issue(&app, "a", &signer, &account, "example.com").await;
    assert!(chain.contains("BEGIN CERTIFICATE"));
}

/// A JWS is bound to the endpoint it was signed for: the `url` it carries names
/// one profile, and replaying it at another is refused before anything else.
#[tokio::test]
async fn a_jws_signed_for_one_profile_is_refused_at_another() {
    let app = two_profiles().await;
    let signer = EcSigner::new();

    let n = nonce(&app, "a").await;
    let body = signer.sign(
        &format!("{}/newAccount", base("a")),
        &n,
        &json!({ "termsOfServiceAgreed": true }),
    );

    let res = post(&app, &path("b", "/newAccount"), body).await;
    assert_eq!(res.status(), StatusCode::BAD_REQUEST);
    assert_eq!(
        body_json(res).await["type"],
        "urn:ietf:params:acme:error:malformed"
    );
}

/// An account URL minted at one endpoint is not an account at another, even
/// when the `kid` is rewritten to name the second one: the row itself is
/// scoped, so there is nothing to find.
#[tokio::test]
async fn an_account_from_one_profile_is_unknown_at_another() {
    let app = two_profiles().await;
    let signer = EcSigner::new();
    let account_at_a = register(&app, "a", &signer).await;

    let id = account_at_a
        .rsplit('/')
        .next()
        .expect("the account URL ends in its id");
    let kid_at_b = format!("{}/acct/{id}", base("b"));

    let n = nonce(&app, "b").await;
    let body = signer.sign_kid(&kid_at_b, &kid_at_b, &n, &json!({}));
    let res = post(&app, &path("b", &format!("/acct/{id}")), body).await;

    assert_eq!(res.status(), StatusCode::BAD_REQUEST);
    assert_eq!(
        body_json(res).await["type"],
        "urn:ietf:params:acme:error:accountDoesNotExist"
    );
}

/// The regression this whole scoping exists for: registering where no External
/// Account Binding is required must not produce an account usable where one is.
#[tokio::test]
async fn registering_without_eab_does_not_open_an_eab_protected_profile() {
    let app = two_profiles().await;
    let signer = EcSigner::new();

    // The same key, at the endpoint that demands a binding: refused.
    let n = nonce(&app, "b").await;
    let body = signer.sign(
        &format!("{}/newAccount", base("b")),
        &n,
        &json!({ "termsOfServiceAgreed": true }),
    );
    let res = post(&app, &path("b", "/newAccount"), body).await;
    assert_eq!(res.status(), StatusCode::BAD_REQUEST);
    assert_eq!(
        body_json(res).await["type"],
        "urn:ietf:params:acme:error:externalAccountRequired"
    );

    // Registering at `a` (no binding needed) and then ordering at `b` with the
    // account `a` handed out must not work either.
    let account_at_a = register(&app, "a", &signer).await;
    let id = account_at_a.rsplit('/').next().unwrap();
    let kid_at_b = format!("{}/acct/{id}", base("b"));

    let n = nonce(&app, "b").await;
    let payload = json!({ "identifiers": [{ "type": "dns", "value": "example.com" }] });
    let body = signer.sign_kid(&kid_at_b, &format!("{}/newOrder", base("b")), &n, &payload);
    let res = post(&app, &path("b", "/newOrder"), body).await;

    assert_eq!(
        res.status(),
        StatusCode::BAD_REQUEST,
        "an account created at `a` must not be usable at `b`"
    );
    assert_eq!(
        body_json(res).await["type"],
        "urn:ietf:params:acme:error:accountDoesNotExist"
    );
}

/// An order lives at the endpoint it was created at. Reading it through another
/// one fails at the account lookup, so a permissive endpoint's authorizations
/// can never be spent on a stricter endpoint's signer.
#[tokio::test]
async fn an_order_is_invisible_from_another_profile() {
    let app = test_app_with_profiles(vec![
        TestProfile::new("a").await,
        TestProfile::new("b").await,
    ])
    .await
    .0;
    let signer = EcSigner::new();

    let account_at_a = register(&app, "a", &signer).await;
    let account_at_b = register(&app, "b", &signer).await;

    let n = nonce(&app, "a").await;
    let payload = json!({ "identifiers": [{ "type": "dns", "value": "example.com" }] });
    let body = signer.sign_kid(
        &account_at_a,
        &format!("{}/newOrder", base("a")),
        &n,
        &payload,
    );
    let res = post(&app, &path("a", "/newOrder"), body).await;
    let order_url = res
        .headers()
        .get("location")
        .and_then(|v| v.to_str().ok())
        .unwrap()
        .to_string();
    let order_id = order_url.rsplit('/').next().unwrap().to_string();

    // Same client key, its own account at `b`, asking for `a`'s order.
    let url_at_b = format!("{}/order/{order_id}", base("b"));
    let n = nonce(&app, "b").await;
    let body = signer.sign_kid_empty(&account_at_b, &url_at_b, &n);
    let res = post(&app, &path("b", &format!("/order/{order_id}")), body).await;

    assert_eq!(res.status(), StatusCode::BAD_REQUEST);
    assert_eq!(
        body_json(res).await["type"],
        "urn:ietf:params:acme:error:malformed"
    );
}

/// Each endpoint signs with its own CA and publishes its own CRL.
#[tokio::test]
async fn each_profile_issues_from_its_own_ca() {
    let app = test_app_with_profiles(vec![
        TestProfile::new("a").await,
        TestProfile::new("b").await,
    ])
    .await
    .0;
    let signer = EcSigner::new();

    let account_at_a = register(&app, "a", &signer).await;
    let account_at_b = register(&app, "b", &signer).await;
    let chain_a = issue(&app, "a", &signer, &account_at_a, "a.example.com").await;
    let chain_b = issue(&app, "b", &signer, &account_at_b, "b.example.com").await;

    // Both CAs carry the same subject name, so the issuer *name* proves
    // nothing: compare the issuing certificates themselves, which is what a
    // client validating the chain would actually rely on.
    let ca_a = chain_a.split("-----BEGIN CERTIFICATE-----").nth(2).unwrap();
    let ca_b = chain_b.split("-----BEGIN CERTIFICATE-----").nth(2).unwrap();
    assert_ne!(
        ca_a, ca_b,
        "two endpoints must not share a CA unless configured to"
    );

    // …and each leaf really is the name that endpoint was asked for (the
    // subject is rcgen's own default; the SAN is what a client validates).
    let leaf_a = first_certificate(&chain_a);
    let (_, parsed_a) = x509_parser::parse_x509_certificate(&leaf_a).unwrap();
    let san = parsed_a
        .subject_alternative_name()
        .unwrap()
        .expect("the leaf carries a SAN");
    assert!(
        format!("{:?}", san.value.general_names).contains("a.example.com"),
        "the leaf is the one this endpoint issued"
    );

    let crl_a = get(&app, &path("a", "/crl")).await;
    let crl_b = get(&app, &path("b", "/crl")).await;
    assert_eq!(crl_a.status(), StatusCode::OK);
    assert_eq!(crl_b.status(), StatusCode::OK);
    let body_a = crl_a.into_body().collect().await.unwrap().to_bytes();
    let body_b = crl_b.into_body().collect().await.unwrap().to_bytes();
    assert_ne!(body_a, body_b, "each CA publishes its own CRL");
}

/// Revocation carries no account (RFC 8555 §7.6 allows the certificate's own
/// key), so the endpoint the request reached is the only thing scoping it: `b`
/// must not revoke — or even recognise — a certificate `a` issued.
#[tokio::test]
async fn a_certificate_cannot_be_revoked_through_another_profile() {
    let app = test_app_with_profiles(vec![
        TestProfile::new("a").await,
        TestProfile::new("b").await,
    ])
    .await
    .0;
    let account_signer = EcSigner::new();
    let account = register(&app, "a", &account_signer).await;

    // An order whose CSR key is also a JWS signer, so revocation can be
    // authorized by the certificate's own key pair.
    let (csr, cert_key) = make_csr_and_keypair("revoke.example.com");
    let n = nonce(&app, "a").await;
    let payload = json!({ "identifiers": [{ "type": "dns", "value": "revoke.example.com" }] });
    let body = account_signer.sign_kid(&account, &format!("{}/newOrder", base("a")), &n, &payload);
    let res = post(&app, &path("a", "/newOrder"), body).await;
    let order_url = res
        .headers()
        .get("location")
        .and_then(|v| v.to_str().ok())
        .unwrap()
        .to_string();
    let order = body_json(res).await;
    let authz_url = order["authorizations"][0].as_str().unwrap().to_string();

    let n = nonce(&app, "a").await;
    let body = account_signer.sign_kid_empty(&account, &authz_url, &n);
    let authz = body_json(post(&app, to_path(&authz_url), body).await).await;
    let chall_url = authz["challenges"][0]["url"].as_str().unwrap().to_string();
    // Validation is queued work, so the trigger only starts it. Poll with the
    // repeat `{}` POST §7.5.1 makes explicitly not a state change.
    for _ in 0..600 {
        let n = nonce(&app, "a").await;
        let body = account_signer.sign_kid(&account, &chall_url, &n, &json!({}));
        let res = post(&app, to_path(&chall_url), body).await;
        if body_json(res).await["status"] != "processing" {
            break;
        }
        tokio::time::sleep(std::time::Duration::from_millis(10)).await;
    }

    let finalize_url = format!("{order_url}/finalize");
    let n = nonce(&app, "a").await;
    let body = account_signer.sign_kid(&account, &finalize_url, &n, &json!({ "csr": csr }));
    let res = post(&app, to_path(&finalize_url), body).await;
    assert_eq!(res.status(), StatusCode::OK);
    let cert_url = await_certificate(&app, "a", &account_signer, &account, &order_url).await;

    let n = nonce(&app, "a").await;
    let body = account_signer.sign_kid_empty(&account, &cert_url, &n);
    let chain = String::from_utf8(
        post(&app, to_path(&cert_url), body)
            .await
            .into_body()
            .collect()
            .await
            .unwrap()
            .to_bytes()
            .to_vec(),
    )
    .unwrap();
    let leaf = BASE64_URL_SAFE_NO_PAD.encode(first_certificate(&chain));

    // The certificate's own key, at the wrong endpoint: unknown there.
    let n = nonce(&app, "b").await;
    let body = cert_key.sign(
        &format!("{}/revokeCert", base("b")),
        &n,
        &json!({ "certificate": leaf }),
    );
    let res = post(&app, &path("b", "/revokeCert"), body).await;
    assert_eq!(res.status(), StatusCode::BAD_REQUEST);
    assert_eq!(
        body_json(res).await["type"],
        "urn:ietf:params:acme:error:malformed"
    );

    // …and at the right one it works, so the refusal above is about the
    // endpoint, not about the request being malformed in some other way.
    let n = nonce(&app, "a").await;
    let body = cert_key.sign(
        &format!("{}/revokeCert", base("a")),
        &n,
        &json!({ "certificate": leaf }),
    );
    assert_eq!(
        post(&app, &path("a", "/revokeCert"), body).await.status(),
        StatusCode::OK
    );
}

/// ARI is unauthenticated, so it is scoped the same way revocation is.
#[tokio::test]
async fn renewal_info_does_not_answer_for_another_profiles_certificate() {
    let app = test_app_with_profiles(vec![
        TestProfile::new("a").await,
        TestProfile::new("b").await,
    ])
    .await
    .0;
    let signer = EcSigner::new();
    let account = register(&app, "a", &signer).await;
    let chain = issue(&app, "a", &signer, &account, "ari.example.com").await;

    // RFC 9773 certID = base64url(AKI).base64url(serial), built from the
    // certificate exactly as a client would. Note each profile has its own CA,
    // so `b` would reject this identifier on the AKI half even if the serials
    // collided — but the isolation being tested here is the profile-scoped
    // lookup, which is why the assertion below is about `b` specifically.
    let leaf = first_certificate(&chain);
    let cert_id = acme_proxy_core::cert::ari_cert_id(&leaf).unwrap();

    let at_a = get(&app, &path("a", &format!("/renewalInfo/{cert_id}"))).await;
    assert_eq!(at_a.status(), StatusCode::OK);

    let at_b = get(&app, &path("b", &format!("/renewalInfo/{cert_id}"))).await;
    assert_eq!(
        at_b.status(),
        StatusCode::BAD_REQUEST,
        "`b` knows nothing about `a`'s certificates"
    );
}

/// A filter that denies every connection, for the per-profile filtering test.
struct DenyAll;

#[async_trait]
impl Check for DenyAll {
    fn kind(&self) -> &'static str {
        "deny-all"
    }

    fn stages(&self) -> StageSet {
        StageSet::both()
    }

    async fn check_connection(&self, _ctx: &ConnectionContext<'_>) -> Verdict {
        Verdict::Fail("denied by policy".to_string())
    }

    async fn check_identifiers(&self, _ctx: &IdentifierContext<'_>) -> Verdict {
        Verdict::Pass
    }
}

/// Filters are per endpoint: one profile can be closed while the other is open,
/// which is the point of running both in one process.
#[tokio::test]
async fn filters_apply_to_their_own_profile_only() {
    let closed = common::policy_with(Arc::new(DenyAll));
    let app = test_app_with_profiles(vec![
        TestProfile::new("open").await,
        TestProfile::new("closed").await.with_filter(closed),
    ])
    .await
    .0;

    let peer = "192.168.1.5:40000";
    let open = send_from(
        &app,
        Request::get(path("open", "/directory"))
            .body(Body::empty())
            .unwrap(),
        peer,
    )
    .await;
    assert_eq!(open.status(), StatusCode::OK);

    let closed = send_from(
        &app,
        Request::get(path("closed", "/directory"))
            .body(Body::empty())
            .unwrap(),
        peer,
    )
    .await;
    assert_eq!(closed.status(), StatusCode::FORBIDDEN);
    // That a refusal still hands back a usable nonce is a POST-only property
    // now (RFC 8555 §6.5), so it is asserted where it can be — over a signed
    // POST, in `filters.rs::a_rejection_still_carries_a_replay_nonce`. What
    // this test is about is which profile the filter applied to.
}

/// Server-level routes live at the root and nowhere else; ACME lives under a
/// profile and nowhere else. Anything in between is a 404, never a surprise.
#[tokio::test]
async fn routing_separates_server_routes_from_acme_routes() {
    let app = two_profiles().await;

    assert_eq!(get(&app, "/health").await.status(), StatusCode::OK);
    assert_eq!(
        get(&app, &path("a", "/health")).await.status(),
        StatusCode::NOT_FOUND,
        "/health is a server route, not part of an endpoint"
    );
    assert_eq!(
        get(&app, "/directory").await.status(),
        StatusCode::NOT_FOUND,
        "nothing ACME is served at the root"
    );
    assert_eq!(
        get(&app, "/profile/nope/directory").await.status(),
        StatusCode::NOT_FOUND,
        "an unconfigured profile is simply not mounted"
    );
}

/// Nonces are process-wide by design. Replay protection is unaffected: the §6.4
/// `url` check is what binds a request to its endpoint, and a nonce is single
/// use wherever it is spent.
#[tokio::test]
async fn a_nonce_minted_at_one_profile_is_accepted_at_another() {
    let app = test_app_with_profiles(vec![
        TestProfile::new("a").await,
        TestProfile::new("b").await,
    ])
    .await
    .0;
    let signer = EcSigner::new();

    let n = nonce(&app, "a").await;
    let body = signer.sign(
        &format!("{}/newAccount", base("b")),
        &n,
        &json!({ "termsOfServiceAgreed": true }),
    );
    let res = post(&app, &path("b", "/newAccount"), body).await;
    assert_eq!(res.status(), StatusCode::CREATED);
}

/// The same client key registering at both endpoints gets two independent
/// accounts — different ids, and deactivating one leaves the other working.
#[tokio::test]
async fn one_key_yields_one_account_per_profile() {
    let app = test_app_with_profiles(vec![
        TestProfile::new("a").await,
        TestProfile::new("b").await,
    ])
    .await
    .0;
    let signer = EcSigner::new();

    let at_a = register(&app, "a", &signer).await;
    let at_b = register(&app, "b", &signer).await;
    assert_ne!(at_a, at_b, "one key, two endpoints, two accounts");

    // Deactivate at `a`.
    let n = nonce(&app, "a").await;
    let body = signer.sign_kid(&at_a, &at_a, &n, &json!({ "status": "deactivated" }));
    assert_eq!(
        post(&app, to_path(&at_a), body).await.status(),
        StatusCode::OK
    );

    // `b` is untouched: it still accepts an order from the same key.
    let n = nonce(&app, "b").await;
    let payload = json!({ "identifiers": [{ "type": "dns", "value": "example.com" }] });
    let body = signer.sign_kid(&at_b, &format!("{}/newOrder", base("b")), &n, &payload);
    let res: Response = post(&app, &path("b", "/newOrder"), body).await;
    assert_eq!(res.status(), StatusCode::CREATED);

    // …while `a` refuses, as a deactivated account must.
    let n = nonce(&app, "a").await;
    let payload = json!({ "identifiers": [{ "type": "dns", "value": "example.com" }] });
    let body = signer.sign_kid(&at_a, &format!("{}/newOrder", base("a")), &n, &payload);
    let res = post(&app, &path("a", "/newOrder"), body).await;
    assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
    let _: Value = body_json(res).await;
}