Skip to content

Commit

Permalink
Fix Sentry Debug Message for empty command
Browse files Browse the repository at this point in the history
  • Loading branch information
mpass99 committed Sep 13, 2024
1 parent 78fce4c commit 704caeb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 3 additions & 1 deletion internal/nomad/command_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ func injectStartDebugMessage(command string, start uint, end int) string {
if strings.HasPrefix(description, "\"") && strings.HasSuffix(description, "\"") {
description = description[1 : len(description)-1]
}

if description == "" {
description = timeDebugFallbackDescription
}
description = dto.BashEscapeCommand(description)
description = description[1 : len(description)-1] // The most outer quotes are not escaped!
return fmt.Sprintf(timeDebugMessageFormatStart, description, command)
Expand Down
3 changes: 2 additions & 1 deletion internal/nomad/sentry_debug_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var (
timeDebugMessagePattern = regexp.MustCompile(
`(?P<before>[\S\s]*?)\x1EPoseidon (?P<text>[^\x1E]+?) (?P<time>\d{13})\x1E(?P<after>[\S\s]*)`)
timeDebugMessagePatternStart = regexp.MustCompile(`\x1EPoseidon`)
timeDebugFallbackDescription = "<empty>"
)

// SentryDebugWriter is scanning the input for the debug message pattern.
Expand Down Expand Up @@ -68,7 +69,7 @@ func (s *SentryDebugWriter) Write(debugData []byte) (n int, err error) {

match := matchAndMapTimeDebugMessage(debugData)
if match == nil {
log.WithContext(s.lastSpan.Context()).WithField("data", debugData).Warn("Exec debug message could not be read completely")
log.WithContext(s.lastSpan.Context()).WithField("data", fmt.Sprintf("%q", debugData)).Warn("Exec debug message could not be read completely")
return 0, nil
}

Expand Down
18 changes: 18 additions & 0 deletions internal/nomad/sentry_debug_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nomad

import (
"bytes"
"os/exec"
)

func (s *MainTestSuite) TestSentryDebugWriter_Write() {
Expand Down Expand Up @@ -29,6 +30,23 @@ func (s *MainTestSuite) TestSentryDebugWriter_WriteComposed() {
s.Contains(buf.String(), "Hello World!")
}

func (s *MainTestSuite) TestSentryDebugWriter_regression_593_empty_command() {
buf := &bytes.Buffer{}
debugWriter := NewSentryDebugWriter(s.TestCtx, buf)

const commandFieldAfterEnv = 4 // instead of "env CODEOCEAN=true /bin/bash -c sleep infinity" just "sleep infinity".
command := injectStartDebugMessage("env CODEOCEAN=true /bin/bash -c \"\"", commandFieldAfterEnv, -1)
cmd := exec.Command("/bin/bash", "-c", command)
stdout, err := cmd.Output()
s.Require().NoError(err)

count, err := debugWriter.Write(stdout)
s.Require().NoError(err)
s.NotEmpty(count)
s.Empty(buf.Bytes())
s.Equal(timeDebugFallbackDescription, debugWriter.lastSpan.Description)
}

func (s *MainTestSuite) TestSentryDebugWriter_regression_issue_678() {
buf := &bytes.Buffer{}
debugWriter := NewSentryDebugWriter(s.TestCtx, buf)
Expand Down

0 comments on commit 704caeb

Please sign in to comment.