Skip to content

Commit

Permalink
[CAPPL-366] Add name to hash to generate workflowID
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-cordenier committed Dec 12, 2024
1 parent 97df614 commit 8738e21
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
22 changes: 17 additions & 5 deletions pkg/workflows/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func EncodeExecutionID(workflowID, eventID string) (string, error) {
return hex.EncodeToString(s.Sum(nil)), nil
}

func GenerateWorkflowIDFromStrings(owner string, workflow []byte, config []byte, secretsURL string) (string, error) {
func GenerateWorkflowIDFromStrings(owner string, name string, workflow []byte, config []byte, secretsURL string) (string, error) {
ownerWithoutPrefix := owner
if strings.HasPrefix(owner, "0x") {
ownerWithoutPrefix = owner[2:]
Expand All @@ -32,21 +32,29 @@ func GenerateWorkflowIDFromStrings(owner string, workflow []byte, config []byte,
return "", err
}

wid, err := GenerateWorkflowID(ownerb, workflow, config, secretsURL)
wid, err := GenerateWorkflowID(ownerb, name, workflow, config, secretsURL)
if err != nil {
return "", err
}

return hex.EncodeToString(wid[:]), nil
}

func GenerateWorkflowID(owner []byte, workflow []byte, config []byte, secretsURL string) ([32]byte, error) {
var (
versionByte = byte(0)
)

func GenerateWorkflowID(owner []byte, name string, workflow []byte, config []byte, secretsURL string) ([32]byte, error) {
s := sha256.New()
_, err := s.Write(owner)
if err != nil {
return [32]byte{}, err
}
_, err = s.Write([]byte(workflow))
_, err = s.Write([]byte(name))
if err != nil {
return [32]byte{}, err
}
_, err = s.Write(workflow)
if err != nil {
return [32]byte{}, err
}
Expand All @@ -58,5 +66,9 @@ func GenerateWorkflowID(owner []byte, workflow []byte, config []byte, secretsURL
if err != nil {
return [32]byte{}, err
}
return [32]byte(s.Sum(nil)), nil

sha := [32]byte(s.Sum(nil))
sha[0] = versionByte

return sha, nil
}
11 changes: 7 additions & 4 deletions pkg/workflows/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,26 @@ func Test_EncodeExecutionID(t *testing.T) {
func Test_GenerateWorkflowIDFromStrings(t *testing.T) {
// With prefix
owner := "0x26729408f179371be6433b9585d8427f121bfe82"
got, err := GenerateWorkflowIDFromStrings(owner, []byte("workflow"), []byte("config"), "http://mysecrets.com")
got, err := GenerateWorkflowIDFromStrings(owner, "porporpore", []byte("workflow"), []byte("config"), "http://mysecrets.com")
require.NoError(t, err)
assert.NotNil(t, got)

// Always starts with the version byte
assert.Equal(t, got[:2], hex.EncodeToString([]byte{versionByte}))

// Without prefix
owner = "26729408f179371be6433b9585d8427f121bfe82"
got, err = GenerateWorkflowIDFromStrings(owner, []byte("workflow"), []byte("config"), "http://mysecrets.com")
got, err = GenerateWorkflowIDFromStrings(owner, "porporpore", []byte("workflow"), []byte("config"), "http://mysecrets.com")
require.NoError(t, err)
assert.NotNil(t, got)

// Very short; empty but with a prefix
owner = "0x"
got, err = GenerateWorkflowIDFromStrings(owner, []byte("workflow"), []byte("config"), "http://mysecrets.com")
got, err = GenerateWorkflowIDFromStrings(owner, "porporpore", []byte("workflow"), []byte("config"), "http://mysecrets.com")
require.NoError(t, err)
assert.NotNil(t, got)

owner = "invalid"
_, err = GenerateWorkflowIDFromStrings(owner, []byte("workflow"), []byte("config"), "http://mysecrets.com")
_, err = GenerateWorkflowIDFromStrings(owner, "porporpore", []byte("workflow"), []byte("config"), "http://mysecrets.com")
assert.ErrorContains(t, err, "encoding/hex")
}

0 comments on commit 8738e21

Please sign in to comment.