secgroup.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 compute
  15. import (
  16. "fmt"
  17. "yunion.io/x/pkg/errors"
  18. "yunion.io/x/pkg/util/regutils"
  19. "yunion.io/x/pkg/util/secrules"
  20. "yunion.io/x/onecloud/pkg/apis"
  21. )
  22. type SSecgroupRuleResource struct {
  23. // 优先级, 数字越大优先级越高
  24. // minimum: 1
  25. // maximum: 100
  26. // required: true
  27. Priority *int `json:"priority"`
  28. // 协议
  29. // required: true
  30. //
  31. //
  32. //
  33. // | protocol | name |
  34. // | -------- | ---- |
  35. // | any | 所有协议|
  36. // | tcp | TCP |
  37. // | icmp | ICMP |
  38. // | udp | UDP |
  39. // enum: ["any", "tcp", "udp", "icmp"]
  40. Protocol string `json:"protocol"`
  41. // 端口列表, 参数为空代表任意端口
  42. // 此参数仅对protocol是tcp, udp时生效
  43. // 支持格式:
  44. // | 格式类型 | 举例 |
  45. // | -------- | ---- |
  46. // | 单端口 | 22 |
  47. // | 端口范围 | 100-200 |
  48. // | 不连续端口| 80,443 |
  49. // requried: false
  50. Ports string `json:"ports"`
  51. // swagger:ignore
  52. PortStart int `json:"port_start"`
  53. // swagger:ignore
  54. PortEnd int `json:"port_end"`
  55. // 方向
  56. // enum: ["in", "out"]
  57. // required: true
  58. Direction string `json:"direction"`
  59. // ip或cidr地址, 若指定peer_secgroup_id此参数不生效
  60. // example: 192.168.222.121
  61. CIDR string `json:"cidr"`
  62. // 行为
  63. // deny: 拒绝
  64. // allow: 允许
  65. // enum: ["deny", "allow"]
  66. // required: true
  67. Action string `json:"action"`
  68. // 规则描述信息
  69. // requried: false
  70. // example: test to create rule
  71. Description string `json:"description"`
  72. }
  73. type SSecgroupRuleResourceSet []SSecgroupRuleResource
  74. type SSecgroupRuleCreateInput struct {
  75. apis.ResourceBaseCreateInput
  76. SSecgroupRuleResource
  77. // swagger:ignore
  78. Secgroup string `json:"secgroup" yunion-deprecated-by:"secgroup_id"`
  79. // swagger:ignore
  80. Status string `json:"status"`
  81. // 安全组ID
  82. // required: true
  83. SecgroupId string `json:"secgroup_id"`
  84. }
  85. type SSecgroupRuleUpdateInput struct {
  86. apis.ResourceBaseUpdateInput
  87. Priority *int `json:"priority"`
  88. Ports *string `json:"ports"`
  89. // ip或cidr地址, 若指定peer_secgroup_id此参数不生效
  90. // example: 192.168.222.121
  91. CIDR *string `json:"cidr"`
  92. // 协议
  93. // required: true
  94. //
  95. //
  96. //
  97. // | protocol | name |
  98. // | -------- | ---- |
  99. // | any | 所有协议|
  100. // | tcp | TCP |
  101. // | icmp | ICMP |
  102. // | udp | UDP |
  103. // enum: ["any", "tcp", "udp", "icmp"]
  104. Protocol *string `json:"protocol"`
  105. // 行为
  106. // deny: 拒绝
  107. // allow: 允许
  108. // enum: ["deny", "allow"]
  109. // required: true
  110. Action *string `json:"action"`
  111. // 规则描述信息
  112. // requried: false
  113. // example: test to create rule
  114. Description string `json:"description"`
  115. }
  116. func (input *SSecgroupRuleResource) Check() error {
  117. priority := 1
  118. if input.Priority != nil {
  119. priority = *input.Priority
  120. }
  121. rule := secrules.SecurityRule{
  122. Priority: priority,
  123. Direction: secrules.TSecurityRuleDirection(input.Direction),
  124. Action: secrules.TSecurityRuleAction(input.Action),
  125. Protocol: input.Protocol,
  126. PortStart: input.PortStart,
  127. PortEnd: input.PortEnd,
  128. Ports: []int{},
  129. }
  130. if len(input.Ports) > 0 {
  131. err := rule.ParsePorts(input.Ports)
  132. if err != nil {
  133. return errors.Wrapf(err, "ParsePorts(%s)", input.Ports)
  134. }
  135. }
  136. if len(input.CIDR) > 0 {
  137. if !regutils.MatchCIDR(input.CIDR) && !regutils.MatchIP4Addr(input.CIDR) && !regutils.MatchCIDR6(input.CIDR) && !regutils.MatchIP6Addr(input.CIDR) {
  138. return fmt.Errorf("invalid ip address: %s", input.CIDR)
  139. }
  140. } else {
  141. // empty CIDR means both IPv4 and IPv6
  142. // input.CIDR = "0.0.0.0/0"
  143. }
  144. return rule.ValidateRule()
  145. }
  146. type SSecgroupCreateInput struct {
  147. apis.SharableVirtualResourceCreateInput
  148. // vpc id
  149. // defualt: default
  150. VpcResourceInput
  151. // swagger:ignore
  152. CloudproviderResourceInput
  153. // swagger:ignore
  154. CloudregionResourceInput
  155. // swagger:ignore
  156. GlobalvpcId string `json:"globalvpc_id"`
  157. // 规则列表
  158. // required: false
  159. Rules []SSecgroupRuleCreateInput `json:"rules"`
  160. }
  161. type SecgroupListInput struct {
  162. apis.SharableVirtualResourceListInput
  163. apis.ExternalizedResourceBaseListInput
  164. ServerResourceInput
  165. DBInstanceResourceInput
  166. ELasticcacheResourceInput
  167. // 按缓存关联主机数排序
  168. // pattern:asc|desc
  169. OrderByGuestCnt string `json:"order_by_guest_cnt"`
  170. // 模糊过滤规则中含有指定ip的安全组
  171. // example: 10.10.2.1
  172. Ip string `json:"ip"`
  173. // 精确匹配规则中含有指定端口的安全组
  174. // example: 100-200
  175. Ports string `json:"ports"`
  176. // 指定过滤规则的方向(仅在指定ip或ports时生效) choices: all|in|out
  177. // default: all
  178. // example: in
  179. Direction string `json:"direction"`
  180. VpcId string `json:"vpc_id"`
  181. LoadbalancerId string `json:"loadbalancer_id"`
  182. RegionalFilterListInput
  183. ManagedResourceListInput
  184. }
  185. type SecurityGroupRuleListInput struct {
  186. apis.ResourceBaseListInput
  187. apis.ExternalizedResourceBaseListInput
  188. SecgroupFilterListInput
  189. Projects []string `json:"projects"`
  190. // 以direction字段过滤安全组规则
  191. Direction string `json:"direction"`
  192. // 以action字段过滤安全组规则
  193. Action string `json:"action"`
  194. // 以protocol字段过滤安全组规则
  195. Protocol string `json:"protocol"`
  196. // 以ports字段过滤安全组规则
  197. Ports string `json:"ports"`
  198. // 根据ip模糊匹配安全组规则
  199. Ip string `json:"ip"`
  200. }
  201. type SecgroupResourceInput struct {
  202. // 过滤关联指定安全组(ID或Name)的列表结果
  203. SecgroupId string `json:"secgroup_id"`
  204. // swagger:ignore
  205. // Deprecated
  206. // filter by secgroup_id
  207. Secgroup string `json:"secgroup" yunion-deprecated-by:"secgroup_id"`
  208. // 模糊匹配安全组规则名称
  209. SecgroupName string `json:"secgroup_name"`
  210. }
  211. type SecgroupFilterListInput struct {
  212. SecgroupResourceInput
  213. RegionalFilterListInput
  214. ManagedResourceListInput
  215. // 以安全组排序
  216. OrderBySecgroup string `json:"order_by_secgroup"`
  217. }
  218. type SecgroupDetails struct {
  219. apis.SharableVirtualResourceDetails
  220. SSecurityGroup
  221. VpcResourceInfo
  222. GlobalVpcResourceInfo
  223. // 关联云主机数量, 不包含回收站云主机
  224. GuestCnt int `json:"guest_cnt,allowempty"`
  225. // 关联云主机网卡数量, 不包含回收站云主机
  226. GuestNicCnt int `json:"guest_nic_cnt,allowempty"`
  227. // 关联此安全组的云主机is_system为true数量, , 不包含回收站云主机
  228. SystemGuestCnt int `json:"system_guest_cnt,allowempty"`
  229. // admin_secgrp_id为此安全组的云主机数量, , 不包含回收站云主机
  230. AdminGuestCnt int `json:"admin_guest_cnt,allowempty"`
  231. // 关联LB数量
  232. LoadbalancerCnt int `json:"loadbalancer_cnt,allowempty"`
  233. // 关联RDS数量
  234. RdsCnt int `json:"rds_cnt,allowempty"`
  235. // 关联Redis数量
  236. RedisCnt int `json:"redis_cnt,allowempty"`
  237. // 所有关联的资源数量
  238. TotalCnt int `json:"total_cnt,allowempty"`
  239. }
  240. type SecurityGroupResourceInfo struct {
  241. // 安全组名称
  242. Secgroup string `json:"secgroup"`
  243. // VPC归属区域ID
  244. CloudregionId string `json:"cloudregion_id"`
  245. CloudregionResourceInfo
  246. // VPC归属云订阅ID
  247. ManagerId string `json:"manager_id"`
  248. ManagedResourceInfo
  249. }
  250. type GuestsecgroupListInput struct {
  251. GuestJointsListInput
  252. SecgroupFilterListInput
  253. }
  254. type GuestnetworksecgroupListInput struct {
  255. apis.ResourceBaseListInput
  256. ServerFilterListInput
  257. SecgroupFilterListInput
  258. NetworkIndex *int `json:"network_index"`
  259. IsAdmin bool `json:"is_admin"`
  260. }
  261. type ElasticcachesecgroupListInput struct {
  262. ElasticcacheJointsListInput
  263. SecgroupFilterListInput
  264. }
  265. type GuestsecgroupDetails struct {
  266. GuestJointResourceDetails
  267. SGuestsecgroup
  268. // 安全组名称
  269. Secgroup string `json:"secgroup"`
  270. }
  271. type GuestnetworksecgroupDetails struct {
  272. GuestResourceInfo
  273. SGuestsecgroup
  274. SecurityGroupResourceInfo
  275. ProjectId string `json:"tenant_id"`
  276. apis.ProjectizedResourceInfo
  277. // 安全组状态
  278. SecgroupStatus string `json:"secgroup_status"`
  279. // VPC ID
  280. VpcId string `json:"vpc_id"`
  281. Vpc string `json:"vpc"`
  282. NetworkIndex int `json:"network_index"`
  283. GuestNetwork string `json:"guest_network"`
  284. MacAddr string `json:"mac_addr"`
  285. Ifname string `json:"ifname"`
  286. IpAddr string `json:"ip_addr"`
  287. Ip6Addr string `json:"ip_6_addr"`
  288. NetworkId string `json:"network_id"`
  289. NetworkName string `json:"network_name"`
  290. Admin bool `json:"admin"`
  291. }
  292. type ElasticcachesecgroupDetails struct {
  293. ElasticcacheJointResourceDetails
  294. SElasticcachesecgroup
  295. // 安全组名称
  296. Secgroup string `json:"secgroup"`
  297. }
  298. type SecurityGroupCloneInput struct {
  299. Name string `json:"name"`
  300. Description string `json:"description"`
  301. }
  302. type SecgroupImportRulesInput struct {
  303. Rules []SSecgroupRuleCreateInput `json:"rules"`
  304. }
  305. type SecgroupJsonDesc struct {
  306. Id string `json:"id"`
  307. Name string `json:"name"`
  308. }
  309. type SSecurityGroupRef struct {
  310. GuestCnt int `json:"guest_cnt"`
  311. AdminGuestCnt int `json:"admin_guest_cnt"`
  312. RdsCnt int `json:"rds_cnt"`
  313. RedisCnt int `json:"redis_cnt"`
  314. LoadbalancerCnt int `json:"loadbalancer_cnt"`
  315. GuestNicCnt int `json:"guest_nic_cnt"`
  316. TotalCnt int `json:"total_cnt"`
  317. }
  318. func (self *SSecurityGroupRef) Sum() {
  319. self.TotalCnt = self.GuestCnt + self.AdminGuestCnt + self.RdsCnt + self.RedisCnt + self.LoadbalancerCnt + self.GuestNicCnt
  320. }
  321. type SecurityGroupSyncstatusInput struct {
  322. }