Skip to content

Commit

Permalink
core/proto: send PolicyDataList along with PolicyList in SetProbeCont…
Browse files Browse the repository at this point in the history
…ainerData

Signed-off-by: tesla59 <[email protected]>
  • Loading branch information
tesla59 authored and daemon1024 committed Dec 27, 2024
1 parent 2023897 commit 5fb7c97
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 104 deletions.
49 changes: 29 additions & 20 deletions KubeArmor/core/karmorprobedata.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package core

import (
"context"
"encoding/json"

"github.com/golang/protobuf/ptypes/empty"
kl "github.com/kubearmor/KubeArmor/KubeArmor/common"
Expand All @@ -28,7 +29,7 @@ type KarmorData struct {
HostVisibility string
}

// Karmor provides structure to serve Policy gRPC service
// Probe provides structure to serve Policy gRPC service
type Probe struct {
pb.ProbeServiceServer
GetContainerData func() ([]string, map[string]*pb.ContainerData, map[string]*pb.HostSecurityPolicies)
Expand Down Expand Up @@ -73,12 +74,11 @@ func (dm *KubeArmorDaemon) SetKarmorData() {

}

// SetKarmorContainerData() keeps track of containers and the applied policies
// SetProbeContainerData keeps track of containers and the applied policies
func (dm *KubeArmorDaemon) SetProbeContainerData() ([]string, map[string]*pb.ContainerData, map[string]*pb.HostSecurityPolicies) {
var containerlist []string
dm.ContainersLock.Lock()
for _, value := range dm.Containers {

containerlist = append(containerlist, value.ContainerName)
}
dm.ContainersLock.Unlock()
Expand All @@ -87,57 +87,66 @@ func (dm *KubeArmorDaemon) SetProbeContainerData() ([]string, map[string]*pb.Con
dm.EndPointsLock.Lock()

for _, ep := range dm.EndPoints {

var policyNames []string
var policyData []*pb.Policy

for _, policy := range ep.SecurityPolicies {

policyNames = append(policyNames, policy.Metadata["policyName"])

policyEventData, err := json.Marshal(policy)
if err != nil {
dm.Logger.Errf("Error marshalling policy data (%s)", err.Error())
} else {
policyData = append(policyData, &pb.Policy{Policy: policyEventData})
}
}
containerMap[ep.EndPointName] = &pb.ContainerData{
PolicyList: policyNames,
PolicyEnabled: int32(ep.PolicyEnabled),
PolicyList: policyNames,
PolicyEnabled: int32(ep.PolicyEnabled),
PolicyDataList: policyData,
}
}
dm.EndPointsLock.Unlock()

// Mapping Hostpolicies to their host hostName : HostPolicy
// Mapping HostPolicies to their host hostName : HostPolicy
hostMap := make(map[string]*pb.HostSecurityPolicies)

dm.HostSecurityPoliciesLock.Lock()
for _, hp := range dm.HostSecurityPolicies {

hostName := dm.Node.NodeName

if val, ok := hostMap[hostName]; ok {

val.PolicyList = append(val.PolicyList, hp.Metadata["policyName"])
policyEventData, err := json.Marshal(hp)
if err != nil {
dm.Logger.Errf("Error marshalling policy data (%s)", err.Error())
} else {
val.PolicyDataList = append(val.PolicyDataList, &pb.Policy{
Policy: policyEventData,
})
}
hostMap[hostName] = val

} else {

policyEventData, err := json.Marshal(hp)
if err != nil {
dm.Logger.Errf("Error marshalling policy data (%s)", err.Error())
}
hostMap[hostName] = &pb.HostSecurityPolicies{
PolicyList: []string{hp.Metadata["policyName"]},
PolicyList: []string{hp.Metadata["policyName"]},
PolicyDataList: []*pb.Policy{{Policy: policyEventData}},
}

}
}
dm.HostSecurityPoliciesLock.Unlock()

return containerlist, containerMap, hostMap

}

// GetProbeData() sends policy data through grpc client
// GetProbeData sends policy data through grpc client
func (p *Probe) GetProbeData(c context.Context, in *empty.Empty) (*pb.ProbeResponse, error) {

containerList, containerMap, hostMap := p.GetContainerData()
res := &pb.ProbeResponse{
ContainerList: containerList,
ContainerMap: containerMap,
HostMap: hostMap,
}

return res, nil
}
1 change: 1 addition & 0 deletions protobuf/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ replace (
)

require (
github.com/golang/protobuf v1.5.4
google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.34.2
)
Expand Down
2 changes: 2 additions & 0 deletions protobuf/go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
Expand Down
171 changes: 98 additions & 73 deletions protobuf/policy.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5fb7c97

Please sign in to comment.