secgrouprules.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 google
  15. import (
  16. "fmt"
  17. "strings"
  18. "time"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/pkg/util/secrules"
  22. "yunion.io/x/pkg/util/stringutils"
  23. )
  24. type SFirewallAction struct {
  25. IPProtocol string
  26. Ports []string
  27. }
  28. type SFirewall struct {
  29. secgroup *SSecurityGroup
  30. Id string
  31. CreationTimestamp time.Time
  32. Name string
  33. Description string
  34. Network string
  35. Priority int
  36. SourceRanges []string
  37. DestinationRanges []string
  38. TargetServiceAccounts []string
  39. TargetTags []string
  40. Allowed []SFirewallAction `json:",allowempty"`
  41. Denied []SFirewallAction `json:",allowempty"`
  42. Direction string
  43. Disabled bool
  44. SelfLink string
  45. Kind string
  46. }
  47. func (self *SFirewall) GetGlobalId() string {
  48. return self.Id
  49. }
  50. func (self *SFirewall) GetAction() secrules.TSecurityRuleAction {
  51. if len(self.Allowed) > 0 {
  52. return secrules.SecurityRuleAllow
  53. }
  54. return secrules.SecurityRuleDeny
  55. }
  56. func (self *SFirewall) GetDescription() string {
  57. return self.Description
  58. }
  59. func (self *SFirewall) GetDirection() secrules.TSecurityRuleDirection {
  60. if strings.ToLower(self.Direction) == "ingress" {
  61. return secrules.DIR_IN
  62. }
  63. return secrules.DIR_OUT
  64. }
  65. func (self *SFirewall) GetCIDRs() []string {
  66. return append(self.SourceRanges, self.DestinationRanges...)
  67. }
  68. func (self *SFirewall) GetProtocol() string {
  69. ret := func() string {
  70. if len(self.Allowed)+len(self.Denied) == 1 {
  71. for _, r := range append(self.Allowed, self.Denied...) {
  72. return r.IPProtocol
  73. }
  74. }
  75. ret := []string{}
  76. for _, r := range append(self.Allowed, self.Denied...) {
  77. ret = append(ret, fmt.Sprintf("%s:%s", r.IPProtocol, strings.Join(r.Ports, ",")))
  78. }
  79. return strings.Join(ret, "|")
  80. }()
  81. if ret == "all" {
  82. return secrules.PROTO_ANY
  83. }
  84. return ret
  85. }
  86. func (self *SFirewall) GetPorts() string {
  87. if len(self.Allowed)+len(self.Denied) == 1 {
  88. for _, r := range append(self.Allowed, self.Denied...) {
  89. return strings.Join(r.Ports, ",")
  90. }
  91. }
  92. ret := []string{}
  93. for _, r := range append(self.Allowed, self.Denied...) {
  94. ret = append(ret, fmt.Sprintf("%s:%s", r.IPProtocol, strings.Join(r.Ports, ",")))
  95. }
  96. return strings.Join(ret, "|")
  97. }
  98. func (self *SFirewall) GetPriority() int {
  99. return self.Priority
  100. }
  101. func (self *SFirewall) Delete() error {
  102. return self.secgroup.gvpc.client.ecsDelete(self.SelfLink, nil)
  103. }
  104. func (self *SFirewall) Update(opts *cloudprovider.SecurityGroupRuleUpdateOptions) error {
  105. params := map[string]string{
  106. "requestId": stringutils.UUID4(),
  107. }
  108. if len(self.SourceRanges) > 0 {
  109. self.SourceRanges = []string{opts.CIDR}
  110. }
  111. if len(self.DestinationRanges) > 0 {
  112. self.DestinationRanges = []string{opts.CIDR}
  113. }
  114. self.Priority = opts.Priority
  115. if len(opts.Desc) > 0 {
  116. self.Description = opts.Desc
  117. }
  118. action := SFirewallAction{}
  119. action.IPProtocol = opts.Protocol
  120. switch opts.Protocol {
  121. case secrules.PROTO_TCP, secrules.PROTO_UDP:
  122. if len(opts.Ports) > 0 {
  123. action.Ports = []string{opts.Ports}
  124. }
  125. case secrules.PROTO_ANY:
  126. action.IPProtocol = "all"
  127. }
  128. switch opts.Action {
  129. case secrules.SecurityRuleAllow:
  130. self.Denied = []SFirewallAction{}
  131. self.Allowed = []SFirewallAction{action}
  132. case secrules.SecurityRuleDeny:
  133. self.Allowed = []SFirewallAction{}
  134. self.Denied = []SFirewallAction{action}
  135. }
  136. resource := fmt.Sprintf("projects/%s/global/firewalls/%s", self.secgroup.gvpc.client.projectId, self.Name)
  137. _, err := self.secgroup.gvpc.client.ecsPatch(resource, "", params, jsonutils.Marshal(self))
  138. return err
  139. }