Module: RedhatAccess::Telemetry::LookUps

Defined Under Namespace

Classes: RecordNotFound

Instance Method Summary collapse

Instance Method Details

#can_mask_rules(user) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 17

def can_mask_rules(user)
  # #TODO move this to an auth class?
  # TODO move this to an auth class?
  return false if user.nil?
  return true if user.admin
  permissions = user.cached_roles.collect(&:permissions).flatten.map!(&:name)
  Rails.logger.debug("User can mask telemetry hosts : #{permissions.include?("rh_telemetry_configurations")}")
  permissions.include?("rh_telemetry_configurations")
end

#can_unregister_system(user) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 7

def can_unregister_system(user)
  # TODO: move this to an auth class?
  return false if user.nil?
  return true if user.admin
  permissions = user.cached_roles.collect(&:permissions).flatten.map!(&:name)
  # Rails.logger.debug("User can unregister telemetry hosts : #{ permissions.include?("rh_telemetry_configurations")}")
  # for now we allow all.
  true
end

#current_organizationObject



50
51
52
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 50

def current_organization
  Organization.current || Organization.find_by_id(session[:organization_id]) if session[:organization_id]
end

#disconnected_org?(org) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 67

def disconnected_org?(org)
  if org
    # TODO: fix hard coding
    org.redhat_repository_url != 'https://cdn.redhat.com'
  else
    raise(RecordNotFound, 'Organization not found or invalid')
  end
end

#get_basic_auth_options(org, ca_file, verify_peer, ssl_version) ⇒ Object



155
156
157
158
159
160
161
162
163
164
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 155

def get_basic_auth_options(org, ca_file, verify_peer, ssl_version)
  opts = {
    :user        => org.telemetry_configuration.portal_user,
    :password    => org.telemetry_configuration.portal_password,
    :ssl_ca_file => ca_file,
    :verify_ssl  => verify_peer
  }
  opts[:ssl_version] = ssl_version if ssl_version
  opts
end

#get_branch_id_for_org(org) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 85

def get_branch_id_for_org(org)
  if org
    owner = upstream_owner(org)
    if !owner['uuid']
      # ldebug('Org manifest not found or invalid in get_branch_id')
      raise(RecordNotFound, 'Branch ID not found for organization')
    else
      branch_id =  owner['uuid']
    end
  else
    raise(RecordNotFound, 'Organization not found or invalid')
  end
end

#get_branch_id_for_uuid(uuid) ⇒ Object



166
167
168
169
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 166

def get_branch_id_for_uuid(uuid)
  org = get_organization(uuid)
  get_branch_id_for_org org
end

#get_content_host(uuid = nil) ⇒ Object



176
177
178
179
180
181
182
183
184
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 176

def get_content_host(uuid = nil)
  uuid ||= params[:id]
  facet = Katello::Host::SubscriptionFacet.where(:uuid => uuid).first
  if facet.nil?
    User.as_anonymous_admin { Katello::Resources::Candlepin::Consumer.get(uuid) }
    return nil
  end
  ::Host::Managed.unscoped.find(facet.host_id)
end

#get_content_hosts(org) ⇒ Object



186
187
188
189
190
191
192
193
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 186

def get_content_hosts(org)
  if org
    host_ids = ::Host::Managed.authorized('view_hosts', ::Host::Managed).where({:organization_id => org.id}).pluck(:id)
    Katello::Host::SubscriptionFacet.where(:host_id => host_ids).pluck(:uuid)
  else
    raise(RecordNotFound, 'Organization not found or invalid')
  end
end

#get_default_ssl_ca_fileObject



126
127
128
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 126

def get_default_ssl_ca_file
  "#{RedhatAccess::Engine.root}/ca/rh_cert-api_chain.pem"
end

#get_http_options(include_user_id = false) ⇒ Object



215
216
217
218
219
220
221
222
223
224
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 215

def get_http_options(include_user_id = false)
  headers = {}
  if include_user_id && User.current
    headers = {:INSIGHTS_USER_ID => (User.current.)}
  end
  {:logger => Rails.logger,
   :http_proxy => get_portal_http_proxy,
   :user_agent => get_http_user_agent,
   :headers => headers}
end

#get_http_user_agentObject



210
211
212
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 210

def get_http_user_agent
  "#{get_plugin_parent_name}/#{get_plugin_parent_version};#{get_rha_plugin_name}/#{get_rha_plugin_version}"
end

#get_leaf_id(uuid) ⇒ Object



76
77
78
79
80
81
82
83
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 76

def get_leaf_id(uuid)
  system = get_content_host(uuid)
  if system.nil?
    Rails.logger.debug('Host not found or invalid')
    raise(RecordNotFound, 'Host not found or invalid')
  end
  uuid
end

#get_mutual_tls_auth_options(org, ca_file, verify_peer, ssl_version) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 130

def get_mutual_tls_auth_options(org, ca_file, verify_peer, ssl_version)
  upstream = upstream_owner(org)
  if !upstream || !upstream['idCert'] || !upstream['idCert']['cert'] || !upstream['idCert']['key']
    raise(RecordNotFound, 'Unable to get portal SSL credentials. Missing org manifest?')
  else
    opts = {
      :ssl_client_cert => OpenSSL::X509::Certificate.new(upstream['idCert']['cert']),
      :ssl_client_key  => OpenSSL::PKey::RSA.new(upstream['idCert']['key']),
      :ssl_ca_file     => ca_file,
      :verify_ssl      => verify_peer
    }
    opts[:ssl_version] = ssl_version if ssl_version
    Rails.logger.debug("Telemetry ssl options => ca_file:#{opts[:ssl_ca_file]} , peer verify #{opts[:verify_ssl]}")
    opts
  end
end

#get_organization(uuid) ⇒ Object



171
172
173
174
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 171

def get_organization(uuid)
  system = get_content_host(uuid)
  system.nil? ? nil : system.organization
end

#get_plugin_parent_nameObject



244
245
246
247
248
249
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 244

def get_plugin_parent_name
  if defined? ForemanThemeSatellite::SATELLITE_VERSION
    return 'Satellite'
  end
  'Foreman'
end

#get_plugin_parent_versionObject



251
252
253
254
255
256
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 251

def get_plugin_parent_version
  if defined? ForemanThemeSatellite::SATELLITE_VERSION
    return ForemanThemeSatellite::SATELLITE_VERSION.gsub(/[a-zA-Z ]/, "")
  end
  Foreman::Version.new.to_s
end

#get_portal_http_proxyObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 195

def get_portal_http_proxy
  proxy = nil
  if SETTINGS[:katello][:cdn_proxy] && SETTINGS[:katello][:cdn_proxy][:host]
    proxy_config = SETTINGS[:katello][:cdn_proxy]
    uri = URI('')
    uri.scheme = URI.parse(proxy_config[:host]).scheme
    uri.host = URI.parse(proxy_config[:host]).host
    uri.port = proxy_config[:port] if proxy_config[:port]
    uri.user =  ERB::Util.url_encode(proxy_config[:user]) if proxy_config[:user]
    uri.password = ERB::Util.url_encode(proxy_config[:password]) if proxy_config[:password]
    proxy = uri.to_s
  end
  proxy
end

#get_rha_plugin_nameObject

TODO: move version and name methods to generic utility



231
232
233
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 231

def get_rha_plugin_name
  'redhat_access'
end

#get_rha_plugin_rpm_nameObject



235
236
237
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 235

def get_rha_plugin_rpm_name
  'foreman-redhat_access'
end

#get_rha_plugin_versionObject



240
241
242
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 240

def get_rha_plugin_version
  RedhatAccess::VERSION
end

#get_ssl_options_for_org(org, ca_file) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 108

def get_ssl_options_for_org(org, ca_file)
  if org
    verify_peer = REDHAT_ACCESS_CONFIG[:telemetry_ssl_verify_peer] ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
    ssl_version = REDHAT_ACCESS_CONFIG[:telemetry_ssl_version] ? REDHAT_ACCESS_CONFIG[:telemetry_ssl_version] : nil
    ca_file = ca_file ? ca_file : get_default_ssl_ca_file
    Rails.logger.debug("Verify peer #{verify_peer}")
    if use_basic_auth?
      Rails.logger.debug("Using basic auth for portal communication")
      get_basic_auth_options(org, ca_file, verify_peer, ssl_version)
    else
      Rails.logger.debug("Using SSL auth for portal communication")
      get_mutual_tls_auth_options(org, ca_file, verify_peer, ssl_version)
    end
  else
    raise(RecordNotFound, 'Organization not found or invalid')
  end
end

#get_ssl_options_for_uuid(uuid, ca_file) ⇒ Object



99
100
101
102
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 99

def get_ssl_options_for_uuid(uuid, ca_file)
  org = get_organization(uuid)
  get_ssl_options_for_org(org, ca_file)
end

#get_telemetry_config(org) ⇒ Object



40
41
42
43
44
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 40

def get_telemetry_config(org)
  TelemetryConfiguration.find_or_create_by(:organization_id => org.id) do |conf|
    conf.enable_telemetry = true
  end
end

#insights_api_hostObject



46
47
48
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 46

def insights_api_host
  REDHAT_ACCESS_CONFIG[:telemetry_api_host]
end

#is_org_selected?Boolean

Returns:

  • (Boolean)


35
36
37
38
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 35

def is_org_selected?
  Rails.logger.debug("Org selected ? #{current_organization.nil?}")
  current_organization.nil? ? false : true
end

#is_susbcribed_to_redhat?(org) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 27

def is_susbcribed_to_redhat?(org)
  if org
    upstream = upstream_owner(org)
    return upstream && upstream['idCert'] ? true : false
  end
  false
end

#telemetry_enabled?(org) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 54

def telemetry_enabled?(org)
  if org
    conf = get_telemetry_config(org)
    return conf.nil? ? false : conf.enable_telemetry
  else
    raise(RecordNotFound, 'Organization not found or invalid')
  end
end

#telemetry_enabled_for_uuid?(uuid) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 63

def telemetry_enabled_for_uuid?(uuid)
  telemetry_enabled?(get_organization(uuid))
end

#upstream_owner(org) ⇒ Object



147
148
149
150
151
152
153
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 147

def upstream_owner(org)
  #We use a cache because owner_details is networkcall to Candlepin
  #We make a lot of these calls each time the UI is accessed
  Rails.cache.fetch("insights_upstream_owner-#{org.id}", expires_in: 1.minute) do
    org.owner_details['upstreamConsumer']
  end
end

#use_basic_auth?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 104

def use_basic_auth?
  REDHAT_ACCESS_CONFIG[:enable_telemetry_basic_auth]
end

#user_login_to_hash(login) ⇒ Object



226
227
228
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 226

def ()
  Digest::SHA1.hexdigest()
end