Class: Wafoo::Run

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/wafoo/run.rb

Instance Method Summary collapse

Methods included from Helper

#added_print, #info_print, #output_table, #removed_print, #split_cidr

Constructor Details

#initialize(options = nil) ⇒ Run

Returns a new instance of Run.



7
8
9
10
11
# File 'lib/wafoo/run.rb', line 7

def initialize(options = nil)
  @waf_regional = Aws::WAFRegional::Client.new
  @waf = Aws::WAF::Client.new
  @regional = options[:regional] unless options.nil?
end

Instance Method Details

#apply_ipsets(ipsets, ip_set_id) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/wafoo/run.rb', line 68

def apply_ipsets(ipsets, ip_set_id)
  waf = @regional ? @waf_regional : @waf
  puts 'Applying IP List...'
  change_token = waf.get_change_token.change_token
  begin
    waf.update_ip_set(
      ip_set_id: ip_set_id,
      change_token: change_token,
      updates: ipsets
    )
    puts 'Apply Finished.'
    exit 0
  rescue => ex
    puts error_print(ex.message)
    exit 1
  end
end

#export_ipsets(ip_set_id) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/wafoo/run.rb', line 58

def export_ipsets(ip_set_id)
  ipsets = read_ipsets_from_api(ip_set_id)
  puts 'Exporting IP List...'
  ipsets.sort.each { |ipset| puts info_print(ipset) }
  File.open(ip_set_id, 'w') do |f|
    ipsets.sort.each { |ipset| f.puts(ipset) }
  end
  puts 'Exported to ' + added_print(ip_set_id)
end

#generate_delete_hash(ipset) ⇒ Object



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
114
115
116
117
118
119
120
# File 'lib/wafoo/run.rb', line 86

def generate_delete_hash(ipset)
  ipset.slice!(0)
  # p ipset
  h = {
    action: 'DELETE',
    ip_set_descriptor: {
      type: 'IPV4',
      value: ipset
    }
  }

  # unless %w(8 16 24 33).include?(ipset.split('/').last)
  #   ips = split_cidr(ipset)
  #   ipsets_array = []
  #   ips.each do |ip|
  #     ipsets_array << {
  #                        action: 'DELETE',
  #                        ip_set_descriptor: {
  #                          type: 'IPV4',
  #                          value: ip + '/32'
  #                        }
  #                     }
  #   end
  #   return ipsets_array
  # end

  ipsets_hash = {
                   action: 'DELETE',
                   ip_set_descriptor: {
                     type: 'IPV4',
                     value: ipset
                   }
                }
  ipsets_hash
end

#generate_insert_hash(ipset) ⇒ Object



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
# File 'lib/wafoo/run.rb', line 122

def generate_insert_hash(ipset)
  ipset.slice!(0)
  # unless %w(8 16 24 33).include?(ipset.split('/').last)
  #   ips = split_cidr(ipset)
  #   ipsets_array = []
  #   ips.each do |ip|
  #     ipsets_array << {
  #                        action: 'INSERT',
  #                        ip_set_descriptor: {
  #                          type: 'IPV4',
  #                          value: ip + '/32'
  #                        }
  #                     }
  #   end
  #   return ipsets_array
  # end

  ipsets_hash = {
                   action: 'INSERT',
                   ip_set_descriptor: {
                     type: 'IPV4',
                     value: ipset
                   }
                }
  ipsets_hash
end

#list_ipsetsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/wafoo/run.rb', line 38

def list_ipsets
  ip_sets = []
  params = {}
  [ @waf_regional, @waf ].each do |w|
    loop do
      res = w.list_ip_sets(params)
      res.ip_sets.each do |set|
        ipset = []
        ipset << w.class.to_s.split('::')[1]
        ipset << set.ip_set_id
        ipset << set.name
        ip_sets << ipset
      end
      break if res.next_marker.nil?
      params[:next_marker] = res.next_marker
    end
  end
  output_table(ip_sets)
end

#read_ipsets_from_api(ip_set_id) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/wafoo/run.rb', line 13

def read_ipsets_from_api(ip_set_id)
  waf = @regional ? @waf_regional : @waf
  resp = waf.get_ip_set({
    ip_set_id: ip_set_id
  })
  ipsets = []
  sorted_ipsets = resp.ip_set.ip_set_descriptors.sort {|a,b| a[:value] <=> b[:value]}
  sorted_ipsets.each do |ipset|
    ipsets << ipset.value
  end

  ipsets
end

#read_ipsets_from_file(ip_set_id) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/wafoo/run.rb', line 27

def read_ipsets_from_file(ip_set_id)
  ipsets = []
  File.open(ip_set_id, 'r') do |file|
    file.read.split("\n").each do |ipset|
      ipsets << ipset
    end
  end

  ipsets.sort
end

#update_ipsets(ip_set_id, dry_run) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/wafoo/run.rb', line 149

def update_ipsets(ip_set_id, dry_run)
  _old = read_ipsets_from_api(ip_set_id).join("\n")
  _new = read_ipsets_from_file(ip_set_id).join("\n")
  ipsets = []
  Diffy::Diff.new(_old, _new).each do |line|
    case line
      when /^\+/ then
        puts 'Add Line: ' + added_print(line.chomp)
        ipsets << generate_insert_hash(line.chomp)
      when /^-/ then
        puts 'Remove Line: ' + removed_print(line.chomp)
        ipsets << generate_delete_hash(line.chomp)
    end
  end

  if !dry_run and ipsets.length > 0 then
    apply_ipsets(ipsets.flatten, ip_set_id)
    export_ipsets(ip_set_id)
  elsif dry_run and ipsets.length > 0 then
    puts 'Above IP list will be changed.'
    exit 0
  else
    puts 'No IP list changed.'
    exit 0
  end
end