Skip to content

Commit 8d59392

Browse files
committed
Tests
1 parent 066a04c commit 8d59392

File tree

6 files changed

+94
-16
lines changed

6 files changed

+94
-16
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

e2e/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ subgraphs = { path = "../bench/subgraphs" }
2424

2525
mockito = "1.7.0"
2626
tempfile = "3.23.0"
27+
28+
hmac = "0.12.1"
29+
sha2 = "0.10.9"
30+
hex = "0.4.3"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
hmac_signature:
2+
enabled: true
3+
secret: VERY_SECRET

e2e/src/hmac.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#[cfg(test)]
2+
mod hmac_e2e_tests {
3+
use crate::testkit::{
4+
init_graphql_request, init_router_from_config_file, wait_for_readiness, SubgraphsServer,
5+
};
6+
7+
use ntex::web::test;
8+
use sonic_rs::JsonValueTrait;
9+
10+
fn create_hmac_signature(secret: &str, query: &str) -> String {
11+
use hex;
12+
use hmac::{Hmac, Mac};
13+
use sha2::Sha256;
14+
15+
type HmacSha256 = Hmac<Sha256>;
16+
17+
let mut mac =
18+
HmacSha256::new_from_slice(secret.as_bytes()).expect("HMAC can take key of any size");
19+
let message = format!("{{\"query\":\"{}\"}}", query);
20+
mac.update(message.as_bytes());
21+
let result = mac.finalize();
22+
let code_bytes = result.into_bytes();
23+
hex::encode(code_bytes)
24+
}
25+
26+
#[ntex::test]
27+
async fn should_forward_hmac_signature_to_subgraph_via_extensions() {
28+
let subgraphs_server = SubgraphsServer::start().await;
29+
let app = init_router_from_config_file("configs/hmac_forward.router.yaml")
30+
.await
31+
.unwrap();
32+
wait_for_readiness(&app.app).await;
33+
let query = "query{users{id}}";
34+
let req = init_graphql_request(query, None);
35+
let resp: ntex::web::WebResponse = test::call_service(&app.app, req.to_request()).await;
36+
37+
assert!(resp.status().is_success(), "Expected 200 OK");
38+
39+
let subgraph_requests = subgraphs_server
40+
.get_subgraph_requests_log("accounts")
41+
.await
42+
.expect("expected requests sent to accounts subgraph");
43+
assert_eq!(
44+
subgraph_requests.len(),
45+
1,
46+
"expected 1 request to accounts subgraph"
47+
);
48+
let extensions = subgraph_requests[0].request_body.get("extensions").unwrap();
49+
50+
let expected_signature = create_hmac_signature("VERY_SECRET", query);
51+
assert_eq!(
52+
extensions.get("hmac_signature").unwrap(),
53+
&expected_signature
54+
);
55+
}
56+
}

e2e/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ mod file_supergraph;
33
#[cfg(test)]
44
mod hive_cdn_supergraph;
55
#[cfg(test)]
6+
mod hmac;
7+
#[cfg(test)]
68
mod jwt;
79
#[cfg(test)]
810
mod override_subgraph_urls;

lib/executor/src/executors/http.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -186,25 +186,30 @@ impl HTTPSubgraphExecutor {
186186
None
187187
};
188188

189-
if let Some(extensions) = &execution_request.extensions {
190-
let mut first = true;
191-
if let Some(hmac_bytes) = hmac_signature_ext {
192-
if first {
193-
body.put(FIRST_EXTENSION_STR);
194-
first = false;
195-
} else {
196-
body.put(COMMA);
197-
}
198-
body.put(self.config.hmac_signature.extension_name.as_bytes());
199-
let hmac_hex = hex::encode(hmac_bytes);
200-
body.put(QUOTE);
201-
body.put(hmac_hex.as_bytes());
202-
body.put(QUOTE);
189+
let mut first_extension = true;
190+
191+
if let Some(hmac_bytes) = hmac_signature_ext {
192+
if first_extension {
193+
body.put(FIRST_EXTENSION_STR);
194+
first_extension = false;
195+
} else {
196+
body.put(COMMA);
203197
}
198+
body.put(QUOTE);
199+
body.put(self.config.hmac_signature.extension_name.as_bytes());
200+
body.put(QUOTE);
201+
body.put(COLON);
202+
let hmac_hex = hex::encode(hmac_bytes);
203+
body.put(QUOTE);
204+
body.put(hmac_hex.as_bytes());
205+
body.put(QUOTE);
206+
}
207+
208+
if let Some(extensions) = &execution_request.extensions {
204209
for (extension_name, extension_value) in extensions {
205-
if first {
210+
if first_extension {
206211
body.put(FIRST_EXTENSION_STR);
207-
first = false;
212+
first_extension = false;
208213
} else {
209214
body.put(COMMA);
210215
}
@@ -222,8 +227,13 @@ impl HTTPSubgraphExecutor {
222227
}
223228
}
224229

230+
if !first_extension {
231+
body.put(CLOSE_BRACE);
232+
}
233+
225234
body.put(CLOSE_BRACE);
226235

236+
println!("Built request body: {}", String::from_utf8_lossy(&body));
227237
Ok(body)
228238
}
229239

0 commit comments

Comments
 (0)