Skip to content

Commit

Permalink
Add Guacone collect files json.bz2 capability (guacsec#1395)
Browse files Browse the repository at this point in the history
Signed-off-by: desmax74 <[email protected]>
  • Loading branch information
desmax74 authored Oct 18, 2023
1 parent 8829931 commit 8daf872
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
21 changes: 20 additions & 1 deletion cmd/guacone/cmd/gcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

package cmd

import "testing"
import (
"testing"

"github.com/spf13/cobra"
)

func TestValidateGCSFlags(t *testing.T) {
testCases := []struct {
Expand Down Expand Up @@ -78,3 +82,18 @@ func TestValidateGCSFlags(t *testing.T) {
}

}

func TestJsonBz2Ingestion(t *testing.T) {
rootCmd := &cobra.Command{
Use: "guacone",
Short: "guacone",
}
rootCmd.AddCommand(collectCmd)
rootCmd.AddCommand(filesCmd)
bz2Path := "./../../../internal/testing/testdata/exampledata/busybox-cyclonedx.json.bz2"
rootCmd.SetArgs([]string{"collect", "files", bz2Path})
err := rootCmd.Execute()
if err != nil {
t.Fatal(err)
}
}
Binary file not shown.
14 changes: 11 additions & 3 deletions pkg/handler/processor/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"encoding/xml"
"fmt"
"io"
"path/filepath"
"strings"

uuid "github.com/gofrs/uuid"
"github.com/guacsec/guac/pkg/emitter"
Expand Down Expand Up @@ -216,6 +218,13 @@ func decodeDocument(ctx context.Context, i *processor.Document) error {
logger := logging.FromContext(ctx)
var reader io.Reader
var err error
if i.Encoding == "" {
ext := filepath.Ext(i.SourceInformation.Source)
encoding, ok := processor.EncodingExts[strings.ToLower(ext)]
if ok {
i.Encoding = encoding
}
}
logger.Infof("Decoding document with encoding: %v", i.Encoding)
switch i.Encoding {
case processor.EncodingBzip2:
Expand All @@ -225,17 +234,16 @@ func decodeDocument(ctx context.Context, i *processor.Document) error {
if err != nil {
return fmt.Errorf("unable to create zstd reader: %w", err)
}
case processor.EncodingUnknown:
}
if reader != nil {
if err := decompressDocument(ctx, i, reader); err != nil {
if err := decompressDocument(i, reader); err != nil {
return fmt.Errorf("unable to decode document: %w", err)
}
}
return nil
}

func decompressDocument(ctx context.Context, i *processor.Document, reader io.Reader) error {
func decompressDocument(i *processor.Document, reader io.Reader) error {
uncompressed, err := io.ReadAll(reader)
if err != nil {
return fmt.Errorf("unable to decompress document: %w", err)
Expand Down
5 changes: 5 additions & 0 deletions pkg/handler/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ const (
EncodingUnknown EncodingType = "UNKNOWN"
)

var EncodingExts = map[string]EncodingType{
".bz2": EncodingBzip2,
".zst": EncodingZstd,
}

// SourceInformation provides additional information about where the document comes from
type SourceInformation struct {
// Collector describes the name of the collector providing this information
Expand Down

0 comments on commit 8daf872

Please sign in to comment.