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(convert): use macro_authorization (#4921)

Will Hutchinson6b17a39f6348
diff --git a/Cargo.lock b/Cargo.lock
index 9b8d42458e..5aa96f843f 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3181,6 +3181,7 @@ dependencies = [
"http-body-util",
"lambda_client",
"macro_auth",
+ "macro_authorization",
"macro_aws_config",
"macro_config",
"macro_cors",
diff --git a/services/convert_service/Cargo.toml b/services/convert_service/Cargo.toml
index 651c6d7977..6cd2c615db 100644
--- a/services/convert_service/Cargo.toml
+++ b/services/convert_service/Cargo.toml
@@ -27,6 +27,7 @@ futures = { workspace = true }
http-body-util = { workspace = true }
lambda_client = { path = "../../crates/lambda_client" }
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" }
@@ -34,9 +35,7 @@ macro_db_client = { path = "../../crates/macro_db_client" }
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", default-features = false, features = [
- "auth",
-] }
+macro_middleware = { path = "../../crates/macro_middleware", default-features = false }
macro_queues = { path = "../../crates/macro_queues" }
macro_uuid = { path = "../../crates/macro_uuid" }
model = { path = "../../crates/model" }
diff --git a/services/convert_service/src/api/backfill/backfill_docx.rs b/services/convert_service/src/api/backfill/backfill_docx.rs
index b14016a399..d0dc34a061 100644
--- a/services/convert_service/src/api/backfill/backfill_docx.rs
+++ b/services/convert_service/src/api/backfill/backfill_docx.rs
@@ -11,6 +11,7 @@ use axum::{
response::{IntoResponse, Response},
};
use futures::StreamExt;
+use macro_authorization::InternalMacroAuthorizationExtractor;
use model::{
convert::ConvertQueueMessage,
document::{BomPart, BomPartWithContent, DocumentMetadata},
@@ -19,7 +20,7 @@ use model::{
};
use s3_key::{build_docx_to_pdf_converted_document_key, build_temp_docx_key};
-use crate::api::context::ApiContext;
+use crate::api::context::{ApiContext, AuthorizationService};
/// Backfill docx all
#[utoipa::path(
@@ -39,9 +40,10 @@ use crate::api::context::ApiContext;
(status = 500, description = "internal server error", body = ErrorResponse),
)
)]
-#[tracing::instrument(skip(ctx))]
+#[tracing::instrument(skip(ctx, _internal_authorization))]
pub async fn handler(
State(ctx): State<ApiContext>,
+ _internal_authorization: InternalMacroAuthorizationExtractor<AuthorizationService>,
extract::Query(pagination): extract::Query<PaginationQueryParams>,
) -> Result<Response, Response> {
let pagination = Pagination::from_query_params(pagination);
diff --git a/services/convert_service/src/api/context.rs b/services/convert_service/src/api/context.rs
index 0423d28d13..a12a916a8e 100644
--- a/services/convert_service/src/api/context.rs
+++ b/services/convert_service/src/api/context.rs
@@ -1,15 +1,19 @@
use axum::extract::FromRef;
-use macro_auth::InternalApiKey;
+use macro_authorization::{
+ MacroAuthJwtValidator, MacroAuthorizationServiceImpl, MacroAuthorizationState,
+};
use s3_client::S3;
use std::sync::Arc;
use crate::config::Config;
+pub(crate) type AuthorizationService = MacroAuthorizationServiceImpl<MacroAuthJwtValidator>;
+
#[derive(Clone, FromRef)]
pub struct ApiContext {
pub db: sqlx::PgPool,
pub s3_client: S3,
pub sqs_client: Arc<sqs_client::SQS>,
- pub internal_api_key: InternalApiKey,
+ pub authorization_state: MacroAuthorizationState<AuthorizationService>,
pub config: Arc<Config>,
}
diff --git a/services/convert_service/src/api/convert.rs b/services/convert_service/src/api/convert.rs
index 06b4ee0e36..ee1b93f16a 100644
--- a/services/convert_service/src/api/convert.rs
+++ b/services/convert_service/src/api/convert.rs
@@ -8,6 +8,7 @@ use axum::{
response::{IntoResponse, Response},
routing::post,
};
+use macro_authorization::InternalMacroAuthorizationExtractor;
use model::{
convert::ConvertRequest,
document::FileType,
@@ -24,7 +25,7 @@ use crate::{
utils::{cleanup_folder, get_lok_filter_from_file_types},
};
-use super::context::ApiContext;
+use super::context::{ApiContext, AuthorizationService};
/// Convert call
#[utoipa::path(
@@ -41,9 +42,10 @@ use super::context::ApiContext;
(status = 500, description = "internal server error", body = ErrorResponse),
)
)]
-#[tracing::instrument(skip(ctx))]
+#[tracing::instrument(skip(ctx, _internal_authorization))]
pub async fn handler(
State(ctx): State<ApiContext>,
+ _internal_authorization: InternalMacroAuthorizationExtractor<AuthorizationService>,
extract::Json(req): extract::Json<ConvertRequest>,
) -> Result<Response, Response> {
let job_id = macro_uuid::generate_uuid_v7().to_string();
diff --git a/services/convert_service/src/api/mod.rs b/services/convert_service/src/api/mod.rs
index 5c0c352a73..952704af3d 100644
--- a/services/convert_service/src/api/mod.rs
+++ b/services/convert_service/src/api/mod.rs
@@ -1,7 +1,6 @@
use anyhow::Context;
use axum::Router;
use context::ApiContext;
-use macro_auth::InternalApiKey;
use tower::ServiceBuilder;
use tower_http::trace::TraceLayer;
use utoipa::OpenApi;
@@ -22,7 +21,7 @@ pub async fn setup_and_serve(state: ApiContext) -> anyhow::Result<()> {
let port = state.config.port;
let env = state.config.environment;
- let app = api_router(state.internal_api_key.clone())
+ let app = api_router()
.with_state(state)
.layer(cors.clone())
.layer(ServiceBuilder::new().layer(TraceLayer::new_for_http()))
@@ -43,21 +42,14 @@ pub async fn setup_and_serve(state: ApiContext) -> anyhow::Result<()> {
.context("error starting service")
}
-fn api_router(internal_api_key: InternalApiKey) -> Router<ApiContext> {
+fn api_router() -> Router<ApiContext> {
Router::new().nest(
"/internal",
Router::new()
.nest("/convert", convert::router())
.nest("/backfill", backfill::router())
- .layer(
- ServiceBuilder::new()
- .layer(axum::middleware::from_fn_with_state(
- internal_api_key,
- macro_middleware::auth::internal_access::handler,
- ))
- .layer(axum::middleware::from_fn(
- macro_middleware::connection_drop_prevention_handler,
- )),
- ),
+ .layer(ServiceBuilder::new().layer(axum::middleware::from_fn(
+ macro_middleware::connection_drop_prevention_handler,
+ ))),
)
}
diff --git a/services/convert_service/src/main.rs b/services/convert_service/src/main.rs
index e7de67bf43..8ac7fc7b29 100644
--- a/services/convert_service/src/main.rs
+++ b/services/convert_service/src/main.rs
@@ -1,7 +1,9 @@
#![recursion_limit = "256"]
-use crate::api::context::ApiContext;
+use crate::api::context::{ApiContext, AuthorizationService};
use anyhow::Context;
use config::{Config, Environment};
+use macro_auth::middleware::decode_jwt::JwtValidationArgs;
+use macro_authorization::{InternalAuthConfig, MacroAuthJwtValidator, MacroAuthorizationState};
use macro_entrypoint::MacroEntrypoint;
use process::runner::run_worker;
use sqlx::postgres::PgPoolOptions;
@@ -65,6 +67,19 @@ async fn main() -> anyhow::Result<()> {
smoke_test_lok(&config.lok_path)?;
let aws_config = macro_aws_config::get_macro_aws_config().await;
+ let secretsmanager_client = secretsmanager_client::SecretsManager::new(
+ aws_sdk_secretsmanager::Client::new(&aws_config),
+ );
+ let jwt_validation_args =
+ JwtValidationArgs::new_with_secret_manager(config.environment, &secretsmanager_client)
+ .await?;
+ let authorization_state = MacroAuthorizationState::new(Arc::new(AuthorizationService::new(
+ MacroAuthJwtValidator::new(jwt_validation_args),
+ InternalAuthConfig {
+ api_key: config.internal_api_key.to_string(),
+ default_user_id: None,
+ },
+ )));
let db = PgPoolOptions::new()
.min_connections(1)
@@ -102,7 +117,7 @@ async fn main() -> anyhow::Result<()> {
api::setup_and_serve(ApiContext {
db,
s3_client,
- internal_api_key: config.internal_api_key.clone(),
+ authorization_state,
sqs_client: Arc::new(sqs_client),
config: Arc::new(config),
})