mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-05-19 01:47:43 +08:00
bake: deny access to local dockerfile for remote invocation with local context
we don't currently support reading a remote Dockerfile with a local context when doing a remote invocation because we automatically derive the dockerfile from the context atm. To avoid mistakenly reading a local Dockerfile, we check if the Dockerfile exists locally and if so, we error out. Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
parent
de9d253f45
commit
7497e6481e
18
bake/bake.go
18
bake/bake.go
@ -1070,6 +1070,24 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
} else if !build.IsRemoteURL(bi.DockerfilePath) && strings.HasPrefix(bi.ContextPath, "cwd://") && (inp != nil && build.IsRemoteURL(inp.URL)) {
|
||||||
|
// We don't currently support reading a remote Dockerfile with a local
|
||||||
|
// context when doing a remote invocation because we automatically
|
||||||
|
// derive the dockerfile from the context atm:
|
||||||
|
//
|
||||||
|
// target "default" {
|
||||||
|
// context = BAKE_CMD_CONTEXT
|
||||||
|
// dockerfile = "Dockerfile.app"
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// > docker buildx bake https://github.com/foo/bar.git
|
||||||
|
// failed to solve: failed to read dockerfile: open /var/lib/docker/tmp/buildkit-mount3004544897/Dockerfile.app: no such file or directory
|
||||||
|
//
|
||||||
|
// To avoid mistakenly reading a local Dockerfile, we check if the
|
||||||
|
// Dockerfile exists locally and if so, we error out.
|
||||||
|
if _, err := os.Stat(filepath.Join(path.Clean(strings.TrimPrefix(bi.ContextPath, "cwd://")), bi.DockerfilePath)); err == nil {
|
||||||
|
return nil, errors.Errorf("reading a dockerfile for a remote build invocation is currently not supported")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(bi.ContextPath, "cwd://") {
|
if strings.HasPrefix(bi.ContextPath, "cwd://") {
|
||||||
bi.ContextPath = path.Clean(strings.TrimPrefix(bi.ContextPath, "cwd://"))
|
bi.ContextPath = path.Clean(strings.TrimPrefix(bi.ContextPath, "cwd://"))
|
||||||
|
@ -27,6 +27,7 @@ var bakeTests = []func(t *testing.T, sb integration.Sandbox){
|
|||||||
testBakeRemoteCmdContextEscapeRoot,
|
testBakeRemoteCmdContextEscapeRoot,
|
||||||
testBakeRemoteCmdContextEscapeRelative,
|
testBakeRemoteCmdContextEscapeRelative,
|
||||||
testBakeRemoteDockerfileCwd,
|
testBakeRemoteDockerfileCwd,
|
||||||
|
testBakeRemoteLocalContextRemoteDockerfile,
|
||||||
}
|
}
|
||||||
|
|
||||||
func testBakeLocal(t *testing.T, sb integration.Sandbox) {
|
func testBakeLocal(t *testing.T, sb integration.Sandbox) {
|
||||||
@ -348,3 +349,42 @@ COPY foo /foo
|
|||||||
)
|
)
|
||||||
require.Error(t, err, out)
|
require.Error(t, err, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testBakeRemoteLocalContextRemoteDockerfile(t *testing.T, sb integration.Sandbox) {
|
||||||
|
bakefile := []byte(`
|
||||||
|
target "default" {
|
||||||
|
context = BAKE_CMD_CONTEXT
|
||||||
|
dockerfile = "Dockerfile.app"
|
||||||
|
}
|
||||||
|
`)
|
||||||
|
dockerfileApp := []byte(`
|
||||||
|
FROM scratch
|
||||||
|
COPY foo /foo
|
||||||
|
`)
|
||||||
|
|
||||||
|
dirSpec := tmpdir(
|
||||||
|
t,
|
||||||
|
fstest.CreateFile("docker-bake.hcl", bakefile, 0600),
|
||||||
|
)
|
||||||
|
dirSrc := tmpdir(
|
||||||
|
t,
|
||||||
|
fstest.CreateFile("Dockerfile.app", dockerfileApp, 0600),
|
||||||
|
fstest.CreateFile("foo", []byte("foo"), 0600),
|
||||||
|
)
|
||||||
|
|
||||||
|
git, err := gitutil.New(gitutil.WithWorkingDir(dirSpec))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
gitutil.GitInit(git, t)
|
||||||
|
gitutil.GitAdd(git, t, "docker-bake.hcl")
|
||||||
|
gitutil.GitCommit(git, t, "initial commit")
|
||||||
|
addr := gitutil.GitServeHTTP(git, t)
|
||||||
|
|
||||||
|
out, err := bakeCmd(
|
||||||
|
sb,
|
||||||
|
withDir(dirSrc),
|
||||||
|
withArgs(addr, "--set", "*.output=type=cacheonly"),
|
||||||
|
)
|
||||||
|
require.Error(t, err, out)
|
||||||
|
require.Contains(t, out, "reading a dockerfile for a remote build invocation is currently not supported")
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user