From a966ec6e23b3ac8e233b2cf9b9ddaa6628a8c996 Mon Sep 17 00:00:00 2001 From: Eric Yang Date: Mon, 30 Apr 2018 19:12:53 -0400 Subject: [PATCH] YARN-8228. Added hostname length check for docker container. Contributed by Shane Kumpf --- .../linux/runtime/DockerLinuxContainerRuntime.java | 6 ++++++ .../linux/runtime/TestDockerContainerRuntime.java | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/linux/runtime/DockerLinuxContainerRuntime.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/linux/runtime/DockerLinuxContainerRuntime.java index 999b343628..9c05c59350 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/linux/runtime/DockerLinuxContainerRuntime.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/linux/runtime/DockerLinuxContainerRuntime.java @@ -199,6 +199,7 @@ public class DockerLinuxContainerRuntime implements LinuxContainerRuntime { HOSTNAME_PATTERN); private static final Pattern USER_MOUNT_PATTERN = Pattern.compile( "(?<=^|,)([^:\\x00]+):([^:\\x00]+):([a-z]+)"); + private static final int HOST_NAME_LENGTH = 64; @InterfaceAudience.Private public static final String ENV_DOCKER_CONTAINER_IMAGE = @@ -541,6 +542,11 @@ public class DockerLinuxContainerRuntime implements LinuxContainerRuntime { throw new ContainerExecutionException("Hostname '" + hostname + "' doesn't match docker hostname pattern"); } + if (hostname.length() > HOST_NAME_LENGTH) { + throw new ContainerExecutionException( + "Hostname can not be greater than " + HOST_NAME_LENGTH + + " characters: " + hostname); + } } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/linux/runtime/TestDockerContainerRuntime.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/linux/runtime/TestDockerContainerRuntime.java index a333bac4c1..6ad35b2a43 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/linux/runtime/TestDockerContainerRuntime.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/linux/runtime/TestDockerContainerRuntime.java @@ -1539,6 +1539,19 @@ public class TestDockerContainerRuntime { } } + @Test + public void testValidDockerHostnameLength() throws Exception { + String validLength = "example.test.site"; + DockerLinuxContainerRuntime.validateHostname(validLength); + } + + @Test(expected = ContainerExecutionException.class) + public void testInvalidDockerHostnameLength() throws Exception { + String invalidLength = + "exampleexampleexampleexampleexampleexampleexampleexample.test.site"; + DockerLinuxContainerRuntime.validateHostname(invalidLength); + } + @SuppressWarnings("unchecked") private void checkVolumeCreateCommand() throws PrivilegedOperationException, IOException {