Skip to content

Commit cfb8f59

Browse files
committed
remove Json deser from get_validation_errors_data method
1 parent 4568609 commit cfb8f59

2 files changed

Lines changed: 98 additions & 135 deletions

File tree

crates/cli/src/sarif/sarif_utils.rs

Lines changed: 96 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use kernel::model::{
1818
};
1919
use path_slash::PathExt;
2020
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
21-
use secrets::model::secret_result::{SecretResult, SecretValidationStatus};
21+
use secrets::model::secret_result::{SecretResult, SecretValidationStatus, ValidationErrorInfo};
2222
use secrets::model::secret_rule::SecretRule;
2323
use serde_sarif::sarif::{
2424
self, Artifact, ArtifactBuilder, ArtifactChangeBuilder, ArtifactLocationBuilder, FixBuilder,
@@ -152,27 +152,11 @@ impl SarifViolation {
152152
}
153153
}
154154

155-
fn get_validation_errors_data(&self) -> Option<serde_json::Value> {
156-
if let Secret(_, validation_status) = &self {
157-
match validation_status {
158-
SecretValidationStatus::ValidationError(error_infos) if !error_infos.is_empty() => {
159-
let errors: Vec<serde_json::Value> = error_infos
160-
.iter()
161-
.map(|error_info| {
162-
serde_json::json!({
163-
"type": error_info.error_type.as_str(),
164-
"statusCode": error_info.status_code,
165-
"message": error_info.message
166-
})
167-
})
168-
.collect();
169-
Some(serde_json::Value::Array(errors))
170-
}
171-
_ => None,
172-
}
173-
} else {
174-
None
175-
}
155+
fn get_validation_errors_data(&self) -> Option<&[ValidationErrorInfo]> {
156+
let Secret(_, SecretValidationStatus::ValidationError(error_infos)) = &self else {
157+
return None;
158+
};
159+
(!error_infos.is_empty()).then_some(error_infos.as_slice())
176160
}
177161
}
178162

@@ -797,9 +781,10 @@ fn generate_results(
797781
let validation_errors_data =
798782
sarif_violation.get_validation_errors_data();
799783
if let Some(errors_data) = validation_errors_data {
784+
let json_array = serde_json::to_value(errors_data)?;
800785
properties.additional_properties.insert(
801786
"datadogSecretValidationErrors".to_string(),
802-
errors_data,
787+
json_array,
803788
);
804789
}
805790

@@ -1630,8 +1615,8 @@ mod tests {
16301615
start: Position { line: 1, col: 1 },
16311616
end: Position { line: 2, col: 2 },
16321617
validation_status: SecretValidationStatus::ValidationError(vec![
1633-
secrets::model::secret_result::ValidationErrorInfo {
1634-
error_type: secrets::model::secret_result::ValidationErrorType::HttpError,
1618+
ValidationErrorInfo {
1619+
error_type: ValidationErrorType::HttpError,
16351620
status_code: 400,
16361621
message: "Invalid token".to_string(),
16371622
},
@@ -1661,39 +1646,31 @@ mod tests {
16611646
)
16621647
.expect("generate sarif report");
16631648

1664-
let expected_subset = serde_json::json!(
1665-
{
1666-
"runs": [
1667-
{
1668-
"results": [
1669-
{
1670-
"properties": {
1671-
"tags": [
1672-
"DATADOG_CATEGORY:SECURITY",
1673-
"DATADOG_SECRET_VALIDATION_STATUS:VALIDATION_ERROR"
1674-
],
1675-
"datadogSecretValidationErrors": [
1676-
{
1677-
"type": "HttpError",
1678-
"statusCode": 400,
1679-
"message": "Invalid token"
1680-
}
1681-
]
1682-
}
1683-
}
1684-
]
1685-
}
1686-
]
1687-
}
1688-
);
1649+
// Test the actual data structures
1650+
let result = &sarif_report.runs[0].results.as_ref().unwrap()[0];
1651+
let properties = result.properties.as_ref().unwrap();
16891652

1690-
let sarif_json = serde_json::to_value(sarif_report).unwrap();
1691-
assert_json_include!(
1692-
actual: sarif_json,
1693-
expected: expected_subset,
1694-
);
1653+
// Check tags
1654+
let tags = properties.tags.as_ref().unwrap();
1655+
assert!(tags.contains(&"DATADOG_CATEGORY:SECURITY".to_string()));
1656+
assert!(tags.contains(&"DATADOG_SECRET_VALIDATION_STATUS:VALIDATION_ERROR".to_string()));
1657+
1658+
// Check validation errors data
1659+
let json_value = properties
1660+
.additional_properties
1661+
.get("datadogSecretValidationErrors")
1662+
.unwrap();
1663+
let parsed: Vec<ValidationErrorInfo> = serde_json::from_value(json_value.clone()).unwrap();
1664+
1665+
let expected_errors = vec![ValidationErrorInfo {
1666+
error_type: ValidationErrorType::HttpError,
1667+
status_code: 400,
1668+
message: "Invalid token".to_string(),
1669+
}];
1670+
assert_eq!(parsed, expected_errors);
16951671

16961672
// validate the schema
1673+
let sarif_json = serde_json::to_value(sarif_report).unwrap();
16971674
assert!(validate_data(&sarif_json));
16981675
}
16991676

@@ -1725,9 +1702,8 @@ mod tests {
17251702
start: Position { line: 1, col: 1 },
17261703
end: Position { line: 2, col: 2 },
17271704
validation_status: SecretValidationStatus::ValidationError(vec![
1728-
secrets::model::secret_result::ValidationErrorInfo {
1729-
error_type:
1730-
secrets::model::secret_result::ValidationErrorType::UnknownResponseType,
1705+
ValidationErrorInfo {
1706+
error_type: ValidationErrorType::UnknownResponseType,
17311707
status_code: 0, // Using 0 as placeholder when no status code is available
17321708
message: "Connection timeout".to_string(),
17331709
},
@@ -1757,39 +1733,31 @@ mod tests {
17571733
)
17581734
.expect("generate sarif report");
17591735

1760-
let expected_subset = serde_json::json!(
1761-
{
1762-
"runs": [
1763-
{
1764-
"results": [
1765-
{
1766-
"properties": {
1767-
"tags": [
1768-
"DATADOG_CATEGORY:SECURITY",
1769-
"DATADOG_SECRET_VALIDATION_STATUS:VALIDATION_ERROR"
1770-
],
1771-
"datadogSecretValidationErrors": [
1772-
{
1773-
"type": "UnknownResponseType",
1774-
"statusCode": 0,
1775-
"message": "Connection timeout"
1776-
}
1777-
]
1778-
}
1779-
}
1780-
]
1781-
}
1782-
]
1783-
}
1784-
);
1736+
// Test the actual data structures
1737+
let result = &sarif_report.runs[0].results.as_ref().unwrap()[0];
1738+
let properties = result.properties.as_ref().unwrap();
17851739

1786-
let sarif_json = serde_json::to_value(sarif_report).unwrap();
1787-
assert_json_include!(
1788-
actual: sarif_json,
1789-
expected: expected_subset,
1790-
);
1740+
// Check tags
1741+
let tags = properties.tags.as_ref().unwrap();
1742+
assert!(tags.contains(&"DATADOG_CATEGORY:SECURITY".to_string()));
1743+
assert!(tags.contains(&"DATADOG_SECRET_VALIDATION_STATUS:VALIDATION_ERROR".to_string()));
1744+
1745+
// Check validation errors data
1746+
let json_value = properties
1747+
.additional_properties
1748+
.get("datadogSecretValidationErrors")
1749+
.unwrap();
1750+
let parsed: Vec<ValidationErrorInfo> = serde_json::from_value(json_value.clone()).unwrap();
1751+
1752+
let expected_errors = vec![ValidationErrorInfo {
1753+
error_type: ValidationErrorType::UnknownResponseType,
1754+
status_code: 0,
1755+
message: "Connection timeout".to_string(),
1756+
}];
1757+
assert_eq!(parsed, expected_errors);
17911758

17921759
// validate the schema
1760+
let sarif_json = serde_json::to_value(sarif_report).unwrap();
17931761
assert!(validate_data(&sarif_json));
17941762
}
17951763

@@ -2161,19 +2129,18 @@ mod tests {
21612129
start: Position { line: 1, col: 1 },
21622130
end: Position { line: 2, col: 2 },
21632131
validation_status: SecretValidationStatus::ValidationError(vec![
2164-
secrets::model::secret_result::ValidationErrorInfo {
2165-
error_type: secrets::model::secret_result::ValidationErrorType::HttpError,
2132+
ValidationErrorInfo {
2133+
error_type: ValidationErrorType::HttpError,
21662134
status_code: 400,
21672135
message: "Invalid token".to_string(),
21682136
},
2169-
secrets::model::secret_result::ValidationErrorInfo {
2170-
error_type: secrets::model::secret_result::ValidationErrorType::HttpError,
2137+
ValidationErrorInfo {
2138+
error_type: ValidationErrorType::HttpError,
21712139
status_code: 401,
21722140
message: "Unauthorized access".to_string(),
21732141
},
2174-
secrets::model::secret_result::ValidationErrorInfo {
2175-
error_type:
2176-
secrets::model::secret_result::ValidationErrorType::UnknownResponseType,
2142+
ValidationErrorInfo {
2143+
error_type: ValidationErrorType::UnknownResponseType,
21772144
status_code: 0, // Test error without status code
21782145
message: "Connection timeout".to_string(),
21792146
},
@@ -2203,49 +2170,43 @@ mod tests {
22032170
)
22042171
.expect("generate sarif report");
22052172

2206-
let expected_subset = serde_json::json!(
2207-
{
2208-
"runs": [
2209-
{
2210-
"results": [
2211-
{
2212-
"properties": {
2213-
"tags": [
2214-
"DATADOG_CATEGORY:SECURITY",
2215-
"DATADOG_SECRET_VALIDATION_STATUS:VALIDATION_ERROR"
2216-
],
2217-
"datadogSecretValidationErrors": [
2218-
{
2219-
"type": "HttpError",
2220-
"statusCode": 400,
2221-
"message": "Invalid token"
2222-
},
2223-
{
2224-
"type": "HttpError",
2225-
"statusCode": 401,
2226-
"message": "Unauthorized access"
2227-
},
2228-
{
2229-
"type": "UnknownResponseType",
2230-
"statusCode": 0,
2231-
"message": "Connection timeout"
2232-
}
2233-
]
2234-
}
2235-
}
2236-
]
2237-
}
2238-
]
2239-
}
2240-
);
2173+
// Test the actual data structures
2174+
let result = &sarif_report.runs[0].results.as_ref().unwrap()[0];
2175+
let properties = result.properties.as_ref().unwrap();
22412176

2242-
let sarif_json = serde_json::to_value(sarif_report).unwrap();
2243-
assert_json_include!(
2244-
actual: sarif_json,
2245-
expected: expected_subset,
2246-
);
2177+
// Check tags
2178+
let tags = properties.tags.as_ref().unwrap();
2179+
assert!(tags.contains(&"DATADOG_CATEGORY:SECURITY".to_string()));
2180+
assert!(tags.contains(&"DATADOG_SECRET_VALIDATION_STATUS:VALIDATION_ERROR".to_string()));
2181+
2182+
// Check validation errors data
2183+
let json_value = properties
2184+
.additional_properties
2185+
.get("datadogSecretValidationErrors")
2186+
.unwrap();
2187+
let parsed: Vec<ValidationErrorInfo> = serde_json::from_value(json_value.clone()).unwrap();
2188+
2189+
let expected_errors = vec![
2190+
ValidationErrorInfo {
2191+
error_type: ValidationErrorType::HttpError,
2192+
status_code: 400,
2193+
message: "Invalid token".to_string(),
2194+
},
2195+
ValidationErrorInfo {
2196+
error_type: ValidationErrorType::HttpError,
2197+
status_code: 401,
2198+
message: "Unauthorized access".to_string(),
2199+
},
2200+
ValidationErrorInfo {
2201+
error_type: ValidationErrorType::UnknownResponseType,
2202+
status_code: 0,
2203+
message: "Connection timeout".to_string(),
2204+
},
2205+
];
2206+
assert_eq!(parsed, expected_errors);
22472207

22482208
// validate the schema
2209+
let sarif_json = serde_json::to_value(sarif_report).unwrap();
22492210
assert!(validate_data(&sarif_json));
22502211
}
22512212
}

crates/secrets/src/model/secret_result.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ pub struct SecretResult {
7373
}
7474

7575
#[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Clone, Hash, Serialize, Deserialize)]
76+
#[serde(rename_all = "camelCase")]
7677
pub struct ValidationErrorInfo {
78+
#[serde(rename = "type")]
7779
pub error_type: ValidationErrorType,
7880
pub status_code: u16,
7981
pub message: String,

0 commit comments

Comments
 (0)