policy.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 tokens
  15. import (
  16. "context"
  17. "net/http"
  18. "time"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/pkg/util/rbacscope"
  22. api "yunion.io/x/onecloud/pkg/apis/identity"
  23. "yunion.io/x/onecloud/pkg/appsrv"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/policy"
  25. "yunion.io/x/onecloud/pkg/httperrors"
  26. "yunion.io/x/onecloud/pkg/keystone/models"
  27. "yunion.io/x/onecloud/pkg/mcclient"
  28. )
  29. func fetchTokenPolicies(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  30. token := policy.FetchUserCredential(ctx)
  31. names, group, err := models.RolePolicyManager.GetMatchPolicyGroupByCred(ctx, token, time.Now(), false)
  32. if err != nil {
  33. httperrors.GeneralServerError(ctx, w, err)
  34. return
  35. }
  36. output := mcclient.SFetchMatchPoliciesOutput{}
  37. output.Names = names
  38. output.Policies = group
  39. appsrv.SendJSON(w, output.Encode())
  40. }
  41. func postTokenPolicies(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  42. _, _, body := appsrv.FetchEnv(ctx, w, r)
  43. if body == nil {
  44. httperrors.InvalidInputError(ctx, w, "empty request body")
  45. return
  46. }
  47. input := mcclient.SCheckPoliciesInput{}
  48. err := body.Unmarshal(&input)
  49. if err != nil {
  50. httperrors.GeneralServerError(ctx, w, err)
  51. return
  52. }
  53. output, err := doCheckPolicies(ctx, input)
  54. if err != nil {
  55. httperrors.GeneralServerError(ctx, w, err)
  56. return
  57. }
  58. appsrv.SendJSON(w, output.Encode())
  59. }
  60. func doCheckPolicies(ctx context.Context, input mcclient.SCheckPoliciesInput) (*mcclient.SFetchMatchPoliciesOutput, error) {
  61. adminToken := policy.FetchUserCredential(ctx)
  62. if adminToken == nil {
  63. return nil, httperrors.NewForbiddenError("missing auth token")
  64. }
  65. if policy.PolicyManager.Allow(rbacscope.ScopeSystem, adminToken, api.SERVICE_TYPE, "tokens", "perform", "check_policies").Result.IsDeny() {
  66. return nil, httperrors.NewForbiddenError("%s not allow to check policies", adminToken.GetUserName())
  67. }
  68. log.Debugf("doCheckPolicies userId: %s projectId: %s", input.UserId, input.ProjectId)
  69. names, group, err := models.RolePolicyManager.GetMatchPolicyGroupByInput(ctx, input.UserId, input.ProjectId, time.Now(), false)
  70. if err != nil {
  71. return nil, errors.Wrap(err, "GetMatchPolicyGroupByInput")
  72. }
  73. output := mcclient.SFetchMatchPoliciesOutput{}
  74. output.Names = names
  75. output.Policies = group
  76. return &output, nil
  77. }