Skip to content

Commit

Permalink
Fixes #16248 - sanitize improper virt-who hostnames (Katello#6266)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlsherrill authored Sep 13, 2016
1 parent f371390 commit fdecd6d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
3 changes: 2 additions & 1 deletion app/lib/actions/katello/host/hypervisors_update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def update_or_create_hypervisor(hypervisor_json)

# Since host names must be unique yet hypervisors may have unique subscription
# facets in different orgs
duplicate_name = "virt-who-#{hypervisor_json[:name]}-#{organization.id}"
sanitized_name = ::Katello::Host::SubscriptionFacet.sanitize_name(hypervisor_json[:name])
duplicate_name = "virt-who-#{sanitized_name}-#{organization.id}"
host = ::Katello::Host::SubscriptionFacet.find_by(:uuid => hypervisor_json[:uuid]).try(:host)
host ||= ::Host.find_by(:name => duplicate_name)
if host && host.organization.try(:id) != organization.id
Expand Down
4 changes: 4 additions & 0 deletions app/models/katello/host/subscription_facet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ def self.find_host(name, organization)
hosts.first
end

def self.sanitize_name(name)
name.gsub('_', '-').chomp('.').downcase
end

def candlepin_consumer
@candlepin_consumer ||= Katello::Candlepin::Consumer.new(self.uuid)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ namespace :katello do
subscription_facet.save!

host = subscription_facet.host
if host.name.include?('_') || host.name != host.name.downcase || host.name.ends_with?('.')
host.name = host.name.gsub('_', '-').chomp('.').downcase
host.save!
end
host.name = ::Katello::Host::SubscriptionFacet.sanitize_name(host.name)
host.save! if host.name_changed?

Katello::Host::SubscriptionFacet.update_facts(subscription_facet.host, candlepin_attrs[:facts])
rescue StandardError => exception
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'katello_test_helper'
require 'rake'

module Katello
class UpdateSubscriptionFacetBackendDataTest < ActiveSupport::TestCase
def setup
Rake.application.rake_require 'katello/tasks/upgrades/3.0/update_subscription_facet_backend_data'

Rake::Task['katello:upgrades:3.0:update_subscription_facet_backend_data'].reenable
Rake::Task.define_task(:environment)
@host = hosts(:one)
end

def test_run
Katello::Host::SubscriptionFacet.where("id != #{@host.subscription_facet.id}").destroy_all
Katello::Host::SubscriptionFacet.any_instance.stubs(:host).returns(@host)
Katello::Candlepin::Consumer.any_instance.stubs(:consumer_attributes).returns(:facts => {:foo => :bar})

Katello::Host::SubscriptionFacet.expects(:update_facts).with(@host, :foo => :bar).once

Rake.application.invoke_task('katello:upgrades:3.0:update_subscription_facet_backend_data')
end
end
end
6 changes: 6 additions & 0 deletions test/models/host/subscription_facet_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ class SubscriptionFacetBase < ActiveSupport::TestCase
end

class SubscriptionFacetTest < SubscriptionFacetBase
def test_sanitize_name
assert_equal 'foo-bar', Host::SubscriptionFacet.sanitize_name('foo_bar')
assert_equal 'foobar', Host::SubscriptionFacet.sanitize_name('foobar.')
assert_equal 'foobar', Host::SubscriptionFacet.sanitize_name('FoOBar')
end

def test_search_release_version
subscription_facet.update_attributes!(:release_version => '7Server')

Expand Down

0 comments on commit fdecd6d

Please sign in to comment.