Class: RedhatAccess::Api::TelemetryApiController

Inherits:
ApiController show all
Includes:
RedhatAccess::Authentication::ClientAuthentication, Telemetry::LookUps
Defined in:
app/controllers/redhat_access/api/telemetry_api_controller.rb

Instance Method Summary collapse

Methods included from Telemetry::LookUps

#can_mask_rules, #can_unregister_system, #current_organization_object, #disconnected_org?, #get_basic_auth_options, #get_branch_id_for_org, #get_branch_id_for_uuid, #get_content_host, #get_content_hosts, #get_default_ssl_ca_file, #get_http_options, #get_http_user_agent, #get_leaf_id, #get_mutual_tls_auth_options, #get_organization, #get_plugin_parent_name, #get_plugin_parent_version, #get_portal_http_proxy, #get_rha_plugin_name, #get_rha_plugin_rpm_name, #get_rha_plugin_version, #get_ssl_options_for_org, #get_ssl_options_for_uuid, #get_tapi_timeout, #get_telemetry_config, #get_upload_timeout, #insights_api_host, #is_org_selected?, #is_susbcribed_to_redhat?, #telemetry_enabled?, #telemetry_enabled_for_uuid?, #upstream_owner, #use_basic_auth?, #use_subsets?, #user_login_to_hash

Methods included from RedhatAccess::Authentication::ClientAuthentication

#authenticate_client, #cert_from_request, #cert_present?, #deny_access, #set_client_user

Methods inherited from ApiController

#api_request?, #http_error_response

Instance Method Details

#action_permissionObject



17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/redhat_access/api/telemetry_api_controller.rb', line 17

def action_permission
  case params[:action]
  when 'proxy'
    :proxy
  when 'connection_status'
    :connection_status
  else
    super
  end
end

#check_telemetry_enabledObject



28
29
30
# File 'app/controllers/redhat_access/api/telemetry_api_controller.rb', line 28

def check_telemetry_enabled
  render_telemetry_off unless telemetry_enabled?(current_organization_object)
end

#connection_statusObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/controllers/redhat_access/api/telemetry_api_controller.rb', line 66

def connection_status
  client = get_api_client
  res = client.call_tapi('GET', 'me', nil, nil, {timeout: get_tapi_timeout})
  Rails.logger.debug(res[:data])
  case res[:code]
  when 200
    resp = JSON.parse(res[:data])
    data = {
      :connectionStatus => 'Connected',
      :account => resp["account_number"],
      :company => resp["company"],
      :orgId  => resp["ord_id"]
    }
    render status: res[:code] , json: data
  when 401
    data = {
      :connectionStatus => 'Authentication Failure',
      :account => 'Unknown',
      :company => 'Unknown',
    }
    render status: 200 , json: data
  else
    data = {
      :connectionStatus => 'Connection Failed',
      :account => 'Unknown',
      :company => 'Unknown',
    }
    render status: 200 , json: data
  end
end

#get_auth_opts(creds) ⇒ Object



42
43
44
# File 'app/controllers/redhat_access/api/telemetry_api_controller.rb', line 42

def get_auth_opts(creds)
  return get_ssl_options_for_org(current_organization_object, nil)
end

#get_credsObject



36
37
38
39
40
# File 'app/controllers/redhat_access/api/telemetry_api_controller.rb', line 36

def get_creds
  # enable this once cert auth is fixed:
  # return User
  #return TelemetryProxyCredentials.limit(1)[0]
end

#get_current_organizationObject



62
63
64
# File 'app/controllers/redhat_access/api/telemetry_api_controller.rb', line 62

def get_current_organization
    current_organization_object
end

#get_machinesObject

# Returns an array of the machine IDs that this user has access to



52
53
54
55
56
57
58
59
# File 'app/controllers/redhat_access/api/telemetry_api_controller.rb', line 52

def get_machines
  #TODO err out if org is not selected
  machines = get_content_hosts(current_organization_object)
  if machines.empty?
    machines = ['NULL_SET']
  end
  machines.sort
end

#indexObject



46
47
48
# File 'app/controllers/redhat_access/api/telemetry_api_controller.rb', line 46

def index
  render :text => "Telemetry API"
end

#proxyObject

The method that “proxies” tapi requests over to Strata



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/controllers/redhat_access/api/telemetry_api_controller.rb', line 98

def proxy
  original_method = request.method
  original_params = request.query_parameters
  if request.user_agent and not request.user_agent.include?('redhat_access_cfme')
    original_params = add_branch_to_params(request.query_parameters)
  end
  original_payload = request.request_parameters[controller_name]
  if request.post? && request.raw_post
    original_payload = request.raw_post.clone
  elsif request.put?
    original_payload = request.body.read
  end
  resource = params[:path] == nil ? "/" : params[:path]

  if params[:file]
    original_payload = get_file_data(params)
  elsif request.format.json? && request.patch? && original_payload
    # because rails behaves stupidly for http PATCH:
    original_payload = original_payload.to_json unless original_payload.is_a? String
  end

  client = get_api_client
  res = client.call_tapi(original_method, URI.escape(resource), original_params, original_payload, {timeout: get_tapi_timeout}, use_subsets)
  #401 errors means our proxy is not configured right.
  #Change it to 502 to distinguish with local applications 401 errors
  resp_data = res[:data]
  if res[:code] == 401
    res[:code] = 502
    resp_data = {
        :message => 'Authentication to the Insights Service failed.',
        :headers => {}
    }
  end
  if resp_data.respond_to?(:headers)
    if resp_data.headers[:content_disposition]
      send_data resp_data, disposition: resp_data.headers[:content_disposition], type: resp_data.headers[:content_type]
      return
    end
    if resp_data.headers[:x_resource_count]
      response.headers['x-resource-count'] = resp_data.headers[:x_resource_count]
    end
    if resp_data.headers[:x_rh_insights_request_id]
      response.headers['x_rh_insights_request_id'] = resp_data.headers[:x_rh_insights_request_id]
    end
    render status: res[:code], json: resp_data
  else
    render status: res[:code], json: resp_data
  end
end

#render_telemetry_offObject



32
33
34
# File 'app/controllers/redhat_access/api/telemetry_api_controller.rb', line 32

def render_telemetry_off
  http_error_response("Telemetry is not enabled for your organization",403)
end