Martin Peřina has uploaded a new change for review.

Change subject: tools: Fix hostname validation in notifier
......................................................................

tools: Fix hostname validation in notifier

Removes hostname resolving during notifier startup. Instead it validates
hostname using regex to be valid hostname by RFC 1123 or valid IPv4
address.

Change-Id: Ic9f269ef71d7f3b73ac930afd01af6699ca26f9d
Signed-off-by: Martin Perina <mper...@redhat.com>
---
A 
backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/HostnameValidator.java
A 
backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/HostnameValidatorTest.java
M 
backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationProperties.java
3 files changed, 158 insertions(+), 12 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/26/24426/1

diff --git 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/HostnameValidator.java
 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/HostnameValidator.java
new file mode 100644
index 0000000..98d41aa
--- /dev/null
+++ 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/HostnameValidator.java
@@ -0,0 +1,54 @@
+package org.ovirt.engine.core.utils;
+
+import java.util.regex.Pattern;
+
+/**
+ * Validates if specified string is valid IPv4 address or hostname (RFC 1123)
+ */
+public class HostnameValidator {
+    /**
+     * Part of IPv4 address
+     */
+    private static final String IPV4_BLOCK = 
"([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])";
+
+    /**
+     * Regex to validate IPv4 address
+     */
+    private static final Pattern IPV4_PATTERN =
+            Pattern.compile("^(" + IPV4_BLOCK + "\\.){3}" + IPV4_BLOCK + "$");
+
+    /**
+     * Part of hostname
+     */
+    private static final String HOSTNAME_BLOCK = 
"([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])";
+
+    /**
+     * Regex to validate hostname as described in RFC 1123
+     */
+    private static final Pattern HOSTNAME_PATTERN =
+            Pattern.compile("^(" + HOSTNAME_BLOCK + "\\.)*" + HOSTNAME_BLOCK + 
"$");
+
+    /**
+     * Validates if specified address is valid IPv4 address
+     *
+     * @param address
+     *            address to check
+     * @return {@code true} if address if valid IPv4 address, otherwise {@code 
false}
+     */
+    public static boolean isIPv4Address(String address) {
+        return address != null
+                && IPV4_PATTERN.matcher(address).matches();
+    }
+
+    /**
+     * Validates if specified hostname is valid hostname definied by RFC 1123
+     *
+     * @param hostname
+     *            hostname to check
+     * @return {@code true} if hostname if valid hostname, otherwise {@code 
false}
+     */
+    public static boolean isHostname(String hostname) {
+        return hostname != null
+                && HOSTNAME_PATTERN.matcher(hostname).matches();
+    }
+}
diff --git 
a/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/HostnameValidatorTest.java
 
b/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/HostnameValidatorTest.java
new file mode 100644
index 0000000..a0aea1e
--- /dev/null
+++ 
b/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/HostnameValidatorTest.java
@@ -0,0 +1,93 @@
+package org.ovirt.engine.core.utils;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/**
+ * Tests for {@link HostnameValidator}
+ */
+@RunWith(JUnit4.class)
+public class HostnameValidatorTest {
+    /**
+     * Tests valid IPv4 addresses
+     */
+    @Test
+    public void testValidIPv4Addresses() {
+        assertTrue(HostnameValidator.isIPv4Address("127.0.0.1"));
+        assertTrue(HostnameValidator.isIPv4Address("10.0.0.1"));
+        assertTrue(HostnameValidator.isIPv4Address("192.168.1.1"));
+        assertTrue(HostnameValidator.isIPv4Address("0.0.0.0"));
+        assertTrue(HostnameValidator.isIPv4Address("255.255.255.255"));
+
+    }
+
+    /**
+     * Tests if address is valid IP address
+     *
+     * @param address
+     *            address to test
+     */
+    protected void failIfIPv4AddressIsValid(String address) {
+        if (HostnameValidator.isIPv4Address(address)) {
+            fail("Address '" + address + "' is not a valid IPv4 address!");
+        }
+    }
+
+    /**
+     * Tests invalid IPv4 addresses
+     */
+    @Test
+    public void testInvalidIPv4Addresses() {
+        failIfIPv4AddressIsValid(null);
+        failIfIPv4AddressIsValid("");
+        failIfIPv4AddressIsValid("0.0.0.");
+        failIfIPv4AddressIsValid("0.0.0.0.");
+        failIfIPv4AddressIsValid("256.0.0.0");
+        failIfIPv4AddressIsValid("0.256.0.0");
+        failIfIPv4AddressIsValid("0.0.256.0");
+        failIfIPv4AddressIsValid("0.0.0.256");
+    }
+
+    /**
+     * Tests valid hostnames
+     */
+    @Test
+    public void testValidHostnames() {
+        assertTrue(HostnameValidator.isHostname("localhost"));
+        assertTrue(HostnameValidator.isHostname("localhost.localdomain"));
+        assertTrue(HostnameValidator.isHostname("domain.com"));
+        assertTrue(HostnameValidator.isHostname("123.com"));
+        assertTrue(HostnameValidator.isHostname("domain-123.info"));
+        assertTrue(HostnameValidator.isHostname("www.domain.cz"));
+
+    }
+
+    /**
+     * Tests if hostname is not valid
+     *
+     * @param hostname
+     *            hostname to test
+     */
+    protected void failIfHostnameIsValid(String hostname) {
+        if (HostnameValidator.isHostname(hostname)) {
+            fail("Hostname '" + hostname + "' is not a valid!");
+        }
+    }
+
+    /**
+     * Tests invalid hostnames
+     */
+    @Test
+    public void testInvalidHostnames() {
+        failIfHostnameIsValid(null);
+        failIfHostnameIsValid("");
+        failIfHostnameIsValid(".com");
+        failIfHostnameIsValid("domain_123.com");
+        failIfHostnameIsValid("domain@com");
+    }
+
+}
diff --git 
a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationProperties.java
 
b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationProperties.java
index ca950c5..86bd1c8 100644
--- 
a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationProperties.java
+++ 
b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationProperties.java
@@ -1,14 +1,15 @@
 package org.ovirt.engine.core.notifier.utils;
 
-import org.apache.commons.lang.StringUtils;
-import org.ovirt.engine.core.notifier.filter.FirstMatchSimpleFilter;
-import org.ovirt.engine.core.utils.LocalConfig;
-import org.snmp4j.smi.OID;
-
-import javax.mail.internet.InternetAddress;
-import java.net.InetAddress;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
+
+import javax.mail.internet.InternetAddress;
+
+import org.apache.commons.lang.StringUtils;
+import org.ovirt.engine.core.notifier.filter.FirstMatchSimpleFilter;
+import org.ovirt.engine.core.utils.HostnameValidator;
+import org.ovirt.engine.core.utils.LocalConfig;
+import org.snmp4j.smi.OID;
 
 /**
  * Defines properties uses by the event notification service
@@ -275,14 +276,12 @@
     }
 
     private void validateHost(String propName, String propVal) {
-        try {
-            InetAddress.getAllByName(propVal);
-        } catch (Exception ex) {
+        if (!HostnameValidator.isHostname(propVal)
+                && !HostnameValidator.isIPv4Address(propVal)) {
             throw new IllegalArgumentException(
                     String.format(
                             GENERIC_MESSAGE + "cannot verify '%s' value",
-                            propName),
-                    ex);
+                            propName));
         }
     }
 


-- 
To view, visit http://gerrit.ovirt.org/24426
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic9f269ef71d7f3b73ac930afd01af6699ca26f9d
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Martin Peřina <mper...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to