Skip to content

Commit

Permalink
fix auth.config.ts for components (#28479)
Browse files Browse the repository at this point in the history
add auth.config.ts to modules in the `/deploy2` apis in the same way it's included to the `/push_config` api.

this fixes auth with components, because the CLI was never sending up the auth config bundle at all, and with this PR it does.

I would like to do the same pattern for schema.ts/schema.js but that can come later.

GitOrigin-RevId: a4875348bfa7903b64ee68bb43b956adae593d74
  • Loading branch information
ldanilek authored and Convex, Inc. committed Jul 30, 2024
1 parent 81331cc commit 37d0c13
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 80 deletions.
2 changes: 0 additions & 2 deletions crates/application/src/deploy_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ impl From<NodeDependencyJson> for NodeDependency {
pub struct AppDefinitionConfigJson {
pub definition: Option<ModuleJson>,
pub dependencies: Vec<String>,
pub auth: Option<ModuleJson>,
pub schema: Option<ModuleJson>,
pub functions: Vec<ModuleJson>,
pub udf_server_version: String,
Expand All @@ -122,7 +121,6 @@ impl TryFrom<AppDefinitionConfigJson> for AppDefinitionConfig {
.into_iter()
.map(|s| s.parse())
.collect::<anyhow::Result<_>>()?,
auth: value.auth.map(TryInto::try_into).transpose()?,
schema: value.schema.map(TryInto::try_into).transpose()?,
functions: value
.functions
Expand Down
50 changes: 40 additions & 10 deletions crates/application/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1865,20 +1865,19 @@ impl<RT: Runtime> Application<RT> {
let environment_variables = EnvironmentVariablesModel::new(&mut tx).get_all().await?;
tx.into_token()?;
// TODO(ENG-6500): Fold in our reads here into the hash.
let mut app_analysis = self
.analyze_modules(
let (auth_module, app_analysis) = self
.analyze_modules_with_auth_config(
app_udf_config.clone(),
config.app_definition.functions.clone(),
app_pkg.clone(),
environment_variables.clone(),
)
.await?;

// Evaluate auth and add in an empty `auth.config.js` to the analysis.
let auth_info = Application::get_evaluated_auth_config(
self.runner(),
environment_variables.clone(),
config.app_definition.auth.clone(),
auth_module,
&ConfigFile {
functions: config.config.functions.clone(),
auth_info: if config.config.auth_info.is_empty() {
Expand All @@ -1889,12 +1888,6 @@ impl<RT: Runtime> Application<RT> {
},
)
.await?;
if let Some(auth_module) = &config.app_definition.auth {
app_analysis.insert(
auth_module.path.clone().canonicalize(),
AnalyzedModule::default(),
);
}

let evaluated_components = self
.evaluate_components(
Expand Down Expand Up @@ -1935,6 +1928,43 @@ impl<RT: Runtime> Application<RT> {
Ok(resp)
}

#[minitrace::trace]
pub async fn analyze_modules_with_auth_config(
&self,
udf_config: UdfConfig,
modules: Vec<ModuleConfig>,
source_package: SourcePackage,
environment_variables: BTreeMap<EnvVarName, EnvVarValue>,
) -> anyhow::Result<(
Option<ModuleConfig>,
BTreeMap<CanonicalizedModulePath, AnalyzedModule>,
)> {
// Don't analyze the auth config module
let (auth_modules, analyzed_modules): (Vec<_>, Vec<_>) =
modules.into_iter().partition(|module| {
module.path.clone().canonicalize() == AUTH_CONFIG_FILE_NAME.parse().unwrap()
});
let auth_module = auth_modules.first();

let mut analyze_result = self
.analyze_modules(
udf_config,
analyzed_modules,
source_package,
environment_variables,
)
.await?;

// Add an empty analyzed result for the auth config module
if let Some(auth_module) = auth_module {
analyze_result.insert(
auth_module.path.clone().canonicalize(),
AnalyzedModule::default(),
);
}
Ok((auth_module.cloned(), analyze_result))
}

async fn upload_packages(
&self,
config: &ProjectConfig,
Expand Down
67 changes: 14 additions & 53 deletions crates/local_backend/src/deploy_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,12 @@ use model::{
ConfigFile,
ConfigMetadata,
ModuleConfig,
AUTH_CONFIG_FILE_NAME,
},
ConfigModel,
},
environment_variables::EnvironmentVariablesModel,
modules::module_versions::AnalyzedModule,
source_packages::types::{
PackageSize,
SourcePackage,
},
source_packages::types::PackageSize,
udf_config::types::UdfConfig,
};
use rand::Rng;
Expand Down Expand Up @@ -347,14 +343,20 @@ pub async fn push_config_handler(
import_phase_unix_timestamp: application.runtime().unix_timestamp(),
};
let begin_analyze = Instant::now();
// Note: This is not transactional with the rest of the deploy to avoid keeping
// a transaction open for a long time.
let mut tx = application.begin(Identity::system()).await?;
let environment_variables = EnvironmentVariablesModel::new(&mut tx).get_all().await?;
drop(tx);
// Run analyze to make sure the new modules are valid.
let (auth_module, analyze_results) = analyze_modules_with_auth_config(
application,
udf_config.clone(),
modules.clone(),
source_package.clone(),
)
.await?;
let (auth_module, analyze_results) = application
.analyze_modules_with_auth_config(
udf_config.clone(),
modules.clone(),
source_package.clone(),
environment_variables,
)
.await?;
let end_analyze = Instant::now();
let (
ConfigMetadataAndSchema {
Expand Down Expand Up @@ -394,44 +396,3 @@ pub async fn push_config_handler(
},
))
}

#[minitrace::trace]
pub async fn analyze_modules_with_auth_config(
application: &Application<ProdRuntime>,
udf_config: UdfConfig,
modules: Vec<ModuleConfig>,
source_package: SourcePackage,
) -> anyhow::Result<(
Option<ModuleConfig>,
BTreeMap<CanonicalizedModulePath, AnalyzedModule>,
)> {
// Don't analyze the auth config module
let (auth_modules, analyzed_modules): (Vec<_>, Vec<_>) =
modules.into_iter().partition(|module| {
module.path.clone().canonicalize() == AUTH_CONFIG_FILE_NAME.parse().unwrap()
});
let auth_module = auth_modules.first();

// Note: This is not transactional with the rest of the deploy to avoid keeping
// a transaction open for a long time.
let mut tx = application.begin(Identity::system()).await?;
let environment_variables = EnvironmentVariablesModel::new(&mut tx).get_all().await?;
drop(tx);
let mut analyze_result = application
.analyze_modules(
udf_config,
analyzed_modules,
source_package,
environment_variables,
)
.await?;

// Add an empty analyzed result for the auth config module
if let Some(auth_module) = auth_module {
analyze_result.insert(
auth_module.path.clone().canonicalize(),
AnalyzedModule::default(),
);
}
Ok((auth_module.cloned(), analyze_result))
}
5 changes: 1 addition & 4 deletions crates/model/src/components/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ pub struct AppDefinitionConfig {
// Dependencies on other components discovered at bundling time.
pub dependencies: BTreeSet<ComponentDefinitionPath>,

// Optional auth.config.js. Not available at runtime.
pub auth: Option<ModuleConfig>,

// Optional schema.js. Not available at runtime.
pub schema: Option<ModuleConfig>,

Expand All @@ -63,6 +60,7 @@ pub struct AppDefinitionConfig {
// - http.js
// - crons.js
// - Bundler dependency chunks within _deps.
// Also includes auth.config.js which is empty at runtime.
pub functions: Vec<ModuleConfig>,

pub udf_server_version: Version,
Expand All @@ -72,7 +70,6 @@ impl AppDefinitionConfig {
pub fn modules(&self) -> impl Iterator<Item = &ModuleConfig> {
self.definition
.iter()
.chain(self.auth.iter())
.chain(self.schema.iter())
.chain(&self.functions)
}
Expand Down
13 changes: 6 additions & 7 deletions npm-packages/convex/src/cli/lib/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,13 @@ async function startComponentsPushAndCodegen(
projectConfig.node.externalPackages,
verbose,
);
const { config: localConfig } = await configFromProjectConfig(
ctx,
projectConfig,
configPath,
verbose,
);
if (options.debugBundlePath) {
const { config: localConfig } = await configFromProjectConfig(
ctx,
projectConfig,
configPath,
verbose,
);
// TODO(ENG-6972): Actually write the bundles for components.
await handleDebugBundlePath(ctx, options.debugBundlePath, localConfig);
logMessage(
Expand All @@ -200,7 +200,6 @@ async function startComponentsPushAndCodegen(

const appDefinition: AppDefinitionConfig = {
...appDefinitionSpecWithoutImpls,
auth: localConfig.authConfig || null,
...appImplementation,
udfServerVersion,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import {
Bundle,
bundle,
bundleAuthConfig,
bundleSchema,
entryPointsByEnvironment,
} from "../../../../bundler/index.js";
Expand Down Expand Up @@ -509,9 +510,10 @@ export async function bundleImplementations(
version: moduleVersion,
});
}
const authBundle = await bundleAuthConfig(ctx, resolvedPath);
appImplementation = {
schema,
functions: functions.concat(nodeResult.modules),
functions: functions.concat(nodeResult.modules).concat(authBundle),
externalNodeDependencies,
};
} else {
Expand Down
2 changes: 0 additions & 2 deletions npm-packages/convex/src/cli/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ export interface Config {
nodeDependencies: NodeDependency[];
schemaId?: string;
udfServerVersion?: string;
authConfig?: Bundle;
}

export interface ConfigWithModuleHashes {
Expand All @@ -80,7 +79,6 @@ export interface ConfigWithModuleHashes {
nodeDependencies: NodeDependency[];
schemaId?: string;
udfServerVersion?: string;
authConfig?: Bundle;
}

const DEFAULT_FUNCTIONS_PATH = "convex/";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { moduleConfig } from "./modules.js";
export const appDefinitionConfig = z.object({
definition: z.nullable(moduleConfig),
dependencies: z.array(componentDefinitionPath),
auth: z.nullable(moduleConfig),
schema: z.nullable(moduleConfig),
functions: z.array(moduleConfig),
udfServerVersion: z.string(),
Expand Down

0 comments on commit 37d0c13

Please sign in to comment.