From 4db54231ee076dc1df6251c6990d4b4d7e6a6b24 Mon Sep 17 00:00:00 2001
From: BrunoQuaresma <bruno_nonato_quaresma@hotmail.com>
Date: Wed, 12 Jun 2024 20:15:24 +0000
Subject: [PATCH 1/3] chore: make sure directives are correct

---
 envbuilder.go                   |  6 +++++
 integration/integration_test.go | 42 +++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)

diff --git a/envbuilder.go b/envbuilder.go
index 307a55dd..19d61b47 100644
--- a/envbuilder.go
+++ b/envbuilder.go
@@ -342,6 +342,12 @@ func Run(ctx context.Context, options Options) error {
 		}
 	}
 
+	// Make sure the Dockerfile is using the correct directives to run envbuilder
+	buildParams.DockerfileContent = buildParams.DockerfileContent + "\n" +
+		"USER root \n" +
+		"WORKDIR / \n" +
+		"ENTRYPOINT [\"/.envbuilder/bin/envbuilder\"]"
+
 	HijackLogrus(func(entry *logrus.Entry) {
 		for _, line := range strings.Split(entry.Message, "\r") {
 			options.Logger(notcodersdk.LogLevelInfo, "#%d: %s", stageNumber, color.HiBlackString(line))
diff --git a/integration/integration_test.go b/integration/integration_test.go
index 4b0e82e8..87694f27 100644
--- a/integration/integration_test.go
+++ b/integration/integration_test.go
@@ -1354,6 +1354,48 @@ COPY --from=a /root/date.txt /date.txt`, testImageAlpine, testImageAlpine),
 	})
 }
 
+func TestDirectives(t *testing.T) {
+	t.Parallel()
+
+	srv := createGitServer(t, gitServerOptions{
+		files: map[string]string{
+			".devcontainer/entrypoint.sh": "#!/bin/sh\necho 'Hello, world!'",
+			".devcontainer/Dockerfile": "FROM " + testImageAlpine + "\n" +
+				"RUN addgroup -S coder && adduser -S coder -G coder \n" +
+				"USER coder\n" +
+				"WORKDIR /app\n" +
+				"ENTRYPOINT [\"./entrypoint.sh\"]",
+			".devcontainer/devcontainer.json": `{
+			"name": "Test",
+			"build": {
+				"dockerfile": "Dockerfile"
+			},
+		}`,
+		},
+	})
+
+	testReg := setupInMemoryRegistry(t, setupInMemoryRegistryOpts{})
+	testRepo := testReg + "/test"
+	ref, err := name.ParseReference(testRepo + ":latest")
+	require.NoError(t, err)
+
+	_, err = runEnvbuilder(t, options{env: []string{
+		envbuilderEnv("GIT_URL", srv.URL),
+		envbuilderEnv("CACHE_REPO", testRepo),
+		envbuilderEnv("PUSH_IMAGE", "1"),
+	}})
+	require.NoError(t, err)
+
+	image, err := remote.Image(ref)
+	require.NoError(t, err, "expected image to be present after build + push")
+	configFile, err := image.ConfigFile()
+	require.NoError(t, err, "expected image to return a config file")
+
+	require.Equal(t, configFile.Config.User, "root", "value does not match any of the possible root user values.")
+	require.Equal(t, configFile.Config.WorkingDir, "/", "expected image to have root working directory")
+	require.Equal(t, configFile.Config.Entrypoint, []string{"/.envbuilder/bin/envbuilder"}, "expected image to have envbuilder entrypoint")
+}
+
 type setupInMemoryRegistryOpts struct {
 	Username string
 	Password string

From 49659fd8cf880d1ff8eec2ca1233db54b58cd00f Mon Sep 17 00:00:00 2001
From: BrunoQuaresma <bruno_nonato_quaresma@hotmail.com>
Date: Thu, 13 Jun 2024 14:39:41 +0000
Subject: [PATCH 2/3] Only set base directives for pushed images

---
 envbuilder.go                   | 12 +++++++-----
 integration/integration_test.go |  2 +-
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/envbuilder.go b/envbuilder.go
index 19d61b47..f87846c7 100644
--- a/envbuilder.go
+++ b/envbuilder.go
@@ -342,11 +342,13 @@ func Run(ctx context.Context, options Options) error {
 		}
 	}
 
-	// Make sure the Dockerfile is using the correct directives to run envbuilder
-	buildParams.DockerfileContent = buildParams.DockerfileContent + "\n" +
-		"USER root \n" +
-		"WORKDIR / \n" +
-		"ENTRYPOINT [\"/.envbuilder/bin/envbuilder\"]"
+	if options.PushImage {
+		// Make sure the Dockerfile is using the correct directives to run envbuilder
+		buildParams.DockerfileContent = buildParams.DockerfileContent + "\n" +
+			"USER root \n" +
+			"WORKDIR / \n" +
+			"ENTRYPOINT [\"/.envbuilder/bin/envbuilder\"]"
+	}
 
 	HijackLogrus(func(entry *logrus.Entry) {
 		for _, line := range strings.Split(entry.Message, "\r") {
diff --git a/integration/integration_test.go b/integration/integration_test.go
index 87694f27..cd2274ae 100644
--- a/integration/integration_test.go
+++ b/integration/integration_test.go
@@ -1354,7 +1354,7 @@ COPY --from=a /root/date.txt /date.txt`, testImageAlpine, testImageAlpine),
 	})
 }
 
-func TestDirectives(t *testing.T) {
+func TestDefaultDirectivesOnPushedImage(t *testing.T) {
 	t.Parallel()
 
 	srv := createGitServer(t, gitServerOptions{

From bda261cbe3e34d2f6b6fc81f87be68d5b2b6cb20 Mon Sep 17 00:00:00 2001
From: BrunoQuaresma <bruno_nonato_quaresma@hotmail.com>
Date: Fri, 14 Jun 2024 17:49:23 +0000
Subject: [PATCH 3/3] Add a comment in tests warning about override

---
 integration/integration_test.go | 1 +
 1 file changed, 1 insertion(+)

diff --git a/integration/integration_test.go b/integration/integration_test.go
index cd2274ae..6aedf022 100644
--- a/integration/integration_test.go
+++ b/integration/integration_test.go
@@ -1362,6 +1362,7 @@ func TestDefaultDirectivesOnPushedImage(t *testing.T) {
 			".devcontainer/entrypoint.sh": "#!/bin/sh\necho 'Hello, world!'",
 			".devcontainer/Dockerfile": "FROM " + testImageAlpine + "\n" +
 				"RUN addgroup -S coder && adduser -S coder -G coder \n" +
+				// These should be overridden by the default directives
 				"USER coder\n" +
 				"WORKDIR /app\n" +
 				"ENTRYPOINT [\"./entrypoint.sh\"]",