Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,18 @@ type stdoutWriter struct {
isatty bool
}

// NewLineChecksum computes a CRC32 over the log line, which can be checked by
// LogLineChecksum computes a CRC32 over the log line, which can be checked by
// log-validator to ensure no unexpected log corruption has occurred.
// It is currently only accepted for Validation, and will be switched in for
// LogLineChecksum in an upcoming release.
func NewLineChecksum(line string) string {
func LogLineChecksum(line string) string {
crc := crc32.ChecksumIEEE([]byte(line))
buf := make([]byte, crc32.Size)
// Error is unreachable because we provide a supported type and buffer size
_, _ = binary.Encode(buf, binary.LittleEndian, crc)
return base64.RawURLEncoding.EncodeToString(buf)
}

// LogLineChecksum is the current checksum algorithm, emitted in every log line.
func LogLineChecksum(line string) string {
// OldLineChecksum was previously used, and is still accepted for validation.
func OldLineChecksum(line string) string {
crc := crc32.ChecksumIEEE([]byte(line))
// Using the hash.Hash32 doesn't make this any easier
// as it also returns a uint32 rather than []byte
Expand Down
12 changes: 6 additions & 6 deletions log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ func TestStdoutLogger(t *testing.T) {
logger.Warning("Warning log")
logger.Info("Info log")

test.AssertEquals(t, stdout.String(), "1970-01-01 prefix 6 log.test pcbo7wk Info log\n")
test.AssertEquals(t, stderr.String(), "1970-01-01 prefix 3 log.test 46_ghQg [AUDIT] Error Audit\n1970-01-01 prefix 4 log.test 97r2xAw Warning log\n")
test.AssertEquals(t, stdout.String(), "1970-01-01 prefix 6 log.test JSP6nQ Info log\n")
test.AssertEquals(t, stderr.String(), "1970-01-01 prefix 3 log.test 4xe4gA [AUDIT] Error Audit\n1970-01-01 prefix 4 log.test d52dyA Warning log\n")
}

func TestSyslogMethods(t *testing.T) {
Expand Down Expand Up @@ -352,14 +352,14 @@ func TestLogLineChecksum(t *testing.T) {
expected string
}{
{
name: "NewLineChecksum with Hello, World!",
function: NewLineChecksum,
name: "LogLineChecksum with Hello, World!",
function: LogLineChecksum,
input: "Hello, World!",
expected: "0MNK7A",
},
{
name: "LogLineChecksum with Info log",
function: LogLineChecksum,
name: "OldLineChecksum with Info log",
function: OldLineChecksum,
input: "Info log",
expected: "pcbo7wk",
},
Expand Down
4 changes: 2 additions & 2 deletions log/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,9 @@ func lineValid(text string) error {
// TODO(#8414): Accept both the old and new checksum format, distinguished by length
var computedChecksum string
if len(checksum) == 6 {
computedChecksum = log.NewLineChecksum(line)
} else {
computedChecksum = log.LogLineChecksum(line)
} else {
computedChecksum = log.OldLineChecksum(line)
}
if checksum != computedChecksum {
return fmt.Errorf("%s invalid checksum (expected %q, got %q)", errorPrefix, computedChecksum, checksum)
Expand Down