Skip to content

Commit

Permalink
Merge pull request #241 from chef/chris-rock/port-centos
Browse files Browse the repository at this point in the history
add port support for centos
  • Loading branch information
arlimus committed Nov 17, 2015
2 parents 6b2d4e2 + c6166e3 commit 98d27dd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/resources/port.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def initialize(port)
@cache = nil

case inspec.os[:family]
when 'ubuntu', 'debian', 'redhat', 'fedora', 'arch'
when 'ubuntu', 'debian', 'redhat', 'fedora', 'centos', 'arch'
@port_manager = LinuxPorts.new(inspec)
when 'darwin'
@port_manager = DarwinPorts.new(inspec)
Expand Down Expand Up @@ -179,7 +179,7 @@ def info
def parse_net_address(net_addr, protocol)
if protocol.eql?('tcp6') || protocol.eql?('udp6')
# prep for URI parsing, parse ip6 port
ip6 = /^(\S+:)(\d+)$/.match(net_addr)
ip6 = /^(\S+):(\d+)$/.match(net_addr)
ip6addr = ip6[1]
ip6addr = '::' if /^:::$/.match(ip6addr)
# build uri
Expand All @@ -193,16 +193,25 @@ def parse_net_address(net_addr, protocol)
port = ip_addr.port
end
[host, port]
rescue URI::InvalidURIError => e
warn "Could not parse #{net_addr}, #{e}"
nil
end

def parse_netstat_line(line)
# parse each line
# 1 - Proto, 2 - Recv-Q, 3 - Send-Q, 4 - Local Address, 5 - Foreign Address, 6 - State, 7 - Inode, 8 - PID/Program name
parsed = /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)$/.match(line)
return {} if parsed.nil?
parsed = /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/.match(line)

return {} if parsed.nil? || line.match(/^proto/i)

# parse ip4 and ip6 addresses
protocol = parsed[1].downcase

# detect protocol if not provided
protocol += '6' if parsed[4].count(':') > 1 && %w{tcp udp}.include?(protocol)

# extract host and port information
host, port = parse_net_address(parsed[4], protocol)

# extract PID
Expand Down Expand Up @@ -261,6 +270,9 @@ def parse_net_address(net_addr, protocol)
port = ip_addr.port
end
[host, port]
rescue URI::InvalidURIError => e
warn "Could not parse #{net_addr}, #{e}"
nil
end

def parse_sockstat_line(line)
Expand Down
9 changes: 9 additions & 0 deletions test/integration/test/integration/default/port_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# encoding: utf-8

if os.unix?
# check that ssh runs
describe port(22) do
it { should be_listening }
its('protocol') { should include('tcp') }
end
end

0 comments on commit 98d27dd

Please sign in to comment.