Skip to content
This repository has been archived by the owner on Jun 27, 2024. It is now read-only.

Commit

Permalink
feat: HOME variable set when running as nonroot (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
lholota committed May 27, 2020
1 parent edb8f96 commit e16d138
Show file tree
Hide file tree
Showing 15 changed files with 103 additions and 57 deletions.
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
**/run eol=lf
*/services.d/* eol=lf
**/run eol=lf
**/runas eol=lf
**/runas eol=lf
**/loop eol=lf
1 change: 1 addition & 0 deletions alpine/example/loop
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/with-contenv ash

echo "TEST_VAR=$TEST_VAR"
echo "HOME=$HOME"

while :
do
Expand Down
1 change: 1 addition & 0 deletions alpine/fs/etc/cont-init.d/10-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ if [ "$PUID" -ne "0" ]
then
adduser -u $PUID -G nonroot -D nonroot
DISPLAY_USER="nonroot"
echo "/home/nonroot" > /var/run/s6/container_environment/HOME
fi

echo '
Expand Down
1 change: 1 addition & 0 deletions centos/example/loop
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/with-contenv bash

echo "TEST_VAR=$TEST_VAR"
echo "HOME=$HOME"

while :
do
Expand Down
2 changes: 2 additions & 0 deletions centos/fs/etc/cont-init.d/10-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ if [ "$PUID" != "0" ]
then
useradd -u $PUID -g $PGID nonroot
DISPLAY_USER="nonroot"

echo "/home/nonroot" > /var/run/s6/container_environment/HOME
fi

echo '
Expand Down
2 changes: 1 addition & 1 deletion tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ repositories {
dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.13'
testImplementation "org.testcontainers:testcontainers:1.14.1"
testImplementation 'io.homecentr:testcontainers-extensions:1.2.2'
testImplementation 'io.homecentr:testcontainers-extensions:1.3.1'
testImplementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.30'
testImplementation group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.30'
}
Expand Down
15 changes: 9 additions & 6 deletions tests/src/test/java/BaseRestartedAsNonRootShould.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import helpers.BaseDockerImageTagResolver;
import helpers.Image;
import io.homecentr.testcontainers.containers.GenericContainerEx;
import io.homecentr.testcontainers.containers.wait.strategy.WaitEx;
import io.homecentr.testcontainers.images.EnvironmentImageTagResolver;
import io.homecentr.testcontainers.images.PullPolicyEx;
import org.junit.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.output.Slf4jLogConsumer;
Expand All @@ -11,7 +14,7 @@
import java.time.Duration;

import static io.homecentr.testcontainers.WaitLoop.waitFor;
import static org.junit.Assert.*;
import static org.junit.Assert.assertFalse;

public class BaseRestartedAsNonRootShould {
private static final Logger logger = LoggerFactory.getLogger(BaseRunningAsRootShould.class);
Expand All @@ -20,11 +23,11 @@ public class BaseRestartedAsNonRootShould {

@BeforeClass
public static void before() {
_container = new GenericContainerEx<>(new EnvironmentImageTagResolver(Helpers.getDockerImageFallback()))
_container = new GenericContainerEx<>(new BaseDockerImageTagResolver())
.withEnv("PUID", "7000")
.withEnv("PGID", "8000")
.withRelativeFileSystemBind(Paths.get(Helpers.getExamplesDir(), "loop").toString(), "/usr/sbin/loop")
.withRelativeFileSystemBind(Paths.get(Helpers.getExamplesDir(), "run").toString(), "/etc/services.d/env-test/run")
.withRelativeFileSystemBind(Paths.get(Image.getExamplesDir(), "loop").toString(), "/usr/sbin/loop")
.withRelativeFileSystemBind(Paths.get(Image.getExamplesDir(), "run").toString(), "/etc/services.d/env-test/run")
.withImagePullPolicy(PullPolicyEx.never())
.waitingFor(WaitEx.forS6OverlayStart());

Expand Down
23 changes: 17 additions & 6 deletions tests/src/test/java/BaseRunningAsDefaultShould.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import helpers.BaseDockerImageTagResolver;
import helpers.Image;
import io.homecentr.testcontainers.containers.GenericContainerEx;
import io.homecentr.testcontainers.containers.wait.strategy.WaitEx;
import io.homecentr.testcontainers.images.EnvironmentImageTagResolver;
import io.homecentr.testcontainers.images.PullPolicyEx;
import org.junit.AfterClass;
import org.junit.BeforeClass;
Expand All @@ -22,9 +23,9 @@ public class BaseRunningAsDefaultShould {

@BeforeClass
public static void before() {
_container = new GenericContainerEx<>(new EnvironmentImageTagResolver(Helpers.getDockerImageFallback()))
.withRelativeFileSystemBind(Paths.get(Helpers.getExamplesDir(), "loop").toString(), "/usr/sbin/loop")
.withRelativeFileSystemBind(Paths.get(Helpers.getExamplesDir(), "run").toString(), "/etc/services.d/env-test/run")
_container = new GenericContainerEx<>(new BaseDockerImageTagResolver())
.withRelativeFileSystemBind(Paths.get(Image.getExamplesDir(), "loop").toString(), "/usr/sbin/loop")
.withRelativeFileSystemBind(Paths.get(Image.getExamplesDir(), "run").toString(), "/etc/services.d/env-test/run")
.withImagePullPolicy(PullPolicyEx.never())
.waitingFor(WaitEx.forS6OverlayStart());

Expand Down Expand Up @@ -53,15 +54,25 @@ public void writePuidIntoOutput() throws Exception {

@Test
public void runServiceAsDefaultUid() throws Exception {
int uid = _container.getProcessUid(Helpers.getShell() + " /usr/sbin/loop");
int uid = _container.getProcessUid(Image.getShell() + " /usr/sbin/loop");

assertEquals(7077, uid);
}

@Test
public void runServiceAsDefaultGid() throws Exception {
int gid = _container.getProcessGid(Helpers.getShell() + " /usr/sbin/loop");
int gid = _container.getProcessGid(Image.getShell() + " /usr/sbin/loop");

assertEquals(7077, gid);
}

@Test
public void updateHomeEnvironmentVariable() throws Exception {
waitFor(Duration.ofSeconds(10), () -> _container.getLogsAnalyzer().contains("HOME=/home/nonroot"));
}

@Test
public void createHomeDirectory() throws Exception {
waitFor(Duration.ofSeconds(10), () -> _container.execInContainer("ls", "/home/nonroot").getExitCode() == 0);
}
}
23 changes: 17 additions & 6 deletions tests/src/test/java/BaseRunningAsNonRootShould.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import helpers.BaseDockerImageTagResolver;
import helpers.Image;
import io.homecentr.testcontainers.containers.GenericContainerEx;
import io.homecentr.testcontainers.containers.wait.strategy.WaitEx;
import io.homecentr.testcontainers.images.EnvironmentImageTagResolver;
import io.homecentr.testcontainers.images.PullPolicyEx;
import org.junit.AfterClass;
import org.junit.BeforeClass;
Expand All @@ -22,11 +23,11 @@ public class BaseRunningAsNonRootShould {

@BeforeClass
public static void before() {
_container = new GenericContainerEx<>(new EnvironmentImageTagResolver(Helpers.getDockerImageFallback()))
_container = new GenericContainerEx<>(new BaseDockerImageTagResolver())
.withEnv("PUID", "7000")
.withEnv("PGID", "8000")
.withRelativeFileSystemBind(Paths.get(Helpers.getExamplesDir(), "loop").toString(), "/usr/sbin/loop")
.withRelativeFileSystemBind(Paths.get(Helpers.getExamplesDir(), "run").toString(), "/etc/services.d/env-test/run")
.withRelativeFileSystemBind(Paths.get(Image.getExamplesDir(), "loop").toString(), "/usr/sbin/loop")
.withRelativeFileSystemBind(Paths.get(Image.getExamplesDir(), "run").toString(), "/etc/services.d/env-test/run")
.withImagePullPolicy(PullPolicyEx.never())
.waitingFor(WaitEx.forS6OverlayStart());

Expand Down Expand Up @@ -55,15 +56,25 @@ public void writePuidIntoOutput() throws Exception {

@Test
public void runServiceAsPassedUid() throws Exception {
int uid = _container.getProcessUid(Helpers.getShell() + " /usr/sbin/loop");
int uid = _container.getProcessUid(Image.getShell() + " /usr/sbin/loop");

assertEquals(7000, uid);
}

@Test
public void runServiceAsPassedGid() throws Exception {
int gid = _container.getProcessGid(Helpers.getShell() + " /usr/sbin/loop");
int gid = _container.getProcessGid(Image.getShell() + " /usr/sbin/loop");

assertEquals(8000, gid);
}

@Test
public void updateHomeEnvironmentVariable() throws Exception {
waitFor(Duration.ofSeconds(10), () -> _container.getLogsAnalyzer().contains("HOME=/home/nonroot"));
}

@Test
public void createHomeDirectory() throws Exception {
waitFor(Duration.ofSeconds(10), () -> _container.execInContainer("ls", "/home/nonroot").getExitCode() == 0);
}
}
18 changes: 12 additions & 6 deletions tests/src/test/java/BaseRunningAsRootShould.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import helpers.BaseDockerImageTagResolver;
import helpers.Image;
import io.homecentr.testcontainers.containers.GenericContainerEx;
import io.homecentr.testcontainers.containers.wait.strategy.WaitEx;
import io.homecentr.testcontainers.images.EnvironmentImageTagResolver;
import io.homecentr.testcontainers.images.PullPolicyEx;
import org.junit.AfterClass;
import org.junit.BeforeClass;
Expand All @@ -22,11 +23,11 @@ public class BaseRunningAsRootShould {

@BeforeClass
public static void before() {
_container = new GenericContainerEx<>(new EnvironmentImageTagResolver(Helpers.getDockerImageFallback()))
_container = new GenericContainerEx<>(new BaseDockerImageTagResolver())
.withEnv("PUID", "0")
.withEnv("PGID", "0")
.withRelativeFileSystemBind(Paths.get(Helpers.getExamplesDir(), "loop").toString(), "/usr/sbin/loop")
.withRelativeFileSystemBind(Paths.get(Helpers.getExamplesDir(), "run").toString(), "/etc/services.d/env-test/run")
.withRelativeFileSystemBind(Paths.get(Image.getExamplesDir(), "loop").toString(), "/usr/sbin/loop")
.withRelativeFileSystemBind(Paths.get(Image.getExamplesDir(), "run").toString(), "/etc/services.d/env-test/run")
.withImagePullPolicy(PullPolicyEx.never())
.waitingFor(WaitEx.forS6OverlayStart());

Expand Down Expand Up @@ -55,15 +56,20 @@ public void writeZeroPuidIntoOutput() throws Exception {

@Test
public void runServiceAsRootUid() throws Exception {
int uid = _container.getProcessUid(Helpers.getShell() + " /usr/sbin/loop");
int uid = _container.getProcessUid(Image.getShell() + " /usr/sbin/loop");

assertEquals(0, uid);
}

@Test
public void runServiceAsRootGid() throws Exception {
int gid = _container.getProcessGid(Helpers.getShell() + " /usr/sbin/loop");
int gid = _container.getProcessGid(Image.getShell() + " /usr/sbin/loop");

assertEquals(0, gid);
}

@Test
public void updateHomeEnvironmentVariable() throws Exception {
waitFor(Duration.ofSeconds(10), () -> _container.getLogsAnalyzer().contains("HOME=/root"));
}
}
9 changes: 5 additions & 4 deletions tests/src/test/java/BaseRunningWithEmptyPgidShould.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import helpers.BaseDockerImageTagResolver;
import helpers.Image;
import io.homecentr.testcontainers.containers.GenericContainerEx;
import io.homecentr.testcontainers.containers.wait.strategy.WaitEx;
import io.homecentr.testcontainers.images.EnvironmentImageTagResolver;
import io.homecentr.testcontainers.images.PullPolicyEx;
import org.junit.BeforeClass;
import org.junit.Test;
Expand All @@ -22,11 +23,11 @@ public class BaseRunningWithEmptyPgidShould {

@BeforeClass
public static void before() {
_container = new GenericContainerEx<>(new EnvironmentImageTagResolver(Helpers.getDockerImageFallback()))
_container = new GenericContainerEx<>(new BaseDockerImageTagResolver())
.withStartupAttempts(1)
.withEnv("PGID", "")
.withRelativeFileSystemBind(Paths.get(Helpers.getExamplesDir(), "loop").toString(), "/usr/sbin/loop")
.withRelativeFileSystemBind(Paths.get(Helpers.getExamplesDir(), "run").toString(), "/etc/services.d/env-test/run")
.withRelativeFileSystemBind(Paths.get(Image.getExamplesDir(), "loop").toString(), "/usr/sbin/loop")
.withRelativeFileSystemBind(Paths.get(Image.getExamplesDir(), "run").toString(), "/etc/services.d/env-test/run")
.withImagePullPolicy(PullPolicyEx.never())
.waitingFor(WaitEx.forS6OverlayStart());

Expand Down
11 changes: 5 additions & 6 deletions tests/src/test/java/BaseRunningWithEmptyPuidShould.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import helpers.BaseDockerImageTagResolver;
import helpers.Image;
import io.homecentr.testcontainers.containers.GenericContainerEx;
import io.homecentr.testcontainers.containers.wait.strategy.WaitEx;
import io.homecentr.testcontainers.images.EnvironmentImageTagResolver;
import io.homecentr.testcontainers.images.PullPolicyEx;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
Expand All @@ -15,7 +15,6 @@
import java.time.Duration;

import static io.homecentr.testcontainers.WaitLoop.waitFor;
import static org.junit.Assert.*;

public class BaseRunningWithEmptyPuidShould {
private static final Logger logger = LoggerFactory.getLogger(BaseRunningAsRootShould.class);
Expand All @@ -24,11 +23,11 @@ public class BaseRunningWithEmptyPuidShould {

@BeforeClass
public static void before() {
_container = new GenericContainerEx<>(new EnvironmentImageTagResolver(Helpers.getDockerImageFallback()))
_container = new GenericContainerEx<>(new BaseDockerImageTagResolver())
.withStartupAttempts(1)
.withEnv("PUID", "")
.withRelativeFileSystemBind(Paths.get(Helpers.getExamplesDir(), "loop").toString(), "/usr/sbin/loop")
.withRelativeFileSystemBind(Paths.get(Helpers.getExamplesDir(), "run").toString(), "/etc/services.d/env-test/run")
.withRelativeFileSystemBind(Paths.get(Image.getExamplesDir(), "loop").toString(), "/usr/sbin/loop")
.withRelativeFileSystemBind(Paths.get(Image.getExamplesDir(), "run").toString(), "/etc/services.d/env-test/run")
.withImagePullPolicy(PullPolicyEx.never())
.waitingFor(WaitEx.forS6OverlayStart());

Expand Down
35 changes: 18 additions & 17 deletions tests/src/test/java/BaseWithSecretFileVariableShould.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import helpers.BaseDockerImageTagResolver;
import helpers.Image;
import io.homecentr.testcontainers.containers.GenericContainerEx;
import io.homecentr.testcontainers.containers.wait.strategy.WaitEx;
import io.homecentr.testcontainers.images.EnvironmentImageTagResolver;
import io.homecentr.testcontainers.images.PullPolicyEx;
import org.junit.After;
import org.junit.Test;
Expand Down Expand Up @@ -28,10 +29,10 @@ public void after() {

@Test
public void setEnvVarFromSecretFileWhenEnvVarPassed() throws Exception {
_container = new GenericContainerEx<>(new EnvironmentImageTagResolver(Helpers.getDockerImageFallback()))
.withRelativeFileSystemBind(Paths.get(Helpers.getExamplesDir(), "loop").toString(), "/usr/sbin/loop")
.withRelativeFileSystemBind(Paths.get(Helpers.getExamplesDir(), "run").toString(), "/etc/services.d/env-test/run")
.withRelativeFileSystemBind(Paths.get(Helpers.getExamplesDir(), "var-value.txt").toString(), "/var-value.txt")
_container = new GenericContainerEx<>(new BaseDockerImageTagResolver())
.withRelativeFileSystemBind(Paths.get(Image.getExamplesDir(), "loop").toString(), "/usr/sbin/loop")
.withRelativeFileSystemBind(Paths.get(Image.getExamplesDir(), "run").toString(), "/etc/services.d/env-test/run")
.withRelativeFileSystemBind(Paths.get(Image.getExamplesDir(), "var-value.txt").toString(), "/var-value.txt")
.withEnv("FILE__TEST_VAR", "/var-value.txt")
.withImagePullPolicy(PullPolicyEx.never())
.waitingFor(WaitEx.forS6OverlayStart());
Expand All @@ -45,10 +46,10 @@ public void setEnvVarFromSecretFileWhenEnvVarPassed() throws Exception {

@Test
public void printToOutputThatVariableWasSetWhenEnvVarPassed() throws Exception {
_container = new GenericContainerEx<>(new EnvironmentImageTagResolver(Helpers.getDockerImageFallback()))
.withRelativeFileSystemBind(Paths.get(Helpers.getExamplesDir(), "loop").toString(), "/usr/sbin/loop")
.withRelativeFileSystemBind(Paths.get(Helpers.getExamplesDir(), "run").toString(), "/etc/services.d/env-test/run")
.withRelativeFileSystemBind(Paths.get(Helpers.getExamplesDir(), "var-value.txt").toString(), "/var-value.txt")
_container = new GenericContainerEx<>(new BaseDockerImageTagResolver())
.withRelativeFileSystemBind(Paths.get(Image.getExamplesDir(), "loop").toString(), "/usr/sbin/loop")
.withRelativeFileSystemBind(Paths.get(Image.getExamplesDir(), "run").toString(), "/etc/services.d/env-test/run")
.withRelativeFileSystemBind(Paths.get(Image.getExamplesDir(), "var-value.txt").toString(), "/var-value.txt")
.withEnv("FILE__TEST_VAR", "/var-value.txt")
.withImagePullPolicy(PullPolicyEx.never())
.waitingFor(WaitEx.forS6OverlayStart());
Expand All @@ -60,10 +61,10 @@ public void printToOutputThatVariableWasSetWhenEnvVarPassed() throws Exception {

@Test
public void printToOutputWhenPassedFileDoesNotExist() throws Exception {
_container = new GenericContainerEx<>(new EnvironmentImageTagResolver(Helpers.getDockerImageFallback()))
.withRelativeFileSystemBind(Paths.get(Helpers.getExamplesDir(), "loop").toString(), "/usr/sbin/loop")
.withRelativeFileSystemBind(Paths.get(Helpers.getExamplesDir(), "run").toString(), "/etc/services.d/env-test/run")
.withRelativeFileSystemBind(Paths.get(Helpers.getExamplesDir(), "var-value.txt").toString(), "/var-value.txt")
_container = new GenericContainerEx<>(new BaseDockerImageTagResolver())
.withRelativeFileSystemBind(Paths.get(Image.getExamplesDir(), "loop").toString(), "/usr/sbin/loop")
.withRelativeFileSystemBind(Paths.get(Image.getExamplesDir(), "run").toString(), "/etc/services.d/env-test/run")
.withRelativeFileSystemBind(Paths.get(Image.getExamplesDir(), "var-value.txt").toString(), "/var-value.txt")
.withEnv("FILE__TEST_VAR", "/not-existing.txt")
.withImagePullPolicy(PullPolicyEx.never())
.waitingFor(WaitEx.forS6OverlayStart());
Expand All @@ -76,10 +77,10 @@ public void printToOutputWhenPassedFileDoesNotExist() throws Exception {

@Test
public void skipWhenNoVariablesPassed() {
_container = new GenericContainerEx<>(new EnvironmentImageTagResolver(Helpers.getDockerImageFallback()))
.withRelativeFileSystemBind(Paths.get(Helpers.getExamplesDir(), "loop").toString(), "/usr/sbin/loop")
.withRelativeFileSystemBind(Paths.get(Helpers.getExamplesDir(), "run").toString(), "/etc/services.d/env-test/run")
.withRelativeFileSystemBind(Paths.get(Helpers.getExamplesDir(), "var-value.txt").toString(), "/var-value.txt")
_container = new GenericContainerEx<>(new BaseDockerImageTagResolver())
.withRelativeFileSystemBind(Paths.get(Image.getExamplesDir(), "loop").toString(), "/usr/sbin/loop")
.withRelativeFileSystemBind(Paths.get(Image.getExamplesDir(), "run").toString(), "/etc/services.d/env-test/run")
.withRelativeFileSystemBind(Paths.get(Image.getExamplesDir(), "var-value.txt").toString(), "/var-value.txt")
.withImagePullPolicy(PullPolicyEx.never())
.waitingFor(WaitEx.forS6OverlayStart());

Expand Down
9 changes: 9 additions & 0 deletions tests/src/test/java/helpers/BaseDockerImageTagResolver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package helpers;

import io.homecentr.testcontainers.images.EnvironmentImageTagResolver;

public class BaseDockerImageTagResolver extends EnvironmentImageTagResolver {
public BaseDockerImageTagResolver() {
super("homecentr/base:local");
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package helpers;

import java.nio.file.Paths;

public class Helpers {
public static String getDockerImageFallback() {
return "homecentr/base:local-" + getBase();
}
public class Image {

public static String getExamplesDir() {
return Paths.get(
Expand Down

0 comments on commit e16d138

Please sign in to comment.