Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding hostname format validation #16714

Merged
merged 1 commit into from
Jan 8, 2018

Conversation

douglasgabriel
Copy link
Member

@douglasgabriel douglasgabriel commented Dec 21, 2017

This PR is based on this discussion:
ManageIQ/manageiq-ui-classic#2455

This PR depends on:
ManageIQ/more_core_extensions#58

This PR extends the UI validation to the backend model.

This PR is able to:

  • validate hostname format in backend;
  • avoid hostname have protocol;
  • avoid hostname have path;
  • avoid hostname have port.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use hostname.ipaddress? instead of adding the regex here.

See https://github.com/ManageIQ/more_core_extensions/blob/master/lib/more_core_extensions/core_ext/string/formats.rb#L28 if there are any missing edge cases.

end

def self.valid_hostname?(hostname)
(hostname =~ IS_HOSTNAME) ? true : false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use .domain_name? instead? https://github.com/ManageIQ/more_core_extensions/blob/master/lib/more_core_extensions/core_ext/string/formats.rb#L12

Again, if there are any other cases not handled in more_core_extensions, I'd prefer to fix / add them there for others to use.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I'm sorry, I didn't know this file. But I believe that I must update this file adding the validation for hostname, this because maybe we want to deal with domain names and hostnames in a different way.

I mean, maybe we want to domain names looks like "subdomain.domain" (as the regex that you put here said) and the hostname looks more flexible so can assume values like "h", "hostname", "host.name" and so on.

We can see more about this discussion in some places:

What do you think?

@douglasgabriel douglasgabriel force-pushed the ipr_hostname_validation branch 3 times, most recently from 0fb13be to ba0b230 Compare December 22, 2017 13:03
@douglasgabriel douglasgabriel changed the title Adding hostname format validation [WIP]Adding hostname format validation Dec 22, 2017
@douglasgabriel douglasgabriel changed the title [WIP]Adding hostname format validation [WIP] Adding hostname format validation Dec 22, 2017
@miq-bot miq-bot added the wip label Dec 22, 2017
@@ -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 hostname.ipaddress? || hostname.hostname?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be a good idea to update the Gemfile setting more_core_extensions to '~> 3.5'https://github.com/ManageIQ/manageiq/blob/master/Gemfile#L49

Copy link
Member Author

@douglasgabriel douglasgabriel Jan 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's done 👍

@douglasgabriel douglasgabriel changed the title [WIP] Adding hostname format validation Adding hostname format validation Jan 8, 2018
@miq-bot miq-bot removed the wip label Jan 8, 2018
def hostname_format_valid?
return if hostname.ipaddress? || hostname.hostname?
error_msg = N_("is in a wrong format.")
errors.add(:hostname, _(error_msg))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want:

errors.add(:hostname, _("format is invalid."))

Is that correct @mzazrivec ?
https://github.com/ManageIQ/guides/blob/master/i18n.md#ruby--haml

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct.

@miq-bot
Copy link
Member

miq-bot commented Jan 8, 2018

Checked commit douglasgabriel@7697bbe with ruby 2.3.3, rubocop 0.47.1, haml-lint 0.20.0, and yamllint 1.10.0
2 files checked, 0 offenses detected
Everything looks fine. 🏆

@bdunne bdunne merged commit 9d63cd7 into ManageIQ:master Jan 8, 2018
@bdunne bdunne added this to the Sprint 77 Ending Jan 15, 2018 milestone Jan 8, 2018
@bdunne bdunne self-assigned this Jan 8, 2018
simaishi pushed a commit that referenced this pull request Jan 9, 2018
Adding hostname format validation
(cherry picked from commit 9d63cd7)
@simaishi
Copy link
Contributor

simaishi commented Jan 9, 2018

Gaprindashvili backport details:

$ git log -1
commit ba99cb7e5c0018cd4cb9b4d92ae51b1fbef1961e
Author: Brandon Dunne <brandondunne@hotmail.com>
Date:   Mon Jan 8 14:58:57 2018 -0500

    Merge pull request #16714 from douglasgabriel/ipr_hostname_validation
    
    Adding hostname format validation
    (cherry picked from commit 9d63cd76ece8c9c1f07b33d1eae80b8d8711c6dd)

@himdel
Copy link
Contributor

himdel commented Jan 9, 2018

This PR breaks ui-classic travis.

Failure: https://travis-ci.org/ManageIQ/manageiq-ui-classic/jobs/326581784#L4126

Should be fixed by ManageIQ/manageiq-ui-classic#3205

@douglasgabriel douglasgabriel deleted the ipr_hostname_validation branch January 11, 2018 13:16
agrare added a commit to agrare/manageiq-providers-vmware that referenced this pull request Jan 12, 2018
ManageIQ/manageiq#16714 Caused the specs which
use invalid hostnames to fail in different ways then expected.  The test
checking for nil ems hostname is now invalid because that isn't possible
and the other changed the hostname to something with a space which can
be replaced with a '-'
@agrare
Copy link
Member

agrare commented Jan 12, 2018

Looks like this broke the VMware tests also, ManageIQ/manageiq-providers-vmware#173

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants