-
Notifications
You must be signed in to change notification settings - Fork 380
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2732 from DataDog/appsec-reuse-rules-when-asm-dd-…
…is-empty [APPSEC-8867] Appsec reuse rules when ASM_DD is empty
- Loading branch information
Showing
10 changed files
with
447 additions
and
315 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../assets' | ||
|
||
module Datadog | ||
module AppSec | ||
class Processor | ||
# RuleLoader utility modules | ||
# that load appsec rules and data from settings | ||
module RuleLoader | ||
class << self | ||
def load_rules(ruleset:) | ||
begin | ||
case ruleset | ||
when :recommended, :strict | ||
JSON.parse(Datadog::AppSec::Assets.waf_rules(ruleset)) | ||
when :risky | ||
Datadog.logger.warn( | ||
'The :risky Application Security Management ruleset has been deprecated and no longer available.'\ | ||
'The `:recommended` ruleset will be used instead.'\ | ||
'Please remove the `appsec.ruleset = :risky` setting from your Datadog.configure block.' | ||
) | ||
JSON.parse(Datadog::AppSec::Assets.waf_rules(:recommended)) | ||
when String | ||
JSON.parse(File.read(File.expand_path(ruleset))) | ||
when File, StringIO | ||
JSON.parse(ruleset.read || '').tap { ruleset.rewind } | ||
when Hash | ||
ruleset | ||
else | ||
raise ArgumentError, "unsupported value for ruleset setting: #{ruleset.inspect}" | ||
end | ||
rescue StandardError => e | ||
Datadog.logger.error do | ||
"libddwaf ruleset failed to load, ruleset: #{ruleset.inspect} error: #{e.inspect}" | ||
end | ||
|
||
nil | ||
end | ||
end | ||
|
||
def load_data(ip_denylist: [], user_id_denylist: []) | ||
data = [] | ||
data << { 'rules_data' => [denylist_data('blocked_ips', ip_denylist)] } if ip_denylist.any? | ||
data << { 'rules_data' => [denylist_data('blocked_users', user_id_denylist)] } if user_id_denylist.any? | ||
|
||
data.any? ? data : nil | ||
end | ||
|
||
private | ||
|
||
def denylist_data(id, denylist) | ||
{ | ||
'id' => id, | ||
'type' => 'data_with_expiration', | ||
'data' => denylist.map { |v| { 'value' => v.to_s, 'expiration' => 2**63 } } | ||
} | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Datadog | ||
module AppSec | ||
class Processor | ||
module RuleLoader | ||
type ruleset = Symbol | String | File | StringIO | Hash[String, untyped] | ||
def self.load_rules: (ruleset: ruleset) -> ::Hash[untyped, untyped]? | ||
|
||
def self.load_data: (?ip_denylist: Array[String], ?user_id_denylist: Array[String]) -> Array[Hash[String, untyped]]? | ||
|
||
private | ||
|
||
def self.denylist_data: (String id, Array[String] denylist) -> ::Hash[::String, untyped] | ||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.