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



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

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

#disconnected_org?(org) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 63

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



142
143
144
145
146
147
148
149
150
151
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 142

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



81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 81

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

#get_branch_id_for_uuid(uuid) ⇒ Object



153
154
155
156
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 153

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

#get_content_host(uuid = nil) ⇒ Object



163
164
165
166
167
168
169
170
171
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 163

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



173
174
175
176
177
178
179
180
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 173

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



121
122
123
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 121

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

#get_http_options(include_user_id = false) ⇒ Object



202
203
204
205
206
207
208
209
210
211
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 202

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



197
198
199
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 197

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



72
73
74
75
76
77
78
79
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 72

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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 125

def get_mutual_tls_auth_options(org, ca_file, verify_peer, ssl_version)
  upstream = org.owner_details['upstreamConsumer']
  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



158
159
160
161
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 158

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

#get_plugin_parent_nameObject



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

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

#get_plugin_parent_versionObject



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

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



182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 182

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



218
219
220
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 218

def get_rha_plugin_name
  'redhat_access'
end

#get_rha_plugin_rpm_nameObject



222
223
224
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 222

def get_rha_plugin_rpm_name
  'foreman-redhat_access'
end

#get_rha_plugin_versionObject



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

def get_rha_plugin_version
  RedhatAccess::VERSION
end

#get_ssl_options_for_org(org, ca_file) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 103

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



94
95
96
97
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 94

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

#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 = org.owner_details['upstreamConsumer']
    return upstream && upstream['idCert'] ? true : false
  end
  false
end

#telemetry_enabled?(org) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 50

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)


59
60
61
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 59

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

#use_basic_auth?Boolean

Returns:

  • (Boolean)


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

def use_basic_auth?
  REDHAT_ACCESS_CONFIG[:enable_telemetry_basic_auth]
end

#user_login_to_hash(login) ⇒ Object



213
214
215
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 213

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