Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: handle newlines in JobSubmission vars correctly #23560

Merged
merged 3 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/23560.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
api: Fixed bug where newlines in JobSubmission vars weren't encoded correctly
```
10 changes: 10 additions & 0 deletions api/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/url"
"sort"
"strconv"
"strings"
"time"

"github.com/hashicorp/cronexpr"
Expand Down Expand Up @@ -997,6 +998,15 @@ func (js *JobSubmission) Canonicalize() {
if len(js.VariableFlags) == 0 {
js.VariableFlags = nil
}

// if there are multiline variables, make sure we escape the newline
// characters to preserve them. This way, when the job gets stopped and
// restarted in the UI, variable values will be parsed correctly.
for k, v := range js.VariableFlags {
if strings.Contains(v, "\n") {
js.VariableFlags[k] = strings.ReplaceAll(v, "\n", "\\n")
}
}
}

func (js *JobSubmission) Copy() *JobSubmission {
Expand Down
9 changes: 9 additions & 0 deletions api/jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,15 @@ func TestJobs_JobSubmission_Canonicalize(t *testing.T) {
js.Canonicalize()
must.Nil(t, js.VariableFlags)
})

t.Run("multiline var values", func(t *testing.T) {
js := &JobSubmission{
Source: "abc123",
VariableFlags: map[string]string{"test": "foo\nbar"},
}
js.Canonicalize()
must.Eq(t, js.VariableFlags["test"], "foo\\nbar")
})
}

func TestJobs_JobSubmission_Copy(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions command/agent/job_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,8 @@ func apiJobSubmissionToStructs(submission *api.JobSubmission) *structs.JobSubmis
if submission == nil {
return nil
}

submission.Canonicalize()
return &structs.JobSubmission{
Source: submission.Source,
Format: submission.Format,
Expand Down