Skip to content

Commit 2e1cf38

Browse files
test(conformance): assert documented error codes on L7 and pipelined denials
Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
1 parent 84b2ab0 commit 2e1cf38

6 files changed

Lines changed: 224 additions & 431 deletions

File tree

‎tests/suites/conformance/src/network_egress.rs‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,15 +406,25 @@ fn rest_policy(port: u16, method: &str) -> Result<proto::SandboxPolicy> {
406406
}
407407

408408
/// Send `requests` pipelined in one write, and fail unless the first gets
409-
/// `200` and the second `403`.
409+
/// `200` and the second a `403` with `policy_denied`.
410410
async fn run_pipeline(sandbox: &Sandbox, port: u16, requests: &str) -> Result {
411411
let reply = http_raw(sandbox, TEST_HOST, port, requests.as_bytes()).await?;
412412
let received = reply.received_text();
413+
let denial_error = received
414+
.split_once("HTTP/1.1 403 Forbidden\r\n")
415+
.and_then(|(_, denial)| denial.split_once("\r\n\r\n"))
416+
.and_then(|(_, body)| {
417+
serde_json::Deserializer::from_str(body)
418+
.into_iter::<Value>()
419+
.next()?
420+
.ok()
421+
})
422+
.and_then(|body| body.get("error")?.as_str().map(str::to_owned));
413423
testkit::ensure!(
414424
reply.status() == Some(200)
415425
&& received.matches("HTTP/1.1 ").count() == 2
416-
&& received.contains("HTTP/1.1 403 Forbidden"),
417-
"expected 200 then 403; got {reply:?}"
426+
&& denial_error.as_deref() == Some("policy_denied"),
427+
"expected 200 then 403 with policy_denied; got {reply:?}"
418428
);
419429
Ok(())
420430
}

‎tests/suites/conformance/src/network_l7.rs‎

Lines changed: 68 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,17 @@ async fn rest_rules_allow_get_and_deny_post(cx: &mut Context) -> testkit::Result
2929
.policy(rest_policy(server.port, "enforce")?)
3030
.create_ready()
3131
.await?;
32-
for (request, expected) in [
33-
(clients::Request::get("/allowed"), 200),
34-
(clients::Request::new("POST", "/allowed").body("test"), 403),
32+
for (request, (status, error)) in [
33+
(clients::Request::get("/allowed"), ALLOWED),
34+
(
35+
clients::Request::new("POST", "/allowed").body("test"),
36+
POLICY_DENIED,
37+
),
3538
] {
3639
let reply = http(&sandbox, TEST_HOST, server.port, &request).await?;
3740
testkit::ensure!(
38-
reply.status() == Some(expected),
39-
"expected {expected} for {request:?}; got {reply:?}"
41+
reply.status() == Some(status) && error_code(&reply).as_deref() == error,
42+
"expected {status} with error {error:?} for {request:?}; got {reply:?}"
4043
);
4144
}
4245
let methods: Vec<String> = server
@@ -127,21 +130,21 @@ async fn graphql_rules_allow_and_deny_operations(cx: &mut Context) -> testkit::R
127130
.await?;
128131
let mut expected = Vec::new();
129132
for path in ["forward", "raw"] {
130-
for (key, status) in [
131-
("query_allowed", 200),
132-
("get_query_allowed", 200),
133-
("duplicate_get_denied", 403),
134-
("persisted_get_allowed", 200),
135-
("unregistered_persisted_get_denied", 403),
136-
("chunked_query_allowed", 200),
137-
("unlisted_field_denied", 403),
138-
("mutation_allowed", 200),
139-
("deny_rule_denied", 403),
133+
for (key, outcome) in [
134+
("query_allowed", ALLOWED),
135+
("get_query_allowed", ALLOWED),
136+
("duplicate_get_denied", POLICY_DENIED),
137+
("persisted_get_allowed", ALLOWED),
138+
("unregistered_persisted_get_denied", POLICY_DENIED),
139+
("chunked_query_allowed", ALLOWED),
140+
("unlisted_field_denied", POLICY_DENIED),
141+
("mutation_allowed", ALLOWED),
142+
("deny_rule_denied", POLICY_DENIED),
140143
] {
141-
expected.push((format!("{path}_{key}"), status));
144+
expected.push((format!("{path}_{key}"), outcome));
142145
}
143146
}
144-
expect_statuses(&output, expected)
147+
expect_outcomes(&output, expected)
145148
}
146149

147150
/// JSON-RPC rules allow listed methods whatever their params, deny a
@@ -194,26 +197,26 @@ async fn jsonrpc_rules_allow_and_deny_methods(cx: &mut Context) -> testkit::Resu
194197
.await?;
195198
let mut expected = Vec::new();
196199
for path in ["forward", "raw"] {
197-
for (key, status) in [
198-
("method_initialize_allowed", 200),
199-
("method_tools_list_allowed", 200),
200-
("method_tools_call_allowed", 200),
201-
("method_tools_call_with_unmatched_params_allowed", 200),
202-
("method_tools_delete_denied", 403),
203-
("unlisted_method_denied", 403),
200+
for (key, outcome) in [
201+
("method_initialize_allowed", ALLOWED),
202+
("method_tools_list_allowed", ALLOWED),
203+
("method_tools_call_allowed", ALLOWED),
204+
("method_tools_call_with_unmatched_params_allowed", ALLOWED),
205+
("method_tools_delete_denied", POLICY_DENIED),
206+
("unlisted_method_denied", POLICY_DENIED),
204207
] {
205-
expected.push((format!("{path}_{key}"), status));
208+
expected.push((format!("{path}_{key}"), outcome));
206209
}
207210
}
208-
for (key, status) in [
209-
("rest_route_allowed", 200),
210-
("forward_batch_all_allowed", 200),
211-
("forward_batch_one_denied", 403),
212-
("forward_invalid_json_denied", 403),
211+
for (key, outcome) in [
212+
("rest_route_allowed", ALLOWED),
213+
("forward_batch_all_allowed", ALLOWED),
214+
("forward_batch_one_denied", POLICY_DENIED),
215+
("forward_invalid_json_denied", POLICY_DENIED),
213216
] {
214-
expected.push((key.to_string(), status));
217+
expected.push((key.to_string(), outcome));
215218
}
216-
expect_statuses(&output, expected)
219+
expect_outcomes(&output, expected)
217220
}
218221

219222
/// A JSON-RPC endpoint without `enforcement` audits an unlisted method and
@@ -252,11 +255,14 @@ async fn jsonrpc_default_audit_mode_still_denies_response_frames(
252255
let output = sandbox
253256
.run_python(scripts::JSONRPC_AUDIT_CLIENT, &[TEST_HOST, &port(&server)])
254257
.await?;
255-
expect_statuses(
258+
expect_outcomes(
256259
&output,
257260
[
258-
("forward_unknown_method_audited".to_string(), 200),
259-
("forward_response_frame_hard_denied".to_string(), 403),
261+
("forward_unknown_method_audited".to_string(), ALLOWED),
262+
(
263+
"forward_response_frame_hard_denied".to_string(),
264+
POLICY_DENIED,
265+
),
260266
],
261267
)
262268
}
@@ -317,36 +323,55 @@ async fn expect_upgrade_refused(sandbox: &Sandbox, port: u16) -> Result {
317323
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nSec-WebSocket-Version: 13\r\n\r\n"
318324
);
319325
let reply = http_raw(sandbox, TEST_HOST, port, request.as_bytes()).await?;
320-
let received = reply.received_text();
321326
testkit::ensure!(
322327
reply.status() == Some(403)
323-
&& received.contains("unsupported_l7_protocol")
324-
&& received.contains("REST endpoints"),
328+
&& error_code(&reply).as_deref() == Some("unsupported_l7_protocol")
329+
&& reply.received_text().contains("REST endpoints"),
325330
"expected the upgrade to {target} refused; got {reply:?}"
326331
);
327332
}
328333
Ok(())
329334
}
330335

336+
/// A response's status and the `error` code in its JSON body, if any.
337+
type Outcome = (u16, Option<&'static str>);
338+
339+
/// Upstream answered.
340+
const ALLOWED: Outcome = (200, None);
341+
342+
/// The sandbox denied the request by policy, as documented under "Proxy
343+
/// Error Responses" in `docs/observability/logging.mdx`.
344+
const POLICY_DENIED: Outcome = (403, Some("policy_denied"));
345+
331346
/// Fail unless the client's JSON result maps exactly the keys in
332-
/// `expected` to their statuses.
333-
fn expect_statuses(
347+
/// `expected` to their statuses and `error` codes.
348+
fn expect_outcomes(
334349
output: &testkit::ExecOutput,
335-
expected: impl IntoIterator<Item = (String, u16)>,
350+
expected: impl IntoIterator<Item = (String, Outcome)>,
336351
) -> Result {
337352
let result = output.last_json()?;
338353
let expected: Value = expected
339354
.into_iter()
340-
.map(|(key, status)| (key, json!(status)))
355+
.map(|(key, (status, error))| (key, json!({"status": status, "error": error})))
341356
.collect::<serde_json::Map<_, _>>()
342357
.into();
343358
testkit::ensure!(
344359
result == expected,
345-
"unexpected statuses:\nexpected: {expected}\ngot: {result}"
360+
"unexpected outcomes:\nexpected: {expected}\ngot: {result}"
346361
);
347362
Ok(())
348363
}
349364

365+
/// The `error` code in the JSON body of `reply`'s response, if any.
366+
pub(crate) fn error_code(reply: &clients::Http) -> Option<String> {
367+
let body = &reply.response.as_ref()?.body;
368+
let error = serde_json::from_slice::<Value>(body)
369+
.ok()?
370+
.get("error")?
371+
.clone();
372+
error.as_str().map(str::to_owned)
373+
}
374+
350375
fn ok_json() -> Response {
351376
Response::json(&json!({"ok": true}))
352377
}

0 commit comments

Comments
 (0)