smart_proxy_openscap 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,71 @@
1
+ require 'test_helper'
2
+ require 'smart_proxy_openscap'
3
+ require 'smart_proxy_openscap/openscap_api'
4
+
5
+ ENV['RACK_ENV'] = 'test'
6
+
7
+ class FetchScapApiTest < Test::Unit::TestCase
8
+ include Rack::Test::Methods
9
+
10
+ def setup
11
+ @foreman_url = 'https://foreman.example.com'
12
+ Proxy::SETTINGS.stubs(:foreman_url).returns(@foreman_url)
13
+ @results_path = ("#{Dir.getwd}/test/test_run_files")
14
+ FileUtils.mkdir_p(@results_path)
15
+ Proxy::OpenSCAP::Plugin.settings.stubs(:contentdir).returns(@results_path)
16
+ Proxy::OpenSCAP::Plugin.settings.stubs(:spooldir).returns(@results_path)
17
+ Proxy::OpenSCAP::Plugin.settings.stubs(:reportsdir).returns(@results_path)
18
+ @scap_content = File.new("#{Dir.getwd}/test/data/ssg-rhel7-ds.xml").read
19
+ @policy_id = 1
20
+ end
21
+
22
+ def teardown
23
+ FileUtils.rm_rf(Dir.glob("#{@results_path}/*"))
24
+ end
25
+
26
+ def app
27
+ ::Proxy::OpenSCAP::Api.new
28
+ end
29
+
30
+ def test_get_scap_content_from_foreman
31
+ stub_request(:get, "#{@foreman_url}/api/v2/compliance/policies/#{@policy_id}/content").to_return(:body => @scap_content)
32
+ get "/policies/#{@policy_id}/content"
33
+ assert_equal("application/xml;charset=utf-8", last_response.header["Content-Type"], "Response header should be application/xml")
34
+ assert File.file?("#{@results_path}/#{@policy_id}/#{@policy_id}_scap_content.xml")
35
+ assert_equal(@scap_content.length, last_response.length, "Scap content should be equal")
36
+ end
37
+
38
+ def test_get_scap_content_from_file
39
+ # Simulate that scap file was previously saved after fetched from Foreman.
40
+ FileUtils.mkdir("#{@results_path}/#{@policy_id}")
41
+ FileUtils.cp("#{Dir.getwd}/test/data/ssg-rhel7-ds.xml", "#{@results_path}/#{@policy_id}/#{@policy_id}_scap_content.xml")
42
+ get "/policies/#{@policy_id}/content"
43
+ assert_equal("application/xml;charset=utf-8", last_response.header["Content-Type"], "Response header should be application/xml")
44
+ assert_equal(@scap_content.length, last_response.length, "Scap content should be equal")
45
+ assert(last_response.successful?, "Response should be success")
46
+ end
47
+
48
+ def test_get_scap_content_no_policy
49
+ stub_request(:get, "#{@foreman_url}/api/v2/compliance/policies/#{@policy_id}/content").to_return(:status => 404, :body => 'not found')
50
+ get "/policies/#{@policy_id}/content"
51
+ assert(last_response.not_found?, "Response should be 404")
52
+ end
53
+
54
+ def test_get_scap_content_permissions
55
+ Proxy::OpenSCAP::FetchScapContent.any_instance.stubs(:get_policy_content).raises(Errno::EACCES)
56
+ stub_request(:get, "#{@foreman_url}/api/v2/compliance/policies/#{@policy_id}/content").to_return(:body => @scap_content)
57
+ get "/policies/#{@policy_id}/content"
58
+ assert_equal(500, last_response.status, "No permissions should raise error 500")
59
+ assert_equal('Error occurred: Permission denied', last_response.body)
60
+ end
61
+
62
+ def test_locked_file_should_serve_from_foreman
63
+ Proxy::FileLock.stubs(:try_locking).returns(nil)
64
+ stub_request(:get, "#{@foreman_url}/api/v2/compliance/policies/#{@policy_id}/content").to_return(:body => @scap_content)
65
+ get "/policies/#{@policy_id}/content"
66
+ refute(File.file?("#{@results_path}/#{@policy_id}/#{@policy_id}_scap_content.xml"), "Scap file should be saved")
67
+ assert_equal("application/xml;charset=utf-8", last_response.header["Content-Type"], "Response header should be application/xml")
68
+ assert_equal(@scap_content.length, last_response.length, "Scap content should be equal")
69
+ assert(last_response.successful?, "Response should be success")
70
+ end
71
+ end
@@ -0,0 +1,52 @@
1
+ require 'test_helper'
2
+ require 'smart_proxy_openscap'
3
+ require 'smart_proxy_openscap/openscap_api'
4
+
5
+ ENV['RACK_ENV'] = 'test'
6
+
7
+ class OpenSCAPGetArfTest < Test::Unit::TestCase
8
+ include Rack::Test::Methods
9
+
10
+ def setup
11
+ @foreman_url = 'https://foreman.example.com'
12
+ Proxy::SETTINGS.stubs(:foreman_url).returns(@foreman_url)
13
+ @results_path = ("#{Dir.getwd}/test/test_run_files")
14
+ Proxy::OpenSCAP::Plugin.settings.stubs(:reportsdir).returns(@results_path + "/reports")
15
+ @arf_report = File.open("#{Dir.getwd}/test/data/arf_report").read
16
+ @policy_id = 1
17
+ @arf_id = 145
18
+ @filename = Digest::SHA256.hexdigest(@arf_report)
19
+ @cname = 'node.example.org'
20
+ @date = Time.now.strftime("%Y-%m-%d")
21
+ # Bypass common_name as it requires ssl certificate
22
+ Proxy::OpenSCAP.stubs(:common_name).returns(@cname)
23
+ FileUtils.mkdir_p("#{@results_path}/reports/arf/#{@cname}/#{@arf_id}/#{@date}")
24
+ FileUtils.cp("#{Dir.getwd}/test/data/arf_report", "#{@results_path}/reports/arf/#{@cname}/#{@arf_id}/#{@date}/#{@filename}")
25
+ end
26
+
27
+ def teardown
28
+ FileUtils.rm_rf(Dir.glob("#{@results_path}/*"))
29
+ end
30
+
31
+ def app
32
+ ::Proxy::OpenSCAP::Api.new
33
+ end
34
+
35
+ def test_get_xml_arf
36
+ get "/arf/#{@arf_id}/#{@cname}/#{@date}/#{@filename}/xml"
37
+ assert(last_response.successful?, "Should return OK")
38
+ assert(last_response.header["Content-Type"].include?('application/x-bzip2'))
39
+ end
40
+
41
+ def test_get_html_arf
42
+ get "/arf/#{@arf_id}/#{@cname}/#{@date}/#{@filename}/html"
43
+ assert(last_response.successful?, "Should return OK")
44
+ assert(last_response.body.start_with?('<!DOCTYPE'), 'File should start with html')
45
+ end
46
+
47
+ def test_get_xml_file_not_found
48
+ get "/arf/#{@arf_id}/somewhere.example.org/#{@date}/#{@filename}/xml"
49
+ assert_equal(500, last_response.status, "Error response should be 500")
50
+ assert(last_response.server_error?)
51
+ end
52
+ end
@@ -0,0 +1,75 @@
1
+ require 'test_helper'
2
+ require 'smart_proxy_openscap'
3
+ require 'smart_proxy_openscap/openscap_api'
4
+
5
+ ENV['RACK_ENV'] = 'test'
6
+
7
+ class OpenSCAPApiTest < Test::Unit::TestCase
8
+ include Rack::Test::Methods
9
+
10
+ def setup
11
+ @foreman_url = 'https://foreman.example.com'
12
+ Proxy::SETTINGS.stubs(:foreman_url).returns(@foreman_url)
13
+ @results_path = ("#{Dir.getwd}/test/test_run_files")
14
+ FileUtils.mkdir_p(@results_path)
15
+ Proxy::OpenSCAP::Plugin.settings.stubs(:contentdir).returns(@results_path)
16
+ Proxy::OpenSCAP::Plugin.settings.stubs(:spooldir).returns(@results_path + "/spool")
17
+ Proxy::OpenSCAP::Plugin.settings.stubs(:reportsdir).returns(@results_path + "/reports")
18
+ Proxy::OpenSCAP::Plugin.settings.stubs(:failed_dir).returns(@results_path + "/failed")
19
+ @arf_report = File.open("#{Dir.getwd}/test/data/arf_report").read
20
+ @policy_id = 1
21
+ @arf_id = 145
22
+ @filename = Digest::SHA256.hexdigest(@arf_report)
23
+ @cname = 'node.example.org'
24
+ @date = Time.now.to_i
25
+ # Bypass common_name as it requires ssl certificate
26
+ Proxy::OpenSCAP.stubs(:common_name).returns(@cname)
27
+ end
28
+
29
+ def teardown
30
+ FileUtils.rm_rf(Dir.glob("#{@results_path}/*"))
31
+ end
32
+
33
+ def app
34
+ ::Proxy::OpenSCAP::Api.new
35
+ end
36
+
37
+ def test_post_arf_report_to_foreman
38
+ stub_request(:post, "#{@foreman_url}/api/v2/compliance/arf_reports/#{@cname}/#{@policy_id}/#{@date}")
39
+ .to_return(:status => 200, :body => "{\"result\":\"OK\",\"id\":\"#{@arf_id}\"}")
40
+ post "/arf/#{@policy_id}", @arf_report, 'CONTENT_TYPE' => 'text/xml', 'CONTENT_ENCODING' => 'x-bzip2'
41
+ assert(last_response.successful?, "Should return OK")
42
+ assert(File.file?("#{@results_path}/reports/arf/#{@cname}/#{@arf_id}/#{@date}/#{@filename}"), "File should be save on Reports directory")
43
+ end
44
+
45
+ def test_post_fails_save_in_spool
46
+ @policy_id = 2
47
+ stub_request(:post, "#{@foreman_url}/api/v2/compliance/arf_reports/#{@cname}/#{@policy_id}/#{@date}")
48
+ .to_return(:status => 500, :body => "{\"result\":\"server error\"}")
49
+ post "/arf/#{@policy_id}", @arf_report, 'CONTENT_TYPE' => 'text/xml', 'CONTENT_ENCODING' => 'x-bzip2'
50
+ assert(last_response.successful?, "Should return OK")
51
+ assert(File.file?("#{@results_path}/spool/arf/#{@cname}/#{@policy_id}/#{@date}/#{@filename}"), "File should be saved in spool directory")
52
+ refute(File.file?("#{@results_path}/reports/arf/#{@cname}/#{@arf_id}/#{@date}/#{@filename}"), "File should not be in Reports directory")
53
+ end
54
+
55
+ def test_fail_save_file_should_raise_error
56
+ @policy_id = 2
57
+ stub_request(:post, "#{@foreman_url}/api/v2/compliance/arf_reports/#{@cname}/#{@policy_id}/#{@date}").to_return(:status => 500, :body => "{\"result\":\"server error\"}")
58
+ Proxy::OpenSCAP::StorageFS.any_instance.stubs(:create_directory).raises(StandardError)
59
+ post "/arf/#{@policy_id}", @arf_report, 'CONTENT_TYPE' => 'text/xml', 'CONTENT_ENCODING' => 'x-bzip2'
60
+ assert(last_response.server_error?, "Should return 500")
61
+ refute(File.file?("#{@results_path}/spool/arf/#{@cname}/#{@policy_id}/#{@date}/#{@filename}"), "File should be saved in spool directory")
62
+ end
63
+
64
+ def test_success_post_fail_save_should_save_spool
65
+ stub_request(:post, "#{@foreman_url}/api/v2/compliance/arf_reports/#{@cname}/#{@policy_id}/#{@date}")
66
+ .to_return(:status => 200, :body => "{\"result\":\"OK\",\"id\":\"#{@arf_id}\"}")
67
+ Proxy::OpenSCAP::StorageFS.any_instance.stubs(:store_archive).raises(Proxy::OpenSCAP::StoreReportError)
68
+ post "/arf/#{@policy_id}", @arf_report, 'CONTENT_TYPE' => 'text/xml', 'CONTENT_ENCODING' => 'x-bzip2'
69
+ refute(File.file?("#{@results_path}/spool/arf/#{@cname}/#{@policy_id}/#{@date}/#{@filename}"), "File should not be in spool directory")
70
+ refute(File.file?("#{@results_path}/reports/arf/#{@cname}/#{@arf_id}/#{@date}/#{@filename}"), "File should not be in Reports directory")
71
+ assert(File.file?("#{@results_path}/failed/arf/#{@cname}/#{@arf_id}/#{@date}/#{@filename}"), "File should be in Failed directory")
72
+ log_file = File.read('logs/test.log')
73
+ assert(log_file.include?('Failed to save Report in reports directory'), 'Logger should notify that failed to save in reports dir')
74
+ end
75
+ end
@@ -0,0 +1,54 @@
1
+ require 'test_helper'
2
+ require 'smart_proxy_openscap'
3
+ require 'smart_proxy_openscap/openscap_api'
4
+
5
+ class ScapContentParserApiTest < Test::Unit::TestCase
6
+ include Rack::Test::Methods
7
+
8
+ def setup
9
+ @foreman_url = 'https://foreman.example.com'
10
+ Proxy::SETTINGS.stubs(:foreman_url).returns(@foreman_url)
11
+ @scap_content = File.new("#{Dir.getwd}/test/data/ssg-rhel7-ds.xml").read
12
+ end
13
+
14
+ def app
15
+ ::Proxy::OpenSCAP::Api.new
16
+ end
17
+
18
+ def test_scap_content_policies
19
+ post '/scap_content/policies', @scap_content, 'CONTENT_TYPE' => 'text/xml'
20
+ expected_response = {"xccdf_org.ssgproject.content_profile_test" => "test",
21
+ "xccdf_org.ssgproject.content_profile_rht-ccp" => "Red Hat Corporate Profile for Certified Cloud Providers (RH CCP)",
22
+ "xccdf_org.ssgproject.content_profile_common" => "Common Profile for General-Purpose Systems",
23
+ "xccdf_org.ssgproject.content_profile_stig-rhel7-server-upstream" => "Common Profile for General-Purpose SystemsPre-release Draft STIG for RHEL 7 Server"}
24
+ assert_equal(expected_response.to_json, last_response.body)
25
+ assert(last_response.successful?)
26
+ end
27
+
28
+ def test_invalid_scap_content_policies
29
+ post '/scap_content/policies', '<xml>blah</xml>', 'CONTENT_TYPE' => 'text/xml'
30
+ assert(last_response.body.include?('Could not create Source DataStream session'))
31
+ end
32
+
33
+ def test_scap_content_validator
34
+ post '/scap_content/validator', @scap_content, 'CONTENT_TYPE' => 'text/xml'
35
+ result = JSON.parse(last_response.body)
36
+ assert_empty(result['errors'])
37
+ assert(last_response.successful?)
38
+ end
39
+
40
+ def test_invalid_scap_content_validator
41
+ Proxy::OpenSCAP::ContentParser.any_instance.stubs(:validate).returns({:errors => 'Invalid SCAP file type'}.to_json)
42
+ post '/scap_content/validator', @scap_content, 'CONTENT_TYPE' => 'text/xml'
43
+ result = JSON.parse(last_response.body)
44
+ refute_empty(result['errors'])
45
+ assert(last_response.successful?)
46
+ end
47
+
48
+ def test_scap_content_guide
49
+ post '/scap_content/guide/xccdf_org.ssgproject.content_profile_rht-ccp', @scap_content, 'CONTENT_TYPE' => 'text/xml'
50
+ result = JSON.parse(last_response.body)
51
+ assert(result['html'].start_with?('<!DOCTYPE html>'))
52
+ assert(last_response.successful?)
53
+ end
54
+ end
@@ -0,0 +1,11 @@
1
+ require 'rack/test'
2
+ require 'test/unit'
3
+ require 'webmock/test_unit'
4
+ require 'mocha/setup'
5
+ require 'json'
6
+ require 'ostruct'
7
+
8
+ require 'smart_proxy_for_testing'
9
+
10
+ # create log directory in our (not smart-proxy) directory
11
+ FileUtils.mkdir_p File.dirname(Proxy::SETTINGS.log_file)
metadata CHANGED
@@ -1,15 +1,87 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smart_proxy_openscap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Šimon Lukašík
8
+ - Shlomi Zadok
9
+ - Marek Hulan
8
10
  autorequire:
9
11
  bindir: bin
10
12
  cert_chain: []
11
- date: 2015-05-19 00:00:00.000000000 Z
12
- dependencies: []
13
+ date: 2015-11-01 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rake
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - '>='
27
+ - !ruby/object:Gem::Version
28
+ version: '0'
29
+ - !ruby/object:Gem::Dependency
30
+ name: rack-test
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ - !ruby/object:Gem::Dependency
44
+ name: mocha
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ - !ruby/object:Gem::Dependency
58
+ name: webmock
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ - !ruby/object:Gem::Dependency
72
+ name: openscap
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 0.4.3
78
+ type: :runtime
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - '>='
83
+ - !ruby/object:Gem::Version
84
+ version: 0.4.3
13
85
  description: |-
14
86
  A plug-in to the Foreman's smart-proxy which receives
15
87
  bzip2ed ARF files and forwards them to the Foreman.
@@ -19,21 +91,39 @@ executables:
19
91
  extensions: []
20
92
  extra_rdoc_files: []
21
93
  files:
94
+ - .rubocop.yml
95
+ - .rubocop_todo.yml
22
96
  - COPYING
97
+ - Gemfile
23
98
  - README.md
99
+ - Rakefile
24
100
  - bin/smart-proxy-openscap-send
25
101
  - bundler.d/openscap.rb
26
102
  - extra/rubygem-smart_proxy_openscap.spec
27
103
  - extra/smart-proxy-openscap-send.cron
28
104
  - lib/smart_proxy_openscap.rb
105
+ - lib/smart_proxy_openscap/fetch_scap_content.rb
106
+ - lib/smart_proxy_openscap/foreman_forwarder.rb
29
107
  - lib/smart_proxy_openscap/http_config.ru
30
108
  - lib/smart_proxy_openscap/openscap_api.rb
109
+ - lib/smart_proxy_openscap/openscap_content_parser.rb
31
110
  - lib/smart_proxy_openscap/openscap_exception.rb
32
111
  - lib/smart_proxy_openscap/openscap_lib.rb
33
112
  - lib/smart_proxy_openscap/openscap_plugin.rb
113
+ - lib/smart_proxy_openscap/openscap_report_parser.rb
34
114
  - lib/smart_proxy_openscap/openscap_version.rb
115
+ - lib/smart_proxy_openscap/spool_forwarder.rb
116
+ - lib/smart_proxy_openscap/storage.rb
117
+ - lib/smart_proxy_openscap/storage_fs.rb
35
118
  - settings.d/openscap.yml.example
36
119
  - smart_proxy_openscap.gemspec
120
+ - test/data/arf_report
121
+ - test/data/ssg-rhel7-ds.xml
122
+ - test/fetch_scap_api_test.rb
123
+ - test/get_report_xml_html_test.rb
124
+ - test/post_report_api_test.rb
125
+ - test/scap_content_parser_api_test.rb
126
+ - test/test_helper.rb
37
127
  homepage: http://github.com/OpenSCAP/smart_proxy_openscap
38
128
  licenses:
39
129
  - GPL-3
@@ -59,4 +149,3 @@ signing_key:
59
149
  specification_version: 4
60
150
  summary: OpenSCAP plug-in for Foreman's smart-proxy.
61
151
  test_files: []
62
- has_rdoc: