From fc77f11d4de54816603968ffdc4f937c7004a910 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Pa=C3=9F?= <22845248+mpass99@users.noreply.github.com> Date: Sun, 21 Aug 2022 12:35:13 +0200 Subject: [PATCH] Enquote file path for shell execution. Also, fix json of 500 response. --- api/swagger.yaml | 4 ++++ internal/api/runners.go | 6 +++++- internal/runner/nomad_runner.go | 4 ++-- pkg/nullio/ls2json.go | 8 +++++--- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/api/swagger.yaml b/api/swagger.yaml index f1e9552..d1b5c45 100644 --- a/api/swagger.yaml +++ b/api/swagger.yaml @@ -324,6 +324,10 @@ paths: $ref: "#/components/schemas/FileHeader" "401": $ref: "#/components/responses/Unauthorized" + "404": + $ref: "#/components/responses/NotFound" + "500": + $ref: "#/components/responses/InternalServerError" patch: summary: Manipulate runner file system description: Delete the files with the given paths from the file system of the specified runner. Afterwards, copy the enclosed files to the runner. Existing files get overwritten and results of previous file copy operations on the same runner are present when executing multiple requests. diff --git a/internal/api/runners.go b/internal/api/runners.go index d1b954b..8e8bc4c 100644 --- a/internal/api/runners.go +++ b/internal/api/runners.go @@ -95,7 +95,11 @@ func (r *RunnerController) listFileSystem(writer http.ResponseWriter, request *h } writer.Header().Set("Content-Type", "application/json") - if err := targetRunner.ListFileSystem(path, recursive, writer, request.Context()); err != nil { + err = targetRunner.ListFileSystem(path, recursive, writer, request.Context()) + if errors.Is(err, runner.ErrFileNotFound) { + writeNotFound(writer, err) + return + } else if err != nil { log.WithError(err).Error("Could not perform the requested listFileSystem.") writeInternalServerError(writer, err, dto.ErrorUnknown) return diff --git a/internal/runner/nomad_runner.go b/internal/runner/nomad_runner.go index aae1407..4db3014 100644 --- a/internal/runner/nomad_runner.go +++ b/internal/runner/nomad_runner.go @@ -127,7 +127,7 @@ func (r *NomadJob) ListFileSystem(path string, recursive bool, content io.Writer ls2json := &nullio.Ls2JsonWriter{Target: content} defer ls2json.Close() - retrieveCommand := (&dto.ExecutionRequest{Command: fmt.Sprintf("%s %s", command, path)}).FullCommand() + retrieveCommand := (&dto.ExecutionRequest{Command: fmt.Sprintf("%s %q", command, path)}).FullCommand() exitCode, err := r.api.ExecuteCommand(r.id, ctx, retrieveCommand, false, &nullio.Reader{}, ls2json, io.Discard) if err != nil { return fmt.Errorf("%w: nomad error during retrieve file headers: %v", @@ -175,7 +175,7 @@ func (r *NomadJob) UpdateFileSystem(copyRequest *dto.UpdateFileSystemRequest) er func (r *NomadJob) GetFileContent(path string, content io.Writer, ctx context.Context) error { r.ResetTimeout() - retrieveCommand := (&dto.ExecutionRequest{Command: fmt.Sprintf("cat %s", path)}).FullCommand() + retrieveCommand := (&dto.ExecutionRequest{Command: fmt.Sprintf("cat %q", path)}).FullCommand() // Improve: Instead of using io.Discard use a **fixed-sized** buffer. With that we could improve the error message. exitCode, err := r.api.ExecuteCommand(r.id, ctx, retrieveCommand, false, &nullio.Reader{}, content, io.Discard) diff --git a/pkg/nullio/ls2json.go b/pkg/nullio/ls2json.go index 5d31700..f0e5789 100644 --- a/pkg/nullio/ls2json.go +++ b/pkg/nullio/ls2json.go @@ -76,9 +76,11 @@ func (w *Ls2JsonWriter) initializeJSONObject() (count int, err error) { } func (w *Ls2JsonWriter) Close() { - count, err := w.Target.Write([]byte("]}")) - if count == 0 || err != nil { - log.WithError(err).Warn("Could not Close ls2json writer") + if w.jsonStartSend { + count, err := w.Target.Write([]byte("]}")) + if count == 0 || err != nil { + log.WithError(err).Warn("Could not Close ls2json writer") + } } }