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

#disconnected_org?(org) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 59

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



138
139
140
141
142
143
144
145
146
147
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 138

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



77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 77

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



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

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

#get_content_host(uuid = nil) ⇒ Object



159
160
161
162
163
164
165
166
167
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 159

def get_content_host(uuid = nil)
  uuid ||= params[:id]
  facet = Katello::Host::SubscriptionFacet.where(:uuid => uuid).first
  if facet.nil?
    User.as_anonymous_admin { Resources::Candlepin::Consumer.get(uuid) }
    raise HttpErrors::NotFound, _("Couldn't find consumer '%s'") % uuid
  end
  @host = facet.host
end

#get_content_hosts(org) ⇒ Object



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

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



117
118
119
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 117

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

#get_http_options(include_user_id = false) ⇒ Object



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

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



193
194
195
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 193

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



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

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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 121

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



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

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

#get_plugin_parent_nameObject



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

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

#get_plugin_parent_versionObject



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

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



178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 178

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



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

def get_rha_plugin_name
  'redhat_access'
end

#get_rha_plugin_rpm_nameObject



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

def get_rha_plugin_rpm_name
  'foreman-redhat_access'
end

#get_rha_plugin_versionObject



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

def get_rha_plugin_version
  RedhatAccess::VERSION
end

#get_ssl_options_for_org(org, ca_file) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 99

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



90
91
92
93
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 90

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 ? #{Organization.current.nil?}")
  Organization.current.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)


46
47
48
49
50
51
52
53
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 46

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

#telemetry_enabled_for_uuid?(uuid) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'app/services/redhat_access/telemetry/look_ups.rb', line 55

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

#use_basic_auth?Boolean

Returns:

  • (Boolean)


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

def use_basic_auth?
  REDHAT_ACCESS_CONFIG[:enable_telemetry_basic_auth]
end

#user_login_to_hash(login) ⇒ Object



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

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