securitygroup.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. "strconv"
  18. "strings"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/pkg/util/netutils"
  22. "yunion.io/x/pkg/util/secrules"
  23. api "yunion.io/x/cloudmux/pkg/apis/compute"
  24. "yunion.io/x/cloudmux/pkg/cloudprovider"
  25. "yunion.io/x/cloudmux/pkg/multicloud"
  26. )
  27. type SSecurityGroup struct {
  28. multicloud.SSecurityGroup
  29. AwsTags
  30. region *SRegion
  31. GroupId string `xml:"groupId"`
  32. VpcId string `xml:"vpcId"`
  33. GroupName string `xml:"groupName"`
  34. GroupDescription string `xml:"groupDescription"`
  35. }
  36. func (self *SSecurityGroup) GetId() string {
  37. return self.GroupId
  38. }
  39. func (self *SSecurityGroup) GetVpcId() string {
  40. return self.VpcId
  41. }
  42. func (self *SSecurityGroup) GetName() string {
  43. if len(self.GroupName) > 0 {
  44. return self.GroupName
  45. }
  46. return self.GroupId
  47. }
  48. func (self *SSecurityGroup) GetGlobalId() string {
  49. return self.GroupId
  50. }
  51. func (self *SSecurityGroup) GetStatus() string {
  52. return api.SECGROUP_STATUS_READY
  53. }
  54. func (self *SSecurityGroup) Refresh() error {
  55. group, err := self.region.GetSecurityGroup(self.GroupId)
  56. if err != nil {
  57. return err
  58. }
  59. return jsonutils.Update(self, group)
  60. }
  61. func (self *SSecurityGroup) SetTags(tags map[string]string, replace bool) error {
  62. return self.region.setTags("security-group", self.GroupId, tags, replace)
  63. }
  64. func (self *SSecurityGroup) GetDescription() string {
  65. return self.GroupDescription
  66. }
  67. func (self *SSecurityGroup) GetRules() ([]cloudprovider.ISecurityGroupRule, error) {
  68. ret := []cloudprovider.ISecurityGroupRule{}
  69. rules, err := self.region.GetSecurityGroupRules(self.GroupId)
  70. if err != nil {
  71. return nil, err
  72. }
  73. for i := range rules {
  74. rules[i].group = self
  75. ret = append(ret, &rules[i])
  76. }
  77. return ret, nil
  78. }
  79. func (self *SRegion) CreateSecurityGroupRule(secGrpId string, opts *cloudprovider.SecurityGroupRuleCreateOptions) (*SSecurityGroupRule, error) {
  80. params := map[string]string{
  81. "GroupId": secGrpId,
  82. "IpPermissions.1.IpProtocol": "-1",
  83. "IpPermissions.1.FromPort": "0",
  84. "IpPermissions.1.ToPort": "65535",
  85. }
  86. if opts.Protocol != secrules.PROTO_ANY {
  87. params["IpPermissions.1.IpProtocol"] = strings.ToLower(opts.Protocol)
  88. }
  89. if len(opts.CIDR) == 0 {
  90. opts.CIDR = "0.0.0.0/0"
  91. }
  92. if _, err := netutils.NewIPV6Prefix(opts.CIDR); err == nil {
  93. params["IpPermissions.1.Ipv6Ranges.1.CidrIpv6"] = opts.CIDR
  94. params["IpPermissions.1.Ipv6Ranges.1.Description"] = opts.Desc
  95. } else {
  96. if !strings.Contains(opts.CIDR, "/") {
  97. opts.CIDR = opts.CIDR + "/32"
  98. }
  99. params["IpPermissions.1.IpRanges.1.CidrIp"] = opts.CIDR
  100. params["IpPermissions.1.IpRanges.1.Description"] = opts.Desc
  101. }
  102. start, end := 0, 0
  103. if len(opts.Ports) > 0 {
  104. if strings.Contains(opts.Ports, "-") {
  105. ports := strings.Split(opts.Ports, "-")
  106. if len(ports) != 2 {
  107. return nil, errors.Errorf("invalid ports %s", opts.Ports)
  108. }
  109. var err error
  110. _start, _end := ports[0], ports[1]
  111. start, err = strconv.Atoi(_start)
  112. if err != nil {
  113. return nil, errors.Errorf("invalid start port %s", _start)
  114. }
  115. end, err = strconv.Atoi(_end)
  116. if err != nil {
  117. return nil, errors.Errorf("invalid end port %s", _end)
  118. }
  119. } else {
  120. port, err := strconv.Atoi(opts.Ports)
  121. if err != nil {
  122. return nil, errors.Errorf("invalid ports %s", opts.Ports)
  123. }
  124. start, end = port, port
  125. }
  126. }
  127. if start > 0 && end > 0 {
  128. params["IpPermissions.1.FromPort"] = fmt.Sprintf("%d", start)
  129. params["IpPermissions.1.ToPort"] = fmt.Sprintf("%d", end)
  130. }
  131. if opts.Protocol == secrules.PROTO_ICMP {
  132. params["IpPermissions.1.FromPort"] = "-1"
  133. params["IpPermissions.1.ToPort"] = "-1"
  134. }
  135. action := "AuthorizeSecurityGroupIngress"
  136. if opts.Direction == secrules.DIR_OUT {
  137. action = "AuthorizeSecurityGroupEgress"
  138. }
  139. ret := struct {
  140. Return bool `xml:"return"`
  141. SecurityGroupRuleSet []SSecurityGroupRule `xml:"securityGroupRuleSet>item"`
  142. }{}
  143. err := self.ec2Request(action, params, &ret)
  144. if err != nil {
  145. return nil, errors.Wrapf(err, "%s", action)
  146. }
  147. for i := range ret.SecurityGroupRuleSet {
  148. return &ret.SecurityGroupRuleSet[i], nil
  149. }
  150. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "after create %s", jsonutils.Marshal(opts))
  151. }
  152. func (self *SRegion) DeleteSecurityGroupRule(secGrpId string, direction, ruleId string) error {
  153. params := map[string]string{
  154. "GroupId": secGrpId,
  155. "SecurityGroupRuleId.1": ruleId,
  156. }
  157. action := "RevokeSecurityGroupEgress"
  158. if direction == secrules.DIR_IN {
  159. action = "RevokeSecurityGroupIngress"
  160. }
  161. return self.ec2Request(action, params, nil)
  162. }
  163. func (self *SRegion) CreateSecurityGroup(opts *cloudprovider.SecurityGroupCreateInput) (string, error) {
  164. params := map[string]string{
  165. "VpcId": opts.VpcId,
  166. "GroupDescription": opts.Desc,
  167. "GroupName": opts.Name,
  168. }
  169. if len(opts.Desc) == 0 {
  170. params["GroupDescription"] = "auto create by cloudpods"
  171. }
  172. tagIdx := 1
  173. for k, v := range opts.Tags {
  174. params[fmt.Sprintf("TagSpecification.1.ResourceType")] = "security-group"
  175. params[fmt.Sprintf("TagSpecification.1.Tag.%d.Key", tagIdx)] = k
  176. params[fmt.Sprintf("TagSpecification.1.Tag.%d.Value", tagIdx)] = v
  177. tagIdx++
  178. }
  179. ret := struct {
  180. GroupId string `xml:"groupId"`
  181. }{}
  182. err := self.ec2Request("CreateSecurityGroup", params, &ret)
  183. if err != nil {
  184. return "", err
  185. }
  186. return ret.GroupId, nil
  187. }
  188. func (self *SRegion) GetSecurityGroup(id string) (*SSecurityGroup, error) {
  189. groups, err := self.GetSecurityGroups("", "", id)
  190. if err != nil {
  191. return nil, err
  192. }
  193. for i := range groups {
  194. if groups[i].GetGlobalId() == id {
  195. groups[i].region = self
  196. return &groups[i], nil
  197. }
  198. }
  199. return nil, cloudprovider.ErrNotFound
  200. }
  201. func (self *SRegion) GetSecurityGroups(vpcId string, name string, secgroupId string) ([]SSecurityGroup, error) {
  202. params := map[string]string{}
  203. idx := 1
  204. if len(vpcId) > 0 {
  205. params[fmt.Sprintf("Filter.%d.Name", idx)] = "vpc-id"
  206. params[fmt.Sprintf("Filter.%d.Value.1", idx)] = vpcId
  207. idx++
  208. }
  209. if len(name) > 0 {
  210. params[fmt.Sprintf("Filter.%d.Name", idx)] = "group-name"
  211. params[fmt.Sprintf("Filter.%d.Value.1", idx)] = name
  212. idx++
  213. }
  214. if len(secgroupId) > 0 {
  215. params["GroupId.0"] = secgroupId
  216. }
  217. result := []SSecurityGroup{}
  218. for {
  219. ret := struct {
  220. NextToken string `xml:"nextToken"`
  221. SecurityGroupInfo []SSecurityGroup `xml:"securityGroupInfo>item"`
  222. }{}
  223. err := self.ec2Request("DescribeSecurityGroups", params, &ret)
  224. if err != nil {
  225. return nil, err
  226. }
  227. result = append(result, ret.SecurityGroupInfo...)
  228. if len(ret.NextToken) == 0 || len(ret.SecurityGroupInfo) == 0 {
  229. break
  230. }
  231. params["NextToken"] = ret.NextToken
  232. }
  233. return result, nil
  234. }
  235. func (self *SSecurityGroup) Delete() error {
  236. if self.GroupName == "default" {
  237. return cloudprovider.ErrNotSupported
  238. }
  239. return self.region.DeleteSecurityGroup(self.GroupId)
  240. }
  241. func (self *SSecurityGroup) CreateRule(opts *cloudprovider.SecurityGroupRuleCreateOptions) (cloudprovider.ISecurityGroupRule, error) {
  242. rule, err := self.region.CreateSecurityGroupRule(self.GroupId, opts)
  243. if err != nil {
  244. return nil, err
  245. }
  246. rule.group = self
  247. return rule, nil
  248. }