This repository has been archived by the owner on Feb 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
[NETOBSERV-44] Consumming goflow2 as a library. #6
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4163a90
[NETOBSERV-44] Consumming goflow2 as a library.
OlivierCazade 137fbcf
Added transport wrapper unit tests
OlivierCazade cb1d18f
Dockerfile update after consumming goflow2 as a library
OlivierCazade cf1866b
Fixing linter warning due to Message flow containing a lock
OlivierCazade b46e471
Updated protobuf package
OlivierCazade 7346b8e
Different code improvements :
OlivierCazade d75e6e3
Changing Dockerfile to use ubi8 images
OlivierCazade File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,20 @@ | ||
FROM golang:alpine as builder | ||
FROM registry.access.redhat.com/ubi8/go-toolset:1.15.14-10 as builder | ||
ARG VERSION="" | ||
|
||
RUN apk --update --no-cache add git build-base gcc | ||
WORKDIR /opt/app-root/src | ||
|
||
COPY go.mod ./ | ||
COPY go.sum ./ | ||
|
||
RUN go mod download | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will surely change in the future if we want to include vendors in sources as Mario did for the loki client. But we can do that as a follow-up. |
||
|
||
COPY . . | ||
|
||
COPY . /build | ||
WORKDIR /build | ||
|
||
RUN go build -ldflags "-X main.version=${VERSION}" -o kube-enricher cmd/kube-enricher/main.go | ||
|
||
FROM netsampler/goflow2:latest | ||
FROM registry.access.redhat.com/ubi8/ubi-minimal:8.4-210 | ||
|
||
COPY --from=builder /build/kube-enricher / | ||
COPY --from=builder /opt/app-root/src/kube-enricher ./ | ||
|
||
ENTRYPOINT ["./goflow2"] | ||
ENTRYPOINT ["./kube-enricher"] |
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
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,48 @@ | ||
package netflow | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
goflow2Format "github.com/netsampler/goflow2/format" | ||
_ "github.com/netsampler/goflow2/format/protobuf" | ||
"github.com/netsampler/goflow2/utils" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
const channelSize = 5 | ||
|
||
type Driver struct { | ||
in chan map[string]interface{} | ||
} | ||
|
||
// Start a new go routine to handle netflow connections | ||
func StartDriver(ctx context.Context, hostname string, port int) *Driver { | ||
gf := Driver{} | ||
gf.in = make(chan map[string]interface{}, channelSize) | ||
|
||
go func() { | ||
transporter := NewWrapper(gf.in) | ||
|
||
formatter, err := goflow2Format.FindFormat(ctx, "pb") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
sNF := &utils.StateNetFlow{ | ||
Format: formatter, | ||
Transport: transporter, | ||
Logger: logrus.StandardLogger(), | ||
} | ||
err = sNF.FlowRoutine(1, hostname, port, false) | ||
log.Fatal(err) | ||
|
||
}() | ||
|
||
return &gf | ||
} | ||
|
||
func (gf *Driver) Next() (map[string]interface{}, error) { | ||
msg := <-gf.in | ||
return msg, nil | ||
} |
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,30 @@ | ||
package netflow | ||
|
||
import ( | ||
pbFormat "github.com/netobserv/goflow2-kube-enricher/pkg/format/pb" | ||
goflowpb "github.com/netsampler/goflow2/pb" | ||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
// This is en implementation of the goflow2 transport interface | ||
type TransportWrapper struct { | ||
c chan map[string]interface{} | ||
} | ||
|
||
func NewWrapper(c chan map[string]interface{}) *TransportWrapper { | ||
tw := TransportWrapper{c: c} | ||
return &tw | ||
} | ||
|
||
func (w *TransportWrapper) Send(key, data []byte) error { | ||
message := goflowpb.FlowMessage{} | ||
err := proto.Unmarshal(data, &message) | ||
if err != nil { | ||
return err | ||
} | ||
renderedMsg, err := pbFormat.RenderMessage(&message) | ||
if err == nil { | ||
w.c <- renderedMsg | ||
} | ||
return err | ||
} |
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,44 @@ | ||
package netflow | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestWrapperSingleMessage(t *testing.T) { | ||
assert := assert.New(t) | ||
c := make(chan map[string]interface{}, 5) | ||
wrapper := NewWrapper(c) | ||
data := []byte{ | ||
0x08, 0x04, 0x10, 0xa6, 0x87, 0x91, 0x8b, 0x06, 0x20, 0x02, 0x32, 0x04, | ||
0x0a, 0xf4, 0x02, 0x03, 0x3a, 0x04, 0x0a, 0xf4, 0x02, 0x02, 0x48, 0xc0, | ||
0xa2, 0x01, 0x50, 0x90, 0x03, 0x5a, 0x04, 0x64, 0x40, 0x00, 0x03, 0x90, | ||
0x01, 0x07, 0xa0, 0x01, 0x06, 0xa8, 0x01, 0xf5, 0x3f, 0xb0, 0x01, 0xb6, | ||
0xda, 0x02, 0xd8, 0x01, 0x83, 0x84, 0xd0, 0xd7, 0x80, 0xcb, 0x02, 0xe0, | ||
0x01, 0x8a, 0xf2, 0xac, 0xec, 0xae, 0xd9, 0x30, 0xf0, 0x01, 0x80, 0x10, | ||
0x0a, 0x08, 0x04, 0x10, 0xe7, 0x87, 0x91, 0x8b, 0x06, 0x20, | ||
} | ||
err := wrapper.Send(nil, data) | ||
assert.Nil(err) | ||
message := <-c | ||
assert.Contains(message, "SrcMac") | ||
assert.Equal(message["SrcMac"], "0a:58:0a:f4:02:03") | ||
assert.Contains(message, "SrcAddr") | ||
assert.Equal(message["SrcAddr"], "10.244.2.3") | ||
assert.Contains(message, "DstMac") | ||
assert.Equal(message["DstMac"], "c2:ca:ed:8b:39:0a") | ||
assert.Contains(message, "DstAddr") | ||
assert.Equal(message["DstAddr"], "10.244.2.2") | ||
} | ||
|
||
func TestWrapperError(t *testing.T) { | ||
assert := assert.New(t) | ||
c := make(chan map[string]interface{}, 5) | ||
wrapper := NewWrapper(c) | ||
data := []byte{ | ||
0xff, 0xab, 0xcd, 0xef, | ||
} | ||
err := wrapper.Send(nil, data) | ||
assert.Error(err) | ||
} |
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
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.
Just noting / realizing that if we stick to the redhat-supported tool chain we have to use this older version of go. Generics will wait :)