Skip to content

Commit 00ca7a0

Browse files
committed
F
1 parent 9e0b391 commit 00ca7a0

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

lib/executor/src/executors/error.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ pub enum SubgraphExecutorError {
2020
RequestFailure(String, String),
2121
#[error("Failed to serialize variable \"{0}\": {1}")]
2222
VariablesSerializationFailure(String, String),
23-
#[error("HMAC signature error: {0}")]
24-
HMACSignatureError(String),
2523
#[error("Failed to serialize extension \"{0}\": {1}")]
2624
ExtensionSerializationFailure(String, String),
25+
#[error("Failed to build HMAC expression for subgraph '{0}'. Please check your VRL expression for syntax errors. Diagnostic: {1}")]
26+
HMACExpressionBuild(String, String),
27+
#[error("HMAC signature error: {0}")]
28+
HMACSignatureError(String),
2729
}
2830

2931
impl From<SubgraphExecutorError> for GraphQLError {
@@ -69,6 +71,9 @@ impl SubgraphExecutorError {
6971
"SUBGRAPH_EXTENSION_SERIALIZATION_FAILURE"
7072
}
7173
SubgraphExecutorError::HMACSignatureError(_) => "SUBGRAPH_HMAC_SIGNATURE_ERROR",
74+
SubgraphExecutorError::HMACExpressionBuild(_, _) => {
75+
"SUBGRAPH_HMAC_EXPRESSION_BUILD_FAILURE"
76+
}
7277
}
7378
}
7479
}

lib/executor/src/executors/http.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::sync::Arc;
33

44
use crate::executors::common::HttpExecutionResponse;
55
use crate::executors::dedupe::{request_fingerprint, ABuildHasher, SharedResponse};
6+
use crate::utils::expression::execute_expression_with_value;
67
use dashmap::DashMap;
7-
use hive_router_config::hmac_signature::BooleanOrExpression;
88
use hive_router_config::HiveRouterConfig;
99
use tokio::sync::OnceCell;
1010

@@ -22,6 +22,7 @@ use hyper_util::client::legacy::{connect::HttpConnector, Client};
2222
use sha2::Sha256;
2323
use tokio::sync::Semaphore;
2424
use tracing::debug;
25+
use vrl::compiler::Program as VrlProgram;
2526

2627
use crate::executors::common::HttpExecutionRequest;
2728
use crate::executors::error::SubgraphExecutorError;
@@ -42,6 +43,7 @@ pub struct HTTPSubgraphExecutor {
4243
pub semaphore: Arc<Semaphore>,
4344
pub config: Arc<HiveRouterConfig>,
4445
pub in_flight_requests: Arc<DashMap<u64, Arc<OnceCell<SharedResponse>>, ABuildHasher>>,
46+
pub should_sign_hmac: BooleanOrProgram,
4547
}
4648

4749
const FIRST_VARIABLE_STR: &[u8] = b",\"variables\":{";
@@ -52,6 +54,12 @@ pub type HttpClient = Client<HttpsConnector<HttpConnector>, Full<Bytes>>;
5254

5355
type HmacSha256 = Hmac<Sha256>;
5456

57+
#[derive(Debug)]
58+
pub enum BooleanOrProgram {
59+
Boolean(bool),
60+
Program(Box<VrlProgram>),
61+
}
62+
5563
impl HTTPSubgraphExecutor {
5664
pub fn new(
5765
subgraph_name: String,
@@ -60,6 +68,7 @@ impl HTTPSubgraphExecutor {
6068
semaphore: Arc<Semaphore>,
6169
config: Arc<HiveRouterConfig>,
6270
in_flight_requests: Arc<DashMap<u64, Arc<OnceCell<SharedResponse>>, ABuildHasher>>,
71+
should_sign_hmac: BooleanOrProgram,
6372
) -> Self {
6473
let mut header_map = HeaderMap::new();
6574
header_map.insert(
@@ -79,6 +88,7 @@ impl HTTPSubgraphExecutor {
7988
semaphore,
8089
config,
8190
in_flight_requests,
91+
should_sign_hmac,
8292
}
8393
}
8494

@@ -126,9 +136,9 @@ impl HTTPSubgraphExecutor {
126136
body.put(CLOSE_BRACE);
127137
}
128138

129-
let should_sign_hmac = match &self.config.hmac_signature.enabled {
130-
BooleanOrExpression::Boolean(b) => *b,
131-
BooleanOrExpression::Expression(expr) => {
139+
let should_sign_hmac = match &self.should_sign_hmac {
140+
BooleanOrProgram::Boolean(b) => *b,
141+
BooleanOrProgram::Program(expr) => {
132142
// .subgraph
133143
let subgraph_value = VrlValue::Object(BTreeMap::from([(
134144
"name".into(),
@@ -140,7 +150,7 @@ impl HTTPSubgraphExecutor {
140150
("subgraph".into(), subgraph_value),
141151
("request".into(), request_value),
142152
]));
143-
let result = expr.execute_with_value(target_value);
153+
let result = execute_expression_with_value(expr, target_value);
144154
match result {
145155
Ok(VrlValue::Boolean(b)) => b,
146156
Ok(_) => {

lib/executor/src/executors/map.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use std::{
66

77
use bytes::{BufMut, Bytes, BytesMut};
88
use dashmap::DashMap;
9-
use hive_router_config::{override_subgraph_urls::UrlOrExpression, HiveRouterConfig};
9+
use hive_router_config::{
10+
hmac_signature::BooleanOrExpression, override_subgraph_urls::UrlOrExpression, HiveRouterConfig,
11+
};
1012
use http::Uri;
1113
use hyper_tls::HttpsConnector;
1214
use hyper_util::{
@@ -25,7 +27,7 @@ use crate::{
2527
},
2628
dedupe::{ABuildHasher, SharedResponse},
2729
error::SubgraphExecutorError,
28-
http::{HTTPSubgraphExecutor, HttpClient},
30+
http::{BooleanOrProgram, HTTPSubgraphExecutor, HttpClient},
2931
},
3032
response::graphql_error::GraphQLError,
3133
utils::expression::{compile_expression, execute_expression_with_value},
@@ -297,13 +299,24 @@ impl SubgraphExecutorMap {
297299
.or_insert_with(|| Arc::new(Semaphore::new(self.max_connections_per_host)))
298300
.clone();
299301

302+
let should_sign_hmac = match &self.config.hmac_signature.enabled {
303+
BooleanOrExpression::Boolean(b) => BooleanOrProgram::Boolean(*b),
304+
BooleanOrExpression::Expression { expression } => {
305+
let program = compile_expression(expression, None).map_err(|err| {
306+
SubgraphExecutorError::HMACExpressionBuild(subgraph_name.to_string(), err)
307+
})?;
308+
BooleanOrProgram::Program(Box::new(program))
309+
}
310+
};
311+
300312
let executor = HTTPSubgraphExecutor::new(
301313
subgraph_name.to_string(),
302314
endpoint_uri,
303315
self.client.clone(),
304316
semaphore,
305317
self.config.clone(),
306318
self.in_flight_requests.clone(),
319+
should_sign_hmac,
307320
);
308321

309322
self.executors_by_subgraph

lib/router-config/src/hmac_signature.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use schemars::JsonSchema;
22
use serde::{Deserialize, Serialize};
33

4-
use crate::primitives::expression::Expression;
5-
64
#[derive(Debug, Clone, Deserialize, Serialize, Default, JsonSchema)]
75
pub struct HMACSignatureConfig {
86
// Whether to sign outgoing requests with HMAC signatures.
@@ -34,9 +32,10 @@ fn default_extension_name() -> String {
3432
}
3533

3634
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
35+
#[serde(untagged)]
3736
pub enum BooleanOrExpression {
3837
Boolean(bool),
39-
Expression(Expression),
38+
Expression { expression: String },
4039
}
4140

4241
impl Default for BooleanOrExpression {
@@ -49,7 +48,7 @@ impl HMACSignatureConfig {
4948
pub fn is_disabled(&self) -> bool {
5049
match &self.enabled {
5150
BooleanOrExpression::Boolean(b) => !*b,
52-
BooleanOrExpression::Expression(_) => {
51+
BooleanOrExpression::Expression { expression: _ } => {
5352
// If it's an expression, we consider it enabled for the purpose of this check.
5453
false
5554
}

0 commit comments

Comments
 (0)