Skip to content

Commit

Permalink
pass test with no connection token
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgb committed Sep 24, 2024
1 parent aad643a commit 28d0721
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 28 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions plane/plane-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ tracing = "0.1.40"
tracing-appender = "0.2.2"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
url = "2.4.1"

[dev-dependencies]
http = "1.1.0"
http-body-util = "0.1.2"
serde = "1.0.210"
4 changes: 3 additions & 1 deletion plane/plane-tests/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ use tokio::time::timeout;
pub mod async_drop;
pub mod auth_mock;
pub mod docker;
pub mod proxy_mock;
pub mod resources;
pub mod simple_axum_server;
pub mod test_env;
pub mod timeout;
pub mod timeout; // NOTE: copied from dynamic-proxy

pub fn run_test<F, Fut>(name: &str, time_limit: Duration, test_function: F)
where
Expand Down
66 changes: 66 additions & 0 deletions plane/plane-tests/tests/common/proxy_mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use dynamic_proxy::server::{HttpsConfig, SimpleHttpServer};
use plane::{
protocol::{RouteInfoRequest, RouteInfoResponse},
proxy::proxy_server::ProxyState,
};
use std::net::SocketAddr;
use tokio::{net::TcpListener, sync::mpsc};

pub struct MockProxy {

Check warning on line 9 in plane/plane-tests/tests/common/proxy_mock.rs

View workflow job for this annotation

GitHub Actions / clippy

struct `MockProxy` is never constructed

warning: struct `MockProxy` is never constructed --> plane/plane-tests/tests/common/proxy_mock.rs:9:12 | 9 | pub struct MockProxy { | ^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default

Check warning on line 9 in plane/plane-tests/tests/common/proxy_mock.rs

View workflow job for this annotation

GitHub Actions / clippy

struct `MockProxy` is never constructed

warning: struct `MockProxy` is never constructed --> plane/plane-tests/tests/common/proxy_mock.rs:9:12 | 9 | pub struct MockProxy { | ^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default

Check warning on line 9 in plane/plane-tests/tests/common/proxy_mock.rs

View workflow job for this annotation

GitHub Actions / clippy

struct `MockProxy` is never constructed

warning: struct `MockProxy` is never constructed --> plane/plane-tests/tests/common/proxy_mock.rs:9:12 | 9 | pub struct MockProxy { | ^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default

Check warning on line 9 in plane/plane-tests/tests/common/proxy_mock.rs

View workflow job for this annotation

GitHub Actions / clippy

struct `MockProxy` is never constructed

warning: struct `MockProxy` is never constructed --> plane/plane-tests/tests/common/proxy_mock.rs:9:12 | 9 | pub struct MockProxy { | ^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default

Check warning on line 9 in plane/plane-tests/tests/common/proxy_mock.rs

View workflow job for this annotation

GitHub Actions / clippy

struct `MockProxy` is never constructed

warning: struct `MockProxy` is never constructed --> plane/plane-tests/tests/common/proxy_mock.rs:9:12 | 9 | pub struct MockProxy { | ^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default

Check warning on line 9 in plane/plane-tests/tests/common/proxy_mock.rs

View workflow job for this annotation

GitHub Actions / clippy

struct `MockProxy` is never constructed

warning: struct `MockProxy` is never constructed --> plane/plane-tests/tests/common/proxy_mock.rs:9:12 | 9 | pub struct MockProxy { | ^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default

Check warning on line 9 in plane/plane-tests/tests/common/proxy_mock.rs

View workflow job for this annotation

GitHub Actions / clippy

struct `MockProxy` is never constructed

warning: struct `MockProxy` is never constructed --> plane/plane-tests/tests/common/proxy_mock.rs:9:12 | 9 | pub struct MockProxy { | ^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default

Check warning on line 9 in plane/plane-tests/tests/common/proxy_mock.rs

View workflow job for this annotation

GitHub Actions / clippy

struct `MockProxy` is never constructed

warning: struct `MockProxy` is never constructed --> plane/plane-tests/tests/common/proxy_mock.rs:9:12 | 9 | pub struct MockProxy { | ^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default

Check warning on line 9 in plane/plane-tests/tests/common/proxy_mock.rs

View workflow job for this annotation

GitHub Actions / clippy

struct `MockProxy` is never constructed

warning: struct `MockProxy` is never constructed --> plane/plane-tests/tests/common/proxy_mock.rs:9:12 | 9 | pub struct MockProxy { | ^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default

Check warning on line 9 in plane/plane-tests/tests/common/proxy_mock.rs

View workflow job for this annotation

GitHub Actions / clippy

struct `MockProxy` is never constructed

warning: struct `MockProxy` is never constructed --> plane/plane-tests/tests/common/proxy_mock.rs:9:12 | 9 | pub struct MockProxy { | ^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default

Check warning on line 9 in plane/plane-tests/tests/common/proxy_mock.rs

View workflow job for this annotation

GitHub Actions / clippy

struct `MockProxy` is never constructed

warning: struct `MockProxy` is never constructed --> plane/plane-tests/tests/common/proxy_mock.rs:9:12 | 9 | pub struct MockProxy { | ^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default

Check warning on line 9 in plane/plane-tests/tests/common/proxy_mock.rs

View workflow job for this annotation

GitHub Actions / clippy

struct `MockProxy` is never constructed

warning: struct `MockProxy` is never constructed --> plane/plane-tests/tests/common/proxy_mock.rs:9:12 | 9 | pub struct MockProxy { | ^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default

Check warning on line 9 in plane/plane-tests/tests/common/proxy_mock.rs

View workflow job for this annotation

GitHub Actions / clippy

struct `MockProxy` is never constructed

warning: struct `MockProxy` is never constructed --> plane/plane-tests/tests/common/proxy_mock.rs:9:12 | 9 | pub struct MockProxy { | ^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default
proxy_state: ProxyState,
route_info_request_receiver: mpsc::Receiver<RouteInfoRequest>,
addr: SocketAddr,
server: SimpleHttpServer,
}

impl MockProxy {
pub async fn new() -> Self {

Check warning on line 17 in plane/plane-tests/tests/common/proxy_mock.rs

View workflow job for this annotation

GitHub Actions / clippy

associated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used

warning: associated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used --> plane/plane-tests/tests/common/proxy_mock.rs:17:18 | 16 | impl MockProxy { | -------------- associated items in this implementation 17 | pub async fn new() -> Self { | ^^^ ... 43 | pub fn addr(&self) -> SocketAddr { | ^^^^ ... 47 | pub async fn recv_route_info_request(&mut self) -> RouteInfoRequest { | ^^^^^^^^^^^^^^^^^^^^^^^ ... 54 | pub async fn expect_no_route_info_request(&mut self) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ... 63 | pub async fn send_route_info_response(&mut self, response: RouteInfoResponse) { | ^^^^^^^^^^^^^^^^^^^^^^^^

Check warning on line 17 in plane/plane-tests/tests/common/proxy_mock.rs

View workflow job for this annotation

GitHub Actions / clippy

associated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used

warning: associated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used --> plane/plane-tests/tests/common/proxy_mock.rs:17:18 | 16 | impl MockProxy { | -------------- associated items in this implementation 17 | pub async fn new() -> Self { | ^^^ ... 43 | pub fn addr(&self) -> SocketAddr { | ^^^^ ... 47 | pub async fn recv_route_info_request(&mut self) -> RouteInfoRequest { | ^^^^^^^^^^^^^^^^^^^^^^^ ... 54 | pub async fn expect_no_route_info_request(&mut self) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ... 63 | pub async fn send_route_info_response(&mut self, response: RouteInfoResponse) { | ^^^^^^^^^^^^^^^^^^^^^^^^

Check warning on line 17 in plane/plane-tests/tests/common/proxy_mock.rs

View workflow job for this annotation

GitHub Actions / clippy

associated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used

warning: associated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used --> plane/plane-tests/tests/common/proxy_mock.rs:17:18 | 16 | impl MockProxy { | -------------- associated items in this implementation 17 | pub async fn new() -> Self { | ^^^ ... 43 | pub fn addr(&self) -> SocketAddr { | ^^^^ ... 47 | pub async fn recv_route_info_request(&mut self) -> RouteInfoRequest { | ^^^^^^^^^^^^^^^^^^^^^^^ ... 54 | pub async fn expect_no_route_info_request(&mut self) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ... 63 | pub async fn send_route_info_response(&mut self, response: RouteInfoResponse) { | ^^^^^^^^^^^^^^^^^^^^^^^^

Check warning on line 17 in plane/plane-tests/tests/common/proxy_mock.rs

View workflow job for this annotation

GitHub Actions / clippy

associated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used

warning: associated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used --> plane/plane-tests/tests/common/proxy_mock.rs:17:18 | 16 | impl MockProxy { | -------------- associated items in this implementation 17 | pub async fn new() -> Self { | ^^^ ... 43 | pub fn addr(&self) -> SocketAddr { | ^^^^ ... 47 | pub async fn recv_route_info_request(&mut self) -> RouteInfoRequest { | ^^^^^^^^^^^^^^^^^^^^^^^ ... 54 | pub async fn expect_no_route_info_request(&mut self) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ... 63 | pub async fn send_route_info_response(&mut self, response: RouteInfoResponse) { | ^^^^^^^^^^^^^^^^^^^^^^^^

Check warning on line 17 in plane/plane-tests/tests/common/proxy_mock.rs

View workflow job for this annotation

GitHub Actions / clippy

associated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used

warning: associated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used --> plane/plane-tests/tests/common/proxy_mock.rs:17:18 | 16 | impl MockProxy { | -------------- associated items in this implementation 17 | pub async fn new() -> Self { | ^^^ ... 43 | pub fn addr(&self) -> SocketAddr { | ^^^^ ... 47 | pub async fn recv_route_info_request(&mut self) -> RouteInfoRequest { | ^^^^^^^^^^^^^^^^^^^^^^^ ... 54 | pub async fn expect_no_route_info_request(&mut self) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ... 63 | pub async fn send_route_info_response(&mut self, response: RouteInfoResponse) { | ^^^^^^^^^^^^^^^^^^^^^^^^

Check warning on line 17 in plane/plane-tests/tests/common/proxy_mock.rs

View workflow job for this annotation

GitHub Actions / clippy

associated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used

warning: associated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used --> plane/plane-tests/tests/common/proxy_mock.rs:17:18 | 16 | impl MockProxy { | -------------- associated items in this implementation 17 | pub async fn new() -> Self { | ^^^ ... 43 | pub fn addr(&self) -> SocketAddr { | ^^^^ ... 47 | pub async fn recv_route_info_request(&mut self) -> RouteInfoRequest { | ^^^^^^^^^^^^^^^^^^^^^^^ ... 54 | pub async fn expect_no_route_info_request(&mut self) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ... 63 | pub async fn send_route_info_response(&mut self, response: RouteInfoResponse) { | ^^^^^^^^^^^^^^^^^^^^^^^^

Check warning on line 17 in plane/plane-tests/tests/common/proxy_mock.rs

View workflow job for this annotation

GitHub Actions / clippy

associated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used

warning: associated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used --> plane/plane-tests/tests/common/proxy_mock.rs:17:18 | 16 | impl MockProxy { | -------------- associated items in this implementation 17 | pub async fn new() -> Self { | ^^^ ... 43 | pub fn addr(&self) -> SocketAddr { | ^^^^ ... 47 | pub async fn recv_route_info_request(&mut self) -> RouteInfoRequest { | ^^^^^^^^^^^^^^^^^^^^^^^ ... 54 | pub async fn expect_no_route_info_request(&mut self) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ... 63 | pub async fn send_route_info_response(&mut self, response: RouteInfoResponse) { | ^^^^^^^^^^^^^^^^^^^^^^^^

Check warning on line 17 in plane/plane-tests/tests/common/proxy_mock.rs

View workflow job for this annotation

GitHub Actions / clippy

associated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used

warning: associated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used --> plane/plane-tests/tests/common/proxy_mock.rs:17:18 | 16 | impl MockProxy { | -------------- associated items in this implementation 17 | pub async fn new() -> Self { | ^^^ ... 43 | pub fn addr(&self) -> SocketAddr { | ^^^^ ... 47 | pub async fn recv_route_info_request(&mut self) -> RouteInfoRequest { | ^^^^^^^^^^^^^^^^^^^^^^^ ... 54 | pub async fn expect_no_route_info_request(&mut self) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ... 63 | pub async fn send_route_info_response(&mut self, response: RouteInfoResponse) { | ^^^^^^^^^^^^^^^^^^^^^^^^

Check warning on line 17 in plane/plane-tests/tests/common/proxy_mock.rs

View workflow job for this annotation

GitHub Actions / clippy

associated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used

warning: associated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used --> plane/plane-tests/tests/common/proxy_mock.rs:17:18 | 16 | impl MockProxy { | -------------- associated items in this implementation 17 | pub async fn new() -> Self { | ^^^ ... 43 | pub fn addr(&self) -> SocketAddr { | ^^^^ ... 47 | pub async fn recv_route_info_request(&mut self) -> RouteInfoRequest { | ^^^^^^^^^^^^^^^^^^^^^^^ ... 54 | pub async fn expect_no_route_info_request(&mut self) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ... 63 | pub async fn send_route_info_response(&mut self, response: RouteInfoResponse) { | ^^^^^^^^^^^^^^^^^^^^^^^^

Check warning on line 17 in plane/plane-tests/tests/common/proxy_mock.rs

View workflow job for this annotation

GitHub Actions / clippy

associated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used

warning: associated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used --> plane/plane-tests/tests/common/proxy_mock.rs:17:18 | 16 | impl MockProxy { | -------------- associated items in this implementation 17 | pub async fn new() -> Self { | ^^^ ... 43 | pub fn addr(&self) -> SocketAddr { | ^^^^ ... 47 | pub async fn recv_route_info_request(&mut self) -> RouteInfoRequest { | ^^^^^^^^^^^^^^^^^^^^^^^ ... 54 | pub async fn expect_no_route_info_request(&mut self) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ... 63 | pub async fn send_route_info_response(&mut self, response: RouteInfoResponse) { | ^^^^^^^^^^^^^^^^^^^^^^^^

Check warning on line 17 in plane/plane-tests/tests/common/proxy_mock.rs

View workflow job for this annotation

GitHub Actions / clippy

associated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used

warning: associated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used --> plane/plane-tests/tests/common/proxy_mock.rs:17:18 | 16 | impl MockProxy { | -------------- associated items in this implementation 17 | pub async fn new() -> Self { | ^^^ ... 43 | pub fn addr(&self) -> SocketAddr { | ^^^^ ... 47 | pub async fn recv_route_info_request(&mut self) -> RouteInfoRequest { | ^^^^^^^^^^^^^^^^^^^^^^^ ... 54 | pub async fn expect_no_route_info_request(&mut self) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ... 63 | pub async fn send_route_info_response(&mut self, response: RouteInfoResponse) { | ^^^^^^^^^^^^^^^^^^^^^^^^

Check warning on line 17 in plane/plane-tests/tests/common/proxy_mock.rs

View workflow job for this annotation

GitHub Actions / clippy

associated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used

warning: associated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used --> plane/plane-tests/tests/common/proxy_mock.rs:17:18 | 16 | impl MockProxy { | -------------- associated items in this implementation 17 | pub async fn new() -> Self { | ^^^ ... 43 | pub fn addr(&self) -> SocketAddr { | ^^^^ ... 47 | pub async fn recv_route_info_request(&mut self) -> RouteInfoRequest { | ^^^^^^^^^^^^^^^^^^^^^^^ ... 54 | pub async fn expect_no_route_info_request(&mut self) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ... 63 | pub async fn send_route_info_response(&mut self, response: RouteInfoResponse) { | ^^^^^^^^^^^^^^^^^^^^^^^^

Check warning on line 17 in plane/plane-tests/tests/common/proxy_mock.rs

View workflow job for this annotation

GitHub Actions / clippy

associated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used

warning: associated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used --> plane/plane-tests/tests/common/proxy_mock.rs:17:18 | 16 | impl MockProxy { | -------------- associated items in this implementation 17 | pub async fn new() -> Self { | ^^^ ... 43 | pub fn addr(&self) -> SocketAddr { | ^^^^ ... 47 | pub async fn recv_route_info_request(&mut self) -> RouteInfoRequest { | ^^^^^^^^^^^^^^^^^^^^^^^ ... 54 | pub async fn expect_no_route_info_request(&mut self) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ... 63 | pub async fn send_route_info_response(&mut self, response: RouteInfoResponse) { | ^^^^^^^^^^^^^^^^^^^^^^^^
let proxy_state = ProxyState::new();
let (route_info_request_sender, route_info_request_receiver) = mpsc::channel(1);

proxy_state.inner.route_map.set_sender(move |m| {
route_info_request_sender
.try_send(m)
.expect("Failed to send route info request");
});

let listener = TcpListener::bind("127.0.0.1:0")
.await
.expect("Failed to bind listener");
let addr = listener.local_addr().expect("Failed to get local address");

let server = SimpleHttpServer::new(proxy_state.clone(), listener, HttpsConfig::http())
.expect("Failed to create server");

Self {
proxy_state,
route_info_request_receiver,
addr,
server,
}
}

pub fn addr(&self) -> SocketAddr {
self.addr
}

pub async fn recv_route_info_request(&mut self) -> RouteInfoRequest {

Check warning on line 47 in plane/plane-tests/tests/common/proxy_mock.rs

View workflow job for this annotation

GitHub Actions / clippy

methods `recv_route_info_request` and `send_route_info_response` are never used

warning: methods `recv_route_info_request` and `send_route_info_response` are never used --> plane/plane-tests/tests/common/proxy_mock.rs:47:18 | 16 | impl MockProxy { | -------------- methods in this implementation ... 47 | pub async fn recv_route_info_request(&mut self) -> RouteInfoRequest { | ^^^^^^^^^^^^^^^^^^^^^^^ ... 63 | pub async fn send_route_info_response(&mut self, response: RouteInfoResponse) { | ^^^^^^^^^^^^^^^^^^^^^^^^
self.route_info_request_receiver
.recv()
.await
.expect("Failed to receive route info request")
}

pub async fn expect_no_route_info_request(&mut self) {
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
assert!(
self.route_info_request_receiver.is_empty(),
"Expected no route info request, but got: {}",
self.route_info_request_receiver.len()
);
}

pub async fn send_route_info_response(&mut self, response: RouteInfoResponse) {
self.proxy_state.inner.route_map.receive(response);
}
}
76 changes: 76 additions & 0 deletions plane/plane-tests/tests/common/simple_axum_server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use axum::{body::Body, extract::Request, routing::any, Json, Router};
use http::Method;
use http_body_util::BodyExt;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, net::SocketAddr};
use tokio::net::TcpListener;

pub struct SimpleAxumServer {
handle: tokio::task::JoinHandle<()>,
addr: SocketAddr,
}

#[allow(unused)]
impl SimpleAxumServer {
pub async fn new() -> Self {
let app = Router::new()
.route("/*path", any(return_request_info))
.route("/", any(return_request_info));

let addr = SocketAddr::from(([127, 0, 0, 1], 0));
let tcp_listener = TcpListener::bind(addr).await.unwrap();
let addr = tcp_listener.local_addr().unwrap();

let handle = tokio::spawn(async {
axum::serve(tcp_listener, app.into_make_service())
.await
.unwrap();
});

Self { handle, addr }
}

pub fn addr(&self) -> SocketAddr {
self.addr
}
}

impl Drop for SimpleAxumServer {
fn drop(&mut self) {
self.handle.abort();
}
}

// Handler function for the root route
async fn return_request_info(method: Method, request: Request<Body>) -> Json<RequestInfo> {
let method = method.to_string();

let path = request.uri().path().to_string();
let query = request.uri().query().unwrap_or("").to_string();

let headers: HashMap<String, String> = request
.headers()
.iter()
.map(|(k, v)| (k.to_string(), v.to_str().unwrap().to_string()))
.collect();

let body = request.into_body().collect().await.unwrap().to_bytes();
let body = String::from_utf8(body.to_vec()).unwrap();

Json(RequestInfo {
path,
query,
method,
headers,
body,
})
}

#[derive(Serialize, Deserialize)]
pub struct RequestInfo {
pub path: String,
pub query: String,
pub method: String,
pub headers: HashMap<String, String>,
pub body: String,
}
18 changes: 18 additions & 0 deletions plane/plane-tests/tests/proxy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use common::{proxy_mock::MockProxy, test_env::TestEnvironment};
use plane_test_macro::plane_test;
use reqwest::StatusCode;

mod common;

#[plane_test]
async fn proxy_no_bearer_token(env: TestEnvironment) {
let mut proxy = MockProxy::new().await;
let url = format!("http://{}", proxy.addr());
let handle = tokio::spawn(async { reqwest::get(url).await.expect("Failed to send request") });

proxy.expect_no_route_info_request().await;

let response = handle.await.unwrap();

assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
}
7 changes: 2 additions & 5 deletions plane/src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ use crate::proxy::cert_manager::watcher_manager_pair;
use crate::{client::PlaneClient, signals::wait_for_shutdown_signal, types::ClusterName};
use anyhow::Result;
use dynamic_proxy::server::{
HttpsConfig, ServerWithHttpRedirect, ServerWithHttpRedirectConfig,
ServerWithHttpRedirectHttpsConfig, SimpleHttpServer,
ServerWithHttpRedirect, ServerWithHttpRedirectConfig, ServerWithHttpRedirectHttpsConfig,
};
use serde::{Deserialize, Serialize};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use tokio::net::TcpListener;
use url::Url;

pub mod cert_manager;
Expand All @@ -21,7 +18,7 @@ pub mod command;
mod connection_monitor;
pub mod proxy_connection;
// mod proxy_service;
mod proxy_server;
pub mod proxy_server;
// mod rewriter;
mod request;
mod route_map;
Expand Down
3 changes: 0 additions & 3 deletions plane/src/proxy/proxy_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ impl ProxyConnection {

loop {
let mut conn = proxy_connection.connect_with_retry(&name).await;
state.set_connected(true);

let sender = conn.sender(MessageFromProxy::CertManagerRequest);
cert_manager.set_request_sender(move |m| {
Expand Down Expand Up @@ -73,8 +72,6 @@ impl ProxyConnection {
}
}
}

state.set_connected(false);
}
})
};
Expand Down
29 changes: 10 additions & 19 deletions plane/src/proxy/proxy_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ use super::{
route_map::RouteMap,
};
use dynamic_proxy::{
body::SimpleBody,
hyper::{body::Incoming, service::Service, Request, Response, Uri},
body::{simple_empty_body, SimpleBody},
hyper::{body::Incoming, service::Service, Request, Response, StatusCode, Uri},
proxy::ProxyClient,
request::MutableRequest,
};
use std::{future::Future, sync::atomic::AtomicBool};
use std::future::{ready, Future};
use std::{pin::Pin, sync::Arc};

pub struct ProxyStateInner {
pub route_map: RouteMap,
pub proxy_client: ProxyClient,
pub monitor: ConnectionMonitorHandle,
pub connected: AtomicBool,
}

#[derive(Clone)]
pub struct ProxyState {
pub inner: Arc<ProxyStateInner>,
}
Expand All @@ -34,25 +34,12 @@ impl ProxyState {
route_map: RouteMap::new(),
proxy_client: ProxyClient::new(),
monitor: ConnectionMonitorHandle::new(),
connected: AtomicBool::new(false),
};

Self {
inner: Arc::new(inner),
}
}

pub fn set_connected(&self, connected: bool) {
self.inner
.connected
.store(connected, std::sync::atomic::Ordering::Relaxed);
}

pub fn connected(&self) -> bool {
self.inner
.connected
.load(std::sync::atomic::Ordering::Relaxed)
}
}

impl Service<Request<Incoming>> for ProxyState {
Expand All @@ -73,8 +60,12 @@ impl Service<Request<Incoming>> for ProxyState {
let mut uri_parts = request.parts.uri.clone().into_parts();
let bearer_token = get_and_maybe_remove_bearer_token(&mut uri_parts);

// TODO
let bearer_token = bearer_token.unwrap();
let Some(bearer_token) = bearer_token else {
return Box::pin(ready(Ok(Response::builder()
.status(StatusCode::UNAUTHORIZED)
.body(simple_empty_body())
.unwrap())));

Check warning on line 67 in plane/src/proxy/proxy_server.rs

View workflow job for this annotation

GitHub Actions / clippy

used `unwrap()` on a `Result` value

warning: used `unwrap()` on a `Result` value --> plane/src/proxy/proxy_server.rs:64:38 | 64 | return Box::pin(ready(Ok(Response::builder() | ______________________________________^ 65 | | .status(StatusCode::UNAUTHORIZED) 66 | | .body(simple_empty_body()) 67 | | .unwrap()))); | |_________________________^ | = note: if this value is an `Err`, it will panic = help: consider using `expect()` to provide a better panic message = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unwrap_used note: the lint level is defined here --> plane/src/lib.rs:1:9 | 1 | #![warn(clippy::unwrap_used)] | ^^^^^^^^^^^^^^^^^^^
};

request.parts.uri = Uri::from_parts(uri_parts).unwrap();

Check warning on line 70 in plane/src/proxy/proxy_server.rs

View workflow job for this annotation

GitHub Actions / clippy

used `unwrap()` on a `Result` value

warning: used `unwrap()` on a `Result` value --> plane/src/proxy/proxy_server.rs:70:29 | 70 | request.parts.uri = Uri::from_parts(uri_parts).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: if this value is an `Err`, it will panic = help: consider using `expect()` to provide a better panic message = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unwrap_used

Expand Down
4 changes: 4 additions & 0 deletions plane/src/proxy/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ pub fn get_and_maybe_remove_bearer_token(parts: &mut uri::Parts) -> Option<Beare
None => (full_path, "/"),
};

if token.is_empty() {
return None;
}

let token = BearerToken::from(token.to_string());

if token.is_static() {
Expand Down

0 comments on commit 28d0721

Please sign in to comment.