foreman_rh_cloud 15.0.0 → 15.0.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.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/api/v2/advisor_engine/advisor_engine_controller.rb +1 -73
  3. data/app/controllers/foreman_inventory_upload/accounts_controller.rb +29 -14
  4. data/app/controllers/insights_cloud/ui_requests_controller.rb +19 -2
  5. data/app/services/foreman_rh_cloud/insights_api_forwarder.rb +93 -79
  6. data/config/package-lock.json.plugin +7160 -21310
  7. data/config/routes.rb +0 -4
  8. data/lib/foreman_inventory_upload/async/generate_host_report.rb +6 -1
  9. data/lib/foreman_inventory_upload/async/host_inventory_report_job.rb +10 -0
  10. data/lib/foreman_inventory_upload/async/queue_for_upload_job.rb +1 -0
  11. data/lib/foreman_inventory_upload/async/upload_report_direct_job.rb +16 -2
  12. data/lib/foreman_inventory_upload/generators/archived_report.rb +4 -4
  13. data/lib/foreman_inventory_upload/generators/slice.rb +55 -12
  14. data/lib/foreman_inventory_upload.rb +69 -0
  15. data/lib/foreman_rh_cloud/plugin.rb +24 -11
  16. data/lib/foreman_rh_cloud/version.rb +1 -1
  17. data/package.json +6 -13
  18. data/test/controllers/insights_cloud/api/advisor_engine_controller_test.rb +4 -15
  19. data/test/controllers/insights_cloud/ui_requests_controller_test.rb +217 -5
  20. data/test/jobs/generate_host_report_test.rb +1 -1
  21. data/test/jobs/host_inventory_report_job_test.rb +72 -0
  22. data/test/jobs/queue_for_upload_job_test.rb +4 -1
  23. data/test/jobs/upload_report_direct_job_test.rb +28 -7
  24. data/test/unit/archived_report_generator_test.rb +21 -0
  25. data/test/unit/foreman_inventory_upload_host_report_path_test.rb +111 -0
  26. data/test/unit/rh_cloud_permissions_test.rb +0 -2
  27. data/test/unit/services/foreman_rh_cloud/insights_api_forwarder_test.rb +189 -356
  28. data/test/unit/slice_generator_test.rb +28 -0
  29. data/webpack/ForemanInventoryUpload/Components/Dashboard/Dashboard.js +1 -0
  30. data/webpack/ForemanInventoryUpload/Components/TaskProgress/TaskProgress.js +8 -2
  31. data/webpack/ForemanInventoryUpload/SubscriptionsPageExtension/InventoryAutoUpload/InventoryAutoUpload.js +3 -1
  32. data/webpack/ForemanInventoryUpload/SubscriptionsPageExtension/InventoryAutoUpload/__tests__/InventoryAutoUpload.test.js +23 -0
  33. data/webpack/HostsIndexExtensions/VulnerabilityAnalysisActions.js +1 -1
  34. data/webpack/test_setup.js +18 -0
  35. metadata +3 -6
  36. data/app/controllers/foreman_inventory_upload/api/tasks_controller.rb +0 -110
  37. data/app/services/foreman_rh_cloud/hits_uploader.rb +0 -50
  38. data/lib/tasks/hybrid_cloud.rake +0 -132
  39. data/test/unit/services/foreman_rh_cloud/hits_uploader_test.rb +0 -131
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 637a2b5c3eb9d907b2c20a3921b728efe617dd2dccb6cd1f99135784f2721bed
4
- data.tar.gz: 2b254832b522c4c60c6f11ff8e727a890c3b0632d1df3e0abc024657ad8a0343
3
+ metadata.gz: 617e69ac154ea7e6e7e5670d28d4d5f815beebb82eb2341649a1c3f8f5e9ba81
4
+ data.tar.gz: c09162b47068f2bd3ea0dfc19cbe398d53abfb4b0c991a34991c25537d25fd82
5
5
  SHA512:
6
- metadata.gz: 9e3c2a143ad9a3e7ff76ddf83440655c0fd0c558f423acc0dac4ddbb88e9245850d39f270e7f455849d58fd2e37904804a0e1611641f7f8438ca2a79e15e1e36
7
- data.tar.gz: 22886acec69b9b059802e3ba750bb43bda9132990c4c104e07a49ed6852f37b54db2302ead5acfeb973e136ba435256437ef786e396253420e89322946d929e7
6
+ metadata.gz: f1617c5eda6cfe897f10d35894c7de3da99bc6645cf4404ad2988ff3075f109f2ed7dfd1776baa081b944668dcb8d8c404f915a1f4dc277e38d71d6e1ab7814e
7
+ data.tar.gz: 3db753143c32cae7428191016375c2dc8ec2c91d9bd2d67a0a83b9c3e3fc231a1a81336a9f79ca47eb1c56063793b5fdbfe0f7e57d4ab29a10b5a0b5698fb85a
@@ -4,10 +4,8 @@ module Api
4
4
  class AdvisorEngineController < ::Api::V2::BaseController
5
5
  include ::Api::Version2
6
6
  include Foreman::Controller::SmartProxyAuth
7
- include ::Foreman::Controller::FilterParameters
8
7
 
9
- filter_parameters :payload
10
- add_smart_proxy_filters [:host_details, :upload_hits]
8
+ add_smart_proxy_filters [:host_details]
11
9
 
12
10
  api :GET, "/advisor_engine/host_details", N_('Fetch Insights-related host details')
13
11
  param :host_uuids, Array, required: true, desc: N_('List of host UUIDs')
@@ -22,76 +20,6 @@ module Api
22
20
  end
23
21
  end
24
22
  end
25
-
26
- api :PATCH, "/advisor_engine/upload_hits", N_("Upload hits from iop-advisor-engine")
27
- param :host_name, String, required: true
28
- param :host_uuid, String
29
-
30
- param :payload, Hash, :desc => N_("iop payload including resolutions, rules, hits") do
31
- param :resolutions, Array, :desc => N_("upload resolutions related to the hits") do
32
- param :rule_id, String, :desc => N_("rule id"), :required => true
33
- param :description, String, :desc => N_("resolution description")
34
- param :needs_reboot, :bool, :desc => N_("Whether the resolution requires reboot")
35
- param :resolution_risk, String, :desc => N_("resolution risk")
36
- param :resolution_type, String, :desc => N_("type")
37
- end
38
-
39
- param :rules, Array, :desc => N_("Upload rules related to the hits") do
40
- param :rule_id, String, :desc => N_("rule id"), :required => true
41
- param :description, String, :desc => N_("rule description")
42
- param :category_name, String, :desc => N_("category name")
43
- param :impact_name, String, :desc => N_("impact name")
44
- param :summary, String, :desc => N_("summary")
45
- param :generic, String, :desc => N_("generic")
46
- param :reason, String, :desc => N_("reason")
47
- param :total_risk, :number, :desc => N_("total risk")
48
- param :reboot_required, :bool, :desc => N_("reboot required")
49
- param :more_info, String, :desc => N_("more info")
50
- param :rating, :number, :desc => N_("rating")
51
- end
52
-
53
- param :hits, Array, :desc => N_("Upload hits information") do
54
- param :rule_id, String, :desc => N_("rule id"), :required => true
55
- param :title, String, :desc => N_("rule title")
56
- param :solution_url, String, :desc => N_("solution url")
57
- param :total_risk, :number, :desc => N_("total risk")
58
- param :likelihood, :number, :desc => N_("likelihood number")
59
- param :publish_date, String, :desc => N_("publish date (YYYY-MM-DD)")
60
- param :results_url, String, :desc => N_("result url")
61
- end
62
- param :details, String, :desc => N_("upload hits details json")
63
- end
64
-
65
- def upload_hits
66
- host = Host.find_by(name: params.require(:host_name))
67
- if host.blank?
68
- render json: { error: 'No host found for the given host name' }, status: :not_found
69
- else
70
- hits_uploader = ForemanRhCloud::HitsUploader.new(host: host, payload: payload_params.to_h, uuid: params[:host_uuid])
71
- hits_uploader.upload!
72
- render json: {
73
- action_status: 'success',
74
- }, status: :ok
75
- end
76
- end
77
-
78
- def payload_params
79
- params.require(:payload).permit(
80
- :details,
81
- {
82
- resolutions: [
83
- :rule_id, :description, :needs_reboot, :resolution_risk, :resolution_type
84
- ],
85
- rules: [
86
- :rule_id, :description, :category_name, :impact_name, :summary, :generic,
87
- :reason, :total_risk, :reboot_required, :more_info, :rating
88
- ],
89
- hits: [
90
- :rule_id, :title, :solution_url, :total_risk, :likelihood, :publish_date, :results_url
91
- ],
92
- }
93
- )
94
- end
95
23
  end
96
24
  end
97
25
  end
@@ -54,27 +54,27 @@ module ForemanInventoryUpload
54
54
  def sub_action_status(task, action_class_name)
55
55
  return nil unless task
56
56
 
57
- # If task is still running, return the state
58
- return task.state unless task.state == 'stopped'
59
-
60
- # For GenerateHostReport: always show status if task completed (generation always runs)
61
- if action_class_name == 'GenerateHostReport'
62
- return task.result
63
- end
64
-
65
- # For UploadReportDirectJob: only show status if task had upload enabled
57
+ # For UploadReportDirectJob: use the recorded upload result so a completed
58
+ # upload stays successful even while later steps run or fail.
66
59
  if action_class_name == 'UploadReportDirectJob'
67
60
  main_action = task.main_action
68
61
  return nil unless main_action.respond_to?(:input)
69
-
70
- # Check if upload was enabled for this task
71
62
  return nil unless main_action.input[:upload]
63
+ return nil unless main_action.respond_to?(:uploaded?)
64
+
65
+ return 'success' if main_action.uploaded?
66
+ return task.state unless task.state == 'stopped'
67
+ return 'error' if %w[error warning].include?(task.result.to_s)
72
68
 
73
- # Return the task result
74
- return task.result
69
+ # Upload requested but not completed (e.g. skipped due to disconnect/cert)
70
+ 'unknown'
75
71
  end
76
72
 
77
- nil
73
+ # If task is still running, return the state
74
+ return task.state unless task.state == 'stopped'
75
+
76
+ # For GenerateHostReport: always show status if task completed (generation always runs)
77
+ task.result if action_class_name == 'GenerateHostReport'
78
78
  rescue StandardError => e
79
79
  Rails.logger.warn("Failed to get sub-action status for #{action_class_name} in task #{task.id}: #{e.message}")
80
80
  nil
@@ -93,6 +93,7 @@ module ForemanInventoryUpload
93
93
  ended_at: task.ended_at,
94
94
  duration: task.try(:duration)&.to_f,
95
95
  report_file_path: task_report_file_path(task),
96
+ uploaded: task_uploaded?(task),
96
97
  }
97
98
  end
98
99
 
@@ -108,5 +109,19 @@ module ForemanInventoryUpload
108
109
  Rails.logger.warn("Failed to get report file path for task #{task.id}: #{e.message}")
109
110
  nil
110
111
  end
112
+
113
+ def task_uploaded?(task)
114
+ return false unless task
115
+
116
+ main_action = task.main_action
117
+ return false unless main_action.respond_to?(:uploaded?)
118
+
119
+ # Use the recorded upload completion from this task, not overall task.result
120
+ # (a later step can fail after upload) and not leftover done-folder files.
121
+ !!main_action.uploaded?
122
+ rescue StandardError => e
123
+ Rails.logger.warn("Failed to determine upload status for task #{task.id}: #{e.message}")
124
+ false
125
+ end
111
126
  end
112
127
  end
@@ -1,5 +1,16 @@
1
1
  module InsightsCloud
2
2
  class UIRequestsController < ::ApplicationController
3
+ # This controller proxies both browser (session + CSRF token) and
4
+ # token-authenticated API requests (e.g. Robottelo). ApplicationController's
5
+ # unconditional protect_from_forgery rejects the latter with a 422, so swap
6
+ # in the same session-only CSRF check Foreman's API base controller uses.
7
+ skip_forgery_protection
8
+ include Foreman::Controller::ApiCsrfProtection
9
+
10
+ protect_from_forgery with: :exception,
11
+ if: :protect_api_from_forgery?,
12
+ prepend: true
13
+
3
14
  layout false
4
15
 
5
16
  before_action :ensure_org, :find_location, :only => [:forward_request]
@@ -18,6 +29,9 @@ module InsightsCloud
18
29
  @organization,
19
30
  @location
20
31
  )
32
+ rescue ::ForemanRhCloud::InsightsApiForwarder::UnsupportedOperationError => e
33
+ message = e.message
34
+ return render json: { message: message, error: message }, status: :not_found
21
35
  rescue ::Foreman::PermissionMissingException => e
22
36
  logger.warn("Permission denied for forwarding request: #{e}")
23
37
  message = e.message
@@ -76,12 +90,15 @@ module InsightsCloud
76
90
  end
77
91
 
78
92
  def translate_insights_host
79
- facet = InsightsFacet.find_by(uuid: params[:uuid])
93
+ # Scope facet lookup to hosts the user can access
94
+ facet = InsightsFacet.joins(:host).merge(Host.authorized(:view_hosts)).find_by(uuid: params[:uuid])
95
+
80
96
  if facet.present?
81
97
  Rails.logger.debug "Found InsightsFacet #{params[:uuid]}"
82
98
  redirect_to host_details_page_path(facet.host_id)
83
99
  else
84
- Rails.logger.error "Could not find InsightsFacet for #{params[:uuid]}"
100
+ # Don't leak whether the UUID exists - treat unauthorized and non-existent the same
101
+ Rails.logger.error "Could not find accessible InsightsFacet for #{params[:uuid]}"
85
102
  redirect_to '/page-not-found'
86
103
  end
87
104
  end
@@ -4,6 +4,9 @@ module ForemanRhCloud
4
4
  class InsightsApiForwarder
5
5
  include ForemanRhCloud::CertAuth
6
6
 
7
+ class UnsupportedOperationError < StandardError
8
+ end
9
+
7
10
  # Permission mapping for API paths:
8
11
  #
9
12
  # Foreman Permission | Paths
@@ -43,24 +46,27 @@ module ForemanRhCloud
43
46
  #
44
47
  # view_advisor | GET /api/insights/v1/*
45
48
  # edit_advisor | POST /api/insights/v1/ack/
49
+ # | PUT /api/insights/v1/ack/{rule_id}/
46
50
  # | DELETE /api/insights/v1/ack/{rule_id}/
47
51
  # | POST /api/insights/v1/hostack/
52
+ # | PUT /api/insights/v1/hostack/{id}/
48
53
  # | DELETE /api/insights/v1/hostack/{id}/
54
+ # | POST /api/insights/v1/rule/{rule_id}/ack_hosts/
49
55
  # | POST /api/insights/v1/rule/{rule_id}/unack_hosts/
50
56
  #
51
57
  SCOPED_REQUESTS = [
52
58
  # Inventory hosts - shared dependency;
53
- # - requires view_compliance or view_vulnerability for GET
59
+ # - requires view_advisor, view_compliance, or view_vulnerability for GET
54
60
  {
55
- test: %r{api/inventory/v1/hosts(/.*)?$},
61
+ test: %r{^api/inventory/v1/hosts(/.*)?$},
56
62
  tag_name: :tags,
57
63
  permissions: {
58
- 'GET' => [:view_compliance, :view_vulnerability],
64
+ 'GET' => [:view_advisor, :view_compliance, :view_vulnerability],
59
65
  },
60
66
  },
61
67
  # Policies - requires view_compliance for GET, edit_compliance for POST/DELETE/PATCH
62
68
  {
63
- test: %r{api/compliance/v2/policies(/[^/]*)?$},
69
+ test: %r{^api/compliance/v2/policies(/[^/]*)?$},
64
70
  permissions: {
65
71
  'GET' => :view_compliance,
66
72
  'POST' => :edit_compliance,
@@ -70,7 +76,7 @@ module ForemanRhCloud
70
76
  },
71
77
  # System and policies assigned to them - requires view_compliance for GET, edit_compliance for POST/DELETE/PATCH
72
78
  {
73
- test: %r{api/compliance/v2/policies/[^/]+/systems(/[^/]*)?$},
79
+ test: %r{^api/compliance/v2/policies/[^/]+/systems(/[^/]*)?$},
74
80
  tag_name: :tags,
75
81
  permissions: {
76
82
  'GET' => :view_compliance,
@@ -81,7 +87,7 @@ module ForemanRhCloud
81
87
  },
82
88
  # Tailorings of assigned policies - requires view_compliance for GET, edit_compliance for POST/PATCH
83
89
  {
84
- test: %r{api/compliance/v2/policies/[^/]+/tailorings(/[^/]*)?$},
90
+ test: %r{^api/compliance/v2/policies/[^/]+/tailorings(/[^/]*)?$},
85
91
  permissions: {
86
92
  'GET' => :view_compliance,
87
93
  'POST' => :edit_compliance,
@@ -90,7 +96,7 @@ module ForemanRhCloud
90
96
  },
91
97
  # Rules of Tailorings - requires view_compliance for GET, edit_compliance for POST/DELETE/PATCH
92
98
  {
93
- test: %r{api/compliance/v2/policies/[^/]+/tailorings/[^/]+/rules(/[^/]*)?$},
99
+ test: %r{^api/compliance/v2/policies/[^/]+/tailorings/[^/]+/rules(/[^/]*)?$},
94
100
  permissions: {
95
101
  'GET' => :view_compliance,
96
102
  'POST' => :edit_compliance,
@@ -100,7 +106,7 @@ module ForemanRhCloud
100
106
  },
101
107
  # Compliance Reports - requires view_compliance for GET, edit_compliance for DELETE
102
108
  {
103
- test: %r{api/compliance/v2/reports(/[^/]*)?$},
109
+ test: %r{^api/compliance/v2/reports(/[^/]*)?$},
104
110
  permissions: {
105
111
  'GET' => :view_compliance,
106
112
  'DELETE' => :edit_compliance,
@@ -108,7 +114,7 @@ module ForemanRhCloud
108
114
  },
109
115
  # Systems assigned to a report - requires view_compliance for GET
110
116
  {
111
- test: %r{api/compliance/v2/reports/[^/]+/systems$},
117
+ test: %r{^api/compliance/v2/reports/[^/]+/systems$},
112
118
  tag_name: :tags,
113
119
  permissions: {
114
120
  'GET' => :view_compliance,
@@ -116,7 +122,7 @@ module ForemanRhCloud
116
122
  },
117
123
  # Individual system in a report - requires view_compliance for GET
118
124
  {
119
- test: %r{api/compliance/v2/reports/[^/]+/systems/[^/]+$},
125
+ test: %r{^api/compliance/v2/reports/[^/]+/systems/[^/]+$},
120
126
  tag_name: :tags,
121
127
  permissions: {
122
128
  'GET' => :view_compliance,
@@ -124,7 +130,7 @@ module ForemanRhCloud
124
130
  },
125
131
  # OS versions for systems in a report - requires view_compliance for GET
126
132
  {
127
- test: %r{api/compliance/v2/reports/[^/]+/systems/os_versions$},
133
+ test: %r{^api/compliance/v2/reports/[^/]+/systems/os_versions$},
128
134
  tag_name: :tags,
129
135
  permissions: {
130
136
  'GET' => :view_compliance,
@@ -132,7 +138,7 @@ module ForemanRhCloud
132
138
  },
133
139
  # OS versions for systems in a policy - requires view_compliance for GET
134
140
  {
135
- test: %r{api/compliance/v2/policies/[^/]+/systems/os_versions$},
141
+ test: %r{^api/compliance/v2/policies/[^/]+/systems/os_versions$},
136
142
  tag_name: :tags,
137
143
  permissions: {
138
144
  'GET' => :view_compliance,
@@ -140,7 +146,7 @@ module ForemanRhCloud
140
146
  },
141
147
  # Compliance Systems list - requires view_compliance for GET
142
148
  {
143
- test: %r{api/compliance/v2/systems$},
149
+ test: %r{^api/compliance/v2/systems$},
144
150
  tag_name: :tags,
145
151
  permissions: {
146
152
  'GET' => :view_compliance,
@@ -148,7 +154,7 @@ module ForemanRhCloud
148
154
  },
149
155
  # Individual compliance system - requires view_compliance for GET
150
156
  {
151
- test: %r{api/compliance/v2/systems/[^/]+$},
157
+ test: %r{^api/compliance/v2/systems/[^/]+$},
152
158
  tag_name: :tags,
153
159
  permissions: {
154
160
  'GET' => :view_compliance,
@@ -156,7 +162,7 @@ module ForemanRhCloud
156
162
  },
157
163
  # OS versions for all systems - requires view_compliance for GET
158
164
  {
159
- test: %r{api/compliance/v2/systems/os_versions$},
165
+ test: %r{^api/compliance/v2/systems/os_versions$},
160
166
  tag_name: :tags,
161
167
  permissions: {
162
168
  'GET' => :view_compliance,
@@ -164,7 +170,7 @@ module ForemanRhCloud
164
170
  },
165
171
  # Policies for a specific system - requires view_compliance for GET
166
172
  {
167
- test: %r{api/compliance/v2/systems/[^/]+/policies$},
173
+ test: %r{^api/compliance/v2/systems/[^/]+/policies$},
168
174
  tag_name: :tags,
169
175
  permissions: {
170
176
  'GET' => :view_compliance,
@@ -172,7 +178,7 @@ module ForemanRhCloud
172
178
  },
173
179
  # Reports for a specific system - requires view_compliance for GET
174
180
  {
175
- test: %r{api/compliance/v2/systems/[^/]+/reports$},
181
+ test: %r{^api/compliance/v2/systems/[^/]+/reports$},
176
182
  tag_name: :tags,
177
183
  permissions: {
178
184
  'GET' => :view_compliance,
@@ -180,7 +186,7 @@ module ForemanRhCloud
180
186
  },
181
187
  # Test results for a report - requires view_compliance for GET
182
188
  {
183
- test: %r{api/compliance/v2/reports/[^/]+/test_results$},
189
+ test: %r{^api/compliance/v2/reports/[^/]+/test_results$},
184
190
  tag_name: :tags,
185
191
  permissions: {
186
192
  'GET' => :view_compliance,
@@ -188,7 +194,7 @@ module ForemanRhCloud
188
194
  },
189
195
  # Individual test result in a report - requires view_compliance for GET
190
196
  {
191
- test: %r{api/compliance/v2/reports/[^/]+/test_results/[^/]+$},
197
+ test: %r{^api/compliance/v2/reports/[^/]+/test_results/[^/]+$},
192
198
  tag_name: :tags,
193
199
  permissions: {
194
200
  'GET' => :view_compliance,
@@ -196,69 +202,72 @@ module ForemanRhCloud
196
202
  },
197
203
  # Other compliance endpoints - GET requires view_compliance
198
204
  {
199
- test: %r{api/compliance/v2/.*},
205
+ test: %r{^api/compliance/v2/.*},
200
206
  permissions: {
201
207
  'GET' => :view_compliance,
202
208
  },
203
209
  },
204
- # Vulnerability CVEs list - POST requires view_vulnerability (no tags support per OpenAPI spec)
210
+ # Vulnerability CVEs list and IDs - GET/POST require view_vulnerability
205
211
  {
206
- test: %r{api/vulnerability/v1/vulnerabilities/cves},
212
+ test: %r{^api/vulnerability/v1/vulnerabilities/cves(/ids)?$},
213
+ tag_name: :tags,
207
214
  permissions: {
215
+ 'GET' => :view_vulnerability,
208
216
  'POST' => :view_vulnerability,
209
217
  },
210
218
  },
211
- # Vulnerability status - PATCH requires edit_vulnerability
219
+ # Vulnerability status - GET requires view_vulnerability, PATCH requires edit_vulnerability
212
220
  # Note: GET /status does not support tags parameter per OpenAPI spec
213
221
  {
214
- test: %r{api/vulnerability/v1/status},
222
+ test: %r{^api/vulnerability/v1/status$},
215
223
  permissions: {
224
+ 'GET' => :view_vulnerability,
216
225
  'PATCH' => :edit_vulnerability,
217
226
  },
218
227
  },
219
228
  # CVE status - PATCH requires edit_vulnerability (no GET endpoint)
220
229
  {
221
- test: %r{api/vulnerability/v1/cves/status},
230
+ test: %r{^api/vulnerability/v1/cves/status$},
222
231
  permissions: {
223
232
  'PATCH' => :edit_vulnerability,
224
233
  },
225
234
  },
226
235
  # CVE business risk - PATCH requires edit_vulnerability (no GET endpoint)
227
236
  {
228
- test: %r{api/vulnerability/v1/cves/business_risk},
237
+ test: %r{^api/vulnerability/v1/cves/business_risk$},
229
238
  permissions: {
230
239
  'PATCH' => :edit_vulnerability,
231
240
  },
232
241
  },
233
242
  # Systems opt out - PATCH requires edit_vulnerability (no GET endpoint)
234
243
  {
235
- test: %r{api/vulnerability/v1/systems/opt_out},
244
+ test: %r{^api/vulnerability/v1/systems/opt_out$},
236
245
  permissions: {
237
246
  'PATCH' => :edit_vulnerability,
238
247
  },
239
248
  },
240
249
  # Endpoints without tags support (per OpenAPI spec) - still require view_vulnerability for GET
241
250
  {
242
- test: %r{api/vulnerability/v1/(apistatus|version|business_risk|announcement)$},
251
+ test: %r{^api/vulnerability/v1/(apistatus|version|business_risk|announcement)$},
243
252
  permissions: {
244
253
  'GET' => :view_vulnerability,
245
254
  },
246
255
  },
247
256
  {
248
- test: %r{api/vulnerability/v1/cves/[^/]+$},
257
+ test: %r{^api/vulnerability/v1/cves/[^/]+$},
249
258
  permissions: {
250
259
  'GET' => :view_vulnerability,
251
260
  },
252
261
  },
253
262
  {
254
- test: %r{api/vulnerability/v1/(playbooks|report)/},
263
+ test: %r{^api/vulnerability/v1/(playbooks|report)/},
255
264
  permissions: {
256
265
  'GET' => :view_vulnerability,
257
266
  },
258
267
  },
259
268
  # CVEs without errata feature flag - no tags support (per OpenAPI spec)
260
269
  {
261
- test: %r{api/vulnerability/v1/feature/cves_without_errata$},
270
+ test: %r{^api/vulnerability/v1/feature/cves_without_errata$},
262
271
  permissions: {
263
272
  'GET' => :view_vulnerability,
264
273
  'PATCH' => :edit_vulnerability,
@@ -266,33 +275,45 @@ module ForemanRhCloud
266
275
  },
267
276
  # Other vulnerability endpoints - GET requires view_vulnerability (with tags support)
268
277
  {
269
- test: %r{api/vulnerability/v1/.*},
278
+ test: %r{^api/vulnerability/v1/.*},
270
279
  tag_name: :tags,
271
280
  permissions: {
272
281
  'GET' => :view_vulnerability,
273
282
  },
274
283
  },
275
- # Advisor ack endpoints - POST/DELETE require edit_advisor
284
+ # Advisor ack endpoints - GET requires view_advisor, POST/PUT/DELETE require edit_advisor
276
285
  {
277
- test: %r{api/insights/v1/ack(/[^/]*)?$},
286
+ test: %r{^api/insights/v1/ack(/[^/]*)?/?$},
278
287
  tag_name: :tags,
279
288
  permissions: {
289
+ 'GET' => :view_advisor,
280
290
  'POST' => :edit_advisor,
291
+ 'PUT' => :edit_advisor,
281
292
  'DELETE' => :edit_advisor,
282
293
  },
283
294
  },
284
- # Advisor hostack endpoints - POST/DELETE require edit_advisor
295
+ # Advisor hostack endpoints - GET requires view_advisor, POST/PUT/DELETE require edit_advisor
285
296
  {
286
- test: %r{api/insights/v1/hostack(/[^/]*)?$},
297
+ test: %r{^api/insights/v1/hostack(/[^/]*)?/?$},
287
298
  tag_name: :tags,
288
299
  permissions: {
300
+ 'GET' => :view_advisor,
289
301
  'POST' => :edit_advisor,
302
+ 'PUT' => :edit_advisor,
290
303
  'DELETE' => :edit_advisor,
291
304
  },
292
305
  },
306
+ # Advisor rule ack_hosts - POST requires edit_advisor
307
+ {
308
+ test: %r{^api/insights/v1/rule/[^/]+/ack_hosts$},
309
+ tag_name: :tags,
310
+ permissions: {
311
+ 'POST' => :edit_advisor,
312
+ },
313
+ },
293
314
  # Advisor rule unack_hosts - POST requires edit_advisor
294
315
  {
295
- test: %r{api/insights/v1/rule/[^/]+/unack_hosts},
316
+ test: %r{^api/insights/v1/rule/[^/]+/unack_hosts$},
296
317
  tag_name: :tags,
297
318
  permissions: {
298
319
  'POST' => :edit_advisor,
@@ -300,22 +321,30 @@ module ForemanRhCloud
300
321
  },
301
322
  # Other Advisor/Insights endpoints - GET requires view_advisor
302
323
  {
303
- test: %r{api/insights/v1/.*},
324
+ test: %r{^api/insights/v1/.*},
304
325
  tag_name: :tags,
305
326
  permissions: {
306
327
  'GET' => :view_advisor,
307
328
  },
308
329
  },
309
- # Other API endpoints (tagging only, no permission enforcement)
310
- { test: %r{api/inventory/.*}, tag_name: :tags },
311
- { test: %r{api/tasks/.*}, tag_name: :tags },
312
330
  ].freeze
313
331
 
332
+ # Already resolved into organization/location by Foreman's taxonomy resolution; re-forwarding them breaks strict downstream specs (e.g. Vulnerability Engine Manager).
333
+ FOREMAN_TAXONOMY_PARAMS = %w[organization_id location_id].freeze
334
+
314
335
  def forward_request(original_request, path, controller_name, user, organization, location)
315
- # Check permissions before forwarding
316
- permissions = required_permission_for(path, original_request.request_method)
317
- if permissions && Array(permissions).none? { |p| user&.can?(p) }
318
- logger.warn("User #{user&.login || 'anonymous'} lacks permissions #{Array(permissions).join(', ')} for #{original_request.request_method} #{path}")
336
+ http_method = original_request.request_method
337
+ pattern = resolve_pattern(path)
338
+ permissions = pattern&.dig(:permissions, http_method)
339
+
340
+ # Deny-by-default: reject if no matching pattern or no permission mapping for this HTTP method
341
+ if Array(permissions).empty?
342
+ raise UnsupportedOperationError.new(N_("This operation is not supported"))
343
+ end
344
+
345
+ # Check user has the required permission(s)
346
+ if Array(permissions).none? { |p| user&.can?(p) }
347
+ logger.warn("User #{user&.login || 'anonymous'} lacks permissions #{Array(permissions).join(', ')} for #{http_method} #{path}")
319
348
  raise ::Foreman::PermissionMissingException.new(N_("You do not have permission to perform this action"))
320
349
  end
321
350
 
@@ -374,7 +403,7 @@ module ForemanRhCloud
374
403
  end
375
404
 
376
405
  def prepare_forward_params(original_request, path, user:, organization:, location:)
377
- forward_params = original_request.query_parameters.to_a
406
+ forward_params = original_request.query_parameters.except(*FOREMAN_TAXONOMY_PARAMS).to_a
378
407
 
379
408
  tag_name = scope_request?(original_request, path)
380
409
  forward_params += prepare_tags(user, organization, location, tag_name) if tag_name
@@ -401,22 +430,7 @@ module ForemanRhCloud
401
430
  def scope_request?(original_request, path)
402
431
  return nil unless original_request.get?
403
432
 
404
- # Find patterns that match this path AND are relevant for GET requests.
405
- # A pattern is relevant if it either:
406
- # - Has tag_name defined (supports scoping)
407
- # - Has GET permissions defined (explicitly handles GET, even without tags)
408
- # This ensures patterns like api/vulnerability/v1/cves/[^/]+$ (GET without tags)
409
- # take precedence over general patterns, while POST-only patterns are ignored.
410
- matching_patterns = SCOPED_REQUESTS.select do |pattern|
411
- pattern[:test].match?(path) && (pattern[:tag_name] || pattern.dig(:permissions, 'GET'))
412
- end
413
- return nil if matching_patterns.empty?
414
-
415
- # Choose the most specific pattern by regex source length
416
- request_pattern = matching_patterns.max_by { |pattern| pattern[:test].source.length }
417
-
418
- # Return the tag_name (may be nil if the most specific pattern doesn't support tags)
419
- request_pattern[:tag_name]
433
+ resolve_pattern(path)&.dig(:tag_name)
420
434
  end
421
435
 
422
436
  def core_app_name
@@ -435,26 +449,26 @@ module ForemanRhCloud
435
449
  Foreman::Logging.logger('app')
436
450
  end
437
451
 
438
- # Returns the required permission for the given path and HTTP method
439
- # Resolves overlapping patterns by choosing the most specific matcher,
440
- # defined as the one with the longest regex source that matches the path.
441
- # This avoids permission changes caused by reordering SCOPED_REQUESTS.
452
+ # Resolves the most specific SCOPED_REQUESTS pattern matching the given path.
453
+ # When multiple patterns match, chooses the one with the longest regex source
454
+ # as a proxy for specificity.
442
455
  # @param path [String] The request path
443
- # @param http_method [String] The HTTP method (GET, POST, etc.)
444
- # @return [Symbol, nil] The required permission symbol or nil if no permission required
445
- def required_permission_for(path, http_method)
446
- # Collect all matching patterns
447
- matching_patterns = SCOPED_REQUESTS.select { |pattern| pattern[:test].match?(path) }
448
- return nil if matching_patterns.empty?
449
-
450
- # Choose the most specific pattern: longest regex source wins.
451
- # This makes overlapping patterns deterministic and independent of array order.
452
- request_pattern = matching_patterns.max_by { |pattern| pattern[:test].source.length }
456
+ # @return [Hash, nil] The matching pattern hash, or nil if no pattern matches
457
+ def resolve_pattern(path)
458
+ normalized = path.chomp('/')
459
+ matching = SCOPED_REQUESTS.select { |pattern| pattern[:test].match?(normalized) }
460
+ return nil if matching.empty?
453
461
 
454
- permissions = request_pattern[:permissions]
455
- return nil unless permissions
462
+ matching.max_by { |pattern| pattern[:test].source.length }
463
+ end
456
464
 
457
- permissions[http_method]
465
+ # Returns the required permission for the given path and HTTP method.
466
+ # @param path [String] The request path
467
+ # @param http_method [String] The HTTP method (GET, POST, etc.)
468
+ # @return [Symbol, Array<Symbol>] The required permission(s)
469
+ # @return [nil] No matching pattern or method not listed in the pattern's permissions
470
+ def required_permission_for(path, http_method)
471
+ resolve_pattern(path)&.dig(:permissions, http_method)
458
472
  end
459
473
  end
460
474
  end