This repository has been archived by the owner on Apr 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiptables-poller
executable file
·319 lines (239 loc) · 7.72 KB
/
iptables-poller
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
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
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
157
158
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
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
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/ruby
require 'socket'
require 'optparse'
require 'English'
# We need IP forwarding and local routing to make this work
SYSCTL_KEYS = {
'net.ipv4.ip_forward' => '1',
'net.ipv4.conf.all.route_localnet' => '1'
}.freeze
# We can ignore rancher IPSec. Strings to avoid type casting
INGORE_PORTS = %w(500 4500)
CATTLE_CHAIN = 'CATTLE_PREROUTING'
CATTLE_CHAIN_COMMAND = "iptables -t nat -S #{CATTLE_CHAIN}"
POLLER_OUTPUT_CHAIN = 'POLLER_OUTPUT'
POLLER_POSTROUTING_CHAIN = 'POLLER_POSTROUTING'
POLLER_OUTPUT = '-d %{host_ip} -p %{proto} --dport %{source_port} -j DNAT --to %{dest_address}:%{dest_port}'
POLLER_POSTROUTING = '-d %{dest_address} -p %{proto} --dport %{dest_port} -j SNAT --to-source %{host_ip}'
POLLER_VERSION = '0.2.0'
# Exception to be raised when system call failed.
class ExecFailed < RuntimeError
attr_reader :command, :exit_code, :output
def initialize(command, exit_code, output)
@command = command.chomp
@exit_code = exit_code
@output = output
end
def inspect
"ExecFailed: command `#{command}`, failed with #{exit_code}, output:\n #{output}"
end
alias_method :to_s, :inspect
end
# :nodoc:
module IPTables
# Instead of regexp, lets try with optparse
class DNAT
# Parsed iptables rule
Rule = Struct.new(:proto, :source_port, :dest_address, :dest_port) do
# <# tcp://:500 -> 10.0.1.1:466>
def to_s
"<#{proto}://:#{source_port} -> #{dest_address}:#{dest_port}>"
end
end
def self.parse(options)
rule = Rule.new
opt_parser = OptionParser.new do |opts|
opts.on('-A ADD_CHAIN')
opts.on('-p PROTO') do |proto|
rule.proto = proto
end
opts.on('-m MARK_TYPE')
opts.on('--dst-type TYPE')
opts.on('--dport PORT') do |source_port|
rule.source_port = source_port
end
opts.on('-j JUMP_CHAIN')
opts.on('--to-destination ADDR') do |dest|
rule.dest_address, rule.dest_port = dest.split(':')
end
end
opt_parser.parse!(options.split)
rule
end
end
end
def log(message)
puts "[#{Time.now}] #{message}"
end
def debug(message)
puts "DEBUG [#{Time.now}] #{message}" if ENV['DEBUG']
end
def exec_safely(command)
output = `#{command} 2>&1`
exit_status = $CHILD_STATUS.exitstatus
if exit_status != 0
fail ExecFailed.new(command, exit_status, output)
else
return output
end
end
# Return current list of IPv4 addresses attachet to the host
def host_ips
return host_ips_from_env if host_ips_from_env.any?
host_ips_from_iface(main_iface)
end
# Return the array of the hosts, if any set by 'POLLER_HOST_IP'
def host_ips_from_env
@env_host_ips ||= ENV.fetch('POLLER_HOST_IP', '').split(/\s*,\s*/)
end
# Return array of IPv4 addresses on the
def host_ips_from_iface(iface_name)
ipv4_ifaddrs.find_all { |iface| iface.name == iface_name }
.map {|iface| iface.addr.ip_address }
end
# Returns the name of the currenly selected interface
def main_iface
@main_iface ||= ENV.fetch('POLLER_IFACE', first_interface_name)
end
# Returns the name of the first interface with non-loopback IPv4 address
def first_interface_name
@first_interface_name ||= ipv4_ifaddrs.map(&:name).uniq.first
end
def ipv4_ifaddrs
Socket.getifaddrs.find_all do |iface|
addr = iface.addr
addr.ipv4? && !addr.ipv4_loopback?
end
end
# Check that we got at least one address.
def check_host_ip
log "Detecting host IP addresses to attach chain rules..."
if host_ips.empty?
log "Can't detect IPv4 addresses on #{first_interface_name}."
log <<-ERROR
Set `POLLER_HOST_IP` enviroment variable to explicitly specify host IP
addresses. Multiple addresses can be specified and separated by comma
For example:
POLLER_HOST_IP= "10.0.0.1, 10.0.0.2"
Set `POLLER_IFACE` enviroment variable to specify the specific interface,
where IP addresses should be detected.
Note: IPv6 and loopback IP's are ignored.
ERROR
exit 1
end
if host_ips_from_env.any?
log "\tUsing POLLER_HOST_IP: #{host_ips.join(", ")}"
else
log "\tUsing '#{main_iface}': #{host_ips.join(", ")}"
end
end
# Check that we can access CATTLE_PREROUTING chain
def check_cattle
log "Checking #{CATTLE_CHAIN} iptables chain..."
raw_cattle_prerouting
log "\tDone. Cattle is available"
end
def check_sysctl
log 'Checking host configuration...'
SYSCTL_KEYS.each do |key, value|
result = exec_safely("sysctl -n #{key}").chomp
next if result == value
log "\t#{key} is not '#{value}'. Please set this key in /etc/sysctl.conf"
log "\tor in /etc/sysctl.d/<xx>-<name>.conf and reboot the host."
log "\tOther required keys: #{SYSCTL_KEYS.keys.join(', ')}"
exit 1
end
log "\tDone. All keys are set"
end
def raw_cattle_prerouting
exec_safely CATTLE_CHAIN_COMMAND
rescue ExecFailed => failure
log "Can't access CATTLE_PREROUTING iptables chain."
log "\tEnsure that container started with --privileged and try again."
log "\tTried with #{failure.command}, exit code #{failure.exit_code}, log:"
puts failure.output
exit 1
end
def cattle_prerouting
lines = raw_cattle_prerouting.split("\n")
_chain_name = lines.shift.split.last
# It will fail on unknown options, but let it fail.
rules = lines.map do |rule|
IPTables::DNAT.parse(rule)
end
rules
.reject { |rule| INGORE_PORTS.include? rule.source_port }
.sort_by { |rule| rule.source_port.to_i }
end
def create_chains
log 'Cleaning and creating chains...'
# We don't actually care about failures here
`iptables -t nat -N #{POLLER_OUTPUT_CHAIN} 2>&1`
`iptables -t nat -N #{POLLER_POSTROUTING_CHAIN} 2>&1`
# Flush if anything exist before
exec_safely "iptables -t nat -F #{POLLER_OUTPUT_CHAIN}"
exec_safely "iptables -t nat -F #{POLLER_POSTROUTING_CHAIN}"
# Reference to the OUTPUT/POSTROUTING chain
exec_safely "iptables -t nat -A OUTPUT -j #{POLLER_OUTPUT_CHAIN}"
exec_safely "iptables -t nat -A POSTROUTING -j #{POLLER_POSTROUTING_CHAIN}"
end
def flush_chains
debug "Flushing chains: #{POLLER_OUTPUT_CHAIN}, #{POLLER_POSTROUTING_CHAIN}"
exec_safely "iptables -t nat -F #{POLLER_OUTPUT_CHAIN}"
exec_safely "iptables -t nat -F #{POLLER_POSTROUTING_CHAIN}"
end
def update_rules(new_set)
flush_chains
# Maybe it's better to generate file and feed it to iptables at once with
# COMMIT command. This way some connections may fail while rules are updated.
host_ips.each do |host_ip|
new_set.each do |rule|
rule = rule.to_h.merge host_ip: host_ip
out_rule = format(POLLER_OUTPUT, rule)
post_rule = format(POLLER_POSTROUTING, rule)
exec_safely "iptables -t nat -A #{POLLER_OUTPUT_CHAIN} #{out_rule}"
exec_safely "iptables -t nat -A #{POLLER_POSTROUTING_CHAIN} #{post_rule}"
end
end
log 'iptables chain updated'
rescue ExecFailed => failure
log "Can't set iptables rules. Tried #{failure.command}, failed with: "
puts failure.output
exit 4
end
log "Starting Rancher iptables poller #{POLLER_VERSION}..."
check_host_ip
check_cattle
check_sysctl
create_chains
@last_set = []
@last_ips = host_ips
@running = true
@delay = 1.0
log "Started with polling #{@delay}s delay"
trap_callback = proc do
puts
log 'Stopping ...'
@running = false
end
Signal.trap('INT', &trap_callback)
Signal.trap('TERM', &trap_callback)
while @running
current_set = cattle_prerouting
if current_set != @last_set
log 'Chain changed:'
log "\tBefore: [#{@last_set.join(', ')}]"
log "\t After: [#{current_set.join(', ')}]"
update_rules current_set
@last_set = current_set
end
if host_ips != @last_ips
log 'IPs changed:'
log "\tBefore: [#{@last_ips.join(', ')}]"
log "\t After: [#{host_ips.join(', ')}]"
update_rules current_set
@last_ips = host_ips
end
sleep @delay
end
log 'Exited.'