This repository was archived by the owner on Aug 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathcsa.rb
More file actions
68 lines (61 loc) · 2.06 KB
/
Copy pathcsa.rb
File metadata and controls
68 lines (61 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require 'yaml'
require 'erb'
class CSA
DETECTIONS_DIR = "#{File.dirname(File.dirname(__FILE__))}/src"
BACKENDS_DIR = "#{File.dirname(File.dirname(__FILE__))}/backends"
#
# Returns a list of paths that contain Detections
#
def detection_paths
Dir["#{DETECTIONS_DIR}/*/*.yaml"].sort
end
#
# Returns a list of backends
#
def backends
@backends ||= Dir.chdir("#{BACKENDS_DIR}") do
Dir.glob('*').select { |f| File.directory? f }
end.sort
end
#
# Returns a map of backends and associated languages
# Example: {"bigquery"=>["sql"], "chronicle"=>["yaral"], "log_analytics"=>["sql"]}
#
def formats
@formats ||= backends.reduce({}) do | hash, backend |
hash[backend] = []
Dir.chdir("#{BACKENDS_DIR}/#{backend}") do
Dir.glob('*').select { |f| File.directory? f }.each do | language |
hash[backend] << language
end
end
hash
end
end
#
# Returns a list of Detections (as Hashes from source YAML)
#
def detections
@detections ||= detection_paths.collect do |path|
detection_yaml = YAML.load(File.read path)
detection_yaml['detection_yaml_path'] = path
# Collect relative and absolute file paths of corresponding queries
detection_yaml['query_rel_paths'] = {}
detection_yaml['query_abs_paths'] = {}
formats.each do | backend, languages|
detection_yaml['query_rel_paths'][backend] = {}
detection_yaml['query_abs_paths'][backend] = {}
languages.each do | language |
filename = "#{detection_yaml['id'].to_s.gsub(/\./,'_')}_#{detection_yaml['name']}.#{language}"
query_relative_path = "../../backends/#{backend}/#{language}/" + filename
query_absolute_path = "#{BACKENDS_DIR}/#{backend}/#{language}/" + filename
if File.file?(query_absolute_path)
detection_yaml['query_abs_paths'][backend][language] = query_absolute_path
detection_yaml['query_rel_paths'][backend][language] = query_relative_path
end
end
end
detection_yaml
end
end
end