-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
[KS-421] Improve logging from remote capabilities #14058
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import ( | |
"encoding/hex" | ||
"errors" | ||
"fmt" | ||
"unicode" | ||
|
||
"google.golang.org/protobuf/proto" | ||
|
||
|
@@ -16,6 +17,12 @@ import ( | |
p2ptypes "github.com/smartcontractkit/chainlink/v2/core/services/p2p/types" | ||
) | ||
|
||
const ( | ||
maxLoggedStringLen = 256 | ||
validWorkflowIDLen = 64 | ||
maxIDLen = 128 | ||
) | ||
|
||
func ValidateMessage(msg p2ptypes.Message, expectedReceiver p2ptypes.PeerID) (*remotetypes.MessageBody, error) { | ||
var topLevelMessage remotetypes.Message | ||
err := proto.Unmarshal(msg.Payload, &topLevelMessage) | ||
|
@@ -93,3 +100,39 @@ func AggregateModeRaw(elemList [][]byte, minIdenticalResponses uint32) ([]byte, | |
} | ||
return found, nil | ||
} | ||
|
||
func SanitizeLogString(s string) string { | ||
tooLongSuffix := "" | ||
if len(s) > maxLoggedStringLen { | ||
s = s[:maxLoggedStringLen] | ||
tooLongSuffix = " [TRUNCATED]" | ||
} | ||
for i := 0; i < len(s); i++ { | ||
if !unicode.IsPrint(rune(s[i])) { | ||
return "[UNPRINTABLE] " + hex.EncodeToString([]byte(s)) + tooLongSuffix | ||
} | ||
} | ||
return s + tooLongSuffix | ||
} | ||
|
||
// Workflow IDs and Execution IDs are 32-byte hex-encoded strings | ||
func IsValidWorkflowOrExecutionID(id string) bool { | ||
if len(id) != validWorkflowIDLen { | ||
return false | ||
} | ||
_, err := hex.DecodeString(id) | ||
return err == nil | ||
} | ||
|
||
// Trigger event IDs and message IDs can only contain printable characters and must be non-empty | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out of curiosity, is there a reason why we can't have the same ID validation as above? Namely, could Trigger event IDs and message IDs also be 32-byte hex-encoded strings? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently trigger event IDs are human readable, e.g. "streams_123456890" and I think that makes it debug-friendly. As for Message IDs we could probably impose some extra restriction ... |
||
func IsValidID(id string) bool { | ||
if len(id) == 0 || len(id) > maxIDLen { | ||
return false | ||
} | ||
for i := 0; i < len(id); i++ { | ||
if !unicode.IsPrint(rune(id[i])) { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: t.Fatal instead?