Skip to content

Commit 21788fe

Browse files
fix(auditbeat system/package): handle empty package.v1 bucket (#44296) (#44301)
Ensure the schema migration logic properly handles cases where the package.v1 bucket exists but is empty, avoiding assumptions about the presence of the state_timestamp key. Fixes #44294 (cherry picked from commit d7332ab) Co-authored-by: Andrew Kroh <[email protected]>
1 parent f6e045a commit 21788fe

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

CHANGELOG.next.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ otherwise no tag is added. {issue}42208[42208] {pull}42403[42403]
137137
- hasher: Add a cached hasher for upcoming backend. {pull}41952[41952]
138138
- Split common tty definitions. {pull}42004[42004]
139139
- Fix potential data loss in add_session_metadata. {pull}42795[42795]
140+
- system/package: Fix an error that can occur while migrating the internal package database schema. {issue}44294[44294] {pull}44296[44296]
140141

141142
*Auditbeat*
142143

x-pack/auditbeat/module/system/package/package.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -758,11 +758,6 @@ func migrateDatastoreSchema(tx *bbolt.Tx) error {
758758
log := logp.NewLogger(metricsetName)
759759
log.Debugf("Migrating data from %v to %v bucket.", bucketNameV1, bucketNameV2)
760760

761-
var timestampGob []byte
762-
if timestampGob = v1Bucket.Get([]byte(bucketKeyStateTimestamp)); len(timestampGob) == 0 {
763-
return fmt.Errorf("error migrating %v data: no timestamp found", bucketNameV1)
764-
}
765-
766761
var packages []*Package
767762
if data := v1Bucket.Get([]byte(bucketKeyPackages)); len(data) > 0 {
768763
dec := gob.NewDecoder(bytes.NewReader(data))
@@ -795,8 +790,11 @@ func migrateDatastoreSchema(tx *bbolt.Tx) error {
795790
return fmt.Errorf("error migrating data: failed to create %v bucket: %w", bucketNameV2, err)
796791
}
797792

798-
if err = v2Bucket.Put([]byte(bucketKeyStateTimestamp), timestampGob); err != nil {
799-
return fmt.Errorf("error migrating data: failed to write %v to %v bucket: %w", bucketKeyStateTimestamp, bucketNameV2, err)
793+
// Copy the gob encoded state timestamp from the v1 bucket to the v2 bucket.
794+
if timestampGob := v1Bucket.Get([]byte(bucketKeyStateTimestamp)); timestampGob != nil {
795+
if err = v2Bucket.Put([]byte(bucketKeyStateTimestamp), timestampGob); err != nil {
796+
return fmt.Errorf("error migrating data: failed to write %v to %v bucket: %w", bucketKeyStateTimestamp, bucketNameV2, err)
797+
}
800798
}
801799

802800
builder, release := fbGetBuilder()

x-pack/auditbeat/module/system/package/package_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,28 @@ func TestPackageDatabaseMigration(t *testing.T) {
241241
}
242242
}
243243

244+
// TestPackageDatabaseMigrationWithEmptyPackageV1Bucket verifies that an
245+
// empty package.v1 bucket can be migrated to the new schema without errors.
246+
//
247+
// This is a reproduction of https://github.com/elastic/beats/issues/44294.
248+
func TestPackageDatabaseMigrationWithEmptyPackageV1Bucket(t *testing.T) {
249+
// Create empty package.v1 bucket.
250+
dbPath := filepath.Join(t.TempDir(), "beat.db")
251+
ds := datastore.New(dbPath, 0o600)
252+
253+
bucket, err := ds.OpenBucket("package.v1")
254+
if err != nil {
255+
t.Fatal(err)
256+
}
257+
if err = bucket.Close(); err != nil {
258+
t.Fatal(err)
259+
}
260+
261+
if err := ds.Update(migrateDatastoreSchema); err != nil {
262+
t.Fatal(err)
263+
}
264+
}
265+
244266
func copyFile(old, new string) error {
245267
o, err := os.Open(old)
246268
if err != nil {

0 commit comments

Comments
 (0)