secgrouprule.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 aws
  15. import (
  16. "fmt"
  17. "strings"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/pkg/util/secrules"
  20. "yunion.io/x/pkg/utils"
  21. )
  22. type SSecurityGroupRule struct {
  23. group *SSecurityGroup
  24. FromPort int `xml:"fromPort"`
  25. GroupId string `xml:"groupId"`
  26. IpProtocol string `xml:"ipProtocol"`
  27. GroupOwnerId string `xml:"groupOwnerId"`
  28. IsEgress bool `xml:"isEgress"`
  29. SecurityGroupRuleId string `xml:"securityGroupRuleId"`
  30. ReferencedGroupInfo struct {
  31. GroupId string `xml:"groupId"`
  32. UserId string `xml:"userId"`
  33. } `xml:"referencedGroupInfo"`
  34. CidrIpv4 string `xml:"cidrIpv4"`
  35. CidrIpv6 string `xml:"cidrIpv6"`
  36. Description string `xml:"description"`
  37. PrefixListId string `xml:"prefixListId"`
  38. ToPort int `xml:"toPort"`
  39. }
  40. func (self *SSecurityGroupRule) GetGlobalId() string {
  41. return self.SecurityGroupRuleId
  42. }
  43. func (self *SSecurityGroupRule) GetAction() secrules.TSecurityRuleAction {
  44. return secrules.SecurityRuleAllow
  45. }
  46. func (self *SSecurityGroupRule) GetDescription() string {
  47. return self.Description
  48. }
  49. func (self *SSecurityGroupRule) GetDirection() secrules.TSecurityRuleDirection {
  50. if self.IsEgress {
  51. return secrules.DIR_OUT
  52. }
  53. return secrules.DIR_IN
  54. }
  55. func (self *SSecurityGroupRule) GetCIDRs() []string {
  56. ret := []string{self.CidrIpv4 + self.CidrIpv6 + self.PrefixListId}
  57. return ret
  58. }
  59. func (self *SSecurityGroupRule) GetProtocol() string {
  60. if self.IpProtocol == "-1" {
  61. return secrules.PROTO_ANY
  62. }
  63. return strings.ToLower(self.IpProtocol)
  64. }
  65. func (self *SSecurityGroupRule) GetPorts() string {
  66. if self.FromPort > 0 && self.ToPort > 0 {
  67. if self.FromPort == self.ToPort {
  68. return fmt.Sprintf("%d", self.FromPort)
  69. }
  70. return fmt.Sprintf("%d-%d", self.FromPort, self.ToPort)
  71. }
  72. return ""
  73. }
  74. func (self *SSecurityGroupRule) GetPriority() int {
  75. return 0
  76. }
  77. func (self *SSecurityGroupRule) Delete() error {
  78. return self.group.region.DeleteSecurityGroupRule(self.GroupId, string(self.GetDirection()), self.SecurityGroupRuleId)
  79. }
  80. func (self *SRegion) GetSecurityGroupRules(id string) ([]SSecurityGroupRule, error) {
  81. ret := []SSecurityGroupRule{}
  82. params := map[string]string{
  83. "Filter.1.Name": "group-id",
  84. "Filter.1.Value.1": id,
  85. }
  86. for {
  87. part := struct {
  88. NextToken string `xml:"nextToken"`
  89. SecurityGroupRuleSet []SSecurityGroupRule `xml:"securityGroupRuleSet>item"`
  90. }{}
  91. err := self.ec2Request("DescribeSecurityGroupRules", params, &part)
  92. if err != nil {
  93. return nil, err
  94. }
  95. ret = append(ret, part.SecurityGroupRuleSet...)
  96. if len(part.NextToken) == 0 || len(part.SecurityGroupRuleSet) == 0 {
  97. break
  98. }
  99. params["NextToken"] = part.NextToken
  100. }
  101. return ret, nil
  102. }
  103. func (self *SSecurityGroupRule) Update(opts *cloudprovider.SecurityGroupRuleUpdateOptions) error {
  104. return self.group.region.UpdateSecurityGroupRule(self.group.GroupId, self.SecurityGroupRuleId, opts)
  105. }
  106. func (self *SRegion) UpdateSecurityGroupRule(secgroupId, ruleId string, opts *cloudprovider.SecurityGroupRuleUpdateOptions) error {
  107. if opts.Protocol == secrules.PROTO_ANY {
  108. opts.Protocol = "-1"
  109. }
  110. from, to := "-1", "-1"
  111. if len(opts.Ports) > 0 && utils.IsInStringArray(opts.Protocol, []string{secrules.PROTO_TCP, secrules.PROTO_UDP}) {
  112. r := secrules.SecurityRule{}
  113. r.ParsePorts(opts.Ports)
  114. if r.PortStart > 0 && r.PortEnd > 0 {
  115. from, to = fmt.Sprintf("%d", r.PortStart), fmt.Sprintf("%d", r.PortEnd)
  116. }
  117. }
  118. params := map[string]string{
  119. "GroupId": secgroupId,
  120. "SecurityGroupRule.1.SecurityGroupRuleId": ruleId,
  121. "SecurityGroupRule.1.SecurityGroupRule.CidrIpv4": opts.CIDR,
  122. "SecurityGroupRule.1.SecurityGroupRule.Description": opts.Desc,
  123. "SecurityGroupRule.1.SecurityGroupRule.IpProtocol": opts.Protocol,
  124. "SecurityGroupRule.1.SecurityGroupRule.FromPort": from,
  125. "SecurityGroupRule.1.SecurityGroupRule.ToPort": to,
  126. }
  127. return self.ec2Request("ModifySecurityGroupRules", params, nil)
  128. }