Skip to content

Commit

Permalink
CBG-3978: Write AuditEvents table to CSV for review/use by PM/Docs (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrks committed Jul 2, 2024
1 parent f61b4a7 commit 47b7d4a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
45 changes: 45 additions & 0 deletions base/audit_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@
package base

import (
"bytes"
"encoding/csv"
"fmt"
"strconv"
"strings"
"testing"

"github.com/stretchr/testify/require"
"golang.org/x/exp/maps"
)

func TestValidateAuditEvents(t *testing.T) {
Expand All @@ -29,3 +34,43 @@ func validateAuditEvents(e events) error {
}
return nil
}

// TestGenerateAuditDescriptorCSV outputs a CSV of AuditEvents.
func TestGenerateAuditDescriptorCSV(t *testing.T) {
b, err := generateCSVModuleDescriptor(AuditEvents)
require.NoError(t, err)
fmt.Print(string(b))
}

// generateCSVModuleDescriptor returns a CSV module descriptor for the given events.
func generateCSVModuleDescriptor(e events) ([]byte, error) {
buf := bytes.NewBuffer(nil)
w := csv.NewWriter(buf)

// Write header
if err := w.Write([]string{"ID", "Name", "Description", "DefaultEnabled", "Filterable", "EventType", "MandatoryFields", "OptionalFields"}); err != nil {
return nil, err
}

for id, event := range e {
if err := w.Write([]string{
id.String(),
event.Name,
event.Description,
strconv.FormatBool(event.EnabledByDefault),
strconv.FormatBool(event.FilteringPermitted),
string(event.EventType),
strings.Join(maps.Keys(event.MandatoryFields), ", "),
strings.Join(maps.Keys(event.OptionalFields), ", "),
}); err != nil {
return nil, err
}
}

w.Flush()
if err := w.Error(); err != nil {
return nil, err
}

return buf.Bytes(), nil
}
2 changes: 1 addition & 1 deletion base/auditd_descriptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/stretchr/testify/require"
)

// TestGenerateAuditdModuleDescriptor outputs a generated auditd module descriptor for SGAuditEvents.
// TestGenerateAuditdModuleDescriptor outputs a generated auditd module descriptor for AuditEvents.
func TestGenerateAuditdModuleDescriptor(t *testing.T) {
b, err := generateAuditdModuleDescriptor(AuditEvents)
require.NoError(t, err)
Expand Down

0 comments on commit 47b7d4a

Please sign in to comment.