Skip to content

Commit

Permalink
Fix detecting docker host (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
bongole authored Feb 15, 2023
1 parent 8c16049 commit ddfd3c1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
20 changes: 19 additions & 1 deletion lib/aws/rails/middleware/ebs_sqs_active_job_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,30 @@ def periodic_task?(request)
end

def sent_from_docker_host?(request)
app_runs_in_docker_container? && request.ip == '172.17.0.1'
app_runs_in_docker_container? && default_gw_ips.include?(request.ip)
end

def app_runs_in_docker_container?
@app_runs_in_docker_container ||= `[ -f /proc/1/cgroup ] && cat /proc/1/cgroup` =~ /docker/
end

def default_gw_ips
default_gw_ips = ['172.17.0.1']

if File.exist?('/proc/net/route')
File.open('/proc/net/route').each_line do |line|
fields = line.strip.split
next if fields.size != 11

# Destination == 0.0.0.0 and Flags & RTF_GATEWAY != 0
if fields[1] == '00000000' && (fields[3].hex & 0x2) != 0
default_gw_ips << IPAddr.new_ntoh([fields[2].hex].pack('L')).to_s
end
end
end

default_gw_ips
end
end
end
end
40 changes: 40 additions & 0 deletions test/aws/rails/middleware/ebs_sqs_active_job_middleware_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,46 @@ module Rails
expect(response[0]).to eq(500)
end

it 'successfully invokes job when docker default gateway ip is changed' do
mock_rack_env = create_mock_env('192.168.176.1', 'aws-sqsd/1.1', false)
test_middleware = EbsSqsActiveJobMiddleware.new(mock_rack_app)

proc_net_route = <<~CONTENT
Iface\tDestination\tGateway\tFlags\tRefCnt\tUse\tMetric\tMask\tMTU\tWindow\tIRTT
eth0\t00000000\t01B0A8C0\t0003\t0\t0\t0\t00000000\t0\t0\t0
eth0\t00B0A8C0\t00000000\t0001\t0\t0\t0\t00F0FFFF\t0\t0\t0
CONTENT

allow(File).to receive(:exist?).and_call_original
allow(File).to receive(:open).and_call_original

expect(File).to receive(:exist?).with('/proc/net/route').and_return(true)
expect(File).to receive(:open).with('/proc/net/route').and_return(StringIO.new(proc_net_route))
expect(test_middleware).to receive(:app_runs_in_docker_container?).and_return(true)

response = test_middleware.call(mock_rack_env)

expect(response[0]).to eq(200)
expect(response[1]['Content-Type']).to eq('text/plain')
expect(response[2]).to eq(['Successfully ran job ElasticBeanstalkJob.'])
end

it 'successfully invokes job when /proc/net/route does not exist' do
mock_rack_env = create_mock_env('172.17.0.1', 'aws-sqsd/1.1', false)
test_middleware = EbsSqsActiveJobMiddleware.new(mock_rack_app)

allow(File).to receive(:exist?).and_call_original

expect(File).to receive(:exist?).with('/proc/net/route').and_return(false)
expect(test_middleware).to receive(:app_runs_in_docker_container?).and_return(true)

response = test_middleware.call(mock_rack_env)

expect(response[0]).to eq(200)
expect(response[1]['Content-Type']).to eq('text/plain')
expect(response[2]).to eq(['Successfully ran job ElasticBeanstalkJob.'])
end

# Create a minimal mock Rack environment hash to test just what we need
def create_mock_env(source_ip, user_agent, is_periodic_task = false)
mock_env = {
Expand Down

0 comments on commit ddfd3c1

Please sign in to comment.