secgrouprules_sort.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 models
  15. import (
  16. "sort"
  17. "yunion.io/x/pkg/util/secrules"
  18. compute_models "yunion.io/x/onecloud/pkg/compute/models"
  19. )
  20. func (el *Guest) OrderedSecurityGroupRules() []*SecurityGroupRule {
  21. // deny any incoming traffic and allow ARP
  22. rs := []*SecurityGroupRule{
  23. {
  24. // deny all in-bound traffic
  25. SSecurityGroupRule: compute_models.SSecurityGroupRule{
  26. Priority: 1,
  27. Direction: string(secrules.SecurityRuleIngress),
  28. Action: string(secrules.SecurityRuleDeny),
  29. },
  30. },
  31. {
  32. // allow in-bound arp traffic
  33. SSecurityGroupRule: compute_models.SSecurityGroupRule{
  34. Priority: 2,
  35. Direction: string(secrules.SecurityRuleIngress),
  36. Protocol: "arp",
  37. Action: string(secrules.SecurityRuleAllow),
  38. },
  39. },
  40. }
  41. for _, secgroup := range el.SecurityGroups {
  42. rs = append(rs, secgroup.securityGroupRules(100)...)
  43. }
  44. if el.AdminSecurityGroup != nil {
  45. rs = append(rs, el.AdminSecurityGroup.securityGroupRules(1000)...)
  46. }
  47. sort.Slice(rs, SecurityGroupRuleLessFunc(rs))
  48. return rs
  49. }
  50. func (el *Guestnetwork) OrderedSecurityGroupRules(guest *Guest) []*SecurityGroupRule {
  51. if len(el.Guestnetworksecgroups) == 0 {
  52. return guest.OrderedSecurityGroupRules()
  53. }
  54. // deny any incoming traffic and allow ARP
  55. rs := []*SecurityGroupRule{
  56. {
  57. // deny all in-bound traffic
  58. SSecurityGroupRule: compute_models.SSecurityGroupRule{
  59. Priority: 1,
  60. Direction: string(secrules.SecurityRuleIngress),
  61. Action: string(secrules.SecurityRuleDeny),
  62. },
  63. },
  64. {
  65. // allow in-bound arp traffic
  66. SSecurityGroupRule: compute_models.SSecurityGroupRule{
  67. Priority: 2,
  68. Direction: string(secrules.SecurityRuleIngress),
  69. Protocol: "arp",
  70. Action: string(secrules.SecurityRuleAllow),
  71. },
  72. },
  73. }
  74. for _, guestSecgroup := range el.Guestnetworksecgroups {
  75. rs = append(rs, guestSecgroup.SecurityGroup.securityGroupRules(100)...)
  76. }
  77. sort.Slice(rs, SecurityGroupRuleLessFunc(rs))
  78. return rs
  79. }
  80. func (el *SecurityGroup) securityGroupRules(basePriority int64) []*SecurityGroupRule {
  81. rs := make([]*SecurityGroupRule, 0, len(el.SecurityGroupRules))
  82. for _, r := range el.SecurityGroupRules {
  83. r = r.Copy()
  84. r.Priority += int(basePriority)
  85. rs = append(rs, r)
  86. }
  87. return rs
  88. }
  89. func SecurityGroupRuleLessFunc(rs []*SecurityGroupRule) func(i, j int) bool {
  90. return func(i, j int) bool {
  91. return rs[i].Priority < rs[i].Priority
  92. }
  93. }