csrfexempt.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package handler
  15. import (
  16. "context"
  17. "net/http"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/appctx"
  21. "yunion.io/x/pkg/util/sets"
  22. "yunion.io/x/onecloud/pkg/appsrv"
  23. "yunion.io/x/onecloud/pkg/httperrors"
  24. "yunion.io/x/onecloud/pkg/mcclient"
  25. "yunion.io/x/onecloud/pkg/mcclient/auth"
  26. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  27. )
  28. type CSRFResourceHandler struct {
  29. *SHandlers
  30. }
  31. func NewCSRFResourceHandler(prefix string) *CSRFResourceHandler {
  32. return &CSRFResourceHandler{NewHandlers(prefix)}
  33. }
  34. func (h *CSRFResourceHandler) Bind(app *appsrv.Application) {
  35. h.AddByMethod(GET, nil, NewHP(getHandlerCsrf, APIVer, "csrf", ResName, ResID))
  36. h.SHandlers.Bind(app)
  37. }
  38. func getAdminSession(ctx context.Context, region string, w http.ResponseWriter) *mcclient.ClientSession {
  39. adminToken := auth.AdminCredential()
  40. if adminToken == nil {
  41. httperrors.NotFoundError(ctx, w, "get admin credential is nil")
  42. return nil
  43. }
  44. regions := adminToken.GetRegions()
  45. log.Infof("CSRF regions: %v", regions)
  46. if len(regions) == 0 {
  47. httperrors.NotFoundError(ctx, w, "no usable regions, please contact admin")
  48. return nil
  49. }
  50. ret, _ := sets.InArray(region, regions)
  51. if !ret {
  52. httperrors.NotFoundError(ctx, w, "illegal region %s, please contact admin", region)
  53. }
  54. s := auth.GetAdminSession(ctx, region)
  55. return s
  56. }
  57. func fetchEnv3Csrf(ctx context.Context, w http.ResponseWriter, r *http.Request) (modulebase.Manager, modulebase.Manager, modulebase.Manager, *mcclient.ClientSession, map[string]string, jsonutils.JSONObject, jsonutils.JSONObject) {
  58. module, module2, session, params, query, body := fetchEnv2Csrf(ctx, w, r)
  59. if module == nil || module2 == nil {
  60. return nil, nil, nil, nil, nil, nil, nil
  61. }
  62. module3, e := modulebase.GetModule(session, params[ResName3])
  63. if e != nil || module == nil {
  64. httperrors.NotFoundError(ctx, w, "resource %s not found", params[ResName3])
  65. return nil, nil, nil, nil, nil, nil, nil
  66. }
  67. return module, module2, module3, session, params, query, body
  68. }
  69. func fetchEnv2Csrf(ctx context.Context, w http.ResponseWriter, r *http.Request) (modulebase.Manager, modulebase.Manager, *mcclient.ClientSession, map[string]string, jsonutils.JSONObject, jsonutils.JSONObject) {
  70. module, session, params, query, body := fetchEnvCsrf(ctx, w, r)
  71. if module == nil {
  72. return nil, nil, nil, nil, nil, nil
  73. }
  74. module2, e := modulebase.GetModule(session, params[ResName2])
  75. if e != nil || module == nil {
  76. httperrors.NotFoundError(ctx, w, "resource %s not found", params[ResName2])
  77. return nil, nil, nil, nil, nil, nil
  78. }
  79. return module, module2, session, params, query, body
  80. }
  81. func fetchEnvCsrf(ctx context.Context, w http.ResponseWriter, r *http.Request) (modulebase.Manager, *mcclient.ClientSession, map[string]string, jsonutils.JSONObject, jsonutils.JSONObject) {
  82. session, params, query, body := fetchEnvCsrf0(ctx, w, r)
  83. module, e := modulebase.GetModule(session, params[ResName])
  84. if e != nil || module == nil {
  85. httperrors.NotFoundError(ctx, w, "resource %s not found", params[ResName])
  86. return nil, nil, nil, nil, nil
  87. }
  88. return module, session, params, query, body
  89. }
  90. func fetchEnvCsrf0(ctx context.Context, w http.ResponseWriter, r *http.Request) (*mcclient.ClientSession, map[string]string, jsonutils.JSONObject, jsonutils.JSONObject) {
  91. params := appctx.AppContextParams(ctx)
  92. region := r.URL.Query().Get("region")
  93. log.Println("csrf region from url:", region)
  94. if len(region) < 1 {
  95. httperrors.NotFoundError(ctx, w, "region %s is empty", region)
  96. return nil, nil, nil, nil
  97. }
  98. log.Infof("csrf region from url: %s", region)
  99. session := getAdminSession(ctx, region, w)
  100. log.Infof("csrf got session: %s", region)
  101. if session == nil {
  102. return nil, nil, nil, nil
  103. }
  104. query, e := jsonutils.ParseQueryString(r.URL.RawQuery)
  105. if e != nil {
  106. log.Errorf("Parse query string %s: %v", r.URL.RawQuery, e)
  107. }
  108. var body jsonutils.JSONObject = nil
  109. if r.Method == PUT || r.Method == POST || r.Method == DELETE || r.Method == PATCH {
  110. body, e = appsrv.FetchJSON(r)
  111. if e != nil {
  112. log.Errorf("Fail to decode JSON request body: %v", e)
  113. }
  114. }
  115. return session, params, query, body
  116. }
  117. func getHandlerCsrf(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  118. module, session, params, query, _ := fetchEnvCsrf(ctx, w, r)
  119. if module == nil {
  120. return
  121. }
  122. obj, e := module.Get(session, params[ResID], query)
  123. if e != nil {
  124. httperrors.GeneralServerError(ctx, w, e)
  125. } else {
  126. appsrv.SendJSON(w, obj)
  127. }
  128. }