From 74956ba96c876b5c5381aac86a65867cdfe40856 Mon Sep 17 00:00:00 2001 From: Seokho Son Date: Wed, 17 Jan 2024 15:31:04 +0900 Subject: [PATCH 1/2] Add forward request to internal system --- src/api/rest/docs/docs.go | 42 +++++++++++++++++++++++++++ src/api/rest/docs/swagger.json | 42 +++++++++++++++++++++++++++ src/api/rest/docs/swagger.yaml | 31 ++++++++++++++++++++ src/api/rest/server/common/utility.go | 33 +++++++++++++++++++++ src/api/rest/server/server.go | 2 ++ src/core/common/client.go | 38 ++++++++++++++++++++++++ 6 files changed, 188 insertions(+) diff --git a/src/api/rest/docs/docs.go b/src/api/rest/docs/docs.go index ef74e3d0b..2d86a66b2 100644 --- a/src/api/rest/docs/docs.go +++ b/src/api/rest/docs/docs.go @@ -294,6 +294,48 @@ const docTemplate = `{ } } }, + "/forward/{path}": { + "post": { + "description": "Forward any (GET) request to CB-Spider", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "[Admin] System utility" + ], + "summary": "Forward any (GET) request to CB-Spider", + "parameters": [ + { + "type": "string", + "default": "vmspec", + "description": "Internal call path to CB-Spider (path without /spider/ prefix) - see [https://documenter.getpostman.com/view/24786935/2s9Ykq8Lpf#231eec23-b0ab-4966-83ce-a0ef92ead7bc] for more details", + "name": "path", + "in": "path", + "required": true + }, + { + "description": "Request body (various formats) - see [https://documenter.getpostman.com/view/24786935/2s9Ykq8Lpf#231eec23-b0ab-4966-83ce-a0ef92ead7bc] for more details", + "name": "Request", + "in": "body", + "schema": { + "type": "object" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "object", + "additionalProperties": true + } + } + } + } + }, "/health": { "get": { "description": "Check Tumblebug is alive", diff --git a/src/api/rest/docs/swagger.json b/src/api/rest/docs/swagger.json index a05c1e056..e06f50474 100644 --- a/src/api/rest/docs/swagger.json +++ b/src/api/rest/docs/swagger.json @@ -287,6 +287,48 @@ } } }, + "/forward/{path}": { + "post": { + "description": "Forward any (GET) request to CB-Spider", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "[Admin] System utility" + ], + "summary": "Forward any (GET) request to CB-Spider", + "parameters": [ + { + "type": "string", + "default": "vmspec", + "description": "Internal call path to CB-Spider (path without /spider/ prefix) - see [https://documenter.getpostman.com/view/24786935/2s9Ykq8Lpf#231eec23-b0ab-4966-83ce-a0ef92ead7bc] for more details", + "name": "path", + "in": "path", + "required": true + }, + { + "description": "Request body (various formats) - see [https://documenter.getpostman.com/view/24786935/2s9Ykq8Lpf#231eec23-b0ab-4966-83ce-a0ef92ead7bc] for more details", + "name": "Request", + "in": "body", + "schema": { + "type": "object" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "object", + "additionalProperties": true + } + } + } + } + }, "/health": { "get": { "description": "Check Tumblebug is alive", diff --git a/src/api/rest/docs/swagger.yaml b/src/api/rest/docs/swagger.yaml index 43a74aff4..2734163da 100644 --- a/src/api/rest/docs/swagger.yaml +++ b/src/api/rest/docs/swagger.yaml @@ -2715,6 +2715,37 @@ paths: summary: Get registered ConnConfig info tags: - '[Admin] Multi-Cloud environment configuration' + /forward/{path}: + post: + consumes: + - application/json + description: Forward any (GET) request to CB-Spider + parameters: + - default: vmspec + description: Internal call path to CB-Spider (path without /spider/ prefix) + - see [https://documenter.getpostman.com/view/24786935/2s9Ykq8Lpf#231eec23-b0ab-4966-83ce-a0ef92ead7bc] + for more details + in: path + name: path + required: true + type: string + - description: Request body (various formats) - see [https://documenter.getpostman.com/view/24786935/2s9Ykq8Lpf#231eec23-b0ab-4966-83ce-a0ef92ead7bc] + for more details + in: body + name: Request + schema: + type: object + produces: + - application/json + responses: + "200": + description: OK + schema: + additionalProperties: true + type: object + summary: Forward any (GET) request to CB-Spider + tags: + - '[Admin] System utility' /health: get: consumes: diff --git a/src/api/rest/server/common/utility.go b/src/api/rest/server/common/utility.go index 287df41b4..383878144 100644 --- a/src/api/rest/server/common/utility.go +++ b/src/api/rest/server/common/utility.go @@ -17,6 +17,7 @@ package common import ( "encoding/json" "fmt" + "io/ioutil" "net/http" "github.com/go-playground/validator/v10" @@ -443,3 +444,35 @@ func RestRegisterCspNativeResourcesAll(c echo.Context) error { content, err := mcis.RegisterCspNativeResourcesAll(u.NsId, u.McisName, option, mcisFlag) return common.EndRequestWithLog(c, reqID, err, content) } + +// RestForwardAnyReqToAny godoc +// @Summary Forward any (GET) request to CB-Spider +// @Description Forward any (GET) request to CB-Spider +// @Tags [Admin] System utility +// @Accept json +// @Produce json +// @Param path path string true "Internal call path to CB-Spider (path without /spider/ prefix) - see [https://documenter.getpostman.com/view/24786935/2s9Ykq8Lpf#231eec23-b0ab-4966-83ce-a0ef92ead7bc] for more details"" default(vmspec) +// @Param Request body interface{} false "Request body (various formats) - see [https://documenter.getpostman.com/view/24786935/2s9Ykq8Lpf#231eec23-b0ab-4966-83ce-a0ef92ead7bc] for more details" +// @Success 200 {object} map[string]interface{} +// @Router /forward/{path} [post] +func RestForwardAnyReqToAny(c echo.Context) error { + reqID := common.StartRequestWithLog(c) + reqPath := c.Param("*") + + fmt.Printf("reqPath: %s\n", reqPath) + + method := "GET" + var requestBody interface{} + if c.Request().Body != nil { + bodyBytes, err := ioutil.ReadAll(c.Request().Body) + if err != nil { + return common.EndRequestWithLog(c, reqID, fmt.Errorf("Failed to read request body: %v", err), nil) + } + requestBody = bodyBytes + } else { + requestBody = common.NoBody + } + + content, err := common.ForwardRequestToAny(reqPath, method, requestBody) + return common.EndRequestWithLog(c, reqID, err, content) +} diff --git a/src/api/rest/server/server.go b/src/api/rest/server/server.go index 33d56c1c4..71d7ee0ce 100644 --- a/src/api/rest/server/server.go +++ b/src/api/rest/server/server.go @@ -190,6 +190,8 @@ func RunServer(port string) { e.GET("/tumblebug/ns/:nsId/loadDefaultResource", rest_mcir.RestLoadDefaultResource) e.DELETE("/tumblebug/ns/:nsId/defaultResources", rest_mcir.RestDelAllDefaultResources) + e.POST("/tumblebug/forward/*", rest_common.RestForwardAnyReqToAny) + // Route for NameSpace subgroup g := e.Group("/tumblebug/ns", common.NsValidation()) diff --git a/src/core/common/client.go b/src/core/common/client.go index 62743425a..519e1532b 100644 --- a/src/core/common/client.go +++ b/src/core/common/client.go @@ -243,3 +243,41 @@ func EndRequestWithLog(c echo.Context, reqID string, err error, responseData int return c.JSON(http.StatusNotFound, map[string]string{"message": "Invalid Request ID"}) } + +// ForwardRequestToAny forwards the given request to the specified path +func ForwardRequestToAny(reqPath string, method string, requestBody interface{}) (interface{}, error) { + client := resty.New() + var callResult interface{} + + url := SpiderRestUrl + "/" + reqPath + + var requestBodyBytes []byte + var ok bool + if requestBodyBytes, ok = requestBody.([]byte); !ok { + return nil, fmt.Errorf("requestBody is not []byte type") + } + + var requestBodyMap map[string]interface{} + err := json.Unmarshal(requestBodyBytes, &requestBodyMap) + if err != nil { + return nil, fmt.Errorf("JSON unmarshal error: %v", err) + } + + err = ExecuteHttpRequest( + client, + method, + url, + nil, + SetUseBody(requestBodyMap), + &requestBodyMap, + &callResult, + MediumDuration, + ) + + if err != nil { + CBLog.Error(err) + return nil, err + } + + return callResult, nil +} From dfee4ae57987c1ab046cf5cd6b30d880ff27d377 Mon Sep 17 00:00:00 2001 From: Seokho Son Date: Wed, 17 Jan 2024 15:42:22 +0900 Subject: [PATCH 2/2] Fix forward path coding issue --- src/api/rest/server/common/utility.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/api/rest/server/common/utility.go b/src/api/rest/server/common/utility.go index 383878144..36cb40b3b 100644 --- a/src/api/rest/server/common/utility.go +++ b/src/api/rest/server/common/utility.go @@ -19,6 +19,7 @@ import ( "fmt" "io/ioutil" "net/http" + "net/url" "github.com/go-playground/validator/v10" "github.com/labstack/echo/v4" @@ -458,6 +459,10 @@ func RestRegisterCspNativeResourcesAll(c echo.Context) error { func RestForwardAnyReqToAny(c echo.Context) error { reqID := common.StartRequestWithLog(c) reqPath := c.Param("*") + reqPath, err := url.PathUnescape(reqPath) + if err != nil { + return common.EndRequestWithLog(c, reqID, err, nil) + } fmt.Printf("reqPath: %s\n", reqPath)