foreman_openscap 0.11.1 → 0.11.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 41886c4e7790fe8c029439e383a4935e63a7bec0
4
- data.tar.gz: 4c3fe7f030d02d3fcf9aabf5deebb45ae961da35
3
+ metadata.gz: 416e356bc2870ed24cb951ba6dcd4d82b8a5ccb3
4
+ data.tar.gz: 53fc14379e71a0ff3ce51711a71974b33993c01d
5
5
  SHA512:
6
- metadata.gz: ddebdbe79d9cd41fa94fdf0e5c84fd2dba412eba1767b1dc48e7d4bc96e9ccf2f38a4f69084eb616f941a47bc0ac9753aa0e91cfa4860e56292cec81a8324c93
7
- data.tar.gz: 276132715e7943d15a90a61e562c8d3c93f71ab578dc0bf8ceb6815dea1abf557f4019b7e2d8b980bdce8014f57124580a5f8a22fea834860bdc2d2dea53acbb
6
+ metadata.gz: 94376bb05a0efba455dbd86ee1044e065b357e43f76572ed78659056128da0c1f2a4cb5be0b241a0372d4d87cddc27665566f170a731adcc223807da8deab881
7
+ data.tar.gz: 36ed74e3c3df1e3d46b47827c2af362c1a7744b5fdc7ec639713f56eace072994f64125a40bd6c141fa2261d1e999208a42a2b1750bcdc8e5b77559430f29480
@@ -50,7 +50,7 @@ module Api
50
50
  def create
51
51
  arf_report = ForemanOpenscap::ArfReport.create_arf(@asset, @smart_proxy, params.to_unsafe_h)
52
52
  @asset.host.refresh_statuses([HostStatus.find_status_by_humanized_name("compliance")])
53
- render :json => { :result => :OK, :id => arf_report.id.to_s }
53
+ respond_for_report arf_report
54
54
  end
55
55
 
56
56
  api :GET, "/compliance/arf_reports/:id/download/", N_("Download bzipped ARF report")
@@ -75,6 +75,16 @@ module Api
75
75
 
76
76
  private
77
77
 
78
+ def respond_for_report(arf_report)
79
+ if arf_report.nil?
80
+ upload_fail(_("Policy with id %s not found.") % params[:policy_id])
81
+ elsif arf_report.new_record?
82
+ upload_fail arf_report.errors.full_messages.to_sentence
83
+ else
84
+ render :json => { :result => :ok, :id => arf_report.id.to_s }
85
+ end
86
+ end
87
+
78
88
  def find_resource
79
89
  not_found && return if params[:id].blank?
80
90
  instance_variable_set("@arf_report", resource_scope.find(params[:id]))
@@ -83,9 +93,14 @@ module Api
83
93
  def find_resources_before_create
84
94
  @asset = ForemanOpenscap::Helper::get_asset(params[:cname], params[:policy_id])
85
95
 
96
+ unless @asset
97
+ upload_fail(_('Could not find host identified by: %s') % params[:cname])
98
+ return
99
+ end
100
+
86
101
  if !params[:openscap_proxy_url] && !params[:openscap_proxy_name] && !@asset.host.openscap_proxy
87
102
  msg = _('Failed to upload Arf Report, OpenSCAP proxy name or url not found in params when uploading for %s and host is missing openscap_proxy') % @asset.host.name
88
- no_proxy_for_host(msg)
103
+ upload_fail(msg)
89
104
  return
90
105
  elsif !params[:openscap_proxy_url] && !params[:openscap_proxy_name] && @asset.host.openscap_proxy
91
106
  logger.debug 'No proxy params found when uploading arf report, falling back to asset.host.openscap_proxy'
@@ -97,7 +112,7 @@ module Api
97
112
 
98
113
  unless @smart_proxy
99
114
  msg = _('No proxy found for %{name} or %{url}') % { :name => params[:openscap_proxy_name], :url => params[:openscap_proxy_url] }
100
- no_proxy_for_host(msg)
115
+ upload_fail(msg)
101
116
  return
102
117
  end
103
118
  end
@@ -106,9 +121,9 @@ module Api
106
121
  render_error 'standard_error', :status => :internal_error, :locals => { :exception => error }
107
122
  end
108
123
 
109
- def no_proxy_for_host(msg)
124
+ def upload_fail(msg)
110
125
  logger.error msg
111
- render :json => { :result => msg }, :status => :unprocessable_entity
126
+ render :json => { :result => :fail, :errors => msg }, :status => :unprocessable_entity
112
127
  end
113
128
 
114
129
  def action_permission
@@ -105,16 +105,17 @@ module ForemanOpenscap
105
105
  end
106
106
 
107
107
  def self.create_arf(asset, proxy, params)
108
- # fail if policy does not exist.
109
108
  arf_report = nil
110
- policy = Policy.find(params[:policy_id])
109
+ policy = Policy.find_by :id => params[:policy_id]
110
+ return unless policy
111
+
111
112
  ArfReport.transaction do
112
- # TODO:RAILS-4.0: This should become arf_report = ArfReport.find_or_create_by! ...
113
- arf_report = ArfReport.create!(:host => asset.host,
114
- :reported_at => Time.at(params[:date].to_i),
115
- :status => params[:metrics],
116
- :metrics => params[:metrics],
117
- :openscap_proxy => proxy)
113
+ arf_report = ArfReport.create(:host => asset.host,
114
+ :reported_at => Time.at(params[:date].to_i),
115
+ :status => params[:metrics],
116
+ :metrics => params[:metrics],
117
+ :openscap_proxy => proxy)
118
+ return arf_report unless arf_report.persisted?
118
119
  PolicyArfReport.where(:arf_report_id => arf_report.id, :policy_id => policy.id, :digest => params[:digest]).first_or_create!
119
120
  if params[:logs]
120
121
  params[:logs].each do |log|
@@ -1,6 +1,7 @@
1
1
  module ForemanOpenscap::Helper
2
2
  def self.get_asset(cname, policy_id)
3
- asset = find_host_by_name_or_uuid(cname).get_asset
3
+ asset = find_host_by_name_or_uuid(cname)&.get_asset
4
+ return unless asset
4
5
  asset.policy_ids += [policy_id]
5
6
  asset
6
7
  end
@@ -16,11 +17,5 @@ module ForemanOpenscap::Helper
16
17
  else
17
18
  host = Host.find_by(name: cname)
18
19
  end
19
-
20
- unless host
21
- Rails.logger.error "Could not find Host with name: #{cname}"
22
- raise ActiveRecord::RecordNotFound
23
- end
24
- host
25
20
  end
26
21
  end
@@ -1,3 +1,3 @@
1
1
  module ForemanOpenscap
2
- VERSION = "0.11.1".freeze
2
+ VERSION = "0.11.2".freeze
3
3
  end
@@ -90,7 +90,41 @@ class Api::V2::Compliance::ArfReportsControllerTest < ActionController::TestCase
90
90
  assert_response :unprocessable_entity
91
91
  res = JSON.parse(@response.body)
92
92
  msg = "Failed to upload Arf Report, OpenSCAP proxy name or url not found in params when uploading for #{asset.host.name} and host is missing openscap_proxy"
93
- assert_equal msg, res["result"]
93
+ assert_equal msg, res["errors"]
94
+ end
95
+
96
+ test "should not create report when host is missing" do
97
+ reports_cleanup
98
+ date = Time.new(1984, 9, 16)
99
+ ForemanOpenscap::Helper.stubs(:get_asset).returns(nil)
100
+ cname = '9521a5c5-8f44-495f-b087-20e86b30bffg'
101
+ post :create,
102
+ :params => @from_json.merge(:cname => cname,
103
+ :policy_id => @policy.id,
104
+ :date => date.to_i,
105
+ :openscap_proxy_name => @proxy.name),
106
+ :session => set_session_user
107
+ assert_response :unprocessable_entity
108
+ res = JSON.parse(@response.body)
109
+ msg = "Could not find host identified by: #{cname}"
110
+ assert_equal msg, res["errors"]
111
+ end
112
+
113
+ test "should not create report when policy is missing" do
114
+ reports_cleanup
115
+ date = Time.new(1984, 9, 17)
116
+ ForemanOpenscap::Helper.stubs(:get_asset).returns(@asset)
117
+ policy_id = 0
118
+ post :create,
119
+ :params => @from_json.merge(:cname => @cname,
120
+ :policy_id => policy_id,
121
+ :date => date.to_i,
122
+ :openscap_proxy_name => @proxy.name),
123
+ :session => set_session_user
124
+ assert_response :unprocessable_entity
125
+ res = JSON.parse(@response.body)
126
+ msg = "Policy with id #{policy_id} not found."
127
+ assert_equal msg, res["errors"]
94
128
  end
95
129
 
96
130
  test "should not duplicate messages" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_openscap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.1
4
+ version: 0.11.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - slukasik@redhat.com
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-13 00:00:00.000000000 Z
11
+ date: 2018-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: deface