secgrouprules_acl_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. // Copyright 2019 Yunion
  15. //
  16. // Licensed under the Apache License, Version 2.0 (the "License");
  17. // you may not use this file except in compliance with the License.
  18. // You may obtain a copy of the License at
  19. //
  20. // http://www.apache.org/licenses/LICENSE-2.0
  21. //
  22. // Unless required by applicable law or agreed to in writing, software
  23. // distributed under the License is distributed on an "AS IS" BASIS,
  24. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. // See the License for the specific language governing permissions and
  26. // limitations under the License
  27. package ovn
  28. import (
  29. "fmt"
  30. "reflect"
  31. "testing"
  32. "yunion.io/x/jsonutils"
  33. "yunion.io/x/ovsdb/schema/ovn_nb"
  34. "yunion.io/x/pkg/util/secrules"
  35. "yunion.io/x/onecloud/pkg/compute/models"
  36. agentmodels "yunion.io/x/onecloud/pkg/vpcagent/models"
  37. )
  38. func TestRuleToACL(t *testing.T) {
  39. lport := "local-port120"
  40. cases := []struct {
  41. rule *agentmodels.SecurityGroupRule
  42. ipv6 bool
  43. acl *ovn_nb.ACL
  44. }{
  45. {
  46. // egress deny 100.10.10.0/24
  47. rule: &agentmodels.SecurityGroupRule{
  48. SSecurityGroupRule: models.SSecurityGroupRule{
  49. Direction: string(secrules.SecurityRuleEgress),
  50. CIDR: "100.10.10.0/24",
  51. Action: string(secrules.SecurityRuleDeny),
  52. Protocol: secrules.PROTO_ANY,
  53. Priority: 100,
  54. },
  55. },
  56. acl: &ovn_nb.ACL{
  57. Direction: aclDirFromLport,
  58. Action: "drop",
  59. Match: fmt.Sprintf("inport == %q && ip4 && ip4.dst == 100.10.10.0/24", lport),
  60. Priority: 100,
  61. },
  62. },
  63. {
  64. // egress allow any
  65. rule: &agentmodels.SecurityGroupRule{
  66. SSecurityGroupRule: models.SSecurityGroupRule{
  67. Direction: string(secrules.SecurityRuleEgress),
  68. CIDR: "",
  69. Action: string(secrules.SecurityRuleAllow),
  70. Protocol: secrules.PROTO_ANY,
  71. Priority: 10,
  72. },
  73. },
  74. acl: &ovn_nb.ACL{
  75. Direction: aclDirFromLport,
  76. Action: "allow-related",
  77. Match: fmt.Sprintf("inport == %q && ip4", lport),
  78. Priority: 10,
  79. },
  80. },
  81. {
  82. // egress allow any
  83. rule: &agentmodels.SecurityGroupRule{
  84. SSecurityGroupRule: models.SSecurityGroupRule{
  85. Direction: string(secrules.SecurityRuleEgress),
  86. CIDR: "",
  87. Action: string(secrules.SecurityRuleAllow),
  88. Protocol: secrules.PROTO_ANY,
  89. Priority: 10,
  90. },
  91. },
  92. ipv6: true,
  93. acl: &ovn_nb.ACL{
  94. Direction: aclDirFromLport,
  95. Action: "allow-related",
  96. Match: fmt.Sprintf("inport == %q && (ip4 || ip6)", lport),
  97. Priority: 10,
  98. },
  99. },
  100. {
  101. // ingress deny all
  102. rule: &agentmodels.SecurityGroupRule{
  103. SSecurityGroupRule: models.SSecurityGroupRule{
  104. Direction: string(secrules.SecurityRuleIngress),
  105. CIDR: "",
  106. Action: string(secrules.SecurityRuleDeny),
  107. Protocol: secrules.PROTO_ANY,
  108. Priority: 100,
  109. },
  110. },
  111. acl: &ovn_nb.ACL{
  112. Direction: aclDirToLport,
  113. Action: "drop",
  114. Match: fmt.Sprintf("outport == %q && ip4", lport),
  115. Priority: 100,
  116. },
  117. },
  118. {
  119. // ingress allow ssh
  120. rule: &agentmodels.SecurityGroupRule{
  121. SSecurityGroupRule: models.SSecurityGroupRule{
  122. Direction: string(secrules.SecurityRuleIngress),
  123. CIDR: "",
  124. Action: string(secrules.SecurityRuleAllow),
  125. Protocol: secrules.PROTO_TCP,
  126. Ports: "22",
  127. Priority: 100,
  128. },
  129. },
  130. acl: &ovn_nb.ACL{
  131. Direction: aclDirToLport,
  132. Action: "allow-related",
  133. Match: fmt.Sprintf("outport == %q && ip4 && tcp && tcp.dst == 22", lport),
  134. Priority: 100,
  135. },
  136. },
  137. {
  138. // ingress allow ssh
  139. rule: &agentmodels.SecurityGroupRule{
  140. SSecurityGroupRule: models.SSecurityGroupRule{
  141. Direction: string(secrules.SecurityRuleIngress),
  142. CIDR: "",
  143. Action: string(secrules.SecurityRuleAllow),
  144. Protocol: secrules.PROTO_TCP,
  145. Ports: "22",
  146. Priority: 100,
  147. },
  148. },
  149. ipv6: true,
  150. acl: &ovn_nb.ACL{
  151. Direction: aclDirToLport,
  152. Action: "allow-related",
  153. Match: fmt.Sprintf("outport == %q && (ip4 || ip6) && tcp && tcp.dst == 22", lport),
  154. Priority: 100,
  155. },
  156. },
  157. }
  158. for _, c := range cases {
  159. got, err := ruleToAcl(lport, c.rule, c.ipv6)
  160. if err != nil {
  161. t.Errorf("ruleToACL fail %s", err)
  162. } else {
  163. if !reflect.DeepEqual(got, c.acl) {
  164. t.Errorf("want: %s got: %s", jsonutils.Marshal(c.acl), jsonutils.Marshal(got))
  165. }
  166. }
  167. }
  168. }