Skip to content

Commit

Permalink
Placing all resources in the Inspec::Resources namespace
Browse files Browse the repository at this point in the history
Many of the resources are named as a top-level class with a fairly generic class name, such as "OS". This causes an issue specifically with kitchen-google which depends on a gem which depends on the "os" gem which itself defines an OS class with a different superclass. This prevents users from using TK, Google Compute, and Inspec without this fix.

Some mocked commands had their digest changed as well due to the new indentation, specifically in the User and RegistryKey classes.

I strongly recommend viewing this diff with `git diff --ignore-space-change`
to see the *real* changes. :)
  • Loading branch information
adamleff committed Mar 8, 2016
1 parent 139fa14 commit 98b9f18
Show file tree
Hide file tree
Showing 52 changed files with 4,461 additions and 4,359 deletions.
40 changes: 21 additions & 19 deletions lib/resources/apache.rb
Expand Up @@ -4,26 +4,28 @@
# author: Dominik Richter
# license: All rights reserved

class Apache < Inspec.resource(1)
name 'apache'
module Inspec::Resources
class Apache < Inspec.resource(1)
name 'apache'

attr_reader :service, :conf_dir, :conf_path, :user
def initialize
case inspec.os[:family]
when 'ubuntu', 'debian'
@service = 'apache2'
@conf_dir = '/etc/apache2/'
@conf_path = File.join @conf_dir, 'apache2.conf'
@user = 'www-data'
else
@service = 'httpd'
@conf_dir = '/etc/httpd/'
@conf_path = File.join @conf_dir, '/conf/httpd.conf'
@user = 'apache'
attr_reader :service, :conf_dir, :conf_path, :user
def initialize
case inspec.os[:family]
when 'ubuntu', 'debian'
@service = 'apache2'
@conf_dir = '/etc/apache2/'
@conf_path = ::File.join @conf_dir, 'apache2.conf'
@user = 'www-data'
else
@service = 'httpd'
@conf_dir = '/etc/httpd/'
@conf_path = ::File.join @conf_dir, '/conf/httpd.conf'
@user = 'apache'
end
end
end

def to_s
'Apache Environment'
def to_s
'Apache Environment'
end
end
end
end
184 changes: 93 additions & 91 deletions lib/resources/apache_conf.rb
Expand Up @@ -7,118 +7,120 @@
require 'utils/simpleconfig'
require 'utils/find_files'

class ApacheConf < Inspec.resource(1)
name 'apache_conf'
desc 'Use the apache_conf InSpec audit resource to test the configuration settings for Apache. This file is typically located under /etc/apache2 on the Debian and Ubuntu platforms and under /etc/httpd on the Fedora, CentOS, Red Hat Enterprise Linux, and Arch Linux platforms. The configuration settings may vary significantly from platform to platform.'
example "
describe apache_conf do
its('setting_name') { should eq 'value' }
end
"
module Inspec::Resources
class ApacheConf < Inspec.resource(1)
name 'apache_conf'
desc 'Use the apache_conf InSpec audit resource to test the configuration settings for Apache. This file is typically located under /etc/apache2 on the Debian and Ubuntu platforms and under /etc/httpd on the Fedora, CentOS, Red Hat Enterprise Linux, and Arch Linux platforms. The configuration settings may vary significantly from platform to platform.'
example "
describe apache_conf do
its('setting_name') { should eq 'value' }
end
"

include FindFiles
include FindFiles

def initialize(conf_path = nil)
@conf_path = conf_path || inspec.apache.conf_path
@conf_dir = File.dirname(@conf_path)
@files_contents = {}
@content = nil
@params = nil
read_content
end
def initialize(conf_path = nil)
@conf_path = conf_path || inspec.apache.conf_path
@conf_dir = ::File.dirname(@conf_path)
@files_contents = {}
@content = nil
@params = nil
read_content
end

def content
@content ||= read_content
end
def content
@content ||= read_content
end

def params(*opts)
@params || read_content
res = @params
opts.each do |opt|
res = res[opt] unless res.nil?
def params(*opts)
@params || read_content
res = @params
opts.each do |opt|
res = res[opt] unless res.nil?
end
res
end
res
end

def method_missing(name)
# ensure params are loaded
@params || read_content
def method_missing(name)
# ensure params are loaded
@params || read_content

# extract values
@params[name.to_s] unless @params.nil?
end
# extract values
@params[name.to_s] unless @params.nil?
end

def filter_comments(data)
content = ''
data.each_line do |line|
if !line.match(/^\s*#/)
content << line
def filter_comments(data)
content = ''
data.each_line do |line|
if !line.match(/^\s*#/)
content << line
end
end
content
end
content
end

def read_content
@content = ''
@params = {}
def read_content
@content = ''
@params = {}

# skip if the main configuration file doesn't exist
file = inspec.file(@conf_path)
if !file.file?
return skip_resource "Can't find file \"#{@conf_path}\""
end
# skip if the main configuration file doesn't exist
file = inspec.file(@conf_path)
if !file.file?
return skip_resource "Can't find file \"#{@conf_path}\""
end

raw_conf = file.content
if raw_conf.empty? && file.size > 0
return skip_resource("Can't read file \"#{@conf_path}\"")
end
raw_conf = file.content
if raw_conf.empty? && file.size > 0
return skip_resource("Can't read file \"#{@conf_path}\"")
end

to_read = [@conf_path]
until to_read.empty?
raw_conf = read_file(to_read[0])
@content += raw_conf

# parse include file parameters
params = SimpleConfig.new(
raw_conf,
assignment_re: /^\s*(\S+)\s+(.*)\s*$/,
multiple_values: true,
).params
@params.merge!(params)

to_read = to_read.drop(1)
to_read += include_files(params).find_all do |fp|
not @files_contents.key? fp
to_read = [@conf_path]
until to_read.empty?
raw_conf = read_file(to_read[0])
@content += raw_conf

# parse include file parameters
params = SimpleConfig.new(
raw_conf,
assignment_re: /^\s*(\S+)\s+(.*)\s*$/,
multiple_values: true,
).params
@params.merge!(params)

to_read = to_read.drop(1)
to_read += include_files(params).find_all do |fp|
not @files_contents.key? fp
end
end

# fiter comments
@content = filter_comments @content
@content
end

# fiter comments
@content = filter_comments @content
@content
end
def include_files(params)
# see if there is more config files to include
include_files = params['Include'] || []
include_files_optional = params['IncludeOptional'] || []

def include_files(params)
# see if there is more config files to include
include_files = params['Include'] || []
include_files_optional = params['IncludeOptional'] || []
includes = []
(include_files + include_files_optional).each do |f|
id = ::File.join(@conf_dir, f)
files = find_files(id, depth: 1, type: 'file')

includes = []
(include_files + include_files_optional).each do |f|
id = File.join(@conf_dir, f)
files = find_files(id, depth: 1, type: 'file')
includes.push(files) if files
end

includes.push(files) if files
# [].flatten! == nil
includes.flatten! || []
end

# [].flatten! == nil
includes.flatten! || []
end

def read_file(path)
@files_contents[path] ||= inspec.file(path).content
end
def read_file(path)
@files_contents[path] ||= inspec.file(path).content
end

def to_s
"Apache Config #{@conf_path}"
def to_s
"Apache Config #{@conf_path}"
end
end
end
end

0 comments on commit 98b9f18

Please sign in to comment.