Class: AddFuncUkaz60_2

Inherits:
Object
  • Object
show all
Defined in:
lib/imperituroard/projects/ukaz60/ukaz60_add_func.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAddFuncUkaz60_2

Returns a new instance of AddFuncUkaz60_2.



8
9
10
# File 'lib/imperituroard/projects/ukaz60/ukaz60_add_func.rb', line 8

def initialize
  @add_functions = LogAddFunctions_2.new
end

Instance Attribute Details

#add_functionsObject

Returns the value of attribute add_functions.



6
7
8
# File 'lib/imperituroard/projects/ukaz60/ukaz60_add_func.rb', line 6

def add_functions
  @add_functions
end

Instance Method Details

#ansible_answer_parse(answer_string) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/imperituroard/projects/ukaz60/ukaz60_add_func.rb', line 116

def ansible_answer_parse(answer_string)
  input_data = {:answer_string => answer_string}
  output_data = {}

  begin
    answ_ans_parsed = answer_string.gsub("\n", " ")
    result_out = {}
    #result_out = []
    num_ok = 0
    num_failed = 0
    final_checks = /(\S+)\s+:\s+ok=(\d)\s+changed=(\d)\s+unreachable=(\d)\s+failed=(\d)\s+skipped=(\d)\s+rescued=(\d)\s+ignored=(\d)\s+/

    processed_data = answ_ans_parsed.scan(final_checks)
    if processed_data!=[] && processed_data!=""
      for aaa in processed_data
        begin
          if aaa[1].to_i>0 && aaa[3].to_i==0 && aaa[4].to_i==0 && aaa[5].to_i==0 && aaa[6].to_i==0 && aaa[7].to_i==0
            result_out[aaa[0]] = "Ok"
            #result_out.push({aaa[0] => "Ok"})
            num_ok = num_ok + 1
          else
            result_out[aaa[0]] = 'Failed'
            #result_out.push({aaa[0] => "Failed"})
            num_failed = num_failed + 1
          end
        rescue
          result_out['unknown'] = aaa.to_s
          #result_out.push({"unknown" => aaa.to_s})
          num_failed = num_failed + 1
        end
      end
      output_data = {:code => 200, :result => "ansible_answer_parse: Request completed successfully", :body => {:list => result_out, :num_ok => num_ok, :num_failed => num_failed}}
    else
      output_data = {:code => 400, :result => "ansible_answer_parse: Ansible returned empty data"}
    end
  rescue
    output_data = {:code => 507, :result => "ansible_answer_parse: Unknown error"}
  end
  add_functions.printer_texter(output_data, "debug")
  output_data
end

#ansible_answer_parse_fmg(answer_string) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/imperituroard/projects/ukaz60/ukaz60_add_func.rb', line 159

def ansible_answer_parse_fmg(answer_string)
  input_data = {:answer_string => answer_string}
  output_data = {}

  begin
    answ_ans_parsed = answer_string.gsub("\n", " ")
    result_out = {}
    #result_out = []
    num_ok = 0
    num_failed = 0
    #final_checks = /(fortimanager1)\s+:\s+ok=(\d)\s+changed=(\d)\s+unreachable=(\d)\s+failed=(\d)\s+/
    final_checks = /(fortimanager1)\s+:\s+ok=(\d+)\s+changed=(\d+)\s+unreachable=(\d+)\s+failed=(\d+)\s+/

    #p answ_ans_parsed
    processed_data = answ_ans_parsed.scan(final_checks)
    #p processed_data
    if processed_data!=[] && processed_data!=""
      for aaa in processed_data
        begin
          if aaa[1].to_i>0 && aaa[3].to_i==0 && aaa[4].to_i==0
            result_out[aaa[0]] = {:ok => aaa[1], :changed => aaa[2], :unreachable => aaa[3], :failed => aaa[4], :result => "Ok"}
            #result_out.push({aaa[0] => "Ok"})
            num_ok = num_ok + 1
          else
            result_out[aaa[0]] = {:ok => aaa[1], :unreachable => aaa[3], :failed => aaa[4], :result => "Failed"}
            #result_out.push({aaa[0] => "Failed"})
            num_failed = num_failed + 1
          end
        rescue
          result_out["unknown"] = aaa.to_s
          #result_out.push({"unknown" => aaa.to_s})
          num_failed = num_failed + 1
        end
      end
      output_data = {:code => 200, :result => "ansible_answer_parse: Request completed successfully", :body => {:list => result_out, :num_ok => num_ok, :num_failed => num_failed}}
    else
      output_data = {:code => 400, :result => "ansible_answer_parse: Ansible returned empty data"}
    end
  rescue
    output_data = {:code => 507, :result => "ansible_answer_parse: Unknown error"}
  end
  add_functions.printer_texter(output_data, "debug")
  output_data
end

#array_hash_compare(array1, array2) ⇒ Object

result array2-array1 array2 - all array1 - blocked



218
219
220
221
222
223
224
225
226
227
228
# File 'lib/imperituroard/projects/ukaz60/ukaz60_add_func.rb', line 218

def array_hash_compare(array1, array2)
  diff = []
  array2.each do |h1|
    if array1.include?(h1)
      nil
    else
      diff.push(h1)
    end
  end
  diff
end

#delete_dump_symbols_url(stroka) ⇒ Object

function for process separate domains for one domain



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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/imperituroard/projects/ukaz60/ukaz60_add_func.rb', line 14

def delete_dump_symbols_url(stroka)
  input_params = {:stroka => stroka}
  output_answer = {}
  processed_domain_in_url = ""
  protocol = ""
  type = ""
  domain_only = ""
  begin

    #p "Step1 stroka: #{stroka}"

    first_url = stroka.gsub("</", "")

    if first_url != "-" && first_url != ""

      #check web protocol (http/https/unknown)
      if stroka.include? "http:"
        protocol = "http"
      elsif stroka.include? "https:"
        protocol = "https"
      else
        protocol = "unknown"
        p "ERROR: delete_dump_symbols_url.unknown protocol"
      end

      if first_url.include? "http://"
        processed_domain_in_url = first_url.gsub("http://", "")
      else
        processed_domain_in_url = processed_domain_in_url + first_url
      end

      if processed_domain_in_url.include? "http:///"
        processed_domain_in_url.gsub!("http:///", "")
      end

      if processed_domain_in_url.include? "http:/"
        processed_domain_in_url.gsub!("http:/", "")
      end

      if processed_domain_in_url.include? "https://"
        processed_domain_in_url.gsub!("https://", "")
      end

      if processed_domain_in_url.include? "https:/"
        processed_domain_in_url.gsub!("https:/", "")
      end

      if processed_domain_in_url.include? " "
        processed_domain_in_url = processed_domain_in_url.gsub!(" ", "")
      end

      #delete / at the end
      if processed_domain_in_url[processed_domain_in_url.size-1]=="/"
        sss = processed_domain_in_url.size
        processed_domain_in_url = processed_domain_in_url[0..sss-2]
      end

      #p "Step2 processed_domain_in_url: #{processed_domain_in_url}"

      if processed_domain_in_url != "-" && processed_domain_in_url != "" && processed_domain_in_url != nil
        #p "Step3 processed_domain_in_url: #{processed_domain_in_url}"
        #if include / then it is url. Else - domain
        if !processed_domain_in_url.include? "/"
          #it is domain. Process domain
          #if it is kirillica then process
          #if processed_domain_in_url.match(/а|б|в|г|д|е|ж|ё|Ё|з|и|й|к|л|м|н|о|п|р|с|т|у|ф|х|ц|ч|ш|щ|ь|ы|ъ|э|ю|я|А|Б|В|Г|Д|Е|Ж|З|И|Й|К|Л|М|Н|О|П|Р|С|Т|У|Ф|Х|Ц|Ч|Ш|Щ|Ь|Ы|Ъ|Э|Ю|Я/)
          if processed_domain_in_url.match(/[а-яА-ЯЁё]/)
            processed_domain_in_url = SimpleIDN.to_ascii(processed_domain_in_url)
          end
          domain_only = domain_only + processed_domain_in_url
          type = 'domain'
        else
          #it is url. process url
          hh = processed_domain_in_url.split('/')
          domain_only = hh[0]
          #if domain_only.match(/а|б|в|г|д|е|ж|ё|Ё|з|и|й|к|л|м|н|о|п|р|с|т|у|ф|х|ц|ч|ш|щ|ь|ы|ъ|э|ю|я|А|Б|В|Г|Д|Е|Ж|З|И|Й|К|Л|М|Н|О|П|Р|С|Т|У|Ф|Х|Ц|Ч|Ш|Щ|Ь|Ы|Ъ|Э|Ю|Я/)
          if domain_only.match(/[а-яА-ЯЁё]/)
            hh[0] = SimpleIDN.to_ascii(domain_only)
            processed_domain_in_url = ''
            for t in hh
              processed_domain_in_url = processed_domain_in_url + t + '/'
            end
            sss1len = processed_domain_in_url.size
            processed_domain_in_url = processed_domain_in_url[0..sss1len-2]
          end
          type = 'url'
        end
      else
        type = 'empty'
      end
      output_answer = {:code => 200, :result => 'Request processed', :ishodnye_dannye => stroka, :first_url => first_url, :processed_domain_in_url => processed_domain_in_url, :domain_only => domain_only, :protocol => protocol, :type => type}

    else
      output_answer = {:code => 202, :result => 'Data not found'}
    end
  rescue
    output_answer = {:code => 507, :result => 'Something wrong'}
  end
  output_answer
end

#format_star_processed_answ(data) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/imperituroard/projects/ukaz60/ukaz60_add_func.rb', line 313

def format_star_processed_answ(data)
  answ_out = {:body => {:to_message => {:code => 200, :result => "Staros processed", :data => {:block_me => {}, :unblock_me=> {}}}, :config => {}}}
  compa_n = 0
  compa_n_data = ""
  flag = 0
  notbl_equal = 0
  data[:body][:for_block][:notblocked_stat].each do |blo_for|
    if flag == 0
      compa_n = blo_for[1]
      compa_n_data = blo_for[0]
    end
    if compa_n == blo_for[1]
      nil
    else
      notbl_equal = 1
    end
  end

  #p notbl_equal
  if notbl_equal == 0
    block_me = {:for_block_count => data[:body][:for_block][:notblocked_stat][compa_n_data]}
    unblock_me = {:for_unblock_count => data[:body][:for_unblock][:for_unblock_stat][compa_n_data]}

    if block_me[:for_block_count] != 0 && unblock_me[:for_unblock_count] != 0
      answ_out[:body][:to_message][:code] = 202
      answ_out[:body][:to_message][:result] = "Something not actual"
    else
      answ_out[:body][:to_message][:code] = 200
      answ_out[:body][:to_message][:result] = "Staros processed"
    end

    answ_out[:body][:to_message][:data][:block_me]= block_me
    answ_out[:body][:to_message][:data][:unblock_me]= unblock_me
  else
    answ_out[:body][:to_message][:data][:block_me] = data[:body][:for_block][:notblocked_stat]
    answ_out[:body][:to_message][:data][:unblock_me] = data[:body][:for_unblock][:for_unblock_stat]
  end

  answ_out
end

#http_to_forti_prep(domain) ⇒ Object



211
212
213
# File 'lib/imperituroard/projects/ukaz60/ukaz60_add_func.rb', line 211

def http_to_forti_prep(domain)
  domain.gsub("#", "")
end

#https_to_forti_prep(domain) ⇒ Object



205
206
207
208
209
# File 'lib/imperituroard/projects/ukaz60/ukaz60_add_func.rb', line 205

def https_to_forti_prep(domain)
  f0 = domain.gsub("#", "")
  f1 = f0.gsub("https://", "").split("/")
  f1[0]
end

#staros_compare_http(data_staros, data_belgim) ⇒ Object

procedure for compare http urls from staros and http urls from belgim



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/imperituroard/projects/ukaz60/ukaz60_add_func.rb', line 232

def staros_compare_http(data_staros, data_belgim)

  notblocked_stat = {}
  alert_unprotected = {}
  alert_unprotected_out = {}
  output_answ = {:body => {:for_block => {}, :for_unblock => {}}}
  for_unblock_tmp = {}
  for_unblock = {}
  for_unblock_stat = {}


  begin
    #only for one ECS instance
    data_staros[:mpc_data].each do |star_host|
      alert_unprotected[star_host[:hostname]] = []
      for_unblock_tmp[star_host[:hostname]] = []

      #check if belgim http url is blocked on STAROS
      data_belgim[:url_all].each do |belg_url|
        blocked = 0
        star_host[:body][:blocked_urls_http].each do |star_dom|
          star_url_curr = star_dom[:url]
          star_dom_len = star_url_curr.length
          star_dom_x = ""
          if star_url_curr[star_dom_len - 1] == "/"
            star_dom_x = star_url_curr[0..star_dom_len - 2]
          else
            star_dom_x = star_url_curr
          end
          if belg_url.include?(star_dom_x) || belg_url.include?(URI.decode(star_dom_x))
            blocked = 1
            for_unblock_tmp[star_host[:hostname]].push(star_dom)
          end
        end
        if blocked == 0
          alert_unprotected[star_host[:hostname]].push(belg_url)
        end
      end
    end

    alert_unprotected.each do |proc_unpro|
      da123 = proc_unpro[1]
      alert_unprotected_out[proc_unpro[0]] = da123.uniq
      notblocked_stat[proc_unpro[0]] = proc_unpro[1].length
    end

    for_unblock_tmp.each do |tmp_unbl|
      hostname_st = tmp_unbl[0]
      host_data_tmp = tmp_unbl[1].uniq
      staros_all = data_staros[:add_format][hostname_st][:blocked_urls_http].uniq
      da_annn = self.array_hash_compare(host_data_tmp, staros_all)
      for_unblock[hostname_st] = da_annn.uniq
      for_unblock_stat[hostname_st] = da_annn.uniq.length
    end

    output_answ[:code] = 200
    output_answ[:result] = "Success"
    output_answ[:body][:for_block][:notblocked_stat] = notblocked_stat
    output_answ[:body][:for_block][:not_blocked_belgim] = alert_unprotected_out
    output_answ[:body][:for_unblock][:for_unblock] = for_unblock
    output_answ[:body][:for_unblock][:for_unblock_stat] = for_unblock_stat

  rescue
    output_answ = {:code => 507, :result => "staros_compare_http: Unknown SDK error"}
  end

  #p JSON.pretty_generate(output_answ)
  #p "staros_compare_http"
  #p output_answ

  #p "temp data"
  for i in output_answ[:body][:for_unblock][:for_unblock]["vit11asr2"]
  #for i in output_answ[:body][:for_block][:not_blocked_belgim]["vit11asr2"]
    p i
  end
  output_answ
end