YARN-8228. Added hostname length check for docker container.

Contributed by Shane Kumpf

(cherry picked from commit a966ec6e23)
This commit is contained in:
Eric Yang 2018-04-30 19:12:53 -04:00
parent 4c13e7e3a0
commit 0fc55a8f57
2 changed files with 19 additions and 0 deletions

View File

@ -197,6 +197,7 @@ public class DockerLinuxContainerRuntime implements LinuxContainerRuntime {
HOSTNAME_PATTERN); HOSTNAME_PATTERN);
private static final Pattern USER_MOUNT_PATTERN = Pattern.compile( private static final Pattern USER_MOUNT_PATTERN = Pattern.compile(
"(?<=^|,)([^:\\x00]+):([^:\\x00]+):([a-z]+)"); "(?<=^|,)([^:\\x00]+):([^:\\x00]+):([a-z]+)");
private static final int HOST_NAME_LENGTH = 64;
@InterfaceAudience.Private @InterfaceAudience.Private
public static final String ENV_DOCKER_CONTAINER_IMAGE = public static final String ENV_DOCKER_CONTAINER_IMAGE =
@ -534,6 +535,11 @@ public class DockerLinuxContainerRuntime implements LinuxContainerRuntime {
throw new ContainerExecutionException("Hostname '" + hostname throw new ContainerExecutionException("Hostname '" + hostname
+ "' doesn't match docker hostname pattern"); + "' 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);
}
} }
} }

View File

@ -1529,6 +1529,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") @SuppressWarnings("unchecked")
private void checkVolumeCreateCommand() private void checkVolumeCreateCommand()
throws PrivilegedOperationException, IOException { throws PrivilegedOperationException, IOException {