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): migrate connection gateway to gateway (#6165)

Will Hutchinsonbfe5c5428fe5
diff --git a/apps/web/scripts/services.ts b/apps/web/scripts/services.ts
index 6210f79b86..6f1fe2422d 100644
--- a/apps/web/scripts/services.ts
+++ b/apps/web/scripts/services.ts
@@ -58,8 +58,8 @@ export const services: Service[] = [
},
{
name: "connection-gateway",
- dev: "https://connection-gateway-dev.macro.com/api-doc/openapi.json",
- prod: "https://connection-gateway-service.macro.com/api-doc/openapi.json",
+ dev: "https://dev-gateway.macro.com/connection-gateway/api-doc/openapi.json",
+ prod: "https://gateway.macro.com/connection-gateway/api-doc/openapi.json",
local: "http://localhost:8082/api-doc/openapi.json",
output: "../src/lib/service-clients/service-connection/",
orvalKey: "connectionGateway",
diff --git a/apps/web/src/lib/core/constant/servers.ts b/apps/web/src/lib/core/constant/servers.ts
index da3bbb0b49..8e6e0fc0ca 100644
--- a/apps/web/src/lib/core/constant/servers.ts
+++ b/apps/web/src/lib/core/constant/servers.ts
@@ -35,7 +35,7 @@ const serverHostRemote = {
'document-storage-service': `${gatewayHost}/dss`,
'websocket-service': `wss://services${devServerSuffix}.macro.com`,
'cognition-service': `https://document-cognition${devServerSuffix}.macro.com`,
- 'connection-gateway': `wss://connection-gateway${devServerSuffix}.macro.com`,
+ 'connection-gateway': `${gatewayHost.replace(/^http/, 'ws')}/connection-gateway`,
'notification-service': `${gatewayHost}/notification`,
'static-file': `https://static-file-service${devServerSuffix}.macro.com`,
'unfurl-service': `${gatewayHost}/unfurl`,
diff --git a/apps/web/src/lib/service-clients/service-connection/websocket.ts b/apps/web/src/lib/service-clients/service-connection/websocket.ts
index 24562616f3..06666e847b 100644
--- a/apps/web/src/lib/service-clients/service-connection/websocket.ts
+++ b/apps/web/src/lib/service-clients/service-connection/websocket.ts
@@ -34,7 +34,7 @@ async function resolveWsUrl() {
const apiToken = await getMacroApiToken();
if (!apiToken) throw new Error('No Macro API token');
- return `${wsHost}/?macro-api-token=${apiToken}`;
+ return `${wsHost}?macro-api-token=${apiToken}`;
}
await fetchToken();
return wsHost;
diff --git a/crates/macro_service_urls/src/lib.rs b/crates/macro_service_urls/src/lib.rs
index 1379463b68..ac055a90cd 100644
--- a/crates/macro_service_urls/src/lib.rs
+++ b/crates/macro_service_urls/src/lib.rs
@@ -527,8 +527,14 @@ service_url! {
/// Connection gateway HTTP API URL.
pub ConnectionGatewayUrl {
local: "http://localhost:8082",
- dev: "https://connection-gateway-dev.macro.com",
- prod: "https://connection-gateway.macro.com",
+ dev: "https://dev-gateway.macro.com/connection-gateway",
+ prod: "https://gateway.macro.com/connection-gateway",
+ },
+ /// Connection gateway WebSocket URL.
+ pub ConnectionGatewayWebsocketUrl {
+ local: "ws://localhost:8082",
+ dev: "wss://dev-gateway.macro.com/connection-gateway",
+ prod: "wss://gateway.macro.com/connection-gateway",
},
/// Document cognition service API URL.
pub DocumentCognitionServiceUrl {
diff --git a/crates/macro_service_urls/src/test.rs b/crates/macro_service_urls/src/test.rs
index 8d258566c0..68aa7adf43 100644
--- a/crates/macro_service_urls/src/test.rs
+++ b/crates/macro_service_urls/src/test.rs
@@ -74,6 +74,49 @@ fn connection_gateway_url_parses() {
assert_parses_for_all_environments(ConnectionGatewayUrl::default_for_environment);
}
+#[test]
+fn connection_gateway_url_has_no_trailing_slash() {
+ for environment in ENVS {
+ let url = ConnectionGatewayUrl::default_for_environment(environment);
+ assert!(
+ !url.as_ref().ends_with('/'),
+ "clients concatenate paths, so {} must not end with /",
+ url.as_ref()
+ );
+ }
+}
+
+#[test]
+fn connection_gateway_websocket_url_parses() {
+ assert_parses_for_all_environments(ConnectionGatewayWebsocketUrl::default_for_environment);
+}
+
+#[test]
+fn connection_gateway_websocket_url_has_no_trailing_slash() {
+ for environment in ENVS {
+ let url = ConnectionGatewayWebsocketUrl::default_for_environment(environment);
+ assert!(
+ !url.as_ref().ends_with('/'),
+ "clients concatenate paths, so {} must not end with /",
+ url.as_ref()
+ );
+ }
+}
+
+#[test]
+fn connection_gateway_websocket_url_is_the_ws_form_of_the_http_url() {
+ for environment in ENVS {
+ let http = ConnectionGatewayUrl::default_for_environment(environment);
+ let websocket = ConnectionGatewayWebsocketUrl::default_for_environment(environment);
+ let expected = http.as_ref().replacen("http", "ws", 1);
+ assert_eq!(
+ websocket.as_ref(),
+ expected,
+ "{environment:?}: websocket URL must be the http URL with the scheme swapped to ws"
+ );
+ }
+}
+
#[test]
fn document_cognition_service_url_parses() {
assert_parses_for_all_environments(DocumentCognitionServiceUrl::default_for_environment);
@@ -330,6 +373,10 @@ fn exported_service_urls_match_local_values() {
service_urls.connection_gateway_url.as_ref(),
"http://localhost:8082",
);
+ assert_eq!(
+ service_urls.connection_gateway_websocket_url.as_ref(),
+ "ws://localhost:8082",
+ );
assert_eq!(
service_urls.document_cognition_service_url.as_ref(),
"http://localhost:8085",
@@ -394,7 +441,11 @@ fn exported_service_urls_match_dev_values() {
);
assert_eq!(
service_urls.connection_gateway_url.as_ref(),
- "https://connection-gateway-dev.macro.com",
+ "https://dev-gateway.macro.com/connection-gateway",
+ );
+ assert_eq!(
+ service_urls.connection_gateway_websocket_url.as_ref(),
+ "wss://dev-gateway.macro.com/connection-gateway",
);
assert_eq!(
service_urls.document_cognition_service_url.as_ref(),
@@ -461,7 +512,11 @@ fn exported_service_urls_match_prod_values() {
);
assert_eq!(
service_urls.connection_gateway_url.as_ref(),
- "https://connection-gateway.macro.com",
+ "https://gateway.macro.com/connection-gateway",
+ );
+ assert_eq!(
+ service_urls.connection_gateway_websocket_url.as_ref(),
+ "wss://gateway.macro.com/connection-gateway",
);
assert_eq!(
service_urls.document_cognition_service_url.as_ref(),
@@ -531,6 +586,10 @@ fn exported_service_url_override_names_are_derived_from_env_var_names() {
ConnectionGatewayUrl::local().override_env_var_name(),
"OVERRIDE_CONNECTION_GATEWAY_URL",
);
+ assert_eq!(
+ ConnectionGatewayWebsocketUrl::local().override_env_var_name(),
+ "OVERRIDE_CONNECTION_GATEWAY_WEBSOCKET_URL",
+ );
assert_eq!(
DocumentCognitionServiceUrl::local().override_env_var_name(),
"OVERRIDE_DOCUMENT_COGNITION_SERVICE_URL",
diff --git a/infra/packages/shared/src/gateway_priorities.ts b/infra/packages/shared/src/gateway_priorities.ts
index a36b36f89a..2fc18f7085 100644
--- a/infra/packages/shared/src/gateway_priorities.ts
+++ b/infra/packages/shared/src/gateway_priorities.ts
@@ -11,6 +11,7 @@ export enum GatewayService {
SEARCH_PROCESSING_SERVICE = 'SEARCH_PROCESSING_SERVICE',
IMAGE_PROXY_SERVICE = 'IMAGE_PROXY_SERVICE',
AGENT_HARNESS_SERVICE = 'AGENT_HARNESS_SERVICE',
+ CONNECTION_GATEWAY = 'CONNECTION_GATEWAY',
}
/**
@@ -30,6 +31,7 @@ export const GATEWAY_PRIORITIES: GatewayPriorityMap = {
[GatewayService.SEARCH_PROCESSING_SERVICE]: 50,
[GatewayService.IMAGE_PROXY_SERVICE]: 60,
[GatewayService.AGENT_HARNESS_SERVICE]: 70,
+ [GatewayService.CONNECTION_GATEWAY]: 90,
[GatewayService.CONVERT_SERVICE]: 3000,
};
diff --git a/infra/packages/shared/src/service_urls.ts b/infra/packages/shared/src/service_urls.ts
index 44699428a1..6df92c1d7f 100644
--- a/infra/packages/shared/src/service_urls.ts
+++ b/infra/packages/shared/src/service_urls.ts
@@ -39,7 +39,7 @@ const DEV_SERVICE_URLS: ServiceUrlMap = {
[ServiceUrl.DOCUMENT_STORAGE_SERVICE_URL]:
'https://dev-gateway.macro.com/dss',
[ServiceUrl.CONNECTION_GATEWAY_URL]:
- 'https://connection-gateway-dev.macro.com',
+ 'https://dev-gateway.macro.com/connection-gateway',
[ServiceUrl.DOCUMENT_COGNITION_SERVICE_URL]:
'https://document-cognition-dev.macro.com',
[ServiceUrl.LEXICAL_SERVICE_URL]:
@@ -61,7 +61,8 @@ const PROD_SERVICE_URLS: ServiceUrlMap = {
'https://gateway.macro.com/notification',
[ServiceUrl.AUTHENTICATION_SERVICE_URL]: 'https://auth-service.macro.com',
[ServiceUrl.DOCUMENT_STORAGE_SERVICE_URL]: 'https://gateway.macro.com/dss',
- [ServiceUrl.CONNECTION_GATEWAY_URL]: 'https://connection-gateway.macro.com',
+ [ServiceUrl.CONNECTION_GATEWAY_URL]:
+ 'https://gateway.macro.com/connection-gateway',
[ServiceUrl.DOCUMENT_COGNITION_SERVICE_URL]:
'https://document-cognition.macro.com',
[ServiceUrl.LEXICAL_SERVICE_URL]:
diff --git a/infra/stacks/connection-gateway/connection_gateway.ts b/infra/stacks/connection-gateway/connection_gateway.ts
index 04b74343f4..fdaab16178 100644
--- a/infra/stacks/connection-gateway/connection_gateway.ts
+++ b/infra/stacks/connection-gateway/connection_gateway.ts
@@ -9,15 +9,20 @@ import {
datadogAgentContainer,
fargateLogRouterSidecarContainer,
serviceLoadBalancer,
+ ServiceTargetGroup,
} from '../../packages/resources';
import { EcrImage } from '../../packages/service';
import {
BASE_DOMAIN,
CLOUD_TRAIL_SNS_TOPIC_ARN,
DopplerEcsEnvironment,
+ getGatewayAlb,
+ GatewayService,
stack,
} from '../../packages/shared';
+const gatewayLoadBalancer = getGatewayAlb();
+
const BASE_NAME = pulumi.getProject();
const REPO_ROOT = '../../..';
@@ -105,6 +110,23 @@ export class ConnectionGateway extends pulumi.ComponentResource {
this.serviceAlbSg = sg.serviceAlbSg;
this.serviceSg = sg.serviceSg;
+ const gatewayTargetGroup = new ServiceTargetGroup(
+ `${stack}-${BASE_NAME}`,
+ {
+ tags: this.tags,
+ listenerArn: gatewayLoadBalancer.httpsListenerArn,
+ vpcId: vpc.vpcId,
+ containerPort: serviceContainerPort,
+ service: GatewayService.CONNECTION_GATEWAY,
+ healthCheckPath,
+ pathPatterns: ['/connection-gateway', '/connection-gateway/*'],
+ serviceSecurityGroupId: this.serviceSg.id,
+ albSecurityGroupId: gatewayLoadBalancer.albSecurityGroupId,
+ deregistrationDelay: 30,
+ },
+ { parent: this }
+ );
+
// lb
const { targetGroup, lb, listener } = serviceLoadBalancer(this, {
serviceName: BASE_NAME, // service name
@@ -197,6 +219,18 @@ export class ConnectionGateway extends pulumi.ComponentResource {
enable: true,
rollback: true,
},
+ loadBalancers: [
+ {
+ targetGroupArn: targetGroup.arn,
+ containerName: 'service',
+ containerPort: serviceContainerPort,
+ },
+ {
+ targetGroupArn: gatewayTargetGroup.target_group.arn,
+ containerName: 'service',
+ containerPort: serviceContainerPort,
+ },
+ ],
taskDefinitionArgs: {
taskRole: {
roleArn: this.role.arn,
@@ -251,6 +285,10 @@ export class ConnectionGateway extends pulumi.ComponentResource {
},
{
parent: this,
+ // ECS refuses a service whose target group is not yet associated with
+ // a load balancer; it is the listener rule that creates that
+ // association
+ dependsOn: [gatewayTargetGroup.listener_rule],
}
);
diff --git a/packages/sdk/src/config.ts b/packages/sdk/src/config.ts
index c654d7eede..7f884fde26 100644
--- a/packages/sdk/src/config.ts
+++ b/packages/sdk/src/config.ts
@@ -35,7 +35,7 @@ export const HOSTS: Record<Env, Record<ServiceName, string>> = {
search: 'https://dev-gateway.macro.com/dss',
'scheduled-action': 'https://agent-schedule-dev.macro.com',
'static-files': 'https://static-file-service-dev.macro.com',
- connection: 'https://connection-gateway-dev.macro.com',
+ connection: 'https://dev-gateway.macro.com/connection-gateway',
contacts: 'https://dev-gateway.macro.com/contacts',
unfurl: 'https://dev-gateway.macro.com/unfurl',
},
@@ -50,7 +50,7 @@ export const HOSTS: Record<Env, Record<ServiceName, string>> = {
search: 'https://gateway.macro.com/dss',
'scheduled-action': 'https://agent-schedule.macro.com',
'static-files': 'https://static-file-service.macro.com',
- connection: 'https://connection-gateway.macro.com',
+ connection: 'https://gateway.macro.com/connection-gateway',
contacts: 'https://gateway.macro.com/contacts',
unfurl: 'https://gateway.macro.com/unfurl',
},
diff --git a/services/connection_gateway/src/api.rs b/services/connection_gateway/src/api.rs
index 07f71b0f84..a9587a3112 100644
--- a/services/connection_gateway/src/api.rs
+++ b/services/connection_gateway/src/api.rs
@@ -1,5 +1,5 @@
use crate::context::AppState;
-use axum::Router;
+use axum::{Router, routing::get};
#[allow(unused_imports)]
pub use connection_gateway_models::{
BatchSendMessageBody, BatchSendUniqueMessagesBody, SendMessageBody,
@@ -16,12 +16,36 @@ pub(crate) mod swagger;
#[cfg(test)]
mod test;
+const GATEWAY_PATH_PREFIX: &str = "/connection-gateway";
+const GATEWAY_PATH_PREFIX_SLASH: &str = "/connection-gateway/";
+
pub fn router(state: AppState) -> Router {
- Router::new()
+ let inner = Router::new()
.merge(connection::router())
.nest("/message", message::router(state.clone()))
.nest("/track", entities::router(state.clone()))
.merge(health::router())
+ .with_state(state.clone());
+ mount_at_root_and_prefix(inner)
+ .merge(
+ Router::new()
+ .route(GATEWAY_PATH_PREFIX_SLASH, get(connection::ws_handler))
+ .with_state(state),
+ )
+ .merge(swagger_ui())
+}
+
+pub(crate) fn swagger_ui() -> Router {
+ Router::new()
.merge(SwaggerUi::new("/docs").url("/api-doc/openapi.json", swagger::ApiDoc::openapi()))
- .with_state(state)
+ .merge(SwaggerUi::new(format!("{GATEWAY_PATH_PREFIX}/docs")).url(
+ format!("{GATEWAY_PATH_PREFIX}/api-doc/openapi.json"),
+ swagger::ApiDoc::openapi(),
+ ))
+}
+
+pub(crate) fn mount_at_root_and_prefix(inner: Router) -> Router {
+ Router::new()
+ .merge(inner.clone())
+ .nest(GATEWAY_PATH_PREFIX, inner)
}
diff --git a/services/connection_gateway/src/api/test.rs b/services/connection_gateway/src/api/test.rs
index 31fd06a116..c5f94bf887 100644
--- a/services/connection_gateway/src/api/test.rs
+++ b/services/connection_gateway/src/api/test.rs
@@ -169,7 +169,7 @@ async fn websocket_rejects_invalid_credentials_before_infrastructure_state() {
vec![("authorization", "Bearer not-a-token")],
vec![(INTERNAL_API_KEY_HEADER, TEST_INTERNAL_API_KEY)],
] {
- let (status, body) = send_websocket_request(address, &headers).await;
+ let (status, body) = send_websocket_request(address, WEBSOCKET_PATH, &headers).await;
assert_eq!(status, StatusCode::UNAUTHORIZED);
assert_eq!(body, UNAUTHORIZED_BODY);
}
@@ -179,13 +179,14 @@ async fn websocket_rejects_invalid_credentials_before_infrastructure_state() {
async fn send_websocket_request(
address: std::net::SocketAddr,
+ path: &str,
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\
+ "GET {path} HTTP/1.1\r\n\
Host: {address}\r\n\
Connection: Upgrade\r\n\
Upgrade: websocket\r\n\
@@ -297,3 +298,103 @@ async fn internal_probe_accepts_both_header_conventions_and_rejects_wrong_keys()
assert_unauthorized(send(invalid_request).await);
}
}
+
+#[tokio::test]
+async fn health_is_reachable_at_root_and_gateway_prefix() {
+ for path in ["/health", "/connection-gateway/health"] {
+ let response = super::mount_at_root_and_prefix(super::health::router())
+ .oneshot(
+ Request::builder()
+ .uri(path)
+ .method("GET")
+ .body(Body::empty())
+ .unwrap(),
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(response.status(), StatusCode::OK, "{path}");
+ }
+}
+
+#[tokio::test]
+async fn unprefixed_unknown_path_is_not_rewritten_onto_the_prefix() {
+ let response = super::mount_at_root_and_prefix(super::health::router())
+ .oneshot(
+ Request::builder()
+ .uri("/missing")
+ .method("GET")
+ .body(Body::empty())
+ .unwrap(),
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(response.status(), StatusCode::NOT_FOUND);
+}
+
+#[tokio::test]
+async fn websocket_upgrade_is_authorized_at_root_and_gateway_prefix() {
+ for path in [
+ WEBSOCKET_PATH,
+ "/connection-gateway",
+ "/connection-gateway/",
+ "/connection-gateway?macro-api-token=not-a-token",
+ ] {
+ 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,
+ super::mount_at_root_and_prefix(test_router())
+ .merge(
+ Router::new()
+ .route("/connection-gateway/", get(connection::ws_handler))
+ .with_state(test_state()),
+ )
+ .into_make_service(),
+ )
+ .await
+ .expect("test server should run");
+ });
+
+ let (status, body) =
+ send_websocket_request(address, path, &[("authorization", "Bearer not-a-token")]).await;
+ assert_eq!(status, StatusCode::UNAUTHORIZED, "{path}");
+ assert_eq!(body, UNAUTHORIZED_BODY, "{path}");
+
+ server.abort();
+ }
+}
+
+#[tokio::test]
+async fn openapi_document_is_served_at_root_and_gateway_prefix() {
+ for path in [
+ "/api-doc/openapi.json",
+ "/connection-gateway/api-doc/openapi.json",
+ ] {
+ let response = super::swagger_ui()
+ .oneshot(
+ Request::builder()
+ .uri(path)
+ .method("GET")
+ .body(Body::empty())
+ .unwrap(),
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(response.status(), StatusCode::OK, "{path}");
+ let body = response.into_body().collect().await.unwrap().to_bytes();
+ let spec: Value = serde_json::from_slice(&body)
+ .unwrap_or_else(|_| panic!("{path} should be OpenAPI JSON"));
+ assert!(
+ spec.get("openapi").is_some(),
+ "{path} should contain an openapi version"
+ );
+ }
+}