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(sfs): use macro_authorization (#4915)

Will Hutchinsone4f17e433621
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.json
index b8297796f6..32e9c7a99f 100644
--- a/.github/workspace-dep-closures.json
+++ b/.github/workspace-dep-closures.json
@@ -5020,20 +5020,16 @@
"crates/ai_usage",
"crates/attachment",
"crates/cowlike",
- "crates/decode_jwt",
"crates/document_sub_type",
- "crates/entity_access_db_utils",
- "crates/ip_extractor",
"crates/macro_auth",
+ "crates/macro_authorization",
"crates/macro_aws_config",
"crates/macro_config",
"crates/macro_config_derive",
"crates/macro_cors",
- "crates/macro_db_client",
"crates/macro_entrypoint",
"crates/macro_env",
"crates/macro_env_var",
- "crates/macro_middleware",
"crates/macro_queues",
"crates/macro_service_urls",
"crates/macro_user_id",
@@ -5044,21 +5040,14 @@
"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_sfs",
- "crates/models_team",
"crates/non_empty",
"crates/prompt",
"crates/remote_env_var",
"crates/s3_key",
"crates/secretsmanager_client",
- "crates/system_properties",
- "crates/user_quota",
"crates/workspace-hack",
"services/static_file_service"
],
diff --git a/Cargo.lock b/Cargo.lock
index f851cf2e6c..9b8d42458e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -13621,13 +13621,13 @@ dependencies = [
"chrono",
"doppler-config",
"macro_auth",
+ "macro_authorization",
"macro_aws_config",
"macro_config",
"macro_cors",
"macro_entrypoint",
"macro_env",
"macro_env_var",
- "macro_middleware",
"macro_queues",
"macro_service_urls",
"model",
@@ -13640,7 +13640,6 @@ dependencies = [
"strum_macros",
"thiserror 2.0.18",
"tokio",
- "tower 0.5.3",
"tower-http",
"tracing",
"tracing-tree",
diff --git a/apps/web/src/lib/service-clients/service-static-files/generated/client.ts b/apps/web/src/lib/service-clients/service-static-files/generated/client.ts
index d008557b26..38068a9be3 100644
--- a/apps/web/src/lib/service-clients/service-static-files/generated/client.ts
+++ b/apps/web/src/lib/service-clients/service-static-files/generated/client.ts
@@ -144,6 +144,11 @@ export type handleGetMetadataResponse200 = {
status: 200;
};
+export type handleGetMetadataResponse401 = {
+ data: string;
+ status: 401;
+};
+
export type handleGetMetadataResponse404 = {
data: string;
status: 404;
@@ -158,6 +163,7 @@ export type handleGetMetadataResponseSuccess = handleGetMetadataResponse200 & {
headers: Headers;
};
export type handleGetMetadataResponseError = (
+ | handleGetMetadataResponse401
| handleGetMetadataResponse404
| handleGetMetadataResponse500
) & {
diff --git a/apps/web/src/lib/service-clients/service-static-files/openapi.json b/apps/web/src/lib/service-clients/service-static-files/openapi.json
index 34e190b651..f655bd020d 100644
--- a/apps/web/src/lib/service-clients/service-static-files/openapi.json
+++ b/apps/web/src/lib/service-clients/service-static-files/openapi.json
@@ -153,6 +153,16 @@
}
}
},
+ "401": {
+ "description": "",
+ "content": {
+ "text/plain": {
+ "schema": {
+ "type": "string"
+ }
+ }
+ }
+ },
"404": {
"description": "",
"content": {
diff --git a/services/image_proxy_service/src/config.rs b/services/image_proxy_service/src/config.rs
index 2c3530aa69..238aa84d5c 100644
--- a/services/image_proxy_service/src/config.rs
+++ b/services/image_proxy_service/src/config.rs
@@ -1,5 +1,5 @@
use anyhow::Context;
-use macro_middleware::auth::internal_access::InternalApiKey;
+use macro_auth::InternalApiKey;
pub use macro_env::Environment;
diff --git a/services/static_file_service/Cargo.toml b/services/static_file_service/Cargo.toml
index 883772176f..fa3b1120c4 100644
--- a/services/static_file_service/Cargo.toml
+++ b/services/static_file_service/Cargo.toml
@@ -18,7 +18,7 @@ path = "src/doppler_config.rs"
[features]
dynamodb_client_test = []
-local_auth = []
+local_auth = ["macro_authorization/local_auth"]
tree_trace = []
[dependencies]
@@ -31,19 +31,17 @@ axum = { workspace = true, features = ["tower-log", "ws"] }
chrono = { workspace = true }
doppler-config = { workspace = true }
macro_auth = { path = "../../crates/macro_auth" }
+macro_authorization = { path = "../../crates/macro_authorization" }
macro_aws_config = { path = "../../crates/macro_aws_config", features = ["s3"] }
-macro_cors = { path = "../../crates/macro_cors" }
macro_config = { path = "../../crates/macro_config" }
+macro_cors = { path = "../../crates/macro_cors" }
macro_entrypoint = { path = "../../crates/macro_entrypoint" }
macro_env = { path = "../../crates/macro_env" }
macro_env_var = { path = "../../crates/macro_env_var" }
macro_queues = { path = "../../crates/macro_queues" }
-macro_middleware = { path = "../../crates/macro_middleware", default-features = false, features = [
- "auth",
-] }
model = { path = "../../crates/model" }
-s3_key = { path = "../../crates/s3_key" }
models_sfs = { path = "../../crates/models_sfs" }
+s3_key = { path = "../../crates/s3_key" }
secretsmanager_client = { path = "../../crates/secretsmanager_client" }
serde = { workspace = true }
serde_dynamo = { workspace = true }
@@ -51,7 +49,6 @@ serde_json = { workspace = true }
strum_macros = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
-tower = { workspace = true }
tower-http = { workspace = true, features = ["limit", "timeout"] }
tracing.workspace = true
tracing-tree = { workspace = true }
diff --git a/services/static_file_service/src/api/context.rs b/services/static_file_service/src/api/context.rs
index 0aa3d88681..67469f2c3d 100644
--- a/services/static_file_service/src/api/context.rs
+++ b/services/static_file_service/src/api/context.rs
@@ -3,14 +3,19 @@ use crate::service::dynamodb::client::DynamodbClient;
use crate::service::s3::client::S3Client;
use aws_sdk_sqs::Client;
use axum::extract::FromRef;
-use macro_auth::InternalApiKey;
+use macro_authorization::{
+ MacroAuthJwtValidator, MacroAuthorizationServiceImpl, MacroAuthorizationState,
+};
use std::sync::Arc;
+/// Concrete authorization service used by the `MacroAuthorizationExtractor` in handlers.
+pub(crate) type AuthorizationService = MacroAuthorizationServiceImpl<MacroAuthJwtValidator>;
+
#[derive(Clone, FromRef)]
pub struct AppState {
pub metadata_client: DynamodbClient,
pub storage_client: Arc<S3Client>,
pub sqs_client: Client,
pub config: Arc<Config>,
- pub internal_api_key: InternalApiKey,
+ pub authorization_state: MacroAuthorizationState<AuthorizationService>,
}
diff --git a/services/static_file_service/src/api/file/bulk_delete_file.rs b/services/static_file_service/src/api/file/bulk_delete_file.rs
index 495f3ce6d8..edcbc29aa5 100644
--- a/services/static_file_service/src/api/file/bulk_delete_file.rs
+++ b/services/static_file_service/src/api/file/bulk_delete_file.rs
@@ -1,13 +1,13 @@
+use crate::api::context::AuthorizationService;
use crate::model::api::{BulkDeleteRequest, BulkDeleteResponse, DeleteResult};
use crate::service::dynamodb::client::DynamodbClient;
use crate::service::s3::client::S3Client;
+use axum::Json;
use axum::extract::State;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
-use axum::{Extension, Json};
-use macro_middleware::auth::internal_access::ValidInternalKey;
+use macro_authorization::MacroAuthorizationExtractor;
use model::response::ErrorResponse;
-use model::user::UserContext;
use std::sync::Arc;
use strum_macros::AsRefStr;
use thiserror::Error;
@@ -52,12 +52,11 @@ impl IntoResponse for BulkDeleteError {
)
)
]
-#[tracing::instrument(skip(metadata_client, storage_client, usr, internal_key), fields(user_id = usr.user_id), err)]
+#[tracing::instrument(skip(metadata_client, storage_client, user), fields(user_id = ?user.macro_user_id), err)]
pub async fn handle_bulk_delete_file(
State(metadata_client): State<DynamodbClient>,
State(storage_client): State<Arc<S3Client>>,
- usr: Extension<UserContext>,
- internal_key: Option<ValidInternalKey>,
+ user: MacroAuthorizationExtractor<AuthorizationService>,
Json(req): Json<BulkDeleteRequest>,
) -> Result<Json<BulkDeleteResponse>, BulkDeleteError> {
// Validate request
@@ -89,7 +88,7 @@ pub async fn handle_bulk_delete_file(
match metadata_results.get(file_id) {
Some(metadata) => {
// Skip owner check for internal requests
- if internal_key.is_none() && metadata.owner_id != usr.user_id {
+ if !user.is_internal_access && metadata.owner_id != user.macro_user_id.as_ref() {
tracing::warn!(file_id = file_id, "delete requested by non-owner");
results.push(DeleteResult {
file_id: file_id.clone(),
diff --git a/services/static_file_service/src/api/file/delete_file.rs b/services/static_file_service/src/api/file/delete_file.rs
index 38f7c4455d..b8f31e5132 100644
--- a/services/static_file_service/src/api/file/delete_file.rs
+++ b/services/static_file_service/src/api/file/delete_file.rs
@@ -1,12 +1,12 @@
+use crate::api::context::AuthorizationService;
use crate::service::dynamodb::client::DynamodbClient;
use crate::service::dynamodb::model::DeleteError;
use crate::service::s3::client::S3Client;
use axum::extract::{Path, State};
use axum::http::StatusCode;
use axum::response::IntoResponse;
-use axum::{Extension, response::Response};
-use macro_middleware::auth::internal_access::ValidInternalKey;
-use model::user::UserContext;
+use axum::response::Response;
+use macro_authorization::MacroAuthorizationExtractor;
use std::sync::Arc;
#[derive(serde::Deserialize)]
@@ -28,12 +28,11 @@ pub struct Params {
)
)
]
-#[tracing::instrument(skip(metadata_client, storage_client, usr, internal_key), fields(user_id = usr.user_id))]
+#[tracing::instrument(skip(metadata_client, storage_client, user), fields(user_id = ?user.macro_user_id))]
pub async fn handle_delete_file(
State(metadata_client): State<DynamodbClient>,
State(storage_client): State<Arc<S3Client>>,
- usr: Extension<UserContext>,
- internal_key: Option<ValidInternalKey>,
+ user: MacroAuthorizationExtractor<AuthorizationService>,
Path(Params { file_id }): Path<Params>,
) -> Result<Response, Response> {
let metadata = metadata_client
@@ -46,7 +45,7 @@ pub async fn handle_delete_file(
.ok_or_else(|| (StatusCode::NOT_FOUND, "not found").into_response())?;
// Skip owner check for internal requests
- if internal_key.is_none() && metadata.owner_id != usr.user_id {
+ if !user.is_internal_access && metadata.owner_id != user.macro_user_id.as_ref() {
tracing::warn!("delete requested by non-owner");
return Err((StatusCode::FORBIDDEN, "access denied").into_response());
}
diff --git a/services/static_file_service/src/api/file/get_file.rs b/services/static_file_service/src/api/file/get_file.rs
index 9ef09d5346..f6134ef6b1 100644
--- a/services/static_file_service/src/api/file/get_file.rs
+++ b/services/static_file_service/src/api/file/get_file.rs
@@ -1,8 +1,10 @@
use axum::extract::{Path, State};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
+use macro_authorization::MacroAuthorizationExtractor;
use std::sync::Arc;
+use crate::api::context::AuthorizationService;
use crate::service::dynamodb::client::DynamodbClient;
use crate::service::s3::client::S3Client;
@@ -19,14 +21,16 @@ pub struct Params {
),
responses(
(status = 200, body=String, description = "Presigned URL for the file"),
+ (status = 401, body=String),
(status = 404, body=String),
(status = 500, body=String)
)
)]
-#[tracing::instrument(skip(metadata_client, storage_client))]
+#[tracing::instrument(skip(metadata_client, storage_client, user), fields(user_id = ?user.macro_user_id))]
pub async fn handle_get_presigned_url(
State(metadata_client): State<DynamodbClient>,
State(storage_client): State<Arc<S3Client>>,
+ user: MacroAuthorizationExtractor<AuthorizationService>,
Path(Params { file_id }): Path<Params>,
) -> Result<Response, Response> {
// First get metadata to ensure file exists and is uploaded
diff --git a/services/static_file_service/src/api/file/metadata.rs b/services/static_file_service/src/api/file/metadata.rs
index 3c8ce8a897..298c07e6e0 100644
--- a/services/static_file_service/src/api/file/metadata.rs
+++ b/services/static_file_service/src/api/file/metadata.rs
@@ -1,9 +1,11 @@
+use crate::api::context::AuthorizationService;
use crate::model::api::*;
use crate::service::dynamodb::client::DynamodbClient;
use axum::extract::{Json, Path, State};
use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::response::Response;
+use macro_authorization::MacroAuthorizationExtractor;
#[derive(serde::Deserialize)]
pub struct Params {
@@ -18,13 +20,15 @@ pub struct Params {
),
responses(
(status = 200, body=GetFileMetadataResponse),
+ (status = 401, body=String),
(status = 404, body=String),
(status = 500, body=String)
)
)]
-#[tracing::instrument(skip(metadata_client))]
+#[tracing::instrument(skip(metadata_client, user), fields(user_id = ?user.macro_user_id))]
pub async fn handle_get_metadata(
State(metadata_client): State<DynamodbClient>,
+ user: MacroAuthorizationExtractor<AuthorizationService>,
Path(Params { file_id }): Path<Params>,
) -> Result<Response, Response> {
metadata_client
diff --git a/services/static_file_service/src/api/file/put_presigned_url.rs b/services/static_file_service/src/api/file/put_presigned_url.rs
index 73c0f63b64..16c0f6f8e1 100644
--- a/services/static_file_service/src/api/file/put_presigned_url.rs
+++ b/services/static_file_service/src/api/file/put_presigned_url.rs
@@ -1,15 +1,15 @@
use std::str::FromStr;
-use crate::api::context::AppState;
+use crate::api::context::{AppState, AuthorizationService};
use crate::model::api::*;
use crate::service::dynamodb::model::MetadataObject;
use axum::extract::{Json, State};
use axum::http::StatusCode;
use axum::response::IntoResponse;
-use axum::{Extension, response::Response};
+use axum::response::Response;
use chrono::Utc;
+use macro_authorization::MacroAuthorizationExtractor;
use model::document::FileType;
-use model::user::UserContext;
use uuid::Uuid;
#[utoipa::path(
@@ -22,10 +22,10 @@ use uuid::Uuid;
(status = 500, body=String),
),
)]
-#[tracing::instrument(skip(ctx, user), fields(user_id=user.user_id))]
+#[tracing::instrument(skip(ctx, user), fields(user_id=?user.macro_user_id))]
pub async fn put_presigned_url(
State(ctx): State<AppState>,
- user: Extension<UserContext>,
+ user: MacroAuthorizationExtractor<AuthorizationService>,
Json(request): Json<PutFileRequest>,
) -> Result<Response, Response> {
let content_type = if let Some(content_type) = request.content_type {
@@ -52,11 +52,7 @@ pub async fn put_presigned_url(
let s3_key = static_file_key.to_key();
let permalink = format!("{}/{}", ctx.config.static_file_service_url, s3_key);
- let owner_id = if !user.user_id.is_empty() {
- user.user_id.clone()
- } else {
- "nobody".to_string()
- };
+ let owner_id = user.macro_user_id.as_ref().to_string();
let metadata = MetadataObject {
file_id: id.clone(),
diff --git a/services/static_file_service/src/api/mod.rs b/services/static_file_service/src/api/mod.rs
index 76c8fe7b3d..dd6f63fd27 100644
--- a/services/static_file_service/src/api/mod.rs
+++ b/services/static_file_service/src/api/mod.rs
@@ -7,20 +7,22 @@ pub(crate) mod swagger;
use super::config::Config;
use super::service::dynamodb::client::DynamodbClient;
use super::service::s3::client::S3Client;
-use crate::api::context::AppState;
+use crate::api::context::{AppState, AuthorizationService};
use anyhow::Context;
use axum::Router;
use axum::extract::DefaultBodyLimit;
use macro_auth::middleware::decode_jwt::JwtValidationArgs;
-use macro_middleware::auth::internal_access::ValidInternalKey;
+use macro_authorization::{InternalAuthConfig, MacroAuthJwtValidator, MacroAuthorizationState};
use std::sync::Arc;
-use tower::ServiceBuilder;
use tower_http::limit::RequestBodyLimitLayer;
use utoipa::OpenApi;
use utoipa_swagger_ui::SwaggerUi;
static MAX_REQUEST_SIZE: usize = 4096;
+/// Identity assumed for internal service callers that don't forward an acting user.
+pub const MACRO_INTERNAL_USER_ID: &str = "macro|[email protected]";
+
pub async fn setup_and_serve(
config: Config,
jwt_validation_args: JwtValidationArgs,
@@ -44,12 +46,20 @@ pub async fn setup_and_serve(
config.static_storage_bucket.as_ref().to_owned(),
);
+ let authorization_state = MacroAuthorizationState::new(Arc::new(AuthorizationService::new(
+ MacroAuthJwtValidator::new(jwt_validation_args),
+ InternalAuthConfig {
+ api_key: config.internal_api_key.as_ref().to_string(),
+ default_user_id: Some(MACRO_INTERNAL_USER_ID.to_string()),
+ },
+ )));
+
let state = AppState {
metadata_client,
storage_client: Arc::new(storage_client),
sqs_client,
- internal_api_key: config.internal_api_key.clone(),
config: Arc::new(config),
+ authorization_state,
};
let state_clone = state.clone();
@@ -59,36 +69,18 @@ pub async fn setup_and_serve(
let port = state.config.port;
let environment = state.config.environment;
+ // Authentication is enforced per-handler by `MacroAuthorizationExtractor`,
+ // which accepts user credentials (JWT) as well as the internal service key.
let app = Router::new()
.nest(
"/api", // needed for cdn routing
- Router::new()
- .merge(
- file::router().layer(
- ServiceBuilder::new()
- .layer(axum::middleware::from_fn_with_state(
- jwt_validation_args.clone(),
- macro_middleware::auth::decode_jwt::handler,
- ))
- .layer(cors.clone()),
- ),
- )
- .merge(health::router().layer(cors.clone())),
+ Router::new().merge(file::router()).merge(health::router()),
)
.nest(
"/internal",
- // Create a copy of the file router, but use internal auth middleware
- // instead of jwt auth
- file::router().layer(
- ServiceBuilder::new()
- .layer(axum::middleware::from_extractor_with_state::<
- ValidInternalKey,
- _,
- >(state.clone()))
- .layer(axum::middleware::from_fn(
- macro_middleware::auth::initialize_user_context::handler,
- )),
- ),
+ // Same file router; internal callers authenticate with the internal
+ // service key headers instead of a jwt
+ file::router(),
)
.with_state(state)
.merge(