reduce_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. "reflect"
  17. "sort"
  18. "testing"
  19. )
  20. func TestReduce(t *testing.T) {
  21. actions := []string{"create", "delete", "get", "list", "perform", "update"}
  22. compute := []string{"disks", "snapshotpolicies", "snapshots"}
  23. image := []string{"images"}
  24. caseIn := make([]SRbacRule, 0, len(actions)*(len(compute)+len(image)))
  25. generate := func(serviceName string, resources []string, denyFunc func(resource, action string) bool) {
  26. for _, resource := range resources {
  27. for _, action := range actions {
  28. if denyFunc(resource, action) {
  29. caseIn = append(caseIn, SRbacRule{
  30. Service: serviceName,
  31. Resource: resource,
  32. Action: action,
  33. Result: Deny,
  34. })
  35. continue
  36. }
  37. caseIn = append(caseIn, SRbacRule{
  38. Service: serviceName,
  39. Resource: resource,
  40. Action: action,
  41. Result: Allow,
  42. })
  43. }
  44. }
  45. }
  46. generate("compute", compute, func(resource, action string) bool {
  47. if resource == "snapshotpolicies" {
  48. return true
  49. }
  50. if resource == "snapshots" && action == "list" {
  51. return true
  52. }
  53. return false
  54. })
  55. generate("image", image, func(resource, actions string) bool {
  56. return true
  57. })
  58. cases := map[string]struct {
  59. in []SRbacRule
  60. want []SRbacRule
  61. }{
  62. "merge1": {
  63. in: []SRbacRule{
  64. {
  65. Service: "compute",
  66. Resource: "servers",
  67. Action: "list",
  68. Result: Allow,
  69. },
  70. {
  71. Service: "compute",
  72. Resource: "servers",
  73. Action: "get",
  74. Result: Allow,
  75. },
  76. {
  77. Service: "compute",
  78. Resource: "servers",
  79. Action: "delete",
  80. Result: Allow,
  81. },
  82. {
  83. Service: "compute",
  84. Resource: "servers",
  85. Action: "create",
  86. Result: Allow,
  87. },
  88. {
  89. Service: "compute",
  90. Resource: "servers",
  91. Action: "update",
  92. Result: Allow,
  93. },
  94. {
  95. Service: "compute",
  96. Resource: "servers",
  97. Action: "perform",
  98. Result: Allow,
  99. },
  100. },
  101. want: []SRbacRule{
  102. {
  103. Service: "compute",
  104. Resource: "servers",
  105. Result: Allow,
  106. },
  107. },
  108. },
  109. "merge2": {
  110. in: caseIn,
  111. want: []SRbacRule{
  112. {
  113. Service: "compute",
  114. Resource: "disks",
  115. Action: "",
  116. Result: Allow,
  117. },
  118. {
  119. Service: "compute",
  120. Resource: "snapshotpolicies",
  121. Action: "",
  122. Result: Deny,
  123. },
  124. {
  125. Service: "compute",
  126. Resource: "snapshots",
  127. Action: "*",
  128. Result: Allow,
  129. },
  130. {
  131. Service: "compute",
  132. Resource: "snapshots",
  133. Action: "list",
  134. Result: Deny,
  135. },
  136. {
  137. Service: "image",
  138. Resource: "images",
  139. Action: "",
  140. Result: Deny,
  141. },
  142. },
  143. },
  144. }
  145. for name, c := range cases {
  146. got := reduceRules(c.in)
  147. for i := range c.want {
  148. c.want[i].Extra = []string{}
  149. }
  150. sort.Slice(got, genLess(got))
  151. sort.Slice(c.want, genLess(c.want))
  152. if !reflect.DeepEqual(got, c.want) {
  153. t.Errorf("[%s]: want: %s got: %s", name, c.want, got)
  154. }
  155. }
  156. }
  157. func genLess(rules []SRbacRule) func(i, j int) bool {
  158. return func(i, j int) bool {
  159. if rules[i].Service == rules[j].Service {
  160. return rules[i].Resource < rules[j].Resource
  161. }
  162. return rules[i].Service < rules[j].Service
  163. }
  164. }