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

Prefer HOME environment variable #1263

Merged
merged 4 commits into from
Apr 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ChangeLog

* 0.34-SNAPSHOT
* **0.34-SNAPSHOT**
- Building 'spring-boot-with-jib' sample fails with NoSuchMethodError ([1384](https://github.com/fabric8io/docker-maven-plugin/issues/1384))
- Loading Image tarball into docker daemon fails in JIB mode ([1385](https://github.com/fabric8io/docker-maven-plugin/issues/1385))
- `assembly.inline` is removed when external properties are enabled ([1082](https://github.com/fabric8io/docker-maven-plugin/issues/1082))
Expand All @@ -16,6 +16,7 @@
- Retry port mapping to avoid race condition where complete port information may not be initially available in Docker Engine 20.10.5 ([1447](https://github.com/fabric8io/docker-maven-plugin/pull/1447))
- New `copy` goal for copying files and directories from containers to host ([752](https://github.com/fabric8io/docker-maven-plugin/issues/752) and [1405](https://github.com/fabric8io/docker-maven-plugin/pull/1405))
- Add support for multiple copy layers using multiple assemblies ([554](https://github.com/fabric8io/docker-maven-plugin/issues/554))
- Prefer HOME environment variable over the Java system property to determine the user's home directory to better resemble the golang client's behavior ([#1236](https://github.com/fabric8io/docker-maven-plugin/pull/1263)

* **0.34.1** (2020-09-27)
- Fix NPE with "skipPush" and no build configuration given ([#1381](https://github.com/fabric8io/docker-maven-plugin/issues/1381))
Expand Down
2 changes: 1 addition & 1 deletion src/main/asciidoc/inc/start/_volumes.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ You can use Maven variables in the path specifications. This should even work fo
----

You can also use relative paths. Relative paths are interpreted relative to the Maven project base directory. Paths
that begin with `~` are interpreted relative to the JVM's `user.home` directory.
that begin with `~` are interpreted relative to the JVM's `HOME` or `user.home` directory.

.Example with relative paths
[source,xml]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import io.fabric8.maven.docker.access.util.LocalSocketUtil;
import io.fabric8.maven.docker.util.*;

import static io.fabric8.maven.docker.util.EnvUtil.getUserHome;

/**
* Detector for determining the Docker access mechanism
*/
Expand Down Expand Up @@ -167,7 +169,7 @@ private void initCertPath(String certPath) throws IOException {
this.certPath = certPath != null ? certPath : System.getenv("DOCKER_CERT_PATH");
// Try default locations as last resort
if (this.certPath == null) {
File dockerHome = new File(System.getProperty("user.home") + "/.docker");
File dockerHome = new File(getUserHome() + "/.docker");
if (dockerHome.isDirectory()) {
String[] entries = dockerHome.list(SuffixFileFilter.PEM_FILTER);
if (entries == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import io.fabric8.maven.docker.assembly.DockerAssemblyConfigurationSource;
import org.yaml.snakeyaml.Yaml;

import static io.fabric8.maven.docker.util.EnvUtil.getUserHome;


/**
* Utility class for dealing with dockerfiles
Expand Down Expand Up @@ -249,11 +251,7 @@ public static Map<String,?> readKubeConfig() {
}

private static File getHomeDir() {
String homeDir = System.getProperty("user.home");
if (homeDir == null) {
homeDir = System.getenv("HOME");
}
return new File(homeDir);
return new File(getUserHome());
}

private static void updateMapWithArgValue(Map<String, String> result, Map<String, String> args, String argString) {
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/io/fabric8/maven/docker/util/EnvUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.UnaryOperator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -37,6 +38,9 @@ public class EnvUtil {

public static final String PROPERTY_COMBINE_POLICY_SUFFIX = "_combine";

// injection point for unit tests
private static UnaryOperator<String> systemGetEnv = System::getenv;

private EnvUtil() {}

// Convert docker host URL to an HTTP(s) URL
Expand Down Expand Up @@ -510,4 +514,16 @@ public static boolean isMaven350OrLater(MavenSession mavenSession) {
String mavenVersion = mavenSession.getSystemProperties().getProperty("maven.version", "3");
return greaterOrEqualsVersion(mavenVersion, "3.5.0");
}

/**
* Get User's HOME directory path
* @return a String value for user's home directory
*/
public static String getUserHome() {
String homeDir = systemGetEnv.apply("HOME");
if (homeDir == null) {
homeDir = System.getProperty("user.home");
}
return homeDir;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.regex.Pattern;

import static io.fabric8.maven.docker.util.DockerPathUtil.resolveAbsolutely;
import static io.fabric8.maven.docker.util.EnvUtil.getUserHome;

/**
* Utility methods for working with Docker volume bindings.
Expand Down Expand Up @@ -169,7 +170,7 @@ public static String resolveRelativeVolumeBinding(File baseDir, String bindingSt
if (isRelativePath(localPath)) {
File resolvedFile;
if (isUserHomeRelativePath(localPath)) {
resolvedFile = resolveAbsolutely(prepareUserHomeRelativePath(localPath), System.getProperty("user.home"));
resolvedFile = resolveAbsolutely(prepareUserHomeRelativePath(localPath), getUserHome());
} else {
if (!baseDir.isAbsolute()) {
throw new IllegalArgumentException("Base directory '" + baseDir + "' must be absolute.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@
import java.io.IOException;
import java.io.Writer;
import java.net.InetAddress;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.UnaryOperator;

import com.google.common.collect.ImmutableMap;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonNull;
import java.util.function.UnaryOperator;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.internal.reflect.ReflectionAccessor;
import io.fabric8.maven.docker.access.AuthConfig;
import io.fabric8.maven.docker.util.aws.AwsSdkAuthConfigFactory;
import mockit.Expectations;
Expand Down Expand Up @@ -294,13 +300,40 @@ private void executeWithTempHomeDir(HomeDirExecutor executor) throws IOException
String userHome = System.getProperty("user.home");
environmentVariables.clear("KUBECONFIG");
try {
File tempDir = Files.createTempDirectory("d-m-p").toFile();
System.setProperty("user.home", tempDir.getAbsolutePath());
executor.exec(tempDir);
} finally {
System.setProperty("user.home",userHome);
Field envField = EnvUtil.class.getDeclaredField("systemGetEnv");
ReflectionAccessor.getInstance().makeAccessible(envField);
@SuppressWarnings("unchecked")
UnaryOperator<String> origEnv = (UnaryOperator<String>) envField.get(null);
String origUserHome = System.getProperty("user.home");
try {
final AtomicReference<String> homeDir = new AtomicReference<>();
UnaryOperator<String> homeEnv = name -> {
return "HOME".equals(name) ? homeDir.get() : origEnv.apply(name);
};
envField.set(null, homeEnv);

// execute with HOME environment variable (preferred)
File tempDir = Files.createTempDirectory("d-m-p").toFile();
homeDir.set(tempDir.getAbsolutePath());
System.setProperty("user.home", "/dev/null/ignore/me");
executor.exec(tempDir);

// execute with user.home system property (fallback)
tempDir = Files.createTempDirectory("d-m-p").toFile();
homeDir.set(null);
System.setProperty("user.home", tempDir.getAbsolutePath());
executor.exec(tempDir);
} finally {
if (origUserHome == null) {
System.clearProperty("user.home");
} else {
System.setProperty("user.home", origUserHome);
}
envField.set(null, origEnv);
}
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new AssertionError(e);
}

}

interface HomeDirExecutor {
Expand Down