policycore.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 rbacutils
  15. import (
  16. "yunion.io/x/jsonutils"
  17. "yunion.io/x/pkg/errors"
  18. "yunion.io/x/onecloud/pkg/httperrors"
  19. )
  20. type TPolicy []SRbacRule
  21. func (policy TPolicy) getMatchRule(req []string) *SRbacRule {
  22. service := WILD_MATCH
  23. if len(req) > levelService {
  24. service = req[levelService]
  25. }
  26. resource := WILD_MATCH
  27. if len(req) > levelResource {
  28. resource = req[levelResource]
  29. }
  30. action := WILD_MATCH
  31. if len(req) > levelAction {
  32. action = req[levelAction]
  33. }
  34. var extra []string
  35. if len(req) > levelExtra {
  36. extra = req[levelExtra:]
  37. } else {
  38. extra = make([]string, 0)
  39. }
  40. return policy.GetMatchRule(service, resource, action, extra...)
  41. }
  42. func (policy TPolicy) GetMatchRule(service string, resource string, action string, extra ...string) *SRbacRule {
  43. return GetMatchRule(policy, service, resource, action, extra...)
  44. }
  45. func decodePolicy(policyJson jsonutils.JSONObject) (TPolicy, error) {
  46. rules, err := json2Rules(policyJson)
  47. if err != nil {
  48. return nil, errors.Wrap(err, "json2Rules")
  49. }
  50. if len(rules) == 0 {
  51. return nil, ErrEmptyPolicy
  52. }
  53. return rules, nil
  54. }
  55. func DecodeRawPolicyData(input jsonutils.JSONObject) (TPolicy, error) {
  56. policyData, err := input.Get("policy")
  57. if err != nil || policyData == nil {
  58. return nil, errors.Wrap(httperrors.ErrInvalidFormat, "invalid policy data")
  59. }
  60. return decodePolicy(policyData)
  61. }
  62. func (policy TPolicy) encode() jsonutils.JSONObject {
  63. return rules2Json(policy)
  64. }
  65. func (policy TPolicy) EncodeRawData() jsonutils.JSONObject {
  66. ret := jsonutils.NewDict()
  67. ret.Add(policy.encode(), "policy")
  68. return ret
  69. }
  70. func (policy TPolicy) Explain(request [][]string) [][]string {
  71. output := make([][]string, len(request))
  72. for i := 0; i < len(request); i += 1 {
  73. rule := policy.getMatchRule(request[i])
  74. if rule == nil {
  75. output[i] = append(request[i], string(Deny))
  76. } else {
  77. output[i] = append(request[i], string(rule.Result))
  78. }
  79. }
  80. return output
  81. }
  82. // Contains of TPolicy
  83. //
  84. // TPolicy p1 contains p2 means any action allowed by p2 is also allowed by p1
  85. // and any action denied by p1 is also denied by p2
  86. func (policy1 TPolicy) Contains(policy2 TPolicy) bool {
  87. for _, rule := range policy2 {
  88. if rule.Result == Allow {
  89. rule := policy1.GetMatchRule(rule.Service, rule.Resource, rule.Action, rule.Extra...)
  90. if rule == nil {
  91. return false
  92. }
  93. if rule.Result == Deny {
  94. return false
  95. }
  96. }
  97. }
  98. for _, rule := range policy1 {
  99. if rule.Result == Deny {
  100. rule := policy2.GetMatchRule(rule.Service, rule.Resource, rule.Action, rule.Extra...)
  101. if rule != nil && rule.Result == Allow {
  102. return false
  103. }
  104. }
  105. }
  106. return true
  107. }