Skip to content
120 changes: 103 additions & 17 deletions cli/src/services/app_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,26 +83,35 @@ pub(crate) fn classify_observability_configuration_error(error: &anyhow::Error)
pub(crate) fn invalid_discovered_config_guidance(
observability_config: &services::config::ResolvedObservabilityRuntimeConfig,
) -> Option<String> {
if observability_config.validation_errors.is_empty() {
discovered_config_guidance(
&observability_config.loaded_config_paths,
&observability_config.validation_errors,
)
}

fn discovered_config_guidance(
loaded_config_paths: &[services::config::LoadedConfigPath],
validation_errors: &[String],
) -> Option<String> {
if validation_errors.is_empty() {
return None;
}

let has_invalid_local_config =
observability_config
.loaded_config_paths
.iter()
.any(|loaded_path| {
loaded_path.source == services::config::ConfigPathSource::DefaultDiscoveredLocal
&& observability_config
.validation_errors
.iter()
.any(|error| error.contains(loaded_path.path.to_string_lossy().as_ref()))
});

Some(if has_invalid_local_config {
"Local `.sce` config is invalid. Fix `.sce` and run `sce config validate`.".to_string()
} else {
"A discovered config file is invalid. Fix it and run `sce config validate`.".to_string()
let has_invalid_local_config = loaded_config_paths.iter().any(|loaded_path| {
loaded_path.source == services::config::ConfigPathSource::DefaultDiscoveredLocal
&& validation_errors
.iter()
.any(|error| error.contains(loaded_path.path.to_string_lossy().as_ref()))
});
let only_older_schema_series = validation_errors
.iter()
.all(|error| services::config::schema_declaration::is_older_series_only_failure(error));

Some(match (has_invalid_local_config, only_older_schema_series) {
(true, true) => "Local `.sce` config declares an older config schema series. Run `sce setup` to upgrade it.".to_string(),
(false, true) => "A discovered config file declares an older config schema series. Run `sce setup` to upgrade it.".to_string(),
(true, false) => "Local `.sce` config is invalid. Fix `.sce` and run `sce config validate`.".to_string(),
(false, false) => "A discovered config file is invalid. Fix it and run `sce config validate`.".to_string(),
})
}

Expand Down Expand Up @@ -226,3 +235,80 @@ fn write_error_diagnostic_with_color_policy<W: Write>(
fn write_startup_diagnostic<W: Write>(writer: &mut W, diagnostic: &str) {
let _ = writeln!(writer, "{}", services::style::error_code(diagnostic));
}

#[cfg(test)]
mod discovered_config_guidance_tests {
use std::path::{Path, PathBuf};

use serde_json::json;

use super::discovered_config_guidance;
use crate::services::config::schema::parse_file_config;
use crate::services::config::{ConfigPathSource, LoadedConfigPath};

const LOCAL_CONFIG_PATH: &str = "/repo/.sce/config.json";

fn local_loaded_path() -> LoadedConfigPath {
LoadedConfigPath {
path: PathBuf::from(LOCAL_CONFIG_PATH),
source: ConfigPathSource::DefaultDiscoveredLocal,
}
}

fn validation_error(value: &serde_json::Value) -> String {
parse_file_config(
&value.to_string(),
Path::new(LOCAL_CONFIG_PATH),
ConfigPathSource::DefaultDiscoveredLocal,
)
.unwrap_err()
.to_string()
}

#[test]
fn older_schema_series_only_guidance_mentions_setup() {
let error = validation_error(&json!({
"$schema": "https://sce.crocoder.dev/v0.0.1/config.json",
}));

let guidance = discovered_config_guidance(&[local_loaded_path()], &[error]).unwrap();

assert!(guidance.contains("sce setup"), "{guidance}");
assert!(guidance.starts_with("Local `.sce` config"), "{guidance}");
}

#[test]
fn older_schema_series_with_other_errors_keeps_generic_guidance() {
let error = validation_error(&json!({
"$schema": "https://sce.crocoder.dev/v0.0.1/config.json",
"log_level": "loud",
}));

let guidance = discovered_config_guidance(&[local_loaded_path()], &[error]).unwrap();

assert_eq!(
guidance,
"Local `.sce` config is invalid. Fix `.sce` and run `sce config validate`."
);
}

#[test]
fn foreign_schema_declaration_keeps_generic_guidance() {
let error = validation_error(&json!({ "$schema": "https://example.com/s.json" }));

let guidance = discovered_config_guidance(&[], &[error]).unwrap();

assert_eq!(
guidance,
"A discovered config file is invalid. Fix it and run `sce config validate`."
);
}

#[test]
fn no_validation_errors_yields_no_guidance() {
assert_eq!(
discovered_config_guidance(&[local_loaded_path()], &[]),
None
);
}
}
1 change: 1 addition & 0 deletions cli/src/services/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod lifecycle;
pub mod policy;
pub mod resolver;
pub mod schema;
pub(crate) mod schema_declaration;
pub mod types;

mod render;
Expand Down
2 changes: 1 addition & 1 deletion cli/src/services/config/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ pub(crate) fn resolve_agent_trace_storage_runtime_config(
)
}

fn resolve_agent_trace_storage_runtime_config_with<FEnv, FRead, FGlobalPath>(
pub(crate) fn resolve_agent_trace_storage_runtime_config_with<FEnv, FRead, FGlobalPath>(
cwd: &Path,
env_lookup: FEnv,
read_file: FRead,
Expand Down
169 changes: 150 additions & 19 deletions cli/src/services/config/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ use serde::Deserialize;
use serde_json::Value;

use super::policy::{parse_bash_policy_presets, parse_custom_bash_policies, CustomBashPolicyEntry};
use super::schema_declaration::{
classify_schema_declaration, current_schema_series, current_schema_version,
schema_declaration_issue, SCHEMA_DECLARATION_POINTER,
};
use super::types::{
parse_optional_workflow_id, AgentTraceDbRetryConfig, AuthMode, ConfigPathSource,
DatabaseRetryConfig, IntegrationTargetId, IntegrationsConfig, LogFormat, LogLevel,
Expand Down Expand Up @@ -234,17 +238,33 @@ pub(crate) type ParsedFilePolicies = (
);

pub(crate) fn validate_config_value_against_schema(value: &Value, path: &Path) -> Result<()> {
let mut errors = config_schema_validator()
.iter_errors(value)
.map(|error| {
let location = error.instance_path().to_string();
if location.is_empty() {
error.to_string()
} else {
format!("{location}: {error}")
let current_version = current_schema_version();
let declaration_issue = schema_declaration_issue(
&classify_schema_declaration(
value.get(CONFIG_SCHEMA_DECLARATION_KEY),
current_schema_series(),
),
&current_version,
);
let mut declaration_reported = false;
let mut errors = Vec::new();
for error in config_schema_validator().iter_errors(value) {
let location = error.instance_path().to_string();
if location.is_empty() {
errors.push(error.to_string());
continue;
}
if location == SCHEMA_DECLARATION_POINTER {
if let Some(issue) = &declaration_issue {
if !declaration_reported {
errors.push(issue.clone());
declaration_reported = true;
}
continue;
}
})
.collect::<Vec<_>>();
}
errors.push(format!("{location}: {error}"));
}

if errors.is_empty() {
return Ok(());
Expand Down Expand Up @@ -970,15 +990,6 @@ mod agent_trace_config_tests {
)
}

#[test]
fn accepts_versioned_schema_declaration() {
let schema_url = format!(
"https://sce.crocoder.dev/v{}/config.json",
env!("CARGO_PKG_VERSION")
);
parse(&format!(r#"{{"$schema":"{schema_url}"}}"#)).unwrap();
}

#[test]
fn parses_agent_trace_repository_identity_keys() {
let config = parse(
Expand Down Expand Up @@ -1114,6 +1125,126 @@ mod agent_trace_config_tests {
}
}

#[cfg(test)]
mod schema_declaration_acceptance_tests {
use std::path::Path;

use serde_json::{json, Value};

use super::{parse_file_config, ConfigPathSource};
use crate::services::version::PACKAGE_VERSION;

fn current_series() -> (u64, u64) {
let mut parts = PACKAGE_VERSION.split('.');
let major = parts.next().unwrap().parse().unwrap();
let minor = parts.next().unwrap().parse().unwrap();
(major, minor)
}

fn schema_url(version: &str) -> String {
format!("https://sce.crocoder.dev/v{version}/config.json")
}

fn parse_value(value: &Value) -> anyhow::Result<super::FileConfig> {
parse_file_config(
&value.to_string(),
Path::new("/tmp/sce-config.json"),
ConfigPathSource::Flag,
)
}

fn declaration_accepted(declaration: &Value) -> bool {
parse_value(&json!({ "$schema": declaration })).is_ok()
}

#[test]
fn schema_declaration_accepts_current_series_releases() {
let (major, minor) = current_series();
let accepted = [
PACKAGE_VERSION.to_string(),
format!("{major}.{minor}.0"),
format!("{major}.{minor}.7"),
format!("{major}.{minor}.0-pre-alpha-v1"),
format!("{major}.{minor}.0-pre-alpha-v14"),
format!("{major}.{minor}.7+build.5"),
format!("{major}.{minor}.12-rc.1+build.5"),
];

for version in accepted {
let url = schema_url(&version);
assert!(declaration_accepted(&json!(url)), "expected {url} accepted");
}
}

#[test]
fn schema_declaration_rejects_other_series() {
let (major, minor) = current_series();
let mut rejected = vec![
format!("{major}.{}.0", minor + 1),
format!("{}.{minor}.0", major + 1),
format!("{}.0.0", major + 1),
];
if minor > 0 {
rejected.push(format!("{major}.{}.3", minor - 1));
}
if major > 0 {
rejected.push(format!("{}.{minor}.0", major - 1));
}

for version in rejected {
let url = schema_url(&version);
assert!(
!declaration_accepted(&json!(url)),
"expected {url} rejected"
);
}
}

#[test]
fn schema_declaration_rejects_malformed_and_foreign_values() {
let (major, minor) = current_series();
let rejected = [
json!("https://example.com/s.json"),
json!("https://sce.crocoder.dev/config.json"),
json!(schema_url(&format!("{major}.{minor}"))),
json!(schema_url(&format!("{major}.{minor}.01"))),
json!(schema_url(&format!("{major}.{minor}.x"))),
json!(format!(
"http://sce.crocoder.dev/v{major}.{minor}.0/config.json"
)),
json!(format!(
"https://sce.crocoder.dev/v{major}.{minor}.0/config.json.bak"
)),
json!(format!(
"https://sce.crocoder.dev/v{major}x{minor}.0/config.json"
)),
json!(format!(
" https://sce.crocoder.dev/v{major}.{minor}.0/config.json"
)),
json!(1),
json!(null),
json!({ "url": schema_url(PACKAGE_VERSION) }),
];

for value in rejected {
assert!(!declaration_accepted(&value), "expected {value} rejected");
}
}

#[test]
fn schema_declaration_valid_still_validates_other_fields() {
let error = parse_value(&json!({
"$schema": schema_url(PACKAGE_VERSION),
"log_level": "loud",
}))
.unwrap_err()
.to_string();

assert!(error.contains("log_level"), "unexpected error: {error}");
assert!(!error.contains("$schema"), "unexpected error: {error}");
}
}

#[cfg(test)]
mod database_retry_config_tests {
use std::path::Path;
Expand Down
Loading
Loading