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 pathgenerate-sqlx.rb
More file actions
executable file
·88 lines (72 loc) · 2.33 KB
/
Copy pathgenerate-sqlx.rb
File metadata and controls
executable file
·88 lines (72 loc) · 2.33 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#! /usr/bin/env ruby
CSA_DIR = "#{File.expand_path("..", File.dirname(__FILE__))}"
CSA_LIB_DIR = "#{CSA_DIR}/lib"
CSA_DATAFORM_DIR = "#{CSA_DIR}/dataform/definitions/raw"
$LOAD_PATH << "#{CSA_LIB_DIR}" unless $LOAD_PATH.include? "#{CSA_LIB_DIR}"
require 'erb'
require 'fileutils'
require 'json'
require 'csv'
require 'csa'
class CSASqlx
CSA = CSA.new
#
# Generates all SQLX queries for all CSA detections with Log Analytics SQL query
#
def generate_all_the_sqlx!
oks = []
fails = []
CSA.detections.each do |detection|
begin
print "Generating sqlx for #{detection['id']}"
query_filename = detection['query_abs_paths']['log_analytics']['sql'] || ''
if not File.exist?(query_filename)
puts " FAIL Source query does not exist."
next
end
sqlx_filename = "#{detection['id'].to_s.gsub(/\./,'_')}_raw.sqlx"
sqlx_filename = "#{CSA_DATAFORM_DIR}/#{sqlx_filename}"
generate_detection_sqlx! query_filename, detection['name'], sqlx_filename
oks << detection['id']
puts " OK"
rescue => ex
fails << detection['id']
puts " FAIL #{ex}\n#{ex.backtrace.join("\n")}"
end
end
puts
puts "Generated sqlx for #{oks.count} detections, #{fails.count} failures"
return oks, fails
end
#
# Generates SQLX query for a specific detection from its Log Analytics SQL query
#
def generate_detection_sqlx!(query_path, description, output_sqlx_path)
sqlx_type = <<~EOF
config {
type: "view",
description: "#{description}"
}
EOF
# Replace FROM clause with data source reference defined in Dataform
sqlx_query = File.read(query_path)
sqlx_query = sqlx_query.gsub(
'`[MY_PROJECT_ID].[MY_LOG_BUCKET_REGION].[MY_LOG_BUCKET_NAME]._AllLogs`',
'${ref("_AllLogs")}'
)
# Updated hardcoded timestamp condition from WHERE clause
sqlx_query = sqlx_query.gsub(
/timestamp >=? .*$/,
'timestamp >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL ${dataform.projectConfig.vars.raw_lookback_days} DAY)'
)
# Strip out copyrights comments
sqlx_query = sqlx_query.gsub(/\/\*.*?\*\//m, "")
print " => #{output_sqlx_path} =>"
File.write output_sqlx_path, sqlx_type + sqlx_query
end
end
#
# MAIN
#
oks, fails = CSASqlx.new.generate_all_the_sqlx!
exit fails.count