summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2018-03-28 21:59:57 -0700
committerTim Smith <tsmith@chef.io>2018-03-28 21:59:57 -0700
commite042718fe4a1a64026a15977bf4ac43f3bbccbcc (patch)
tree8f90830d84bf3828883b5603eda6ace58c1402c9
parent55ac0da13539f5a80080809ee9059b676dabc5a8 (diff)
downloadchef-validate_hostname.tar.gz
Do the bare minimum validation to make sure we don't get a FQDN on windowsvalidate_hostname
Throws out the full regex and instead just makes sure someone doesn't give us a FQDN, which will entirely fail the run and result in a infinite reboot. RuntimeError ------------ hostname[chefnode.example.com] (bft::windows line 1) had an error: RuntimeError: Windows hostnames cannot contain a period. Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--lib/chef/resource/hostname.rb15
-rw-r--r--spec/unit/resource/hostname_spec.rb25
2 files changed, 7 insertions, 33 deletions
diff --git a/lib/chef/resource/hostname.rb b/lib/chef/resource/hostname.rb
index 1c91e412a8..497176f45d 100644
--- a/lib/chef/resource/hostname.rb
+++ b/lib/chef/resource/hostname.rb
@@ -11,8 +11,6 @@ class Chef
introduced "14.0"
property :hostname, String,
- regex: /^([A-Za-z0-9])+([A-Za-z0-9-._])*$/, # http://rubular.com/r/c5xKppWX4i
- validation_message: "Hostnames can contain alphanumeric characters, -, ., and _ ,but must start with an alphanumeric character.",
description: "The hostname if different than the resource's name",
name_property: true
@@ -68,12 +66,12 @@ class Chef
action :set do
description "Sets the node's hostname"
- ohai "reload hostname" do
- plugin "hostname"
- action :nothing
- end
-
if node["platform_family"] != "windows"
+ ohai "reload hostname" do
+ plugin "hostname"
+ action :nothing
+ end
+
# set the hostname via /bin/hostname
declare_resource(:execute, "set hostname to #{new_resource.hostname}") do
command "/bin/hostname #{new_resource.hostname}"
@@ -205,8 +203,9 @@ class Chef
end
else # windows
+ raise "Windows hostnames cannot contain a period." if new_resource.hostname.match?(/./)
- # suppress EC2 config service from setting our hostname
+ # suppress EC2 config service from setting our hostname
if ::File.exist?('C:\Program Files\Amazon\Ec2ConfigService\Settings\config.xml')
xml_contents = updated_ec2_config_xml
if xml_contents.empty?
diff --git a/spec/unit/resource/hostname_spec.rb b/spec/unit/resource/hostname_spec.rb
index b7219e2d98..33f944dbc9 100644
--- a/spec/unit/resource/hostname_spec.rb
+++ b/spec/unit/resource/hostname_spec.rb
@@ -25,31 +25,6 @@ describe Chef::Resource::Hostname do
expect(resource.resource_name).to eql(:hostname)
end
- it "hostname fails validation with invalid characters" do
- expect { resource.hostname "something^" }.to raise_error Chef::Exceptions::ValidationFailed
- expect { resource.hostname "something^" }.to raise_error Chef::Exceptions::ValidationFailed
- expect { resource.hostname "something!" }.to raise_error Chef::Exceptions::ValidationFailed
- expect { resource.hostname "something@" }.to raise_error Chef::Exceptions::ValidationFailed
- expect { resource.hostname "something#" }.to raise_error Chef::Exceptions::ValidationFailed
- expect { resource.hostname "something$" }.to raise_error Chef::Exceptions::ValidationFailed
- expect { resource.hostname "something%" }.to raise_error Chef::Exceptions::ValidationFailed
- expect { resource.hostname "something*" }.to raise_error Chef::Exceptions::ValidationFailed
- expect { resource.hostname "something[" }.to raise_error Chef::Exceptions::ValidationFailed
- expect { resource.hostname "something]" }.to raise_error Chef::Exceptions::ValidationFailed
- expect { resource.hostname "something(" }.to raise_error Chef::Exceptions::ValidationFailed
- expect { resource.hostname "something)" }.to raise_error Chef::Exceptions::ValidationFailed
- end
-
- it "hostname fails validation if it starts with _, -, or ." do
- expect { resource.hostname ".something" }.to raise_error Chef::Exceptions::ValidationFailed
- expect { resource.hostname "-something" }.to raise_error Chef::Exceptions::ValidationFailed
- expect { resource.hostname "_something" }.to raise_error Chef::Exceptions::ValidationFailed
- end
-
- it "hostname can contain ., _, or -" do
- expect { resource.hostname "something._-something" }.to_not raise_error Chef::Exceptions::ValidationFailed
- end
-
it "has a default action of set" do
expect(resource.action).to eql([:set])
end