Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(connection_gateway): use macro authorization (#4938)

Will Hutchinsonf947074564ba
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.json
index 46759c77e9..4fa44b2c39 100644
--- a/.github/workspace-dep-closures.json
+++ b/.github/workspace-dep-closures.json
@@ -1016,24 +1016,20 @@
"crates/connection_gateway_client",
"crates/connection_gateway_models",
"crates/cowlike",
- "crates/decode_jwt",
"crates/document_sub_type",
"crates/ensure_exists",
- "crates/entity_access_db_utils",
"crates/filter_ast",
"crates/frecency",
- "crates/ip_extractor",
"crates/item_filters",
"crates/last_online_tracker",
"crates/macro_auth",
+ "crates/macro_authorization",
"crates/macro_aws_config",
"crates/macro_config",
"crates/macro_config_derive",
- "crates/macro_db_client",
"crates/macro_entrypoint",
"crates/macro_env",
"crates/macro_env_var",
- "crates/macro_middleware",
"crates/macro_user_id",
"crates/macro_uuid",
"crates/model",
@@ -1042,20 +1038,13 @@
"crates/model_file_type",
"crates/model_user",
"crates/models_bulk_upload",
- "crates/models_dcs",
- "crates/models_entity_access_management",
- "crates/models_opensearch",
"crates/models_pagination",
"crates/models_permissions",
- "crates/models_properties",
- "crates/models_team",
"crates/non_empty",
"crates/prompt",
"crates/remote_env_var",
"crates/secretsmanager_client",
"crates/stream",
- "crates/system_properties",
- "crates/user_quota",
"crates/workspace-hack",
"services/connection_gateway"
],
diff --git a/Cargo.lock b/Cargo.lock
index ff1d50b8d7..1529d966ce 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2962,12 +2962,12 @@ dependencies = [
"itertools 0.14.0",
"last_online_tracker",
"macro_auth",
+ "macro_authorization",
"macro_aws_config",
"macro_config",
"macro_entrypoint",
"macro_env",
"macro_env_var",
- "macro_middleware",
"macro_user_id",
"mockall",
"model",
diff --git a/apps/web/src/lib/service-clients/service-connection/openapi.json b/apps/web/src/lib/service-clients/service-connection/openapi.json
index fab4f20dc9..f8bd19cdc8 100644
--- a/apps/web/src/lib/service-clients/service-connection/openapi.json
+++ b/apps/web/src/lib/service-clients/service-connection/openapi.json
@@ -45,7 +45,12 @@
}
}
}
- }
+ },
+ "security": [
+ {
+ "internal-api-key": []
+ }
+ ]
}
},
"/message/send/{entity_type}/{entity_id}": {
@@ -123,7 +128,12 @@
}
}
}
- }
+ },
+ "security": [
+ {
+ "internal-api-key": []
+ }
+ ]
}
},
"/track/{entity_type}/{entity_id}": {
@@ -184,7 +194,12 @@
}
}
}
- }
+ },
+ "security": [
+ {
+ "internal-api-key": []
+ }
+ ]
}
}
},
@@ -562,6 +577,13 @@
}
]
}
+ },
+ "securitySchemes": {
+ "internal-api-key": {
+ "type": "apiKey",
+ "in": "header",
+ "name": "x-internal-auth-key"
+ }
}
},
"tags": [
diff --git a/services/connection_gateway/Cargo.toml b/services/connection_gateway/Cargo.toml
index d410fd2511..955323dc9c 100644
--- a/services/connection_gateway/Cargo.toml
+++ b/services/connection_gateway/Cargo.toml
@@ -25,6 +25,9 @@ path = "scripts/stale_connections.rs"
name = "connection_gateway_openapi"
path = "src/openapi.rs"
+[features]
+local_auth = ["macro_authorization/local_auth"]
+
[dependencies]
anyhow = { workspace = true }
async-trait = { workspace = true }
@@ -47,13 +50,13 @@ http-body-util = { workspace = true }
itertools = { workspace = true }
last_online_tracker = { path = "../../crates/last_online_tracker" }
macro_auth = { path = "../../crates/macro_auth" }
+macro_authorization = { path = "../../crates/macro_authorization" }
macro_user_id = { path = "../../crates/macro_user_id" }
macro_aws_config = { path = "../../crates/macro_aws_config" }
macro_config = { path = "../../crates/macro_config" }
macro_entrypoint = { path = "../../crates/macro_entrypoint" }
macro_env = { path = "../../crates/macro_env" }
macro_env_var = { path = "../../crates/macro_env_var" }
-macro_middleware = { path = "../../crates/macro_middleware", features = ["auth"] }
mockall.workspace = true
model = { path = "../../crates/model" }
model-entity = { path = "../../crates/model-entity" }
@@ -85,3 +88,4 @@ doppler-config = { workspace = true }
[dev-dependencies]
cool_asserts = "2"
+macro_auth = { path = "../../crates/macro_auth", features = ["testing"] }
diff --git a/services/connection_gateway/justfile b/services/connection_gateway/justfile
index 18858cc9a9..c055f8b32d 100644
--- a/services/connection_gateway/justfile
+++ b/services/connection_gateway/justfile
@@ -3,6 +3,7 @@ export AUDIENCE := env('AUDIENCE', 'audience')
export ISSUER := env('ISSUER', 'issuer')
export CONNECTION_GATEWAY_TABLE := env('CONNECTION_GATEWAY_TABLE', 'my-table')
export INTERNAL_API_SECRET_KEY := env('INTERNAL_API_SECRET_KEY', 'internal-api-secret-key')
+export LOCAL_USER_ID := env('LOCAL_USER_ID', 'macro|[email protected]')
export REDIS_HOST := env('REDIS_HOST', 'redis://127.0.0.1:6379')
export MACRO_API_TOKEN_PUBLIC_KEY := env('MACRO_API_TOKEN_PUBLIC_KEY', '123456789')
export MACRO_API_TOKEN_ISSUER := env('MACRO_API_TOKEN_ISSER', '123456789')
@@ -21,7 +22,7 @@ deploy-dev:
just ci::deploy-dev connection-gateway
run *FLAGS: ensure::ensure_dynamodb ensure::ensure_redis
- cargo run --bin connection_gateway_service --features macro_middleware/local_auth {{ FLAGS }}
+ cargo run --bin connection_gateway_service --features local_auth {{ FLAGS }}
run_cleanup:
cargo run --bin stale_connections
diff --git a/services/connection_gateway/src/api/connection/mod.rs b/services/connection_gateway/src/api/connection/mod.rs
index cfb06bb483..6311441bdc 100644
--- a/services/connection_gateway/src/api/connection/mod.rs
+++ b/services/connection_gateway/src/api/connection/mod.rs
@@ -1,6 +1,6 @@
use crate::{
config::Config,
- context::{ApiContext, AppState},
+ context::{ApiContext, AppState, AuthorizationService},
model::{
connection::ConnectionContext, message::OutgoingMessage, tracking::EntityConnectionExt,
},
@@ -9,18 +9,18 @@ use anyhow::Result;
use axum::{
Router,
extract::{
- Extension, State,
+ State,
ws::{Message as AxumWebsocketMessage, WebSocket, WebSocketUpgrade},
},
response::IntoResponse,
routing::get,
};
-use cowlike::CowLike;
use futures::{
FutureExt,
sink::SinkExt,
stream::{SplitSink, StreamExt},
};
+use macro_authorization::MacroAuthorizationExtractor;
use macro_user_id::user_id::MacroUserIdStr;
use messages::handle_websocket_stream;
use model::user::UserContext;
@@ -35,15 +35,21 @@ pub fn router() -> Router<AppState> {
}
/// Handle upgrading the https connection to a websocket connection
-#[tracing::instrument(skip(ws, ctx, user_context, config), fields(user_id=?user_context.user_id))]
+#[tracing::instrument(skip(ws, authorization, ctx, config), fields(user_id=?authorization.macro_user_id))]
pub async fn ws_handler(
ws: WebSocketUpgrade,
+ authorization: MacroAuthorizationExtractor<AuthorizationService>,
State(ctx): State<ApiContext>,
State(config): State<Arc<Config>>,
- user_context: Extension<UserContext>,
) -> impl IntoResponse {
ws.on_upgrade(move |socket| {
- handle_websocket_connection(socket, ctx, config, user_context.clone())
+ handle_websocket_connection(
+ socket,
+ ctx,
+ config,
+ authorization.macro_user_id,
+ authorization.user_context,
+ )
})
}
@@ -51,23 +57,20 @@ pub async fn ws_handler(
/// Should create a new connection in the connection manager,
/// and spawn tasks for both forwarding of messages, and reading incoming messages from the client.
/// If any part of forwarding or reading fails, then the connection should be removed from the connection manager.
-#[tracing::instrument(skip(socket, ctx, user_context, config), fields(user_id=?user_context.user_id))]
+#[tracing::instrument(skip(socket, ctx, config, macro_user_id, user_context), fields(user_id=?macro_user_id))]
async fn handle_websocket_connection(
socket: WebSocket,
ctx: ApiContext,
config: Arc<Config>,
- user_context: Extension<UserContext>,
+ macro_user_id: MacroUserIdStr<'static>,
+ user_context: UserContext,
) {
let (sink, stream) = socket.split();
let (sender, receiver) = tokio::sync::mpsc::channel::<OutgoingMessage>(100);
let connection_id = uuid::Uuid::new_v4().to_string();
- let Ok(user_id) = MacroUserIdStr::parse_from_str(&user_context.user_id) else {
- return;
- };
- let user_id = user_id.into_owned();
// Create guard that records last online time when websocket connection closes
- let last_online_guard = ctx.last_online_worker.new_guard(user_id.clone());
+ let last_online_guard = ctx.last_online_worker.new_guard(macro_user_id.clone());
let sender_task = tokio::spawn(async move { forwarder(sink, receiver).await });
@@ -75,9 +78,9 @@ async fn handle_websocket_connection(
.connection_manager
.add_connection(
EntityType::User
- .with_entity_str(user_id.as_ref())
+ .with_entity_str(macro_user_id.as_ref())
.with_connection_str(&connection_id)
- .with_user_str(user_id.as_ref()),
+ .with_user_str(macro_user_id.as_ref()),
sender.clone(),
sender_task.abort_handle(),
)
diff --git a/services/connection_gateway/src/api/entities/mod.rs b/services/connection_gateway/src/api/entities/mod.rs
index 9d76b6146c..1d33899f51 100644
--- a/services/connection_gateway/src/api/entities/mod.rs
+++ b/services/connection_gateway/src/api/entities/mod.rs
@@ -1,6 +1,6 @@
use crate::{
constants::DEFAULT_TIMEOUT_THRESHOLD,
- context::{ApiContext, AppState},
+ context::{ApiContext, AppState, AuthorizationService},
};
use anyhow::Result;
use axum::{
@@ -10,7 +10,7 @@ use axum::{
response::ErrorResponse,
routing::get,
};
-use macro_middleware::auth;
+use macro_authorization::InternalMacroAuthorizationExtractor;
use model_entity::Entity;
use utoipa::ToSchema;
@@ -20,10 +20,6 @@ where
{
Router::new()
.route("/{entity_type}/{entity_id}", get(get_entity_handler))
- .layer(axum::middleware::from_fn_with_state(
- state.clone(),
- auth::internal_access::handler,
- ))
.with_state(state)
}
@@ -36,6 +32,7 @@ pub struct QueryParams {
#[utoipa::path(
get,
path = "/track/{entity_type}/{entity_id}",
+ security(("internal-api-key" = [])),
params(
("entity_type" = String, Path, description = "the type of the entity to send the msssage to e.g. \"user\" | \"channel\" | \"document\" etc..."),
("entity_id" = String, Path, description = "the id of the entity to send the message to"),
@@ -46,9 +43,10 @@ pub struct QueryParams {
(status = 500, body=String),
)
)]
-#[tracing::instrument(skip(ctx))]
+#[tracing::instrument(skip(_internal_authorization, ctx))]
#[axum::debug_handler(state = AppState)]
pub async fn get_entity_handler(
+ _internal_authorization: InternalMacroAuthorizationExtractor<AuthorizationService>,
State(ctx): State<ApiContext>,
Path(entity): Path<Entity<'static>>,
Query(query_params): Query<QueryParams>,
diff --git a/services/connection_gateway/src/api/message/mod.rs b/services/connection_gateway/src/api/message/mod.rs
index e9f048e4e8..52563ccb7a 100644
--- a/services/connection_gateway/src/api/message/mod.rs
+++ b/services/connection_gateway/src/api/message/mod.rs
@@ -1,4 +1,8 @@
-use crate::{context::AppState, model::message::Message, service::sender::send_message_to_entity};
+use crate::{
+ context::{AppState, AuthorizationService},
+ model::message::Message,
+ service::sender::send_message_to_entity,
+};
use axum::{
Json as JsonResponse, Router,
extract::{Json, Path, State},
@@ -9,7 +13,7 @@ use connection_gateway_models::{
BatchSendMessageBody, BatchSendUniqueMessagesBody, SendMessageBody, SendMessageResponse,
};
use futures::future::try_join_all;
-use macro_middleware::auth;
+use macro_authorization::InternalMacroAuthorizationExtractor;
use model_entity::Entity;
use std::time::Instant;
@@ -27,16 +31,13 @@ where
"/batch_send_unique",
post(batch_send_unique_messages_handler),
)
- .layer(axum::middleware::from_fn_with_state(
- state.clone(),
- auth::internal_access::handler,
- ))
.with_state(state)
}
#[utoipa::path(
post,
path = "/message/send/{entity_type}/{entity_id}",
+ security(("internal-api-key" = [])),
params(
("entity_type" = String, Path, description = "the type of the entity to send the msssage to e.g. \"user\" | \"channel\" | \"document\" etc..."),
("entity_id" = String, Path, description = "the id of the entity to send the message to"),
@@ -48,9 +49,10 @@ where
(status = 500, body=String),
)
)]
-#[tracing::instrument(skip(ctx))]
+#[tracing::instrument(skip(_internal_authorization, ctx))]
#[axum::debug_handler(state = AppState)]
pub async fn send_message_handler(
+ _internal_authorization: InternalMacroAuthorizationExtractor<AuthorizationService>,
State(ctx): State<AppState>,
Path(entity): Path<Entity<'static>>,
Json(body): Json<SendMessageBody>,
@@ -84,14 +86,16 @@ pub async fn send_message_handler(
#[utoipa::path(
post,
path = "/batch_send",
+ security(("internal-api-key" = [])),
request_body = BatchSendMessageBody,
responses(
(status = 200, description = "Message sent successfully", body = SendMessageResponse),
(status = 500, description = "Internal server error", body = String),
)
)]
-#[tracing::instrument(skip(ctx))]
+#[tracing::instrument(skip(_internal_authorization, ctx))]
pub async fn batch_send_message_handler(
+ _internal_authorization: InternalMacroAuthorizationExtractor<AuthorizationService>,
State(ctx): State<AppState>,
Json(body): Json<BatchSendMessageBody<'static>>,
) -> Result<(StatusCode, JsonResponse<SendMessageResponse>), (StatusCode, String)> {
@@ -140,9 +144,10 @@ pub async fn batch_send_message_handler(
(status = 500, description = "Internal server error", body = String),
)
)]
-#[tracing::instrument(skip(ctx))]
+#[tracing::instrument(skip(_internal_authorization, ctx))]
#[axum::debug_handler(state = AppState)]
pub async fn batch_send_unique_messages_handler(
+ _internal_authorization: InternalMacroAuthorizationExtractor<AuthorizationService>,
State(ctx): State<AppState>,
Json(body): Json<BatchSendUniqueMessagesBody>,
) -> Result<(StatusCode, JsonResponse<SendMessageResponse>), (StatusCode, String)> {
diff --git a/services/connection_gateway/src/api/mod.rs b/services/connection_gateway/src/api/mod.rs
index 3ada56b5e9..07f71b0f84 100644
--- a/services/connection_gateway/src/api/mod.rs
+++ b/services/connection_gateway/src/api/mod.rs
@@ -4,8 +4,6 @@ use axum::Router;
pub use connection_gateway_models::{
BatchSendMessageBody, BatchSendUniqueMessagesBody, SendMessageBody,
};
-use macro_middleware::auth::{attach_user, initialize_user_context};
-use tower::ServiceBuilder;
use utoipa::OpenApi;
use utoipa_swagger_ui::SwaggerUi;
@@ -15,18 +13,12 @@ mod health;
mod message;
pub(crate) mod swagger;
+#[cfg(test)]
+mod test;
+
pub fn router(state: AppState) -> Router {
Router::new()
- .merge(
- connection::router().layer(
- ServiceBuilder::new()
- .layer(axum::middleware::from_fn(initialize_user_context::handler))
- .layer(axum::middleware::from_fn_with_state(
- state.clone(),
- attach_user::handler,
- )),
- ),
- )
+ .merge(connection::router())
.nest("/message", message::router(state.clone()))
.nest("/track", entities::router(state.clone()))
.merge(health::router())
diff --git a/services/connection_gateway/src/api/swagger.rs b/services/connection_gateway/src/api/swagger.rs
index 0dbb93789d..5be3c070a7 100644
--- a/services/connection_gateway/src/api/swagger.rs
+++ b/services/connection_gateway/src/api/swagger.rs
@@ -10,10 +10,27 @@ use model::response::{GenericErrorResponse, StringIDResponse};
use model_entity::{Entity, EntityType};
use models_bulk_upload::UploadFolderStatusUpdate;
use stream::domain::{StreamEvent, StreamItem};
-use utoipa::OpenApi;
+use utoipa::{
+ Modify, OpenApi,
+ openapi::security::{ApiKey, ApiKeyValue, SecurityScheme},
+};
+
+struct SecurityAddon;
+
+impl Modify for SecurityAddon {
+ fn modify(&self, openapi: &mut utoipa::openapi::OpenApi) {
+ if let Some(components) = openapi.components.as_mut() {
+ components.add_security_scheme(
+ "internal-api-key",
+ SecurityScheme::ApiKey(ApiKey::Header(ApiKeyValue::new("x-internal-auth-key"))),
+ );
+ }
+ }
+}
#[derive(OpenApi)]
#[openapi(
+ modifiers(&SecurityAddon),
info(
terms_of_service = "https://macro.com/terms",
),
diff --git a/services/connection_gateway/src/api/test.rs b/services/connection_gateway/src/api/test.rs
new file mode 100644
index 0000000000..3bd000bcf9
--- /dev/null
+++ b/services/connection_gateway/src/api/test.rs
@@ -0,0 +1,297 @@
+use std::{sync::Arc, time::Duration};
+
+use axum::{
+ Json, Router,
+ body::Body,
+ extract::FromRef,
+ http::{Method, Request, StatusCode},
+ routing::{get, post},
+};
+use http_body_util::BodyExt;
+use macro_auth::middleware::decode_jwt::JwtValidationArgs;
+#[allow(deprecated)]
+use macro_authorization::LEGACY_DSS_INTERNAL_API_KEY_HEADER;
+use macro_authorization::{
+ INTERNAL_API_KEY_HEADER, InternalAuthConfig, InternalMacroAuthorizationExtractor,
+ MacroAuthJwtValidator, MacroAuthorizationServiceImpl, MacroAuthorizationState,
+};
+use serde_json::{Value, json};
+use tokio::io::{AsyncReadExt, AsyncWriteExt};
+use tower::ServiceExt;
+
+use super::{connection, entities, message};
+use crate::{
+ config::Config,
+ context::{ApiContext, AppState, AuthorizationService},
+};
+
+const TEST_INTERNAL_API_KEY: &str = "connection-gateway-test-internal-key";
+const WRONG_INTERNAL_API_KEY: &str = "wrong-internal-key";
+const WEBSOCKET_PATH: &str = "/";
+const UNAUTHORIZED_BODY: &str = r#"{"message":"unauthorized"}"#;
+
+#[derive(Clone)]
+struct TestState {
+ authorization_state: MacroAuthorizationState<AuthorizationService>,
+}
+
+impl FromRef<TestState> for MacroAuthorizationState<AuthorizationService> {
+ fn from_ref(state: &TestState) -> Self {
+ state.authorization_state.clone()
+ }
+}
+
+impl FromRef<TestState> for AppState {
+ fn from_ref(_state: &TestState) -> Self {
+ panic!("authorization must reject before AppState extraction")
+ }
+}
+
+impl FromRef<TestState> for ApiContext {
+ fn from_ref(_state: &TestState) -> Self {
+ panic!("authorization must reject before ApiContext extraction")
+ }
+}
+
+impl FromRef<TestState> for Arc<Config> {
+ fn from_ref(_state: &TestState) -> Self {
+ panic!("authorization must reject before websocket configuration extraction")
+ }
+}
+
+async fn internal_auth_probe(
+ _authorization: InternalMacroAuthorizationExtractor<AuthorizationService>,
+) -> Json<Value> {
+ Json(json!({ "authorized": true }))
+}
+
+fn test_state() -> TestState {
+ let authorization_service = MacroAuthorizationServiceImpl::new(
+ MacroAuthJwtValidator::new(JwtValidationArgs::new_testing()),
+ InternalAuthConfig {
+ api_key: TEST_INTERNAL_API_KEY.to_string(),
+ default_user_id: None,
+ },
+ );
+
+ TestState {
+ authorization_state: MacroAuthorizationState::new(Arc::new(authorization_service)),
+ }
+}
+
+fn test_router() -> Router {
+ Router::new()
+ .route(
+ "/message/send/{entity_type}/{entity_id}",
+ post(message::send_message_handler),
+ )
+ .route(
+ "/message/batch_send",
+ post(message::batch_send_message_handler),
+ )
+ .route(
+ "/message/batch_send_unique",
+ post(message::batch_send_unique_messages_handler),
+ )
+ .route(
+ "/track/{entity_type}/{entity_id}",
+ get(entities::get_entity_handler),
+ )
+ .route(WEBSOCKET_PATH, get(connection::ws_handler))
+ .route("/internal-auth-probe", get(internal_auth_probe))
+ .with_state(test_state())
+}
+
+async fn send(request: Request<Body>) -> (StatusCode, Value) {
+ let response = test_router()
+ .oneshot(request)
+ .await
+ .expect("test router should return a response");
+ let status = response.status();
+ let body = response
+ .into_body()
+ .collect()
+ .await
+ .expect("response body should collect")
+ .to_bytes();
+ let body = serde_json::from_slice(&body).expect("response should contain JSON");
+
+ (status, body)
+}
+
+fn assert_unauthorized((status, body): (StatusCode, Value)) {
+ assert_eq!(status, StatusCode::UNAUTHORIZED);
+ assert_eq!(body, json!({ "message": "unauthorized" }));
+}
+
+#[tokio::test]
+async fn internal_routes_reject_missing_and_incorrect_keys_before_other_extraction() {
+ let endpoints = [
+ (Method::POST, "/message/send/user/macro%[email protected]"),
+ (Method::POST, "/message/batch_send"),
+ (Method::POST, "/message/batch_send_unique"),
+ (Method::GET, "/track/user/macro%[email protected]"),
+ ];
+
+ for (method, uri) in endpoints {
+ for key in [None, Some(WRONG_INTERNAL_API_KEY)] {
+ let mut request = Request::builder().method(method.clone()).uri(uri);
+ if let Some(key) = key {
+ request = request.header(INTERNAL_API_KEY_HEADER, key);
+ }
+
+ let request = request
+ .body(Body::empty())
+ .expect("test request should build");
+ assert_unauthorized(send(request).await);
+ }
+ }
+}
+
+#[tokio::test]
+async fn websocket_rejects_invalid_credentials_before_infrastructure_state() {
+ let listener = tokio::net::TcpListener::bind(("127.0.0.1", 0))
+ .await
+ .expect("test listener should bind");
+ let address = listener
+ .local_addr()
+ .expect("test listener should have a local address");
+ let server = tokio::spawn(async move {
+ axum::serve(listener, test_router().into_make_service())
+ .await
+ .expect("test server should run");
+ });
+
+ for headers in [
+ Vec::new(),
+ vec![("authorization", "Bearer not-a-token")],
+ vec![(INTERNAL_API_KEY_HEADER, TEST_INTERNAL_API_KEY)],
+ ] {
+ let (status, body) = send_websocket_request(address, &headers).await;
+ assert_eq!(status, StatusCode::UNAUTHORIZED);
+ assert_eq!(body, UNAUTHORIZED_BODY);
+ }
+
+ server.abort();
+}
+
+async fn send_websocket_request(
+ address: std::net::SocketAddr,
+ headers: &[(&str, &str)],
+) -> (StatusCode, String) {
+ let mut stream = tokio::net::TcpStream::connect(address)
+ .await
+ .expect("test client should connect");
+ let mut request = format!(
+ "GET {WEBSOCKET_PATH} HTTP/1.1\r\n\
+ Host: {address}\r\n\
+ Connection: Upgrade\r\n\
+ Upgrade: websocket\r\n\
+ Sec-WebSocket-Version: 13\r\n\
+ Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
+ );
+ for (name, value) in headers {
+ request.push_str(name);
+ request.push_str(": ");
+ request.push_str(value);
+ request.push_str("\r\n");
+ }
+ request.push_str("\r\n");
+
+ stream
+ .write_all(request.as_bytes())
+ .await
+ .expect("websocket request should write");
+
+ let response = read_http_response(&mut stream).await;
+ parse_http_response(&response)
+}
+
+async fn read_http_response(stream: &mut tokio::net::TcpStream) -> Vec<u8> {
+ let mut response = Vec::new();
+ let mut buffer = [0; 1024];
+
+ loop {
+ let bytes_read = tokio::time::timeout(Duration::from_secs(5), stream.read(&mut buffer))
+ .await
+ .expect("test server should respond before timeout")
+ .expect("test response should read");
+ assert_ne!(
+ bytes_read, 0,
+ "test server closed before sending a full response"
+ );
+ response.extend_from_slice(&buffer[..bytes_read]);
+
+ if response_is_complete(&response) {
+ return response;
+ }
+ }
+}
+
+fn response_is_complete(response: &[u8]) -> bool {
+ let Some(header_end) = find_bytes(response, b"\r\n\r\n") else {
+ return false;
+ };
+ let header_end = header_end + 4;
+ let headers = std::str::from_utf8(&response[..header_end])
+ .expect("test response headers should be UTF-8");
+ let content_length = headers
+ .lines()
+ .find_map(|line| {
+ let (name, value) = line.split_once(':')?;
+ name.eq_ignore_ascii_case("content-length").then(|| {
+ value
+ .trim()
+ .parse::<usize>()
+ .expect("content length should parse")
+ })
+ })
+ .expect("test response should have a content length");
+
+ response.len() >= header_end + content_length
+}
+
+fn parse_http_response(response: &[u8]) -> (StatusCode, String) {
+ let header_end =
+ find_bytes(response, b"\r\n\r\n").expect("response should contain headers") + 4;
+ let headers = std::str::from_utf8(&response[..header_end])
+ .expect("test response headers should be UTF-8");
+ let status = headers
+ .lines()
+ .next()
+ .and_then(|line| line.split_whitespace().nth(1))
+ .and_then(|status| status.parse::<u16>().ok())
+ .and_then(|status| StatusCode::from_u16(status).ok())
+ .expect("test response should contain a valid status");
+ let body = std::str::from_utf8(&response[header_end..])
+ .expect("test response body should be UTF-8")
+ .to_string();
+
+ (status, body)
+}
+
+fn find_bytes(haystack: &[u8], needle: &[u8]) -> Option<usize> {
+ haystack
+ .windows(needle.len())
+ .position(|window| window == needle)
+}
+
+#[allow(deprecated)]
+#[tokio::test]
+async fn internal_probe_accepts_both_header_conventions_and_rejects_wrong_keys() {
+ for header in [INTERNAL_API_KEY_HEADER, LEGACY_DSS_INTERNAL_API_KEY_HEADER] {
+ let valid_request = Request::get("/internal-auth-probe")
+ .header(header, TEST_INTERNAL_API_KEY)
+ .body(Body::empty())
+ .expect("valid probe request should build");
+ let (status, body) = send(valid_request).await;
+ assert_eq!(status, StatusCode::OK);
+ assert_eq!(body, json!({ "authorized": true }));
+
+ let invalid_request = Request::get("/internal-auth-probe")
+ .header(header, WRONG_INTERNAL_API_KEY)
+ .body(Body::empty())
+ .expect("invalid probe request should build");
+ assert_unauthorized(send(invalid_request).await);
+ }
+}
diff --git a/services/connection_gateway/src/context.rs b/services/connection_gateway/src/context.rs
index d1acc5f3a6..936dd7dcc6 100644
--- a/services/connection_gateway/src/context.rs
+++ b/services/connection_gateway/src/context.rs
@@ -6,11 +6,15 @@ use frecency::{
outbound::postgres::FrecencyPgStorage,
};
use last_online_tracker::inbound::LastOnlineWorker;
-use macro_auth::{InternalApiKey, middleware::decode_jwt::JwtValidationArgs};
+use macro_authorization::{
+ MacroAuthJwtValidator, MacroAuthorizationServiceImpl, MacroAuthorizationState,
+};
use redis::{RedisError, aio::MultiplexedConnection};
use std::sync::Arc;
use stream::domain::StreamManager;
+pub type AuthorizationService = MacroAuthorizationServiceImpl<MacroAuthJwtValidator>;
+
#[derive(Clone, FromRef)]
pub struct ApiContext {
pub connection_manager: crate::service::connection::ConnectionManager,
@@ -31,8 +35,7 @@ impl ApiContext {
pub struct AppState {
pub context: ApiContext,
pub config: Arc<Config>,
- pub jwt_args: JwtValidationArgs,
- pub internal_api_key: InternalApiKey,
+ pub authorization_state: MacroAuthorizationState<AuthorizationService>,
pub frecency_worker: Arc<FrecencyAggregatorWorkerHandle>,
}
diff --git a/services/connection_gateway/src/main.rs b/services/connection_gateway/src/main.rs
index b9cb8aeac2..74190673d4 100644
--- a/services/connection_gateway/src/main.rs
+++ b/services/connection_gateway/src/main.rs
@@ -7,7 +7,10 @@ mod model;
mod service;
use std::{sync::Arc, time::Duration};
-use crate::{api::router, context::AppState};
+use crate::{
+ api::router,
+ context::{AppState, AuthorizationService},
+};
use anyhow::{Context, Result};
use axum::http::{
Method,
@@ -29,6 +32,7 @@ use last_online_tracker::{
outbound::{redis::RedisLastOnlineRepo, time::DefaultTime as LastOnlineDefaultTime},
};
use macro_auth::middleware::decode_jwt::JwtValidationArgs;
+use macro_authorization::{InternalAuthConfig, MacroAuthJwtValidator, MacroAuthorizationState};
use macro_entrypoint::MacroEntrypoint;
use macro_env::Environment;
use service::dynamodb::create_dynamo_db_connection_manager;
@@ -118,12 +122,18 @@ async fn main() -> Result<()> {
tokio::spawn(poll_messages(context.clone()));
let config = Arc::new(config);
+ let authorization_state = MacroAuthorizationState::new(Arc::new(AuthorizationService::new(
+ MacroAuthJwtValidator::new(jwt_args),
+ InternalAuthConfig {
+ api_key: config.internal_api_key.to_string(),
+ default_user_id: None,
+ },
+ )));
let app = router(AppState {
context,
- internal_api_key: config.internal_api_key.clone(),
config: Arc::clone(&config),
- jwt_args,
+ authorization_state,
frecency_worker: Arc::new(FrecencyAggregatorWorkerHandle::new_worker(
PullAggregatorImpl::new(FrecencyPgProcessor::new(pgpool), DefaultTime),
Duration::from_secs(60),