Skip to content

Commit

Permalink
fix: search and replace null bytes for postgres only.Fixes argoproj#1…
Browse files Browse the repository at this point in the history
…3711 (argoproj#14030)

Signed-off-by: isubasinghe <[email protected]>
  • Loading branch information
isubasinghe authored Jan 5, 2025
1 parent ef41f83 commit b93ffd8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
13 changes: 11 additions & 2 deletions persist/sqldb/workflow_archive.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package sqldb

import (
"bytes"
"encoding/json"
"fmt"
"strings"
"time"

log "github.com/sirupsen/logrus"
Expand All @@ -20,8 +22,9 @@ import (
)

const (
archiveTableName = "argo_archived_workflows"
archiveLabelsTableName = archiveTableName + "_labels"
archiveTableName = "argo_archived_workflows"
archiveLabelsTableName = archiveTableName + "_labels"
postgresNullReplacement = "ARGO_POSTGRES_NULL_REPLACEMENT"
)

type archivedWorkflowMetadata struct {
Expand Down Expand Up @@ -103,6 +106,9 @@ func (r *workflowArchive) ArchiveWorkflow(wf *wfv1.Workflow) error {
if err != nil {
return err
}
if r.dbType == Postgres {
workflow = bytes.ReplaceAll(workflow, []byte("\\u0000"), []byte(postgresNullReplacement))
}
return r.session.Tx(func(sess db.Session) error {
_, err := sess.SQL().
DeleteFrom(archiveTableName).
Expand Down Expand Up @@ -387,6 +393,9 @@ func (r *workflowArchive) GetWorkflow(uid string, namespace string, name string)
return nil, err
}
var wf *wfv1.Workflow
if r.dbType == Postgres {
archivedWf.Workflow = strings.ReplaceAll(archivedWf.Workflow, postgresNullReplacement, "\\u0000")
}
err = json.Unmarshal([]byte(archivedWf.Workflow), &wf)
if err != nil {
return nil, err
Expand Down
36 changes: 36 additions & 0 deletions test/e2e/argo_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2135,6 +2135,42 @@ func (s *ArgoServerSuite) TestRateLimitHeader() {
})
}

func (s *ArgoServerSuite) TestPostgresNullBytes() {
// only meaningful for postgres, but shouldn't fail for mysql.
var uid types.UID
_ = uid

s.Given().
Workflow(`
metadata:
generateName: archie-
labels:
foo: 1
spec:
entrypoint: run-archie
templates:
- name: run-archie
container:
image: argoproj/argosay:v2
args: [echo, "hello \u0000"]`).
When().
SubmitWorkflow().
WaitForWorkflow(fixtures.ToBeArchived).
Then().
ExpectWorkflow(func(t *testing.T, metadata *metav1.ObjectMeta, status *wfv1.WorkflowStatus) {
uid = metadata.UID
})

j := s.e().GET("/api/v1/archived-workflows/{uid}", uid).
Expect().
Status(200).
JSON()
j.
Path("$.spec.templates[0].container.args[1]").
IsEqual("hello \u0000")

}

func TestArgoServerSuite(t *testing.T) {
suite.Run(t, new(ArgoServerSuite))
}

0 comments on commit b93ffd8

Please sign in to comment.