secgrouprule.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 aliyun
  15. import (
  16. "fmt"
  17. "strings"
  18. "time"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/pkg/util/netutils"
  21. "yunion.io/x/pkg/util/secrules"
  22. "yunion.io/x/pkg/utils"
  23. )
  24. type SPermission struct {
  25. region *SRegion
  26. CreateTime time.Time
  27. Description string
  28. DestCidrIp string
  29. Ipv6DestCidrIp string
  30. DestGroupId string
  31. DestGroupName string
  32. DestGroupOwnerAccount string
  33. Direction string
  34. IpProtocol string
  35. NicType SecurityGroupPermissionNicType
  36. Policy string
  37. PortRange string
  38. Priority int
  39. SourceCidrIp string
  40. Ipv6SourceCidrIp string
  41. SourceGroupId string
  42. SourceGroupName string
  43. SourceGroupOwnerAccount string
  44. SecurityGroupRuleId string
  45. SecurityGroupId string
  46. }
  47. func (self *SPermission) GetGlobalId() string {
  48. return self.SecurityGroupRuleId
  49. }
  50. func (self *SPermission) GetAction() secrules.TSecurityRuleAction {
  51. if self.Policy == "Drop" {
  52. return secrules.SecurityRuleDeny
  53. }
  54. return secrules.SecurityRuleAllow
  55. }
  56. func (self *SPermission) GetDescription() string {
  57. return self.Description
  58. }
  59. func (self *SPermission) GetDirection() secrules.TSecurityRuleDirection {
  60. if self.Direction == "ingress" {
  61. return secrules.DIR_IN
  62. }
  63. return secrules.DIR_OUT
  64. }
  65. func (self *SPermission) GetCIDRs() []string {
  66. ret := []string{}
  67. if len(self.SourceCidrIp) > 0 {
  68. ret = append(ret, self.SourceCidrIp)
  69. }
  70. if len(self.SourceGroupId) > 0 {
  71. ret = append(ret, self.SourceGroupId)
  72. }
  73. if len(self.DestGroupId) > 0 {
  74. ret = append(ret, self.SourceGroupId)
  75. }
  76. if len(self.DestCidrIp) > 0 {
  77. ret = append(ret, self.DestCidrIp)
  78. }
  79. if len(self.Ipv6DestCidrIp) > 0 {
  80. ret = append(ret, self.Ipv6DestCidrIp)
  81. }
  82. if len(self.Ipv6SourceCidrIp) > 0 {
  83. ret = append(ret, self.Ipv6SourceCidrIp)
  84. }
  85. return ret
  86. }
  87. func (self *SPermission) GetProtocol() string {
  88. if strings.ToLower(self.IpProtocol) == "all" {
  89. return secrules.PROTO_ANY
  90. }
  91. return strings.ToLower(self.IpProtocol)
  92. }
  93. func (self *SPermission) GetPorts() string {
  94. if self.PortRange == "-1/-1" || self.PortRange == "1/65535" || self.PortRange == "" {
  95. return ""
  96. }
  97. info := strings.Split(self.PortRange, "/")
  98. if len(info) != 2 {
  99. return ""
  100. }
  101. if info[0] == info[1] {
  102. if info[0] == "-1" {
  103. return ""
  104. }
  105. return info[0]
  106. }
  107. return fmt.Sprintf("%s-%s", info[0], info[1])
  108. }
  109. func (self *SPermission) GetPriority() int {
  110. return self.Priority
  111. }
  112. func (self *SPermission) Delete() error {
  113. return self.region.DeleteSecurityGroupRule(self.SecurityGroupId, self.GetDirection(), self.SecurityGroupRuleId)
  114. }
  115. func (self *SPermission) Update(opts *cloudprovider.SecurityGroupRuleUpdateOptions) error {
  116. return self.region.UpdateSecurityGroupRule(self.SecurityGroupId, self.SecurityGroupRuleId, self.GetDirection(), opts)
  117. }
  118. func (self *SRegion) GetSecurityGroupRules(id string) ([]SPermission, error) {
  119. params := map[string]string{
  120. "SecurityGroupId": id,
  121. "RegionId": self.RegionId,
  122. }
  123. resp, err := self.ecsRequest("DescribeSecurityGroupAttribute", params)
  124. if err != nil {
  125. return nil, err
  126. }
  127. ret := struct {
  128. Permissions struct {
  129. Permission []SPermission
  130. }
  131. SecurityGroupId string
  132. }{}
  133. err = resp.Unmarshal(&ret)
  134. if err != nil {
  135. return nil, err
  136. }
  137. for i := range ret.Permissions.Permission {
  138. ret.Permissions.Permission[i].SecurityGroupId = ret.SecurityGroupId
  139. }
  140. return ret.Permissions.Permission, nil
  141. }
  142. func (self *SRegion) DeleteSecurityGroupRule(groupId string, direction secrules.TSecurityRuleDirection, ruleId string) error {
  143. action := "RevokeSecurityGroup"
  144. if direction == secrules.DIR_OUT {
  145. action = "RevokeSecurityGroupEgress"
  146. }
  147. params := map[string]string{
  148. "RegionId": self.RegionId,
  149. "ClientToken": utils.GenRequestId(20),
  150. "SecurityGroupId": groupId,
  151. "SecurityGroupRuleId.1": ruleId,
  152. }
  153. _, err := self.ecsRequest(action, params)
  154. return err
  155. }
  156. func (self *SRegion) UpdateSecurityGroupRule(groupId, ruleId string, direction secrules.TSecurityRuleDirection, opts *cloudprovider.SecurityGroupRuleUpdateOptions) error {
  157. params := map[string]string{
  158. "ClientToken": utils.GenRequestId(20),
  159. "SecurityGroupId": groupId,
  160. "SecurityGroupRuleId": ruleId,
  161. }
  162. if len(opts.Desc) > 0 {
  163. params["Description"] = opts.Desc
  164. }
  165. switch opts.Protocol {
  166. case secrules.PROTO_TCP, secrules.PROTO_UDP:
  167. params["IpProtocol"] = opts.Protocol
  168. if len(opts.Ports) > 0 {
  169. params["PortRange"] = fmt.Sprintf("%s/%s", opts.Ports, opts.Ports)
  170. if strings.Contains(opts.Ports, "-") {
  171. params["PortRange"] = strings.ReplaceAll(opts.Ports, "-", "/")
  172. }
  173. }
  174. case secrules.PROTO_ICMP:
  175. params["IpProtocol"] = "icmp"
  176. case secrules.PROTO_ANY:
  177. params["IpProtocol"] = "all"
  178. }
  179. if opts.Priority > 0 {
  180. params["Priority"] = fmt.Sprintf("%d", opts.Priority)
  181. }
  182. switch opts.Action {
  183. case secrules.SecurityRuleAllow:
  184. params["Policy"] = "accept"
  185. case secrules.SecurityRuleDeny:
  186. params["Policy"] = "drop"
  187. }
  188. action := "ModifySecurityGroupRule"
  189. switch direction {
  190. case secrules.DIR_IN:
  191. if len(opts.CIDR) > 0 {
  192. if _, err := netutils.NewIPV6Prefix(opts.CIDR); err == nil {
  193. params["Ipv6SourceCidrIp"] = opts.CIDR
  194. } else {
  195. params["SourceCidrIp"] = opts.CIDR
  196. }
  197. }
  198. case secrules.DIR_OUT:
  199. action = "ModifySecurityGroupEgressRule"
  200. if len(opts.CIDR) > 0 {
  201. if _, err := netutils.NewIPV6Prefix(opts.CIDR); err == nil {
  202. params["Ipv6DestCidrIp"] = opts.CIDR
  203. } else {
  204. params["DestCidrIp"] = opts.CIDR
  205. }
  206. }
  207. }
  208. _, err := self.ecsRequest(action, params)
  209. return err
  210. }