Skip to content

Commit

Permalink
Catch List FS Link permission denied error
Browse files Browse the repository at this point in the history
  • Loading branch information
mpass99 committed Aug 21, 2024
1 parent 80e08d7 commit 3be6d03
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
14 changes: 9 additions & 5 deletions internal/runner/nomad_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,21 +165,25 @@ func (r *NomadJob) ListFileSystem(
}

ls2json := &nullio.Ls2JsonWriter{Target: content, Ctx: ctx}
defer ls2json.Close()
retrieveCommand := (&dto.ExecutionRequest{Command: fmt.Sprintf("%s %q", command, path)}).FullCommand()
exitCode, err := r.api.ExecuteCommand(ctx, r.id, retrieveCommand, false, privilegedExecution,
&nullio.Reader{Ctx: ctx}, ls2json, io.Discard)
writerErr := ls2json.Close()

switch {
case ls2json.HasStartedWriting() && err == nil && exitCode == 0:
// Successful. Nothing to do.
case ls2json.HasStartedWriting() && err == nil && errors.Is(writerErr, nullio.ErrLinkTargetPermissionDenied):
// Nothing to do. All available information has been returned.
case ls2json.HasStartedWriting():
// if HasStartedWriting the status code of the response is already sent.
// Therefore, we cannot notify CodeOcean about an error at this point anymore.
log.WithError(err).WithField("exitCode", exitCode).Warn("Ignoring error of listing the file system")
log.WithError(err).WithField("writerError", writerErr).WithField("exitCode", exitCode).
Warn("Ignoring error of listing the file system")

Check warning on line 182 in internal/runner/nomad_runner.go

View check run for this annotation

Codecov / codecov/patch

internal/runner/nomad_runner.go#L181-L182

Added lines #L181 - L182 were not covered by tests
err = nil
case err != nil:
err = fmt.Errorf("%w: nomad error during retrieve file headers: %w",
nomad.ErrExecutorCommunicationFailed, err)
case err != nil || writerErr != nil:
err = fmt.Errorf("%w: nomad error during retrieve file headers: %w, %w",
nomad.ErrExecutorCommunicationFailed, err, writerErr)

Check warning on line 186 in internal/runner/nomad_runner.go

View check run for this annotation

Codecov / codecov/patch

internal/runner/nomad_runner.go#L184-L186

Added lines #L184 - L186 were not covered by tests
case exitCode != 0:
err = ErrFileNotFound
case !ls2json.HasStartedWriting():
Expand Down
8 changes: 6 additions & 2 deletions pkg/nullio/ls2json.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"regexp"
Expand All @@ -20,6 +21,7 @@ var (
pathLineRegex = regexp.MustCompile(`(.*):$`)
headerLineRegex = regexp.
MustCompile(`([-aAbcCdDlMnpPsw?])([-rwxXsStT]{9})(\+?) +\d+ +(.+?) +(.+?) +(\d+) +(\d+) +(.*)$`)
ErrLinkTargetPermissionDenied = errors.New("permission denied for reading link target")
)

const (
Expand All @@ -44,6 +46,7 @@ type Ls2JsonWriter struct {
remaining []byte
latestPath []byte
sentrySpan *sentry.Span
err error
}

func (w *Ls2JsonWriter) HasStartedWriting() bool {
Expand Down Expand Up @@ -101,14 +104,15 @@ func (w *Ls2JsonWriter) initializeJSONObject() (count int, err error) {
return count, err
}

func (w *Ls2JsonWriter) Close() {
func (w *Ls2JsonWriter) Close() error {
if w.jsonStartSent {
count, err := w.Target.Write([]byte("]}"))
if count == 0 || err != nil {
log.WithContext(w.sentrySpan.Context()).WithError(err).Warn("Could not Close ls2json writer")
}
w.sentrySpan.Finish()
}
return w.err
}

func (w *Ls2JsonWriter) writeLine(line []byte) (count int, err error) {
Expand Down Expand Up @@ -169,7 +173,7 @@ func (w *Ls2JsonWriter) parseFileHeader(matches [][]byte) ([]byte, error) {
linkTarget = dto.FilePath(parts[1])
case 1:
// This case happens when a user tries to read the target of a link without permission. See #596.
// Nothing to do. The target remains empty. The name is set.
w.err = errors.Join(w.err, ErrLinkTargetPermissionDenied)
default:
log.WithContext(w.sentrySpan.Context()).WithField("name", name).Error("could not split link into name and target")

Check warning on line 178 in pkg/nullio/ls2json.go

View check run for this annotation

Codecov / codecov/patch

pkg/nullio/ls2json.go#L177-L178

Added lines #L177 - L178 were not covered by tests
}
Expand Down

0 comments on commit 3be6d03

Please sign in to comment.