Skip to content

Commit 6da8c8b

Browse files
committed
Update test case for Internal HTTP API Websocket to verify auth sematics
1 parent cb10087 commit 6da8c8b

3 files changed

Lines changed: 49 additions & 25 deletions

File tree

src/environmentd/src/http.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl HttpServer {
160160
.route("/api/experimental/sql", routing::get(sql::handle_sql_ws))
161161
.with_state(WsState {
162162
frontegg,
163-
adapter_client_rx: adapter_client_rx,
163+
adapter_client_rx,
164164
active_connection_count,
165165
});
166166

@@ -452,7 +452,7 @@ impl InternalHttpServer {
452452
.layer(middleware::from_fn(internal_http_auth))
453453
.with_state(WsState {
454454
frontegg: Arc::new(None),
455-
adapter_client_rx: adapter_client_rx,
455+
adapter_client_rx,
456456
active_connection_count,
457457
});
458458

src/environmentd/tests/server.rs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ use itertools::Itertools;
9595
use mz_environmentd::http::{
9696
BecomeLeaderResponse, BecomeLeaderResult, LeaderStatus, LeaderStatusResponse,
9797
};
98-
use mz_environmentd::WebSocketResponse;
98+
use mz_environmentd::{WebSocketAuth, WebSocketResponse};
9999
use mz_ore::cast::CastFrom;
100100
use mz_ore::cast::CastLossy;
101101
use mz_ore::cast::TryCastFrom;
@@ -2117,25 +2117,38 @@ fn test_internal_ws_auth() {
21172117

21182118
// Create our WebSocket.
21192119
let ws_url = server.internal_ws_addr();
2120-
let req = Request::builder()
2121-
.uri(ws_url.as_str())
2122-
.method("GET")
2123-
.header("Host", ws_url.host_str().unwrap())
2124-
.header("Connection", "Upgrade")
2125-
.header("Upgrade", "websocket")
2126-
.header("Sec-WebSocket-Version", "13")
2127-
.header("Sec-WebSocket-Key", "foobar")
2128-
// Set our user to the mz_support user
2129-
.header("x-materialize-user", "mz_support")
2130-
.body(())
2131-
.unwrap();
2120+
let make_req = || {
2121+
Request::builder()
2122+
.uri(ws_url.as_str())
2123+
.method("GET")
2124+
.header("Host", ws_url.host_str().unwrap())
2125+
.header("Connection", "Upgrade")
2126+
.header("Upgrade", "websocket")
2127+
.header("Sec-WebSocket-Version", "13")
2128+
.header("Sec-WebSocket-Key", "foobar")
2129+
// Set our user to the mz_support user
2130+
.header("x-materialize-user", "mz_support")
2131+
.body(())
2132+
.unwrap()
2133+
};
21322134

2133-
let (mut ws, _resp) = tungstenite::connect(req).unwrap();
2135+
let (mut ws, _resp) = tungstenite::connect(make_req()).unwrap();
21342136
let options = BTreeMap::from([(
21352137
"application_name".to_string(),
21362138
"billion_dollar_idea".to_string(),
21372139
)]);
2138-
util::auth_with_ws(&mut ws, options).unwrap();
2140+
// We should receive error if sending the standard bearer auth, since that is unexpected
2141+
// for the Internal HTTP API
2142+
assert_eq!(util::auth_with_ws(&mut ws, options.clone()).is_err(), true);
2143+
2144+
// Recreate the websocket
2145+
let (mut ws, _resp) = tungstenite::connect(make_req()).unwrap();
2146+
// Auth with OptionsOnly
2147+
util::auth_with_ws_impl(
2148+
&mut ws,
2149+
Message::Text(serde_json::to_string(&WebSocketAuth::OptionsOnly { options }).unwrap()),
2150+
)
2151+
.unwrap();
21392152

21402153
// Query to make sure we get back the correct user, which should be
21412154
// set from the headers passed with the websocket request.

src/environmentd/tests/util.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -818,14 +818,25 @@ pub fn auth_with_ws(
818818
ws: &mut WebSocket<MaybeTlsStream<TcpStream>>,
819819
options: BTreeMap<String, String>,
820820
) -> Result<Vec<WebSocketResponse>, anyhow::Error> {
821-
ws.send(Message::Text(
822-
serde_json::to_string(&WebSocketAuth::Basic {
823-
user: "materialize".into(),
824-
password: "".into(),
825-
options,
826-
})
827-
.unwrap(),
828-
))?;
821+
auth_with_ws_impl(
822+
ws,
823+
Message::Text(
824+
serde_json::to_string(&WebSocketAuth::Basic {
825+
user: "materialize".into(),
826+
password: "".into(),
827+
options,
828+
})
829+
.unwrap(),
830+
),
831+
)
832+
}
833+
834+
pub fn auth_with_ws_impl(
835+
ws: &mut WebSocket<MaybeTlsStream<TcpStream>>,
836+
auth_message: Message,
837+
) -> Result<Vec<WebSocketResponse>, anyhow::Error> {
838+
ws.send(auth_message)?;
839+
829840
// Wait for initial ready response.
830841
let mut msgs = Vec::new();
831842
loop {

0 commit comments

Comments
 (0)