2015-04-09 20:01:23 +00:00
# encoding: utf-8
2015-07-15 13:15:18 +00:00
# copyright: 2015, Vulcano Security GmbH
2015-04-09 20:01:23 +00:00
require 'utils/simpleconfig'
2016-03-08 18:06:55 +00:00
module Inspec::Resources
class SshConf < Inspec . resource ( 1 )
name 'ssh_config'
2018-02-19 14:26:49 +00:00
supports platform : 'unix'
2017-04-18 10:01:55 +00:00
desc 'Use the `ssh_config` InSpec audit resource to test OpenSSH client configuration data located at `/etc/ssh/ssh_config` on Linux and Unix platforms.'
2016-03-08 18:06:55 +00:00
example "
2017-04-18 10:01:55 +00:00
describe ssh_config do
its ( 'cipher' ) { should contain '3des' }
its ( 'port' ) { should eq '22' }
its ( 'hostname' ) { should include ( 'example.com' ) }
2016-03-08 18:06:55 +00:00
end
"
def initialize ( conf_path = nil , type = nil )
@conf_path = conf_path || '/etc/ssh/ssh_config'
typename = ( @conf_path . include? ( 'sshd' ) ? 'Server' : 'Client' )
@type = type || " SSH #{ typename } configuration #{ conf_path } "
2015-11-27 13:02:38 +00:00
end
2015-04-09 20:01:23 +00:00
2016-03-08 18:06:55 +00:00
def content
read_content
end
2015-04-09 20:01:23 +00:00
2016-03-08 18:06:55 +00:00
def params ( * opts )
opts . inject ( read_params ) do | res , nxt |
res . respond_to? ( :key ) ? res [ nxt ] : nil
end
end
2015-04-09 20:01:23 +00:00
2016-08-15 20:03:44 +00:00
def convert_hash ( hash )
new_hash = { }
2016-08-15 21:39:28 +00:00
hash . each do | k , v |
2016-08-15 20:03:44 +00:00
new_hash [ k . downcase ] = v
end
new_hash
end
2016-03-08 18:06:55 +00:00
def method_missing ( name )
2016-08-15 20:03:44 +00:00
param = read_params [ name . to_s . downcase ]
2016-03-08 18:06:55 +00:00
return nil if param . nil?
# extract first value if we have only one value in array
return param [ 0 ] if param . length == 1
param
2015-04-09 20:01:23 +00:00
end
2016-03-08 18:06:55 +00:00
def to_s
'SSH Configuration'
end
2015-04-09 20:01:23 +00:00
2016-03-08 18:06:55 +00:00
private
2015-10-12 11:01:58 +00:00
2016-03-08 18:06:55 +00:00
def read_content
return @content if defined? ( @content )
file = inspec . file ( @conf_path )
if ! file . file?
return skip_resource " Can't find file \" #{ @conf_path } \" "
end
2015-09-01 22:50:52 +00:00
2016-03-08 18:06:55 +00:00
@content = file . content
2017-10-06 13:41:48 +00:00
if @content . nil? || ( @content . empty? && ! file . size . zero? )
2016-03-08 18:06:55 +00:00
return skip_resource " Can't read file \" #{ @conf_path } \" "
end
2015-08-28 03:02:38 +00:00
2016-03-08 18:06:55 +00:00
@content
2015-06-21 14:33:08 +00:00
end
2015-08-28 03:02:38 +00:00
2016-03-08 18:06:55 +00:00
def read_params
return @params if defined? ( @params )
return @params = { } if read_content . nil?
conf = SimpleConfig . new (
read_content ,
2017-04-26 21:18:14 +00:00
assignment_regex : / ^ \ s*( \ S+?) \ s+(.*?) \ s*$ / ,
2016-03-08 18:06:55 +00:00
multiple_values : true ,
)
2016-08-15 20:03:44 +00:00
@params = convert_hash ( conf . params )
2016-03-08 18:06:55 +00:00
end
2015-04-09 20:01:23 +00:00
end
2016-03-08 18:06:55 +00:00
class SshdConf < SshConf
name 'sshd_config'
2018-02-19 14:26:49 +00:00
supports platform : 'unix'
2017-04-18 10:01:55 +00:00
desc 'Use the sshd_config InSpec audit resource to test configuration data for the Open SSH daemon located at /etc/ssh/sshd_config on Linux and UNIX platforms. sshd---the Open SSH daemon---listens on dedicated ports, starts a daemon for each incoming connection, and then handles encryption, authentication, key exchanges, command execution, and data exchanges.'
example "
describe sshd_config do
its ( 'Protocol' ) { should eq '2' }
end
"
2015-08-28 17:10:03 +00:00
2016-03-08 18:06:55 +00:00
def initialize ( path = nil )
super ( path || '/etc/ssh/sshd_config' )
end
2017-04-18 10:01:55 +00:00
def to_s
'SSHD Configuration'
end
2015-08-01 07:43:38 +00:00
end
end