Skip to content
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

Add validation endpoint for Security Monitoring Rules #2453

Merged
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
8 changes: 4 additions & 4 deletions .apigentools-info
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"spec_versions": {
"v1": {
"apigentools_version": "1.6.6",
"regenerated": "2024-03-26 15:17:45.384991",
"spec_repo_commit": "46383d02"
"regenerated": "2024-03-27 22:12:41.864092",
"spec_repo_commit": "85625198"
},
"v2": {
"apigentools_version": "1.6.6",
"regenerated": "2024-03-26 15:17:45.404876",
"spec_repo_commit": "46383d02"
"regenerated": "2024-03-27 22:12:41.882235",
"spec_repo_commit": "85625198"
}
}
}
28 changes: 28 additions & 0 deletions .generator/schemas/v2/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32287,6 +32287,34 @@ paths:
tags:
- Security Monitoring
x-codegen-request-body-name: body
/api/v2/security_monitoring/rules/validation:
post:
description: Validate a detection rule.
operationId: ValidateSecurityMonitoringRule
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/SecurityMonitoringRuleCreatePayload'
required: true
responses:
'204':
description: OK
'400':
$ref: '#/components/responses/BadRequestResponse'
'403':
$ref: '#/components/responses/NotAuthorizedResponse'
'429':
$ref: '#/components/responses/TooManyRequestsResponse'
security:
- apiKeyAuth: []
appKeyAuth: []
- AuthZ:
- security_monitoring_rules_write
summary: Validate a detection rule
tags:
- Security Monitoring
x-codegen-request-body-name: body
/api/v2/security_monitoring/rules/{rule_id}:
delete:
description: Delete an existing rule. Default rules cannot be deleted.
Expand Down
63 changes: 63 additions & 0 deletions api/datadogV2/api_security_monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -2268,6 +2268,69 @@ func (a *SecurityMonitoringApi) UpdateSecurityMonitoringSuppression(ctx _context
return localVarReturnValue, localVarHTTPResponse, nil
}

// ValidateSecurityMonitoringRule Validate a detection rule.
// Validate a detection rule.
func (a *SecurityMonitoringApi) ValidateSecurityMonitoringRule(ctx _context.Context, body SecurityMonitoringRuleCreatePayload) (*_nethttp.Response, error) {
var (
localVarHTTPMethod = _nethttp.MethodPost
localVarPostBody interface{}
)

localBasePath, err := a.Client.Cfg.ServerURLWithContext(ctx, "v2.SecurityMonitoringApi.ValidateSecurityMonitoringRule")
if err != nil {
return nil, datadog.GenericOpenAPIError{ErrorMessage: err.Error()}
}

localVarPath := localBasePath + "/api/v2/security_monitoring/rules/validation"

localVarHeaderParams := make(map[string]string)
localVarQueryParams := _neturl.Values{}
localVarFormParams := _neturl.Values{}
localVarHeaderParams["Content-Type"] = "application/json"
localVarHeaderParams["Accept"] = "*/*"

// body params
localVarPostBody = &body
datadog.SetAuthKeys(
ctx,
&localVarHeaderParams,
[2]string{"apiKeyAuth", "DD-API-KEY"},
[2]string{"appKeyAuth", "DD-APPLICATION-KEY"},
)
req, err := a.Client.PrepareRequest(ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, nil)
if err != nil {
return nil, err
}

localVarHTTPResponse, err := a.Client.CallAPI(req)
if err != nil || localVarHTTPResponse == nil {
return localVarHTTPResponse, err
}

localVarBody, err := datadog.ReadBody(localVarHTTPResponse)
if err != nil {
return localVarHTTPResponse, err
}

if localVarHTTPResponse.StatusCode >= 300 {
newErr := datadog.GenericOpenAPIError{
ErrorBody: localVarBody,
ErrorMessage: localVarHTTPResponse.Status,
}
if localVarHTTPResponse.StatusCode == 400 || localVarHTTPResponse.StatusCode == 403 || localVarHTTPResponse.StatusCode == 429 {
var v APIErrorResponse
err = a.Client.Decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
if err != nil {
return localVarHTTPResponse, newErr
}
newErr.ErrorModel = v
}
return localVarHTTPResponse, newErr
}

return localVarHTTPResponse, nil
}

// NewSecurityMonitoringApi Returns NewSecurityMonitoringApi.
func NewSecurityMonitoringApi(client *datadog.APIClient) *SecurityMonitoringApi {
return &SecurityMonitoringApi{
Expand Down
1 change: 1 addition & 0 deletions api/datadogV2/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@
// - [SecurityMonitoringApi.UpdateSecurityFilter]
// - [SecurityMonitoringApi.UpdateSecurityMonitoringRule]
// - [SecurityMonitoringApi.UpdateSecurityMonitoringSuppression]
// - [SecurityMonitoringApi.ValidateSecurityMonitoringRule]
// - [SensitiveDataScannerApi.CreateScanningGroup]
// - [SensitiveDataScannerApi.CreateScanningRule]
// - [SensitiveDataScannerApi.DeleteScanningGroup]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Validate a detection rule returns "OK" response

package main

import (
"context"
"fmt"
"os"

"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
)

func main() {
body := datadogV2.SecurityMonitoringRuleCreatePayload{
SecurityMonitoringStandardRuleCreatePayload: &datadogV2.SecurityMonitoringStandardRuleCreatePayload{
Cases: []datadogV2.SecurityMonitoringRuleCaseCreate{
{
Name: datadog.PtrString(""),
Status: datadogV2.SECURITYMONITORINGRULESEVERITY_INFO,
Notifications: []string{},
Condition: datadog.PtrString("a > 0"),
},
},
HasExtendedTitle: datadog.PtrBool(true),
IsEnabled: true,
Message: "My security monitoring rule",
Name: "My security monitoring rule",
Options: datadogV2.SecurityMonitoringRuleOptions{
EvaluationWindow: datadogV2.SECURITYMONITORINGRULEEVALUATIONWINDOW_THIRTY_MINUTES.Ptr(),
KeepAlive: datadogV2.SECURITYMONITORINGRULEKEEPALIVE_THIRTY_MINUTES.Ptr(),
MaxSignalDuration: datadogV2.SECURITYMONITORINGRULEMAXSIGNALDURATION_THIRTY_MINUTES.Ptr(),
DetectionMethod: datadogV2.SECURITYMONITORINGRULEDETECTIONMETHOD_THRESHOLD.Ptr(),
},
Queries: []datadogV2.SecurityMonitoringStandardRuleQuery{
{
Query: datadog.PtrString("source:source_here"),
GroupByFields: []string{
"@userIdentity.assumed_role",
},
DistinctFields: []string{},
Aggregation: datadogV2.SECURITYMONITORINGRULEQUERYAGGREGATION_COUNT.Ptr(),
Name: datadog.PtrString(""),
},
},
Tags: []string{
"env:prod",
"team:security",
},
Type: datadogV2.SECURITYMONITORINGRULETYPECREATE_LOG_DETECTION.Ptr(),
}}
ctx := datadog.NewDefaultContext(context.Background())
configuration := datadog.NewConfiguration()
apiClient := datadog.NewAPIClient(configuration)
api := datadogV2.NewSecurityMonitoringApi(apiClient)
r, err := api.ValidateSecurityMonitoringRule(ctx, body)

if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `SecurityMonitoringApi.ValidateSecurityMonitoringRule`: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2024-03-27T16:23:09.814Z
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
interactions:
- request:
body: |
{"cases":[{"condition":"a \u003e 0","name":"","notifications":[],"status":"info"}],"hasExtendedTitle":true,"isEnabled":true,"message":"My security monitoring rule","name":"My security monitoring rule","options":{"detectionMethod":"threshold","evaluationWindow":1800,"keepAlive":999999,"maxSignalDuration":1800},"queries":[{"aggregation":"count","distinctFields":[],"groupByFields":["@userIdentity.assumed_role"],"name":"","query":"source:source_here"}],"tags":["env:prod","team:security"],"type":"log_detection"}
form: {}
headers:
Accept:
- '*/*'
Content-Type:
- application/json
id: 0
method: POST
url: https://api.datadoghq.com/api/v2/security_monitoring/rules/validation
response:
body: '{"error":{"code":"InvalidArgument","message":"Invalid rule configuration","details":[{"code":"InvalidArgument","message":"Max
signal duration must be greater than or equal to keep alive","target":"maxSignalDuration"},{"code":"InvalidArgument","message":"Keep
alive is not in allowed durations: 0, 1, 5, 10, 15, 30, 60, 120, 180, 360 (in
minutes)","target":"keepAlive"}]}}

'
code: 400
duration: 0ms
headers:
Content-Type:
- application/json
status: 400 Bad Request
version: 2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2024-03-27T16:23:10.157Z
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
interactions:
- request:
body: |
{"cases":[{"condition":"a \u003e 0","name":"","notifications":[],"status":"info"}],"hasExtendedTitle":true,"isEnabled":true,"message":"My security monitoring rule","name":"My security monitoring rule","options":{"detectionMethod":"threshold","evaluationWindow":1800,"keepAlive":1800,"maxSignalDuration":1800},"queries":[{"aggregation":"count","distinctFields":[],"groupByFields":["@userIdentity.assumed_role"],"name":"","query":"source:source_here"}],"tags":["env:prod","team:security"],"type":"log_detection"}
form: {}
headers:
Accept:
- '*/*'
Content-Type:
- application/json
id: 0
method: POST
url: https://api.datadoghq.com/api/v2/security_monitoring/rules/validation
response:
body: ''
code: 204
duration: 0ms
headers:
Content-Type:
- text/html; charset=utf-8
status: 204 No Content
version: 2
14 changes: 14 additions & 0 deletions tests/scenarios/features/v2/security_monitoring.feature
Original file line number Diff line number Diff line change
Expand Up @@ -606,3 +606,17 @@ Feature: Security Monitoring
Then the response status is 200 OK
And the response "name" is equal to "{{ unique }}-Updated"
And the response "id" has the same value as "security_rule.id"

@skip-go @skip-java @skip-python @skip-ruby @skip-rust @skip-typescript @skip-validation @team:DataDog/k9-cloud-security-platform
Scenario: Validate a detection rule returns "Bad Request" response
Given new "ValidateSecurityMonitoringRule" request
And body with value {"cases":[{"name":"","status":"info","notifications":[],"condition":"a > 0"}],"hasExtendedTitle":true,"isEnabled":true,"message":"My security monitoring rule","name":"My security monitoring rule","options":{"evaluationWindow":1800,"keepAlive":999999,"maxSignalDuration":1800,"detectionMethod":"threshold"},"queries":[{"query":"source:source_here","groupByFields":["@userIdentity.assumed_role"],"distinctFields":[],"aggregation":"count","name":""}],"tags":["env:prod","team:security"],"type":"log_detection"}
When the request is sent
Then the response status is 400 Bad Request

@team:DataDog/k9-cloud-security-platform
Scenario: Validate a detection rule returns "OK" response
Given new "ValidateSecurityMonitoringRule" request
And body with value {"cases":[{"name":"","status":"info","notifications":[],"condition":"a > 0"}],"hasExtendedTitle":true,"isEnabled":true,"message":"My security monitoring rule","name":"My security monitoring rule","options":{"evaluationWindow":1800,"keepAlive":1800,"maxSignalDuration":1800,"detectionMethod":"threshold"},"queries":[{"query":"source:source_here","groupByFields":["@userIdentity.assumed_role"],"distinctFields":[],"aggregation":"count","name":""}],"tags":["env:prod","team:security"],"type":"log_detection"}
When the request is sent
Then the response status is 204 OK
6 changes: 6 additions & 0 deletions tests/scenarios/features/v2/undo.json
Original file line number Diff line number Diff line change
Expand Up @@ -1751,6 +1751,12 @@
"type": "unsafe"
}
},
"ValidateSecurityMonitoringRule": {
"tag": "Security Monitoring",
"undo": {
"type": "idempotent"
}
},
"DeleteSecurityMonitoringRule": {
"tag": "Security Monitoring",
"undo": {
Expand Down
Loading