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
[auditbeat] - Migration of system/package module to flatbuffers #34817
Merged
Merged
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
dcf6e76
initial working commit
ShourieG 870305f
flatbuffers migration now working with tests
ShourieG cc8592b
updated changelog
ShourieG c38ae22
Merge remote-tracking branch 'upstream/main' into auditbeat/flat_buffers
ShourieG 1cffa01
removed comments
ShourieG aaf3fc2
resolved PR suggetions and updated license
ShourieG 5d0f6e4
updated notice
ShourieG 4ac7ef8
updated tests, added benchmarks, renamed files
ShourieG 2d55de8
Merge remote-tracking branch 'upstream/main' into auditbeat/flat_buffers
ShourieG 7525656
updated tests & added error storage in schema
ShourieG 7166fb3
license issue fixed
ShourieG 4d7bee9
Merge remote-tracking branch 'upstream/main' into auditbeat/flat_buffers
ShourieG d6f553a
updated according to PR suggestions
ShourieG 2aa8112
Merge remote-tracking branch 'upstream/main' into auditbeat/flat_buffers
ShourieG 892e37e
refactored package migration based on bucket version and is now done …
ShourieG 15e1fd1
Merge remote-tracking branch 'upstream/main' into auditbeat/flat_buffers
ShourieG f8369d9
Merge remote-tracking branch 'elastic/main' into auditbeat/flat_buffers
andrewkroh 5096686
Perform migration inside of a bolt transaction, fix linter warnings
andrewkroh c7feb02
Merge remote-tracking branch 'upstream/main' into auditbeat/flat_buffers
ShourieG File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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,193 @@ | ||
// 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. | ||
|
||
//go:build !windows | ||
// +build !windows | ||
|
||
package pkg | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
flatbuffers "github.com/google/flatbuffers/go" | ||
|
||
"github.com/elastic/beats/v7/x-pack/auditbeat/module/system/package/schema" | ||
) | ||
|
||
// Requires the Google flatbuffer compiler and Elastic go-licenser. | ||
//go:generate flatc --go schema.fbs | ||
//go:generate go-licenser schema | ||
ShourieG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
var bufferPool sync.Pool | ||
|
||
func init() { | ||
bufferPool.New = func() interface{} { | ||
return flatbuffers.NewBuilder(1024) | ||
} | ||
} | ||
|
||
// fbGetBuilder returns a Builder that can be used for encoding data. The builder | ||
// should be put back into the pool by invoking the put function after the encoded bytes | ||
// are no longer in used (i.e. a copy of b.FinishedBytes() has been made). | ||
func fbGetBuilder() (b *flatbuffers.Builder, put func()) { | ||
b = bufferPool.Get().(*flatbuffers.Builder) | ||
b.Reset() | ||
return b, func() { bufferPool.Put(b) } | ||
} | ||
|
||
// encodePackages, encodes an array of packages by creating a vector of packages and tracking offsets. It uses the | ||
// func fbEncodePackage to encode individual packages, and returns a []byte containing the encoded data | ||
func encodePackages(builder *flatbuffers.Builder, packages []*Package) []byte { | ||
offsets := make([]flatbuffers.UOffsetT, len(packages)) | ||
|
||
for i, p := range packages { | ||
offsets[i] = fbEncodePackage(builder, p) | ||
} | ||
schema.PackageContainerStartPackagesVector(builder, len(offsets)) | ||
for _, offset := range offsets { | ||
builder.PrependUOffsetT(offset) | ||
} | ||
packageContainerVector := builder.EndVector(len(offsets)) | ||
schema.PackageContainerStart(builder) | ||
schema.PackageContainerAddPackages(builder, packageContainerVector) | ||
root := schema.PackageContainerEnd(builder) | ||
builder.Finish(root) | ||
return builder.FinishedBytes() | ||
} | ||
|
||
// fbEncodePackage encodes the given Package to a flatbuffer. The returned bytes | ||
// are a pointer into the Builder's memory. | ||
func fbEncodePackage(b *flatbuffers.Builder, p *Package) flatbuffers.UOffsetT { | ||
if p == nil { | ||
return 0 | ||
} | ||
|
||
return fbWritePackage(b, p) | ||
} | ||
|
||
func fbWritePackage(b *flatbuffers.Builder, p *Package) flatbuffers.UOffsetT { | ||
if p == nil { | ||
return 0 | ||
} | ||
|
||
var ( | ||
packageNameOffset, | ||
packageVersionOffset, | ||
packageReleaseOffset, | ||
packageArchOffset, | ||
packageLicenseOffset, | ||
packageSummaryOffset, | ||
packageURLOffset, | ||
packageTypeOffset, | ||
packageErrorOffset flatbuffers.UOffsetT | ||
) | ||
|
||
if p.Name != "" { | ||
packageNameOffset = b.CreateString(p.Name) | ||
} | ||
if p.Version != "" { | ||
packageVersionOffset = b.CreateString(p.Version) | ||
} | ||
if p.Release != "" { | ||
packageReleaseOffset = b.CreateString(p.Release) | ||
} | ||
if p.Arch != "" { | ||
packageArchOffset = b.CreateString(p.Arch) | ||
} | ||
if p.License != "" { | ||
packageLicenseOffset = b.CreateString(p.License) | ||
} | ||
if p.Summary != "" { | ||
packageSummaryOffset = b.CreateString(p.Summary) | ||
} | ||
if p.URL != "" { | ||
packageURLOffset = b.CreateString(p.URL) | ||
} | ||
if p.Type != "" { | ||
packageTypeOffset = b.CreateString(p.Type) | ||
} | ||
if p.error != nil { | ||
packageErrorOffset = b.CreateString(p.error.Error()) | ||
} | ||
|
||
schema.PackageStart(b) | ||
schema.PackageAddInstalltime(b, uint64(p.InstallTime.Unix())) | ||
schema.PackageAddSize(b, p.Size) | ||
|
||
if packageNameOffset > 0 { | ||
schema.PackageAddName(b, packageNameOffset) | ||
} | ||
if packageVersionOffset > 0 { | ||
schema.PackageAddVersion(b, packageVersionOffset) | ||
} | ||
if packageReleaseOffset > 0 { | ||
schema.PackageAddRelease(b, packageReleaseOffset) | ||
} | ||
if packageArchOffset > 0 { | ||
schema.PackageAddArch(b, packageArchOffset) | ||
} | ||
if packageLicenseOffset > 0 { | ||
schema.PackageAddLicense(b, packageLicenseOffset) | ||
} | ||
if packageSummaryOffset > 0 { | ||
schema.PackageAddSummary(b, packageSummaryOffset) | ||
} | ||
if packageURLOffset > 0 { | ||
schema.PackageAddUrl(b, packageURLOffset) | ||
} | ||
if packageTypeOffset > 0 { | ||
schema.PackageAddType(b, packageTypeOffset) | ||
} | ||
if packageErrorOffset > 0 { | ||
schema.PackageAddError(b, packageErrorOffset) | ||
} | ||
|
||
return schema.PackageEnd(b) | ||
} | ||
|
||
// decodePackagesFromContainer, accepts a flatbuffer encoded byte slice, and decodes | ||
ShourieG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// each package from the container vector with the help of he func fbDecodePackage. | ||
// It returns an array of package objects. | ||
func decodePackagesFromContainer(data []byte) ([]*Package, error) { | ||
var packages []*Package | ||
container := schema.GetRootAsPackageContainer(data, 0) | ||
for i := 0; i < container.PackagesLength(); i++ { | ||
sPkg := schema.Package{} | ||
done := container.Packages(&sPkg, i) | ||
if !done { | ||
return nil, fmt.Errorf("failed to load package at container vector position: %d", i) | ||
} else { | ||
p := fbDecodePackage(&sPkg) | ||
packages = append(packages, p) | ||
} | ||
} | ||
return packages, nil | ||
} | ||
|
||
// fbDecodePackage decodes flatbuffer package data and copies it into a Package | ||
// object that is returned. | ||
func fbDecodePackage(p *schema.Package) *Package { | ||
var err error | ||
if string(p.Error()) != "" { | ||
err = errors.New(string(p.Error())) | ||
} | ||
|
||
return &Package{ | ||
Name: string(p.Name()), | ||
Version: string(p.Version()), | ||
Release: string(p.Release()), | ||
Arch: string(p.Arch()), | ||
License: string(p.License()), | ||
InstallTime: time.Unix(int64(p.Installtime()), 0).UTC(), | ||
Size: p.Size(), | ||
Summary: string(p.Summary()), | ||
URL: string(p.Url()), | ||
Type: string(p.Type()), | ||
error: err, | ||
} | ||
|
||
ShourieG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This code is repeated a lot. How about adding a
boltDatastore.init() error
method.