Class: ForemanRhCloud::CloudRequestForwarder

Inherits:
Object
  • Object
show all
Includes:
CloudRequest
Defined in:
app/services/foreman_rh_cloud/cloud_request_forwarder.rb

Instance Method Summary collapse

Methods included from CloudRequest

#execute_cloud_request

Instance Method Details

#connection_test_request?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 100

def connection_test_request?
  ->(request_path) { request_path =~ /redhat_access\/r\/insights\/?$/ }
end

#core_app_nameObject



111
112
113
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 111

def core_app_name
  BranchInfo.new.core_app_name
end

#core_app_versionObject



115
116
117
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 115

def core_app_version
  BranchInfo.new.core_app_version
end

#forward_request(original_request, controller_name, branch_id, certs) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 7

def forward_request(original_request, controller_name, branch_id, certs)
  forward_params = prepare_forward_params(original_request, branch_id)
  logger.debug("Request parameters for telemetry request: #{forward_params}")

  forward_payload = prepare_forward_payload(original_request, controller_name)

  logger.debug("User agent for telemetry is: #{http_user_agent original_request}")

  request_opts = prepare_request_opts(original_request, forward_payload, forward_params, certs)

  logger.debug("Sending request to: #{request_opts[:url]}")

  execute_cloud_request(request_opts)
rescue RestClient::Exception => error_response
  error_response.response
end

#http_user_agent(original_request) ⇒ Object



119
120
121
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 119

def http_user_agent(original_request)
  "#{core_app_name}/#{core_app_version};#{ForemanRhCloud::Engine.engine_name}/#{ForemanRhCloud::VERSION};#{original_request.env['HTTP_USER_AGENT']}"
end

#loggerObject



123
124
125
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 123

def logger
  Foreman::Logging.logger('app')
end

#original_headers(original_request) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 86

def original_headers(original_request)
  headers = {
    if_none_match: original_request.if_none_match,
    if_modified_since: original_request.if_modified_since,
  }.compact

  logger.debug("Sending headers: #{headers}")
  headers
end

#path_params(request_path, certs) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 62

def path_params(request_path, certs)
  case request_path
  when platform_request?
    {
      url: ForemanRhCloud.cert_base_url + request_path.sub('/redhat_access/r/insights/platform', '/api'),
      ssl_client_cert: OpenSSL::X509::Certificate.new(certs[:cert]),
      ssl_client_key: OpenSSL::PKey.read(certs[:key]),
    }
  when connection_test_request?
    {
      url: ForemanRhCloud.cert_base_url + '/api/apicast-tests/ping',
      ssl_client_cert: OpenSSL::X509::Certificate.new(certs[:cert]),
      ssl_client_key: OpenSSL::PKey.read(certs[:key]),
    }
  else # Legacy insights API
    {
      url: ForemanRhCloud.legacy_insights_url + request_path.sub('/redhat_access/r/insights', '/r/insights'),
      ssl_client_cert: OpenSSL::X509::Certificate.new(certs[:cert]),
      ssl_client_key: OpenSSL::PKey.read(certs[:key]),
      ssl_ca_file: ForemanRhCloud.legacy_insights_ca,
    }
  end
end

#platform_request?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 96

def platform_request?
  ->(request_path) { request_path.include? '/platform' }
end

#prepare_forward_cloud_url(base_url, request_path) ⇒ Object



104
105
106
107
108
109
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 104

def prepare_forward_cloud_url(base_url, request_path)
  cloud_path = request_path.sub('/redhat_access/r/insights/platform/', '')
                           .sub('/redhat_access/r/insights/', '')

  "#{base_url}/api/#{cloud_path}"
end

#prepare_forward_params(original_request, branch_id) ⇒ Object



53
54
55
56
57
58
59
60
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 53

def prepare_forward_params(original_request, branch_id)
  forward_params = original_request.query_parameters
  if original_request.user_agent && !original_request.user_agent.include?('redhat_access_cfme')
    forward_params = forward_params.merge(:branch_id => branch_id)
  end

  forward_params
end

#prepare_forward_payload(original_request, controller_name) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 40

def prepare_forward_payload(original_request, controller_name)
  forward_payload = original_request.request_parameters[controller_name]

  forward_payload = original_request.raw_post.clone if (original_request.post? || original_request.patch?) && original_request.raw_post
  forward_payload = original_request.body.read if original_request.put?

  forward_payload = original_request.params.slice(:file, :metadata) if original_request.params[:file]

  # fix rails behaviour for http PATCH:
  forward_payload = forward_payload.to_json if original_request.format.json? && original_request.patch? && forward_payload && !forward_payload.is_a?(String)
  forward_payload
end

#prepare_request_opts(original_request, forward_payload, forward_params, certs) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 24

def prepare_request_opts(original_request, forward_payload, forward_params, certs)
  base_params = {
    method: original_request.method,
    payload: forward_payload,
    headers: original_headers(original_request).merge(
      {
        params: forward_params,
        user_agent: http_user_agent(original_request),
        content_type: original_request.media_type.presence || original_request.format.to_s,
      }
    ),
  }
  requested_url = original_request.original_fullpath.end_with?('/') ? original_request.path + '/' : original_request.path
  base_params.merge(path_params(requested_url, certs))
end