Class: Api::Rhsm::CandlepinProxiesController

Inherits:
V2::ApiController
  • Object
show all
Includes:
Katello::Authentication::ClientAuthentication
Defined in:
app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

IF_MODIFIED_SINCE_HEADER =
'If-Modified-Since'.freeze

Instance Method Summary collapse

Methods included from Katello::Authentication::ClientAuthentication

#add_candlepin_version_header, #authenticate_client, #cert_from_request, #cert_present?, #set_client_user

Instance Method Details

#async_hypervisors_updateObject

api :POST, “/hypervisors/OWNER” Note that this request comes in as content-type ‘text/plain’ so that tomcat won’t parse the json. Here we just pass the plain body through to candlepin



128
129
130
131
132
133
134
135
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 128

def async_hypervisors_update
  task = Katello::Resources::Candlepin::Consumer.async_hypervisors(owner: params[:owner],
                                                                   reporter_id: params[:reporter_id],
                                                                   raw_json: request.raw_post)
  async_task(::Actions::Katello::Host::Hypervisors, nil, :task_id => task['id'])

  render :json => task
end

#available_releasesObject



170
171
172
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 170

def available_releases
  render :json => @host.content_facet.try(:available_releases) || []
end

#consumer_activateObject

used for registering with activation keys api :POST, “/consumers”, N_(“Register a system with activation key (compatibility)”) param :activation_keys, String, :required => true



235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 235

def consumer_activate
  # Activation keys are userless by definition so use the internal generic user
  # Set it before calling find_activation_keys to allow communication with candlepin
  User.current = User.anonymous_admin
  additional_set_taxonomy
  activation_keys = find_activation_keys

  host = Katello::RegistrationManager.process_registration(rhsm_params, nil, activation_keys)

  update_host_registered_through(host, request.headers)
  host.reload

  render :json => Resources::Candlepin::Consumer.get(host.subscription_facet.uuid)
end

#consumer_checkinObject

api :PUT, “/consumers/:id/checkin/”, N_(“Update consumer check-in time”) param :date, String, :desc => N_(“check-in time”)



155
156
157
158
159
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 155

def consumer_checkin
  @host.update(:last_checkin => params[:date])
  Candlepin::Consumer.new(@host.subscription_facet.uuid, @host.organization.label).checkin(params[:date])
  render :json => Resources::Candlepin::Consumer.get(@host.subscription_facet.uuid)
end

#consumer_createObject

api :POST, “/environments/:environment_id/consumers”, N_(“Register a consumer in environment”)



213
214
215
216
217
218
219
220
221
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 213

def consumer_create
  host = Katello::RegistrationManager.process_registration(rhsm_params, find_content_view_environments)

  host.reload

  update_host_registered_through(host, request.headers)

  render :json => Resources::Candlepin::Consumer.get(host.subscription_facet.uuid)
end

#consumer_destroyObject

api :DELETE, “/consumers/:id”, N_(“Unregister a consumer”) param :id, String, :desc => N_(“UUID of the consumer”), :required => true



225
226
227
228
229
230
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 225

def consumer_destroy
  User.as_anonymous_admin do
    Katello::RegistrationManager.unregister_host(@host, :unregistering => !Setting['unregister_delete_host'])
  end
  render :plain => _("Deleted consumer '%s'") % params[:id], :status => :no_content
end

#consumer_showObject

api :GET, “/consumers/:id”, N_(“Show a system”) param :id, String, :desc => N_(“UUID of the consumer”), :required => true



107
108
109
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 107

def consumer_show
  render :json => Resources::Candlepin::Consumer.get(params[:id])
end

#deleteObject



87
88
89
90
91
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 87

def delete
  r = Resources::Candlepin::Proxy.delete(@request_path, @request_body.read)
  logger.debug filter_sensitive_data(r)
  render :json => r
end

#drop_api_namespace(original_request_path) ⇒ Object



70
71
72
73
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 70

def drop_api_namespace(original_request_path)
  prefix = "/rhsm"
  original_request_path.gsub(prefix, '')
end

#enabled_reposObject



200
201
202
203
204
205
206
207
208
209
210
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 200

def enabled_repos
  repos_params = params.dig('enabled_repos', 'repos')
  fail(HttpErrors::BadRequest, _("The request did not contain any repository information.")) if repos_params.nil?

  result = nil
  User.as_anonymous_admin do
    result = @host.import_enabled_repositories(repos_params)
  end

  respond_for_show :resource => result
end

#factsObject

api :PUT, “/consumers/:id”, N_(“Update consumer information”)



259
260
261
262
263
264
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 259

def facts
  User.current = User.anonymous_admin
  @host.update_candlepin_associations(rhsm_params)
  update_host_registered_through(@host, request.headers)
  render :json => {:content => _("Facts successfully updated.")}, :status => :ok
end

#getObject



75
76
77
78
79
80
81
82
83
84
85
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 75

def get
  extra_headers = {}
  modified_since = request.headers[IF_MODIFIED_SINCE_HEADER]
  if modified_since.present?
    extra_headers[IF_MODIFIED_SINCE_HEADER] = modified_since
  end

  r = Resources::Candlepin::Proxy.get(@request_path, extra_headers)
  logger.debug filter_sensitive_data(r)
  render :json => r, :status => r.code
end

#get_parent_host(headers) ⇒ Object



272
273
274
275
276
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 272

def get_parent_host(headers)
  hostnames = headers["HTTP_X_FORWARDED_HOST"]
  host = hostnames.split(/[,,:]/)[0].strip if hostnames
  host || URI.parse(Setting[:foreman_url]).host
end

#hypervisors_heartbeatObject



149
150
151
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 149

def hypervisors_heartbeat
  render json: Katello::Resources::Candlepin::Consumer.hypervisors_heartbeat(owner: params[:owner], reporter_id: params[:reporter_id])
end

#hypervisors_updateObject

api :POST, “/hypervisors”, N_(“Update the hypervisors information for environment”) desc ‘See virt-who tool for more details.’



139
140
141
142
143
144
145
146
147
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 139

def hypervisors_update
   = User.consumer? ? User.anonymous_api_admin. : User.current.
  task = User.as() do
    params['owner'] = @organization.label #override owner label if
    params['env'] = nil #hypervisors don't need an environment
    sync_task(::Actions::Katello::Host::Hypervisors, params.except(:controller, :action, :format).to_h)
  end
  render :json => task.output[:results]
end

#list_ownersObject



174
175
176
177
178
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 174

def list_owners
  orgs = User.current.allowed_organizations
  # rhsm expects owner (Candlepin format)
  respond_for_index :collection => orgs.map { |o| { :key => o.label, :displayName => o.name } }
end

#postObject



93
94
95
96
97
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 93

def post
  r = Resources::Candlepin::Proxy.post(@request_path, @request_body.read)
  logger.debug filter_sensitive_data(r)
  render :json => r
end

#proxy_request_bodyObject



66
67
68
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 66

def proxy_request_body
  @request_body = @_request.body
end

#proxy_request_pathObject



62
63
64
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 62

def proxy_request_path
  @request_path = drop_api_namespace(@_request.fullpath)
end

#putObject



99
100
101
102
103
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 99

def put
  r = Resources::Candlepin::Proxy.put(@request_path, @request_body.read)
  logger.debug filter_sensitive_data(r)
  render :json => r
end

#regenerate_identity_certificatesObject

api :POST, “/consumers/:id”, N_(“Regenerate consumer identity”) param :id, String, :desc => N_(“UUID of the consumer”) desc ‘Schedules the consumer identity certificate regeneration’



183
184
185
186
187
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 183

def regenerate_identity_certificates
  uuid = @host.subscription_facet.uuid
  Candlepin::Consumer.new(uuid, @host.organization.label).regenerate_identity_certificates
  render :json => Resources::Candlepin::Consumer.get(uuid)
end

#repackage_messageObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 36

def repackage_message
  yield
ensure
  if response.status >= 400
    begin
      body_json = JSON.parse(response.body)
      if body_json['message'] && body_json['displayMessage'].nil?
        body_json['displayMessage'] = body_json['message']
      end
      response.body = body_json.to_s
    rescue JSON::ParserError
      # Not a json response, leave as-is
    end
  end
end

#rhsm_indexObject

api :GET, “/owners/:organization_id/environments”, N_(“List environments for RHSM”)



112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 112

def rhsm_index
  @all_environments = get_content_view_environments(params[:name], Organization.current).collect do |env|
    {
      :id => env.cp_id,
      :name => env.label,
      :display_name => env.name,
      :description => env.content_view.description
    }
  end

  respond_for_index :collection => @all_environments
end

#serialsObject



266
267
268
269
270
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 266

def serials
  @host.subscription_facet.last_checkin = Time.now
  @host.subscription_facet.save!
  render :json => Katello::Resources::Candlepin::Consumer.serials(@host.subscription_facet.uuid)
end

#server_statusObject

api :GET, “/status”, N_(“Shows version information”) description N_(“This service is available for unauthenticated users”)



252
253
254
255
256
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 252

def server_status
  candlepin_response = Resources::Candlepin::CandlepinPing.ping
  candlepin_response[:managerCapabilities] << 'combined_reporting'
  render :json => candlepin_response
end

#upload_tracer_profileObject



163
164
165
166
167
168
# File 'app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb', line 163

def upload_tracer_profile
  User.as_anonymous_admin do
    @host.import_tracer_profile(params[:traces])
  end
  render json: { displayMessage: _("Tracer profile uploaded successfully") }
end