Skip to content

Commit

Permalink
systemd policy
Browse files Browse the repository at this point in the history
Signed-off-by: Aryan-sharma11 <[email protected]>
  • Loading branch information
Aryan-sharma11 committed Sep 4, 2023
1 parent 82ac61e commit ab27d26
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 58 deletions.
34 changes: 34 additions & 0 deletions vm/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package vm

import (
"fmt"

pb "github.com/kubearmor/KubeArmor/protobuf"
)

func responseHandler(resp *pb.Response) error {

switch resp.Status {

case 0, 4, -3:
return fmt.Errorf("failed")

case 1:
fmt.Println("Success")

case 2:
fmt.Println("Policy applied successfully")

case 3:
fmt.Println("Policy configured")

case -1:
fmt.Println("Policy deleted")

case -2:
return fmt.Errorf("policy doesn't exist")

}
return nil

}
64 changes: 6 additions & 58 deletions vm/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type PolicyOptions struct {
GRPC string
}

func sendPolicyOverGRPC(o PolicyOptions, policyEventData []byte, kind string, eventType string) error {
func sendPolicyOverGRPC(o PolicyOptions, policyEventData []byte, kind string) error {
gRPC := ""

if o.GRPC != "" {
Expand Down Expand Up @@ -69,73 +69,21 @@ func sendPolicyOverGRPC(o PolicyOptions, policyEventData []byte, kind string, ev
if err != nil {
return fmt.Errorf("failed to send policy")
}
if eventType == "ADDED" {

if resp.Status == 1 {
if resp.Present == true {
fmt.Println("Policy configured successfully ")
} else {
fmt.Println("Policy applied successfully")
}
} else {
return fmt.Errorf(" Failed to apply policy")
}

}
if eventType == "DELETED" {

if resp.Status == 1 {
if resp.Applied {
fmt.Println("Policy Deleted successfully")
} else if resp.Present {
return fmt.Errorf("Failed to delete policy")
} else {
return fmt.Errorf("Policy doesn't exist")
}
} else {
return fmt.Errorf("Failed to delete policy")
}
err = responseHandler(resp)

}
return err

} else {

resp, err := client.ContainerPolicy(context.Background(), &req)
if err != nil {
return fmt.Errorf("failed to send policy")
}
if eventType == "ADDED" {

if resp.Status == 1 {
if resp.Present == true {
fmt.Println("Policy configured successfully ")
} else {
fmt.Println("Policy applied successfully")
}
} else {
return fmt.Errorf(" Failed to apply policy")
}

}
if eventType == "DELETED" {

if resp.Status == 1 {
if resp.Applied {
fmt.Println("Policy Deleted successfully")
} else if resp.Present {
return fmt.Errorf("Failed to delete policy")
} else {
return fmt.Errorf("Policy doesn't exist")
}
} else {
return fmt.Errorf("Failed to delete policy")
}

}
err = responseHandler(resp)

return err
}

return nil
}

func sendPolicyOverHTTP(address string, kind string, policyEventData []byte) error {
Expand Down Expand Up @@ -257,7 +205,7 @@ func PolicyHandling(t string, path string, o PolicyOptions, httpAddress string,
}
} else {
// Systemd mode, hence send policy over gRPC
if err = sendPolicyOverGRPC(o, policyEventData, k.Kind, t); err != nil {
if err = sendPolicyOverGRPC(o, policyEventData, k.Kind); err != nil {
return err

}
Expand Down

0 comments on commit ab27d26

Please sign in to comment.