2015-09-06 15:01:28 +00:00
|
|
|
# encoding: utf-8
|
2015-10-06 16:55:44 +00:00
|
|
|
# author: Christoph Hartmann
|
|
|
|
# author: Dominik Richter
|
2015-09-06 15:01:28 +00:00
|
|
|
|
|
|
|
require 'resources/file'
|
|
|
|
|
|
|
|
# Usage:
|
|
|
|
# describe yum do
|
|
|
|
# its('repos') { should exist }
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# describe yum do
|
|
|
|
# its('repos') { should include 'base/7/x86_64' }
|
|
|
|
# its('epel') { should exist }
|
|
|
|
# its('epel') { should be_enabled }
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Filter for a specific repo by name
|
|
|
|
# - use full identifier e.g. 'updates/7/x86_64'
|
|
|
|
# - use short identifier e.g. 'updates'
|
|
|
|
#
|
|
|
|
# describe yum.repo('epel') do
|
|
|
|
# it { should exist }
|
|
|
|
# it { should be_enabled }
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# deprecated:
|
|
|
|
# describe yumrepo('epel') do
|
|
|
|
# it { should exist }
|
|
|
|
# it { should be_enabled }
|
|
|
|
# end
|
|
|
|
|
2015-10-26 03:04:18 +00:00
|
|
|
class Yum < Inspec.resource(1)
|
2015-10-10 17:54:45 +00:00
|
|
|
name 'yum'
|
2015-11-27 13:02:38 +00:00
|
|
|
desc 'Use the yum InSpec audit resource to test packages in the Yum repository.'
|
|
|
|
example "
|
|
|
|
describe yum.repo('name') do
|
|
|
|
it { should exist }
|
|
|
|
it { should be_enabled }
|
|
|
|
end
|
|
|
|
"
|
2015-10-10 17:54:45 +00:00
|
|
|
|
|
|
|
# returns all repositories
|
|
|
|
# works as following:
|
|
|
|
# search for Repo-id
|
|
|
|
# parse data in hashmap
|
|
|
|
# store data in object
|
|
|
|
# until \n
|
|
|
|
def repositories
|
|
|
|
return @cache if defined?(@cache)
|
|
|
|
# parse the repository data from yum
|
|
|
|
# we cannot use -C, because this is not reliable and may lead to errors
|
2015-10-26 03:04:18 +00:00
|
|
|
@command_result = inspec.command('yum -v repolist all')
|
2015-10-10 17:54:45 +00:00
|
|
|
@content = @command_result.stdout
|
|
|
|
@cache = []
|
|
|
|
repo = {}
|
|
|
|
in_repo = false
|
|
|
|
@content.each_line do |line|
|
|
|
|
# detect repo start
|
2016-01-15 02:59:00 +00:00
|
|
|
in_repo = true if line =~ /^\s*Repo-id\s*:\s*(.*)\b/
|
2015-10-10 17:54:45 +00:00
|
|
|
# detect repo end
|
|
|
|
if line == "\n" && in_repo
|
|
|
|
in_repo = false
|
|
|
|
@cache.push(repo)
|
|
|
|
repo = {}
|
|
|
|
end
|
|
|
|
# parse repo content
|
|
|
|
if in_repo == true
|
|
|
|
val = /^\s*([^:]*?)\s*:\s*(.*?)\s*$/.match(line)
|
|
|
|
repo[repo_key(strip(val[1]))] = strip(val[2])
|
2015-09-06 15:01:28 +00:00
|
|
|
end
|
|
|
|
end
|
2015-10-10 17:54:45 +00:00
|
|
|
@cache
|
|
|
|
end
|
2015-09-06 15:01:28 +00:00
|
|
|
|
2015-10-10 17:54:45 +00:00
|
|
|
def repos
|
|
|
|
repositories.map { |repo| repo['id'] }
|
|
|
|
end
|
2015-09-06 15:01:28 +00:00
|
|
|
|
2015-10-10 17:54:45 +00:00
|
|
|
def repo(repo)
|
|
|
|
YumRepo.new(self, repo)
|
|
|
|
end
|
2015-09-06 15:01:28 +00:00
|
|
|
|
2015-10-10 17:54:45 +00:00
|
|
|
# alias for yum.repo('reponame')
|
|
|
|
def method_missing(name)
|
|
|
|
repo(name.to_s) if !name.nil?
|
|
|
|
end
|
2015-09-06 15:01:28 +00:00
|
|
|
|
2015-10-10 17:54:45 +00:00
|
|
|
def to_s
|
|
|
|
'Yum Repository'
|
|
|
|
end
|
2015-10-12 11:01:58 +00:00
|
|
|
|
2015-10-10 17:54:45 +00:00
|
|
|
private
|
2015-09-06 15:01:28 +00:00
|
|
|
|
2015-10-10 17:54:45 +00:00
|
|
|
# Removes lefthand and righthand whitespace
|
|
|
|
def strip(value)
|
2016-01-15 02:59:00 +00:00
|
|
|
value.strip if !value.nil?
|
2015-10-10 17:54:45 +00:00
|
|
|
end
|
2015-09-06 15:01:28 +00:00
|
|
|
|
2015-10-10 17:54:45 +00:00
|
|
|
# Optimize the key value
|
|
|
|
def repo_key(key)
|
|
|
|
return key if key.nil?
|
|
|
|
key.gsub('Repo-', '').downcase
|
2015-09-06 15:01:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class YumRepo
|
|
|
|
def initialize(yum, reponame)
|
|
|
|
@yum = yum
|
|
|
|
@reponame = reponame
|
|
|
|
end
|
|
|
|
|
|
|
|
# extracts the shortname from a repo id
|
|
|
|
# e.g. extras/7/x86_64 -> extras
|
|
|
|
def shortname(id)
|
2015-09-09 16:52:27 +00:00
|
|
|
val = %r{^\s*([^/]*?)/(.*?)\s*$}.match(id)
|
2015-09-17 15:24:51 +00:00
|
|
|
val.nil? ? nil : val[1]
|
2015-09-06 15:01:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def info
|
2015-10-01 13:25:08 +00:00
|
|
|
return @cache if defined?(@cache)
|
2015-09-06 15:01:28 +00:00
|
|
|
selection = @yum.repositories.select { |e| e['id'] == @reponame || shortname(e['id']) == @reponame }
|
|
|
|
@cache = selection[0] if !selection.nil? && selection.length == 1
|
|
|
|
@cache
|
|
|
|
end
|
|
|
|
|
2015-09-18 10:44:29 +00:00
|
|
|
def exist?
|
2015-09-06 15:01:28 +00:00
|
|
|
!info.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
def enabled?
|
|
|
|
repo = info
|
|
|
|
return false if repo.nil?
|
|
|
|
info['status'] == 'enabled'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# for compatability with serverspec
|
|
|
|
# this is deprecated syntax and will be removed in future versions
|
2015-10-10 17:54:45 +00:00
|
|
|
class YumRepoLegacy < Yum
|
|
|
|
name 'yumrepo'
|
2015-09-06 15:01:28 +00:00
|
|
|
|
2015-10-10 17:54:45 +00:00
|
|
|
def initialize(name)
|
|
|
|
super()
|
|
|
|
@repository = repo(name)
|
|
|
|
end
|
2015-09-06 15:01:28 +00:00
|
|
|
|
2015-10-10 17:54:45 +00:00
|
|
|
def exists?
|
|
|
|
deprecated
|
|
|
|
@repository.exist?
|
|
|
|
end
|
2015-09-06 15:01:28 +00:00
|
|
|
|
2015-10-10 17:54:45 +00:00
|
|
|
def enabled?
|
|
|
|
deprecated
|
|
|
|
@repository.enabled?
|
|
|
|
end
|
2015-09-06 15:01:28 +00:00
|
|
|
|
2015-10-10 17:54:45 +00:00
|
|
|
def deprecated
|
|
|
|
warn '[DEPRECATION] `yumrepo(reponame)` is deprecated. Please use `yum.repo(reponame)` instead.'
|
2015-09-06 15:01:28 +00:00
|
|
|
end
|
|
|
|
end
|