-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
logging: Sanitize Journald Field Key Names
As seen over at Icinga Notifications[0], the use of non-alphanumeric characters for journaldCore causes fields to be silently discarded. This was due to journald's field key validation, which is unfortunately not very well documented. Looking at its implementation[1], this library code could be adapted to ensure that valid field keys are always used. [0]: Icinga/icinga-notifications#254 [1]: https://github.com/systemd/systemd/blob/11d5e2b5fbf9f6bfa5763fd45b56829ad4f0777f/src/libsystemd/sd-journal/journal-file.c#L1703
- Loading branch information
Showing
2 changed files
with
89 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package logging | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
func Test_journaldFieldEncode(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
output string | ||
}{ | ||
{"empty", "", "EMPTY_KEY"}, | ||
{"lowercase", "foo", "FOO"}, | ||
{"uppercase", "FOO", "FOO"}, | ||
{"dash", "foo-bar", "FOO_BAR"}, | ||
{"non ascii", "snow_☃", "SNOW__"}, | ||
{"leading number", "23", "ESC_23"}, | ||
{"leading underscore", "_foo", "ESC__FOO"}, | ||
{"leading invalid", " foo", "ESC__FOO"}, | ||
{"max length", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA1234", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA1234"}, | ||
{"too long", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA12345", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA1234"}, | ||
{"too long leading number", "1234567890123456789012345678901234567890123456789012345678901234", "ESC_123456789012345678901234567890123456789012345678901234567890"}, | ||
{"concrete example", "icinga-notifications" + "_" + "error", "ICINGA_NOTIFICATIONS_ERROR"}, | ||
{"example syslog_identifier", "SYSLOG_IDENTIFIER", "SYSLOG_IDENTIFIER"}, | ||
} | ||
|
||
check := regexp.MustCompile(`^[A-Z][A-Z0-9_]{0,63}$`) | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
out := journaldFieldEncode(test.input) | ||
require.Equal(t, test.output, out) | ||
require.True(t, check.MatchString(out), "check regular expression") | ||
}) | ||
} | ||
} |