mirror of
https://github.com/inspec/inspec
synced 2024-11-30 00:20:28 +00:00
implement port resource for linux
This commit is contained in:
parent
f505148abe
commit
c187230336
5 changed files with 175 additions and 0 deletions
145
lib/resources/port.rb
Normal file
145
lib/resources/port.rb
Normal file
|
@ -0,0 +1,145 @@
|
|||
# encoding: utf-8
|
||||
|
||||
# Usage:
|
||||
# describe port(80) do
|
||||
# it { should be_listening }
|
||||
# its('protocol') {should eq 'tcp'}
|
||||
# end
|
||||
#
|
||||
# not supported serverspec syntax
|
||||
# describe port(80) do
|
||||
# it { should be_listening.with('tcp') }
|
||||
# end
|
||||
#
|
||||
# TODO: currently we return local ip only
|
||||
# TODO: improve handling of same port on multiple interfaces
|
||||
class Port < Vulcano.resource(1)
|
||||
name 'port'
|
||||
|
||||
def initialize(port)
|
||||
@port = port
|
||||
@port_manager = nil
|
||||
@cache = nil
|
||||
|
||||
case vulcano.os[:family]
|
||||
when 'ubuntu', 'debian', 'redhat', 'fedora', 'arch'
|
||||
@port_manager = LinuxPorts.new(vulcano)
|
||||
else
|
||||
return skip_resource 'The `port` resource is not supported on your OS yet.'
|
||||
end
|
||||
end
|
||||
|
||||
def listening?(_protocol = nil, _local_address = nil)
|
||||
ports = info
|
||||
return false if ports.nil?
|
||||
match = ports.select { |p| p[:port] == @port }
|
||||
match.size > 0 ? true : false
|
||||
end
|
||||
|
||||
def protocol
|
||||
ports = info
|
||||
(ports.size > 0) ? ports[0][:protocol] : nil
|
||||
end
|
||||
|
||||
def process
|
||||
ports = info
|
||||
(ports.size > 0) ? ports[0][:process] : nil
|
||||
end
|
||||
|
||||
def pid
|
||||
ports = info
|
||||
(ports.size > 0) ? ports[0][:pid] : nil
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def info
|
||||
return @cache if !@cache.nil?
|
||||
# abort if os detection has not worked
|
||||
return @cache = [] if @port_manager.nil?
|
||||
# query ports
|
||||
ports = @port_manager.info
|
||||
if ports.nil?
|
||||
@cache = []
|
||||
else
|
||||
@cache = ports.select { |p| p[:port] == @port }
|
||||
end
|
||||
@cache
|
||||
end
|
||||
end
|
||||
|
||||
# implements an info method and returns all ip adresses and protocols for
|
||||
# each port
|
||||
# [{
|
||||
# port: 80,
|
||||
# address: [{
|
||||
# ip: '0.0.0.0'
|
||||
# protocol: 'tcp'
|
||||
# }],
|
||||
# }]
|
||||
class PortsInfo
|
||||
def initialize(vulcano)
|
||||
@vulcano = vulcano
|
||||
end
|
||||
end
|
||||
|
||||
# extract port information from netstat
|
||||
class LinuxPorts < PortsInfo
|
||||
def info
|
||||
cmd = @vulcano.run_command('netstat -tulpen')
|
||||
return nil if cmd.exit_status.to_i != 0
|
||||
|
||||
ports = []
|
||||
# split on each newline
|
||||
cmd.stdout.each_line do |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)
|
||||
|
||||
if !parsed.nil?
|
||||
protocol = parsed[1].downcase
|
||||
|
||||
# parse ip4 and ip6 addresses
|
||||
net_addr = parsed[4]
|
||||
if protocol.eql?('tcp6') || protocol.eql?('udp6')
|
||||
# prep for URI parsing, parse ip6 port
|
||||
ip6 = /^(\S+:)(\d+)$/.match(net_addr)
|
||||
ip6addr = ip6[1]
|
||||
ip6addr = '::' if /^:::$/.match(ip6addr)
|
||||
# build uri
|
||||
ip_addr = URI('addr://[' + ip6addr +']:' + ip6[2])
|
||||
# replace []
|
||||
host = ip_addr.host[1..ip_addr.host.size-2]
|
||||
port = ip_addr.port
|
||||
else
|
||||
ip_addr = URI('addr://'+net_addr)
|
||||
host = ip_addr.host
|
||||
port = ip_addr.port
|
||||
end
|
||||
|
||||
# extract PID
|
||||
process = parsed[9].split('/')
|
||||
pid = process[0]
|
||||
pid = pid.to_i if /^\d+$/.match(pid)
|
||||
process = process[1]
|
||||
|
||||
# map tcp6 and udp6
|
||||
protocol = 'tcp' if protocol.eql?('tcp6')
|
||||
protocol = 'udp' if protocol.eql?('udp6')
|
||||
|
||||
# map data
|
||||
port_info = {
|
||||
port: port,
|
||||
address: host,
|
||||
protocol: protocol,
|
||||
process: process,
|
||||
pid: pid,
|
||||
}
|
||||
|
||||
# push data, if not headerfile
|
||||
ports.push(port_info) if protocol.eql?('tcp') || protocol.eql?('udp')
|
||||
end
|
||||
end
|
||||
ports
|
||||
end
|
||||
end
|
|
@ -47,6 +47,7 @@ require 'resources/package'
|
|||
require 'resources/parse_config'
|
||||
require 'resources/passwd'
|
||||
require 'resources/pip'
|
||||
require 'resources/port'
|
||||
require 'resources/postgres'
|
||||
require 'resources/postgres_conf'
|
||||
require 'resources/postgres_session'
|
||||
|
|
|
@ -97,6 +97,7 @@ class MockLoader
|
|||
"Get-WindowsFeature | Where-Object {$_.Name -eq 'dhcp' -or $_.DisplayName -eq 'dhcp'} | Select-Object -Property Name,DisplayName,Description,Installed,InstallState | ConvertTo-Json" => cmd.call('get-windows-feature'),
|
||||
'lsmod' => cmd.call('lsmod'),
|
||||
'/sbin/sysctl -q -n net.ipv4.conf.all.forwarding' => cmd.call('sbin_sysctl'),
|
||||
'netstat -tulpen' => cmd.call('netstat-tulpen'),
|
||||
}
|
||||
|
||||
# set os emulation
|
||||
|
|
5
test/unit/mock/cmd/netstat-tulpen
Normal file
5
test/unit/mock/cmd/netstat-tulpen
Normal file
|
@ -0,0 +1,5 @@
|
|||
Active Internet connections (only servers)
|
||||
Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name
|
||||
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 0 21699 1/sshd
|
||||
tcp6 0 48 2601:1:ad80:1445::54776 2620:0:861:52:208::6667 LISTEN 0 21702 2043/pidgin
|
||||
tcp6 0 0 :::22 :::* LISTEN 0 21701 1/sshd
|
23
test/unit/resource_port_test.rb
Normal file
23
test/unit/resource_port_test.rb
Normal file
|
@ -0,0 +1,23 @@
|
|||
# encoding: utf-8
|
||||
|
||||
require 'helper'
|
||||
require 'vulcano/resource'
|
||||
|
||||
describe 'Vulcano::Resources::Port' do
|
||||
|
||||
it 'verify port on Ubuntu 14.04' do
|
||||
resource = MockLoader.new(:ubuntu1404).load_resource('port', 22)
|
||||
_(resource.listening?).must_equal true
|
||||
_(resource.protocol).must_equal 'tcp'
|
||||
_(resource.pid).must_equal 1
|
||||
_(resource.process).must_equal 'sshd'
|
||||
end
|
||||
|
||||
it 'verify running on undefined' do
|
||||
resource = MockLoader.new(:undefined).load_resource('port', 22)
|
||||
_(resource.listening?).must_equal false
|
||||
_(resource.protocol).must_equal nil
|
||||
_(resource.pid).must_equal nil
|
||||
_(resource.process).must_equal nil
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue