diff --git a/internal/pkg/agent/application/upgrade/marker_access_common.go b/internal/pkg/agent/application/upgrade/marker_access_common.go index fc069466c2c..d195577fa2d 100644 --- a/internal/pkg/agent/application/upgrade/marker_access_common.go +++ b/internal/pkg/agent/application/upgrade/marker_access_common.go @@ -10,7 +10,7 @@ import ( ) func writeMarkerFileCommon(markerFile string, markerBytes []byte, shouldFsync bool) error { - f, err := os.OpenFile(markerFile, os.O_WRONLY|os.O_CREATE, 0600) + f, err := os.OpenFile(markerFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) if err != nil { return fmt.Errorf("failed to open upgrade marker file for writing: %w", err) } diff --git a/internal/pkg/agent/application/upgrade/marker_access_common_test.go b/internal/pkg/agent/application/upgrade/marker_access_common_test.go new file mode 100644 index 00000000000..5a599071794 --- /dev/null +++ b/internal/pkg/agent/application/upgrade/marker_access_common_test.go @@ -0,0 +1,56 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +package upgrade + +import ( + "math/rand" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestWriteMarkerFileWithTruncation(t *testing.T) { + tmpDir := t.TempDir() + testMarkerFile := filepath.Join(tmpDir, markerFilename) + + // Write a long marker file + err := writeMarkerFileCommon(testMarkerFile, randomBytes(40), true) + require.NoError(t, err) + + // Get length of file + fileInfo, err := os.Stat(testMarkerFile) + require.NoError(t, err) + originalSize := fileInfo.Size() + + // Write a shorter marker file + err = writeMarkerFileCommon(testMarkerFile, randomBytes(25), true) + require.NoError(t, err) + + // Get length of file + fileInfo, err = os.Stat(testMarkerFile) + require.NoError(t, err) + newSize := fileInfo.Size() + + // Make sure shorter file has is smaller in length than + // the original long marker file + require.Less(t, newSize, originalSize) +} + +func randomBytes(length int) []byte { + chars := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ" + + "abcdefghijklmnopqrstuvwxyzåäö" + + "0123456789" + + "~=+%^*/()[]{}/!@#$?|") + + var b []byte + for i := 0; i < length; i++ { + rune := chars[rand.Intn(len(chars))] + b = append(b, byte(rune)) + } + + return b +}