Skip to content

Commit f53ed87

Browse files
committed
Add X-Org-Id header for IoP requests
1 parent 76d6f60 commit f53ed87

File tree

8 files changed

+43
-35
lines changed

8 files changed

+43
-35
lines changed

app/services/foreman_rh_cloud/cert_auth.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,17 @@ def cert_auth_available?(organization)
1212
def execute_cloud_request(params)
1313
organization = params.delete(:organization)
1414
certs = ForemanRhCloud.with_iop_smart_proxy? ? foreman_certificate : candlepin_id_cert(organization)
15-
final_params = {
15+
default_params = {
1616
ssl_client_cert: OpenSSL::X509::Certificate.new(certs[:cert]),
1717
ssl_client_key: OpenSSL::PKey.read(certs[:key]),
18-
}.deep_merge(params)
18+
}
19+
20+
if ForemanRhCloud.with_iop_smart_proxy?
21+
default_params[:headers] ||= {}
22+
default_params[:headers]['X-Org-Id'] = organization&.label
23+
end
24+
25+
final_params = default_params.compact.deep_merge(params)
1926

2027
super(final_params)
2128
end

app/services/foreman_rh_cloud/gateway_request.rb

Lines changed: 0 additions & 26 deletions
This file was deleted.

app/services/foreman_rh_cloud/insights_api_forwarder.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module ForemanRhCloud
44
class InsightsApiForwarder
5-
include ForemanRhCloud::GatewayRequest
5+
include ForemanRhCloud::CertAuth
66

77
SCOPED_REQUESTS = [
88
{ test: %r{api/vulnerability/v1/vulnerabilities/cves}, tag_name: :tags },
@@ -26,6 +26,8 @@ def forward_request(original_request, path, controller_name, user, organization,
2626

2727
request_opts = prepare_request_opts(original_request, path, forward_payload, forward_params)
2828

29+
request_opts[:organization] = organization
30+
2931
logger.debug("Sending request to: #{request_opts[:url]}")
3032

3133
execute_cloud_request(request_opts)

app/services/foreman_rh_cloud/tags_auth.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module ForemanRhCloud
22
class TagsAuth
3-
include GatewayRequest
3+
include CertAuth
44

55
TAG_NAMESPACE = 'sat_iam'.freeze
66
TAG_SHORT_NAME = 'scope'.freeze
@@ -24,6 +24,7 @@ def update_tag
2424

2525
payload = tags_query_payload
2626
params = {
27+
organization: @org,
2728
method: :post,
2829
url: "#{InsightsCloud.gateway_url}/tags",
2930
headers: {

lib/foreman_inventory_upload/async/upload_report_job.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ def command
4848
def env
4949
env_vars = super.merge(
5050
'FILES' => filename,
51-
'CER_PATH' => @cer_path
51+
'CER_PATH' => @cer_path,
52+
'ORG_ID' => organization.label
5253
)
5354

5455
http_proxy_string = ForemanRhCloud.http_proxy_string

lib/foreman_inventory_upload/scripts/uploader.sh.erb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ else
2727
AUTH_VAL="\"$RH_USERNAME\":\"$RH_PASSWORD\""
2828
fi
2929

30+
ORG_HEADER=""
31+
if [ -n "$ORG_ID" ]
32+
then
33+
ORG_HEADER='-H "X-Org-Id: '"$ORG_ID"'"'
34+
fi
35+
3036
# /tmp/a b/x.pem
3137
# curl --cert /tmp/a\ b/x.pem
3238

@@ -36,7 +42,7 @@ mkdir -p $DONE_DIR
3642

3743
for f in $FILES
3844
do
39-
curl -k -vvv -# --fail -F "file=@$f;type=application/vnd.redhat.qpc.tar+tgz" $DEST "$AUTH_KEY" "$AUTH_VAL"
45+
curl -k -vvv -# --fail -F "file=@$f;type=application/vnd.redhat.qpc.tar+tgz" $DEST "$AUTH_KEY" "$AUTH_VAL" "$ORG_HEADER"
4046
status=$?
4147
if [ $status -eq 0 ]; then
4248
mv $f $DONE_DIR

lib/insights_cloud/async/vmaas_reposcan_sync.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@ def plan(repo, *_args)
2323
return
2424
end
2525

26-
plan_self
26+
organization_id = Katello::Repository.find(repo_id).organization_id
27+
28+
plan_self(organization_id: organization_id)
2729
end
2830

2931
def run
3032
url = ::InsightsCloud.vmaas_reposcan_sync_url
3133

3234
response = execute_cloud_request(
35+
organization: organization,
3336
method: :put,
3437
url: url,
3538
headers: { 'Content-Type' => 'application/json' }
@@ -61,6 +64,10 @@ def rescue_strategy_for_self
6164
Dynflow::Action::Rescue::Skip
6265
end
6366

67+
def organization
68+
@organization ||= Organization.find(input[:organization_id])
69+
end
70+
6471
private
6572

6673
def logger

test/unit/lib/insights_cloud/async/vmaas_reposcan_sync_test.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@ class VmaasReposcanSyncTest < ActiveSupport::TestCase
55
include ForemanTasks::TestHelpers::WithInThreadExecutor
66

77
setup do
8-
@repo_payload = { id: 123 }
8+
@root = FactoryBot.build(:katello_root_repository, :fedora_17_x86_64_dev_root)
9+
@root.save(validate: false)
10+
@repo = FactoryBot.create(
11+
:katello_repository,
12+
:with_product,
13+
distribution_family: 'Red Hat',
14+
distribution_version: '7.5',
15+
root: @root
16+
)
17+
@repo_payload = { id: @repo.id }
918
@expected_url = 'https://example.com/api/v1/vmaas/reposcan/sync'
1019
InsightsCloud.stubs(:vmaas_reposcan_sync_url).returns(@expected_url)
1120
ForemanRhCloud.stubs(:with_iop_smart_proxy?).returns(true)
@@ -68,7 +77,8 @@ class VmaasReposcanSyncTest < ActiveSupport::TestCase
6877
params[:method] == :put &&
6978
params[:url] == @expected_url &&
7079
params[:headers].is_a?(Hash) &&
71-
params[:headers]['Content-Type'] == 'application/json'
80+
params[:headers]['Content-Type'] == 'application/json' &&
81+
params[:organization] == @repo.organization
7282
end
7383
.returns(mock_response)
7484

0 commit comments

Comments
 (0)