New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes #14158 - Use tailoring file for scan #18
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,10 +9,13 @@ module ForemanScapClient | |
CONFIG_FILE = '/etc/foreman_scap_client/config.yaml' | ||
|
||
class Client | ||
attr_reader :config, :policy_id, :tailored | ||
|
||
def run(policy_id) | ||
@policy_id = policy_id | ||
ensure_policy_exist | ||
load_config | ||
ensure_scan_file | ||
ensure_tailoring_file | ||
Dir.mktmpdir do |dir| | ||
@tmp_dir = dir | ||
scan | ||
|
@@ -23,8 +26,10 @@ def run(policy_id) | |
|
||
private | ||
|
||
def config | ||
def load_config | ||
@config ||= YAML.load_file(CONFIG_FILE) | ||
ensure_policy_exist | ||
@tailored = !@config[policy_id][:tailoring_path].empty? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should also handle the case that this option is not there yet (so can be |
||
rescue => e | ||
puts 'Config file could not be loaded' | ||
puts e.message | ||
|
@@ -57,7 +62,11 @@ def scan_command | |
else | ||
profile = '' | ||
end | ||
"oscap xccdf eval #{profile} --results-arf #{results_path} #{config[@policy_id][:content_path]}" | ||
"oscap xccdf eval #{profile} #{tailoring_subcommand} --results-arf #{results_path} #{config[@policy_id][:content_path]}" | ||
end | ||
|
||
def tailoring_subcommand | ||
tailored ? "--tailoring-file #{config[policy_id][:tailoring_path]}" : "" | ||
end | ||
|
||
def bzip_command | ||
|
@@ -125,25 +134,34 @@ def ensure_policy_exist | |
end | ||
end | ||
|
||
def ensure_scan_file | ||
return if File.exist?(config[@policy_id][:content_path]) | ||
puts "File #{config[@policy_id][:content_path]} is missing. Downloading it from proxy" | ||
def ensure_file(dir, download_path, type_humanized) | ||
return if File.exist?(config[policy_id][dir]) | ||
puts "File #{config[policy_id][dir]} is missing. Downloading it from proxy." | ||
begin | ||
FileUtils.mkdir_p(File.dirname(config[@policy_id][:content_path])) | ||
uri = URI.parse(download_uri(config[@policy_id][:download_path])) | ||
puts "Download scap content xml from: #{uri}" | ||
FileUtils.mkdir_p(File.dirname(config[policy_id][dir])) | ||
uri = URI.parse(download_uri(config[policy_id][download_path])) | ||
puts "Download #{type_humanized} xml from: #{uri}" | ||
request = generate_https_object(uri).get(uri.path) | ||
request.value | ||
scap_content_xml = request.body | ||
open(config[@policy_id][:content_path], 'wb') do |file| | ||
file << scap_content_xml | ||
ds_content_xml = request.body | ||
open(config[policy_id][dir], 'wb') do |file| | ||
file << ds_content_xml | ||
end | ||
rescue StandardError => e | ||
puts "SCAP file is missing and download failed with error: #{e.message}" | ||
puts "#{type_humanized} is missing and download failed with error: #{e.message}" | ||
exit(5) | ||
end | ||
end | ||
|
||
def ensure_scan_file | ||
ensure_file :content_path, :download_path, "SCAP content" | ||
end | ||
|
||
def ensure_tailoring_file | ||
return unless tailored | ||
ensure_file :tailoring_path, :tailoring_download_path, "Tailoring file" | ||
end | ||
|
||
def download_uri(download_path) | ||
foreman_proxy_uri + "#{download_path}" | ||
end | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getting following, because ensure_policy_exist on line above requires config to be present already