Class: ForemanRhCloud::InsightsApiForwarder

Inherits:
Object
  • Object
show all
Includes:
CertAuth
Defined in:
app/services/foreman_rh_cloud/insights_api_forwarder.rb

Constant Summary collapse

SCOPED_REQUESTS =
[
  { test: %r{api/vulnerability/v1/vulnerabilities/cves}, tag_name: :tags },
  { test: %r{api/vulnerability/v1/dashbar}, tag_name: :tags },
  { test: %r{api/vulnerability/v1/cves/[^/]+/affected_systems}, tag_name: :tags },
  { test: %r{api/vulnerability/v1/systems/[^/]+/cves}, tag_name: :tags },
  { test: %r{api/insights/.*}, tag_name: :tags },
  { test: %r{api/inventory/.*}, tag_name: :tags },
  { test: %r{api/tasks/.*}, tag_name: :tags },
].freeze

Instance Method Summary collapse

Methods included from CertAuth

#cert_auth_available?, #execute_cloud_request, #foreman_certificate

Methods included from InsightsCloud::CandlepinCache

#candlepin_id_cert, #cp_owner_id, #upstream_owner

Methods included from CloudRequest

#execute_cloud_request

Instance Method Details

#core_app_nameObject



104
105
106
# File 'app/services/foreman_rh_cloud/insights_api_forwarder.rb', line 104

def core_app_name
  BranchInfo.new.core_app_name
end

#core_app_versionObject



108
109
110
# File 'app/services/foreman_rh_cloud/insights_api_forwarder.rb', line 108

def core_app_version
  BranchInfo.new.core_app_version
end

#forward_request(original_request, path, controller_name, user, organization, location) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/services/foreman_rh_cloud/insights_api_forwarder.rb', line 17

def forward_request(original_request, path, controller_name, user, organization, location)
  TagsAuth.new(user, organization, location, logger).update_tag if scope_request?(original_request, path)

  forward_params = prepare_forward_params(original_request, path, user: user, organization: organization, location: location).to_a
  logger.debug("Request parameters for UI request: #{forward_params}")

  forward_payload = prepare_forward_payload(original_request, controller_name)

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

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

  request_opts[:organization] = organization

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

  execute_cloud_request(request_opts)
end

#http_user_agent(original_request) ⇒ Object



112
113
114
# File 'app/services/foreman_rh_cloud/insights_api_forwarder.rb', line 112

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



116
117
118
# File 'app/services/foreman_rh_cloud/insights_api_forwarder.rb', line 116

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

#original_headers(original_request) ⇒ Object



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

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(path) ⇒ Object



81
82
83
84
85
# File 'app/services/foreman_rh_cloud/insights_api_forwarder.rb', line 81

def path_params(path)
  {
    url: "#{InsightsCloud.ui_base_url}/#{path}",
  }
end

#prepare_forward_params(original_request, path, user:, organization:, location:) ⇒ Object



72
73
74
75
76
77
78
79
# File 'app/services/foreman_rh_cloud/insights_api_forwarder.rb', line 72

def prepare_forward_params(original_request, path, user:, organization:, location:)
  forward_params = original_request.query_parameters.to_a

  tag_name = scope_request?(original_request, path)
  forward_params += prepare_tags(user, organization, location, tag_name) if tag_name

  forward_params
end

#prepare_forward_payload(original_request, controller_name) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/services/foreman_rh_cloud/insights_api_forwarder.rb', line 59

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, path, forward_payload, forward_params) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/services/foreman_rh_cloud/insights_api_forwarder.rb', line 42

def prepare_request_opts(original_request, path, forward_payload, forward_params)
  base_params = {
    method: original_request.method,
    payload: forward_payload,
    headers: original_headers(original_request).merge(
      {
        params: RestClient::ParamsArray.new(forward_params),
        user_agent: http_user_agent(original_request),
        content_type: original_request.media_type.presence || original_request.format.to_s,
      }
    ),
  }
  params = path_params(path)

  base_params.merge(params)
end

#prepare_tags(user, organization, location, tag_name) ⇒ Object



36
37
38
39
40
# File 'app/services/foreman_rh_cloud/insights_api_forwarder.rb', line 36

def prepare_tags(user, organization, location, tag_name)
  [
    TagsAuth.auth_tag_for(user, organization, location),
  ].map { |tag_value| [tag_name, tag_value] }
end

#scope_request?(original_request, path) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
# File 'app/services/foreman_rh_cloud/insights_api_forwarder.rb', line 97

def scope_request?(original_request, path)
  return nil unless original_request.get?

  request_pattern = SCOPED_REQUESTS.find { |pattern| pattern[:test].match?(path) }
  request_pattern[:tag_name] if request_pattern
end