From 8738e215034771d00ea80d2cabcdc2f9df868e91 Mon Sep 17 00:00:00 2001 From: Cedric Cordenier Date: Thu, 12 Dec 2024 14:18:00 +0000 Subject: [PATCH] [CAPPL-366] Add name to hash to generate workflowID --- pkg/workflows/utils.go | 22 +++++++++++++++++----- pkg/workflows/utils_test.go | 11 +++++++---- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/pkg/workflows/utils.go b/pkg/workflows/utils.go index d7aae90b0..250912c63 100644 --- a/pkg/workflows/utils.go +++ b/pkg/workflows/utils.go @@ -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:] @@ -32,7 +32,7 @@ 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 } @@ -40,13 +40,21 @@ func GenerateWorkflowIDFromStrings(owner string, workflow []byte, config []byte, 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 } @@ -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 } diff --git a/pkg/workflows/utils_test.go b/pkg/workflows/utils_test.go index 1fccc6839..477807733 100644 --- a/pkg/workflows/utils_test.go +++ b/pkg/workflows/utils_test.go @@ -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") }