Skip to content

Commit

Permalink
adding hostname format validation
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasgabriel committed Dec 21, 2017
1 parent 2d95d40 commit c259d40
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
8 changes: 7 additions & 1 deletion app/models/ext_management_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def self.supported_types_and_descriptions_hash

validates :name, :presence => true, :uniqueness => {:scope => [:tenant_id]}
validates :hostname, :presence => true, :if => :hostname_required?
validate :hostname_uniqueness_valid?, :if => :hostname_required?
validate :hostname_uniqueness_valid?, :hostname_format_valid?, :if => :hostname_required?

scope :with_eligible_manager_types, ->(eligible_types) { where(:type => eligible_types) }

Expand All @@ -92,6 +92,12 @@ def hostname_uniqueness_valid?
errors.add(:hostname, N_("has to be unique per provider type")) if existing_hostnames.include?(hostname.downcase)
end

def hostname_format_valid?
return if Validators::Hostname.valid?(hostname)
error_msg = N_("is in a wrong format.")
errors.add(:hostname, _(error_msg))
end

include NewWithTypeStiMixin
include UuidMixin
include EmsRefresh::Manager
Expand Down
3 changes: 2 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class Application < Rails::Application
config.autoload_paths << Rails.root.join("app", "controllers", "mixins")
config.autoload_paths << Rails.root.join("lib")
config.autoload_paths << Rails.root.join("lib", "services")
config.autoload_paths << Rails.root.join("lib", "validators")

config.autoload_once_paths << Rails.root.join("lib", "vmdb", "console_methods.rb")

Expand Down Expand Up @@ -166,4 +167,4 @@ class Application < Rails::Application
TOPLEVEL_BINDING.eval('self').extend(Vmdb::ConsoleMethods)
end
end
end
end
18 changes: 18 additions & 0 deletions lib/validators/hostname.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Validators
module Hostname
IS_IPV4_OR_IPV6 = /((^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{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}))|:)))(%.+)?\s*$))/
IS_HOSTNAME = /^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])$/

def self.ipv4_or_ipv6?(hostname)
(hostname =~ IS_IPV4_OR_IPV6) ? true : false
end

def self.valid_hostname?(hostname)
(hostname =~ IS_HOSTNAME) ? true : false
end

def self.valid?(hostname)
self.valid_hostname?(hostname) || self.ipv4_or_ipv6?(hostname)
end
end
end
31 changes: 31 additions & 0 deletions spec/lib/validators/hostname_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
describe Validators::Hostname do
context "invalid hostname" do
it "return false if hostname has protocol" do
hostname = "http://example.com"
expect(described_class.valid?(hostname)).to be false
end
it "return false if hostname has path" do
hostname = "example.com/path"
expect(described_class.valid?(hostname)).to be false
end
it "return false if hostname has port" do
hostname = "example.com:8080"
expect(described_class.valid?(hostname)).to be false
end
end

context "valid hostname" do
it "return true if is a domain name" do
hostname = "example.com"
expect(described_class.valid?(hostname)).to be true
end
it "return true if is a ipv4" do
hostname = "10.10.10.10"
expect(described_class.valid?(hostname)).to be true
end
it "return true if is a ipv6" do
hostname = "2001:cdba:0000:0000:0000:0000:3257:9652"
expect(described_class.valid?(hostname)).to be true
end
end
end

0 comments on commit c259d40

Please sign in to comment.