@@ -18,6 +18,7 @@ package agent
1818
1919import (
2020 "bytes"
21+ "encoding/json"
2122 "errors"
2223 "fmt"
2324 "io"
@@ -27,13 +28,12 @@ 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"
3434 "github.com/deepflowio/deepflow/server/controller/config"
35- mysql "github.com/deepflowio/deepflow/server/controller/db/metadb"
36- mysqlmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model"
35+ metadb "github.com/deepflowio/deepflow/server/controller/db/metadb"
36+ metadbmodel "github.com/deepflowio/deepflow/server/controller/db/metadb/model"
3737 httpcommon "github.com/deepflowio/deepflow/server/controller/http/common"
3838 "github.com/deepflowio/deepflow/server/controller/http/common/response"
3939 service "github.com/deepflowio/deepflow/server/controller/http/service/agent"
@@ -91,7 +91,7 @@ func (a *AgentCMD) RegisterTo(e *gin.Engine) {
9191func forwardToServerConnectedByAgent () gin.HandlerFunc {
9292 return func (c * gin.Context ) {
9393 orgID , _ := c .Get (common .HEADER_KEY_X_ORG_ID )
94- db , err := mysql .GetDB (orgID .(int ))
94+ db , err := metadb .GetDB (orgID .(int ))
9595 if err != nil {
9696 log .Error (err , db .LogPrefixORGID )
9797 response .JSON (c , response .SetOptStatus (httpcommon .SERVER_ERROR ), response .SetError (err ))
@@ -105,7 +105,7 @@ func forwardToServerConnectedByAgent() gin.HandlerFunc {
105105 c .Abort ()
106106 return
107107 }
108- var agent * mysqlmodel .VTap
108+ var agent * metadbmodel .VTap
109109 if err = db .Where ("id = ?" , agentID ).First (& agent ).Error ; err != nil {
110110 log .Error (err , db .LogPrefixORGID )
111111 response .JSON (c , response .SetOptStatus (httpcommon .SERVER_ERROR ), response .SetError (err ))
@@ -188,7 +188,7 @@ func forwardToServerConnectedByAgent() gin.HandlerFunc {
188188func (a * AgentCMD ) getCMDAndNamespaceHandler () gin.HandlerFunc {
189189 return func (c * gin.Context ) {
190190 orgID , _ := c .Get (common .HEADER_KEY_X_ORG_ID )
191- db , err := mysql .GetDB (orgID .(int ))
191+ db , err := metadb .GetDB (orgID .(int ))
192192 if err != nil {
193193 response .JSON (c , response .SetError (err ))
194194 return
@@ -198,7 +198,7 @@ func (a *AgentCMD) getCMDAndNamespaceHandler() gin.HandlerFunc {
198198 response .JSON (c , response .SetOptStatus (httpcommon .INVALID_PARAMETERS ), response .SetError (err ))
199199 return
200200 }
201- var agent * mysqlmodel .VTap
201+ var agent * metadbmodel .VTap
202202 if err = db .Where ("id = ?" , agentID ).First (& agent ).Error ; err != nil {
203203 response .JSON (c , response .SetError (err ))
204204 return
@@ -241,14 +241,14 @@ func (a *AgentCMD) getCMDAndNamespaceHandler() gin.HandlerFunc {
241241 }
242242}
243243
244- func getAgentID (c * gin.Context , db * mysql .DB ) (int , error ) {
244+ func getAgentID (c * gin.Context , db * metadb .DB ) (int , error ) {
245245 agentIDentStr := c .Param ("id-or-name" )
246246 if agentIDentStr == "" {
247247 return 0 , errors .New ("ident can not be empty" )
248248 }
249249 agentID , err := strconv .Atoi (agentIDentStr )
250250 if err != nil {
251- var agent mysqlmodel .VTap
251+ var agent metadbmodel .VTap
252252 if err := db .Where ("name = ?" , agentIDentStr ).First (& agent ).Error ; err != nil {
253253 return 0 , fmt .Errorf ("failed to get agent by name(%s), error: %s" , err .Error ())
254254 }
@@ -259,8 +259,32 @@ func getAgentID(c *gin.Context, db *mysql.DB) (int, error) {
259259
260260func (a * AgentCMD ) cmdRunHandler () gin.HandlerFunc {
261261 return func (c * gin.Context ) {
262+ userID , ok := c .Get (common .HEADER_KEY_X_USER_ID )
263+ if ! ok {
264+ response .JSON (c , response .SetOptStatus (httpcommon .INVALID_PARAMETERS ), response .SetError (fmt .Errorf ("missing header %s" , common .HEADER_KEY_X_USER_ID )))
265+ return
266+ }
267+ orgID , ok := c .Get (common .HEADER_KEY_X_ORG_ID )
268+ if ! ok {
269+ response .JSON (c , response .SetOptStatus (httpcommon .INVALID_PARAMETERS ), response .SetError (fmt .Errorf ("missing header %s" , common .HEADER_KEY_X_ORG_ID )))
270+ return
271+ }
272+
273+ cipherKey := string (common .DerivePBKDF2Key (userID .(int ), orgID .(int )))
274+ rawPayload , err := io .ReadAll (c .Request .Body )
275+ if err != nil {
276+ response .JSON (c , response .SetOptStatus (httpcommon .INVALID_PARAMETERS ), response .SetError (err ))
277+ return
278+ }
279+
280+ decryptedPayload , err := common .AesDecrypt (string (rawPayload ), cipherKey )
281+ if err != nil {
282+ response .JSON (c , response .SetOptStatus (httpcommon .INVALID_PARAMETERS ), response .SetError (err ))
283+ return
284+ }
285+
262286 req := service.RemoteExecReq {}
263- if err := c . ShouldBindBodyWith ( & req , binding . JSON ); err != nil {
287+ if err := json . Unmarshal ([] byte ( decryptedPayload ), & req ); err != nil {
264288 response .JSON (c , response .SetOptStatus (httpcommon .INVALID_PARAMETERS ), response .SetError (err ))
265289 return
266290 }
@@ -283,8 +307,7 @@ func (a *AgentCMD) cmdRunHandler() gin.HandlerFunc {
283307 Params : req .Params ,
284308 }
285309
286- orgID , _ := c .Get (common .HEADER_KEY_X_ORG_ID )
287- db , err := mysql .GetDB (orgID .(int ))
310+ db , err := metadb .GetDB (orgID .(int ))
288311 if err != nil {
289312 response .JSON (c , response .SetError (err ))
290313 return
@@ -295,16 +318,21 @@ func (a *AgentCMD) cmdRunHandler() gin.HandlerFunc {
295318 return
296319 }
297320 content , err := service .RunAgentCMD (a .cfg .AgentCommandTimeout , orgID .(int ), agentID , & agentReq , req .CMD )
321+ encryptedContent , encryptErr := common .AesEncrypt (content , cipherKey )
322+ if encryptErr != nil {
323+ response .JSON (c , response .SetData (content ), response .SetOptStatus (httpcommon .SERVER_ERROR ), response .SetError (encryptErr ))
324+ return
325+ }
298326 if err != nil {
299- response .JSON (c , response .SetData (content ), response .SetOptStatus (httpcommon .SERVER_ERROR ), response .SetError (err ))
327+ response .JSON (c , response .SetData (encryptedContent ), response .SetOptStatus (httpcommon .SERVER_ERROR ), response .SetError (err ))
300328 return
301329 }
302330
303331 if req .OutputFormat .String () == grpcapi .OutputFormat_TEXT .String () {
304- response .JSON (c , response .SetData (content ))
332+ response .JSON (c , response .SetData (encryptedContent ))
305333 return
306334 }
307- sendAsFile (c , req .OutputFilename , bytes .NewBuffer ([]byte (content )))
335+ sendAsFile (c , req .OutputFilename , bytes .NewBuffer ([]byte (encryptedContent )))
308336 }
309337}
310338
0 commit comments