Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pingora-router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ pingora = { version = "0.5.0", features = ["lb", "boringssl"] }
serde = "1.0.219"
serde_json = "1.0.140"
chrono = "0.4.40"
uuid = "1.16.0"
uuid = "1.16.0"
bincode = "2.0.1"
3 changes: 2 additions & 1 deletion reverse-proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ futures = "0.3.31"
boring = "4.17.0"
once_cell = "1.21.3"
dotenv = "0.15.0"
ntor = { git = "https://github.com/globe-and-citizen/ntor.git", tag = "0.1.1" }
ntor = { git = "https://github.com/globe-and-citizen/ntor.git", branch = "feat/provide-bincode-serde"}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just use tag or version

config = "0.15.11"
toml = "0.8.23"
uuid = { version = "1.16.0", features = ["v4"] }
utils = { path = "../utils", version = "0.1.0" }
envy = "0.4.2"
hex = "0.4.3"
tracing = "0.1.41"
bincode = "2.0.1"
3 changes: 2 additions & 1 deletion reverse-proxy/src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,10 @@ impl ReverseHandler {
shared_secret,
) {
Ok(encrypted_message) => {
let body = bincode::encode_to_vec(encrypted_message, bincode::config::standard()).expect("we expect the encrypted message to bincode serializable");
APIHandlerResponse {
status: StatusCode::OK,
body: Some(encrypted_message.to_bytes()),
body: Some(body),
}
}
Err(res) => res
Expand Down
76 changes: 39 additions & 37 deletions reverse-proxy/src/handler/proxy/handler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use pingora_router::ctx::{Layer8Context, Layer8ContextTrait};
use reqwest::header::HeaderMap;
use pingora_router::handler::{APIHandlerResponse, DefaultHandlerTrait, ResponseBodyTrait};
use ntor::common::NTorParty;
use ntor::common::{EncryptedMessage, NTorParty};
use ntor::server::NTorServer;
use reqwest::Client;
use pingora::http::StatusCode;
Expand All @@ -10,7 +10,7 @@ use utils::bytes_to_json;
use utils::jwt::JWTClaims;
use crate::handler::common::consts::{HeaderKeys, LogTypes};
use crate::handler::common::types::ErrorResponse;
use crate::handler::proxy::{EncryptedMessage, L8ResponseObject, L8RequestObject};
use crate::handler::proxy::{L8ResponseObject, L8RequestObject};

/// Struct containing only associated methods (no instance methods or fields)
pub struct ProxyHandler {}
Expand Down Expand Up @@ -103,28 +103,25 @@ impl ProxyHandler {
{
let correlation_id = ctx.get_correlation_id();

match ProxyHandler::parse_request_body::<
EncryptedMessage,
ErrorResponse
>(&ctx.get_request_body()) {
Ok(res) => Ok(res),
// deserialize from bincode
match bincode::decode_from_slice(&ctx.get_response_body(), bincode::config::standard()) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you keep avoiding ProxyHandler::parse_request_body (or pingora-router utils), what's your reason?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The init handshake still uses json

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The init handshake still uses json

is it related to my above question?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll get the bincode into the utils

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You alluded to us having that conversation/ticket later

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You alluded to us having that conversation/ticket later

Haha, yeah. But I’m honestly curious about your reason for not using the utils? If you feel a utility should be removed because it’s unnecessary, it’s important to look at its usages and the original purpose behind it.

Ok((res, _)) => Ok(res),
Err(err) => {
let body = match err {
None => None,
Some(err_response) => {
error!(
%correlation_id,
log_type=LogTypes::HANDLE_PROXY_REQUEST,
"Error parsing request body: {}",
err_response.error
);
Some(err_response.to_bytes())
}
};
Err(APIHandlerResponse {
error!(
%correlation_id,
log_type=LogTypes::HANDLE_PROXY_REQUEST,
"Error parsing request body: {}",
err
);
return Err(APIHandlerResponse {
status: StatusCode::BAD_REQUEST,
body,
})
body: Some(
ErrorResponse {
error: format!("Error parsing request body: {}", err),
}
.to_bytes(),
),
});
}
}
}
Expand All @@ -139,25 +136,30 @@ impl ProxyHandler {
ntor_server.set_shared_secret(shared_secret.clone());

// Decrypt the request body using nTor shared secret
let decrypted_data = ntor_server.decrypt(ntor::common::EncryptedMessage {
nonce: <[u8; 12]>::try_from(request_body.nonce).unwrap_or_default(),
data: request_body.data,
}).map_err(|err| {
return APIHandlerResponse {
status: StatusCode::BAD_REQUEST,
body: Some(format!("Decryption failed: {}", err).as_bytes().to_vec()),
};
})?;
// let decrypted_data = request_body.data;

// parse decrypted data into WrappedUserRequest
let wrapped_request: L8RequestObject = bytes_to_json(decrypted_data)
let decrypted_data = ntor_server
.decrypt(ntor::common::EncryptedMessage {
nonce: <[u8; 12]>::try_from(request_body.nonce).unwrap_or_default(),
data: request_body.data,
})
.map_err(|err| {
return APIHandlerResponse {
status: StatusCode::BAD_REQUEST,
body: Some(format!("Failed to parse request body: {}", err).as_bytes().to_vec()),
body: Some(format!("Decryption failed: {}", err).as_bytes().to_vec()),
};
})?;
// let decrypted_data = request_body.data;

// parse decrypted data into WrappedUserRequest
let wrapped_request: L8RequestObject = bytes_to_json(decrypted_data).map_err(|err| {
return APIHandlerResponse {
status: StatusCode::BAD_REQUEST,
body: Some(
format!("Failed to parse request body: {}", err)
.as_bytes()
.to_vec(),
),
};
})?;

Ok(wrapped_request)
}
Expand Down Expand Up @@ -269,7 +271,7 @@ impl ProxyHandler {
})?;

Ok(EncryptedMessage {
nonce: encrypted_data.nonce.to_vec(),
nonce: encrypted_data.nonce,
data: encrypted_data.data,
})
}
Expand Down
9 changes: 0 additions & 9 deletions reverse-proxy/src/handler/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@ use std::collections::HashMap;
use pingora_router::handler::{RequestBodyTrait, ResponseBodyTrait};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
pub struct EncryptedMessage {
pub nonce: Vec<u8>,
pub data: Vec<u8>,
}

impl RequestBodyTrait for EncryptedMessage {}
impl ResponseBodyTrait for EncryptedMessage {}

#[derive(Serialize, Deserialize, Debug)]
pub struct L8RequestObject {
pub method: String,
Expand Down
Loading