From 9ed85f487d4760fcbf59515415def37d4bc37f27 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 4 Jan 2025 23:57:39 +0100 Subject: [PATCH] daemon: don't discard "invalid parameter" errors for archive endpoints Commit ebcb7d6b406fe50ea9a237c73004d75884184c33 removed code that string- matched errors to return the appropriate error-type. As part of that, it defaulted to using a "System" (status 500) error. We need to verify codepaths used by the archive functions, but let's start with detecting invalid parameter errors, and preserve those. This patch updates the code backing the following endpoints to return a 400 (Invalid Parameter) error when present; - `HEAD /containers/{name:.*}/archive` - `GET /containers/{name:.*}/archive` - `PUT /containers/{name:.*}/archive` Signed-off-by: Sebastiaan van Stijn --- daemon/archive.go | 51 ++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/daemon/archive.go b/daemon/archive.go index b18e74649f..5de94339c4 100644 --- a/daemon/archive.go +++ b/daemon/archive.go @@ -17,14 +17,17 @@ func (daemon *Daemon) ContainerStatPath(name string, path string) (stat *contain } stat, err = daemon.containerStatPath(ctr, path) - if err == nil { - return stat, nil + if err != nil { + if os.IsNotExist(err) { + return nil, containerFileNotFound{path, name} + } + // TODO(thaJeztah): check if daemon.containerStatPath returns any errors that are not typed; if not, then return as-is + if errdefs.IsInvalidParameter(err) { + return nil, err + } + return nil, errdefs.System(err) } - - if os.IsNotExist(err) { - return nil, containerFileNotFound{path, name} - } - return nil, errdefs.System(err) + return stat, nil } // ContainerArchivePath creates an archive of the filesystem resource at the @@ -37,14 +40,17 @@ func (daemon *Daemon) ContainerArchivePath(name string, path string) (content io } content, stat, err = daemon.containerArchivePath(ctr, path) - if err == nil { - return content, stat, nil + if err != nil { + if os.IsNotExist(err) { + return nil, nil, containerFileNotFound{path, name} + } + // TODO(thaJeztah): check if daemon.containerArchivePath returns any errors that are not typed; if not, then return as-is + if errdefs.IsInvalidParameter(err) { + return nil, nil, err + } + return nil, nil, errdefs.System(err) } - - if os.IsNotExist(err) { - return nil, nil, containerFileNotFound{path, name} - } - return nil, nil, errdefs.System(err) + return content, stat, nil } // ContainerExtractToDir extracts the given archive to the specified location @@ -60,12 +66,15 @@ func (daemon *Daemon) ContainerExtractToDir(name, path string, copyUIDGID, noOve } err = daemon.containerExtractToDir(ctr, path, copyUIDGID, noOverwriteDirNonDir, content) - if err == nil { - return nil + if err != nil { + if os.IsNotExist(err) { + return containerFileNotFound{path, name} + } + // TODO(thaJeztah): check if daemon.containerExtractToDir returns any errors that are not typed; if not, then return as-is + if errdefs.IsInvalidParameter(err) { + return err + } + return errdefs.System(err) } - - if os.IsNotExist(err) { - return containerFileNotFound{path, name} - } - return errdefs.System(err) + return nil }