@@ -18,6 +18,7 @@ package agent
1818
1919import (
2020 "bytes"
21+ "encoding/json"
2122 "errors"
2223 "fmt"
2324 "io"
@@ -27,7 +28,6 @@ import (
2728 "strconv"
2829
2930 "github.com/gin-gonic/gin"
30- "github.com/gin-gonic/gin/binding"
3131
3232 grpcapi "github.com/deepflowio/deepflow/message/agent"
3333 "github.com/deepflowio/deepflow/server/controller/common"
@@ -263,8 +263,32 @@ func getAgentID(c *gin.Context, db *metadb.DB) (int, error) {
263263
264264func (a * AgentCMD ) cmdRunHandler () gin.HandlerFunc {
265265 return func (c * gin.Context ) {
266+ userID , ok := c .Get (common .HEADER_KEY_X_USER_ID )
267+ if ! ok {
268+ response .JSON (c , response .SetOptStatus (httpcommon .INVALID_PARAMETERS ), response .SetError (fmt .Errorf ("missing header %s" , common .HEADER_KEY_X_USER_ID )))
269+ return
270+ }
271+ orgID , ok := c .Get (common .HEADER_KEY_X_ORG_ID )
272+ if ! ok {
273+ response .JSON (c , response .SetOptStatus (httpcommon .INVALID_PARAMETERS ), response .SetError (fmt .Errorf ("missing header %s" , common .HEADER_KEY_X_ORG_ID )))
274+ return
275+ }
276+
277+ cipherKey := string (common .DerivePBKDF2Key (userID .(int ), orgID .(int )))
278+ rawPayload , err := io .ReadAll (c .Request .Body )
279+ if err != nil {
280+ response .JSON (c , response .SetOptStatus (httpcommon .INVALID_PARAMETERS ), response .SetError (err ))
281+ return
282+ }
283+
284+ decryptedPayload , err := common .AesDecrypt (string (rawPayload ), cipherKey )
285+ if err != nil {
286+ response .JSON (c , response .SetOptStatus (httpcommon .INVALID_PARAMETERS ), response .SetError (err ))
287+ return
288+ }
289+
266290 req := service.RemoteExecReq {}
267- if err := c . ShouldBindBodyWith ( & req , binding . JSON ); err != nil {
291+ if err := json . Unmarshal ([] byte ( decryptedPayload ), & req ); err != nil {
268292 response .JSON (c , response .SetOptStatus (httpcommon .INVALID_PARAMETERS ), response .SetError (err ))
269293 return
270294 }
@@ -287,7 +311,6 @@ func (a *AgentCMD) cmdRunHandler() gin.HandlerFunc {
287311 Params : req .Params ,
288312 }
289313
290- orgID , _ := c .Get (common .HEADER_KEY_X_ORG_ID )
291314 db , err := metadb .GetDB (orgID .(int ))
292315 if err != nil {
293316 response .JSON (c , response .SetError (err ))
@@ -299,16 +322,21 @@ func (a *AgentCMD) cmdRunHandler() gin.HandlerFunc {
299322 return
300323 }
301324 content , err := service .RunAgentCMD (a .cfg .AgentCommandTimeout , orgID .(int ), agentID , & agentReq , req .CMD )
325+ encryptedContent , encryptErr := common .AesEncrypt (content , cipherKey )
326+ if encryptErr != nil {
327+ response .JSON (c , response .SetData (content ), response .SetOptStatus (httpcommon .SERVER_ERROR ), response .SetError (encryptErr ))
328+ return
329+ }
302330 if err != nil {
303- response .JSON (c , response .SetData (content ), response .SetOptStatus (httpcommon .SERVER_ERROR ), response .SetError (err ))
331+ response .JSON (c , response .SetData (encryptedContent ), response .SetOptStatus (httpcommon .SERVER_ERROR ), response .SetError (err ))
304332 return
305333 }
306334
307335 if req .OutputFormat .String () == grpcapi .OutputFormat_TEXT .String () {
308- response .JSON (c , response .SetData (content ))
336+ response .JSON (c , response .SetData (encryptedContent ))
309337 return
310338 }
311- sendAsFile (c , req .OutputFilename , bytes .NewBuffer ([]byte (content )))
339+ sendAsFile (c , req .OutputFilename , bytes .NewBuffer ([]byte (encryptedContent )))
312340 }
313341}
314342
0 commit comments