Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
Edits.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joerger committed Nov 3, 2023
1 parent 9ba669a commit 9d616c2
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 112 deletions.
3 changes: 2 additions & 1 deletion event-handler/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import (
"time"

"github.com/alecthomas/kong"
"github.com/gravitational/trace"

"github.com/gravitational/teleport/integrations/lib/logger"
"github.com/gravitational/teleport/integrations/lib/stringset"
"github.com/gravitational/trace"

"github.com/gravitational/teleport-plugins/event-handler/lib"
)
Expand Down
56 changes: 45 additions & 11 deletions event-handler/teleport_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ import (
"encoding/hex"
"testing"

auditlogpb "github.com/gravitational/teleport/api/gen/proto/go/teleport/auditlog/v1"
"github.com/gravitational/teleport/api/types/events"
"github.com/gravitational/trace"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/gravitational/teleport-plugins/event-handler/lib"
auditlogpb "github.com/gravitational/teleport/api/gen/proto/go/teleport/auditlog/v1"
"github.com/gravitational/teleport/api/types/events"
)

func TestNew(t *testing.T) {
Expand All @@ -39,7 +40,10 @@ func TestNew(t *testing.T) {
},
}

event, err := NewTeleportEvent(eventToJSON(t, events.AuditEvent(e)), "cursor")
protoEvent, err := eventToProto(events.AuditEvent(e))
require.NoError(t, err)

event, err := NewTeleportEvent(protoEvent, "cursor")
require.NoError(t, err)
assert.Equal(t, "test", event.ID)
assert.Equal(t, "mock", event.Type)
Expand All @@ -49,7 +53,10 @@ func TestNew(t *testing.T) {
func TestGenID(t *testing.T) {
e := &events.SessionPrint{}

event, err := NewTeleportEvent(eventToJSON(t, events.AuditEvent(e)), "cursor")
protoEvent, err := eventToProto(events.AuditEvent(e))
require.NoError(t, err)

event, err := NewTeleportEvent(protoEvent, "cursor")
require.NoError(t, err)
assert.NotEmpty(t, event.ID)
}
Expand All @@ -64,7 +71,10 @@ func TestSessionEnd(t *testing.T) {
},
}

event, err := NewTeleportEvent(eventToJSON(t, events.AuditEvent(e)), "cursor")
protoEvent, err := eventToProto(events.AuditEvent(e))
require.NoError(t, err)

event, err := NewTeleportEvent(protoEvent, "cursor")
require.NoError(t, err)
assert.NotEmpty(t, event.ID)
assert.NotEmpty(t, event.SessionID)
Expand All @@ -81,7 +91,10 @@ func TestFailedLogin(t *testing.T) {
},
}

event, err := NewTeleportEvent(eventToJSON(t, events.AuditEvent(e)), "cursor")
protoEvent, err := eventToProto(events.AuditEvent(e))
require.NoError(t, err)

event, err := NewTeleportEvent(protoEvent, "cursor")
require.NoError(t, err)
assert.NotEmpty(t, event.ID)
assert.True(t, event.IsFailedLogin)
Expand All @@ -97,28 +110,49 @@ func TestSuccessLogin(t *testing.T) {
},
}

event, err := NewTeleportEvent(eventToJSON(t, events.AuditEvent(e)), "cursor")
protoEvent, err := eventToProto(events.AuditEvent(e))
require.NoError(t, err)

event, err := NewTeleportEvent(protoEvent, "cursor")
require.NoError(t, err)
assert.NotEmpty(t, event.ID)
assert.False(t, event.IsFailedLogin)
}

func eventToJSON(t *testing.T, e events.AuditEvent) *auditlogpb.EventUnstructured {
func eventToProto(e events.AuditEvent) (*auditlogpb.EventUnstructured, error) {
data, err := lib.FastMarshal(e)
require.NoError(t, err)
if err != nil {
return nil, trace.Wrap(err)
}

str := &structpb.Struct{}
err = str.UnmarshalJSON(data)
require.NoError(t, err)
if err = str.UnmarshalJSON(data); err != nil {
return nil, trace.Wrap(err)
}

id := e.GetID()
if id == "" {
hash := sha256.Sum256(data)
id = hex.EncodeToString(hash[:])
}

return &auditlogpb.EventUnstructured{
Type: e.GetType(),
Unstructured: str,
Id: id,
Index: e.GetIndex(),
Time: timestamppb.New(e.GetTime()),
}, nil
}

func eventsToProto(events []events.AuditEvent) ([]*auditlogpb.EventUnstructured, error) {
protoEvents := make([]*auditlogpb.EventUnstructured, len(events))
for i, event := range events {
protoEvent, err := eventToProto(event)
if err != nil {
return nil, trace.Wrap(err)
}
protoEvents[i] = protoEvent
}
return protoEvents, nil
}
7 changes: 4 additions & 3 deletions event-handler/teleport_events_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import (
"fmt"
"time"

"github.com/gravitational/trace"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"

"github.com/gravitational/teleport/api/client"
"github.com/gravitational/teleport/api/client/proto"
auditlogpb "github.com/gravitational/teleport/api/gen/proto/go/teleport/auditlog/v1"
Expand All @@ -28,9 +32,6 @@ import (
"github.com/gravitational/teleport/integrations/lib"
"github.com/gravitational/teleport/integrations/lib/credentials"
"github.com/gravitational/teleport/integrations/lib/logger"
"github.com/gravitational/trace"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
)

const (
Expand Down
Loading

0 comments on commit 9d616c2

Please sign in to comment.