waf.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 qcloud
  15. import (
  16. "fmt"
  17. "time"
  18. "yunion.io/x/cloudmux/pkg/apis"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/cloudmux/pkg/multicloud"
  21. "yunion.io/x/jsonutils"
  22. "yunion.io/x/pkg/errors"
  23. )
  24. type SWafInstance struct {
  25. multicloud.SResourceBase
  26. QcloudTags
  27. region *SRegion
  28. AccessStatus string
  29. AlbType string
  30. ApiStatus int
  31. AppId string
  32. BotStatus string
  33. CCList []string
  34. CdcClusters string
  35. CloudType string
  36. ClsStatus string
  37. Cname string
  38. IsCdn *int
  39. CreateTime time.Time
  40. Domain string
  41. DomainId string
  42. Edition string
  43. Engine int
  44. FlowMode int
  45. InstanceId string
  46. InstanceName string
  47. UpstreamScheme *string
  48. HttpsUpstreamPort *int
  49. Ipv6Status int
  50. Level int
  51. Mode int
  52. IpHeaders *[]string
  53. Note string
  54. Ports []struct {
  55. NginxServerId string
  56. Port int
  57. Protocol string
  58. UpstreamPort int
  59. UpstreamProtocol string
  60. }
  61. PostCKafkaStatus int
  62. PostCLSStatus string
  63. Region string
  64. RsList []string
  65. SgDetail string
  66. SrcList []string
  67. Status int
  68. UpstreamDomainList []string
  69. SSLId *string
  70. }
  71. func (self *SWafInstance) GetName() string {
  72. return self.Domain
  73. }
  74. func (self *SWafInstance) GetGlobalId() string {
  75. return self.DomainId
  76. }
  77. func (self *SWafInstance) GetId() string {
  78. return self.Domain
  79. }
  80. func (self *SWafInstance) GetWafType() cloudprovider.TWafType {
  81. switch self.Edition {
  82. case "sparta-waf":
  83. return cloudprovider.WafTypeSaaS
  84. case "clb-waf", "cdc-clb-waf":
  85. return cloudprovider.WafTypeLoadbalancer
  86. default:
  87. return cloudprovider.TWafType(self.Edition)
  88. }
  89. }
  90. func (self *SWafInstance) GetCreatedAt() time.Time {
  91. return self.CreateTime
  92. }
  93. func (self *SWafInstance) GetDefaultAction() *cloudprovider.DefaultAction {
  94. return &cloudprovider.DefaultAction{}
  95. }
  96. func (self *SWafInstance) GetRules() ([]cloudprovider.ICloudWafRule, error) {
  97. return nil, cloudprovider.ErrNotImplemented
  98. }
  99. func (self *SWafInstance) AddRule(opts *cloudprovider.SWafRule) (cloudprovider.ICloudWafRule, error) {
  100. return nil, cloudprovider.ErrNotImplemented
  101. }
  102. func (self *SWafInstance) GetCloudResources() ([]cloudprovider.SCloudResource, error) {
  103. return nil, cloudprovider.ErrNotImplemented
  104. }
  105. func (self *SWafInstance) Refresh() error {
  106. waf, err := self.region.GetWafInstance(self.Domain, self.DomainId, self.InstanceId)
  107. if err != nil {
  108. return err
  109. }
  110. return jsonutils.Update(self, waf)
  111. }
  112. func (self *SWafInstance) GetEnabled() bool {
  113. return true
  114. }
  115. func (self *SWafInstance) GetStatus() string {
  116. return apis.STATUS_AVAILABLE
  117. }
  118. func (self *SWafInstance) GetCname() string {
  119. return self.Cname
  120. }
  121. func (self *SWafInstance) GetIsAccessProduct() bool {
  122. if self.IsCdn == nil {
  123. err := self.Refresh()
  124. if err != nil {
  125. return false
  126. }
  127. }
  128. if self.IsCdn != nil {
  129. return *self.IsCdn != 0
  130. }
  131. return false
  132. }
  133. func (self *SWafInstance) GetAccessHeaders() []string {
  134. if !self.GetIsAccessProduct() {
  135. return []string{}
  136. }
  137. switch *self.IsCdn {
  138. case 1:
  139. return []string{"X-Forwarded-For"}
  140. default:
  141. if self.IpHeaders != nil {
  142. return *self.IpHeaders
  143. }
  144. }
  145. return []string{}
  146. }
  147. func (self *SWafInstance) GetHttpPorts() []int {
  148. ret := []int{}
  149. for _, port := range self.Ports {
  150. if port.Protocol == "http" {
  151. ret = append(ret, port.Port)
  152. }
  153. }
  154. return ret
  155. }
  156. func (self *SWafInstance) GetHttpsPorts() []int {
  157. ret := []int{}
  158. for _, port := range self.Ports {
  159. if port.Protocol == "https" {
  160. ret = append(ret, port.Port)
  161. }
  162. }
  163. return ret
  164. }
  165. func (self *SWafInstance) GetCertId() string {
  166. if self.SSLId == nil {
  167. self.Refresh()
  168. }
  169. if self.SSLId != nil {
  170. return *self.SSLId
  171. }
  172. return ""
  173. }
  174. func (self *SWafInstance) GetCertName() string {
  175. sslId := self.GetCertId()
  176. if len(sslId) > 0 {
  177. cert, err := self.region.client.GetCertificate(sslId)
  178. if err != nil {
  179. return ""
  180. }
  181. return cert.GetName()
  182. }
  183. return ""
  184. }
  185. func (self *SWafInstance) GetSourceIps() []string {
  186. return append(self.SrcList, self.UpstreamDomainList...)
  187. }
  188. func (self *SWafInstance) GetCcList() []string {
  189. return self.CCList
  190. }
  191. func (self *SWafInstance) Delete() error {
  192. return cloudprovider.ErrNotImplemented
  193. }
  194. func (self *SWafInstance) GetUpstreamScheme() string {
  195. if self.UpstreamScheme == nil {
  196. self.Refresh()
  197. }
  198. if self.UpstreamScheme != nil {
  199. return *self.UpstreamScheme
  200. }
  201. return ""
  202. }
  203. func (self *SWafInstance) GetUpstreamPort() int {
  204. if self.HttpsUpstreamPort == nil {
  205. self.Refresh()
  206. }
  207. if self.HttpsUpstreamPort != nil {
  208. return *self.HttpsUpstreamPort
  209. }
  210. return 0
  211. }
  212. func (self *SRegion) GetWafInstances() ([]SWafInstance, error) {
  213. params := map[string]string{
  214. "Limit": "1000",
  215. }
  216. ret := []SWafInstance{}
  217. for {
  218. params["Offset"] = fmt.Sprintf("%d", len(ret))
  219. resp, err := self.wafRequest("DescribeDomains", params)
  220. if err != nil {
  221. return nil, err
  222. }
  223. part := struct {
  224. Domains []SWafInstance
  225. Total int
  226. }{}
  227. err = resp.Unmarshal(&part)
  228. if err != nil {
  229. return nil, err
  230. }
  231. ret = append(ret, part.Domains...)
  232. if len(ret) >= part.Total || len(part.Domains) == 0 {
  233. break
  234. }
  235. }
  236. return ret, nil
  237. }
  238. func (self *SRegion) GetICloudWafInstances() ([]cloudprovider.ICloudWafInstance, error) {
  239. wafs, err := self.GetWafInstances()
  240. if err != nil {
  241. return nil, err
  242. }
  243. ret := []cloudprovider.ICloudWafInstance{}
  244. for i := range wafs {
  245. wafs[i].region = self
  246. ret = append(ret, &wafs[i])
  247. }
  248. return ret, nil
  249. }
  250. func (self *SRegion) GetICloudWafInstanceById(id string) (cloudprovider.ICloudWafInstance, error) {
  251. wafs, err := self.GetWafInstances()
  252. if err != nil {
  253. return nil, err
  254. }
  255. for i := range wafs {
  256. wafs[i].region = self
  257. if wafs[i].GetGlobalId() == id {
  258. return &wafs[i], nil
  259. }
  260. }
  261. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", id)
  262. }
  263. func (self *SRegion) GetWafInstance(domain, domainId, instanceId string) (*SWafInstance, error) {
  264. params := map[string]string{
  265. "Domain": domain,
  266. "DomainId": domainId,
  267. "InstanceId": instanceId,
  268. }
  269. resp, err := self.wafRequest("DescribeDomainDetailsSaas", params)
  270. if err != nil {
  271. return nil, err
  272. }
  273. ret := &SWafInstance{}
  274. err = resp.Unmarshal(ret, "DomainsPartInfo")
  275. if err != nil {
  276. return nil, err
  277. }
  278. return ret, nil
  279. }