Skip to content

Commit

Permalink
Fixes #16890 - prevent discovery of managed hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
lzap authored and dLobatog committed Jan 20, 2017
1 parent eb3abdc commit 210f143
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
10 changes: 3 additions & 7 deletions app/models/host/discovered.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ class Host::Discovered < ::Host::Base
where(taxonomy_conditions).order("hosts.created_at DESC")
}

before_destroy { |record|
record.update_attribute(:managed, false)
}

def self.import_host facts
raise(::Foreman::Exception.new(N_("Invalid facts, must be a Hash"))) unless facts.is_a?(Hash)

Expand All @@ -49,18 +45,18 @@ def self.import_host facts
name_fact = return_first_valid_fact(Setting::Discovered.discovery_hostname_fact_array, facts)
raise(::Foreman::Exception.new(N_("Invalid facts: hash does not contain a valid value for any of the facts in the discovery_hostname setting: %s"), Setting::Discovered.discovery_hostname_fact_array.join(', '))) unless name_fact && name_fact.present?
hostname = normalize_string_for_hostname("#{hostname_prefix}#{name_fact}")
Rails.logger.warn "Hostname does not start with an alphabetical character" unless hostname.downcase.match /^[a-z]/
Rails.logger.warn "Hostname does not start with an alphabetical character" unless hostname.downcase.match(/^[a-z]/)

# find existing or create new host record
bootif_mac = FacterUtils::bootif_mac(facts).try(:downcase)
hosts = ::Nic::Managed.where(:mac => bootif_mac, :primary => true)
if hosts.size == 0
if hosts.empty?
host = Host.new(:name => hostname, :type => "Host::Discovered")
else
Rails.logger.warn "Multiple discovered hosts found with MAC address #{name_fact}, choosing one" if hosts.size > 1
host = hosts.first.host
end
host.type = "Host::Discovered"
raise ::Foreman::Exception.new("Host already exists as managed: %s", "#{host.name}/#{bootif_mac}") if host.type != "Host::Discovered"

# and save (interfaces are created via puppet parser extension)
host.save(:validate => false) if host.new_record?
Expand Down
9 changes: 9 additions & 0 deletions test/unit/host_discovered_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ class HostDiscoveredTest < ActiveSupport::TestCase
assert_match(/Unable to detect primary interface using MAC/, exception.message)
end

test "should not create discovered host when managed host exists" do
FactoryGirl.create(:host, :mac => 'E4:1F:13:CC:36:58')
raw = parse_json_fixture('/facts.json')
exception = assert_raises(::Foreman::Exception) do
Host::Discovered.import_host(raw['facts'])
end
assert_match(/Host already exists as managed/, exception.message)
end

test "should create discovered host with prefix" do
raw = parse_json_fixture('/facts.json')
Setting[:discovery_prefix] = 'test'
Expand Down

0 comments on commit 210f143

Please sign in to comment.