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(image_proxy_service): migrate image proxy service to gateway (#6145)

Will Hutchinson12206f885168
diff --git a/apps/web/src/lib/core/constant/servers.ts b/apps/web/src/lib/core/constant/servers.ts
index 14089b5191..306a56bfb5 100644
--- a/apps/web/src/lib/core/constant/servers.ts
+++ b/apps/web/src/lib/core/constant/servers.ts
@@ -41,7 +41,7 @@ const serverHostRemote = {
'unfurl-service': `${gatewayHost}/unfurl`,
contacts: `${gatewayHost}/contacts`,
'email-service': `https://email-service${devServerSuffix}.macro.com`,
- 'image-proxy-service': `https://image-proxy${devServerSuffix}.macro.com`,
+ 'image-proxy-service': `${gatewayHost}/image-proxy`,
'scheduled-action': `https://agent-schedule${devServerSuffix}.macro.com`,
'agent-harness': `https://agent-harness${devServerSuffix}.macro.com`,
} as const;
diff --git a/crates/macro_service_urls/src/lib.rs b/crates/macro_service_urls/src/lib.rs
index c2ff8ce955..55bb759acd 100644
--- a/crates/macro_service_urls/src/lib.rs
+++ b/crates/macro_service_urls/src/lib.rs
@@ -576,8 +576,8 @@ service_url! {
/// Image proxy service API URL.
pub ImageProxyServiceUrl {
local: "http://localhost:8097",
- dev: "https://image-proxy-dev.macro.com",
- prod: "https://image-proxy.macro.com",
+ dev: "https://dev-gateway.macro.com/image-proxy",
+ prod: "https://gateway.macro.com/image-proxy",
},
/// Lexical conversion service API URL.
pub LexicalServiceUrl {
diff --git a/crates/macro_service_urls/src/test.rs b/crates/macro_service_urls/src/test.rs
index 39d5126efc..22059c4850 100644
--- a/crates/macro_service_urls/src/test.rs
+++ b/crates/macro_service_urls/src/test.rs
@@ -121,6 +121,18 @@ fn image_proxy_service_url_parses() {
assert_parses_for_all_environments(ImageProxyServiceUrl::default_for_environment);
}
+#[test]
+fn image_proxy_service_url_has_no_trailing_slash() {
+ for environment in ENVS {
+ let url = ImageProxyServiceUrl::default_for_environment(environment);
+ assert!(
+ !url.as_ref().ends_with('/'),
+ "clients concatenate paths, so {} must not end with /",
+ url.as_ref()
+ );
+ }
+}
+
#[test]
fn lexical_service_url_parses() {
assert_parses_for_all_environments(LexicalServiceUrl::default_for_environment);
@@ -393,7 +405,7 @@ fn exported_service_urls_match_dev_values() {
);
assert_eq!(
service_urls.image_proxy_service_url.as_ref(),
- "https://image-proxy-dev.macro.com",
+ "https://dev-gateway.macro.com/image-proxy",
);
assert_eq!(
service_urls.lexical_service_url.as_ref(),
@@ -456,7 +468,7 @@ fn exported_service_urls_match_prod_values() {
);
assert_eq!(
service_urls.image_proxy_service_url.as_ref(),
- "https://image-proxy.macro.com",
+ "https://gateway.macro.com/image-proxy",
);
assert_eq!(
service_urls.lexical_service_url.as_ref(),
diff --git a/infra/packages/shared/src/gateway_priorities.ts b/infra/packages/shared/src/gateway_priorities.ts
index a25fe133c0..960800db9e 100644
--- a/infra/packages/shared/src/gateway_priorities.ts
+++ b/infra/packages/shared/src/gateway_priorities.ts
@@ -9,6 +9,7 @@ export enum GatewayService {
NOTIFICATION_SERVICE = 'NOTIFICATION_SERVICE',
CONTACTS_SERVICE = 'CONTACTS_SERVICE',
SEARCH_PROCESSING_SERVICE = 'SEARCH_PROCESSING_SERVICE',
+ IMAGE_PROXY_SERVICE = 'IMAGE_PROXY_SERVICE',
}
/**
@@ -26,6 +27,7 @@ export const GATEWAY_PRIORITIES: GatewayPriorityMap = {
[GatewayService.NOTIFICATION_SERVICE]: 30,
[GatewayService.CONTACTS_SERVICE]: 40,
[GatewayService.SEARCH_PROCESSING_SERVICE]: 50,
+ [GatewayService.IMAGE_PROXY_SERVICE]: 60,
[GatewayService.CONVERT_SERVICE]: 3000,
};
diff --git a/infra/stacks/image-proxy-service/image-proxy-service.ts b/infra/stacks/image-proxy-service/image-proxy-service.ts
index ce5134c02a..2fd5d53f02 100644
--- a/infra/stacks/image-proxy-service/image-proxy-service.ts
+++ b/infra/stacks/image-proxy-service/image-proxy-service.ts
@@ -8,15 +8,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 = '../../..';
@@ -102,6 +107,22 @@ export class ImageProxyService 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.IMAGE_PROXY_SERVICE,
+ healthCheckPath,
+ pathPatterns: ['/image-proxy', '/image-proxy/*'],
+ serviceSecurityGroupId: this.serviceSg.id,
+ albSecurityGroupId: gatewayLoadBalancer.albSecurityGroupId,
+ },
+ { parent: this }
+ );
+
// lb
const { targetGroup, lb, listener } = serviceLoadBalancer(this, {
serviceName: BASE_NAME,
@@ -190,6 +211,21 @@ export class ImageProxyService extends pulumi.ComponentResource {
enable: true,
rollback: true,
},
+ // An explicit `loadBalancers` replaces the list awsx derives from
+ // `portMappings.targetGroup`, so the legacy entry must be listed here
+ // too.
+ loadBalancers: [
+ {
+ targetGroupArn: targetGroup.arn,
+ containerName: 'service',
+ containerPort: serviceContainerPort,
+ },
+ {
+ targetGroupArn: gatewayTargetGroup.target_group.arn,
+ containerName: 'service',
+ containerPort: serviceContainerPort,
+ },
+ ],
taskDefinitionArgs: {
taskRole: {
roleArn: this.role.arn,
@@ -244,12 +280,19 @@ export class ImageProxyService 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],
}
);
this.service = service;
- this.setupAutoScaling();
+ this.setupAutoScaling({
+ gatewayAlbArnSuffix: gatewayLoadBalancer.albArnSuffix,
+ gatewayTargetGroup: gatewayTargetGroup.target_group,
+ });
this.setupServiceAlarms();
@@ -377,7 +420,13 @@ export class ImageProxyService extends pulumi.ComponentResource {
return { serviceAlbSg, serviceSg };
}
- setupAutoScaling() {
+ setupAutoScaling({
+ gatewayAlbArnSuffix,
+ gatewayTargetGroup,
+ }: {
+ gatewayAlbArnSuffix: pulumi.Output<string>;
+ gatewayTargetGroup: aws.lb.TargetGroup;
+ }) {
if (!this.service) return;
const serviceScalableTarget = new aws.appautoscaling.Target(
@@ -393,19 +442,7 @@ export class ImageProxyService extends pulumi.ComponentResource {
{ parent: this }
);
- const lbPortion: pulumi.Output<string> = this.lb.arn.apply((arn) => {
- const parts = arn.split(':loadbalancer/');
- return parts[1];
- });
-
- const tgPortion: pulumi.Output<string> = this.targetGroup.arn.apply(
- (arn) => {
- const parts = arn.split(':');
- return parts[parts.length - 1];
- }
- );
-
- const resourceLabel = pulumi.interpolate`${lbPortion}/${tgPortion}`;
+ const resourceLabel = pulumi.interpolate`${gatewayAlbArnSuffix}/${gatewayTargetGroup.arnSuffix}`;
new aws.appautoscaling.Policy(
`${BASE_NAME}-scaling-policy-request-count-${stack}`,
diff --git a/infra/stacks/image-proxy-service/index.ts b/infra/stacks/image-proxy-service/index.ts
index 1e55057da3..816d67f2f8 100644
--- a/infra/stacks/image-proxy-service/index.ts
+++ b/infra/stacks/image-proxy-service/index.ts
@@ -1,6 +1,11 @@
import * as aws from '@pulumi/aws';
import * as pulumi from '@pulumi/pulumi';
-import { config, getMacroApiToken, stack } from '../../packages/shared';
+import {
+ BASE_DOMAIN,
+ config,
+ getMacroApiToken,
+ stack,
+} from '../../packages/shared';
import { get_coparse_api_vpc } from '../../packages/vpc';
import { ImageProxyService } from './image-proxy-service';
@@ -72,4 +77,6 @@ const imageProxyService = new ImageProxyService(
export const imageProxyServiceSgId = imageProxyService.serviceSg.id;
export const imageProxyServiceAlbSgId = imageProxyService.serviceAlbSg.id;
-export const imageProxyServiceUrl = pulumi.interpolate`${imageProxyService.domain}`;
+export const imageProxyServiceUrl = `https://${
+ stack === 'prod' ? '' : `${stack}-`
+}gateway.${BASE_DOMAIN}/image-proxy`;
diff --git a/services/image_proxy_service/src/api.rs b/services/image_proxy_service/src/api.rs
index 012bdab372..2e7fd86942 100644
--- a/services/image_proxy_service/src/api.rs
+++ b/services/image_proxy_service/src/api.rs
@@ -14,6 +14,15 @@ pub(crate) mod swagger;
#[cfg(test)]
mod test;
+/// Path prefix the shared gateway ALB forwards unmodified.
+const GATEWAY_PATH_PREFIX: &str = "/image-proxy";
+
+fn mount_at_root_and_prefix(inner: Router) -> Router {
+ Router::new()
+ .merge(inner.clone())
+ .nest(GATEWAY_PATH_PREFIX, inner)
+}
+
pub async fn setup_and_serve(state: ApiContext, port: usize) -> anyhow::Result<()> {
let env = state.environment;
let app = app(state);
@@ -36,12 +45,18 @@ pub async fn setup_and_serve(state: ApiContext, port: usize) -> anyhow::Result<(
fn app(state: ApiContext) -> Router {
let cors = macro_cors::cors_layer();
- api_router()
+ let inner = api_router()
.with_state(state)
.layer(ServiceBuilder::new().layer(TraceLayer::new_for_http()))
.merge(health::router())
- .layer(cors)
+ .layer(cors);
+
+ mount_at_root_and_prefix(inner)
.merge(SwaggerUi::new("/docs").url("/api-doc/openapi.json", swagger::ApiDoc::openapi()))
+ .merge(SwaggerUi::new("/image-proxy/docs").url(
+ "/image-proxy/api-doc/openapi.json",
+ swagger::ApiDoc::openapi(),
+ ))
}
fn api_router() -> Router<ApiContext> {
diff --git a/services/image_proxy_service/src/api/test.rs b/services/image_proxy_service/src/api/test.rs
index 68603ed3f1..54d719b605 100644
--- a/services/image_proxy_service/src/api/test.rs
+++ b/services/image_proxy_service/src/api/test.rs
@@ -15,7 +15,7 @@ use macro_env::Environment;
use serde_json::{Value, json};
use tower::ServiceExt;
-use super::*;
+use super::{health, mount_at_root_and_prefix, *};
const ACCESS_TOKEN_COOKIE: &str = "macro-access-token";
const INTERNAL_API_KEY_HEADER: &str = "x-internal-auth-key";
@@ -186,8 +186,57 @@ async fn proxy_rejects_internal_credentials_without_an_acting_user() {
#[tokio::test]
async fn health_remains_public() {
- let request = Request::get("/health").body(Body::empty()).unwrap();
- let (status, _) = send(request).await;
+ for path in ["/health", "/image-proxy/health"] {
+ let request = Request::get(path).body(Body::empty()).unwrap();
+ let (status, _) = send(request).await;
- assert_eq!(status, StatusCode::OK);
+ assert_eq!(status, StatusCode::OK, "{path}");
+ }
+}
+
+#[tokio::test]
+async fn health_is_reachable_at_root_and_gateway_prefix() {
+ for path in ["/health", "/image-proxy/health"] {
+ let response = mount_at_root_and_prefix(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 = mount_at_root_and_prefix(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 prefixed_proxy_reaches_existing_auth() {
+ let request = proxy_request("/image-proxy/proxy?url=http://127.0.0.1/image.png")
+ .body(Body::empty())
+ .unwrap();
+
+ assert_json_response(
+ send(request).await,
+ StatusCode::UNAUTHORIZED,
+ json!({ "message": "unauthorized" }),
+ );
}