diff --git a/src/main/java/com/palantir/docker/compose/configuration/DaemonEnvironmentValidator.java b/src/main/java/com/palantir/docker/compose/configuration/DaemonEnvironmentValidator.java index bd7abf011..6672f7c71 100644 --- a/src/main/java/com/palantir/docker/compose/configuration/DaemonEnvironmentValidator.java +++ b/src/main/java/com/palantir/docker/compose/configuration/DaemonEnvironmentValidator.java @@ -21,18 +21,26 @@ import static com.palantir.docker.compose.configuration.EnvironmentVariables.DOCKER_TLS_VERIFY; import static java.util.stream.Collectors.joining; +import com.google.common.base.Supplier; +import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableSet; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; -public final class DaemonEnvironmentValidator { +public final class DaemonEnvironmentValidator implements EnvironmentValidator { private static final Set ILLEGAL_VARIABLES = ImmutableSet.of(DOCKER_TLS_VERIFY, DOCKER_HOST, DOCKER_CERT_PATH); + private static final Supplier SUPPLIER = Suppliers.memoize( + () -> new DaemonEnvironmentValidator()); + + public static DaemonEnvironmentValidator instance() { + return SUPPLIER.get(); + } private DaemonEnvironmentValidator() {} - public static Map validate(Map dockerEnvironment) { + public void validateEnvironmentVariables(Map dockerEnvironment) { Set invalidVariables = ILLEGAL_VARIABLES.stream() .filter(dockerEnvironment::containsKey) .collect(Collectors.toSet()); @@ -42,7 +50,6 @@ public static Map validate(Map dockerEnvironment "These variables were set: ", ". They cannot be set when connecting to a local docker daemon.")); checkState(invalidVariables.isEmpty(), errorMessage); - return dockerEnvironment; } } diff --git a/src/main/java/com/palantir/docker/compose/configuration/DockerType.java b/src/main/java/com/palantir/docker/compose/configuration/DockerType.java index a4ded3180..80e8dbfe9 100644 --- a/src/main/java/com/palantir/docker/compose/configuration/DockerType.java +++ b/src/main/java/com/palantir/docker/compose/configuration/DockerType.java @@ -15,20 +15,41 @@ */ package com.palantir.docker.compose.configuration; -import com.google.common.base.StandardSystemProperty; +import java.util.Map; +import java.util.Optional; -public enum DockerType { +public enum DockerType implements HostIpResolver, EnvironmentValidator { + DAEMON(DaemonEnvironmentValidator.instance(), new DaemonHostIpResolver()), + REMOTE(RemoteEnvironmentValidator.instance(), new RemoteHostIpResolver()); - DAEMON, REMOTE; + private final EnvironmentValidator validator; + private final HostIpResolver resolver; - public static final String MAC_OS = "Mac"; + DockerType(EnvironmentValidator validator, HostIpResolver resolver) { + this.validator = validator; + this.resolver = resolver; + } + + @Override + public void validateEnvironmentVariables(Map dockerEnvironment) { + validator.validateEnvironmentVariables(dockerEnvironment); + } + + @Override + public String resolveIp(String dockerHost) { + return resolver.resolveIp(dockerHost); + } - public static DockerType getDefaultLocalDockerType() { - if (StandardSystemProperty.OS_NAME.value().startsWith(MAC_OS)) { - return REMOTE; - } else { - return DAEMON; + public static Optional getFirstValidDockerTypeForEnvironment(Map environment) { + for (DockerType currType : DockerType.values()) { + try { + currType.validateEnvironmentVariables(environment); + return Optional.of(currType); + } catch (IllegalStateException e) { + // ignore and try next type + } } + return Optional.empty(); } } diff --git a/src/main/java/com/palantir/docker/compose/configuration/EnvironmentValidator.java b/src/main/java/com/palantir/docker/compose/configuration/EnvironmentValidator.java new file mode 100644 index 000000000..277881049 --- /dev/null +++ b/src/main/java/com/palantir/docker/compose/configuration/EnvironmentValidator.java @@ -0,0 +1,32 @@ +/* + * Copyright 2016 Palantir Technologies, Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.palantir.docker.compose.configuration; + +import java.util.Map; + +public interface EnvironmentValidator { + + /** + * Validates that the entries in the provided map are valid for the current environment. + * The provided map represents the environment variables that should be used for the + * process, where the keys are the environment variable names and the values are the values. + * If the validator determines the state represented by the map is invalid (either because + * required values are missing or forbidden values are present), the method should throw + * an exception. + */ + void validateEnvironmentVariables(Map dockerEnvironment); + +} diff --git a/src/main/java/com/palantir/docker/compose/configuration/RemoteEnvironmentValidator.java b/src/main/java/com/palantir/docker/compose/configuration/RemoteEnvironmentValidator.java index 71ccb2fc9..c91823c4a 100644 --- a/src/main/java/com/palantir/docker/compose/configuration/RemoteEnvironmentValidator.java +++ b/src/main/java/com/palantir/docker/compose/configuration/RemoteEnvironmentValidator.java @@ -30,18 +30,20 @@ import java.util.Set; import java.util.stream.Collectors; -public class RemoteEnvironmentValidator { +public final class RemoteEnvironmentValidator implements EnvironmentValidator { private static final Set SECURE_VARIABLES = ImmutableSet.of(DOCKER_TLS_VERIFY, DOCKER_CERT_PATH); + private static final RemoteEnvironmentValidator VALIDATOR = new RemoteEnvironmentValidator(); - private final Map dockerEnvironment; + public static RemoteEnvironmentValidator instance() { + return VALIDATOR; + } - public RemoteEnvironmentValidator(Map dockerEnvironment) { - this.dockerEnvironment = dockerEnvironment; + private RemoteEnvironmentValidator() { } - public Map validate() { - Collection missingVariables = getMissingEnvVariables(); + public void validateEnvironmentVariables(Map dockerEnvironment) { + Collection missingVariables = getMissingEnvVariables(dockerEnvironment); String errorMessage = missingVariables.stream() .collect(joining(", ", "Missing required environment variables: ", @@ -49,26 +51,22 @@ public Map validate() { + "ensure they are set on the DockerComposition.")); Preconditions.checkState(missingVariables.isEmpty(), errorMessage); - return dockerEnvironment; } - private Collection getMissingEnvVariables() { - Collection requiredVariables = Sets.union(newHashSet(DOCKER_HOST), secureVariablesRequired()); + private Collection getMissingEnvVariables(Map dockerEnvironment) { + Collection requiredVariables = Sets.union(newHashSet(DOCKER_HOST), + secureVariablesRequired(dockerEnvironment)); return requiredVariables.stream() .filter(envVariable -> Strings.isNullOrEmpty(dockerEnvironment.get(envVariable))) .collect(Collectors.toSet()); } - private Set secureVariablesRequired() { - return certVerificationEnabled() ? SECURE_VARIABLES : newHashSet(); + private Set secureVariablesRequired(Map dockerEnvironment) { + return certVerificationEnabled(dockerEnvironment) ? SECURE_VARIABLES : newHashSet(); } - private boolean certVerificationEnabled() { + private boolean certVerificationEnabled(Map dockerEnvironment) { return dockerEnvironment.containsKey(DOCKER_TLS_VERIFY); } - public static Map validate(Map dockerEnvironment) { - return new RemoteEnvironmentValidator(dockerEnvironment).validate(); - } - } diff --git a/src/main/java/com/palantir/docker/compose/connection/DockerMachine.java b/src/main/java/com/palantir/docker/compose/connection/DockerMachine.java index 0c3f2c88c..ae601bb28 100644 --- a/src/main/java/com/palantir/docker/compose/connection/DockerMachine.java +++ b/src/main/java/com/palantir/docker/compose/connection/DockerMachine.java @@ -23,18 +23,20 @@ import com.google.common.collect.ImmutableMap; import com.palantir.docker.compose.configuration.AdditionalEnvironmentValidator; -import com.palantir.docker.compose.configuration.DaemonEnvironmentValidator; -import com.palantir.docker.compose.configuration.DaemonHostIpResolver; import com.palantir.docker.compose.configuration.DockerType; -import com.palantir.docker.compose.configuration.HostIpResolver; -import com.palantir.docker.compose.configuration.RemoteEnvironmentValidator; import com.palantir.docker.compose.configuration.RemoteHostIpResolver; import com.palantir.docker.compose.execution.DockerConfiguration; import java.util.HashMap; import java.util.Map; +import java.util.Optional; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class DockerMachine implements DockerConfiguration { + private static final Logger log = LoggerFactory.getLogger(DockerMachine.class); + private static final DockerType FALLBACK_DOCKER_TYPE = DockerType.DAEMON; + private final String hostIp; private final Map environment; @@ -59,7 +61,15 @@ private void augmentGivenEnvironment(Map environmentToAugment) { } public static LocalBuilder localMachine() { - return localMachine(DockerType.getDefaultLocalDockerType()); + Map systemEnv = System.getenv(); + Optional dockerType = DockerType.getFirstValidDockerTypeForEnvironment(systemEnv); + if (!dockerType.isPresent()) { + log.debug( + "Failed to determine Docker type (daemon or remote) based on current environment. " + + "Proceeding with {} as the type.", FALLBACK_DOCKER_TYPE); + } + + return new LocalBuilder(dockerType.orElse(FALLBACK_DOCKER_TYPE), systemEnv); } public static LocalBuilder localMachine(DockerType dockerType) { @@ -88,14 +98,7 @@ public LocalBuilder withEnvironment(Map newEnvironment) { } public DockerMachine build() { - HostIpResolver hostIp; - if (DockerType.DAEMON == dockerType) { - DaemonEnvironmentValidator.validate(systemEnvironment); - hostIp = new DaemonHostIpResolver(); - } else { - RemoteEnvironmentValidator.validate(systemEnvironment); - hostIp = new RemoteHostIpResolver(); - } + dockerType.validateEnvironmentVariables(systemEnvironment); AdditionalEnvironmentValidator.validate(additionalEnvironment); Map environment = ImmutableMap.builder() .putAll(systemEnvironment) @@ -103,7 +106,7 @@ public DockerMachine build() { .build(); String dockerHost = systemEnvironment.getOrDefault(DOCKER_HOST, ""); - return new DockerMachine(hostIp.resolveIp(dockerHost), environment); + return new DockerMachine(dockerType.resolveIp(dockerHost), environment); } } @@ -146,7 +149,7 @@ public RemoteBuilder withEnvironment(Map newEnvironment) { } public DockerMachine build() { - RemoteEnvironmentValidator.validate(dockerEnvironment); + DockerType.REMOTE.validateEnvironmentVariables(dockerEnvironment); AdditionalEnvironmentValidator.validate(additionalEnvironment); String dockerHost = dockerEnvironment.getOrDefault(DOCKER_HOST, ""); diff --git a/src/test/java/com/palantir/docker/compose/configuration/DaemonEnvironmentValidatorShould.java b/src/test/java/com/palantir/docker/compose/configuration/DaemonEnvironmentValidatorShould.java index cc3dfc42a..b2431a273 100644 --- a/src/test/java/com/palantir/docker/compose/configuration/DaemonEnvironmentValidatorShould.java +++ b/src/test/java/com/palantir/docker/compose/configuration/DaemonEnvironmentValidatorShould.java @@ -18,8 +18,6 @@ import static com.palantir.docker.compose.configuration.EnvironmentVariables.DOCKER_CERT_PATH; import static com.palantir.docker.compose.configuration.EnvironmentVariables.DOCKER_HOST; import static com.palantir.docker.compose.configuration.EnvironmentVariables.DOCKER_TLS_VERIFY; -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; import com.google.common.collect.ImmutableMap; import java.util.Map; @@ -39,7 +37,7 @@ public void validate_successfully_when_docker_environment_does_not_contain_docke .put("ANOTHER_VARIABLE", "ANOTHER_VALUE") .build(); - assertThat(DaemonEnvironmentValidator.validate(variables), is(variables)); + DaemonEnvironmentValidator.instance().validateEnvironmentVariables(variables); } @Test @@ -56,7 +54,7 @@ public void throw_exception_when_docker_environment_contains_illegal_docker_vari exception.expectMessage(DOCKER_CERT_PATH); exception.expectMessage(DOCKER_TLS_VERIFY); exception.expectMessage("They cannot be set when connecting to a local docker daemon"); - assertThat(DaemonEnvironmentValidator.validate(variables), is(variables)); + DaemonEnvironmentValidator.instance().validateEnvironmentVariables(variables); } } diff --git a/src/test/java/com/palantir/docker/compose/configuration/DockerTypeShould.java b/src/test/java/com/palantir/docker/compose/configuration/DockerTypeShould.java new file mode 100644 index 000000000..5fb9e1783 --- /dev/null +++ b/src/test/java/com/palantir/docker/compose/configuration/DockerTypeShould.java @@ -0,0 +1,55 @@ +/* + * Copyright 2016 Palantir Technologies, Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.palantir.docker.compose.configuration; + +import static com.palantir.docker.compose.configuration.EnvironmentVariables.DOCKER_CERT_PATH; +import static com.palantir.docker.compose.configuration.EnvironmentVariables.DOCKER_HOST; +import static com.palantir.docker.compose.configuration.EnvironmentVariables.DOCKER_TLS_VERIFY; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +import com.google.common.collect.ImmutableMap; +import java.util.Map; +import java.util.Optional; +import org.junit.Test; + +public class DockerTypeShould { + + @Test + public void return_remote_as_first_valid_type_if_environment_is_illegal_for_daemon() { + Map variables = ImmutableMap.builder() + .put(DOCKER_HOST, "tcp://192.168.99.100:2376") + .put(DOCKER_TLS_VERIFY, "1") + .put(DOCKER_CERT_PATH, "/path/to/certs") + .build(); + assertThat(DockerType.getFirstValidDockerTypeForEnvironment(variables), is(Optional.of(DockerType.REMOTE))); + } + + @Test + public void return_daemon_as_first_valid_type_if_environment_is_illegal_for_remote() { + Map variables = ImmutableMap.of(); + assertThat(DockerType.getFirstValidDockerTypeForEnvironment(variables), is(Optional.of(DockerType.DAEMON))); + } + + @Test + public void return_absent_as_first_valid_type_if_environment_is_illegal_for_all() { + Map variables = ImmutableMap.builder() + .put(DOCKER_TLS_VERIFY, "1") + .build(); + assertThat(DockerType.getFirstValidDockerTypeForEnvironment(variables), is(Optional.empty())); + } + +} diff --git a/src/test/java/com/palantir/docker/compose/configuration/DockerTypeTest.java b/src/test/java/com/palantir/docker/compose/configuration/DockerTypeTest.java deleted file mode 100644 index 2552d791f..000000000 --- a/src/test/java/com/palantir/docker/compose/configuration/DockerTypeTest.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2016 Palantir Technologies, Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.palantir.docker.compose.configuration; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -import static org.junit.Assume.assumeFalse; -import static org.junit.Assume.assumeTrue; - -import com.google.common.base.StandardSystemProperty; -import org.junit.Test; - -public class DockerTypeTest { - - @Test - public void testDefaultDockerTypeForMacIsRemote() { - assumeTrue("Running on a non-Mac system; skipping Mac unit test", - StandardSystemProperty.OS_NAME.value().startsWith(DockerType.MAC_OS)); - assertThat(DockerType.REMOTE, is(DockerType.getDefaultLocalDockerType())); - } - - @Test - public void testDefaultDockerTypeForNonMacIsDaemon() { - assumeFalse("Running on a Mac system; skipping non-Mac unit test", - StandardSystemProperty.OS_NAME.value().startsWith(DockerType.MAC_OS)); - assertThat(DockerType.DAEMON, is(DockerType.getDefaultLocalDockerType())); - } - -} diff --git a/src/test/java/com/palantir/docker/compose/configuration/RemoteEnvironmentValidatorShould.java b/src/test/java/com/palantir/docker/compose/configuration/RemoteEnvironmentValidatorShould.java index 4ff69502b..ea745a1ef 100644 --- a/src/test/java/com/palantir/docker/compose/configuration/RemoteEnvironmentValidatorShould.java +++ b/src/test/java/com/palantir/docker/compose/configuration/RemoteEnvironmentValidatorShould.java @@ -18,8 +18,6 @@ import static com.palantir.docker.compose.configuration.EnvironmentVariables.DOCKER_CERT_PATH; import static com.palantir.docker.compose.configuration.EnvironmentVariables.DOCKER_HOST; import static com.palantir.docker.compose.configuration.EnvironmentVariables.DOCKER_TLS_VERIFY; -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; import com.google.common.collect.ImmutableMap; import java.util.Map; @@ -41,7 +39,7 @@ public void throw_exception_if_docker_host_is_not_set() throws Exception { exception.expect(IllegalStateException.class); exception.expectMessage("Missing required environment variables: "); exception.expectMessage(DOCKER_HOST); - RemoteEnvironmentValidator.validate(variables); + RemoteEnvironmentValidator.instance().validateEnvironmentVariables(variables); } @Test @@ -51,11 +49,10 @@ public void throw_exception_if_docker_cert_path_is_missing_and_tls_is_on() throw .put(DOCKER_TLS_VERIFY, "1") .build(); - new RemoteEnvironmentValidator(variables); exception.expect(IllegalStateException.class); exception.expectMessage("Missing required environment variables: "); exception.expectMessage(DOCKER_CERT_PATH); - RemoteEnvironmentValidator.validate(variables); + RemoteEnvironmentValidator.instance().validateEnvironmentVariables(variables); } @Test @@ -65,7 +62,7 @@ public void validate_environment_with_all_valid_variables_set_without_tls() thro .put("SOME_VARIABLE", "SOME_VALUE") .build(); - assertThat(RemoteEnvironmentValidator.validate(variables), is(variables)); + RemoteEnvironmentValidator.instance().validateEnvironmentVariables(variables); } @Test @@ -77,6 +74,6 @@ public void validate_environment_with_all_valid_variables_set_with_tls() throws .put("SOME_VARIABLE", "SOME_VALUE") .build(); - assertThat(RemoteEnvironmentValidator.validate(variables), is(variables)); + RemoteEnvironmentValidator.instance().validateEnvironmentVariables(variables); } }