Skip to content

Commit f95139e

Browse files
committed
Temporary: artificially disable v2 config parsing in all binaries (but not within unit tests)
1 parent e495f67 commit f95139e

4 files changed

Lines changed: 55 additions & 7 deletions

File tree

crates/bins/src/bin/datadog_static_analyzer_server/ide/configuration_file/static_analysis_config_file.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use super::error::ConfigFileError;
33
use indexmap::IndexMap;
44
use itertools::Itertools;
55
use kernel::config::common::{
6-
parse_any_schema_yaml, ConfigError, PathConfig, PathPattern, RuleConfig, RulesetConfig,
7-
WithVersion,
6+
parse_any_schema_yaml, parse_only_v1_yaml, ConfigError, PathConfig, PathPattern, RuleConfig,
7+
RulesetConfig, WithVersion,
88
};
99
use kernel::config::file_v1::config_file_to_yaml;
1010
use kernel::config::{file_v1, file_v2};
@@ -47,7 +47,12 @@ impl TryFrom<String> for StaticAnalysisConfigFile {
4747
if content.trim().is_empty() {
4848
return Ok(Self::default());
4949
}
50-
let parsed = parse_any_schema_yaml(&content).map_err(|err| {
50+
let parsed = if cfg!(test) {
51+
parse_any_schema_yaml(&content)
52+
} else {
53+
parse_only_v1_yaml(&content)
54+
};
55+
let parsed = parsed.map_err(|err| {
5156
match err {
5257
// Artificially represent this as a "parse" error for backwards compatibility.
5358
ConfigError::UnsupportedSchema(_) => ConfigFileError::Parser {

crates/cli/src/config_file.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use crate::datadog_utils::{
55
};
66
use crate::git_utils::get_repository_url;
77
use anyhow::{anyhow, Context};
8-
use kernel::config::common::{parse_any_schema_yaml, ConfigMethod, WithVersion};
8+
use kernel::config::common::{
9+
parse_any_schema_yaml, parse_only_v1_yaml, ConfigMethod, WithVersion,
10+
};
911
use kernel::config::file_v2;
1012
use kernel::utils::{decode_base64_string, encode_base64_string};
1113
use std::path::Path;
@@ -51,7 +53,13 @@ pub fn get_config(
5153
let local_file_contents = read_config_file(path)?;
5254
let local_yaml = local_file_contents
5355
.as_ref()
54-
.map(|c| parse_any_schema_yaml(c))
56+
.map(|c| {
57+
if cfg!(test) {
58+
parse_any_schema_yaml(c)
59+
} else {
60+
parse_only_v1_yaml(c)
61+
}
62+
})
5563
.transpose()?;
5664
let local_config: Option<file_v2::ConfigFile> = local_yaml.map(|v| match v {
5765
WithVersion::V1(v1) => file_v2::YamlConfigFile::from(v1).into(),

crates/static-analysis-kernel/src/config/common.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,19 @@ pub fn parse_any_schema_yaml(
314314
}
315315
}
316316

317+
/// Like [`parse_any_schema_yaml`], except only ever returns a [`WithVersion::V1`].
318+
///
319+
/// # Note
320+
/// This is a temporary function used to artificially disable v2 support within the static analyzer.
321+
/// When the Datadog backend implements v2 support, this will be removed.
322+
pub fn parse_only_v1_yaml(
323+
config_contents: &str,
324+
) -> Result<WithVersion<file_v1::YamlConfigFile, file_v2::YamlConfigFile>, ConfigError> {
325+
file_v1::parse_yaml(config_contents)
326+
.map(WithVersion::V1)
327+
.map_err(ConfigError::Parse)
328+
}
329+
317330
#[cfg(test)]
318331
mod tests {
319332
use super::*;
@@ -391,4 +404,21 @@ schema-version: v2
391404
));
392405
}
393406
}
407+
408+
/// See [`parse_only_v1_yaml`] for documentation.
409+
#[test]
410+
fn parse_v1_yaml_v1_only() {
411+
// language=yaml
412+
let v1 = "\
413+
schema-version: v1
414+
rulesets:
415+
- java-security
416+
";
417+
// language=yaml
418+
let v2 = "\
419+
schema-version: v2
420+
";
421+
assert!(matches!(parse_only_v1_yaml(v2), Err(ConfigError::Parse(_))));
422+
assert!(matches!(parse_only_v1_yaml(v1), Ok(WithVersion::V1(_))));
423+
}
394424
}

crates/static-analysis-server/src/request.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::model::violation::ServerViolation;
88
use common::analysis_options::AnalysisOptions;
99
use kernel::analysis::analyze::analyze_with;
1010
use kernel::analysis::ddsa_lib::JsRuntime;
11-
use kernel::config::common::{parse_any_schema_yaml, WithVersion};
11+
use kernel::config::common::{parse_any_schema_yaml, parse_only_v1_yaml, WithVersion};
1212
use kernel::config::file_v2;
1313
use kernel::model::rule::RuleInternal;
1414
use kernel::rule_config::RuleConfigProvider;
@@ -29,7 +29,12 @@ pub fn process_analysis_request<T: Borrow<RuleInternal>>(
2929
let configuration = if let Some(config_b64) = request.configuration_base64 {
3030
let config =
3131
decode_base64_string(config_b64).map_err(|_| ERROR_CONFIGURATION_NOT_BASE64)?;
32-
let v2_yaml = parse_any_schema_yaml(&config)
32+
let parsed = if cfg!(test) {
33+
parse_any_schema_yaml(&config)
34+
} else {
35+
parse_only_v1_yaml(&config)
36+
};
37+
let v2_yaml = parsed
3338
.map(|v| match v {
3439
WithVersion::V1(yaml) => file_v2::YamlConfigFile::from(yaml),
3540
WithVersion::V2(yaml) => yaml,

0 commit comments

Comments
 (0)