Skip to content

Add endpoint to retrieve Security Monitoring rule version history #480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions .generator/schemas/v2/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13006,6 +13006,30 @@ components:
$ref: '#/components/schemas/GetInterfacesData'
type: array
type: object
GetRuleVersionHistoryData:
description: Data for the rule version history.
properties:
attributes:
$ref: '#/components/schemas/RuleVersionHistory'
id:
description: ID of the rule.
type: string
type:
$ref: '#/components/schemas/GetRuleVersionHistoryDataType'
type: object
GetRuleVersionHistoryDataType:
description: Type of data.
enum:
- GetRuleVersionHistoryResponse
type: string
x-enum-varnames:
- GETRULEVERSIONHISTORYRESPONSE
GetRuleVersionHistoryResponse:
description: Response for getting the rule version history.
properties:
data:
$ref: '#/components/schemas/GetRuleVersionHistoryData'
type: object
GetSBOMResponse:
description: The expected response schema when getting an SBOM.
properties:
Expand Down Expand Up @@ -23825,6 +23849,57 @@ components:
example: John Doe
type: string
type: object
RuleVersionHistory:
description: Response object containing the version history of a rule.
properties:
count:
description: The number of rule versions.
format: int32
maximum: 2147483647
type: integer
data:
additionalProperties:
$ref: '#/components/schemas/RuleVersions'
description: A rule version with a list of updates.
description: The `RuleVersionHistory` `data`.
type: object
type: object
RuleVersionUpdate:
description: A change in a rule version.
properties:
change:
description: The new value of the field.
example: cloud_provider:aws
type: string
field:
description: The field that was changed.
example: Tags
type: string
type:
$ref: '#/components/schemas/RuleVersionUpdateType'
type: object
RuleVersionUpdateType:
description: The type of change.
enum:
- create
- update
- delete
type: string
x-enum-varnames:
- CREATE
- UPDATE
- DELETE
RuleVersions:
description: A rule version with a list of updates.
properties:
changes:
description: A list of changes.
items:
$ref: '#/components/schemas/RuleVersionUpdate'
type: array
rule:
$ref: '#/components/schemas/SecurityMonitoringRuleResponse'
type: object
RumMetricCompute:
description: The compute rule to compute the rum-based metric.
properties:
Expand Down Expand Up @@ -46712,6 +46787,42 @@ paths:
operator: OR
permissions:
- security_monitoring_rules_write
/api/v2/security_monitoring/rules/{rule_id}/version_history:
get:
description: Get a rule's version history.
operationId: GetRuleVersionHistory
parameters:
- $ref: '#/components/parameters/SecurityMonitoringRuleID'
- $ref: '#/components/parameters/PageSize'
- $ref: '#/components/parameters/PageNumber'
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/GetRuleVersionHistoryResponse'
description: OK
'400':
$ref: '#/components/responses/BadRequestResponse'
'403':
$ref: '#/components/responses/NotAuthorizedResponse'
'404':
$ref: '#/components/responses/NotFoundResponse'
'429':
$ref: '#/components/responses/TooManyRequestsResponse'
security:
- apiKeyAuth: []
appKeyAuth: []
- AuthZ:
- security_monitoring_rules_read
summary: Get a rule's version history
tags:
- Security Monitoring
x-permission:
operator: OR
permissions:
- security_monitoring_rules_read
x-unstable: '**Note**: This endpoint is in beta and may be subject to changes.'
/api/v2/security_monitoring/signals:
get:
description: 'The list endpoint returns security signals that match a search
Expand Down
22 changes: 22 additions & 0 deletions examples/v2_security-monitoring_GetRuleVersionHistory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Get a rule's version history returns "OK" response
use datadog_api_client::datadog;
use datadog_api_client::datadogV2::api_security_monitoring::GetRuleVersionHistoryOptionalParams;
use datadog_api_client::datadogV2::api_security_monitoring::SecurityMonitoringAPI;

#[tokio::main]
async fn main() {
let mut configuration = datadog::Configuration::new();
configuration.set_unstable_operation_enabled("v2.GetRuleVersionHistory", true);
let api = SecurityMonitoringAPI::with_config(configuration);
let resp = api
.get_rule_version_history(
"rule_id".to_string(),
GetRuleVersionHistoryOptionalParams::default(),
)
.await;
if let Ok(value) = resp {
println!("{:#?}", value);
} else {
println!("{:#?}", resp.unwrap_err());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Get rule version history returns "OK" response
use datadog_api_client::datadog;
use datadog_api_client::datadogV2::api_security_monitoring::GetRuleVersionHistoryOptionalParams;
use datadog_api_client::datadogV2::api_security_monitoring::SecurityMonitoringAPI;

#[tokio::main]
async fn main() {
// there is a valid "security_rule" in the system
let security_rule_id = std::env::var("SECURITY_RULE_ID").unwrap();
let mut configuration = datadog::Configuration::new();
configuration.set_unstable_operation_enabled("v2.GetRuleVersionHistory", true);
let api = SecurityMonitoringAPI::with_config(configuration);
let resp = api
.get_rule_version_history(
security_rule_id.clone(),
GetRuleVersionHistoryOptionalParams::default(),
)
.await;
if let Ok(value) = resp {
println!("{:#?}", value);
} else {
println!("{:#?}", resp.unwrap_err());
}
}
1 change: 1 addition & 0 deletions src/datadog/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ impl Default for Configuration {
("v2.delete_historical_job".to_owned(), false),
("v2.get_finding".to_owned(), false),
("v2.get_historical_job".to_owned(), false),
("v2.get_rule_version_history".to_owned(), false),
("v2.list_findings".to_owned(), false),
("v2.list_historical_jobs".to_owned(), false),
("v2.mute_findings".to_owned(), false),
Expand Down
166 changes: 166 additions & 0 deletions src/datadogV2/api/api_security_monitoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,29 @@ impl GetFindingOptionalParams {
}
}

/// GetRuleVersionHistoryOptionalParams is a struct for passing parameters to the method [`SecurityMonitoringAPI::get_rule_version_history`]
#[non_exhaustive]
#[derive(Clone, Default, Debug)]
pub struct GetRuleVersionHistoryOptionalParams {
/// Size for a given page. The maximum allowed value is 100.
pub page_size: Option<i64>,
/// Specific page number to return.
pub page_number: Option<i64>,
}

impl GetRuleVersionHistoryOptionalParams {
/// Size for a given page. The maximum allowed value is 100.
pub fn page_size(mut self, value: i64) -> Self {
self.page_size = Some(value);
self
}
/// Specific page number to return.
pub fn page_number(mut self, value: i64) -> Self {
self.page_number = Some(value);
self
}
}

/// GetSBOMOptionalParams is a struct for passing parameters to the method [`SecurityMonitoringAPI::get_sbom`]
#[non_exhaustive]
#[derive(Clone, Default, Debug)]
Expand Down Expand Up @@ -858,6 +881,14 @@ pub enum GetHistoricalJobError {
UnknownValue(serde_json::Value),
}

/// GetRuleVersionHistoryError is a struct for typed errors of method [`SecurityMonitoringAPI::get_rule_version_history`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetRuleVersionHistoryError {
APIErrorResponse(crate::datadogV2::model::APIErrorResponse),
UnknownValue(serde_json::Value),
}

/// GetSBOMError is a struct for typed errors of method [`SecurityMonitoringAPI::get_sbom`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
Expand Down Expand Up @@ -3739,6 +3770,141 @@ impl SecurityMonitoringAPI {
}
}

/// Get a rule's version history.
pub async fn get_rule_version_history(
&self,
rule_id: String,
params: GetRuleVersionHistoryOptionalParams,
) -> Result<
crate::datadogV2::model::GetRuleVersionHistoryResponse,
datadog::Error<GetRuleVersionHistoryError>,
> {
match self
.get_rule_version_history_with_http_info(rule_id, params)
.await
{
Ok(response_content) => {
if let Some(e) = response_content.entity {
Ok(e)
} else {
Err(datadog::Error::Serde(serde::de::Error::custom(
"response content was None",
)))
}
}
Err(err) => Err(err),
}
}

/// Get a rule's version history.
pub async fn get_rule_version_history_with_http_info(
&self,
rule_id: String,
params: GetRuleVersionHistoryOptionalParams,
) -> Result<
datadog::ResponseContent<crate::datadogV2::model::GetRuleVersionHistoryResponse>,
datadog::Error<GetRuleVersionHistoryError>,
> {
let local_configuration = &self.config;
let operation_id = "v2.get_rule_version_history";
if local_configuration.is_unstable_operation_enabled(operation_id) {
warn!("Using unstable operation {operation_id}");
} else {
let local_error = datadog::UnstableOperationDisabledError {
msg: "Operation 'v2.get_rule_version_history' is not enabled".to_string(),
};
return Err(datadog::Error::UnstableOperationDisabledError(local_error));
}

// unbox and build optional parameters
let page_size = params.page_size;
let page_number = params.page_number;

let local_client = &self.client;

let local_uri_str = format!(
"{}/api/v2/security_monitoring/rules/{rule_id}/version_history",
local_configuration.get_operation_host(operation_id),
rule_id = datadog::urlencode(rule_id)
);
let mut local_req_builder =
local_client.request(reqwest::Method::GET, local_uri_str.as_str());

if let Some(ref local_query_param) = page_size {
local_req_builder =
local_req_builder.query(&[("page[size]", &local_query_param.to_string())]);
};
if let Some(ref local_query_param) = page_number {
local_req_builder =
local_req_builder.query(&[("page[number]", &local_query_param.to_string())]);
};

// build headers
let mut headers = HeaderMap::new();
headers.insert("Accept", HeaderValue::from_static("application/json"));

// build user agent
match HeaderValue::from_str(local_configuration.user_agent.as_str()) {
Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent),
Err(e) => {
log::warn!("Failed to parse user agent header: {e}, falling back to default");
headers.insert(
reqwest::header::USER_AGENT,
HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()),
)
}
};

// build auth
if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") {
headers.insert(
"DD-API-KEY",
HeaderValue::from_str(local_key.key.as_str())
.expect("failed to parse DD-API-KEY header"),
);
};
if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") {
headers.insert(
"DD-APPLICATION-KEY",
HeaderValue::from_str(local_key.key.as_str())
.expect("failed to parse DD-APPLICATION-KEY header"),
);
};

local_req_builder = local_req_builder.headers(headers);
let local_req = local_req_builder.build()?;
log::debug!("request content: {:?}", local_req.body());
let local_resp = local_client.execute(local_req).await?;

let local_status = local_resp.status();
let local_content = local_resp.text().await?;
log::debug!("response content: {}", local_content);

if !local_status.is_client_error() && !local_status.is_server_error() {
match serde_json::from_str::<crate::datadogV2::model::GetRuleVersionHistoryResponse>(
&local_content,
) {
Ok(e) => {
return Ok(datadog::ResponseContent {
status: local_status,
content: local_content,
entity: Some(e),
})
}
Err(e) => return Err(datadog::Error::Serde(e)),
};
} else {
let local_entity: Option<GetRuleVersionHistoryError> =
serde_json::from_str(&local_content).ok();
let local_error = datadog::ResponseContent {
status: local_status,
content: local_content,
entity: local_entity,
};
Err(datadog::Error::ResponseError(local_error))
}
}

/// Get a single SBOM related to an asset by its type and name.
///
pub async fn get_sbom(
Expand Down
14 changes: 14 additions & 0 deletions src/datadogV2/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3150,6 +3150,20 @@ pub mod model_security_monitoring_rule_update_payload;
pub use self::model_security_monitoring_rule_update_payload::SecurityMonitoringRuleUpdatePayload;
pub mod model_security_monitoring_rule_query;
pub use self::model_security_monitoring_rule_query::SecurityMonitoringRuleQuery;
pub mod model_get_rule_version_history_response;
pub use self::model_get_rule_version_history_response::GetRuleVersionHistoryResponse;
pub mod model_get_rule_version_history_data;
pub use self::model_get_rule_version_history_data::GetRuleVersionHistoryData;
pub mod model_rule_version_history;
pub use self::model_rule_version_history::RuleVersionHistory;
pub mod model_rule_versions;
pub use self::model_rule_versions::RuleVersions;
pub mod model_rule_version_update;
pub use self::model_rule_version_update::RuleVersionUpdate;
pub mod model_rule_version_update_type;
pub use self::model_rule_version_update_type::RuleVersionUpdateType;
pub mod model_get_rule_version_history_data_type;
pub use self::model_get_rule_version_history_data_type::GetRuleVersionHistoryDataType;
pub mod model_security_monitoring_signals_sort;
pub use self::model_security_monitoring_signals_sort::SecurityMonitoringSignalsSort;
pub mod model_security_monitoring_signals_list_response;
Expand Down
Loading
Loading