@@ -9,10 +9,7 @@ import (
9
9
"time"
10
10
11
11
"github.com/armosec/armoapi-go/apis"
12
- "github.com/armosec/armoapi-go/armotypes"
13
12
"github.com/armosec/cluster-notifier-api-go/notificationserver"
14
- opapolicy "github.com/armosec/opa-utils/reporthandling"
15
- "github.com/mitchellh/mapstructure"
16
13
"gopkg.in/mgo.v2/bson"
17
14
18
15
"github.com/golang/glog"
@@ -54,48 +51,57 @@ func NewCommands() interface{} {
54
51
55
52
func parseNotificationCommand (notification interface {}) (* apis.Commands , error ) {
56
53
cmds := & apis.Commands {}
57
- if err := mapstructure .Decode (notification , cmds ); err != nil {
58
- return nil , fmt .Errorf ("parseNotificationCommand: failed to convert notification payload to commands structure" )
59
- }
60
54
61
- return cmds , nil
55
+ var notificationBytes []byte
56
+ var err error
57
+ switch b := notification .(type ) {
58
+ case []byte :
59
+ notificationBytes = b
60
+ default :
61
+ if notificationBytes , err = json .Marshal (notification ); err != nil {
62
+ return nil , fmt .Errorf ("failed to marshal notification payload from command, reason: %s" , err .Error ())
63
+ }
64
+ }
65
+ if err = json .Unmarshal (notificationBytes , cmds ); err != nil {
66
+ return nil , fmt .Errorf ("failed to convert notification payload to commands structure, reason: %s" , err .Error ())
67
+ }
68
+ return cmds , err
62
69
}
63
-
64
70
func (notification * NotificationHandler ) handleNotification (notif * notificationserver.Notification ) error {
65
71
dst := notif .Target ["dest" ]
66
72
switch dst {
67
- case "kubescape" :
68
- // sent by this function in dash BE: KubescapeInClusterHandler
69
- policyNotificationBytes , ok := notif .Notification .([]byte )
70
- if ! ok {
71
- return fmt .Errorf ("handleNotification, kubescape, failed to get policyNotificationBytes" )
72
- }
73
- policyNotification := & opapolicy.PolicyNotification {}
74
- if err := json .Unmarshal (policyNotificationBytes , policyNotification ); err != nil {
75
- return fmt .Errorf ("handleNotification, kubescape, failed to Unmarshal: %v" , err )
76
- }
77
-
78
- sessionOnj := cautils .NewSessionObj (& apis.Command {
79
- CommandName : string (policyNotification .NotificationType ),
80
- Designators : []armotypes.PortalDesignator {policyNotification .Designators },
81
- JobTracking : apis.JobTracking {JobID : policyNotification .JobID },
82
- Args : map [string ]interface {}{
83
- "kubescapeJobParams" : policyNotification .KubescapeJobParams ,
84
- "rules" : policyNotification .Rules },
85
- }, "WebSocket" , "" , policyNotification .JobID , 1 )
86
- * notification .sessionObj <- * sessionOnj
87
-
88
- case "trigger" :
73
+ // case "kubescape":
74
+ // // sent by this function in dash BE: KubescapeInClusterHandler
75
+ // policyNotificationBytes, ok := notif.Notification.([]byte)
76
+ // if !ok {
77
+ // return fmt.Errorf("handleNotification, kubescape, failed to get policyNotificationBytes")
78
+ // }
79
+ // policyNotification := &opapolicy.PolicyNotification{}
80
+ // if err := json.Unmarshal(policyNotificationBytes, policyNotification); err != nil {
81
+ // return fmt.Errorf("handleNotification, kubescape, failed to Unmarshal: %v", err)
82
+ // }
83
+
84
+ // sessionOnj := cautils.NewSessionObj(&apis.Command{
85
+ // CommandName: string(policyNotification.NotificationType),
86
+ // Designators: []armotypes.PortalDesignator{policyNotification.Designators},
87
+ // JobTracking: apis.JobTracking{JobID: policyNotification.JobID},
88
+ // Args: map[string]interface{}{
89
+ // "kubescapeJobParams": policyNotification.KubescapeJobParams,
90
+ // "rules": policyNotification.Rules},
91
+ // }, "WebSocket", "", policyNotification.JobID, 1)
92
+ // *notification.sessionObj <- *sessionOnj
93
+
94
+ case "trigger" , "kubescape" :
89
95
cmds , err := parseNotificationCommand (notif .Notification )
90
96
if err != nil {
91
97
return err
92
98
}
93
99
for _ , cmd := range cmds .Commands {
94
- sessionObj := cautils .NewSessionObj (& cmd , "WebSocket" , "" , "" , 1 )
100
+ sessionObj := cautils .NewSessionObj (& cmd , "WebSocket" , cmd . JobTracking . ParentID , cmd . JobTracking . JobID , 1 )
95
101
* notification .sessionObj <- * sessionObj
96
102
}
97
103
98
- case "" , " safeMode" :
104
+ case "safeMode" :
99
105
safeMode , e := parseSafeModeNotification (notif .Notification )
100
106
if e != nil {
101
107
return e
0 commit comments