Skip to content

Commit 4077a87

Browse files
authored
fix: set MagicDir to tempdir when performing cache probe (#48)
* fix: set MagicDir to tempdir when performing cache probe * chore: update envbuilder to b7781d8 * imgutil: get default envbuilder binary path from envbuilder options
1 parent 6137223 commit 4077a87

File tree

8 files changed

+67
-35
lines changed

8 files changed

+67
-35
lines changed

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ replace tailscale.com => github.com/coder/tailscale v1.1.1-0.20240702054557-aa55
1010

1111
require (
1212
github.com/GoogleContainerTools/kaniko v1.9.2
13-
github.com/coder/envbuilder v1.0.0-rc.0.0.20240830145058-fb7e689f39ed
13+
github.com/coder/envbuilder v1.0.0-rc.0.0.20240910082823-b7781d802f88
1414
github.com/coder/serpent v0.7.0
1515
github.com/docker/docker v26.1.5+incompatible
1616
github.com/gliderlabs/ssh v0.3.7
@@ -295,7 +295,7 @@ require (
295295
go4.org/netipx v0.0.0-20230728180743-ad4cb58a6516 // indirect
296296
golang.org/x/crypto v0.26.0 // indirect
297297
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect
298-
golang.org/x/mod v0.19.0 // indirect
298+
golang.org/x/mod v0.21.0 // indirect
299299
golang.org/x/net v0.26.0 // indirect
300300
golang.org/x/oauth2 v0.20.0 // indirect
301301
golang.org/x/sync v0.8.0 // indirect

go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ github.com/cockroachdb/errors v1.2.4/go.mod h1:rQD95gz6FARkaKkQXUksEje/d9a6wBJoC
186186
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f/go.mod h1:i/u985jwjWRlyHXQbwatDASoW0RMlZ/3i9yJHE2xLkI=
187187
github.com/coder/coder/v2 v2.10.1-0.20240704130443-c2d44d16a352 h1:L/EjCuZxs5tOcqqCaASj/nu65TRYEFcTt8qRQfHZXX0=
188188
github.com/coder/coder/v2 v2.10.1-0.20240704130443-c2d44d16a352/go.mod h1:P1KoQSgnKEAG6Mnd3YlGzAophty+yKA9VV48LpfNRvo=
189-
github.com/coder/envbuilder v1.0.0-rc.0.0.20240830145058-fb7e689f39ed h1:sDEjs9qB2uJ7O85vGmzMja99IZuLvesxElOUFyy22UY=
190-
github.com/coder/envbuilder v1.0.0-rc.0.0.20240830145058-fb7e689f39ed/go.mod h1:LWImvtIWaX3eiAI3zyU46WE/PrE099nCut1zJultSk0=
189+
github.com/coder/envbuilder v1.0.0-rc.0.0.20240910082823-b7781d802f88 h1:eXOILD2tWepnV1r7XZalBX0yC4NJMnpf6OP1nF8O2Ak=
190+
github.com/coder/envbuilder v1.0.0-rc.0.0.20240910082823-b7781d802f88/go.mod h1:krXpDmUsORgNNdvBe6tnwWCGGDLhabom1UUqAZq9+v0=
191191
github.com/coder/kaniko v0.0.0-20240830141327-f307586e3dca h1:PrcSWrllqipTrtet50a3VyAJEQmjziIZyhpy0bsC6o0=
192192
github.com/coder/kaniko v0.0.0-20240830141327-f307586e3dca/go.mod h1:XoTDIhNF0Ll4tLmRYdOn31udU9w5zFrY2PME/crSRCA=
193193
github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0 h1:3A0ES21Ke+FxEM8CXx9n47SZOKOpgSE1bbJzlE4qPVs=
@@ -895,8 +895,8 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
895895
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
896896
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
897897
golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
898-
golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8=
899-
golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
898+
golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
899+
golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
900900
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
901901
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
902902
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=

internal/imgutil/imgutil.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import (
77
"io"
88
"os"
99
"path/filepath"
10+
"strings"
1011

11-
"github.com/coder/envbuilder/constants"
12+
eboptions "github.com/coder/envbuilder/options"
1213
"github.com/google/go-containerregistry/pkg/authn"
1314
"github.com/google/go-containerregistry/pkg/name"
1415
v1 "github.com/google/go-containerregistry/pkg/v1"
@@ -34,7 +35,9 @@ func GetRemoteImage(imgRef string) (v1.Image, error) {
3435
// ExtractEnvbuilderFromImage reads the image located at imgRef and extracts
3536
// MagicBinaryLocation to destPath.
3637
func ExtractEnvbuilderFromImage(ctx context.Context, imgRef, destPath string) error {
37-
needle := filepath.Clean(constants.MagicBinaryLocation)[1:] // skip leading '/'
38+
var o eboptions.Options
39+
o.SetDefaults()
40+
needle := strings.TrimPrefix(o.BinaryPath, "/")
3841
img, err := GetRemoteImage(imgRef)
3942
if err != nil {
4043
return fmt.Errorf("check remote image: %w", err)

internal/provider/cached_image_resource.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
kconfig "github.com/GoogleContainerTools/kaniko/pkg/config"
1212
"github.com/coder/envbuilder"
13-
"github.com/coder/envbuilder/constants"
1413
eboptions "github.com/coder/envbuilder/options"
1514
"github.com/coder/terraform-provider-envbuilder/internal/imgutil"
1615
"github.com/coder/terraform-provider-envbuilder/internal/tfutil"
@@ -455,7 +454,7 @@ func runCacheProbe(ctx context.Context, builderImage string, opts eboptions.Opti
455454
}()
456455

457456
oldKanikoDir := kconfig.KanikoDir
458-
tmpKanikoDir := filepath.Join(tmpDir, constants.MagicDir)
457+
tmpKanikoDir := filepath.Join(tmpDir, ".envbuilder")
459458
// Normally you would set the KANIKO_DIR environment variable, but we are importing kaniko directly.
460459
kconfig.KanikoDir = tmpKanikoDir
461460
tflog.Info(ctx, "set kaniko dir to "+tmpKanikoDir)
@@ -467,6 +466,8 @@ func runCacheProbe(ctx context.Context, builderImage string, opts eboptions.Opti
467466
if err := os.MkdirAll(tmpKanikoDir, 0o755); err != nil {
468467
return nil, fmt.Errorf("failed to create kaniko dir: %w", err)
469468
}
469+
// Use the temporary directory as our 'magic dir'.
470+
opts.MagicDirBase = tmpKanikoDir
470471

471472
// In order to correctly reproduce the final layer of the cached image, we
472473
// need the envbuilder binary used to originally build the image!
@@ -496,13 +497,6 @@ func runCacheProbe(ctx context.Context, builderImage string, opts eboptions.Opti
496497
tflog.Debug(ctx, "workspace_folder not specified, using temp dir", map[string]any{"workspace_folder": opts.WorkspaceFolder})
497498
}
498499

499-
// We need a place to clone the repo.
500-
repoDir := filepath.Join(tmpDir, "repo")
501-
if err := os.MkdirAll(repoDir, 0o755); err != nil {
502-
return nil, fmt.Errorf("failed to create repo dir: %w", err)
503-
}
504-
opts.RemoteRepoDir = repoDir
505-
506500
// The below options are not relevant and are set to their zero value
507501
// explicitly.
508502
// They must be set by extra_env to be used in the final builder image.

internal/provider/cached_image_resource_test.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func TestAccCachedImageResource(t *testing.T) {
4646
"CODER_AGENT_TOKEN", "some-token",
4747
"CODER_AGENT_URL", "https://coder.example.com",
4848
"ENVBUILDER_CACHE_REPO", deps.CacheRepo,
49+
"ENVBUILDER_DOCKER_CONFIG_BASE64", deps.DockerConfigBase64,
4950
"ENVBUILDER_GIT_SSH_PRIVATE_KEY_PATH", deps.Repo.Key,
5051
"ENVBUILDER_GIT_URL", deps.Repo.URL,
5152
"ENVBUILDER_REMOTE_REPO_BUILD_MODE", "true",
@@ -78,6 +79,7 @@ RUN date > /date.txt`,
7879
"CODER_AGENT_TOKEN", "some-token",
7980
"CODER_AGENT_URL", "https://coder.example.com",
8081
"ENVBUILDER_CACHE_REPO", deps.CacheRepo,
82+
"ENVBUILDER_DOCKER_CONFIG_BASE64", deps.DockerConfigBase64,
8183
"ENVBUILDER_GIT_SSH_PRIVATE_KEY_PATH", deps.Repo.Key,
8284
"ENVBUILDER_GIT_URL", deps.Repo.URL,
8385
"ENVBUILDER_REMOTE_REPO_BUILD_MODE", "true",
@@ -88,9 +90,8 @@ RUN date > /date.txt`,
8890
},
8991
},
9092
{
91-
// This test case ensures that parameters passed via extra_env are
92-
// handled correctly.
93-
name: "extra_env",
93+
// This test case ensures that overriding the devcontainer directory works.
94+
name: "different_dir",
9495
files: map[string]string{
9596
"path/to/.devcontainer/devcontainer.json": `{"build": { "dockerfile": "Dockerfile" }}`,
9697
"path/to/.devcontainer/Dockerfile": `FROM localhost:5000/test-ubuntu:latest
@@ -115,6 +116,7 @@ RUN date > /date.txt`,
115116
"ENVBUILDER_DEVCONTAINER_DIR", "path/to/.devcontainer",
116117
"ENVBUILDER_DEVCONTAINER_JSON_PATH", "path/to/.devcontainer/devcontainer.json",
117118
"ENVBUILDER_DOCKERFILE_PATH", "path/to/.devcontainer/Dockerfile",
119+
"ENVBUILDER_DOCKER_CONFIG_BASE64", deps.DockerConfigBase64,
118120
"ENVBUILDER_GIT_SSH_PRIVATE_KEY_PATH", deps.Repo.Key,
119121
"ENVBUILDER_GIT_URL", deps.Repo.URL,
120122
"ENVBUILDER_REMOTE_REPO_BUILD_MODE", "true",
@@ -149,6 +151,7 @@ RUN date > /date.txt`,
149151
"CODER_AGENT_URL", "https://coder.example.com",
150152
"ENVBUILDER_CACHE_REPO", deps.CacheRepo,
151153
"ENVBUILDER_DOCKERFILE_PATH", "Dockerfile",
154+
"ENVBUILDER_DOCKER_CONFIG_BASE64", deps.DockerConfigBase64,
152155
"ENVBUILDER_GIT_SSH_PRIVATE_KEY_PATH", deps.Repo.Key,
153156
"ENVBUILDER_GIT_URL", deps.Repo.URL,
154157
"ENVBUILDER_REMOTE_REPO_BUILD_MODE", "true",

internal/provider/provider_internal_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,6 @@ func Test_computeEnvFromOptions(t *testing.T) {
272272
PostStartScriptPath: "string",
273273
PushImage: true,
274274
RemoteRepoBuildMode: true,
275-
RemoteRepoDir: "string",
276275
SetupScript: "string",
277276
SkipRebuild: true,
278277
SSLCertBase64: "string",
@@ -314,7 +313,6 @@ func Test_computeEnvFromOptions(t *testing.T) {
314313
"ENVBUILDER_POST_START_SCRIPT_PATH": "string",
315314
"ENVBUILDER_PUSH_IMAGE": "true",
316315
"ENVBUILDER_REMOTE_REPO_BUILD_MODE": "true",
317-
"ENVBUILDER_REMOTE_REPO_DIR": "string",
318316
"ENVBUILDER_SETUP_SCRIPT": "string",
319317
"ENVBUILDER_SKIP_REBUILD": "true",
320318
"ENVBUILDER_SSL_CERT_BASE64": "string",

internal/provider/provider_test.go

+26-12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package provider
33
import (
44
"bufio"
55
"context"
6+
"encoding/base64"
67
"fmt"
78
"io"
89
"os"
@@ -35,10 +36,11 @@ var testAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServe
3536

3637
// testDependencies contain information about stuff the test depends on.
3738
type testDependencies struct {
38-
BuilderImage string
39-
CacheRepo string
40-
ExtraEnv map[string]string
41-
Repo testGitRepoSSH
39+
BuilderImage string
40+
CacheRepo string
41+
DockerConfigBase64 string
42+
ExtraEnv map[string]string
43+
Repo testGitRepoSSH
4244
}
4345

4446
// Config generates a valid Terraform config file from the dependencies.
@@ -47,8 +49,9 @@ func (d *testDependencies) Config(t testing.TB) string {
4749

4850
tpl := `provider envbuilder {}
4951
resource "envbuilder_cached_image" "test" {
50-
builder_image = {{ quote .BuilderImage }}
52+
builder_image = {{ quote .BuilderImage }}
5153
cache_repo = {{ quote .CacheRepo }}
54+
docker_config_base64 = {{ quote .DockerConfigBase64 }}
5255
git_url = {{ quote .Repo.URL }}
5356
extra_env = {
5457
"ENVBUILDER_GIT_SSH_PRIVATE_KEY_PATH": {{ quote .Repo.Key }}
@@ -78,19 +81,29 @@ func setup(ctx context.Context, t testing.TB, extraEnv, files map[string]string)
7881
envbuilderVersion := getEnvOrDefault("ENVBUILDER_VERSION", "latest")
7982
envbuilderImageRef := envbuilderImage + ":" + envbuilderVersion
8083

81-
// TODO: envbuilder creates /.envbuilder/bin/envbuilder owned by root:root which we are unable to clean up.
82-
// This causes tests to fail.
84+
testUsername := "testuser"
85+
testPassword := "testpassword"
86+
testAuthBase64 := base64.URLEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", testUsername, testPassword)))
8387
regDir := t.TempDir()
84-
reg := registrytest.New(t, regDir)
88+
reg := registrytest.New(t, regDir, registrytest.BasicAuthMW(t, testUsername, testPassword))
8589

8690
repoDir := setupGitRepo(t, files)
8791
gitRepo := serveGitRepoSSH(ctx, t, repoDir)
92+
dockerConfigJSON := fmt.Sprintf(`{
93+
"auths": {
94+
"%s": {
95+
"auth": "%s",
96+
}
97+
}
98+
}`, reg, testAuthBase64)
99+
dockerConfigJSONBase64 := base64.StdEncoding.EncodeToString([]byte(dockerConfigJSON))
88100

89101
return testDependencies{
90-
BuilderImage: envbuilderImageRef,
91-
CacheRepo: reg + "/test",
92-
ExtraEnv: extraEnv,
93-
Repo: gitRepo,
102+
BuilderImage: envbuilderImageRef,
103+
CacheRepo: reg + "/test",
104+
ExtraEnv: extraEnv,
105+
Repo: gitRepo,
106+
DockerConfigBase64: dockerConfigJSONBase64,
94107
}
95108
}
96109

@@ -115,6 +128,7 @@ func seedCache(ctx context.Context, t testing.TB, deps testDependencies) {
115128
"ENVBUILDER_VERBOSE": "true",
116129
"ENVBUILDER_GIT_URL": deps.Repo.URL,
117130
"ENVBUILDER_GIT_SSH_PRIVATE_KEY_PATH": "/id_ed25519",
131+
"ENVBUILDER_DOCKER_CONFIG_BASE64": deps.DockerConfigBase64,
118132
}
119133

120134
for k, v := range deps.ExtraEnv {

testutil/registrytest/registrytest.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package registrytest
22

33
import (
44
"fmt"
5+
"net/http"
56
"net/http/httptest"
67
"net/url"
78
"testing"
@@ -13,12 +14,31 @@ import (
1314
// New starts a new Docker registry listening on localhost.
1415
// It will automatically shut down when the test finishes.
1516
// It will store data in dir.
16-
func New(t testing.TB, dir string) string {
17+
func New(t testing.TB, dir string, mws ...func(http.Handler) http.Handler) string {
1718
t.Helper()
1819
regHandler := registry.New(registry.WithBlobHandler(registry.NewDiskBlobHandler(dir)))
20+
for _, mw := range mws {
21+
regHandler = mw(regHandler)
22+
}
1923
regSrv := httptest.NewServer(regHandler)
2024
t.Cleanup(func() { regSrv.Close() })
2125
regSrvURL, err := url.Parse(regSrv.URL)
2226
require.NoError(t, err)
2327
return fmt.Sprintf("localhost:%s", regSrvURL.Port())
2428
}
29+
30+
func BasicAuthMW(t testing.TB, username, password string) func(http.Handler) http.Handler {
31+
return func(next http.Handler) http.Handler {
32+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
33+
if username != "" || password != "" {
34+
authUser, authPass, ok := r.BasicAuth()
35+
if !ok || username != authUser || password != authPass {
36+
t.Logf("basic auth failed: got user %q, pass %q", authUser, authPass)
37+
w.WriteHeader(http.StatusUnauthorized)
38+
return
39+
}
40+
}
41+
next.ServeHTTP(w, r)
42+
})
43+
}
44+
}

0 commit comments

Comments
 (0)