Skip to content
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

CLI changes #152

Merged
merged 6 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21

require (
github.com/aws/amazon-vpc-cni-k8s v1.15.1
github.com/aws/aws-ebpf-sdk-go v1.0.4
github.com/aws/aws-ebpf-sdk-go v1.0.6
github.com/aws/aws-sdk-go v1.47.5
github.com/go-logr/logr v1.3.0
github.com/go-logr/zapr v1.2.4
Expand All @@ -18,7 +18,7 @@ require (
github.com/stretchr/testify v1.8.4
github.com/vishvananda/netlink v1.2.1-beta.2
go.uber.org/zap v1.26.0
golang.org/x/sys v0.14.0
golang.org/x/sys v0.15.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
k8s.io/api v0.28.4
k8s.io/apimachinery v0.28.4
Expand Down Expand Up @@ -58,11 +58,12 @@ require (
github.com/vishvananda/netns v0.0.4 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.16.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.31.0 // indirect
Expand Down
24 changes: 12 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/aws/amazon-vpc-cni-k8s v1.15.1 h1:zKhJ58AoFj+QaZfo768mSVFpLr3qeSVV0Qn0aeV2fhE=
github.com/aws/amazon-vpc-cni-k8s v1.15.1/go.mod h1:VjgdEc3U5d05RY5Jnovqt6pLbHmnIkzsgX6sDC6I4II=
github.com/aws/aws-ebpf-sdk-go v1.0.4 h1:WJeuAYd8ThiC22kKJHpGZCJ63wotsJ04rY3JsHhdwVM=
github.com/aws/aws-ebpf-sdk-go v1.0.4/go.mod h1:CCXK40H7FN2eN1FLt/O2vT9eNIDH0uXZxZGxQEdJaIM=
github.com/aws/aws-ebpf-sdk-go v1.0.6 h1:f4f2HKmDEA2hQLd4Sz5jA0YYAFLmgLVnopZIycKcxnA=
github.com/aws/aws-ebpf-sdk-go v1.0.6/go.mod h1:sZz5xaENDQ1Y/FXjGIMJfVCSmHMB4LG9Bc+8+V1MLTo=
github.com/aws/aws-sdk-go v1.47.5 h1:U2JlfPmrUoz5p+2X/XwKxmaJFo2oV+LbJqx8jyEvyAY=
github.com/aws/aws-sdk-go v1.47.5/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
Expand Down Expand Up @@ -161,8 +161,8 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/oauth2 v0.8.0 h1:6dkIjl3j3LtZ/O3sTgZTMsLKSftL/B8Zgq4huOIIUu8=
golang.org/x/oauth2 v0.8.0/go.mod h1:yr7u4HXZRm1R1kBWqr/xKNqewf0plRYoB7sla+BCIXE=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -179,16 +179,16 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand All @@ -198,8 +198,8 @@ golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roY
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.9.3 h1:Gn1I8+64MsuTb/HpH+LmQtNas23LhUVr3rYZ0eKuaMM=
golang.org/x/tools v0.9.3/go.mod h1:owI94Op576fPu3cIGQeHs3joujW/2Oc6MtlxbF5dfNc=
golang.org/x/tools v0.16.0 h1:GO788SKMRunPIBCXiQyo2AaexLstOrVhuAL5YwsckQM=
golang.org/x/tools v0.16.0/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
38 changes: 26 additions & 12 deletions pkg/clihelper/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
func Show() error {

bpfSDKclient := goelf.New()
bpfState, err := bpfSDKclient.RecoverAllBpfProgramsAndMaps()
bpfState, err := bpfSDKclient.GetAllBpfProgramsAndMaps()
if err != nil {
return err
}
Expand All @@ -30,7 +30,7 @@ func Show() error {
line := fmt.Sprintf("Pod Identifier : %s Direction : %s \n", podIdentifier, direction)
fmt.Print(line)
bpfProg := bpfEntry.Program
fmt.Println("Prog FD: ", bpfProg.ProgFD)
fmt.Println("Prog ID: ", bpfProg.ProgID)
fmt.Println("Associated Maps -> ")
bpfMaps := bpfEntry.Maps
for k, v := range bpfMaps {
Expand Down Expand Up @@ -111,17 +111,24 @@ func MapWalk(mapID int) error {
} else {
for {

iterValue := utils.BPFTrieVal{}
iterValue := [24]utils.BPFTrieVal{}
err = goebpfmaps.GetMapEntryByID(uintptr(unsafe.Pointer(&iterKey)), uintptr(unsafe.Pointer(&iterValue)), mapID)
if err != nil {
return fmt.Errorf("Unable to get map entry: %v", err)
} else {
retrievedKey := fmt.Sprintf("Key : IP/Prefixlen - %s/%d ", utils.ConvIntToIPv4(iterKey.IP).String(), iterKey.PrefixLen)
fmt.Println(retrievedKey)
fmt.Println("Value : ")
fmt.Println("Protocol - ", iterValue.Protocol)
fmt.Println("StartPort - ", iterValue.StartPort)
fmt.Println("Endport - ", iterValue.EndPort)
for i := 0; i < len(iterValue); i++ {
if iterValue[i].Protocol == 0 {
continue
}
fmt.Println("-------------------")
fmt.Println("Value Entry : ", i)
fmt.Println("Protocol - ", utils.GetProtocol(int(iterValue[i].Protocol)))
fmt.Println("StartPort - ", iterValue[i].StartPort)
fmt.Println("Endport - ", iterValue[i].EndPort)
fmt.Println("-------------------")
}
fmt.Println("*******************************")
}

Expand Down Expand Up @@ -213,7 +220,7 @@ func MapWalkv6(mapID int) error {
} else {
for {

iterValue := utils.BPFTrieVal{}
iterValue := [24]utils.BPFTrieVal{}

err = goebpfmaps.GetMapEntryByID(uintptr(unsafe.Pointer(&byteSlice[0])), uintptr(unsafe.Pointer(&iterValue)), mapID)
if err != nil {
Expand All @@ -222,10 +229,17 @@ func MapWalkv6(mapID int) error {
v6key := utils.ConvByteToTrieV6(byteSlice)
retrievedKey := fmt.Sprintf("Key : IP/Prefixlen - %s/%d ", utils.ConvByteToIPv6(v6key.IP).String(), v6key.PrefixLen)
fmt.Println(retrievedKey)
fmt.Println("Value : ")
fmt.Println("Protocol - ", iterValue.Protocol)
fmt.Println("StartPort - ", iterValue.StartPort)
fmt.Println("Endport - ", iterValue.EndPort)
for i := 0; i < len(iterValue); i++ {
if iterValue[i].Protocol == 0 {
continue
}
fmt.Println("-------------------")
fmt.Println("Value Entry : ", i)
fmt.Println("Protocol - ", utils.GetProtocol(int(iterValue[i].Protocol)))
fmt.Println("StartPort - ", iterValue[i].StartPort)
fmt.Println("Endport - ", iterValue[i].EndPort)
fmt.Println("-------------------")
}
fmt.Println("*******************************")
}

Expand Down
18 changes: 2 additions & 16 deletions pkg/ebpf/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,6 @@ func setupCW(logger logr.Logger) error {
return nil
}

func getProtocol(protocolNum int) string {
protocolStr := "UNKNOWN"
if protocolNum == utils.TCP_PROTOCOL_NUMBER {
protocolStr = "TCP"
} else if protocolNum == utils.UDP_PROTOCOL_NUMBER {
protocolStr = "UDP"
} else if protocolNum == utils.SCTP_PROTOCOL_NUMBER {
protocolStr = "SCTP"
} else if protocolNum == utils.ICMP_PROTOCOL_NUMBER {
protocolStr = "ICMP"
}
return protocolStr
}

func getVerdict(verdict int) string {
verdictStr := "DENY"
if verdict == utils.ACCEPT.Index() {
Expand Down Expand Up @@ -186,7 +172,7 @@ func capturePolicyEvents(ringbufferdata <-chan []byte, log logr.Logger, enableCl
continue
}

protocol := getProtocol(int(rb.Protocol))
protocol := utils.GetProtocol(int(rb.Protocol))
verdict := getVerdict(int(rb.Verdict))

log.Info("Flow Info: ", "Src IP", utils.ConvByteToIPv6(rb.SourceIP).String(), "Src Port", rb.SourcePort,
Expand All @@ -201,7 +187,7 @@ func capturePolicyEvents(ringbufferdata <-chan []byte, log logr.Logger, enableCl
log.Info("Failed to read from Ring buf", err)
continue
}
protocol := getProtocol(int(rb.Protocol))
protocol := utils.GetProtocol(int(rb.Protocol))
verdict := getVerdict(int(rb.Verdict))

log.Info("Flow Info: ", "Src IP", utils.ConvByteArrayToIP(rb.SourceIP), "Src Port", rb.SourcePort,
Expand Down
18 changes: 18 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ var (
ErrMissingFilter = "no active filter to detach"
)

func GetProtocol(protocolNum int) string {
protocolStr := "UNKNOWN"
if protocolNum == TCP_PROTOCOL_NUMBER {
protocolStr = "TCP"
} else if protocolNum == UDP_PROTOCOL_NUMBER {
protocolStr = "UDP"
} else if protocolNum == SCTP_PROTOCOL_NUMBER {
protocolStr = "SCTP"
} else if protocolNum == ICMP_PROTOCOL_NUMBER {
protocolStr = "ICMP"
} else if protocolNum == RESERVED_IP_PROTOCOL_NUMBER {
protocolStr = "RESERVED"
} else if protocolNum == ANY_IP_PROTOCOL {
protocolStr = "ANY PROTOCOL"
}
return protocolStr
}

type VerdictType int

const (
Expand Down